@nxtedition/lib 23.6.17 → 23.6.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/http.js +61 -16
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -42,27 +42,20 @@ export class Context {
|
|
|
42
42
|
#req
|
|
43
43
|
#res
|
|
44
44
|
#ac
|
|
45
|
-
#target
|
|
46
45
|
#logger
|
|
47
46
|
#query
|
|
47
|
+
#target
|
|
48
48
|
|
|
49
49
|
constructor(req, res, logger) {
|
|
50
50
|
assert(req)
|
|
51
51
|
assert(res)
|
|
52
52
|
|
|
53
|
-
this.#id = req.
|
|
53
|
+
this.#id = req.headers['request-id'] || req.headers['Request-Id'] || genReqId()
|
|
54
54
|
this.#userAgent = req.headers['user-agent'] || req.headers['User-Agent'] || ''
|
|
55
55
|
this.#req = req
|
|
56
56
|
this.#res = res
|
|
57
57
|
this.#logger = logger.child({ rid: this.#id })
|
|
58
|
-
this.#target = requestTarget(this.#req)
|
|
59
58
|
this.#query = undefined
|
|
60
|
-
|
|
61
|
-
// TODO (fix): This should not be necessary...
|
|
62
|
-
req.target ??= this.#target
|
|
63
|
-
// req.userAgent ??= this.#userAgent
|
|
64
|
-
req.id ??= this.#id
|
|
65
|
-
req.logger ??= this.#logger
|
|
66
59
|
}
|
|
67
60
|
|
|
68
61
|
get id() {
|
|
@@ -284,6 +277,35 @@ export async function requestMiddleware(ctx, next) {
|
|
|
284
277
|
}
|
|
285
278
|
}
|
|
286
279
|
|
|
280
|
+
export class IncomingMessage extends http.IncomingMessage {
|
|
281
|
+
#target
|
|
282
|
+
#host
|
|
283
|
+
#url
|
|
284
|
+
#socket
|
|
285
|
+
|
|
286
|
+
constructor(...args) {
|
|
287
|
+
super(...args)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
get id() {
|
|
291
|
+
return this.headers['request-id'] || this.headers['Request-Id']
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
get target() {
|
|
295
|
+
if (
|
|
296
|
+
this.#host !== this.headers.host ||
|
|
297
|
+
this.#url !== this.url ||
|
|
298
|
+
this.#socket !== this.socket
|
|
299
|
+
) {
|
|
300
|
+
this.#target = undefined
|
|
301
|
+
this.#host = this.headers.host
|
|
302
|
+
this.#url = this.url
|
|
303
|
+
this.#socket = this.socket
|
|
304
|
+
}
|
|
305
|
+
return (this.#target ??= requestTarget(this))
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
287
309
|
export class ServerResponse extends http.ServerResponse {
|
|
288
310
|
#created = 0
|
|
289
311
|
#bytesWritten = 0
|
|
@@ -383,6 +405,35 @@ export class ServerResponse extends http.ServerResponse {
|
|
|
383
405
|
}
|
|
384
406
|
}
|
|
385
407
|
|
|
408
|
+
export class Http2IncomingMessage extends http2.IncomingMessage {
|
|
409
|
+
#target
|
|
410
|
+
#host
|
|
411
|
+
#url
|
|
412
|
+
#socket
|
|
413
|
+
|
|
414
|
+
constructor(...args) {
|
|
415
|
+
super(...args)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
get id() {
|
|
419
|
+
return this.headers['request-id'] || this.headers['Request-Id']
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
get target() {
|
|
423
|
+
if (
|
|
424
|
+
this.#host !== this.headers.host ||
|
|
425
|
+
this.#url !== this.url ||
|
|
426
|
+
this.#socket !== this.socket
|
|
427
|
+
) {
|
|
428
|
+
this.#target = undefined
|
|
429
|
+
this.#host = this.headers.host
|
|
430
|
+
this.#url = this.url
|
|
431
|
+
this.#socket = this.socket
|
|
432
|
+
}
|
|
433
|
+
return (this.#target ??= requestTarget(this))
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
386
437
|
export class Http2ServerResponse extends http2.Http2ServerResponse {
|
|
387
438
|
#created = 0
|
|
388
439
|
#bytesWritten = 0
|
|
@@ -479,8 +530,6 @@ export async function request(ctx, next) {
|
|
|
479
530
|
const { req, res, logger } = ctx
|
|
480
531
|
const startTime = performance.now()
|
|
481
532
|
|
|
482
|
-
const ac = ctx.signal !== undefined ? null : new AbortController()
|
|
483
|
-
|
|
484
533
|
let reqLogger = logger
|
|
485
534
|
|
|
486
535
|
pendingSet.add(ctx)
|
|
@@ -499,10 +548,6 @@ export async function request(ctx, next) {
|
|
|
499
548
|
res.log =
|
|
500
549
|
logger.child({ req: { id: req.id, url: req.url, userAgent: ctx.userAgent } })
|
|
501
550
|
|
|
502
|
-
if (ac) {
|
|
503
|
-
ctx.signal = ac.signal
|
|
504
|
-
}
|
|
505
|
-
|
|
506
551
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
507
552
|
req.resume() // Dump the body if there is one.
|
|
508
553
|
}
|
|
@@ -541,7 +586,7 @@ export async function request(ctx, next) {
|
|
|
541
586
|
reqLogger.debug({ res, elapsedTime }, 'request completed')
|
|
542
587
|
}
|
|
543
588
|
} catch (err) {
|
|
544
|
-
|
|
589
|
+
ctx[kAbortController]?.abort(err)
|
|
545
590
|
|
|
546
591
|
const statusCode = err.statusCode || err.$metadata?.httpStatusCode || 500
|
|
547
592
|
const elapsedTime = performance.now() - startTime
|