|
| 1 | +import { type CloudflareOptions, wrapRequestHandler } from '@sentry/cloudflare'; |
| 2 | +import { getDefaultIntegrations as getDefaultCloudflareIntegrations } from '@sentry/cloudflare'; |
| 3 | +import type { Handle } from '@sveltejs/kit'; |
| 4 | + |
| 5 | +import { addNonEnumerableProperty } from '@sentry/core'; |
| 6 | +import { rewriteFramesIntegration } from '../server-common/rewriteFramesIntegration'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Initializes Sentry SvelteKit Cloudflare SDK |
| 10 | + * This should be before the sentryHandle() call. |
| 11 | + * |
| 12 | + * In the Node export, this is a stub that does nothing. |
| 13 | + */ |
| 14 | +export function initCloudflareSentryHandle(options: CloudflareOptions): Handle { |
| 15 | + const opts: CloudflareOptions = { |
| 16 | + defaultIntegrations: [...getDefaultCloudflareIntegrations(options), rewriteFramesIntegration()], |
| 17 | + ...options, |
| 18 | + }; |
| 19 | + |
| 20 | + const handleInitSentry: Handle = ({ event, resolve }) => { |
| 21 | + // if event.platform exists (should be there in a cloudflare worker), then do the cloudflare sentry init |
| 22 | + if (event.platform) { |
| 23 | + // This is an optional local that the `sentryHandle` handler checks for to avoid double isolation |
| 24 | + // In Cloudflare the `wrapRequestHandler` function already takes care of |
| 25 | + // - request isolation |
| 26 | + // - trace continuation |
| 27 | + // -setting the request onto the scope |
| 28 | + addNonEnumerableProperty(event.locals, '_sentrySkipRequestIsolation', true); |
| 29 | + return wrapRequestHandler( |
| 30 | + { |
| 31 | + options: opts, |
| 32 | + request: event.request, |
| 33 | + // @ts-expect-error This will exist in Cloudflare |
| 34 | + context: event.platform.context, |
| 35 | + }, |
| 36 | + () => resolve(event), |
| 37 | + ); |
| 38 | + } |
| 39 | + return resolve(event); |
| 40 | + }; |
| 41 | + |
| 42 | + return handleInitSentry; |
| 43 | +} |
0 commit comments