Skip to content

ref(utils): Simplify collection of xhr request payload #4402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ function instrumentXHR(): void {
return;
}

// Poor man's implementation of ES6 `Map`, tracking and keeping in sync key and value separately.
const requestKeys: XMLHttpRequest[] = [];
const requestValues: Array<any>[] = [];
const xhrproto = XMLHttpRequest.prototype;

fill(xhrproto, 'open', function(originalOpen: () => void): () => void {
Expand Down Expand Up @@ -251,20 +248,6 @@ function instrumentXHR(): void {
/* do nothing */
}

try {
const requestPos = requestKeys.indexOf(xhr);
if (requestPos !== -1) {
// Make sure to pop both key and value to keep it in sync.
requestKeys.splice(requestPos);
const args = requestValues.splice(requestPos)[0];
if (args[0] !== undefined) {
xhrInfo.body = args[0] as XHRSendInput;
}
}
} catch (e) {
/* do nothing */
}

triggerHandlers('xhr', {
args,
endTimestamp: Date.now(),
Expand All @@ -291,8 +274,9 @@ function instrumentXHR(): void {

fill(xhrproto, 'send', function(originalSend: () => void): () => void {
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
requestKeys.push(this);
requestValues.push(args);
if (this.__sentry_xhr__ && args[0] !== undefined) {
this.__sentry_xhr__.body = args[0];
}

triggerHandlers('xhr', {
args,
Expand Down