@nxtedition/lib 23.7.6 → 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.
- package/http.js +31 -25
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -8,7 +8,6 @@ import compose from 'koa-compose'
|
|
|
8
8
|
import http from 'http'
|
|
9
9
|
import fp from 'lodash/fp.js'
|
|
10
10
|
import tp from 'timers/promises'
|
|
11
|
-
import querystring from 'fast-querystring'
|
|
12
11
|
|
|
13
12
|
export const kAbortController = Symbol('abortController')
|
|
14
13
|
|
|
@@ -39,11 +38,13 @@ function onTimeout() {
|
|
|
39
38
|
|
|
40
39
|
export class Context {
|
|
41
40
|
#id
|
|
42
|
-
#userAgent
|
|
43
41
|
#req
|
|
44
42
|
#res
|
|
45
43
|
#ac
|
|
46
44
|
#logger
|
|
45
|
+
|
|
46
|
+
// deprecated fields
|
|
47
|
+
#userAgent
|
|
47
48
|
#query
|
|
48
49
|
#target
|
|
49
50
|
|
|
@@ -55,16 +56,11 @@ export class Context {
|
|
|
55
56
|
this.#userAgent = req.headers['user-agent'] || req.headers['User-Agent'] || ''
|
|
56
57
|
this.#req = req
|
|
57
58
|
this.#res = res
|
|
58
|
-
this.#logger = logger.child({ rid: this.#id })
|
|
59
|
-
this.#query = undefined
|
|
59
|
+
this.#logger = logger.child({ rid: this.#id, userAgent: this.#userAgent })
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
get id() {
|
|
63
|
-
|
|
64
|
-
return this.#req.id
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return this.#id
|
|
63
|
+
return this.#req.id ?? this.#id
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
get logger() {
|
|
@@ -90,11 +86,7 @@ export class Context {
|
|
|
90
86
|
|
|
91
87
|
/** @deprecated */
|
|
92
88
|
get userAgent() {
|
|
93
|
-
|
|
94
|
-
return this.#req.userAgent
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return this.#userAgent
|
|
89
|
+
return this.#req.userAgent ?? this.#userAgent
|
|
98
90
|
}
|
|
99
91
|
|
|
100
92
|
/** @deprecated */
|
|
@@ -143,13 +135,9 @@ export class Context {
|
|
|
143
135
|
return this.#query
|
|
144
136
|
}
|
|
145
137
|
|
|
146
|
-
/** @deprecated */
|
|
147
|
-
set query(value) {}
|
|
148
|
-
|
|
149
138
|
/** @deprecated */
|
|
150
139
|
set logger(logger) {
|
|
151
|
-
this.#logger = logger.child({
|
|
152
|
-
this.#req.logger = this.#logger
|
|
140
|
+
this.#logger = logger.child({ rid: this.#id })
|
|
153
141
|
}
|
|
154
142
|
}
|
|
155
143
|
|
|
@@ -757,12 +745,30 @@ export async function request(ctx, next) {
|
|
|
757
745
|
}
|
|
758
746
|
}
|
|
759
747
|
|
|
760
|
-
/** @deprecated */
|
|
761
748
|
export function createServer(options, ctx, middleware) {
|
|
762
|
-
middleware =
|
|
763
|
-
middleware =
|
|
764
|
-
|
|
765
|
-
|
|
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
|
+
}
|
|
766
772
|
|
|
767
773
|
const server = http.createServer(
|
|
768
774
|
{
|
|
@@ -773,7 +779,7 @@ export function createServer(options, ctx, middleware) {
|
|
|
773
779
|
requestTimeout: 0,
|
|
774
780
|
...options,
|
|
775
781
|
},
|
|
776
|
-
(req, res) => middleware(
|
|
782
|
+
(req, res) => middleware(factory(req, res)),
|
|
777
783
|
)
|
|
778
784
|
|
|
779
785
|
server.setTimeout(options.socketTimeout ?? options.timeout ?? 2 * 60e3)
|