|
1 |
| -import { Event, Response, SentryRequest, Session, TransportOptions } from '@sentry/types'; |
2 |
| -import { SentryError, supportsReferrerPolicy, SyncPromise } from '@sentry/utils'; |
| 1 | +import { createTransport } from '@sentry/core'; |
| 2 | +import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types'; |
3 | 3 |
|
4 |
| -import { BaseTransport } from './base'; |
5 | 4 | import { FetchImpl, getNativeFetchImplementation } from './utils';
|
6 | 5 |
|
7 |
| -/** `fetch` based transport */ |
8 |
| -export class FetchTransport extends BaseTransport { |
9 |
| - /** |
10 |
| - * Fetch API reference which always points to native browser implementation. |
11 |
| - */ |
12 |
| - private _fetch: typeof fetch; |
13 |
| - |
14 |
| - public constructor(options: TransportOptions, fetchImpl: FetchImpl = getNativeFetchImplementation()) { |
15 |
| - super(options); |
16 |
| - this._fetch = fetchImpl; |
17 |
| - } |
18 |
| - |
19 |
| - /** |
20 |
| - * @param sentryRequest Prepared SentryRequest to be delivered |
21 |
| - * @param originalPayload Original payload used to create SentryRequest |
22 |
| - */ |
23 |
| - protected _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> { |
24 |
| - // eslint-disable-next-line deprecation/deprecation |
25 |
| - if (this._isRateLimited(sentryRequest.type)) { |
26 |
| - this.recordLostEvent('ratelimit_backoff', sentryRequest.type); |
27 |
| - |
28 |
| - return Promise.reject({ |
29 |
| - event: originalPayload, |
30 |
| - type: sentryRequest.type, |
31 |
| - // eslint-disable-next-line deprecation/deprecation |
32 |
| - reason: `Transport for ${sentryRequest.type} requests locked till ${this._disabledUntil( |
33 |
| - sentryRequest.type, |
34 |
| - )} due to too many requests.`, |
35 |
| - status: 429, |
36 |
| - }); |
37 |
| - } |
| 6 | +export interface FetchTransportOptions extends BaseTransportOptions { |
| 7 | + requestOptions?: RequestInit; |
| 8 | +} |
38 | 9 |
|
39 |
| - const options: RequestInit = { |
40 |
| - body: sentryRequest.body, |
| 10 | +/** |
| 11 | + * Creates a Transport that uses the Fetch API to send events to Sentry. |
| 12 | + */ |
| 13 | +export function makeFetchTransport( |
| 14 | + options: FetchTransportOptions, |
| 15 | + nativeFetch: FetchImpl = getNativeFetchImplementation(), |
| 16 | +): Transport { |
| 17 | + function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> { |
| 18 | + const requestOptions: RequestInit = { |
| 19 | + body: request.body, |
41 | 20 | method: 'POST',
|
42 |
| - // Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default' |
43 |
| - // (see https://caniuse.com/#feat=referrer-policy), |
44 |
| - // it doesn't. And it throws an exception instead of ignoring this parameter... |
45 |
| - // REF: https://github.com/getsentry/raven-js/issues/1233 |
46 |
| - referrerPolicy: (supportsReferrerPolicy() ? 'origin' : '') as ReferrerPolicy, |
| 21 | + referrerPolicy: 'origin', |
| 22 | + ...options.requestOptions, |
47 | 23 | };
|
48 |
| - if (this.options.fetchParameters !== undefined) { |
49 |
| - Object.assign(options, this.options.fetchParameters); |
50 |
| - } |
51 |
| - if (this.options.headers !== undefined) { |
52 |
| - options.headers = this.options.headers; |
53 |
| - } |
54 | 24 |
|
55 |
| - return this._buffer |
56 |
| - .add( |
57 |
| - () => |
58 |
| - new SyncPromise<Response>((resolve, reject) => { |
59 |
| - void this._fetch(sentryRequest.url, options) |
60 |
| - .then(response => { |
61 |
| - const headers = { |
62 |
| - 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'), |
63 |
| - 'retry-after': response.headers.get('Retry-After'), |
64 |
| - }; |
65 |
| - this._handleResponse({ |
66 |
| - requestType: sentryRequest.type, |
67 |
| - response, |
68 |
| - headers, |
69 |
| - resolve, |
70 |
| - reject, |
71 |
| - }); |
72 |
| - }) |
73 |
| - .catch(reject); |
74 |
| - }), |
75 |
| - ) |
76 |
| - .then(undefined, reason => { |
77 |
| - // It's either buffer rejection or any other xhr/fetch error, which are treated as NetworkError. |
78 |
| - if (reason instanceof SentryError) { |
79 |
| - this.recordLostEvent('queue_overflow', sentryRequest.type); |
80 |
| - } else { |
81 |
| - this.recordLostEvent('network_error', sentryRequest.type); |
82 |
| - } |
83 |
| - throw reason; |
84 |
| - }); |
| 25 | + return nativeFetch(options.url, requestOptions).then(response => { |
| 26 | + return response.text().then(body => ({ |
| 27 | + body, |
| 28 | + headers: { |
| 29 | + 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'), |
| 30 | + 'retry-after': response.headers.get('Retry-After'), |
| 31 | + }, |
| 32 | + reason: response.statusText, |
| 33 | + statusCode: response.status, |
| 34 | + })); |
| 35 | + }); |
85 | 36 | }
|
| 37 | + |
| 38 | + return createTransport({ bufferSize: options.bufferSize }, makeRequest); |
86 | 39 | }
|
0 commit comments