Skip to content

Commit

Permalink
feat(v8/browser): Remove XHR transport (#10703)
Browse files Browse the repository at this point in the history
resolves #5927

Removes `makeXHRTransport` and makes the fetch transport the default
one.

I've also added a warning warning to browser client init that states
that the sentry browser sdk requires the fetch API to function.
  • Loading branch information
AbhiPrasad committed Feb 21, 2024
1 parent c2baeac commit b0752b5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 162 deletions.
5 changes: 5 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ to access and mutate the current scope.

`@sentry/hub` has been removed. All exports from `@sentry.hub` should be available in `@sentry/core`.

## Removal of `makeXHRTransport` transport (#10703)

The `makeXHRTransport` transport has been removed. Only `makeFetchTransport` is available now. This means that the
Sentry SDK requires the fetch API to be available in the environment.

## General API Changes

- The minumum supported Node version for all the SDK packages is Node 14 (#10527)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Sentry.init({
replaysOnErrorSampleRate: 1.0,
integrations: [window.Replay],
transport: options => {
const transport = new Sentry.makeXHRTransport(options);
const transport = new Sentry.makeFetchTransport(options);

delete transport.send.__sentry__baseTransport__;

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export * from './metrics';

export { WINDOW } from './helpers';
export { BrowserClient } from './client';
export { makeFetchTransport, makeXHRTransport } from './transports';
export { makeFetchTransport } from './transports/fetch';
export {
defaultStackParser,
defaultStackLineParsers,
Expand Down
11 changes: 9 additions & 2 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { httpContextIntegration } from './integrations/httpcontext';
import { linkedErrorsIntegration } from './integrations/linkederrors';
import { browserApiErrorsIntegration } from './integrations/trycatch';
import { defaultStackParser } from './stack-parsers';
import { makeFetchTransport, makeXHRTransport } from './transports';
import { makeFetchTransport } from './transports/fetch';

/** Get the default integrations for the browser SDK. */
export function getDefaultIntegrations(_options: Options): Integration[] {
Expand Down Expand Up @@ -116,11 +116,18 @@ export function init(options: BrowserOptions = {}): void {
options.sendClientReports = true;
}

if (DEBUG_BUILD) {
if (!supportsFetch()) {
logger.warn(
'No Fetch API detected. The Sentry SDK requires a Fetch API compatible environment to send events. Please add a Fetch API polyfill.',
);
}
}
const clientOptions: BrowserClientOptions = {
...options,
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
integrations: getIntegrationsToSetup(options),
transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport),
transport: options.transport || makeFetchTransport,
};

initAndBind(BrowserClient, clientOptions);
Expand Down
7 changes: 6 additions & 1 deletion packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { clearCachedFetchImplementation, getNativeFetchImplementation } from './
*/
export function makeFetchTransport(
options: BrowserTransportOptions,
nativeFetch: FetchImpl = getNativeFetchImplementation(),
nativeFetch: FetchImpl | undefined = getNativeFetchImplementation(),
): Transport {
let pendingBodySize = 0;
let pendingCount = 0;
Expand Down Expand Up @@ -41,6 +41,11 @@ export function makeFetchTransport(
...options.fetchOptions,
};

if (!nativeFetch) {
clearCachedFetchImplementation();
return rejectedSyncPromise('No fetch implementation available');
}

try {
return nativeFetch(options.url, requestOptions).then(response => {
pendingBodySize -= requestSize;
Expand Down
2 changes: 0 additions & 2 deletions packages/browser/src/transports/index.ts

This file was deleted.

10 changes: 8 additions & 2 deletions packages/browser/src/transports/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type FetchImpl = typeof fetch;
* Firefox: NetworkError when attempting to fetch resource
* Safari: resource blocked by content blocker
*/
export function getNativeFetchImplementation(): FetchImpl {
export function getNativeFetchImplementation(): FetchImpl | undefined {
if (cachedFetchImpl) {
return cachedFetchImpl;
}
Expand Down Expand Up @@ -75,7 +75,13 @@ export function getNativeFetchImplementation(): FetchImpl {
}
}

return (cachedFetchImpl = fetchImpl.bind(WINDOW));
try {
return (cachedFetchImpl = fetchImpl.bind(WINDOW));
} catch (e) {
// empty
}

return undefined;
/* eslint-enable @typescript-eslint/unbound-method */
}

Expand Down
52 changes: 0 additions & 52 deletions packages/browser/src/transports/xhr.ts

This file was deleted.

99 changes: 0 additions & 99 deletions packages/browser/test/unit/transports/xhr.test.ts

This file was deleted.

29 changes: 27 additions & 2 deletions packages/vue/test/integration/VueIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ describe('Sentry.VueIntegration', () => {
let loggerWarnings: unknown[] = [];
let warnings: unknown[] = [];

const globalFetch = globalThis.fetch;
const globalResponse = globalThis.Response;
const globalRequest = globalThis.Request;

beforeAll(() => {
globalThis.fetch = jest.fn();
// @ts-expect-error This is a mock
globalThis.Response = jest.fn();
globalThis.Request = jest.fn();
});

afterAll(() => {
globalThis.fetch = globalFetch;
globalThis.Response = globalResponse;
globalThis.Request = globalRequest;
});

beforeEach(() => {
warnings = [];
loggerWarnings = [];
Expand All @@ -28,7 +45,11 @@ describe('Sentry.VueIntegration', () => {
});

it('allows to initialize integration later', () => {
Sentry.init({ dsn: PUBLIC_DSN, defaultIntegrations: false, autoSessionTracking: false });
Sentry.init({
dsn: PUBLIC_DSN,
defaultIntegrations: false,
autoSessionTracking: false,
});

const el = document.createElement('div');
const app = createApp({
Expand All @@ -48,7 +69,11 @@ describe('Sentry.VueIntegration', () => {
});

it('warns when mounting before SDK.VueIntegration', () => {
Sentry.init({ dsn: PUBLIC_DSN, defaultIntegrations: false, autoSessionTracking: false });
Sentry.init({
dsn: PUBLIC_DSN,
defaultIntegrations: false,
autoSessionTracking: false,
});

const el = document.createElement('div');
const app = createApp({
Expand Down

0 comments on commit b0752b5

Please sign in to comment.