Skip to content

Commit 1be0388

Browse files
committed
fix(deno): patch Deno.serve safely
1 parent 1a8c2cd commit 1be0388

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

packages/deno/src/integrations/deno-serve.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IntegrationFn } from '@sentry/core';
2-
import { defineIntegration } from '@sentry/core';
2+
import { debug, defineIntegration } from '@sentry/core';
33
import { setAsyncLocalStorageAsyncContextStrategy } from '../async';
44
import type { RequestHandlerWrapperOptions } from '../wrap-deno-request-handler';
55
import { wrapDenoRequestHandler } from '../wrap-deno-request-handler';
@@ -44,24 +44,40 @@ const applyHandlerWrap = <A extends Deno.Addr>(
4444
() => handler(request, info as Deno.ServeHandlerInfo<A>),
4545
)) as Deno.ServeHandler;
4646

47+
const instrumentedDenoServe = (serve: typeof Deno.serve): typeof Deno.serve =>
48+
new Proxy(serve, {
49+
apply(target, thisArg, args: ServeParams) {
50+
if (isSimpleHandler(args)) {
51+
args[0] = applyHandlerWrap(args[0]);
52+
} else if (isServeOptWithFunction(args)) {
53+
args[1] = applyHandlerWrap(args[1], args[0]);
54+
} else if (isServeInitOptions(args)) {
55+
args[0].handler = applyHandlerWrap(args[0].handler, args[0]);
56+
}
57+
// if none of those matched, it'll crash, most likely.
58+
return target.apply(thisArg, args);
59+
},
60+
});
61+
4762
const _denoServeIntegration = (() => {
4863
return {
4964
name: INTEGRATION_NAME,
5065
setupOnce() {
5166
setAsyncLocalStorageAsyncContextStrategy();
52-
Deno.serve = new Proxy(Deno.serve, {
53-
apply(target, thisArg, args: ServeParams) {
54-
if (isSimpleHandler(args)) {
55-
args[0] = applyHandlerWrap(args[0]);
56-
} else if (isServeOptWithFunction(args)) {
57-
args[1] = applyHandlerWrap(args[1], args[0]);
58-
} else if (isServeInitOptions(args)) {
59-
args[0].handler = applyHandlerWrap(args[0].handler, args[0]);
60-
}
61-
// if none of those matched, it'll crash, most likely.
62-
return target.apply(thisArg, args);
63-
},
64-
});
67+
68+
const originalServe = Deno.serve;
69+
const wrappedServe = instrumentedDenoServe(originalServe);
70+
71+
try {
72+
Object.defineProperty(Deno, 'serve', {
73+
configurable: true,
74+
enumerable: true,
75+
writable: true,
76+
value: wrappedServe,
77+
});
78+
} catch (error) {
79+
debug.warn('Could not instrument Deno.serve.', error);
80+
}
6581
},
6682
};
6783
}) satisfies IntegrationFn;

0 commit comments

Comments
 (0)