|
| 1 | +import { defineIntegration, getClient } from '@sentry/core'; |
| 2 | +import type { IntegrationFn } from '@sentry/core'; |
| 3 | +import { setAsyncLocalStorageAsyncContextStrategy } from '../async'; |
| 4 | +import { DenoOptions } from '../types'; |
| 5 | +import { wrapDenoRequestHandler } from '../wrap-deno-request-handler'; |
| 6 | + |
| 7 | +export type RequestHandlerWrapperOptions<Addr extends Deno.Addr> = { |
| 8 | + request: Request; |
| 9 | + options: DenoOptions; |
| 10 | + info: Deno.ServeHandlerInfo<Addr>; |
| 11 | + serveOptions?: Deno.ServeOptions<Addr>; |
| 12 | +}; |
| 13 | + |
| 14 | +const INTEGRATION_NAME = 'DenoServe'; |
| 15 | + |
| 16 | +export type ServeParams = |
| 17 | + // [(Request) => Response] |
| 18 | + | [Deno.ServeHandler<Deno.NetAddr>] |
| 19 | + // [{ options }, (Request) => Response] |
| 20 | + | [Deno.ServeUnixOptions, Deno.ServeHandler<Deno.UnixAddr>] |
| 21 | + | [Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem), Deno.ServeHandler<Deno.NetAddr>] |
| 22 | + // [{ handler: (Request) => Response }] |
| 23 | + | [Deno.ServeUnixOptions & Deno.ServeInit<Deno.UnixAddr>] |
| 24 | + | [(Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem)) & Deno.ServeInit<Deno.NetAddr>]; |
| 25 | + |
| 26 | +const isServeOptWithFunction = ( |
| 27 | + p: ServeParams, |
| 28 | +): p is |
| 29 | + | [Deno.ServeUnixOptions, Deno.ServeHandler<Deno.UnixAddr>] |
| 30 | + | [Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem), Deno.ServeHandler<Deno.NetAddr>] => |
| 31 | + p.length >= 2 && typeof p[1] === 'function' && !!p[0] && typeof p[0] === 'object'; |
| 32 | + |
| 33 | +const isServeInitOptions = ( |
| 34 | + p: ServeParams, |
| 35 | +): p is |
| 36 | + | [Deno.ServeUnixOptions & Deno.ServeInit<Deno.UnixAddr>] |
| 37 | + | [(Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem)) & Deno.ServeInit<Deno.NetAddr>] => |
| 38 | + typeof p[0] === 'object' && |
| 39 | + !!p[0] && |
| 40 | + !isServeOptWithFunction(p) && |
| 41 | + 'handler' in p[0] && |
| 42 | + typeof p[0].handler === 'function'; |
| 43 | + |
| 44 | +const isSimpleServeHandler = (p: ServeParams): p is [Deno.ServeHandler<Deno.NetAddr>] => typeof p[0] === 'function'; |
| 45 | + |
| 46 | +const isServeUnixHandler = (p: ServeParams): p is [Deno.ServeUnixOptions, Deno.ServeHandler<Deno.UnixAddr>] => |
| 47 | + isServeOptWithFunction(p) && 'path' in p[0] && typeof p[0].path === 'string'; |
| 48 | + |
| 49 | +const isServeTcpHandler = ( |
| 50 | + p: ServeParams, |
| 51 | +): p is [Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem), Deno.ServeHandler<Deno.NetAddr>] => |
| 52 | + isServeOptWithFunction(p) && (!('path' in p[0]) || typeof p[0].path !== 'string'); |
| 53 | + |
| 54 | +const isServeUnixInit = (p: ServeParams): p is [Deno.ServeUnixOptions & Deno.ServeInit<Deno.UnixAddr>] => |
| 55 | + isServeInitOptions(p) && 'path' in p[0] && typeof p[0].path === 'string'; |
| 56 | + |
| 57 | +const isServeTcpInit = ( |
| 58 | + p: ServeParams, |
| 59 | +): p is [(Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem)) & Deno.ServeInit<Deno.NetAddr>] => |
| 60 | + isServeInitOptions(p) && (!('path' in p[0]) || typeof p[0].path !== 'string'); |
| 61 | + |
| 62 | + |
| 63 | +const applyHandlerWrap = |
| 64 | + <A extends Deno.Addr>( |
| 65 | + handler: Deno.ServeHandler<A>, |
| 66 | + options: DenoOptions, |
| 67 | + serveOptions?: Deno.ServeOptions<A>, |
| 68 | + ): Deno.ServeHandler<A> => |
| 69 | + async (request, info) => |
| 70 | + await wrapDenoRequestHandler<A>( |
| 71 | + { |
| 72 | + options, |
| 73 | + request, |
| 74 | + info, |
| 75 | + serveOptions, |
| 76 | + }, |
| 77 | + () => handler(request, info), |
| 78 | + ); |
| 79 | + |
| 80 | +const _denoServeIntegration = (() => { |
| 81 | + return { |
| 82 | + name: INTEGRATION_NAME, |
| 83 | + setupOnce() { |
| 84 | + setAsyncLocalStorageAsyncContextStrategy(); |
| 85 | + Deno.serve = new Proxy(Deno.serve, { |
| 86 | + apply(target, thisArg, args: ServeParams) { |
| 87 | + const options: DenoOptions = getClient()?.getOptions() ?? {}; |
| 88 | + if (isSimpleServeHandler(args)) { |
| 89 | + args[0] = applyHandlerWrap(args[0], options); |
| 90 | + } else if (isServeUnixHandler(args)) { |
| 91 | + args[1] = applyHandlerWrap(args[1], options, args[0]); |
| 92 | + } else if (isServeTcpHandler(args)) { |
| 93 | + args[1] = applyHandlerWrap(args[1], options, args[0]); |
| 94 | + } else if (isServeUnixInit(args)) { |
| 95 | + args[0].handler = applyHandlerWrap(args[0].handler, options, args[0]); |
| 96 | + } else if (isServeTcpInit(args)) { |
| 97 | + args[0].handler = applyHandlerWrap(args[0].handler, options, args[0]); |
| 98 | + } |
| 99 | + // if none of those matched, it'll crash, most likely. |
| 100 | + return target.apply(thisArg, args); |
| 101 | + }, |
| 102 | + }); |
| 103 | + }, |
| 104 | + }; |
| 105 | +}) satisfies IntegrationFn; |
| 106 | + |
| 107 | +export const denoServeIntegration = defineIntegration(_denoServeIntegration); |
0 commit comments