Repository navigation
Expand file tree
/
Copy pathnetworkUtils.ts
More file actions
99 lines (84 loc) · 3.2 KB
/
Copy pathnetworkUtils.ts
File metadata and controls
99 lines (84 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { debug } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';
import type { NetworkMetaWarning } from './types';
// Symbol used by e.g. the Replay integration to store original body on Request objects
export const ORIGINAL_REQ_BODY = Symbol.for('sentry__originalRequestBody');
/**
* Serializes FormData.
*
* This is a bit simplified, but gives us a decent estimate.
* This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'.
*
*/
export function serializeFormData(formData: FormData): string {
// @ts-expect-error passing FormData to URLSearchParams actually works
return new URLSearchParams(formData).toString();
}
/** Get the string representation of a body. */
export function getBodyString(body: unknown, _debug: typeof debug = debug): [string | undefined, NetworkMetaWarning?] {
try {
if (typeof body === 'string') {
return [body];
}
if (body instanceof URLSearchParams) {
return [body.toString()];
}
if (body instanceof FormData) {
return [serializeFormData(body)];
}
if (!body) {
return [undefined];
}
} catch (error) {
DEBUG_BUILD && _debug.error(error, 'Failed to serialize body', body);
return [undefined, 'BODY_PARSE_ERROR'];
}
DEBUG_BUILD && _debug.log('Skipping network body because of body type', body);
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
}
/**
* Parses the fetch arguments to extract the request payload.
*
* In case of a Request object, this function attempts to retrieve the original body by looking for a Sentry-patched symbol.
*/
export function getFetchRequestArgBody(fetchArgs: unknown[] = []): RequestInit['body'] | undefined {
// Second argument with body options takes precedence
if (fetchArgs.length >= 2 && fetchArgs[1] && typeof fetchArgs[1] === 'object' && 'body' in fetchArgs[1]) {
return (fetchArgs[1] as RequestInit).body;
}
if (fetchArgs.length >= 1 && fetchArgs[0] instanceof Request) {
const request = fetchArgs[0];
/* The Request interface's body is a ReadableStream, which we cannot directly access.
Some integrations (e.g. Replay) patch the Request object to store the original body. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const originalBody = (request as any)[ORIGINAL_REQ_BODY];
if (originalBody !== undefined) {
return originalBody;
}
return undefined; // Fall back to returning undefined (as we don't want to return a ReadableStream)
}
return undefined;
}
/**
* Parses XMLHttpRequest response headers into a Record.
* Extracted from replay internals to be reusable.
*/
export function parseXhrResponseHeaders(xhr: XMLHttpRequest): Record<string, string> {
let headers: string | undefined;
try {
headers = xhr.getAllResponseHeaders();
} catch (error) {
DEBUG_BUILD && debug.error(error, 'Failed to get xhr response headers', xhr);
return {};
}
if (!headers) {
return {};
}
return headers.split('\r\n').reduce((acc: Record<string, string>, line: string) => {
const [key, value] = line.split(': ') as [string, string | undefined];
if (value) {
acc[key.toLowerCase()] = value;
}
return acc;
}, {});
}