@nxtedition/lib 25.1.1 → 25.1.3
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 +67 -6
- package/package.json +1 -2
package/http.js
CHANGED
|
@@ -4,7 +4,6 @@ import createError from 'http-errors'
|
|
|
4
4
|
import { performance } from 'perf_hooks'
|
|
5
5
|
import requestTarget from 'request-target'
|
|
6
6
|
import querystring from 'fast-querystring'
|
|
7
|
-
import compose from 'koa-compose'
|
|
8
7
|
import http from 'http'
|
|
9
8
|
import fp from 'lodash/fp.js'
|
|
10
9
|
import tp from 'timers/promises'
|
|
@@ -220,17 +219,14 @@ export async function requestMiddleware(ctx, next) {
|
|
|
220
219
|
const { req, res } = ctx
|
|
221
220
|
const startTime = performance.now()
|
|
222
221
|
|
|
223
|
-
req.on('error', noop)
|
|
224
|
-
res.on('error', noop)
|
|
225
|
-
|
|
226
|
-
const isHealthcheck = req.url === '/healthcheck' || req.url === '/_up'
|
|
227
|
-
|
|
228
222
|
pendingSet.add(ctx)
|
|
229
223
|
try {
|
|
230
224
|
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
|
|
231
225
|
req.resume() // Dump the body if there is one.
|
|
232
226
|
}
|
|
233
227
|
|
|
228
|
+
const isHealthcheck = req.url === '/healthcheck' || req.url === '/_up'
|
|
229
|
+
|
|
234
230
|
if (!isHealthcheck) {
|
|
235
231
|
ctx.logger?.debug({ req }, 'request started')
|
|
236
232
|
}
|
|
@@ -242,6 +238,8 @@ export async function requestMiddleware(ctx, next) {
|
|
|
242
238
|
const thenable = next()
|
|
243
239
|
|
|
244
240
|
if (thenable?.then) {
|
|
241
|
+
req.on('error', noop)
|
|
242
|
+
res.on('error', noop)
|
|
245
243
|
await thenable
|
|
246
244
|
}
|
|
247
245
|
|
|
@@ -951,3 +949,66 @@ export async function retry(fn, options) {
|
|
|
951
949
|
}
|
|
952
950
|
}
|
|
953
951
|
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* @param {Array} middleware
|
|
955
|
+
* @return {Function}
|
|
956
|
+
*/
|
|
957
|
+
const composeSlim = (middleware) => (ctx, next) => {
|
|
958
|
+
const dispatch = (i) => () => {
|
|
959
|
+
const fn = i === middleware.length ? next : middleware[i]
|
|
960
|
+
return fn ? fn(ctx, dispatch(i + 1)) : undefined
|
|
961
|
+
}
|
|
962
|
+
return dispatch(0)()
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/** @typedef {import("koa").Middleware} Middleware */
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* Compose `middleware` returning
|
|
969
|
+
* a fully valid middleware comprised
|
|
970
|
+
* of all those which are passed.
|
|
971
|
+
*
|
|
972
|
+
* @param {...(Middleware | Middleware[])} middleware
|
|
973
|
+
* @return {Middleware}
|
|
974
|
+
* @api public
|
|
975
|
+
*/
|
|
976
|
+
|
|
977
|
+
export const compose = (...middleware) => {
|
|
978
|
+
const funcs = middleware.flat()
|
|
979
|
+
|
|
980
|
+
for (const fn of funcs) {
|
|
981
|
+
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
if (process.env.NODE_ENV === 'production') {
|
|
985
|
+
return composeSlim(funcs)
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
return async (ctx, next) => {
|
|
989
|
+
const dispatch = async (i) => {
|
|
990
|
+
const fn = i === funcs.length ? next : funcs[i]
|
|
991
|
+
if (!fn) return
|
|
992
|
+
|
|
993
|
+
let nextCalled = false
|
|
994
|
+
let nextResolved = false
|
|
995
|
+
const nextProxy = async () => {
|
|
996
|
+
if (nextCalled) throw Error('next() called multiple times')
|
|
997
|
+
nextCalled = true
|
|
998
|
+
try {
|
|
999
|
+
return await dispatch(i + 1)
|
|
1000
|
+
} finally {
|
|
1001
|
+
nextResolved = true
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
const result = await fn(ctx, nextProxy)
|
|
1005
|
+
if (nextCalled && !nextResolved) {
|
|
1006
|
+
throw Error(
|
|
1007
|
+
'Middleware resolved before downstream.\n\tYou are probably missing an await or return',
|
|
1008
|
+
)
|
|
1009
|
+
}
|
|
1010
|
+
return result
|
|
1011
|
+
}
|
|
1012
|
+
return dispatch(0)
|
|
1013
|
+
}
|
|
1014
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "25.1.
|
|
3
|
+
"version": "25.1.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -78,7 +78,6 @@
|
|
|
78
78
|
"hasha": "^6.0.0",
|
|
79
79
|
"http-errors": "^2.0.0",
|
|
80
80
|
"json5": "^2.2.3",
|
|
81
|
-
"koa-compose": "^4.1.0",
|
|
82
81
|
"lodash": "^4.17.21",
|
|
83
82
|
"lru-cache": "^11.1.0",
|
|
84
83
|
"mime": "^4.0.7",
|