Skip to content

feat: Delete old transports #4967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 26, 2022
4 changes: 2 additions & 2 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { IS_DEBUG_BUILD } from './flags';
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
import { Breadcrumbs, Dedupe, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
import { defaultStackParsers } from './stack-parsers';
import { makeNewFetchTransport, makeNewXHRTransport } from './transports';
import { makeFetchTransport, makeXHRTransport } from './transports';

export const defaultIntegrations = [
new CoreIntegrations.InboundFilters(),
Expand Down Expand Up @@ -106,7 +106,7 @@ export function init(options: BrowserOptions = {}): void {
...options,
stackParser: stackParserFromOptions(options.stackParser || defaultStackParsers),
integrations: getIntegrationsToSetup(options),
transport: options.transport || (supportsFetch() ? makeNewFetchTransport : makeNewXHRTransport),
transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport),
};

initAndBind(BrowserClient, clientOptions);
Expand Down
212 changes: 0 additions & 212 deletions packages/browser/src/transports/base.ts

This file was deleted.

107 changes: 30 additions & 77 deletions packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,39 @@
import { Event, Response, SentryRequest, Session, TransportOptions } from '@sentry/types';
import { SentryError, supportsReferrerPolicy, SyncPromise } from '@sentry/utils';
import { createTransport } from '@sentry/core';
import { BaseTransportOptions, Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';

import { BaseTransport } from './base';
import { FetchImpl, getNativeFetchImplementation } from './utils';

/** `fetch` based transport */
export class FetchTransport extends BaseTransport {
/**
* Fetch API reference which always points to native browser implementation.
*/
private _fetch: typeof fetch;

public constructor(options: TransportOptions, fetchImpl: FetchImpl = getNativeFetchImplementation()) {
super(options);
this._fetch = fetchImpl;
}

/**
* @param sentryRequest Prepared SentryRequest to be delivered
* @param originalPayload Original payload used to create SentryRequest
*/
protected _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> {
// eslint-disable-next-line deprecation/deprecation
if (this._isRateLimited(sentryRequest.type)) {
this.recordLostEvent('ratelimit_backoff', sentryRequest.type);

return Promise.reject({
event: originalPayload,
type: sentryRequest.type,
// eslint-disable-next-line deprecation/deprecation
reason: `Transport for ${sentryRequest.type} requests locked till ${this._disabledUntil(
sentryRequest.type,
)} due to too many requests.`,
status: 429,
});
}
export interface FetchTransportOptions extends BaseTransportOptions {
requestOptions?: RequestInit;
}

const options: RequestInit = {
body: sentryRequest.body,
/**
* Creates a Transport that uses the Fetch API to send events to Sentry.
*/
export function makeFetchTransport(
options: FetchTransportOptions,
nativeFetch: FetchImpl = getNativeFetchImplementation(),
): Transport {
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> {
const requestOptions: RequestInit = {
body: request.body,
method: 'POST',
// Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default'
// (see https://caniuse.com/#feat=referrer-policy),
// it doesn't. And it throws an exception instead of ignoring this parameter...
// REF: https://github.com/getsentry/raven-js/issues/1233
referrerPolicy: (supportsReferrerPolicy() ? 'origin' : '') as ReferrerPolicy,
referrerPolicy: 'origin',
...options.requestOptions,
};
if (this.options.fetchParameters !== undefined) {
Object.assign(options, this.options.fetchParameters);
}
if (this.options.headers !== undefined) {
options.headers = this.options.headers;
}

return this._buffer
.add(
() =>
new SyncPromise<Response>((resolve, reject) => {
void this._fetch(sentryRequest.url, options)
.then(response => {
const headers = {
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
'retry-after': response.headers.get('Retry-After'),
};
this._handleResponse({
requestType: sentryRequest.type,
response,
headers,
resolve,
reject,
});
})
.catch(reject);
}),
)
.then(undefined, reason => {
// It's either buffer rejection or any other xhr/fetch error, which are treated as NetworkError.
if (reason instanceof SentryError) {
this.recordLostEvent('queue_overflow', sentryRequest.type);
} else {
this.recordLostEvent('network_error', sentryRequest.type);
}
throw reason;
});
return nativeFetch(options.url, requestOptions).then(response => {
return response.text().then(body => ({
body,
headers: {
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
'retry-after': response.headers.get('Retry-After'),
},
reason: response.statusText,
statusCode: response.status,
}));
});
}

return createTransport({ bufferSize: options.bufferSize }, makeRequest);
}
8 changes: 2 additions & 6 deletions packages/browser/src/transports/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
export { BaseTransport } from './base';
export { FetchTransport } from './fetch';
export { XHRTransport } from './xhr';

export { makeNewFetchTransport } from './new-fetch';
export { makeNewXHRTransport } from './new-xhr';
export { makeFetchTransport } from './fetch';
export { makeXHRTransport } from './xhr';
39 changes: 0 additions & 39 deletions packages/browser/src/transports/new-fetch.ts

This file was deleted.

Loading