@nxtedition/http 1.0.1 → 1.0.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/lib/index.d.ts +7 -7
- package/lib/index.js +23 -18
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -94,7 +94,7 @@ export declare class Http2ServerResponse extends http2.Http2ServerResponse {
|
|
|
94
94
|
end(data: string | Uint8Array, encoding: BufferEncoding, callback?: () => void): this;
|
|
95
95
|
destroy(err?: Error): this;
|
|
96
96
|
}
|
|
97
|
-
interface CreateServerOptions extends http.ServerOptions {
|
|
97
|
+
export interface CreateServerOptions extends http.ServerOptions {
|
|
98
98
|
logger?: Logger;
|
|
99
99
|
signal?: AbortSignal;
|
|
100
100
|
socketTimeout?: number;
|
|
@@ -102,11 +102,11 @@ interface CreateServerOptions extends http.ServerOptions {
|
|
|
102
102
|
headersTimeout?: number;
|
|
103
103
|
requestTimeout?: number;
|
|
104
104
|
}
|
|
105
|
-
type ContextFactory = (req: IncomingMessage, res: ServerResponse) => Context;
|
|
106
|
-
type ContextOptions = Record<string, unknown> & {
|
|
105
|
+
export type ContextFactory = (req: IncomingMessage, res: ServerResponse) => Context;
|
|
106
|
+
export type ContextOptions = Record<string, unknown> & {
|
|
107
107
|
logger?: Logger;
|
|
108
108
|
};
|
|
109
|
-
export declare function createServer(ctx: ContextFactory | ContextOptions, middleware?:
|
|
110
|
-
export declare function createServer(options: CreateServerOptions, ctx: ContextOptions | ContextFactory, middleware?:
|
|
111
|
-
type
|
|
112
|
-
export declare const compose: (...middleware: (
|
|
109
|
+
export declare function createServer(ctx: ContextFactory | ContextOptions, middleware?: Middleware | Middleware[]): http.Server;
|
|
110
|
+
export declare function createServer(options: CreateServerOptions, ctx: ContextOptions | ContextFactory, middleware?: Middleware | Middleware[]): http.Server;
|
|
111
|
+
export type Middleware = (ctx: Context, next: () => void | Promise<void>) => void | Promise<void>;
|
|
112
|
+
export declare const compose: (...middleware: (Middleware | Middleware[])[]) => Middleware;
|
package/lib/index.js
CHANGED
|
@@ -932,7 +932,7 @@ export class Http2ServerResponse extends http2.Http2ServerResponse {
|
|
|
932
932
|
}
|
|
933
933
|
}
|
|
934
934
|
|
|
935
|
-
|
|
935
|
+
|
|
936
936
|
|
|
937
937
|
|
|
938
938
|
|
|
@@ -941,32 +941,32 @@ export class Http2ServerResponse extends http2.Http2ServerResponse {
|
|
|
941
941
|
|
|
942
942
|
|
|
943
943
|
|
|
944
|
-
|
|
945
|
-
|
|
944
|
+
|
|
945
|
+
|
|
946
946
|
|
|
947
947
|
const httpServers = ((globalThis ).__nxt_lib_http_servers =
|
|
948
948
|
new Array ())
|
|
949
949
|
|
|
950
950
|
|
|
951
|
-
|
|
951
|
+
|
|
952
952
|
|
|
953
953
|
|
|
954
954
|
|
|
955
955
|
|
|
956
|
-
|
|
956
|
+
|
|
957
957
|
|
|
958
958
|
export function createServer(...args ) {
|
|
959
959
|
let options
|
|
960
960
|
let ctx
|
|
961
|
-
let middleware
|
|
961
|
+
let middleware
|
|
962
962
|
|
|
963
963
|
if (typeof args[0] === 'function') {
|
|
964
964
|
ctx = args[0]
|
|
965
|
-
middleware = args[1]
|
|
965
|
+
middleware = args[1]
|
|
966
966
|
} else {
|
|
967
967
|
options = args[0]
|
|
968
968
|
ctx = args[1]
|
|
969
|
-
middleware = args[2]
|
|
969
|
+
middleware = args[2]
|
|
970
970
|
}
|
|
971
971
|
|
|
972
972
|
if (middleware) {
|
|
@@ -975,6 +975,8 @@ export function createServer(...args ) {
|
|
|
975
975
|
? middleware
|
|
976
976
|
: [requestMiddleware, ...middleware]
|
|
977
977
|
middleware = compose(middleware)
|
|
978
|
+
} else {
|
|
979
|
+
middleware = requestMiddleware
|
|
978
980
|
}
|
|
979
981
|
|
|
980
982
|
let factory
|
|
@@ -1003,10 +1005,13 @@ export function createServer(...args ) {
|
|
|
1003
1005
|
requestTimeout: 48 * 60e3,
|
|
1004
1006
|
...options,
|
|
1005
1007
|
},
|
|
1006
|
-
(req, res) =>
|
|
1007
|
-
middleware
|
|
1008
|
-
|
|
1009
|
-
|
|
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
|
+
},
|
|
1010
1015
|
)
|
|
1011
1016
|
|
|
1012
1017
|
server.setTimeout(options?.socketTimeout ?? 2 * 60e3)
|
|
@@ -1022,19 +1027,19 @@ export function createServer(...args ) {
|
|
|
1022
1027
|
return server
|
|
1023
1028
|
}
|
|
1024
1029
|
|
|
1025
|
-
|
|
1030
|
+
|
|
1026
1031
|
|
|
1027
1032
|
const composeSlim =
|
|
1028
|
-
(middleware
|
|
1033
|
+
(middleware ) =>
|
|
1029
1034
|
(ctx, next) => {
|
|
1030
|
-
const dispatch = (i ) => ()
|
|
1035
|
+
const dispatch = (i ) => () => {
|
|
1031
1036
|
const fn = i === middleware.length ? next : middleware[i]
|
|
1032
1037
|
return fn ? fn(ctx, dispatch(i + 1)) : undefined
|
|
1033
1038
|
}
|
|
1034
1039
|
return dispatch(0)()
|
|
1035
1040
|
}
|
|
1036
1041
|
|
|
1037
|
-
export const compose = (...middleware
|
|
1042
|
+
export const compose = (...middleware ) => {
|
|
1038
1043
|
const funcs = middleware.flat()
|
|
1039
1044
|
|
|
1040
1045
|
for (const fn of funcs) {
|
|
@@ -1047,8 +1052,8 @@ export const compose = (...middleware )
|
|
|
1047
1052
|
return composeSlim(funcs)
|
|
1048
1053
|
}
|
|
1049
1054
|
|
|
1050
|
-
return async (ctx , next
|
|
1051
|
-
const dispatch = async (i )
|
|
1055
|
+
return async (ctx , next = noop) => {
|
|
1056
|
+
const dispatch = async (i ) => {
|
|
1052
1057
|
const fn = i === funcs.length ? next : funcs[i]
|
|
1053
1058
|
if (!fn) {
|
|
1054
1059
|
return
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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": "
|
|
35
|
+
"gitHead": "be57f23ab8b0c6d10e8a5d600e0d5f15a9e209d5"
|
|
36
36
|
}
|