Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const types = require('../types.js'); const SENTRY_XHR_DATA_KEY = "__sentry_xhr_v3__"; function addXhrInstrumentationHandler(handler) { const type = "xhr"; core.addHandler(type, handler); core.maybeInstrument(type, instrumentXHR); } function instrumentXHR() { if (!types.WINDOW.XMLHttpRequest) { return; } const xhrproto = XMLHttpRequest.prototype; xhrproto.open = new Proxy(xhrproto.open, { apply(originalOpen, xhrOpenThisArg, xhrOpenArgArray) { const virtualError = new Error(); const startTimestamp = core.timestampInSeconds() * 1e3; const method = core.isString(xhrOpenArgArray[0]) ? xhrOpenArgArray[0].toUpperCase() : void 0; const url = parseXhrUrlArg(xhrOpenArgArray[1]); if (!method || !url) { return originalOpen.apply(xhrOpenThisArg, xhrOpenArgArray); } xhrOpenThisArg[SENTRY_XHR_DATA_KEY] = { method, url, request_headers: {} }; if (method === "POST" && url.match(/sentry_key/)) { xhrOpenThisArg.__sentry_own_request__ = true; } const onreadystatechangeHandler = () => { const xhrInfo = xhrOpenThisArg[SENTRY_XHR_DATA_KEY]; if (!xhrInfo) { return; } if (xhrOpenThisArg.readyState === 4) { try { xhrInfo.status_code = xhrOpenThisArg.status; } catch { } const handlerData = { endTimestamp: core.timestampInSeconds() * 1e3, startTimestamp, xhr: xhrOpenThisArg, virtualError }; core.triggerHandlers("xhr", handlerData); } }; if ("onreadystatechange" in xhrOpenThisArg && typeof xhrOpenThisArg.onreadystatechange === "function") { xhrOpenThisArg.onreadystatechange = new Proxy(xhrOpenThisArg.onreadystatechange, { apply(originalOnreadystatechange, onreadystatechangeThisArg, onreadystatechangeArgArray) { onreadystatechangeHandler(); return originalOnreadystatechange.apply(onreadystatechangeThisArg, onreadystatechangeArgArray); } }); } else { xhrOpenThisArg.addEventListener("readystatechange", onreadystatechangeHandler); } xhrOpenThisArg.setRequestHeader = new Proxy(xhrOpenThisArg.setRequestHeader, { apply(originalSetRequestHeader, setRequestHeaderThisArg, setRequestHeaderArgArray) { const [header, value] = setRequestHeaderArgArray; const xhrInfo = setRequestHeaderThisArg[SENTRY_XHR_DATA_KEY]; if (xhrInfo && core.isString(header) && core.isString(value)) { xhrInfo.request_headers[header.toLowerCase()] = value; } return originalSetRequestHeader.apply(setRequestHeaderThisArg, setRequestHeaderArgArray); } }); return originalOpen.apply(xhrOpenThisArg, xhrOpenArgArray); } }); xhrproto.send = new Proxy(xhrproto.send, { apply(originalSend, sendThisArg, sendArgArray) { const sentryXhrData = sendThisArg[SENTRY_XHR_DATA_KEY]; if (!sentryXhrData) { return originalSend.apply(sendThisArg, sendArgArray); } if (sendArgArray[0] !== void 0) { sentryXhrData.body = sendArgArray[0]; } const handlerData = { startTimestamp: core.timestampInSeconds() * 1e3, xhr: sendThisArg }; core.triggerHandlers("xhr", handlerData); return originalSend.apply(sendThisArg, sendArgArray); } }); } function parseXhrUrlArg(url) { if (core.isString(url)) { return url; } try { return url.toString(); } catch { } return void 0; } exports.SENTRY_XHR_DATA_KEY = SENTRY_XHR_DATA_KEY; exports.addXhrInstrumentationHandler = addXhrInstrumentationHandler; exports.instrumentXHR = instrumentXHR; //# sourceMappingURL=xhr.js.map