Skip to content

Commit 5c51fdb

Browse files
authored
feat(cloudflare): Allow specifying a custom fetch in Cloudflare transport options (#18335)
Adds a new transport option to the Cloudflare provider, this allows specifying a custom fetch implementation. This allows for [Workers VPC](https://developers.cloudflare.com/workers-vpc/) to send into an internal Sentry. Example usage: ```js export default Sentry.withSentry( (env: Env) => ({ dsn: env.SENTRY_DSN, release: env.VERSION_METADATA.tag ?? env.VERSION_METADATA.id, transportOptions: { fetch: env.SENTRY_VPC_BRIDGE.fetch.bind(env.SENTRY_VPC_BRIDGE), }, }), { async fetch(req, env) { return new Response("ok"); }, }; ``` --- Before submitting a pull request, please take a look at our [Contributing](https://github.com/getsentry/sentry-javascript/blob/master/CONTRIBUTING.md) guidelines and verify: - [x] If you've added code that should be tested, please add tests. - [x] Ensure your code lints and the test suite passes (`yarn lint`) & (`yarn test`).
1 parent 30dd610 commit 5c51fdb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/cloudflare/src/transport.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { BaseTransportOptions, Transport, TransportMakeRequestResponse, Tra
22
import { createTransport, SENTRY_BUFFER_FULL_ERROR, suppressTracing } from '@sentry/core';
33

44
export interface CloudflareTransportOptions extends BaseTransportOptions {
5+
/** Custom fetch function to use. This allows usage of things like Workers VPC */
6+
fetch?: typeof fetch;
57
/** Fetch API init parameters. */
68
fetchOptions?: RequestInit;
79
}
@@ -87,7 +89,7 @@ export function makeCloudflareTransport(options: CloudflareTransportOptions): Tr
8789
};
8890

8991
return suppressTracing(() => {
90-
return fetch(options.url, requestOptions).then(response => {
92+
return (options.fetch ?? fetch)(options.url, requestOptions).then(response => {
9193
return {
9294
statusCode: response.status,
9395
headers: {

packages/cloudflare/test/transport.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,20 @@ describe('IsolatedPromiseBuffer', () => {
161161
expect(task1).toHaveBeenCalled();
162162
expect(task2).toHaveBeenCalled();
163163
});
164+
165+
it('should allow for a custom fetch function to be passed in', async () => {
166+
const customFetch = vi.fn(async () => {
167+
return {
168+
headers: new Headers(),
169+
status: 200,
170+
text: () => Promise.resolve({}),
171+
} as unknown as Response;
172+
});
173+
174+
const transport = makeCloudflareTransport({ ...DEFAULT_EDGE_TRANSPORT_OPTIONS, fetch: customFetch });
175+
176+
await transport.send(ERROR_ENVELOPE);
177+
await transport.flush();
178+
expect(customFetch).toHaveBeenCalledTimes(1);
179+
});
164180
});

0 commit comments

Comments
 (0)