claude-alfred 0.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/README.ja.md +283 -0
- package/README.md +283 -0
- package/content/hooks/hooks.json +41 -0
- package/content/mcp/.mcp.json +12 -0
- package/dist/audit-DujZ6YAy.mjs +18 -0
- package/dist/cli.mjs +509 -0
- package/dist/dispatcher-BzOdcjaa.mjs +93 -0
- package/dist/embedder-BshPIMrW.mjs +215 -0
- package/dist/epic-CdRKNGvP.mjs +227 -0
- package/dist/fts-BDdUbNfM.mjs +195 -0
- package/dist/helpers-BsdW4kgn.mjs +94 -0
- package/dist/knowledge-CCCixwb8.mjs +156 -0
- package/dist/post-tool-qemgso2b.mjs +88 -0
- package/dist/postinstall.mjs +49 -0
- package/dist/pre-compact-Cmg9kprV.mjs +181 -0
- package/dist/project-CpgK3fwQ.mjs +79 -0
- package/dist/schema-CcIFwr_0.mjs +289 -0
- package/dist/server-DF7CXxKi.mjs +2635 -0
- package/dist/server-Dsf47Pd4.mjs +19220 -0
- package/dist/session-start-DUYF6E0V.mjs +209 -0
- package/dist/store-Clcihees.mjs +338 -0
- package/dist/types-C3butmI8.mjs +6823 -0
- package/dist/user-prompt-BDeST0mR.mjs +144 -0
- package/dist/vectors-DvuAqDeO.mjs +83 -0
- package/package.json +46 -0
- package/web/dist/assets/activity-UyW12k7Z.js +1 -0
- package/web/dist/assets/api-BI8AW-mC.js +1 -0
- package/web/dist/assets/dist-BHj_gZG8.js +1 -0
- package/web/dist/assets/dist-DDZSXOC-.js +1 -0
- package/web/dist/assets/index-B9C85vN2.js +10 -0
- package/web/dist/assets/index-bIyYMf1a.css +1 -0
- package/web/dist/assets/knowledge-DmvXTX67.js +5 -0
- package/web/dist/assets/link-BSgD_zxQ.js +1 -0
- package/web/dist/assets/matchContext-CO01nzZ3.js +1 -0
- package/web/dist/assets/progress-DBmt_Ww6.js +6 -0
- package/web/dist/assets/routes-zEN1XNFl.js +1 -0
- package/web/dist/assets/scroll-area-DPCDB42s.js +45 -0
- package/web/dist/assets/separator-5sy8HYz5.js +1 -0
- package/web/dist/assets/skeleton-D7GRd6oJ.js +1 -0
- package/web/dist/assets/tabs-VSkG1f0-.js +1 -0
- package/web/dist/assets/tasks-CKNc1U7M.js +1 -0
- package/web/dist/assets/tasks._slug-DPzi78wf.js +8 -0
- package/web/dist/assets/utils-Dw49HYRP.js +1 -0
- package/web/dist/index.html +17 -0
|
@@ -0,0 +1,2635 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { a as filesForSize, n as VALID_SLUG, s as readActiveState, t as SpecDir } from "./types-C3butmI8.mjs";
|
|
3
|
+
import { r as listAllEpics } from "./epic-CdRKNGvP.mjs";
|
|
4
|
+
import { d as setKnowledgeEnabled, i as getKnowledgeStats, s as listAllKnowledge } from "./knowledge-CCCixwb8.mjs";
|
|
5
|
+
import { n as searchKnowledgeFTS } from "./fts-BDdUbNfM.mjs";
|
|
6
|
+
import { t as detectProject } from "./project-CpgK3fwQ.mjs";
|
|
7
|
+
import { createServer } from "http";
|
|
8
|
+
import { Http2ServerRequest } from "http2";
|
|
9
|
+
import { Readable } from "stream";
|
|
10
|
+
import crypto from "crypto";
|
|
11
|
+
import { createReadStream, existsSync, statSync } from "fs";
|
|
12
|
+
import { join } from "path";
|
|
13
|
+
import { versions } from "process";
|
|
14
|
+
import { existsSync as existsSync$1, readFileSync, readdirSync, statSync as statSync$1 } from "node:fs";
|
|
15
|
+
import { join as join$1 } from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
//#region node_modules/hono/dist/compose.js
|
|
18
|
+
var compose = (middleware, onError, onNotFound) => {
|
|
19
|
+
return (context, next) => {
|
|
20
|
+
let index = -1;
|
|
21
|
+
return dispatch(0);
|
|
22
|
+
async function dispatch(i) {
|
|
23
|
+
if (i <= index) throw new Error("next() called multiple times");
|
|
24
|
+
index = i;
|
|
25
|
+
let res;
|
|
26
|
+
let isError = false;
|
|
27
|
+
let handler;
|
|
28
|
+
if (middleware[i]) {
|
|
29
|
+
handler = middleware[i][0][0];
|
|
30
|
+
context.req.routeIndex = i;
|
|
31
|
+
} else handler = i === middleware.length && next || void 0;
|
|
32
|
+
if (handler) try {
|
|
33
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if (err instanceof Error && onError) {
|
|
36
|
+
context.error = err;
|
|
37
|
+
res = await onError(err, context);
|
|
38
|
+
isError = true;
|
|
39
|
+
} else throw err;
|
|
40
|
+
}
|
|
41
|
+
else if (context.finalized === false && onNotFound) res = await onNotFound(context);
|
|
42
|
+
if (res && (context.finalized === false || isError)) context.res = res;
|
|
43
|
+
return context;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region node_modules/hono/dist/request/constants.js
|
|
49
|
+
var GET_MATCH_RESULT = /* @__PURE__ */ Symbol();
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region node_modules/hono/dist/utils/body.js
|
|
52
|
+
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
53
|
+
const { all = false, dot = false } = options;
|
|
54
|
+
const contentType = (request instanceof HonoRequest ? request.raw.headers : request.headers).get("Content-Type");
|
|
55
|
+
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) return parseFormData(request, {
|
|
56
|
+
all,
|
|
57
|
+
dot
|
|
58
|
+
});
|
|
59
|
+
return {};
|
|
60
|
+
};
|
|
61
|
+
async function parseFormData(request, options) {
|
|
62
|
+
const formData = await request.formData();
|
|
63
|
+
if (formData) return convertFormDataToBodyData(formData, options);
|
|
64
|
+
return {};
|
|
65
|
+
}
|
|
66
|
+
function convertFormDataToBodyData(formData, options) {
|
|
67
|
+
const form = /* @__PURE__ */ Object.create(null);
|
|
68
|
+
formData.forEach((value, key) => {
|
|
69
|
+
if (!(options.all || key.endsWith("[]"))) form[key] = value;
|
|
70
|
+
else handleParsingAllValues(form, key, value);
|
|
71
|
+
});
|
|
72
|
+
if (options.dot) Object.entries(form).forEach(([key, value]) => {
|
|
73
|
+
if (key.includes(".")) {
|
|
74
|
+
handleParsingNestedValues(form, key, value);
|
|
75
|
+
delete form[key];
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return form;
|
|
79
|
+
}
|
|
80
|
+
var handleParsingAllValues = (form, key, value) => {
|
|
81
|
+
if (form[key] !== void 0) if (Array.isArray(form[key])) form[key].push(value);
|
|
82
|
+
else form[key] = [form[key], value];
|
|
83
|
+
else if (!key.endsWith("[]")) form[key] = value;
|
|
84
|
+
else form[key] = [value];
|
|
85
|
+
};
|
|
86
|
+
var handleParsingNestedValues = (form, key, value) => {
|
|
87
|
+
if (/(?:^|\.)__proto__\./.test(key)) return;
|
|
88
|
+
let nestedForm = form;
|
|
89
|
+
const keys = key.split(".");
|
|
90
|
+
keys.forEach((key2, index) => {
|
|
91
|
+
if (index === keys.length - 1) nestedForm[key2] = value;
|
|
92
|
+
else {
|
|
93
|
+
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
94
|
+
nestedForm = nestedForm[key2];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region node_modules/hono/dist/utils/url.js
|
|
100
|
+
var splitPath = (path) => {
|
|
101
|
+
const paths = path.split("/");
|
|
102
|
+
if (paths[0] === "") paths.shift();
|
|
103
|
+
return paths;
|
|
104
|
+
};
|
|
105
|
+
var splitRoutingPath = (routePath) => {
|
|
106
|
+
const { groups, path } = extractGroupsFromPath(routePath);
|
|
107
|
+
return replaceGroupMarks(splitPath(path), groups);
|
|
108
|
+
};
|
|
109
|
+
var extractGroupsFromPath = (path) => {
|
|
110
|
+
const groups = [];
|
|
111
|
+
path = path.replace(/\{[^}]+\}/g, (match, index) => {
|
|
112
|
+
const mark = `@${index}`;
|
|
113
|
+
groups.push([mark, match]);
|
|
114
|
+
return mark;
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
groups,
|
|
118
|
+
path
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
var replaceGroupMarks = (paths, groups) => {
|
|
122
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
123
|
+
const [mark] = groups[i];
|
|
124
|
+
for (let j = paths.length - 1; j >= 0; j--) if (paths[j].includes(mark)) {
|
|
125
|
+
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return paths;
|
|
130
|
+
};
|
|
131
|
+
var patternCache = {};
|
|
132
|
+
var getPattern = (label, next) => {
|
|
133
|
+
if (label === "*") return "*";
|
|
134
|
+
const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
135
|
+
if (match) {
|
|
136
|
+
const cacheKey = `${label}#${next}`;
|
|
137
|
+
if (!patternCache[cacheKey]) if (match[2]) patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [
|
|
138
|
+
cacheKey,
|
|
139
|
+
match[1],
|
|
140
|
+
new RegExp(`^${match[2]}(?=/${next})`)
|
|
141
|
+
] : [
|
|
142
|
+
label,
|
|
143
|
+
match[1],
|
|
144
|
+
new RegExp(`^${match[2]}$`)
|
|
145
|
+
];
|
|
146
|
+
else patternCache[cacheKey] = [
|
|
147
|
+
label,
|
|
148
|
+
match[1],
|
|
149
|
+
true
|
|
150
|
+
];
|
|
151
|
+
return patternCache[cacheKey];
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
var tryDecode$1 = (str, decoder) => {
|
|
156
|
+
try {
|
|
157
|
+
return decoder(str);
|
|
158
|
+
} catch {
|
|
159
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
160
|
+
try {
|
|
161
|
+
return decoder(match);
|
|
162
|
+
} catch {
|
|
163
|
+
return match;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
var tryDecodeURI$1 = (str) => tryDecode$1(str, decodeURI);
|
|
169
|
+
var getPath = (request) => {
|
|
170
|
+
const url = request.url;
|
|
171
|
+
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
172
|
+
let i = start;
|
|
173
|
+
for (; i < url.length; i++) {
|
|
174
|
+
const charCode = url.charCodeAt(i);
|
|
175
|
+
if (charCode === 37) {
|
|
176
|
+
const queryIndex = url.indexOf("?", i);
|
|
177
|
+
const hashIndex = url.indexOf("#", i);
|
|
178
|
+
const end = queryIndex === -1 ? hashIndex === -1 ? void 0 : hashIndex : hashIndex === -1 ? queryIndex : Math.min(queryIndex, hashIndex);
|
|
179
|
+
const path = url.slice(start, end);
|
|
180
|
+
return tryDecodeURI$1(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
|
|
181
|
+
} else if (charCode === 63 || charCode === 35) break;
|
|
182
|
+
}
|
|
183
|
+
return url.slice(start, i);
|
|
184
|
+
};
|
|
185
|
+
var getPathNoStrict = (request) => {
|
|
186
|
+
const result = getPath(request);
|
|
187
|
+
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
188
|
+
};
|
|
189
|
+
var mergePath = (base, sub, ...rest) => {
|
|
190
|
+
if (rest.length) sub = mergePath(sub, ...rest);
|
|
191
|
+
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
192
|
+
};
|
|
193
|
+
var checkOptionalParameter = (path) => {
|
|
194
|
+
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) return null;
|
|
195
|
+
const segments = path.split("/");
|
|
196
|
+
const results = [];
|
|
197
|
+
let basePath = "";
|
|
198
|
+
segments.forEach((segment) => {
|
|
199
|
+
if (segment !== "" && !/\:/.test(segment)) basePath += "/" + segment;
|
|
200
|
+
else if (/\:/.test(segment)) if (/\?/.test(segment)) {
|
|
201
|
+
if (results.length === 0 && basePath === "") results.push("/");
|
|
202
|
+
else results.push(basePath);
|
|
203
|
+
const optionalSegment = segment.replace("?", "");
|
|
204
|
+
basePath += "/" + optionalSegment;
|
|
205
|
+
results.push(basePath);
|
|
206
|
+
} else basePath += "/" + segment;
|
|
207
|
+
});
|
|
208
|
+
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
209
|
+
};
|
|
210
|
+
var _decodeURI = (value) => {
|
|
211
|
+
if (!/[%+]/.test(value)) return value;
|
|
212
|
+
if (value.indexOf("+") !== -1) value = value.replace(/\+/g, " ");
|
|
213
|
+
return value.indexOf("%") !== -1 ? tryDecode$1(value, decodeURIComponent_) : value;
|
|
214
|
+
};
|
|
215
|
+
var _getQueryParam = (url, key, multiple) => {
|
|
216
|
+
let encoded;
|
|
217
|
+
if (!multiple && key && !/[%+]/.test(key)) {
|
|
218
|
+
let keyIndex2 = url.indexOf("?", 8);
|
|
219
|
+
if (keyIndex2 === -1) return;
|
|
220
|
+
if (!url.startsWith(key, keyIndex2 + 1)) keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
221
|
+
while (keyIndex2 !== -1) {
|
|
222
|
+
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
223
|
+
if (trailingKeyCode === 61) {
|
|
224
|
+
const valueIndex = keyIndex2 + key.length + 2;
|
|
225
|
+
const endIndex = url.indexOf("&", valueIndex);
|
|
226
|
+
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
|
|
227
|
+
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) return "";
|
|
228
|
+
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
229
|
+
}
|
|
230
|
+
encoded = /[%+]/.test(url);
|
|
231
|
+
if (!encoded) return;
|
|
232
|
+
}
|
|
233
|
+
const results = {};
|
|
234
|
+
encoded ??= /[%+]/.test(url);
|
|
235
|
+
let keyIndex = url.indexOf("?", 8);
|
|
236
|
+
while (keyIndex !== -1) {
|
|
237
|
+
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
238
|
+
let valueIndex = url.indexOf("=", keyIndex);
|
|
239
|
+
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) valueIndex = -1;
|
|
240
|
+
let name = url.slice(keyIndex + 1, valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex);
|
|
241
|
+
if (encoded) name = _decodeURI(name);
|
|
242
|
+
keyIndex = nextKeyIndex;
|
|
243
|
+
if (name === "") continue;
|
|
244
|
+
let value;
|
|
245
|
+
if (valueIndex === -1) value = "";
|
|
246
|
+
else {
|
|
247
|
+
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
|
|
248
|
+
if (encoded) value = _decodeURI(value);
|
|
249
|
+
}
|
|
250
|
+
if (multiple) {
|
|
251
|
+
if (!(results[name] && Array.isArray(results[name]))) results[name] = [];
|
|
252
|
+
results[name].push(value);
|
|
253
|
+
} else results[name] ??= value;
|
|
254
|
+
}
|
|
255
|
+
return key ? results[key] : results;
|
|
256
|
+
};
|
|
257
|
+
var getQueryParam = _getQueryParam;
|
|
258
|
+
var getQueryParams = (url, key) => {
|
|
259
|
+
return _getQueryParam(url, key, true);
|
|
260
|
+
};
|
|
261
|
+
var decodeURIComponent_ = decodeURIComponent;
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region node_modules/hono/dist/request.js
|
|
264
|
+
var tryDecodeURIComponent = (str) => tryDecode$1(str, decodeURIComponent_);
|
|
265
|
+
var HonoRequest = class {
|
|
266
|
+
/**
|
|
267
|
+
* `.raw` can get the raw Request object.
|
|
268
|
+
*
|
|
269
|
+
* @see {@link https://hono.dev/docs/api/request#raw}
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* // For Cloudflare Workers
|
|
274
|
+
* app.post('/', async (c) => {
|
|
275
|
+
* const metadata = c.req.raw.cf?.hostMetadata?
|
|
276
|
+
* ...
|
|
277
|
+
* })
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
raw;
|
|
281
|
+
#validatedData;
|
|
282
|
+
#matchResult;
|
|
283
|
+
routeIndex = 0;
|
|
284
|
+
/**
|
|
285
|
+
* `.path` can get the pathname of the request.
|
|
286
|
+
*
|
|
287
|
+
* @see {@link https://hono.dev/docs/api/request#path}
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* app.get('/about/me', (c) => {
|
|
292
|
+
* const pathname = c.req.path // `/about/me`
|
|
293
|
+
* })
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
path;
|
|
297
|
+
bodyCache = {};
|
|
298
|
+
constructor(request, path = "/", matchResult = [[]]) {
|
|
299
|
+
this.raw = request;
|
|
300
|
+
this.path = path;
|
|
301
|
+
this.#matchResult = matchResult;
|
|
302
|
+
this.#validatedData = {};
|
|
303
|
+
}
|
|
304
|
+
param(key) {
|
|
305
|
+
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
306
|
+
}
|
|
307
|
+
#getDecodedParam(key) {
|
|
308
|
+
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
309
|
+
const param = this.#getParamValue(paramKey);
|
|
310
|
+
return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
|
|
311
|
+
}
|
|
312
|
+
#getAllDecodedParams() {
|
|
313
|
+
const decoded = {};
|
|
314
|
+
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
315
|
+
for (const key of keys) {
|
|
316
|
+
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
317
|
+
if (value !== void 0) decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
318
|
+
}
|
|
319
|
+
return decoded;
|
|
320
|
+
}
|
|
321
|
+
#getParamValue(paramKey) {
|
|
322
|
+
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
323
|
+
}
|
|
324
|
+
query(key) {
|
|
325
|
+
return getQueryParam(this.url, key);
|
|
326
|
+
}
|
|
327
|
+
queries(key) {
|
|
328
|
+
return getQueryParams(this.url, key);
|
|
329
|
+
}
|
|
330
|
+
header(name) {
|
|
331
|
+
if (name) return this.raw.headers.get(name) ?? void 0;
|
|
332
|
+
const headerData = {};
|
|
333
|
+
this.raw.headers.forEach((value, key) => {
|
|
334
|
+
headerData[key] = value;
|
|
335
|
+
});
|
|
336
|
+
return headerData;
|
|
337
|
+
}
|
|
338
|
+
async parseBody(options) {
|
|
339
|
+
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
340
|
+
}
|
|
341
|
+
#cachedBody = (key) => {
|
|
342
|
+
const { bodyCache, raw } = this;
|
|
343
|
+
const cachedBody = bodyCache[key];
|
|
344
|
+
if (cachedBody) return cachedBody;
|
|
345
|
+
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
346
|
+
if (anyCachedKey) return bodyCache[anyCachedKey].then((body) => {
|
|
347
|
+
if (anyCachedKey === "json") body = JSON.stringify(body);
|
|
348
|
+
return new Response(body)[key]();
|
|
349
|
+
});
|
|
350
|
+
return bodyCache[key] = raw[key]();
|
|
351
|
+
};
|
|
352
|
+
/**
|
|
353
|
+
* `.json()` can parse Request body of type `application/json`
|
|
354
|
+
*
|
|
355
|
+
* @see {@link https://hono.dev/docs/api/request#json}
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* app.post('/entry', async (c) => {
|
|
360
|
+
* const body = await c.req.json()
|
|
361
|
+
* })
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
json() {
|
|
365
|
+
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* `.text()` can parse Request body of type `text/plain`
|
|
369
|
+
*
|
|
370
|
+
* @see {@link https://hono.dev/docs/api/request#text}
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* app.post('/entry', async (c) => {
|
|
375
|
+
* const body = await c.req.text()
|
|
376
|
+
* })
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
text() {
|
|
380
|
+
return this.#cachedBody("text");
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* `.arrayBuffer()` parse Request body as an `ArrayBuffer`
|
|
384
|
+
*
|
|
385
|
+
* @see {@link https://hono.dev/docs/api/request#arraybuffer}
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* app.post('/entry', async (c) => {
|
|
390
|
+
* const body = await c.req.arrayBuffer()
|
|
391
|
+
* })
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
arrayBuffer() {
|
|
395
|
+
return this.#cachedBody("arrayBuffer");
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Parses the request body as a `Blob`.
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* app.post('/entry', async (c) => {
|
|
402
|
+
* const body = await c.req.blob();
|
|
403
|
+
* });
|
|
404
|
+
* ```
|
|
405
|
+
* @see https://hono.dev/docs/api/request#blob
|
|
406
|
+
*/
|
|
407
|
+
blob() {
|
|
408
|
+
return this.#cachedBody("blob");
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Parses the request body as `FormData`.
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* app.post('/entry', async (c) => {
|
|
415
|
+
* const body = await c.req.formData();
|
|
416
|
+
* });
|
|
417
|
+
* ```
|
|
418
|
+
* @see https://hono.dev/docs/api/request#formdata
|
|
419
|
+
*/
|
|
420
|
+
formData() {
|
|
421
|
+
return this.#cachedBody("formData");
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Adds validated data to the request.
|
|
425
|
+
*
|
|
426
|
+
* @param target - The target of the validation.
|
|
427
|
+
* @param data - The validated data to add.
|
|
428
|
+
*/
|
|
429
|
+
addValidatedData(target, data) {
|
|
430
|
+
this.#validatedData[target] = data;
|
|
431
|
+
}
|
|
432
|
+
valid(target) {
|
|
433
|
+
return this.#validatedData[target];
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* `.url()` can get the request url strings.
|
|
437
|
+
*
|
|
438
|
+
* @see {@link https://hono.dev/docs/api/request#url}
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* app.get('/about/me', (c) => {
|
|
443
|
+
* const url = c.req.url // `http://localhost:8787/about/me`
|
|
444
|
+
* ...
|
|
445
|
+
* })
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
get url() {
|
|
449
|
+
return this.raw.url;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* `.method()` can get the method name of the request.
|
|
453
|
+
*
|
|
454
|
+
* @see {@link https://hono.dev/docs/api/request#method}
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* ```ts
|
|
458
|
+
* app.get('/about/me', (c) => {
|
|
459
|
+
* const method = c.req.method // `GET`
|
|
460
|
+
* })
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
get method() {
|
|
464
|
+
return this.raw.method;
|
|
465
|
+
}
|
|
466
|
+
get [GET_MATCH_RESULT]() {
|
|
467
|
+
return this.#matchResult;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* `.matchedRoutes()` can return a matched route in the handler
|
|
471
|
+
*
|
|
472
|
+
* @deprecated
|
|
473
|
+
*
|
|
474
|
+
* Use matchedRoutes helper defined in "hono/route" instead.
|
|
475
|
+
*
|
|
476
|
+
* @see {@link https://hono.dev/docs/api/request#matchedroutes}
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* app.use('*', async function logger(c, next) {
|
|
481
|
+
* await next()
|
|
482
|
+
* c.req.matchedRoutes.forEach(({ handler, method, path }, i) => {
|
|
483
|
+
* const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]')
|
|
484
|
+
* console.log(
|
|
485
|
+
* method,
|
|
486
|
+
* ' ',
|
|
487
|
+
* path,
|
|
488
|
+
* ' '.repeat(Math.max(10 - path.length, 0)),
|
|
489
|
+
* name,
|
|
490
|
+
* i === c.req.routeIndex ? '<- respond from here' : ''
|
|
491
|
+
* )
|
|
492
|
+
* })
|
|
493
|
+
* })
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
get matchedRoutes() {
|
|
497
|
+
return this.#matchResult[0].map(([[, route]]) => route);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* `routePath()` can retrieve the path registered within the handler
|
|
501
|
+
*
|
|
502
|
+
* @deprecated
|
|
503
|
+
*
|
|
504
|
+
* Use routePath helper defined in "hono/route" instead.
|
|
505
|
+
*
|
|
506
|
+
* @see {@link https://hono.dev/docs/api/request#routepath}
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* app.get('/posts/:id', (c) => {
|
|
511
|
+
* return c.json({ path: c.req.routePath })
|
|
512
|
+
* })
|
|
513
|
+
* ```
|
|
514
|
+
*/
|
|
515
|
+
get routePath() {
|
|
516
|
+
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region node_modules/hono/dist/utils/html.js
|
|
521
|
+
var HtmlEscapedCallbackPhase = {
|
|
522
|
+
Stringify: 1,
|
|
523
|
+
BeforeStream: 2,
|
|
524
|
+
Stream: 3
|
|
525
|
+
};
|
|
526
|
+
var raw = (value, callbacks) => {
|
|
527
|
+
const escapedString = new String(value);
|
|
528
|
+
escapedString.isEscaped = true;
|
|
529
|
+
escapedString.callbacks = callbacks;
|
|
530
|
+
return escapedString;
|
|
531
|
+
};
|
|
532
|
+
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
533
|
+
if (typeof str === "object" && !(str instanceof String)) {
|
|
534
|
+
if (!(str instanceof Promise)) str = str.toString();
|
|
535
|
+
if (str instanceof Promise) str = await str;
|
|
536
|
+
}
|
|
537
|
+
const callbacks = str.callbacks;
|
|
538
|
+
if (!callbacks?.length) return Promise.resolve(str);
|
|
539
|
+
if (buffer) buffer[0] += str;
|
|
540
|
+
else buffer = [str];
|
|
541
|
+
const resStr = Promise.all(callbacks.map((c) => c({
|
|
542
|
+
phase,
|
|
543
|
+
buffer,
|
|
544
|
+
context
|
|
545
|
+
}))).then((res) => Promise.all(res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))).then(() => buffer[0]));
|
|
546
|
+
if (preserveCallbacks) return raw(await resStr, callbacks);
|
|
547
|
+
else return resStr;
|
|
548
|
+
};
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region node_modules/hono/dist/context.js
|
|
551
|
+
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
552
|
+
var setDefaultContentType = (contentType, headers) => {
|
|
553
|
+
return {
|
|
554
|
+
"Content-Type": contentType,
|
|
555
|
+
...headers
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
var createResponseInstance = (body, init) => new Response(body, init);
|
|
559
|
+
var Context = class {
|
|
560
|
+
#rawRequest;
|
|
561
|
+
#req;
|
|
562
|
+
/**
|
|
563
|
+
* `.env` can get bindings (environment variables, secrets, KV namespaces, D1 database, R2 bucket etc.) in Cloudflare Workers.
|
|
564
|
+
*
|
|
565
|
+
* @see {@link https://hono.dev/docs/api/context#env}
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```ts
|
|
569
|
+
* // Environment object for Cloudflare Workers
|
|
570
|
+
* app.get('*', async c => {
|
|
571
|
+
* const counter = c.env.COUNTER
|
|
572
|
+
* })
|
|
573
|
+
* ```
|
|
574
|
+
*/
|
|
575
|
+
env = {};
|
|
576
|
+
#var;
|
|
577
|
+
finalized = false;
|
|
578
|
+
/**
|
|
579
|
+
* `.error` can get the error object from the middleware if the Handler throws an error.
|
|
580
|
+
*
|
|
581
|
+
* @see {@link https://hono.dev/docs/api/context#error}
|
|
582
|
+
*
|
|
583
|
+
* @example
|
|
584
|
+
* ```ts
|
|
585
|
+
* app.use('*', async (c, next) => {
|
|
586
|
+
* await next()
|
|
587
|
+
* if (c.error) {
|
|
588
|
+
* // do something...
|
|
589
|
+
* }
|
|
590
|
+
* })
|
|
591
|
+
* ```
|
|
592
|
+
*/
|
|
593
|
+
error;
|
|
594
|
+
#status;
|
|
595
|
+
#executionCtx;
|
|
596
|
+
#res;
|
|
597
|
+
#layout;
|
|
598
|
+
#renderer;
|
|
599
|
+
#notFoundHandler;
|
|
600
|
+
#preparedHeaders;
|
|
601
|
+
#matchResult;
|
|
602
|
+
#path;
|
|
603
|
+
/**
|
|
604
|
+
* Creates an instance of the Context class.
|
|
605
|
+
*
|
|
606
|
+
* @param req - The Request object.
|
|
607
|
+
* @param options - Optional configuration options for the context.
|
|
608
|
+
*/
|
|
609
|
+
constructor(req, options) {
|
|
610
|
+
this.#rawRequest = req;
|
|
611
|
+
if (options) {
|
|
612
|
+
this.#executionCtx = options.executionCtx;
|
|
613
|
+
this.env = options.env;
|
|
614
|
+
this.#notFoundHandler = options.notFoundHandler;
|
|
615
|
+
this.#path = options.path;
|
|
616
|
+
this.#matchResult = options.matchResult;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* `.req` is the instance of {@link HonoRequest}.
|
|
621
|
+
*/
|
|
622
|
+
get req() {
|
|
623
|
+
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
624
|
+
return this.#req;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* @see {@link https://hono.dev/docs/api/context#event}
|
|
628
|
+
* The FetchEvent associated with the current request.
|
|
629
|
+
*
|
|
630
|
+
* @throws Will throw an error if the context does not have a FetchEvent.
|
|
631
|
+
*/
|
|
632
|
+
get event() {
|
|
633
|
+
if (this.#executionCtx && "respondWith" in this.#executionCtx) return this.#executionCtx;
|
|
634
|
+
else throw Error("This context has no FetchEvent");
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* @see {@link https://hono.dev/docs/api/context#executionctx}
|
|
638
|
+
* The ExecutionContext associated with the current request.
|
|
639
|
+
*
|
|
640
|
+
* @throws Will throw an error if the context does not have an ExecutionContext.
|
|
641
|
+
*/
|
|
642
|
+
get executionCtx() {
|
|
643
|
+
if (this.#executionCtx) return this.#executionCtx;
|
|
644
|
+
else throw Error("This context has no ExecutionContext");
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* @see {@link https://hono.dev/docs/api/context#res}
|
|
648
|
+
* The Response object for the current request.
|
|
649
|
+
*/
|
|
650
|
+
get res() {
|
|
651
|
+
return this.#res ||= createResponseInstance(null, { headers: this.#preparedHeaders ??= new Headers() });
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Sets the Response object for the current request.
|
|
655
|
+
*
|
|
656
|
+
* @param _res - The Response object to set.
|
|
657
|
+
*/
|
|
658
|
+
set res(_res) {
|
|
659
|
+
if (this.#res && _res) {
|
|
660
|
+
_res = createResponseInstance(_res.body, _res);
|
|
661
|
+
for (const [k, v] of this.#res.headers.entries()) {
|
|
662
|
+
if (k === "content-type") continue;
|
|
663
|
+
if (k === "set-cookie") {
|
|
664
|
+
const cookies = this.#res.headers.getSetCookie();
|
|
665
|
+
_res.headers.delete("set-cookie");
|
|
666
|
+
for (const cookie of cookies) _res.headers.append("set-cookie", cookie);
|
|
667
|
+
} else _res.headers.set(k, v);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
this.#res = _res;
|
|
671
|
+
this.finalized = true;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* `.render()` can create a response within a layout.
|
|
675
|
+
*
|
|
676
|
+
* @see {@link https://hono.dev/docs/api/context#render-setrenderer}
|
|
677
|
+
*
|
|
678
|
+
* @example
|
|
679
|
+
* ```ts
|
|
680
|
+
* app.get('/', (c) => {
|
|
681
|
+
* return c.render('Hello!')
|
|
682
|
+
* })
|
|
683
|
+
* ```
|
|
684
|
+
*/
|
|
685
|
+
render = (...args) => {
|
|
686
|
+
this.#renderer ??= (content) => this.html(content);
|
|
687
|
+
return this.#renderer(...args);
|
|
688
|
+
};
|
|
689
|
+
/**
|
|
690
|
+
* Sets the layout for the response.
|
|
691
|
+
*
|
|
692
|
+
* @param layout - The layout to set.
|
|
693
|
+
* @returns The layout function.
|
|
694
|
+
*/
|
|
695
|
+
setLayout = (layout) => this.#layout = layout;
|
|
696
|
+
/**
|
|
697
|
+
* Gets the current layout for the response.
|
|
698
|
+
*
|
|
699
|
+
* @returns The current layout function.
|
|
700
|
+
*/
|
|
701
|
+
getLayout = () => this.#layout;
|
|
702
|
+
/**
|
|
703
|
+
* `.setRenderer()` can set the layout in the custom middleware.
|
|
704
|
+
*
|
|
705
|
+
* @see {@link https://hono.dev/docs/api/context#render-setrenderer}
|
|
706
|
+
*
|
|
707
|
+
* @example
|
|
708
|
+
* ```tsx
|
|
709
|
+
* app.use('*', async (c, next) => {
|
|
710
|
+
* c.setRenderer((content) => {
|
|
711
|
+
* return c.html(
|
|
712
|
+
* <html>
|
|
713
|
+
* <body>
|
|
714
|
+
* <p>{content}</p>
|
|
715
|
+
* </body>
|
|
716
|
+
* </html>
|
|
717
|
+
* )
|
|
718
|
+
* })
|
|
719
|
+
* await next()
|
|
720
|
+
* })
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
setRenderer = (renderer) => {
|
|
724
|
+
this.#renderer = renderer;
|
|
725
|
+
};
|
|
726
|
+
/**
|
|
727
|
+
* `.header()` can set headers.
|
|
728
|
+
*
|
|
729
|
+
* @see {@link https://hono.dev/docs/api/context#header}
|
|
730
|
+
*
|
|
731
|
+
* @example
|
|
732
|
+
* ```ts
|
|
733
|
+
* app.get('/welcome', (c) => {
|
|
734
|
+
* // Set headers
|
|
735
|
+
* c.header('X-Message', 'Hello!')
|
|
736
|
+
* c.header('Content-Type', 'text/plain')
|
|
737
|
+
*
|
|
738
|
+
* return c.body('Thank you for coming')
|
|
739
|
+
* })
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
header = (name, value, options) => {
|
|
743
|
+
if (this.finalized) this.#res = createResponseInstance(this.#res.body, this.#res);
|
|
744
|
+
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers();
|
|
745
|
+
if (value === void 0) headers.delete(name);
|
|
746
|
+
else if (options?.append) headers.append(name, value);
|
|
747
|
+
else headers.set(name, value);
|
|
748
|
+
};
|
|
749
|
+
status = (status) => {
|
|
750
|
+
this.#status = status;
|
|
751
|
+
};
|
|
752
|
+
/**
|
|
753
|
+
* `.set()` can set the value specified by the key.
|
|
754
|
+
*
|
|
755
|
+
* @see {@link https://hono.dev/docs/api/context#set-get}
|
|
756
|
+
*
|
|
757
|
+
* @example
|
|
758
|
+
* ```ts
|
|
759
|
+
* app.use('*', async (c, next) => {
|
|
760
|
+
* c.set('message', 'Hono is hot!!')
|
|
761
|
+
* await next()
|
|
762
|
+
* })
|
|
763
|
+
* ```
|
|
764
|
+
*/
|
|
765
|
+
set = (key, value) => {
|
|
766
|
+
this.#var ??= /* @__PURE__ */ new Map();
|
|
767
|
+
this.#var.set(key, value);
|
|
768
|
+
};
|
|
769
|
+
/**
|
|
770
|
+
* `.get()` can use the value specified by the key.
|
|
771
|
+
*
|
|
772
|
+
* @see {@link https://hono.dev/docs/api/context#set-get}
|
|
773
|
+
*
|
|
774
|
+
* @example
|
|
775
|
+
* ```ts
|
|
776
|
+
* app.get('/', (c) => {
|
|
777
|
+
* const message = c.get('message')
|
|
778
|
+
* return c.text(`The message is "${message}"`)
|
|
779
|
+
* })
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
get = (key) => {
|
|
783
|
+
return this.#var ? this.#var.get(key) : void 0;
|
|
784
|
+
};
|
|
785
|
+
/**
|
|
786
|
+
* `.var` can access the value of a variable.
|
|
787
|
+
*
|
|
788
|
+
* @see {@link https://hono.dev/docs/api/context#var}
|
|
789
|
+
*
|
|
790
|
+
* @example
|
|
791
|
+
* ```ts
|
|
792
|
+
* const result = c.var.client.oneMethod()
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
get var() {
|
|
796
|
+
if (!this.#var) return {};
|
|
797
|
+
return Object.fromEntries(this.#var);
|
|
798
|
+
}
|
|
799
|
+
#newResponse(data, arg, headers) {
|
|
800
|
+
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers();
|
|
801
|
+
if (typeof arg === "object" && "headers" in arg) {
|
|
802
|
+
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
803
|
+
for (const [key, value] of argHeaders) if (key.toLowerCase() === "set-cookie") responseHeaders.append(key, value);
|
|
804
|
+
else responseHeaders.set(key, value);
|
|
805
|
+
}
|
|
806
|
+
if (headers) for (const [k, v] of Object.entries(headers)) if (typeof v === "string") responseHeaders.set(k, v);
|
|
807
|
+
else {
|
|
808
|
+
responseHeaders.delete(k);
|
|
809
|
+
for (const v2 of v) responseHeaders.append(k, v2);
|
|
810
|
+
}
|
|
811
|
+
return createResponseInstance(data, {
|
|
812
|
+
status: typeof arg === "number" ? arg : arg?.status ?? this.#status,
|
|
813
|
+
headers: responseHeaders
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
newResponse = (...args) => this.#newResponse(...args);
|
|
817
|
+
/**
|
|
818
|
+
* `.body()` can return the HTTP response.
|
|
819
|
+
* You can set headers with `.header()` and set HTTP status code with `.status`.
|
|
820
|
+
* This can also be set in `.text()`, `.json()` and so on.
|
|
821
|
+
*
|
|
822
|
+
* @see {@link https://hono.dev/docs/api/context#body}
|
|
823
|
+
*
|
|
824
|
+
* @example
|
|
825
|
+
* ```ts
|
|
826
|
+
* app.get('/welcome', (c) => {
|
|
827
|
+
* // Set headers
|
|
828
|
+
* c.header('X-Message', 'Hello!')
|
|
829
|
+
* c.header('Content-Type', 'text/plain')
|
|
830
|
+
* // Set HTTP status code
|
|
831
|
+
* c.status(201)
|
|
832
|
+
*
|
|
833
|
+
* // Return the response body
|
|
834
|
+
* return c.body('Thank you for coming')
|
|
835
|
+
* })
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
839
|
+
/**
|
|
840
|
+
* `.text()` can render text as `Content-Type:text/plain`.
|
|
841
|
+
*
|
|
842
|
+
* @see {@link https://hono.dev/docs/api/context#text}
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* app.get('/say', (c) => {
|
|
847
|
+
* return c.text('Hello!')
|
|
848
|
+
* })
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
text = (text, arg, headers) => {
|
|
852
|
+
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(text, arg, setDefaultContentType(TEXT_PLAIN, headers));
|
|
853
|
+
};
|
|
854
|
+
/**
|
|
855
|
+
* `.json()` can render JSON as `Content-Type:application/json`.
|
|
856
|
+
*
|
|
857
|
+
* @see {@link https://hono.dev/docs/api/context#json}
|
|
858
|
+
*
|
|
859
|
+
* @example
|
|
860
|
+
* ```ts
|
|
861
|
+
* app.get('/api', (c) => {
|
|
862
|
+
* return c.json({ message: 'Hello!' })
|
|
863
|
+
* })
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
json = (object, arg, headers) => {
|
|
867
|
+
return this.#newResponse(JSON.stringify(object), arg, setDefaultContentType("application/json", headers));
|
|
868
|
+
};
|
|
869
|
+
html = (html, arg, headers) => {
|
|
870
|
+
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
871
|
+
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
872
|
+
};
|
|
873
|
+
/**
|
|
874
|
+
* `.redirect()` can Redirect, default status code is 302.
|
|
875
|
+
*
|
|
876
|
+
* @see {@link https://hono.dev/docs/api/context#redirect}
|
|
877
|
+
*
|
|
878
|
+
* @example
|
|
879
|
+
* ```ts
|
|
880
|
+
* app.get('/redirect', (c) => {
|
|
881
|
+
* return c.redirect('/')
|
|
882
|
+
* })
|
|
883
|
+
* app.get('/redirect-permanently', (c) => {
|
|
884
|
+
* return c.redirect('/', 301)
|
|
885
|
+
* })
|
|
886
|
+
* ```
|
|
887
|
+
*/
|
|
888
|
+
redirect = (location, status) => {
|
|
889
|
+
const locationString = String(location);
|
|
890
|
+
this.header("Location", !/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString));
|
|
891
|
+
return this.newResponse(null, status ?? 302);
|
|
892
|
+
};
|
|
893
|
+
/**
|
|
894
|
+
* `.notFound()` can return the Not Found Response.
|
|
895
|
+
*
|
|
896
|
+
* @see {@link https://hono.dev/docs/api/context#notfound}
|
|
897
|
+
*
|
|
898
|
+
* @example
|
|
899
|
+
* ```ts
|
|
900
|
+
* app.get('/notfound', (c) => {
|
|
901
|
+
* return c.notFound()
|
|
902
|
+
* })
|
|
903
|
+
* ```
|
|
904
|
+
*/
|
|
905
|
+
notFound = () => {
|
|
906
|
+
this.#notFoundHandler ??= () => createResponseInstance();
|
|
907
|
+
return this.#notFoundHandler(this);
|
|
908
|
+
};
|
|
909
|
+
};
|
|
910
|
+
//#endregion
|
|
911
|
+
//#region node_modules/hono/dist/router.js
|
|
912
|
+
var METHODS = [
|
|
913
|
+
"get",
|
|
914
|
+
"post",
|
|
915
|
+
"put",
|
|
916
|
+
"delete",
|
|
917
|
+
"options",
|
|
918
|
+
"patch"
|
|
919
|
+
];
|
|
920
|
+
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
921
|
+
var UnsupportedPathError = class extends Error {};
|
|
922
|
+
//#endregion
|
|
923
|
+
//#region node_modules/hono/dist/utils/constants.js
|
|
924
|
+
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
925
|
+
//#endregion
|
|
926
|
+
//#region node_modules/hono/dist/hono-base.js
|
|
927
|
+
var notFoundHandler = (c) => {
|
|
928
|
+
return c.text("404 Not Found", 404);
|
|
929
|
+
};
|
|
930
|
+
var errorHandler = (err, c) => {
|
|
931
|
+
if ("getResponse" in err) {
|
|
932
|
+
const res = err.getResponse();
|
|
933
|
+
return c.newResponse(res.body, res);
|
|
934
|
+
}
|
|
935
|
+
console.error(err);
|
|
936
|
+
return c.text("Internal Server Error", 500);
|
|
937
|
+
};
|
|
938
|
+
var Hono$1 = class _Hono {
|
|
939
|
+
get;
|
|
940
|
+
post;
|
|
941
|
+
put;
|
|
942
|
+
delete;
|
|
943
|
+
options;
|
|
944
|
+
patch;
|
|
945
|
+
all;
|
|
946
|
+
on;
|
|
947
|
+
use;
|
|
948
|
+
router;
|
|
949
|
+
getPath;
|
|
950
|
+
_basePath = "/";
|
|
951
|
+
#path = "/";
|
|
952
|
+
routes = [];
|
|
953
|
+
constructor(options = {}) {
|
|
954
|
+
[...METHODS, "all"].forEach((method) => {
|
|
955
|
+
this[method] = (args1, ...args) => {
|
|
956
|
+
if (typeof args1 === "string") this.#path = args1;
|
|
957
|
+
else this.#addRoute(method, this.#path, args1);
|
|
958
|
+
args.forEach((handler) => {
|
|
959
|
+
this.#addRoute(method, this.#path, handler);
|
|
960
|
+
});
|
|
961
|
+
return this;
|
|
962
|
+
};
|
|
963
|
+
});
|
|
964
|
+
this.on = (method, path, ...handlers) => {
|
|
965
|
+
for (const p of [path].flat()) {
|
|
966
|
+
this.#path = p;
|
|
967
|
+
for (const m of [method].flat()) handlers.map((handler) => {
|
|
968
|
+
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
return this;
|
|
972
|
+
};
|
|
973
|
+
this.use = (arg1, ...handlers) => {
|
|
974
|
+
if (typeof arg1 === "string") this.#path = arg1;
|
|
975
|
+
else {
|
|
976
|
+
this.#path = "*";
|
|
977
|
+
handlers.unshift(arg1);
|
|
978
|
+
}
|
|
979
|
+
handlers.forEach((handler) => {
|
|
980
|
+
this.#addRoute("ALL", this.#path, handler);
|
|
981
|
+
});
|
|
982
|
+
return this;
|
|
983
|
+
};
|
|
984
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
985
|
+
Object.assign(this, optionsWithoutStrict);
|
|
986
|
+
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
987
|
+
}
|
|
988
|
+
#clone() {
|
|
989
|
+
const clone = new _Hono({
|
|
990
|
+
router: this.router,
|
|
991
|
+
getPath: this.getPath
|
|
992
|
+
});
|
|
993
|
+
clone.errorHandler = this.errorHandler;
|
|
994
|
+
clone.#notFoundHandler = this.#notFoundHandler;
|
|
995
|
+
clone.routes = this.routes;
|
|
996
|
+
return clone;
|
|
997
|
+
}
|
|
998
|
+
#notFoundHandler = notFoundHandler;
|
|
999
|
+
errorHandler = errorHandler;
|
|
1000
|
+
/**
|
|
1001
|
+
* `.route()` allows grouping other Hono instance in routes.
|
|
1002
|
+
*
|
|
1003
|
+
* @see {@link https://hono.dev/docs/api/routing#grouping}
|
|
1004
|
+
*
|
|
1005
|
+
* @param {string} path - base Path
|
|
1006
|
+
* @param {Hono} app - other Hono instance
|
|
1007
|
+
* @returns {Hono} routed Hono instance
|
|
1008
|
+
*
|
|
1009
|
+
* @example
|
|
1010
|
+
* ```ts
|
|
1011
|
+
* const app = new Hono()
|
|
1012
|
+
* const app2 = new Hono()
|
|
1013
|
+
*
|
|
1014
|
+
* app2.get("/user", (c) => c.text("user"))
|
|
1015
|
+
* app.route("/api", app2) // GET /api/user
|
|
1016
|
+
* ```
|
|
1017
|
+
*/
|
|
1018
|
+
route(path, app) {
|
|
1019
|
+
const subApp = this.basePath(path);
|
|
1020
|
+
app.routes.map((r) => {
|
|
1021
|
+
let handler;
|
|
1022
|
+
if (app.errorHandler === errorHandler) handler = r.handler;
|
|
1023
|
+
else {
|
|
1024
|
+
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
1025
|
+
handler[COMPOSED_HANDLER] = r.handler;
|
|
1026
|
+
}
|
|
1027
|
+
subApp.#addRoute(r.method, r.path, handler);
|
|
1028
|
+
});
|
|
1029
|
+
return this;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* `.basePath()` allows base paths to be specified.
|
|
1033
|
+
*
|
|
1034
|
+
* @see {@link https://hono.dev/docs/api/routing#base-path}
|
|
1035
|
+
*
|
|
1036
|
+
* @param {string} path - base Path
|
|
1037
|
+
* @returns {Hono} changed Hono instance
|
|
1038
|
+
*
|
|
1039
|
+
* @example
|
|
1040
|
+
* ```ts
|
|
1041
|
+
* const api = new Hono().basePath('/api')
|
|
1042
|
+
* ```
|
|
1043
|
+
*/
|
|
1044
|
+
basePath(path) {
|
|
1045
|
+
const subApp = this.#clone();
|
|
1046
|
+
subApp._basePath = mergePath(this._basePath, path);
|
|
1047
|
+
return subApp;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* `.onError()` handles an error and returns a customized Response.
|
|
1051
|
+
*
|
|
1052
|
+
* @see {@link https://hono.dev/docs/api/hono#error-handling}
|
|
1053
|
+
*
|
|
1054
|
+
* @param {ErrorHandler} handler - request Handler for error
|
|
1055
|
+
* @returns {Hono} changed Hono instance
|
|
1056
|
+
*
|
|
1057
|
+
* @example
|
|
1058
|
+
* ```ts
|
|
1059
|
+
* app.onError((err, c) => {
|
|
1060
|
+
* console.error(`${err}`)
|
|
1061
|
+
* return c.text('Custom Error Message', 500)
|
|
1062
|
+
* })
|
|
1063
|
+
* ```
|
|
1064
|
+
*/
|
|
1065
|
+
onError = (handler) => {
|
|
1066
|
+
this.errorHandler = handler;
|
|
1067
|
+
return this;
|
|
1068
|
+
};
|
|
1069
|
+
/**
|
|
1070
|
+
* `.notFound()` allows you to customize a Not Found Response.
|
|
1071
|
+
*
|
|
1072
|
+
* @see {@link https://hono.dev/docs/api/hono#not-found}
|
|
1073
|
+
*
|
|
1074
|
+
* @param {NotFoundHandler} handler - request handler for not-found
|
|
1075
|
+
* @returns {Hono} changed Hono instance
|
|
1076
|
+
*
|
|
1077
|
+
* @example
|
|
1078
|
+
* ```ts
|
|
1079
|
+
* app.notFound((c) => {
|
|
1080
|
+
* return c.text('Custom 404 Message', 404)
|
|
1081
|
+
* })
|
|
1082
|
+
* ```
|
|
1083
|
+
*/
|
|
1084
|
+
notFound = (handler) => {
|
|
1085
|
+
this.#notFoundHandler = handler;
|
|
1086
|
+
return this;
|
|
1087
|
+
};
|
|
1088
|
+
/**
|
|
1089
|
+
* `.mount()` allows you to mount applications built with other frameworks into your Hono application.
|
|
1090
|
+
*
|
|
1091
|
+
* @see {@link https://hono.dev/docs/api/hono#mount}
|
|
1092
|
+
*
|
|
1093
|
+
* @param {string} path - base Path
|
|
1094
|
+
* @param {Function} applicationHandler - other Request Handler
|
|
1095
|
+
* @param {MountOptions} [options] - options of `.mount()`
|
|
1096
|
+
* @returns {Hono} mounted Hono instance
|
|
1097
|
+
*
|
|
1098
|
+
* @example
|
|
1099
|
+
* ```ts
|
|
1100
|
+
* import { Router as IttyRouter } from 'itty-router'
|
|
1101
|
+
* import { Hono } from 'hono'
|
|
1102
|
+
* // Create itty-router application
|
|
1103
|
+
* const ittyRouter = IttyRouter()
|
|
1104
|
+
* // GET /itty-router/hello
|
|
1105
|
+
* ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
|
|
1106
|
+
*
|
|
1107
|
+
* const app = new Hono()
|
|
1108
|
+
* app.mount('/itty-router', ittyRouter.handle)
|
|
1109
|
+
* ```
|
|
1110
|
+
*
|
|
1111
|
+
* @example
|
|
1112
|
+
* ```ts
|
|
1113
|
+
* const app = new Hono()
|
|
1114
|
+
* // Send the request to another application without modification.
|
|
1115
|
+
* app.mount('/app', anotherApp, {
|
|
1116
|
+
* replaceRequest: (req) => req,
|
|
1117
|
+
* })
|
|
1118
|
+
* ```
|
|
1119
|
+
*/
|
|
1120
|
+
mount(path, applicationHandler, options) {
|
|
1121
|
+
let replaceRequest;
|
|
1122
|
+
let optionHandler;
|
|
1123
|
+
if (options) if (typeof options === "function") optionHandler = options;
|
|
1124
|
+
else {
|
|
1125
|
+
optionHandler = options.optionHandler;
|
|
1126
|
+
if (options.replaceRequest === false) replaceRequest = (request) => request;
|
|
1127
|
+
else replaceRequest = options.replaceRequest;
|
|
1128
|
+
}
|
|
1129
|
+
const getOptions = optionHandler ? (c) => {
|
|
1130
|
+
const options2 = optionHandler(c);
|
|
1131
|
+
return Array.isArray(options2) ? options2 : [options2];
|
|
1132
|
+
} : (c) => {
|
|
1133
|
+
let executionContext = void 0;
|
|
1134
|
+
try {
|
|
1135
|
+
executionContext = c.executionCtx;
|
|
1136
|
+
} catch {}
|
|
1137
|
+
return [c.env, executionContext];
|
|
1138
|
+
};
|
|
1139
|
+
replaceRequest ||= (() => {
|
|
1140
|
+
const mergedPath = mergePath(this._basePath, path);
|
|
1141
|
+
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
1142
|
+
return (request) => {
|
|
1143
|
+
const url = new URL(request.url);
|
|
1144
|
+
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
1145
|
+
return new Request(url, request);
|
|
1146
|
+
};
|
|
1147
|
+
})();
|
|
1148
|
+
const handler = async (c, next) => {
|
|
1149
|
+
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
1150
|
+
if (res) return res;
|
|
1151
|
+
await next();
|
|
1152
|
+
};
|
|
1153
|
+
this.#addRoute("ALL", mergePath(path, "*"), handler);
|
|
1154
|
+
return this;
|
|
1155
|
+
}
|
|
1156
|
+
#addRoute(method, path, handler) {
|
|
1157
|
+
method = method.toUpperCase();
|
|
1158
|
+
path = mergePath(this._basePath, path);
|
|
1159
|
+
const r = {
|
|
1160
|
+
basePath: this._basePath,
|
|
1161
|
+
path,
|
|
1162
|
+
method,
|
|
1163
|
+
handler
|
|
1164
|
+
};
|
|
1165
|
+
this.router.add(method, path, [handler, r]);
|
|
1166
|
+
this.routes.push(r);
|
|
1167
|
+
}
|
|
1168
|
+
#handleError(err, c) {
|
|
1169
|
+
if (err instanceof Error) return this.errorHandler(err, c);
|
|
1170
|
+
throw err;
|
|
1171
|
+
}
|
|
1172
|
+
#dispatch(request, executionCtx, env, method) {
|
|
1173
|
+
if (method === "HEAD") return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
1174
|
+
const path = this.getPath(request, { env });
|
|
1175
|
+
const matchResult = this.router.match(method, path);
|
|
1176
|
+
const c = new Context(request, {
|
|
1177
|
+
path,
|
|
1178
|
+
matchResult,
|
|
1179
|
+
env,
|
|
1180
|
+
executionCtx,
|
|
1181
|
+
notFoundHandler: this.#notFoundHandler
|
|
1182
|
+
});
|
|
1183
|
+
if (matchResult[0].length === 1) {
|
|
1184
|
+
let res;
|
|
1185
|
+
try {
|
|
1186
|
+
res = matchResult[0][0][0][0](c, async () => {
|
|
1187
|
+
c.res = await this.#notFoundHandler(c);
|
|
1188
|
+
});
|
|
1189
|
+
} catch (err) {
|
|
1190
|
+
return this.#handleError(err, c);
|
|
1191
|
+
}
|
|
1192
|
+
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);
|
|
1193
|
+
}
|
|
1194
|
+
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
1195
|
+
return (async () => {
|
|
1196
|
+
try {
|
|
1197
|
+
const context = await composed(c);
|
|
1198
|
+
if (!context.finalized) throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");
|
|
1199
|
+
return context.res;
|
|
1200
|
+
} catch (err) {
|
|
1201
|
+
return this.#handleError(err, c);
|
|
1202
|
+
}
|
|
1203
|
+
})();
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* `.fetch()` will be entry point of your app.
|
|
1207
|
+
*
|
|
1208
|
+
* @see {@link https://hono.dev/docs/api/hono#fetch}
|
|
1209
|
+
*
|
|
1210
|
+
* @param {Request} request - request Object of request
|
|
1211
|
+
* @param {Env} Env - env Object
|
|
1212
|
+
* @param {ExecutionContext} - context of execution
|
|
1213
|
+
* @returns {Response | Promise<Response>} response of request
|
|
1214
|
+
*
|
|
1215
|
+
*/
|
|
1216
|
+
fetch = (request, ...rest) => {
|
|
1217
|
+
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
1218
|
+
};
|
|
1219
|
+
/**
|
|
1220
|
+
* `.request()` is a useful method for testing.
|
|
1221
|
+
* You can pass a URL or pathname to send a GET request.
|
|
1222
|
+
* app will return a Response object.
|
|
1223
|
+
* ```ts
|
|
1224
|
+
* test('GET /hello is ok', async () => {
|
|
1225
|
+
* const res = await app.request('/hello')
|
|
1226
|
+
* expect(res.status).toBe(200)
|
|
1227
|
+
* })
|
|
1228
|
+
* ```
|
|
1229
|
+
* @see https://hono.dev/docs/api/hono#request
|
|
1230
|
+
*/
|
|
1231
|
+
request = (input, requestInit, Env, executionCtx) => {
|
|
1232
|
+
if (input instanceof Request) return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
1233
|
+
input = input.toString();
|
|
1234
|
+
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`, requestInit), Env, executionCtx);
|
|
1235
|
+
};
|
|
1236
|
+
/**
|
|
1237
|
+
* `.fire()` automatically adds a global fetch event listener.
|
|
1238
|
+
* This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
|
|
1239
|
+
* @deprecated
|
|
1240
|
+
* Use `fire` from `hono/service-worker` instead.
|
|
1241
|
+
* ```ts
|
|
1242
|
+
* import { Hono } from 'hono'
|
|
1243
|
+
* import { fire } from 'hono/service-worker'
|
|
1244
|
+
*
|
|
1245
|
+
* const app = new Hono()
|
|
1246
|
+
* // ...
|
|
1247
|
+
* fire(app)
|
|
1248
|
+
* ```
|
|
1249
|
+
* @see https://hono.dev/docs/api/hono#fire
|
|
1250
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
|
|
1251
|
+
* @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
|
|
1252
|
+
*/
|
|
1253
|
+
fire = () => {
|
|
1254
|
+
addEventListener("fetch", (event) => {
|
|
1255
|
+
event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
|
|
1256
|
+
});
|
|
1257
|
+
};
|
|
1258
|
+
};
|
|
1259
|
+
//#endregion
|
|
1260
|
+
//#region node_modules/hono/dist/router/reg-exp-router/matcher.js
|
|
1261
|
+
var emptyParam = [];
|
|
1262
|
+
function match(method, path) {
|
|
1263
|
+
const matchers = this.buildAllMatchers();
|
|
1264
|
+
const match2 = ((method2, path2) => {
|
|
1265
|
+
const matcher = matchers[method2] || matchers["ALL"];
|
|
1266
|
+
const staticMatch = matcher[2][path2];
|
|
1267
|
+
if (staticMatch) return staticMatch;
|
|
1268
|
+
const match3 = path2.match(matcher[0]);
|
|
1269
|
+
if (!match3) return [[], emptyParam];
|
|
1270
|
+
const index = match3.indexOf("", 1);
|
|
1271
|
+
return [matcher[1][index], match3];
|
|
1272
|
+
});
|
|
1273
|
+
this.match = match2;
|
|
1274
|
+
return match2(method, path);
|
|
1275
|
+
}
|
|
1276
|
+
//#endregion
|
|
1277
|
+
//#region node_modules/hono/dist/router/reg-exp-router/node.js
|
|
1278
|
+
var LABEL_REG_EXP_STR = "[^/]+";
|
|
1279
|
+
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
1280
|
+
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
1281
|
+
var PATH_ERROR = /* @__PURE__ */ Symbol();
|
|
1282
|
+
var regExpMetaChars = /* @__PURE__ */ new Set(".\\+*[^]$()");
|
|
1283
|
+
function compareKey(a, b) {
|
|
1284
|
+
if (a.length === 1) return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
1285
|
+
if (b.length === 1) return 1;
|
|
1286
|
+
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) return 1;
|
|
1287
|
+
else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) return -1;
|
|
1288
|
+
if (a === LABEL_REG_EXP_STR) return 1;
|
|
1289
|
+
else if (b === LABEL_REG_EXP_STR) return -1;
|
|
1290
|
+
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
1291
|
+
}
|
|
1292
|
+
var Node$1 = class _Node {
|
|
1293
|
+
#index;
|
|
1294
|
+
#varIndex;
|
|
1295
|
+
#children = /* @__PURE__ */ Object.create(null);
|
|
1296
|
+
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
1297
|
+
if (tokens.length === 0) {
|
|
1298
|
+
if (this.#index !== void 0) throw PATH_ERROR;
|
|
1299
|
+
if (pathErrorCheckOnly) return;
|
|
1300
|
+
this.#index = index;
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
const [token, ...restTokens] = tokens;
|
|
1304
|
+
const pattern = token === "*" ? restTokens.length === 0 ? [
|
|
1305
|
+
"",
|
|
1306
|
+
"",
|
|
1307
|
+
ONLY_WILDCARD_REG_EXP_STR
|
|
1308
|
+
] : [
|
|
1309
|
+
"",
|
|
1310
|
+
"",
|
|
1311
|
+
LABEL_REG_EXP_STR
|
|
1312
|
+
] : token === "/*" ? [
|
|
1313
|
+
"",
|
|
1314
|
+
"",
|
|
1315
|
+
TAIL_WILDCARD_REG_EXP_STR
|
|
1316
|
+
] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
1317
|
+
let node;
|
|
1318
|
+
if (pattern) {
|
|
1319
|
+
const name = pattern[1];
|
|
1320
|
+
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
|
1321
|
+
if (name && pattern[2]) {
|
|
1322
|
+
if (regexpStr === ".*") throw PATH_ERROR;
|
|
1323
|
+
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
1324
|
+
if (/\((?!\?:)/.test(regexpStr)) throw PATH_ERROR;
|
|
1325
|
+
}
|
|
1326
|
+
node = this.#children[regexpStr];
|
|
1327
|
+
if (!node) {
|
|
1328
|
+
if (Object.keys(this.#children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) throw PATH_ERROR;
|
|
1329
|
+
if (pathErrorCheckOnly) return;
|
|
1330
|
+
node = this.#children[regexpStr] = new _Node();
|
|
1331
|
+
if (name !== "") node.#varIndex = context.varIndex++;
|
|
1332
|
+
}
|
|
1333
|
+
if (!pathErrorCheckOnly && name !== "") paramMap.push([name, node.#varIndex]);
|
|
1334
|
+
} else {
|
|
1335
|
+
node = this.#children[token];
|
|
1336
|
+
if (!node) {
|
|
1337
|
+
if (Object.keys(this.#children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) throw PATH_ERROR;
|
|
1338
|
+
if (pathErrorCheckOnly) return;
|
|
1339
|
+
node = this.#children[token] = new _Node();
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
1343
|
+
}
|
|
1344
|
+
buildRegExpStr() {
|
|
1345
|
+
const strList = Object.keys(this.#children).sort(compareKey).map((k) => {
|
|
1346
|
+
const c = this.#children[k];
|
|
1347
|
+
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
1348
|
+
});
|
|
1349
|
+
if (typeof this.#index === "number") strList.unshift(`#${this.#index}`);
|
|
1350
|
+
if (strList.length === 0) return "";
|
|
1351
|
+
if (strList.length === 1) return strList[0];
|
|
1352
|
+
return "(?:" + strList.join("|") + ")";
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
//#endregion
|
|
1356
|
+
//#region node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
1357
|
+
var Trie = class {
|
|
1358
|
+
#context = { varIndex: 0 };
|
|
1359
|
+
#root = new Node$1();
|
|
1360
|
+
insert(path, index, pathErrorCheckOnly) {
|
|
1361
|
+
const paramAssoc = [];
|
|
1362
|
+
const groups = [];
|
|
1363
|
+
for (let i = 0;;) {
|
|
1364
|
+
let replaced = false;
|
|
1365
|
+
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
1366
|
+
const mark = `@\\${i}`;
|
|
1367
|
+
groups[i] = [mark, m];
|
|
1368
|
+
i++;
|
|
1369
|
+
replaced = true;
|
|
1370
|
+
return mark;
|
|
1371
|
+
});
|
|
1372
|
+
if (!replaced) break;
|
|
1373
|
+
}
|
|
1374
|
+
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
1375
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1376
|
+
const [mark] = groups[i];
|
|
1377
|
+
for (let j = tokens.length - 1; j >= 0; j--) if (tokens[j].indexOf(mark) !== -1) {
|
|
1378
|
+
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
1379
|
+
break;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
1383
|
+
return paramAssoc;
|
|
1384
|
+
}
|
|
1385
|
+
buildRegExp() {
|
|
1386
|
+
let regexp = this.#root.buildRegExpStr();
|
|
1387
|
+
if (regexp === "") return [
|
|
1388
|
+
/^$/,
|
|
1389
|
+
[],
|
|
1390
|
+
[]
|
|
1391
|
+
];
|
|
1392
|
+
let captureIndex = 0;
|
|
1393
|
+
const indexReplacementMap = [];
|
|
1394
|
+
const paramReplacementMap = [];
|
|
1395
|
+
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
1396
|
+
if (handlerIndex !== void 0) {
|
|
1397
|
+
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
1398
|
+
return "$()";
|
|
1399
|
+
}
|
|
1400
|
+
if (paramIndex !== void 0) {
|
|
1401
|
+
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
1402
|
+
return "";
|
|
1403
|
+
}
|
|
1404
|
+
return "";
|
|
1405
|
+
});
|
|
1406
|
+
return [
|
|
1407
|
+
new RegExp(`^${regexp}`),
|
|
1408
|
+
indexReplacementMap,
|
|
1409
|
+
paramReplacementMap
|
|
1410
|
+
];
|
|
1411
|
+
}
|
|
1412
|
+
};
|
|
1413
|
+
//#endregion
|
|
1414
|
+
//#region node_modules/hono/dist/router/reg-exp-router/router.js
|
|
1415
|
+
var nullMatcher = [
|
|
1416
|
+
/^$/,
|
|
1417
|
+
[],
|
|
1418
|
+
/* @__PURE__ */ Object.create(null)
|
|
1419
|
+
];
|
|
1420
|
+
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1421
|
+
function buildWildcardRegExp(path) {
|
|
1422
|
+
return wildcardRegExpCache[path] ??= new RegExp(path === "*" ? "" : `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)")}$`);
|
|
1423
|
+
}
|
|
1424
|
+
function clearWildcardRegExpCache() {
|
|
1425
|
+
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1426
|
+
}
|
|
1427
|
+
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
1428
|
+
const trie = new Trie();
|
|
1429
|
+
const handlerData = [];
|
|
1430
|
+
if (routes.length === 0) return nullMatcher;
|
|
1431
|
+
const routesWithStaticPathFlag = routes.map((route) => [!/\*|\/:/.test(route[0]), ...route]).sort(([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length);
|
|
1432
|
+
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
1433
|
+
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
1434
|
+
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
1435
|
+
if (pathErrorCheckOnly) staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
1436
|
+
else j++;
|
|
1437
|
+
let paramAssoc;
|
|
1438
|
+
try {
|
|
1439
|
+
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
|
1440
|
+
} catch (e) {
|
|
1441
|
+
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
|
1442
|
+
}
|
|
1443
|
+
if (pathErrorCheckOnly) continue;
|
|
1444
|
+
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
1445
|
+
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
1446
|
+
paramCount -= 1;
|
|
1447
|
+
for (; paramCount >= 0; paramCount--) {
|
|
1448
|
+
const [key, value] = paramAssoc[paramCount];
|
|
1449
|
+
paramIndexMap[key] = value;
|
|
1450
|
+
}
|
|
1451
|
+
return [h, paramIndexMap];
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
1455
|
+
for (let i = 0, len = handlerData.length; i < len; i++) for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
|
|
1456
|
+
const map = handlerData[i][j]?.[1];
|
|
1457
|
+
if (!map) continue;
|
|
1458
|
+
const keys = Object.keys(map);
|
|
1459
|
+
for (let k = 0, len3 = keys.length; k < len3; k++) map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
1460
|
+
}
|
|
1461
|
+
const handlerMap = [];
|
|
1462
|
+
for (const i in indexReplacementMap) handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
1463
|
+
return [
|
|
1464
|
+
regexp,
|
|
1465
|
+
handlerMap,
|
|
1466
|
+
staticMap
|
|
1467
|
+
];
|
|
1468
|
+
}
|
|
1469
|
+
function findMiddleware(middleware, path) {
|
|
1470
|
+
if (!middleware) return;
|
|
1471
|
+
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) if (buildWildcardRegExp(k).test(path)) return [...middleware[k]];
|
|
1472
|
+
}
|
|
1473
|
+
var RegExpRouter = class {
|
|
1474
|
+
name = "RegExpRouter";
|
|
1475
|
+
#middleware;
|
|
1476
|
+
#routes;
|
|
1477
|
+
constructor() {
|
|
1478
|
+
this.#middleware = { ["ALL"]: /* @__PURE__ */ Object.create(null) };
|
|
1479
|
+
this.#routes = { ["ALL"]: /* @__PURE__ */ Object.create(null) };
|
|
1480
|
+
}
|
|
1481
|
+
add(method, path, handler) {
|
|
1482
|
+
const middleware = this.#middleware;
|
|
1483
|
+
const routes = this.#routes;
|
|
1484
|
+
if (!middleware || !routes) throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1485
|
+
if (!middleware[method]) [middleware, routes].forEach((handlerMap) => {
|
|
1486
|
+
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
1487
|
+
Object.keys(handlerMap["ALL"]).forEach((p) => {
|
|
1488
|
+
handlerMap[method][p] = [...handlerMap["ALL"][p]];
|
|
1489
|
+
});
|
|
1490
|
+
});
|
|
1491
|
+
if (path === "/*") path = "*";
|
|
1492
|
+
const paramCount = (path.match(/\/:/g) || []).length;
|
|
1493
|
+
if (/\*$/.test(path)) {
|
|
1494
|
+
const re = buildWildcardRegExp(path);
|
|
1495
|
+
if (method === "ALL") Object.keys(middleware).forEach((m) => {
|
|
1496
|
+
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware["ALL"], path) || [];
|
|
1497
|
+
});
|
|
1498
|
+
else middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware["ALL"], path) || [];
|
|
1499
|
+
Object.keys(middleware).forEach((m) => {
|
|
1500
|
+
if (method === "ALL" || method === m) Object.keys(middleware[m]).forEach((p) => {
|
|
1501
|
+
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
1502
|
+
});
|
|
1503
|
+
});
|
|
1504
|
+
Object.keys(routes).forEach((m) => {
|
|
1505
|
+
if (method === "ALL" || method === m) Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push([handler, paramCount]));
|
|
1506
|
+
});
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
const paths = checkOptionalParameter(path) || [path];
|
|
1510
|
+
for (let i = 0, len = paths.length; i < len; i++) {
|
|
1511
|
+
const path2 = paths[i];
|
|
1512
|
+
Object.keys(routes).forEach((m) => {
|
|
1513
|
+
if (method === "ALL" || method === m) {
|
|
1514
|
+
routes[m][path2] ||= [...findMiddleware(middleware[m], path2) || findMiddleware(middleware["ALL"], path2) || []];
|
|
1515
|
+
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
match = match;
|
|
1521
|
+
buildAllMatchers() {
|
|
1522
|
+
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1523
|
+
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
1524
|
+
matchers[method] ||= this.#buildMatcher(method);
|
|
1525
|
+
});
|
|
1526
|
+
this.#middleware = this.#routes = void 0;
|
|
1527
|
+
clearWildcardRegExpCache();
|
|
1528
|
+
return matchers;
|
|
1529
|
+
}
|
|
1530
|
+
#buildMatcher(method) {
|
|
1531
|
+
const routes = [];
|
|
1532
|
+
let hasOwnRoute = method === "ALL";
|
|
1533
|
+
[this.#middleware, this.#routes].forEach((r) => {
|
|
1534
|
+
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
1535
|
+
if (ownRoute.length !== 0) {
|
|
1536
|
+
hasOwnRoute ||= true;
|
|
1537
|
+
routes.push(...ownRoute);
|
|
1538
|
+
} else if (method !== "ALL") routes.push(...Object.keys(r["ALL"]).map((path) => [path, r["ALL"][path]]));
|
|
1539
|
+
});
|
|
1540
|
+
if (!hasOwnRoute) return null;
|
|
1541
|
+
else return buildMatcherFromPreprocessedRoutes(routes);
|
|
1542
|
+
}
|
|
1543
|
+
};
|
|
1544
|
+
//#endregion
|
|
1545
|
+
//#region node_modules/hono/dist/router/smart-router/router.js
|
|
1546
|
+
var SmartRouter = class {
|
|
1547
|
+
name = "SmartRouter";
|
|
1548
|
+
#routers = [];
|
|
1549
|
+
#routes = [];
|
|
1550
|
+
constructor(init) {
|
|
1551
|
+
this.#routers = init.routers;
|
|
1552
|
+
}
|
|
1553
|
+
add(method, path, handler) {
|
|
1554
|
+
if (!this.#routes) throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1555
|
+
this.#routes.push([
|
|
1556
|
+
method,
|
|
1557
|
+
path,
|
|
1558
|
+
handler
|
|
1559
|
+
]);
|
|
1560
|
+
}
|
|
1561
|
+
match(method, path) {
|
|
1562
|
+
if (!this.#routes) throw new Error("Fatal error");
|
|
1563
|
+
const routers = this.#routers;
|
|
1564
|
+
const routes = this.#routes;
|
|
1565
|
+
const len = routers.length;
|
|
1566
|
+
let i = 0;
|
|
1567
|
+
let res;
|
|
1568
|
+
for (; i < len; i++) {
|
|
1569
|
+
const router = routers[i];
|
|
1570
|
+
try {
|
|
1571
|
+
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) router.add(...routes[i2]);
|
|
1572
|
+
res = router.match(method, path);
|
|
1573
|
+
} catch (e) {
|
|
1574
|
+
if (e instanceof UnsupportedPathError) continue;
|
|
1575
|
+
throw e;
|
|
1576
|
+
}
|
|
1577
|
+
this.match = router.match.bind(router);
|
|
1578
|
+
this.#routers = [router];
|
|
1579
|
+
this.#routes = void 0;
|
|
1580
|
+
break;
|
|
1581
|
+
}
|
|
1582
|
+
if (i === len) throw new Error("Fatal error");
|
|
1583
|
+
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
1584
|
+
return res;
|
|
1585
|
+
}
|
|
1586
|
+
get activeRouter() {
|
|
1587
|
+
if (this.#routes || this.#routers.length !== 1) throw new Error("No active router has been determined yet.");
|
|
1588
|
+
return this.#routers[0];
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
//#endregion
|
|
1592
|
+
//#region node_modules/hono/dist/router/trie-router/node.js
|
|
1593
|
+
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
1594
|
+
var hasChildren = (children) => {
|
|
1595
|
+
for (const _ in children) return true;
|
|
1596
|
+
return false;
|
|
1597
|
+
};
|
|
1598
|
+
var Node = class _Node {
|
|
1599
|
+
#methods;
|
|
1600
|
+
#children;
|
|
1601
|
+
#patterns;
|
|
1602
|
+
#order = 0;
|
|
1603
|
+
#params = emptyParams;
|
|
1604
|
+
constructor(method, handler, children) {
|
|
1605
|
+
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
1606
|
+
this.#methods = [];
|
|
1607
|
+
if (method && handler) {
|
|
1608
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
1609
|
+
m[method] = {
|
|
1610
|
+
handler,
|
|
1611
|
+
possibleKeys: [],
|
|
1612
|
+
score: 0
|
|
1613
|
+
};
|
|
1614
|
+
this.#methods = [m];
|
|
1615
|
+
}
|
|
1616
|
+
this.#patterns = [];
|
|
1617
|
+
}
|
|
1618
|
+
insert(method, path, handler) {
|
|
1619
|
+
this.#order = ++this.#order;
|
|
1620
|
+
let curNode = this;
|
|
1621
|
+
const parts = splitRoutingPath(path);
|
|
1622
|
+
const possibleKeys = [];
|
|
1623
|
+
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1624
|
+
const p = parts[i];
|
|
1625
|
+
const nextP = parts[i + 1];
|
|
1626
|
+
const pattern = getPattern(p, nextP);
|
|
1627
|
+
const key = Array.isArray(pattern) ? pattern[0] : p;
|
|
1628
|
+
if (key in curNode.#children) {
|
|
1629
|
+
curNode = curNode.#children[key];
|
|
1630
|
+
if (pattern) possibleKeys.push(pattern[1]);
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
curNode.#children[key] = new _Node();
|
|
1634
|
+
if (pattern) {
|
|
1635
|
+
curNode.#patterns.push(pattern);
|
|
1636
|
+
possibleKeys.push(pattern[1]);
|
|
1637
|
+
}
|
|
1638
|
+
curNode = curNode.#children[key];
|
|
1639
|
+
}
|
|
1640
|
+
curNode.#methods.push({ [method]: {
|
|
1641
|
+
handler,
|
|
1642
|
+
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
1643
|
+
score: this.#order
|
|
1644
|
+
} });
|
|
1645
|
+
return curNode;
|
|
1646
|
+
}
|
|
1647
|
+
#pushHandlerSets(handlerSets, node, method, nodeParams, params) {
|
|
1648
|
+
for (let i = 0, len = node.#methods.length; i < len; i++) {
|
|
1649
|
+
const m = node.#methods[i];
|
|
1650
|
+
const handlerSet = m[method] || m["ALL"];
|
|
1651
|
+
const processedSet = {};
|
|
1652
|
+
if (handlerSet !== void 0) {
|
|
1653
|
+
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
1654
|
+
handlerSets.push(handlerSet);
|
|
1655
|
+
if (nodeParams !== emptyParams || params && params !== emptyParams) for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
1656
|
+
const key = handlerSet.possibleKeys[i2];
|
|
1657
|
+
const processed = processedSet[handlerSet.score];
|
|
1658
|
+
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
1659
|
+
processedSet[handlerSet.score] = true;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
search(method, path) {
|
|
1665
|
+
const handlerSets = [];
|
|
1666
|
+
this.#params = emptyParams;
|
|
1667
|
+
let curNodes = [this];
|
|
1668
|
+
const parts = splitPath(path);
|
|
1669
|
+
const curNodesQueue = [];
|
|
1670
|
+
const len = parts.length;
|
|
1671
|
+
let partOffsets = null;
|
|
1672
|
+
for (let i = 0; i < len; i++) {
|
|
1673
|
+
const part = parts[i];
|
|
1674
|
+
const isLast = i === len - 1;
|
|
1675
|
+
const tempNodes = [];
|
|
1676
|
+
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
1677
|
+
const node = curNodes[j];
|
|
1678
|
+
const nextNode = node.#children[part];
|
|
1679
|
+
if (nextNode) {
|
|
1680
|
+
nextNode.#params = node.#params;
|
|
1681
|
+
if (isLast) {
|
|
1682
|
+
if (nextNode.#children["*"]) this.#pushHandlerSets(handlerSets, nextNode.#children["*"], method, node.#params);
|
|
1683
|
+
this.#pushHandlerSets(handlerSets, nextNode, method, node.#params);
|
|
1684
|
+
} else tempNodes.push(nextNode);
|
|
1685
|
+
}
|
|
1686
|
+
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
|
|
1687
|
+
const pattern = node.#patterns[k];
|
|
1688
|
+
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
1689
|
+
if (pattern === "*") {
|
|
1690
|
+
const astNode = node.#children["*"];
|
|
1691
|
+
if (astNode) {
|
|
1692
|
+
this.#pushHandlerSets(handlerSets, astNode, method, node.#params);
|
|
1693
|
+
astNode.#params = params;
|
|
1694
|
+
tempNodes.push(astNode);
|
|
1695
|
+
}
|
|
1696
|
+
continue;
|
|
1697
|
+
}
|
|
1698
|
+
const [key, name, matcher] = pattern;
|
|
1699
|
+
if (!part && !(matcher instanceof RegExp)) continue;
|
|
1700
|
+
const child = node.#children[key];
|
|
1701
|
+
if (matcher instanceof RegExp) {
|
|
1702
|
+
if (partOffsets === null) {
|
|
1703
|
+
partOffsets = new Array(len);
|
|
1704
|
+
let offset = path[0] === "/" ? 1 : 0;
|
|
1705
|
+
for (let p = 0; p < len; p++) {
|
|
1706
|
+
partOffsets[p] = offset;
|
|
1707
|
+
offset += parts[p].length + 1;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
const restPathString = path.substring(partOffsets[i]);
|
|
1711
|
+
const m = matcher.exec(restPathString);
|
|
1712
|
+
if (m) {
|
|
1713
|
+
params[name] = m[0];
|
|
1714
|
+
this.#pushHandlerSets(handlerSets, child, method, node.#params, params);
|
|
1715
|
+
if (hasChildren(child.#children)) {
|
|
1716
|
+
child.#params = params;
|
|
1717
|
+
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
1718
|
+
(curNodesQueue[componentCount] ||= []).push(child);
|
|
1719
|
+
}
|
|
1720
|
+
continue;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
if (matcher === true || matcher.test(part)) {
|
|
1724
|
+
params[name] = part;
|
|
1725
|
+
if (isLast) {
|
|
1726
|
+
this.#pushHandlerSets(handlerSets, child, method, params, node.#params);
|
|
1727
|
+
if (child.#children["*"]) this.#pushHandlerSets(handlerSets, child.#children["*"], method, params, node.#params);
|
|
1728
|
+
} else {
|
|
1729
|
+
child.#params = params;
|
|
1730
|
+
tempNodes.push(child);
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
const shifted = curNodesQueue.shift();
|
|
1736
|
+
curNodes = shifted ? tempNodes.concat(shifted) : tempNodes;
|
|
1737
|
+
}
|
|
1738
|
+
if (handlerSets.length > 1) handlerSets.sort((a, b) => {
|
|
1739
|
+
return a.score - b.score;
|
|
1740
|
+
});
|
|
1741
|
+
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
//#endregion
|
|
1745
|
+
//#region node_modules/hono/dist/router/trie-router/router.js
|
|
1746
|
+
var TrieRouter = class {
|
|
1747
|
+
name = "TrieRouter";
|
|
1748
|
+
#node;
|
|
1749
|
+
constructor() {
|
|
1750
|
+
this.#node = new Node();
|
|
1751
|
+
}
|
|
1752
|
+
add(method, path, handler) {
|
|
1753
|
+
const results = checkOptionalParameter(path);
|
|
1754
|
+
if (results) {
|
|
1755
|
+
for (let i = 0, len = results.length; i < len; i++) this.#node.insert(method, results[i], handler);
|
|
1756
|
+
return;
|
|
1757
|
+
}
|
|
1758
|
+
this.#node.insert(method, path, handler);
|
|
1759
|
+
}
|
|
1760
|
+
match(method, path) {
|
|
1761
|
+
return this.#node.search(method, path);
|
|
1762
|
+
}
|
|
1763
|
+
};
|
|
1764
|
+
//#endregion
|
|
1765
|
+
//#region node_modules/hono/dist/hono.js
|
|
1766
|
+
var Hono = class extends Hono$1 {
|
|
1767
|
+
/**
|
|
1768
|
+
* Creates an instance of the Hono class.
|
|
1769
|
+
*
|
|
1770
|
+
* @param options - Optional configuration options for the Hono instance.
|
|
1771
|
+
*/
|
|
1772
|
+
constructor(options = {}) {
|
|
1773
|
+
super(options);
|
|
1774
|
+
this.router = options.router ?? new SmartRouter({ routers: [new RegExpRouter(), new TrieRouter()] });
|
|
1775
|
+
}
|
|
1776
|
+
};
|
|
1777
|
+
//#endregion
|
|
1778
|
+
//#region node_modules/@hono/node-server/dist/index.mjs
|
|
1779
|
+
var RequestError = class extends Error {
|
|
1780
|
+
constructor(message, options) {
|
|
1781
|
+
super(message, options);
|
|
1782
|
+
this.name = "RequestError";
|
|
1783
|
+
}
|
|
1784
|
+
};
|
|
1785
|
+
var toRequestError = (e) => {
|
|
1786
|
+
if (e instanceof RequestError) return e;
|
|
1787
|
+
return new RequestError(e.message, { cause: e });
|
|
1788
|
+
};
|
|
1789
|
+
var GlobalRequest = global.Request;
|
|
1790
|
+
var Request$1 = class extends GlobalRequest {
|
|
1791
|
+
constructor(input, options) {
|
|
1792
|
+
if (typeof input === "object" && getRequestCache in input) input = input[getRequestCache]();
|
|
1793
|
+
if (typeof options?.body?.getReader !== "undefined") options.duplex ??= "half";
|
|
1794
|
+
super(input, options);
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
var newHeadersFromIncoming = (incoming) => {
|
|
1798
|
+
const headerRecord = [];
|
|
1799
|
+
const rawHeaders = incoming.rawHeaders;
|
|
1800
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
1801
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
1802
|
+
if (key.charCodeAt(0) !== 58) headerRecord.push([key, value]);
|
|
1803
|
+
}
|
|
1804
|
+
return new Headers(headerRecord);
|
|
1805
|
+
};
|
|
1806
|
+
var wrapBodyStream = Symbol("wrapBodyStream");
|
|
1807
|
+
var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
|
|
1808
|
+
const init = {
|
|
1809
|
+
method,
|
|
1810
|
+
headers,
|
|
1811
|
+
signal: abortController.signal
|
|
1812
|
+
};
|
|
1813
|
+
if (method === "TRACE") {
|
|
1814
|
+
init.method = "GET";
|
|
1815
|
+
const req = new Request$1(url, init);
|
|
1816
|
+
Object.defineProperty(req, "method", { get() {
|
|
1817
|
+
return "TRACE";
|
|
1818
|
+
} });
|
|
1819
|
+
return req;
|
|
1820
|
+
}
|
|
1821
|
+
if (!(method === "GET" || method === "HEAD")) if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) init.body = new ReadableStream({ start(controller) {
|
|
1822
|
+
controller.enqueue(incoming.rawBody);
|
|
1823
|
+
controller.close();
|
|
1824
|
+
} });
|
|
1825
|
+
else if (incoming[wrapBodyStream]) {
|
|
1826
|
+
let reader;
|
|
1827
|
+
init.body = new ReadableStream({ async pull(controller) {
|
|
1828
|
+
try {
|
|
1829
|
+
reader ||= Readable.toWeb(incoming).getReader();
|
|
1830
|
+
const { done, value } = await reader.read();
|
|
1831
|
+
if (done) controller.close();
|
|
1832
|
+
else controller.enqueue(value);
|
|
1833
|
+
} catch (error) {
|
|
1834
|
+
controller.error(error);
|
|
1835
|
+
}
|
|
1836
|
+
} });
|
|
1837
|
+
} else init.body = Readable.toWeb(incoming);
|
|
1838
|
+
return new Request$1(url, init);
|
|
1839
|
+
};
|
|
1840
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
1841
|
+
var requestCache = Symbol("requestCache");
|
|
1842
|
+
var incomingKey = Symbol("incomingKey");
|
|
1843
|
+
var urlKey = Symbol("urlKey");
|
|
1844
|
+
var headersKey = Symbol("headersKey");
|
|
1845
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
1846
|
+
var requestPrototype = {
|
|
1847
|
+
get method() {
|
|
1848
|
+
return this[incomingKey].method || "GET";
|
|
1849
|
+
},
|
|
1850
|
+
get url() {
|
|
1851
|
+
return this[urlKey];
|
|
1852
|
+
},
|
|
1853
|
+
get headers() {
|
|
1854
|
+
return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
|
|
1855
|
+
},
|
|
1856
|
+
[Symbol("getAbortController")]() {
|
|
1857
|
+
this[getRequestCache]();
|
|
1858
|
+
return this[abortControllerKey];
|
|
1859
|
+
},
|
|
1860
|
+
[getRequestCache]() {
|
|
1861
|
+
this[abortControllerKey] ||= new AbortController();
|
|
1862
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this[urlKey], this.headers, this[incomingKey], this[abortControllerKey]);
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
[
|
|
1866
|
+
"body",
|
|
1867
|
+
"bodyUsed",
|
|
1868
|
+
"cache",
|
|
1869
|
+
"credentials",
|
|
1870
|
+
"destination",
|
|
1871
|
+
"integrity",
|
|
1872
|
+
"mode",
|
|
1873
|
+
"redirect",
|
|
1874
|
+
"referrer",
|
|
1875
|
+
"referrerPolicy",
|
|
1876
|
+
"signal",
|
|
1877
|
+
"keepalive"
|
|
1878
|
+
].forEach((k) => {
|
|
1879
|
+
Object.defineProperty(requestPrototype, k, { get() {
|
|
1880
|
+
return this[getRequestCache]()[k];
|
|
1881
|
+
} });
|
|
1882
|
+
});
|
|
1883
|
+
[
|
|
1884
|
+
"arrayBuffer",
|
|
1885
|
+
"blob",
|
|
1886
|
+
"clone",
|
|
1887
|
+
"formData",
|
|
1888
|
+
"json",
|
|
1889
|
+
"text"
|
|
1890
|
+
].forEach((k) => {
|
|
1891
|
+
Object.defineProperty(requestPrototype, k, { value: function() {
|
|
1892
|
+
return this[getRequestCache]()[k]();
|
|
1893
|
+
} });
|
|
1894
|
+
});
|
|
1895
|
+
Object.setPrototypeOf(requestPrototype, Request$1.prototype);
|
|
1896
|
+
var newRequest = (incoming, defaultHostname) => {
|
|
1897
|
+
const req = Object.create(requestPrototype);
|
|
1898
|
+
req[incomingKey] = incoming;
|
|
1899
|
+
const incomingUrl = incoming.url || "";
|
|
1900
|
+
if (incomingUrl[0] !== "/" && (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
|
|
1901
|
+
if (incoming instanceof Http2ServerRequest) throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
|
|
1902
|
+
try {
|
|
1903
|
+
req[urlKey] = new URL(incomingUrl).href;
|
|
1904
|
+
} catch (e) {
|
|
1905
|
+
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
1906
|
+
}
|
|
1907
|
+
return req;
|
|
1908
|
+
}
|
|
1909
|
+
const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
|
|
1910
|
+
if (!host) throw new RequestError("Missing host header");
|
|
1911
|
+
let scheme;
|
|
1912
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
1913
|
+
scheme = incoming.scheme;
|
|
1914
|
+
if (!(scheme === "http" || scheme === "https")) throw new RequestError("Unsupported scheme");
|
|
1915
|
+
} else scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
|
|
1916
|
+
const url = new URL(`${scheme}://${host}${incomingUrl}`);
|
|
1917
|
+
if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) throw new RequestError("Invalid host header");
|
|
1918
|
+
req[urlKey] = url.href;
|
|
1919
|
+
return req;
|
|
1920
|
+
};
|
|
1921
|
+
var responseCache = Symbol("responseCache");
|
|
1922
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
1923
|
+
var cacheKey = Symbol("cache");
|
|
1924
|
+
var GlobalResponse = global.Response;
|
|
1925
|
+
var Response2 = class _Response {
|
|
1926
|
+
#body;
|
|
1927
|
+
#init;
|
|
1928
|
+
[getResponseCache]() {
|
|
1929
|
+
delete this[cacheKey];
|
|
1930
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
1931
|
+
}
|
|
1932
|
+
constructor(body, init) {
|
|
1933
|
+
let headers;
|
|
1934
|
+
this.#body = body;
|
|
1935
|
+
if (init instanceof _Response) {
|
|
1936
|
+
const cachedGlobalResponse = init[responseCache];
|
|
1937
|
+
if (cachedGlobalResponse) {
|
|
1938
|
+
this.#init = cachedGlobalResponse;
|
|
1939
|
+
this[getResponseCache]();
|
|
1940
|
+
return;
|
|
1941
|
+
} else {
|
|
1942
|
+
this.#init = init.#init;
|
|
1943
|
+
headers = new Headers(init.#init.headers);
|
|
1944
|
+
}
|
|
1945
|
+
} else this.#init = init;
|
|
1946
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) this[cacheKey] = [
|
|
1947
|
+
init?.status || 200,
|
|
1948
|
+
body,
|
|
1949
|
+
headers || init?.headers
|
|
1950
|
+
];
|
|
1951
|
+
}
|
|
1952
|
+
get headers() {
|
|
1953
|
+
const cache = this[cacheKey];
|
|
1954
|
+
if (cache) {
|
|
1955
|
+
if (!(cache[2] instanceof Headers)) cache[2] = new Headers(cache[2] || { "content-type": "text/plain; charset=UTF-8" });
|
|
1956
|
+
return cache[2];
|
|
1957
|
+
}
|
|
1958
|
+
return this[getResponseCache]().headers;
|
|
1959
|
+
}
|
|
1960
|
+
get status() {
|
|
1961
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
1962
|
+
}
|
|
1963
|
+
get ok() {
|
|
1964
|
+
const status = this.status;
|
|
1965
|
+
return status >= 200 && status < 300;
|
|
1966
|
+
}
|
|
1967
|
+
};
|
|
1968
|
+
[
|
|
1969
|
+
"body",
|
|
1970
|
+
"bodyUsed",
|
|
1971
|
+
"redirected",
|
|
1972
|
+
"statusText",
|
|
1973
|
+
"trailers",
|
|
1974
|
+
"type",
|
|
1975
|
+
"url"
|
|
1976
|
+
].forEach((k) => {
|
|
1977
|
+
Object.defineProperty(Response2.prototype, k, { get() {
|
|
1978
|
+
return this[getResponseCache]()[k];
|
|
1979
|
+
} });
|
|
1980
|
+
});
|
|
1981
|
+
[
|
|
1982
|
+
"arrayBuffer",
|
|
1983
|
+
"blob",
|
|
1984
|
+
"clone",
|
|
1985
|
+
"formData",
|
|
1986
|
+
"json",
|
|
1987
|
+
"text"
|
|
1988
|
+
].forEach((k) => {
|
|
1989
|
+
Object.defineProperty(Response2.prototype, k, { value: function() {
|
|
1990
|
+
return this[getResponseCache]()[k]();
|
|
1991
|
+
} });
|
|
1992
|
+
});
|
|
1993
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
1994
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
1995
|
+
async function readWithoutBlocking(readPromise) {
|
|
1996
|
+
return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
|
|
1997
|
+
}
|
|
1998
|
+
function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
|
|
1999
|
+
const cancel = (error) => {
|
|
2000
|
+
reader.cancel(error).catch(() => {});
|
|
2001
|
+
};
|
|
2002
|
+
writable.on("close", cancel);
|
|
2003
|
+
writable.on("error", cancel);
|
|
2004
|
+
(currentReadPromise ?? reader.read()).then(flow, handleStreamError);
|
|
2005
|
+
return reader.closed.finally(() => {
|
|
2006
|
+
writable.off("close", cancel);
|
|
2007
|
+
writable.off("error", cancel);
|
|
2008
|
+
});
|
|
2009
|
+
function handleStreamError(error) {
|
|
2010
|
+
if (error) writable.destroy(error);
|
|
2011
|
+
}
|
|
2012
|
+
function onDrain() {
|
|
2013
|
+
reader.read().then(flow, handleStreamError);
|
|
2014
|
+
}
|
|
2015
|
+
function flow({ done, value }) {
|
|
2016
|
+
try {
|
|
2017
|
+
if (done) writable.end();
|
|
2018
|
+
else if (!writable.write(value)) writable.once("drain", onDrain);
|
|
2019
|
+
else return reader.read().then(flow, handleStreamError);
|
|
2020
|
+
} catch (e) {
|
|
2021
|
+
handleStreamError(e);
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
function writeFromReadableStream(stream, writable) {
|
|
2026
|
+
if (stream.locked) throw new TypeError("ReadableStream is locked.");
|
|
2027
|
+
else if (writable.destroyed) return;
|
|
2028
|
+
return writeFromReadableStreamDefaultReader(stream.getReader(), writable);
|
|
2029
|
+
}
|
|
2030
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
2031
|
+
const res = {};
|
|
2032
|
+
if (!(headers instanceof Headers)) headers = new Headers(headers ?? void 0);
|
|
2033
|
+
const cookies = [];
|
|
2034
|
+
for (const [k, v] of headers) if (k === "set-cookie") cookies.push(v);
|
|
2035
|
+
else res[k] = v;
|
|
2036
|
+
if (cookies.length > 0) res["set-cookie"] = cookies;
|
|
2037
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
2038
|
+
return res;
|
|
2039
|
+
};
|
|
2040
|
+
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
2041
|
+
if (typeof global.crypto === "undefined") global.crypto = crypto;
|
|
2042
|
+
var outgoingEnded = Symbol("outgoingEnded");
|
|
2043
|
+
var handleRequestError = () => new Response(null, { status: 400 });
|
|
2044
|
+
var handleFetchError = (e) => new Response(null, { status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500 });
|
|
2045
|
+
var handleResponseError = (e, outgoing) => {
|
|
2046
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
2047
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") console.info("The user aborted a request.");
|
|
2048
|
+
else {
|
|
2049
|
+
console.error(e);
|
|
2050
|
+
if (!outgoing.headersSent) outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
2051
|
+
outgoing.end(`Error: ${err.message}`);
|
|
2052
|
+
outgoing.destroy(err);
|
|
2053
|
+
}
|
|
2054
|
+
};
|
|
2055
|
+
var flushHeaders = (outgoing) => {
|
|
2056
|
+
if ("flushHeaders" in outgoing && outgoing.writable) outgoing.flushHeaders();
|
|
2057
|
+
};
|
|
2058
|
+
var responseViaCache = async (res, outgoing) => {
|
|
2059
|
+
let [status, body, header] = res[cacheKey];
|
|
2060
|
+
let hasContentLength = false;
|
|
2061
|
+
if (!header) header = { "content-type": "text/plain; charset=UTF-8" };
|
|
2062
|
+
else if (header instanceof Headers) {
|
|
2063
|
+
hasContentLength = header.has("content-length");
|
|
2064
|
+
header = buildOutgoingHttpHeaders(header);
|
|
2065
|
+
} else if (Array.isArray(header)) {
|
|
2066
|
+
const headerObj = new Headers(header);
|
|
2067
|
+
hasContentLength = headerObj.has("content-length");
|
|
2068
|
+
header = buildOutgoingHttpHeaders(headerObj);
|
|
2069
|
+
} else for (const key in header) if (key.length === 14 && key.toLowerCase() === "content-length") {
|
|
2070
|
+
hasContentLength = true;
|
|
2071
|
+
break;
|
|
2072
|
+
}
|
|
2073
|
+
if (!hasContentLength) {
|
|
2074
|
+
if (typeof body === "string") header["Content-Length"] = Buffer.byteLength(body);
|
|
2075
|
+
else if (body instanceof Uint8Array) header["Content-Length"] = body.byteLength;
|
|
2076
|
+
else if (body instanceof Blob) header["Content-Length"] = body.size;
|
|
2077
|
+
}
|
|
2078
|
+
outgoing.writeHead(status, header);
|
|
2079
|
+
if (typeof body === "string" || body instanceof Uint8Array) outgoing.end(body);
|
|
2080
|
+
else if (body instanceof Blob) outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
2081
|
+
else {
|
|
2082
|
+
flushHeaders(outgoing);
|
|
2083
|
+
await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing));
|
|
2084
|
+
}
|
|
2085
|
+
outgoing[outgoingEnded]?.();
|
|
2086
|
+
};
|
|
2087
|
+
var isPromise = (res) => typeof res.then === "function";
|
|
2088
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
2089
|
+
if (isPromise(res)) if (options.errorHandler) try {
|
|
2090
|
+
res = await res;
|
|
2091
|
+
} catch (err) {
|
|
2092
|
+
const errRes = await options.errorHandler(err);
|
|
2093
|
+
if (!errRes) return;
|
|
2094
|
+
res = errRes;
|
|
2095
|
+
}
|
|
2096
|
+
else res = await res.catch(handleFetchError);
|
|
2097
|
+
if (cacheKey in res) return responseViaCache(res, outgoing);
|
|
2098
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
2099
|
+
if (res.body) {
|
|
2100
|
+
const reader = res.body.getReader();
|
|
2101
|
+
const values = [];
|
|
2102
|
+
let done = false;
|
|
2103
|
+
let currentReadPromise = void 0;
|
|
2104
|
+
if (resHeaderRecord["transfer-encoding"] !== "chunked") {
|
|
2105
|
+
let maxReadCount = 2;
|
|
2106
|
+
for (let i = 0; i < maxReadCount; i++) {
|
|
2107
|
+
currentReadPromise ||= reader.read();
|
|
2108
|
+
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
2109
|
+
console.error(e);
|
|
2110
|
+
done = true;
|
|
2111
|
+
});
|
|
2112
|
+
if (!chunk) {
|
|
2113
|
+
if (i === 1) {
|
|
2114
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
2115
|
+
maxReadCount = 3;
|
|
2116
|
+
continue;
|
|
2117
|
+
}
|
|
2118
|
+
break;
|
|
2119
|
+
}
|
|
2120
|
+
currentReadPromise = void 0;
|
|
2121
|
+
if (chunk.value) values.push(chunk.value);
|
|
2122
|
+
if (chunk.done) {
|
|
2123
|
+
done = true;
|
|
2124
|
+
break;
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
if (done && !("content-length" in resHeaderRecord)) resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
|
|
2128
|
+
}
|
|
2129
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
2130
|
+
values.forEach((value) => {
|
|
2131
|
+
outgoing.write(value);
|
|
2132
|
+
});
|
|
2133
|
+
if (done) outgoing.end();
|
|
2134
|
+
else {
|
|
2135
|
+
if (values.length === 0) flushHeaders(outgoing);
|
|
2136
|
+
await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
|
|
2137
|
+
}
|
|
2138
|
+
} else if (resHeaderRecord[X_ALREADY_SENT]) {} else {
|
|
2139
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
2140
|
+
outgoing.end();
|
|
2141
|
+
}
|
|
2142
|
+
outgoing[outgoingEnded]?.();
|
|
2143
|
+
};
|
|
2144
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
2145
|
+
const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
|
|
2146
|
+
if (options.overrideGlobalObjects !== false && global.Request !== Request$1) {
|
|
2147
|
+
Object.defineProperty(global, "Request", { value: Request$1 });
|
|
2148
|
+
Object.defineProperty(global, "Response", { value: Response2 });
|
|
2149
|
+
}
|
|
2150
|
+
return async (incoming, outgoing) => {
|
|
2151
|
+
let res, req;
|
|
2152
|
+
try {
|
|
2153
|
+
req = newRequest(incoming, options.hostname);
|
|
2154
|
+
let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
|
|
2155
|
+
if (!incomingEnded) {
|
|
2156
|
+
incoming[wrapBodyStream] = true;
|
|
2157
|
+
incoming.on("end", () => {
|
|
2158
|
+
incomingEnded = true;
|
|
2159
|
+
});
|
|
2160
|
+
if (incoming instanceof Http2ServerRequest) outgoing[outgoingEnded] = () => {
|
|
2161
|
+
if (!incomingEnded) setTimeout(() => {
|
|
2162
|
+
if (!incomingEnded) setTimeout(() => {
|
|
2163
|
+
incoming.destroy();
|
|
2164
|
+
outgoing.destroy();
|
|
2165
|
+
});
|
|
2166
|
+
});
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
outgoing.on("close", () => {
|
|
2170
|
+
if (req[abortControllerKey]) {
|
|
2171
|
+
if (incoming.errored) req[abortControllerKey].abort(incoming.errored.toString());
|
|
2172
|
+
else if (!outgoing.writableFinished) req[abortControllerKey].abort("Client connection prematurely closed.");
|
|
2173
|
+
}
|
|
2174
|
+
if (!incomingEnded) setTimeout(() => {
|
|
2175
|
+
if (!incomingEnded) setTimeout(() => {
|
|
2176
|
+
incoming.destroy();
|
|
2177
|
+
});
|
|
2178
|
+
});
|
|
2179
|
+
});
|
|
2180
|
+
res = fetchCallback(req, {
|
|
2181
|
+
incoming,
|
|
2182
|
+
outgoing
|
|
2183
|
+
});
|
|
2184
|
+
if (cacheKey in res) return responseViaCache(res, outgoing);
|
|
2185
|
+
} catch (e) {
|
|
2186
|
+
if (!res) if (options.errorHandler) {
|
|
2187
|
+
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
2188
|
+
if (!res) return;
|
|
2189
|
+
} else if (!req) res = handleRequestError();
|
|
2190
|
+
else res = handleFetchError(e);
|
|
2191
|
+
else return handleResponseError(e, outgoing);
|
|
2192
|
+
}
|
|
2193
|
+
try {
|
|
2194
|
+
return await responseViaResponseObject(res, outgoing, options);
|
|
2195
|
+
} catch (e) {
|
|
2196
|
+
return handleResponseError(e, outgoing);
|
|
2197
|
+
}
|
|
2198
|
+
};
|
|
2199
|
+
};
|
|
2200
|
+
var createAdaptorServer = (options) => {
|
|
2201
|
+
const fetchCallback = options.fetch;
|
|
2202
|
+
const requestListener = getRequestListener(fetchCallback, {
|
|
2203
|
+
hostname: options.hostname,
|
|
2204
|
+
overrideGlobalObjects: options.overrideGlobalObjects,
|
|
2205
|
+
autoCleanupIncoming: options.autoCleanupIncoming
|
|
2206
|
+
});
|
|
2207
|
+
return (options.createServer || createServer)(options.serverOptions || {}, requestListener);
|
|
2208
|
+
};
|
|
2209
|
+
var serve = (options, listeningListener) => {
|
|
2210
|
+
const server = createAdaptorServer(options);
|
|
2211
|
+
server.listen(options?.port ?? 3e3, options.hostname, () => {
|
|
2212
|
+
const serverInfo = server.address();
|
|
2213
|
+
listeningListener && listeningListener(serverInfo);
|
|
2214
|
+
});
|
|
2215
|
+
return server;
|
|
2216
|
+
};
|
|
2217
|
+
//#endregion
|
|
2218
|
+
//#region node_modules/hono/dist/utils/mime.js
|
|
2219
|
+
var getMimeType = (filename, mimes = baseMimes) => {
|
|
2220
|
+
const match = filename.match(/\.([a-zA-Z0-9]+?)$/);
|
|
2221
|
+
if (!match) return;
|
|
2222
|
+
let mimeType = mimes[match[1].toLowerCase()];
|
|
2223
|
+
if (mimeType && mimeType.startsWith("text")) mimeType += "; charset=utf-8";
|
|
2224
|
+
return mimeType;
|
|
2225
|
+
};
|
|
2226
|
+
var baseMimes = {
|
|
2227
|
+
aac: "audio/aac",
|
|
2228
|
+
avi: "video/x-msvideo",
|
|
2229
|
+
avif: "image/avif",
|
|
2230
|
+
av1: "video/av1",
|
|
2231
|
+
bin: "application/octet-stream",
|
|
2232
|
+
bmp: "image/bmp",
|
|
2233
|
+
css: "text/css",
|
|
2234
|
+
csv: "text/csv",
|
|
2235
|
+
eot: "application/vnd.ms-fontobject",
|
|
2236
|
+
epub: "application/epub+zip",
|
|
2237
|
+
gif: "image/gif",
|
|
2238
|
+
gz: "application/gzip",
|
|
2239
|
+
htm: "text/html",
|
|
2240
|
+
html: "text/html",
|
|
2241
|
+
ico: "image/x-icon",
|
|
2242
|
+
ics: "text/calendar",
|
|
2243
|
+
jpeg: "image/jpeg",
|
|
2244
|
+
jpg: "image/jpeg",
|
|
2245
|
+
js: "text/javascript",
|
|
2246
|
+
json: "application/json",
|
|
2247
|
+
jsonld: "application/ld+json",
|
|
2248
|
+
map: "application/json",
|
|
2249
|
+
mid: "audio/x-midi",
|
|
2250
|
+
midi: "audio/x-midi",
|
|
2251
|
+
mjs: "text/javascript",
|
|
2252
|
+
mp3: "audio/mpeg",
|
|
2253
|
+
mp4: "video/mp4",
|
|
2254
|
+
mpeg: "video/mpeg",
|
|
2255
|
+
oga: "audio/ogg",
|
|
2256
|
+
ogv: "video/ogg",
|
|
2257
|
+
ogx: "application/ogg",
|
|
2258
|
+
opus: "audio/opus",
|
|
2259
|
+
otf: "font/otf",
|
|
2260
|
+
pdf: "application/pdf",
|
|
2261
|
+
png: "image/png",
|
|
2262
|
+
rtf: "application/rtf",
|
|
2263
|
+
svg: "image/svg+xml",
|
|
2264
|
+
tif: "image/tiff",
|
|
2265
|
+
tiff: "image/tiff",
|
|
2266
|
+
ts: "video/mp2t",
|
|
2267
|
+
ttf: "font/ttf",
|
|
2268
|
+
txt: "text/plain",
|
|
2269
|
+
wasm: "application/wasm",
|
|
2270
|
+
webm: "video/webm",
|
|
2271
|
+
weba: "audio/webm",
|
|
2272
|
+
webmanifest: "application/manifest+json",
|
|
2273
|
+
webp: "image/webp",
|
|
2274
|
+
woff: "font/woff",
|
|
2275
|
+
woff2: "font/woff2",
|
|
2276
|
+
xhtml: "application/xhtml+xml",
|
|
2277
|
+
xml: "application/xml",
|
|
2278
|
+
zip: "application/zip",
|
|
2279
|
+
"3gp": "video/3gpp",
|
|
2280
|
+
"3g2": "video/3gpp2",
|
|
2281
|
+
gltf: "model/gltf+json",
|
|
2282
|
+
glb: "model/gltf-binary"
|
|
2283
|
+
};
|
|
2284
|
+
//#endregion
|
|
2285
|
+
//#region node_modules/@hono/node-server/dist/serve-static.mjs
|
|
2286
|
+
var COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\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;
|
|
2287
|
+
var ENCODINGS = {
|
|
2288
|
+
br: ".br",
|
|
2289
|
+
zstd: ".zst",
|
|
2290
|
+
gzip: ".gz"
|
|
2291
|
+
};
|
|
2292
|
+
var ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS);
|
|
2293
|
+
var pr54206Applied = () => {
|
|
2294
|
+
const [major, minor] = versions.node.split(".").map((component) => parseInt(component));
|
|
2295
|
+
return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18;
|
|
2296
|
+
};
|
|
2297
|
+
var useReadableToWeb = pr54206Applied();
|
|
2298
|
+
var createStreamBody = (stream) => {
|
|
2299
|
+
if (useReadableToWeb) return Readable.toWeb(stream);
|
|
2300
|
+
return new ReadableStream({
|
|
2301
|
+
start(controller) {
|
|
2302
|
+
stream.on("data", (chunk) => {
|
|
2303
|
+
controller.enqueue(chunk);
|
|
2304
|
+
});
|
|
2305
|
+
stream.on("error", (err) => {
|
|
2306
|
+
controller.error(err);
|
|
2307
|
+
});
|
|
2308
|
+
stream.on("end", () => {
|
|
2309
|
+
controller.close();
|
|
2310
|
+
});
|
|
2311
|
+
},
|
|
2312
|
+
cancel() {
|
|
2313
|
+
stream.destroy();
|
|
2314
|
+
}
|
|
2315
|
+
});
|
|
2316
|
+
};
|
|
2317
|
+
var getStats = (path) => {
|
|
2318
|
+
let stats;
|
|
2319
|
+
try {
|
|
2320
|
+
stats = statSync(path);
|
|
2321
|
+
} catch {}
|
|
2322
|
+
return stats;
|
|
2323
|
+
};
|
|
2324
|
+
var tryDecode = (str, decoder) => {
|
|
2325
|
+
try {
|
|
2326
|
+
return decoder(str);
|
|
2327
|
+
} catch {
|
|
2328
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
2329
|
+
try {
|
|
2330
|
+
return decoder(match);
|
|
2331
|
+
} catch {
|
|
2332
|
+
return match;
|
|
2333
|
+
}
|
|
2334
|
+
});
|
|
2335
|
+
}
|
|
2336
|
+
};
|
|
2337
|
+
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
2338
|
+
var serveStatic = (options = { root: "" }) => {
|
|
2339
|
+
const root = options.root || "";
|
|
2340
|
+
const optionPath = options.path;
|
|
2341
|
+
if (root !== "" && !existsSync(root)) console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`);
|
|
2342
|
+
return async (c, next) => {
|
|
2343
|
+
if (c.finalized) return next();
|
|
2344
|
+
let filename;
|
|
2345
|
+
if (optionPath) filename = optionPath;
|
|
2346
|
+
else try {
|
|
2347
|
+
filename = tryDecodeURI(c.req.path);
|
|
2348
|
+
if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) throw new Error();
|
|
2349
|
+
} catch {
|
|
2350
|
+
await options.onNotFound?.(c.req.path, c);
|
|
2351
|
+
return next();
|
|
2352
|
+
}
|
|
2353
|
+
let path = join(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename);
|
|
2354
|
+
let stats = getStats(path);
|
|
2355
|
+
if (stats && stats.isDirectory()) {
|
|
2356
|
+
const indexFile = options.index ?? "index.html";
|
|
2357
|
+
path = join(path, indexFile);
|
|
2358
|
+
stats = getStats(path);
|
|
2359
|
+
}
|
|
2360
|
+
if (!stats) {
|
|
2361
|
+
await options.onNotFound?.(path, c);
|
|
2362
|
+
return next();
|
|
2363
|
+
}
|
|
2364
|
+
const mimeType = getMimeType(path);
|
|
2365
|
+
c.header("Content-Type", mimeType || "application/octet-stream");
|
|
2366
|
+
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
|
|
2367
|
+
const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim()));
|
|
2368
|
+
for (const encoding of ENCODINGS_ORDERED_KEYS) {
|
|
2369
|
+
if (!acceptEncodingSet.has(encoding)) continue;
|
|
2370
|
+
const precompressedStats = getStats(path + ENCODINGS[encoding]);
|
|
2371
|
+
if (precompressedStats) {
|
|
2372
|
+
c.header("Content-Encoding", encoding);
|
|
2373
|
+
c.header("Vary", "Accept-Encoding", { append: true });
|
|
2374
|
+
stats = precompressedStats;
|
|
2375
|
+
path = path + ENCODINGS[encoding];
|
|
2376
|
+
break;
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
let result;
|
|
2381
|
+
const size = stats.size;
|
|
2382
|
+
const range = c.req.header("range") || "";
|
|
2383
|
+
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
2384
|
+
c.header("Content-Length", size.toString());
|
|
2385
|
+
c.status(200);
|
|
2386
|
+
result = c.body(null);
|
|
2387
|
+
} else if (!range) {
|
|
2388
|
+
c.header("Content-Length", size.toString());
|
|
2389
|
+
result = c.body(createStreamBody(createReadStream(path)), 200);
|
|
2390
|
+
} else {
|
|
2391
|
+
c.header("Accept-Ranges", "bytes");
|
|
2392
|
+
c.header("Date", stats.birthtime.toUTCString());
|
|
2393
|
+
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
2394
|
+
const start = parseInt(parts[0], 10) || 0;
|
|
2395
|
+
let end = parseInt(parts[1], 10) || size - 1;
|
|
2396
|
+
if (size < end - start + 1) end = size - 1;
|
|
2397
|
+
const chunksize = end - start + 1;
|
|
2398
|
+
const stream = createReadStream(path, {
|
|
2399
|
+
start,
|
|
2400
|
+
end
|
|
2401
|
+
});
|
|
2402
|
+
c.header("Content-Length", chunksize.toString());
|
|
2403
|
+
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
2404
|
+
result = c.body(createStreamBody(stream), 206);
|
|
2405
|
+
}
|
|
2406
|
+
await options.onFound?.(path, c);
|
|
2407
|
+
return result;
|
|
2408
|
+
};
|
|
2409
|
+
};
|
|
2410
|
+
//#endregion
|
|
2411
|
+
//#region src/api/server.ts
|
|
2412
|
+
function createApp(projectPath, store, emb, version) {
|
|
2413
|
+
const app = new Hono();
|
|
2414
|
+
const proj = detectProject(projectPath);
|
|
2415
|
+
app.get("/api/version", (c) => c.json({ version }));
|
|
2416
|
+
app.get("/api/project", (c) => c.json({
|
|
2417
|
+
path: projectPath,
|
|
2418
|
+
name: proj.name
|
|
2419
|
+
}));
|
|
2420
|
+
app.get("/api/tasks", (c) => {
|
|
2421
|
+
try {
|
|
2422
|
+
const state = readActiveState(projectPath);
|
|
2423
|
+
return c.json({
|
|
2424
|
+
active: state.primary,
|
|
2425
|
+
tasks: state.tasks
|
|
2426
|
+
});
|
|
2427
|
+
} catch {
|
|
2428
|
+
return c.json({
|
|
2429
|
+
active: "",
|
|
2430
|
+
tasks: []
|
|
2431
|
+
});
|
|
2432
|
+
}
|
|
2433
|
+
});
|
|
2434
|
+
app.get("/api/tasks/:slug/specs/:file", (c) => {
|
|
2435
|
+
const slug = c.req.param("slug");
|
|
2436
|
+
const file = c.req.param("file");
|
|
2437
|
+
if (!VALID_SLUG.test(slug)) return c.json({ error: "invalid slug" }, 400);
|
|
2438
|
+
const sd = new SpecDir(projectPath, slug);
|
|
2439
|
+
try {
|
|
2440
|
+
const content = sd.readFile(file);
|
|
2441
|
+
return c.json({ content });
|
|
2442
|
+
} catch {
|
|
2443
|
+
return c.json({ error: "spec file not found" }, 404);
|
|
2444
|
+
}
|
|
2445
|
+
});
|
|
2446
|
+
app.get("/api/tasks/:slug/specs", (c) => {
|
|
2447
|
+
const slug = c.req.param("slug");
|
|
2448
|
+
if (!VALID_SLUG.test(slug)) return c.json({ error: "invalid slug" }, 400);
|
|
2449
|
+
const sd = new SpecDir(projectPath, slug);
|
|
2450
|
+
const sections = sd.exists() ? sd.allSections() : [];
|
|
2451
|
+
return c.json({ specs: sections });
|
|
2452
|
+
});
|
|
2453
|
+
app.get("/api/tasks/:slug/validation", (c) => {
|
|
2454
|
+
const slug = c.req.param("slug");
|
|
2455
|
+
if (!VALID_SLUG.test(slug)) return c.json({ error: "invalid slug" }, 400);
|
|
2456
|
+
const sd = new SpecDir(projectPath, slug);
|
|
2457
|
+
if (!sd.exists()) return c.json({ error: "not found" }, 404);
|
|
2458
|
+
let state;
|
|
2459
|
+
try {
|
|
2460
|
+
state = readActiveState(projectPath);
|
|
2461
|
+
} catch {
|
|
2462
|
+
return c.json({ checks: [] });
|
|
2463
|
+
}
|
|
2464
|
+
const task = state.tasks.find((t) => t.slug === slug);
|
|
2465
|
+
const checks = filesForSize(task?.size ?? "L", task?.spec_type ?? "feature").map((f) => {
|
|
2466
|
+
try {
|
|
2467
|
+
sd.readFile(f);
|
|
2468
|
+
return {
|
|
2469
|
+
name: f,
|
|
2470
|
+
status: "pass"
|
|
2471
|
+
};
|
|
2472
|
+
} catch {
|
|
2473
|
+
return {
|
|
2474
|
+
name: f,
|
|
2475
|
+
status: "fail"
|
|
2476
|
+
};
|
|
2477
|
+
}
|
|
2478
|
+
});
|
|
2479
|
+
return c.json({ checks });
|
|
2480
|
+
});
|
|
2481
|
+
app.get("/api/knowledge", (c) => {
|
|
2482
|
+
const limit = Math.min(parseInt(c.req.query("limit") ?? "50", 10) || 50, 500);
|
|
2483
|
+
const entries = listAllKnowledge(store, proj.remote, proj.path, limit);
|
|
2484
|
+
return c.json({ entries });
|
|
2485
|
+
});
|
|
2486
|
+
app.get("/api/knowledge/search", (c) => {
|
|
2487
|
+
const query = c.req.query("q");
|
|
2488
|
+
if (!query) return c.json({ error: "query parameter 'q' is required" }, 400);
|
|
2489
|
+
const entries = searchKnowledgeFTS(store, query, Math.min(parseInt(c.req.query("limit") ?? "10", 10) || 10, 500));
|
|
2490
|
+
return c.json({
|
|
2491
|
+
entries,
|
|
2492
|
+
method: "fts5"
|
|
2493
|
+
});
|
|
2494
|
+
});
|
|
2495
|
+
app.get("/api/knowledge/stats", (c) => {
|
|
2496
|
+
const stats = getKnowledgeStats(store);
|
|
2497
|
+
return c.json(stats);
|
|
2498
|
+
});
|
|
2499
|
+
app.patch("/api/knowledge/:id/enabled", async (c) => {
|
|
2500
|
+
const id = parseInt(c.req.param("id"), 10);
|
|
2501
|
+
if (isNaN(id)) return c.json({ error: "invalid id" }, 400);
|
|
2502
|
+
setKnowledgeEnabled(store, id, (await c.req.json()).enabled);
|
|
2503
|
+
return c.json({ ok: true });
|
|
2504
|
+
});
|
|
2505
|
+
app.get("/api/activity", (c) => {
|
|
2506
|
+
const auditPath = join$1(projectPath, ".alfred", "audit.jsonl");
|
|
2507
|
+
const entries = [];
|
|
2508
|
+
try {
|
|
2509
|
+
const content = readFileSync(auditPath, "utf-8");
|
|
2510
|
+
for (const line of content.split("\n")) if (line.trim()) try {
|
|
2511
|
+
entries.push(JSON.parse(line));
|
|
2512
|
+
} catch {}
|
|
2513
|
+
} catch {}
|
|
2514
|
+
return c.json({ entries: entries.reverse().slice(0, 100) });
|
|
2515
|
+
});
|
|
2516
|
+
app.get("/api/epics", (c) => {
|
|
2517
|
+
const epics = listAllEpics(projectPath);
|
|
2518
|
+
return c.json({ epics });
|
|
2519
|
+
});
|
|
2520
|
+
app.get("/api/health", (c) => {
|
|
2521
|
+
const stats = getKnowledgeStats(store);
|
|
2522
|
+
return c.json({
|
|
2523
|
+
total: stats.total,
|
|
2524
|
+
bySubType: stats.bySubType
|
|
2525
|
+
});
|
|
2526
|
+
});
|
|
2527
|
+
app.get("/api/events", (c) => {
|
|
2528
|
+
return c.newResponse(new ReadableStream({ start(controller) {
|
|
2529
|
+
const encoder = new TextEncoder();
|
|
2530
|
+
controller.enqueue(encoder.encode("event: connected\ndata: {}\n\n"));
|
|
2531
|
+
const alfredDir = join$1(projectPath, ".alfred");
|
|
2532
|
+
let lastMtime = dirMaxMtime(alfredDir);
|
|
2533
|
+
const interval = setInterval(() => {
|
|
2534
|
+
const mtime = dirMaxMtime(alfredDir);
|
|
2535
|
+
if (mtime > lastMtime) {
|
|
2536
|
+
lastMtime = mtime;
|
|
2537
|
+
controller.enqueue(encoder.encode("event: refresh\ndata: {}\n\n"));
|
|
2538
|
+
}
|
|
2539
|
+
}, 5e3);
|
|
2540
|
+
const signal = c.req.raw.signal;
|
|
2541
|
+
if (signal) signal.addEventListener("abort", () => {
|
|
2542
|
+
clearInterval(interval);
|
|
2543
|
+
controller.close();
|
|
2544
|
+
});
|
|
2545
|
+
} }), { headers: {
|
|
2546
|
+
"Content-Type": "text/event-stream",
|
|
2547
|
+
"Cache-Control": "no-cache",
|
|
2548
|
+
"Connection": "keep-alive"
|
|
2549
|
+
} });
|
|
2550
|
+
});
|
|
2551
|
+
if (process.env["ALFRED_DEV"] === "1") app.all("/*", async (c) => {
|
|
2552
|
+
const url = new URL(c.req.url);
|
|
2553
|
+
url.host = "localhost:5173";
|
|
2554
|
+
url.protocol = "http:";
|
|
2555
|
+
const resp = await fetch(url.toString(), {
|
|
2556
|
+
method: c.req.method,
|
|
2557
|
+
headers: c.req.raw.headers
|
|
2558
|
+
});
|
|
2559
|
+
return new Response(resp.body, {
|
|
2560
|
+
status: resp.status,
|
|
2561
|
+
headers: resp.headers
|
|
2562
|
+
});
|
|
2563
|
+
});
|
|
2564
|
+
else {
|
|
2565
|
+
const webDistPath = resolveWebDist();
|
|
2566
|
+
if (webDistPath && existsSync$1(webDistPath)) {
|
|
2567
|
+
app.use("/*", serveStatic({ root: webDistPath }));
|
|
2568
|
+
app.get("*", (c) => {
|
|
2569
|
+
const indexPath = join$1(webDistPath, "index.html");
|
|
2570
|
+
try {
|
|
2571
|
+
const html = readFileSync(indexPath, "utf-8");
|
|
2572
|
+
return c.html(html);
|
|
2573
|
+
} catch {
|
|
2574
|
+
return c.text("Dashboard not built. Run: npm run build:web", 404);
|
|
2575
|
+
}
|
|
2576
|
+
});
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
return app;
|
|
2580
|
+
}
|
|
2581
|
+
async function startDashboard(projectPath, store, emb, opts) {
|
|
2582
|
+
const app = createApp(projectPath, store, emb, opts.version);
|
|
2583
|
+
const addr = `http://localhost:${opts.port}`;
|
|
2584
|
+
if (opts.urlOnly) console.log(addr);
|
|
2585
|
+
else {
|
|
2586
|
+
console.error(`alfred dashboard: ${addr}`);
|
|
2587
|
+
openBrowser(addr);
|
|
2588
|
+
}
|
|
2589
|
+
serve({
|
|
2590
|
+
fetch: app.fetch,
|
|
2591
|
+
port: opts.port
|
|
2592
|
+
});
|
|
2593
|
+
await new Promise((resolve) => {
|
|
2594
|
+
process.on("SIGINT", () => {
|
|
2595
|
+
console.error("\nshutting down...");
|
|
2596
|
+
resolve();
|
|
2597
|
+
});
|
|
2598
|
+
process.on("SIGTERM", () => {
|
|
2599
|
+
console.error("\nshutting down...");
|
|
2600
|
+
resolve();
|
|
2601
|
+
});
|
|
2602
|
+
});
|
|
2603
|
+
}
|
|
2604
|
+
function resolveWebDist() {
|
|
2605
|
+
const thisDir = fileURLToPath(new URL(".", import.meta.url));
|
|
2606
|
+
const candidates = [join$1(thisDir, "..", "web", "dist"), join$1(thisDir, "..", "..", "web", "dist")];
|
|
2607
|
+
for (const p of candidates) if (existsSync$1(join$1(p, "index.html"))) return p;
|
|
2608
|
+
return join$1(process.cwd(), "web", "dist");
|
|
2609
|
+
}
|
|
2610
|
+
function dirMaxMtime(dir) {
|
|
2611
|
+
let maxT = 0;
|
|
2612
|
+
try {
|
|
2613
|
+
for (const entry of readdirSync(dir)) try {
|
|
2614
|
+
const info = statSync$1(join$1(dir, entry));
|
|
2615
|
+
if (info.mtimeMs > maxT) maxT = info.mtimeMs;
|
|
2616
|
+
if (info.isDirectory()) for (const sub of readdirSync(join$1(dir, entry))) try {
|
|
2617
|
+
const si = statSync$1(join$1(dir, entry, sub));
|
|
2618
|
+
if (si.mtimeMs > maxT) maxT = si.mtimeMs;
|
|
2619
|
+
} catch {
|
|
2620
|
+
continue;
|
|
2621
|
+
}
|
|
2622
|
+
} catch {
|
|
2623
|
+
continue;
|
|
2624
|
+
}
|
|
2625
|
+
} catch {}
|
|
2626
|
+
return maxT;
|
|
2627
|
+
}
|
|
2628
|
+
function openBrowser(url) {
|
|
2629
|
+
import("node:child_process").then(({ execSync }) => {
|
|
2630
|
+
if (process.platform === "darwin") execSync(`open "${url}"`, { stdio: "ignore" });
|
|
2631
|
+
else if (process.platform === "linux") execSync(`xdg-open "${url}"`, { stdio: "ignore" });
|
|
2632
|
+
}).catch(() => {});
|
|
2633
|
+
}
|
|
2634
|
+
//#endregion
|
|
2635
|
+
export { startDashboard };
|