UNPKG

react-native-xenon

Version:

A powerful in-app debugging tool for React Native.

67 lines (66 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _utils = require("../core/utils"); var _types = require("../types"); var _HttpInterceptor = _interopRequireDefault(require("./HttpInterceptor")); var _class, _class2; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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; } const originalXHROpen = XMLHttpRequest.prototype.open; const originalXHRSend = XMLHttpRequest.prototype.send; const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; let XHRInterceptor = exports.default = (0, _utils.singleton)(_class = (_class2 = class XHRInterceptor extends _HttpInterceptor.default { 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 = (0, _utils.getHttpInterceptorId)(); openCallback?.(this._interceptionId, _types.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, data); const startTime = Date.now(); 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; headerReceivedCallback?.(this._interceptionId, responseContentType, responseSize, this.getAllResponseHeaders()); } if (this.readyState === this.DONE) { const endTime = Date.now(); const duration = endTime - startTime; responseCallback?.(this._interceptionId, this.status, this.timeout, duration, 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", [_utils.frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "enableInterception"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "disableInterception", [_utils.frozen], Object.getOwnPropertyDescriptor(_class2.prototype, "disableInterception"), _class2.prototype), _class2)) || _class; //# sourceMappingURL=XHRInterceptor.js.map