@zuplo/cli 7.15.17 → 7.15.19
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
|
@@ -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
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* router
|
|
3
|
+
* MIT Licensed
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const isPromise = require('is-promise')
|
|
9
|
+
const deprecate = require('depd')('router')
|
|
10
|
+
|
|
11
|
+
module.exports = callParam
|
|
12
|
+
|
|
13
|
+
// Share invocation and promise handling between traced and untraced callbacks.
|
|
14
|
+
// A resolved promise may notify tracing, but does not call next(). onSettle gets
|
|
15
|
+
// the resolved value, or (reason, true) if the promise rejected.
|
|
16
|
+
function callParam (fn, req, res, next, value, name, onSettle) {
|
|
17
|
+
try {
|
|
18
|
+
const ret = fn(req, res, next, value, name)
|
|
19
|
+
if (isPromise(ret)) {
|
|
20
|
+
if (!(ret instanceof Promise)) {
|
|
21
|
+
deprecate('parameters that are Promise-like are deprecated, use a native Promise instead')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
ret.then(onSettle || noop, function (err) {
|
|
26
|
+
rejected(err || new Error('Rejected promise'))
|
|
27
|
+
})
|
|
28
|
+
} catch (err) {
|
|
29
|
+
// A `then` that throws is a rejection, forwarded verbatim as before
|
|
30
|
+
rejected(err)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
next(err)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function rejected (err) {
|
|
38
|
+
next(err)
|
|
39
|
+
if (onSettle) onSettle(err, true)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Passed to then() instead of null, since some thenables call their callbacks
|
|
45
|
+
* without checking, and the traced path always passes a function.
|
|
46
|
+
*/
|
|
47
|
+
function noop () {}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
2
|
+
import type { Layer as RouterLayer, Params as RouterParams, Request as RouterRequest, Response as RouterResponse } from '../index'
|
|
3
|
+
|
|
4
|
+
declare const Route: Route.RouteConstructor
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare -- Merge types with the CommonJS constructor export.
|
|
7
|
+
declare namespace Route {
|
|
8
|
+
/**
|
|
9
|
+
* Call `next()` to continue to the next handler.
|
|
10
|
+
* In a route handler, `next('route')` skips the remaining handlers of that route.
|
|
11
|
+
* `next('router')` exits the current router and invokes its completion callback.
|
|
12
|
+
* Other truthy arguments enter error handling; falsy arguments continue normally.
|
|
13
|
+
*/
|
|
14
|
+
type NextFunction = (err?: any) => void
|
|
15
|
+
|
|
16
|
+
type Params = RouterParams
|
|
17
|
+
type Request<P = Params> = RouterRequest<P>
|
|
18
|
+
type Response = RouterResponse
|
|
19
|
+
type Layer = RouterLayer
|
|
20
|
+
|
|
21
|
+
type RequestHandler<P = Params> = (req: Request<P>, res: Response, next: NextFunction) => unknown
|
|
22
|
+
type ErrorRequestHandler<P = Params> = (err: any, req: Request<P>, res: Response, next: NextFunction) => unknown
|
|
23
|
+
|
|
24
|
+
type Handler<P = Params> = RequestHandler<P> | ErrorRequestHandler<P>
|
|
25
|
+
type HandlerArg<F extends Handler<any> = Handler> = F | ReadonlyArray<HandlerArg<F>>
|
|
26
|
+
|
|
27
|
+
type NestedStringPath = string | ReadonlyArray<NestedStringPath>
|
|
28
|
+
type Path = string | RegExp | ReadonlyArray<RegExp | NestedStringPath>
|
|
29
|
+
|
|
30
|
+
// Follow the route parameter inference used by @types/express-serve-static-core.
|
|
31
|
+
type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S
|
|
32
|
+
type GetRouteParameter<S extends string> = RemoveTail<RemoveTail<RemoveTail<S, `/${string}`>, `-${string}`>, `.${string}`>
|
|
33
|
+
|
|
34
|
+
type RouteParameters<R extends Path> = R extends string
|
|
35
|
+
? R extends `${infer Required}{${infer Optional}}${infer Next}`
|
|
36
|
+
? ParseRouteParameters<Required> & Partial<ParseRouteParameters<Optional>> & RouteParameters<Next>
|
|
37
|
+
: ParseRouteParameters<R>
|
|
38
|
+
: Params
|
|
39
|
+
|
|
40
|
+
type ParseRouteParameters<R extends string> = string extends R ? Params
|
|
41
|
+
: R extends `${string}:${infer Rest}`
|
|
42
|
+
? (GetRouteParameter<Rest> extends never ? Params : { [P in GetRouteParameter<Rest>]: string })
|
|
43
|
+
& (Rest extends `${GetRouteParameter<Rest>}${infer Next}` ? RouteParameters<Next> : unknown)
|
|
44
|
+
: R extends `${string}*${infer Rest}`
|
|
45
|
+
? (GetRouteParameter<Rest> extends never ? Params : { [P in GetRouteParameter<Rest>]: string[] })
|
|
46
|
+
& (Rest extends `${GetRouteParameter<Rest>}${infer Next}` ? RouteParameters<Next> : unknown)
|
|
47
|
+
: {}
|
|
48
|
+
|
|
49
|
+
type RouteMethod<T, DefaultParams = Params> = {
|
|
50
|
+
<F extends Handler<any> = RequestHandler<DefaultParams>>(handler: HandlerArg<F>, ...handlers: Array<HandlerArg<F>>): T
|
|
51
|
+
<P = DefaultParams>(handler: HandlerArg<RequestHandler<P>>, ...handlers: Array<HandlerArg<RequestHandler<P>>>): T
|
|
52
|
+
<P = DefaultParams>(handler: HandlerArg<Handler<P>>, ...handlers: Array<HandlerArg<Handler<P>>>): T
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type Method =
|
|
56
|
+
| 'acl' | 'bind' | 'checkout' | 'connect' | 'copy' | 'delete' | 'get'
|
|
57
|
+
| 'head' | 'link' | 'lock' | 'm-search' | 'merge' | 'mkactivity'
|
|
58
|
+
| 'mkcalendar' | 'mkcol' | 'move' | 'notify' | 'options' | 'patch'
|
|
59
|
+
| 'post' | 'propfind' | 'proppatch' | 'purge' | 'put' | 'query'
|
|
60
|
+
| 'rebind' | 'report' | 'search' | 'source' | 'subscribe' | 'trace'
|
|
61
|
+
| 'unbind' | 'unlink' | 'unlock' | 'unsubscribe'
|
|
62
|
+
|
|
63
|
+
type MethodHandlers<T> = { [M in Method | 'all']: RouteMethod<T> }
|
|
64
|
+
|
|
65
|
+
interface Route<R extends Path = Path> {
|
|
66
|
+
path: R
|
|
67
|
+
stack: Layer[]
|
|
68
|
+
methods: Record<string, boolean>
|
|
69
|
+
|
|
70
|
+
_handlesMethod(method: string): boolean
|
|
71
|
+
/** Include `'*'` for `.all()` handlers when `includeAll` is true. */
|
|
72
|
+
_methods(includeAll?: boolean): string[]
|
|
73
|
+
dispatch(req: IncomingMessage, res: ServerResponse, done: NextFunction): void
|
|
74
|
+
|
|
75
|
+
all: RouteMethod<this, RouteParameters<R>>
|
|
76
|
+
acl: RouteMethod<this, RouteParameters<R>>
|
|
77
|
+
bind: RouteMethod<this, RouteParameters<R>>
|
|
78
|
+
checkout: RouteMethod<this, RouteParameters<R>>
|
|
79
|
+
connect: RouteMethod<this, RouteParameters<R>>
|
|
80
|
+
copy: RouteMethod<this, RouteParameters<R>>
|
|
81
|
+
delete: RouteMethod<this, RouteParameters<R>>
|
|
82
|
+
get: RouteMethod<this, RouteParameters<R>>
|
|
83
|
+
head: RouteMethod<this, RouteParameters<R>>
|
|
84
|
+
link: RouteMethod<this, RouteParameters<R>>
|
|
85
|
+
lock: RouteMethod<this, RouteParameters<R>>
|
|
86
|
+
'm-search': RouteMethod<this, RouteParameters<R>>
|
|
87
|
+
merge: RouteMethod<this, RouteParameters<R>>
|
|
88
|
+
mkactivity: RouteMethod<this, RouteParameters<R>>
|
|
89
|
+
mkcalendar: RouteMethod<this, RouteParameters<R>>
|
|
90
|
+
mkcol: RouteMethod<this, RouteParameters<R>>
|
|
91
|
+
move: RouteMethod<this, RouteParameters<R>>
|
|
92
|
+
notify: RouteMethod<this, RouteParameters<R>>
|
|
93
|
+
options: RouteMethod<this, RouteParameters<R>>
|
|
94
|
+
patch: RouteMethod<this, RouteParameters<R>>
|
|
95
|
+
post: RouteMethod<this, RouteParameters<R>>
|
|
96
|
+
propfind: RouteMethod<this, RouteParameters<R>>
|
|
97
|
+
proppatch: RouteMethod<this, RouteParameters<R>>
|
|
98
|
+
purge: RouteMethod<this, RouteParameters<R>>
|
|
99
|
+
put: RouteMethod<this, RouteParameters<R>>
|
|
100
|
+
query: RouteMethod<this, RouteParameters<R>>
|
|
101
|
+
rebind: RouteMethod<this, RouteParameters<R>>
|
|
102
|
+
report: RouteMethod<this, RouteParameters<R>>
|
|
103
|
+
search: RouteMethod<this, RouteParameters<R>>
|
|
104
|
+
source: RouteMethod<this, RouteParameters<R>>
|
|
105
|
+
subscribe: RouteMethod<this, RouteParameters<R>>
|
|
106
|
+
trace: RouteMethod<this, RouteParameters<R>>
|
|
107
|
+
unbind: RouteMethod<this, RouteParameters<R>>
|
|
108
|
+
unlink: RouteMethod<this, RouteParameters<R>>
|
|
109
|
+
unlock: RouteMethod<this, RouteParameters<R>>
|
|
110
|
+
unsubscribe: RouteMethod<this, RouteParameters<R>>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface RouteConstructor {
|
|
114
|
+
new <R extends Path>(path: R): Route<R>
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export = Route
|
|
@@ -69,21 +69,27 @@ Route.prototype._handlesMethod = function _handlesMethod (method) {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
+
* @param {boolean} [includeAll=false] include a wildcard for `.all()` handlers
|
|
72
73
|
* @return {array} supported HTTP methods
|
|
73
74
|
* @private
|
|
74
75
|
*/
|
|
75
76
|
|
|
76
|
-
Route.prototype._methods = function _methods () {
|
|
77
|
-
const methods =
|
|
77
|
+
Route.prototype._methods = function _methods (includeAll) {
|
|
78
|
+
const methods = []
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
for (const method of Object.keys(this.methods)) {
|
|
81
|
+
if (method === '_all') {
|
|
82
|
+
if (includeAll) {
|
|
83
|
+
methods.push('*')
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
methods.push(method.toUpperCase())
|
|
87
|
+
}
|
|
82
88
|
}
|
|
83
89
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
methods
|
|
90
|
+
// append automatic head
|
|
91
|
+
if (this.methods.get && !this.methods.head) {
|
|
92
|
+
methods.push('HEAD')
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
return methods
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* router
|
|
3
|
+
* MIT Licensed
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const dc = require('node:diagnostics_channel')
|
|
9
|
+
const isPromise = require('is-promise')
|
|
10
|
+
const callParam = require('./param')
|
|
11
|
+
|
|
12
|
+
const requestChannel = dc.tracingChannel && dc.tracingChannel('express.router.request')
|
|
13
|
+
|
|
14
|
+
// Share error identity tracking across callbacks and router boundaries. Keep
|
|
15
|
+
// the registered symbol compatible with separately installed copies of router.
|
|
16
|
+
const seenErrors = Symbol.for('router.tracing.publishedErrors')
|
|
17
|
+
|
|
18
|
+
exports.publishEnd = publishEnd
|
|
19
|
+
exports.recordError = recordError
|
|
20
|
+
exports.recordThrown = recordThrown
|
|
21
|
+
exports.requestChannel = requestChannel
|
|
22
|
+
exports.shouldTrace = shouldTrace
|
|
23
|
+
exports.traceAsyncEnd = traceAsyncEnd
|
|
24
|
+
exports.traceParam = traceParam
|
|
25
|
+
exports.wrapNext = wrapNext
|
|
26
|
+
|
|
27
|
+
// TracingChannel#hasSubscribers is undefined before Node 20.13/22.
|
|
28
|
+
function shouldTrace (ch) {
|
|
29
|
+
return Boolean(ch) && (
|
|
30
|
+
ch.start.hasSubscribers ||
|
|
31
|
+
ch.end.hasSubscribers ||
|
|
32
|
+
ch.asyncStart.hasSubscribers ||
|
|
33
|
+
ch.asyncEnd.hasSubscribers ||
|
|
34
|
+
ch.error.hasSubscribers
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Remember errors already observed, including those from untraced code or
|
|
39
|
+
// route matching. Only the first report from a traced invocation is published.
|
|
40
|
+
function recordError (req, err) {
|
|
41
|
+
if (!err || isRoutingSignal(err)) return false
|
|
42
|
+
|
|
43
|
+
let seen = req[seenErrors]
|
|
44
|
+
if (!seen) {
|
|
45
|
+
seen = req[seenErrors] = new Set()
|
|
46
|
+
} else if (seen.has(err)) {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
seen.add(err)
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `'route'` and `'router'` are routing signals, not errors.
|
|
55
|
+
*/
|
|
56
|
+
function isRoutingSignal (err) {
|
|
57
|
+
return err === 'route' || err === 'router'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Record a throw or rejection on the context. A rethrown error isn't published
|
|
62
|
+
* again, but the terminal event still needs it. A routing signal isn't an
|
|
63
|
+
* error, so a sync one finishes like calling next('route') and returning.
|
|
64
|
+
*/
|
|
65
|
+
function recordThrown (ctx, err, sync) {
|
|
66
|
+
if (isRoutingSignal(err)) {
|
|
67
|
+
if (sync) ctx.result = undefined
|
|
68
|
+
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
if (!('error' in ctx)) ctx.error = err
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Forget an error an error handler has dealt with, so a later report of the
|
|
76
|
+
* same object is published again.
|
|
77
|
+
*/
|
|
78
|
+
function forgetError (req, err) {
|
|
79
|
+
const seen = req[seenErrors]
|
|
80
|
+
if (seen) seen.delete(err)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function wrapNext (ctx, next) {
|
|
84
|
+
// An error received by an error handler did not originate there, even if
|
|
85
|
+
// tracing was disabled at its origin. A replacement error is still reported.
|
|
86
|
+
if (ctx.errorHandler) recordError(ctx.req, ctx.incomingError)
|
|
87
|
+
|
|
88
|
+
return function wrappedNext (err) {
|
|
89
|
+
// An error handler that recovers or replaces the error has handled it, so
|
|
90
|
+
// the same object reported later in the request is a new failure.
|
|
91
|
+
if (ctx.errorHandler && err !== ctx.incomingError) forgetError(ctx.req, ctx.incomingError)
|
|
92
|
+
// Routing signals aren't errors. Report a real error once, at its origin,
|
|
93
|
+
// before forwarding it to another callback, layer or enclosing router.
|
|
94
|
+
if (err && recordError(ctx.req, err)) {
|
|
95
|
+
ctx.error = err
|
|
96
|
+
requestChannel.error.publish(ctx)
|
|
97
|
+
}
|
|
98
|
+
next(err)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// An error reported before a promise is returned is published before `end`, so
|
|
103
|
+
// it's hidden during `end` to keep `result` or `error` there meaning done.
|
|
104
|
+
function publishEnd (ctx, async) {
|
|
105
|
+
if (!async || !('error' in ctx)) return requestChannel.end.publish(ctx)
|
|
106
|
+
|
|
107
|
+
const error = ctx.error
|
|
108
|
+
delete ctx.error
|
|
109
|
+
requestChannel.end.publish(ctx)
|
|
110
|
+
ctx.error = error
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function traceAsyncEnd (ctx) {
|
|
114
|
+
// Scope completion to asyncStart's bound stores without scheduling more work.
|
|
115
|
+
requestChannel.asyncStart.runStores(ctx, publishAsyncEnd, null, ctx)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function publishAsyncEnd (ctx) {
|
|
119
|
+
requestChannel.asyncEnd.publish(ctx)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Wrap the shared parameter invocation with its tracing context and lifecycle.
|
|
123
|
+
function traceParam (fn, ctx, next, value, name) {
|
|
124
|
+
const wrappedNext = wrapNext(ctx, next)
|
|
125
|
+
let async = false
|
|
126
|
+
let ended = false
|
|
127
|
+
let settled = false
|
|
128
|
+
let early
|
|
129
|
+
|
|
130
|
+
requestChannel.start.runStores(ctx, function () {
|
|
131
|
+
try {
|
|
132
|
+
callParam(invoke, ctx.req, ctx.res, wrappedNext, value, name, settle)
|
|
133
|
+
} finally {
|
|
134
|
+
publishEnd(ctx, async)
|
|
135
|
+
ended = true
|
|
136
|
+
if (early) early()
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
// Same as layers, a throw skips the assignment so no result is set
|
|
141
|
+
function invoke (req, res, next, value, name) {
|
|
142
|
+
let ret
|
|
143
|
+
try {
|
|
144
|
+
ret = fn(req, res, next, value, name)
|
|
145
|
+
} catch (err) {
|
|
146
|
+
recordThrown(ctx, err, true)
|
|
147
|
+
throw err
|
|
148
|
+
}
|
|
149
|
+
async = isPromise(ret)
|
|
150
|
+
if (!async) ctx.result = ret
|
|
151
|
+
|
|
152
|
+
return ret
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function settle (value, rejected) {
|
|
156
|
+
// Same as tracePromise, only the first outcome settles the trace
|
|
157
|
+
if (settled) return
|
|
158
|
+
settled = true
|
|
159
|
+
// Same as layers, a thenable that settles synchronously still settles after `end`
|
|
160
|
+
if (!ended) {
|
|
161
|
+
early = function () { finish(value, rejected) }
|
|
162
|
+
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
finish(value, rejected)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function finish (value, rejected) {
|
|
169
|
+
if (!rejected) ctx.result = value
|
|
170
|
+
else recordThrown(ctx, value, false)
|
|
171
|
+
traceAsyncEnd(ctx)
|
|
172
|
+
}
|
|
173
|
+
}
|