@twsxtd/hapi-openclaw 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/dist/index.js +2106 -0
- package/openclaw.plugin.json +60 -0
- package/package.json +48 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2106 @@
|
|
|
1
|
+
// ../node_modules/.bun/openclaw@2026.4.11+63c81f13a188c8c3/node_modules/openclaw/dist/config-schema-66-MD4FQ.js
|
|
2
|
+
function error(message) {
|
|
3
|
+
return {
|
|
4
|
+
success: false,
|
|
5
|
+
error: { issues: [{
|
|
6
|
+
path: [],
|
|
7
|
+
message
|
|
8
|
+
}] }
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function emptyPluginConfigSchema() {
|
|
12
|
+
return {
|
|
13
|
+
safeParse(value) {
|
|
14
|
+
if (value === undefined)
|
|
15
|
+
return {
|
|
16
|
+
success: true,
|
|
17
|
+
data: undefined
|
|
18
|
+
};
|
|
19
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
20
|
+
return error("expected config object");
|
|
21
|
+
if (Object.keys(value).length > 0)
|
|
22
|
+
return error("config must be empty");
|
|
23
|
+
return {
|
|
24
|
+
success: true,
|
|
25
|
+
data: value
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
jsonSchema: {
|
|
29
|
+
type: "object",
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
properties: {}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ../node_modules/.bun/openclaw@2026.4.11+63c81f13a188c8c3/node_modules/openclaw/dist/plugin-entry-BFdKnvae.js
|
|
37
|
+
function createCachedLazyValueGetter(value, fallback) {
|
|
38
|
+
let resolved = false;
|
|
39
|
+
let cached;
|
|
40
|
+
return () => {
|
|
41
|
+
if (!resolved) {
|
|
42
|
+
cached = (typeof value === "function" ? value() : value) ?? fallback;
|
|
43
|
+
resolved = true;
|
|
44
|
+
}
|
|
45
|
+
return cached;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function definePluginEntry({ id, name, description, kind, configSchema = emptyPluginConfigSchema, reload, nodeHostCommands, securityAuditCollectors, register }) {
|
|
49
|
+
const getConfigSchema = createCachedLazyValueGetter(configSchema);
|
|
50
|
+
return {
|
|
51
|
+
id,
|
|
52
|
+
name,
|
|
53
|
+
description,
|
|
54
|
+
...kind ? { kind } : {},
|
|
55
|
+
...reload ? { reload } : {},
|
|
56
|
+
...nodeHostCommands ? { nodeHostCommands } : {},
|
|
57
|
+
...securityAuditCollectors ? { securityAuditCollectors } : {},
|
|
58
|
+
get configSchema() {
|
|
59
|
+
return getConfigSchema();
|
|
60
|
+
},
|
|
61
|
+
register
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/pluginId.ts
|
|
66
|
+
var OPENCLAW_PLUGIN_ID = "hapi-openclaw";
|
|
67
|
+
var OPENCLAW_PLUGIN_NAME = "HAPI OpenClaw Adapter";
|
|
68
|
+
var OPENCLAW_PLUGIN_DESCRIPTION = "Bridge HAPI to OpenClaw through a native plugin route surface.";
|
|
69
|
+
var OPENCLAW_PLUGIN_VERSION = "0.1.0";
|
|
70
|
+
|
|
71
|
+
// src/config.ts
|
|
72
|
+
var PLUGIN_CONFIG_JSON_SCHEMA = {
|
|
73
|
+
type: "object",
|
|
74
|
+
additionalProperties: false,
|
|
75
|
+
required: ["hapiBaseUrl", "sharedSecret"],
|
|
76
|
+
properties: {
|
|
77
|
+
hapiBaseUrl: { type: "string", minLength: 1 },
|
|
78
|
+
sharedSecret: { type: "string", minLength: 1 },
|
|
79
|
+
namespace: { type: "string", minLength: 1 },
|
|
80
|
+
prototypeCaptureSessionKey: { type: "string", minLength: 1 },
|
|
81
|
+
prototypeCaptureFileName: { type: "string", minLength: 1 }
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function readOptionalNonEmptyString(value) {
|
|
85
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
86
|
+
}
|
|
87
|
+
function readRequiredString(value, fieldName) {
|
|
88
|
+
const parsed = readOptionalNonEmptyString(value);
|
|
89
|
+
if (!parsed) {
|
|
90
|
+
throw new Error(`Invalid ${OPENCLAW_PLUGIN_ID} config: ${fieldName} must be a non-empty string`);
|
|
91
|
+
}
|
|
92
|
+
return parsed;
|
|
93
|
+
}
|
|
94
|
+
function isRecord(value) {
|
|
95
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
96
|
+
}
|
|
97
|
+
function resolvePluginConfig(value) {
|
|
98
|
+
if (!isRecord(value)) {
|
|
99
|
+
throw new Error(`Invalid ${OPENCLAW_PLUGIN_ID} config: expected an object`);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
hapiBaseUrl: readRequiredString(value.hapiBaseUrl, "hapiBaseUrl"),
|
|
103
|
+
sharedSecret: readRequiredString(value.sharedSecret, "sharedSecret"),
|
|
104
|
+
namespace: readOptionalNonEmptyString(value.namespace) ?? "default",
|
|
105
|
+
prototypeCaptureSessionKey: readOptionalNonEmptyString(value.prototypeCaptureSessionKey),
|
|
106
|
+
prototypeCaptureFileName: readOptionalNonEmptyString(value.prototypeCaptureFileName) ?? "transcript-capture.jsonl"
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function resolvePluginConfigFromOpenClawConfig(config) {
|
|
110
|
+
const pluginConfig = config.plugins?.entries?.[OPENCLAW_PLUGIN_ID]?.config;
|
|
111
|
+
if (!pluginConfig) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return resolvePluginConfig(pluginConfig);
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/nativeRoute.ts
|
|
122
|
+
import { Readable } from "node:stream";
|
|
123
|
+
function isBodyAllowed(method) {
|
|
124
|
+
return method !== "GET" && method !== "HEAD";
|
|
125
|
+
}
|
|
126
|
+
function buildHeaders(req) {
|
|
127
|
+
const headers = new Headers;
|
|
128
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
for (const item of value) {
|
|
131
|
+
headers.append(key, item);
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (typeof value === "string") {
|
|
136
|
+
headers.set(key, value);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return headers;
|
|
140
|
+
}
|
|
141
|
+
async function forwardNodeRequestToHono(app, req, res) {
|
|
142
|
+
const method = req.method ?? "GET";
|
|
143
|
+
const url = new URL(req.url ?? "/", "http://openclaw.local");
|
|
144
|
+
const init = {
|
|
145
|
+
method,
|
|
146
|
+
headers: buildHeaders(req)
|
|
147
|
+
};
|
|
148
|
+
if (isBodyAllowed(method)) {
|
|
149
|
+
init.body = Readable.toWeb(req);
|
|
150
|
+
init.duplex = "half";
|
|
151
|
+
}
|
|
152
|
+
const response = await app.fetch(new Request(url.toString(), init));
|
|
153
|
+
res.statusCode = response.status;
|
|
154
|
+
response.headers.forEach((value, key) => {
|
|
155
|
+
res.setHeader(key, value);
|
|
156
|
+
});
|
|
157
|
+
if (!response.body) {
|
|
158
|
+
res.end();
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
162
|
+
res.end(body);
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ../node_modules/.bun/openclaw@2026.4.11+63c81f13a188c8c3/node_modules/openclaw/dist/runtime-store-B1YLS6z5.js
|
|
167
|
+
function createPluginRuntimeStore(errorMessage) {
|
|
168
|
+
let runtime = null;
|
|
169
|
+
return {
|
|
170
|
+
setRuntime(next) {
|
|
171
|
+
runtime = next;
|
|
172
|
+
},
|
|
173
|
+
clearRuntime() {
|
|
174
|
+
runtime = null;
|
|
175
|
+
},
|
|
176
|
+
tryGetRuntime() {
|
|
177
|
+
return runtime;
|
|
178
|
+
},
|
|
179
|
+
getRuntime() {
|
|
180
|
+
if (!runtime)
|
|
181
|
+
throw new Error(errorMessage);
|
|
182
|
+
return runtime;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/runtimeStore.ts
|
|
188
|
+
var runtimeStore = createPluginRuntimeStore("OpenClaw plugin runtime is not available outside native plugin registration");
|
|
189
|
+
|
|
190
|
+
// src/transcriptCapture.ts
|
|
191
|
+
import { appendFile, mkdir } from "node:fs/promises";
|
|
192
|
+
import { join } from "node:path";
|
|
193
|
+
var CAPTURE_DIRECTORY = "hapi-openclaw";
|
|
194
|
+
function resolveCaptureFilePath(ctx, config) {
|
|
195
|
+
const fileName = config?.prototypeCaptureFileName ?? "transcript-capture.jsonl";
|
|
196
|
+
return join(ctx.stateDir, CAPTURE_DIRECTORY, fileName);
|
|
197
|
+
}
|
|
198
|
+
async function writeCaptureRecord(ctx, config, record) {
|
|
199
|
+
const filePath = resolveCaptureFilePath(ctx, config);
|
|
200
|
+
await mkdir(join(ctx.stateDir, CAPTURE_DIRECTORY), { recursive: true });
|
|
201
|
+
await appendFile(filePath, `${JSON.stringify(record)}
|
|
202
|
+
`, "utf8");
|
|
203
|
+
}
|
|
204
|
+
function shouldCapture(config, sessionKey) {
|
|
205
|
+
if (!config?.prototypeCaptureSessionKey) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
return sessionKey === config.prototypeCaptureSessionKey;
|
|
209
|
+
}
|
|
210
|
+
function createTranscriptCaptureService() {
|
|
211
|
+
let stopListening = null;
|
|
212
|
+
return {
|
|
213
|
+
id: `${OPENCLAW_PLUGIN_ID}:transcript-capture`,
|
|
214
|
+
async start(ctx) {
|
|
215
|
+
const runtime = runtimeStore.getRuntime();
|
|
216
|
+
const config = resolvePluginConfigFromOpenClawConfig(ctx.config);
|
|
217
|
+
stopListening = runtime.events.onSessionTranscriptUpdate((update) => {
|
|
218
|
+
if (!shouldCapture(config, update.sessionKey)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
writeCaptureRecord(ctx, config, {
|
|
222
|
+
capturedAt: new Date().toISOString(),
|
|
223
|
+
sessionFile: update.sessionFile,
|
|
224
|
+
sessionKey: update.sessionKey,
|
|
225
|
+
messageId: update.messageId,
|
|
226
|
+
message: update.message
|
|
227
|
+
}).catch((error2) => {
|
|
228
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
229
|
+
ctx.logger.error(`Failed to write transcript capture: ${message}`);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
ctx.logger.info(`Started ${OPENCLAW_PLUGIN_ID} transcript-capture service`);
|
|
233
|
+
},
|
|
234
|
+
async stop() {
|
|
235
|
+
stopListening?.();
|
|
236
|
+
stopListening = null;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/signing.ts
|
|
242
|
+
import { createHmac } from "node:crypto";
|
|
243
|
+
function signCallbackBody(timestamp, rawBody, secret) {
|
|
244
|
+
return createHmac("sha256", secret).update(`${timestamp}.${rawBody}`).digest("hex");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/hapiClient.ts
|
|
248
|
+
class HapiCallbackClient {
|
|
249
|
+
hapiBaseUrl;
|
|
250
|
+
sharedSecret;
|
|
251
|
+
constructor(hapiBaseUrl, sharedSecret) {
|
|
252
|
+
this.hapiBaseUrl = hapiBaseUrl;
|
|
253
|
+
this.sharedSecret = sharedSecret;
|
|
254
|
+
}
|
|
255
|
+
async postEvent(event) {
|
|
256
|
+
const rawBody = JSON.stringify(event);
|
|
257
|
+
const timestamp = Date.now();
|
|
258
|
+
const signature = signCallbackBody(timestamp, rawBody, this.sharedSecret);
|
|
259
|
+
const response = await fetch(new URL("/api/openclaw/channel/events", this.hapiBaseUrl).toString(), {
|
|
260
|
+
method: "POST",
|
|
261
|
+
headers: {
|
|
262
|
+
"content-type": "application/json",
|
|
263
|
+
"x-openclaw-timestamp": `${timestamp}`,
|
|
264
|
+
"x-openclaw-signature": signature
|
|
265
|
+
},
|
|
266
|
+
body: rawBody
|
|
267
|
+
});
|
|
268
|
+
if (!response.ok) {
|
|
269
|
+
const text = await response.text().catch(() => "");
|
|
270
|
+
const detail = text ? `: ${text}` : "";
|
|
271
|
+
throw new Error(`HAPI callback failed with HTTP ${response.status}${detail}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/openclawRuntime.ts
|
|
277
|
+
import { randomUUID } from "node:crypto";
|
|
278
|
+
|
|
279
|
+
class MockOpenClawRuntime {
|
|
280
|
+
namespace;
|
|
281
|
+
constructor(namespace) {
|
|
282
|
+
this.namespace = namespace;
|
|
283
|
+
}
|
|
284
|
+
ensureDefaultConversation(externalUserKey) {
|
|
285
|
+
return {
|
|
286
|
+
conversationId: `openclaw-plugin:${externalUserKey}`,
|
|
287
|
+
title: "OpenClaw"
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
run(action) {
|
|
291
|
+
const now = Date.now();
|
|
292
|
+
if (action.kind === "send-message") {
|
|
293
|
+
if (action.text.toLowerCase().includes("approval")) {
|
|
294
|
+
const requestId = `approval:${randomUUID()}`;
|
|
295
|
+
return [
|
|
296
|
+
{
|
|
297
|
+
type: "state",
|
|
298
|
+
eventId: randomUUID(),
|
|
299
|
+
occurredAt: now,
|
|
300
|
+
namespace: this.namespace,
|
|
301
|
+
conversationId: action.conversationId,
|
|
302
|
+
connected: true,
|
|
303
|
+
thinking: true,
|
|
304
|
+
lastError: null
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
type: "approval-request",
|
|
308
|
+
eventId: randomUUID(),
|
|
309
|
+
occurredAt: now + 1,
|
|
310
|
+
namespace: this.namespace,
|
|
311
|
+
conversationId: action.conversationId,
|
|
312
|
+
requestId,
|
|
313
|
+
title: "Approve OpenClaw action",
|
|
314
|
+
description: action.text,
|
|
315
|
+
createdAt: now + 1
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
type: "state",
|
|
319
|
+
eventId: randomUUID(),
|
|
320
|
+
occurredAt: now + 2,
|
|
321
|
+
namespace: this.namespace,
|
|
322
|
+
conversationId: action.conversationId,
|
|
323
|
+
connected: true,
|
|
324
|
+
thinking: false,
|
|
325
|
+
lastError: null
|
|
326
|
+
}
|
|
327
|
+
];
|
|
328
|
+
}
|
|
329
|
+
const externalMessageId = `assistant:${randomUUID()}`;
|
|
330
|
+
return [
|
|
331
|
+
{
|
|
332
|
+
type: "state",
|
|
333
|
+
eventId: randomUUID(),
|
|
334
|
+
occurredAt: now,
|
|
335
|
+
namespace: this.namespace,
|
|
336
|
+
conversationId: action.conversationId,
|
|
337
|
+
connected: true,
|
|
338
|
+
thinking: true,
|
|
339
|
+
lastError: null
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
type: "message",
|
|
343
|
+
eventId: randomUUID(),
|
|
344
|
+
occurredAt: now + 1,
|
|
345
|
+
namespace: this.namespace,
|
|
346
|
+
conversationId: action.conversationId,
|
|
347
|
+
externalMessageId,
|
|
348
|
+
role: "assistant",
|
|
349
|
+
content: { mode: "replace", text: "OpenClaw plugin echo: " },
|
|
350
|
+
createdAt: now + 1,
|
|
351
|
+
status: "streaming"
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
type: "message",
|
|
355
|
+
eventId: randomUUID(),
|
|
356
|
+
occurredAt: now + 2,
|
|
357
|
+
namespace: this.namespace,
|
|
358
|
+
conversationId: action.conversationId,
|
|
359
|
+
externalMessageId,
|
|
360
|
+
role: "assistant",
|
|
361
|
+
content: { mode: "append", delta: action.text.trim() || "(empty message)" },
|
|
362
|
+
createdAt: now + 2,
|
|
363
|
+
status: "completed"
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
type: "state",
|
|
367
|
+
eventId: randomUUID(),
|
|
368
|
+
occurredAt: now + 3,
|
|
369
|
+
namespace: this.namespace,
|
|
370
|
+
conversationId: action.conversationId,
|
|
371
|
+
connected: true,
|
|
372
|
+
thinking: false,
|
|
373
|
+
lastError: null
|
|
374
|
+
}
|
|
375
|
+
];
|
|
376
|
+
}
|
|
377
|
+
if (action.kind === "approve") {
|
|
378
|
+
return [{
|
|
379
|
+
type: "approval-resolved",
|
|
380
|
+
eventId: randomUUID(),
|
|
381
|
+
occurredAt: now,
|
|
382
|
+
namespace: this.namespace,
|
|
383
|
+
conversationId: action.conversationId,
|
|
384
|
+
requestId: action.requestId,
|
|
385
|
+
status: "approved"
|
|
386
|
+
}];
|
|
387
|
+
}
|
|
388
|
+
return [{
|
|
389
|
+
type: "approval-resolved",
|
|
390
|
+
eventId: randomUUID(),
|
|
391
|
+
occurredAt: now,
|
|
392
|
+
namespace: this.namespace,
|
|
393
|
+
conversationId: action.conversationId,
|
|
394
|
+
requestId: action.requestId,
|
|
395
|
+
status: "denied"
|
|
396
|
+
}];
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/routes.ts
|
|
401
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
402
|
+
|
|
403
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/compose.js
|
|
404
|
+
var compose = (middleware, onError, onNotFound) => {
|
|
405
|
+
return (context, next) => {
|
|
406
|
+
let index = -1;
|
|
407
|
+
return dispatch(0);
|
|
408
|
+
async function dispatch(i) {
|
|
409
|
+
if (i <= index) {
|
|
410
|
+
throw new Error("next() called multiple times");
|
|
411
|
+
}
|
|
412
|
+
index = i;
|
|
413
|
+
let res;
|
|
414
|
+
let isError = false;
|
|
415
|
+
let handler;
|
|
416
|
+
if (middleware[i]) {
|
|
417
|
+
handler = middleware[i][0][0];
|
|
418
|
+
context.req.routeIndex = i;
|
|
419
|
+
} else {
|
|
420
|
+
handler = i === middleware.length && next || undefined;
|
|
421
|
+
}
|
|
422
|
+
if (handler) {
|
|
423
|
+
try {
|
|
424
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
425
|
+
} catch (err) {
|
|
426
|
+
if (err instanceof Error && onError) {
|
|
427
|
+
context.error = err;
|
|
428
|
+
res = await onError(err, context);
|
|
429
|
+
isError = true;
|
|
430
|
+
} else {
|
|
431
|
+
throw err;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
} else {
|
|
435
|
+
if (context.finalized === false && onNotFound) {
|
|
436
|
+
res = await onNotFound(context);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (res && (context.finalized === false || isError)) {
|
|
440
|
+
context.res = res;
|
|
441
|
+
}
|
|
442
|
+
return context;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/request/constants.js
|
|
448
|
+
var GET_MATCH_RESULT = /* @__PURE__ */ Symbol();
|
|
449
|
+
|
|
450
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/utils/body.js
|
|
451
|
+
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
452
|
+
const { all = false, dot = false } = options;
|
|
453
|
+
const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
|
|
454
|
+
const contentType = headers.get("Content-Type");
|
|
455
|
+
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
|
|
456
|
+
return parseFormData(request, { all, dot });
|
|
457
|
+
}
|
|
458
|
+
return {};
|
|
459
|
+
};
|
|
460
|
+
async function parseFormData(request, options) {
|
|
461
|
+
const formData = await request.formData();
|
|
462
|
+
if (formData) {
|
|
463
|
+
return convertFormDataToBodyData(formData, options);
|
|
464
|
+
}
|
|
465
|
+
return {};
|
|
466
|
+
}
|
|
467
|
+
function convertFormDataToBodyData(formData, options) {
|
|
468
|
+
const form = /* @__PURE__ */ Object.create(null);
|
|
469
|
+
formData.forEach((value, key) => {
|
|
470
|
+
const shouldParseAllValues = options.all || key.endsWith("[]");
|
|
471
|
+
if (!shouldParseAllValues) {
|
|
472
|
+
form[key] = value;
|
|
473
|
+
} else {
|
|
474
|
+
handleParsingAllValues(form, key, value);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
if (options.dot) {
|
|
478
|
+
Object.entries(form).forEach(([key, value]) => {
|
|
479
|
+
const shouldParseDotValues = key.includes(".");
|
|
480
|
+
if (shouldParseDotValues) {
|
|
481
|
+
handleParsingNestedValues(form, key, value);
|
|
482
|
+
delete form[key];
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
return form;
|
|
487
|
+
}
|
|
488
|
+
var handleParsingAllValues = (form, key, value) => {
|
|
489
|
+
if (form[key] !== undefined) {
|
|
490
|
+
if (Array.isArray(form[key])) {
|
|
491
|
+
form[key].push(value);
|
|
492
|
+
} else {
|
|
493
|
+
form[key] = [form[key], value];
|
|
494
|
+
}
|
|
495
|
+
} else {
|
|
496
|
+
if (!key.endsWith("[]")) {
|
|
497
|
+
form[key] = value;
|
|
498
|
+
} else {
|
|
499
|
+
form[key] = [value];
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
var handleParsingNestedValues = (form, key, value) => {
|
|
504
|
+
let nestedForm = form;
|
|
505
|
+
const keys = key.split(".");
|
|
506
|
+
keys.forEach((key2, index) => {
|
|
507
|
+
if (index === keys.length - 1) {
|
|
508
|
+
nestedForm[key2] = value;
|
|
509
|
+
} else {
|
|
510
|
+
if (!nestedForm[key2] || typeof nestedForm[key2] !== "object" || Array.isArray(nestedForm[key2]) || nestedForm[key2] instanceof File) {
|
|
511
|
+
nestedForm[key2] = /* @__PURE__ */ Object.create(null);
|
|
512
|
+
}
|
|
513
|
+
nestedForm = nestedForm[key2];
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/utils/url.js
|
|
519
|
+
var splitPath = (path) => {
|
|
520
|
+
const paths = path.split("/");
|
|
521
|
+
if (paths[0] === "") {
|
|
522
|
+
paths.shift();
|
|
523
|
+
}
|
|
524
|
+
return paths;
|
|
525
|
+
};
|
|
526
|
+
var splitRoutingPath = (routePath) => {
|
|
527
|
+
const { groups, path } = extractGroupsFromPath(routePath);
|
|
528
|
+
const paths = splitPath(path);
|
|
529
|
+
return replaceGroupMarks(paths, groups);
|
|
530
|
+
};
|
|
531
|
+
var extractGroupsFromPath = (path) => {
|
|
532
|
+
const groups = [];
|
|
533
|
+
path = path.replace(/\{[^}]+\}/g, (match, index) => {
|
|
534
|
+
const mark = `@${index}`;
|
|
535
|
+
groups.push([mark, match]);
|
|
536
|
+
return mark;
|
|
537
|
+
});
|
|
538
|
+
return { groups, path };
|
|
539
|
+
};
|
|
540
|
+
var replaceGroupMarks = (paths, groups) => {
|
|
541
|
+
for (let i = groups.length - 1;i >= 0; i--) {
|
|
542
|
+
const [mark] = groups[i];
|
|
543
|
+
for (let j = paths.length - 1;j >= 0; j--) {
|
|
544
|
+
if (paths[j].includes(mark)) {
|
|
545
|
+
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return paths;
|
|
551
|
+
};
|
|
552
|
+
var patternCache = {};
|
|
553
|
+
var getPattern = (label, next) => {
|
|
554
|
+
if (label === "*") {
|
|
555
|
+
return "*";
|
|
556
|
+
}
|
|
557
|
+
const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
558
|
+
if (match) {
|
|
559
|
+
const cacheKey = `${label}#${next}`;
|
|
560
|
+
if (!patternCache[cacheKey]) {
|
|
561
|
+
if (match[2]) {
|
|
562
|
+
patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [cacheKey, match[1], new RegExp(`^${match[2]}(?=/${next})`)] : [label, match[1], new RegExp(`^${match[2]}$`)];
|
|
563
|
+
} else {
|
|
564
|
+
patternCache[cacheKey] = [label, match[1], true];
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return patternCache[cacheKey];
|
|
568
|
+
}
|
|
569
|
+
return null;
|
|
570
|
+
};
|
|
571
|
+
var tryDecode = (str, decoder) => {
|
|
572
|
+
try {
|
|
573
|
+
return decoder(str);
|
|
574
|
+
} catch {
|
|
575
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
576
|
+
try {
|
|
577
|
+
return decoder(match);
|
|
578
|
+
} catch {
|
|
579
|
+
return match;
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
585
|
+
var getPath = (request) => {
|
|
586
|
+
const url = request.url;
|
|
587
|
+
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
588
|
+
let i = start;
|
|
589
|
+
for (;i < url.length; i++) {
|
|
590
|
+
const charCode = url.charCodeAt(i);
|
|
591
|
+
if (charCode === 37) {
|
|
592
|
+
const queryIndex = url.indexOf("?", i);
|
|
593
|
+
const path = url.slice(start, queryIndex === -1 ? undefined : queryIndex);
|
|
594
|
+
return tryDecodeURI(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
|
|
595
|
+
} else if (charCode === 63) {
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return url.slice(start, i);
|
|
600
|
+
};
|
|
601
|
+
var getPathNoStrict = (request) => {
|
|
602
|
+
const result = getPath(request);
|
|
603
|
+
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
604
|
+
};
|
|
605
|
+
var mergePath = (base, sub, ...rest) => {
|
|
606
|
+
if (rest.length) {
|
|
607
|
+
sub = mergePath(sub, ...rest);
|
|
608
|
+
}
|
|
609
|
+
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
610
|
+
};
|
|
611
|
+
var checkOptionalParameter = (path) => {
|
|
612
|
+
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
const segments = path.split("/");
|
|
616
|
+
const results = [];
|
|
617
|
+
let basePath = "";
|
|
618
|
+
segments.forEach((segment) => {
|
|
619
|
+
if (segment !== "" && !/\:/.test(segment)) {
|
|
620
|
+
basePath += "/" + segment;
|
|
621
|
+
} else if (/\:/.test(segment)) {
|
|
622
|
+
if (/\?/.test(segment)) {
|
|
623
|
+
if (results.length === 0 && basePath === "") {
|
|
624
|
+
results.push("/");
|
|
625
|
+
} else {
|
|
626
|
+
results.push(basePath);
|
|
627
|
+
}
|
|
628
|
+
const optionalSegment = segment.replace("?", "");
|
|
629
|
+
basePath += "/" + optionalSegment;
|
|
630
|
+
results.push(basePath);
|
|
631
|
+
} else {
|
|
632
|
+
basePath += "/" + segment;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
637
|
+
};
|
|
638
|
+
var _decodeURI = (value) => {
|
|
639
|
+
if (!/[%+]/.test(value)) {
|
|
640
|
+
return value;
|
|
641
|
+
}
|
|
642
|
+
if (value.indexOf("+") !== -1) {
|
|
643
|
+
value = value.replace(/\+/g, " ");
|
|
644
|
+
}
|
|
645
|
+
return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
|
|
646
|
+
};
|
|
647
|
+
var _getQueryParam = (url, key, multiple) => {
|
|
648
|
+
let encoded;
|
|
649
|
+
if (!multiple && key && !/[%+]/.test(key)) {
|
|
650
|
+
let keyIndex2 = url.indexOf("?", 8);
|
|
651
|
+
if (keyIndex2 === -1) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
if (!url.startsWith(key, keyIndex2 + 1)) {
|
|
655
|
+
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
656
|
+
}
|
|
657
|
+
while (keyIndex2 !== -1) {
|
|
658
|
+
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
659
|
+
if (trailingKeyCode === 61) {
|
|
660
|
+
const valueIndex = keyIndex2 + key.length + 2;
|
|
661
|
+
const endIndex = url.indexOf("&", valueIndex);
|
|
662
|
+
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex));
|
|
663
|
+
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
|
|
664
|
+
return "";
|
|
665
|
+
}
|
|
666
|
+
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
667
|
+
}
|
|
668
|
+
encoded = /[%+]/.test(url);
|
|
669
|
+
if (!encoded) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const results = {};
|
|
674
|
+
encoded ??= /[%+]/.test(url);
|
|
675
|
+
let keyIndex = url.indexOf("?", 8);
|
|
676
|
+
while (keyIndex !== -1) {
|
|
677
|
+
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
678
|
+
let valueIndex = url.indexOf("=", keyIndex);
|
|
679
|
+
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
|
|
680
|
+
valueIndex = -1;
|
|
681
|
+
}
|
|
682
|
+
let name = url.slice(keyIndex + 1, valueIndex === -1 ? nextKeyIndex === -1 ? undefined : nextKeyIndex : valueIndex);
|
|
683
|
+
if (encoded) {
|
|
684
|
+
name = _decodeURI(name);
|
|
685
|
+
}
|
|
686
|
+
keyIndex = nextKeyIndex;
|
|
687
|
+
if (name === "") {
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
let value;
|
|
691
|
+
if (valueIndex === -1) {
|
|
692
|
+
value = "";
|
|
693
|
+
} else {
|
|
694
|
+
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex);
|
|
695
|
+
if (encoded) {
|
|
696
|
+
value = _decodeURI(value);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
if (multiple) {
|
|
700
|
+
if (!(results[name] && Array.isArray(results[name]))) {
|
|
701
|
+
results[name] = [];
|
|
702
|
+
}
|
|
703
|
+
results[name].push(value);
|
|
704
|
+
} else {
|
|
705
|
+
results[name] ??= value;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return key ? results[key] : results;
|
|
709
|
+
};
|
|
710
|
+
var getQueryParam = _getQueryParam;
|
|
711
|
+
var getQueryParams = (url, key) => {
|
|
712
|
+
return _getQueryParam(url, key, true);
|
|
713
|
+
};
|
|
714
|
+
var decodeURIComponent_ = decodeURIComponent;
|
|
715
|
+
|
|
716
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/request.js
|
|
717
|
+
var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
|
718
|
+
var HonoRequest = class {
|
|
719
|
+
raw;
|
|
720
|
+
#validatedData;
|
|
721
|
+
#matchResult;
|
|
722
|
+
routeIndex = 0;
|
|
723
|
+
path;
|
|
724
|
+
bodyCache = {};
|
|
725
|
+
constructor(request, path = "/", matchResult = [[]]) {
|
|
726
|
+
this.raw = request;
|
|
727
|
+
this.path = path;
|
|
728
|
+
this.#matchResult = matchResult;
|
|
729
|
+
this.#validatedData = {};
|
|
730
|
+
}
|
|
731
|
+
param(key) {
|
|
732
|
+
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
733
|
+
}
|
|
734
|
+
#getDecodedParam(key) {
|
|
735
|
+
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
736
|
+
const param = this.#getParamValue(paramKey);
|
|
737
|
+
return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
|
|
738
|
+
}
|
|
739
|
+
#getAllDecodedParams() {
|
|
740
|
+
const decoded = {};
|
|
741
|
+
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
742
|
+
for (const key of keys) {
|
|
743
|
+
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
744
|
+
if (value !== undefined) {
|
|
745
|
+
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return decoded;
|
|
749
|
+
}
|
|
750
|
+
#getParamValue(paramKey) {
|
|
751
|
+
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
752
|
+
}
|
|
753
|
+
query(key) {
|
|
754
|
+
return getQueryParam(this.url, key);
|
|
755
|
+
}
|
|
756
|
+
queries(key) {
|
|
757
|
+
return getQueryParams(this.url, key);
|
|
758
|
+
}
|
|
759
|
+
header(name) {
|
|
760
|
+
if (name) {
|
|
761
|
+
return this.raw.headers.get(name) ?? undefined;
|
|
762
|
+
}
|
|
763
|
+
const headerData = {};
|
|
764
|
+
this.raw.headers.forEach((value, key) => {
|
|
765
|
+
headerData[key] = value;
|
|
766
|
+
});
|
|
767
|
+
return headerData;
|
|
768
|
+
}
|
|
769
|
+
async parseBody(options) {
|
|
770
|
+
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
771
|
+
}
|
|
772
|
+
#cachedBody = (key) => {
|
|
773
|
+
const { bodyCache, raw } = this;
|
|
774
|
+
const cachedBody = bodyCache[key];
|
|
775
|
+
if (cachedBody) {
|
|
776
|
+
return cachedBody;
|
|
777
|
+
}
|
|
778
|
+
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
779
|
+
if (anyCachedKey) {
|
|
780
|
+
return bodyCache[anyCachedKey].then((body) => {
|
|
781
|
+
if (anyCachedKey === "json") {
|
|
782
|
+
body = JSON.stringify(body);
|
|
783
|
+
}
|
|
784
|
+
return new Response(body)[key]();
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
return bodyCache[key] = raw[key]();
|
|
788
|
+
};
|
|
789
|
+
json() {
|
|
790
|
+
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
791
|
+
}
|
|
792
|
+
text() {
|
|
793
|
+
return this.#cachedBody("text");
|
|
794
|
+
}
|
|
795
|
+
arrayBuffer() {
|
|
796
|
+
return this.#cachedBody("arrayBuffer");
|
|
797
|
+
}
|
|
798
|
+
blob() {
|
|
799
|
+
return this.#cachedBody("blob");
|
|
800
|
+
}
|
|
801
|
+
formData() {
|
|
802
|
+
return this.#cachedBody("formData");
|
|
803
|
+
}
|
|
804
|
+
addValidatedData(target, data) {
|
|
805
|
+
this.#validatedData[target] = data;
|
|
806
|
+
}
|
|
807
|
+
valid(target) {
|
|
808
|
+
return this.#validatedData[target];
|
|
809
|
+
}
|
|
810
|
+
get url() {
|
|
811
|
+
return this.raw.url;
|
|
812
|
+
}
|
|
813
|
+
get method() {
|
|
814
|
+
return this.raw.method;
|
|
815
|
+
}
|
|
816
|
+
get [GET_MATCH_RESULT]() {
|
|
817
|
+
return this.#matchResult;
|
|
818
|
+
}
|
|
819
|
+
get matchedRoutes() {
|
|
820
|
+
return this.#matchResult[0].map(([[, route]]) => route);
|
|
821
|
+
}
|
|
822
|
+
get routePath() {
|
|
823
|
+
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/utils/html.js
|
|
828
|
+
var HtmlEscapedCallbackPhase = {
|
|
829
|
+
Stringify: 1,
|
|
830
|
+
BeforeStream: 2,
|
|
831
|
+
Stream: 3
|
|
832
|
+
};
|
|
833
|
+
var raw = (value, callbacks) => {
|
|
834
|
+
const escapedString = new String(value);
|
|
835
|
+
escapedString.isEscaped = true;
|
|
836
|
+
escapedString.callbacks = callbacks;
|
|
837
|
+
return escapedString;
|
|
838
|
+
};
|
|
839
|
+
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
840
|
+
if (typeof str === "object" && !(str instanceof String)) {
|
|
841
|
+
if (!(str instanceof Promise)) {
|
|
842
|
+
str = str.toString();
|
|
843
|
+
}
|
|
844
|
+
if (str instanceof Promise) {
|
|
845
|
+
str = await str;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
const callbacks = str.callbacks;
|
|
849
|
+
if (!callbacks?.length) {
|
|
850
|
+
return Promise.resolve(str);
|
|
851
|
+
}
|
|
852
|
+
if (buffer) {
|
|
853
|
+
buffer[0] += str;
|
|
854
|
+
} else {
|
|
855
|
+
buffer = [str];
|
|
856
|
+
}
|
|
857
|
+
const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then((res) => Promise.all(res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))).then(() => buffer[0]));
|
|
858
|
+
if (preserveCallbacks) {
|
|
859
|
+
return raw(await resStr, callbacks);
|
|
860
|
+
} else {
|
|
861
|
+
return resStr;
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/context.js
|
|
866
|
+
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
867
|
+
var setDefaultContentType = (contentType, headers) => {
|
|
868
|
+
return {
|
|
869
|
+
"Content-Type": contentType,
|
|
870
|
+
...headers
|
|
871
|
+
};
|
|
872
|
+
};
|
|
873
|
+
var Context = class {
|
|
874
|
+
#rawRequest;
|
|
875
|
+
#req;
|
|
876
|
+
env = {};
|
|
877
|
+
#var;
|
|
878
|
+
finalized = false;
|
|
879
|
+
error;
|
|
880
|
+
#status;
|
|
881
|
+
#executionCtx;
|
|
882
|
+
#res;
|
|
883
|
+
#layout;
|
|
884
|
+
#renderer;
|
|
885
|
+
#notFoundHandler;
|
|
886
|
+
#preparedHeaders;
|
|
887
|
+
#matchResult;
|
|
888
|
+
#path;
|
|
889
|
+
constructor(req, options) {
|
|
890
|
+
this.#rawRequest = req;
|
|
891
|
+
if (options) {
|
|
892
|
+
this.#executionCtx = options.executionCtx;
|
|
893
|
+
this.env = options.env;
|
|
894
|
+
this.#notFoundHandler = options.notFoundHandler;
|
|
895
|
+
this.#path = options.path;
|
|
896
|
+
this.#matchResult = options.matchResult;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
get req() {
|
|
900
|
+
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
901
|
+
return this.#req;
|
|
902
|
+
}
|
|
903
|
+
get event() {
|
|
904
|
+
if (this.#executionCtx && "respondWith" in this.#executionCtx) {
|
|
905
|
+
return this.#executionCtx;
|
|
906
|
+
} else {
|
|
907
|
+
throw Error("This context has no FetchEvent");
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
get executionCtx() {
|
|
911
|
+
if (this.#executionCtx) {
|
|
912
|
+
return this.#executionCtx;
|
|
913
|
+
} else {
|
|
914
|
+
throw Error("This context has no ExecutionContext");
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
get res() {
|
|
918
|
+
return this.#res ||= new Response(null, {
|
|
919
|
+
headers: this.#preparedHeaders ??= new Headers
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
set res(_res) {
|
|
923
|
+
if (this.#res && _res) {
|
|
924
|
+
_res = new Response(_res.body, _res);
|
|
925
|
+
for (const [k, v] of this.#res.headers.entries()) {
|
|
926
|
+
if (k === "content-type") {
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
if (k === "set-cookie") {
|
|
930
|
+
const cookies = this.#res.headers.getSetCookie();
|
|
931
|
+
_res.headers.delete("set-cookie");
|
|
932
|
+
for (const cookie of cookies) {
|
|
933
|
+
_res.headers.append("set-cookie", cookie);
|
|
934
|
+
}
|
|
935
|
+
} else {
|
|
936
|
+
_res.headers.set(k, v);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
this.#res = _res;
|
|
941
|
+
this.finalized = true;
|
|
942
|
+
}
|
|
943
|
+
render = (...args) => {
|
|
944
|
+
this.#renderer ??= (content) => this.html(content);
|
|
945
|
+
return this.#renderer(...args);
|
|
946
|
+
};
|
|
947
|
+
setLayout = (layout) => this.#layout = layout;
|
|
948
|
+
getLayout = () => this.#layout;
|
|
949
|
+
setRenderer = (renderer) => {
|
|
950
|
+
this.#renderer = renderer;
|
|
951
|
+
};
|
|
952
|
+
header = (name, value, options) => {
|
|
953
|
+
if (this.finalized) {
|
|
954
|
+
this.#res = new Response(this.#res.body, this.#res);
|
|
955
|
+
}
|
|
956
|
+
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers;
|
|
957
|
+
if (value === undefined) {
|
|
958
|
+
headers.delete(name);
|
|
959
|
+
} else if (options?.append) {
|
|
960
|
+
headers.append(name, value);
|
|
961
|
+
} else {
|
|
962
|
+
headers.set(name, value);
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
status = (status) => {
|
|
966
|
+
this.#status = status;
|
|
967
|
+
};
|
|
968
|
+
set = (key, value) => {
|
|
969
|
+
this.#var ??= /* @__PURE__ */ new Map;
|
|
970
|
+
this.#var.set(key, value);
|
|
971
|
+
};
|
|
972
|
+
get = (key) => {
|
|
973
|
+
return this.#var ? this.#var.get(key) : undefined;
|
|
974
|
+
};
|
|
975
|
+
get var() {
|
|
976
|
+
if (!this.#var) {
|
|
977
|
+
return {};
|
|
978
|
+
}
|
|
979
|
+
return Object.fromEntries(this.#var);
|
|
980
|
+
}
|
|
981
|
+
#newResponse(data, arg, headers) {
|
|
982
|
+
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers;
|
|
983
|
+
if (typeof arg === "object" && "headers" in arg) {
|
|
984
|
+
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
985
|
+
for (const [key, value] of argHeaders) {
|
|
986
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
987
|
+
responseHeaders.append(key, value);
|
|
988
|
+
} else {
|
|
989
|
+
responseHeaders.set(key, value);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
if (headers) {
|
|
994
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
995
|
+
if (typeof v === "string") {
|
|
996
|
+
responseHeaders.set(k, v);
|
|
997
|
+
} else {
|
|
998
|
+
responseHeaders.delete(k);
|
|
999
|
+
for (const v2 of v) {
|
|
1000
|
+
responseHeaders.append(k, v2);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
const status = typeof arg === "number" ? arg : arg?.status ?? this.#status;
|
|
1006
|
+
return new Response(data, { status, headers: responseHeaders });
|
|
1007
|
+
}
|
|
1008
|
+
newResponse = (...args) => this.#newResponse(...args);
|
|
1009
|
+
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
1010
|
+
text = (text, arg, headers) => {
|
|
1011
|
+
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(text, arg, setDefaultContentType(TEXT_PLAIN, headers));
|
|
1012
|
+
};
|
|
1013
|
+
json = (object, arg, headers) => {
|
|
1014
|
+
return this.#newResponse(JSON.stringify(object), arg, setDefaultContentType("application/json", headers));
|
|
1015
|
+
};
|
|
1016
|
+
html = (html, arg, headers) => {
|
|
1017
|
+
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
1018
|
+
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
1019
|
+
};
|
|
1020
|
+
redirect = (location, status) => {
|
|
1021
|
+
const locationString = String(location);
|
|
1022
|
+
this.header("Location", !/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString));
|
|
1023
|
+
return this.newResponse(null, status ?? 302);
|
|
1024
|
+
};
|
|
1025
|
+
notFound = () => {
|
|
1026
|
+
this.#notFoundHandler ??= () => new Response;
|
|
1027
|
+
return this.#notFoundHandler(this);
|
|
1028
|
+
};
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router.js
|
|
1032
|
+
var METHOD_NAME_ALL = "ALL";
|
|
1033
|
+
var METHOD_NAME_ALL_LOWERCASE = "all";
|
|
1034
|
+
var METHODS = ["get", "post", "put", "delete", "options", "patch"];
|
|
1035
|
+
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
1036
|
+
var UnsupportedPathError = class extends Error {
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/utils/constants.js
|
|
1040
|
+
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
1041
|
+
|
|
1042
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/hono-base.js
|
|
1043
|
+
var notFoundHandler = (c) => {
|
|
1044
|
+
return c.text("404 Not Found", 404);
|
|
1045
|
+
};
|
|
1046
|
+
var errorHandler = (err, c) => {
|
|
1047
|
+
if ("getResponse" in err) {
|
|
1048
|
+
const res = err.getResponse();
|
|
1049
|
+
return c.newResponse(res.body, res);
|
|
1050
|
+
}
|
|
1051
|
+
console.error(err);
|
|
1052
|
+
return c.text("Internal Server Error", 500);
|
|
1053
|
+
};
|
|
1054
|
+
var Hono = class _Hono {
|
|
1055
|
+
get;
|
|
1056
|
+
post;
|
|
1057
|
+
put;
|
|
1058
|
+
delete;
|
|
1059
|
+
options;
|
|
1060
|
+
patch;
|
|
1061
|
+
all;
|
|
1062
|
+
on;
|
|
1063
|
+
use;
|
|
1064
|
+
router;
|
|
1065
|
+
getPath;
|
|
1066
|
+
_basePath = "/";
|
|
1067
|
+
#path = "/";
|
|
1068
|
+
routes = [];
|
|
1069
|
+
constructor(options = {}) {
|
|
1070
|
+
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
1071
|
+
allMethods.forEach((method) => {
|
|
1072
|
+
this[method] = (args1, ...args) => {
|
|
1073
|
+
if (typeof args1 === "string") {
|
|
1074
|
+
this.#path = args1;
|
|
1075
|
+
} else {
|
|
1076
|
+
this.#addRoute(method, this.#path, args1);
|
|
1077
|
+
}
|
|
1078
|
+
args.forEach((handler) => {
|
|
1079
|
+
this.#addRoute(method, this.#path, handler);
|
|
1080
|
+
});
|
|
1081
|
+
return this;
|
|
1082
|
+
};
|
|
1083
|
+
});
|
|
1084
|
+
this.on = (method, path, ...handlers) => {
|
|
1085
|
+
for (const p of [path].flat()) {
|
|
1086
|
+
this.#path = p;
|
|
1087
|
+
for (const m of [method].flat()) {
|
|
1088
|
+
handlers.map((handler) => {
|
|
1089
|
+
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
return this;
|
|
1094
|
+
};
|
|
1095
|
+
this.use = (arg1, ...handlers) => {
|
|
1096
|
+
if (typeof arg1 === "string") {
|
|
1097
|
+
this.#path = arg1;
|
|
1098
|
+
} else {
|
|
1099
|
+
this.#path = "*";
|
|
1100
|
+
handlers.unshift(arg1);
|
|
1101
|
+
}
|
|
1102
|
+
handlers.forEach((handler) => {
|
|
1103
|
+
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
1104
|
+
});
|
|
1105
|
+
return this;
|
|
1106
|
+
};
|
|
1107
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
1108
|
+
Object.assign(this, optionsWithoutStrict);
|
|
1109
|
+
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
1110
|
+
}
|
|
1111
|
+
#clone() {
|
|
1112
|
+
const clone = new _Hono({
|
|
1113
|
+
router: this.router,
|
|
1114
|
+
getPath: this.getPath
|
|
1115
|
+
});
|
|
1116
|
+
clone.errorHandler = this.errorHandler;
|
|
1117
|
+
clone.#notFoundHandler = this.#notFoundHandler;
|
|
1118
|
+
clone.routes = this.routes;
|
|
1119
|
+
return clone;
|
|
1120
|
+
}
|
|
1121
|
+
#notFoundHandler = notFoundHandler;
|
|
1122
|
+
errorHandler = errorHandler;
|
|
1123
|
+
route(path, app) {
|
|
1124
|
+
const subApp = this.basePath(path);
|
|
1125
|
+
app.routes.map((r) => {
|
|
1126
|
+
let handler;
|
|
1127
|
+
if (app.errorHandler === errorHandler) {
|
|
1128
|
+
handler = r.handler;
|
|
1129
|
+
} else {
|
|
1130
|
+
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
1131
|
+
handler[COMPOSED_HANDLER] = r.handler;
|
|
1132
|
+
}
|
|
1133
|
+
subApp.#addRoute(r.method, r.path, handler);
|
|
1134
|
+
});
|
|
1135
|
+
return this;
|
|
1136
|
+
}
|
|
1137
|
+
basePath(path) {
|
|
1138
|
+
const subApp = this.#clone();
|
|
1139
|
+
subApp._basePath = mergePath(this._basePath, path);
|
|
1140
|
+
return subApp;
|
|
1141
|
+
}
|
|
1142
|
+
onError = (handler) => {
|
|
1143
|
+
this.errorHandler = handler;
|
|
1144
|
+
return this;
|
|
1145
|
+
};
|
|
1146
|
+
notFound = (handler) => {
|
|
1147
|
+
this.#notFoundHandler = handler;
|
|
1148
|
+
return this;
|
|
1149
|
+
};
|
|
1150
|
+
mount(path, applicationHandler, options) {
|
|
1151
|
+
let replaceRequest;
|
|
1152
|
+
let optionHandler;
|
|
1153
|
+
if (options) {
|
|
1154
|
+
if (typeof options === "function") {
|
|
1155
|
+
optionHandler = options;
|
|
1156
|
+
} else {
|
|
1157
|
+
optionHandler = options.optionHandler;
|
|
1158
|
+
if (options.replaceRequest === false) {
|
|
1159
|
+
replaceRequest = (request) => request;
|
|
1160
|
+
} else {
|
|
1161
|
+
replaceRequest = options.replaceRequest;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
const getOptions = optionHandler ? (c) => {
|
|
1166
|
+
const options2 = optionHandler(c);
|
|
1167
|
+
return Array.isArray(options2) ? options2 : [options2];
|
|
1168
|
+
} : (c) => {
|
|
1169
|
+
let executionContext = undefined;
|
|
1170
|
+
try {
|
|
1171
|
+
executionContext = c.executionCtx;
|
|
1172
|
+
} catch {}
|
|
1173
|
+
return [c.env, executionContext];
|
|
1174
|
+
};
|
|
1175
|
+
replaceRequest ||= (() => {
|
|
1176
|
+
const mergedPath = mergePath(this._basePath, path);
|
|
1177
|
+
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
1178
|
+
return (request) => {
|
|
1179
|
+
const url = new URL(request.url);
|
|
1180
|
+
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
1181
|
+
return new Request(url, request);
|
|
1182
|
+
};
|
|
1183
|
+
})();
|
|
1184
|
+
const handler = async (c, next) => {
|
|
1185
|
+
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
1186
|
+
if (res) {
|
|
1187
|
+
return res;
|
|
1188
|
+
}
|
|
1189
|
+
await next();
|
|
1190
|
+
};
|
|
1191
|
+
this.#addRoute(METHOD_NAME_ALL, mergePath(path, "*"), handler);
|
|
1192
|
+
return this;
|
|
1193
|
+
}
|
|
1194
|
+
#addRoute(method, path, handler) {
|
|
1195
|
+
method = method.toUpperCase();
|
|
1196
|
+
path = mergePath(this._basePath, path);
|
|
1197
|
+
const r = { basePath: this._basePath, path, method, handler };
|
|
1198
|
+
this.router.add(method, path, [handler, r]);
|
|
1199
|
+
this.routes.push(r);
|
|
1200
|
+
}
|
|
1201
|
+
#handleError(err, c) {
|
|
1202
|
+
if (err instanceof Error) {
|
|
1203
|
+
return this.errorHandler(err, c);
|
|
1204
|
+
}
|
|
1205
|
+
throw err;
|
|
1206
|
+
}
|
|
1207
|
+
#dispatch(request, executionCtx, env, method) {
|
|
1208
|
+
if (method === "HEAD") {
|
|
1209
|
+
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
1210
|
+
}
|
|
1211
|
+
const path = this.getPath(request, { env });
|
|
1212
|
+
const matchResult = this.router.match(method, path);
|
|
1213
|
+
const c = new Context(request, {
|
|
1214
|
+
path,
|
|
1215
|
+
matchResult,
|
|
1216
|
+
env,
|
|
1217
|
+
executionCtx,
|
|
1218
|
+
notFoundHandler: this.#notFoundHandler
|
|
1219
|
+
});
|
|
1220
|
+
if (matchResult[0].length === 1) {
|
|
1221
|
+
let res;
|
|
1222
|
+
try {
|
|
1223
|
+
res = matchResult[0][0][0][0](c, async () => {
|
|
1224
|
+
c.res = await this.#notFoundHandler(c);
|
|
1225
|
+
});
|
|
1226
|
+
} catch (err) {
|
|
1227
|
+
return this.#handleError(err, c);
|
|
1228
|
+
}
|
|
1229
|
+
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);
|
|
1230
|
+
}
|
|
1231
|
+
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
1232
|
+
return (async () => {
|
|
1233
|
+
try {
|
|
1234
|
+
const context = await composed(c);
|
|
1235
|
+
if (!context.finalized) {
|
|
1236
|
+
throw new Error("Context is not finalized. Did you forget to return a Response object or `await next()`?");
|
|
1237
|
+
}
|
|
1238
|
+
return context.res;
|
|
1239
|
+
} catch (err) {
|
|
1240
|
+
return this.#handleError(err, c);
|
|
1241
|
+
}
|
|
1242
|
+
})();
|
|
1243
|
+
}
|
|
1244
|
+
fetch = (request, ...rest) => {
|
|
1245
|
+
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
1246
|
+
};
|
|
1247
|
+
request = (input, requestInit, Env, executionCtx) => {
|
|
1248
|
+
if (input instanceof Request) {
|
|
1249
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
1250
|
+
}
|
|
1251
|
+
input = input.toString();
|
|
1252
|
+
return this.fetch(new Request(/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`, requestInit), Env, executionCtx);
|
|
1253
|
+
};
|
|
1254
|
+
fire = () => {
|
|
1255
|
+
addEventListener("fetch", (event) => {
|
|
1256
|
+
event.respondWith(this.#dispatch(event.request, event, undefined, event.request.method));
|
|
1257
|
+
});
|
|
1258
|
+
};
|
|
1259
|
+
};
|
|
1260
|
+
|
|
1261
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/reg-exp-router/matcher.js
|
|
1262
|
+
var emptyParam = [];
|
|
1263
|
+
function match(method, path) {
|
|
1264
|
+
const matchers = this.buildAllMatchers();
|
|
1265
|
+
const match2 = (method2, path2) => {
|
|
1266
|
+
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
1267
|
+
const staticMatch = matcher[2][path2];
|
|
1268
|
+
if (staticMatch) {
|
|
1269
|
+
return staticMatch;
|
|
1270
|
+
}
|
|
1271
|
+
const match3 = path2.match(matcher[0]);
|
|
1272
|
+
if (!match3) {
|
|
1273
|
+
return [[], emptyParam];
|
|
1274
|
+
}
|
|
1275
|
+
const index = match3.indexOf("", 1);
|
|
1276
|
+
return [matcher[1][index], match3];
|
|
1277
|
+
};
|
|
1278
|
+
this.match = match2;
|
|
1279
|
+
return match2(method, path);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/reg-exp-router/node.js
|
|
1283
|
+
var LABEL_REG_EXP_STR = "[^/]+";
|
|
1284
|
+
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
1285
|
+
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
1286
|
+
var PATH_ERROR = /* @__PURE__ */ Symbol();
|
|
1287
|
+
var regExpMetaChars = new Set(".\\+*[^]$()");
|
|
1288
|
+
function compareKey(a, b) {
|
|
1289
|
+
if (a.length === 1) {
|
|
1290
|
+
return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
1291
|
+
}
|
|
1292
|
+
if (b.length === 1) {
|
|
1293
|
+
return 1;
|
|
1294
|
+
}
|
|
1295
|
+
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
|
|
1296
|
+
return 1;
|
|
1297
|
+
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
|
|
1298
|
+
return -1;
|
|
1299
|
+
}
|
|
1300
|
+
if (a === LABEL_REG_EXP_STR) {
|
|
1301
|
+
return 1;
|
|
1302
|
+
} else if (b === LABEL_REG_EXP_STR) {
|
|
1303
|
+
return -1;
|
|
1304
|
+
}
|
|
1305
|
+
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
1306
|
+
}
|
|
1307
|
+
var Node = class _Node {
|
|
1308
|
+
#index;
|
|
1309
|
+
#varIndex;
|
|
1310
|
+
#children = /* @__PURE__ */ Object.create(null);
|
|
1311
|
+
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
1312
|
+
if (tokens.length === 0) {
|
|
1313
|
+
if (this.#index !== undefined) {
|
|
1314
|
+
throw PATH_ERROR;
|
|
1315
|
+
}
|
|
1316
|
+
if (pathErrorCheckOnly) {
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
this.#index = index;
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
const [token, ...restTokens] = tokens;
|
|
1323
|
+
const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
1324
|
+
let node;
|
|
1325
|
+
if (pattern) {
|
|
1326
|
+
const name = pattern[1];
|
|
1327
|
+
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
|
1328
|
+
if (name && pattern[2]) {
|
|
1329
|
+
if (regexpStr === ".*") {
|
|
1330
|
+
throw PATH_ERROR;
|
|
1331
|
+
}
|
|
1332
|
+
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
1333
|
+
if (/\((?!\?:)/.test(regexpStr)) {
|
|
1334
|
+
throw PATH_ERROR;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
node = this.#children[regexpStr];
|
|
1338
|
+
if (!node) {
|
|
1339
|
+
if (Object.keys(this.#children).some((k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
|
|
1340
|
+
throw PATH_ERROR;
|
|
1341
|
+
}
|
|
1342
|
+
if (pathErrorCheckOnly) {
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
node = this.#children[regexpStr] = new _Node;
|
|
1346
|
+
if (name !== "") {
|
|
1347
|
+
node.#varIndex = context.varIndex++;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
if (!pathErrorCheckOnly && name !== "") {
|
|
1351
|
+
paramMap.push([name, node.#varIndex]);
|
|
1352
|
+
}
|
|
1353
|
+
} else {
|
|
1354
|
+
node = this.#children[token];
|
|
1355
|
+
if (!node) {
|
|
1356
|
+
if (Object.keys(this.#children).some((k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR)) {
|
|
1357
|
+
throw PATH_ERROR;
|
|
1358
|
+
}
|
|
1359
|
+
if (pathErrorCheckOnly) {
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
node = this.#children[token] = new _Node;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
1366
|
+
}
|
|
1367
|
+
buildRegExpStr() {
|
|
1368
|
+
const childKeys = Object.keys(this.#children).sort(compareKey);
|
|
1369
|
+
const strList = childKeys.map((k) => {
|
|
1370
|
+
const c = this.#children[k];
|
|
1371
|
+
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
1372
|
+
});
|
|
1373
|
+
if (typeof this.#index === "number") {
|
|
1374
|
+
strList.unshift(`#${this.#index}`);
|
|
1375
|
+
}
|
|
1376
|
+
if (strList.length === 0) {
|
|
1377
|
+
return "";
|
|
1378
|
+
}
|
|
1379
|
+
if (strList.length === 1) {
|
|
1380
|
+
return strList[0];
|
|
1381
|
+
}
|
|
1382
|
+
return "(?:" + strList.join("|") + ")";
|
|
1383
|
+
}
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
1387
|
+
var Trie = class {
|
|
1388
|
+
#context = { varIndex: 0 };
|
|
1389
|
+
#root = new Node;
|
|
1390
|
+
insert(path, index, pathErrorCheckOnly) {
|
|
1391
|
+
const paramAssoc = [];
|
|
1392
|
+
const groups = [];
|
|
1393
|
+
for (let i = 0;; ) {
|
|
1394
|
+
let replaced = false;
|
|
1395
|
+
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
1396
|
+
const mark = `@\\${i}`;
|
|
1397
|
+
groups[i] = [mark, m];
|
|
1398
|
+
i++;
|
|
1399
|
+
replaced = true;
|
|
1400
|
+
return mark;
|
|
1401
|
+
});
|
|
1402
|
+
if (!replaced) {
|
|
1403
|
+
break;
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
1407
|
+
for (let i = groups.length - 1;i >= 0; i--) {
|
|
1408
|
+
const [mark] = groups[i];
|
|
1409
|
+
for (let j = tokens.length - 1;j >= 0; j--) {
|
|
1410
|
+
if (tokens[j].indexOf(mark) !== -1) {
|
|
1411
|
+
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
1412
|
+
break;
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
1417
|
+
return paramAssoc;
|
|
1418
|
+
}
|
|
1419
|
+
buildRegExp() {
|
|
1420
|
+
let regexp = this.#root.buildRegExpStr();
|
|
1421
|
+
if (regexp === "") {
|
|
1422
|
+
return [/^$/, [], []];
|
|
1423
|
+
}
|
|
1424
|
+
let captureIndex = 0;
|
|
1425
|
+
const indexReplacementMap = [];
|
|
1426
|
+
const paramReplacementMap = [];
|
|
1427
|
+
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
1428
|
+
if (handlerIndex !== undefined) {
|
|
1429
|
+
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
1430
|
+
return "$()";
|
|
1431
|
+
}
|
|
1432
|
+
if (paramIndex !== undefined) {
|
|
1433
|
+
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
1434
|
+
return "";
|
|
1435
|
+
}
|
|
1436
|
+
return "";
|
|
1437
|
+
});
|
|
1438
|
+
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
|
|
1439
|
+
}
|
|
1440
|
+
};
|
|
1441
|
+
|
|
1442
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/reg-exp-router/router.js
|
|
1443
|
+
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
1444
|
+
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1445
|
+
function buildWildcardRegExp(path) {
|
|
1446
|
+
return wildcardRegExpCache[path] ??= new RegExp(path === "*" ? "" : `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)")}$`);
|
|
1447
|
+
}
|
|
1448
|
+
function clearWildcardRegExpCache() {
|
|
1449
|
+
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1450
|
+
}
|
|
1451
|
+
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
1452
|
+
const trie = new Trie;
|
|
1453
|
+
const handlerData = [];
|
|
1454
|
+
if (routes.length === 0) {
|
|
1455
|
+
return nullMatcher;
|
|
1456
|
+
}
|
|
1457
|
+
const routesWithStaticPathFlag = routes.map((route) => [!/\*|\/:/.test(route[0]), ...route]).sort(([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length);
|
|
1458
|
+
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
1459
|
+
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length;i < len; i++) {
|
|
1460
|
+
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
1461
|
+
if (pathErrorCheckOnly) {
|
|
1462
|
+
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
1463
|
+
} else {
|
|
1464
|
+
j++;
|
|
1465
|
+
}
|
|
1466
|
+
let paramAssoc;
|
|
1467
|
+
try {
|
|
1468
|
+
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
|
1469
|
+
} catch (e) {
|
|
1470
|
+
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
|
1471
|
+
}
|
|
1472
|
+
if (pathErrorCheckOnly) {
|
|
1473
|
+
continue;
|
|
1474
|
+
}
|
|
1475
|
+
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
1476
|
+
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
1477
|
+
paramCount -= 1;
|
|
1478
|
+
for (;paramCount >= 0; paramCount--) {
|
|
1479
|
+
const [key, value] = paramAssoc[paramCount];
|
|
1480
|
+
paramIndexMap[key] = value;
|
|
1481
|
+
}
|
|
1482
|
+
return [h, paramIndexMap];
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
1486
|
+
for (let i = 0, len = handlerData.length;i < len; i++) {
|
|
1487
|
+
for (let j = 0, len2 = handlerData[i].length;j < len2; j++) {
|
|
1488
|
+
const map = handlerData[i][j]?.[1];
|
|
1489
|
+
if (!map) {
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
const keys = Object.keys(map);
|
|
1493
|
+
for (let k = 0, len3 = keys.length;k < len3; k++) {
|
|
1494
|
+
map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
const handlerMap = [];
|
|
1499
|
+
for (const i in indexReplacementMap) {
|
|
1500
|
+
handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
1501
|
+
}
|
|
1502
|
+
return [regexp, handlerMap, staticMap];
|
|
1503
|
+
}
|
|
1504
|
+
function findMiddleware(middleware, path) {
|
|
1505
|
+
if (!middleware) {
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
1509
|
+
if (buildWildcardRegExp(k).test(path)) {
|
|
1510
|
+
return [...middleware[k]];
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
var RegExpRouter = class {
|
|
1516
|
+
name = "RegExpRouter";
|
|
1517
|
+
#middleware;
|
|
1518
|
+
#routes;
|
|
1519
|
+
constructor() {
|
|
1520
|
+
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1521
|
+
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1522
|
+
}
|
|
1523
|
+
add(method, path, handler) {
|
|
1524
|
+
const middleware = this.#middleware;
|
|
1525
|
+
const routes = this.#routes;
|
|
1526
|
+
if (!middleware || !routes) {
|
|
1527
|
+
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1528
|
+
}
|
|
1529
|
+
if (!middleware[method]) {
|
|
1530
|
+
[middleware, routes].forEach((handlerMap) => {
|
|
1531
|
+
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
1532
|
+
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
1533
|
+
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
1534
|
+
});
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
if (path === "/*") {
|
|
1538
|
+
path = "*";
|
|
1539
|
+
}
|
|
1540
|
+
const paramCount = (path.match(/\/:/g) || []).length;
|
|
1541
|
+
if (/\*$/.test(path)) {
|
|
1542
|
+
const re = buildWildcardRegExp(path);
|
|
1543
|
+
if (method === METHOD_NAME_ALL) {
|
|
1544
|
+
Object.keys(middleware).forEach((m) => {
|
|
1545
|
+
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1546
|
+
});
|
|
1547
|
+
} else {
|
|
1548
|
+
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
1549
|
+
}
|
|
1550
|
+
Object.keys(middleware).forEach((m) => {
|
|
1551
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1552
|
+
Object.keys(middleware[m]).forEach((p) => {
|
|
1553
|
+
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
});
|
|
1557
|
+
Object.keys(routes).forEach((m) => {
|
|
1558
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1559
|
+
Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push([handler, paramCount]));
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
return;
|
|
1563
|
+
}
|
|
1564
|
+
const paths = checkOptionalParameter(path) || [path];
|
|
1565
|
+
for (let i = 0, len = paths.length;i < len; i++) {
|
|
1566
|
+
const path2 = paths[i];
|
|
1567
|
+
Object.keys(routes).forEach((m) => {
|
|
1568
|
+
if (method === METHOD_NAME_ALL || method === m) {
|
|
1569
|
+
routes[m][path2] ||= [
|
|
1570
|
+
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
1571
|
+
];
|
|
1572
|
+
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
1573
|
+
}
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
match = match;
|
|
1578
|
+
buildAllMatchers() {
|
|
1579
|
+
const matchers = /* @__PURE__ */ Object.create(null);
|
|
1580
|
+
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
1581
|
+
matchers[method] ||= this.#buildMatcher(method);
|
|
1582
|
+
});
|
|
1583
|
+
this.#middleware = this.#routes = undefined;
|
|
1584
|
+
clearWildcardRegExpCache();
|
|
1585
|
+
return matchers;
|
|
1586
|
+
}
|
|
1587
|
+
#buildMatcher(method) {
|
|
1588
|
+
const routes = [];
|
|
1589
|
+
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
1590
|
+
[this.#middleware, this.#routes].forEach((r) => {
|
|
1591
|
+
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
1592
|
+
if (ownRoute.length !== 0) {
|
|
1593
|
+
hasOwnRoute ||= true;
|
|
1594
|
+
routes.push(...ownRoute);
|
|
1595
|
+
} else if (method !== METHOD_NAME_ALL) {
|
|
1596
|
+
routes.push(...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]]));
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
if (!hasOwnRoute) {
|
|
1600
|
+
return null;
|
|
1601
|
+
} else {
|
|
1602
|
+
return buildMatcherFromPreprocessedRoutes(routes);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
};
|
|
1606
|
+
|
|
1607
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/reg-exp-router/prepared-router.js
|
|
1608
|
+
var PreparedRegExpRouter = class {
|
|
1609
|
+
name = "PreparedRegExpRouter";
|
|
1610
|
+
#matchers;
|
|
1611
|
+
#relocateMap;
|
|
1612
|
+
constructor(matchers, relocateMap) {
|
|
1613
|
+
this.#matchers = matchers;
|
|
1614
|
+
this.#relocateMap = relocateMap;
|
|
1615
|
+
}
|
|
1616
|
+
#addWildcard(method, handlerData) {
|
|
1617
|
+
const matcher = this.#matchers[method];
|
|
1618
|
+
matcher[1].forEach((list) => list && list.push(handlerData));
|
|
1619
|
+
Object.values(matcher[2]).forEach((list) => list[0].push(handlerData));
|
|
1620
|
+
}
|
|
1621
|
+
#addPath(method, path, handler, indexes, map) {
|
|
1622
|
+
const matcher = this.#matchers[method];
|
|
1623
|
+
if (!map) {
|
|
1624
|
+
matcher[2][path][0].push([handler, {}]);
|
|
1625
|
+
} else {
|
|
1626
|
+
indexes.forEach((index) => {
|
|
1627
|
+
if (typeof index === "number") {
|
|
1628
|
+
matcher[1][index].push([handler, map]);
|
|
1629
|
+
} else {
|
|
1630
|
+
matcher[2][index || path][0].push([handler, map]);
|
|
1631
|
+
}
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
add(method, path, handler) {
|
|
1636
|
+
if (!this.#matchers[method]) {
|
|
1637
|
+
const all = this.#matchers[METHOD_NAME_ALL];
|
|
1638
|
+
const staticMap = {};
|
|
1639
|
+
for (const key in all[2]) {
|
|
1640
|
+
staticMap[key] = [all[2][key][0].slice(), emptyParam];
|
|
1641
|
+
}
|
|
1642
|
+
this.#matchers[method] = [
|
|
1643
|
+
all[0],
|
|
1644
|
+
all[1].map((list) => Array.isArray(list) ? list.slice() : 0),
|
|
1645
|
+
staticMap
|
|
1646
|
+
];
|
|
1647
|
+
}
|
|
1648
|
+
if (path === "/*" || path === "*") {
|
|
1649
|
+
const handlerData = [handler, {}];
|
|
1650
|
+
if (method === METHOD_NAME_ALL) {
|
|
1651
|
+
for (const m in this.#matchers) {
|
|
1652
|
+
this.#addWildcard(m, handlerData);
|
|
1653
|
+
}
|
|
1654
|
+
} else {
|
|
1655
|
+
this.#addWildcard(method, handlerData);
|
|
1656
|
+
}
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1659
|
+
const data = this.#relocateMap[path];
|
|
1660
|
+
if (!data) {
|
|
1661
|
+
throw new Error(`Path ${path} is not registered`);
|
|
1662
|
+
}
|
|
1663
|
+
for (const [indexes, map] of data) {
|
|
1664
|
+
if (method === METHOD_NAME_ALL) {
|
|
1665
|
+
for (const m in this.#matchers) {
|
|
1666
|
+
this.#addPath(m, path, handler, indexes, map);
|
|
1667
|
+
}
|
|
1668
|
+
} else {
|
|
1669
|
+
this.#addPath(method, path, handler, indexes, map);
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
buildAllMatchers() {
|
|
1674
|
+
return this.#matchers;
|
|
1675
|
+
}
|
|
1676
|
+
match = match;
|
|
1677
|
+
};
|
|
1678
|
+
|
|
1679
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/smart-router/router.js
|
|
1680
|
+
var SmartRouter = class {
|
|
1681
|
+
name = "SmartRouter";
|
|
1682
|
+
#routers = [];
|
|
1683
|
+
#routes = [];
|
|
1684
|
+
constructor(init) {
|
|
1685
|
+
this.#routers = init.routers;
|
|
1686
|
+
}
|
|
1687
|
+
add(method, path, handler) {
|
|
1688
|
+
if (!this.#routes) {
|
|
1689
|
+
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1690
|
+
}
|
|
1691
|
+
this.#routes.push([method, path, handler]);
|
|
1692
|
+
}
|
|
1693
|
+
match(method, path) {
|
|
1694
|
+
if (!this.#routes) {
|
|
1695
|
+
throw new Error("Fatal error");
|
|
1696
|
+
}
|
|
1697
|
+
const routers = this.#routers;
|
|
1698
|
+
const routes = this.#routes;
|
|
1699
|
+
const len = routers.length;
|
|
1700
|
+
let i = 0;
|
|
1701
|
+
let res;
|
|
1702
|
+
for (;i < len; i++) {
|
|
1703
|
+
const router = routers[i];
|
|
1704
|
+
try {
|
|
1705
|
+
for (let i2 = 0, len2 = routes.length;i2 < len2; i2++) {
|
|
1706
|
+
router.add(...routes[i2]);
|
|
1707
|
+
}
|
|
1708
|
+
res = router.match(method, path);
|
|
1709
|
+
} catch (e) {
|
|
1710
|
+
if (e instanceof UnsupportedPathError) {
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
throw e;
|
|
1714
|
+
}
|
|
1715
|
+
this.match = router.match.bind(router);
|
|
1716
|
+
this.#routers = [router];
|
|
1717
|
+
this.#routes = undefined;
|
|
1718
|
+
break;
|
|
1719
|
+
}
|
|
1720
|
+
if (i === len) {
|
|
1721
|
+
throw new Error("Fatal error");
|
|
1722
|
+
}
|
|
1723
|
+
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
1724
|
+
return res;
|
|
1725
|
+
}
|
|
1726
|
+
get activeRouter() {
|
|
1727
|
+
if (this.#routes || this.#routers.length !== 1) {
|
|
1728
|
+
throw new Error("No active router has been determined yet.");
|
|
1729
|
+
}
|
|
1730
|
+
return this.#routers[0];
|
|
1731
|
+
}
|
|
1732
|
+
};
|
|
1733
|
+
|
|
1734
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/trie-router/node.js
|
|
1735
|
+
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
1736
|
+
var Node2 = class _Node2 {
|
|
1737
|
+
#methods;
|
|
1738
|
+
#children;
|
|
1739
|
+
#patterns;
|
|
1740
|
+
#order = 0;
|
|
1741
|
+
#params = emptyParams;
|
|
1742
|
+
constructor(method, handler, children) {
|
|
1743
|
+
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
1744
|
+
this.#methods = [];
|
|
1745
|
+
if (method && handler) {
|
|
1746
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
1747
|
+
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
1748
|
+
this.#methods = [m];
|
|
1749
|
+
}
|
|
1750
|
+
this.#patterns = [];
|
|
1751
|
+
}
|
|
1752
|
+
insert(method, path, handler) {
|
|
1753
|
+
this.#order = ++this.#order;
|
|
1754
|
+
let curNode = this;
|
|
1755
|
+
const parts = splitRoutingPath(path);
|
|
1756
|
+
const possibleKeys = [];
|
|
1757
|
+
for (let i = 0, len = parts.length;i < len; i++) {
|
|
1758
|
+
const p = parts[i];
|
|
1759
|
+
const nextP = parts[i + 1];
|
|
1760
|
+
const pattern = getPattern(p, nextP);
|
|
1761
|
+
const key = Array.isArray(pattern) ? pattern[0] : p;
|
|
1762
|
+
if (key in curNode.#children) {
|
|
1763
|
+
curNode = curNode.#children[key];
|
|
1764
|
+
if (pattern) {
|
|
1765
|
+
possibleKeys.push(pattern[1]);
|
|
1766
|
+
}
|
|
1767
|
+
continue;
|
|
1768
|
+
}
|
|
1769
|
+
curNode.#children[key] = new _Node2;
|
|
1770
|
+
if (pattern) {
|
|
1771
|
+
curNode.#patterns.push(pattern);
|
|
1772
|
+
possibleKeys.push(pattern[1]);
|
|
1773
|
+
}
|
|
1774
|
+
curNode = curNode.#children[key];
|
|
1775
|
+
}
|
|
1776
|
+
curNode.#methods.push({
|
|
1777
|
+
[method]: {
|
|
1778
|
+
handler,
|
|
1779
|
+
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
1780
|
+
score: this.#order
|
|
1781
|
+
}
|
|
1782
|
+
});
|
|
1783
|
+
return curNode;
|
|
1784
|
+
}
|
|
1785
|
+
#getHandlerSets(node, method, nodeParams, params) {
|
|
1786
|
+
const handlerSets = [];
|
|
1787
|
+
for (let i = 0, len = node.#methods.length;i < len; i++) {
|
|
1788
|
+
const m = node.#methods[i];
|
|
1789
|
+
const handlerSet = m[method] || m[METHOD_NAME_ALL];
|
|
1790
|
+
const processedSet = {};
|
|
1791
|
+
if (handlerSet !== undefined) {
|
|
1792
|
+
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
1793
|
+
handlerSets.push(handlerSet);
|
|
1794
|
+
if (nodeParams !== emptyParams || params && params !== emptyParams) {
|
|
1795
|
+
for (let i2 = 0, len2 = handlerSet.possibleKeys.length;i2 < len2; i2++) {
|
|
1796
|
+
const key = handlerSet.possibleKeys[i2];
|
|
1797
|
+
const processed = processedSet[handlerSet.score];
|
|
1798
|
+
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
1799
|
+
processedSet[handlerSet.score] = true;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
return handlerSets;
|
|
1805
|
+
}
|
|
1806
|
+
search(method, path) {
|
|
1807
|
+
const handlerSets = [];
|
|
1808
|
+
this.#params = emptyParams;
|
|
1809
|
+
const curNode = this;
|
|
1810
|
+
let curNodes = [curNode];
|
|
1811
|
+
const parts = splitPath(path);
|
|
1812
|
+
const curNodesQueue = [];
|
|
1813
|
+
for (let i = 0, len = parts.length;i < len; i++) {
|
|
1814
|
+
const part = parts[i];
|
|
1815
|
+
const isLast = i === len - 1;
|
|
1816
|
+
const tempNodes = [];
|
|
1817
|
+
for (let j = 0, len2 = curNodes.length;j < len2; j++) {
|
|
1818
|
+
const node = curNodes[j];
|
|
1819
|
+
const nextNode = node.#children[part];
|
|
1820
|
+
if (nextNode) {
|
|
1821
|
+
nextNode.#params = node.#params;
|
|
1822
|
+
if (isLast) {
|
|
1823
|
+
if (nextNode.#children["*"]) {
|
|
1824
|
+
handlerSets.push(...this.#getHandlerSets(nextNode.#children["*"], method, node.#params));
|
|
1825
|
+
}
|
|
1826
|
+
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
|
|
1827
|
+
} else {
|
|
1828
|
+
tempNodes.push(nextNode);
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
for (let k = 0, len3 = node.#patterns.length;k < len3; k++) {
|
|
1832
|
+
const pattern = node.#patterns[k];
|
|
1833
|
+
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
1834
|
+
if (pattern === "*") {
|
|
1835
|
+
const astNode = node.#children["*"];
|
|
1836
|
+
if (astNode) {
|
|
1837
|
+
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
|
|
1838
|
+
astNode.#params = params;
|
|
1839
|
+
tempNodes.push(astNode);
|
|
1840
|
+
}
|
|
1841
|
+
continue;
|
|
1842
|
+
}
|
|
1843
|
+
const [key, name, matcher] = pattern;
|
|
1844
|
+
if (!part && !(matcher instanceof RegExp)) {
|
|
1845
|
+
continue;
|
|
1846
|
+
}
|
|
1847
|
+
const child = node.#children[key];
|
|
1848
|
+
const restPathString = parts.slice(i).join("/");
|
|
1849
|
+
if (matcher instanceof RegExp) {
|
|
1850
|
+
const m = matcher.exec(restPathString);
|
|
1851
|
+
if (m) {
|
|
1852
|
+
params[name] = m[0];
|
|
1853
|
+
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
1854
|
+
if (Object.keys(child.#children).length) {
|
|
1855
|
+
child.#params = params;
|
|
1856
|
+
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
1857
|
+
const targetCurNodes = curNodesQueue[componentCount] ||= [];
|
|
1858
|
+
targetCurNodes.push(child);
|
|
1859
|
+
}
|
|
1860
|
+
continue;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
if (matcher === true || matcher.test(part)) {
|
|
1864
|
+
params[name] = part;
|
|
1865
|
+
if (isLast) {
|
|
1866
|
+
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
1867
|
+
if (child.#children["*"]) {
|
|
1868
|
+
handlerSets.push(...this.#getHandlerSets(child.#children["*"], method, params, node.#params));
|
|
1869
|
+
}
|
|
1870
|
+
} else {
|
|
1871
|
+
child.#params = params;
|
|
1872
|
+
tempNodes.push(child);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
|
|
1878
|
+
}
|
|
1879
|
+
if (handlerSets.length > 1) {
|
|
1880
|
+
handlerSets.sort((a, b) => {
|
|
1881
|
+
return a.score - b.score;
|
|
1882
|
+
});
|
|
1883
|
+
}
|
|
1884
|
+
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
1885
|
+
}
|
|
1886
|
+
};
|
|
1887
|
+
|
|
1888
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/router/trie-router/router.js
|
|
1889
|
+
var TrieRouter = class {
|
|
1890
|
+
name = "TrieRouter";
|
|
1891
|
+
#node;
|
|
1892
|
+
constructor() {
|
|
1893
|
+
this.#node = new Node2;
|
|
1894
|
+
}
|
|
1895
|
+
add(method, path, handler) {
|
|
1896
|
+
const results = checkOptionalParameter(path);
|
|
1897
|
+
if (results) {
|
|
1898
|
+
for (let i = 0, len = results.length;i < len; i++) {
|
|
1899
|
+
this.#node.insert(method, results[i], handler);
|
|
1900
|
+
}
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1903
|
+
this.#node.insert(method, path, handler);
|
|
1904
|
+
}
|
|
1905
|
+
match(method, path) {
|
|
1906
|
+
return this.#node.search(method, path);
|
|
1907
|
+
}
|
|
1908
|
+
};
|
|
1909
|
+
|
|
1910
|
+
// ../node_modules/.bun/hono@4.11.2/node_modules/hono/dist/hono.js
|
|
1911
|
+
var Hono2 = class extends Hono {
|
|
1912
|
+
constructor(options = {}) {
|
|
1913
|
+
super(options);
|
|
1914
|
+
this.router = options.router ?? new SmartRouter({
|
|
1915
|
+
routers: [new RegExpRouter, new TrieRouter]
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1918
|
+
};
|
|
1919
|
+
|
|
1920
|
+
// src/routes.ts
|
|
1921
|
+
function isAuthorized(req, sharedSecret) {
|
|
1922
|
+
const header = req.headers.get("authorization")?.trim();
|
|
1923
|
+
return header === `Bearer ${sharedSecret}`;
|
|
1924
|
+
}
|
|
1925
|
+
async function dispatchEvents(callbackClient, events) {
|
|
1926
|
+
for (const event of events) {
|
|
1927
|
+
await callbackClient.postEvent(event);
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
function createPluginApp(deps) {
|
|
1931
|
+
const app = new Hono2;
|
|
1932
|
+
const healthHandler = (c) => {
|
|
1933
|
+
const status = {
|
|
1934
|
+
ok: true,
|
|
1935
|
+
pluginVersion: OPENCLAW_PLUGIN_VERSION,
|
|
1936
|
+
openclawConnected: true,
|
|
1937
|
+
prototypeCapture: {
|
|
1938
|
+
enabled: Boolean(deps.prototypeCaptureSessionKey),
|
|
1939
|
+
sessionKey: deps.prototypeCaptureSessionKey ?? null,
|
|
1940
|
+
fileName: deps.prototypeCaptureFileName ?? "transcript-capture.jsonl"
|
|
1941
|
+
}
|
|
1942
|
+
};
|
|
1943
|
+
return c.json(status);
|
|
1944
|
+
};
|
|
1945
|
+
const authMiddleware = async (c, next) => {
|
|
1946
|
+
if (!isAuthorized(c.req.raw, deps.sharedSecret)) {
|
|
1947
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
1948
|
+
}
|
|
1949
|
+
return await next();
|
|
1950
|
+
};
|
|
1951
|
+
app.get("/hapi/health", healthHandler);
|
|
1952
|
+
app.get("/hapi/debug/transcript-capture", healthHandler);
|
|
1953
|
+
app.use("/hapi/channel/*", authMiddleware);
|
|
1954
|
+
const ensureDefaultConversationHandler = async (c) => {
|
|
1955
|
+
const body = await c.req.json().catch(() => null);
|
|
1956
|
+
if (!body?.externalUserKey) {
|
|
1957
|
+
return c.json({ error: "Invalid body" }, 400);
|
|
1958
|
+
}
|
|
1959
|
+
return c.json(deps.runtime.ensureDefaultConversation(body.externalUserKey));
|
|
1960
|
+
};
|
|
1961
|
+
const sendMessageHandler = async (c) => {
|
|
1962
|
+
const idempotencyKey = c.req.header("idempotency-key");
|
|
1963
|
+
if (!idempotencyKey) {
|
|
1964
|
+
return c.json({ error: "Missing idempotency-key" }, 400);
|
|
1965
|
+
}
|
|
1966
|
+
const cached = deps.idempotencyCache.get(idempotencyKey);
|
|
1967
|
+
if (cached) {
|
|
1968
|
+
return c.json(cached);
|
|
1969
|
+
}
|
|
1970
|
+
const body = await c.req.json().catch(() => null);
|
|
1971
|
+
if (!body?.conversationId || typeof body.text !== "string" || !body.localMessageId) {
|
|
1972
|
+
return c.json({ error: "Invalid body" }, 400);
|
|
1973
|
+
}
|
|
1974
|
+
const ack = {
|
|
1975
|
+
accepted: true,
|
|
1976
|
+
upstreamRequestId: `plugin-send:${randomUUID2()}`,
|
|
1977
|
+
upstreamConversationId: body.conversationId,
|
|
1978
|
+
retryAfterMs: null
|
|
1979
|
+
};
|
|
1980
|
+
deps.idempotencyCache.set(idempotencyKey, ack);
|
|
1981
|
+
queueMicrotask(() => {
|
|
1982
|
+
dispatchEvents(deps.callbackClient, deps.runtime.run({
|
|
1983
|
+
kind: "send-message",
|
|
1984
|
+
conversationId: body.conversationId,
|
|
1985
|
+
text: body.text,
|
|
1986
|
+
localMessageId: body.localMessageId
|
|
1987
|
+
}));
|
|
1988
|
+
});
|
|
1989
|
+
return c.json(ack);
|
|
1990
|
+
};
|
|
1991
|
+
const approveHandler = async (c) => {
|
|
1992
|
+
const idempotencyKey = c.req.header("idempotency-key");
|
|
1993
|
+
if (!idempotencyKey) {
|
|
1994
|
+
return c.json({ error: "Missing idempotency-key" }, 400);
|
|
1995
|
+
}
|
|
1996
|
+
const cached = deps.idempotencyCache.get(idempotencyKey);
|
|
1997
|
+
if (cached) {
|
|
1998
|
+
return c.json(cached);
|
|
1999
|
+
}
|
|
2000
|
+
const body = await c.req.json().catch(() => null);
|
|
2001
|
+
if (!body?.conversationId) {
|
|
2002
|
+
return c.json({ error: "Invalid body" }, 400);
|
|
2003
|
+
}
|
|
2004
|
+
const ack = {
|
|
2005
|
+
accepted: true,
|
|
2006
|
+
upstreamRequestId: `plugin-approve:${randomUUID2()}`,
|
|
2007
|
+
upstreamConversationId: body.conversationId,
|
|
2008
|
+
retryAfterMs: null
|
|
2009
|
+
};
|
|
2010
|
+
deps.idempotencyCache.set(idempotencyKey, ack);
|
|
2011
|
+
queueMicrotask(() => {
|
|
2012
|
+
dispatchEvents(deps.callbackClient, deps.runtime.run({
|
|
2013
|
+
kind: "approve",
|
|
2014
|
+
conversationId: body.conversationId,
|
|
2015
|
+
requestId: c.req.param("requestId")
|
|
2016
|
+
}));
|
|
2017
|
+
});
|
|
2018
|
+
return c.json(ack);
|
|
2019
|
+
};
|
|
2020
|
+
const denyHandler = async (c) => {
|
|
2021
|
+
const idempotencyKey = c.req.header("idempotency-key");
|
|
2022
|
+
if (!idempotencyKey) {
|
|
2023
|
+
return c.json({ error: "Missing idempotency-key" }, 400);
|
|
2024
|
+
}
|
|
2025
|
+
const cached = deps.idempotencyCache.get(idempotencyKey);
|
|
2026
|
+
if (cached) {
|
|
2027
|
+
return c.json(cached);
|
|
2028
|
+
}
|
|
2029
|
+
const body = await c.req.json().catch(() => null);
|
|
2030
|
+
if (!body?.conversationId) {
|
|
2031
|
+
return c.json({ error: "Invalid body" }, 400);
|
|
2032
|
+
}
|
|
2033
|
+
const ack = {
|
|
2034
|
+
accepted: true,
|
|
2035
|
+
upstreamRequestId: `plugin-deny:${randomUUID2()}`,
|
|
2036
|
+
upstreamConversationId: body.conversationId,
|
|
2037
|
+
retryAfterMs: null
|
|
2038
|
+
};
|
|
2039
|
+
deps.idempotencyCache.set(idempotencyKey, ack);
|
|
2040
|
+
queueMicrotask(() => {
|
|
2041
|
+
dispatchEvents(deps.callbackClient, deps.runtime.run({
|
|
2042
|
+
kind: "deny",
|
|
2043
|
+
conversationId: body.conversationId,
|
|
2044
|
+
requestId: c.req.param("requestId")
|
|
2045
|
+
}));
|
|
2046
|
+
});
|
|
2047
|
+
return c.json(ack);
|
|
2048
|
+
};
|
|
2049
|
+
app.post("/hapi/channel/conversations/default", ensureDefaultConversationHandler);
|
|
2050
|
+
app.post("/hapi/channel/messages", sendMessageHandler);
|
|
2051
|
+
app.post("/hapi/channel/approvals/:requestId/approve", approveHandler);
|
|
2052
|
+
app.post("/hapi/channel/approvals/:requestId/deny", denyHandler);
|
|
2053
|
+
return app;
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
// src/index.ts
|
|
2057
|
+
function registerPluginRoutes(api) {
|
|
2058
|
+
const config = resolvePluginConfig(api.pluginConfig ?? {});
|
|
2059
|
+
const callbackClient = new HapiCallbackClient(config.hapiBaseUrl, config.sharedSecret);
|
|
2060
|
+
const runtime = new MockOpenClawRuntime(config.namespace);
|
|
2061
|
+
const app = createPluginApp({
|
|
2062
|
+
sharedSecret: config.sharedSecret,
|
|
2063
|
+
namespace: config.namespace,
|
|
2064
|
+
callbackClient,
|
|
2065
|
+
runtime,
|
|
2066
|
+
idempotencyCache: new Map,
|
|
2067
|
+
prototypeCaptureSessionKey: config.prototypeCaptureSessionKey,
|
|
2068
|
+
prototypeCaptureFileName: config.prototypeCaptureFileName
|
|
2069
|
+
});
|
|
2070
|
+
api.registerHttpRoute({
|
|
2071
|
+
path: "/hapi",
|
|
2072
|
+
auth: "plugin",
|
|
2073
|
+
match: "prefix",
|
|
2074
|
+
handler: async (req, res) => await forwardNodeRequestToHono(app, req, res)
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
var src_default = definePluginEntry({
|
|
2078
|
+
id: OPENCLAW_PLUGIN_ID,
|
|
2079
|
+
name: OPENCLAW_PLUGIN_NAME,
|
|
2080
|
+
description: OPENCLAW_PLUGIN_DESCRIPTION,
|
|
2081
|
+
configSchema: {
|
|
2082
|
+
jsonSchema: PLUGIN_CONFIG_JSON_SCHEMA,
|
|
2083
|
+
validate(value) {
|
|
2084
|
+
try {
|
|
2085
|
+
resolvePluginConfig(value);
|
|
2086
|
+
return { ok: true, value };
|
|
2087
|
+
} catch (error2) {
|
|
2088
|
+
return {
|
|
2089
|
+
ok: false,
|
|
2090
|
+
errors: [error2 instanceof Error ? error2.message : "Invalid plugin config"]
|
|
2091
|
+
};
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
},
|
|
2095
|
+
register(api) {
|
|
2096
|
+
if (api.registrationMode !== "full") {
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
runtimeStore.setRuntime(api.runtime);
|
|
2100
|
+
registerPluginRoutes(api);
|
|
2101
|
+
api.registerService(createTranscriptCaptureService());
|
|
2102
|
+
}
|
|
2103
|
+
});
|
|
2104
|
+
export {
|
|
2105
|
+
src_default as default
|
|
2106
|
+
};
|