@nxtedition/lib 23.9.0 → 23.9.2
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 +29 -27
- package/package.json +1 -1
- package/util/template/javascript.js +11 -1
package/http.js
CHANGED
|
@@ -146,7 +146,7 @@ function noop() {}
|
|
|
146
146
|
const pendingSet = (globalThis._nxt_lib_http_pending ??= new Set())
|
|
147
147
|
|
|
148
148
|
export async function upgradeMiddleware(ctx, next) {
|
|
149
|
-
const { req, socket
|
|
149
|
+
const { req, socket } = ctx
|
|
150
150
|
const startTime = performance.now()
|
|
151
151
|
|
|
152
152
|
let aborted = false
|
|
@@ -160,7 +160,6 @@ export async function upgradeMiddleware(ctx, next) {
|
|
|
160
160
|
})
|
|
161
161
|
|
|
162
162
|
const isHealthcheck = req.url === '/healthcheck' || req.url === '/_up'
|
|
163
|
-
const reqLogger = isHealthcheck ? null : logger.child({ req })
|
|
164
163
|
|
|
165
164
|
pendingSet.add(ctx)
|
|
166
165
|
try {
|
|
@@ -168,7 +167,9 @@ export async function upgradeMiddleware(ctx, next) {
|
|
|
168
167
|
req.resume() // Dump the body if there is one.
|
|
169
168
|
}
|
|
170
169
|
|
|
171
|
-
|
|
170
|
+
if (!isHealthcheck) {
|
|
171
|
+
ctx.logger?.debug({ req }, 'stream started')
|
|
172
|
+
}
|
|
172
173
|
|
|
173
174
|
const thenable = next()
|
|
174
175
|
|
|
@@ -183,15 +184,15 @@ export async function upgradeMiddleware(ctx, next) {
|
|
|
183
184
|
if (isHealthcheck) {
|
|
184
185
|
// Do nothing...
|
|
185
186
|
} else if (socket.errored) {
|
|
186
|
-
|
|
187
|
+
ctx.logger?.error({ err: socket.errored, req, socket, elapsedTime }, 'stream error')
|
|
187
188
|
} else if (!socket.writableEnded) {
|
|
188
|
-
|
|
189
|
+
ctx.logger?.debug({ req, socket, elapsedTime }, 'stream aborted')
|
|
189
190
|
} else if (socket.statusCode >= 500) {
|
|
190
|
-
|
|
191
|
+
ctx.logger?.error({ req, socket, elapsedTime }, 'stream error')
|
|
191
192
|
} else if (socket.statusCode >= 400) {
|
|
192
|
-
|
|
193
|
+
ctx.logger?.warn({ req, socket, elapsedTime }, 'stream failed')
|
|
193
194
|
} else {
|
|
194
|
-
|
|
195
|
+
ctx.logger?.debug({ req, socket, elapsedTime }, 'stream completed')
|
|
195
196
|
}
|
|
196
197
|
} catch (err) {
|
|
197
198
|
ctx[kAbortController]?.abort(err)
|
|
@@ -200,11 +201,11 @@ export async function upgradeMiddleware(ctx, next) {
|
|
|
200
201
|
const elapsedTime = performance.now() - startTime
|
|
201
202
|
|
|
202
203
|
if (req.aborted || aborted || (!socket.errored && socket.closed) || err.name === 'AbortError') {
|
|
203
|
-
|
|
204
|
+
ctx.logger?.debug({ err, req, socket, elapsedTime }, 'stream aborted')
|
|
204
205
|
} else if (statusCode < 500) {
|
|
205
|
-
|
|
206
|
+
ctx.logger?.warn({ err, req, socket, elapsedTime }, 'stream failed')
|
|
206
207
|
} else {
|
|
207
|
-
|
|
208
|
+
ctx.logger?.error({ err, req, socket, elapsedTime }, 'stream error')
|
|
208
209
|
}
|
|
209
210
|
socket.destroy(err)
|
|
210
211
|
} finally {
|
|
@@ -212,20 +213,19 @@ export async function upgradeMiddleware(ctx, next) {
|
|
|
212
213
|
|
|
213
214
|
if (!socket.writableEnded && !socket.destroyed) {
|
|
214
215
|
socket.destroy()
|
|
215
|
-
|
|
216
|
+
ctx.logger?.warn('socket destroyed')
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
220
|
|
|
220
221
|
export async function requestMiddleware(ctx, next) {
|
|
221
|
-
const { req, res
|
|
222
|
+
const { req, res } = ctx
|
|
222
223
|
const startTime = performance.now()
|
|
223
224
|
|
|
224
225
|
req.on('error', noop)
|
|
225
226
|
res.on('error', noop)
|
|
226
227
|
|
|
227
228
|
const isHealthcheck = req.url === '/healthcheck' || req.url === '/_up'
|
|
228
|
-
const reqLogger = isHealthcheck ? null : logger.child({ req })
|
|
229
229
|
|
|
230
230
|
pendingSet.add(ctx)
|
|
231
231
|
try {
|
|
@@ -233,7 +233,9 @@ export async function requestMiddleware(ctx, next) {
|
|
|
233
233
|
req.resume() // Dump the body if there is one.
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
|
|
236
|
+
if (!isHealthcheck) {
|
|
237
|
+
ctx.logger?.debug('request started')
|
|
238
|
+
}
|
|
237
239
|
|
|
238
240
|
if (ctx.id) {
|
|
239
241
|
res.setHeader('request-id', ctx.id)
|
|
@@ -252,15 +254,15 @@ export async function requestMiddleware(ctx, next) {
|
|
|
252
254
|
if (isHealthcheck) {
|
|
253
255
|
// Do nothing...
|
|
254
256
|
} else if (res.errored) {
|
|
255
|
-
|
|
257
|
+
ctx.logger?.error({ err: res.errored, req, res, elapsedTime }, 'request error')
|
|
256
258
|
} else if (!res.writableEnded) {
|
|
257
|
-
|
|
259
|
+
ctx.logger?.debug({ req, res, elapsedTime }, 'request aborted')
|
|
258
260
|
} else if (res.statusCode >= 500) {
|
|
259
|
-
|
|
261
|
+
ctx.logger?.error({ req, res, elapsedTime }, 'request error')
|
|
260
262
|
} else if (res.statusCode >= 400) {
|
|
261
|
-
|
|
263
|
+
ctx.logger?.warn({ req, res, elapsedTime }, 'request failed')
|
|
262
264
|
} else {
|
|
263
|
-
|
|
265
|
+
ctx.logger?.debug({ req, res, elapsedTime }, 'request completed')
|
|
264
266
|
}
|
|
265
267
|
} catch (err) {
|
|
266
268
|
ctx[kAbortController]?.abort(err)
|
|
@@ -300,7 +302,7 @@ export async function requestMiddleware(ctx, next) {
|
|
|
300
302
|
}
|
|
301
303
|
}
|
|
302
304
|
} else if (err.headers != null) {
|
|
303
|
-
|
|
305
|
+
ctx.logger?.warn({ req, err }, 'invalid headers')
|
|
304
306
|
}
|
|
305
307
|
|
|
306
308
|
if (fp.isPlainObject(err.body)) {
|
|
@@ -315,17 +317,17 @@ export async function requestMiddleware(ctx, next) {
|
|
|
315
317
|
}
|
|
316
318
|
|
|
317
319
|
if (statusCode < 500) {
|
|
318
|
-
|
|
320
|
+
ctx.logger?.warn({ req, res, err, elapsedTime }, 'request failed')
|
|
319
321
|
} else {
|
|
320
|
-
|
|
322
|
+
ctx.logger?.error({ req, res, err, elapsedTime }, 'request error')
|
|
321
323
|
}
|
|
322
324
|
} else {
|
|
323
325
|
if (req.aborted || (!res.errored && res.closed) || err.name === 'AbortError') {
|
|
324
|
-
|
|
326
|
+
ctx.logger?.debug({ req, res, err, elapsedTime }, 'request aborted')
|
|
325
327
|
} else if (statusCode < 500) {
|
|
326
|
-
|
|
328
|
+
ctx.logger?.warn({ req, res, err, elapsedTime }, 'request failed')
|
|
327
329
|
} else {
|
|
328
|
-
|
|
330
|
+
ctx.logger?.error({ req, res, err, elapsedTime }, 'request error')
|
|
329
331
|
}
|
|
330
332
|
res.destroy(err)
|
|
331
333
|
}
|
|
@@ -334,7 +336,7 @@ export async function requestMiddleware(ctx, next) {
|
|
|
334
336
|
|
|
335
337
|
if (!res.writableEnded && !res.destroyed) {
|
|
336
338
|
res.destroy()
|
|
337
|
-
|
|
339
|
+
ctx.logger?.warn({ req }, 'request destroyed')
|
|
338
340
|
}
|
|
339
341
|
}
|
|
340
342
|
}
|
package/package.json
CHANGED
|
@@ -45,13 +45,17 @@ class FetchEntry {
|
|
|
45
45
|
this.status = null
|
|
46
46
|
this.error = null
|
|
47
47
|
|
|
48
|
-
let { headers, body } = options
|
|
48
|
+
let { headers, body, method } = options ?? {}
|
|
49
49
|
|
|
50
50
|
if (fp.isPlainObject(body) || fp.isArray(body)) {
|
|
51
51
|
headers = { 'content-type': 'application/json', ...headers }
|
|
52
52
|
body = JSON.stringify(body)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
if (!method && body) {
|
|
56
|
+
method = 'POST'
|
|
57
|
+
}
|
|
58
|
+
|
|
55
59
|
try {
|
|
56
60
|
request(resource, {
|
|
57
61
|
...options,
|
|
@@ -573,6 +577,12 @@ export default function ({ ds, proxify, compiler, logger }) {
|
|
|
573
577
|
throw new Error(`invalid argument: state (${state})`)
|
|
574
578
|
}
|
|
575
579
|
|
|
580
|
+
if (typeof state === 'string') {
|
|
581
|
+
state = ds.record.STATE[state.toUpperCase()]
|
|
582
|
+
} else if (state == null) {
|
|
583
|
+
state = ds.record.STATE.SERVER
|
|
584
|
+
}
|
|
585
|
+
|
|
576
586
|
const key = '' + (name ?? '') + SEP + (path ?? '') + SEP + (state ?? '')
|
|
577
587
|
const entry = this._getEntry(key, RecordEntry, { ds, name, path, state })
|
|
578
588
|
|