-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathxhrUtils.ts
214 lines (184 loc) · 6.16 KB
/
xhrUtils.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { SENTRY_XHR_DATA_KEY, getBodyString } from '@sentry-internal/browser-utils';
import type { NetworkMetaWarning, XhrHint } from '@sentry-internal/browser-utils';
import type { Breadcrumb, XhrBreadcrumbData } from '@sentry/core';
import { DEBUG_BUILD } from '../../debug-build';
import type { ReplayContainer, ReplayNetworkOptions, ReplayNetworkRequestData } from '../../types';
import { logger } from '../../util/logger';
import { addNetworkBreadcrumb } from './addNetworkBreadcrumb';
import {
buildNetworkRequestOrResponse,
buildSkippedNetworkRequestOrResponse,
getAllowedHeaders,
getBodySize,
makeNetworkReplayBreadcrumb,
mergeWarning,
parseContentLengthHeader,
urlMatches,
} from './networkUtils';
/**
* Capture an XHR breadcrumb to a replay.
* This adds additional data (where appropriate).
*/
export async function captureXhrBreadcrumbToReplay(
breadcrumb: Breadcrumb & { data: XhrBreadcrumbData },
hint: Partial<XhrHint>,
options: ReplayNetworkOptions & { replay: ReplayContainer },
): Promise<void> {
try {
const data = _prepareXhrData(breadcrumb, hint, options);
// Create a replay performance entry from this breadcrumb
const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
addNetworkBreadcrumb(options.replay, result);
} catch (error) {
DEBUG_BUILD && logger.exception(error, 'Failed to capture xhr breadcrumb');
}
}
/**
* Enrich a breadcrumb with additional data.
* This has to be sync & mutate the given breadcrumb,
* as the breadcrumb is afterwards consumed by other handlers.
*/
export function enrichXhrBreadcrumb(
breadcrumb: Breadcrumb & { data: XhrBreadcrumbData },
hint: Partial<XhrHint>,
): void {
const { xhr, input } = hint;
if (!xhr) {
return;
}
const reqSize = getBodySize(input);
const resSize = xhr.getResponseHeader('content-length')
? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
: _getBodySize(xhr.response, xhr.responseType);
if (reqSize !== undefined) {
breadcrumb.data.request_body_size = reqSize;
}
if (resSize !== undefined) {
breadcrumb.data.response_body_size = resSize;
}
}
function _prepareXhrData(
breadcrumb: Breadcrumb & { data: XhrBreadcrumbData },
hint: Partial<XhrHint>,
options: ReplayNetworkOptions,
): ReplayNetworkRequestData | null {
const now = Date.now();
const { startTimestamp = now, endTimestamp = now, input, xhr } = hint;
const {
url,
method,
status_code: statusCode = 0,
request_body_size: requestBodySize,
response_body_size: responseBodySize,
} = breadcrumb.data;
if (!url) {
return null;
}
if (!xhr || !urlMatches(url, options.networkDetailAllowUrls) || urlMatches(url, options.networkDetailDenyUrls)) {
const request = buildSkippedNetworkRequestOrResponse(requestBodySize);
const response = buildSkippedNetworkRequestOrResponse(responseBodySize);
return {
startTimestamp,
endTimestamp,
url,
method,
statusCode,
request,
response,
};
}
const xhrInfo = xhr[SENTRY_XHR_DATA_KEY];
const networkRequestHeaders = xhrInfo
? getAllowedHeaders(xhrInfo.request_headers, options.networkRequestHeaders)
: {};
const networkResponseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.networkResponseHeaders);
const [requestBody, requestWarning] = options.networkCaptureBodies ? getBodyString(input, logger) : [undefined];
const [responseBody, responseWarning] = options.networkCaptureBodies ? _getXhrResponseBody(xhr) : [undefined];
const request = buildNetworkRequestOrResponse(networkRequestHeaders, requestBodySize, requestBody);
const response = buildNetworkRequestOrResponse(networkResponseHeaders, responseBodySize, responseBody);
return {
startTimestamp,
endTimestamp,
url,
method,
statusCode,
request: requestWarning ? mergeWarning(request, requestWarning) : request,
response: responseWarning ? mergeWarning(response, responseWarning) : response,
};
}
function getResponseHeaders(xhr: XMLHttpRequest): Record<string, string> {
const headers = xhr.getAllResponseHeaders();
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;
}, {});
}
function _getXhrResponseBody(xhr: XMLHttpRequest): [string | undefined, NetworkMetaWarning?] {
// We collect errors that happen, but only log them if we can't get any response body
const errors: unknown[] = [];
try {
return [xhr.responseText];
} catch (e) {
errors.push(e);
}
// Try to manually parse the response body, if responseText fails
try {
return _parseXhrResponse(xhr.response, xhr.responseType);
} catch (e) {
errors.push(e);
}
DEBUG_BUILD && logger.warn('Failed to get xhr response body', ...errors);
return [undefined];
}
/**
* Get the string representation of the XHR response.
* Based on MDN, these are the possible types of the response:
* string
* ArrayBuffer
* Blob
* Document
* POJO
*
* Exported only for tests.
*/
export function _parseXhrResponse(
body: XMLHttpRequest['response'],
responseType: XMLHttpRequest['responseType'],
): [string | undefined, NetworkMetaWarning?] {
try {
if (typeof body === 'string') {
return [body];
}
if (body instanceof Document) {
return [body.body.outerHTML];
}
if (responseType === 'json' && body && typeof body === 'object') {
return [JSON.stringify(body)];
}
if (!body) {
return [undefined];
}
} catch (error) {
DEBUG_BUILD && logger.exception(error, 'Failed to serialize body', body);
return [undefined, 'BODY_PARSE_ERROR'];
}
DEBUG_BUILD && logger.info('Skipping network body because of body type', body);
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
}
function _getBodySize(
body: XMLHttpRequest['response'],
responseType: XMLHttpRequest['responseType'],
): number | undefined {
try {
const bodyStr = responseType === 'json' && body && typeof body === 'object' ? JSON.stringify(body) : body;
return getBodySize(bodyStr);
} catch {
return undefined;
}
}