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