@zuplo/cli 7.15.16 → 7.15.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@hono/node-server/dist/index.cjs +16 -4
- package/node_modules/@hono/node-server/dist/index.mjs +16 -4
- package/node_modules/@hono/node-server/package.json +1 -1
- package/node_modules/@zuplo/core/package.json +1 -1
- package/node_modules/@zuplo/graphql/package.json +1 -1
- package/node_modules/@zuplo/openapi-tools/package.json +1 -1
- package/node_modules/@zuplo/otel/out/esm/{chunk-6YAK7LGJ.js → chunk-BJ3PRHGL.js} +2 -2
- package/node_modules/@zuplo/otel/out/esm/chunk-BJ3PRHGL.js.map +1 -0
- package/node_modules/@zuplo/otel/out/esm/index.js +1 -1
- package/node_modules/@zuplo/otel/out/types/index.d.ts +10 -0
- package/node_modules/@zuplo/otel/package.json +1 -1
- package/node_modules/@zuplo/runtime/out/esm/{chunk-HMKEMARF.js → chunk-6KHAYHSW.js} +76 -78
- package/node_modules/@zuplo/runtime/out/esm/chunk-6KHAYHSW.js.map +1 -0
- package/node_modules/@zuplo/runtime/out/esm/index.js +1 -1
- package/node_modules/@zuplo/runtime/out/esm/mcp-gateway/index.js +1 -1
- package/node_modules/@zuplo/runtime/out/esm/mocks/index.js +1 -1
- package/node_modules/@zuplo/runtime/package.json +1 -1
- package/node_modules/router/HISTORY.md +67 -52
- package/node_modules/router/README.md +222 -2
- package/node_modules/router/index.d.ts +188 -0
- package/node_modules/router/index.js +86 -50
- package/node_modules/router/lib/layer.js +154 -47
- package/node_modules/router/lib/param.js +47 -0
- package/node_modules/router/lib/route.d.ts +118 -0
- package/node_modules/router/lib/route.js +14 -8
- package/node_modules/router/lib/symbols.js +4 -0
- package/node_modules/router/lib/tracing.js +173 -0
- package/node_modules/router/package.json +24 -9
- package/package.json +11 -11
- package/node_modules/@zuplo/otel/out/esm/chunk-6YAK7LGJ.js.map +0 -1
- package/node_modules/@zuplo/runtime/out/esm/chunk-HMKEMARF.js.map +0 -1
- /package/node_modules/@zuplo/runtime/out/esm/{chunk-HMKEMARF.js.LEGAL.txt → chunk-6KHAYHSW.js.LEGAL.txt} +0 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
2
|
+
import type { MatchFunction } from 'path-to-regexp'
|
|
3
|
+
import RouteModule = require('./lib/route')
|
|
4
|
+
|
|
5
|
+
declare const Router: Router.RouterConstructor
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare -- Merge types with the callable and constructable CommonJS export.
|
|
8
|
+
declare namespace Router {
|
|
9
|
+
/**
|
|
10
|
+
* Continues dispatch, forwards an error, or skips the current route or router.
|
|
11
|
+
* Use `next('route')` to skip a route and `next('router')` to exit the router.
|
|
12
|
+
*/
|
|
13
|
+
type NextFunction = RouteModule.NextFunction
|
|
14
|
+
type Params = Record<string, string | string[] | undefined>
|
|
15
|
+
|
|
16
|
+
/** Can be augmented to describe properties added by middleware. */
|
|
17
|
+
interface Request<P = Params> extends IncomingMessage {
|
|
18
|
+
params: P
|
|
19
|
+
baseUrl: string
|
|
20
|
+
originalUrl: string
|
|
21
|
+
route?: Route
|
|
22
|
+
next?: NextFunction
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Can be augmented to describe properties and methods added by middleware. */
|
|
26
|
+
interface Response extends ServerResponse {}
|
|
27
|
+
|
|
28
|
+
type RequestHandler<P = Params> = RouteModule.RequestHandler<P>
|
|
29
|
+
type ErrorRequestHandler<P = Params> = RouteModule.ErrorRequestHandler<P>
|
|
30
|
+
/**
|
|
31
|
+
* Processes a matched path parameter before the layer's handlers run.
|
|
32
|
+
* Call `next()` to continue, or respond to the request.
|
|
33
|
+
* Rejected promises are forwarded as errors; resolved promises do not call `next()`.
|
|
34
|
+
*
|
|
35
|
+
* @param value The matched parameter value.
|
|
36
|
+
* @param name The parameter name supplied to `router.param()`.
|
|
37
|
+
*/
|
|
38
|
+
type RequestParamHandler = (req: Request, res: Response, next: NextFunction, value: any, name: string) => void
|
|
39
|
+
type Handler<P = Params> = RouteModule.Handler<P>
|
|
40
|
+
type HandlerArg<F extends Handler<any> = Handler> = RouteModule.HandlerArg<F>
|
|
41
|
+
type Method = RouteModule.Method
|
|
42
|
+
type Path = RouteModule.Path
|
|
43
|
+
type Route<R extends Path = Path> = RouteModule.Route<R>
|
|
44
|
+
type RouteParameters<R extends Path> = RouteModule.RouteParameters<R>
|
|
45
|
+
|
|
46
|
+
/** A mounted router may come from a version without route introspection. */
|
|
47
|
+
interface MountedRouter {
|
|
48
|
+
(req: IncomingMessage, res: ServerResponse, next: NextFunction): unknown
|
|
49
|
+
stack: unknown[]
|
|
50
|
+
listRoutes?(): RouteInfo[]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One registered path, with either route methods or a mounted router. */
|
|
54
|
+
type RouteInfo = {
|
|
55
|
+
path: string | RegExp
|
|
56
|
+
} & ({
|
|
57
|
+
/** Uppercase methods and `'*'` for `.all()`, or an empty array for a route without handlers. */
|
|
58
|
+
methods: string[]
|
|
59
|
+
router: undefined
|
|
60
|
+
} | {
|
|
61
|
+
methods: undefined
|
|
62
|
+
router: MountedRouter
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/** The result of one internal layer match, separate from other requests. */
|
|
66
|
+
interface LayerMatch {
|
|
67
|
+
params: Params
|
|
68
|
+
path: string
|
|
69
|
+
keys: string[]
|
|
70
|
+
pattern: string | RegExp
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Internal layer metadata. Its shape may change between releases. */
|
|
74
|
+
interface Layer {
|
|
75
|
+
handle: Handler<any>
|
|
76
|
+
name: string
|
|
77
|
+
/** The registered path, with path arrays flattened and copied at registration. */
|
|
78
|
+
rawPath: string | RegExp | Array<string | RegExp>
|
|
79
|
+
slash: boolean
|
|
80
|
+
route?: Route | undefined
|
|
81
|
+
method?: Method | undefined
|
|
82
|
+
matchers: Array<MatchFunction<Params>>
|
|
83
|
+
match(path: string | null | undefined): LayerMatch | false
|
|
84
|
+
handleRequest(req: Request<any>, res: Response, next: NextFunction): void
|
|
85
|
+
handleError(error: any, req: Request<any>, res: Response, next: NextFunction): void
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
type RouterMethod<T> = {
|
|
89
|
+
<R extends string | RegExp, P = RouteParameters<R>>(path: R, handler: HandlerArg<RequestHandler<P>>, ...handlers: Array<HandlerArg<RequestHandler<P>>>): T
|
|
90
|
+
<R extends string | RegExp, P = RouteParameters<R>>(path: R, handler: HandlerArg<Handler<P>>, ...handlers: Array<HandlerArg<Handler<P>>>): T
|
|
91
|
+
<F extends Handler<any> = RequestHandler>(path: Path, handler: HandlerArg<F>, ...handlers: Array<HandlerArg<F>>): T
|
|
92
|
+
<P = Params>(path: Path, handler: HandlerArg<RequestHandler<P>>, ...handlers: Array<HandlerArg<RequestHandler<P>>>): T
|
|
93
|
+
<P = Params>(path: Path, handler: HandlerArg<Handler<P>>, ...handlers: Array<HandlerArg<Handler<P>>>): T
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface RouterOptions {
|
|
97
|
+
caseSensitive?: boolean
|
|
98
|
+
/**
|
|
99
|
+
* Preserve parent `req.params` when matching this router's layers.
|
|
100
|
+
* This router's named parameters take precedence over parent parameters.
|
|
101
|
+
* Parent parameter names are not inferred from the child route's path;
|
|
102
|
+
* include them in an explicit parameter type when needed.
|
|
103
|
+
* @default false
|
|
104
|
+
*/
|
|
105
|
+
mergeParams?: boolean
|
|
106
|
+
strict?: boolean
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type MethodHandlers<T> = { [M in Method | 'all']: RouterMethod<T> }
|
|
110
|
+
|
|
111
|
+
interface Router {
|
|
112
|
+
(req: IncomingMessage, res: ServerResponse, callback: NextFunction): void
|
|
113
|
+
|
|
114
|
+
params: Record<string, any>
|
|
115
|
+
stack: Layer[]
|
|
116
|
+
caseSensitive: boolean | undefined
|
|
117
|
+
/** Whether to preserve parent `req.params`. Disabled when false or undefined. */
|
|
118
|
+
mergeParams: boolean | undefined
|
|
119
|
+
strict: boolean | undefined
|
|
120
|
+
|
|
121
|
+
handle(req: IncomingMessage, res: ServerResponse, callback: NextFunction): void
|
|
122
|
+
/**
|
|
123
|
+
* Register a callback for a named path parameter on this router.
|
|
124
|
+
* Callbacks run in registration order before the matching layer's handlers.
|
|
125
|
+
* They are local to this router and are not inherited by mounted routers.
|
|
126
|
+
*
|
|
127
|
+
* @param name The parameter name, without the `:` or `*` prefix.
|
|
128
|
+
* @param fn Receives `(req, res, next, value, name)`.
|
|
129
|
+
* @returns This router for chaining.
|
|
130
|
+
*/
|
|
131
|
+
param(name: string, fn: RequestParamHandler): this
|
|
132
|
+
route<R extends Path>(path: R): Route<R>
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* List routes and mounted routers in registration order, one entry per path.
|
|
136
|
+
* Methods include `'*'` for `.all()` and implicit `HEAD` for `GET` routes.
|
|
137
|
+
* Plain middleware is omitted; mounted routers are not traversed recursively.
|
|
138
|
+
*/
|
|
139
|
+
listRoutes(): RouteInfo[]
|
|
140
|
+
|
|
141
|
+
use: RouterMethod<this> & RouteModule.RouteMethod<this>
|
|
142
|
+
|
|
143
|
+
all: RouterMethod<this>
|
|
144
|
+
acl: RouterMethod<this>
|
|
145
|
+
bind: RouterMethod<this>
|
|
146
|
+
checkout: RouterMethod<this>
|
|
147
|
+
connect: RouterMethod<this>
|
|
148
|
+
copy: RouterMethod<this>
|
|
149
|
+
delete: RouterMethod<this>
|
|
150
|
+
get: RouterMethod<this>
|
|
151
|
+
head: RouterMethod<this>
|
|
152
|
+
link: RouterMethod<this>
|
|
153
|
+
lock: RouterMethod<this>
|
|
154
|
+
'm-search': RouterMethod<this>
|
|
155
|
+
merge: RouterMethod<this>
|
|
156
|
+
mkactivity: RouterMethod<this>
|
|
157
|
+
mkcalendar: RouterMethod<this>
|
|
158
|
+
mkcol: RouterMethod<this>
|
|
159
|
+
move: RouterMethod<this>
|
|
160
|
+
notify: RouterMethod<this>
|
|
161
|
+
options: RouterMethod<this>
|
|
162
|
+
patch: RouterMethod<this>
|
|
163
|
+
post: RouterMethod<this>
|
|
164
|
+
propfind: RouterMethod<this>
|
|
165
|
+
proppatch: RouterMethod<this>
|
|
166
|
+
purge: RouterMethod<this>
|
|
167
|
+
put: RouterMethod<this>
|
|
168
|
+
query: RouterMethod<this>
|
|
169
|
+
rebind: RouterMethod<this>
|
|
170
|
+
report: RouterMethod<this>
|
|
171
|
+
search: RouterMethod<this>
|
|
172
|
+
source: RouterMethod<this>
|
|
173
|
+
subscribe: RouterMethod<this>
|
|
174
|
+
trace: RouterMethod<this>
|
|
175
|
+
unbind: RouterMethod<this>
|
|
176
|
+
unlink: RouterMethod<this>
|
|
177
|
+
unlock: RouterMethod<this>
|
|
178
|
+
unsubscribe: RouterMethod<this>
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface RouterConstructor {
|
|
182
|
+
(options?: RouterOptions): Router
|
|
183
|
+
new (options?: RouterOptions): Router
|
|
184
|
+
Route: typeof RouteModule
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export = Router
|
|
@@ -12,13 +12,14 @@
|
|
|
12
12
|
* @private
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const callParam = require('./lib/param')
|
|
16
16
|
const Layer = require('./lib/layer')
|
|
17
|
+
const { matchedRoute } = require('./lib/symbols')
|
|
18
|
+
const { recordError, requestChannel, shouldTrace, traceParam } = require('./lib/tracing')
|
|
17
19
|
const { METHODS } = require('node:http')
|
|
18
20
|
const parseUrl = require('parseurl')
|
|
19
21
|
const Route = require('./lib/route')
|
|
20
22
|
const debug = require('debug')('router')
|
|
21
|
-
const deprecate = require('depd')('router')
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Module variables.
|
|
@@ -168,7 +169,7 @@ Router.prototype.handle = function handle (req, res, callback) {
|
|
|
168
169
|
// manage inter-router variables
|
|
169
170
|
const parentParams = req.params
|
|
170
171
|
const parentUrl = req.baseUrl || ''
|
|
171
|
-
let done = restore(callback, req, 'baseUrl', 'next', 'params')
|
|
172
|
+
let done = restore(callback, req, 'baseUrl', 'next', 'params', matchedRoute)
|
|
172
173
|
|
|
173
174
|
// setup next layer
|
|
174
175
|
req.next = next
|
|
@@ -186,13 +187,21 @@ Router.prototype.handle = function handle (req, res, callback) {
|
|
|
186
187
|
next()
|
|
187
188
|
|
|
188
189
|
function next (err) {
|
|
190
|
+
// The error has already left its originating callback. Remember it even
|
|
191
|
+
// when that callback was not traced, so enclosing routers cannot claim it.
|
|
192
|
+
if (err) recordError(req, err)
|
|
193
|
+
|
|
189
194
|
let layerError = err === 'route'
|
|
190
195
|
? null
|
|
191
196
|
: err
|
|
192
197
|
|
|
193
|
-
// remove added slash
|
|
198
|
+
// remove added slash, unless the layer rewrote the path under a mount
|
|
199
|
+
// path that has no trailing slash
|
|
194
200
|
if (slashAdded) {
|
|
195
|
-
req.url
|
|
201
|
+
const pathIsRoot = req.url === '/' || req.url.startsWith('/?')
|
|
202
|
+
if (pathIsRoot || (removed.endsWith('/') && req.url[0] === '/')) {
|
|
203
|
+
req.url = req.url.slice(1)
|
|
204
|
+
}
|
|
196
205
|
slashAdded = false
|
|
197
206
|
}
|
|
198
207
|
|
|
@@ -232,17 +241,22 @@ Router.prototype.handle = function handle (req, res, callback) {
|
|
|
232
241
|
let match
|
|
233
242
|
let route
|
|
234
243
|
|
|
235
|
-
while (match
|
|
244
|
+
while (!match && idx < stack.length) {
|
|
236
245
|
layer = stack[idx++]
|
|
237
|
-
match = matchLayer(layer, path)
|
|
238
246
|
route = layer.route
|
|
239
247
|
|
|
240
|
-
|
|
248
|
+
try {
|
|
249
|
+
match = layer.match(path)
|
|
250
|
+
} catch (err) {
|
|
251
|
+
// Matching can fail before a handler runs (for example, URI decoding).
|
|
252
|
+
// Forwarding that error must not attribute it to a later handler.
|
|
253
|
+
recordError(req, err)
|
|
241
254
|
// hold on to layerError
|
|
242
|
-
layerError = layerError ||
|
|
255
|
+
layerError = layerError || err
|
|
256
|
+
match = false
|
|
243
257
|
}
|
|
244
258
|
|
|
245
|
-
if (match
|
|
259
|
+
if (!match) {
|
|
246
260
|
continue
|
|
247
261
|
}
|
|
248
262
|
|
|
@@ -272,7 +286,7 @@ Router.prototype.handle = function handle (req, res, callback) {
|
|
|
272
286
|
}
|
|
273
287
|
|
|
274
288
|
// no match
|
|
275
|
-
if (match
|
|
289
|
+
if (!match) {
|
|
276
290
|
return done(layerError)
|
|
277
291
|
}
|
|
278
292
|
|
|
@@ -281,20 +295,20 @@ Router.prototype.handle = function handle (req, res, callback) {
|
|
|
281
295
|
req.route = route
|
|
282
296
|
}
|
|
283
297
|
|
|
284
|
-
//
|
|
298
|
+
// Keep matched values local to this request before param callbacks can yield.
|
|
299
|
+
req[matchedRoute] = match.pattern
|
|
285
300
|
req.params = self.mergeParams
|
|
286
|
-
? mergeParams(
|
|
287
|
-
:
|
|
288
|
-
const layerPath = layer.path
|
|
301
|
+
? mergeParams(match.params, parentParams)
|
|
302
|
+
: match.params
|
|
289
303
|
|
|
290
304
|
// this should be done for the layer
|
|
291
|
-
processParams(self.params, layer, paramcalled, req, res, function (err) {
|
|
305
|
+
processParams(self.params, layer, match.keys, paramcalled, req, res, function (err) {
|
|
292
306
|
if (err) {
|
|
293
307
|
next(layerError || err)
|
|
294
308
|
} else if (route) {
|
|
295
309
|
layer.handleRequest(req, res, next)
|
|
296
310
|
} else {
|
|
297
|
-
trimPrefix(layer, layerError,
|
|
311
|
+
trimPrefix(layer, layerError, match.path, path)
|
|
298
312
|
}
|
|
299
313
|
|
|
300
314
|
sync = 0
|
|
@@ -441,6 +455,48 @@ Router.prototype.route = function route (path) {
|
|
|
441
455
|
return route
|
|
442
456
|
}
|
|
443
457
|
|
|
458
|
+
/**
|
|
459
|
+
* List the routes and mounted routers registered on this router.
|
|
460
|
+
*
|
|
461
|
+
* Returns one `{ path, methods, router }` object per registered path.
|
|
462
|
+
* `methods` lists registered HTTP methods in first registration order,
|
|
463
|
+
* with `'*'` representing `.all()` handlers. The automatic `HEAD` for
|
|
464
|
+
* `GET` routes is appended unless explicitly registered. It is `undefined`
|
|
465
|
+
* for mounted routers. Plain middleware is not listed.
|
|
466
|
+
* `router` is the mounted router instance for
|
|
467
|
+
* `.use(path, router)` layers, so consumers can recurse by calling
|
|
468
|
+
* `router.listRoutes()` themselves when it is available.
|
|
469
|
+
*
|
|
470
|
+
* @return {Array<{path: string|RegExp, methods: Array<string>|undefined, router: Router|undefined}>}
|
|
471
|
+
* @public
|
|
472
|
+
*/
|
|
473
|
+
Router.prototype.listRoutes = function listRoutes () {
|
|
474
|
+
const routes = []
|
|
475
|
+
|
|
476
|
+
for (const layer of this.stack) {
|
|
477
|
+
const route = layer.route
|
|
478
|
+
const router = !route && Array.isArray(layer.handle.stack)
|
|
479
|
+
? layer.handle
|
|
480
|
+
: undefined
|
|
481
|
+
|
|
482
|
+
if (!route && !router) {
|
|
483
|
+
continue
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const methods = route ? route._methods(true) : undefined
|
|
487
|
+
|
|
488
|
+
if (Array.isArray(layer.rawPath)) {
|
|
489
|
+
for (const path of layer.rawPath) {
|
|
490
|
+
routes.push({ path, methods: methods && methods.slice(), router })
|
|
491
|
+
}
|
|
492
|
+
} else {
|
|
493
|
+
routes.push({ path: layer.rawPath, methods, router })
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return routes
|
|
498
|
+
}
|
|
499
|
+
|
|
444
500
|
// create Router#VERB functions
|
|
445
501
|
methods.concat('all').forEach(function (method) {
|
|
446
502
|
Router.prototype[method] = function (path) {
|
|
@@ -506,22 +562,6 @@ function getProtohost (url) {
|
|
|
506
562
|
: undefined
|
|
507
563
|
}
|
|
508
564
|
|
|
509
|
-
/**
|
|
510
|
-
* Match path to a layer.
|
|
511
|
-
*
|
|
512
|
-
* @param {Layer} layer
|
|
513
|
-
* @param {string} path
|
|
514
|
-
* @private
|
|
515
|
-
*/
|
|
516
|
-
|
|
517
|
-
function matchLayer (layer, path) {
|
|
518
|
-
try {
|
|
519
|
-
return layer.match(path)
|
|
520
|
-
} catch (err) {
|
|
521
|
-
return err
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
565
|
/**
|
|
526
566
|
* Merge params with parent params
|
|
527
567
|
*
|
|
@@ -573,10 +613,7 @@ function mergeParams (params, parent) {
|
|
|
573
613
|
* @private
|
|
574
614
|
*/
|
|
575
615
|
|
|
576
|
-
function processParams (params, layer, called, req, res, done) {
|
|
577
|
-
// captured parameters from the layer, keys and values
|
|
578
|
-
const keys = layer.keys
|
|
579
|
-
|
|
616
|
+
function processParams (params, layer, keys, called, req, res, done) {
|
|
580
617
|
// fast track
|
|
581
618
|
if (!keys || keys.length === 0) {
|
|
582
619
|
return done()
|
|
@@ -645,20 +682,19 @@ function processParams (params, layer, called, req, res, done) {
|
|
|
645
682
|
|
|
646
683
|
if (!fn) return param()
|
|
647
684
|
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
}
|
|
659
|
-
} catch (e) {
|
|
660
|
-
paramCallback(e)
|
|
685
|
+
if (shouldTrace(requestChannel)) {
|
|
686
|
+
traceParam(fn, {
|
|
687
|
+
req,
|
|
688
|
+
res,
|
|
689
|
+
layer,
|
|
690
|
+
handler: fn,
|
|
691
|
+
route: req[matchedRoute],
|
|
692
|
+
param: key
|
|
693
|
+
}, paramCallback, paramVal, key)
|
|
694
|
+
return
|
|
661
695
|
}
|
|
696
|
+
|
|
697
|
+
callParam(fn, req, res, paramCallback, paramVal, key)
|
|
662
698
|
}
|
|
663
699
|
|
|
664
700
|
param()
|
|
@@ -16,6 +16,8 @@ const isPromise = require('is-promise')
|
|
|
16
16
|
const pathRegexp = require('path-to-regexp')
|
|
17
17
|
const debug = require('debug')('router:layer')
|
|
18
18
|
const deprecate = require('depd')('router')
|
|
19
|
+
const { matchedRoute } = require('./symbols')
|
|
20
|
+
const { publishEnd, recordThrown, requestChannel, shouldTrace, traceAsyncEnd, wrapNext } = require('./tracing')
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* Module variables.
|
|
@@ -40,10 +42,9 @@ function Layer (path, options, fn) {
|
|
|
40
42
|
const opts = options || {}
|
|
41
43
|
|
|
42
44
|
this.handle = fn
|
|
43
|
-
this.keys = []
|
|
44
45
|
this.name = fn.name || '<anonymous>'
|
|
45
|
-
|
|
46
|
-
this.
|
|
46
|
+
// flat unnests nested path arrays and snapshots the caller's array
|
|
47
|
+
this.rawPath = Array.isArray(path) ? path.flat(Infinity) : path
|
|
47
48
|
this.slash = path === '/' && opts.end === false
|
|
48
49
|
|
|
49
50
|
function matcher (_path) {
|
|
@@ -90,7 +91,7 @@ function Layer (path, options, fn) {
|
|
|
90
91
|
decode: decodeParam
|
|
91
92
|
})
|
|
92
93
|
}
|
|
93
|
-
this.matchers = Array.isArray(
|
|
94
|
+
this.matchers = Array.isArray(this.rawPath) ? this.rawPath.map(matcher) : [matcher(path)]
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
/**
|
|
@@ -111,23 +112,34 @@ Layer.prototype.handleError = function handleError (error, req, res, next) {
|
|
|
111
112
|
return next(error)
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
if (!shouldTrace(requestChannel)) {
|
|
116
|
+
try {
|
|
117
|
+
// invoke function
|
|
118
|
+
const ret = handlePromise(fn(error, req, res, next))
|
|
117
119
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
// wait for returned promise
|
|
121
|
+
if (ret) {
|
|
122
|
+
ret.then(noop, function (error) {
|
|
123
|
+
next(error || new Error('Rejected promise'))
|
|
124
|
+
})
|
|
122
125
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
next(error || new Error('Rejected promise'))
|
|
126
|
-
})
|
|
126
|
+
} catch (err) {
|
|
127
|
+
next(err)
|
|
127
128
|
}
|
|
128
|
-
|
|
129
|
-
next(err)
|
|
129
|
+
return
|
|
130
130
|
}
|
|
131
|
+
|
|
132
|
+
invokeWithTrace(function (wrappedNext) {
|
|
133
|
+
return fn(error, req, res, wrappedNext)
|
|
134
|
+
}, {
|
|
135
|
+
req,
|
|
136
|
+
res,
|
|
137
|
+
incomingError: error,
|
|
138
|
+
layer: this,
|
|
139
|
+
handler: fn,
|
|
140
|
+
route: req[matchedRoute],
|
|
141
|
+
errorHandler: true
|
|
142
|
+
}, next)
|
|
131
143
|
}
|
|
132
144
|
|
|
133
145
|
/**
|
|
@@ -147,46 +159,143 @@ Layer.prototype.handleRequest = function handleRequest (req, res, next) {
|
|
|
147
159
|
return next()
|
|
148
160
|
}
|
|
149
161
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
162
|
+
// Skip tracing for route dispatch wrappers (this.route is only set on the
|
|
163
|
+
// internal layer that calls route.dispatch); the user handlers inside the
|
|
164
|
+
// route are traced individually. Also skip when there are no subscribers,
|
|
165
|
+
// to avoid allocating a context object on every request.
|
|
166
|
+
if (this.route || !shouldTrace(requestChannel)) {
|
|
167
|
+
try {
|
|
168
|
+
// invoke function
|
|
169
|
+
const ret = handlePromise(fn(req, res, next))
|
|
170
|
+
|
|
171
|
+
// wait for returned promise
|
|
172
|
+
if (ret) {
|
|
173
|
+
ret.then(noop, function (error) {
|
|
174
|
+
next(error || new Error('Rejected promise'))
|
|
175
|
+
})
|
|
158
176
|
}
|
|
177
|
+
} catch (err) {
|
|
178
|
+
next(err)
|
|
179
|
+
}
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
invokeWithTrace(function (wrappedNext) {
|
|
184
|
+
return fn(req, res, wrappedNext)
|
|
185
|
+
}, {
|
|
186
|
+
req,
|
|
187
|
+
res,
|
|
188
|
+
layer: this,
|
|
189
|
+
handler: fn,
|
|
190
|
+
route: req[matchedRoute]
|
|
191
|
+
}, next)
|
|
192
|
+
}
|
|
159
193
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Invoke a handler wrapped in the request TracingChannel. Only called when the
|
|
196
|
+
* channel has subscribers, so ctx is always present.
|
|
197
|
+
* @private
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
function invokeWithTrace (exec, ctx, next) {
|
|
201
|
+
const wrappedNext = wrapNext(ctx, next)
|
|
202
|
+
let settled = false
|
|
203
|
+
|
|
204
|
+
// runStores fires `start` and sets the async context. Synchronous layers
|
|
205
|
+
// emit only start/end. All outcomes flow through wrappedNext to keep signal
|
|
206
|
+
// filtering and dedup in one place.
|
|
207
|
+
requestChannel.start.runStores(ctx, function () {
|
|
208
|
+
let out
|
|
209
|
+
try {
|
|
210
|
+
const ret = exec(wrappedNext)
|
|
211
|
+
out = handlePromise(ret)
|
|
212
|
+
// Same as tracePromise, a promise only gets its result once it resolves
|
|
213
|
+
if (!out) ctx.result = ret
|
|
214
|
+
} catch (err) {
|
|
215
|
+
// A sync throw is forwarded verbatim, so a falsy value keeps routing
|
|
216
|
+
// exactly as the untraced path does.
|
|
217
|
+
wrappedNext(err)
|
|
218
|
+
recordThrown(ctx, err, true)
|
|
219
|
+
publishEnd(ctx, false)
|
|
220
|
+
return
|
|
163
221
|
}
|
|
164
|
-
|
|
165
|
-
|
|
222
|
+
|
|
223
|
+
publishEnd(ctx, Boolean(out))
|
|
224
|
+
if (!out) return
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
out.then(
|
|
228
|
+
function (value) {
|
|
229
|
+
// Same as tracePromise, only the first outcome settles the trace
|
|
230
|
+
if (settled) return
|
|
231
|
+
settled = true
|
|
232
|
+
ctx.result = value
|
|
233
|
+
traceAsyncEnd(ctx)
|
|
234
|
+
},
|
|
235
|
+
function (err) {
|
|
236
|
+
// A rejected promise is an error even when the reason is falsy;
|
|
237
|
+
// normalize it to the error the router forwards to next().
|
|
238
|
+
rejected(err || new Error('Rejected promise'))
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
} catch (err) {
|
|
242
|
+
// Same as tracePromise, a `then` that throws is a rejection, forwarded
|
|
243
|
+
// verbatim as the untraced path does
|
|
244
|
+
rejected(err)
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
function rejected (err) {
|
|
249
|
+
// Routing still gets every outcome, as the untraced path does
|
|
250
|
+
wrappedNext(err)
|
|
251
|
+
if (settled) return
|
|
252
|
+
settled = true
|
|
253
|
+
recordThrown(ctx, err, false)
|
|
254
|
+
traceAsyncEnd(ctx)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Passed to then() instead of null, since some thenables call their callbacks
|
|
260
|
+
* without checking, and the traced path always passes a function.
|
|
261
|
+
*
|
|
262
|
+
* @private
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
function noop () {}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* If the return value is a promise, validate it and return it.
|
|
269
|
+
* @private
|
|
270
|
+
*/
|
|
271
|
+
|
|
272
|
+
function handlePromise (ret) {
|
|
273
|
+
if (isPromise(ret)) {
|
|
274
|
+
if (!(ret instanceof Promise)) {
|
|
275
|
+
deprecate('handlers that are Promise-like are deprecated, use a native Promise instead')
|
|
276
|
+
}
|
|
277
|
+
return ret
|
|
166
278
|
}
|
|
167
279
|
}
|
|
168
280
|
|
|
169
281
|
/**
|
|
170
|
-
*
|
|
171
|
-
* populate `.params`.
|
|
282
|
+
* Return the matched values without storing request state on the layer.
|
|
172
283
|
*
|
|
173
284
|
* @param {String} path
|
|
174
|
-
* @return {
|
|
285
|
+
* @return {Object|false}
|
|
175
286
|
* @api private
|
|
176
287
|
*/
|
|
177
288
|
|
|
178
289
|
Layer.prototype.match = function match (path) {
|
|
179
290
|
let match
|
|
291
|
+
let i = 0
|
|
180
292
|
|
|
181
293
|
if (path != null) {
|
|
182
294
|
// fast path non-ending match for / (any path matches)
|
|
183
295
|
if (this.slash) {
|
|
184
|
-
|
|
185
|
-
this.path = ''
|
|
186
|
-
return true
|
|
296
|
+
return { params: {}, path: '', keys: [], pattern: this.rawPath }
|
|
187
297
|
}
|
|
188
298
|
|
|
189
|
-
let i = 0
|
|
190
299
|
while (!match && i < this.matchers.length) {
|
|
191
300
|
// match the path
|
|
192
301
|
match = this.matchers[i](path)
|
|
@@ -195,17 +304,15 @@ Layer.prototype.match = function match (path) {
|
|
|
195
304
|
}
|
|
196
305
|
|
|
197
306
|
if (!match) {
|
|
198
|
-
this.params = undefined
|
|
199
|
-
this.path = undefined
|
|
200
307
|
return false
|
|
201
308
|
}
|
|
202
309
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
310
|
+
return {
|
|
311
|
+
params: match.params,
|
|
312
|
+
path: match.path,
|
|
313
|
+
keys: Object.keys(match.params),
|
|
314
|
+
pattern: Array.isArray(this.rawPath) ? this.rawPath[i - 1] : this.rawPath
|
|
315
|
+
}
|
|
209
316
|
}
|
|
210
317
|
|
|
211
318
|
/**
|
|
@@ -217,7 +324,7 @@ Layer.prototype.match = function match (path) {
|
|
|
217
324
|
*/
|
|
218
325
|
|
|
219
326
|
function decodeParam (val) {
|
|
220
|
-
if (typeof val !== 'string' || val.length === 0) {
|
|
327
|
+
if (typeof val !== 'string' || val.length === 0 || val.indexOf('%') === -1) {
|
|
221
328
|
return val
|
|
222
329
|
}
|
|
223
330
|
|
|
@@ -226,7 +333,7 @@ function decodeParam (val) {
|
|
|
226
333
|
} catch (err) {
|
|
227
334
|
if (err instanceof URIError) {
|
|
228
335
|
err.message = 'Failed to decode param \'' + val + '\''
|
|
229
|
-
err.status = 400
|
|
336
|
+
err.status = err.statusCode = 400
|
|
230
337
|
}
|
|
231
338
|
|
|
232
339
|
throw err
|