@hyperspan/framework 0.5.5 → 1.0.0-alpha.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/package.json +34 -26
- package/src/callsites.ts +17 -0
- package/src/client/css.ts +2 -0
- package/src/client/js.ts +82 -0
- package/src/layout.ts +31 -0
- package/src/middleware/zod.ts +46 -0
- package/src/middleware.ts +53 -14
- package/src/plugins.ts +64 -58
- package/src/server.test.ts +88 -0
- package/src/server.ts +285 -428
- package/src/types.ts +110 -0
- package/src/utils.ts +5 -0
- package/build.ts +0 -16
- package/dist/assets.js +0 -120
- package/dist/chunk-atw8cdg1.js +0 -19
- package/dist/middleware.js +0 -179
- package/dist/server.js +0 -2266
- package/src/actions.test.ts +0 -106
- package/src/actions.ts +0 -256
- package/src/assets.ts +0 -176
package/dist/server.js
DELETED
|
@@ -1,2266 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CLIENTJS_PUBLIC_PATH,
|
|
3
|
-
assetHash,
|
|
4
|
-
buildClientCSS,
|
|
5
|
-
buildClientJS,
|
|
6
|
-
clientImportMap
|
|
7
|
-
} from "./assets.js";
|
|
8
|
-
import"./chunk-atw8cdg1.js";
|
|
9
|
-
|
|
10
|
-
// src/plugins.ts
|
|
11
|
-
var CLIENT_JS_CACHE = new Map;
|
|
12
|
-
var EXPORT_REGEX = /export\{(.*)\}/g;
|
|
13
|
-
async function clientJSPlugin(config) {
|
|
14
|
-
await Bun.plugin({
|
|
15
|
-
name: "Hyperspan Client JS Loader",
|
|
16
|
-
async setup(build) {
|
|
17
|
-
build.onLoad({ filter: /\.client\.ts$/ }, async (args) => {
|
|
18
|
-
const jsId = assetHash(args.path);
|
|
19
|
-
if (IS_PROD && CLIENT_JS_CACHE.has(jsId)) {
|
|
20
|
-
return {
|
|
21
|
-
contents: CLIENT_JS_CACHE.get(jsId) || "",
|
|
22
|
-
loader: "js"
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const result = await Bun.build({
|
|
26
|
-
entrypoints: [args.path],
|
|
27
|
-
outdir: `./public/${CLIENTJS_PUBLIC_PATH}`,
|
|
28
|
-
naming: IS_PROD ? "[dir]/[name]-[hash].[ext]" : undefined,
|
|
29
|
-
external: Array.from(clientImportMap.keys()),
|
|
30
|
-
minify: true,
|
|
31
|
-
format: "esm",
|
|
32
|
-
target: "browser",
|
|
33
|
-
env: "APP_PUBLIC_*"
|
|
34
|
-
});
|
|
35
|
-
const esmName = String(result.outputs[0].path.split("/").reverse()[0]).replace(".js", "");
|
|
36
|
-
clientImportMap.set(esmName, `${CLIENTJS_PUBLIC_PATH}/${esmName}.js`);
|
|
37
|
-
const contents = await result.outputs[0].text();
|
|
38
|
-
const exportLine = EXPORT_REGEX.exec(contents);
|
|
39
|
-
let exports = "{}";
|
|
40
|
-
if (exportLine) {
|
|
41
|
-
const exportName = exportLine[1];
|
|
42
|
-
exports = "{" + exportName.split(",").map((name) => name.trim().split(" as ")).map(([name, alias]) => `${alias === "default" ? "default as " + name : alias}`).join(", ") + "}";
|
|
43
|
-
}
|
|
44
|
-
const fnArgs = exports.replace(/(\w+)\s*as\s*(\w+)/g, "$1: $2");
|
|
45
|
-
const moduleCode = `// hyperspan:processed
|
|
46
|
-
import { functionToString } from '@hyperspan/framework/assets';
|
|
47
|
-
|
|
48
|
-
// Original file contents
|
|
49
|
-
${contents}
|
|
50
|
-
|
|
51
|
-
// hyperspan:client-js-plugin
|
|
52
|
-
export const __CLIENT_JS = {
|
|
53
|
-
id: "${jsId}",
|
|
54
|
-
esmName: "${esmName}",
|
|
55
|
-
sourceFile: "${args.path}",
|
|
56
|
-
outputFile: "${result.outputs[0].path}",
|
|
57
|
-
renderScriptTag: ({ loadScript }) => {
|
|
58
|
-
const fn = loadScript ? (typeof loadScript === 'string' ? loadScript : \`const fn = \${functionToString(loadScript)}; fn(${fnArgs});\`) : '';
|
|
59
|
-
return \`<script type="module" data-source-id="${jsId}">import ${exports} from "${esmName}";
|
|
60
|
-
\${fn}</script>\`;
|
|
61
|
-
},
|
|
62
|
-
}
|
|
63
|
-
`;
|
|
64
|
-
CLIENT_JS_CACHE.set(jsId, moduleCode);
|
|
65
|
-
return {
|
|
66
|
-
contents: moduleCode,
|
|
67
|
-
loader: "js"
|
|
68
|
-
};
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// src/server.ts
|
|
75
|
-
import { html, isHSHtml, renderStream, renderAsync, render } from "@hyperspan/html";
|
|
76
|
-
import { readdir } from "node:fs/promises";
|
|
77
|
-
import { basename, extname, join as join2 } from "node:path";
|
|
78
|
-
|
|
79
|
-
// node_modules/isbot/index.mjs
|
|
80
|
-
var fullPattern = " daum[ /]| deusu/|(?:^|[^g])news(?!sapphire)|(?<! (?:channel/|google/))google(?!(app|/google| pixel))|(?<! cu)bots?(?:\\b|_)|(?<!(?:lib))http|(?<![hg]m)score|(?<!cam)scan|@[a-z][\\w-]+\\.|\\(\\)|\\.com\\b|\\btime/|\\||^<|^[\\w \\.\\-\\(?:\\):%]+(?:/v?\\d+(?:\\.\\d+)?(?:\\.\\d{1,10})*?)?(?:,|$)|^[^ ]{50,}$|^\\d+\\b|^\\w*search\\b|^\\w+/[\\w\\(\\)]*$|^active|^ad muncher|^amaya|^avsdevicesdk/|^azure|^biglotron|^bot|^bw/|^clamav[ /]|^client/|^cobweb/|^custom|^ddg[_-]android|^discourse|^dispatch/\\d|^downcast/|^duckduckgo|^email|^facebook|^getright/|^gozilla/|^hobbit|^hotzonu|^hwcdn/|^igetter/|^jeode/|^jetty/|^jigsaw|^microsoft bits|^movabletype|^mozilla/\\d\\.\\d\\s[\\w\\.-]+$|^mozilla/\\d\\.\\d\\s\\(compatible;?(?:\\s\\w+\\/\\d+\\.\\d+)?\\)$|^navermailapp|^netsurf|^offline|^openai/|^owler|^php|^postman|^python|^rank|^read|^reed|^rest|^rss|^snapchat|^space bison|^svn|^swcd |^taringa|^thumbor/|^track|^w3c|^webbandit/|^webcopier|^wget|^whatsapp|^wordpress|^xenu link sleuth|^yahoo|^yandex|^zdm/\\d|^zoom marketplace/|agent|analyzer|archive|ask jeeves/teoma|audit|bit\\.ly/|bluecoat drtr|browsex|burpcollaborator|capture|catch|check\\b|checker|chrome-lighthouse|chromeframe|classifier|cloudflare|convertify|crawl|cypress/|dareboost|datanyze|dejaclick|detect|dmbrowser|download|evc-batch/|exaleadcloudview|feed|fetcher|firephp|functionize|grab|headless|httrack|hubspot marketing grader|hydra|ibisbrowser|infrawatch|insight|inspect|iplabel|java(?!;)|library|linkcheck|mail\\.ru/|manager|measure|neustar wpm|node|nutch|offbyone|onetrust|optimize|pageburst|pagespeed|parser|perl|phantomjs|pingdom|powermarks|preview|proxy|ptst[ /]\\d|retriever|rexx;|rigor|rss\\b|scrape|server|sogou|sparkler/|speedcurve|spider|splash|statuscake|supercleaner|synapse|synthetic|tools|torrent|transcoder|url|validator|virtuoso|wappalyzer|webglance|webkit2png|whatcms/|xtate/";
|
|
81
|
-
var naivePattern = /bot|crawl|http|lighthouse|scan|search|spider/i;
|
|
82
|
-
var pattern;
|
|
83
|
-
function getPattern() {
|
|
84
|
-
if (pattern instanceof RegExp) {
|
|
85
|
-
return pattern;
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
pattern = new RegExp(fullPattern, "i");
|
|
89
|
-
} catch (error) {
|
|
90
|
-
pattern = naivePattern;
|
|
91
|
-
}
|
|
92
|
-
return pattern;
|
|
93
|
-
}
|
|
94
|
-
function isbot(userAgent) {
|
|
95
|
-
return Boolean(userAgent) && getPattern().test(userAgent);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// node_modules/hono/dist/compose.js
|
|
99
|
-
var compose = (middleware, onError, onNotFound) => {
|
|
100
|
-
return (context, next) => {
|
|
101
|
-
let index = -1;
|
|
102
|
-
return dispatch(0);
|
|
103
|
-
async function dispatch(i) {
|
|
104
|
-
if (i <= index) {
|
|
105
|
-
throw new Error("next() called multiple times");
|
|
106
|
-
}
|
|
107
|
-
index = i;
|
|
108
|
-
let res;
|
|
109
|
-
let isError = false;
|
|
110
|
-
let handler;
|
|
111
|
-
if (middleware[i]) {
|
|
112
|
-
handler = middleware[i][0][0];
|
|
113
|
-
context.req.routeIndex = i;
|
|
114
|
-
} else {
|
|
115
|
-
handler = i === middleware.length && next || undefined;
|
|
116
|
-
}
|
|
117
|
-
if (handler) {
|
|
118
|
-
try {
|
|
119
|
-
res = await handler(context, () => dispatch(i + 1));
|
|
120
|
-
} catch (err) {
|
|
121
|
-
if (err instanceof Error && onError) {
|
|
122
|
-
context.error = err;
|
|
123
|
-
res = await onError(err, context);
|
|
124
|
-
isError = true;
|
|
125
|
-
} else {
|
|
126
|
-
throw err;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
if (context.finalized === false && onNotFound) {
|
|
131
|
-
res = await onNotFound(context);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (res && (context.finalized === false || isError)) {
|
|
135
|
-
context.res = res;
|
|
136
|
-
}
|
|
137
|
-
return context;
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
// node_modules/hono/dist/request/constants.js
|
|
143
|
-
var GET_MATCH_RESULT = Symbol();
|
|
144
|
-
|
|
145
|
-
// node_modules/hono/dist/utils/body.js
|
|
146
|
-
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
147
|
-
const { all = false, dot = false } = options;
|
|
148
|
-
const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
|
|
149
|
-
const contentType = headers.get("Content-Type");
|
|
150
|
-
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
|
|
151
|
-
return parseFormData(request, { all, dot });
|
|
152
|
-
}
|
|
153
|
-
return {};
|
|
154
|
-
};
|
|
155
|
-
async function parseFormData(request, options) {
|
|
156
|
-
const formData = await request.formData();
|
|
157
|
-
if (formData) {
|
|
158
|
-
return convertFormDataToBodyData(formData, options);
|
|
159
|
-
}
|
|
160
|
-
return {};
|
|
161
|
-
}
|
|
162
|
-
function convertFormDataToBodyData(formData, options) {
|
|
163
|
-
const form = /* @__PURE__ */ Object.create(null);
|
|
164
|
-
formData.forEach((value, key) => {
|
|
165
|
-
const shouldParseAllValues = options.all || key.endsWith("[]");
|
|
166
|
-
if (!shouldParseAllValues) {
|
|
167
|
-
form[key] = value;
|
|
168
|
-
} else {
|
|
169
|
-
handleParsingAllValues(form, key, value);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
if (options.dot) {
|
|
173
|
-
Object.entries(form).forEach(([key, value]) => {
|
|
174
|
-
const shouldParseDotValues = key.includes(".");
|
|
175
|
-
if (shouldParseDotValues) {
|
|
176
|
-
handleParsingNestedValues(form, key, value);
|
|
177
|
-
delete form[key];
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
return form;
|
|
182
|
-
}
|
|
183
|
-
var handleParsingAllValues = (form, key, value) => {
|
|
184
|
-
if (form[key] !== undefined) {
|
|
185
|
-
if (Array.isArray(form[key])) {
|
|
186
|
-
form[key].push(value);
|
|
187
|
-
} else {
|
|
188
|
-
form[key] = [form[key], value];
|
|
189
|
-
}
|
|
190
|
-
} else {
|
|
191
|
-
if (!key.endsWith("[]")) {
|
|
192
|
-
form[key] = value;
|
|
193
|
-
} else {
|
|
194
|
-
form[key] = [value];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
var handleParsingNestedValues = (form, key, value) => {
|
|
199
|
-
let nestedForm = form;
|
|
200
|
-
const keys = key.split(".");
|
|
201
|
-
keys.forEach((key2, index) => {
|
|
202
|
-
if (index === keys.length - 1) {
|
|
203
|
-
nestedForm[key2] = value;
|
|
204
|
-
} else {
|
|
205
|
-
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) {
|
|
206
|
-
nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
207
|
-
}
|
|
208
|
-
nestedForm = nestedForm[key2];
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
// node_modules/hono/dist/utils/url.js
|
|
214
|
-
var splitPath = (path) => {
|
|
215
|
-
const paths = path.split("/");
|
|
216
|
-
if (paths[0] === "") {
|
|
217
|
-
paths.shift();
|
|
218
|
-
}
|
|
219
|
-
return paths;
|
|
220
|
-
};
|
|
221
|
-
var splitRoutingPath = (routePath) => {
|
|
222
|
-
const { groups, path } = extractGroupsFromPath(routePath);
|
|
223
|
-
const paths = splitPath(path);
|
|
224
|
-
return replaceGroupMarks(paths, groups);
|
|
225
|
-
};
|
|
226
|
-
var extractGroupsFromPath = (path) => {
|
|
227
|
-
const groups = [];
|
|
228
|
-
path = path.replace(/\{[^}]+\}/g, (match, index) => {
|
|
229
|
-
const mark = `@${index}`;
|
|
230
|
-
groups.push([mark, match]);
|
|
231
|
-
return mark;
|
|
232
|
-
});
|
|
233
|
-
return { groups, path };
|
|
234
|
-
};
|
|
235
|
-
var replaceGroupMarks = (paths, groups) => {
|
|
236
|
-
for (let i = groups.length - 1;i >= 0; i--) {
|
|
237
|
-
const [mark] = groups[i];
|
|
238
|
-
for (let j = paths.length - 1;j >= 0; j--) {
|
|
239
|
-
if (paths[j].includes(mark)) {
|
|
240
|
-
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
241
|
-
break;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
return paths;
|
|
246
|
-
};
|
|
247
|
-
var patternCache = {};
|
|
248
|
-
var getPattern2 = (label, next) => {
|
|
249
|
-
if (label === "*") {
|
|
250
|
-
return "*";
|
|
251
|
-
}
|
|
252
|
-
const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
253
|
-
if (match) {
|
|
254
|
-
const cacheKey = `${label}#${next}`;
|
|
255
|
-
if (!patternCache[cacheKey]) {
|
|
256
|
-
if (match[2]) {
|
|
257
|
-
patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [cacheKey, match[1], new RegExp(`^${match[2]}(?=/${next})`)] : [label, match[1], new RegExp(`^${match[2]}$`)];
|
|
258
|
-
} else {
|
|
259
|
-
patternCache[cacheKey] = [label, match[1], true];
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return patternCache[cacheKey];
|
|
263
|
-
}
|
|
264
|
-
return null;
|
|
265
|
-
};
|
|
266
|
-
var tryDecode = (str, decoder) => {
|
|
267
|
-
try {
|
|
268
|
-
return decoder(str);
|
|
269
|
-
} catch {
|
|
270
|
-
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
271
|
-
try {
|
|
272
|
-
return decoder(match);
|
|
273
|
-
} catch {
|
|
274
|
-
return match;
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
280
|
-
var getPath = (request) => {
|
|
281
|
-
const url = request.url;
|
|
282
|
-
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
283
|
-
let i = start;
|
|
284
|
-
for (;i < url.length; i++) {
|
|
285
|
-
const charCode = url.charCodeAt(i);
|
|
286
|
-
if (charCode === 37) {
|
|
287
|
-
const queryIndex = url.indexOf("?", i);
|
|
288
|
-
const path = url.slice(start, queryIndex === -1 ? undefined : queryIndex);
|
|
289
|
-
return tryDecodeURI(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
|
|
290
|
-
} else if (charCode === 63) {
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
return url.slice(start, i);
|
|
295
|
-
};
|
|
296
|
-
var getPathNoStrict = (request) => {
|
|
297
|
-
const result = getPath(request);
|
|
298
|
-
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
299
|
-
};
|
|
300
|
-
var mergePath = (base, sub, ...rest) => {
|
|
301
|
-
if (rest.length) {
|
|
302
|
-
sub = mergePath(sub, ...rest);
|
|
303
|
-
}
|
|
304
|
-
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
305
|
-
};
|
|
306
|
-
var checkOptionalParameter = (path) => {
|
|
307
|
-
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
308
|
-
return null;
|
|
309
|
-
}
|
|
310
|
-
const segments = path.split("/");
|
|
311
|
-
const results = [];
|
|
312
|
-
let basePath = "";
|
|
313
|
-
segments.forEach((segment) => {
|
|
314
|
-
if (segment !== "" && !/\:/.test(segment)) {
|
|
315
|
-
basePath += "/" + segment;
|
|
316
|
-
} else if (/\:/.test(segment)) {
|
|
317
|
-
if (/\?/.test(segment)) {
|
|
318
|
-
if (results.length === 0 && basePath === "") {
|
|
319
|
-
results.push("/");
|
|
320
|
-
} else {
|
|
321
|
-
results.push(basePath);
|
|
322
|
-
}
|
|
323
|
-
const optionalSegment = segment.replace("?", "");
|
|
324
|
-
basePath += "/" + optionalSegment;
|
|
325
|
-
results.push(basePath);
|
|
326
|
-
} else {
|
|
327
|
-
basePath += "/" + segment;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
332
|
-
};
|
|
333
|
-
var _decodeURI = (value) => {
|
|
334
|
-
if (!/[%+]/.test(value)) {
|
|
335
|
-
return value;
|
|
336
|
-
}
|
|
337
|
-
if (value.indexOf("+") !== -1) {
|
|
338
|
-
value = value.replace(/\+/g, " ");
|
|
339
|
-
}
|
|
340
|
-
return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
|
|
341
|
-
};
|
|
342
|
-
var _getQueryParam = (url, key, multiple) => {
|
|
343
|
-
let encoded;
|
|
344
|
-
if (!multiple && key && !/[%+]/.test(key)) {
|
|
345
|
-
let keyIndex2 = url.indexOf(`?${key}`, 8);
|
|
346
|
-
if (keyIndex2 === -1) {
|
|
347
|
-
keyIndex2 = url.indexOf(`&${key}`, 8);
|
|
348
|
-
}
|
|
349
|
-
while (keyIndex2 !== -1) {
|
|
350
|
-
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
351
|
-
if (trailingKeyCode === 61) {
|
|
352
|
-
const valueIndex = keyIndex2 + key.length + 2;
|
|
353
|
-
const endIndex = url.indexOf("&", valueIndex);
|
|
354
|
-
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex));
|
|
355
|
-
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
|
|
356
|
-
return "";
|
|
357
|
-
}
|
|
358
|
-
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
359
|
-
}
|
|
360
|
-
encoded = /[%+]/.test(url);
|
|
361
|
-
if (!encoded) {
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
const results = {};
|
|
366
|
-
encoded ??= /[%+]/.test(url);
|
|
367
|
-
let keyIndex = url.indexOf("?", 8);
|
|
368
|
-
while (keyIndex !== -1) {
|
|
369
|
-
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
370
|
-
let valueIndex = url.indexOf("=", keyIndex);
|
|
371
|
-
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
|
|
372
|
-
valueIndex = -1;
|
|
373
|
-
}
|
|
374
|
-
let name = url.slice(keyIndex + 1, valueIndex === -1 ? nextKeyIndex === -1 ? undefined : nextKeyIndex : valueIndex);
|
|
375
|
-
if (encoded) {
|
|
376
|
-
name = _decodeURI(name);
|
|
377
|
-
}
|
|
378
|
-
keyIndex = nextKeyIndex;
|
|
379
|
-
if (name === "") {
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
let value;
|
|
383
|
-
if (valueIndex === -1) {
|
|
384
|
-
value = "";
|
|
385
|
-
} else {
|
|
386
|
-
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex);
|
|
387
|
-
if (encoded) {
|
|
388
|
-
value = _decodeURI(value);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (multiple) {
|
|
392
|
-
if (!(results[name] && Array.isArray(results[name]))) {
|
|
393
|
-
results[name] = [];
|
|
394
|
-
}
|
|
395
|
-
results[name].push(value);
|
|
396
|
-
} else {
|
|
397
|
-
results[name] ??= value;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return key ? results[key] : results;
|
|
401
|
-
};
|
|
402
|
-
var getQueryParam = _getQueryParam;
|
|
403
|
-
var getQueryParams = (url, key) => {
|
|
404
|
-
return _getQueryParam(url, key, true);
|
|
405
|
-
};
|
|
406
|
-
var decodeURIComponent_ = decodeURIComponent;
|
|
407
|
-
|
|
408
|
-
// node_modules/hono/dist/request.js
|
|
409
|
-
var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
|
410
|
-
var HonoRequest = class {
|
|
411
|
-
raw;
|
|
412
|
-
#validatedData;
|
|
413
|
-
#matchResult;
|
|
414
|
-
routeIndex = 0;
|
|
415
|
-
path;
|
|
416
|
-
bodyCache = {};
|
|
417
|
-
constructor(request, path = "/", matchResult = [[]]) {
|
|
418
|
-
this.raw = request;
|
|
419
|
-
this.path = path;
|
|
420
|
-
this.#matchResult = matchResult;
|
|
421
|
-
this.#validatedData = {};
|
|
422
|
-
}
|
|
423
|
-
param(key) {
|
|
424
|
-
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
425
|
-
}
|
|
426
|
-
#getDecodedParam(key) {
|
|
427
|
-
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
428
|
-
const param = this.#getParamValue(paramKey);
|
|
429
|
-
return param ? /\%/.test(param) ? tryDecodeURIComponent(param) : param : undefined;
|
|
430
|
-
}
|
|
431
|
-
#getAllDecodedParams() {
|
|
432
|
-
const decoded = {};
|
|
433
|
-
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
434
|
-
for (const key of keys) {
|
|
435
|
-
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
436
|
-
if (value && typeof value === "string") {
|
|
437
|
-
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return decoded;
|
|
441
|
-
}
|
|
442
|
-
#getParamValue(paramKey) {
|
|
443
|
-
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
444
|
-
}
|
|
445
|
-
query(key) {
|
|
446
|
-
return getQueryParam(this.url, key);
|
|
447
|
-
}
|
|
448
|
-
queries(key) {
|
|
449
|
-
return getQueryParams(this.url, key);
|
|
450
|
-
}
|
|
451
|
-
header(name) {
|
|
452
|
-
if (name) {
|
|
453
|
-
return this.raw.headers.get(name) ?? undefined;
|
|
454
|
-
}
|
|
455
|
-
const headerData = {};
|
|
456
|
-
this.raw.headers.forEach((value, key) => {
|
|
457
|
-
headerData[key] = value;
|
|
458
|
-
});
|
|
459
|
-
return headerData;
|
|
460
|
-
}
|
|
461
|
-
async parseBody(options) {
|
|
462
|
-
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
463
|
-
}
|
|
464
|
-
#cachedBody = (key) => {
|
|
465
|
-
const { bodyCache, raw } = this;
|
|
466
|
-
const cachedBody = bodyCache[key];
|
|
467
|
-
if (cachedBody) {
|
|
468
|
-
return cachedBody;
|
|
469
|
-
}
|
|
470
|
-
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
471
|
-
if (anyCachedKey) {
|
|
472
|
-
return bodyCache[anyCachedKey].then((body) => {
|
|
473
|
-
if (anyCachedKey === "json") {
|
|
474
|
-
body = JSON.stringify(body);
|
|
475
|
-
}
|
|
476
|
-
return new Response(body)[key]();
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
return bodyCache[key] = raw[key]();
|
|
480
|
-
};
|
|
481
|
-
json() {
|
|
482
|
-
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
483
|
-
}
|
|
484
|
-
text() {
|
|
485
|
-
return this.#cachedBody("text");
|
|
486
|
-
}
|
|
487
|
-
arrayBuffer() {
|
|
488
|
-
return this.#cachedBody("arrayBuffer");
|
|
489
|
-
}
|
|
490
|
-
blob() {
|
|
491
|
-
return this.#cachedBody("blob");
|
|
492
|
-
}
|
|
493
|
-
formData() {
|
|
494
|
-
return this.#cachedBody("formData");
|
|
495
|
-
}
|
|
496
|
-
addValidatedData(target, data) {
|
|
497
|
-
this.#validatedData[target] = data;
|
|
498
|
-
}
|
|
499
|
-
valid(target) {
|
|
500
|
-
return this.#validatedData[target];
|
|
501
|
-
}
|
|
502
|
-
get url() {
|
|
503
|
-
return this.raw.url;
|
|
504
|
-
}
|
|
505
|
-
get method() {
|
|
506
|
-
return this.raw.method;
|
|
507
|
-
}
|
|
508
|
-
get [GET_MATCH_RESULT]() {
|
|
509
|
-
return this.#matchResult;
|
|
510
|
-
}
|
|
511
|
-
get matchedRoutes() {
|
|
512
|
-
return this.#matchResult[0].map(([[, route]]) => route);
|
|
513
|
-
}
|
|
514
|
-
get routePath() {
|
|
515
|
-
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
516
|
-
}
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
// node_modules/hono/dist/utils/html.js
|
|
520
|
-
var HtmlEscapedCallbackPhase = {
|
|
521
|
-
Stringify: 1,
|
|
522
|
-
BeforeStream: 2,
|
|
523
|
-
Stream: 3
|
|
524
|
-
};
|
|
525
|
-
var raw = (value, callbacks) => {
|
|
526
|
-
const escapedString = new String(value);
|
|
527
|
-
escapedString.isEscaped = true;
|
|
528
|
-
escapedString.callbacks = callbacks;
|
|
529
|
-
return escapedString;
|
|
530
|
-
};
|
|
531
|
-
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
532
|
-
if (typeof str === "object" && !(str instanceof String)) {
|
|
533
|
-
if (!(str instanceof Promise)) {
|
|
534
|
-
str = str.toString();
|
|
535
|
-
}
|
|
536
|
-
if (str instanceof Promise) {
|
|
537
|
-
str = await str;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
const callbacks = str.callbacks;
|
|
541
|
-
if (!callbacks?.length) {
|
|
542
|
-
return Promise.resolve(str);
|
|
543
|
-
}
|
|
544
|
-
if (buffer) {
|
|
545
|
-
buffer[0] += str;
|
|
546
|
-
} else {
|
|
547
|
-
buffer = [str];
|
|
548
|
-
}
|
|
549
|
-
const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then((res) => Promise.all(res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))).then(() => buffer[0]));
|
|
550
|
-
if (preserveCallbacks) {
|
|
551
|
-
return raw(await resStr, callbacks);
|
|
552
|
-
} else {
|
|
553
|
-
return resStr;
|
|
554
|
-
}
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
// node_modules/hono/dist/context.js
|
|
558
|
-
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
559
|
-
var setDefaultContentType = (contentType, headers) => {
|
|
560
|
-
return {
|
|
561
|
-
"Content-Type": contentType,
|
|
562
|
-
...headers
|
|
563
|
-
};
|
|
564
|
-
};
|
|
565
|
-
var Context = class {
|
|
566
|
-
#rawRequest;
|
|
567
|
-
#req;
|
|
568
|
-
env = {};
|
|
569
|
-
#var;
|
|
570
|
-
finalized = false;
|
|
571
|
-
error;
|
|
572
|
-
#status;
|
|
573
|
-
#executionCtx;
|
|
574
|
-
#res;
|
|
575
|
-
#layout;
|
|
576
|
-
#renderer;
|
|
577
|
-
#notFoundHandler;
|
|
578
|
-
#preparedHeaders;
|
|
579
|
-
#matchResult;
|
|
580
|
-
#path;
|
|
581
|
-
constructor(req, options) {
|
|
582
|
-
this.#rawRequest = req;
|
|
583
|
-
if (options) {
|
|
584
|
-
this.#executionCtx = options.executionCtx;
|
|
585
|
-
this.env = options.env;
|
|
586
|
-
this.#notFoundHandler = options.notFoundHandler;
|
|
587
|
-
this.#path = options.path;
|
|
588
|
-
this.#matchResult = options.matchResult;
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
get req() {
|
|
592
|
-
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
593
|
-
return this.#req;
|
|
594
|
-
}
|
|
595
|
-
get event() {
|
|
596
|
-
if (this.#executionCtx && "respondWith" in this.#executionCtx) {
|
|
597
|
-
return this.#executionCtx;
|
|
598
|
-
} else {
|
|
599
|
-
throw Error("This context has no FetchEvent");
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
get executionCtx() {
|
|
603
|
-
if (this.#executionCtx) {
|
|
604
|
-
return this.#executionCtx;
|
|
605
|
-
} else {
|
|
606
|
-
throw Error("This context has no ExecutionContext");
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
get res() {
|
|
610
|
-
return this.#res ||= new Response(null, {
|
|
611
|
-
headers: this.#preparedHeaders ??= new Headers
|
|
612
|
-
});
|
|
613
|
-
}
|
|
614
|
-
set res(_res) {
|
|
615
|
-
if (this.#res && _res) {
|
|
616
|
-
_res = new Response(_res.body, _res);
|
|
617
|
-
for (const [k, v] of this.#res.headers.entries()) {
|
|
618
|
-
if (k === "content-type") {
|
|
619
|
-
continue;
|
|
620
|
-
}
|
|
621
|
-
if (k === "set-cookie") {
|
|
622
|
-
const cookies = this.#res.headers.getSetCookie();
|
|
623
|
-
_res.headers.delete("set-cookie");
|
|
624
|
-
for (const cookie of cookies) {
|
|
625
|
-
_res.headers.append("set-cookie", cookie);
|
|
626
|
-
}
|
|
627
|
-
} else {
|
|
628
|
-
_res.headers.set(k, v);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
this.#res = _res;
|
|
633
|
-
this.finalized = true;
|
|
634
|
-
}
|
|
635
|
-
render = (...args) => {
|
|
636
|
-
this.#renderer ??= (content) => this.html(content);
|
|
637
|
-
return this.#renderer(...args);
|
|
638
|
-
};
|
|
639
|
-
setLayout = (layout) => this.#layout = layout;
|
|
640
|
-
getLayout = () => this.#layout;
|
|
641
|
-
setRenderer = (renderer) => {
|
|
642
|
-
this.#renderer = renderer;
|
|
643
|
-
};
|
|
644
|
-
header = (name, value, options) => {
|
|
645
|
-
if (this.finalized) {
|
|
646
|
-
this.#res = new Response(this.#res.body, this.#res);
|
|
647
|
-
}
|
|
648
|
-
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers;
|
|
649
|
-
if (value === undefined) {
|
|
650
|
-
headers.delete(name);
|
|
651
|
-
} else if (options?.append) {
|
|
652
|
-
headers.append(name, value);
|
|
653
|
-
} else {
|
|
654
|
-
headers.set(name, value);
|
|
655
|
-
}
|
|
656
|
-
};
|
|
657
|
-
status = (status) => {
|
|
658
|
-
this.#status = status;
|
|
659
|
-
};
|
|
660
|
-
set = (key, value) => {
|
|
661
|
-
this.#var ??= /* @__PURE__ */ new Map;
|
|
662
|
-
this.#var.set(key, value);
|
|
663
|
-
};
|
|
664
|
-
get = (key) => {
|
|
665
|
-
return this.#var ? this.#var.get(key) : undefined;
|
|
666
|
-
};
|
|
667
|
-
get var() {
|
|
668
|
-
if (!this.#var) {
|
|
669
|
-
return {};
|
|
670
|
-
}
|
|
671
|
-
return Object.fromEntries(this.#var);
|
|
672
|
-
}
|
|
673
|
-
#newResponse(data, arg, headers) {
|
|
674
|
-
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers;
|
|
675
|
-
if (typeof arg === "object" && "headers" in arg) {
|
|
676
|
-
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
677
|
-
for (const [key, value] of argHeaders) {
|
|
678
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
679
|
-
responseHeaders.append(key, value);
|
|
680
|
-
} else {
|
|
681
|
-
responseHeaders.set(key, value);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
if (headers) {
|
|
686
|
-
for (const [k, v] of Object.entries(headers)) {
|
|
687
|
-
if (typeof v === "string") {
|
|
688
|
-
responseHeaders.set(k, v);
|
|
689
|
-
} else {
|
|
690
|
-
responseHeaders.delete(k);
|
|
691
|
-
for (const v2 of v) {
|
|
692
|
-
responseHeaders.append(k, v2);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
const status = typeof arg === "number" ? arg : arg?.status ?? this.#status;
|
|
698
|
-
return new Response(data, { status, headers: responseHeaders });
|
|
699
|
-
}
|
|
700
|
-
newResponse = (...args) => this.#newResponse(...args);
|
|
701
|
-
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
702
|
-
text = (text, arg, headers) => {
|
|
703
|
-
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(text, arg, setDefaultContentType(TEXT_PLAIN, headers));
|
|
704
|
-
};
|
|
705
|
-
json = (object, arg, headers) => {
|
|
706
|
-
return this.#newResponse(JSON.stringify(object), arg, setDefaultContentType("application/json", headers));
|
|
707
|
-
};
|
|
708
|
-
html = (html, arg, headers) => {
|
|
709
|
-
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
710
|
-
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
711
|
-
};
|
|
712
|
-
redirect = (location, status) => {
|
|
713
|
-
const locationString = String(location);
|
|
714
|
-
this.header("Location", !/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString));
|
|
715
|
-
return this.newResponse(null, status ?? 302);
|
|
716
|
-
};
|
|
717
|
-
notFound = () => {
|
|
718
|
-
this.#notFoundHandler ??= () => new Response;
|
|
719
|
-
return this.#notFoundHandler(this);
|
|
720
|
-
};
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
// node_modules/hono/dist/router.js
|
|
724
|
-
var METHOD_NAME_ALL = "ALL";
|
|
725
|
-
var METHOD_NAME_ALL_LOWERCASE = "all";
|
|
726
|
-
var METHODS = ["get", "post", "put", "delete", "options", "patch"];
|
|
727
|
-
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
728
|
-
var UnsupportedPathError = class extends Error {
|
|
729
|
-
};
|
|
730
|
-
|
|
731
|
-
// node_modules/hono/dist/utils/constants.js
|
|
732
|
-
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
733
|
-
|
|
734
|
-
// node_modules/hono/dist/hono-base.js
|
|
735
|
-
var notFoundHandler = (c) => {
|
|
736
|
-
return c.text("404 Not Found", 404);
|
|
737
|
-
};
|
|
738
|
-
var errorHandler = (err, c) => {
|
|
739
|
-
if ("getResponse" in err) {
|
|
740
|
-
const res = err.getResponse();
|
|
741
|
-
return c.newResponse(res.body, res);
|
|
742
|
-
}
|
|
743
|
-
console.error(err);
|
|
744
|
-
return c.text("Internal Server Error", 500);
|
|
745
|
-
};
|
|
746
|
-
var Hono = class {
|
|
747
|
-
get;
|
|
748
|
-
post;
|
|
749
|
-
put;
|
|
750
|
-
delete;
|
|
751
|
-
options;
|
|
752
|
-
patch;
|
|
753
|
-
all;
|
|
754
|
-
on;
|
|
755
|
-
use;
|
|
756
|
-
router;
|
|
757
|
-
getPath;
|
|
758
|
-
_basePath = "/";
|
|
759
|
-
#path = "/";
|
|
760
|
-
routes = [];
|
|
761
|
-
constructor(options = {}) {
|
|
762
|
-
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
763
|
-
allMethods.forEach((method) => {
|
|
764
|
-
this[method] = (args1, ...args) => {
|
|
765
|
-
if (typeof args1 === "string") {
|
|
766
|
-
this.#path = args1;
|
|
767
|
-
} else {
|
|
768
|
-
this.#addRoute(method, this.#path, args1);
|
|
769
|
-
}
|
|
770
|
-
args.forEach((handler) => {
|
|
771
|
-
this.#addRoute(method, this.#path, handler);
|
|
772
|
-
});
|
|
773
|
-
return this;
|
|
774
|
-
};
|
|
775
|
-
});
|
|
776
|
-
this.on = (method, path, ...handlers) => {
|
|
777
|
-
for (const p of [path].flat()) {
|
|
778
|
-
this.#path = p;
|
|
779
|
-
for (const m of [method].flat()) {
|
|
780
|
-
handlers.map((handler) => {
|
|
781
|
-
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
return this;
|
|
786
|
-
};
|
|
787
|
-
this.use = (arg1, ...handlers) => {
|
|
788
|
-
if (typeof arg1 === "string") {
|
|
789
|
-
this.#path = arg1;
|
|
790
|
-
} else {
|
|
791
|
-
this.#path = "*";
|
|
792
|
-
handlers.unshift(arg1);
|
|
793
|
-
}
|
|
794
|
-
handlers.forEach((handler) => {
|
|
795
|
-
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
796
|
-
});
|
|
797
|
-
return this;
|
|
798
|
-
};
|
|
799
|
-
const { strict, ...optionsWithoutStrict } = options;
|
|
800
|
-
Object.assign(this, optionsWithoutStrict);
|
|
801
|
-
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
802
|
-
}
|
|
803
|
-
#clone() {
|
|
804
|
-
const clone = new Hono({
|
|
805
|
-
router: this.router,
|
|
806
|
-
getPath: this.getPath
|
|
807
|
-
});
|
|
808
|
-
clone.errorHandler = this.errorHandler;
|
|
809
|
-
clone.#notFoundHandler = this.#notFoundHandler;
|
|
810
|
-
clone.routes = this.routes;
|
|
811
|
-
return clone;
|
|
812
|
-
}
|
|
813
|
-
#notFoundHandler = notFoundHandler;
|
|
814
|
-
errorHandler = errorHandler;
|
|
815
|
-
route(path, app) {
|
|
816
|
-
const subApp = this.basePath(path);
|
|
817
|
-
app.routes.map((r) => {
|
|
818
|
-
let handler;
|
|
819
|
-
if (app.errorHandler === errorHandler) {
|
|
820
|
-
handler = r.handler;
|
|
821
|
-
} else {
|
|
822
|
-
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
823
|
-
handler[COMPOSED_HANDLER] = r.handler;
|
|
824
|
-
}
|
|
825
|
-
subApp.#addRoute(r.method, r.path, handler);
|
|
826
|
-
});
|
|
827
|
-
return this;
|
|
828
|
-
}
|
|
829
|
-
basePath(path) {
|
|
830
|
-
const subApp = this.#clone();
|
|
831
|
-
subApp._basePath = mergePath(this._basePath, path);
|
|
832
|
-
return subApp;
|
|
833
|
-
}
|
|
834
|
-
onError = (handler) => {
|
|
835
|
-
this.errorHandler = handler;
|
|
836
|
-
return this;
|
|
837
|
-
};
|
|
838
|
-
notFound = (handler) => {
|
|
839
|
-
this.#notFoundHandler = handler;
|
|
840
|
-
return this;
|
|
841
|
-
};
|
|
842
|
-
mount(path, applicationHandler, options) {
|
|
843
|
-
let replaceRequest;
|
|
844
|
-
let optionHandler;
|
|
845
|
-
if (options) {
|
|
846
|
-
if (typeof options === "function") {
|
|
847
|
-
optionHandler = options;
|
|
848
|
-
} else {
|
|
849
|
-
optionHandler = options.optionHandler;
|
|
850
|
-
if (options.replaceRequest === false) {
|
|
851
|
-
replaceRequest = (request) => request;
|
|
852
|
-
} else {
|
|
853
|
-
replaceRequest = options.replaceRequest;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
const getOptions = optionHandler ? (c) => {
|
|
858
|
-
const options2 = optionHandler(c);
|
|
859
|
-
return Array.isArray(options2) ? options2 : [options2];
|
|
860
|
-
} : (c) => {
|
|
861
|
-
let executionContext = undefined;
|
|
862
|
-
try {
|
|
863
|
-
executionContext = c.executionCtx;
|
|
864
|
-
} catch {}
|
|
865
|
-
return [c.env, executionContext];
|
|
866
|
-
};
|
|
867
|
-
replaceRequest ||= (() => {
|
|
868
|
-
const mergedPath = mergePath(this._basePath, path);
|
|
869
|
-
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
870
|
-
return (request) => {
|
|
871
|
-
const url = new URL(request.url);
|
|
872
|
-
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
873
|
-
return new Request(url, request);
|
|
874
|
-
};
|
|
875
|
-
})();
|
|
876
|
-
const handler = async (c, next) => {
|
|
877
|
-
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
878
|
-
if (res) {
|
|
879
|
-
return res;
|
|
880
|
-
}
|
|
881
|
-
await next();
|
|
882
|
-
};
|
|
883
|
-
this.#addRoute(METHOD_NAME_ALL, mergePath(path, "*"), handler);
|
|
884
|
-
return this;
|
|
885
|
-
}
|
|
886
|
-
#addRoute(method, path, handler) {
|
|
887
|
-
method = method.toUpperCase();
|
|
888
|
-
path = mergePath(this._basePath, path);
|
|
889
|
-
const r = { basePath: this._basePath, path, method, handler };
|
|
890
|
-
this.router.add(method, path, [handler, r]);
|
|
891
|
-
this.routes.push(r);
|
|
892
|
-
}
|
|
893
|
-
#handleError(err, c) {
|
|
894
|
-
if (err instanceof Error) {
|
|
895
|
-
return this.errorHandler(err, c);
|
|
896
|
-
}
|
|
897
|
-
throw err;
|
|
898
|
-
}
|
|
899
|
-
#dispatch(request, executionCtx, env, method) {
|
|
900
|
-
if (method === "HEAD") {
|
|
901
|
-
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
902
|
-
}
|
|
903
|
-
const path = this.getPath(request, { env });
|
|
904
|
-
const matchResult = this.router.match(method, path);
|
|
905
|
-
const c = new Context(request, {
|
|
906
|
-
path,
|
|
907
|
-
matchResult,
|
|
908
|
-
env,
|
|
909
|
-
executionCtx,
|
|
910
|
-
notFoundHandler: this.#notFoundHandler
|
|
911
|
-
});
|
|
912
|
-
if (matchResult[0].length === 1) {
|
|
913
|
-
let res;
|
|
914
|
-
try {
|
|
915
|
-
res = matchResult[0][0][0][0](c, async () => {
|
|
916
|
-
c.res = await this.#notFoundHandler(c);
|
|
917
|
-
});
|
|
918
|
-
} catch (err) {
|
|
919
|
-
return this.#handleError(err, c);
|
|
920
|
-
}
|
|
921
|
-
return res instanceof Promise ? res.then((resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
922
|
-
}
|
|
923
|
-
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
924
|
-
return (async () => {
|
|
925
|
-
try {
|
|
926
|
-
const context = await composed(c);
|
|
927
|
-
if (!context.finalized) {
|
|
928
|
-
throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");
|
|
929
|
-
}
|
|
930
|
-
return context.res;
|
|
931
|
-
} catch (err) {
|
|
932
|
-
return this.#handleError(err, c);
|
|
933
|
-
}
|
|
934
|
-
})();
|
|
935
|
-
}
|
|
936
|
-
fetch = (request, ...rest) => {
|
|
937
|
-
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
938
|
-
};
|
|
939
|
-
request = (input, requestInit, Env, executionCtx) => {
|
|
940
|
-
if (input instanceof Request) {
|
|
941
|
-
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
942
|
-
}
|
|
943
|
-
input = input.toString();
|
|
944
|
-
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`, requestInit), Env, executionCtx);
|
|
945
|
-
};
|
|
946
|
-
fire = () => {
|
|
947
|
-
addEventListener("fetch", (event) => {
|
|
948
|
-
event.respondWith(this.#dispatch(event.request, event, undefined, event.request.method));
|
|
949
|
-
});
|
|
950
|
-
};
|
|
951
|
-
};
|
|
952
|
-
|
|
953
|
-
// node_modules/hono/dist/router/reg-exp-router/node.js
|
|
954
|
-
var LABEL_REG_EXP_STR = "[^/]+";
|
|
955
|
-
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
956
|
-
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
957
|
-
var PATH_ERROR = Symbol();
|
|
958
|
-
var regExpMetaChars = new Set(".\\+*[^]$()");
|
|
959
|
-
function compareKey(a, b) {
|
|
960
|
-
if (a.length === 1) {
|
|
961
|
-
return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
962
|
-
}
|
|
963
|
-
if (b.length === 1) {
|
|
964
|
-
return 1;
|
|
965
|
-
}
|
|
966
|
-
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
|
|
967
|
-
return 1;
|
|
968
|
-
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
|
|
969
|
-
return -1;
|
|
970
|
-
}
|
|
971
|
-
if (a === LABEL_REG_EXP_STR) {
|
|
972
|
-
return 1;
|
|
973
|
-
} else if (b === LABEL_REG_EXP_STR) {
|
|
974
|
-
return -1;
|
|
975
|
-
}
|
|
976
|
-
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
977
|
-
}
|
|
978
|
-
var Node = class {
|
|
979
|
-
#index;
|
|
980
|
-
#varIndex;
|
|
981
|
-
#children = /* @__PURE__ */ Object.create(null);
|
|
982
|
-
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
983
|
-
if (tokens.length === 0) {
|
|
984
|
-
if (this.#index !== undefined) {
|
|
985
|
-
throw PATH_ERROR;
|
|
986
|
-
}
|
|
987
|
-
if (pathErrorCheckOnly) {
|
|
988
|
-
return;
|
|
989
|
-
}
|
|
990
|
-
this.#index = index;
|
|
991
|
-
return;
|
|
992
|
-
}
|
|
993
|
-
const [token, ...restTokens] = tokens;
|
|
994
|
-
const pattern2 = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
995
|
-
let node;
|
|
996
|
-
if (pattern2) {
|
|
997
|
-
const name = pattern2[1];
|
|
998
|
-
let regexpStr = pattern2[2] || LABEL_REG_EXP_STR;
|
|
999
|
-
if (name && pattern2[2]) {
|
|
1000
|
-
if (regexpStr === ".*") {
|
|
1001
|
-
throw PATH_ERROR;
|
|
1002
|
-
}
|
|
1003
|
-
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
1004
|
-
if (/\((?!\?:)/.test(regexpStr)) {
|
|
1005
|
-
throw PATH_ERROR;
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
node = this.#children[regexpStr];
|
|
1009
|
-
if (!node) {
|
|
1010
|
-
if (Object.keys(this.#children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
|
|
1011
|
-
throw PATH_ERROR;
|
|
1012
|
-
}
|
|
1013
|
-
if (pathErrorCheckOnly) {
|
|
1014
|
-
return;
|
|
1015
|
-
}
|
|
1016
|
-
node = this.#children[regexpStr] = new Node;
|
|
1017
|
-
if (name !== "") {
|
|
1018
|
-
node.#varIndex = context.varIndex++;
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
if (!pathErrorCheckOnly && name !== "") {
|
|
1022
|
-
paramMap.push([name, node.#varIndex]);
|
|
1023
|
-
}
|
|
1024
|
-
} else {
|
|
1025
|
-
node = this.#children[token];
|
|
1026
|
-
if (!node) {
|
|
1027
|
-
if (Object.keys(this.#children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
|
|
1028
|
-
throw PATH_ERROR;
|
|
1029
|
-
}
|
|
1030
|
-
if (pathErrorCheckOnly) {
|
|
1031
|
-
return;
|
|
1032
|
-
}
|
|
1033
|
-
node = this.#children[token] = new Node;
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
1037
|
-
}
|
|
1038
|
-
buildRegExpStr() {
|
|
1039
|
-
const childKeys = Object.keys(this.#children).sort(compareKey);
|
|
1040
|
-
const strList = childKeys.map((k) => {
|
|
1041
|
-
const c = this.#children[k];
|
|
1042
|
-
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
1043
|
-
});
|
|
1044
|
-
if (typeof this.#index === "number") {
|
|
1045
|
-
strList.unshift(`#${this.#index}`);
|
|
1046
|
-
}
|
|
1047
|
-
if (strList.length === 0) {
|
|
1048
|
-
return "";
|
|
1049
|
-
}
|
|
1050
|
-
if (strList.length === 1) {
|
|
1051
|
-
return strList[0];
|
|
1052
|
-
}
|
|
1053
|
-
return "(?:" + strList.join("|") + ")";
|
|
1054
|
-
}
|
|
1055
|
-
};
|
|
1056
|
-
|
|
1057
|
-
// node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
1058
|
-
var Trie = class {
|
|
1059
|
-
#context = { varIndex: 0 };
|
|
1060
|
-
#root = new Node;
|
|
1061
|
-
insert(path, index, pathErrorCheckOnly) {
|
|
1062
|
-
const paramAssoc = [];
|
|
1063
|
-
const groups = [];
|
|
1064
|
-
for (let i = 0;; ) {
|
|
1065
|
-
let replaced = false;
|
|
1066
|
-
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
1067
|
-
const mark = `@\\${i}`;
|
|
1068
|
-
groups[i] = [mark, m];
|
|
1069
|
-
i++;
|
|
1070
|
-
replaced = true;
|
|
1071
|
-
return mark;
|
|
1072
|
-
});
|
|
1073
|
-
if (!replaced) {
|
|
1074
|
-
break;
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
1078
|
-
for (let i = groups.length - 1;i >= 0; i--) {
|
|
1079
|
-
const [mark] = groups[i];
|
|
1080
|
-
for (let j = tokens.length - 1;j >= 0; j--) {
|
|
1081
|
-
if (tokens[j].indexOf(mark) !== -1) {
|
|
1082
|
-
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
1083
|
-
break;
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1087
|
-
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
1088
|
-
return paramAssoc;
|
|
1089
|
-
}
|
|
1090
|
-
buildRegExp() {
|
|
1091
|
-
let regexp = this.#root.buildRegExpStr();
|
|
1092
|
-
if (regexp === "") {
|
|
1093
|
-
return [/^$/, [], []];
|
|
1094
|
-
}
|
|
1095
|
-
let captureIndex = 0;
|
|
1096
|
-
const indexReplacementMap = [];
|
|
1097
|
-
const paramReplacementMap = [];
|
|
1098
|
-
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
1099
|
-
if (handlerIndex !== undefined) {
|
|
1100
|
-
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
1101
|
-
return "$()";
|
|
1102
|
-
}
|
|
1103
|
-
if (paramIndex !== undefined) {
|
|
1104
|
-
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
1105
|
-
return "";
|
|
1106
|
-
}
|
|
1107
|
-
return "";
|
|
1108
|
-
});
|
|
1109
|
-
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
|
|
1110
|
-
}
|
|
1111
|
-
};
|
|
1112
|
-
|
|
1113
|
-
// node_modules/hono/dist/router/reg-exp-router/router.js
|
|
1114
|
-
var emptyParam = [];
|
|
1115
|
-
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
1116
|
-
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1117
|
-
function buildWildcardRegExp(path) {
|
|
1118
|
-
return wildcardRegExpCache[path] ??= new RegExp(path === "*" ? "" : `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)")}$`);
|
|
1119
|
-
}
|
|
1120
|
-
function clearWildcardRegExpCache() {
|
|
1121
|
-
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1122
|
-
}
|
|
1123
|
-
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
1124
|
-
const trie = new Trie;
|
|
1125
|
-
const handlerData = [];
|
|
1126
|
-
if (routes.length === 0) {
|
|
1127
|
-
return nullMatcher;
|
|
1128
|
-
}
|
|
1129
|
-
const routesWithStaticPathFlag = routes.map((route) => [!/\*|\/:/.test(route[0]), ...route]).sort(([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length);
|
|
1130
|
-
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
1131
|
-
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length;i < len; i++) {
|
|
1132
|
-
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
1133
|
-
if (pathErrorCheckOnly) {
|
|
1134
|
-
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
1135
|
-
} else {
|
|
1136
|
-
j++;
|
|
1137
|
-
}
|
|
1138
|
-
let paramAssoc;
|
|
1139
|
-
try {
|
|
1140
|
-
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
|
1141
|
-
} catch (e) {
|
|
1142
|
-
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
|
1143
|
-
}
|
|
1144
|
-
if (pathErrorCheckOnly) {
|
|
1145
|
-
continue;
|
|
1146
|
-
}
|
|
1147
|
-
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
1148
|
-
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
1149
|
-
paramCount -= 1;
|
|
1150
|
-
for (;paramCount >= 0; paramCount--) {
|
|
1151
|
-
const [key, value] = paramAssoc[paramCount];
|
|
1152
|
-
paramIndexMap[key] = value;
|
|
1153
|
-
}
|
|
1154
|
-
return [h, paramIndexMap];
|
|
1155
|
-
});
|
|
1156
|
-
}
|
|
1157
|
-
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
1158
|
-
for (let i = 0, len = handlerData.length;i < len; i++) {
|
|
1159
|
-
for (let j = 0, len2 = handlerData[i].length;j < len2; j++) {
|
|
1160
|
-
const map = handlerData[i][j]?.[1];
|
|
1161
|
-
if (!map) {
|
|
1162
|
-
continue;
|
|
1163
|
-
}
|
|
1164
|
-
const keys = Object.keys(map);
|
|
1165
|
-
for (let k = 0, len3 = keys.length;k < len3; k++) {
|
|
1166
|
-
map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
const handlerMap = [];
|
|
1171
|
-
for (const i in indexReplacementMap) {
|
|
1172
|
-
handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
1173
|
-
}
|
|
1174
|
-
return [regexp, handlerMap, staticMap];
|
|
1175
|
-
}
|
|
1176
|
-
function findMiddleware(middleware, path) {
|
|
1177
|
-
if (!middleware) {
|
|
1178
|
-
return;
|
|
1179
|
-
}
|
|
1180
|
-
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
1181
|
-
if (buildWildcardRegExp(k).test(path)) {
|
|
1182
|
-
return [...middleware[k]];
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
return;
|
|
1186
|
-
}
|
|
1187
|
-
var RegExpRouter = class {
|
|
1188
|
-
name = "RegExpRouter";
|
|
1189
|
-
#middleware;
|
|
1190
|
-
#routes;
|
|
1191
|
-
constructor() {
|
|
1192
|
-
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1193
|
-
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1194
|
-
}
|
|
1195
|
-
add(method, path, handler) {
|
|
1196
|
-
const middleware = this.#middleware;
|
|
1197
|
-
const routes = this.#routes;
|
|
1198
|
-
if (!middleware || !routes) {
|
|
1199
|
-
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1200
|
-
}
|
|
1201
|
-
if (!middleware[method]) {
|
|
1202
|
-
[middleware, routes].forEach((handlerMap) => {
|
|
1203
|
-
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
1204
|
-
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
1205
|
-
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
1206
|
-
});
|
|
1207
|
-
});
|
|
1208
|
-
}
|
|
1209
|
-
if (path === "/*") {
|
|
1210
|
-
path = "*";
|
|
1211
|
-
}
|
|
1212
|
-
const paramCount = (path.match(/\/:/g) || []).length;
|
|
1213
|
-
if (/\*$/.test(path)) {
|
|
1214
|
-
const re = buildWildcardRegExp(path);
|
|
1215
|
-
if (method === METHOD_NAME_ALL) {
|
|
1216
|
-
Object.keys(middleware).forEach((m) => {
|
|
1217
|
-
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1218
|
-
});
|
|
1219
|
-
} else {
|
|
1220
|
-
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1221
|
-
}
|
|
1222
|
-
Object.keys(middleware).forEach((m) => {
|
|
1223
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
1224
|
-
Object.keys(middleware[m]).forEach((p) => {
|
|
1225
|
-
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
1226
|
-
});
|
|
1227
|
-
}
|
|
1228
|
-
});
|
|
1229
|
-
Object.keys(routes).forEach((m) => {
|
|
1230
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
1231
|
-
Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push([handler, paramCount]));
|
|
1232
|
-
}
|
|
1233
|
-
});
|
|
1234
|
-
return;
|
|
1235
|
-
}
|
|
1236
|
-
const paths = checkOptionalParameter(path) || [path];
|
|
1237
|
-
for (let i = 0, len = paths.length;i < len; i++) {
|
|
1238
|
-
const path2 = paths[i];
|
|
1239
|
-
Object.keys(routes).forEach((m) => {
|
|
1240
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
1241
|
-
routes[m][path2] ||= [
|
|
1242
|
-
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
1243
|
-
];
|
|
1244
|
-
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
1245
|
-
}
|
|
1246
|
-
});
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
match(method, path) {
|
|
1250
|
-
clearWildcardRegExpCache();
|
|
1251
|
-
const matchers = this.#buildAllMatchers();
|
|
1252
|
-
this.match = (method2, path2) => {
|
|
1253
|
-
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
1254
|
-
const staticMatch = matcher[2][path2];
|
|
1255
|
-
if (staticMatch) {
|
|
1256
|
-
return staticMatch;
|
|
1257
|
-
}
|
|
1258
|
-
const match = path2.match(matcher[0]);
|
|
1259
|
-
if (!match) {
|
|
1260
|
-
return [[], emptyParam];
|
|
1261
|
-
}
|
|
1262
|
-
const index = match.indexOf("", 1);
|
|
1263
|
-
return [matcher[1][index], match];
|
|
1264
|
-
};
|
|
1265
|
-
return this.match(method, path);
|
|
1266
|
-
}
|
|
1267
|
-
#buildAllMatchers() {
|
|
1268
|
-
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1269
|
-
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
1270
|
-
matchers[method] ||= this.#buildMatcher(method);
|
|
1271
|
-
});
|
|
1272
|
-
this.#middleware = this.#routes = undefined;
|
|
1273
|
-
return matchers;
|
|
1274
|
-
}
|
|
1275
|
-
#buildMatcher(method) {
|
|
1276
|
-
const routes = [];
|
|
1277
|
-
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
1278
|
-
[this.#middleware, this.#routes].forEach((r) => {
|
|
1279
|
-
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
1280
|
-
if (ownRoute.length !== 0) {
|
|
1281
|
-
hasOwnRoute ||= true;
|
|
1282
|
-
routes.push(...ownRoute);
|
|
1283
|
-
} else if (method !== METHOD_NAME_ALL) {
|
|
1284
|
-
routes.push(...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]]));
|
|
1285
|
-
}
|
|
1286
|
-
});
|
|
1287
|
-
if (!hasOwnRoute) {
|
|
1288
|
-
return null;
|
|
1289
|
-
} else {
|
|
1290
|
-
return buildMatcherFromPreprocessedRoutes(routes);
|
|
1291
|
-
}
|
|
1292
|
-
}
|
|
1293
|
-
};
|
|
1294
|
-
|
|
1295
|
-
// node_modules/hono/dist/router/smart-router/router.js
|
|
1296
|
-
var SmartRouter = class {
|
|
1297
|
-
name = "SmartRouter";
|
|
1298
|
-
#routers = [];
|
|
1299
|
-
#routes = [];
|
|
1300
|
-
constructor(init) {
|
|
1301
|
-
this.#routers = init.routers;
|
|
1302
|
-
}
|
|
1303
|
-
add(method, path, handler) {
|
|
1304
|
-
if (!this.#routes) {
|
|
1305
|
-
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1306
|
-
}
|
|
1307
|
-
this.#routes.push([method, path, handler]);
|
|
1308
|
-
}
|
|
1309
|
-
match(method, path) {
|
|
1310
|
-
if (!this.#routes) {
|
|
1311
|
-
throw new Error("Fatal error");
|
|
1312
|
-
}
|
|
1313
|
-
const routers = this.#routers;
|
|
1314
|
-
const routes = this.#routes;
|
|
1315
|
-
const len = routers.length;
|
|
1316
|
-
let i = 0;
|
|
1317
|
-
let res;
|
|
1318
|
-
for (;i < len; i++) {
|
|
1319
|
-
const router = routers[i];
|
|
1320
|
-
try {
|
|
1321
|
-
for (let i2 = 0, len2 = routes.length;i2 < len2; i2++) {
|
|
1322
|
-
router.add(...routes[i2]);
|
|
1323
|
-
}
|
|
1324
|
-
res = router.match(method, path);
|
|
1325
|
-
} catch (e) {
|
|
1326
|
-
if (e instanceof UnsupportedPathError) {
|
|
1327
|
-
continue;
|
|
1328
|
-
}
|
|
1329
|
-
throw e;
|
|
1330
|
-
}
|
|
1331
|
-
this.match = router.match.bind(router);
|
|
1332
|
-
this.#routers = [router];
|
|
1333
|
-
this.#routes = undefined;
|
|
1334
|
-
break;
|
|
1335
|
-
}
|
|
1336
|
-
if (i === len) {
|
|
1337
|
-
throw new Error("Fatal error");
|
|
1338
|
-
}
|
|
1339
|
-
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
1340
|
-
return res;
|
|
1341
|
-
}
|
|
1342
|
-
get activeRouter() {
|
|
1343
|
-
if (this.#routes || this.#routers.length !== 1) {
|
|
1344
|
-
throw new Error("No active router has been determined yet.");
|
|
1345
|
-
}
|
|
1346
|
-
return this.#routers[0];
|
|
1347
|
-
}
|
|
1348
|
-
};
|
|
1349
|
-
|
|
1350
|
-
// node_modules/hono/dist/router/trie-router/node.js
|
|
1351
|
-
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
1352
|
-
var Node2 = class {
|
|
1353
|
-
#methods;
|
|
1354
|
-
#children;
|
|
1355
|
-
#patterns;
|
|
1356
|
-
#order = 0;
|
|
1357
|
-
#params = emptyParams;
|
|
1358
|
-
constructor(method, handler, children) {
|
|
1359
|
-
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
1360
|
-
this.#methods = [];
|
|
1361
|
-
if (method && handler) {
|
|
1362
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
1363
|
-
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
1364
|
-
this.#methods = [m];
|
|
1365
|
-
}
|
|
1366
|
-
this.#patterns = [];
|
|
1367
|
-
}
|
|
1368
|
-
insert(method, path, handler) {
|
|
1369
|
-
this.#order = ++this.#order;
|
|
1370
|
-
let curNode = this;
|
|
1371
|
-
const parts = splitRoutingPath(path);
|
|
1372
|
-
const possibleKeys = [];
|
|
1373
|
-
for (let i = 0, len = parts.length;i < len; i++) {
|
|
1374
|
-
const p = parts[i];
|
|
1375
|
-
const nextP = parts[i + 1];
|
|
1376
|
-
const pattern2 = getPattern2(p, nextP);
|
|
1377
|
-
const key = Array.isArray(pattern2) ? pattern2[0] : p;
|
|
1378
|
-
if (key in curNode.#children) {
|
|
1379
|
-
curNode = curNode.#children[key];
|
|
1380
|
-
if (pattern2) {
|
|
1381
|
-
possibleKeys.push(pattern2[1]);
|
|
1382
|
-
}
|
|
1383
|
-
continue;
|
|
1384
|
-
}
|
|
1385
|
-
curNode.#children[key] = new Node2;
|
|
1386
|
-
if (pattern2) {
|
|
1387
|
-
curNode.#patterns.push(pattern2);
|
|
1388
|
-
possibleKeys.push(pattern2[1]);
|
|
1389
|
-
}
|
|
1390
|
-
curNode = curNode.#children[key];
|
|
1391
|
-
}
|
|
1392
|
-
curNode.#methods.push({
|
|
1393
|
-
[method]: {
|
|
1394
|
-
handler,
|
|
1395
|
-
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
1396
|
-
score: this.#order
|
|
1397
|
-
}
|
|
1398
|
-
});
|
|
1399
|
-
return curNode;
|
|
1400
|
-
}
|
|
1401
|
-
#getHandlerSets(node, method, nodeParams, params) {
|
|
1402
|
-
const handlerSets = [];
|
|
1403
|
-
for (let i = 0, len = node.#methods.length;i < len; i++) {
|
|
1404
|
-
const m = node.#methods[i];
|
|
1405
|
-
const handlerSet = m[method] || m[METHOD_NAME_ALL];
|
|
1406
|
-
const processedSet = {};
|
|
1407
|
-
if (handlerSet !== undefined) {
|
|
1408
|
-
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
1409
|
-
handlerSets.push(handlerSet);
|
|
1410
|
-
if (nodeParams !== emptyParams || params && params !== emptyParams) {
|
|
1411
|
-
for (let i2 = 0, len2 = handlerSet.possibleKeys.length;i2 < len2; i2++) {
|
|
1412
|
-
const key = handlerSet.possibleKeys[i2];
|
|
1413
|
-
const processed = processedSet[handlerSet.score];
|
|
1414
|
-
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
1415
|
-
processedSet[handlerSet.score] = true;
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
}
|
|
1420
|
-
return handlerSets;
|
|
1421
|
-
}
|
|
1422
|
-
search(method, path) {
|
|
1423
|
-
const handlerSets = [];
|
|
1424
|
-
this.#params = emptyParams;
|
|
1425
|
-
const curNode = this;
|
|
1426
|
-
let curNodes = [curNode];
|
|
1427
|
-
const parts = splitPath(path);
|
|
1428
|
-
const curNodesQueue = [];
|
|
1429
|
-
for (let i = 0, len = parts.length;i < len; i++) {
|
|
1430
|
-
const part = parts[i];
|
|
1431
|
-
const isLast = i === len - 1;
|
|
1432
|
-
const tempNodes = [];
|
|
1433
|
-
for (let j = 0, len2 = curNodes.length;j < len2; j++) {
|
|
1434
|
-
const node = curNodes[j];
|
|
1435
|
-
const nextNode = node.#children[part];
|
|
1436
|
-
if (nextNode) {
|
|
1437
|
-
nextNode.#params = node.#params;
|
|
1438
|
-
if (isLast) {
|
|
1439
|
-
if (nextNode.#children["*"]) {
|
|
1440
|
-
handlerSets.push(...this.#getHandlerSets(nextNode.#children["*"], method, node.#params));
|
|
1441
|
-
}
|
|
1442
|
-
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
|
|
1443
|
-
} else {
|
|
1444
|
-
tempNodes.push(nextNode);
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1447
|
-
for (let k = 0, len3 = node.#patterns.length;k < len3; k++) {
|
|
1448
|
-
const pattern2 = node.#patterns[k];
|
|
1449
|
-
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
1450
|
-
if (pattern2 === "*") {
|
|
1451
|
-
const astNode = node.#children["*"];
|
|
1452
|
-
if (astNode) {
|
|
1453
|
-
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
|
|
1454
|
-
astNode.#params = params;
|
|
1455
|
-
tempNodes.push(astNode);
|
|
1456
|
-
}
|
|
1457
|
-
continue;
|
|
1458
|
-
}
|
|
1459
|
-
const [key, name, matcher] = pattern2;
|
|
1460
|
-
if (!part && !(matcher instanceof RegExp)) {
|
|
1461
|
-
continue;
|
|
1462
|
-
}
|
|
1463
|
-
const child = node.#children[key];
|
|
1464
|
-
const restPathString = parts.slice(i).join("/");
|
|
1465
|
-
if (matcher instanceof RegExp) {
|
|
1466
|
-
const m = matcher.exec(restPathString);
|
|
1467
|
-
if (m) {
|
|
1468
|
-
params[name] = m[0];
|
|
1469
|
-
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
1470
|
-
if (Object.keys(child.#children).length) {
|
|
1471
|
-
child.#params = params;
|
|
1472
|
-
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
1473
|
-
const targetCurNodes = curNodesQueue[componentCount] ||= [];
|
|
1474
|
-
targetCurNodes.push(child);
|
|
1475
|
-
}
|
|
1476
|
-
continue;
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
if (matcher === true || matcher.test(part)) {
|
|
1480
|
-
params[name] = part;
|
|
1481
|
-
if (isLast) {
|
|
1482
|
-
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
1483
|
-
if (child.#children["*"]) {
|
|
1484
|
-
handlerSets.push(...this.#getHandlerSets(child.#children["*"], method, params, node.#params));
|
|
1485
|
-
}
|
|
1486
|
-
} else {
|
|
1487
|
-
child.#params = params;
|
|
1488
|
-
tempNodes.push(child);
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1491
|
-
}
|
|
1492
|
-
}
|
|
1493
|
-
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
|
|
1494
|
-
}
|
|
1495
|
-
if (handlerSets.length > 1) {
|
|
1496
|
-
handlerSets.sort((a, b) => {
|
|
1497
|
-
return a.score - b.score;
|
|
1498
|
-
});
|
|
1499
|
-
}
|
|
1500
|
-
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
1501
|
-
}
|
|
1502
|
-
};
|
|
1503
|
-
|
|
1504
|
-
// node_modules/hono/dist/router/trie-router/router.js
|
|
1505
|
-
var TrieRouter = class {
|
|
1506
|
-
name = "TrieRouter";
|
|
1507
|
-
#node;
|
|
1508
|
-
constructor() {
|
|
1509
|
-
this.#node = new Node2;
|
|
1510
|
-
}
|
|
1511
|
-
add(method, path, handler) {
|
|
1512
|
-
const results = checkOptionalParameter(path);
|
|
1513
|
-
if (results) {
|
|
1514
|
-
for (let i = 0, len = results.length;i < len; i++) {
|
|
1515
|
-
this.#node.insert(method, results[i], handler);
|
|
1516
|
-
}
|
|
1517
|
-
return;
|
|
1518
|
-
}
|
|
1519
|
-
this.#node.insert(method, path, handler);
|
|
1520
|
-
}
|
|
1521
|
-
match(method, path) {
|
|
1522
|
-
return this.#node.search(method, path);
|
|
1523
|
-
}
|
|
1524
|
-
};
|
|
1525
|
-
|
|
1526
|
-
// node_modules/hono/dist/hono.js
|
|
1527
|
-
var Hono2 = class extends Hono {
|
|
1528
|
-
constructor(options = {}) {
|
|
1529
|
-
super(options);
|
|
1530
|
-
this.router = options.router ?? new SmartRouter({
|
|
1531
|
-
routers: [new RegExpRouter, new TrieRouter]
|
|
1532
|
-
});
|
|
1533
|
-
}
|
|
1534
|
-
};
|
|
1535
|
-
|
|
1536
|
-
// node_modules/hono/dist/adapter/bun/serve-static.js
|
|
1537
|
-
import { stat } from "node:fs/promises";
|
|
1538
|
-
import { join } from "node:path";
|
|
1539
|
-
|
|
1540
|
-
// node_modules/hono/dist/utils/compress.js
|
|
1541
|
-
var COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
1542
|
-
|
|
1543
|
-
// node_modules/hono/dist/utils/mime.js
|
|
1544
|
-
var getMimeType = (filename, mimes = baseMimes) => {
|
|
1545
|
-
const regexp = /\.([a-zA-Z0-9]+?)$/;
|
|
1546
|
-
const match = filename.match(regexp);
|
|
1547
|
-
if (!match) {
|
|
1548
|
-
return;
|
|
1549
|
-
}
|
|
1550
|
-
let mimeType = mimes[match[1]];
|
|
1551
|
-
if (mimeType && mimeType.startsWith("text")) {
|
|
1552
|
-
mimeType += "; charset=utf-8";
|
|
1553
|
-
}
|
|
1554
|
-
return mimeType;
|
|
1555
|
-
};
|
|
1556
|
-
var _baseMimes = {
|
|
1557
|
-
aac: "audio/aac",
|
|
1558
|
-
avi: "video/x-msvideo",
|
|
1559
|
-
avif: "image/avif",
|
|
1560
|
-
av1: "video/av1",
|
|
1561
|
-
bin: "application/octet-stream",
|
|
1562
|
-
bmp: "image/bmp",
|
|
1563
|
-
css: "text/css",
|
|
1564
|
-
csv: "text/csv",
|
|
1565
|
-
eot: "application/vnd.ms-fontobject",
|
|
1566
|
-
epub: "application/epub+zip",
|
|
1567
|
-
gif: "image/gif",
|
|
1568
|
-
gz: "application/gzip",
|
|
1569
|
-
htm: "text/html",
|
|
1570
|
-
html: "text/html",
|
|
1571
|
-
ico: "image/x-icon",
|
|
1572
|
-
ics: "text/calendar",
|
|
1573
|
-
jpeg: "image/jpeg",
|
|
1574
|
-
jpg: "image/jpeg",
|
|
1575
|
-
js: "text/javascript",
|
|
1576
|
-
json: "application/json",
|
|
1577
|
-
jsonld: "application/ld+json",
|
|
1578
|
-
map: "application/json",
|
|
1579
|
-
mid: "audio/x-midi",
|
|
1580
|
-
midi: "audio/x-midi",
|
|
1581
|
-
mjs: "text/javascript",
|
|
1582
|
-
mp3: "audio/mpeg",
|
|
1583
|
-
mp4: "video/mp4",
|
|
1584
|
-
mpeg: "video/mpeg",
|
|
1585
|
-
oga: "audio/ogg",
|
|
1586
|
-
ogv: "video/ogg",
|
|
1587
|
-
ogx: "application/ogg",
|
|
1588
|
-
opus: "audio/opus",
|
|
1589
|
-
otf: "font/otf",
|
|
1590
|
-
pdf: "application/pdf",
|
|
1591
|
-
png: "image/png",
|
|
1592
|
-
rtf: "application/rtf",
|
|
1593
|
-
svg: "image/svg+xml",
|
|
1594
|
-
tif: "image/tiff",
|
|
1595
|
-
tiff: "image/tiff",
|
|
1596
|
-
ts: "video/mp2t",
|
|
1597
|
-
ttf: "font/ttf",
|
|
1598
|
-
txt: "text/plain",
|
|
1599
|
-
wasm: "application/wasm",
|
|
1600
|
-
webm: "video/webm",
|
|
1601
|
-
weba: "audio/webm",
|
|
1602
|
-
webmanifest: "application/manifest+json",
|
|
1603
|
-
webp: "image/webp",
|
|
1604
|
-
woff: "font/woff",
|
|
1605
|
-
woff2: "font/woff2",
|
|
1606
|
-
xhtml: "application/xhtml+xml",
|
|
1607
|
-
xml: "application/xml",
|
|
1608
|
-
zip: "application/zip",
|
|
1609
|
-
"3gp": "video/3gpp",
|
|
1610
|
-
"3g2": "video/3gpp2",
|
|
1611
|
-
gltf: "model/gltf+json",
|
|
1612
|
-
glb: "model/gltf-binary"
|
|
1613
|
-
};
|
|
1614
|
-
var baseMimes = _baseMimes;
|
|
1615
|
-
|
|
1616
|
-
// node_modules/hono/dist/middleware/serve-static/path.js
|
|
1617
|
-
var defaultJoin = (...paths) => {
|
|
1618
|
-
let result = paths.filter((p) => p !== "").join("/");
|
|
1619
|
-
result = result.replace(/(?<=\/)\/+/g, "");
|
|
1620
|
-
const segments = result.split("/");
|
|
1621
|
-
const resolved = [];
|
|
1622
|
-
for (const segment of segments) {
|
|
1623
|
-
if (segment === ".." && resolved.length > 0 && resolved.at(-1) !== "..") {
|
|
1624
|
-
resolved.pop();
|
|
1625
|
-
} else if (segment !== ".") {
|
|
1626
|
-
resolved.push(segment);
|
|
1627
|
-
}
|
|
1628
|
-
}
|
|
1629
|
-
return resolved.join("/") || ".";
|
|
1630
|
-
};
|
|
1631
|
-
|
|
1632
|
-
// node_modules/hono/dist/middleware/serve-static/index.js
|
|
1633
|
-
var ENCODINGS = {
|
|
1634
|
-
br: ".br",
|
|
1635
|
-
zstd: ".zst",
|
|
1636
|
-
gzip: ".gz"
|
|
1637
|
-
};
|
|
1638
|
-
var ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS);
|
|
1639
|
-
var DEFAULT_DOCUMENT = "index.html";
|
|
1640
|
-
var serveStatic = (options) => {
|
|
1641
|
-
const root = options.root ?? "./";
|
|
1642
|
-
const optionPath = options.path;
|
|
1643
|
-
const join = options.join ?? defaultJoin;
|
|
1644
|
-
return async (c, next) => {
|
|
1645
|
-
if (c.finalized) {
|
|
1646
|
-
return next();
|
|
1647
|
-
}
|
|
1648
|
-
let filename;
|
|
1649
|
-
if (options.path) {
|
|
1650
|
-
filename = options.path;
|
|
1651
|
-
} else {
|
|
1652
|
-
try {
|
|
1653
|
-
filename = decodeURIComponent(c.req.path);
|
|
1654
|
-
if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {
|
|
1655
|
-
throw new Error;
|
|
1656
|
-
}
|
|
1657
|
-
} catch {
|
|
1658
|
-
await options.onNotFound?.(c.req.path, c);
|
|
1659
|
-
return next();
|
|
1660
|
-
}
|
|
1661
|
-
}
|
|
1662
|
-
let path = join(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename);
|
|
1663
|
-
if (options.isDir && await options.isDir(path)) {
|
|
1664
|
-
path = join(path, DEFAULT_DOCUMENT);
|
|
1665
|
-
}
|
|
1666
|
-
const getContent = options.getContent;
|
|
1667
|
-
let content = await getContent(path, c);
|
|
1668
|
-
if (content instanceof Response) {
|
|
1669
|
-
return c.newResponse(content.body, content);
|
|
1670
|
-
}
|
|
1671
|
-
if (content) {
|
|
1672
|
-
const mimeType = options.mimes && getMimeType(path, options.mimes) || getMimeType(path);
|
|
1673
|
-
c.header("Content-Type", mimeType || "application/octet-stream");
|
|
1674
|
-
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
|
|
1675
|
-
const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim()));
|
|
1676
|
-
for (const encoding of ENCODINGS_ORDERED_KEYS) {
|
|
1677
|
-
if (!acceptEncodingSet.has(encoding)) {
|
|
1678
|
-
continue;
|
|
1679
|
-
}
|
|
1680
|
-
const compressedContent = await getContent(path + ENCODINGS[encoding], c);
|
|
1681
|
-
if (compressedContent) {
|
|
1682
|
-
content = compressedContent;
|
|
1683
|
-
c.header("Content-Encoding", encoding);
|
|
1684
|
-
c.header("Vary", "Accept-Encoding", { append: true });
|
|
1685
|
-
break;
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
}
|
|
1689
|
-
await options.onFound?.(path, c);
|
|
1690
|
-
return c.body(content);
|
|
1691
|
-
}
|
|
1692
|
-
await options.onNotFound?.(path, c);
|
|
1693
|
-
await next();
|
|
1694
|
-
return;
|
|
1695
|
-
};
|
|
1696
|
-
};
|
|
1697
|
-
|
|
1698
|
-
// node_modules/hono/dist/adapter/bun/serve-static.js
|
|
1699
|
-
var serveStatic2 = (options) => {
|
|
1700
|
-
return async function serveStatic2(c, next) {
|
|
1701
|
-
const getContent = async (path) => {
|
|
1702
|
-
const file = Bun.file(path);
|
|
1703
|
-
return await file.exists() ? file : null;
|
|
1704
|
-
};
|
|
1705
|
-
const isDir = async (path) => {
|
|
1706
|
-
let isDir2;
|
|
1707
|
-
try {
|
|
1708
|
-
const stats = await stat(path);
|
|
1709
|
-
isDir2 = stats.isDirectory();
|
|
1710
|
-
} catch {}
|
|
1711
|
-
return isDir2;
|
|
1712
|
-
};
|
|
1713
|
-
return serveStatic({
|
|
1714
|
-
...options,
|
|
1715
|
-
getContent,
|
|
1716
|
-
join,
|
|
1717
|
-
isDir
|
|
1718
|
-
})(c, next);
|
|
1719
|
-
};
|
|
1720
|
-
};
|
|
1721
|
-
|
|
1722
|
-
// node_modules/hono/dist/helper/ssg/middleware.js
|
|
1723
|
-
var X_HONO_DISABLE_SSG_HEADER_KEY = "x-hono-disable-ssg";
|
|
1724
|
-
var SSG_DISABLED_RESPONSE = (() => {
|
|
1725
|
-
try {
|
|
1726
|
-
return new Response("SSG is disabled", {
|
|
1727
|
-
status: 404,
|
|
1728
|
-
headers: { [X_HONO_DISABLE_SSG_HEADER_KEY]: "true" }
|
|
1729
|
-
});
|
|
1730
|
-
} catch {
|
|
1731
|
-
return null;
|
|
1732
|
-
}
|
|
1733
|
-
})();
|
|
1734
|
-
// node_modules/hono/dist/adapter/bun/ssg.js
|
|
1735
|
-
var { write } = Bun;
|
|
1736
|
-
|
|
1737
|
-
// node_modules/hono/dist/helper/websocket/index.js
|
|
1738
|
-
var WSContext = class {
|
|
1739
|
-
#init;
|
|
1740
|
-
constructor(init) {
|
|
1741
|
-
this.#init = init;
|
|
1742
|
-
this.raw = init.raw;
|
|
1743
|
-
this.url = init.url ? new URL(init.url) : null;
|
|
1744
|
-
this.protocol = init.protocol ?? null;
|
|
1745
|
-
}
|
|
1746
|
-
send(source, options) {
|
|
1747
|
-
this.#init.send(source, options ?? {});
|
|
1748
|
-
}
|
|
1749
|
-
raw;
|
|
1750
|
-
binaryType = "arraybuffer";
|
|
1751
|
-
get readyState() {
|
|
1752
|
-
return this.#init.readyState;
|
|
1753
|
-
}
|
|
1754
|
-
url;
|
|
1755
|
-
protocol;
|
|
1756
|
-
close(code, reason) {
|
|
1757
|
-
this.#init.close(code, reason);
|
|
1758
|
-
}
|
|
1759
|
-
};
|
|
1760
|
-
var defineWebSocketHelper = (handler) => {
|
|
1761
|
-
return (...args) => {
|
|
1762
|
-
if (typeof args[0] === "function") {
|
|
1763
|
-
const [createEvents, options] = args;
|
|
1764
|
-
return async function upgradeWebSocket(c, next) {
|
|
1765
|
-
const events = await createEvents(c);
|
|
1766
|
-
const result = await handler(c, events, options);
|
|
1767
|
-
if (result) {
|
|
1768
|
-
return result;
|
|
1769
|
-
}
|
|
1770
|
-
await next();
|
|
1771
|
-
};
|
|
1772
|
-
} else {
|
|
1773
|
-
const [c, events, options] = args;
|
|
1774
|
-
return (async () => {
|
|
1775
|
-
const upgraded = await handler(c, events, options);
|
|
1776
|
-
if (!upgraded) {
|
|
1777
|
-
throw new Error("Failed to upgrade WebSocket");
|
|
1778
|
-
}
|
|
1779
|
-
return upgraded;
|
|
1780
|
-
})();
|
|
1781
|
-
}
|
|
1782
|
-
};
|
|
1783
|
-
};
|
|
1784
|
-
|
|
1785
|
-
// node_modules/hono/dist/adapter/bun/server.js
|
|
1786
|
-
var getBunServer = (c) => ("server" in c.env) ? c.env.server : c.env;
|
|
1787
|
-
|
|
1788
|
-
// node_modules/hono/dist/adapter/bun/websocket.js
|
|
1789
|
-
var upgradeWebSocket = defineWebSocketHelper((c, events) => {
|
|
1790
|
-
const server = getBunServer(c);
|
|
1791
|
-
if (!server) {
|
|
1792
|
-
throw new TypeError("env has to include the 2nd argument of fetch.");
|
|
1793
|
-
}
|
|
1794
|
-
const upgradeResult = server.upgrade(c.req.raw, {
|
|
1795
|
-
data: {
|
|
1796
|
-
events,
|
|
1797
|
-
url: new URL(c.req.url),
|
|
1798
|
-
protocol: c.req.url
|
|
1799
|
-
}
|
|
1800
|
-
});
|
|
1801
|
-
if (upgradeResult) {
|
|
1802
|
-
return new Response(null);
|
|
1803
|
-
}
|
|
1804
|
-
return;
|
|
1805
|
-
});
|
|
1806
|
-
|
|
1807
|
-
// node_modules/hono/dist/http-exception.js
|
|
1808
|
-
var HTTPException = class extends Error {
|
|
1809
|
-
res;
|
|
1810
|
-
status;
|
|
1811
|
-
constructor(status = 500, options) {
|
|
1812
|
-
super(options?.message, { cause: options?.cause });
|
|
1813
|
-
this.res = options?.res;
|
|
1814
|
-
this.status = status;
|
|
1815
|
-
}
|
|
1816
|
-
getResponse() {
|
|
1817
|
-
if (this.res) {
|
|
1818
|
-
const newResponse = new Response(this.res.body, {
|
|
1819
|
-
status: this.status,
|
|
1820
|
-
headers: this.res.headers
|
|
1821
|
-
});
|
|
1822
|
-
return newResponse;
|
|
1823
|
-
}
|
|
1824
|
-
return new Response(this.message, {
|
|
1825
|
-
status: this.status
|
|
1826
|
-
});
|
|
1827
|
-
}
|
|
1828
|
-
};
|
|
1829
|
-
|
|
1830
|
-
// node_modules/hono/dist/middleware/csrf/index.js
|
|
1831
|
-
var secFetchSiteValues = ["same-origin", "same-site", "none", "cross-site"];
|
|
1832
|
-
var isSecFetchSite = (value) => secFetchSiteValues.includes(value);
|
|
1833
|
-
var isSafeMethodRe = /^(GET|HEAD)$/;
|
|
1834
|
-
var isRequestedByFormElementRe = /^\b(application\/x-www-form-urlencoded|multipart\/form-data|text\/plain)\b/i;
|
|
1835
|
-
var csrf = (options) => {
|
|
1836
|
-
const originHandler = ((optsOrigin) => {
|
|
1837
|
-
if (!optsOrigin) {
|
|
1838
|
-
return (origin, c) => origin === new URL(c.req.url).origin;
|
|
1839
|
-
} else if (typeof optsOrigin === "string") {
|
|
1840
|
-
return (origin) => origin === optsOrigin;
|
|
1841
|
-
} else if (typeof optsOrigin === "function") {
|
|
1842
|
-
return optsOrigin;
|
|
1843
|
-
} else {
|
|
1844
|
-
return (origin) => optsOrigin.includes(origin);
|
|
1845
|
-
}
|
|
1846
|
-
})(options?.origin);
|
|
1847
|
-
const isAllowedOrigin = (origin, c) => {
|
|
1848
|
-
if (origin === undefined) {
|
|
1849
|
-
return false;
|
|
1850
|
-
}
|
|
1851
|
-
return originHandler(origin, c);
|
|
1852
|
-
};
|
|
1853
|
-
const secFetchSiteHandler = ((optsSecFetchSite) => {
|
|
1854
|
-
if (!optsSecFetchSite) {
|
|
1855
|
-
return (secFetchSite) => secFetchSite === "same-origin";
|
|
1856
|
-
} else if (typeof optsSecFetchSite === "string") {
|
|
1857
|
-
return (secFetchSite) => secFetchSite === optsSecFetchSite;
|
|
1858
|
-
} else if (typeof optsSecFetchSite === "function") {
|
|
1859
|
-
return optsSecFetchSite;
|
|
1860
|
-
} else {
|
|
1861
|
-
return (secFetchSite) => optsSecFetchSite.includes(secFetchSite);
|
|
1862
|
-
}
|
|
1863
|
-
})(options?.secFetchSite);
|
|
1864
|
-
const isAllowedSecFetchSite = (secFetchSite, c) => {
|
|
1865
|
-
if (secFetchSite === undefined) {
|
|
1866
|
-
return false;
|
|
1867
|
-
}
|
|
1868
|
-
if (!isSecFetchSite(secFetchSite)) {
|
|
1869
|
-
return false;
|
|
1870
|
-
}
|
|
1871
|
-
return secFetchSiteHandler(secFetchSite, c);
|
|
1872
|
-
};
|
|
1873
|
-
return async function csrf2(c, next) {
|
|
1874
|
-
if (!isSafeMethodRe.test(c.req.method) && isRequestedByFormElementRe.test(c.req.header("content-type") || "text/plain") && !isAllowedSecFetchSite(c.req.header("sec-fetch-site"), c) && !isAllowedOrigin(c.req.header("origin"), c)) {
|
|
1875
|
-
const res = new Response("Forbidden", { status: 403 });
|
|
1876
|
-
throw new HTTPException(403, { res });
|
|
1877
|
-
}
|
|
1878
|
-
await next();
|
|
1879
|
-
};
|
|
1880
|
-
};
|
|
1881
|
-
|
|
1882
|
-
// src/server.ts
|
|
1883
|
-
var IS_PROD = false;
|
|
1884
|
-
var CWD = process.cwd();
|
|
1885
|
-
function createConfig(config) {
|
|
1886
|
-
return config;
|
|
1887
|
-
}
|
|
1888
|
-
function createRoute(handler) {
|
|
1889
|
-
let _handlers = {};
|
|
1890
|
-
let _middleware = [];
|
|
1891
|
-
if (handler) {
|
|
1892
|
-
_handlers["GET"] = handler;
|
|
1893
|
-
}
|
|
1894
|
-
const api = {
|
|
1895
|
-
_kind: "hsRoute",
|
|
1896
|
-
get(handler2) {
|
|
1897
|
-
_handlers["GET"] = handler2;
|
|
1898
|
-
return api;
|
|
1899
|
-
},
|
|
1900
|
-
post(handler2) {
|
|
1901
|
-
_handlers["POST"] = handler2;
|
|
1902
|
-
return api;
|
|
1903
|
-
},
|
|
1904
|
-
middleware(middleware) {
|
|
1905
|
-
_middleware = middleware;
|
|
1906
|
-
return api;
|
|
1907
|
-
},
|
|
1908
|
-
_getRouteHandlers() {
|
|
1909
|
-
return [
|
|
1910
|
-
..._middleware,
|
|
1911
|
-
async (context) => {
|
|
1912
|
-
const method = context.req.method.toUpperCase();
|
|
1913
|
-
if (method === "OPTIONS") {
|
|
1914
|
-
return context.html(render(html`
|
|
1915
|
-
<!DOCTYPE html>
|
|
1916
|
-
<html lang="en"></html>
|
|
1917
|
-
`), {
|
|
1918
|
-
status: 200,
|
|
1919
|
-
headers: {
|
|
1920
|
-
"Access-Control-Allow-Origin": "*",
|
|
1921
|
-
"Access-Control-Allow-Methods": [
|
|
1922
|
-
"HEAD",
|
|
1923
|
-
"OPTIONS",
|
|
1924
|
-
...Object.keys(_handlers)
|
|
1925
|
-
].join(", "),
|
|
1926
|
-
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
1927
|
-
}
|
|
1928
|
-
});
|
|
1929
|
-
}
|
|
1930
|
-
return returnHTMLResponse(context, () => {
|
|
1931
|
-
const handler2 = method === "HEAD" ? _handlers["GET"] : _handlers[method];
|
|
1932
|
-
if (!handler2) {
|
|
1933
|
-
throw new HTTPException(405, { message: "Method not allowed" });
|
|
1934
|
-
}
|
|
1935
|
-
return handler2(context);
|
|
1936
|
-
});
|
|
1937
|
-
}
|
|
1938
|
-
];
|
|
1939
|
-
}
|
|
1940
|
-
};
|
|
1941
|
-
return api;
|
|
1942
|
-
}
|
|
1943
|
-
function createAPIRoute(handler) {
|
|
1944
|
-
let _handlers = {};
|
|
1945
|
-
let _middleware = [];
|
|
1946
|
-
if (handler) {
|
|
1947
|
-
_handlers["GET"] = handler;
|
|
1948
|
-
}
|
|
1949
|
-
const api = {
|
|
1950
|
-
_kind: "hsAPIRoute",
|
|
1951
|
-
get(handler2) {
|
|
1952
|
-
_handlers["GET"] = handler2;
|
|
1953
|
-
return api;
|
|
1954
|
-
},
|
|
1955
|
-
post(handler2) {
|
|
1956
|
-
_handlers["POST"] = handler2;
|
|
1957
|
-
return api;
|
|
1958
|
-
},
|
|
1959
|
-
put(handler2) {
|
|
1960
|
-
_handlers["PUT"] = handler2;
|
|
1961
|
-
return api;
|
|
1962
|
-
},
|
|
1963
|
-
delete(handler2) {
|
|
1964
|
-
_handlers["DELETE"] = handler2;
|
|
1965
|
-
return api;
|
|
1966
|
-
},
|
|
1967
|
-
patch(handler2) {
|
|
1968
|
-
_handlers["PATCH"] = handler2;
|
|
1969
|
-
return api;
|
|
1970
|
-
},
|
|
1971
|
-
middleware(middleware) {
|
|
1972
|
-
_middleware = middleware;
|
|
1973
|
-
return api;
|
|
1974
|
-
},
|
|
1975
|
-
_getRouteHandlers() {
|
|
1976
|
-
return [
|
|
1977
|
-
..._middleware,
|
|
1978
|
-
async (context) => {
|
|
1979
|
-
const method = context.req.method.toUpperCase();
|
|
1980
|
-
if (method === "OPTIONS") {
|
|
1981
|
-
return context.json({
|
|
1982
|
-
meta: { success: true, dtResponse: new Date },
|
|
1983
|
-
data: {}
|
|
1984
|
-
}, {
|
|
1985
|
-
status: 200,
|
|
1986
|
-
headers: {
|
|
1987
|
-
"Access-Control-Allow-Origin": "*",
|
|
1988
|
-
"Access-Control-Allow-Methods": [
|
|
1989
|
-
"HEAD",
|
|
1990
|
-
"OPTIONS",
|
|
1991
|
-
...Object.keys(_handlers)
|
|
1992
|
-
].join(", "),
|
|
1993
|
-
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
1994
|
-
}
|
|
1995
|
-
});
|
|
1996
|
-
}
|
|
1997
|
-
const handler2 = method === "HEAD" ? _handlers["GET"] : _handlers[method];
|
|
1998
|
-
if (!handler2) {
|
|
1999
|
-
return context.json({
|
|
2000
|
-
meta: { success: false, dtResponse: new Date },
|
|
2001
|
-
data: {},
|
|
2002
|
-
error: {
|
|
2003
|
-
message: "Method not allowed"
|
|
2004
|
-
}
|
|
2005
|
-
}, { status: 405 });
|
|
2006
|
-
}
|
|
2007
|
-
try {
|
|
2008
|
-
const response = await handler2(context);
|
|
2009
|
-
if (response instanceof Response) {
|
|
2010
|
-
return response;
|
|
2011
|
-
}
|
|
2012
|
-
return context.json({ meta: { success: true, dtResponse: new Date }, data: response }, { status: 200 });
|
|
2013
|
-
} catch (err) {
|
|
2014
|
-
const e = err;
|
|
2015
|
-
!IS_PROD && console.error(e);
|
|
2016
|
-
return context.json({
|
|
2017
|
-
meta: { success: false, dtResponse: new Date },
|
|
2018
|
-
data: {},
|
|
2019
|
-
error: {
|
|
2020
|
-
message: e.message,
|
|
2021
|
-
stack: IS_PROD ? undefined : e.stack?.split(`
|
|
2022
|
-
`)
|
|
2023
|
-
}
|
|
2024
|
-
}, { status: 500 });
|
|
2025
|
-
}
|
|
2026
|
-
}
|
|
2027
|
-
];
|
|
2028
|
-
}
|
|
2029
|
-
};
|
|
2030
|
-
return api;
|
|
2031
|
-
}
|
|
2032
|
-
async function returnHTMLResponse(context, handlerFn, responseOptions) {
|
|
2033
|
-
try {
|
|
2034
|
-
const routeContent = await handlerFn();
|
|
2035
|
-
if (routeContent instanceof Response) {
|
|
2036
|
-
return routeContent;
|
|
2037
|
-
}
|
|
2038
|
-
const userIsBot = isbot(context.req.header("User-Agent"));
|
|
2039
|
-
const streamOpt = context.req.query("__nostream");
|
|
2040
|
-
const streamingEnabled = !userIsBot && (streamOpt !== undefined ? streamOpt : true);
|
|
2041
|
-
if (isHSHtml(routeContent)) {
|
|
2042
|
-
if (streamingEnabled && routeContent.asyncContent?.length > 0) {
|
|
2043
|
-
return new StreamResponse(renderStream(routeContent), responseOptions);
|
|
2044
|
-
} else {
|
|
2045
|
-
const output = await renderAsync(routeContent);
|
|
2046
|
-
return context.html(output, responseOptions);
|
|
2047
|
-
}
|
|
2048
|
-
}
|
|
2049
|
-
return context.html(String(routeContent), responseOptions);
|
|
2050
|
-
} catch (e) {
|
|
2051
|
-
!IS_PROD && console.error(e);
|
|
2052
|
-
return await showErrorReponse(context, e, responseOptions);
|
|
2053
|
-
}
|
|
2054
|
-
}
|
|
2055
|
-
function getRunnableRoute(route) {
|
|
2056
|
-
if (isRunnableRoute(route)) {
|
|
2057
|
-
return route;
|
|
2058
|
-
}
|
|
2059
|
-
const kind = typeof route;
|
|
2060
|
-
if (kind === "function") {
|
|
2061
|
-
return createRoute(route);
|
|
2062
|
-
}
|
|
2063
|
-
if (kind === "object" && "default" in route) {
|
|
2064
|
-
return getRunnableRoute(route.default);
|
|
2065
|
-
}
|
|
2066
|
-
throw new Error(`Route not runnable. Use "export default createRoute()" to create a Hyperspan route. Exported methods found were: ${Object.keys(route).join(", ")}`);
|
|
2067
|
-
}
|
|
2068
|
-
function isRunnableRoute(route) {
|
|
2069
|
-
if (typeof route !== "object") {
|
|
2070
|
-
return false;
|
|
2071
|
-
}
|
|
2072
|
-
const obj = route;
|
|
2073
|
-
const runnableKind = ["hsRoute", "hsAPIRoute", "hsAction"].includes(obj?._kind);
|
|
2074
|
-
return runnableKind && "_getRouteHandlers" in obj;
|
|
2075
|
-
}
|
|
2076
|
-
async function showErrorReponse(context, err, responseOptions) {
|
|
2077
|
-
let status = 500;
|
|
2078
|
-
const message = err.message || "Internal Server Error";
|
|
2079
|
-
if (err instanceof HTTPException) {
|
|
2080
|
-
status = err.status;
|
|
2081
|
-
}
|
|
2082
|
-
const stack = !IS_PROD && err.stack ? err.stack.split(`
|
|
2083
|
-
`).slice(1).join(`
|
|
2084
|
-
`) : "";
|
|
2085
|
-
if (context.req.header("X-Request-Type") === "partial") {
|
|
2086
|
-
const output2 = render(html`
|
|
2087
|
-
<section style="padding: 20px;">
|
|
2088
|
-
<p style="margin-bottom: 10px;"><strong>Error</strong></p>
|
|
2089
|
-
<strong>${message}</strong>
|
|
2090
|
-
${stack ? html`<pre>${stack}</pre>` : ""}
|
|
2091
|
-
</section>
|
|
2092
|
-
`);
|
|
2093
|
-
return context.html(output2, Object.assign({ status }, responseOptions));
|
|
2094
|
-
}
|
|
2095
|
-
const output = render(html`
|
|
2096
|
-
<!DOCTYPE html>
|
|
2097
|
-
<html lang="en">
|
|
2098
|
-
<head>
|
|
2099
|
-
<meta charset="UTF-8" />
|
|
2100
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
2101
|
-
<title>Application Error</title>
|
|
2102
|
-
</head>
|
|
2103
|
-
<body>
|
|
2104
|
-
<main>
|
|
2105
|
-
<h1>Application Error</h1>
|
|
2106
|
-
<strong>${message}</strong>
|
|
2107
|
-
${stack ? html`<pre>${stack}</pre>` : ""}
|
|
2108
|
-
</main>
|
|
2109
|
-
</body>
|
|
2110
|
-
</html>
|
|
2111
|
-
`);
|
|
2112
|
-
return context.html(output, Object.assign({ status }, responseOptions));
|
|
2113
|
-
}
|
|
2114
|
-
var ROUTE_SEGMENT = /(\[[a-zA-Z_\.]+\])/g;
|
|
2115
|
-
async function buildRoutes(config) {
|
|
2116
|
-
const routesDir = join2(config.appDir, "routes");
|
|
2117
|
-
const files = await readdir(routesDir, { recursive: true });
|
|
2118
|
-
const routes = [];
|
|
2119
|
-
for (const file of files) {
|
|
2120
|
-
if (!file.includes(".") || basename(file).startsWith(".") || file.includes(".test.") || file.includes(".spec.")) {
|
|
2121
|
-
continue;
|
|
2122
|
-
}
|
|
2123
|
-
let route = "/" + file.replace(extname(file), "");
|
|
2124
|
-
if (route.endsWith("index")) {
|
|
2125
|
-
route = route === "index" ? "/" : route.substring(0, route.length - 6);
|
|
2126
|
-
}
|
|
2127
|
-
let params = [];
|
|
2128
|
-
const dynamicPaths = ROUTE_SEGMENT.test(route);
|
|
2129
|
-
if (dynamicPaths) {
|
|
2130
|
-
params = [];
|
|
2131
|
-
route = route.replace(ROUTE_SEGMENT, (match) => {
|
|
2132
|
-
const paramName = match.replace(/[^a-zA-Z_\.]+/g, "");
|
|
2133
|
-
if (match.includes("...")) {
|
|
2134
|
-
params.push(paramName.replace("...", ""));
|
|
2135
|
-
return "*";
|
|
2136
|
-
} else {
|
|
2137
|
-
params.push(paramName);
|
|
2138
|
-
return ":" + paramName;
|
|
2139
|
-
}
|
|
2140
|
-
});
|
|
2141
|
-
}
|
|
2142
|
-
routes.push({
|
|
2143
|
-
file: join2("./", routesDir, file),
|
|
2144
|
-
route: normalizePath(route || "/"),
|
|
2145
|
-
params
|
|
2146
|
-
});
|
|
2147
|
-
}
|
|
2148
|
-
return await Promise.all(routes.map(async (route) => {
|
|
2149
|
-
route.module = (await import(join2(CWD, route.file))).default;
|
|
2150
|
-
return route;
|
|
2151
|
-
}));
|
|
2152
|
-
}
|
|
2153
|
-
async function buildActions(config) {
|
|
2154
|
-
const routesDir = join2(config.appDir, "actions");
|
|
2155
|
-
const files = await readdir(routesDir, { recursive: true });
|
|
2156
|
-
const routes = [];
|
|
2157
|
-
for (const file of files) {
|
|
2158
|
-
if (!file.includes(".") || basename(file).startsWith(".") || file.includes(".test.") || file.includes(".spec.")) {
|
|
2159
|
-
continue;
|
|
2160
|
-
}
|
|
2161
|
-
let route = assetHash("/" + file.replace(extname(file), ""));
|
|
2162
|
-
routes.push({
|
|
2163
|
-
file: join2("./", routesDir, file),
|
|
2164
|
-
route: `/__actions/${route}`,
|
|
2165
|
-
params: []
|
|
2166
|
-
});
|
|
2167
|
-
}
|
|
2168
|
-
return await Promise.all(routes.map(async (route) => {
|
|
2169
|
-
route.module = (await import(join2(CWD, route.file))).default;
|
|
2170
|
-
route.route = route.module._route;
|
|
2171
|
-
return route;
|
|
2172
|
-
}));
|
|
2173
|
-
}
|
|
2174
|
-
function createRouteFromModule(RouteModule) {
|
|
2175
|
-
const route = getRunnableRoute(RouteModule);
|
|
2176
|
-
return route._getRouteHandlers();
|
|
2177
|
-
}
|
|
2178
|
-
async function createServer(config) {
|
|
2179
|
-
await Promise.all([buildClientJS(), buildClientCSS(), clientJSPlugin(config)]);
|
|
2180
|
-
const app = new Hono2;
|
|
2181
|
-
app.use(csrf());
|
|
2182
|
-
config.beforeRoutesAdded && config.beforeRoutesAdded(app);
|
|
2183
|
-
const [routes, actions] = await Promise.all([buildRoutes(config), buildActions(config)]);
|
|
2184
|
-
const fileRoutes = routes.concat(actions);
|
|
2185
|
-
const routeMap = [];
|
|
2186
|
-
for (let i = 0;i < fileRoutes.length; i++) {
|
|
2187
|
-
let route = fileRoutes[i];
|
|
2188
|
-
routeMap.push({ route: route.route, file: route.file });
|
|
2189
|
-
if (!route.module) {
|
|
2190
|
-
throw new Error(`Route module not loaded! File: ${route.file}`);
|
|
2191
|
-
}
|
|
2192
|
-
const routeHandlers = createRouteFromModule(route.module);
|
|
2193
|
-
app.all(route.route, ...routeHandlers);
|
|
2194
|
-
}
|
|
2195
|
-
if (routeMap.length === 0) {
|
|
2196
|
-
app.get("/", (context) => {
|
|
2197
|
-
return context.text("No routes found. Add routes to app/routes. Example: `app/routes/index.ts`", { status: 404 });
|
|
2198
|
-
});
|
|
2199
|
-
}
|
|
2200
|
-
if (!IS_PROD) {
|
|
2201
|
-
console.log("[Hyperspan] File system routes (in app/routes):");
|
|
2202
|
-
console.table(routeMap);
|
|
2203
|
-
}
|
|
2204
|
-
config.afterRoutesAdded && config.afterRoutesAdded(app);
|
|
2205
|
-
app.use("*", serveStatic2({
|
|
2206
|
-
root: config.staticFileRoot,
|
|
2207
|
-
onFound: IS_PROD ? (_, c) => {
|
|
2208
|
-
c.header("Cache-Control", "public, max-age=2592000");
|
|
2209
|
-
} : undefined
|
|
2210
|
-
}));
|
|
2211
|
-
app.notFound((context) => {
|
|
2212
|
-
return context.text("Not... found?", { status: 404 });
|
|
2213
|
-
});
|
|
2214
|
-
return app;
|
|
2215
|
-
}
|
|
2216
|
-
|
|
2217
|
-
class StreamResponse extends Response {
|
|
2218
|
-
constructor(iterator, options = {}) {
|
|
2219
|
-
super();
|
|
2220
|
-
const stream = createReadableStreamFromAsyncGenerator(iterator);
|
|
2221
|
-
return new Response(stream, {
|
|
2222
|
-
status: 200,
|
|
2223
|
-
headers: {
|
|
2224
|
-
"Transfer-Encoding": "chunked",
|
|
2225
|
-
"Content-Type": "text/html; charset=UTF-8",
|
|
2226
|
-
"Content-Encoding": "Identity",
|
|
2227
|
-
...options?.headers ?? {}
|
|
2228
|
-
},
|
|
2229
|
-
...options
|
|
2230
|
-
});
|
|
2231
|
-
}
|
|
2232
|
-
}
|
|
2233
|
-
function createReadableStreamFromAsyncGenerator(output) {
|
|
2234
|
-
const encoder = new TextEncoder;
|
|
2235
|
-
return new ReadableStream({
|
|
2236
|
-
async start(controller) {
|
|
2237
|
-
while (true) {
|
|
2238
|
-
const { done, value } = await output.next();
|
|
2239
|
-
if (done) {
|
|
2240
|
-
controller.close();
|
|
2241
|
-
break;
|
|
2242
|
-
}
|
|
2243
|
-
controller.enqueue(encoder.encode(value));
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2246
|
-
});
|
|
2247
|
-
}
|
|
2248
|
-
function normalizePath(urlPath) {
|
|
2249
|
-
return (urlPath.endsWith("/") ? urlPath.substring(0, urlPath.length - 1) : urlPath).toLowerCase() || "/";
|
|
2250
|
-
}
|
|
2251
|
-
export {
|
|
2252
|
-
returnHTMLResponse,
|
|
2253
|
-
normalizePath,
|
|
2254
|
-
isRunnableRoute,
|
|
2255
|
-
getRunnableRoute,
|
|
2256
|
-
createServer,
|
|
2257
|
-
createRouteFromModule,
|
|
2258
|
-
createRoute,
|
|
2259
|
-
createReadableStreamFromAsyncGenerator,
|
|
2260
|
-
createConfig,
|
|
2261
|
-
createAPIRoute,
|
|
2262
|
-
buildRoutes,
|
|
2263
|
-
buildActions,
|
|
2264
|
-
StreamResponse,
|
|
2265
|
-
IS_PROD
|
|
2266
|
-
};
|