|
| 1 | +import * as diagnosticsChannel from 'node:diagnostics_channel'; |
| 2 | +import { createMultiMatcher } from 'remix/route-pattern/match'; |
| 3 | +import { remixV3Channels } from '@sentry/server-utils/orchestrion/config'; |
| 4 | + |
| 5 | +import type { MatcherLike, RouterOptionsLike } from '../types'; |
| 6 | +import { sentryRemixMiddleware } from './middleware'; |
| 7 | + |
| 8 | +const NOOP = (): void => {}; |
| 9 | + |
| 10 | +/** The call's arguments, as orchestrion's transform attaches them to a tracing channel context. */ |
| 11 | +interface ChannelContext { |
| 12 | + arguments: unknown[]; |
| 13 | +} |
| 14 | + |
| 15 | +// Marks an options object already injected into, so the same mutation cannot be applied twice. |
| 16 | +const INJECTED = Symbol.for('SentryRemixV3Injected'); |
| 17 | + |
| 18 | +// `subscribe()` takes a fresh object literal and the channel keys handlers by identity, so nothing else |
| 19 | +// stops a second call adding a second set. The documented setup calls this twice, from the `--import` |
| 20 | +// entry and from `setupOnce()`. |
| 21 | +let subscribed = false; |
| 22 | + |
| 23 | +/** |
| 24 | + * Prepend the Sentry middleware to every router an app builds. |
| 25 | + * |
| 26 | + * Injection happens on the channel's `start` event, before `createRouter` reads its options. |
| 27 | + * Orchestrion's transform collects the arguments into an array and spreads them back into the call, so |
| 28 | + * assigning at an index the caller never passed works. That is what covers `createRouter()` with no |
| 29 | + * arguments at all. |
| 30 | + */ |
| 31 | +export function instrumentRemixV3(): void { |
| 32 | + if (subscribed || !diagnosticsChannel.tracingChannel) { |
| 33 | + return; |
| 34 | + } |
| 35 | + subscribed = true; |
| 36 | + |
| 37 | + diagnosticsChannel.tracingChannel<ChannelContext, ChannelContext>(remixV3Channels.REMIX_V3_CREATE_ROUTER).subscribe({ |
| 38 | + start(data) { |
| 39 | + // Node rethrows anything this handler throws as an uncaught exception, which would kill an app |
| 40 | + // that runs fine without Sentry. Frozen options, a non-writable property and a `route-pattern` |
| 41 | + // copy that cannot build a matcher all reach here, so the router is left uninstrumented instead. |
| 42 | + try { |
| 43 | + injectRouterMiddleware(ensureOptions(data.arguments)); |
| 44 | + } catch { |
| 45 | + // Ignored on purpose. |
| 46 | + } |
| 47 | + }, |
| 48 | + end: NOOP, |
| 49 | + asyncStart: NOOP, |
| 50 | + asyncEnd: NOOP, |
| 51 | + error: NOOP, |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * The options object, created when the caller omitted it. `undefined` when the caller passed something |
| 57 | + * that is not an options object, which must not be overwritten. |
| 58 | + */ |
| 59 | +function ensureOptions(args: unknown[]): Record<string, unknown> | undefined { |
| 60 | + const existing = args[0]; |
| 61 | + |
| 62 | + if (existing === undefined || existing === null) { |
| 63 | + const created: Record<string, unknown> = {}; |
| 64 | + args[0] = created; |
| 65 | + return created; |
| 66 | + } |
| 67 | + |
| 68 | + return typeof existing === 'object' ? (existing as Record<string, unknown>) : undefined; |
| 69 | +} |
| 70 | + |
| 71 | +function injectRouterMiddleware(raw: Record<string, unknown> | undefined): void { |
| 72 | + if (!raw) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const marker = raw as { [INJECTED]?: boolean }; |
| 77 | + if (marker[INJECTED]) { |
| 78 | + return; |
| 79 | + } |
| 80 | + marker[INJECTED] = true; |
| 81 | + |
| 82 | + const options = raw as RouterOptionsLike; |
| 83 | + |
| 84 | + // The router never exposes its matcher, and resolving the route pattern needs one, so supplying it is |
| 85 | + // the only way to hold a reference. An app that supplied its own keeps it. |
| 86 | + const matcher: MatcherLike = options.matcher ?? (createMultiMatcher() as MatcherLike); |
| 87 | + options.matcher = matcher; |
| 88 | + |
| 89 | + // Prepended rather than appended, so the Sentry middleware wraps the app's own. |
| 90 | + options.middleware = [sentryRemixMiddleware(matcher), ...(options.middleware ?? [])]; |
| 91 | +} |
0 commit comments