@nxtedition/lib 23.7.5 → 23.8.0

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 +69 -30
  2. package/package.json +2 -2
package/http.js CHANGED
@@ -38,11 +38,13 @@ function onTimeout() {
38
38
 
39
39
  export class Context {
40
40
  #id
41
- #userAgent
42
41
  #req
43
42
  #res
44
43
  #ac
45
44
  #logger
45
+
46
+ // deprecated fields
47
+ #userAgent
46
48
  #query
47
49
  #target
48
50
 
@@ -54,16 +56,11 @@ export class Context {
54
56
  this.#userAgent = req.headers['user-agent'] || req.headers['User-Agent'] || ''
55
57
  this.#req = req
56
58
  this.#res = res
57
- this.#logger = logger.child({ rid: this.#id })
58
- this.#query = undefined
59
+ this.#logger = logger.child({ rid: this.#id, userAgent: this.#userAgent })
59
60
  }
60
61
 
61
62
  get id() {
62
- if (this.#req.id) {
63
- return this.#req.id
64
- }
65
-
66
- return this.#id
63
+ return this.#req.id ?? this.#id
67
64
  }
68
65
 
69
66
  get logger() {
@@ -89,11 +86,7 @@ export class Context {
89
86
 
90
87
  /** @deprecated */
91
88
  get userAgent() {
92
- if (this.#req.userAgent) {
93
- return this.#req.userAgent
94
- }
95
-
96
- return this.#userAgent
89
+ return this.#req.userAgent ?? this.#userAgent
97
90
  }
98
91
 
99
92
  /** @deprecated */
@@ -142,13 +135,9 @@ export class Context {
142
135
  return this.#query
143
136
  }
144
137
 
145
- /** @deprecated */
146
- set query(value) {}
147
-
148
138
  /** @deprecated */
149
139
  set logger(logger) {
150
- this.#logger = logger.child({ req: { id: this.#req.id } })
151
- this.#req.logger = this.#logger
140
+ this.#logger = logger.child({ rid: this.#id })
152
141
  }
153
142
  }
154
143
 
@@ -351,10 +340,13 @@ export async function requestMiddleware(ctx, next) {
351
340
  }
352
341
 
353
342
  export class IncomingMessage extends http.IncomingMessage {
354
- #target
355
343
  #host
356
344
  #url
357
345
  #socket
346
+ #target
347
+
348
+ #search
349
+ #query
358
350
 
359
351
  constructor(...args) {
360
352
  super(...args)
@@ -370,13 +362,26 @@ export class IncomingMessage extends http.IncomingMessage {
370
362
  this.#url !== this.url ||
371
363
  this.#socket !== this.socket
372
364
  ) {
373
- this.#target = undefined
365
+ this.#socket = this.socket
374
366
  this.#host = this.headers.host
375
367
  this.#url = this.url
376
- this.#socket = this.socket
368
+ this.#target = undefined
377
369
  }
370
+
378
371
  return (this.#target ??= requestTarget(this))
379
372
  }
373
+
374
+ get query() {
375
+ const search = this.target.search
376
+
377
+ if (this.#search !== search) {
378
+ this.#search = search
379
+ this.#query = undefined
380
+ }
381
+
382
+ return (this.#query ??=
383
+ search.length > 1 ? Object.freeze(querystring.parse(search.slice(1))) : {})
384
+ }
380
385
  }
381
386
 
382
387
  export class ServerResponse extends http.ServerResponse {
@@ -479,10 +484,13 @@ export class ServerResponse extends http.ServerResponse {
479
484
  }
480
485
 
481
486
  export class Http2ServerRequest extends http2.Http2ServerRequest {
482
- #target
483
487
  #host
484
488
  #url
485
489
  #socket
490
+ #target
491
+
492
+ #search
493
+ #query
486
494
 
487
495
  constructor(...args) {
488
496
  super(...args)
@@ -498,13 +506,26 @@ export class Http2ServerRequest extends http2.Http2ServerRequest {
498
506
  this.#url !== this.url ||
499
507
  this.#socket !== this.socket
500
508
  ) {
501
- this.#target = undefined
509
+ this.#socket = this.socket
502
510
  this.#host = this.headers.host
503
511
  this.#url = this.url
504
- this.#socket = this.socket
512
+ this.#target = undefined
505
513
  }
514
+
506
515
  return (this.#target ??= requestTarget(this))
507
516
  }
517
+
518
+ get query() {
519
+ const search = this.target.search
520
+
521
+ if (this.#search !== search) {
522
+ this.#search = search
523
+ this.#query = undefined
524
+ }
525
+
526
+ return (this.#query ??=
527
+ search.length > 1 ? Object.freeze(querystring.parse(search.slice(1))) : {})
528
+ }
508
529
  }
509
530
 
510
531
  export class Http2ServerResponse extends http2.Http2ServerResponse {
@@ -724,12 +745,30 @@ export async function request(ctx, next) {
724
745
  }
725
746
  }
726
747
 
727
- /** @deprecated */
728
748
  export function createServer(options, ctx, middleware) {
729
- middleware = Array.isArray(middleware) ? middleware : [middleware]
730
- middleware = fp.values(middleware)
731
- middleware = middleware.flat().filter(Boolean)
732
- middleware = compose([requestMiddleware, ...middleware])
749
+ middleware = [middleware].flat(16).filter(Boolean)
750
+ middleware = middleware.includes(requestMiddleware)
751
+ ? middleware
752
+ : [requestMiddleware, ...middleware]
753
+ middleware = compose(middleware)
754
+
755
+ let factory
756
+ if (typeof ctx === 'function') {
757
+ factory = ctx
758
+ } else {
759
+ let { logger, ...opaque } = ctx ?? {}
760
+ if (Object.keys(opaque).length === 0) {
761
+ opaque = null
762
+ }
763
+
764
+ factory = (req, res) => {
765
+ const context = new Context(req, res, ctx.logger ?? options?.logger)
766
+ if (opaque) {
767
+ Object.assign(context, opaque)
768
+ }
769
+ return middleware(context)
770
+ }
771
+ }
733
772
 
734
773
  const server = http.createServer(
735
774
  {
@@ -740,7 +779,7 @@ export function createServer(options, ctx, middleware) {
740
779
  requestTimeout: 0,
741
780
  ...options,
742
781
  },
743
- (req, res) => middleware(ctx ? { req, res, ...ctx } : { req, res }),
782
+ (req, res) => middleware(factory(req, res)),
744
783
  )
745
784
 
746
785
  server.setTimeout(options.socketTimeout ?? options.timeout ?? 2 * 60e3)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.7.5",
3
+ "version": "23.8.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -72,7 +72,7 @@
72
72
  "@swc/wasm-web": "^1.11.22",
73
73
  "date-fns": "^4.1.0",
74
74
  "diff": "5.2.0",
75
- "fast-querystring": "^1.1.1",
75
+ "fast-querystring": "^1.1.2",
76
76
  "hasha": "^6.0.0",
77
77
  "http-errors": "^2.0.0",
78
78
  "json5": "^2.2.3",