Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 1 | import {waitFor} from 'poll-until-promise'; |
| 2 | |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 3 | import {correctArrayKeys} from '../common/protojs'; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 4 | import ResponseModifier from '../xhrInterceptor/responseModifiers/index.js'; |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 5 | import * as utils from '../xhrInterceptor/utils.js'; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 6 | |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 7 | const kSpecialEvents = ['load', 'loadend']; |
| 8 | const kErrorEvents = ['error', 'timeout', 'abort']; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 9 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 10 | const kStandardMethods = [ |
| 11 | 'open', 'abort', 'setRequestHeader', 'send', 'getResponseHeader', |
| 12 | 'getAllResponseHeaders', 'dispatchEvent', 'overrideMimeType' |
| 13 | ]; |
| 14 | const kStandardScalars = [ |
| 15 | 'onabort', 'onerror', 'onload', 'onloadstart', 'onloadend', 'onprogress', |
| 16 | 'onreadystatechange', 'readyState', 'responseType', 'responseXML', 'status', |
| 17 | 'statusText', 'upload', 'withCredentials', 'DONE', 'UNSENT', |
| 18 | 'HEADERS_RECEIVED', 'LOADING', 'OPENED' |
| 19 | ]; |
| 20 | |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 21 | const kCheckInterceptionOptions = { |
| 22 | interval: 50, |
| 23 | timeout: 100 * 1000, |
| 24 | }; |
avm99963 | 1f50f6f | 2021-08-12 23:04:41 +0200 | [diff] [blame] | 25 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 26 | /** |
| 27 | * Implements the flatten options concept of the DOM spec. |
| 28 | * |
| 29 | * @see {@link https://dom.spec.whatwg.org/#concept-flatten-options} |
| 30 | */ |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 31 | function flattenOptions(options) { |
| 32 | if (typeof options === 'boolean') return options; |
| 33 | if (options) return options['capture']; |
| 34 | return undefined; |
| 35 | } |
| 36 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 37 | /** |
| 38 | * Class which, when instantiated, overrides window.XMLHttpRequest to proxy the |
| 39 | * requests through our internal interceptors to read/modify requests/responses. |
| 40 | * |
| 41 | * Slightly based in https://stackoverflow.com/a/24561614. |
| 42 | */ |
Adrià Vilanova Martínez | 4757ae8 | 2022-12-28 16:41:37 +0100 | [diff] [blame] | 43 | export default class XHRProxy { |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 44 | constructor() { |
| 45 | this.originalXMLHttpRequest = window.XMLHttpRequest; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 46 | |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 47 | this.messageID = 0; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 48 | this.responseModifier = new ResponseModifier(); |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 49 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 50 | this.#overrideXHRObject(); |
| 51 | } |
| 52 | |
| 53 | #overrideXHRObject() { |
| 54 | this.#overrideConstructor(); |
| 55 | this.#addProxyEventsMethods(); |
| 56 | this.#overrideMethods(); |
| 57 | this.#addMethodInterceptors(); |
| 58 | this.#overrideScalars(); |
| 59 | this.#setOriginalResponseScalar(); |
| 60 | } |
| 61 | |
| 62 | #overrideConstructor() { |
| 63 | const XHRProxyInstance = this; |
| 64 | |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 65 | window.XMLHttpRequest = function() { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 66 | this.xhr = new XHRProxyInstance.originalXMLHttpRequest(); |
| 67 | this.$TWPTID = XHRProxyInstance.messageID++; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 68 | this.$responseModified = false; |
| 69 | this.$responseIntercepted = false; |
| 70 | this.specialHandlers = { |
| 71 | load: new Set(), |
| 72 | loadend: new Set(), |
| 73 | }; |
| 74 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 75 | this.$proxyEvents(); |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | #addProxyEventsMethods() { |
| 80 | window.XMLHttpRequest.prototype.$proxyEvents = function() { |
| 81 | this.$proxySpecialEvents(); |
| 82 | this.$proxyErrorEvents(); |
| 83 | }; |
| 84 | this.#addProxySpecialEvents(); |
| 85 | this.#addProxyErrorEvents(); |
| 86 | } |
| 87 | |
| 88 | #overrideMethods() { |
| 89 | this.#overrideStandardMethods(); |
| 90 | this.#overrideEventsMethods(); |
| 91 | } |
| 92 | |
| 93 | #addMethodInterceptors() { |
| 94 | this.#addOpenInterceptor(); |
| 95 | this.#addSetRequestHeaderInterceptor(); |
| 96 | this.#addSendInterceptor(); |
| 97 | } |
| 98 | |
| 99 | #overrideScalars() { |
| 100 | this.#overrideStandardScalars(); |
| 101 | this.#overrideResponseScalars(); |
| 102 | } |
| 103 | |
| 104 | #overrideStandardMethods() { |
| 105 | kStandardMethods.forEach(method => { |
| 106 | this.#overrideStandardMethod(method); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | #overrideEventsMethods() { |
| 111 | this.#overrideAddEventListener(); |
| 112 | this.#overrideRemoveEventListener(); |
| 113 | } |
| 114 | |
| 115 | #overrideStandardScalars() { |
| 116 | kStandardScalars.forEach(scalar => { |
| 117 | this.#overrideStandardScalar(scalar); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | #overrideResponseScalars() { |
| 122 | this.#overrideResponse(); |
| 123 | this.#overrideResponseText(); |
| 124 | } |
| 125 | |
| 126 | #addProxySpecialEvents() { |
| 127 | const XHRProxyInstance = this; |
| 128 | window.XMLHttpRequest.prototype.$proxySpecialEvents = function() { |
| 129 | const proxyInstance = this; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 130 | kSpecialEvents.forEach(eventName => { |
| 131 | this.xhr.addEventListener(eventName, function() { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 132 | let interceptedPromise; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 133 | if (eventName === 'load') { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 134 | interceptedPromise = |
| 135 | XHRProxyInstance.responseModifier.intercept(proxyInstance) |
| 136 | .then(() => { |
| 137 | proxyInstance.$responseIntercepted = true; |
| 138 | }); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 139 | } else { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 140 | interceptedPromise = waitFor(() => { |
| 141 | if (proxyInstance.$responseIntercepted) return Promise.resolve(); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 142 | return Promise.reject(); |
| 143 | }, kCheckInterceptionOptions); |
| 144 | } |
| 145 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 146 | interceptedPromise.then(() => { |
| 147 | for (const e of proxyInstance.specialHandlers[eventName]) { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 148 | e[1](arguments); |
| 149 | } |
| 150 | }); |
| 151 | }); |
| 152 | }); |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 153 | }; |
| 154 | } |
| 155 | |
| 156 | #addProxyErrorEvents() { |
| 157 | window.XMLHttpRequest.prototype.$proxyErrorEvents = function() { |
| 158 | const proxyInstance = this; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 159 | kErrorEvents.forEach(eventName => { |
| 160 | this.xhr.addEventListener(eventName, function() { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 161 | proxyInstance.$responseIntercepted = true; |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 162 | }); |
| 163 | }); |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 166 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 167 | #overrideStandardMethod(method) { |
| 168 | window.XMLHttpRequest.prototype[method] = function() { |
| 169 | if (method == 'open') |
| 170 | this.$interceptOpen(...arguments); |
| 171 | else if (method == 'setRequestHeader') |
| 172 | this.$interceptSetRequestHeader(...arguments); |
| 173 | else if (method == 'send') |
| 174 | this.$interceptSend(...arguments); |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 175 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 176 | return this.xhr[method].apply(this.xhr, arguments); |
| 177 | } |
| 178 | } |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 179 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 180 | #overrideStandardScalar(scalar) { |
| 181 | Object.defineProperty(window.XMLHttpRequest.prototype, scalar, { |
| 182 | get: function() { |
| 183 | return this.xhr[scalar]; |
| 184 | }, |
| 185 | set: function(val) { |
| 186 | this.xhr[scalar] = val; |
| 187 | }, |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 188 | }); |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 189 | } |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 190 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 191 | #overrideAddEventListener() { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 192 | window.XMLHttpRequest.prototype.addEventListener = function() { |
| 193 | if (!kSpecialEvents.includes(arguments[0])) |
| 194 | return this.xhr.addEventListener.apply(this.xhr, arguments); |
| 195 | |
| 196 | this.specialHandlers[arguments[0]].add(arguments); |
| 197 | }; |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 198 | } |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 199 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 200 | #overrideRemoveEventListener() { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 201 | window.XMLHttpRequest.prototype.removeEventListener = function( |
| 202 | type, callback, options) { |
| 203 | if (!kSpecialEvents.includes(type)) |
| 204 | return this.xhr.removeEventListener.apply(this.xhr, arguments); |
| 205 | |
| 206 | const flattenedOptions = flattenOptions(options); |
| 207 | for (const e of this.specialHandlers[type]) { |
| 208 | if (callback === e[1] && flattenOptions(e[2]) === flattenedOptions) { |
| 209 | return this.specialHandlers[type].delete(e); |
| 210 | } |
| 211 | } |
| 212 | }; |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 213 | } |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 214 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 215 | #addOpenInterceptor() { |
| 216 | window.XMLHttpRequest.prototype.$interceptOpen = function() { |
| 217 | const proxyInstance = this; |
| 218 | |
| 219 | this.$TWPTRequestURL = arguments[1] || location.href; |
| 220 | |
| 221 | var interceptors = |
| 222 | utils.matchInterceptors('response', this.$TWPTRequestURL); |
| 223 | if (interceptors.length > 0) { |
Adrià Vilanova Martínez | 88e5ef2 | 2023-11-15 22:45:13 +0100 | [diff] [blame] | 224 | this.addEventListener('load', function() { |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 225 | var body = utils.getResponseJSON({ |
Adrià Vilanova Martínez | 88e5ef2 | 2023-11-15 22:45:13 +0100 | [diff] [blame] | 226 | responseType: proxyInstance.responseType, |
| 227 | response: proxyInstance.response, |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 228 | $TWPTRequestURL: proxyInstance.$TWPTRequestURL, |
| 229 | $isArrayProto: proxyInstance.$isArrayProto, |
| 230 | }); |
| 231 | if (body !== undefined) |
| 232 | interceptors.forEach(i => { |
| 233 | utils.triggerEvent(i.eventName, body, proxyInstance.$TWPTID); |
| 234 | }); |
| 235 | }); |
| 236 | } |
| 237 | }; |
| 238 | } |
| 239 | |
| 240 | #addSetRequestHeaderInterceptor() { |
| 241 | window.XMLHttpRequest.prototype.$interceptSetRequestHeader = function() { |
| 242 | let header = arguments[0]; |
| 243 | let value = arguments[1]; |
| 244 | if ('Content-Type'.localeCompare( |
| 245 | header, undefined, {sensitivity: 'accent'}) == 0) |
| 246 | this.$isArrayProto = (value == 'application/json+protobuf'); |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | #addSendInterceptor() { |
| 251 | window.XMLHttpRequest.prototype.$interceptSend = function() { |
| 252 | var interceptors = utils.matchInterceptors( |
| 253 | 'request', this.$TWPTRequestURL || location.href); |
| 254 | if (interceptors.length > 0) { |
| 255 | let rawBody = arguments[0]; |
| 256 | let body; |
| 257 | if (typeof (rawBody) === 'object' && |
| 258 | (rawBody instanceof Object.getPrototypeOf(Uint8Array))) { |
| 259 | let dec = new TextDecoder('utf-8'); |
| 260 | body = dec.decode(rawBody); |
| 261 | } else if (typeof (rawBody) === 'string') { |
| 262 | body = rawBody; |
| 263 | } else { |
| 264 | console.error( |
| 265 | 'Unexpected type of request body (' + typeof (rawBody) + ').', |
| 266 | this.$TWPTRequestURL); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | let JSONBody = JSON.parse(body); |
| 271 | if (this.$isArrayProto) JSONBody = correctArrayKeys(JSONBody); |
| 272 | |
| 273 | interceptors.forEach(i => { |
| 274 | utils.triggerEvent(i.eventName, JSONBody, this.$TWPTID); |
| 275 | }); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #setOriginalResponseScalar() { |
| 281 | Object.defineProperty(window.XMLHttpRequest.prototype, 'originalResponse', { |
| 282 | get: function() { |
| 283 | return this.xhr.response; |
| 284 | }, |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 285 | }); |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 286 | } |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 287 | |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 288 | #overrideResponse() { |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 289 | Object.defineProperty(window.XMLHttpRequest.prototype, 'response', { |
| 290 | get: function() { |
| 291 | if (!this.$responseIntercepted) return undefined; |
| 292 | if (this.$responseModified) return this.$newResponse; |
| 293 | return this.xhr.response; |
| 294 | }, |
| 295 | }); |
Adrià Vilanova Martínez | 810383b | 2023-11-15 22:07:57 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | #overrideResponseText() { |
Adrià Vilanova Martínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 299 | Object.defineProperty(window.XMLHttpRequest.prototype, 'responseText', { |
| 300 | get: function() { |
| 301 | if (!this.$responseIntercepted) return undefined; |
| 302 | if (this.$responseModified) return this.$newResponseText; |
| 303 | return this.xhr.responseText; |
| 304 | }, |
| 305 | }); |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 306 | } |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 307 | } |