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