hono 4.7.1 → 4.7.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/dist/cjs/compose.js +8 -14
- package/dist/cjs/helper/proxy/index.js +23 -9
- package/dist/compose.js +8 -14
- package/dist/helper/proxy/index.js +23 -9
- package/dist/types/compose.d.ts +4 -19
- package/package.json +1 -1
package/dist/cjs/compose.js
CHANGED
|
@@ -21,11 +21,9 @@ __export(compose_exports, {
|
|
|
21
21
|
compose: () => compose
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(compose_exports);
|
|
24
|
-
var import_context = require("./context");
|
|
25
24
|
const compose = (middleware, onError, onNotFound) => {
|
|
26
25
|
return (context, next) => {
|
|
27
26
|
let index = -1;
|
|
28
|
-
const isContext = context instanceof import_context.Context;
|
|
29
27
|
return dispatch(0);
|
|
30
28
|
async function dispatch(i) {
|
|
31
29
|
if (i <= index) {
|
|
@@ -37,23 +35,15 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
37
35
|
let handler;
|
|
38
36
|
if (middleware[i]) {
|
|
39
37
|
handler = middleware[i][0][0];
|
|
40
|
-
|
|
41
|
-
context.req.routeIndex = i;
|
|
42
|
-
}
|
|
38
|
+
context.req.routeIndex = i;
|
|
43
39
|
} else {
|
|
44
40
|
handler = i === middleware.length && next || void 0;
|
|
45
41
|
}
|
|
46
|
-
if (
|
|
47
|
-
if (isContext && context.finalized === false && onNotFound) {
|
|
48
|
-
res = await onNotFound(context);
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
42
|
+
if (handler) {
|
|
51
43
|
try {
|
|
52
|
-
res = await handler(context, () =>
|
|
53
|
-
return dispatch(i + 1);
|
|
54
|
-
});
|
|
44
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
55
45
|
} catch (err) {
|
|
56
|
-
if (err instanceof Error &&
|
|
46
|
+
if (err instanceof Error && onError) {
|
|
57
47
|
context.error = err;
|
|
58
48
|
res = await onError(err, context);
|
|
59
49
|
isError = true;
|
|
@@ -61,6 +51,10 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
61
51
|
throw err;
|
|
62
52
|
}
|
|
63
53
|
}
|
|
54
|
+
} else {
|
|
55
|
+
if (context.finalized === false && onNotFound) {
|
|
56
|
+
res = await onNotFound(context);
|
|
57
|
+
}
|
|
64
58
|
}
|
|
65
59
|
if (res && (context.finalized === false || isError)) {
|
|
66
60
|
context.res = res;
|
|
@@ -42,18 +42,31 @@ const buildRequestInitFromRequest = (request) => {
|
|
|
42
42
|
method: request.method,
|
|
43
43
|
body: request.body,
|
|
44
44
|
duplex: request.body ? "half" : void 0,
|
|
45
|
-
headers
|
|
45
|
+
headers,
|
|
46
|
+
signal: request.signal
|
|
46
47
|
};
|
|
47
48
|
};
|
|
49
|
+
const preprocessRequestInit = (requestInit) => {
|
|
50
|
+
if (!requestInit.headers || Array.isArray(requestInit.headers) || requestInit.headers instanceof Headers) {
|
|
51
|
+
return requestInit;
|
|
52
|
+
}
|
|
53
|
+
const headers = new Headers();
|
|
54
|
+
for (const [key, value] of Object.entries(requestInit.headers)) {
|
|
55
|
+
if (value == null) {
|
|
56
|
+
headers.delete(key);
|
|
57
|
+
} else {
|
|
58
|
+
headers.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
requestInit.headers = headers;
|
|
62
|
+
return requestInit;
|
|
63
|
+
};
|
|
48
64
|
const proxy = async (input, proxyInit) => {
|
|
49
65
|
const { raw, ...requestInit } = proxyInit ?? {};
|
|
50
|
-
const req = new Request(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...requestInit
|
|
55
|
-
}
|
|
56
|
-
);
|
|
66
|
+
const req = new Request(input, {
|
|
67
|
+
...buildRequestInitFromRequest(raw),
|
|
68
|
+
...preprocessRequestInit(requestInit)
|
|
69
|
+
});
|
|
57
70
|
req.headers.delete("accept-encoding");
|
|
58
71
|
const res = await fetch(req);
|
|
59
72
|
const resHeaders = new Headers(res.headers);
|
|
@@ -65,7 +78,8 @@ const proxy = async (input, proxyInit) => {
|
|
|
65
78
|
resHeaders.delete("content-length");
|
|
66
79
|
}
|
|
67
80
|
return new Response(res.body, {
|
|
68
|
-
|
|
81
|
+
status: res.status,
|
|
82
|
+
statusText: res.statusText,
|
|
69
83
|
headers: resHeaders
|
|
70
84
|
});
|
|
71
85
|
};
|
package/dist/compose.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
// src/compose.ts
|
|
2
|
-
import { Context } from "./context.js";
|
|
3
2
|
var compose = (middleware, onError, onNotFound) => {
|
|
4
3
|
return (context, next) => {
|
|
5
4
|
let index = -1;
|
|
6
|
-
const isContext = context instanceof Context;
|
|
7
5
|
return dispatch(0);
|
|
8
6
|
async function dispatch(i) {
|
|
9
7
|
if (i <= index) {
|
|
@@ -15,23 +13,15 @@ var compose = (middleware, onError, onNotFound) => {
|
|
|
15
13
|
let handler;
|
|
16
14
|
if (middleware[i]) {
|
|
17
15
|
handler = middleware[i][0][0];
|
|
18
|
-
|
|
19
|
-
context.req.routeIndex = i;
|
|
20
|
-
}
|
|
16
|
+
context.req.routeIndex = i;
|
|
21
17
|
} else {
|
|
22
18
|
handler = i === middleware.length && next || void 0;
|
|
23
19
|
}
|
|
24
|
-
if (
|
|
25
|
-
if (isContext && context.finalized === false && onNotFound) {
|
|
26
|
-
res = await onNotFound(context);
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
20
|
+
if (handler) {
|
|
29
21
|
try {
|
|
30
|
-
res = await handler(context, () =>
|
|
31
|
-
return dispatch(i + 1);
|
|
32
|
-
});
|
|
22
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
33
23
|
} catch (err) {
|
|
34
|
-
if (err instanceof Error &&
|
|
24
|
+
if (err instanceof Error && onError) {
|
|
35
25
|
context.error = err;
|
|
36
26
|
res = await onError(err, context);
|
|
37
27
|
isError = true;
|
|
@@ -39,6 +29,10 @@ var compose = (middleware, onError, onNotFound) => {
|
|
|
39
29
|
throw err;
|
|
40
30
|
}
|
|
41
31
|
}
|
|
32
|
+
} else {
|
|
33
|
+
if (context.finalized === false && onNotFound) {
|
|
34
|
+
res = await onNotFound(context);
|
|
35
|
+
}
|
|
42
36
|
}
|
|
43
37
|
if (res && (context.finalized === false || isError)) {
|
|
44
38
|
context.res = res;
|
|
@@ -20,18 +20,31 @@ var buildRequestInitFromRequest = (request) => {
|
|
|
20
20
|
method: request.method,
|
|
21
21
|
body: request.body,
|
|
22
22
|
duplex: request.body ? "half" : void 0,
|
|
23
|
-
headers
|
|
23
|
+
headers,
|
|
24
|
+
signal: request.signal
|
|
24
25
|
};
|
|
25
26
|
};
|
|
27
|
+
var preprocessRequestInit = (requestInit) => {
|
|
28
|
+
if (!requestInit.headers || Array.isArray(requestInit.headers) || requestInit.headers instanceof Headers) {
|
|
29
|
+
return requestInit;
|
|
30
|
+
}
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
for (const [key, value] of Object.entries(requestInit.headers)) {
|
|
33
|
+
if (value == null) {
|
|
34
|
+
headers.delete(key);
|
|
35
|
+
} else {
|
|
36
|
+
headers.set(key, value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
requestInit.headers = headers;
|
|
40
|
+
return requestInit;
|
|
41
|
+
};
|
|
26
42
|
var proxy = async (input, proxyInit) => {
|
|
27
43
|
const { raw, ...requestInit } = proxyInit ?? {};
|
|
28
|
-
const req = new Request(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
...requestInit
|
|
33
|
-
}
|
|
34
|
-
);
|
|
44
|
+
const req = new Request(input, {
|
|
45
|
+
...buildRequestInitFromRequest(raw),
|
|
46
|
+
...preprocessRequestInit(requestInit)
|
|
47
|
+
});
|
|
35
48
|
req.headers.delete("accept-encoding");
|
|
36
49
|
const res = await fetch(req);
|
|
37
50
|
const resHeaders = new Headers(res.headers);
|
|
@@ -43,7 +56,8 @@ var proxy = async (input, proxyInit) => {
|
|
|
43
56
|
resHeaders.delete("content-length");
|
|
44
57
|
}
|
|
45
58
|
return new Response(res.body, {
|
|
46
|
-
|
|
59
|
+
status: res.status,
|
|
60
|
+
statusText: res.statusText,
|
|
47
61
|
headers: resHeaders
|
|
48
62
|
});
|
|
49
63
|
};
|
package/dist/types/compose.d.ts
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
|
+
import type { Context } from './context';
|
|
1
2
|
import type { Env, ErrorHandler, NotFoundHandler } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Interface representing the context for a composition operation.
|
|
4
|
-
*/
|
|
5
|
-
interface ComposeContext {
|
|
6
|
-
/**
|
|
7
|
-
* Indicates whether the composition process has been finalized.
|
|
8
|
-
*/
|
|
9
|
-
finalized: boolean;
|
|
10
|
-
/**
|
|
11
|
-
* The result of the composition process. The type is unknown and should be
|
|
12
|
-
* specified based on the context where this interface is used.
|
|
13
|
-
*/
|
|
14
|
-
res: unknown;
|
|
15
|
-
}
|
|
16
3
|
/**
|
|
17
4
|
* Compose middleware functions into a single function based on `koa-compose` package.
|
|
18
5
|
*
|
|
19
|
-
* @template C - The context type.
|
|
20
6
|
* @template E - The environment type.
|
|
21
7
|
*
|
|
22
8
|
* @param {[[Function, unknown], ParamIndexMap | Params][]} middleware - An array of middleware functions and their corresponding parameters.
|
|
23
9
|
* @param {ErrorHandler<E>} [onError] - An optional error handler function.
|
|
24
10
|
* @param {NotFoundHandler<E>} [onNotFound] - An optional not-found handler function.
|
|
25
11
|
*
|
|
26
|
-
* @returns {(context:
|
|
12
|
+
* @returns {(context: Context, next?: Function) => Promise<>} - A composed middleware function.
|
|
27
13
|
*/
|
|
28
|
-
export declare const compose: <
|
|
14
|
+
export declare const compose: <E extends Env = Env>(middleware: [
|
|
29
15
|
[
|
|
30
16
|
Function,
|
|
31
17
|
unknown
|
|
@@ -35,5 +21,4 @@ export declare const compose: <C extends ComposeContext, E extends Env = Env>(mi
|
|
|
35
21
|
[
|
|
36
22
|
Function
|
|
37
23
|
]
|
|
38
|
-
][], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E>) => ((context:
|
|
39
|
-
export {};
|
|
24
|
+
][], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E>) => ((context: Context, next?: Function) => Promise<Context>);
|