@jeffreycao/copilot-api 1.9.0-beta.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/README.zh-CN.md +10 -0
- package/dist/{auth-DCB53u05.js → auth-C3UbfaIB.js} +6 -6
- package/dist/{auth-DCB53u05.js.map → auth-C3UbfaIB.js.map} +1 -1
- package/dist/{check-usage-iKUAIfc_.js → check-usage-CELArOu5.js} +6 -6
- package/dist/{check-usage-iKUAIfc_.js.map → check-usage-CELArOu5.js.map} +1 -1
- package/dist/{config-D3dkAXQE.js → config-BQvWqYh_.js} +3 -3
- package/dist/{config-D3dkAXQE.js.map → config-BQvWqYh_.js.map} +1 -1
- package/dist/{debug-CUx-7_jF.js → debug-DcC7ZPH0.js} +4 -4
- package/dist/{debug-CUx-7_jF.js.map → debug-DcC7ZPH0.js.map} +1 -1
- package/dist/main.js +6 -6
- package/dist/{paths-DvrimVju.js → paths-Cla6y5eD.js} +2 -2
- package/dist/{paths-DvrimVju.js.map → paths-Cla6y5eD.js.map} +1 -1
- package/dist/{server-BGqhE4N_.js → server-CA39Gy7x.js} +45 -1647
- package/dist/server-CA39Gy7x.js.map +1 -0
- package/dist/start-DKwPr9vl.js +268 -0
- package/dist/start-DKwPr9vl.js.map +1 -0
- package/dist/{token-BCdEIfN_.js → token-CIwAFD_6.js} +9 -5
- package/dist/token-CIwAFD_6.js.map +1 -0
- package/dist/{utils-DXuuBUT_.js → utils-DG6CB51Z.js} +4 -5
- package/dist/{utils-DXuuBUT_.js.map → utils-DG6CB51Z.js.map} +1 -1
- package/package.json +2 -1
- package/dist/GptEncoding-TiCgsNrQ.js +0 -887
- package/dist/GptEncoding-TiCgsNrQ.js.map +0 -1
- package/dist/chunk-BZ41Y9eH.js +0 -39
- package/dist/cl100k_base-kiDWemr-.js +0 -101375
- package/dist/cl100k_base-kiDWemr-.js.map +0 -1
- package/dist/dist-B3jIqeb6.js +0 -961
- package/dist/dist-B3jIqeb6.js.map +0 -1
- package/dist/dist-CSEoNgAt.js +0 -334
- package/dist/dist-CSEoNgAt.js.map +0 -1
- package/dist/o200k_base-BlrOP1Jc.js +0 -204726
- package/dist/o200k_base-BlrOP1Jc.js.map +0 -1
- package/dist/p50k_base-DA6KYT1Z.js +0 -11
- package/dist/p50k_base-DA6KYT1Z.js.map +0 -1
- package/dist/p50k_base-tS38LhIO.js +0 -50483
- package/dist/p50k_base-tS38LhIO.js.map +0 -1
- package/dist/p50k_edit-BlA1HcF4.js +0 -11
- package/dist/p50k_edit-BlA1HcF4.js.map +0 -1
- package/dist/prompt-CyV2d7YW.js +0 -850
- package/dist/prompt-CyV2d7YW.js.map +0 -1
- package/dist/r50k_base-CaCWe6-8.js +0 -50466
- package/dist/r50k_base-CaCWe6-8.js.map +0 -1
- package/dist/registry-B_2v83L7.js +0 -797
- package/dist/registry-B_2v83L7.js.map +0 -1
- package/dist/server-BGqhE4N_.js.map +0 -1
- package/dist/start-Cxl5pgll.js +0 -27379
- package/dist/start-Cxl5pgll.js.map +0 -1
- package/dist/token-BCdEIfN_.js.map +0 -1
|
@@ -1,1368 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import util from "node:util";
|
|
1
|
+
import { PATHS } from "./paths-Cla6y5eD.js";
|
|
2
|
+
import { COMPACT_AUTO_CONTINUE, COMPACT_REQUEST, HTTPError, cacheModels, compactAutoContinuePromptStarts, compactMessageSections, compactSummaryPromptStart, compactSystemPromptStarts, compactTextOnlyGuard, copilotBaseUrl, copilotHeaders, forwardError, generateRequestIdFromPayload, getCopilotUsage, getRootSessionId, getUUID, isNullish, parseUserIdMetadata, prepareForCompact, prepareInteractionHeaders, prepareMessageProxyHeaders, requestContext, resolveTraceId, sleep, state } from "./utils-DG6CB51Z.js";
|
|
3
|
+
import { getAnthropicApiKey, getClaudeTokenMultiplier, getConfig, getExtraPromptForModel, getProviderConfig, getReasoningEffortForModel, getSmallModel, isMessagesApiEnabled, isResponsesApiContextManagementModel, isResponsesApiWebSearchEnabled } from "./config-BQvWqYh_.js";
|
|
4
|
+
import consola from "consola";
|
|
6
5
|
import path from "node:path";
|
|
6
|
+
import { Hono } from "hono";
|
|
7
|
+
import { cors } from "hono/cors";
|
|
8
|
+
import { logger } from "hono/logger";
|
|
7
9
|
import fs, { readFileSync } from "node:fs";
|
|
10
|
+
import { streamSSE } from "hono/streaming";
|
|
11
|
+
import util from "node:util";
|
|
12
|
+
import { events } from "fetch-event-stream";
|
|
8
13
|
|
|
9
|
-
//#region 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) throw new Error("next() called multiple times");
|
|
16
|
-
index = i;
|
|
17
|
-
let res;
|
|
18
|
-
let isError = false;
|
|
19
|
-
let handler;
|
|
20
|
-
if (middleware[i]) {
|
|
21
|
-
handler = middleware[i][0][0];
|
|
22
|
-
context.req.routeIndex = i;
|
|
23
|
-
} else handler = i === middleware.length && next || void 0;
|
|
24
|
-
if (handler) try {
|
|
25
|
-
res = await handler(context, () => dispatch(i + 1));
|
|
26
|
-
} catch (err) {
|
|
27
|
-
if (err instanceof Error && onError) {
|
|
28
|
-
context.error = err;
|
|
29
|
-
res = await onError(err, context);
|
|
30
|
-
isError = true;
|
|
31
|
-
} else throw err;
|
|
32
|
-
}
|
|
33
|
-
else if (context.finalized === false && onNotFound) res = await onNotFound(context);
|
|
34
|
-
if (res && (context.finalized === false || isError)) context.res = res;
|
|
35
|
-
return context;
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
//#endregion
|
|
41
|
-
//#region node_modules/hono/dist/request/constants.js
|
|
42
|
-
var GET_MATCH_RESULT = Symbol();
|
|
43
|
-
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region node_modules/hono/dist/utils/body.js
|
|
46
|
-
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
47
|
-
const { all = false, dot = false } = options;
|
|
48
|
-
const contentType = (request instanceof HonoRequest ? request.raw.headers : request.headers).get("Content-Type");
|
|
49
|
-
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) return parseFormData(request, {
|
|
50
|
-
all,
|
|
51
|
-
dot
|
|
52
|
-
});
|
|
53
|
-
return {};
|
|
54
|
-
};
|
|
55
|
-
async function parseFormData(request, options) {
|
|
56
|
-
const formData = await request.formData();
|
|
57
|
-
if (formData) return convertFormDataToBodyData(formData, options);
|
|
58
|
-
return {};
|
|
59
|
-
}
|
|
60
|
-
function convertFormDataToBodyData(formData, options) {
|
|
61
|
-
const form = /* @__PURE__ */ Object.create(null);
|
|
62
|
-
formData.forEach((value, key) => {
|
|
63
|
-
if (!(options.all || key.endsWith("[]"))) form[key] = value;
|
|
64
|
-
else handleParsingAllValues(form, key, value);
|
|
65
|
-
});
|
|
66
|
-
if (options.dot) Object.entries(form).forEach(([key, value]) => {
|
|
67
|
-
if (key.includes(".")) {
|
|
68
|
-
handleParsingNestedValues(form, key, value);
|
|
69
|
-
delete form[key];
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
return form;
|
|
73
|
-
}
|
|
74
|
-
var handleParsingAllValues = (form, key, value) => {
|
|
75
|
-
if (form[key] !== void 0) if (Array.isArray(form[key])) form[key].push(value);
|
|
76
|
-
else form[key] = [form[key], value];
|
|
77
|
-
else if (!key.endsWith("[]")) form[key] = value;
|
|
78
|
-
else form[key] = [value];
|
|
79
|
-
};
|
|
80
|
-
var handleParsingNestedValues = (form, key, value) => {
|
|
81
|
-
let nestedForm = form;
|
|
82
|
-
const keys = key.split(".");
|
|
83
|
-
keys.forEach((key2, index) => {
|
|
84
|
-
if (index === keys.length - 1) nestedForm[key2] = value;
|
|
85
|
-
else {
|
|
86
|
-
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
87
|
-
nestedForm = nestedForm[key2];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region node_modules/hono/dist/utils/url.js
|
|
94
|
-
var splitPath = (path$1) => {
|
|
95
|
-
const paths = path$1.split("/");
|
|
96
|
-
if (paths[0] === "") paths.shift();
|
|
97
|
-
return paths;
|
|
98
|
-
};
|
|
99
|
-
var splitRoutingPath = (routePath) => {
|
|
100
|
-
const { groups, path: path$1 } = extractGroupsFromPath(routePath);
|
|
101
|
-
const paths = splitPath(path$1);
|
|
102
|
-
return replaceGroupMarks(paths, groups);
|
|
103
|
-
};
|
|
104
|
-
var extractGroupsFromPath = (path$1) => {
|
|
105
|
-
const groups = [];
|
|
106
|
-
path$1 = path$1.replace(/\{[^}]+\}/g, (match, index) => {
|
|
107
|
-
const mark = `@${index}`;
|
|
108
|
-
groups.push([mark, match]);
|
|
109
|
-
return mark;
|
|
110
|
-
});
|
|
111
|
-
return {
|
|
112
|
-
groups,
|
|
113
|
-
path: path$1
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
var replaceGroupMarks = (paths, groups) => {
|
|
117
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
118
|
-
const [mark] = groups[i];
|
|
119
|
-
for (let j = paths.length - 1; j >= 0; j--) if (paths[j].includes(mark)) {
|
|
120
|
-
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return paths;
|
|
125
|
-
};
|
|
126
|
-
var patternCache = {};
|
|
127
|
-
var getPattern = (label, next) => {
|
|
128
|
-
if (label === "*") return "*";
|
|
129
|
-
const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
130
|
-
if (match) {
|
|
131
|
-
const cacheKey = `${label}#${next}`;
|
|
132
|
-
if (!patternCache[cacheKey]) if (match[2]) patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [
|
|
133
|
-
cacheKey,
|
|
134
|
-
match[1],
|
|
135
|
-
/* @__PURE__ */ new RegExp(`^${match[2]}(?=/${next})`)
|
|
136
|
-
] : [
|
|
137
|
-
label,
|
|
138
|
-
match[1],
|
|
139
|
-
/* @__PURE__ */ new RegExp(`^${match[2]}$`)
|
|
140
|
-
];
|
|
141
|
-
else patternCache[cacheKey] = [
|
|
142
|
-
label,
|
|
143
|
-
match[1],
|
|
144
|
-
true
|
|
145
|
-
];
|
|
146
|
-
return patternCache[cacheKey];
|
|
147
|
-
}
|
|
148
|
-
return null;
|
|
149
|
-
};
|
|
150
|
-
var tryDecode = (str, decoder) => {
|
|
151
|
-
try {
|
|
152
|
-
return decoder(str);
|
|
153
|
-
} catch {
|
|
154
|
-
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
155
|
-
try {
|
|
156
|
-
return decoder(match);
|
|
157
|
-
} catch {
|
|
158
|
-
return match;
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
164
|
-
var getPath = (request) => {
|
|
165
|
-
const url = request.url;
|
|
166
|
-
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
167
|
-
let i = start;
|
|
168
|
-
for (; i < url.length; i++) {
|
|
169
|
-
const charCode = url.charCodeAt(i);
|
|
170
|
-
if (charCode === 37) {
|
|
171
|
-
const queryIndex = url.indexOf("?", i);
|
|
172
|
-
const path$1 = url.slice(start, queryIndex === -1 ? void 0 : queryIndex);
|
|
173
|
-
return tryDecodeURI(path$1.includes("%25") ? path$1.replace(/%25/g, "%2525") : path$1);
|
|
174
|
-
} else if (charCode === 63) break;
|
|
175
|
-
}
|
|
176
|
-
return url.slice(start, i);
|
|
177
|
-
};
|
|
178
|
-
var getPathNoStrict = (request) => {
|
|
179
|
-
const result = getPath(request);
|
|
180
|
-
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
181
|
-
};
|
|
182
|
-
var mergePath = (base, sub, ...rest) => {
|
|
183
|
-
if (rest.length) sub = mergePath(sub, ...rest);
|
|
184
|
-
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
185
|
-
};
|
|
186
|
-
var checkOptionalParameter = (path$1) => {
|
|
187
|
-
if (path$1.charCodeAt(path$1.length - 1) !== 63 || !path$1.includes(":")) return null;
|
|
188
|
-
const segments = path$1.split("/");
|
|
189
|
-
const results = [];
|
|
190
|
-
let basePath = "";
|
|
191
|
-
segments.forEach((segment) => {
|
|
192
|
-
if (segment !== "" && !/\:/.test(segment)) basePath += "/" + segment;
|
|
193
|
-
else if (/\:/.test(segment)) if (/\?/.test(segment)) {
|
|
194
|
-
if (results.length === 0 && basePath === "") results.push("/");
|
|
195
|
-
else results.push(basePath);
|
|
196
|
-
const optionalSegment = segment.replace("?", "");
|
|
197
|
-
basePath += "/" + optionalSegment;
|
|
198
|
-
results.push(basePath);
|
|
199
|
-
} else basePath += "/" + segment;
|
|
200
|
-
});
|
|
201
|
-
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
202
|
-
};
|
|
203
|
-
var _decodeURI = (value) => {
|
|
204
|
-
if (!/[%+]/.test(value)) return value;
|
|
205
|
-
if (value.indexOf("+") !== -1) value = value.replace(/\+/g, " ");
|
|
206
|
-
return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
|
|
207
|
-
};
|
|
208
|
-
var _getQueryParam = (url, key, multiple) => {
|
|
209
|
-
let encoded;
|
|
210
|
-
if (!multiple && key && !/[%+]/.test(key)) {
|
|
211
|
-
let keyIndex2 = url.indexOf(`?${key}`, 8);
|
|
212
|
-
if (keyIndex2 === -1) keyIndex2 = url.indexOf(`&${key}`, 8);
|
|
213
|
-
while (keyIndex2 !== -1) {
|
|
214
|
-
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
215
|
-
if (trailingKeyCode === 61) {
|
|
216
|
-
const valueIndex = keyIndex2 + key.length + 2;
|
|
217
|
-
const endIndex = url.indexOf("&", valueIndex);
|
|
218
|
-
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
|
|
219
|
-
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) return "";
|
|
220
|
-
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
221
|
-
}
|
|
222
|
-
encoded = /[%+]/.test(url);
|
|
223
|
-
if (!encoded) return;
|
|
224
|
-
}
|
|
225
|
-
const results = {};
|
|
226
|
-
encoded ??= /[%+]/.test(url);
|
|
227
|
-
let keyIndex = url.indexOf("?", 8);
|
|
228
|
-
while (keyIndex !== -1) {
|
|
229
|
-
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
230
|
-
let valueIndex = url.indexOf("=", keyIndex);
|
|
231
|
-
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) valueIndex = -1;
|
|
232
|
-
let name = url.slice(keyIndex + 1, valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex);
|
|
233
|
-
if (encoded) name = _decodeURI(name);
|
|
234
|
-
keyIndex = nextKeyIndex;
|
|
235
|
-
if (name === "") continue;
|
|
236
|
-
let value;
|
|
237
|
-
if (valueIndex === -1) value = "";
|
|
238
|
-
else {
|
|
239
|
-
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
|
|
240
|
-
if (encoded) value = _decodeURI(value);
|
|
241
|
-
}
|
|
242
|
-
if (multiple) {
|
|
243
|
-
if (!(results[name] && Array.isArray(results[name]))) results[name] = [];
|
|
244
|
-
results[name].push(value);
|
|
245
|
-
} else results[name] ??= value;
|
|
246
|
-
}
|
|
247
|
-
return key ? results[key] : results;
|
|
248
|
-
};
|
|
249
|
-
var getQueryParam = _getQueryParam;
|
|
250
|
-
var getQueryParams = (url, key) => {
|
|
251
|
-
return _getQueryParam(url, key, true);
|
|
252
|
-
};
|
|
253
|
-
var decodeURIComponent_ = decodeURIComponent;
|
|
254
|
-
|
|
255
|
-
//#endregion
|
|
256
|
-
//#region node_modules/hono/dist/request.js
|
|
257
|
-
var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
|
258
|
-
var HonoRequest = class {
|
|
259
|
-
raw;
|
|
260
|
-
#validatedData;
|
|
261
|
-
#matchResult;
|
|
262
|
-
routeIndex = 0;
|
|
263
|
-
path;
|
|
264
|
-
bodyCache = {};
|
|
265
|
-
constructor(request, path$1 = "/", matchResult = [[]]) {
|
|
266
|
-
this.raw = request;
|
|
267
|
-
this.path = path$1;
|
|
268
|
-
this.#matchResult = matchResult;
|
|
269
|
-
this.#validatedData = {};
|
|
270
|
-
}
|
|
271
|
-
param(key) {
|
|
272
|
-
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
273
|
-
}
|
|
274
|
-
#getDecodedParam(key) {
|
|
275
|
-
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
276
|
-
const param = this.#getParamValue(paramKey);
|
|
277
|
-
return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
|
|
278
|
-
}
|
|
279
|
-
#getAllDecodedParams() {
|
|
280
|
-
const decoded = {};
|
|
281
|
-
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
282
|
-
for (const key of keys) {
|
|
283
|
-
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
284
|
-
if (value !== void 0) decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
285
|
-
}
|
|
286
|
-
return decoded;
|
|
287
|
-
}
|
|
288
|
-
#getParamValue(paramKey) {
|
|
289
|
-
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
290
|
-
}
|
|
291
|
-
query(key) {
|
|
292
|
-
return getQueryParam(this.url, key);
|
|
293
|
-
}
|
|
294
|
-
queries(key) {
|
|
295
|
-
return getQueryParams(this.url, key);
|
|
296
|
-
}
|
|
297
|
-
header(name) {
|
|
298
|
-
if (name) return this.raw.headers.get(name) ?? void 0;
|
|
299
|
-
const headerData = {};
|
|
300
|
-
this.raw.headers.forEach((value, key) => {
|
|
301
|
-
headerData[key] = value;
|
|
302
|
-
});
|
|
303
|
-
return headerData;
|
|
304
|
-
}
|
|
305
|
-
async parseBody(options) {
|
|
306
|
-
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
307
|
-
}
|
|
308
|
-
#cachedBody = (key) => {
|
|
309
|
-
const { bodyCache, raw: raw$1 } = this;
|
|
310
|
-
const cachedBody = bodyCache[key];
|
|
311
|
-
if (cachedBody) return cachedBody;
|
|
312
|
-
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
313
|
-
if (anyCachedKey) return bodyCache[anyCachedKey].then((body) => {
|
|
314
|
-
if (anyCachedKey === "json") body = JSON.stringify(body);
|
|
315
|
-
return new Response(body)[key]();
|
|
316
|
-
});
|
|
317
|
-
return bodyCache[key] = raw$1[key]();
|
|
318
|
-
};
|
|
319
|
-
json() {
|
|
320
|
-
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
321
|
-
}
|
|
322
|
-
text() {
|
|
323
|
-
return this.#cachedBody("text");
|
|
324
|
-
}
|
|
325
|
-
arrayBuffer() {
|
|
326
|
-
return this.#cachedBody("arrayBuffer");
|
|
327
|
-
}
|
|
328
|
-
blob() {
|
|
329
|
-
return this.#cachedBody("blob");
|
|
330
|
-
}
|
|
331
|
-
formData() {
|
|
332
|
-
return this.#cachedBody("formData");
|
|
333
|
-
}
|
|
334
|
-
addValidatedData(target, data) {
|
|
335
|
-
this.#validatedData[target] = data;
|
|
336
|
-
}
|
|
337
|
-
valid(target) {
|
|
338
|
-
return this.#validatedData[target];
|
|
339
|
-
}
|
|
340
|
-
get url() {
|
|
341
|
-
return this.raw.url;
|
|
342
|
-
}
|
|
343
|
-
get method() {
|
|
344
|
-
return this.raw.method;
|
|
345
|
-
}
|
|
346
|
-
get [GET_MATCH_RESULT]() {
|
|
347
|
-
return this.#matchResult;
|
|
348
|
-
}
|
|
349
|
-
get matchedRoutes() {
|
|
350
|
-
return this.#matchResult[0].map(([[, route]]) => route);
|
|
351
|
-
}
|
|
352
|
-
get routePath() {
|
|
353
|
-
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
//#endregion
|
|
358
|
-
//#region node_modules/hono/dist/utils/html.js
|
|
359
|
-
var HtmlEscapedCallbackPhase = {
|
|
360
|
-
Stringify: 1,
|
|
361
|
-
BeforeStream: 2,
|
|
362
|
-
Stream: 3
|
|
363
|
-
};
|
|
364
|
-
var raw = (value, callbacks) => {
|
|
365
|
-
const escapedString = new String(value);
|
|
366
|
-
escapedString.isEscaped = true;
|
|
367
|
-
escapedString.callbacks = callbacks;
|
|
368
|
-
return escapedString;
|
|
369
|
-
};
|
|
370
|
-
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
371
|
-
if (typeof str === "object" && !(str instanceof String)) {
|
|
372
|
-
if (!(str instanceof Promise)) str = str.toString();
|
|
373
|
-
if (str instanceof Promise) str = await str;
|
|
374
|
-
}
|
|
375
|
-
const callbacks = str.callbacks;
|
|
376
|
-
if (!callbacks?.length) return Promise.resolve(str);
|
|
377
|
-
if (buffer) buffer[0] += str;
|
|
378
|
-
else buffer = [str];
|
|
379
|
-
const resStr = Promise.all(callbacks.map((c) => c({
|
|
380
|
-
phase,
|
|
381
|
-
buffer,
|
|
382
|
-
context
|
|
383
|
-
}))).then((res) => Promise.all(res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))).then(() => buffer[0]));
|
|
384
|
-
if (preserveCallbacks) return raw(await resStr, callbacks);
|
|
385
|
-
else return resStr;
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
//#endregion
|
|
389
|
-
//#region node_modules/hono/dist/context.js
|
|
390
|
-
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
391
|
-
var setDefaultContentType = (contentType, headers) => {
|
|
392
|
-
return {
|
|
393
|
-
"Content-Type": contentType,
|
|
394
|
-
...headers
|
|
395
|
-
};
|
|
396
|
-
};
|
|
397
|
-
var Context = class {
|
|
398
|
-
#rawRequest;
|
|
399
|
-
#req;
|
|
400
|
-
env = {};
|
|
401
|
-
#var;
|
|
402
|
-
finalized = false;
|
|
403
|
-
error;
|
|
404
|
-
#status;
|
|
405
|
-
#executionCtx;
|
|
406
|
-
#res;
|
|
407
|
-
#layout;
|
|
408
|
-
#renderer;
|
|
409
|
-
#notFoundHandler;
|
|
410
|
-
#preparedHeaders;
|
|
411
|
-
#matchResult;
|
|
412
|
-
#path;
|
|
413
|
-
constructor(req, options) {
|
|
414
|
-
this.#rawRequest = req;
|
|
415
|
-
if (options) {
|
|
416
|
-
this.#executionCtx = options.executionCtx;
|
|
417
|
-
this.env = options.env;
|
|
418
|
-
this.#notFoundHandler = options.notFoundHandler;
|
|
419
|
-
this.#path = options.path;
|
|
420
|
-
this.#matchResult = options.matchResult;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
get req() {
|
|
424
|
-
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
425
|
-
return this.#req;
|
|
426
|
-
}
|
|
427
|
-
get event() {
|
|
428
|
-
if (this.#executionCtx && "respondWith" in this.#executionCtx) return this.#executionCtx;
|
|
429
|
-
else throw Error("This context has no FetchEvent");
|
|
430
|
-
}
|
|
431
|
-
get executionCtx() {
|
|
432
|
-
if (this.#executionCtx) return this.#executionCtx;
|
|
433
|
-
else throw Error("This context has no ExecutionContext");
|
|
434
|
-
}
|
|
435
|
-
get res() {
|
|
436
|
-
return this.#res ||= new Response(null, { headers: this.#preparedHeaders ??= new Headers() });
|
|
437
|
-
}
|
|
438
|
-
set res(_res) {
|
|
439
|
-
if (this.#res && _res) {
|
|
440
|
-
_res = new Response(_res.body, _res);
|
|
441
|
-
for (const [k, v] of this.#res.headers.entries()) {
|
|
442
|
-
if (k === "content-type") continue;
|
|
443
|
-
if (k === "set-cookie") {
|
|
444
|
-
const cookies = this.#res.headers.getSetCookie();
|
|
445
|
-
_res.headers.delete("set-cookie");
|
|
446
|
-
for (const cookie of cookies) _res.headers.append("set-cookie", cookie);
|
|
447
|
-
} else _res.headers.set(k, v);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
this.#res = _res;
|
|
451
|
-
this.finalized = true;
|
|
452
|
-
}
|
|
453
|
-
render = (...args) => {
|
|
454
|
-
this.#renderer ??= (content) => this.html(content);
|
|
455
|
-
return this.#renderer(...args);
|
|
456
|
-
};
|
|
457
|
-
setLayout = (layout) => this.#layout = layout;
|
|
458
|
-
getLayout = () => this.#layout;
|
|
459
|
-
setRenderer = (renderer) => {
|
|
460
|
-
this.#renderer = renderer;
|
|
461
|
-
};
|
|
462
|
-
header = (name, value, options) => {
|
|
463
|
-
if (this.finalized) this.#res = new Response(this.#res.body, this.#res);
|
|
464
|
-
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers();
|
|
465
|
-
if (value === void 0) headers.delete(name);
|
|
466
|
-
else if (options?.append) headers.append(name, value);
|
|
467
|
-
else headers.set(name, value);
|
|
468
|
-
};
|
|
469
|
-
status = (status) => {
|
|
470
|
-
this.#status = status;
|
|
471
|
-
};
|
|
472
|
-
set = (key, value) => {
|
|
473
|
-
this.#var ??= /* @__PURE__ */ new Map();
|
|
474
|
-
this.#var.set(key, value);
|
|
475
|
-
};
|
|
476
|
-
get = (key) => {
|
|
477
|
-
return this.#var ? this.#var.get(key) : void 0;
|
|
478
|
-
};
|
|
479
|
-
get var() {
|
|
480
|
-
if (!this.#var) return {};
|
|
481
|
-
return Object.fromEntries(this.#var);
|
|
482
|
-
}
|
|
483
|
-
#newResponse(data, arg, headers) {
|
|
484
|
-
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers();
|
|
485
|
-
if (typeof arg === "object" && "headers" in arg) {
|
|
486
|
-
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
487
|
-
for (const [key, value] of argHeaders) if (key.toLowerCase() === "set-cookie") responseHeaders.append(key, value);
|
|
488
|
-
else responseHeaders.set(key, value);
|
|
489
|
-
}
|
|
490
|
-
if (headers) for (const [k, v] of Object.entries(headers)) if (typeof v === "string") responseHeaders.set(k, v);
|
|
491
|
-
else {
|
|
492
|
-
responseHeaders.delete(k);
|
|
493
|
-
for (const v2 of v) responseHeaders.append(k, v2);
|
|
494
|
-
}
|
|
495
|
-
const status = typeof arg === "number" ? arg : arg?.status ?? this.#status;
|
|
496
|
-
return new Response(data, {
|
|
497
|
-
status,
|
|
498
|
-
headers: responseHeaders
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
newResponse = (...args) => this.#newResponse(...args);
|
|
502
|
-
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
503
|
-
text = (text, arg, headers) => {
|
|
504
|
-
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(text, arg, setDefaultContentType(TEXT_PLAIN, headers));
|
|
505
|
-
};
|
|
506
|
-
json = (object, arg, headers) => {
|
|
507
|
-
return this.#newResponse(JSON.stringify(object), arg, setDefaultContentType("application/json", headers));
|
|
508
|
-
};
|
|
509
|
-
html = (html, arg, headers) => {
|
|
510
|
-
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
511
|
-
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
512
|
-
};
|
|
513
|
-
redirect = (location, status) => {
|
|
514
|
-
const locationString = String(location);
|
|
515
|
-
this.header("Location", !/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString));
|
|
516
|
-
return this.newResponse(null, status ?? 302);
|
|
517
|
-
};
|
|
518
|
-
notFound = () => {
|
|
519
|
-
this.#notFoundHandler ??= () => new Response();
|
|
520
|
-
return this.#notFoundHandler(this);
|
|
521
|
-
};
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
//#endregion
|
|
525
|
-
//#region node_modules/hono/dist/router.js
|
|
526
|
-
var METHOD_NAME_ALL = "ALL";
|
|
527
|
-
var METHOD_NAME_ALL_LOWERCASE = "all";
|
|
528
|
-
var METHODS = [
|
|
529
|
-
"get",
|
|
530
|
-
"post",
|
|
531
|
-
"put",
|
|
532
|
-
"delete",
|
|
533
|
-
"options",
|
|
534
|
-
"patch"
|
|
535
|
-
];
|
|
536
|
-
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
537
|
-
var UnsupportedPathError = class extends Error {};
|
|
538
|
-
|
|
539
|
-
//#endregion
|
|
540
|
-
//#region node_modules/hono/dist/utils/constants.js
|
|
541
|
-
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
542
|
-
|
|
543
|
-
//#endregion
|
|
544
|
-
//#region node_modules/hono/dist/hono-base.js
|
|
545
|
-
var notFoundHandler = (c) => {
|
|
546
|
-
return c.text("404 Not Found", 404);
|
|
547
|
-
};
|
|
548
|
-
var errorHandler = (err, c) => {
|
|
549
|
-
if ("getResponse" in err) {
|
|
550
|
-
const res = err.getResponse();
|
|
551
|
-
return c.newResponse(res.body, res);
|
|
552
|
-
}
|
|
553
|
-
console.error(err);
|
|
554
|
-
return c.text("Internal Server Error", 500);
|
|
555
|
-
};
|
|
556
|
-
var Hono$1 = class {
|
|
557
|
-
get;
|
|
558
|
-
post;
|
|
559
|
-
put;
|
|
560
|
-
delete;
|
|
561
|
-
options;
|
|
562
|
-
patch;
|
|
563
|
-
all;
|
|
564
|
-
on;
|
|
565
|
-
use;
|
|
566
|
-
router;
|
|
567
|
-
getPath;
|
|
568
|
-
_basePath = "/";
|
|
569
|
-
#path = "/";
|
|
570
|
-
routes = [];
|
|
571
|
-
constructor(options = {}) {
|
|
572
|
-
[...METHODS, METHOD_NAME_ALL_LOWERCASE].forEach((method) => {
|
|
573
|
-
this[method] = (args1, ...args) => {
|
|
574
|
-
if (typeof args1 === "string") this.#path = args1;
|
|
575
|
-
else this.#addRoute(method, this.#path, args1);
|
|
576
|
-
args.forEach((handler) => {
|
|
577
|
-
this.#addRoute(method, this.#path, handler);
|
|
578
|
-
});
|
|
579
|
-
return this;
|
|
580
|
-
};
|
|
581
|
-
});
|
|
582
|
-
this.on = (method, path$1, ...handlers) => {
|
|
583
|
-
for (const p of [path$1].flat()) {
|
|
584
|
-
this.#path = p;
|
|
585
|
-
for (const m of [method].flat()) handlers.map((handler) => {
|
|
586
|
-
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
return this;
|
|
590
|
-
};
|
|
591
|
-
this.use = (arg1, ...handlers) => {
|
|
592
|
-
if (typeof arg1 === "string") this.#path = arg1;
|
|
593
|
-
else {
|
|
594
|
-
this.#path = "*";
|
|
595
|
-
handlers.unshift(arg1);
|
|
596
|
-
}
|
|
597
|
-
handlers.forEach((handler) => {
|
|
598
|
-
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
599
|
-
});
|
|
600
|
-
return this;
|
|
601
|
-
};
|
|
602
|
-
const { strict,...optionsWithoutStrict } = options;
|
|
603
|
-
Object.assign(this, optionsWithoutStrict);
|
|
604
|
-
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
605
|
-
}
|
|
606
|
-
#clone() {
|
|
607
|
-
const clone = new Hono$1({
|
|
608
|
-
router: this.router,
|
|
609
|
-
getPath: this.getPath
|
|
610
|
-
});
|
|
611
|
-
clone.errorHandler = this.errorHandler;
|
|
612
|
-
clone.#notFoundHandler = this.#notFoundHandler;
|
|
613
|
-
clone.routes = this.routes;
|
|
614
|
-
return clone;
|
|
615
|
-
}
|
|
616
|
-
#notFoundHandler = notFoundHandler;
|
|
617
|
-
errorHandler = errorHandler;
|
|
618
|
-
route(path$1, app) {
|
|
619
|
-
const subApp = this.basePath(path$1);
|
|
620
|
-
app.routes.map((r) => {
|
|
621
|
-
let handler;
|
|
622
|
-
if (app.errorHandler === errorHandler) handler = r.handler;
|
|
623
|
-
else {
|
|
624
|
-
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
625
|
-
handler[COMPOSED_HANDLER] = r.handler;
|
|
626
|
-
}
|
|
627
|
-
subApp.#addRoute(r.method, r.path, handler);
|
|
628
|
-
});
|
|
629
|
-
return this;
|
|
630
|
-
}
|
|
631
|
-
basePath(path$1) {
|
|
632
|
-
const subApp = this.#clone();
|
|
633
|
-
subApp._basePath = mergePath(this._basePath, path$1);
|
|
634
|
-
return subApp;
|
|
635
|
-
}
|
|
636
|
-
onError = (handler) => {
|
|
637
|
-
this.errorHandler = handler;
|
|
638
|
-
return this;
|
|
639
|
-
};
|
|
640
|
-
notFound = (handler) => {
|
|
641
|
-
this.#notFoundHandler = handler;
|
|
642
|
-
return this;
|
|
643
|
-
};
|
|
644
|
-
mount(path$1, applicationHandler, options) {
|
|
645
|
-
let replaceRequest;
|
|
646
|
-
let optionHandler;
|
|
647
|
-
if (options) if (typeof options === "function") optionHandler = options;
|
|
648
|
-
else {
|
|
649
|
-
optionHandler = options.optionHandler;
|
|
650
|
-
if (options.replaceRequest === false) replaceRequest = (request) => request;
|
|
651
|
-
else replaceRequest = options.replaceRequest;
|
|
652
|
-
}
|
|
653
|
-
const getOptions = optionHandler ? (c) => {
|
|
654
|
-
const options2 = optionHandler(c);
|
|
655
|
-
return Array.isArray(options2) ? options2 : [options2];
|
|
656
|
-
} : (c) => {
|
|
657
|
-
let executionContext = void 0;
|
|
658
|
-
try {
|
|
659
|
-
executionContext = c.executionCtx;
|
|
660
|
-
} catch {}
|
|
661
|
-
return [c.env, executionContext];
|
|
662
|
-
};
|
|
663
|
-
replaceRequest ||= (() => {
|
|
664
|
-
const mergedPath = mergePath(this._basePath, path$1);
|
|
665
|
-
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
666
|
-
return (request) => {
|
|
667
|
-
const url = new URL(request.url);
|
|
668
|
-
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
669
|
-
return new Request(url, request);
|
|
670
|
-
};
|
|
671
|
-
})();
|
|
672
|
-
const handler = async (c, next) => {
|
|
673
|
-
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
674
|
-
if (res) return res;
|
|
675
|
-
await next();
|
|
676
|
-
};
|
|
677
|
-
this.#addRoute(METHOD_NAME_ALL, mergePath(path$1, "*"), handler);
|
|
678
|
-
return this;
|
|
679
|
-
}
|
|
680
|
-
#addRoute(method, path$1, handler) {
|
|
681
|
-
method = method.toUpperCase();
|
|
682
|
-
path$1 = mergePath(this._basePath, path$1);
|
|
683
|
-
const r = {
|
|
684
|
-
basePath: this._basePath,
|
|
685
|
-
path: path$1,
|
|
686
|
-
method,
|
|
687
|
-
handler
|
|
688
|
-
};
|
|
689
|
-
this.router.add(method, path$1, [handler, r]);
|
|
690
|
-
this.routes.push(r);
|
|
691
|
-
}
|
|
692
|
-
#handleError(err, c) {
|
|
693
|
-
if (err instanceof Error) return this.errorHandler(err, c);
|
|
694
|
-
throw err;
|
|
695
|
-
}
|
|
696
|
-
#dispatch(request, executionCtx, env, method) {
|
|
697
|
-
if (method === "HEAD") return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
698
|
-
const path$1 = this.getPath(request, { env });
|
|
699
|
-
const matchResult = this.router.match(method, path$1);
|
|
700
|
-
const c = new Context(request, {
|
|
701
|
-
path: path$1,
|
|
702
|
-
matchResult,
|
|
703
|
-
env,
|
|
704
|
-
executionCtx,
|
|
705
|
-
notFoundHandler: this.#notFoundHandler
|
|
706
|
-
});
|
|
707
|
-
if (matchResult[0].length === 1) {
|
|
708
|
-
let res;
|
|
709
|
-
try {
|
|
710
|
-
res = matchResult[0][0][0][0](c, async () => {
|
|
711
|
-
c.res = await this.#notFoundHandler(c);
|
|
712
|
-
});
|
|
713
|
-
} catch (err) {
|
|
714
|
-
return this.#handleError(err, c);
|
|
715
|
-
}
|
|
716
|
-
return res instanceof Promise ? res.then((resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
717
|
-
}
|
|
718
|
-
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
719
|
-
return (async () => {
|
|
720
|
-
try {
|
|
721
|
-
const context = await composed(c);
|
|
722
|
-
if (!context.finalized) throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");
|
|
723
|
-
return context.res;
|
|
724
|
-
} catch (err) {
|
|
725
|
-
return this.#handleError(err, c);
|
|
726
|
-
}
|
|
727
|
-
})();
|
|
728
|
-
}
|
|
729
|
-
fetch = (request, ...rest) => {
|
|
730
|
-
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
731
|
-
};
|
|
732
|
-
request = (input, requestInit, Env, executionCtx) => {
|
|
733
|
-
if (input instanceof Request) return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
734
|
-
input = input.toString();
|
|
735
|
-
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`, requestInit), Env, executionCtx);
|
|
736
|
-
};
|
|
737
|
-
fire = () => {
|
|
738
|
-
addEventListener("fetch", (event) => {
|
|
739
|
-
event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
|
|
740
|
-
});
|
|
741
|
-
};
|
|
742
|
-
};
|
|
743
|
-
|
|
744
|
-
//#endregion
|
|
745
|
-
//#region node_modules/hono/dist/router/reg-exp-router/node.js
|
|
746
|
-
var LABEL_REG_EXP_STR = "[^/]+";
|
|
747
|
-
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
748
|
-
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
749
|
-
var PATH_ERROR = Symbol();
|
|
750
|
-
var regExpMetaChars = /* @__PURE__ */ new Set(".\\+*[^]$()");
|
|
751
|
-
function compareKey(a, b) {
|
|
752
|
-
if (a.length === 1) return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
753
|
-
if (b.length === 1) return 1;
|
|
754
|
-
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) return 1;
|
|
755
|
-
else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) return -1;
|
|
756
|
-
if (a === LABEL_REG_EXP_STR) return 1;
|
|
757
|
-
else if (b === LABEL_REG_EXP_STR) return -1;
|
|
758
|
-
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
759
|
-
}
|
|
760
|
-
var Node$1 = class {
|
|
761
|
-
#index;
|
|
762
|
-
#varIndex;
|
|
763
|
-
#children = /* @__PURE__ */ Object.create(null);
|
|
764
|
-
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
765
|
-
if (tokens.length === 0) {
|
|
766
|
-
if (this.#index !== void 0) throw PATH_ERROR;
|
|
767
|
-
if (pathErrorCheckOnly) return;
|
|
768
|
-
this.#index = index;
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
const [token, ...restTokens] = tokens;
|
|
772
|
-
const pattern = token === "*" ? restTokens.length === 0 ? [
|
|
773
|
-
"",
|
|
774
|
-
"",
|
|
775
|
-
ONLY_WILDCARD_REG_EXP_STR
|
|
776
|
-
] : [
|
|
777
|
-
"",
|
|
778
|
-
"",
|
|
779
|
-
LABEL_REG_EXP_STR
|
|
780
|
-
] : token === "/*" ? [
|
|
781
|
-
"",
|
|
782
|
-
"",
|
|
783
|
-
TAIL_WILDCARD_REG_EXP_STR
|
|
784
|
-
] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
785
|
-
let node;
|
|
786
|
-
if (pattern) {
|
|
787
|
-
const name = pattern[1];
|
|
788
|
-
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
|
789
|
-
if (name && pattern[2]) {
|
|
790
|
-
if (regexpStr === ".*") throw PATH_ERROR;
|
|
791
|
-
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
792
|
-
if (/\((?!\?:)/.test(regexpStr)) throw PATH_ERROR;
|
|
793
|
-
}
|
|
794
|
-
node = this.#children[regexpStr];
|
|
795
|
-
if (!node) {
|
|
796
|
-
if (Object.keys(this.#children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) throw PATH_ERROR;
|
|
797
|
-
if (pathErrorCheckOnly) return;
|
|
798
|
-
node = this.#children[regexpStr] = new Node$1();
|
|
799
|
-
if (name !== "") node.#varIndex = context.varIndex++;
|
|
800
|
-
}
|
|
801
|
-
if (!pathErrorCheckOnly && name !== "") paramMap.push([name, node.#varIndex]);
|
|
802
|
-
} else {
|
|
803
|
-
node = this.#children[token];
|
|
804
|
-
if (!node) {
|
|
805
|
-
if (Object.keys(this.#children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) throw PATH_ERROR;
|
|
806
|
-
if (pathErrorCheckOnly) return;
|
|
807
|
-
node = this.#children[token] = new Node$1();
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
811
|
-
}
|
|
812
|
-
buildRegExpStr() {
|
|
813
|
-
const strList = Object.keys(this.#children).sort(compareKey).map((k) => {
|
|
814
|
-
const c = this.#children[k];
|
|
815
|
-
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
816
|
-
});
|
|
817
|
-
if (typeof this.#index === "number") strList.unshift(`#${this.#index}`);
|
|
818
|
-
if (strList.length === 0) return "";
|
|
819
|
-
if (strList.length === 1) return strList[0];
|
|
820
|
-
return "(?:" + strList.join("|") + ")";
|
|
821
|
-
}
|
|
822
|
-
};
|
|
823
|
-
|
|
824
|
-
//#endregion
|
|
825
|
-
//#region node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
826
|
-
var Trie = class {
|
|
827
|
-
#context = { varIndex: 0 };
|
|
828
|
-
#root = new Node$1();
|
|
829
|
-
insert(path$1, index, pathErrorCheckOnly) {
|
|
830
|
-
const paramAssoc = [];
|
|
831
|
-
const groups = [];
|
|
832
|
-
for (let i = 0;;) {
|
|
833
|
-
let replaced = false;
|
|
834
|
-
path$1 = path$1.replace(/\{[^}]+\}/g, (m) => {
|
|
835
|
-
const mark = `@\\${i}`;
|
|
836
|
-
groups[i] = [mark, m];
|
|
837
|
-
i++;
|
|
838
|
-
replaced = true;
|
|
839
|
-
return mark;
|
|
840
|
-
});
|
|
841
|
-
if (!replaced) break;
|
|
842
|
-
}
|
|
843
|
-
const tokens = path$1.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
844
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
845
|
-
const [mark] = groups[i];
|
|
846
|
-
for (let j = tokens.length - 1; j >= 0; j--) if (tokens[j].indexOf(mark) !== -1) {
|
|
847
|
-
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
848
|
-
break;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
852
|
-
return paramAssoc;
|
|
853
|
-
}
|
|
854
|
-
buildRegExp() {
|
|
855
|
-
let regexp = this.#root.buildRegExpStr();
|
|
856
|
-
if (regexp === "") return [
|
|
857
|
-
/^$/,
|
|
858
|
-
[],
|
|
859
|
-
[]
|
|
860
|
-
];
|
|
861
|
-
let captureIndex = 0;
|
|
862
|
-
const indexReplacementMap = [];
|
|
863
|
-
const paramReplacementMap = [];
|
|
864
|
-
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
865
|
-
if (handlerIndex !== void 0) {
|
|
866
|
-
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
867
|
-
return "$()";
|
|
868
|
-
}
|
|
869
|
-
if (paramIndex !== void 0) {
|
|
870
|
-
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
871
|
-
return "";
|
|
872
|
-
}
|
|
873
|
-
return "";
|
|
874
|
-
});
|
|
875
|
-
return [
|
|
876
|
-
/* @__PURE__ */ new RegExp(`^${regexp}`),
|
|
877
|
-
indexReplacementMap,
|
|
878
|
-
paramReplacementMap
|
|
879
|
-
];
|
|
880
|
-
}
|
|
881
|
-
};
|
|
882
|
-
|
|
883
|
-
//#endregion
|
|
884
|
-
//#region node_modules/hono/dist/router/reg-exp-router/router.js
|
|
885
|
-
var emptyParam = [];
|
|
886
|
-
var nullMatcher = [
|
|
887
|
-
/^$/,
|
|
888
|
-
[],
|
|
889
|
-
/* @__PURE__ */ Object.create(null)
|
|
890
|
-
];
|
|
891
|
-
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
892
|
-
function buildWildcardRegExp(path$1) {
|
|
893
|
-
return wildcardRegExpCache[path$1] ??= /* @__PURE__ */ new RegExp(path$1 === "*" ? "" : `^${path$1.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)")}$`);
|
|
894
|
-
}
|
|
895
|
-
function clearWildcardRegExpCache() {
|
|
896
|
-
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
897
|
-
}
|
|
898
|
-
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
899
|
-
const trie = new Trie();
|
|
900
|
-
const handlerData = [];
|
|
901
|
-
if (routes.length === 0) return nullMatcher;
|
|
902
|
-
const routesWithStaticPathFlag = routes.map((route) => [!/\*|\/:/.test(route[0]), ...route]).sort(([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length);
|
|
903
|
-
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
904
|
-
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
905
|
-
const [pathErrorCheckOnly, path$1, handlers] = routesWithStaticPathFlag[i];
|
|
906
|
-
if (pathErrorCheckOnly) staticMap[path$1] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
907
|
-
else j++;
|
|
908
|
-
let paramAssoc;
|
|
909
|
-
try {
|
|
910
|
-
paramAssoc = trie.insert(path$1, j, pathErrorCheckOnly);
|
|
911
|
-
} catch (e) {
|
|
912
|
-
throw e === PATH_ERROR ? new UnsupportedPathError(path$1) : e;
|
|
913
|
-
}
|
|
914
|
-
if (pathErrorCheckOnly) continue;
|
|
915
|
-
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
916
|
-
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
917
|
-
paramCount -= 1;
|
|
918
|
-
for (; paramCount >= 0; paramCount--) {
|
|
919
|
-
const [key, value] = paramAssoc[paramCount];
|
|
920
|
-
paramIndexMap[key] = value;
|
|
921
|
-
}
|
|
922
|
-
return [h, paramIndexMap];
|
|
923
|
-
});
|
|
924
|
-
}
|
|
925
|
-
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
926
|
-
for (let i = 0, len = handlerData.length; i < len; i++) for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
|
|
927
|
-
const map = handlerData[i][j]?.[1];
|
|
928
|
-
if (!map) continue;
|
|
929
|
-
const keys = Object.keys(map);
|
|
930
|
-
for (let k = 0, len3 = keys.length; k < len3; k++) map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
931
|
-
}
|
|
932
|
-
const handlerMap = [];
|
|
933
|
-
for (const i in indexReplacementMap) handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
934
|
-
return [
|
|
935
|
-
regexp,
|
|
936
|
-
handlerMap,
|
|
937
|
-
staticMap
|
|
938
|
-
];
|
|
939
|
-
}
|
|
940
|
-
function findMiddleware(middleware, path$1) {
|
|
941
|
-
if (!middleware) return;
|
|
942
|
-
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) if (buildWildcardRegExp(k).test(path$1)) return [...middleware[k]];
|
|
943
|
-
}
|
|
944
|
-
var RegExpRouter = class {
|
|
945
|
-
name = "RegExpRouter";
|
|
946
|
-
#middleware;
|
|
947
|
-
#routes;
|
|
948
|
-
constructor() {
|
|
949
|
-
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
950
|
-
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
951
|
-
}
|
|
952
|
-
add(method, path$1, handler) {
|
|
953
|
-
const middleware = this.#middleware;
|
|
954
|
-
const routes = this.#routes;
|
|
955
|
-
if (!middleware || !routes) throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
956
|
-
if (!middleware[method]) [middleware, routes].forEach((handlerMap) => {
|
|
957
|
-
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
958
|
-
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
959
|
-
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
960
|
-
});
|
|
961
|
-
});
|
|
962
|
-
if (path$1 === "/*") path$1 = "*";
|
|
963
|
-
const paramCount = (path$1.match(/\/:/g) || []).length;
|
|
964
|
-
if (/\*$/.test(path$1)) {
|
|
965
|
-
const re = buildWildcardRegExp(path$1);
|
|
966
|
-
if (method === METHOD_NAME_ALL) Object.keys(middleware).forEach((m) => {
|
|
967
|
-
middleware[m][path$1] ||= findMiddleware(middleware[m], path$1) || findMiddleware(middleware[METHOD_NAME_ALL], path$1) || [];
|
|
968
|
-
});
|
|
969
|
-
else middleware[method][path$1] ||= findMiddleware(middleware[method], path$1) || findMiddleware(middleware[METHOD_NAME_ALL], path$1) || [];
|
|
970
|
-
Object.keys(middleware).forEach((m) => {
|
|
971
|
-
if (method === METHOD_NAME_ALL || method === m) Object.keys(middleware[m]).forEach((p) => {
|
|
972
|
-
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
973
|
-
});
|
|
974
|
-
});
|
|
975
|
-
Object.keys(routes).forEach((m) => {
|
|
976
|
-
if (method === METHOD_NAME_ALL || method === m) Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push([handler, paramCount]));
|
|
977
|
-
});
|
|
978
|
-
return;
|
|
979
|
-
}
|
|
980
|
-
const paths = checkOptionalParameter(path$1) || [path$1];
|
|
981
|
-
for (let i = 0, len = paths.length; i < len; i++) {
|
|
982
|
-
const path2 = paths[i];
|
|
983
|
-
Object.keys(routes).forEach((m) => {
|
|
984
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
985
|
-
routes[m][path2] ||= [...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []];
|
|
986
|
-
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
987
|
-
}
|
|
988
|
-
});
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
match(method, path$1) {
|
|
992
|
-
clearWildcardRegExpCache();
|
|
993
|
-
const matchers = this.#buildAllMatchers();
|
|
994
|
-
this.match = (method2, path2) => {
|
|
995
|
-
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
996
|
-
const staticMatch = matcher[2][path2];
|
|
997
|
-
if (staticMatch) return staticMatch;
|
|
998
|
-
const match = path2.match(matcher[0]);
|
|
999
|
-
if (!match) return [[], emptyParam];
|
|
1000
|
-
const index = match.indexOf("", 1);
|
|
1001
|
-
return [matcher[1][index], match];
|
|
1002
|
-
};
|
|
1003
|
-
return this.match(method, path$1);
|
|
1004
|
-
}
|
|
1005
|
-
#buildAllMatchers() {
|
|
1006
|
-
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1007
|
-
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
1008
|
-
matchers[method] ||= this.#buildMatcher(method);
|
|
1009
|
-
});
|
|
1010
|
-
this.#middleware = this.#routes = void 0;
|
|
1011
|
-
return matchers;
|
|
1012
|
-
}
|
|
1013
|
-
#buildMatcher(method) {
|
|
1014
|
-
const routes = [];
|
|
1015
|
-
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
1016
|
-
[this.#middleware, this.#routes].forEach((r) => {
|
|
1017
|
-
const ownRoute = r[method] ? Object.keys(r[method]).map((path$1) => [path$1, r[method][path$1]]) : [];
|
|
1018
|
-
if (ownRoute.length !== 0) {
|
|
1019
|
-
hasOwnRoute ||= true;
|
|
1020
|
-
routes.push(...ownRoute);
|
|
1021
|
-
} else if (method !== METHOD_NAME_ALL) routes.push(...Object.keys(r[METHOD_NAME_ALL]).map((path$1) => [path$1, r[METHOD_NAME_ALL][path$1]]));
|
|
1022
|
-
});
|
|
1023
|
-
if (!hasOwnRoute) return null;
|
|
1024
|
-
else return buildMatcherFromPreprocessedRoutes(routes);
|
|
1025
|
-
}
|
|
1026
|
-
};
|
|
1027
|
-
|
|
1028
|
-
//#endregion
|
|
1029
|
-
//#region node_modules/hono/dist/router/smart-router/router.js
|
|
1030
|
-
var SmartRouter = class {
|
|
1031
|
-
name = "SmartRouter";
|
|
1032
|
-
#routers = [];
|
|
1033
|
-
#routes = [];
|
|
1034
|
-
constructor(init) {
|
|
1035
|
-
this.#routers = init.routers;
|
|
1036
|
-
}
|
|
1037
|
-
add(method, path$1, handler) {
|
|
1038
|
-
if (!this.#routes) throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1039
|
-
this.#routes.push([
|
|
1040
|
-
method,
|
|
1041
|
-
path$1,
|
|
1042
|
-
handler
|
|
1043
|
-
]);
|
|
1044
|
-
}
|
|
1045
|
-
match(method, path$1) {
|
|
1046
|
-
if (!this.#routes) throw new Error("Fatal error");
|
|
1047
|
-
const routers = this.#routers;
|
|
1048
|
-
const routes = this.#routes;
|
|
1049
|
-
const len = routers.length;
|
|
1050
|
-
let i = 0;
|
|
1051
|
-
let res;
|
|
1052
|
-
for (; i < len; i++) {
|
|
1053
|
-
const router = routers[i];
|
|
1054
|
-
try {
|
|
1055
|
-
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) router.add(...routes[i2]);
|
|
1056
|
-
res = router.match(method, path$1);
|
|
1057
|
-
} catch (e) {
|
|
1058
|
-
if (e instanceof UnsupportedPathError) continue;
|
|
1059
|
-
throw e;
|
|
1060
|
-
}
|
|
1061
|
-
this.match = router.match.bind(router);
|
|
1062
|
-
this.#routers = [router];
|
|
1063
|
-
this.#routes = void 0;
|
|
1064
|
-
break;
|
|
1065
|
-
}
|
|
1066
|
-
if (i === len) throw new Error("Fatal error");
|
|
1067
|
-
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
1068
|
-
return res;
|
|
1069
|
-
}
|
|
1070
|
-
get activeRouter() {
|
|
1071
|
-
if (this.#routes || this.#routers.length !== 1) throw new Error("No active router has been determined yet.");
|
|
1072
|
-
return this.#routers[0];
|
|
1073
|
-
}
|
|
1074
|
-
};
|
|
1075
|
-
|
|
1076
|
-
//#endregion
|
|
1077
|
-
//#region node_modules/hono/dist/router/trie-router/node.js
|
|
1078
|
-
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
1079
|
-
var Node = class {
|
|
1080
|
-
#methods;
|
|
1081
|
-
#children;
|
|
1082
|
-
#patterns;
|
|
1083
|
-
#order = 0;
|
|
1084
|
-
#params = emptyParams;
|
|
1085
|
-
constructor(method, handler, children) {
|
|
1086
|
-
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
1087
|
-
this.#methods = [];
|
|
1088
|
-
if (method && handler) {
|
|
1089
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
1090
|
-
m[method] = {
|
|
1091
|
-
handler,
|
|
1092
|
-
possibleKeys: [],
|
|
1093
|
-
score: 0
|
|
1094
|
-
};
|
|
1095
|
-
this.#methods = [m];
|
|
1096
|
-
}
|
|
1097
|
-
this.#patterns = [];
|
|
1098
|
-
}
|
|
1099
|
-
insert(method, path$1, handler) {
|
|
1100
|
-
this.#order = ++this.#order;
|
|
1101
|
-
let curNode = this;
|
|
1102
|
-
const parts = splitRoutingPath(path$1);
|
|
1103
|
-
const possibleKeys = [];
|
|
1104
|
-
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1105
|
-
const p = parts[i];
|
|
1106
|
-
const nextP = parts[i + 1];
|
|
1107
|
-
const pattern = getPattern(p, nextP);
|
|
1108
|
-
const key = Array.isArray(pattern) ? pattern[0] : p;
|
|
1109
|
-
if (key in curNode.#children) {
|
|
1110
|
-
curNode = curNode.#children[key];
|
|
1111
|
-
if (pattern) possibleKeys.push(pattern[1]);
|
|
1112
|
-
continue;
|
|
1113
|
-
}
|
|
1114
|
-
curNode.#children[key] = new Node();
|
|
1115
|
-
if (pattern) {
|
|
1116
|
-
curNode.#patterns.push(pattern);
|
|
1117
|
-
possibleKeys.push(pattern[1]);
|
|
1118
|
-
}
|
|
1119
|
-
curNode = curNode.#children[key];
|
|
1120
|
-
}
|
|
1121
|
-
curNode.#methods.push({ [method]: {
|
|
1122
|
-
handler,
|
|
1123
|
-
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
1124
|
-
score: this.#order
|
|
1125
|
-
} });
|
|
1126
|
-
return curNode;
|
|
1127
|
-
}
|
|
1128
|
-
#getHandlerSets(node, method, nodeParams, params) {
|
|
1129
|
-
const handlerSets = [];
|
|
1130
|
-
for (let i = 0, len = node.#methods.length; i < len; i++) {
|
|
1131
|
-
const m = node.#methods[i];
|
|
1132
|
-
const handlerSet = m[method] || m[METHOD_NAME_ALL];
|
|
1133
|
-
const processedSet = {};
|
|
1134
|
-
if (handlerSet !== void 0) {
|
|
1135
|
-
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
1136
|
-
handlerSets.push(handlerSet);
|
|
1137
|
-
if (nodeParams !== emptyParams || params && params !== emptyParams) for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
1138
|
-
const key = handlerSet.possibleKeys[i2];
|
|
1139
|
-
const processed = processedSet[handlerSet.score];
|
|
1140
|
-
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
1141
|
-
processedSet[handlerSet.score] = true;
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
return handlerSets;
|
|
1146
|
-
}
|
|
1147
|
-
search(method, path$1) {
|
|
1148
|
-
const handlerSets = [];
|
|
1149
|
-
this.#params = emptyParams;
|
|
1150
|
-
let curNodes = [this];
|
|
1151
|
-
const parts = splitPath(path$1);
|
|
1152
|
-
const curNodesQueue = [];
|
|
1153
|
-
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1154
|
-
const part = parts[i];
|
|
1155
|
-
const isLast = i === len - 1;
|
|
1156
|
-
const tempNodes = [];
|
|
1157
|
-
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
1158
|
-
const node = curNodes[j];
|
|
1159
|
-
const nextNode = node.#children[part];
|
|
1160
|
-
if (nextNode) {
|
|
1161
|
-
nextNode.#params = node.#params;
|
|
1162
|
-
if (isLast) {
|
|
1163
|
-
if (nextNode.#children["*"]) handlerSets.push(...this.#getHandlerSets(nextNode.#children["*"], method, node.#params));
|
|
1164
|
-
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
|
|
1165
|
-
} else tempNodes.push(nextNode);
|
|
1166
|
-
}
|
|
1167
|
-
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
|
|
1168
|
-
const pattern = node.#patterns[k];
|
|
1169
|
-
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
1170
|
-
if (pattern === "*") {
|
|
1171
|
-
const astNode = node.#children["*"];
|
|
1172
|
-
if (astNode) {
|
|
1173
|
-
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
|
|
1174
|
-
astNode.#params = params;
|
|
1175
|
-
tempNodes.push(astNode);
|
|
1176
|
-
}
|
|
1177
|
-
continue;
|
|
1178
|
-
}
|
|
1179
|
-
const [key, name, matcher] = pattern;
|
|
1180
|
-
if (!part && !(matcher instanceof RegExp)) continue;
|
|
1181
|
-
const child = node.#children[key];
|
|
1182
|
-
const restPathString = parts.slice(i).join("/");
|
|
1183
|
-
if (matcher instanceof RegExp) {
|
|
1184
|
-
const m = matcher.exec(restPathString);
|
|
1185
|
-
if (m) {
|
|
1186
|
-
params[name] = m[0];
|
|
1187
|
-
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
1188
|
-
if (Object.keys(child.#children).length) {
|
|
1189
|
-
child.#params = params;
|
|
1190
|
-
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
1191
|
-
(curNodesQueue[componentCount] ||= []).push(child);
|
|
1192
|
-
}
|
|
1193
|
-
continue;
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
if (matcher === true || matcher.test(part)) {
|
|
1197
|
-
params[name] = part;
|
|
1198
|
-
if (isLast) {
|
|
1199
|
-
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
1200
|
-
if (child.#children["*"]) handlerSets.push(...this.#getHandlerSets(child.#children["*"], method, params, node.#params));
|
|
1201
|
-
} else {
|
|
1202
|
-
child.#params = params;
|
|
1203
|
-
tempNodes.push(child);
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
|
|
1209
|
-
}
|
|
1210
|
-
if (handlerSets.length > 1) handlerSets.sort((a, b) => {
|
|
1211
|
-
return a.score - b.score;
|
|
1212
|
-
});
|
|
1213
|
-
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
1214
|
-
}
|
|
1215
|
-
};
|
|
1216
|
-
|
|
1217
|
-
//#endregion
|
|
1218
|
-
//#region node_modules/hono/dist/router/trie-router/router.js
|
|
1219
|
-
var TrieRouter = class {
|
|
1220
|
-
name = "TrieRouter";
|
|
1221
|
-
#node;
|
|
1222
|
-
constructor() {
|
|
1223
|
-
this.#node = new Node();
|
|
1224
|
-
}
|
|
1225
|
-
add(method, path$1, handler) {
|
|
1226
|
-
const results = checkOptionalParameter(path$1);
|
|
1227
|
-
if (results) {
|
|
1228
|
-
for (let i = 0, len = results.length; i < len; i++) this.#node.insert(method, results[i], handler);
|
|
1229
|
-
return;
|
|
1230
|
-
}
|
|
1231
|
-
this.#node.insert(method, path$1, handler);
|
|
1232
|
-
}
|
|
1233
|
-
match(method, path$1) {
|
|
1234
|
-
return this.#node.search(method, path$1);
|
|
1235
|
-
}
|
|
1236
|
-
};
|
|
1237
|
-
|
|
1238
|
-
//#endregion
|
|
1239
|
-
//#region node_modules/hono/dist/hono.js
|
|
1240
|
-
var Hono = class extends Hono$1 {
|
|
1241
|
-
constructor(options = {}) {
|
|
1242
|
-
super(options);
|
|
1243
|
-
this.router = options.router ?? new SmartRouter({ routers: [new RegExpRouter(), new TrieRouter()] });
|
|
1244
|
-
}
|
|
1245
|
-
};
|
|
1246
|
-
|
|
1247
|
-
//#endregion
|
|
1248
|
-
//#region node_modules/hono/dist/middleware/cors/index.js
|
|
1249
|
-
var cors = (options) => {
|
|
1250
|
-
const opts = {
|
|
1251
|
-
origin: "*",
|
|
1252
|
-
allowMethods: [
|
|
1253
|
-
"GET",
|
|
1254
|
-
"HEAD",
|
|
1255
|
-
"PUT",
|
|
1256
|
-
"POST",
|
|
1257
|
-
"DELETE",
|
|
1258
|
-
"PATCH"
|
|
1259
|
-
],
|
|
1260
|
-
allowHeaders: [],
|
|
1261
|
-
exposeHeaders: [],
|
|
1262
|
-
...options
|
|
1263
|
-
};
|
|
1264
|
-
const findAllowOrigin = ((optsOrigin) => {
|
|
1265
|
-
if (typeof optsOrigin === "string") if (optsOrigin === "*") return () => optsOrigin;
|
|
1266
|
-
else return (origin) => optsOrigin === origin ? origin : null;
|
|
1267
|
-
else if (typeof optsOrigin === "function") return optsOrigin;
|
|
1268
|
-
else return (origin) => optsOrigin.includes(origin) ? origin : null;
|
|
1269
|
-
})(opts.origin);
|
|
1270
|
-
const findAllowMethods = ((optsAllowMethods) => {
|
|
1271
|
-
if (typeof optsAllowMethods === "function") return optsAllowMethods;
|
|
1272
|
-
else if (Array.isArray(optsAllowMethods)) return () => optsAllowMethods;
|
|
1273
|
-
else return () => [];
|
|
1274
|
-
})(opts.allowMethods);
|
|
1275
|
-
return async function cors2(c, next) {
|
|
1276
|
-
function set(key, value) {
|
|
1277
|
-
c.res.headers.set(key, value);
|
|
1278
|
-
}
|
|
1279
|
-
const allowOrigin = await findAllowOrigin(c.req.header("origin") || "", c);
|
|
1280
|
-
if (allowOrigin) set("Access-Control-Allow-Origin", allowOrigin);
|
|
1281
|
-
if (opts.origin !== "*") {
|
|
1282
|
-
const existingVary = c.req.header("Vary");
|
|
1283
|
-
if (existingVary) set("Vary", existingVary);
|
|
1284
|
-
else set("Vary", "Origin");
|
|
1285
|
-
}
|
|
1286
|
-
if (opts.credentials) set("Access-Control-Allow-Credentials", "true");
|
|
1287
|
-
if (opts.exposeHeaders?.length) set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
|
1288
|
-
if (c.req.method === "OPTIONS") {
|
|
1289
|
-
if (opts.maxAge != null) set("Access-Control-Max-Age", opts.maxAge.toString());
|
|
1290
|
-
const allowMethods = await findAllowMethods(c.req.header("origin") || "", c);
|
|
1291
|
-
if (allowMethods.length) set("Access-Control-Allow-Methods", allowMethods.join(","));
|
|
1292
|
-
let headers = opts.allowHeaders;
|
|
1293
|
-
if (!headers?.length) {
|
|
1294
|
-
const requestHeaders = c.req.header("Access-Control-Request-Headers");
|
|
1295
|
-
if (requestHeaders) headers = requestHeaders.split(/\s*,\s*/);
|
|
1296
|
-
}
|
|
1297
|
-
if (headers?.length) {
|
|
1298
|
-
set("Access-Control-Allow-Headers", headers.join(","));
|
|
1299
|
-
c.res.headers.append("Vary", "Access-Control-Request-Headers");
|
|
1300
|
-
}
|
|
1301
|
-
c.res.headers.delete("Content-Length");
|
|
1302
|
-
c.res.headers.delete("Content-Type");
|
|
1303
|
-
return new Response(null, {
|
|
1304
|
-
headers: c.res.headers,
|
|
1305
|
-
status: 204,
|
|
1306
|
-
statusText: "No Content"
|
|
1307
|
-
});
|
|
1308
|
-
}
|
|
1309
|
-
await next();
|
|
1310
|
-
};
|
|
1311
|
-
};
|
|
1312
|
-
|
|
1313
|
-
//#endregion
|
|
1314
|
-
//#region node_modules/hono/dist/utils/color.js
|
|
1315
|
-
function getColorEnabled() {
|
|
1316
|
-
const { process: process$1, Deno } = globalThis;
|
|
1317
|
-
return !(typeof Deno?.noColor === "boolean" ? Deno.noColor : process$1 !== void 0 ? "NO_COLOR" in process$1?.env : false);
|
|
1318
|
-
}
|
|
1319
|
-
async function getColorEnabledAsync() {
|
|
1320
|
-
const { navigator } = globalThis;
|
|
1321
|
-
const cfWorkers = "cloudflare:workers";
|
|
1322
|
-
return !(navigator !== void 0 && navigator.userAgent === "Cloudflare-Workers" ? await (async () => {
|
|
1323
|
-
try {
|
|
1324
|
-
return "NO_COLOR" in ((await import(cfWorkers)).env ?? {});
|
|
1325
|
-
} catch {
|
|
1326
|
-
return false;
|
|
1327
|
-
}
|
|
1328
|
-
})() : !getColorEnabled());
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
//#endregion
|
|
1332
|
-
//#region node_modules/hono/dist/middleware/logger/index.js
|
|
1333
|
-
var humanize = (times) => {
|
|
1334
|
-
const [delimiter, separator] = [",", "."];
|
|
1335
|
-
return times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter)).join(separator);
|
|
1336
|
-
};
|
|
1337
|
-
var time = (start) => {
|
|
1338
|
-
const delta = Date.now() - start;
|
|
1339
|
-
return humanize([delta < 1e3 ? delta + "ms" : Math.round(delta / 1e3) + "s"]);
|
|
1340
|
-
};
|
|
1341
|
-
var colorStatus = async (status) => {
|
|
1342
|
-
if (await getColorEnabledAsync()) switch (status / 100 | 0) {
|
|
1343
|
-
case 5: return `\x1B[31m${status}\x1B[0m`;
|
|
1344
|
-
case 4: return `\x1B[33m${status}\x1B[0m`;
|
|
1345
|
-
case 3: return `\x1B[36m${status}\x1B[0m`;
|
|
1346
|
-
case 2: return `\x1B[32m${status}\x1B[0m`;
|
|
1347
|
-
}
|
|
1348
|
-
return `${status}`;
|
|
1349
|
-
};
|
|
1350
|
-
async function log(fn, prefix, method, path$1, status = 0, elapsed) {
|
|
1351
|
-
const out = prefix === "<--" ? `${prefix} ${method} ${path$1}` : `${prefix} ${method} ${path$1} ${await colorStatus(status)} ${elapsed}`;
|
|
1352
|
-
fn(out);
|
|
1353
|
-
}
|
|
1354
|
-
var logger = (fn = console.log) => {
|
|
1355
|
-
return async function logger2(c, next) {
|
|
1356
|
-
const { method, url } = c.req;
|
|
1357
|
-
const path$1 = url.slice(url.indexOf("/", 8));
|
|
1358
|
-
await log(fn, "<--", method, path$1);
|
|
1359
|
-
const start = Date.now();
|
|
1360
|
-
await next();
|
|
1361
|
-
await log(fn, "-->", method, path$1, c.res.status, time(start));
|
|
1362
|
-
};
|
|
1363
|
-
};
|
|
1364
|
-
|
|
1365
|
-
//#endregion
|
|
1366
14
|
//#region src/lib/request-auth.ts
|
|
1367
15
|
function normalizeApiKeys(apiKeys) {
|
|
1368
16
|
if (!Array.isArray(apiKeys)) {
|
|
@@ -1425,130 +73,6 @@ const traceIdMiddleware = async (c, next) => {
|
|
|
1425
73
|
});
|
|
1426
74
|
};
|
|
1427
75
|
|
|
1428
|
-
//#endregion
|
|
1429
|
-
//#region node_modules/hono/dist/utils/stream.js
|
|
1430
|
-
var StreamingApi = class {
|
|
1431
|
-
writer;
|
|
1432
|
-
encoder;
|
|
1433
|
-
writable;
|
|
1434
|
-
abortSubscribers = [];
|
|
1435
|
-
responseReadable;
|
|
1436
|
-
aborted = false;
|
|
1437
|
-
closed = false;
|
|
1438
|
-
constructor(writable, _readable) {
|
|
1439
|
-
this.writable = writable;
|
|
1440
|
-
this.writer = writable.getWriter();
|
|
1441
|
-
this.encoder = new TextEncoder();
|
|
1442
|
-
const reader = _readable.getReader();
|
|
1443
|
-
this.abortSubscribers.push(async () => {
|
|
1444
|
-
await reader.cancel();
|
|
1445
|
-
});
|
|
1446
|
-
this.responseReadable = new ReadableStream({
|
|
1447
|
-
async pull(controller) {
|
|
1448
|
-
const { done, value } = await reader.read();
|
|
1449
|
-
done ? controller.close() : controller.enqueue(value);
|
|
1450
|
-
},
|
|
1451
|
-
cancel: () => {
|
|
1452
|
-
this.abort();
|
|
1453
|
-
}
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
async write(input) {
|
|
1457
|
-
try {
|
|
1458
|
-
if (typeof input === "string") input = this.encoder.encode(input);
|
|
1459
|
-
await this.writer.write(input);
|
|
1460
|
-
} catch {}
|
|
1461
|
-
return this;
|
|
1462
|
-
}
|
|
1463
|
-
async writeln(input) {
|
|
1464
|
-
await this.write(input + "\n");
|
|
1465
|
-
return this;
|
|
1466
|
-
}
|
|
1467
|
-
sleep(ms) {
|
|
1468
|
-
return new Promise((res) => setTimeout(res, ms));
|
|
1469
|
-
}
|
|
1470
|
-
async close() {
|
|
1471
|
-
try {
|
|
1472
|
-
await this.writer.close();
|
|
1473
|
-
} catch {}
|
|
1474
|
-
this.closed = true;
|
|
1475
|
-
}
|
|
1476
|
-
async pipe(body) {
|
|
1477
|
-
this.writer.releaseLock();
|
|
1478
|
-
await body.pipeTo(this.writable, { preventClose: true });
|
|
1479
|
-
this.writer = this.writable.getWriter();
|
|
1480
|
-
}
|
|
1481
|
-
onAbort(listener) {
|
|
1482
|
-
this.abortSubscribers.push(listener);
|
|
1483
|
-
}
|
|
1484
|
-
abort() {
|
|
1485
|
-
if (!this.aborted) {
|
|
1486
|
-
this.aborted = true;
|
|
1487
|
-
this.abortSubscribers.forEach((subscriber) => subscriber());
|
|
1488
|
-
}
|
|
1489
|
-
}
|
|
1490
|
-
};
|
|
1491
|
-
|
|
1492
|
-
//#endregion
|
|
1493
|
-
//#region node_modules/hono/dist/helper/streaming/utils.js
|
|
1494
|
-
var isOldBunVersion = () => {
|
|
1495
|
-
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
|
1496
|
-
if (version === void 0) return false;
|
|
1497
|
-
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
|
1498
|
-
isOldBunVersion = () => result;
|
|
1499
|
-
return result;
|
|
1500
|
-
};
|
|
1501
|
-
|
|
1502
|
-
//#endregion
|
|
1503
|
-
//#region node_modules/hono/dist/helper/streaming/sse.js
|
|
1504
|
-
var SSEStreamingApi = class extends StreamingApi {
|
|
1505
|
-
constructor(writable, readable) {
|
|
1506
|
-
super(writable, readable);
|
|
1507
|
-
}
|
|
1508
|
-
async writeSSE(message) {
|
|
1509
|
-
const dataLines = (await resolveCallback(message.data, HtmlEscapedCallbackPhase.Stringify, false, {})).split("\n").map((line) => {
|
|
1510
|
-
return `data: ${line}`;
|
|
1511
|
-
}).join("\n");
|
|
1512
|
-
const sseData = [
|
|
1513
|
-
message.event && `event: ${message.event}`,
|
|
1514
|
-
dataLines,
|
|
1515
|
-
message.id && `id: ${message.id}`,
|
|
1516
|
-
message.retry && `retry: ${message.retry}`
|
|
1517
|
-
].filter(Boolean).join("\n") + "\n\n";
|
|
1518
|
-
await this.write(sseData);
|
|
1519
|
-
}
|
|
1520
|
-
};
|
|
1521
|
-
var run = async (stream$1, cb, onError) => {
|
|
1522
|
-
try {
|
|
1523
|
-
await cb(stream$1);
|
|
1524
|
-
} catch (e) {
|
|
1525
|
-
if (e instanceof Error && onError) {
|
|
1526
|
-
await onError(e, stream$1);
|
|
1527
|
-
await stream$1.writeSSE({
|
|
1528
|
-
event: "error",
|
|
1529
|
-
data: e.message
|
|
1530
|
-
});
|
|
1531
|
-
} else console.error(e);
|
|
1532
|
-
} finally {
|
|
1533
|
-
stream$1.close();
|
|
1534
|
-
}
|
|
1535
|
-
};
|
|
1536
|
-
var contextStash = /* @__PURE__ */ new WeakMap();
|
|
1537
|
-
var streamSSE = (c, cb, onError) => {
|
|
1538
|
-
const { readable, writable } = new TransformStream();
|
|
1539
|
-
const stream$1 = new SSEStreamingApi(writable, readable);
|
|
1540
|
-
if (isOldBunVersion()) c.req.raw.signal.addEventListener("abort", () => {
|
|
1541
|
-
if (!stream$1.closed) stream$1.abort();
|
|
1542
|
-
});
|
|
1543
|
-
contextStash.set(stream$1.responseReadable, c);
|
|
1544
|
-
c.header("Transfer-Encoding", "chunked");
|
|
1545
|
-
c.header("Content-Type", "text/event-stream");
|
|
1546
|
-
c.header("Cache-Control", "no-cache");
|
|
1547
|
-
c.header("Connection", "keep-alive");
|
|
1548
|
-
run(stream$1, cb, onError);
|
|
1549
|
-
return c.newResponse(stream$1.responseReadable);
|
|
1550
|
-
};
|
|
1551
|
-
|
|
1552
76
|
//#endregion
|
|
1553
77
|
//#region src/lib/approval.ts
|
|
1554
78
|
const awaitApproval = async () => {
|
|
@@ -1603,9 +127,9 @@ const maybeUnref = (timer) => {
|
|
|
1603
127
|
const flushBuffer = (filePath) => {
|
|
1604
128
|
const buffer = logBuffers.get(filePath);
|
|
1605
129
|
if (!buffer || buffer.length === 0) return;
|
|
1606
|
-
const stream
|
|
130
|
+
const stream = getLogStream(filePath);
|
|
1607
131
|
const content = buffer.join("\n") + "\n";
|
|
1608
|
-
stream
|
|
132
|
+
stream.write(content, (error) => {
|
|
1609
133
|
if (error) console.warn("Failed to write handler log", error);
|
|
1610
134
|
});
|
|
1611
135
|
logBuffers.set(filePath, []);
|
|
@@ -1623,7 +147,7 @@ const cleanup = () => {
|
|
|
1623
147
|
cleanupInterval = void 0;
|
|
1624
148
|
}
|
|
1625
149
|
flushAllBuffers();
|
|
1626
|
-
for (const stream
|
|
150
|
+
for (const stream of logStreams.values()) stream.end();
|
|
1627
151
|
logStreams.clear();
|
|
1628
152
|
logBuffers.clear();
|
|
1629
153
|
};
|
|
@@ -1648,16 +172,16 @@ const initializeLoggerRuntime = () => {
|
|
|
1648
172
|
};
|
|
1649
173
|
const getLogStream = (filePath) => {
|
|
1650
174
|
initializeLoggerRuntime();
|
|
1651
|
-
let stream
|
|
1652
|
-
if (!stream
|
|
1653
|
-
stream
|
|
1654
|
-
logStreams.set(filePath, stream
|
|
1655
|
-
stream
|
|
175
|
+
let stream = logStreams.get(filePath);
|
|
176
|
+
if (!stream || stream.destroyed) {
|
|
177
|
+
stream = fs.createWriteStream(filePath, { flags: "a" });
|
|
178
|
+
logStreams.set(filePath, stream);
|
|
179
|
+
stream.on("error", (error) => {
|
|
1656
180
|
console.warn("Log stream error", error);
|
|
1657
181
|
logStreams.delete(filePath);
|
|
1658
182
|
});
|
|
1659
183
|
}
|
|
1660
|
-
return stream
|
|
184
|
+
return stream;
|
|
1661
185
|
};
|
|
1662
186
|
const appendLine = (filePath, line) => {
|
|
1663
187
|
let buffer = logBuffers.get(filePath);
|
|
@@ -1724,132 +248,6 @@ async function checkRateLimit(state$1) {
|
|
|
1724
248
|
consola.info("Rate limit wait completed, proceeding with request");
|
|
1725
249
|
}
|
|
1726
250
|
|
|
1727
|
-
//#endregion
|
|
1728
|
-
//#region node_modules/fetch-event-stream/esm/deps/jsr.io/@std/streams/0.221.0/text_line_stream.js
|
|
1729
|
-
/**
|
|
1730
|
-
* Transform a stream into a stream where each chunk is divided by a newline,
|
|
1731
|
-
* be it `\n` or `\r\n`. `\r` can be enabled via the `allowCR` option.
|
|
1732
|
-
*
|
|
1733
|
-
* @example
|
|
1734
|
-
* ```ts
|
|
1735
|
-
* import { TextLineStream } from "@std/streams/text-line-stream";
|
|
1736
|
-
*
|
|
1737
|
-
* const res = await fetch("https://example.com");
|
|
1738
|
-
* const lines = res.body!
|
|
1739
|
-
* .pipeThrough(new TextDecoderStream())
|
|
1740
|
-
* .pipeThrough(new TextLineStream());
|
|
1741
|
-
* ```
|
|
1742
|
-
*/
|
|
1743
|
-
var TextLineStream = class extends TransformStream {
|
|
1744
|
-
#currentLine = "";
|
|
1745
|
-
/** Constructs a new instance. */
|
|
1746
|
-
constructor(options = { allowCR: false }) {
|
|
1747
|
-
super({
|
|
1748
|
-
transform: (chars, controller) => {
|
|
1749
|
-
chars = this.#currentLine + chars;
|
|
1750
|
-
while (true) {
|
|
1751
|
-
const lfIndex = chars.indexOf("\n");
|
|
1752
|
-
const crIndex = options.allowCR ? chars.indexOf("\r") : -1;
|
|
1753
|
-
if (crIndex !== -1 && crIndex !== chars.length - 1 && (lfIndex === -1 || lfIndex - 1 > crIndex)) {
|
|
1754
|
-
controller.enqueue(chars.slice(0, crIndex));
|
|
1755
|
-
chars = chars.slice(crIndex + 1);
|
|
1756
|
-
continue;
|
|
1757
|
-
}
|
|
1758
|
-
if (lfIndex === -1) break;
|
|
1759
|
-
const endIndex = chars[lfIndex - 1] === "\r" ? lfIndex - 1 : lfIndex;
|
|
1760
|
-
controller.enqueue(chars.slice(0, endIndex));
|
|
1761
|
-
chars = chars.slice(lfIndex + 1);
|
|
1762
|
-
}
|
|
1763
|
-
this.#currentLine = chars;
|
|
1764
|
-
},
|
|
1765
|
-
flush: (controller) => {
|
|
1766
|
-
if (this.#currentLine === "") return;
|
|
1767
|
-
const currentLine = options.allowCR && this.#currentLine.endsWith("\r") ? this.#currentLine.slice(0, -1) : this.#currentLine;
|
|
1768
|
-
controller.enqueue(currentLine);
|
|
1769
|
-
}
|
|
1770
|
-
});
|
|
1771
|
-
}
|
|
1772
|
-
};
|
|
1773
|
-
|
|
1774
|
-
//#endregion
|
|
1775
|
-
//#region node_modules/fetch-event-stream/esm/utils.js
|
|
1776
|
-
function stream(input) {
|
|
1777
|
-
let decoder = new TextDecoderStream();
|
|
1778
|
-
let split$1 = new TextLineStream({ allowCR: true });
|
|
1779
|
-
return input.pipeThrough(decoder).pipeThrough(split$1);
|
|
1780
|
-
}
|
|
1781
|
-
function split(input) {
|
|
1782
|
-
let match = /[:]\s*/.exec(input);
|
|
1783
|
-
let idx = match && match.index;
|
|
1784
|
-
if (idx) return [input.substring(0, idx), input.substring(idx + match[0].length)];
|
|
1785
|
-
}
|
|
1786
|
-
|
|
1787
|
-
//#endregion
|
|
1788
|
-
//#region node_modules/fetch-event-stream/esm/mod.js
|
|
1789
|
-
/**
|
|
1790
|
-
* Convert a `Response` body containing Server Sent Events (SSE) into an Async Iterator that yields {@linkcode ServerSentEventMessage} objects.
|
|
1791
|
-
*
|
|
1792
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events}
|
|
1793
|
-
*
|
|
1794
|
-
* @example
|
|
1795
|
-
* ```js
|
|
1796
|
-
* // Optional
|
|
1797
|
-
* let abort = new AbortController;
|
|
1798
|
-
*
|
|
1799
|
-
* // Manually fetch a Response
|
|
1800
|
-
* let res = await fetch('https://...', {
|
|
1801
|
-
* method: 'POST',
|
|
1802
|
-
* signal: abort.signal,
|
|
1803
|
-
* headers: {
|
|
1804
|
-
* 'api-key': 'token <value>',
|
|
1805
|
-
* 'content-type': 'application/json',
|
|
1806
|
-
* },
|
|
1807
|
-
* body: JSON.stringify({
|
|
1808
|
-
* stream: true, // <- hypothetical
|
|
1809
|
-
* // ...
|
|
1810
|
-
* })
|
|
1811
|
-
* });
|
|
1812
|
-
*
|
|
1813
|
-
* if (res.ok) {
|
|
1814
|
-
* let stream = events(res, abort.signal);
|
|
1815
|
-
* for await (let event of stream) {
|
|
1816
|
-
* console.log('<<', event.data);
|
|
1817
|
-
* }
|
|
1818
|
-
* }
|
|
1819
|
-
* ```
|
|
1820
|
-
*/
|
|
1821
|
-
async function* events(res, signal) {
|
|
1822
|
-
if (!res.body) return;
|
|
1823
|
-
let iter = stream(res.body);
|
|
1824
|
-
let line, reader = iter.getReader();
|
|
1825
|
-
let event;
|
|
1826
|
-
for (;;) {
|
|
1827
|
-
if (signal && signal.aborted) return reader.cancel();
|
|
1828
|
-
line = await reader.read();
|
|
1829
|
-
if (line.done) return;
|
|
1830
|
-
if (!line.value) {
|
|
1831
|
-
if (event) yield event;
|
|
1832
|
-
event = void 0;
|
|
1833
|
-
continue;
|
|
1834
|
-
}
|
|
1835
|
-
let [field, value] = split(line.value) || [];
|
|
1836
|
-
if (!field) continue;
|
|
1837
|
-
if (field === "data") {
|
|
1838
|
-
event ||= {};
|
|
1839
|
-
event[field] = event[field] ? event[field] + "\n" + value : value;
|
|
1840
|
-
} else if (field === "event") {
|
|
1841
|
-
event ||= {};
|
|
1842
|
-
event[field] = value;
|
|
1843
|
-
} else if (field === "id") {
|
|
1844
|
-
event ||= {};
|
|
1845
|
-
event[field] = +value || value;
|
|
1846
|
-
} else if (field === "retry") {
|
|
1847
|
-
event ||= {};
|
|
1848
|
-
event[field] = +value || void 0;
|
|
1849
|
-
}
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1852
|
-
|
|
1853
251
|
//#endregion
|
|
1854
252
|
//#region src/lib/copilot-rate-limit.ts
|
|
1855
253
|
const copilotRateLimitTypes = ["session", "weekly"];
|
|
@@ -1963,10 +361,10 @@ async function handleCompletion$1(c) {
|
|
|
1963
361
|
return c.json(response);
|
|
1964
362
|
}
|
|
1965
363
|
logger$6.debug("Streaming response");
|
|
1966
|
-
return streamSSE(c, async (stream
|
|
364
|
+
return streamSSE(c, async (stream) => {
|
|
1967
365
|
for await (const chunk of response) {
|
|
1968
366
|
debugJson(logger$6, "Streaming chunk:", chunk);
|
|
1969
|
-
await stream
|
|
367
|
+
await stream.writeSSE(chunk);
|
|
1970
368
|
}
|
|
1971
369
|
});
|
|
1972
370
|
}
|
|
@@ -2012,11 +410,11 @@ embeddingRoutes.post("/", async (c) => {
|
|
|
2012
410
|
//#endregion
|
|
2013
411
|
//#region src/lib/tokenizer.ts
|
|
2014
412
|
const ENCODING_MAP = {
|
|
2015
|
-
o200k_base: () => import("
|
|
2016
|
-
cl100k_base: () => import("
|
|
2017
|
-
p50k_base: () => import("
|
|
2018
|
-
p50k_edit: () => import("
|
|
2019
|
-
r50k_base: () => import("
|
|
413
|
+
o200k_base: () => import("gpt-tokenizer/encoding/o200k_base"),
|
|
414
|
+
cl100k_base: () => import("gpt-tokenizer/encoding/cl100k_base"),
|
|
415
|
+
p50k_base: () => import("gpt-tokenizer/encoding/p50k_base"),
|
|
416
|
+
p50k_edit: () => import("gpt-tokenizer/encoding/p50k_edit"),
|
|
417
|
+
r50k_base: () => import("gpt-tokenizer/encoding/r50k_base")
|
|
2020
418
|
};
|
|
2021
419
|
const encodingCache = /* @__PURE__ */ new Map();
|
|
2022
420
|
/**
|
|
@@ -2659,11 +1057,11 @@ const encodeCompactionCarrierSignature = (compaction) => {
|
|
|
2659
1057
|
};
|
|
2660
1058
|
const decodeCompactionCarrierSignature = (signature) => {
|
|
2661
1059
|
if (signature.startsWith(COMPACTION_SIGNATURE_PREFIX)) {
|
|
2662
|
-
const raw
|
|
2663
|
-
const separatorIndex = raw
|
|
2664
|
-
if (separatorIndex <= 0 || separatorIndex === raw
|
|
2665
|
-
const encrypted_content = raw
|
|
2666
|
-
const id = raw
|
|
1060
|
+
const raw = signature.slice(4);
|
|
1061
|
+
const separatorIndex = raw.indexOf(COMPACTION_SIGNATURE_SEPARATOR);
|
|
1062
|
+
if (separatorIndex <= 0 || separatorIndex === raw.length - 1) return;
|
|
1063
|
+
const encrypted_content = raw.slice(0, separatorIndex);
|
|
1064
|
+
const id = raw.slice(separatorIndex + 1);
|
|
2667
1065
|
if (!encrypted_content) return;
|
|
2668
1066
|
return {
|
|
2669
1067
|
id,
|
|
@@ -4108,7 +2506,7 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
|
|
|
4108
2506
|
return c.json(anthropicResponse);
|
|
4109
2507
|
}
|
|
4110
2508
|
logger$7.debug("Streaming response from Copilot");
|
|
4111
|
-
return streamSSE(c, async (stream
|
|
2509
|
+
return streamSSE(c, async (stream) => {
|
|
4112
2510
|
const streamState = {
|
|
4113
2511
|
messageStartSent: false,
|
|
4114
2512
|
contentBlockIndex: 0,
|
|
@@ -4125,7 +2523,7 @@ const handleWithChatCompletions = async (c, anthropicPayload, options) => {
|
|
|
4125
2523
|
for (const event of events$1) {
|
|
4126
2524
|
const eventData = JSON.stringify(event);
|
|
4127
2525
|
debugLazy(logger$7, () => ["Translated Anthropic event:", eventData]);
|
|
4128
|
-
await stream
|
|
2526
|
+
await stream.writeSSE({
|
|
4129
2527
|
event: event.type,
|
|
4130
2528
|
data: eventData
|
|
4131
2529
|
});
|
|
@@ -4150,11 +2548,11 @@ const handleWithResponsesApi = async (c, anthropicPayload, options) => {
|
|
|
4150
2548
|
});
|
|
4151
2549
|
if (responsesPayload.stream && isAsyncIterable$1(response)) {
|
|
4152
2550
|
logger$7.debug("Streaming response from Copilot (Responses API)");
|
|
4153
|
-
return streamSSE(c, async (stream
|
|
2551
|
+
return streamSSE(c, async (stream) => {
|
|
4154
2552
|
const streamState = createResponsesStreamState();
|
|
4155
2553
|
for await (const chunk of response) {
|
|
4156
2554
|
if (chunk.event === "ping") {
|
|
4157
|
-
await stream
|
|
2555
|
+
await stream.writeSSE({
|
|
4158
2556
|
event: "ping",
|
|
4159
2557
|
data: "{\"type\":\"ping\"}"
|
|
4160
2558
|
});
|
|
@@ -4167,7 +2565,7 @@ const handleWithResponsesApi = async (c, anthropicPayload, options) => {
|
|
|
4167
2565
|
for (const event of events$1) {
|
|
4168
2566
|
const eventData = JSON.stringify(event);
|
|
4169
2567
|
debugLazy(logger$7, () => ["Translated Anthropic event:", eventData]);
|
|
4170
|
-
await stream
|
|
2568
|
+
await stream.writeSSE({
|
|
4171
2569
|
event: event.type,
|
|
4172
2570
|
data: eventData
|
|
4173
2571
|
});
|
|
@@ -4180,7 +2578,7 @@ const handleWithResponsesApi = async (c, anthropicPayload, options) => {
|
|
|
4180
2578
|
if (!streamState.messageCompleted) {
|
|
4181
2579
|
logger$7.warn("Responses stream ended without completion; sending error event");
|
|
4182
2580
|
const errorEvent = buildErrorEvent("Responses stream ended without completion");
|
|
4183
|
-
await stream
|
|
2581
|
+
await stream.writeSSE({
|
|
4184
2582
|
event: errorEvent.type,
|
|
4185
2583
|
data: JSON.stringify(errorEvent)
|
|
4186
2584
|
});
|
|
@@ -4207,14 +2605,14 @@ const handleWithMessagesApi = async (c, anthropicPayload, options) => {
|
|
|
4207
2605
|
});
|
|
4208
2606
|
if (isAsyncIterable$1(response)) {
|
|
4209
2607
|
logger$7.debug("Streaming response from Copilot (Messages API)");
|
|
4210
|
-
return streamSSE(c, async (stream
|
|
2608
|
+
return streamSSE(c, async (stream) => {
|
|
4211
2609
|
for await (const event of response) {
|
|
4212
2610
|
const eventName = event.event;
|
|
4213
2611
|
const data = event.data ?? "";
|
|
4214
2612
|
if (data === "[DONE]") break;
|
|
4215
2613
|
if (!data) continue;
|
|
4216
2614
|
debugLazy(logger$7, () => ["Messages raw stream event:", data]);
|
|
4217
|
-
await stream
|
|
2615
|
+
await stream.writeSSE({
|
|
4218
2616
|
event: eventName,
|
|
4219
2617
|
data
|
|
4220
2618
|
});
|
|
@@ -4520,12 +2918,12 @@ async function handleProviderMessages(c) {
|
|
|
4520
2918
|
const contentType = upstreamResponse.headers.get("content-type") ?? "";
|
|
4521
2919
|
if (Boolean(payload.stream) && contentType.includes("text/event-stream")) {
|
|
4522
2920
|
logger$3.debug("provider.messages.streaming");
|
|
4523
|
-
return streamSSE(c, async (stream
|
|
2921
|
+
return streamSSE(c, async (stream) => {
|
|
4524
2922
|
for await (const chunk of events(upstreamResponse)) {
|
|
4525
2923
|
logger$3.debug("provider.messages.raw_stream_event:", chunk.data);
|
|
4526
2924
|
const eventName = chunk.event;
|
|
4527
2925
|
if (eventName === "ping") {
|
|
4528
|
-
await stream
|
|
2926
|
+
await stream.writeSSE({
|
|
4529
2927
|
event: "ping",
|
|
4530
2928
|
data: "{\"type\":\"ping\"}"
|
|
4531
2929
|
});
|
|
@@ -4545,7 +2943,7 @@ async function handleProviderMessages(c) {
|
|
|
4545
2943
|
originalData: data
|
|
4546
2944
|
});
|
|
4547
2945
|
}
|
|
4548
|
-
await stream
|
|
2946
|
+
await stream.writeSSE({
|
|
4549
2947
|
event: eventName,
|
|
4550
2948
|
data
|
|
4551
2949
|
});
|
|
@@ -4685,12 +3083,12 @@ const handleResponses = async (c) => {
|
|
|
4685
3083
|
});
|
|
4686
3084
|
if (isStreamingRequested(payload) && isAsyncIterable(response)) {
|
|
4687
3085
|
logger$1.debug("Forwarding native Responses stream");
|
|
4688
|
-
return streamSSE(c, async (stream
|
|
3086
|
+
return streamSSE(c, async (stream) => {
|
|
4689
3087
|
const idTracker = createStreamIdTracker();
|
|
4690
3088
|
for await (const chunk of response) {
|
|
4691
3089
|
debugJson(logger$1, "Responses stream chunk:", chunk);
|
|
4692
3090
|
const processedData = fixStreamIds(chunk.data ?? "", chunk.event, idTracker);
|
|
4693
|
-
await stream
|
|
3091
|
+
await stream.writeSSE({
|
|
4694
3092
|
id: chunk.id,
|
|
4695
3093
|
event: chunk.event,
|
|
4696
3094
|
data: processedData
|
|
@@ -4824,4 +3222,4 @@ server.route("/:provider/v1/models", providerModelRoutes);
|
|
|
4824
3222
|
|
|
4825
3223
|
//#endregion
|
|
4826
3224
|
export { server };
|
|
4827
|
-
//# sourceMappingURL=server-
|
|
3225
|
+
//# sourceMappingURL=server-CA39Gy7x.js.map
|