Skip to content

Commit cf6d23a

Browse files
authored
ref(utils): Simplify collection of xhr request payload (getsentry#4402)
This simplifies the implementation of XHR request body harvesting for breadcrumbs, introduced in getsentry#2904. Instead of caching the data and grabbing it at a later point, this grabs it as soon as it's available, in the xhr `send` method.
1 parent 2cf6f94 commit cf6d23a

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

packages/utils/src/instrument.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,6 @@ function instrumentXHR(): void {
219219
return;
220220
}
221221

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

227224
fill(xhrproto, 'open', function(originalOpen: () => void): () => void {
@@ -251,20 +248,6 @@ function instrumentXHR(): void {
251248
/* do nothing */
252249
}
253250

254-
try {
255-
const requestPos = requestKeys.indexOf(xhr);
256-
if (requestPos !== -1) {
257-
// Make sure to pop both key and value to keep it in sync.
258-
requestKeys.splice(requestPos);
259-
const args = requestValues.splice(requestPos)[0];
260-
if (args[0] !== undefined) {
261-
xhrInfo.body = args[0] as XHRSendInput;
262-
}
263-
}
264-
} catch (e) {
265-
/* do nothing */
266-
}
267-
268251
triggerHandlers('xhr', {
269252
args,
270253
endTimestamp: Date.now(),
@@ -291,8 +274,9 @@ function instrumentXHR(): void {
291274

292275
fill(xhrproto, 'send', function(originalSend: () => void): () => void {
293276
return function(this: SentryWrappedXMLHttpRequest, ...args: any[]): void {
294-
requestKeys.push(this);
295-
requestValues.push(args);
277+
if (this.__sentry_xhr__ && args[0] !== undefined) {
278+
this.__sentry_xhr__.body = args[0];
279+
}
296280

297281
triggerHandlers('xhr', {
298282
args,

0 commit comments

Comments
 (0)