Repository navigation
Expand file tree
/
Copy pathpatchRoute.ts
More file actions
137 lines (118 loc) · 4.69 KB
/
Copy pathpatchRoute.ts
File metadata and controls
137 lines (118 loc) · 4.69 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { debug, getOriginalFunction } from '@sentry/core';
import type { WrappedFunction } from '@sentry/core';
import type { Hono, MiddlewareHandler } from 'hono';
import { Hono as HonoClass } from 'hono';
import { DEBUG_BUILD } from '../debug-build';
import { isMiddleware } from '../utils/isMiddleware';
import { patchAppRequest } from './patchAppRequest';
import { wrapMiddlewareWithSpan } from './wrapMiddlewareSpan';
export type HonoRoute = {
method: string;
path: string;
handler: MiddlewareHandler;
};
// oxlint-disable-next-line typescript/no-explicit-any
type HonoAny = Hono<any>;
export type RouteHookHandle = {
activate: () => void;
getPendingSubApps: () => Set<HonoAny>;
};
type HonoBaseProto = {
route?: (path: string, app: HonoAny) => HonoAny;
__sentryRouteHook__?: RouteHookHandle;
};
/**
* Creates the two-phase state machine for the route hook.
*
* - Pre-activation: collects sub-app references into a pending set.
* - Post-activation: instruments sub-apps immediately at mount time.
*/
function createRouteHook(): { handle: RouteHookHandle; onSubAppMounted: (subApp: HonoAny) => void } {
const pendingSubApps = new Set<HonoAny>();
let activated = false;
return {
handle: {
activate: () => {
activated = true;
},
getPendingSubApps: () => pendingSubApps,
},
onSubAppMounted: (subApp: HonoAny) => {
if (activated) {
DEBUG_BUILD && debug.log(`[hono] Instrumenting sub-app at mount time (${subApp.routes.length} routes).`);
wrapSubAppMiddleware(subApp.routes as HonoRoute[]);
patchAppRequest(subApp);
} else {
DEBUG_BUILD &&
debug.log(`[hono] Collecting sub-app for deferred instrumentation (${subApp.routes.length} routes).`);
pendingSubApps.add(subApp);
}
},
};
}
/**
* Installs a hook on `HonoBase.prototype.route` to intercept sub-app mounting.
*
* Returns a handle with `activate()` and `getPendingSubApps()`.
* Idempotent: subsequent calls return the same handle
*/
export function installRouteHookOnPrototype(): RouteHookHandle {
const noopHandle: RouteHookHandle = { activate: () => {}, getPendingSubApps: () => new Set() };
// `route` is defined on HonoBase.prototype, one level above the concrete subclass
const honoBaseProto = Object.getPrototypeOf(HonoClass.prototype) as HonoBaseProto;
if (!honoBaseProto || typeof honoBaseProto.route !== 'function') {
DEBUG_BUILD && debug.warn('[hono] Could not find HonoBase.prototype.route — sub-app instrumentation disabled.');
return noopHandle;
}
// Already patched
if (getOriginalFunction(honoBaseProto.route as unknown as WrappedFunction)) {
return honoBaseProto.__sentryRouteHook__ ?? noopHandle;
}
const originalRoute = honoBaseProto.route;
const { handle, onSubAppMounted } = createRouteHook();
honoBaseProto.route = new Proxy(originalRoute, {
apply(_target, thisArg, args: [string, HonoAny]) {
const [, subApp] = args;
if (subApp && Array.isArray(subApp.routes)) {
onSubAppMounted(subApp);
}
return Reflect.apply(_target, thisArg, args);
},
get(target, prop, receiver) {
if (prop === '__sentry_original__') {
return originalRoute;
}
return Reflect.get(target, prop, receiver);
},
});
honoBaseProto.__sentryRouteHook__ = handle;
DEBUG_BUILD && debug.log('[hono] Installed route hook on HonoBase.prototype.');
return handle;
}
/**
* Identifies middleware handlers in a sub-app's flat routes array and wraps them in spans.
*
* Heuristics (since Hono has no "isMiddleware" flag):
* 1. Position: `app.get('/path', mw, handler)` produces entries with the same method+path.
* All but the LAST are middleware (they call `next()`).
* 2. Arity (# of params) for method 'ALL': `.use()` handlers always have 2+ params (context, next),
* while `.all()` route handlers typically have 1 (`context` only).
* See: https://github.com/honojs/hono/blob/18fe604c8cefc2628240651b1af219692e1918c1/src/hono-base.ts#L156-L168
*/
export function wrapSubAppMiddleware(routes: HonoRoute[]): void {
const lastIndexByKey = new Map<string, number>();
for (const [i, route] of routes.entries()) {
// \0 (null byte) is a collision-free delimiter: it cannot appear in a valid HTTP method name or URL path
lastIndexByKey.set(`${route.method}\0${route.path}`, i);
}
for (const [i, route] of routes.entries()) {
if (typeof route.handler !== 'function') {
continue;
}
const isLastForGroup = lastIndexByKey.get(`${route.method}\0${route.path}`) === i;
const isMW = !isLastForGroup || (route.method === 'ALL' && isMiddleware(route.handler));
if (isMW) {
route.handler = wrapMiddlewareWithSpan(route.handler);
}
}
}