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