-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathrouter.js
More file actions
670 lines (563 loc) · 22.3 KB
/
Copy pathrouter.js
File metadata and controls
670 lines (563 loc) · 22.3 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
'use strict'
const METHODS = [...require('http').METHODS.map(v => v.toLowerCase()), 'all']
const ROUTE_RESOLUTION_FAILED = Symbol('routeResolutionFailed')
const shimmer = require('../../datadog-shimmer')
const { addHook, channel, createErrorPublisher } = require('./helpers/instrument')
const { getCompileToRegexp } = require('./path-to-regexp')
const {
getRouterMountPaths,
joinPath,
setLayerMeta,
getLayerMeta,
isAppMounted,
setRouterMountPath,
extractMountPaths,
getRouteFullPaths,
wrapRouteMethodsAndPublish,
collectRoutesFromRouter,
} = require('./helpers/router-helper')
function isFastStar (layer, matchers) {
return layer.regexp?.fast_star ?? matchers.hasStarPath
}
function isFastSlash (layer, matchers) {
return layer.regexp?.fast_slash ?? matchers.hasSlashPath
}
/**
* Cache the per-layer dispatch metadata in a side table instead of replacing
* `layer.handle`. Phase-sorting hosts (loopback's `_findLayerByHandler`) map a
* layer back to the user handler by scanning the handle, so the handle has to
* stay the user's function.
*
* @param {{ handle: Function, name?: string, path?: string,
* regexp?: { fast_star?: boolean, fast_slash?: boolean } }} layer
* @param {Array<{ path?: string, regex?: RegExp }> & { hasStarPath?: boolean, hasSlashPath?: boolean }} matchers
* @returns {void}
*/
function annotateLayer (layer, matchers) {
const handle = layer.handle
const name = handle._name || layer.name || handle.name
let captureRoute
let needMultiMatch = false
if (matchers.length !== 0 && !isFastStar(layer, matchers) && !isFastSlash(layer, matchers)) {
if (matchers.length === 1) {
captureRoute = matchers[0].path
} else {
needMultiMatch = true
}
}
setLayerMeta(layer, { name, captureRoute, needMultiMatch, matchers })
}
/**
* Resolve the route for a dispatched layer. Single-pattern layers carry a
* constant route; only multi-pattern stacks need the per-request `layer.path`
* match the host already computed.
*
* @param {{ captureRoute?: string, needMultiMatch: boolean,
* matchers: Array<{ path?: string, regex?: RegExp }> }} meta
* @param {{ path?: string }} layer
* @returns {string | undefined}
*/
function resolveLayerRoute (meta, layer) {
if (!meta.needMultiMatch) return meta.captureRoute
for (const matcher of meta.matchers) {
if (matcher.regex?.test(layer.path)) return matcher.path
}
}
/**
* Preserve the host Layer's error boundary while resolving a route outside its
* dispatch method. This path runs only for multi-pattern layers.
*
* @param {{ captureRoute?: string, needMultiMatch: boolean,
* matchers: Array<{ path?: string, regex?: RegExp }> }} meta
* @param {{ path?: string }} layer
* @param {Function} next
* @returns {string | undefined | typeof ROUTE_RESOLUTION_FAILED}
*/
function resolveLayerRouteOrForwardError (meta, layer, next) {
try {
return resolveLayerRoute(meta, layer)
} catch (error) {
next(error)
return ROUTE_RESOLUTION_FAILED
}
}
/**
* Build the request/error dispatch wrappers for one host (`express` / `router`).
* They wrap the layer's prototype dispatch and read the side-table metadata, so
* `layer.handle` is never replaced. The arity guard mirrors the host's own
* (`handle_request` skips 4-arg handlers, `handle_error` skips the rest), so a
* span is published only for the layer the host actually runs.
*
* @param {string} name Channel namespace (`apm:<name>:middleware:*`).
* @returns {{
* wrapLayerRequest: (originalRequest: Function) => Function,
* wrapLayerError: (originalError: Function) => Function,
* wrapLegacyHandle: (layer: object, original: Function, guardRepeatedNext?: boolean) => Function
* }}
*/
function createLayerDispatchWrappers (name) {
const enterChannel = channel(`apm:${name}:middleware:enter`)
const exitChannel = channel(`apm:${name}:middleware:exit`)
const finishChannel = channel(`apm:${name}:middleware:finish`)
const errorChannel = channel(`apm:${name}:middleware:error`)
const nextChannel = channel(`apm:${name}:middleware:next`)
const repeatChannel = channel(`apm:${name}:middleware:repeat`)
// Bound per name so express and a bare router keep independent guards.
const publishError = createErrorPublisher(errorChannel)
/**
* @param {import('node:http').IncomingMessage} req
* @param {string | undefined} layerName
* @param {(error?: unknown) => void} originalNext
*/
function wrapNext (req, layerName, originalNext) {
// Per layer dispatch, N per request. Named `next`/arity-1 mirrors the
// router continuation so wrapCallback skips its name/length rewrite.
let calls = 0
return shimmer.wrapCallback(originalNext, original => function next (error) {
// A handler that calls `next()` and then rejects (`next(); await bg()`)
// makes the host call this continuation twice. Publish once so the second
// pass cannot tag the already-finished span's parent with a late error.
calls++
if (calls === 1) {
if (error && error !== 'route' && error !== 'router') {
publishError({ req, error })
}
nextChannel.publish({ req })
finishChannel.publish({ req })
} else if (calls === 2) {
// Surface the repeat as a diagnostic on the still-live request span. The
// host cannot tell a legitimate `next(); await bg()` from a buggy double
// `next()`, so this only records that it happened, not that it is wrong.
repeatChannel.publish({ req, name: layerName, error })
}
original.apply(this, arguments)
})
}
// Every host dispatch turns a synchronous throw into `next(error)`, and the
// hosts that await the handler (router >=2, express 5, express 4 +
// express-async-errors) do the same for a rejected promise. Passing
// `wrappedNext` through captures both without a tracer-side try/catch; only
// `exit` needs the `finally`. express 4's native dispatch converts only the
// synchronous throw — exactly what the pre-refactor handle wrap caught.
/**
* @param {Function} originalRequest
* @returns {Function}
*/
function wrapLayerRequest (originalRequest) {
return function (req, res, next) {
if (!enterChannel.hasSubscribers) return originalRequest.call(this, req, res, next)
const meta = getLayerMeta(this)
if (meta === undefined || this.handle.length > 3) return originalRequest.call(this, req, res, next)
let route = meta.captureRoute
if (meta.needMultiMatch) {
route = resolveLayerRouteOrForwardError(meta, this, next)
if (route === ROUTE_RESOLUTION_FAILED) return
}
const wrappedNext = typeof next === 'function' ? wrapNext(req, meta.name, next) : next
enterChannel.publish({ name: meta.name, req, route, layer: this })
try {
return originalRequest.call(this, req, res, wrappedNext)
} finally {
exitChannel.publish({ req })
}
}
}
/**
* @param {Function} originalError
* @returns {Function}
*/
function wrapLayerError (originalError) {
return function (error, req, res, next) {
if (!enterChannel.hasSubscribers) return originalError.call(this, error, req, res, next)
const meta = getLayerMeta(this)
if (meta === undefined || this.handle.length !== 4) return originalError.call(this, error, req, res, next)
let route = meta.captureRoute
if (meta.needMultiMatch) {
route = resolveLayerRouteOrForwardError(meta, this, next)
if (route === ROUTE_RESOLUTION_FAILED) return
}
const wrappedNext = typeof next === 'function' ? wrapNext(req, meta.name, next) : next
enterChannel.publish({ name: meta.name, req, route, layer: this })
try {
return originalError.call(this, error, req, res, wrappedNext)
} finally {
exitChannel.publish({ req })
}
}
}
// express <4.6.0 dispatches `layer.handle` directly, so its replacement must preserve arity.
/**
* @param {object} layer
* @param {Function} original
* @returns {Function}
*/
function wrapNativeLegacyRequestHandle (layer, original) {
const meta = getLayerMeta(layer)
const { name, captureRoute, needMultiMatch } = meta
return shimmer.wrapFunction(original, inner => function (req, res, next) {
if (!enterChannel.hasSubscribers) return inner.call(this, req, res, next)
let calls = 0
if (typeof next === 'function') {
next = shimmer.wrapCallback(next, originalNext => function next (error) {
calls++
if (calls === 1) {
if (error && error !== 'route' && error !== 'router') {
publishError({ req, error })
}
nextChannel.publish({ req })
finishChannel.publish({ req })
} else if (calls === 2) {
repeatChannel.publish({ req, name, error })
}
originalNext.apply(this, arguments)
})
}
const route = needMultiMatch ? resolveLayerRoute(meta, layer) : captureRoute
enterChannel.publish({ name, req, route, layer })
try {
return inner.call(this, req, res, next)
} catch (error) {
if (calls === 0) {
calls = 1
publishError({ req, error })
nextChannel.publish({ req })
finishChannel.publish({ req })
}
throw error
} finally {
exitChannel.publish({ req })
}
})
}
/**
* @param {object} layer
* @param {Function} original
* @returns {Function}
*/
function wrapNativeLegacyErrorHandle (layer, original) {
const meta = getLayerMeta(layer)
const { name, captureRoute, needMultiMatch } = meta
return shimmer.wrapFunction(original, inner => function (error, req, res, next) {
if (!enterChannel.hasSubscribers) return inner.call(this, error, req, res, next)
let calls = 0
if (typeof next === 'function') {
next = shimmer.wrapCallback(next, originalNext => function next (nextError) {
calls++
if (calls === 1) {
if (nextError && nextError !== 'route' && nextError !== 'router') {
publishError({ req, error: nextError })
}
nextChannel.publish({ req })
finishChannel.publish({ req })
} else if (calls === 2) {
repeatChannel.publish({ req, name, error: nextError })
}
originalNext.apply(this, arguments)
})
}
const route = needMultiMatch ? resolveLayerRoute(meta, layer) : captureRoute
enterChannel.publish({ name, req, route, layer })
try {
return inner.call(this, error, req, res, next)
} catch (caught) {
if (calls === 0) {
calls = 1
publishError({ req, error: caught })
nextChannel.publish({ req })
finishChannel.publish({ req })
}
throw caught
} finally {
exitChannel.publish({ req })
}
})
}
/**
* @param {object} layer
* @param {Function} original
* @param {boolean} [guardRepeatedNext]
* @returns {Function}
*/
function wrapLegacyHandle (layer, original, guardRepeatedNext = false) {
if (!guardRepeatedNext) {
return original.length === 4
? wrapNativeLegacyErrorHandle(layer, original)
: wrapNativeLegacyRequestHandle(layer, original)
}
// `annotateLayer` always runs first in `wrapStack`, so the captured meta is
// never undefined here (unlike the prototype wraps, where `this` can be any
// layer the host dispatches).
const meta = getLayerMeta(layer)
const { name, captureRoute, needMultiMatch } = meta
const wrapped = shimmer.wrapFunction(original, inner => function (...args) {
if (!enterChannel.hasSubscribers) return inner.apply(this, args)
const isErrorHandler = original.length === 4
const req = args[isErrorHandler ? 1 : 0]
const nextIndex = isErrorHandler ? 3 : 2
let calls = 0
if (typeof args[nextIndex] === 'function') {
args[nextIndex] = shimmer.wrapCallback(args[nextIndex], originalNext => function next (error) {
calls++
if (calls === 1) {
if (error && error !== 'route' && error !== 'router') {
publishError({ req, error })
}
nextChannel.publish({ req })
finishChannel.publish({ req })
} else if (calls === 2) {
repeatChannel.publish({ req, name, error })
}
originalNext.apply(this, arguments)
})
}
const route = needMultiMatch ? resolveLayerRoute(meta, layer) : captureRoute
enterChannel.publish({ name, req, route, layer })
try {
return inner.apply(this, args)
} catch (error) {
// Legacy hosts catch outside the layer and never call its wrapped `next`, so finish before rethrowing.
if (calls === 0) {
calls = 1
publishError({ req, error })
nextChannel.publish({ req })
finishChannel.publish({ req })
}
throw error
} finally {
exitChannel.publish({ req })
}
})
Object.defineProperty(wrapped, 'length', { value: original.length, configurable: true })
return wrapped
}
return { wrapLayerRequest, wrapLayerError, wrapLegacyHandle }
}
/**
* @param {{ handle_request?: unknown, handleRequest?: unknown }} layer
* @returns {boolean}
*/
function hasLayerDispatch (layer) {
return typeof layer.handle_request === 'function' || typeof layer.handleRequest === 'function'
}
// TODO: Move this function to a shared file between Express and Router
/**
* @param {string} name Channel namespace (`apm:<name>:middleware:*`).
* @param {((pattern: string | RegExp) => RegExp | undefined) | undefined} compile
* Host-resolved path-to-regexp compile adapter, or undefined when the host
* instance ships no path-to-regexp. Captured here so each express/router
* instance keeps the dialect it actually loaded.
* @param {((layer: object, original: Function, guardRepeatedNext?: boolean) => Function) | undefined}
* [wrapLegacyHandle]
* Fallback that replaces `layer.handle` for hosts without a `Layer` prototype
* dispatch (express <4.6.0). Omitted for hosts that always ship one.
* @returns {(original: Function) => Function}
*/
function createWrapRouterMethod (name, compile, wrapLegacyHandle) {
const routeAddedChannel = channel(`apm:${name}:route:added`)
function wrapStack (layers, matchers) {
for (const layer of layers) {
annotateLayer(layer, matchers)
if (wrapLegacyHandle !== undefined && !hasLayerDispatch(layer)) {
if (layer.__handle) { // express-async-errors
layer.__handle = wrapLegacyHandle(layer, layer.__handle, true)
} else {
layer.handle = wrapLegacyHandle(layer, layer.handle)
}
}
if (layer.route) {
for (const method of METHODS) {
if (typeof layer.route.stack === 'function') {
layer.route.stack = [{ handle: layer.route.stack }]
}
layer.route[method] = wrapMethod(layer.route[method])
}
}
}
}
function extractMatchers (fn) {
const arg = Array.isArray(fn) ? fn.flat(Infinity) : [fn]
if (typeof arg[0] === 'function') {
return []
}
if (arg.length === 1) {
const pattern = arg[0]
const path = pattern instanceof RegExp ? `(${pattern})` : pattern
const matchers = [{ path }]
matchers.hasStarPath = path === '*'
matchers.hasSlashPath = path === '/'
return matchers
}
// hasStarPath/hasSlashPath cache the lookups isFastStar/isFastSlash
// would otherwise re-run on every request.
let hasStarPath = false
let hasSlashPath = false
const matchers = arg.map(pattern => {
const isRegExp = pattern instanceof RegExp
const path = isRegExp ? `(${pattern})` : pattern
if (path === '*') {
hasStarPath = true
} else if (path === '/') {
hasSlashPath = true
}
return {
path,
regex: isRegExp ? pattern : compile?.(pattern),
}
})
matchers.hasStarPath = hasStarPath
matchers.hasSlashPath = hasSlashPath
return matchers
}
function wrapMethod (original) {
return shimmer.wrapFunction(original, original => function methodWithTrace (...args) {
let offset = 0
if (this.stack) {
offset = Array.isArray(this.stack) ? this.stack.length : 1
}
const router = original.apply(this, args)
if (typeof this.stack === 'function') {
this.stack = [{ handle: this.stack }]
}
if (routeAddedChannel.hasSubscribers) {
routeAddedChannel.publish({ topOfStackFunc: methodWithTrace, layer: this.stack?.at(-1) })
}
const fn = args[0]
// Publish only if this router was mounted by app.use() (prevents early '/sub/...')
if (routeAddedChannel.hasSubscribers && isAppMounted(this) && this.stack?.length > offset) {
// Handle nested router mounting for 'use' method
if (original.name === 'use' && args.length >= 2) {
const { mountPaths, startIdx } = extractMountPaths(fn)
if (mountPaths.length) {
const parentPaths = getRouterMountPaths(this)
for (let i = startIdx; i < args.length; i++) {
const nestedRouter = args[i]
if (!nestedRouter || typeof nestedRouter !== 'function') continue
for (const parentPath of parentPaths) {
for (const normalizedMountPath of mountPaths) {
const fullMountPath = joinPath(parentPath, normalizedMountPath)
if (fullMountPath === null) continue
setRouterMountPath(nestedRouter, fullMountPath)
collectRoutesFromRouter(nestedRouter, fullMountPath)
}
}
}
}
}
const mountPaths = getRouterMountPaths(this)
if (mountPaths.length) {
const layer = this.stack.at(-1)
if (layer?.route) {
const route = layer.route
const fullPaths = mountPaths.flatMap(mountPath => getRouteFullPaths(route, mountPath))
wrapRouteMethodsAndPublish(route, fullPaths, (payload) => {
routeAddedChannel.publish(payload)
})
}
}
}
if (this.stack?.length > offset) {
wrapStack(this.stack.slice(offset), extractMatchers(fn))
}
return router
})
}
return wrapMethod
}
addHook({ name: 'router', versions: ['>=1 <2'] }, Router => {
const wrapRouterMethod = createWrapRouterMethod('router', getCompileToRegexp())
shimmer.wrap(Router.prototype, 'use', wrapRouterMethod)
shimmer.wrap(Router.prototype, 'route', wrapRouterMethod)
return Router
})
addHook({ name: 'router', file: 'lib/layer.js', versions: ['>=1 <2'] }, Layer => {
const { wrapLayerRequest, wrapLayerError } = createLayerDispatchWrappers('router')
shimmer.wrap(Layer.prototype, 'handle_request', wrapLayerRequest)
shimmer.wrap(Layer.prototype, 'handle_error', wrapLayerError)
return Layer
})
const queryParserReadCh = channel('datadog:query:read:finish')
addHook({ name: 'router', versions: ['>=2'] }, Router => {
const wrapRouterMethod = createWrapRouterMethod('router', getCompileToRegexp())
const WrappedRouter = shimmer.wrapFunction(Router, function (originalRouter) {
return function wrappedMethod (...args) {
const router = originalRouter.apply(this, args)
shimmer.wrap(router, 'handle', function wrapHandle (originalHandle) {
return function wrappedHandle (req, res, next) {
if (queryParserReadCh.hasSubscribers && req) {
const abortController = new AbortController()
queryParserReadCh.publish({ req, res, query: req.query, abortController })
if (abortController.signal.aborted) return
}
return originalHandle.call(this, req, res, next)
}
})
return router
}
})
shimmer.wrap(WrappedRouter.prototype, 'use', wrapRouterMethod)
shimmer.wrap(WrappedRouter.prototype, 'route', wrapRouterMethod)
return WrappedRouter
})
const routerParamStartCh = channel('datadog:router:param:start')
const visitedParams = new WeakSet()
function wrapHandleRequest (original) {
return function wrappedHandleRequest (...args) {
const req = args[0]
// eslint-disable-next-line no-restricted-syntax -- arbitrary param names; publishing {} sets a WAF address
if (routerParamStartCh.hasSubscribers && !visitedParams.has(req.params) && Object.keys(req.params).length) {
visitedParams.add(req.params)
const abortController = new AbortController()
routerParamStartCh.publish({
req,
res: args[1],
params: req?.params,
abortController,
})
if (abortController.signal.aborted) return
}
return Reflect.apply(original, this, args)
}
}
addHook({
name: 'router', file: 'lib/layer.js', versions: ['>=2'],
}, Layer => {
const { wrapLayerRequest, wrapLayerError } = createLayerDispatchWrappers('router')
// `handleRequest` carries two concerns: the middleware dispatch span and the
// param-start publish (`wrapHandleRequest`). Wrap the dispatch first so it
// sits inner and param-start still fires before `middleware:enter`, matching
// the order from when the handle itself was wrapped.
shimmer.wrap(Layer.prototype, 'handleRequest', wrapLayerRequest)
shimmer.wrap(Layer.prototype, 'handleError', wrapLayerError)
shimmer.wrap(Layer.prototype, 'handleRequest', wrapHandleRequest)
return Layer
})
function wrapParam (original) {
return function wrappedProcessParams (...args) {
args[1] = shimmer.wrapFunction(args[1], (originalFn) => {
return function wrappedFn (...fnArgs) {
const req = fnArgs[0]
// eslint-disable-next-line no-restricted-syntax -- arbitrary param names; publishing {} sets a WAF address
if (routerParamStartCh.hasSubscribers && Object.keys(req.params).length && !visitedParams.has(req.params)) {
visitedParams.add(req.params)
const abortController = new AbortController()
routerParamStartCh.publish({
req,
res: fnArgs[1],
params: req?.params,
abortController,
})
if (abortController.signal.aborted) return
}
return Reflect.apply(originalFn, this, fnArgs)
}
})
return original.apply(this, args)
}
}
addHook({
name: 'router', versions: ['>=2'],
}, router => {
shimmer.wrap(router.prototype, 'param', wrapParam)
return router
})
module.exports = { createWrapRouterMethod, createLayerDispatchWrappers }