@nxtedition/lib 23.7.6 → 23.8.1
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 +34 -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,33 @@ export async function request(ctx, next) {
|
|
|
757
745
|
}
|
|
758
746
|
}
|
|
759
747
|
|
|
760
|
-
/** @deprecated */
|
|
761
748
|
export function createServer(options, ctx, middleware) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
749
|
+
if (middleware) {
|
|
750
|
+
middleware = [middleware].flat(16).filter(Boolean)
|
|
751
|
+
middleware = middleware.includes(requestMiddleware)
|
|
752
|
+
? middleware
|
|
753
|
+
: [requestMiddleware, ...middleware]
|
|
754
|
+
middleware = compose(middleware)
|
|
755
|
+
middleware = middleware.length === 0 ? null : middleware
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
let factory
|
|
759
|
+
if (typeof ctx === 'function') {
|
|
760
|
+
factory = ctx
|
|
761
|
+
} else {
|
|
762
|
+
let { logger, ...opaque } = ctx ?? {}
|
|
763
|
+
if (Object.keys(opaque).length === 0) {
|
|
764
|
+
opaque = null
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
factory = (req, res) => {
|
|
768
|
+
const context = new Context(req, res, ctx.logger ?? options?.logger)
|
|
769
|
+
if (opaque) {
|
|
770
|
+
Object.assign(context, opaque)
|
|
771
|
+
}
|
|
772
|
+
return context
|
|
773
|
+
}
|
|
774
|
+
}
|
|
766
775
|
|
|
767
776
|
const server = http.createServer(
|
|
768
777
|
{
|
|
@@ -773,7 +782,7 @@ export function createServer(options, ctx, middleware) {
|
|
|
773
782
|
requestTimeout: 0,
|
|
774
783
|
...options,
|
|
775
784
|
},
|
|
776
|
-
(req, res) => middleware
|
|
785
|
+
(req, res) => (middleware ? middleware(factory(req, res)) : factory(req, res)),
|
|
777
786
|
)
|
|
778
787
|
|
|
779
788
|
server.setTimeout(options.socketTimeout ?? options.timeout ?? 2 * 60e3)
|