Skip to content

Commit 9f20419

Browse files
committed
feat(browser) add new v7 xhr transport
add makeNewXHRTransport function (WIP, needs testing(
1 parent 0cf3014 commit 9f20419

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
BaseTransportOptions,
3+
createTransport,
4+
NewTransport,
5+
TransportMakeRequestResponse,
6+
TransportRequest,
7+
} from '@sentry/core';
8+
import { SyncPromise } from '@sentry/utils';
9+
10+
export interface XHRTransportOptions extends BaseTransportOptions {
11+
headers?: { [key: string]: string };
12+
}
13+
14+
/**
15+
* Creates a Transport that uses the XMLHttpRequest API to send events to Sentry.
16+
*/
17+
export function makeNewXHRTransport(options: XHRTransportOptions): NewTransport {
18+
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
19+
return new SyncPromise<TransportMakeRequestResponse>((resolve, _reject) => {
20+
const xhr = new XMLHttpRequest();
21+
22+
xhr.onreadystatechange = (): void => {
23+
if (xhr.readyState === 4) {
24+
const headers = {
25+
'x-sentry-rate-limits': xhr.getResponseHeader('X-Sentry-Rate-Limits'),
26+
'retry-after': xhr.getResponseHeader('Retry-After'),
27+
};
28+
const response = {
29+
body: xhr.response,
30+
headers,
31+
reason: xhr.statusText,
32+
statusCode: xhr.status,
33+
};
34+
35+
resolve(response);
36+
}
37+
};
38+
39+
xhr.open('POST', options.url);
40+
for (const header in options.headers) {
41+
if (Object.prototype.hasOwnProperty.call(options.headers, header)) {
42+
xhr.setRequestHeader(header, options.headers[header]);
43+
}
44+
}
45+
xhr.send(request.body);
46+
});
47+
}
48+
49+
return createTransport({ bufferSize: options.bufferSize }, makeRequest);
50+
}

0 commit comments

Comments
 (0)