hono 4.0.10 → 4.1.0
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/adapter/aws-lambda/handler.js +1 -1
- package/dist/adapter/bun/index.js +2 -0
- package/dist/adapter/bun/websocket.js +77 -0
- package/dist/adapter/cloudflare-workers/index.js +3 -1
- package/dist/adapter/cloudflare-workers/websocket.js +57 -0
- package/dist/adapter/deno/index.js +3 -1
- package/dist/adapter/deno/websocket.js +29 -0
- package/dist/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/adapter/aws-lambda/handler.js +1 -1
- package/dist/cjs/adapter/bun/index.js +3 -0
- package/dist/cjs/adapter/bun/websocket.js +98 -0
- package/dist/cjs/adapter/cloudflare-workers/index.js +5 -2
- package/dist/cjs/adapter/cloudflare-workers/websocket.js +80 -0
- package/dist/cjs/adapter/deno/index.js +5 -2
- package/dist/cjs/adapter/deno/websocket.js +52 -0
- package/dist/cjs/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/client/client.js +68 -61
- package/dist/cjs/context.js +164 -188
- package/dist/cjs/helper/adapter/index.js +1 -1
- package/dist/cjs/helper/cookie/index.js +48 -8
- package/dist/cjs/helper/dev/index.js +3 -4
- package/dist/cjs/helper/factory/index.js +1 -3
- package/dist/cjs/helper/ssg/index.js +9 -159
- package/dist/cjs/helper/ssg/middleware.js +57 -0
- package/dist/cjs/helper/ssg/ssg.js +212 -0
- package/dist/cjs/helper/websocket/index.js +32 -0
- package/dist/cjs/helper.js +1 -0
- package/dist/cjs/hono-base.js +46 -65
- package/dist/cjs/http-exception.js +3 -1
- package/dist/cjs/jsx/base.js +7 -2
- package/dist/cjs/jsx/components.js +1 -1
- package/dist/cjs/jsx/dom/index.js +45 -3
- package/dist/cjs/jsx/dom/render.js +6 -6
- package/dist/cjs/jsx/hooks/index.js +25 -16
- package/dist/cjs/jsx/index.js +34 -2
- package/dist/cjs/middleware/body-limit/index.js +80 -0
- package/dist/cjs/middleware/logger/index.js +9 -7
- package/dist/cjs/request.js +29 -48
- package/dist/cjs/router/linear-router/router.js +3 -5
- package/dist/cjs/router/pattern-router/router.js +2 -4
- package/dist/cjs/router/reg-exp-router/node.js +3 -3
- package/dist/cjs/router/reg-exp-router/router.js +11 -12
- package/dist/cjs/router/reg-exp-router/trie.js +2 -4
- package/dist/cjs/router/smart-router/router.js +3 -3
- package/dist/cjs/router/trie-router/node.js +6 -2
- package/dist/cjs/router/trie-router/router.js +2 -1
- package/dist/cjs/test-utils/setup-vitest.js +2 -0
- package/dist/cjs/utils/color.js +32 -0
- package/dist/cjs/utils/concurrent.js +62 -0
- package/dist/cjs/utils/cookie.js +28 -1
- package/dist/cjs/utils/stream.js +5 -1
- package/dist/cjs/utils/url.js +2 -2
- package/dist/client/client.js +68 -61
- package/dist/context.js +164 -189
- package/dist/helper/adapter/index.js +1 -1
- package/dist/helper/cookie/index.js +48 -8
- package/dist/helper/dev/index.js +3 -4
- package/dist/helper/factory/index.js +1 -3
- package/dist/helper/ssg/index.js +3 -151
- package/dist/helper/ssg/middleware.js +29 -0
- package/dist/helper/ssg/ssg.js +187 -0
- package/dist/helper/websocket/index.js +9 -0
- package/dist/helper.js +1 -0
- package/dist/hono-base.js +45 -65
- package/dist/http-exception.js +3 -1
- package/dist/jsx/base.js +7 -2
- package/dist/jsx/components.js +1 -1
- package/dist/jsx/dom/index.js +44 -4
- package/dist/jsx/dom/render.js +6 -6
- package/dist/jsx/hooks/index.js +23 -16
- package/dist/jsx/index.js +34 -3
- package/dist/middleware/body-limit/index.js +57 -0
- package/dist/middleware/logger/index.js +9 -7
- package/dist/request.js +29 -49
- package/dist/router/linear-router/router.js +3 -5
- package/dist/router/pattern-router/router.js +2 -4
- package/dist/router/reg-exp-router/node.js +3 -3
- package/dist/router/reg-exp-router/router.js +11 -12
- package/dist/router/reg-exp-router/trie.js +2 -4
- package/dist/router/smart-router/router.js +3 -3
- package/dist/router/trie-router/node.js +6 -2
- package/dist/router/trie-router/router.js +2 -1
- package/dist/test-utils/setup-vitest.js +2 -0
- package/dist/types/adapter/bun/index.d.ts +1 -0
- package/dist/types/adapter/bun/websocket.d.ts +26 -0
- package/dist/types/adapter/cloudflare-workers/index.d.ts +1 -0
- package/dist/types/adapter/cloudflare-workers/websocket.d.ts +2 -0
- package/dist/types/adapter/deno/index.d.ts +1 -0
- package/dist/types/adapter/deno/websocket.d.ts +2 -0
- package/dist/types/client/types.d.ts +9 -0
- package/dist/types/helper/cookie/index.d.ts +3 -1
- package/dist/types/helper/ssg/index.d.ts +2 -105
- package/dist/types/helper/ssg/middleware.d.ts +38 -0
- package/dist/types/helper/ssg/ssg.d.ts +70 -0
- package/dist/types/helper/websocket/index.d.ts +34 -0
- package/dist/types/http-exception.d.ts +1 -0
- package/dist/types/jsx/dom/index.d.ts +40 -7
- package/dist/types/jsx/hooks/index.d.ts +2 -0
- package/dist/types/jsx/index.d.ts +41 -5
- package/dist/types/middleware/body-limit/index.d.ts +28 -0
- package/dist/types/utils/color.d.ts +1 -0
- package/dist/types/utils/concurrent.d.ts +7 -0
- package/dist/types/utils/cookie.d.ts +2 -0
- package/dist/utils/color.js +9 -0
- package/dist/utils/concurrent.js +39 -0
- package/dist/utils/cookie.js +28 -1
- package/dist/utils/stream.js +5 -1
- package/dist/utils/url.js +2 -2
- package/package.json +18 -2
|
@@ -24,9 +24,7 @@ __export(factory_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(factory_exports);
|
|
26
26
|
class Factory {
|
|
27
|
-
|
|
28
|
-
this.createMiddleware = (middleware) => middleware;
|
|
29
|
-
}
|
|
27
|
+
createMiddleware = (middleware) => middleware;
|
|
30
28
|
createHandlers(...handlers) {
|
|
31
29
|
return handlers.filter((handler) => handler !== void 0);
|
|
32
30
|
}
|
|
@@ -15,174 +15,24 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
20
|
var ssg_exports = {};
|
|
20
21
|
__export(ssg_exports, {
|
|
21
|
-
SSG_DISABLED_RESPONSE: () => SSG_DISABLED_RESPONSE,
|
|
22
|
-
disableSSG: () => disableSSG,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
saveContentToFiles: () => saveContentToFiles,
|
|
27
|
-
ssgParams: () => ssgParams,
|
|
28
|
-
toSSG: () => toSSG
|
|
22
|
+
SSG_DISABLED_RESPONSE: () => import_middleware.SSG_DISABLED_RESPONSE,
|
|
23
|
+
disableSSG: () => import_middleware.disableSSG,
|
|
24
|
+
isSSGContext: () => import_middleware.isSSGContext,
|
|
25
|
+
onlySSG: () => import_middleware.onlySSG,
|
|
26
|
+
ssgParams: () => import_middleware.ssgParams
|
|
29
27
|
});
|
|
30
28
|
module.exports = __toCommonJS(ssg_exports);
|
|
31
|
-
|
|
32
|
-
var
|
|
33
|
-
var import_utils2 = require("./utils");
|
|
34
|
-
const SSG_CONTEXT = "HONO_SSG_CONTEXT";
|
|
35
|
-
const SSG_DISABLED_RESPONSE = new Response("SSG is disabled", { status: 404 });
|
|
36
|
-
const generateFilePath = (routePath, outDir, mimeType) => {
|
|
37
|
-
const extension = determineExtension(mimeType);
|
|
38
|
-
if (routePath.endsWith(`.${extension}`)) {
|
|
39
|
-
return (0, import_utils2.joinPaths)(outDir, routePath);
|
|
40
|
-
}
|
|
41
|
-
if (routePath === "/") {
|
|
42
|
-
return (0, import_utils2.joinPaths)(outDir, `index.${extension}`);
|
|
43
|
-
}
|
|
44
|
-
if (routePath.endsWith("/")) {
|
|
45
|
-
return (0, import_utils2.joinPaths)(outDir, routePath, `index.${extension}`);
|
|
46
|
-
}
|
|
47
|
-
return (0, import_utils2.joinPaths)(outDir, `${routePath}.${extension}`);
|
|
48
|
-
};
|
|
49
|
-
const parseResponseContent = async (response) => {
|
|
50
|
-
const contentType = response.headers.get("Content-Type");
|
|
51
|
-
try {
|
|
52
|
-
if (contentType?.includes("text") || contentType?.includes("json")) {
|
|
53
|
-
return await response.text();
|
|
54
|
-
} else {
|
|
55
|
-
return await response.arrayBuffer();
|
|
56
|
-
}
|
|
57
|
-
} catch (error) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Error processing response: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
const determineExtension = (mimeType) => {
|
|
64
|
-
switch (mimeType) {
|
|
65
|
-
case "text/html":
|
|
66
|
-
return "html";
|
|
67
|
-
case "text/xml":
|
|
68
|
-
case "application/xml":
|
|
69
|
-
return "xml";
|
|
70
|
-
default: {
|
|
71
|
-
return (0, import_mime.getExtension)(mimeType) || "html";
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const ssgParams = (params) => async (c, next) => {
|
|
76
|
-
;
|
|
77
|
-
c.req.raw.ssgParams = Array.isArray(params) ? params : await params(c);
|
|
78
|
-
await next();
|
|
79
|
-
};
|
|
80
|
-
const fetchRoutesContent = async (app, beforeRequestHook, afterResponseHook) => {
|
|
81
|
-
const htmlMap = /* @__PURE__ */ new Map();
|
|
82
|
-
const baseURL = "http://localhost";
|
|
83
|
-
for (const route of (0, import_utils2.filterStaticGenerateRoutes)(app)) {
|
|
84
|
-
const thisRouteBaseURL = new URL(route.path, baseURL).toString();
|
|
85
|
-
let forGetInfoURLRequest = new Request(thisRouteBaseURL);
|
|
86
|
-
if (beforeRequestHook) {
|
|
87
|
-
const maybeRequest = beforeRequestHook(forGetInfoURLRequest);
|
|
88
|
-
if (!maybeRequest) {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
forGetInfoURLRequest = maybeRequest;
|
|
92
|
-
}
|
|
93
|
-
await app.fetch(forGetInfoURLRequest);
|
|
94
|
-
if (!forGetInfoURLRequest.ssgParams) {
|
|
95
|
-
if (isDynamicRoute(route.path)) {
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
forGetInfoURLRequest.ssgParams = [{}];
|
|
99
|
-
}
|
|
100
|
-
const requestInit = {
|
|
101
|
-
method: forGetInfoURLRequest.method,
|
|
102
|
-
headers: forGetInfoURLRequest.headers
|
|
103
|
-
};
|
|
104
|
-
for (const param of forGetInfoURLRequest.ssgParams) {
|
|
105
|
-
const replacedUrlParam = (0, import_utils.replaceUrlParam)(route.path, param);
|
|
106
|
-
let response = await app.request(replacedUrlParam, requestInit, {
|
|
107
|
-
[SSG_CONTEXT]: true
|
|
108
|
-
});
|
|
109
|
-
if (response === SSG_DISABLED_RESPONSE) {
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
if (afterResponseHook) {
|
|
113
|
-
const maybeResponse = afterResponseHook(response);
|
|
114
|
-
if (!maybeResponse) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
response = maybeResponse;
|
|
118
|
-
}
|
|
119
|
-
const mimeType = response.headers.get("Content-Type")?.split(";")[0] || "text/plain";
|
|
120
|
-
const content = await parseResponseContent(response);
|
|
121
|
-
htmlMap.set(replacedUrlParam, {
|
|
122
|
-
mimeType,
|
|
123
|
-
content
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return htmlMap;
|
|
128
|
-
};
|
|
129
|
-
const isDynamicRoute = (path) => {
|
|
130
|
-
return path.split("/").some((segment) => segment.startsWith(":") || segment.includes("*"));
|
|
131
|
-
};
|
|
132
|
-
const saveContentToFiles = async (htmlMap, fsModule, outDir) => {
|
|
133
|
-
const files = [];
|
|
134
|
-
for (const [routePath, { content, mimeType }] of htmlMap) {
|
|
135
|
-
const filePath = generateFilePath(routePath, outDir, mimeType);
|
|
136
|
-
const dirPath = (0, import_utils2.dirname)(filePath);
|
|
137
|
-
await fsModule.mkdir(dirPath, { recursive: true });
|
|
138
|
-
if (typeof content === "string") {
|
|
139
|
-
await fsModule.writeFile(filePath, content);
|
|
140
|
-
} else if (content instanceof ArrayBuffer) {
|
|
141
|
-
await fsModule.writeFile(filePath, new Uint8Array(content));
|
|
142
|
-
}
|
|
143
|
-
files.push(filePath);
|
|
144
|
-
}
|
|
145
|
-
return files;
|
|
146
|
-
};
|
|
147
|
-
const toSSG = async (app, fs, options) => {
|
|
148
|
-
let result = void 0;
|
|
149
|
-
try {
|
|
150
|
-
const outputDir = options?.dir ?? "./static";
|
|
151
|
-
const maps = await fetchRoutesContent(
|
|
152
|
-
app,
|
|
153
|
-
options?.beforeRequestHook,
|
|
154
|
-
options?.afterResponseHook
|
|
155
|
-
);
|
|
156
|
-
const files = await saveContentToFiles(maps, fs, outputDir);
|
|
157
|
-
result = { success: true, files };
|
|
158
|
-
} catch (error) {
|
|
159
|
-
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
160
|
-
result = { success: false, files: [], error: errorObj };
|
|
161
|
-
}
|
|
162
|
-
await options?.afterGenerateHook?.(result);
|
|
163
|
-
return result;
|
|
164
|
-
};
|
|
165
|
-
const isSSGContext = (c) => !!c.env?.[SSG_CONTEXT];
|
|
166
|
-
const disableSSG = () => async function disableSSG2(c, next) {
|
|
167
|
-
if (isSSGContext(c)) {
|
|
168
|
-
return SSG_DISABLED_RESPONSE;
|
|
169
|
-
}
|
|
170
|
-
await next();
|
|
171
|
-
};
|
|
172
|
-
const onlySSG = () => async function onlySSG2(c, next) {
|
|
173
|
-
if (!isSSGContext(c)) {
|
|
174
|
-
return c.notFound();
|
|
175
|
-
}
|
|
176
|
-
await next();
|
|
177
|
-
};
|
|
29
|
+
__reExport(ssg_exports, require("./ssg"), module.exports);
|
|
30
|
+
var import_middleware = require("./middleware");
|
|
178
31
|
// Annotate the CommonJS export names for ESM import in node:
|
|
179
32
|
0 && (module.exports = {
|
|
180
33
|
SSG_DISABLED_RESPONSE,
|
|
181
34
|
disableSSG,
|
|
182
|
-
fetchRoutesContent,
|
|
183
35
|
isSSGContext,
|
|
184
36
|
onlySSG,
|
|
185
|
-
|
|
186
|
-
ssgParams,
|
|
187
|
-
toSSG
|
|
37
|
+
ssgParams
|
|
188
38
|
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var middleware_exports = {};
|
|
20
|
+
__export(middleware_exports, {
|
|
21
|
+
SSG_CONTEXT: () => SSG_CONTEXT,
|
|
22
|
+
SSG_DISABLED_RESPONSE: () => SSG_DISABLED_RESPONSE,
|
|
23
|
+
disableSSG: () => disableSSG,
|
|
24
|
+
isSSGContext: () => isSSGContext,
|
|
25
|
+
onlySSG: () => onlySSG,
|
|
26
|
+
ssgParams: () => ssgParams
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
29
|
+
const SSG_CONTEXT = "HONO_SSG_CONTEXT";
|
|
30
|
+
const SSG_DISABLED_RESPONSE = new Response("SSG is disabled", { status: 404 });
|
|
31
|
+
const ssgParams = (params) => async (c, next) => {
|
|
32
|
+
;
|
|
33
|
+
c.req.raw.ssgParams = Array.isArray(params) ? params : await params(c);
|
|
34
|
+
await next();
|
|
35
|
+
};
|
|
36
|
+
const isSSGContext = (c) => !!c.env?.[SSG_CONTEXT];
|
|
37
|
+
const disableSSG = () => async function disableSSG2(c, next) {
|
|
38
|
+
if (isSSGContext(c)) {
|
|
39
|
+
return SSG_DISABLED_RESPONSE;
|
|
40
|
+
}
|
|
41
|
+
await next();
|
|
42
|
+
};
|
|
43
|
+
const onlySSG = () => async function onlySSG2(c, next) {
|
|
44
|
+
if (!isSSGContext(c)) {
|
|
45
|
+
return c.notFound();
|
|
46
|
+
}
|
|
47
|
+
await next();
|
|
48
|
+
};
|
|
49
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
50
|
+
0 && (module.exports = {
|
|
51
|
+
SSG_CONTEXT,
|
|
52
|
+
SSG_DISABLED_RESPONSE,
|
|
53
|
+
disableSSG,
|
|
54
|
+
isSSGContext,
|
|
55
|
+
onlySSG,
|
|
56
|
+
ssgParams
|
|
57
|
+
});
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var ssg_exports = {};
|
|
20
|
+
__export(ssg_exports, {
|
|
21
|
+
fetchRoutesContent: () => fetchRoutesContent,
|
|
22
|
+
saveContentToFile: () => saveContentToFile,
|
|
23
|
+
toSSG: () => toSSG
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(ssg_exports);
|
|
26
|
+
var import_utils = require("../../client/utils");
|
|
27
|
+
var import_concurrent = require("../../utils/concurrent");
|
|
28
|
+
var import_mime = require("../../utils/mime");
|
|
29
|
+
var import_middleware = require("./middleware");
|
|
30
|
+
var import_utils2 = require("./utils");
|
|
31
|
+
const DEFAULT_CONCURRENCY = 2;
|
|
32
|
+
const generateFilePath = (routePath, outDir, mimeType) => {
|
|
33
|
+
const extension = determineExtension(mimeType);
|
|
34
|
+
if (routePath.endsWith(`.${extension}`)) {
|
|
35
|
+
return (0, import_utils2.joinPaths)(outDir, routePath);
|
|
36
|
+
}
|
|
37
|
+
if (routePath === "/") {
|
|
38
|
+
return (0, import_utils2.joinPaths)(outDir, `index.${extension}`);
|
|
39
|
+
}
|
|
40
|
+
if (routePath.endsWith("/")) {
|
|
41
|
+
return (0, import_utils2.joinPaths)(outDir, routePath, `index.${extension}`);
|
|
42
|
+
}
|
|
43
|
+
return (0, import_utils2.joinPaths)(outDir, `${routePath}.${extension}`);
|
|
44
|
+
};
|
|
45
|
+
const parseResponseContent = async (response) => {
|
|
46
|
+
const contentType = response.headers.get("Content-Type");
|
|
47
|
+
try {
|
|
48
|
+
if (contentType?.includes("text") || contentType?.includes("json")) {
|
|
49
|
+
return await response.text();
|
|
50
|
+
} else {
|
|
51
|
+
return await response.arrayBuffer();
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Error processing response: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const determineExtension = (mimeType) => {
|
|
60
|
+
switch (mimeType) {
|
|
61
|
+
case "text/html":
|
|
62
|
+
return "html";
|
|
63
|
+
case "text/xml":
|
|
64
|
+
case "application/xml":
|
|
65
|
+
return "xml";
|
|
66
|
+
default: {
|
|
67
|
+
return (0, import_mime.getExtension)(mimeType) || "html";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const fetchRoutesContent = function* (app, beforeRequestHook, afterResponseHook, concurrency) {
|
|
72
|
+
const baseURL = "http://localhost";
|
|
73
|
+
const pool = (0, import_concurrent.createPool)({ concurrency });
|
|
74
|
+
for (const route of (0, import_utils2.filterStaticGenerateRoutes)(app)) {
|
|
75
|
+
const thisRouteBaseURL = new URL(route.path, baseURL).toString();
|
|
76
|
+
let forGetInfoURLRequest = new Request(thisRouteBaseURL);
|
|
77
|
+
if (beforeRequestHook) {
|
|
78
|
+
const maybeRequest = beforeRequestHook(forGetInfoURLRequest);
|
|
79
|
+
if (!maybeRequest) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
forGetInfoURLRequest = maybeRequest;
|
|
83
|
+
}
|
|
84
|
+
yield new Promise(async (resolveGetInfo, rejectGetInfo) => {
|
|
85
|
+
try {
|
|
86
|
+
await pool.run(() => app.fetch(forGetInfoURLRequest));
|
|
87
|
+
if (!forGetInfoURLRequest.ssgParams) {
|
|
88
|
+
if (isDynamicRoute(route.path)) {
|
|
89
|
+
resolveGetInfo(void 0);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
forGetInfoURLRequest.ssgParams = [{}];
|
|
93
|
+
}
|
|
94
|
+
const requestInit = {
|
|
95
|
+
method: forGetInfoURLRequest.method,
|
|
96
|
+
headers: forGetInfoURLRequest.headers
|
|
97
|
+
};
|
|
98
|
+
resolveGetInfo(
|
|
99
|
+
function* () {
|
|
100
|
+
for (const param of forGetInfoURLRequest.ssgParams) {
|
|
101
|
+
yield new Promise(async (resolveReq, rejectReq) => {
|
|
102
|
+
try {
|
|
103
|
+
const replacedUrlParam = (0, import_utils.replaceUrlParam)(route.path, param);
|
|
104
|
+
let response = await pool.run(
|
|
105
|
+
() => app.request(replacedUrlParam, requestInit, {
|
|
106
|
+
[import_middleware.SSG_CONTEXT]: true
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
if (response === import_middleware.SSG_DISABLED_RESPONSE) {
|
|
110
|
+
resolveReq(void 0);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (afterResponseHook) {
|
|
114
|
+
const maybeResponse = afterResponseHook(response);
|
|
115
|
+
if (!maybeResponse) {
|
|
116
|
+
resolveReq(void 0);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
response = maybeResponse;
|
|
120
|
+
}
|
|
121
|
+
const mimeType = response.headers.get("Content-Type")?.split(";")[0] || "text/plain";
|
|
122
|
+
const content = await parseResponseContent(response);
|
|
123
|
+
resolveReq({
|
|
124
|
+
routePath: replacedUrlParam,
|
|
125
|
+
mimeType,
|
|
126
|
+
content
|
|
127
|
+
});
|
|
128
|
+
} catch (error) {
|
|
129
|
+
rejectReq(error);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}()
|
|
134
|
+
);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
rejectGetInfo(error);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const isDynamicRoute = (path) => {
|
|
142
|
+
return path.split("/").some((segment) => segment.startsWith(":") || segment.includes("*"));
|
|
143
|
+
};
|
|
144
|
+
const createdDirs = /* @__PURE__ */ new Set();
|
|
145
|
+
const saveContentToFile = async (data, fsModule, outDir) => {
|
|
146
|
+
const awaitedData = await data;
|
|
147
|
+
if (!awaitedData) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const { routePath, content, mimeType } = awaitedData;
|
|
151
|
+
const filePath = generateFilePath(routePath, outDir, mimeType);
|
|
152
|
+
const dirPath = (0, import_utils2.dirname)(filePath);
|
|
153
|
+
if (!createdDirs.has(dirPath)) {
|
|
154
|
+
await fsModule.mkdir(dirPath, { recursive: true });
|
|
155
|
+
createdDirs.add(dirPath);
|
|
156
|
+
}
|
|
157
|
+
if (typeof content === "string") {
|
|
158
|
+
await fsModule.writeFile(filePath, content);
|
|
159
|
+
} else if (content instanceof ArrayBuffer) {
|
|
160
|
+
await fsModule.writeFile(filePath, new Uint8Array(content));
|
|
161
|
+
}
|
|
162
|
+
return filePath;
|
|
163
|
+
};
|
|
164
|
+
const toSSG = async (app, fs, options) => {
|
|
165
|
+
let result = void 0;
|
|
166
|
+
const getInfoPromises = [];
|
|
167
|
+
const savePromises = [];
|
|
168
|
+
try {
|
|
169
|
+
const outputDir = options?.dir ?? "./static";
|
|
170
|
+
const concurrency = options?.concurrency ?? DEFAULT_CONCURRENCY;
|
|
171
|
+
const getInfoGen = fetchRoutesContent(
|
|
172
|
+
app,
|
|
173
|
+
options?.beforeRequestHook,
|
|
174
|
+
options?.afterResponseHook,
|
|
175
|
+
concurrency
|
|
176
|
+
);
|
|
177
|
+
for (const getInfo of getInfoGen) {
|
|
178
|
+
getInfoPromises.push(
|
|
179
|
+
getInfo.then((getContentGen) => {
|
|
180
|
+
if (!getContentGen) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
for (const content of getContentGen) {
|
|
184
|
+
savePromises.push(saveContentToFile(content, fs, outputDir).catch((e) => e));
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
await Promise.all(getInfoPromises);
|
|
190
|
+
const files = [];
|
|
191
|
+
for (const savePromise of savePromises) {
|
|
192
|
+
const fileOrError = await savePromise;
|
|
193
|
+
if (typeof fileOrError === "string") {
|
|
194
|
+
files.push(fileOrError);
|
|
195
|
+
} else if (fileOrError) {
|
|
196
|
+
throw fileOrError;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
result = { success: true, files };
|
|
200
|
+
} catch (error) {
|
|
201
|
+
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
202
|
+
result = { success: false, files: [], error: errorObj };
|
|
203
|
+
}
|
|
204
|
+
await options?.afterGenerateHook?.(result);
|
|
205
|
+
return result;
|
|
206
|
+
};
|
|
207
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
208
|
+
0 && (module.exports = {
|
|
209
|
+
fetchRoutesContent,
|
|
210
|
+
saveContentToFile,
|
|
211
|
+
toSSG
|
|
212
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var websocket_exports = {};
|
|
20
|
+
__export(websocket_exports, {
|
|
21
|
+
createWSMessageEvent: () => createWSMessageEvent
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(websocket_exports);
|
|
24
|
+
const createWSMessageEvent = (source) => {
|
|
25
|
+
return new MessageEvent("message", {
|
|
26
|
+
data: source
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
createWSMessageEvent
|
|
32
|
+
});
|
package/dist/cjs/helper.js
CHANGED
|
@@ -25,3 +25,4 @@ __reExport(helper_exports, require("./helper/streaming"), module.exports);
|
|
|
25
25
|
__reExport(helper_exports, require("./helper/testing"), module.exports);
|
|
26
26
|
__reExport(helper_exports, require("./helper/dev"), module.exports);
|
|
27
27
|
__reExport(helper_exports, require("./adapter/deno/ssg"), module.exports);
|
|
28
|
+
__reExport(helper_exports, require("./adapter/deno/websocket"), module.exports);
|
package/dist/cjs/hono-base.js
CHANGED
|
@@ -16,24 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
var hono_base_exports = {};
|
|
38
20
|
__export(hono_base_exports, {
|
|
39
21
|
COMPOSED_HANDLER: () => COMPOSED_HANDLER,
|
|
@@ -46,7 +28,6 @@ var import_http_exception = require("./http-exception");
|
|
|
46
28
|
var import_request = require("./request");
|
|
47
29
|
var import_router = require("./router");
|
|
48
30
|
var import_url = require("./utils/url");
|
|
49
|
-
var _path;
|
|
50
31
|
const COMPOSED_HANDLER = Symbol("composedHandler");
|
|
51
32
|
function defineDynamicClass() {
|
|
52
33
|
return class {
|
|
@@ -62,53 +43,25 @@ const errorHandler = (err, c) => {
|
|
|
62
43
|
console.error(err);
|
|
63
44
|
return c.text("Internal Server Error", 500);
|
|
64
45
|
};
|
|
65
|
-
|
|
46
|
+
class Hono extends defineDynamicClass() {
|
|
47
|
+
router;
|
|
48
|
+
getPath;
|
|
49
|
+
_basePath = "/";
|
|
50
|
+
#path = "/";
|
|
51
|
+
routes = [];
|
|
66
52
|
constructor(options = {}) {
|
|
67
53
|
super();
|
|
68
|
-
this._basePath = "/";
|
|
69
|
-
__privateAdd(this, _path, "/");
|
|
70
|
-
this.routes = [];
|
|
71
|
-
this.notFoundHandler = notFoundHandler;
|
|
72
|
-
this.errorHandler = errorHandler;
|
|
73
|
-
this.onError = (handler) => {
|
|
74
|
-
this.errorHandler = handler;
|
|
75
|
-
return this;
|
|
76
|
-
};
|
|
77
|
-
this.notFound = (handler) => {
|
|
78
|
-
this.notFoundHandler = handler;
|
|
79
|
-
return this;
|
|
80
|
-
};
|
|
81
|
-
this.fetch = (request, Env, executionCtx) => {
|
|
82
|
-
return this.dispatch(request, executionCtx, Env, request.method);
|
|
83
|
-
};
|
|
84
|
-
this.request = (input, requestInit, Env, executionCtx) => {
|
|
85
|
-
if (input instanceof Request) {
|
|
86
|
-
if (requestInit !== void 0) {
|
|
87
|
-
input = new Request(input, requestInit);
|
|
88
|
-
}
|
|
89
|
-
return this.fetch(input, Env, executionCtx);
|
|
90
|
-
}
|
|
91
|
-
input = input.toString();
|
|
92
|
-
const path = /^https?:\/\//.test(input) ? input : `http://localhost${(0, import_url.mergePath)("/", input)}`;
|
|
93
|
-
const req = new Request(path, requestInit);
|
|
94
|
-
return this.fetch(req, Env, executionCtx);
|
|
95
|
-
};
|
|
96
|
-
this.fire = () => {
|
|
97
|
-
addEventListener("fetch", (event) => {
|
|
98
|
-
event.respondWith(this.dispatch(event.request, event, void 0, event.request.method));
|
|
99
|
-
});
|
|
100
|
-
};
|
|
101
54
|
const allMethods = [...import_router.METHODS, import_router.METHOD_NAME_ALL_LOWERCASE];
|
|
102
55
|
allMethods.map((method) => {
|
|
103
56
|
this[method] = (args1, ...args) => {
|
|
104
57
|
if (typeof args1 === "string") {
|
|
105
|
-
|
|
58
|
+
this.#path = args1;
|
|
106
59
|
} else {
|
|
107
|
-
this.addRoute(method,
|
|
60
|
+
this.addRoute(method, this.#path, args1);
|
|
108
61
|
}
|
|
109
62
|
args.map((handler) => {
|
|
110
63
|
if (typeof handler !== "string") {
|
|
111
|
-
this.addRoute(method,
|
|
64
|
+
this.addRoute(method, this.#path, handler);
|
|
112
65
|
}
|
|
113
66
|
});
|
|
114
67
|
return this;
|
|
@@ -119,10 +72,10 @@ const _Hono = class extends defineDynamicClass() {
|
|
|
119
72
|
return this;
|
|
120
73
|
}
|
|
121
74
|
for (const p of [path].flat()) {
|
|
122
|
-
|
|
75
|
+
this.#path = p;
|
|
123
76
|
for (const m of [method].flat()) {
|
|
124
77
|
handlers.map((handler) => {
|
|
125
|
-
this.addRoute(m.toUpperCase(),
|
|
78
|
+
this.addRoute(m.toUpperCase(), this.#path, handler);
|
|
126
79
|
});
|
|
127
80
|
}
|
|
128
81
|
}
|
|
@@ -130,13 +83,13 @@ const _Hono = class extends defineDynamicClass() {
|
|
|
130
83
|
};
|
|
131
84
|
this.use = (arg1, ...handlers) => {
|
|
132
85
|
if (typeof arg1 === "string") {
|
|
133
|
-
|
|
86
|
+
this.#path = arg1;
|
|
134
87
|
} else {
|
|
135
|
-
|
|
88
|
+
this.#path = "*";
|
|
136
89
|
handlers.unshift(arg1);
|
|
137
90
|
}
|
|
138
91
|
handlers.map((handler) => {
|
|
139
|
-
this.addRoute(import_router.METHOD_NAME_ALL,
|
|
92
|
+
this.addRoute(import_router.METHOD_NAME_ALL, this.#path, handler);
|
|
140
93
|
});
|
|
141
94
|
return this;
|
|
142
95
|
};
|
|
@@ -146,13 +99,15 @@ const _Hono = class extends defineDynamicClass() {
|
|
|
146
99
|
this.getPath = strict ? options.getPath ?? import_url.getPath : import_url.getPathNoStrict;
|
|
147
100
|
}
|
|
148
101
|
clone() {
|
|
149
|
-
const clone = new
|
|
102
|
+
const clone = new Hono({
|
|
150
103
|
router: this.router,
|
|
151
104
|
getPath: this.getPath
|
|
152
105
|
});
|
|
153
106
|
clone.routes = this.routes;
|
|
154
107
|
return clone;
|
|
155
108
|
}
|
|
109
|
+
notFoundHandler = notFoundHandler;
|
|
110
|
+
errorHandler = errorHandler;
|
|
156
111
|
route(path, app) {
|
|
157
112
|
const subApp = this.basePath(path);
|
|
158
113
|
if (!app) {
|
|
@@ -175,6 +130,14 @@ const _Hono = class extends defineDynamicClass() {
|
|
|
175
130
|
subApp._basePath = (0, import_url.mergePath)(this._basePath, path);
|
|
176
131
|
return subApp;
|
|
177
132
|
}
|
|
133
|
+
onError = (handler) => {
|
|
134
|
+
this.errorHandler = handler;
|
|
135
|
+
return this;
|
|
136
|
+
};
|
|
137
|
+
notFound = (handler) => {
|
|
138
|
+
this.notFoundHandler = handler;
|
|
139
|
+
return this;
|
|
140
|
+
};
|
|
178
141
|
mount(path, applicationHandler, optionHandler) {
|
|
179
142
|
const mergedPath = (0, import_url.mergePath)(this._basePath, path);
|
|
180
143
|
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
@@ -257,9 +220,27 @@ const _Hono = class extends defineDynamicClass() {
|
|
|
257
220
|
}
|
|
258
221
|
})();
|
|
259
222
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
223
|
+
fetch = (request, Env, executionCtx) => {
|
|
224
|
+
return this.dispatch(request, executionCtx, Env, request.method);
|
|
225
|
+
};
|
|
226
|
+
request = (input, requestInit, Env, executionCtx) => {
|
|
227
|
+
if (input instanceof Request) {
|
|
228
|
+
if (requestInit !== void 0) {
|
|
229
|
+
input = new Request(input, requestInit);
|
|
230
|
+
}
|
|
231
|
+
return this.fetch(input, Env, executionCtx);
|
|
232
|
+
}
|
|
233
|
+
input = input.toString();
|
|
234
|
+
const path = /^https?:\/\//.test(input) ? input : `http://localhost${(0, import_url.mergePath)("/", input)}`;
|
|
235
|
+
const req = new Request(path, requestInit);
|
|
236
|
+
return this.fetch(req, Env, executionCtx);
|
|
237
|
+
};
|
|
238
|
+
fire = () => {
|
|
239
|
+
addEventListener("fetch", (event) => {
|
|
240
|
+
event.respondWith(this.dispatch(event.request, event, void 0, event.request.method));
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
}
|
|
263
244
|
// Annotate the CommonJS export names for ESM import in node:
|
|
264
245
|
0 && (module.exports = {
|
|
265
246
|
COMPOSED_HANDLER,
|