@react-grab/cursor 0.0.55
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/LICENSE +21 -0
- package/dist/client.cjs +87 -0
- package/dist/client.d.cts +14 -0
- package/dist/client.d.ts +14 -0
- package/dist/client.js +84 -0
- package/dist/server.cjs +2392 -0
- package/dist/server.d.cts +6 -0
- package/dist/server.d.ts +6 -0
- package/dist/server.js +2386 -0
- package/package.json +32 -0
package/dist/server.js
ADDED
|
@@ -0,0 +1,2386 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { createServer as createServer$1 } from 'http';
|
|
3
|
+
import { Http2ServerRequest } from 'http2';
|
|
4
|
+
import { Readable } from 'stream';
|
|
5
|
+
import crypto from 'crypto';
|
|
6
|
+
|
|
7
|
+
// src/server.ts
|
|
8
|
+
|
|
9
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/compose.js
|
|
10
|
+
var compose = (middleware, onError, onNotFound) => {
|
|
11
|
+
return (context, next) => {
|
|
12
|
+
let index = -1;
|
|
13
|
+
return dispatch(0);
|
|
14
|
+
async function dispatch(i) {
|
|
15
|
+
if (i <= index) {
|
|
16
|
+
throw new Error("next() called multiple times");
|
|
17
|
+
}
|
|
18
|
+
index = i;
|
|
19
|
+
let res;
|
|
20
|
+
let isError = false;
|
|
21
|
+
let handler;
|
|
22
|
+
if (middleware[i]) {
|
|
23
|
+
handler = middleware[i][0][0];
|
|
24
|
+
context.req.routeIndex = i;
|
|
25
|
+
} else {
|
|
26
|
+
handler = i === middleware.length && next || void 0;
|
|
27
|
+
}
|
|
28
|
+
if (handler) {
|
|
29
|
+
try {
|
|
30
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
31
|
+
} catch (err) {
|
|
32
|
+
if (err instanceof Error && onError) {
|
|
33
|
+
context.error = err;
|
|
34
|
+
res = await onError(err, context);
|
|
35
|
+
isError = true;
|
|
36
|
+
} else {
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
if (context.finalized === false && onNotFound) {
|
|
42
|
+
res = await onNotFound(context);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (res && (context.finalized === false || isError)) {
|
|
46
|
+
context.res = res;
|
|
47
|
+
}
|
|
48
|
+
return context;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/request/constants.js
|
|
54
|
+
var GET_MATCH_RESULT = Symbol();
|
|
55
|
+
|
|
56
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/utils/body.js
|
|
57
|
+
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
58
|
+
const { all = false, dot = false } = options;
|
|
59
|
+
const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
|
|
60
|
+
const contentType = headers.get("Content-Type");
|
|
61
|
+
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
|
|
62
|
+
return parseFormData(request, { all, dot });
|
|
63
|
+
}
|
|
64
|
+
return {};
|
|
65
|
+
};
|
|
66
|
+
async function parseFormData(request, options) {
|
|
67
|
+
const formData = await request.formData();
|
|
68
|
+
if (formData) {
|
|
69
|
+
return convertFormDataToBodyData(formData, options);
|
|
70
|
+
}
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
function convertFormDataToBodyData(formData, options) {
|
|
74
|
+
const form = /* @__PURE__ */ Object.create(null);
|
|
75
|
+
formData.forEach((value, key) => {
|
|
76
|
+
const shouldParseAllValues = options.all || key.endsWith("[]");
|
|
77
|
+
if (!shouldParseAllValues) {
|
|
78
|
+
form[key] = value;
|
|
79
|
+
} else {
|
|
80
|
+
handleParsingAllValues(form, key, value);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (options.dot) {
|
|
84
|
+
Object.entries(form).forEach(([key, value]) => {
|
|
85
|
+
const shouldParseDotValues = key.includes(".");
|
|
86
|
+
if (shouldParseDotValues) {
|
|
87
|
+
handleParsingNestedValues(form, key, value);
|
|
88
|
+
delete form[key];
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return form;
|
|
93
|
+
}
|
|
94
|
+
var handleParsingAllValues = (form, key, value) => {
|
|
95
|
+
if (form[key] !== void 0) {
|
|
96
|
+
if (Array.isArray(form[key])) {
|
|
97
|
+
form[key].push(value);
|
|
98
|
+
} else {
|
|
99
|
+
form[key] = [form[key], value];
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
if (!key.endsWith("[]")) {
|
|
103
|
+
form[key] = value;
|
|
104
|
+
} else {
|
|
105
|
+
form[key] = [value];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var handleParsingNestedValues = (form, key, value) => {
|
|
110
|
+
let nestedForm = form;
|
|
111
|
+
const keys = key.split(".");
|
|
112
|
+
keys.forEach((key2, index) => {
|
|
113
|
+
if (index === keys.length - 1) {
|
|
114
|
+
nestedForm[key2] = value;
|
|
115
|
+
} else {
|
|
116
|
+
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) {
|
|
117
|
+
nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
118
|
+
}
|
|
119
|
+
nestedForm = nestedForm[key2];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/utils/url.js
|
|
125
|
+
var splitPath = (path) => {
|
|
126
|
+
const paths = path.split("/");
|
|
127
|
+
if (paths[0] === "") {
|
|
128
|
+
paths.shift();
|
|
129
|
+
}
|
|
130
|
+
return paths;
|
|
131
|
+
};
|
|
132
|
+
var splitRoutingPath = (routePath) => {
|
|
133
|
+
const { groups, path } = extractGroupsFromPath(routePath);
|
|
134
|
+
const paths = splitPath(path);
|
|
135
|
+
return replaceGroupMarks(paths, groups);
|
|
136
|
+
};
|
|
137
|
+
var extractGroupsFromPath = (path) => {
|
|
138
|
+
const groups = [];
|
|
139
|
+
path = path.replace(/\{[^}]+\}/g, (match2, index) => {
|
|
140
|
+
const mark = `@${index}`;
|
|
141
|
+
groups.push([mark, match2]);
|
|
142
|
+
return mark;
|
|
143
|
+
});
|
|
144
|
+
return { groups, path };
|
|
145
|
+
};
|
|
146
|
+
var replaceGroupMarks = (paths, groups) => {
|
|
147
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
148
|
+
const [mark] = groups[i];
|
|
149
|
+
for (let j = paths.length - 1; j >= 0; j--) {
|
|
150
|
+
if (paths[j].includes(mark)) {
|
|
151
|
+
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return paths;
|
|
157
|
+
};
|
|
158
|
+
var patternCache = {};
|
|
159
|
+
var getPattern = (label, next) => {
|
|
160
|
+
if (label === "*") {
|
|
161
|
+
return "*";
|
|
162
|
+
}
|
|
163
|
+
const match2 = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
164
|
+
if (match2) {
|
|
165
|
+
const cacheKey2 = `${label}#${next}`;
|
|
166
|
+
if (!patternCache[cacheKey2]) {
|
|
167
|
+
if (match2[2]) {
|
|
168
|
+
patternCache[cacheKey2] = next && next[0] !== ":" && next[0] !== "*" ? [cacheKey2, match2[1], new RegExp(`^${match2[2]}(?=/${next})`)] : [label, match2[1], new RegExp(`^${match2[2]}$`)];
|
|
169
|
+
} else {
|
|
170
|
+
patternCache[cacheKey2] = [label, match2[1], true];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return patternCache[cacheKey2];
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
};
|
|
177
|
+
var tryDecode = (str, decoder) => {
|
|
178
|
+
try {
|
|
179
|
+
return decoder(str);
|
|
180
|
+
} catch {
|
|
181
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match2) => {
|
|
182
|
+
try {
|
|
183
|
+
return decoder(match2);
|
|
184
|
+
} catch {
|
|
185
|
+
return match2;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
191
|
+
var getPath = (request) => {
|
|
192
|
+
const url = request.url;
|
|
193
|
+
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
194
|
+
let i = start;
|
|
195
|
+
for (; i < url.length; i++) {
|
|
196
|
+
const charCode = url.charCodeAt(i);
|
|
197
|
+
if (charCode === 37) {
|
|
198
|
+
const queryIndex = url.indexOf("?", i);
|
|
199
|
+
const path = url.slice(start, queryIndex === -1 ? void 0 : queryIndex);
|
|
200
|
+
return tryDecodeURI(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
|
|
201
|
+
} else if (charCode === 63) {
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return url.slice(start, i);
|
|
206
|
+
};
|
|
207
|
+
var getPathNoStrict = (request) => {
|
|
208
|
+
const result = getPath(request);
|
|
209
|
+
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
210
|
+
};
|
|
211
|
+
var mergePath = (base, sub, ...rest) => {
|
|
212
|
+
if (rest.length) {
|
|
213
|
+
sub = mergePath(sub, ...rest);
|
|
214
|
+
}
|
|
215
|
+
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
216
|
+
};
|
|
217
|
+
var checkOptionalParameter = (path) => {
|
|
218
|
+
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
const segments = path.split("/");
|
|
222
|
+
const results = [];
|
|
223
|
+
let basePath = "";
|
|
224
|
+
segments.forEach((segment) => {
|
|
225
|
+
if (segment !== "" && !/\:/.test(segment)) {
|
|
226
|
+
basePath += "/" + segment;
|
|
227
|
+
} else if (/\:/.test(segment)) {
|
|
228
|
+
if (/\?/.test(segment)) {
|
|
229
|
+
if (results.length === 0 && basePath === "") {
|
|
230
|
+
results.push("/");
|
|
231
|
+
} else {
|
|
232
|
+
results.push(basePath);
|
|
233
|
+
}
|
|
234
|
+
const optionalSegment = segment.replace("?", "");
|
|
235
|
+
basePath += "/" + optionalSegment;
|
|
236
|
+
results.push(basePath);
|
|
237
|
+
} else {
|
|
238
|
+
basePath += "/" + segment;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
243
|
+
};
|
|
244
|
+
var _decodeURI = (value) => {
|
|
245
|
+
if (!/[%+]/.test(value)) {
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
if (value.indexOf("+") !== -1) {
|
|
249
|
+
value = value.replace(/\+/g, " ");
|
|
250
|
+
}
|
|
251
|
+
return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
|
|
252
|
+
};
|
|
253
|
+
var _getQueryParam = (url, key, multiple) => {
|
|
254
|
+
let encoded;
|
|
255
|
+
if (!multiple && key && !/[%+]/.test(key)) {
|
|
256
|
+
let keyIndex2 = url.indexOf("?", 8);
|
|
257
|
+
if (keyIndex2 === -1) {
|
|
258
|
+
return void 0;
|
|
259
|
+
}
|
|
260
|
+
if (!url.startsWith(key, keyIndex2 + 1)) {
|
|
261
|
+
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
262
|
+
}
|
|
263
|
+
while (keyIndex2 !== -1) {
|
|
264
|
+
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
265
|
+
if (trailingKeyCode === 61) {
|
|
266
|
+
const valueIndex = keyIndex2 + key.length + 2;
|
|
267
|
+
const endIndex = url.indexOf("&", valueIndex);
|
|
268
|
+
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
|
|
269
|
+
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
|
|
270
|
+
return "";
|
|
271
|
+
}
|
|
272
|
+
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
273
|
+
}
|
|
274
|
+
encoded = /[%+]/.test(url);
|
|
275
|
+
if (!encoded) {
|
|
276
|
+
return void 0;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const results = {};
|
|
280
|
+
encoded ??= /[%+]/.test(url);
|
|
281
|
+
let keyIndex = url.indexOf("?", 8);
|
|
282
|
+
while (keyIndex !== -1) {
|
|
283
|
+
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
284
|
+
let valueIndex = url.indexOf("=", keyIndex);
|
|
285
|
+
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
|
|
286
|
+
valueIndex = -1;
|
|
287
|
+
}
|
|
288
|
+
let name = url.slice(
|
|
289
|
+
keyIndex + 1,
|
|
290
|
+
valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex
|
|
291
|
+
);
|
|
292
|
+
if (encoded) {
|
|
293
|
+
name = _decodeURI(name);
|
|
294
|
+
}
|
|
295
|
+
keyIndex = nextKeyIndex;
|
|
296
|
+
if (name === "") {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
let value;
|
|
300
|
+
if (valueIndex === -1) {
|
|
301
|
+
value = "";
|
|
302
|
+
} else {
|
|
303
|
+
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
|
|
304
|
+
if (encoded) {
|
|
305
|
+
value = _decodeURI(value);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (multiple) {
|
|
309
|
+
if (!(results[name] && Array.isArray(results[name]))) {
|
|
310
|
+
results[name] = [];
|
|
311
|
+
}
|
|
312
|
+
results[name].push(value);
|
|
313
|
+
} else {
|
|
314
|
+
results[name] ??= value;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return key ? results[key] : results;
|
|
318
|
+
};
|
|
319
|
+
var getQueryParam = _getQueryParam;
|
|
320
|
+
var getQueryParams = (url, key) => {
|
|
321
|
+
return _getQueryParam(url, key, true);
|
|
322
|
+
};
|
|
323
|
+
var decodeURIComponent_ = decodeURIComponent;
|
|
324
|
+
|
|
325
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/request.js
|
|
326
|
+
var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
|
327
|
+
var HonoRequest = class {
|
|
328
|
+
raw;
|
|
329
|
+
#validatedData;
|
|
330
|
+
#matchResult;
|
|
331
|
+
routeIndex = 0;
|
|
332
|
+
path;
|
|
333
|
+
bodyCache = {};
|
|
334
|
+
constructor(request, path = "/", matchResult = [[]]) {
|
|
335
|
+
this.raw = request;
|
|
336
|
+
this.path = path;
|
|
337
|
+
this.#matchResult = matchResult;
|
|
338
|
+
this.#validatedData = {};
|
|
339
|
+
}
|
|
340
|
+
param(key) {
|
|
341
|
+
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
342
|
+
}
|
|
343
|
+
#getDecodedParam(key) {
|
|
344
|
+
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
345
|
+
const param = this.#getParamValue(paramKey);
|
|
346
|
+
return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
|
|
347
|
+
}
|
|
348
|
+
#getAllDecodedParams() {
|
|
349
|
+
const decoded = {};
|
|
350
|
+
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
351
|
+
for (const key of keys) {
|
|
352
|
+
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
353
|
+
if (value !== void 0) {
|
|
354
|
+
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return decoded;
|
|
358
|
+
}
|
|
359
|
+
#getParamValue(paramKey) {
|
|
360
|
+
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
361
|
+
}
|
|
362
|
+
query(key) {
|
|
363
|
+
return getQueryParam(this.url, key);
|
|
364
|
+
}
|
|
365
|
+
queries(key) {
|
|
366
|
+
return getQueryParams(this.url, key);
|
|
367
|
+
}
|
|
368
|
+
header(name) {
|
|
369
|
+
if (name) {
|
|
370
|
+
return this.raw.headers.get(name) ?? void 0;
|
|
371
|
+
}
|
|
372
|
+
const headerData = {};
|
|
373
|
+
this.raw.headers.forEach((value, key) => {
|
|
374
|
+
headerData[key] = value;
|
|
375
|
+
});
|
|
376
|
+
return headerData;
|
|
377
|
+
}
|
|
378
|
+
async parseBody(options) {
|
|
379
|
+
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
380
|
+
}
|
|
381
|
+
#cachedBody = (key) => {
|
|
382
|
+
const { bodyCache, raw: raw2 } = this;
|
|
383
|
+
const cachedBody = bodyCache[key];
|
|
384
|
+
if (cachedBody) {
|
|
385
|
+
return cachedBody;
|
|
386
|
+
}
|
|
387
|
+
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
388
|
+
if (anyCachedKey) {
|
|
389
|
+
return bodyCache[anyCachedKey].then((body) => {
|
|
390
|
+
if (anyCachedKey === "json") {
|
|
391
|
+
body = JSON.stringify(body);
|
|
392
|
+
}
|
|
393
|
+
return new Response(body)[key]();
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
return bodyCache[key] = raw2[key]();
|
|
397
|
+
};
|
|
398
|
+
json() {
|
|
399
|
+
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
400
|
+
}
|
|
401
|
+
text() {
|
|
402
|
+
return this.#cachedBody("text");
|
|
403
|
+
}
|
|
404
|
+
arrayBuffer() {
|
|
405
|
+
return this.#cachedBody("arrayBuffer");
|
|
406
|
+
}
|
|
407
|
+
blob() {
|
|
408
|
+
return this.#cachedBody("blob");
|
|
409
|
+
}
|
|
410
|
+
formData() {
|
|
411
|
+
return this.#cachedBody("formData");
|
|
412
|
+
}
|
|
413
|
+
addValidatedData(target, data) {
|
|
414
|
+
this.#validatedData[target] = data;
|
|
415
|
+
}
|
|
416
|
+
valid(target) {
|
|
417
|
+
return this.#validatedData[target];
|
|
418
|
+
}
|
|
419
|
+
get url() {
|
|
420
|
+
return this.raw.url;
|
|
421
|
+
}
|
|
422
|
+
get method() {
|
|
423
|
+
return this.raw.method;
|
|
424
|
+
}
|
|
425
|
+
get [GET_MATCH_RESULT]() {
|
|
426
|
+
return this.#matchResult;
|
|
427
|
+
}
|
|
428
|
+
get matchedRoutes() {
|
|
429
|
+
return this.#matchResult[0].map(([[, route]]) => route);
|
|
430
|
+
}
|
|
431
|
+
get routePath() {
|
|
432
|
+
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/utils/html.js
|
|
437
|
+
var HtmlEscapedCallbackPhase = {
|
|
438
|
+
Stringify: 1};
|
|
439
|
+
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
440
|
+
if (typeof str === "object" && !(str instanceof String)) {
|
|
441
|
+
if (!(str instanceof Promise)) {
|
|
442
|
+
str = str.toString();
|
|
443
|
+
}
|
|
444
|
+
if (str instanceof Promise) {
|
|
445
|
+
str = await str;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const callbacks = str.callbacks;
|
|
449
|
+
if (!callbacks?.length) {
|
|
450
|
+
return Promise.resolve(str);
|
|
451
|
+
}
|
|
452
|
+
if (buffer) {
|
|
453
|
+
buffer[0] += str;
|
|
454
|
+
} else {
|
|
455
|
+
buffer = [str];
|
|
456
|
+
}
|
|
457
|
+
const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then(
|
|
458
|
+
(res) => Promise.all(
|
|
459
|
+
res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))
|
|
460
|
+
).then(() => buffer[0])
|
|
461
|
+
);
|
|
462
|
+
{
|
|
463
|
+
return resStr;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/context.js
|
|
468
|
+
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
469
|
+
var setDefaultContentType = (contentType, headers) => {
|
|
470
|
+
return {
|
|
471
|
+
"Content-Type": contentType,
|
|
472
|
+
...headers
|
|
473
|
+
};
|
|
474
|
+
};
|
|
475
|
+
var Context = class {
|
|
476
|
+
#rawRequest;
|
|
477
|
+
#req;
|
|
478
|
+
env = {};
|
|
479
|
+
#var;
|
|
480
|
+
finalized = false;
|
|
481
|
+
error;
|
|
482
|
+
#status;
|
|
483
|
+
#executionCtx;
|
|
484
|
+
#res;
|
|
485
|
+
#layout;
|
|
486
|
+
#renderer;
|
|
487
|
+
#notFoundHandler;
|
|
488
|
+
#preparedHeaders;
|
|
489
|
+
#matchResult;
|
|
490
|
+
#path;
|
|
491
|
+
constructor(req, options) {
|
|
492
|
+
this.#rawRequest = req;
|
|
493
|
+
if (options) {
|
|
494
|
+
this.#executionCtx = options.executionCtx;
|
|
495
|
+
this.env = options.env;
|
|
496
|
+
this.#notFoundHandler = options.notFoundHandler;
|
|
497
|
+
this.#path = options.path;
|
|
498
|
+
this.#matchResult = options.matchResult;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
get req() {
|
|
502
|
+
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
503
|
+
return this.#req;
|
|
504
|
+
}
|
|
505
|
+
get event() {
|
|
506
|
+
if (this.#executionCtx && "respondWith" in this.#executionCtx) {
|
|
507
|
+
return this.#executionCtx;
|
|
508
|
+
} else {
|
|
509
|
+
throw Error("This context has no FetchEvent");
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
get executionCtx() {
|
|
513
|
+
if (this.#executionCtx) {
|
|
514
|
+
return this.#executionCtx;
|
|
515
|
+
} else {
|
|
516
|
+
throw Error("This context has no ExecutionContext");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
get res() {
|
|
520
|
+
return this.#res ||= new Response(null, {
|
|
521
|
+
headers: this.#preparedHeaders ??= new Headers()
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
set res(_res) {
|
|
525
|
+
if (this.#res && _res) {
|
|
526
|
+
_res = new Response(_res.body, _res);
|
|
527
|
+
for (const [k, v] of this.#res.headers.entries()) {
|
|
528
|
+
if (k === "content-type") {
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (k === "set-cookie") {
|
|
532
|
+
const cookies = this.#res.headers.getSetCookie();
|
|
533
|
+
_res.headers.delete("set-cookie");
|
|
534
|
+
for (const cookie of cookies) {
|
|
535
|
+
_res.headers.append("set-cookie", cookie);
|
|
536
|
+
}
|
|
537
|
+
} else {
|
|
538
|
+
_res.headers.set(k, v);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
this.#res = _res;
|
|
543
|
+
this.finalized = true;
|
|
544
|
+
}
|
|
545
|
+
render = (...args) => {
|
|
546
|
+
this.#renderer ??= (content) => this.html(content);
|
|
547
|
+
return this.#renderer(...args);
|
|
548
|
+
};
|
|
549
|
+
setLayout = (layout) => this.#layout = layout;
|
|
550
|
+
getLayout = () => this.#layout;
|
|
551
|
+
setRenderer = (renderer) => {
|
|
552
|
+
this.#renderer = renderer;
|
|
553
|
+
};
|
|
554
|
+
header = (name, value, options) => {
|
|
555
|
+
if (this.finalized) {
|
|
556
|
+
this.#res = new Response(this.#res.body, this.#res);
|
|
557
|
+
}
|
|
558
|
+
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers();
|
|
559
|
+
if (value === void 0) {
|
|
560
|
+
headers.delete(name);
|
|
561
|
+
} else if (options?.append) {
|
|
562
|
+
headers.append(name, value);
|
|
563
|
+
} else {
|
|
564
|
+
headers.set(name, value);
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
status = (status) => {
|
|
568
|
+
this.#status = status;
|
|
569
|
+
};
|
|
570
|
+
set = (key, value) => {
|
|
571
|
+
this.#var ??= /* @__PURE__ */ new Map();
|
|
572
|
+
this.#var.set(key, value);
|
|
573
|
+
};
|
|
574
|
+
get = (key) => {
|
|
575
|
+
return this.#var ? this.#var.get(key) : void 0;
|
|
576
|
+
};
|
|
577
|
+
get var() {
|
|
578
|
+
if (!this.#var) {
|
|
579
|
+
return {};
|
|
580
|
+
}
|
|
581
|
+
return Object.fromEntries(this.#var);
|
|
582
|
+
}
|
|
583
|
+
#newResponse(data, arg, headers) {
|
|
584
|
+
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers();
|
|
585
|
+
if (typeof arg === "object" && "headers" in arg) {
|
|
586
|
+
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
587
|
+
for (const [key, value] of argHeaders) {
|
|
588
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
589
|
+
responseHeaders.append(key, value);
|
|
590
|
+
} else {
|
|
591
|
+
responseHeaders.set(key, value);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (headers) {
|
|
596
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
597
|
+
if (typeof v === "string") {
|
|
598
|
+
responseHeaders.set(k, v);
|
|
599
|
+
} else {
|
|
600
|
+
responseHeaders.delete(k);
|
|
601
|
+
for (const v2 of v) {
|
|
602
|
+
responseHeaders.append(k, v2);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const status = typeof arg === "number" ? arg : arg?.status ?? this.#status;
|
|
608
|
+
return new Response(data, { status, headers: responseHeaders });
|
|
609
|
+
}
|
|
610
|
+
newResponse = (...args) => this.#newResponse(...args);
|
|
611
|
+
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
612
|
+
text = (text, arg, headers) => {
|
|
613
|
+
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(
|
|
614
|
+
text,
|
|
615
|
+
arg,
|
|
616
|
+
setDefaultContentType(TEXT_PLAIN, headers)
|
|
617
|
+
);
|
|
618
|
+
};
|
|
619
|
+
json = (object, arg, headers) => {
|
|
620
|
+
return this.#newResponse(
|
|
621
|
+
JSON.stringify(object),
|
|
622
|
+
arg,
|
|
623
|
+
setDefaultContentType("application/json", headers)
|
|
624
|
+
);
|
|
625
|
+
};
|
|
626
|
+
html = (html, arg, headers) => {
|
|
627
|
+
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
628
|
+
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
629
|
+
};
|
|
630
|
+
redirect = (location, status) => {
|
|
631
|
+
const locationString = String(location);
|
|
632
|
+
this.header(
|
|
633
|
+
"Location",
|
|
634
|
+
!/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString)
|
|
635
|
+
);
|
|
636
|
+
return this.newResponse(null, status ?? 302);
|
|
637
|
+
};
|
|
638
|
+
notFound = () => {
|
|
639
|
+
this.#notFoundHandler ??= () => new Response();
|
|
640
|
+
return this.#notFoundHandler(this);
|
|
641
|
+
};
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router.js
|
|
645
|
+
var METHOD_NAME_ALL = "ALL";
|
|
646
|
+
var METHOD_NAME_ALL_LOWERCASE = "all";
|
|
647
|
+
var METHODS = ["get", "post", "put", "delete", "options", "patch"];
|
|
648
|
+
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
649
|
+
var UnsupportedPathError = class extends Error {
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/utils/constants.js
|
|
653
|
+
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
654
|
+
|
|
655
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/hono-base.js
|
|
656
|
+
var notFoundHandler = (c) => {
|
|
657
|
+
return c.text("404 Not Found", 404);
|
|
658
|
+
};
|
|
659
|
+
var errorHandler = (err, c) => {
|
|
660
|
+
if ("getResponse" in err) {
|
|
661
|
+
const res = err.getResponse();
|
|
662
|
+
return c.newResponse(res.body, res);
|
|
663
|
+
}
|
|
664
|
+
console.error(err);
|
|
665
|
+
return c.text("Internal Server Error", 500);
|
|
666
|
+
};
|
|
667
|
+
var Hono = class {
|
|
668
|
+
get;
|
|
669
|
+
post;
|
|
670
|
+
put;
|
|
671
|
+
delete;
|
|
672
|
+
options;
|
|
673
|
+
patch;
|
|
674
|
+
all;
|
|
675
|
+
on;
|
|
676
|
+
use;
|
|
677
|
+
router;
|
|
678
|
+
getPath;
|
|
679
|
+
_basePath = "/";
|
|
680
|
+
#path = "/";
|
|
681
|
+
routes = [];
|
|
682
|
+
constructor(options = {}) {
|
|
683
|
+
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
684
|
+
allMethods.forEach((method) => {
|
|
685
|
+
this[method] = (args1, ...args) => {
|
|
686
|
+
if (typeof args1 === "string") {
|
|
687
|
+
this.#path = args1;
|
|
688
|
+
} else {
|
|
689
|
+
this.#addRoute(method, this.#path, args1);
|
|
690
|
+
}
|
|
691
|
+
args.forEach((handler) => {
|
|
692
|
+
this.#addRoute(method, this.#path, handler);
|
|
693
|
+
});
|
|
694
|
+
return this;
|
|
695
|
+
};
|
|
696
|
+
});
|
|
697
|
+
this.on = (method, path, ...handlers) => {
|
|
698
|
+
for (const p of [path].flat()) {
|
|
699
|
+
this.#path = p;
|
|
700
|
+
for (const m of [method].flat()) {
|
|
701
|
+
handlers.map((handler) => {
|
|
702
|
+
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return this;
|
|
707
|
+
};
|
|
708
|
+
this.use = (arg1, ...handlers) => {
|
|
709
|
+
if (typeof arg1 === "string") {
|
|
710
|
+
this.#path = arg1;
|
|
711
|
+
} else {
|
|
712
|
+
this.#path = "*";
|
|
713
|
+
handlers.unshift(arg1);
|
|
714
|
+
}
|
|
715
|
+
handlers.forEach((handler) => {
|
|
716
|
+
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
717
|
+
});
|
|
718
|
+
return this;
|
|
719
|
+
};
|
|
720
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
721
|
+
Object.assign(this, optionsWithoutStrict);
|
|
722
|
+
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
723
|
+
}
|
|
724
|
+
#clone() {
|
|
725
|
+
const clone = new Hono({
|
|
726
|
+
router: this.router,
|
|
727
|
+
getPath: this.getPath
|
|
728
|
+
});
|
|
729
|
+
clone.errorHandler = this.errorHandler;
|
|
730
|
+
clone.#notFoundHandler = this.#notFoundHandler;
|
|
731
|
+
clone.routes = this.routes;
|
|
732
|
+
return clone;
|
|
733
|
+
}
|
|
734
|
+
#notFoundHandler = notFoundHandler;
|
|
735
|
+
errorHandler = errorHandler;
|
|
736
|
+
route(path, app2) {
|
|
737
|
+
const subApp = this.basePath(path);
|
|
738
|
+
app2.routes.map((r) => {
|
|
739
|
+
let handler;
|
|
740
|
+
if (app2.errorHandler === errorHandler) {
|
|
741
|
+
handler = r.handler;
|
|
742
|
+
} else {
|
|
743
|
+
handler = async (c, next) => (await compose([], app2.errorHandler)(c, () => r.handler(c, next))).res;
|
|
744
|
+
handler[COMPOSED_HANDLER] = r.handler;
|
|
745
|
+
}
|
|
746
|
+
subApp.#addRoute(r.method, r.path, handler);
|
|
747
|
+
});
|
|
748
|
+
return this;
|
|
749
|
+
}
|
|
750
|
+
basePath(path) {
|
|
751
|
+
const subApp = this.#clone();
|
|
752
|
+
subApp._basePath = mergePath(this._basePath, path);
|
|
753
|
+
return subApp;
|
|
754
|
+
}
|
|
755
|
+
onError = (handler) => {
|
|
756
|
+
this.errorHandler = handler;
|
|
757
|
+
return this;
|
|
758
|
+
};
|
|
759
|
+
notFound = (handler) => {
|
|
760
|
+
this.#notFoundHandler = handler;
|
|
761
|
+
return this;
|
|
762
|
+
};
|
|
763
|
+
mount(path, applicationHandler, options) {
|
|
764
|
+
let replaceRequest;
|
|
765
|
+
let optionHandler;
|
|
766
|
+
if (options) {
|
|
767
|
+
if (typeof options === "function") {
|
|
768
|
+
optionHandler = options;
|
|
769
|
+
} else {
|
|
770
|
+
optionHandler = options.optionHandler;
|
|
771
|
+
if (options.replaceRequest === false) {
|
|
772
|
+
replaceRequest = (request) => request;
|
|
773
|
+
} else {
|
|
774
|
+
replaceRequest = options.replaceRequest;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
const getOptions = optionHandler ? (c) => {
|
|
779
|
+
const options2 = optionHandler(c);
|
|
780
|
+
return Array.isArray(options2) ? options2 : [options2];
|
|
781
|
+
} : (c) => {
|
|
782
|
+
let executionContext = void 0;
|
|
783
|
+
try {
|
|
784
|
+
executionContext = c.executionCtx;
|
|
785
|
+
} catch {
|
|
786
|
+
}
|
|
787
|
+
return [c.env, executionContext];
|
|
788
|
+
};
|
|
789
|
+
replaceRequest ||= (() => {
|
|
790
|
+
const mergedPath = mergePath(this._basePath, path);
|
|
791
|
+
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
792
|
+
return (request) => {
|
|
793
|
+
const url = new URL(request.url);
|
|
794
|
+
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
795
|
+
return new Request(url, request);
|
|
796
|
+
};
|
|
797
|
+
})();
|
|
798
|
+
const handler = async (c, next) => {
|
|
799
|
+
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
800
|
+
if (res) {
|
|
801
|
+
return res;
|
|
802
|
+
}
|
|
803
|
+
await next();
|
|
804
|
+
};
|
|
805
|
+
this.#addRoute(METHOD_NAME_ALL, mergePath(path, "*"), handler);
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
808
|
+
#addRoute(method, path, handler) {
|
|
809
|
+
method = method.toUpperCase();
|
|
810
|
+
path = mergePath(this._basePath, path);
|
|
811
|
+
const r = { basePath: this._basePath, path, method, handler };
|
|
812
|
+
this.router.add(method, path, [handler, r]);
|
|
813
|
+
this.routes.push(r);
|
|
814
|
+
}
|
|
815
|
+
#handleError(err, c) {
|
|
816
|
+
if (err instanceof Error) {
|
|
817
|
+
return this.errorHandler(err, c);
|
|
818
|
+
}
|
|
819
|
+
throw err;
|
|
820
|
+
}
|
|
821
|
+
#dispatch(request, executionCtx, env, method) {
|
|
822
|
+
if (method === "HEAD") {
|
|
823
|
+
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
824
|
+
}
|
|
825
|
+
const path = this.getPath(request, { env });
|
|
826
|
+
const matchResult = this.router.match(method, path);
|
|
827
|
+
const c = new Context(request, {
|
|
828
|
+
path,
|
|
829
|
+
matchResult,
|
|
830
|
+
env,
|
|
831
|
+
executionCtx,
|
|
832
|
+
notFoundHandler: this.#notFoundHandler
|
|
833
|
+
});
|
|
834
|
+
if (matchResult[0].length === 1) {
|
|
835
|
+
let res;
|
|
836
|
+
try {
|
|
837
|
+
res = matchResult[0][0][0][0](c, async () => {
|
|
838
|
+
c.res = await this.#notFoundHandler(c);
|
|
839
|
+
});
|
|
840
|
+
} catch (err) {
|
|
841
|
+
return this.#handleError(err, c);
|
|
842
|
+
}
|
|
843
|
+
return res instanceof Promise ? res.then(
|
|
844
|
+
(resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
|
|
845
|
+
).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
846
|
+
}
|
|
847
|
+
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
848
|
+
return (async () => {
|
|
849
|
+
try {
|
|
850
|
+
const context = await composed(c);
|
|
851
|
+
if (!context.finalized) {
|
|
852
|
+
throw new Error(
|
|
853
|
+
"Context is not finalized. Did you forget to return a Response object or `await next()`?"
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
return context.res;
|
|
857
|
+
} catch (err) {
|
|
858
|
+
return this.#handleError(err, c);
|
|
859
|
+
}
|
|
860
|
+
})();
|
|
861
|
+
}
|
|
862
|
+
fetch = (request, ...rest) => {
|
|
863
|
+
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
864
|
+
};
|
|
865
|
+
request = (input, requestInit, Env, executionCtx) => {
|
|
866
|
+
if (input instanceof Request) {
|
|
867
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
868
|
+
}
|
|
869
|
+
input = input.toString();
|
|
870
|
+
return this.fetch(
|
|
871
|
+
new Request(
|
|
872
|
+
/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`,
|
|
873
|
+
requestInit
|
|
874
|
+
),
|
|
875
|
+
Env,
|
|
876
|
+
executionCtx
|
|
877
|
+
);
|
|
878
|
+
};
|
|
879
|
+
fire = () => {
|
|
880
|
+
addEventListener("fetch", (event) => {
|
|
881
|
+
event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
|
|
882
|
+
});
|
|
883
|
+
};
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/reg-exp-router/matcher.js
|
|
887
|
+
var emptyParam = [];
|
|
888
|
+
function match(method, path) {
|
|
889
|
+
const matchers = this.buildAllMatchers();
|
|
890
|
+
const match2 = (method2, path2) => {
|
|
891
|
+
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
892
|
+
const staticMatch = matcher[2][path2];
|
|
893
|
+
if (staticMatch) {
|
|
894
|
+
return staticMatch;
|
|
895
|
+
}
|
|
896
|
+
const match3 = path2.match(matcher[0]);
|
|
897
|
+
if (!match3) {
|
|
898
|
+
return [[], emptyParam];
|
|
899
|
+
}
|
|
900
|
+
const index = match3.indexOf("", 1);
|
|
901
|
+
return [matcher[1][index], match3];
|
|
902
|
+
};
|
|
903
|
+
this.match = match2;
|
|
904
|
+
return match2(method, path);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/reg-exp-router/node.js
|
|
908
|
+
var LABEL_REG_EXP_STR = "[^/]+";
|
|
909
|
+
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
910
|
+
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
911
|
+
var PATH_ERROR = Symbol();
|
|
912
|
+
var regExpMetaChars = new Set(".\\+*[^]$()");
|
|
913
|
+
function compareKey(a, b) {
|
|
914
|
+
if (a.length === 1) {
|
|
915
|
+
return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
916
|
+
}
|
|
917
|
+
if (b.length === 1) {
|
|
918
|
+
return 1;
|
|
919
|
+
}
|
|
920
|
+
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
|
|
921
|
+
return 1;
|
|
922
|
+
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
|
|
923
|
+
return -1;
|
|
924
|
+
}
|
|
925
|
+
if (a === LABEL_REG_EXP_STR) {
|
|
926
|
+
return 1;
|
|
927
|
+
} else if (b === LABEL_REG_EXP_STR) {
|
|
928
|
+
return -1;
|
|
929
|
+
}
|
|
930
|
+
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
931
|
+
}
|
|
932
|
+
var Node = class {
|
|
933
|
+
#index;
|
|
934
|
+
#varIndex;
|
|
935
|
+
#children = /* @__PURE__ */ Object.create(null);
|
|
936
|
+
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
937
|
+
if (tokens.length === 0) {
|
|
938
|
+
if (this.#index !== void 0) {
|
|
939
|
+
throw PATH_ERROR;
|
|
940
|
+
}
|
|
941
|
+
if (pathErrorCheckOnly) {
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
this.#index = index;
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
const [token, ...restTokens] = tokens;
|
|
948
|
+
const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
949
|
+
let node;
|
|
950
|
+
if (pattern) {
|
|
951
|
+
const name = pattern[1];
|
|
952
|
+
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
|
953
|
+
if (name && pattern[2]) {
|
|
954
|
+
if (regexpStr === ".*") {
|
|
955
|
+
throw PATH_ERROR;
|
|
956
|
+
}
|
|
957
|
+
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
958
|
+
if (/\((?!\?:)/.test(regexpStr)) {
|
|
959
|
+
throw PATH_ERROR;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
node = this.#children[regexpStr];
|
|
963
|
+
if (!node) {
|
|
964
|
+
if (Object.keys(this.#children).some(
|
|
965
|
+
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
966
|
+
)) {
|
|
967
|
+
throw PATH_ERROR;
|
|
968
|
+
}
|
|
969
|
+
if (pathErrorCheckOnly) {
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
node = this.#children[regexpStr] = new Node();
|
|
973
|
+
if (name !== "") {
|
|
974
|
+
node.#varIndex = context.varIndex++;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if (!pathErrorCheckOnly && name !== "") {
|
|
978
|
+
paramMap.push([name, node.#varIndex]);
|
|
979
|
+
}
|
|
980
|
+
} else {
|
|
981
|
+
node = this.#children[token];
|
|
982
|
+
if (!node) {
|
|
983
|
+
if (Object.keys(this.#children).some(
|
|
984
|
+
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
985
|
+
)) {
|
|
986
|
+
throw PATH_ERROR;
|
|
987
|
+
}
|
|
988
|
+
if (pathErrorCheckOnly) {
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
node = this.#children[token] = new Node();
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
995
|
+
}
|
|
996
|
+
buildRegExpStr() {
|
|
997
|
+
const childKeys = Object.keys(this.#children).sort(compareKey);
|
|
998
|
+
const strList = childKeys.map((k) => {
|
|
999
|
+
const c = this.#children[k];
|
|
1000
|
+
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
1001
|
+
});
|
|
1002
|
+
if (typeof this.#index === "number") {
|
|
1003
|
+
strList.unshift(`#${this.#index}`);
|
|
1004
|
+
}
|
|
1005
|
+
if (strList.length === 0) {
|
|
1006
|
+
return "";
|
|
1007
|
+
}
|
|
1008
|
+
if (strList.length === 1) {
|
|
1009
|
+
return strList[0];
|
|
1010
|
+
}
|
|
1011
|
+
return "(?:" + strList.join("|") + ")";
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
1016
|
+
var Trie = class {
|
|
1017
|
+
#context = { varIndex: 0 };
|
|
1018
|
+
#root = new Node();
|
|
1019
|
+
insert(path, index, pathErrorCheckOnly) {
|
|
1020
|
+
const paramAssoc = [];
|
|
1021
|
+
const groups = [];
|
|
1022
|
+
for (let i = 0; ; ) {
|
|
1023
|
+
let replaced = false;
|
|
1024
|
+
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
1025
|
+
const mark = `@\\${i}`;
|
|
1026
|
+
groups[i] = [mark, m];
|
|
1027
|
+
i++;
|
|
1028
|
+
replaced = true;
|
|
1029
|
+
return mark;
|
|
1030
|
+
});
|
|
1031
|
+
if (!replaced) {
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
1036
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1037
|
+
const [mark] = groups[i];
|
|
1038
|
+
for (let j = tokens.length - 1; j >= 0; j--) {
|
|
1039
|
+
if (tokens[j].indexOf(mark) !== -1) {
|
|
1040
|
+
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
1041
|
+
break;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
1046
|
+
return paramAssoc;
|
|
1047
|
+
}
|
|
1048
|
+
buildRegExp() {
|
|
1049
|
+
let regexp = this.#root.buildRegExpStr();
|
|
1050
|
+
if (regexp === "") {
|
|
1051
|
+
return [/^$/, [], []];
|
|
1052
|
+
}
|
|
1053
|
+
let captureIndex = 0;
|
|
1054
|
+
const indexReplacementMap = [];
|
|
1055
|
+
const paramReplacementMap = [];
|
|
1056
|
+
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
1057
|
+
if (handlerIndex !== void 0) {
|
|
1058
|
+
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
1059
|
+
return "$()";
|
|
1060
|
+
}
|
|
1061
|
+
if (paramIndex !== void 0) {
|
|
1062
|
+
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
1063
|
+
return "";
|
|
1064
|
+
}
|
|
1065
|
+
return "";
|
|
1066
|
+
});
|
|
1067
|
+
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/reg-exp-router/router.js
|
|
1072
|
+
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
1073
|
+
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1074
|
+
function buildWildcardRegExp(path) {
|
|
1075
|
+
return wildcardRegExpCache[path] ??= new RegExp(
|
|
1076
|
+
path === "*" ? "" : `^${path.replace(
|
|
1077
|
+
/\/\*$|([.\\+*[^\]$()])/g,
|
|
1078
|
+
(_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)"
|
|
1079
|
+
)}$`
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
function clearWildcardRegExpCache() {
|
|
1083
|
+
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1084
|
+
}
|
|
1085
|
+
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
1086
|
+
const trie = new Trie();
|
|
1087
|
+
const handlerData = [];
|
|
1088
|
+
if (routes.length === 0) {
|
|
1089
|
+
return nullMatcher;
|
|
1090
|
+
}
|
|
1091
|
+
const routesWithStaticPathFlag = routes.map(
|
|
1092
|
+
(route) => [!/\*|\/:/.test(route[0]), ...route]
|
|
1093
|
+
).sort(
|
|
1094
|
+
([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length
|
|
1095
|
+
);
|
|
1096
|
+
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
1097
|
+
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
1098
|
+
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
1099
|
+
if (pathErrorCheckOnly) {
|
|
1100
|
+
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
1101
|
+
} else {
|
|
1102
|
+
j++;
|
|
1103
|
+
}
|
|
1104
|
+
let paramAssoc;
|
|
1105
|
+
try {
|
|
1106
|
+
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
|
1107
|
+
} catch (e) {
|
|
1108
|
+
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
|
1109
|
+
}
|
|
1110
|
+
if (pathErrorCheckOnly) {
|
|
1111
|
+
continue;
|
|
1112
|
+
}
|
|
1113
|
+
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
1114
|
+
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
1115
|
+
paramCount -= 1;
|
|
1116
|
+
for (; paramCount >= 0; paramCount--) {
|
|
1117
|
+
const [key, value] = paramAssoc[paramCount];
|
|
1118
|
+
paramIndexMap[key] = value;
|
|
1119
|
+
}
|
|
1120
|
+
return [h, paramIndexMap];
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
1124
|
+
for (let i = 0, len = handlerData.length; i < len; i++) {
|
|
1125
|
+
for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
|
|
1126
|
+
const map = handlerData[i][j]?.[1];
|
|
1127
|
+
if (!map) {
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
const keys = Object.keys(map);
|
|
1131
|
+
for (let k = 0, len3 = keys.length; k < len3; k++) {
|
|
1132
|
+
map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
const handlerMap = [];
|
|
1137
|
+
for (const i in indexReplacementMap) {
|
|
1138
|
+
handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
1139
|
+
}
|
|
1140
|
+
return [regexp, handlerMap, staticMap];
|
|
1141
|
+
}
|
|
1142
|
+
function findMiddleware(middleware, path) {
|
|
1143
|
+
if (!middleware) {
|
|
1144
|
+
return void 0;
|
|
1145
|
+
}
|
|
1146
|
+
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
1147
|
+
if (buildWildcardRegExp(k).test(path)) {
|
|
1148
|
+
return [...middleware[k]];
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return void 0;
|
|
1152
|
+
}
|
|
1153
|
+
var RegExpRouter = class {
|
|
1154
|
+
name = "RegExpRouter";
|
|
1155
|
+
#middleware;
|
|
1156
|
+
#routes;
|
|
1157
|
+
constructor() {
|
|
1158
|
+
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1159
|
+
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1160
|
+
}
|
|
1161
|
+
add(method, path, handler) {
|
|
1162
|
+
const middleware = this.#middleware;
|
|
1163
|
+
const routes = this.#routes;
|
|
1164
|
+
if (!middleware || !routes) {
|
|
1165
|
+
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1166
|
+
}
|
|
1167
|
+
if (!middleware[method]) {
|
|
1168
|
+
[middleware, routes].forEach((handlerMap) => {
|
|
1169
|
+
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
1170
|
+
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
1171
|
+
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
1172
|
+
});
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
if (path === "/*") {
|
|
1176
|
+
path = "*";
|
|
1177
|
+
}
|
|
1178
|
+
const paramCount = (path.match(/\/:/g) || []).length;
|
|
1179
|
+
if (/\*$/.test(path)) {
|
|
1180
|
+
const re = buildWildcardRegExp(path);
|
|
1181
|
+
if (method === METHOD_NAME_ALL) {
|
|
1182
|
+
Object.keys(middleware).forEach((m) => {
|
|
1183
|
+
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1184
|
+
});
|
|
1185
|
+
} else {
|
|
1186
|
+
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1187
|
+
}
|
|
1188
|
+
Object.keys(middleware).forEach((m) => {
|
|
1189
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1190
|
+
Object.keys(middleware[m]).forEach((p) => {
|
|
1191
|
+
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
Object.keys(routes).forEach((m) => {
|
|
1196
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1197
|
+
Object.keys(routes[m]).forEach(
|
|
1198
|
+
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
const paths = checkOptionalParameter(path) || [path];
|
|
1205
|
+
for (let i = 0, len = paths.length; i < len; i++) {
|
|
1206
|
+
const path2 = paths[i];
|
|
1207
|
+
Object.keys(routes).forEach((m) => {
|
|
1208
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1209
|
+
routes[m][path2] ||= [
|
|
1210
|
+
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
1211
|
+
];
|
|
1212
|
+
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
match = match;
|
|
1218
|
+
buildAllMatchers() {
|
|
1219
|
+
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1220
|
+
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
1221
|
+
matchers[method] ||= this.#buildMatcher(method);
|
|
1222
|
+
});
|
|
1223
|
+
this.#middleware = this.#routes = void 0;
|
|
1224
|
+
clearWildcardRegExpCache();
|
|
1225
|
+
return matchers;
|
|
1226
|
+
}
|
|
1227
|
+
#buildMatcher(method) {
|
|
1228
|
+
const routes = [];
|
|
1229
|
+
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
1230
|
+
[this.#middleware, this.#routes].forEach((r) => {
|
|
1231
|
+
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
1232
|
+
if (ownRoute.length !== 0) {
|
|
1233
|
+
hasOwnRoute ||= true;
|
|
1234
|
+
routes.push(...ownRoute);
|
|
1235
|
+
} else if (method !== METHOD_NAME_ALL) {
|
|
1236
|
+
routes.push(
|
|
1237
|
+
...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]])
|
|
1238
|
+
);
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
if (!hasOwnRoute) {
|
|
1242
|
+
return null;
|
|
1243
|
+
} else {
|
|
1244
|
+
return buildMatcherFromPreprocessedRoutes(routes);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
|
|
1249
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/smart-router/router.js
|
|
1250
|
+
var SmartRouter = class {
|
|
1251
|
+
name = "SmartRouter";
|
|
1252
|
+
#routers = [];
|
|
1253
|
+
#routes = [];
|
|
1254
|
+
constructor(init) {
|
|
1255
|
+
this.#routers = init.routers;
|
|
1256
|
+
}
|
|
1257
|
+
add(method, path, handler) {
|
|
1258
|
+
if (!this.#routes) {
|
|
1259
|
+
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1260
|
+
}
|
|
1261
|
+
this.#routes.push([method, path, handler]);
|
|
1262
|
+
}
|
|
1263
|
+
match(method, path) {
|
|
1264
|
+
if (!this.#routes) {
|
|
1265
|
+
throw new Error("Fatal error");
|
|
1266
|
+
}
|
|
1267
|
+
const routers = this.#routers;
|
|
1268
|
+
const routes = this.#routes;
|
|
1269
|
+
const len = routers.length;
|
|
1270
|
+
let i = 0;
|
|
1271
|
+
let res;
|
|
1272
|
+
for (; i < len; i++) {
|
|
1273
|
+
const router = routers[i];
|
|
1274
|
+
try {
|
|
1275
|
+
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
|
|
1276
|
+
router.add(...routes[i2]);
|
|
1277
|
+
}
|
|
1278
|
+
res = router.match(method, path);
|
|
1279
|
+
} catch (e) {
|
|
1280
|
+
if (e instanceof UnsupportedPathError) {
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
throw e;
|
|
1284
|
+
}
|
|
1285
|
+
this.match = router.match.bind(router);
|
|
1286
|
+
this.#routers = [router];
|
|
1287
|
+
this.#routes = void 0;
|
|
1288
|
+
break;
|
|
1289
|
+
}
|
|
1290
|
+
if (i === len) {
|
|
1291
|
+
throw new Error("Fatal error");
|
|
1292
|
+
}
|
|
1293
|
+
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
1294
|
+
return res;
|
|
1295
|
+
}
|
|
1296
|
+
get activeRouter() {
|
|
1297
|
+
if (this.#routes || this.#routers.length !== 1) {
|
|
1298
|
+
throw new Error("No active router has been determined yet.");
|
|
1299
|
+
}
|
|
1300
|
+
return this.#routers[0];
|
|
1301
|
+
}
|
|
1302
|
+
};
|
|
1303
|
+
|
|
1304
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/trie-router/node.js
|
|
1305
|
+
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
1306
|
+
var Node2 = class {
|
|
1307
|
+
#methods;
|
|
1308
|
+
#children;
|
|
1309
|
+
#patterns;
|
|
1310
|
+
#order = 0;
|
|
1311
|
+
#params = emptyParams;
|
|
1312
|
+
constructor(method, handler, children) {
|
|
1313
|
+
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
1314
|
+
this.#methods = [];
|
|
1315
|
+
if (method && handler) {
|
|
1316
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
1317
|
+
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
1318
|
+
this.#methods = [m];
|
|
1319
|
+
}
|
|
1320
|
+
this.#patterns = [];
|
|
1321
|
+
}
|
|
1322
|
+
insert(method, path, handler) {
|
|
1323
|
+
this.#order = ++this.#order;
|
|
1324
|
+
let curNode = this;
|
|
1325
|
+
const parts = splitRoutingPath(path);
|
|
1326
|
+
const possibleKeys = [];
|
|
1327
|
+
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1328
|
+
const p = parts[i];
|
|
1329
|
+
const nextP = parts[i + 1];
|
|
1330
|
+
const pattern = getPattern(p, nextP);
|
|
1331
|
+
const key = Array.isArray(pattern) ? pattern[0] : p;
|
|
1332
|
+
if (key in curNode.#children) {
|
|
1333
|
+
curNode = curNode.#children[key];
|
|
1334
|
+
if (pattern) {
|
|
1335
|
+
possibleKeys.push(pattern[1]);
|
|
1336
|
+
}
|
|
1337
|
+
continue;
|
|
1338
|
+
}
|
|
1339
|
+
curNode.#children[key] = new Node2();
|
|
1340
|
+
if (pattern) {
|
|
1341
|
+
curNode.#patterns.push(pattern);
|
|
1342
|
+
possibleKeys.push(pattern[1]);
|
|
1343
|
+
}
|
|
1344
|
+
curNode = curNode.#children[key];
|
|
1345
|
+
}
|
|
1346
|
+
curNode.#methods.push({
|
|
1347
|
+
[method]: {
|
|
1348
|
+
handler,
|
|
1349
|
+
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
1350
|
+
score: this.#order
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
return curNode;
|
|
1354
|
+
}
|
|
1355
|
+
#getHandlerSets(node, method, nodeParams, params) {
|
|
1356
|
+
const handlerSets = [];
|
|
1357
|
+
for (let i = 0, len = node.#methods.length; i < len; i++) {
|
|
1358
|
+
const m = node.#methods[i];
|
|
1359
|
+
const handlerSet = m[method] || m[METHOD_NAME_ALL];
|
|
1360
|
+
const processedSet = {};
|
|
1361
|
+
if (handlerSet !== void 0) {
|
|
1362
|
+
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
1363
|
+
handlerSets.push(handlerSet);
|
|
1364
|
+
if (nodeParams !== emptyParams || params && params !== emptyParams) {
|
|
1365
|
+
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
1366
|
+
const key = handlerSet.possibleKeys[i2];
|
|
1367
|
+
const processed = processedSet[handlerSet.score];
|
|
1368
|
+
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
1369
|
+
processedSet[handlerSet.score] = true;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
return handlerSets;
|
|
1375
|
+
}
|
|
1376
|
+
search(method, path) {
|
|
1377
|
+
const handlerSets = [];
|
|
1378
|
+
this.#params = emptyParams;
|
|
1379
|
+
const curNode = this;
|
|
1380
|
+
let curNodes = [curNode];
|
|
1381
|
+
const parts = splitPath(path);
|
|
1382
|
+
const curNodesQueue = [];
|
|
1383
|
+
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1384
|
+
const part = parts[i];
|
|
1385
|
+
const isLast = i === len - 1;
|
|
1386
|
+
const tempNodes = [];
|
|
1387
|
+
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
1388
|
+
const node = curNodes[j];
|
|
1389
|
+
const nextNode = node.#children[part];
|
|
1390
|
+
if (nextNode) {
|
|
1391
|
+
nextNode.#params = node.#params;
|
|
1392
|
+
if (isLast) {
|
|
1393
|
+
if (nextNode.#children["*"]) {
|
|
1394
|
+
handlerSets.push(
|
|
1395
|
+
...this.#getHandlerSets(nextNode.#children["*"], method, node.#params)
|
|
1396
|
+
);
|
|
1397
|
+
}
|
|
1398
|
+
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
|
|
1399
|
+
} else {
|
|
1400
|
+
tempNodes.push(nextNode);
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
|
|
1404
|
+
const pattern = node.#patterns[k];
|
|
1405
|
+
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
1406
|
+
if (pattern === "*") {
|
|
1407
|
+
const astNode = node.#children["*"];
|
|
1408
|
+
if (astNode) {
|
|
1409
|
+
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
|
|
1410
|
+
astNode.#params = params;
|
|
1411
|
+
tempNodes.push(astNode);
|
|
1412
|
+
}
|
|
1413
|
+
continue;
|
|
1414
|
+
}
|
|
1415
|
+
const [key, name, matcher] = pattern;
|
|
1416
|
+
if (!part && !(matcher instanceof RegExp)) {
|
|
1417
|
+
continue;
|
|
1418
|
+
}
|
|
1419
|
+
const child = node.#children[key];
|
|
1420
|
+
const restPathString = parts.slice(i).join("/");
|
|
1421
|
+
if (matcher instanceof RegExp) {
|
|
1422
|
+
const m = matcher.exec(restPathString);
|
|
1423
|
+
if (m) {
|
|
1424
|
+
params[name] = m[0];
|
|
1425
|
+
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
1426
|
+
if (Object.keys(child.#children).length) {
|
|
1427
|
+
child.#params = params;
|
|
1428
|
+
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
1429
|
+
const targetCurNodes = curNodesQueue[componentCount] ||= [];
|
|
1430
|
+
targetCurNodes.push(child);
|
|
1431
|
+
}
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
if (matcher === true || matcher.test(part)) {
|
|
1436
|
+
params[name] = part;
|
|
1437
|
+
if (isLast) {
|
|
1438
|
+
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
1439
|
+
if (child.#children["*"]) {
|
|
1440
|
+
handlerSets.push(
|
|
1441
|
+
...this.#getHandlerSets(child.#children["*"], method, params, node.#params)
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1444
|
+
} else {
|
|
1445
|
+
child.#params = params;
|
|
1446
|
+
tempNodes.push(child);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
|
|
1452
|
+
}
|
|
1453
|
+
if (handlerSets.length > 1) {
|
|
1454
|
+
handlerSets.sort((a, b) => {
|
|
1455
|
+
return a.score - b.score;
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
1459
|
+
}
|
|
1460
|
+
};
|
|
1461
|
+
|
|
1462
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/router/trie-router/router.js
|
|
1463
|
+
var TrieRouter = class {
|
|
1464
|
+
name = "TrieRouter";
|
|
1465
|
+
#node;
|
|
1466
|
+
constructor() {
|
|
1467
|
+
this.#node = new Node2();
|
|
1468
|
+
}
|
|
1469
|
+
add(method, path, handler) {
|
|
1470
|
+
const results = checkOptionalParameter(path);
|
|
1471
|
+
if (results) {
|
|
1472
|
+
for (let i = 0, len = results.length; i < len; i++) {
|
|
1473
|
+
this.#node.insert(method, results[i], handler);
|
|
1474
|
+
}
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
this.#node.insert(method, path, handler);
|
|
1478
|
+
}
|
|
1479
|
+
match(method, path) {
|
|
1480
|
+
return this.#node.search(method, path);
|
|
1481
|
+
}
|
|
1482
|
+
};
|
|
1483
|
+
|
|
1484
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/hono.js
|
|
1485
|
+
var Hono2 = class extends Hono {
|
|
1486
|
+
constructor(options = {}) {
|
|
1487
|
+
super(options);
|
|
1488
|
+
this.router = options.router ?? new SmartRouter({
|
|
1489
|
+
routers: [new RegExpRouter(), new TrieRouter()]
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
|
|
1494
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/middleware/cors/index.js
|
|
1495
|
+
var cors = (options) => {
|
|
1496
|
+
const defaults = {
|
|
1497
|
+
origin: "*",
|
|
1498
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
|
|
1499
|
+
allowHeaders: [],
|
|
1500
|
+
exposeHeaders: []
|
|
1501
|
+
};
|
|
1502
|
+
const opts = {
|
|
1503
|
+
...defaults,
|
|
1504
|
+
...options
|
|
1505
|
+
};
|
|
1506
|
+
const findAllowOrigin = ((optsOrigin) => {
|
|
1507
|
+
if (typeof optsOrigin === "string") {
|
|
1508
|
+
if (optsOrigin === "*") {
|
|
1509
|
+
return () => optsOrigin;
|
|
1510
|
+
} else {
|
|
1511
|
+
return (origin) => optsOrigin === origin ? origin : null;
|
|
1512
|
+
}
|
|
1513
|
+
} else if (typeof optsOrigin === "function") {
|
|
1514
|
+
return optsOrigin;
|
|
1515
|
+
} else {
|
|
1516
|
+
return (origin) => optsOrigin.includes(origin) ? origin : null;
|
|
1517
|
+
}
|
|
1518
|
+
})(opts.origin);
|
|
1519
|
+
const findAllowMethods = ((optsAllowMethods) => {
|
|
1520
|
+
if (typeof optsAllowMethods === "function") {
|
|
1521
|
+
return optsAllowMethods;
|
|
1522
|
+
} else if (Array.isArray(optsAllowMethods)) {
|
|
1523
|
+
return () => optsAllowMethods;
|
|
1524
|
+
} else {
|
|
1525
|
+
return () => [];
|
|
1526
|
+
}
|
|
1527
|
+
})(opts.allowMethods);
|
|
1528
|
+
return async function cors2(c, next) {
|
|
1529
|
+
function set(key, value) {
|
|
1530
|
+
c.res.headers.set(key, value);
|
|
1531
|
+
}
|
|
1532
|
+
const allowOrigin = await findAllowOrigin(c.req.header("origin") || "", c);
|
|
1533
|
+
if (allowOrigin) {
|
|
1534
|
+
set("Access-Control-Allow-Origin", allowOrigin);
|
|
1535
|
+
}
|
|
1536
|
+
if (opts.credentials) {
|
|
1537
|
+
set("Access-Control-Allow-Credentials", "true");
|
|
1538
|
+
}
|
|
1539
|
+
if (opts.exposeHeaders?.length) {
|
|
1540
|
+
set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
|
1541
|
+
}
|
|
1542
|
+
if (c.req.method === "OPTIONS") {
|
|
1543
|
+
if (opts.origin !== "*") {
|
|
1544
|
+
set("Vary", "Origin");
|
|
1545
|
+
}
|
|
1546
|
+
if (opts.maxAge != null) {
|
|
1547
|
+
set("Access-Control-Max-Age", opts.maxAge.toString());
|
|
1548
|
+
}
|
|
1549
|
+
const allowMethods = await findAllowMethods(c.req.header("origin") || "", c);
|
|
1550
|
+
if (allowMethods.length) {
|
|
1551
|
+
set("Access-Control-Allow-Methods", allowMethods.join(","));
|
|
1552
|
+
}
|
|
1553
|
+
let headers = opts.allowHeaders;
|
|
1554
|
+
if (!headers?.length) {
|
|
1555
|
+
const requestHeaders = c.req.header("Access-Control-Request-Headers");
|
|
1556
|
+
if (requestHeaders) {
|
|
1557
|
+
headers = requestHeaders.split(/\s*,\s*/);
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
if (headers?.length) {
|
|
1561
|
+
set("Access-Control-Allow-Headers", headers.join(","));
|
|
1562
|
+
c.res.headers.append("Vary", "Access-Control-Request-Headers");
|
|
1563
|
+
}
|
|
1564
|
+
c.res.headers.delete("Content-Length");
|
|
1565
|
+
c.res.headers.delete("Content-Type");
|
|
1566
|
+
return new Response(null, {
|
|
1567
|
+
headers: c.res.headers,
|
|
1568
|
+
status: 204,
|
|
1569
|
+
statusText: "No Content"
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
await next();
|
|
1573
|
+
if (opts.origin !== "*") {
|
|
1574
|
+
c.header("Vary", "Origin", { append: true });
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
};
|
|
1578
|
+
|
|
1579
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/utils/stream.js
|
|
1580
|
+
var StreamingApi = class {
|
|
1581
|
+
writer;
|
|
1582
|
+
encoder;
|
|
1583
|
+
writable;
|
|
1584
|
+
abortSubscribers = [];
|
|
1585
|
+
responseReadable;
|
|
1586
|
+
aborted = false;
|
|
1587
|
+
closed = false;
|
|
1588
|
+
constructor(writable, _readable) {
|
|
1589
|
+
this.writable = writable;
|
|
1590
|
+
this.writer = writable.getWriter();
|
|
1591
|
+
this.encoder = new TextEncoder();
|
|
1592
|
+
const reader = _readable.getReader();
|
|
1593
|
+
this.abortSubscribers.push(async () => {
|
|
1594
|
+
await reader.cancel();
|
|
1595
|
+
});
|
|
1596
|
+
this.responseReadable = new ReadableStream({
|
|
1597
|
+
async pull(controller) {
|
|
1598
|
+
const { done, value } = await reader.read();
|
|
1599
|
+
done ? controller.close() : controller.enqueue(value);
|
|
1600
|
+
},
|
|
1601
|
+
cancel: () => {
|
|
1602
|
+
this.abort();
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
async write(input) {
|
|
1607
|
+
try {
|
|
1608
|
+
if (typeof input === "string") {
|
|
1609
|
+
input = this.encoder.encode(input);
|
|
1610
|
+
}
|
|
1611
|
+
await this.writer.write(input);
|
|
1612
|
+
} catch {
|
|
1613
|
+
}
|
|
1614
|
+
return this;
|
|
1615
|
+
}
|
|
1616
|
+
async writeln(input) {
|
|
1617
|
+
await this.write(input + "\n");
|
|
1618
|
+
return this;
|
|
1619
|
+
}
|
|
1620
|
+
sleep(ms) {
|
|
1621
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
1622
|
+
}
|
|
1623
|
+
async close() {
|
|
1624
|
+
try {
|
|
1625
|
+
await this.writer.close();
|
|
1626
|
+
} catch {
|
|
1627
|
+
}
|
|
1628
|
+
this.closed = true;
|
|
1629
|
+
}
|
|
1630
|
+
async pipe(body) {
|
|
1631
|
+
this.writer.releaseLock();
|
|
1632
|
+
await body.pipeTo(this.writable, { preventClose: true });
|
|
1633
|
+
this.writer = this.writable.getWriter();
|
|
1634
|
+
}
|
|
1635
|
+
onAbort(listener) {
|
|
1636
|
+
this.abortSubscribers.push(listener);
|
|
1637
|
+
}
|
|
1638
|
+
abort() {
|
|
1639
|
+
if (!this.aborted) {
|
|
1640
|
+
this.aborted = true;
|
|
1641
|
+
this.abortSubscribers.forEach((subscriber) => subscriber());
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
};
|
|
1645
|
+
|
|
1646
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/helper/streaming/utils.js
|
|
1647
|
+
var isOldBunVersion = () => {
|
|
1648
|
+
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
1649
|
+
if (version === void 0) {
|
|
1650
|
+
return false;
|
|
1651
|
+
}
|
|
1652
|
+
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
|
1653
|
+
isOldBunVersion = () => result;
|
|
1654
|
+
return result;
|
|
1655
|
+
};
|
|
1656
|
+
|
|
1657
|
+
// ../../node_modules/.pnpm/hono@4.10.7/node_modules/hono/dist/helper/streaming/sse.js
|
|
1658
|
+
var SSEStreamingApi = class extends StreamingApi {
|
|
1659
|
+
constructor(writable, readable) {
|
|
1660
|
+
super(writable, readable);
|
|
1661
|
+
}
|
|
1662
|
+
async writeSSE(message) {
|
|
1663
|
+
const data = await resolveCallback(message.data, HtmlEscapedCallbackPhase.Stringify, false, {});
|
|
1664
|
+
const dataLines = data.split("\n").map((line) => {
|
|
1665
|
+
return `data: ${line}`;
|
|
1666
|
+
}).join("\n");
|
|
1667
|
+
const sseData = [
|
|
1668
|
+
message.event && `event: ${message.event}`,
|
|
1669
|
+
dataLines,
|
|
1670
|
+
message.id && `id: ${message.id}`,
|
|
1671
|
+
message.retry && `retry: ${message.retry}`
|
|
1672
|
+
].filter(Boolean).join("\n") + "\n\n";
|
|
1673
|
+
await this.write(sseData);
|
|
1674
|
+
}
|
|
1675
|
+
};
|
|
1676
|
+
var run = async (stream2, cb, onError) => {
|
|
1677
|
+
try {
|
|
1678
|
+
await cb(stream2);
|
|
1679
|
+
} catch (e) {
|
|
1680
|
+
{
|
|
1681
|
+
console.error(e);
|
|
1682
|
+
}
|
|
1683
|
+
} finally {
|
|
1684
|
+
stream2.close();
|
|
1685
|
+
}
|
|
1686
|
+
};
|
|
1687
|
+
var contextStash = /* @__PURE__ */ new WeakMap();
|
|
1688
|
+
var streamSSE = (c, cb, onError) => {
|
|
1689
|
+
const { readable, writable } = new TransformStream();
|
|
1690
|
+
const stream2 = new SSEStreamingApi(writable, readable);
|
|
1691
|
+
if (isOldBunVersion()) {
|
|
1692
|
+
c.req.raw.signal.addEventListener("abort", () => {
|
|
1693
|
+
if (!stream2.closed) {
|
|
1694
|
+
stream2.abort();
|
|
1695
|
+
}
|
|
1696
|
+
});
|
|
1697
|
+
}
|
|
1698
|
+
contextStash.set(stream2.responseReadable, c);
|
|
1699
|
+
c.header("Transfer-Encoding", "chunked");
|
|
1700
|
+
c.header("Content-Type", "text/event-stream");
|
|
1701
|
+
c.header("Cache-Control", "no-cache");
|
|
1702
|
+
c.header("Connection", "keep-alive");
|
|
1703
|
+
run(stream2, cb);
|
|
1704
|
+
return c.newResponse(stream2.responseReadable);
|
|
1705
|
+
};
|
|
1706
|
+
var RequestError = class extends Error {
|
|
1707
|
+
constructor(message, options) {
|
|
1708
|
+
super(message, options);
|
|
1709
|
+
this.name = "RequestError";
|
|
1710
|
+
}
|
|
1711
|
+
};
|
|
1712
|
+
var toRequestError = (e) => {
|
|
1713
|
+
if (e instanceof RequestError) {
|
|
1714
|
+
return e;
|
|
1715
|
+
}
|
|
1716
|
+
return new RequestError(e.message, { cause: e });
|
|
1717
|
+
};
|
|
1718
|
+
var GlobalRequest = global.Request;
|
|
1719
|
+
var Request2 = class extends GlobalRequest {
|
|
1720
|
+
constructor(input, options) {
|
|
1721
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
1722
|
+
input = input[getRequestCache]();
|
|
1723
|
+
}
|
|
1724
|
+
if (typeof options?.body?.getReader !== "undefined") {
|
|
1725
|
+
options.duplex ??= "half";
|
|
1726
|
+
}
|
|
1727
|
+
super(input, options);
|
|
1728
|
+
}
|
|
1729
|
+
};
|
|
1730
|
+
var newHeadersFromIncoming = (incoming) => {
|
|
1731
|
+
const headerRecord = [];
|
|
1732
|
+
const rawHeaders = incoming.rawHeaders;
|
|
1733
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
1734
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
1735
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
1736
|
+
58) {
|
|
1737
|
+
headerRecord.push([key, value]);
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
return new Headers(headerRecord);
|
|
1741
|
+
};
|
|
1742
|
+
var wrapBodyStream = Symbol("wrapBodyStream");
|
|
1743
|
+
var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
|
|
1744
|
+
const init = {
|
|
1745
|
+
method,
|
|
1746
|
+
headers,
|
|
1747
|
+
signal: abortController.signal
|
|
1748
|
+
};
|
|
1749
|
+
if (method === "TRACE") {
|
|
1750
|
+
init.method = "GET";
|
|
1751
|
+
const req = new Request2(url, init);
|
|
1752
|
+
Object.defineProperty(req, "method", {
|
|
1753
|
+
get() {
|
|
1754
|
+
return "TRACE";
|
|
1755
|
+
}
|
|
1756
|
+
});
|
|
1757
|
+
return req;
|
|
1758
|
+
}
|
|
1759
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
1760
|
+
if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) {
|
|
1761
|
+
init.body = new ReadableStream({
|
|
1762
|
+
start(controller) {
|
|
1763
|
+
controller.enqueue(incoming.rawBody);
|
|
1764
|
+
controller.close();
|
|
1765
|
+
}
|
|
1766
|
+
});
|
|
1767
|
+
} else if (incoming[wrapBodyStream]) {
|
|
1768
|
+
let reader;
|
|
1769
|
+
init.body = new ReadableStream({
|
|
1770
|
+
async pull(controller) {
|
|
1771
|
+
try {
|
|
1772
|
+
reader ||= Readable.toWeb(incoming).getReader();
|
|
1773
|
+
const { done, value } = await reader.read();
|
|
1774
|
+
if (done) {
|
|
1775
|
+
controller.close();
|
|
1776
|
+
} else {
|
|
1777
|
+
controller.enqueue(value);
|
|
1778
|
+
}
|
|
1779
|
+
} catch (error) {
|
|
1780
|
+
controller.error(error);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
});
|
|
1784
|
+
} else {
|
|
1785
|
+
init.body = Readable.toWeb(incoming);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
return new Request2(url, init);
|
|
1789
|
+
};
|
|
1790
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
1791
|
+
var requestCache = Symbol("requestCache");
|
|
1792
|
+
var incomingKey = Symbol("incomingKey");
|
|
1793
|
+
var urlKey = Symbol("urlKey");
|
|
1794
|
+
var headersKey = Symbol("headersKey");
|
|
1795
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
1796
|
+
var getAbortController = Symbol("getAbortController");
|
|
1797
|
+
var requestPrototype = {
|
|
1798
|
+
get method() {
|
|
1799
|
+
return this[incomingKey].method || "GET";
|
|
1800
|
+
},
|
|
1801
|
+
get url() {
|
|
1802
|
+
return this[urlKey];
|
|
1803
|
+
},
|
|
1804
|
+
get headers() {
|
|
1805
|
+
return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
|
|
1806
|
+
},
|
|
1807
|
+
[getAbortController]() {
|
|
1808
|
+
this[getRequestCache]();
|
|
1809
|
+
return this[abortControllerKey];
|
|
1810
|
+
},
|
|
1811
|
+
[getRequestCache]() {
|
|
1812
|
+
this[abortControllerKey] ||= new AbortController();
|
|
1813
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
1814
|
+
this.method,
|
|
1815
|
+
this[urlKey],
|
|
1816
|
+
this.headers,
|
|
1817
|
+
this[incomingKey],
|
|
1818
|
+
this[abortControllerKey]
|
|
1819
|
+
);
|
|
1820
|
+
}
|
|
1821
|
+
};
|
|
1822
|
+
[
|
|
1823
|
+
"body",
|
|
1824
|
+
"bodyUsed",
|
|
1825
|
+
"cache",
|
|
1826
|
+
"credentials",
|
|
1827
|
+
"destination",
|
|
1828
|
+
"integrity",
|
|
1829
|
+
"mode",
|
|
1830
|
+
"redirect",
|
|
1831
|
+
"referrer",
|
|
1832
|
+
"referrerPolicy",
|
|
1833
|
+
"signal",
|
|
1834
|
+
"keepalive"
|
|
1835
|
+
].forEach((k) => {
|
|
1836
|
+
Object.defineProperty(requestPrototype, k, {
|
|
1837
|
+
get() {
|
|
1838
|
+
return this[getRequestCache]()[k];
|
|
1839
|
+
}
|
|
1840
|
+
});
|
|
1841
|
+
});
|
|
1842
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
1843
|
+
Object.defineProperty(requestPrototype, k, {
|
|
1844
|
+
value: function() {
|
|
1845
|
+
return this[getRequestCache]()[k]();
|
|
1846
|
+
}
|
|
1847
|
+
});
|
|
1848
|
+
});
|
|
1849
|
+
Object.setPrototypeOf(requestPrototype, Request2.prototype);
|
|
1850
|
+
var newRequest = (incoming, defaultHostname) => {
|
|
1851
|
+
const req = Object.create(requestPrototype);
|
|
1852
|
+
req[incomingKey] = incoming;
|
|
1853
|
+
const incomingUrl = incoming.url || "";
|
|
1854
|
+
if (incomingUrl[0] !== "/" && // short-circuit for performance. most requests are relative URL.
|
|
1855
|
+
(incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
|
|
1856
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
1857
|
+
throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
|
|
1858
|
+
}
|
|
1859
|
+
try {
|
|
1860
|
+
const url2 = new URL(incomingUrl);
|
|
1861
|
+
req[urlKey] = url2.href;
|
|
1862
|
+
} catch (e) {
|
|
1863
|
+
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
1864
|
+
}
|
|
1865
|
+
return req;
|
|
1866
|
+
}
|
|
1867
|
+
const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
|
|
1868
|
+
if (!host) {
|
|
1869
|
+
throw new RequestError("Missing host header");
|
|
1870
|
+
}
|
|
1871
|
+
let scheme;
|
|
1872
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
1873
|
+
scheme = incoming.scheme;
|
|
1874
|
+
if (!(scheme === "http" || scheme === "https")) {
|
|
1875
|
+
throw new RequestError("Unsupported scheme");
|
|
1876
|
+
}
|
|
1877
|
+
} else {
|
|
1878
|
+
scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
|
|
1879
|
+
}
|
|
1880
|
+
const url = new URL(`${scheme}://${host}${incomingUrl}`);
|
|
1881
|
+
if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
|
|
1882
|
+
throw new RequestError("Invalid host header");
|
|
1883
|
+
}
|
|
1884
|
+
req[urlKey] = url.href;
|
|
1885
|
+
return req;
|
|
1886
|
+
};
|
|
1887
|
+
var responseCache = Symbol("responseCache");
|
|
1888
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
1889
|
+
var cacheKey = Symbol("cache");
|
|
1890
|
+
var GlobalResponse = global.Response;
|
|
1891
|
+
var Response2 = class _Response {
|
|
1892
|
+
#body;
|
|
1893
|
+
#init;
|
|
1894
|
+
[getResponseCache]() {
|
|
1895
|
+
delete this[cacheKey];
|
|
1896
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
1897
|
+
}
|
|
1898
|
+
constructor(body, init) {
|
|
1899
|
+
let headers;
|
|
1900
|
+
this.#body = body;
|
|
1901
|
+
if (init instanceof _Response) {
|
|
1902
|
+
const cachedGlobalResponse = init[responseCache];
|
|
1903
|
+
if (cachedGlobalResponse) {
|
|
1904
|
+
this.#init = cachedGlobalResponse;
|
|
1905
|
+
this[getResponseCache]();
|
|
1906
|
+
return;
|
|
1907
|
+
} else {
|
|
1908
|
+
this.#init = init.#init;
|
|
1909
|
+
headers = new Headers(init.#init.headers);
|
|
1910
|
+
}
|
|
1911
|
+
} else {
|
|
1912
|
+
this.#init = init;
|
|
1913
|
+
}
|
|
1914
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
1915
|
+
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
1916
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
get headers() {
|
|
1920
|
+
const cache = this[cacheKey];
|
|
1921
|
+
if (cache) {
|
|
1922
|
+
if (!(cache[2] instanceof Headers)) {
|
|
1923
|
+
cache[2] = new Headers(cache[2]);
|
|
1924
|
+
}
|
|
1925
|
+
return cache[2];
|
|
1926
|
+
}
|
|
1927
|
+
return this[getResponseCache]().headers;
|
|
1928
|
+
}
|
|
1929
|
+
get status() {
|
|
1930
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
1931
|
+
}
|
|
1932
|
+
get ok() {
|
|
1933
|
+
const status = this.status;
|
|
1934
|
+
return status >= 200 && status < 300;
|
|
1935
|
+
}
|
|
1936
|
+
};
|
|
1937
|
+
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
1938
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
1939
|
+
get() {
|
|
1940
|
+
return this[getResponseCache]()[k];
|
|
1941
|
+
}
|
|
1942
|
+
});
|
|
1943
|
+
});
|
|
1944
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
1945
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
1946
|
+
value: function() {
|
|
1947
|
+
return this[getResponseCache]()[k]();
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
});
|
|
1951
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
1952
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
1953
|
+
async function readWithoutBlocking(readPromise) {
|
|
1954
|
+
return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
|
|
1955
|
+
}
|
|
1956
|
+
function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
|
|
1957
|
+
const cancel = (error) => {
|
|
1958
|
+
reader.cancel(error).catch(() => {
|
|
1959
|
+
});
|
|
1960
|
+
};
|
|
1961
|
+
writable.on("close", cancel);
|
|
1962
|
+
writable.on("error", cancel);
|
|
1963
|
+
(currentReadPromise ?? reader.read()).then(flow, handleStreamError);
|
|
1964
|
+
return reader.closed.finally(() => {
|
|
1965
|
+
writable.off("close", cancel);
|
|
1966
|
+
writable.off("error", cancel);
|
|
1967
|
+
});
|
|
1968
|
+
function handleStreamError(error) {
|
|
1969
|
+
if (error) {
|
|
1970
|
+
writable.destroy(error);
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
function onDrain() {
|
|
1974
|
+
reader.read().then(flow, handleStreamError);
|
|
1975
|
+
}
|
|
1976
|
+
function flow({ done, value }) {
|
|
1977
|
+
try {
|
|
1978
|
+
if (done) {
|
|
1979
|
+
writable.end();
|
|
1980
|
+
} else if (!writable.write(value)) {
|
|
1981
|
+
writable.once("drain", onDrain);
|
|
1982
|
+
} else {
|
|
1983
|
+
return reader.read().then(flow, handleStreamError);
|
|
1984
|
+
}
|
|
1985
|
+
} catch (e) {
|
|
1986
|
+
handleStreamError(e);
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
function writeFromReadableStream(stream2, writable) {
|
|
1991
|
+
if (stream2.locked) {
|
|
1992
|
+
throw new TypeError("ReadableStream is locked.");
|
|
1993
|
+
} else if (writable.destroyed) {
|
|
1994
|
+
return;
|
|
1995
|
+
}
|
|
1996
|
+
return writeFromReadableStreamDefaultReader(stream2.getReader(), writable);
|
|
1997
|
+
}
|
|
1998
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
1999
|
+
const res = {};
|
|
2000
|
+
if (!(headers instanceof Headers)) {
|
|
2001
|
+
headers = new Headers(headers ?? void 0);
|
|
2002
|
+
}
|
|
2003
|
+
const cookies = [];
|
|
2004
|
+
for (const [k, v] of headers) {
|
|
2005
|
+
if (k === "set-cookie") {
|
|
2006
|
+
cookies.push(v);
|
|
2007
|
+
} else {
|
|
2008
|
+
res[k] = v;
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
if (cookies.length > 0) {
|
|
2012
|
+
res["set-cookie"] = cookies;
|
|
2013
|
+
}
|
|
2014
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
2015
|
+
return res;
|
|
2016
|
+
};
|
|
2017
|
+
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
2018
|
+
var webFetch = global.fetch;
|
|
2019
|
+
if (typeof global.crypto === "undefined") {
|
|
2020
|
+
global.crypto = crypto;
|
|
2021
|
+
}
|
|
2022
|
+
global.fetch = (info, init) => {
|
|
2023
|
+
init = {
|
|
2024
|
+
// Disable compression handling so people can return the result of a fetch
|
|
2025
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
2026
|
+
compress: false,
|
|
2027
|
+
...init
|
|
2028
|
+
};
|
|
2029
|
+
return webFetch(info, init);
|
|
2030
|
+
};
|
|
2031
|
+
var outgoingEnded = Symbol("outgoingEnded");
|
|
2032
|
+
var handleRequestError = () => new Response(null, {
|
|
2033
|
+
status: 400
|
|
2034
|
+
});
|
|
2035
|
+
var handleFetchError = (e) => new Response(null, {
|
|
2036
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
2037
|
+
});
|
|
2038
|
+
var handleResponseError = (e, outgoing) => {
|
|
2039
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
2040
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
2041
|
+
console.info("The user aborted a request.");
|
|
2042
|
+
} else {
|
|
2043
|
+
console.error(e);
|
|
2044
|
+
if (!outgoing.headersSent) {
|
|
2045
|
+
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
2046
|
+
}
|
|
2047
|
+
outgoing.end(`Error: ${err.message}`);
|
|
2048
|
+
outgoing.destroy(err);
|
|
2049
|
+
}
|
|
2050
|
+
};
|
|
2051
|
+
var flushHeaders = (outgoing) => {
|
|
2052
|
+
if ("flushHeaders" in outgoing && outgoing.writable) {
|
|
2053
|
+
outgoing.flushHeaders();
|
|
2054
|
+
}
|
|
2055
|
+
};
|
|
2056
|
+
var responseViaCache = async (res, outgoing) => {
|
|
2057
|
+
let [status, body, header] = res[cacheKey];
|
|
2058
|
+
if (header instanceof Headers) {
|
|
2059
|
+
header = buildOutgoingHttpHeaders(header);
|
|
2060
|
+
}
|
|
2061
|
+
if (typeof body === "string") {
|
|
2062
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
2063
|
+
} else if (body instanceof Uint8Array) {
|
|
2064
|
+
header["Content-Length"] = body.byteLength;
|
|
2065
|
+
} else if (body instanceof Blob) {
|
|
2066
|
+
header["Content-Length"] = body.size;
|
|
2067
|
+
}
|
|
2068
|
+
outgoing.writeHead(status, header);
|
|
2069
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
2070
|
+
outgoing.end(body);
|
|
2071
|
+
} else if (body instanceof Blob) {
|
|
2072
|
+
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
2073
|
+
} else {
|
|
2074
|
+
flushHeaders(outgoing);
|
|
2075
|
+
await writeFromReadableStream(body, outgoing)?.catch(
|
|
2076
|
+
(e) => handleResponseError(e, outgoing)
|
|
2077
|
+
);
|
|
2078
|
+
}
|
|
2079
|
+
outgoing[outgoingEnded]?.();
|
|
2080
|
+
};
|
|
2081
|
+
var isPromise = (res) => typeof res.then === "function";
|
|
2082
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
2083
|
+
if (isPromise(res)) {
|
|
2084
|
+
if (options.errorHandler) {
|
|
2085
|
+
try {
|
|
2086
|
+
res = await res;
|
|
2087
|
+
} catch (err) {
|
|
2088
|
+
const errRes = await options.errorHandler(err);
|
|
2089
|
+
if (!errRes) {
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
res = errRes;
|
|
2093
|
+
}
|
|
2094
|
+
} else {
|
|
2095
|
+
res = await res.catch(handleFetchError);
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
if (cacheKey in res) {
|
|
2099
|
+
return responseViaCache(res, outgoing);
|
|
2100
|
+
}
|
|
2101
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
2102
|
+
if (res.body) {
|
|
2103
|
+
const reader = res.body.getReader();
|
|
2104
|
+
const values = [];
|
|
2105
|
+
let done = false;
|
|
2106
|
+
let currentReadPromise = void 0;
|
|
2107
|
+
if (resHeaderRecord["transfer-encoding"] !== "chunked") {
|
|
2108
|
+
let maxReadCount = 2;
|
|
2109
|
+
for (let i = 0; i < maxReadCount; i++) {
|
|
2110
|
+
currentReadPromise ||= reader.read();
|
|
2111
|
+
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
2112
|
+
console.error(e);
|
|
2113
|
+
done = true;
|
|
2114
|
+
});
|
|
2115
|
+
if (!chunk) {
|
|
2116
|
+
if (i === 1) {
|
|
2117
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
2118
|
+
maxReadCount = 3;
|
|
2119
|
+
continue;
|
|
2120
|
+
}
|
|
2121
|
+
break;
|
|
2122
|
+
}
|
|
2123
|
+
currentReadPromise = void 0;
|
|
2124
|
+
if (chunk.value) {
|
|
2125
|
+
values.push(chunk.value);
|
|
2126
|
+
}
|
|
2127
|
+
if (chunk.done) {
|
|
2128
|
+
done = true;
|
|
2129
|
+
break;
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
if (done && !("content-length" in resHeaderRecord)) {
|
|
2133
|
+
resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
2137
|
+
values.forEach((value) => {
|
|
2138
|
+
outgoing.write(value);
|
|
2139
|
+
});
|
|
2140
|
+
if (done) {
|
|
2141
|
+
outgoing.end();
|
|
2142
|
+
} else {
|
|
2143
|
+
if (values.length === 0) {
|
|
2144
|
+
flushHeaders(outgoing);
|
|
2145
|
+
}
|
|
2146
|
+
await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
|
|
2147
|
+
}
|
|
2148
|
+
} else if (resHeaderRecord[X_ALREADY_SENT]) ; else {
|
|
2149
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
2150
|
+
outgoing.end();
|
|
2151
|
+
}
|
|
2152
|
+
outgoing[outgoingEnded]?.();
|
|
2153
|
+
};
|
|
2154
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
2155
|
+
const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
|
|
2156
|
+
if (options.overrideGlobalObjects !== false && global.Request !== Request2) {
|
|
2157
|
+
Object.defineProperty(global, "Request", {
|
|
2158
|
+
value: Request2
|
|
2159
|
+
});
|
|
2160
|
+
Object.defineProperty(global, "Response", {
|
|
2161
|
+
value: Response2
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
return async (incoming, outgoing) => {
|
|
2165
|
+
let res, req;
|
|
2166
|
+
try {
|
|
2167
|
+
req = newRequest(incoming, options.hostname);
|
|
2168
|
+
let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
|
|
2169
|
+
if (!incomingEnded) {
|
|
2170
|
+
;
|
|
2171
|
+
incoming[wrapBodyStream] = true;
|
|
2172
|
+
incoming.on("end", () => {
|
|
2173
|
+
incomingEnded = true;
|
|
2174
|
+
});
|
|
2175
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
2176
|
+
;
|
|
2177
|
+
outgoing[outgoingEnded] = () => {
|
|
2178
|
+
if (!incomingEnded) {
|
|
2179
|
+
setTimeout(() => {
|
|
2180
|
+
if (!incomingEnded) {
|
|
2181
|
+
setTimeout(() => {
|
|
2182
|
+
incoming.destroy();
|
|
2183
|
+
outgoing.destroy();
|
|
2184
|
+
});
|
|
2185
|
+
}
|
|
2186
|
+
});
|
|
2187
|
+
}
|
|
2188
|
+
};
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
outgoing.on("close", () => {
|
|
2192
|
+
const abortController = req[abortControllerKey];
|
|
2193
|
+
if (abortController) {
|
|
2194
|
+
if (incoming.errored) {
|
|
2195
|
+
req[abortControllerKey].abort(incoming.errored.toString());
|
|
2196
|
+
} else if (!outgoing.writableFinished) {
|
|
2197
|
+
req[abortControllerKey].abort("Client connection prematurely closed.");
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
if (!incomingEnded) {
|
|
2201
|
+
setTimeout(() => {
|
|
2202
|
+
if (!incomingEnded) {
|
|
2203
|
+
setTimeout(() => {
|
|
2204
|
+
incoming.destroy();
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
});
|
|
2208
|
+
}
|
|
2209
|
+
});
|
|
2210
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
2211
|
+
if (cacheKey in res) {
|
|
2212
|
+
return responseViaCache(res, outgoing);
|
|
2213
|
+
}
|
|
2214
|
+
} catch (e) {
|
|
2215
|
+
if (!res) {
|
|
2216
|
+
if (options.errorHandler) {
|
|
2217
|
+
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
2218
|
+
if (!res) {
|
|
2219
|
+
return;
|
|
2220
|
+
}
|
|
2221
|
+
} else if (!req) {
|
|
2222
|
+
res = handleRequestError();
|
|
2223
|
+
} else {
|
|
2224
|
+
res = handleFetchError(e);
|
|
2225
|
+
}
|
|
2226
|
+
} else {
|
|
2227
|
+
return handleResponseError(e, outgoing);
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
try {
|
|
2231
|
+
return await responseViaResponseObject(res, outgoing, options);
|
|
2232
|
+
} catch (e) {
|
|
2233
|
+
return handleResponseError(e, outgoing);
|
|
2234
|
+
}
|
|
2235
|
+
};
|
|
2236
|
+
};
|
|
2237
|
+
var createAdaptorServer = (options) => {
|
|
2238
|
+
const fetchCallback = options.fetch;
|
|
2239
|
+
const requestListener = getRequestListener(fetchCallback, {
|
|
2240
|
+
hostname: options.hostname,
|
|
2241
|
+
overrideGlobalObjects: options.overrideGlobalObjects,
|
|
2242
|
+
autoCleanupIncoming: options.autoCleanupIncoming
|
|
2243
|
+
});
|
|
2244
|
+
const createServer2 = options.createServer || createServer$1;
|
|
2245
|
+
const server = createServer2(options.serverOptions || {}, requestListener);
|
|
2246
|
+
return server;
|
|
2247
|
+
};
|
|
2248
|
+
var serve = (options, listeningListener) => {
|
|
2249
|
+
const server = createAdaptorServer(options);
|
|
2250
|
+
server.listen(options?.port, options.hostname, () => {
|
|
2251
|
+
server.address();
|
|
2252
|
+
});
|
|
2253
|
+
return server;
|
|
2254
|
+
};
|
|
2255
|
+
|
|
2256
|
+
// src/constants.ts
|
|
2257
|
+
var DEFAULT_PORT = 5567;
|
|
2258
|
+
|
|
2259
|
+
// src/server.ts
|
|
2260
|
+
var parseStreamLine = (line) => {
|
|
2261
|
+
const trimmed = line.trim();
|
|
2262
|
+
if (!trimmed) return null;
|
|
2263
|
+
try {
|
|
2264
|
+
return JSON.parse(trimmed);
|
|
2265
|
+
} catch {
|
|
2266
|
+
return null;
|
|
2267
|
+
}
|
|
2268
|
+
};
|
|
2269
|
+
var extractTextFromMessage = (message) => {
|
|
2270
|
+
if (!message?.content) return "";
|
|
2271
|
+
return message.content.filter((block) => block.type === "text").map((block) => block.text).join(" ").trim();
|
|
2272
|
+
};
|
|
2273
|
+
var createServer = () => {
|
|
2274
|
+
const app2 = new Hono2();
|
|
2275
|
+
app2.use("/*", cors());
|
|
2276
|
+
app2.post("/agent", async (context) => {
|
|
2277
|
+
const body = await context.req.json();
|
|
2278
|
+
const { content, prompt, options } = body;
|
|
2279
|
+
const fullPrompt = `${prompt}
|
|
2280
|
+
|
|
2281
|
+
${content}`;
|
|
2282
|
+
return streamSSE(context, async (stream2) => {
|
|
2283
|
+
const cursorAgentArgs = [
|
|
2284
|
+
"--print",
|
|
2285
|
+
"--output-format",
|
|
2286
|
+
"stream-json",
|
|
2287
|
+
"--force"
|
|
2288
|
+
];
|
|
2289
|
+
if (options?.model) {
|
|
2290
|
+
cursorAgentArgs.push("--model", options.model);
|
|
2291
|
+
}
|
|
2292
|
+
if (options?.workspace) {
|
|
2293
|
+
cursorAgentArgs.push("--workspace", options.workspace);
|
|
2294
|
+
} else {
|
|
2295
|
+
cursorAgentArgs.push("--workspace", process.cwd());
|
|
2296
|
+
}
|
|
2297
|
+
try {
|
|
2298
|
+
await stream2.writeSSE({ data: "Starting Cursor Agent...", event: "status" });
|
|
2299
|
+
const cursorProcess = spawn("cursor-agent", cursorAgentArgs, {
|
|
2300
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
2301
|
+
env: { ...process.env }
|
|
2302
|
+
});
|
|
2303
|
+
let buffer = "";
|
|
2304
|
+
const processLine = async (line) => {
|
|
2305
|
+
const event = parseStreamLine(line);
|
|
2306
|
+
if (!event) return;
|
|
2307
|
+
switch (event.type) {
|
|
2308
|
+
case "system":
|
|
2309
|
+
if (event.subtype === "init") {
|
|
2310
|
+
await stream2.writeSSE({ data: "Connected to Cursor Agent", event: "status" });
|
|
2311
|
+
}
|
|
2312
|
+
break;
|
|
2313
|
+
case "thinking":
|
|
2314
|
+
if (event.subtype === "completed") {
|
|
2315
|
+
await stream2.writeSSE({ data: "Processing...", event: "status" });
|
|
2316
|
+
}
|
|
2317
|
+
break;
|
|
2318
|
+
case "assistant": {
|
|
2319
|
+
const text = extractTextFromMessage(event.message);
|
|
2320
|
+
if (text) {
|
|
2321
|
+
const statusUpdate = text.length > 150 ? `${text.slice(0, 150)}...` : text;
|
|
2322
|
+
await stream2.writeSSE({ data: statusUpdate, event: "status" });
|
|
2323
|
+
}
|
|
2324
|
+
break;
|
|
2325
|
+
}
|
|
2326
|
+
case "result":
|
|
2327
|
+
if (event.subtype === "success") {
|
|
2328
|
+
await stream2.writeSSE({ data: "Completed successfully", event: "status" });
|
|
2329
|
+
} else if (event.subtype === "error" || event.is_error) {
|
|
2330
|
+
await stream2.writeSSE({ data: `Error: ${event.result || "Unknown error"}`, event: "error" });
|
|
2331
|
+
} else {
|
|
2332
|
+
await stream2.writeSSE({ data: "Task finished", event: "status" });
|
|
2333
|
+
}
|
|
2334
|
+
break;
|
|
2335
|
+
}
|
|
2336
|
+
};
|
|
2337
|
+
cursorProcess.stdout.on("data", async (chunk) => {
|
|
2338
|
+
buffer += chunk.toString();
|
|
2339
|
+
let newlineIndex;
|
|
2340
|
+
while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
|
|
2341
|
+
const line = buffer.slice(0, newlineIndex);
|
|
2342
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
2343
|
+
await processLine(line);
|
|
2344
|
+
}
|
|
2345
|
+
});
|
|
2346
|
+
cursorProcess.stderr.on("data", (chunk) => {
|
|
2347
|
+
console.error("[cursor-agent stderr]:", chunk.toString());
|
|
2348
|
+
});
|
|
2349
|
+
cursorProcess.stdin.write(fullPrompt);
|
|
2350
|
+
cursorProcess.stdin.end();
|
|
2351
|
+
await new Promise((resolve, reject) => {
|
|
2352
|
+
cursorProcess.on("close", (code) => {
|
|
2353
|
+
if (code === 0) {
|
|
2354
|
+
resolve();
|
|
2355
|
+
} else {
|
|
2356
|
+
reject(new Error(`cursor-agent exited with code ${code}`));
|
|
2357
|
+
}
|
|
2358
|
+
});
|
|
2359
|
+
cursorProcess.on("error", (error) => {
|
|
2360
|
+
reject(error);
|
|
2361
|
+
});
|
|
2362
|
+
});
|
|
2363
|
+
if (buffer.trim()) {
|
|
2364
|
+
await processLine(buffer);
|
|
2365
|
+
}
|
|
2366
|
+
await stream2.writeSSE({ data: "", event: "done" });
|
|
2367
|
+
} catch (error) {
|
|
2368
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
2369
|
+
await stream2.writeSSE({ data: `Error: ${errorMessage}`, event: "error" });
|
|
2370
|
+
await stream2.writeSSE({ data: "", event: "done" });
|
|
2371
|
+
}
|
|
2372
|
+
});
|
|
2373
|
+
});
|
|
2374
|
+
app2.get("/health", (context) => {
|
|
2375
|
+
return context.json({ status: "ok", provider: "cursor" });
|
|
2376
|
+
});
|
|
2377
|
+
return app2;
|
|
2378
|
+
};
|
|
2379
|
+
var app = createServer();
|
|
2380
|
+
serve({
|
|
2381
|
+
fetch: app.fetch,
|
|
2382
|
+
port: DEFAULT_PORT
|
|
2383
|
+
});
|
|
2384
|
+
console.log("React Grab Cursor server running on port", DEFAULT_PORT);
|
|
2385
|
+
|
|
2386
|
+
export { createServer };
|