@nxtedition/http 1.0.0 → 1.0.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/lib/index.d.ts CHANGED
@@ -108,5 +108,5 @@ type ContextOptions = Record<string, unknown> & {
108
108
  };
109
109
  export declare function createServer(ctx: ContextFactory | ContextOptions, middleware?: MiddlewareFn | MiddlewareFn[]): http.Server;
110
110
  export declare function createServer(options: CreateServerOptions, ctx: ContextOptions | ContextFactory, middleware?: MiddlewareFn | MiddlewareFn[]): http.Server;
111
- type MiddlewareFn = (ctx: Context, next: () => unknown) => unknown;
111
+ type MiddlewareFn = (ctx: Context, next: () => void | Promise<void>) => void | Promise<void>;
112
112
  export declare const compose: (...middleware: (MiddlewareFn | MiddlewareFn[])[]) => MiddlewareFn;
package/lib/index.js CHANGED
@@ -220,10 +220,10 @@ export async function upgradeMiddleware(
220
220
  reqLogger?.debug({ req }, 'request started')
221
221
  }
222
222
 
223
- const thenable = next()
223
+ const thenable = next()
224
224
 
225
- if (Object.hasOwnProperty.call(thenable, 'then')) {
226
- await thenable
225
+ if (thenable?.then) {
226
+ await (thenable )
227
227
  }
228
228
 
229
229
  if (!socket.destroyed && !socket.writableEnded) {
@@ -323,7 +323,11 @@ export async function requestMiddleware(
323
323
  r._read()
324
324
  }
325
325
 
326
- await next()
326
+ const thenable = next()
327
+
328
+ if (thenable?.then) {
329
+ await (thenable )
330
+ }
327
331
 
328
332
  if (!res.destroyed && !res.writableEnded) {
329
333
  throw new Error('Response not completed')
@@ -971,6 +975,8 @@ export function createServer(...args ) {
971
975
  ? middleware
972
976
  : [requestMiddleware, ...middleware]
973
977
  middleware = compose(middleware)
978
+ } else {
979
+ middleware = requestMiddleware
974
980
  }
975
981
 
976
982
  let factory
@@ -999,10 +1005,13 @@ export function createServer(...args ) {
999
1005
  requestTimeout: 48 * 60e3,
1000
1006
  ...options,
1001
1007
  },
1002
- (req, res) =>
1003
- middleware
1004
- ? middleware(factory(req , res ), noop)
1005
- : factory(req , res ),
1008
+ (req, res) => {
1009
+ const ret = middleware(factory(req , res ), noop)
1010
+ ret?.catch((err) => {
1011
+ res.on('error', noop)
1012
+ res.destroy(err )
1013
+ })
1014
+ },
1006
1015
  )
1007
1016
 
1008
1017
  server.setTimeout(options?.socketTimeout ?? 2 * 60e3)
@@ -1018,12 +1027,12 @@ export function createServer(...args ) {
1018
1027
  return server
1019
1028
  }
1020
1029
 
1021
-
1030
+
1022
1031
 
1023
1032
  const composeSlim =
1024
1033
  (middleware ) =>
1025
1034
  (ctx, next) => {
1026
- const dispatch = (i ) => () => {
1035
+ const dispatch = (i ) => () => {
1027
1036
  const fn = i === middleware.length ? next : middleware[i]
1028
1037
  return fn ? fn(ctx, dispatch(i + 1)) : undefined
1029
1038
  }
@@ -1043,8 +1052,8 @@ export const compose = (...middleware )
1043
1052
  return composeSlim(funcs)
1044
1053
  }
1045
1054
 
1046
- return async (ctx , next = noop) => {
1047
- const dispatch = async (i ) => {
1055
+ return async (ctx , next = noop) => {
1056
+ const dispatch = async (i ) => {
1048
1057
  const fn = i === funcs.length ? next : funcs[i]
1049
1058
  if (!fn) {
1050
1059
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/http",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -32,5 +32,5 @@
32
32
  "rimraf": "^6.1.2",
33
33
  "typescript": "^5.9.3"
34
34
  },
35
- "gitHead": "64c1bd97cf9a785ca4a750b5380418878cbed2d2"
35
+ "gitHead": "37c6127808e4ed12b94810c04dc9788afaad7222"
36
36
  }