@nxtedition/lib 20.2.3 → 20.3.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/couch.js +25 -33
- package/http.js +47 -15
- package/package.json +1 -1
- package/serializers.js +31 -9
package/couch.js
CHANGED
|
@@ -1136,53 +1136,45 @@ const defaultDispatcher = new Agent({
|
|
|
1136
1136
|
connections: 8,
|
|
1137
1137
|
})
|
|
1138
1138
|
|
|
1139
|
-
export function request(
|
|
1140
|
-
url
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
dispatcher = defaultDispatcher,
|
|
1147
|
-
signal = null,
|
|
1148
|
-
logger = null,
|
|
1149
|
-
stream = false,
|
|
1150
|
-
blocking = Boolean(stream),
|
|
1151
|
-
headerTimeout,
|
|
1152
|
-
bodyTimeout,
|
|
1153
|
-
} = {},
|
|
1154
|
-
) {
|
|
1155
|
-
signal?.throwIfAborted()
|
|
1156
|
-
|
|
1157
|
-
url = new URL(url)
|
|
1158
|
-
if (url.search) {
|
|
1159
|
-
throw new Error('invalid url: ' + url.href)
|
|
1139
|
+
export function request(url, opts) {
|
|
1140
|
+
if (typeof url === 'string') {
|
|
1141
|
+
opts = { url: new URL(url), ...opts }
|
|
1142
|
+
} else if (url instanceof URL) {
|
|
1143
|
+
opts = { url, ...opts }
|
|
1144
|
+
} else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
|
|
1145
|
+
opts = opts ? { ...url, ...opts } : url
|
|
1160
1146
|
}
|
|
1161
1147
|
|
|
1162
|
-
|
|
1148
|
+
if (opts == null && typeof url === 'object' && url != null) {
|
|
1149
|
+
opts = url
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
const ureq = {
|
|
1153
|
+
...opts,
|
|
1163
1154
|
url,
|
|
1164
|
-
method,
|
|
1165
|
-
|
|
1166
|
-
blocking,
|
|
1155
|
+
method: opts.method ?? (opts.body ? 'POST' : 'GET'),
|
|
1156
|
+
blocking: opts.blocking ?? Boolean(stream),
|
|
1167
1157
|
headers: {
|
|
1168
|
-
'content-type':
|
|
1158
|
+
'content-type':
|
|
1159
|
+
opts.body != null && typeof opts.body === 'object' ? 'application/json' : 'plain/text',
|
|
1169
1160
|
accept: 'application/json',
|
|
1170
|
-
...headers,
|
|
1161
|
+
...opts.headers,
|
|
1171
1162
|
},
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
headerTimeout,
|
|
1175
|
-
bodyTimeout,
|
|
1163
|
+
body:
|
|
1164
|
+
opts.body != null && typeof opts.body === 'object' ? JSON.stringify(opts.body) : opts.body,
|
|
1176
1165
|
}
|
|
1177
1166
|
|
|
1167
|
+
const dispatcher = opts.dispatcher ?? defaultDispatcher
|
|
1168
|
+
const signal = opts.signal
|
|
1169
|
+
|
|
1178
1170
|
if (stream) {
|
|
1179
1171
|
const handler = new StreamOutput({ signal, ...stream })
|
|
1180
|
-
dispatch(dispatcher,
|
|
1172
|
+
dispatch(dispatcher, ureq, handler)
|
|
1181
1173
|
return handler
|
|
1182
1174
|
} else {
|
|
1183
1175
|
return new Promise((resolve, reject) => {
|
|
1184
1176
|
const handler = new PromiseOutput({ resolve, reject, signal })
|
|
1185
|
-
dispatch(dispatcher,
|
|
1177
|
+
dispatch(dispatcher, ureq, handler)
|
|
1186
1178
|
})
|
|
1187
1179
|
}
|
|
1188
1180
|
}
|
package/http.js
CHANGED
|
@@ -163,41 +163,73 @@ export async function request(ctx, next) {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
export class ServerResponse extends http.ServerResponse {
|
|
166
|
+
#now = 0
|
|
167
|
+
#created = 0
|
|
168
|
+
#timing = {
|
|
169
|
+
connect: -1,
|
|
170
|
+
headers: -1,
|
|
171
|
+
data: -1,
|
|
172
|
+
complete: -1,
|
|
173
|
+
error: -1,
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
get timing() {
|
|
177
|
+
return this.#timing
|
|
178
|
+
}
|
|
179
|
+
|
|
166
180
|
constructor(req) {
|
|
167
181
|
super(req)
|
|
168
|
-
|
|
169
|
-
this
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
182
|
+
|
|
183
|
+
this.#created = performance.now()
|
|
184
|
+
this.#now = this.#created
|
|
185
|
+
this.#timing.connect = 0
|
|
173
186
|
}
|
|
174
187
|
|
|
175
188
|
flushHeaders() {
|
|
176
|
-
if (this.
|
|
177
|
-
this.
|
|
189
|
+
if (this.#timing.headers === -1) {
|
|
190
|
+
this.#timing.headers = performance.now() - this.#now
|
|
191
|
+
this.#now += this.#timing.headers
|
|
178
192
|
}
|
|
179
193
|
return super.flushHeaders()
|
|
180
194
|
}
|
|
181
195
|
|
|
182
196
|
write(chunk, encoding, callback) {
|
|
183
|
-
if (this.
|
|
184
|
-
this.
|
|
197
|
+
if (this.#timing.data === -1) {
|
|
198
|
+
this.#timing.data = performance.now() - this.#now
|
|
199
|
+
this.#now += this.#timing.data
|
|
185
200
|
}
|
|
186
|
-
|
|
187
|
-
|
|
201
|
+
|
|
202
|
+
if (this.#timing.headers === -1) {
|
|
203
|
+
this.#timing.headers = this.#timing.data
|
|
188
204
|
}
|
|
205
|
+
|
|
189
206
|
return super.write(chunk, encoding, callback)
|
|
190
207
|
}
|
|
191
208
|
|
|
192
209
|
end(chunk, encoding, callback) {
|
|
193
|
-
if (this.
|
|
194
|
-
this.
|
|
210
|
+
if (this.#timing.data === -1) {
|
|
211
|
+
this.#timing.data = performance.now() - this.#now
|
|
212
|
+
this.#now += this.#timing.data
|
|
195
213
|
}
|
|
196
|
-
|
|
197
|
-
|
|
214
|
+
|
|
215
|
+
if (this.#timing.headers === -1) {
|
|
216
|
+
this.#timing.headers = this.#timing.data
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (this.#timing.complete === -1) {
|
|
220
|
+
this.#timing.complete = performance.now() - this.#created
|
|
198
221
|
}
|
|
222
|
+
|
|
199
223
|
return super.end(chunk, encoding, callback)
|
|
200
224
|
}
|
|
225
|
+
|
|
226
|
+
destroy(err) {
|
|
227
|
+
if (err != null && this.#timing.error === -1) {
|
|
228
|
+
this.#timing.error = performance.now() - this.#created
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return super.destroy(err)
|
|
232
|
+
}
|
|
201
233
|
}
|
|
202
234
|
|
|
203
235
|
export function createServer(options, ctx, middleware) {
|
package/package.json
CHANGED
package/serializers.js
CHANGED
|
@@ -21,19 +21,25 @@ function getHeaders(obj) {
|
|
|
21
21
|
)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
function getTiming(obj) {
|
|
25
|
+
const timing = obj?.timing ?? obj.stats
|
|
26
|
+
if (!timing) {
|
|
27
|
+
return undefined
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
created: timing.created ?? -1,
|
|
31
|
+
connect: timing.connect ?? -1,
|
|
32
|
+
headers: timing.headers ?? -1,
|
|
33
|
+
data: timing.data ?? -1,
|
|
34
|
+
complete: timing.complete ?? -1,
|
|
35
|
+
error: timing.error ?? -1,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
24
39
|
export default {
|
|
25
40
|
data: (data) =>
|
|
26
41
|
data != null && typeof data === 'object' ? JSON.stringify(data, undefined, 2) : data,
|
|
27
42
|
err: (err) => errSerializer(err),
|
|
28
|
-
res: (res) =>
|
|
29
|
-
res && {
|
|
30
|
-
id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
|
|
31
|
-
stats: res.stats,
|
|
32
|
-
statusCode: res.statusCode || res.status,
|
|
33
|
-
bytesWritten: res.bytesWritten,
|
|
34
|
-
headers: getHeaders(res),
|
|
35
|
-
headersSent: res.headersSent,
|
|
36
|
-
},
|
|
37
43
|
socket: (socket) =>
|
|
38
44
|
socket && {
|
|
39
45
|
id: socket.id || null,
|
|
@@ -43,21 +49,35 @@ export default {
|
|
|
43
49
|
remoteAddress: socket.remoteAddress ?? null,
|
|
44
50
|
headers: socket.headers,
|
|
45
51
|
},
|
|
52
|
+
res: (res) =>
|
|
53
|
+
res && {
|
|
54
|
+
id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
|
|
55
|
+
timing: getTiming(res),
|
|
56
|
+
statusCode: res.statusCode || res.status,
|
|
57
|
+
bytesWritten: res.bytesWritten,
|
|
58
|
+
bytesWrittenPerSecond: res.bytesWrittenPerSecond,
|
|
59
|
+
headers: getHeaders(res),
|
|
60
|
+
headersSent: res.headersSent,
|
|
61
|
+
},
|
|
46
62
|
req: (req) =>
|
|
47
63
|
req && {
|
|
48
64
|
id: req.id || getHeader(req, 'request-id'),
|
|
65
|
+
timing: getTiming(req),
|
|
49
66
|
method: req.method,
|
|
50
67
|
url: req.url,
|
|
51
68
|
headers: getHeaders(req),
|
|
52
69
|
bytesRead: req.bytesRead,
|
|
70
|
+
bytesReadPerSecond: req.bytesReadPerSecond,
|
|
53
71
|
remoteAddress: req.socket?.remoteAddress,
|
|
54
72
|
remotePort: req.socket?.remotePort,
|
|
55
73
|
},
|
|
56
74
|
ures: (ures) =>
|
|
57
75
|
ures && {
|
|
58
76
|
id: ures.id || getHeader(ures, 'request-id') || getHeader(ures.req, 'request-id'),
|
|
77
|
+
timing: getTiming(ures),
|
|
59
78
|
statusCode: ures.statusCode ?? ures.status,
|
|
60
79
|
bytesRead: ures.bytesRead,
|
|
80
|
+
bytesReadPerSecond: ures.bytesReadPerSecond,
|
|
61
81
|
body: typeof ures.body === 'string' ? ures.body : null,
|
|
62
82
|
headers: getHeaders(ures),
|
|
63
83
|
},
|
|
@@ -80,10 +100,12 @@ export default {
|
|
|
80
100
|
|
|
81
101
|
return {
|
|
82
102
|
id: ureq.id || getHeader(ureq, 'request-id'),
|
|
103
|
+
timing: getTiming(ureq),
|
|
83
104
|
method: ureq.method,
|
|
84
105
|
url,
|
|
85
106
|
body: typeof ureq.body === 'string' ? ureq.body : null,
|
|
86
107
|
bytesWritten: ureq.bytesWritten,
|
|
108
|
+
bytesWrittenPerSecond: ureq.bytesWrittenPerSecond,
|
|
87
109
|
headers: getHeaders(ureq),
|
|
88
110
|
query: ureq.query,
|
|
89
111
|
}
|