-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.ts
More file actions
54 lines (45 loc) · 2.19 KB
/
middleware.ts
File metadata and controls
54 lines (45 loc) · 2.19 KB
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
50
51
52
53
54
import { withSentry } from '@sentry/cloudflare';
import { applySdkMetadata, type BaseTransportOptions, debug, getIntegrationsToSetup, type Options } from '@sentry/core';
import type { Env, Hono, MiddlewareHandler } from 'hono';
import { requestHandler, responseHandler } from '../shared/middlewareHandlers';
import { patchAppUse } from '../shared/patchAppUse';
import { filterHonoIntegration } from '../shared/filterHonoIntegration';
export interface HonoCloudflareOptions extends Options<BaseTransportOptions> {}
/**
* Sentry middleware for Hono on Cloudflare Workers.
*/
export function sentry<E extends Env>(
app: Hono<E>,
options: HonoCloudflareOptions | ((env: E['Bindings']) => HonoCloudflareOptions),
): MiddlewareHandler {
withSentry(
env => {
const honoOptions = typeof options === 'function' ? options(env as E['Bindings']) : options;
applySdkMetadata(honoOptions, 'hono', ['hono', 'cloudflare']);
honoOptions.debug && debug.log('Initialized Sentry Hono middleware (Cloudflare)');
const { integrations: userIntegrations } = honoOptions;
return {
...honoOptions,
// Always filter out the Hono integration from defaults and user integrations.
// The Hono integration is already set up by withSentry, so adding it again would cause capturing too early (in Cloudflare SDK) and non-parametrized URLs.
integrations: Array.isArray(userIntegrations)
? defaults =>
getIntegrationsToSetup({
defaultIntegrations: defaults.filter(filterHonoIntegration),
integrations: userIntegrations.filter(filterHonoIntegration),
})
: typeof userIntegrations === 'function'
? defaults => userIntegrations(defaults).filter(filterHonoIntegration)
: defaults => defaults.filter(filterHonoIntegration),
};
},
// Cast needed because Hono<E> exposes a narrower fetch signature than ExportedHandler<unknown>
app as unknown as ExportedHandler<unknown>,
);
patchAppUse(app);
return async (context, next) => {
requestHandler(context);
await next(); // Handler runs in between Request above ⤴ and Response below ⤵
responseHandler(context);
};
}