UNPKG

react-native-xenon

Version:

A powerful in-app debugging tool for React Native.

66 lines (65 loc) 3.92 kB
"use strict"; var _class, _class2; function _applyDecoratedDescriptor(i, e, r, n, l) { var a = {}; return Object.keys(n).forEach(function (i) { a[i] = n[i]; }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function (r, n) { return n(i, e, r) || r; }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a; } import { frozen, getHttpInterceptorId, singleton } from "../core/utils.js"; import { NetworkType } from "../types/index.js"; import HttpInterceptor from "./HttpInterceptor.js"; const originalXHROpen = XMLHttpRequest.prototype.open; const originalXHRSend = XMLHttpRequest.prototype.send; const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; let XHRInterceptor = singleton(_class = (_class2 = class XHRInterceptor extends HttpInterceptor { enableInterception() { if (this.isInterceptorEnabled) return; const { openCallback, requestHeaderCallback, sendCallback, headerReceivedCallback, responseCallback } = this.getCallbacks(); const isInterceptorEnabled = () => this.isInterceptorEnabled; XMLHttpRequest.prototype.open = function (method, url, ...args) { this._interceptionId = getHttpInterceptorId(); openCallback?.(this._interceptionId, NetworkType.XHR, method, url); originalXHROpen.call(this, method, url, ...args); }; XMLHttpRequest.prototype.setRequestHeader = function (header, value) { requestHeaderCallback?.(this._interceptionId, header, value); originalXHRSetRequestHeader.call(this, header, value); }; XMLHttpRequest.prototype.send = function (data) { sendCallback?.(this._interceptionId, Date.now(), data); this.addEventListener?.('readystatechange', () => { if (!isInterceptorEnabled()) return; if (this.readyState === this.HEADERS_RECEIVED) { const contentTypeString = this.getResponseHeader('Content-Type'); const contentLengthString = this.getResponseHeader('Content-Length'); const responseContentType = contentTypeString ? contentTypeString.split(';')[0] : undefined; const responseSize = contentLengthString ? parseInt(contentLengthString, 10) : undefined; const responseHeadersArray = this.getAllResponseHeaders().split('\n'); const responseHeaders = responseHeadersArray.reduce((acc, header) => { const [key, value] = header.split(': '); if (key && value) acc.set(key, value); return acc; }, new Map()); headerReceivedCallback?.(this._interceptionId, responseContentType, responseSize, responseHeaders); } if (this.readyState === this.DONE) { responseCallback?.(this._interceptionId, this.status, this.timeout, Date.now(), this.response, this.responseURL, this.responseType); } }); originalXHRSend.call(this, data); }; this.isInterceptorEnabled = true; } disableInterception() { if (!this.isInterceptorEnabled) return; this.isInterceptorEnabled = false; XMLHttpRequest.prototype.send = originalXHRSend; XMLHttpRequest.prototype.open = originalXHROpen; XMLHttpRequest.prototype.setRequestHeader = originalXHRSetRequestHeader; this.clearCallbacks(); } }, _applyDecoratedDescriptor(_class2.prototype, "enableInterception", [frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "enableInterception"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "disableInterception", [frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "disableInterception"), _class2.prototype), _class2)) || _class; export { XHRInterceptor as default }; //# sourceMappingURL=XHRInterceptor.js.map