import { addHandler, maybeInstrument, timestampInSeconds, isString, triggerHandlers } from '@sentry/core'; import { WINDOW } from '../types.js'; const SENTRY_XHR_DATA_KEY = "__sentry_xhr_v3__"; function addXhrInstrumentationHandler(handler) { const type = "xhr"; addHandler(type, handler); maybeInstrument(type, instrumentXHR); } function instrumentXHR() { if (!WINDOW.XMLHttpRequest) { return; } const xhrproto = XMLHttpRequest.prototype; xhrproto.open = new Proxy(xhrproto.open, { apply(originalOpen, xhrOpenThisArg, xhrOpenArgArray) { const virtualError = new Error(); const startTimestamp = timestampInSeconds() * 1e3; const method = 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: timestampInSeconds() * 1e3, startTimestamp, xhr: xhrOpenThisArg, virtualError }; 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 && isString(header) && 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: timestampInSeconds() * 1e3, xhr: sendThisArg }; triggerHandlers("xhr", handlerData); return originalSend.apply(sendThisArg, sendArgArray); } }); } function parseXhrUrlArg(url) { if (isString(url)) { return url; } try { return url.toString(); } catch { } return void 0; } export { SENTRY_XHR_DATA_KEY, addXhrInstrumentationHandler, instrumentXHR }; //# sourceMappingURL=xhr.js.map