@zuplo/cli 7.15.17 → 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/package.json +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
|
@@ -18,6 +18,95 @@ This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
|
18
18
|
$ npm install router
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
+
## TypeScript
|
|
22
|
+
|
|
23
|
+
TypeScript declarations are included. To describe properties added by your
|
|
24
|
+
middleware, augment the `Request` and `Response` interfaces in the `router` module:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import Router from 'router'
|
|
28
|
+
|
|
29
|
+
declare module 'router' {
|
|
30
|
+
interface Request {
|
|
31
|
+
user?: { id: string }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Response {
|
|
35
|
+
requestId?: string
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const router = Router()
|
|
40
|
+
|
|
41
|
+
router.use((req, res, next) => {
|
|
42
|
+
req.user = { id: '123' }
|
|
43
|
+
res.requestId = 'request-123'
|
|
44
|
+
next()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
router.get('/me', (req, res) => {
|
|
48
|
+
if (res.requestId) res.setHeader('X-Request-Id', res.requestId)
|
|
49
|
+
res.end(req.user?.id)
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The augmentation applies to middleware, route handlers, error handlers, and
|
|
54
|
+
`router.param` callbacks. It only describes types; your middleware must assign
|
|
55
|
+
the property. Mark properties optional when they may not be present.
|
|
56
|
+
|
|
57
|
+
Literal route strings infer parameter names and values, following the approach
|
|
58
|
+
used by `@types/express-serve-static-core`:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
router.get('/users/:id', (req, res) => {
|
|
62
|
+
res.end(req.params.id.toUpperCase()) // id: string
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
router.get('/files{/*path}', (req, res) => {
|
|
66
|
+
res.end(req.params.path?.join('/')) // path: string[] | undefined
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Inference also applies to `router.use(path, ...)`, `router.route(path)`, and
|
|
71
|
+
`new Router.Route(path)`, including chained routing methods.
|
|
72
|
+
|
|
73
|
+
To declare route parameters explicitly, pass their type to a routing method or
|
|
74
|
+
to `Request`, `RequestHandler`, or `ErrorRequestHandler`:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
type UserParams = { id: string }
|
|
78
|
+
|
|
79
|
+
router.get<UserParams>('/users/:id', (req, res) => {
|
|
80
|
+
res.end(req.params.id.toUpperCase())
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const showUser: Router.RequestHandler<UserParams> = (req, res) => {
|
|
84
|
+
res.end(req.params.id)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
router.route('/users/:id').get<UserParams>(showUser)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Dynamic strings, regular expressions, and path arrays keep the default parameter
|
|
91
|
+
values of `string | string[] | undefined` unless you supply an explicit type.
|
|
92
|
+
Use explicit types for syntax the inference does not cover, such as quoted
|
|
93
|
+
parameter names. These types do not validate the path or convert parameter values.
|
|
94
|
+
|
|
95
|
+
Path arrays can be mutable or readonly, including arrays declared with `as const`.
|
|
96
|
+
|
|
97
|
+
Use `RequestParamHandler` for reusable `router.param` callbacks:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const loadUser: Router.RequestParamHandler = (req, res, next, value, name) => {
|
|
101
|
+
req.user = { id: String(value) }
|
|
102
|
+
next()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
router.param('id', loadUser)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Routing methods preserve extended router and route types when chaining.
|
|
109
|
+
|
|
21
110
|
## API
|
|
22
111
|
|
|
23
112
|
```js
|
|
@@ -65,6 +154,10 @@ consider it one for handling `OPTIONS` requests.
|
|
|
65
154
|
|
|
66
155
|
* Note: If a `path` is specified, that `path` is stripped from the start of
|
|
67
156
|
`req.url`.
|
|
157
|
+
* Note: The stripped `path` is added back to `req.url` when the middleware calls
|
|
158
|
+
`next()`. A `req.url` rewritten by the middleware is treated as relative to the
|
|
159
|
+
mount `path`, so rewriting `req.url` to `/index.html` inside a router mounted
|
|
160
|
+
on `/app` continues with `/app/index.html`.
|
|
68
161
|
|
|
69
162
|
<!-- eslint-disable no-undef -->
|
|
70
163
|
|
|
@@ -143,6 +236,58 @@ router.param('user_id', function (req, res, next, id) {
|
|
|
143
236
|
})
|
|
144
237
|
```
|
|
145
238
|
|
|
239
|
+
### router.listRoutes()
|
|
240
|
+
|
|
241
|
+
Returns an array with one `{ path, methods, router }` object per path
|
|
242
|
+
registered on this router, in registration order.
|
|
243
|
+
|
|
244
|
+
- `path` is the path the route was registered with (a string or a `RegExp`).
|
|
245
|
+
Routes registered with an array of paths produce one entry per path.
|
|
246
|
+
- `methods` lists registered HTTP methods in uppercase, in the order they
|
|
247
|
+
were first registered. `.all()` is represented by `'*'` in that order to
|
|
248
|
+
indicate that the route accepts any HTTP method. Repeated registrations
|
|
249
|
+
do not add duplicate entries. The automatic `HEAD` for `GET` routes is
|
|
250
|
+
appended unless explicitly registered. For example, `.all(...).get(...)`
|
|
251
|
+
produces `['*', 'GET', 'HEAD']`, while `.get(...).all(...)` produces
|
|
252
|
+
`['GET', '*', 'HEAD']`. It is `undefined` for mounted routers, whose
|
|
253
|
+
internal routes determine the methods they handle.
|
|
254
|
+
An empty array means the route was created with
|
|
255
|
+
`router.route(path)` but has no handlers yet, so it matches no method.
|
|
256
|
+
- `router` is the mounted router instance for `.use(path, router)` entries,
|
|
257
|
+
otherwise `undefined`. Nested routes are not resolved recursively; consumers
|
|
258
|
+
can recurse themselves by calling `router.listRoutes()` when the mounted
|
|
259
|
+
router implements it. Note that `.use()`
|
|
260
|
+
entries match their path as a prefix, while route entries match the full
|
|
261
|
+
path.
|
|
262
|
+
|
|
263
|
+
Plain middleware functions registered with `.use()` are not listed.
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
const router = new Router()
|
|
267
|
+
const admin = new Router()
|
|
268
|
+
|
|
269
|
+
admin.get('/', (req, res) => {
|
|
270
|
+
res.end('Hello')
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
router.use('/admin', admin)
|
|
274
|
+
|
|
275
|
+
router.all('/:id', function (req, res) {
|
|
276
|
+
res.end('Hello')
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
console.log(router.listRoutes())
|
|
280
|
+
// [
|
|
281
|
+
// { path: '/admin', methods: undefined, router: admin },
|
|
282
|
+
// { path: '/:id', methods: ['*'], router: undefined }
|
|
283
|
+
// ]
|
|
284
|
+
|
|
285
|
+
console.log(admin.listRoutes())
|
|
286
|
+
// [
|
|
287
|
+
// { path: '/', methods: ['GET', 'HEAD'], router: undefined }
|
|
288
|
+
// ]
|
|
289
|
+
```
|
|
290
|
+
|
|
146
291
|
### router.route(path)
|
|
147
292
|
|
|
148
293
|
Creates an instance of a single `Route` for the given `path`.
|
|
@@ -316,7 +461,7 @@ var finalhandler = require('finalhandler')
|
|
|
316
461
|
// this example is about the mergeParams option
|
|
317
462
|
var opts = { mergeParams: true }
|
|
318
463
|
|
|
319
|
-
// make a router with
|
|
464
|
+
// make a router with our special options
|
|
320
465
|
var router = Router(opts)
|
|
321
466
|
var server = http.createServer(function onRequest (req, res) {
|
|
322
467
|
// set something to be passed into the router
|
|
@@ -329,7 +474,7 @@ router.get('/', function (req, res) {
|
|
|
329
474
|
res.statusCode = 200
|
|
330
475
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
|
331
476
|
|
|
332
|
-
// with respond with the
|
|
477
|
+
// with respond with the params that were passed in
|
|
333
478
|
res.end(req.params.type + '\n')
|
|
334
479
|
})
|
|
335
480
|
|
|
@@ -400,6 +545,81 @@ router.route('/pet/:id')
|
|
|
400
545
|
server.listen(8080)
|
|
401
546
|
```
|
|
402
547
|
|
|
548
|
+
## Diagnostics
|
|
549
|
+
|
|
550
|
+
`router` integrates with Node.js [`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html)
|
|
551
|
+
via a [`TracingChannel`](https://nodejs.org/api/diagnostics_channel.html#class-tracingchannel)
|
|
552
|
+
named `express.router.request`. This lets observability tools (APMs, tracers,
|
|
553
|
+
loggers) hook into middleware and route handler execution without monkey-patching.
|
|
554
|
+
|
|
555
|
+
Each middleware, route handler, and `router.param()` callback invocation publishes
|
|
556
|
+
the standard tracing channel sub-events (`start`, `end`, `asyncStart`, `asyncEnd`,
|
|
557
|
+
`error`). The published context object contains:
|
|
558
|
+
|
|
559
|
+
- `req`: the incoming `http.IncomingMessage`
|
|
560
|
+
- `res`: the `http.ServerResponse`
|
|
561
|
+
- `layer`: the internal `Layer` associated with the invocation (exposes `.name`, `.rawPath`, `.handle`, etc.). For parameter callbacks, this is the layer whose parameters are being processed. Note that `Layer` is an internal implementation detail and its shape may change between releases.
|
|
562
|
+
- `handler`: the function being invoked. For parameter callbacks, this is the callback registered with `router.param()`, rather than `layer.handle`.
|
|
563
|
+
- `param`: the parameter name, such as `'id'`, present only for `router.param()` callbacks.
|
|
564
|
+
- `route`: the path pattern that matched, e.g. `'/users/:id'`. Route handlers get their route's path; middleware and mounted routers get the path they were registered with
|
|
565
|
+
- `error`: the error reported by the invocation, set when it calls `next(err)`, throws, or rejects. A throw or rejection always sets it, even when the error was already published upstream and isn't published again
|
|
566
|
+
- `incomingError`: the error an error handler received, only set for error handlers
|
|
567
|
+
- `errorHandler`: `true` when the layer is an error-handling middleware (4-arg signature)
|
|
568
|
+
- `result`: the value the invocation produced, as with `tracePromise`. A synchronous return value is set before `end`; for a promise it is the resolved value, set before `asyncStart`. It is not set when the invocation throws or rejects.
|
|
569
|
+
|
|
570
|
+
The `error` event is published once, on the handler or parameter callback where
|
|
571
|
+
the error originates, whether it calls `next(err)`, throws, or returns a rejected
|
|
572
|
+
promise, as soon as it happens. `result` or `error` being set at `end` means no
|
|
573
|
+
`asyncStart` or `asyncEnd` will follow. If an invocation calls `next(err)` and
|
|
574
|
+
then returns a promise, `error` is published before `end` but unset during it. An
|
|
575
|
+
error bubbling up through outer layers is not reported again. The `'route'` and
|
|
576
|
+
`'router'` routing signals are not treated as errors and will not publish to the
|
|
577
|
+
`error` channel.
|
|
578
|
+
|
|
579
|
+
Errors originating before an invocation is traced, or during route matching
|
|
580
|
+
(such as URI decoding), are not attributed to handlers that forward them. They
|
|
581
|
+
still reach error handlers and appear in those handlers' tracing contexts. If
|
|
582
|
+
an error handler creates a different error, that new error is published normally.
|
|
583
|
+
|
|
584
|
+
Parameter callbacks get the matched layer's path pattern in `route`. Use
|
|
585
|
+
`handler.name` to identify the callback; `layer.name` identifies the associated
|
|
586
|
+
layer. A cached parameter result does not invoke its callbacks again, so it does
|
|
587
|
+
not produce additional callback events.
|
|
588
|
+
|
|
589
|
+
Stores bound with `start.bindStore()` apply during handler invocation. Stores
|
|
590
|
+
bound with `asyncStart.bindStore()` apply during `asyncEnd` publication when a
|
|
591
|
+
returned promise settles.
|
|
592
|
+
|
|
593
|
+
When no subscribers are attached, handler execution skips tracing context
|
|
594
|
+
allocation and channel publishing. Error identities are tracked per request so
|
|
595
|
+
that subscribers attached later cannot attribute a forwarded error to its receiver.
|
|
596
|
+
|
|
597
|
+
```js
|
|
598
|
+
const dc = require('node:diagnostics_channel')
|
|
599
|
+
|
|
600
|
+
const channel = dc.tracingChannel('express.router.request')
|
|
601
|
+
|
|
602
|
+
channel.subscribe({
|
|
603
|
+
start (ctx) {
|
|
604
|
+
ctx.startTime = process.hrtime.bigint()
|
|
605
|
+
},
|
|
606
|
+
end (ctx) {
|
|
607
|
+
// do whatever you need on synchronous completion
|
|
608
|
+
},
|
|
609
|
+
asyncStart (ctx) {
|
|
610
|
+
// do whatever you need when the async portion begins
|
|
611
|
+
},
|
|
612
|
+
asyncEnd (ctx) {
|
|
613
|
+
const durationNs = process.hrtime.bigint() - ctx.startTime
|
|
614
|
+
console.log('%s %s -> %s (%dns)',
|
|
615
|
+
ctx.req.method, ctx.req.url, ctx.handler.name || '<anonymous>', durationNs)
|
|
616
|
+
},
|
|
617
|
+
error (ctx) {
|
|
618
|
+
console.error('handler error in %s:', ctx.handler.name || '<anonymous>', ctx.error)
|
|
619
|
+
}
|
|
620
|
+
})
|
|
621
|
+
```
|
|
622
|
+
|
|
403
623
|
## License
|
|
404
624
|
|
|
405
625
|
[MIT](LICENSE)
|
|
@@ -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()
|