@ubiquity-os/plugin-sdk 3.8.0 → 3.8.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 +43 -27
- package/dist/configuration.d.mts +3 -3
- package/dist/configuration.d.ts +3 -3
- package/dist/configuration.js +12 -2632
- package/dist/configuration.mjs +2 -2632
- package/dist/{context-zLHgu52i.d.ts → context-BE4WjJZf.d.ts} +0 -28
- package/dist/{context-Dwl3aRX-.d.mts → context-Ckj1HMjz.d.mts} +0 -28
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +127 -2857
- package/dist/index.mjs +93 -2823
- package/dist/llm.d.mts +2 -2
- package/dist/llm.d.ts +2 -2
- package/dist/llm.js +11 -0
- package/dist/llm.mjs +11 -0
- package/dist/octokit.d.mts +2 -2
- package/dist/octokit.d.ts +2 -2
- package/dist/octokit.js +2 -164
- package/dist/octokit.mjs +1 -163
- package/package.json +11 -1
package/dist/index.js
CHANGED
|
@@ -42,343 +42,11 @@ module.exports = __toCommonJS(src_exports);
|
|
|
42
42
|
// src/actions.ts
|
|
43
43
|
var core = __toESM(require("@actions/core"));
|
|
44
44
|
var import_value3 = require("@sinclair/typebox/value");
|
|
45
|
-
|
|
46
|
-
// ../../node_modules/@ubiquity-os/ubiquity-os-logger/dist/index.js
|
|
47
|
-
var COLORS = {
|
|
48
|
-
reset: "\x1B[0m",
|
|
49
|
-
bright: "\x1B[1m",
|
|
50
|
-
dim: "\x1B[2m",
|
|
51
|
-
underscore: "\x1B[4m",
|
|
52
|
-
blink: "\x1B[5m",
|
|
53
|
-
reverse: "\x1B[7m",
|
|
54
|
-
hidden: "\x1B[8m",
|
|
55
|
-
fgBlack: "\x1B[30m",
|
|
56
|
-
fgRed: "\x1B[31m",
|
|
57
|
-
fgGreen: "\x1B[32m",
|
|
58
|
-
fgYellow: "\x1B[33m",
|
|
59
|
-
fgBlue: "\x1B[34m",
|
|
60
|
-
fgMagenta: "\x1B[35m",
|
|
61
|
-
fgCyan: "\x1B[36m",
|
|
62
|
-
fgWhite: "\x1B[37m",
|
|
63
|
-
bgBlack: "\x1B[40m",
|
|
64
|
-
bgRed: "\x1B[41m",
|
|
65
|
-
bgGreen: "\x1B[42m",
|
|
66
|
-
bgYellow: "\x1B[43m",
|
|
67
|
-
bgBlue: "\x1B[44m",
|
|
68
|
-
bgMagenta: "\x1B[45m",
|
|
69
|
-
bgCyan: "\x1B[46m",
|
|
70
|
-
bgWhite: "\x1B[47m"
|
|
71
|
-
};
|
|
72
|
-
var LOG_LEVEL = {
|
|
73
|
-
FATAL: "fatal",
|
|
74
|
-
ERROR: "error",
|
|
75
|
-
WARN: "warn",
|
|
76
|
-
INFO: "info",
|
|
77
|
-
VERBOSE: "verbose",
|
|
78
|
-
DEBUG: "debug"
|
|
79
|
-
};
|
|
80
|
-
var PrettyLogs = class {
|
|
81
|
-
constructor() {
|
|
82
|
-
this.ok = this.ok.bind(this);
|
|
83
|
-
this.info = this.info.bind(this);
|
|
84
|
-
this.error = this.error.bind(this);
|
|
85
|
-
this.fatal = this.fatal.bind(this);
|
|
86
|
-
this.warn = this.warn.bind(this);
|
|
87
|
-
this.debug = this.debug.bind(this);
|
|
88
|
-
this.verbose = this.verbose.bind(this);
|
|
89
|
-
}
|
|
90
|
-
fatal(message, metadata) {
|
|
91
|
-
this._logWithStack(LOG_LEVEL.FATAL, message, metadata);
|
|
92
|
-
}
|
|
93
|
-
error(message, metadata) {
|
|
94
|
-
this._logWithStack(LOG_LEVEL.ERROR, message, metadata);
|
|
95
|
-
}
|
|
96
|
-
warn(message, metadata) {
|
|
97
|
-
this._logWithStack(LOG_LEVEL.WARN, message, metadata);
|
|
98
|
-
}
|
|
99
|
-
ok(message, metadata) {
|
|
100
|
-
this._logWithStack("ok", message, metadata);
|
|
101
|
-
}
|
|
102
|
-
info(message, metadata) {
|
|
103
|
-
this._logWithStack(LOG_LEVEL.INFO, message, metadata);
|
|
104
|
-
}
|
|
105
|
-
debug(message, metadata) {
|
|
106
|
-
this._logWithStack(LOG_LEVEL.DEBUG, message, metadata);
|
|
107
|
-
}
|
|
108
|
-
verbose(message, metadata) {
|
|
109
|
-
this._logWithStack(LOG_LEVEL.VERBOSE, message, metadata);
|
|
110
|
-
}
|
|
111
|
-
_logWithStack(type, message, metaData) {
|
|
112
|
-
this._log(type, message);
|
|
113
|
-
if (typeof metaData === "string") {
|
|
114
|
-
this._log(type, metaData);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
if (metaData) {
|
|
118
|
-
const metadata = metaData;
|
|
119
|
-
let stack = metadata?.error?.stack || metadata?.stack;
|
|
120
|
-
if (!stack) {
|
|
121
|
-
const stackTrace = new Error().stack?.split("\n");
|
|
122
|
-
if (stackTrace) {
|
|
123
|
-
stackTrace.splice(0, 4);
|
|
124
|
-
stack = stackTrace.filter((line) => line.includes(".ts:")).join("\n");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
const newMetadata = { ...metadata };
|
|
128
|
-
delete newMetadata.message;
|
|
129
|
-
delete newMetadata.name;
|
|
130
|
-
delete newMetadata.stack;
|
|
131
|
-
if (!this._isEmpty(newMetadata)) {
|
|
132
|
-
this._log(type, newMetadata);
|
|
133
|
-
}
|
|
134
|
-
if (typeof stack == "string") {
|
|
135
|
-
const prettyStack = this._formatStackTrace(stack, 1);
|
|
136
|
-
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
|
|
137
|
-
this._log(type, colorizedStack);
|
|
138
|
-
} else if (stack) {
|
|
139
|
-
const prettyStack = this._formatStackTrace(stack.join("\n"), 1);
|
|
140
|
-
const colorizedStack = this._colorizeText(prettyStack, COLORS.dim);
|
|
141
|
-
this._log(type, colorizedStack);
|
|
142
|
-
} else {
|
|
143
|
-
throw new Error("Stack is null");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
_colorizeText(text, color) {
|
|
148
|
-
if (!color) {
|
|
149
|
-
throw new Error(`Invalid color: ${color}`);
|
|
150
|
-
}
|
|
151
|
-
return color.concat(text).concat(COLORS.reset);
|
|
152
|
-
}
|
|
153
|
-
_formatStackTrace(stack, linesToRemove = 0, prefix = "") {
|
|
154
|
-
const lines = stack.split("\n");
|
|
155
|
-
for (let i = 0; i < linesToRemove; i++) {
|
|
156
|
-
lines.shift();
|
|
157
|
-
}
|
|
158
|
-
return lines.map((line) => `${prefix}${line.replace(/\s*at\s*/, " \u21B3 ")}`).join("\n");
|
|
159
|
-
}
|
|
160
|
-
_isEmpty(obj) {
|
|
161
|
-
return !Reflect.ownKeys(obj).some((key) => typeof obj[String(key)] !== "function");
|
|
162
|
-
}
|
|
163
|
-
_log(type, message) {
|
|
164
|
-
const defaultSymbols = {
|
|
165
|
-
fatal: "\xD7",
|
|
166
|
-
ok: "\u2713",
|
|
167
|
-
warn: "\u26A0",
|
|
168
|
-
error: "\u26A0",
|
|
169
|
-
info: "\u203A",
|
|
170
|
-
debug: "\u203A\u203A",
|
|
171
|
-
verbose: "\u{1F4AC}"
|
|
172
|
-
};
|
|
173
|
-
const symbol = defaultSymbols[type];
|
|
174
|
-
const messageFormatted = typeof message === "string" ? message : JSON.stringify(message, null, 2);
|
|
175
|
-
const lines = messageFormatted.split("\n");
|
|
176
|
-
const logString = lines.map((line, index) => {
|
|
177
|
-
const prefix = index === 0 ? ` ${symbol}` : ` ${" ".repeat(symbol.length)}`;
|
|
178
|
-
return `${prefix} ${line}`;
|
|
179
|
-
}).join("\n");
|
|
180
|
-
const fullLogString = logString;
|
|
181
|
-
const colorMap = {
|
|
182
|
-
fatal: ["error", COLORS.fgRed],
|
|
183
|
-
ok: ["log", COLORS.fgGreen],
|
|
184
|
-
warn: ["warn", COLORS.fgYellow],
|
|
185
|
-
error: ["warn", COLORS.fgYellow],
|
|
186
|
-
info: ["info", COLORS.dim],
|
|
187
|
-
debug: ["debug", COLORS.fgMagenta],
|
|
188
|
-
verbose: ["debug", COLORS.dim]
|
|
189
|
-
};
|
|
190
|
-
const _console = console[colorMap[type][0]];
|
|
191
|
-
if (typeof _console === "function" && fullLogString.length > 12) {
|
|
192
|
-
_console(this._colorizeText(fullLogString, colorMap[type][1]));
|
|
193
|
-
} else if (fullLogString.length <= 12) {
|
|
194
|
-
return;
|
|
195
|
-
} else {
|
|
196
|
-
throw new Error(fullLogString);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
var LogReturn = class {
|
|
201
|
-
logMessage;
|
|
202
|
-
metadata;
|
|
203
|
-
constructor(logMessage, metadata) {
|
|
204
|
-
this.logMessage = logMessage;
|
|
205
|
-
this.metadata = metadata;
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
var Logs = class _Logs {
|
|
209
|
-
_maxLevel = -1;
|
|
210
|
-
static console;
|
|
211
|
-
_log({ level, consoleLog, logMessage, metadata, type }) {
|
|
212
|
-
if (this._getNumericLevel(level) <= this._maxLevel) {
|
|
213
|
-
consoleLog(logMessage, metadata);
|
|
214
|
-
}
|
|
215
|
-
return new LogReturn(
|
|
216
|
-
{
|
|
217
|
-
raw: logMessage,
|
|
218
|
-
diff: this._diffColorCommentMessage(type, logMessage),
|
|
219
|
-
type,
|
|
220
|
-
level
|
|
221
|
-
},
|
|
222
|
-
metadata
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
_addDiagnosticInformation(metadata) {
|
|
226
|
-
if (!metadata) {
|
|
227
|
-
metadata = {};
|
|
228
|
-
} else if (typeof metadata !== "object") {
|
|
229
|
-
metadata = { message: metadata };
|
|
230
|
-
}
|
|
231
|
-
const stackLines = new Error().stack?.split("\n") || [];
|
|
232
|
-
if (stackLines.length > 3) {
|
|
233
|
-
const callerLine = stackLines[3];
|
|
234
|
-
const match2 = callerLine.match(/at (\S+)/);
|
|
235
|
-
if (match2) {
|
|
236
|
-
metadata.caller = match2[1];
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return metadata;
|
|
240
|
-
}
|
|
241
|
-
ok(log, metadata) {
|
|
242
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
243
|
-
return this._log({
|
|
244
|
-
level: LOG_LEVEL.INFO,
|
|
245
|
-
consoleLog: _Logs.console.ok,
|
|
246
|
-
logMessage: log,
|
|
247
|
-
metadata,
|
|
248
|
-
type: "ok"
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
info(log, metadata) {
|
|
252
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
253
|
-
return this._log({
|
|
254
|
-
level: LOG_LEVEL.INFO,
|
|
255
|
-
consoleLog: _Logs.console.info,
|
|
256
|
-
logMessage: log,
|
|
257
|
-
metadata,
|
|
258
|
-
type: "info"
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
warn(log, metadata) {
|
|
262
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
263
|
-
return this._log({
|
|
264
|
-
level: LOG_LEVEL.WARN,
|
|
265
|
-
consoleLog: _Logs.console.warn,
|
|
266
|
-
logMessage: log,
|
|
267
|
-
metadata,
|
|
268
|
-
type: "warn"
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
error(log, metadata) {
|
|
272
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
273
|
-
return this._log({
|
|
274
|
-
level: LOG_LEVEL.ERROR,
|
|
275
|
-
consoleLog: _Logs.console.error,
|
|
276
|
-
logMessage: log,
|
|
277
|
-
metadata,
|
|
278
|
-
type: "error"
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
debug(log, metadata) {
|
|
282
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
283
|
-
return this._log({
|
|
284
|
-
level: LOG_LEVEL.DEBUG,
|
|
285
|
-
consoleLog: _Logs.console.debug,
|
|
286
|
-
logMessage: log,
|
|
287
|
-
metadata,
|
|
288
|
-
type: "debug"
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
fatal(log, metadata) {
|
|
292
|
-
if (!metadata) {
|
|
293
|
-
metadata = _Logs.convertErrorsIntoObjects(new Error(log));
|
|
294
|
-
const stack = metadata.stack;
|
|
295
|
-
stack.splice(1, 1);
|
|
296
|
-
metadata.stack = stack;
|
|
297
|
-
}
|
|
298
|
-
if (metadata instanceof Error) {
|
|
299
|
-
metadata = _Logs.convertErrorsIntoObjects(metadata);
|
|
300
|
-
const stack = metadata.stack;
|
|
301
|
-
stack.splice(1, 1);
|
|
302
|
-
metadata.stack = stack;
|
|
303
|
-
}
|
|
304
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
305
|
-
return this._log({
|
|
306
|
-
level: LOG_LEVEL.FATAL,
|
|
307
|
-
consoleLog: _Logs.console.fatal,
|
|
308
|
-
logMessage: log,
|
|
309
|
-
metadata,
|
|
310
|
-
type: "fatal"
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
verbose(log, metadata) {
|
|
314
|
-
metadata = this._addDiagnosticInformation(metadata);
|
|
315
|
-
return this._log({
|
|
316
|
-
level: LOG_LEVEL.VERBOSE,
|
|
317
|
-
consoleLog: _Logs.console.verbose,
|
|
318
|
-
logMessage: log,
|
|
319
|
-
metadata,
|
|
320
|
-
type: "verbose"
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
constructor(logLevel) {
|
|
324
|
-
this._maxLevel = this._getNumericLevel(logLevel);
|
|
325
|
-
_Logs.console = new PrettyLogs();
|
|
326
|
-
}
|
|
327
|
-
_diffColorCommentMessage(type, message) {
|
|
328
|
-
const diffPrefix = {
|
|
329
|
-
fatal: "> [!CAUTION]",
|
|
330
|
-
error: "> [!CAUTION]",
|
|
331
|
-
warn: "> [!WARNING]",
|
|
332
|
-
ok: "> [!TIP]",
|
|
333
|
-
info: "> [!NOTE]",
|
|
334
|
-
debug: "> [!IMPORTANT]",
|
|
335
|
-
verbose: "> [!NOTE]"
|
|
336
|
-
};
|
|
337
|
-
const selected = diffPrefix[type];
|
|
338
|
-
if (selected) {
|
|
339
|
-
message = message.trim().split("\n").map((line) => `> ${line}`).join("\n");
|
|
340
|
-
}
|
|
341
|
-
return [selected, message].join("\n");
|
|
342
|
-
}
|
|
343
|
-
_getNumericLevel(level) {
|
|
344
|
-
switch (level) {
|
|
345
|
-
case LOG_LEVEL.FATAL:
|
|
346
|
-
return 0;
|
|
347
|
-
case LOG_LEVEL.ERROR:
|
|
348
|
-
return 1;
|
|
349
|
-
case LOG_LEVEL.WARN:
|
|
350
|
-
return 2;
|
|
351
|
-
case LOG_LEVEL.INFO:
|
|
352
|
-
return 3;
|
|
353
|
-
case LOG_LEVEL.VERBOSE:
|
|
354
|
-
return 4;
|
|
355
|
-
case LOG_LEVEL.DEBUG:
|
|
356
|
-
return 5;
|
|
357
|
-
default:
|
|
358
|
-
return -1;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
static convertErrorsIntoObjects(obj) {
|
|
362
|
-
if (obj instanceof Error) {
|
|
363
|
-
return {
|
|
364
|
-
message: obj.message,
|
|
365
|
-
name: obj.name,
|
|
366
|
-
stack: obj.stack ? obj.stack.split("\n") : null
|
|
367
|
-
};
|
|
368
|
-
} else if (typeof obj === "object" && obj !== null) {
|
|
369
|
-
const keys = Object.keys(obj);
|
|
370
|
-
keys.forEach((key) => {
|
|
371
|
-
obj[key] = this.convertErrorsIntoObjects(obj[key]);
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
return obj;
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
// src/actions.ts
|
|
45
|
+
var import_ubiquity_os_logger3 = require("@ubiquity-os/ubiquity-os-logger");
|
|
379
46
|
var import_dotenv = require("dotenv");
|
|
380
47
|
|
|
381
48
|
// src/error.ts
|
|
49
|
+
var import_ubiquity_os_logger = require("@ubiquity-os/ubiquity-os-logger");
|
|
382
50
|
function getErrorStatus(err) {
|
|
383
51
|
if (!err || typeof err !== "object") return null;
|
|
384
52
|
const candidate = err;
|
|
@@ -389,9 +57,9 @@ function getErrorStatus(err) {
|
|
|
389
57
|
if (Number.isFinite(parsed)) return parsed;
|
|
390
58
|
}
|
|
391
59
|
if (err instanceof Error) {
|
|
392
|
-
const
|
|
393
|
-
if (
|
|
394
|
-
const parsed = Number.parseInt(
|
|
60
|
+
const match = /LLM API error:\s*(\d{3})/i.exec(err.message);
|
|
61
|
+
if (match) {
|
|
62
|
+
const parsed = Number.parseInt(match[1], 10);
|
|
395
63
|
if (Number.isFinite(parsed)) return parsed;
|
|
396
64
|
}
|
|
397
65
|
}
|
|
@@ -408,12 +76,12 @@ function logByStatus(context, message, metadata) {
|
|
|
408
76
|
return context.logger.error(message, payload);
|
|
409
77
|
}
|
|
410
78
|
function transformError(context, error) {
|
|
411
|
-
if (error instanceof LogReturn) {
|
|
79
|
+
if (error instanceof import_ubiquity_os_logger.LogReturn) {
|
|
412
80
|
return error;
|
|
413
81
|
}
|
|
414
82
|
if (error instanceof AggregateError) {
|
|
415
83
|
const message = error.errors.map((err) => {
|
|
416
|
-
if (err instanceof LogReturn) {
|
|
84
|
+
if (err instanceof import_ubiquity_os_logger.LogReturn) {
|
|
417
85
|
return err.logMessage.raw;
|
|
418
86
|
}
|
|
419
87
|
if (err instanceof Error) {
|
|
@@ -429,56 +97,8 @@ function transformError(context, error) {
|
|
|
429
97
|
return logByStatus(context, String(error), { err: error });
|
|
430
98
|
}
|
|
431
99
|
|
|
432
|
-
//
|
|
433
|
-
var
|
|
434
|
-
const global = globalThis;
|
|
435
|
-
const globalEnv = global?.process?.env;
|
|
436
|
-
runtime ??= getRuntimeKey();
|
|
437
|
-
const runtimeEnvHandlers = {
|
|
438
|
-
bun: () => globalEnv,
|
|
439
|
-
node: () => globalEnv,
|
|
440
|
-
"edge-light": () => globalEnv,
|
|
441
|
-
deno: () => {
|
|
442
|
-
return Deno.env.toObject();
|
|
443
|
-
},
|
|
444
|
-
workerd: () => c.env,
|
|
445
|
-
// On Fastly Compute, you can use the ConfigStore to manage user-defined data.
|
|
446
|
-
fastly: () => ({}),
|
|
447
|
-
other: () => ({})
|
|
448
|
-
};
|
|
449
|
-
return runtimeEnvHandlers[runtime]();
|
|
450
|
-
};
|
|
451
|
-
var knownUserAgents = {
|
|
452
|
-
deno: "Deno",
|
|
453
|
-
bun: "Bun",
|
|
454
|
-
workerd: "Cloudflare-Workers",
|
|
455
|
-
node: "Node.js"
|
|
456
|
-
};
|
|
457
|
-
var getRuntimeKey = () => {
|
|
458
|
-
const global = globalThis;
|
|
459
|
-
const userAgentSupported = typeof navigator !== "undefined" && typeof navigator.userAgent === "string";
|
|
460
|
-
if (userAgentSupported) {
|
|
461
|
-
for (const [runtimeKey, userAgent] of Object.entries(knownUserAgents)) {
|
|
462
|
-
if (checkUserAgentEquals(userAgent)) {
|
|
463
|
-
return runtimeKey;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
if (typeof global?.EdgeRuntime === "string") {
|
|
468
|
-
return "edge-light";
|
|
469
|
-
}
|
|
470
|
-
if (global?.fastly !== void 0) {
|
|
471
|
-
return "fastly";
|
|
472
|
-
}
|
|
473
|
-
if (global?.process?.release?.name === "node") {
|
|
474
|
-
return "node";
|
|
475
|
-
}
|
|
476
|
-
return "other";
|
|
477
|
-
};
|
|
478
|
-
var checkUserAgentEquals = (platform) => {
|
|
479
|
-
const userAgent = navigator.userAgent;
|
|
480
|
-
return userAgent.startsWith(platform);
|
|
481
|
-
};
|
|
100
|
+
// src/helpers/runtime-info.ts
|
|
101
|
+
var import_adapter = require("hono/adapter");
|
|
482
102
|
|
|
483
103
|
// src/helpers/github-context.ts
|
|
484
104
|
var github = __toESM(require("@actions/github"));
|
|
@@ -499,29 +119,29 @@ function getGithubContext() {
|
|
|
499
119
|
var PluginRuntimeInfo = class _PluginRuntimeInfo {
|
|
500
120
|
static _instance = null;
|
|
501
121
|
_env = {};
|
|
502
|
-
constructor(
|
|
503
|
-
if (
|
|
504
|
-
this._env =
|
|
122
|
+
constructor(env) {
|
|
123
|
+
if (env) {
|
|
124
|
+
this._env = env;
|
|
505
125
|
}
|
|
506
126
|
}
|
|
507
|
-
static getInstance(
|
|
127
|
+
static getInstance(env) {
|
|
508
128
|
if (!_PluginRuntimeInfo._instance) {
|
|
509
|
-
switch (getRuntimeKey()) {
|
|
129
|
+
switch ((0, import_adapter.getRuntimeKey)()) {
|
|
510
130
|
case "workerd":
|
|
511
|
-
_PluginRuntimeInfo._instance = new CfRuntimeInfo(
|
|
131
|
+
_PluginRuntimeInfo._instance = new CfRuntimeInfo(env);
|
|
512
132
|
break;
|
|
513
133
|
case "deno":
|
|
514
134
|
if (process.env.CI) {
|
|
515
|
-
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(
|
|
135
|
+
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(env);
|
|
516
136
|
} else {
|
|
517
|
-
_PluginRuntimeInfo._instance = new DenoRuntimeInfo(
|
|
137
|
+
_PluginRuntimeInfo._instance = new DenoRuntimeInfo(env);
|
|
518
138
|
}
|
|
519
139
|
break;
|
|
520
140
|
case "node":
|
|
521
|
-
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(
|
|
141
|
+
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(env);
|
|
522
142
|
break;
|
|
523
143
|
default:
|
|
524
|
-
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(
|
|
144
|
+
_PluginRuntimeInfo._instance = new NodeRuntimeInfo(env);
|
|
525
145
|
break;
|
|
526
146
|
}
|
|
527
147
|
}
|
|
@@ -587,6 +207,9 @@ var DenoRuntimeInfo = class extends PluginRuntimeInfo {
|
|
|
587
207
|
}
|
|
588
208
|
};
|
|
589
209
|
|
|
210
|
+
// src/util.ts
|
|
211
|
+
var import_ubiquity_os_logger2 = require("@ubiquity-os/ubiquity-os-logger");
|
|
212
|
+
|
|
590
213
|
// src/constants.ts
|
|
591
214
|
var KERNEL_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
592
215
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs96DOU+JqM8SyNXOB6u3
|
|
@@ -625,7 +248,7 @@ function getPluginOptions(options) {
|
|
|
625
248
|
return {
|
|
626
249
|
// Important to use || and not ?? to not consider empty strings
|
|
627
250
|
kernelPublicKey: options?.kernelPublicKey || KERNEL_PUBLIC_KEY,
|
|
628
|
-
logLevel: options?.logLevel || LOG_LEVEL.INFO,
|
|
251
|
+
logLevel: options?.logLevel || import_ubiquity_os_logger2.LOG_LEVEL.INFO,
|
|
629
252
|
postCommentOnError: options?.postCommentOnError ?? true,
|
|
630
253
|
settingsSchema: options?.settingsSchema,
|
|
631
254
|
envSchema: options?.envSchema,
|
|
@@ -637,54 +260,6 @@ function getPluginOptions(options) {
|
|
|
637
260
|
}
|
|
638
261
|
|
|
639
262
|
// src/comment.ts
|
|
640
|
-
var COMMAND_RESPONSE_KIND = "command-response";
|
|
641
|
-
var COMMAND_RESPONSE_MARKER = `"commentKind": "${COMMAND_RESPONSE_KIND}"`;
|
|
642
|
-
var COMMAND_RESPONSE_COMMENT_LIMIT = 50;
|
|
643
|
-
var RECENT_COMMENTS_QUERY = `
|
|
644
|
-
query($owner: String!, $repo: String!, $number: Int!, $last: Int!) {
|
|
645
|
-
repository(owner: $owner, name: $repo) {
|
|
646
|
-
issueOrPullRequest(number: $number) {
|
|
647
|
-
__typename
|
|
648
|
-
... on Issue {
|
|
649
|
-
comments(last: $last) {
|
|
650
|
-
nodes {
|
|
651
|
-
id
|
|
652
|
-
body
|
|
653
|
-
isMinimized
|
|
654
|
-
minimizedReason
|
|
655
|
-
author {
|
|
656
|
-
login
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
... on PullRequest {
|
|
662
|
-
comments(last: $last) {
|
|
663
|
-
nodes {
|
|
664
|
-
id
|
|
665
|
-
body
|
|
666
|
-
isMinimized
|
|
667
|
-
minimizedReason
|
|
668
|
-
author {
|
|
669
|
-
login
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
`;
|
|
678
|
-
var MINIMIZE_COMMENT_MUTATION = `
|
|
679
|
-
mutation($id: ID!, $classifier: ReportedContentClassifiers!) {
|
|
680
|
-
minimizeComment(input: { subjectId: $id, classifier: $classifier }) {
|
|
681
|
-
minimizedComment {
|
|
682
|
-
isMinimized
|
|
683
|
-
minimizedReason
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
`;
|
|
688
263
|
function logByStatus2(logger, message, status, metadata) {
|
|
689
264
|
const payload = { ...metadata, ...status ? { status } : {} };
|
|
690
265
|
if (status && status >= 500) return logger.error(message, payload);
|
|
@@ -697,7 +272,6 @@ function logByStatus2(logger, message, status, metadata) {
|
|
|
697
272
|
var CommentHandler = class _CommentHandler {
|
|
698
273
|
static HEADER_NAME = "UbiquityOS";
|
|
699
274
|
_lastCommentId = { reviewCommentId: null, issueCommentId: null };
|
|
700
|
-
_commandResponsePolicyApplied = false;
|
|
701
275
|
async _updateIssueComment(context, params) {
|
|
702
276
|
if (!this._lastCommentId.issueCommentId) {
|
|
703
277
|
throw context.logger.error("issueCommentId is missing");
|
|
@@ -752,11 +326,6 @@ var CommentHandler = class _CommentHandler {
|
|
|
752
326
|
_getCommentId(context) {
|
|
753
327
|
return "pull_request" in context.payload && "comment" in context.payload ? context.payload.comment.id : void 0;
|
|
754
328
|
}
|
|
755
|
-
_getCommentNodeId(context) {
|
|
756
|
-
const payload = context.payload;
|
|
757
|
-
const nodeId = payload.comment?.node_id;
|
|
758
|
-
return typeof nodeId === "string" && nodeId.trim() ? nodeId : null;
|
|
759
|
-
}
|
|
760
329
|
_extractIssueContext(context) {
|
|
761
330
|
if (!("repository" in context.payload) || !context.payload.repository?.owner?.login) {
|
|
762
331
|
return null;
|
|
@@ -770,86 +339,6 @@ var CommentHandler = class _CommentHandler {
|
|
|
770
339
|
repo: context.payload.repository.name
|
|
771
340
|
};
|
|
772
341
|
}
|
|
773
|
-
_extractIssueLocator(context) {
|
|
774
|
-
if (!("issue" in context.payload) && !("pull_request" in context.payload)) {
|
|
775
|
-
return null;
|
|
776
|
-
}
|
|
777
|
-
const issueContext = this._extractIssueContext(context);
|
|
778
|
-
if (!issueContext) return null;
|
|
779
|
-
return {
|
|
780
|
-
owner: issueContext.owner,
|
|
781
|
-
repo: issueContext.repo,
|
|
782
|
-
issueNumber: issueContext.issueNumber
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
_shouldApplyCommandResponsePolicy(context) {
|
|
786
|
-
const payload = context;
|
|
787
|
-
return Boolean(payload.command);
|
|
788
|
-
}
|
|
789
|
-
_isCommandResponseComment(body) {
|
|
790
|
-
return typeof body === "string" && body.includes(COMMAND_RESPONSE_MARKER);
|
|
791
|
-
}
|
|
792
|
-
_getGraphqlClient(context) {
|
|
793
|
-
const graphql = context.octokit.graphql;
|
|
794
|
-
return typeof graphql === "function" ? graphql : null;
|
|
795
|
-
}
|
|
796
|
-
async _fetchRecentComments(context, locator, last = COMMAND_RESPONSE_COMMENT_LIMIT) {
|
|
797
|
-
const graphql = this._getGraphqlClient(context);
|
|
798
|
-
if (!graphql) return [];
|
|
799
|
-
try {
|
|
800
|
-
const data = await graphql(RECENT_COMMENTS_QUERY, {
|
|
801
|
-
owner: locator.owner,
|
|
802
|
-
repo: locator.repo,
|
|
803
|
-
number: locator.issueNumber,
|
|
804
|
-
last
|
|
805
|
-
});
|
|
806
|
-
const nodes = data.repository?.issueOrPullRequest?.comments?.nodes ?? [];
|
|
807
|
-
return nodes.filter((node) => Boolean(node));
|
|
808
|
-
} catch (error) {
|
|
809
|
-
context.logger.debug("Failed to fetch recent comments (non-fatal)", { err: error });
|
|
810
|
-
return [];
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
_findPreviousCommandResponseComment(comments, currentCommentId) {
|
|
814
|
-
for (let idx = comments.length - 1; idx >= 0; idx -= 1) {
|
|
815
|
-
const comment = comments[idx];
|
|
816
|
-
if (!comment) continue;
|
|
817
|
-
if (currentCommentId && comment.id === currentCommentId) continue;
|
|
818
|
-
if (this._isCommandResponseComment(comment.body)) {
|
|
819
|
-
return comment;
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
return null;
|
|
823
|
-
}
|
|
824
|
-
async _minimizeComment(context, commentNodeId, classifier = "RESOLVED") {
|
|
825
|
-
const graphql = this._getGraphqlClient(context);
|
|
826
|
-
if (!graphql) return;
|
|
827
|
-
try {
|
|
828
|
-
await graphql(MINIMIZE_COMMENT_MUTATION, {
|
|
829
|
-
id: commentNodeId,
|
|
830
|
-
classifier
|
|
831
|
-
});
|
|
832
|
-
} catch (error) {
|
|
833
|
-
context.logger.debug("Failed to minimize comment (non-fatal)", { err: error, commentNodeId });
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
async _applyCommandResponsePolicy(context) {
|
|
837
|
-
if (this._commandResponsePolicyApplied) return;
|
|
838
|
-
this._commandResponsePolicyApplied = true;
|
|
839
|
-
if (!this._shouldApplyCommandResponsePolicy(context)) return;
|
|
840
|
-
const locator = this._extractIssueLocator(context);
|
|
841
|
-
const commentNodeId = this._getCommentNodeId(context);
|
|
842
|
-
const comments = locator ? await this._fetchRecentComments(context, locator) : [];
|
|
843
|
-
const current = commentNodeId ? comments.find((comment) => comment.id === commentNodeId) : null;
|
|
844
|
-
const currentIsMinimized = current?.isMinimized ?? false;
|
|
845
|
-
if (commentNodeId && !currentIsMinimized) {
|
|
846
|
-
await this._minimizeComment(context, commentNodeId);
|
|
847
|
-
}
|
|
848
|
-
const previous = this._findPreviousCommandResponseComment(comments, commentNodeId);
|
|
849
|
-
if (previous && !previous.isMinimized) {
|
|
850
|
-
await this._minimizeComment(context, previous.id);
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
342
|
_processMessage(context, message) {
|
|
854
343
|
if (message instanceof Error) {
|
|
855
344
|
const metadata2 = {
|
|
@@ -901,9 +390,7 @@ var CommentHandler = class _CommentHandler {
|
|
|
901
390
|
}
|
|
902
391
|
_createCommentBody(context, message, options) {
|
|
903
392
|
const { metadata, logMessage } = this._processMessage(context, message);
|
|
904
|
-
const
|
|
905
|
-
const metadataWithKind = shouldTagCommandResponse ? { ...metadata, commentKind: options?.commentKind } : metadata;
|
|
906
|
-
const { header, jsonPretty } = this._createMetadataContent(context, metadataWithKind);
|
|
393
|
+
const { header, jsonPretty } = this._createMetadataContent(context, metadata);
|
|
907
394
|
const metadataContent = this._formatMetadataContent(logMessage, header, jsonPretty);
|
|
908
395
|
return `${options?.raw ? logMessage?.raw : logMessage?.diff}
|
|
909
396
|
|
|
@@ -911,17 +398,12 @@ ${metadataContent}
|
|
|
911
398
|
`;
|
|
912
399
|
}
|
|
913
400
|
async postComment(context, message, options = { updateComment: true, raw: false }) {
|
|
914
|
-
await this._applyCommandResponsePolicy(context);
|
|
915
401
|
const issueContext = this._extractIssueContext(context);
|
|
916
402
|
if (!issueContext) {
|
|
917
403
|
context.logger.warn("Cannot post comment: missing issue context in payload");
|
|
918
404
|
return null;
|
|
919
405
|
}
|
|
920
|
-
const
|
|
921
|
-
const body = this._createCommentBody(context, message, {
|
|
922
|
-
...options,
|
|
923
|
-
commentKind: options.commentKind ?? (shouldTagCommandResponse ? COMMAND_RESPONSE_KIND : void 0)
|
|
924
|
-
});
|
|
406
|
+
const body = this._createCommentBody(context, message, options);
|
|
925
407
|
const { issueNumber, commentId, owner, repo } = issueContext;
|
|
926
408
|
const params = { owner, repo, body, issueNumber };
|
|
927
409
|
if (options.updateComment) {
|
|
@@ -968,169 +450,7 @@ function decompressString(compressed) {
|
|
|
968
450
|
|
|
969
451
|
// src/octokit.ts
|
|
970
452
|
var import_core = require("@octokit/core");
|
|
971
|
-
|
|
972
|
-
// ../../node_modules/@octokit/plugin-paginate-graphql/dist-bundle/index.js
|
|
973
|
-
var generateMessage = (path, cursorValue) => `The cursor at "${path.join(
|
|
974
|
-
","
|
|
975
|
-
)}" did not change its value "${cursorValue}" after a page transition. Please make sure your that your query is set up correctly.`;
|
|
976
|
-
var MissingCursorChange = class extends Error {
|
|
977
|
-
constructor(pageInfo, cursorValue) {
|
|
978
|
-
super(generateMessage(pageInfo.pathInQuery, cursorValue));
|
|
979
|
-
this.pageInfo = pageInfo;
|
|
980
|
-
this.cursorValue = cursorValue;
|
|
981
|
-
if (Error.captureStackTrace) {
|
|
982
|
-
Error.captureStackTrace(this, this.constructor);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
name = "MissingCursorChangeError";
|
|
986
|
-
};
|
|
987
|
-
var MissingPageInfo = class extends Error {
|
|
988
|
-
constructor(response) {
|
|
989
|
-
super(
|
|
990
|
-
`No pageInfo property found in response. Please make sure to specify the pageInfo in your query. Response-Data: ${JSON.stringify(
|
|
991
|
-
response,
|
|
992
|
-
null,
|
|
993
|
-
2
|
|
994
|
-
)}`
|
|
995
|
-
);
|
|
996
|
-
this.response = response;
|
|
997
|
-
if (Error.captureStackTrace) {
|
|
998
|
-
Error.captureStackTrace(this, this.constructor);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
name = "MissingPageInfo";
|
|
1002
|
-
};
|
|
1003
|
-
var isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
|
|
1004
|
-
function findPaginatedResourcePath(responseData) {
|
|
1005
|
-
const paginatedResourcePath = deepFindPathToProperty(
|
|
1006
|
-
responseData,
|
|
1007
|
-
"pageInfo"
|
|
1008
|
-
);
|
|
1009
|
-
if (paginatedResourcePath.length === 0) {
|
|
1010
|
-
throw new MissingPageInfo(responseData);
|
|
1011
|
-
}
|
|
1012
|
-
return paginatedResourcePath;
|
|
1013
|
-
}
|
|
1014
|
-
var deepFindPathToProperty = (object, searchProp, path = []) => {
|
|
1015
|
-
for (const key of Object.keys(object)) {
|
|
1016
|
-
const currentPath = [...path, key];
|
|
1017
|
-
const currentValue = object[key];
|
|
1018
|
-
if (isObject(currentValue)) {
|
|
1019
|
-
if (currentValue.hasOwnProperty(searchProp)) {
|
|
1020
|
-
return currentPath;
|
|
1021
|
-
}
|
|
1022
|
-
const result = deepFindPathToProperty(
|
|
1023
|
-
currentValue,
|
|
1024
|
-
searchProp,
|
|
1025
|
-
currentPath
|
|
1026
|
-
);
|
|
1027
|
-
if (result.length > 0) {
|
|
1028
|
-
return result;
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
return [];
|
|
1033
|
-
};
|
|
1034
|
-
var get = (object, path) => {
|
|
1035
|
-
return path.reduce((current, nextProperty) => current[nextProperty], object);
|
|
1036
|
-
};
|
|
1037
|
-
var set = (object, path, mutator) => {
|
|
1038
|
-
const lastProperty = path[path.length - 1];
|
|
1039
|
-
const parentPath = [...path].slice(0, -1);
|
|
1040
|
-
const parent = get(object, parentPath);
|
|
1041
|
-
if (typeof mutator === "function") {
|
|
1042
|
-
parent[lastProperty] = mutator(parent[lastProperty]);
|
|
1043
|
-
} else {
|
|
1044
|
-
parent[lastProperty] = mutator;
|
|
1045
|
-
}
|
|
1046
|
-
};
|
|
1047
|
-
var extractPageInfos = (responseData) => {
|
|
1048
|
-
const pageInfoPath = findPaginatedResourcePath(responseData);
|
|
1049
|
-
return {
|
|
1050
|
-
pathInQuery: pageInfoPath,
|
|
1051
|
-
pageInfo: get(responseData, [...pageInfoPath, "pageInfo"])
|
|
1052
|
-
};
|
|
1053
|
-
};
|
|
1054
|
-
var isForwardSearch = (givenPageInfo) => {
|
|
1055
|
-
return givenPageInfo.hasOwnProperty("hasNextPage");
|
|
1056
|
-
};
|
|
1057
|
-
var getCursorFrom = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.endCursor : pageInfo.startCursor;
|
|
1058
|
-
var hasAnotherPage = (pageInfo) => isForwardSearch(pageInfo) ? pageInfo.hasNextPage : pageInfo.hasPreviousPage;
|
|
1059
|
-
var createIterator = (octokit) => {
|
|
1060
|
-
return (query, initialParameters = {}) => {
|
|
1061
|
-
let nextPageExists = true;
|
|
1062
|
-
let parameters = { ...initialParameters };
|
|
1063
|
-
return {
|
|
1064
|
-
[Symbol.asyncIterator]: () => ({
|
|
1065
|
-
async next() {
|
|
1066
|
-
if (!nextPageExists) return { done: true, value: {} };
|
|
1067
|
-
const response = await octokit.graphql(
|
|
1068
|
-
query,
|
|
1069
|
-
parameters
|
|
1070
|
-
);
|
|
1071
|
-
const pageInfoContext = extractPageInfos(response);
|
|
1072
|
-
const nextCursorValue = getCursorFrom(pageInfoContext.pageInfo);
|
|
1073
|
-
nextPageExists = hasAnotherPage(pageInfoContext.pageInfo);
|
|
1074
|
-
if (nextPageExists && nextCursorValue === parameters.cursor) {
|
|
1075
|
-
throw new MissingCursorChange(pageInfoContext, nextCursorValue);
|
|
1076
|
-
}
|
|
1077
|
-
parameters = {
|
|
1078
|
-
...parameters,
|
|
1079
|
-
cursor: nextCursorValue
|
|
1080
|
-
};
|
|
1081
|
-
return { done: false, value: response };
|
|
1082
|
-
}
|
|
1083
|
-
})
|
|
1084
|
-
};
|
|
1085
|
-
};
|
|
1086
|
-
};
|
|
1087
|
-
var mergeResponses = (response1, response2) => {
|
|
1088
|
-
if (Object.keys(response1).length === 0) {
|
|
1089
|
-
return Object.assign(response1, response2);
|
|
1090
|
-
}
|
|
1091
|
-
const path = findPaginatedResourcePath(response1);
|
|
1092
|
-
const nodesPath = [...path, "nodes"];
|
|
1093
|
-
const newNodes = get(response2, nodesPath);
|
|
1094
|
-
if (newNodes) {
|
|
1095
|
-
set(response1, nodesPath, (values) => {
|
|
1096
|
-
return [...values, ...newNodes];
|
|
1097
|
-
});
|
|
1098
|
-
}
|
|
1099
|
-
const edgesPath = [...path, "edges"];
|
|
1100
|
-
const newEdges = get(response2, edgesPath);
|
|
1101
|
-
if (newEdges) {
|
|
1102
|
-
set(response1, edgesPath, (values) => {
|
|
1103
|
-
return [...values, ...newEdges];
|
|
1104
|
-
});
|
|
1105
|
-
}
|
|
1106
|
-
const pageInfoPath = [...path, "pageInfo"];
|
|
1107
|
-
set(response1, pageInfoPath, get(response2, pageInfoPath));
|
|
1108
|
-
return response1;
|
|
1109
|
-
};
|
|
1110
|
-
var createPaginate = (octokit) => {
|
|
1111
|
-
const iterator = createIterator(octokit);
|
|
1112
|
-
return async (query, initialParameters = {}) => {
|
|
1113
|
-
let mergedResponse = {};
|
|
1114
|
-
for await (const response of iterator(
|
|
1115
|
-
query,
|
|
1116
|
-
initialParameters
|
|
1117
|
-
)) {
|
|
1118
|
-
mergedResponse = mergeResponses(mergedResponse, response);
|
|
1119
|
-
}
|
|
1120
|
-
return mergedResponse;
|
|
1121
|
-
};
|
|
1122
|
-
};
|
|
1123
|
-
function paginateGraphQL(octokit) {
|
|
1124
|
-
return {
|
|
1125
|
-
graphql: Object.assign(octokit.graphql, {
|
|
1126
|
-
paginate: Object.assign(createPaginate(octokit), {
|
|
1127
|
-
iterator: createIterator(octokit)
|
|
1128
|
-
})
|
|
1129
|
-
})
|
|
1130
|
-
};
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
// src/octokit.ts
|
|
453
|
+
var import_plugin_paginate_graphql = require("@octokit/plugin-paginate-graphql");
|
|
1134
454
|
var import_plugin_paginate_rest = require("@octokit/plugin-paginate-rest");
|
|
1135
455
|
var import_plugin_rest_endpoint_methods = require("@octokit/plugin-rest-endpoint-methods");
|
|
1136
456
|
var import_plugin_retry = require("@octokit/plugin-retry");
|
|
@@ -1151,7 +471,7 @@ var defaultOptions = {
|
|
|
1151
471
|
}
|
|
1152
472
|
}
|
|
1153
473
|
};
|
|
1154
|
-
var customOctokit = import_core.Octokit.plugin(import_plugin_throttling.throttling, import_plugin_retry.retry, import_plugin_paginate_rest.paginateRest, import_plugin_rest_endpoint_methods.restEndpointMethods, paginateGraphQL).defaults((instanceOptions) => {
|
|
474
|
+
var customOctokit = import_core.Octokit.plugin(import_plugin_throttling.throttling, import_plugin_retry.retry, import_plugin_paginate_rest.paginateRest, import_plugin_rest_endpoint_methods.restEndpointMethods, import_plugin_paginate_graphql.paginateGraphQL).defaults((instanceOptions) => {
|
|
1155
475
|
return { ...defaultOptions, ...instanceOptions };
|
|
1156
476
|
});
|
|
1157
477
|
|
|
@@ -1262,17 +582,17 @@ async function createActionsPlugin(handler, options) {
|
|
|
1262
582
|
} else {
|
|
1263
583
|
config2 = inputs.settings;
|
|
1264
584
|
}
|
|
1265
|
-
let
|
|
585
|
+
let env;
|
|
1266
586
|
if (pluginOptions.envSchema) {
|
|
1267
587
|
try {
|
|
1268
|
-
|
|
588
|
+
env = import_value3.Value.Decode(pluginOptions.envSchema, import_value3.Value.Default(pluginOptions.envSchema, process.env));
|
|
1269
589
|
} catch (e) {
|
|
1270
590
|
console.dir(...import_value3.Value.Errors(pluginOptions.envSchema, process.env), { depth: null });
|
|
1271
591
|
core.setFailed(`Error: Invalid environment provided.`);
|
|
1272
592
|
throw e;
|
|
1273
593
|
}
|
|
1274
594
|
} else {
|
|
1275
|
-
|
|
595
|
+
env = process.env;
|
|
1276
596
|
}
|
|
1277
597
|
const command = getCommand(inputs, pluginOptions);
|
|
1278
598
|
const context = {
|
|
@@ -1283,8 +603,8 @@ async function createActionsPlugin(handler, options) {
|
|
|
1283
603
|
ubiquityKernelToken: inputs.ubiquityKernelToken,
|
|
1284
604
|
octokit: new customOctokit({ auth: inputs.authToken }),
|
|
1285
605
|
config: config2,
|
|
1286
|
-
env
|
|
1287
|
-
logger: new Logs(pluginOptions.logLevel),
|
|
606
|
+
env,
|
|
607
|
+
logger: new import_ubiquity_os_logger3.Logs(pluginOptions.logLevel),
|
|
1288
608
|
commentHandler: new CommentHandler()
|
|
1289
609
|
};
|
|
1290
610
|
try {
|
|
@@ -1319,12 +639,12 @@ function cleanMarkdown(md, options = {}) {
|
|
|
1319
639
|
const segments = [];
|
|
1320
640
|
let lastIndex = 0;
|
|
1321
641
|
const matches = [...md.matchAll(codeBlockRegex)];
|
|
1322
|
-
for (const
|
|
1323
|
-
if (
|
|
1324
|
-
segments.push(processSegment(md.slice(lastIndex,
|
|
642
|
+
for (const match of matches) {
|
|
643
|
+
if (match.index > lastIndex) {
|
|
644
|
+
segments.push(processSegment(md.slice(lastIndex, match.index), tags, shouldCollapseEmptyLines));
|
|
1325
645
|
}
|
|
1326
|
-
segments.push(
|
|
1327
|
-
lastIndex =
|
|
646
|
+
segments.push(match[0]);
|
|
647
|
+
lastIndex = match.index + match[0].length;
|
|
1328
648
|
}
|
|
1329
649
|
if (lastIndex < md.length) {
|
|
1330
650
|
segments.push(processSegment(md.slice(lastIndex), tags, shouldCollapseEmptyLines));
|
|
@@ -1339,9 +659,9 @@ function processSegment(segment, extraTags, shouldCollapseEmptyLines) {
|
|
|
1339
659
|
return `__INLINE_CODE_${inlineCodes.length - 1}__`;
|
|
1340
660
|
});
|
|
1341
661
|
s = s.replace(/<!--[\s\S]*?-->/g, "");
|
|
1342
|
-
for (const
|
|
1343
|
-
if (!
|
|
1344
|
-
const tag =
|
|
662
|
+
for (const raw of extraTags) {
|
|
663
|
+
if (!raw) continue;
|
|
664
|
+
const tag = raw.toLowerCase().trim().replace(/[^\w:-]/g, "");
|
|
1345
665
|
if (!tag) continue;
|
|
1346
666
|
if (VOID_TAGS.has(tag)) {
|
|
1347
667
|
const voidRe = new RegExp(`<${tag}\\b[^>]*\\/?>`, "gi");
|
|
@@ -1366,2154 +686,93 @@ function processSegment(segment, extraTags, shouldCollapseEmptyLines) {
|
|
|
1366
686
|
|
|
1367
687
|
// src/server.ts
|
|
1368
688
|
var import_value4 = require("@sinclair/typebox/value");
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
var
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
}
|
|
1379
|
-
index = i;
|
|
1380
|
-
let res;
|
|
1381
|
-
let isError = false;
|
|
1382
|
-
let handler;
|
|
1383
|
-
if (middleware[i]) {
|
|
1384
|
-
handler = middleware[i][0][0];
|
|
1385
|
-
context.req.routeIndex = i;
|
|
1386
|
-
} else {
|
|
1387
|
-
handler = i === middleware.length && next || void 0;
|
|
1388
|
-
}
|
|
1389
|
-
if (handler) {
|
|
1390
|
-
try {
|
|
1391
|
-
res = await handler(context, () => dispatch(i + 1));
|
|
1392
|
-
} catch (err) {
|
|
1393
|
-
if (err instanceof Error && onError) {
|
|
1394
|
-
context.error = err;
|
|
1395
|
-
res = await onError(err, context);
|
|
1396
|
-
isError = true;
|
|
1397
|
-
} else {
|
|
1398
|
-
throw err;
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
} else {
|
|
1402
|
-
if (context.finalized === false && onNotFound) {
|
|
1403
|
-
res = await onNotFound(context);
|
|
1404
|
-
}
|
|
1405
|
-
}
|
|
1406
|
-
if (res && (context.finalized === false || isError)) {
|
|
1407
|
-
context.res = res;
|
|
1408
|
-
}
|
|
1409
|
-
return context;
|
|
1410
|
-
}
|
|
1411
|
-
};
|
|
1412
|
-
};
|
|
1413
|
-
|
|
1414
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/http-exception.js
|
|
1415
|
-
var HTTPException = class extends Error {
|
|
1416
|
-
res;
|
|
1417
|
-
status;
|
|
1418
|
-
/**
|
|
1419
|
-
* Creates an instance of `HTTPException`.
|
|
1420
|
-
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
1421
|
-
* @param options - Additional options for the exception.
|
|
1422
|
-
*/
|
|
1423
|
-
constructor(status = 500, options) {
|
|
1424
|
-
super(options?.message, { cause: options?.cause });
|
|
1425
|
-
this.res = options?.res;
|
|
1426
|
-
this.status = status;
|
|
1427
|
-
}
|
|
1428
|
-
/**
|
|
1429
|
-
* Returns the response object associated with the exception.
|
|
1430
|
-
* If a response object is not provided, a new response is created with the error message and status code.
|
|
1431
|
-
* @returns The response object.
|
|
1432
|
-
*/
|
|
1433
|
-
getResponse() {
|
|
1434
|
-
if (this.res) {
|
|
1435
|
-
const newResponse = new Response(this.res.body, {
|
|
1436
|
-
status: this.status,
|
|
1437
|
-
headers: this.res.headers
|
|
1438
|
-
});
|
|
1439
|
-
return newResponse;
|
|
1440
|
-
}
|
|
1441
|
-
return new Response(this.message, {
|
|
1442
|
-
status: this.status
|
|
1443
|
-
});
|
|
1444
|
-
}
|
|
1445
|
-
};
|
|
1446
|
-
|
|
1447
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/request/constants.js
|
|
1448
|
-
var GET_MATCH_RESULT = /* @__PURE__ */ Symbol();
|
|
1449
|
-
|
|
1450
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/utils/body.js
|
|
1451
|
-
var parseBody = async (request, options = /* @__PURE__ */ Object.create(null)) => {
|
|
1452
|
-
const { all = false, dot = false } = options;
|
|
1453
|
-
const headers = request instanceof HonoRequest ? request.raw.headers : request.headers;
|
|
1454
|
-
const contentType = headers.get("Content-Type");
|
|
1455
|
-
if (contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded")) {
|
|
1456
|
-
return parseFormData(request, { all, dot });
|
|
1457
|
-
}
|
|
1458
|
-
return {};
|
|
1459
|
-
};
|
|
1460
|
-
async function parseFormData(request, options) {
|
|
1461
|
-
const formData = await request.formData();
|
|
1462
|
-
if (formData) {
|
|
1463
|
-
return convertFormDataToBodyData(formData, options);
|
|
689
|
+
var import_ubiquity_os_logger4 = require("@ubiquity-os/ubiquity-os-logger");
|
|
690
|
+
var import_hono = require("hono");
|
|
691
|
+
var import_adapter2 = require("hono/adapter");
|
|
692
|
+
var import_http_exception = require("hono/http-exception");
|
|
693
|
+
async function handleError2(context, pluginOptions, error) {
|
|
694
|
+
console.error(error);
|
|
695
|
+
const loggerError = transformError(context, error);
|
|
696
|
+
if (pluginOptions.postCommentOnError && loggerError) {
|
|
697
|
+
await context.commentHandler.postComment(context, loggerError);
|
|
1464
698
|
}
|
|
1465
|
-
|
|
699
|
+
throw new import_http_exception.HTTPException(500, { message: "Unexpected error" });
|
|
1466
700
|
}
|
|
1467
|
-
function
|
|
1468
|
-
const
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
form[key] = value;
|
|
1473
|
-
} else {
|
|
1474
|
-
handleParsingAllValues(form, key, value);
|
|
1475
|
-
}
|
|
701
|
+
function createPlugin(handler, manifest, options) {
|
|
702
|
+
const pluginOptions = getPluginOptions(options);
|
|
703
|
+
const app = new import_hono.Hono();
|
|
704
|
+
app.get("/manifest.json", (ctx) => {
|
|
705
|
+
return ctx.json(manifest);
|
|
1476
706
|
});
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
707
|
+
app.post("/", async function appPost(ctx) {
|
|
708
|
+
if (ctx.req.header("content-type") !== "application/json") {
|
|
709
|
+
throw new import_http_exception.HTTPException(400, { message: "Content-Type must be application/json" });
|
|
710
|
+
}
|
|
711
|
+
const body = await ctx.req.json();
|
|
712
|
+
const inputSchemaErrors = [...import_value4.Value.Errors(inputSchema, body)];
|
|
713
|
+
if (inputSchemaErrors.length) {
|
|
714
|
+
console.dir(inputSchemaErrors, { depth: null });
|
|
715
|
+
throw new import_http_exception.HTTPException(400, { message: "Invalid body" });
|
|
716
|
+
}
|
|
717
|
+
const signature = body.signature;
|
|
718
|
+
if (!pluginOptions.bypassSignatureVerification && !await verifySignature(pluginOptions.kernelPublicKey, body, signature)) {
|
|
719
|
+
throw new import_http_exception.HTTPException(400, { message: "Invalid signature" });
|
|
720
|
+
}
|
|
721
|
+
const inputs = import_value4.Value.Decode(inputSchema, body);
|
|
722
|
+
let config2;
|
|
723
|
+
if (pluginOptions.settingsSchema) {
|
|
724
|
+
try {
|
|
725
|
+
config2 = import_value4.Value.Decode(pluginOptions.settingsSchema, import_value4.Value.Default(pluginOptions.settingsSchema, inputs.settings));
|
|
726
|
+
} catch (e) {
|
|
727
|
+
console.dir(...import_value4.Value.Errors(pluginOptions.settingsSchema, inputs.settings), { depth: null });
|
|
728
|
+
throw e;
|
|
1483
729
|
}
|
|
1484
|
-
});
|
|
1485
|
-
}
|
|
1486
|
-
return form;
|
|
1487
|
-
}
|
|
1488
|
-
var handleParsingAllValues = (form, key, value) => {
|
|
1489
|
-
if (form[key] !== void 0) {
|
|
1490
|
-
if (Array.isArray(form[key])) {
|
|
1491
|
-
;
|
|
1492
|
-
form[key].push(value);
|
|
1493
730
|
} else {
|
|
1494
|
-
|
|
731
|
+
config2 = inputs.settings;
|
|
1495
732
|
}
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
733
|
+
let env;
|
|
734
|
+
const honoEnvironment = (0, import_adapter2.env)(ctx);
|
|
735
|
+
if (pluginOptions.envSchema) {
|
|
736
|
+
try {
|
|
737
|
+
env = import_value4.Value.Decode(pluginOptions.envSchema, import_value4.Value.Default(pluginOptions.envSchema, honoEnvironment));
|
|
738
|
+
} catch (e) {
|
|
739
|
+
console.dir(...import_value4.Value.Errors(pluginOptions.envSchema, honoEnvironment), { depth: null });
|
|
740
|
+
throw e;
|
|
741
|
+
}
|
|
1499
742
|
} else {
|
|
1500
|
-
|
|
743
|
+
env = ctx.env;
|
|
1501
744
|
}
|
|
1502
|
-
|
|
1503
|
-
};
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
745
|
+
const workerName = new URL(inputs.ref).hostname.split(".")[0];
|
|
746
|
+
PluginRuntimeInfo.getInstance({ ...env, CLOUDFLARE_WORKER_NAME: workerName });
|
|
747
|
+
const command = getCommand(inputs, pluginOptions);
|
|
748
|
+
const context = {
|
|
749
|
+
eventName: inputs.eventName,
|
|
750
|
+
payload: inputs.eventPayload,
|
|
751
|
+
command,
|
|
752
|
+
authToken: inputs.authToken,
|
|
753
|
+
ubiquityKernelToken: inputs.ubiquityKernelToken,
|
|
754
|
+
octokit: new customOctokit({ auth: inputs.authToken }),
|
|
755
|
+
config: config2,
|
|
756
|
+
env,
|
|
757
|
+
logger: new import_ubiquity_os_logger4.Logs(pluginOptions.logLevel),
|
|
758
|
+
commentHandler: new CommentHandler()
|
|
759
|
+
};
|
|
760
|
+
try {
|
|
761
|
+
const result = await handler(context);
|
|
762
|
+
return ctx.json({ stateId: inputs.stateId, output: result ?? {} });
|
|
763
|
+
} catch (error) {
|
|
764
|
+
await handleError2(context, pluginOptions, error);
|
|
1515
765
|
}
|
|
1516
766
|
});
|
|
1517
|
-
|
|
767
|
+
return app;
|
|
768
|
+
}
|
|
1518
769
|
|
|
1519
|
-
//
|
|
1520
|
-
var
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
return paths;
|
|
1526
|
-
};
|
|
1527
|
-
var splitRoutingPath = (routePath) => {
|
|
1528
|
-
const { groups, path } = extractGroupsFromPath(routePath);
|
|
1529
|
-
const paths = splitPath(path);
|
|
1530
|
-
return replaceGroupMarks(paths, groups);
|
|
1531
|
-
};
|
|
1532
|
-
var extractGroupsFromPath = (path) => {
|
|
1533
|
-
const groups = [];
|
|
1534
|
-
path = path.replace(/\{[^}]+\}/g, (match2, index) => {
|
|
1535
|
-
const mark = `@${index}`;
|
|
1536
|
-
groups.push([mark, match2]);
|
|
1537
|
-
return mark;
|
|
1538
|
-
});
|
|
1539
|
-
return { groups, path };
|
|
1540
|
-
};
|
|
1541
|
-
var replaceGroupMarks = (paths, groups) => {
|
|
1542
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1543
|
-
const [mark] = groups[i];
|
|
1544
|
-
for (let j = paths.length - 1; j >= 0; j--) {
|
|
1545
|
-
if (paths[j].includes(mark)) {
|
|
1546
|
-
paths[j] = paths[j].replace(mark, groups[i][1]);
|
|
1547
|
-
break;
|
|
1548
|
-
}
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
return paths;
|
|
1552
|
-
};
|
|
1553
|
-
var patternCache = {};
|
|
1554
|
-
var getPattern = (label, next) => {
|
|
1555
|
-
if (label === "*") {
|
|
1556
|
-
return "*";
|
|
1557
|
-
}
|
|
1558
|
-
const match2 = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
1559
|
-
if (match2) {
|
|
1560
|
-
const cacheKey = `${label}#${next}`;
|
|
1561
|
-
if (!patternCache[cacheKey]) {
|
|
1562
|
-
if (match2[2]) {
|
|
1563
|
-
patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [cacheKey, match2[1], new RegExp(`^${match2[2]}(?=/${next})`)] : [label, match2[1], new RegExp(`^${match2[2]}$`)];
|
|
1564
|
-
} else {
|
|
1565
|
-
patternCache[cacheKey] = [label, match2[1], true];
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
return patternCache[cacheKey];
|
|
1569
|
-
}
|
|
1570
|
-
return null;
|
|
1571
|
-
};
|
|
1572
|
-
var tryDecode = (str, decoder) => {
|
|
1573
|
-
try {
|
|
1574
|
-
return decoder(str);
|
|
1575
|
-
} catch {
|
|
1576
|
-
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match2) => {
|
|
1577
|
-
try {
|
|
1578
|
-
return decoder(match2);
|
|
1579
|
-
} catch {
|
|
1580
|
-
return match2;
|
|
1581
|
-
}
|
|
1582
|
-
});
|
|
1583
|
-
}
|
|
1584
|
-
};
|
|
1585
|
-
var tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
1586
|
-
var getPath = (request) => {
|
|
1587
|
-
const url = request.url;
|
|
1588
|
-
const start = url.indexOf("/", url.indexOf(":") + 4);
|
|
1589
|
-
let i = start;
|
|
1590
|
-
for (; i < url.length; i++) {
|
|
1591
|
-
const charCode = url.charCodeAt(i);
|
|
1592
|
-
if (charCode === 37) {
|
|
1593
|
-
const queryIndex = url.indexOf("?", i);
|
|
1594
|
-
const path = url.slice(start, queryIndex === -1 ? void 0 : queryIndex);
|
|
1595
|
-
return tryDecodeURI(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
|
|
1596
|
-
} else if (charCode === 63) {
|
|
1597
|
-
break;
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
return url.slice(start, i);
|
|
1601
|
-
};
|
|
1602
|
-
var getPathNoStrict = (request) => {
|
|
1603
|
-
const result = getPath(request);
|
|
1604
|
-
return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
|
|
1605
|
-
};
|
|
1606
|
-
var mergePath = (base, sub, ...rest) => {
|
|
1607
|
-
if (rest.length) {
|
|
1608
|
-
sub = mergePath(sub, ...rest);
|
|
1609
|
-
}
|
|
1610
|
-
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
1611
|
-
};
|
|
1612
|
-
var checkOptionalParameter = (path) => {
|
|
1613
|
-
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
1614
|
-
return null;
|
|
1615
|
-
}
|
|
1616
|
-
const segments = path.split("/");
|
|
1617
|
-
const results = [];
|
|
1618
|
-
let basePath = "";
|
|
1619
|
-
segments.forEach((segment) => {
|
|
1620
|
-
if (segment !== "" && !/\:/.test(segment)) {
|
|
1621
|
-
basePath += "/" + segment;
|
|
1622
|
-
} else if (/\:/.test(segment)) {
|
|
1623
|
-
if (/\?/.test(segment)) {
|
|
1624
|
-
if (results.length === 0 && basePath === "") {
|
|
1625
|
-
results.push("/");
|
|
1626
|
-
} else {
|
|
1627
|
-
results.push(basePath);
|
|
1628
|
-
}
|
|
1629
|
-
const optionalSegment = segment.replace("?", "");
|
|
1630
|
-
basePath += "/" + optionalSegment;
|
|
1631
|
-
results.push(basePath);
|
|
1632
|
-
} else {
|
|
1633
|
-
basePath += "/" + segment;
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
});
|
|
1637
|
-
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
1638
|
-
};
|
|
1639
|
-
var _decodeURI = (value) => {
|
|
1640
|
-
if (!/[%+]/.test(value)) {
|
|
1641
|
-
return value;
|
|
1642
|
-
}
|
|
1643
|
-
if (value.indexOf("+") !== -1) {
|
|
1644
|
-
value = value.replace(/\+/g, " ");
|
|
1645
|
-
}
|
|
1646
|
-
return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
|
|
1647
|
-
};
|
|
1648
|
-
var _getQueryParam = (url, key, multiple) => {
|
|
1649
|
-
let encoded;
|
|
1650
|
-
if (!multiple && key && !/[%+]/.test(key)) {
|
|
1651
|
-
let keyIndex2 = url.indexOf("?", 8);
|
|
1652
|
-
if (keyIndex2 === -1) {
|
|
1653
|
-
return void 0;
|
|
1654
|
-
}
|
|
1655
|
-
if (!url.startsWith(key, keyIndex2 + 1)) {
|
|
1656
|
-
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
1657
|
-
}
|
|
1658
|
-
while (keyIndex2 !== -1) {
|
|
1659
|
-
const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
|
|
1660
|
-
if (trailingKeyCode === 61) {
|
|
1661
|
-
const valueIndex = keyIndex2 + key.length + 2;
|
|
1662
|
-
const endIndex = url.indexOf("&", valueIndex);
|
|
1663
|
-
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
|
|
1664
|
-
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
|
|
1665
|
-
return "";
|
|
1666
|
-
}
|
|
1667
|
-
keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
|
|
1668
|
-
}
|
|
1669
|
-
encoded = /[%+]/.test(url);
|
|
1670
|
-
if (!encoded) {
|
|
1671
|
-
return void 0;
|
|
1672
|
-
}
|
|
1673
|
-
}
|
|
1674
|
-
const results = {};
|
|
1675
|
-
encoded ??= /[%+]/.test(url);
|
|
1676
|
-
let keyIndex = url.indexOf("?", 8);
|
|
1677
|
-
while (keyIndex !== -1) {
|
|
1678
|
-
const nextKeyIndex = url.indexOf("&", keyIndex + 1);
|
|
1679
|
-
let valueIndex = url.indexOf("=", keyIndex);
|
|
1680
|
-
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
|
|
1681
|
-
valueIndex = -1;
|
|
1682
|
-
}
|
|
1683
|
-
let name = url.slice(
|
|
1684
|
-
keyIndex + 1,
|
|
1685
|
-
valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex
|
|
1686
|
-
);
|
|
1687
|
-
if (encoded) {
|
|
1688
|
-
name = _decodeURI(name);
|
|
1689
|
-
}
|
|
1690
|
-
keyIndex = nextKeyIndex;
|
|
1691
|
-
if (name === "") {
|
|
1692
|
-
continue;
|
|
1693
|
-
}
|
|
1694
|
-
let value;
|
|
1695
|
-
if (valueIndex === -1) {
|
|
1696
|
-
value = "";
|
|
1697
|
-
} else {
|
|
1698
|
-
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
|
|
1699
|
-
if (encoded) {
|
|
1700
|
-
value = _decodeURI(value);
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
if (multiple) {
|
|
1704
|
-
if (!(results[name] && Array.isArray(results[name]))) {
|
|
1705
|
-
results[name] = [];
|
|
1706
|
-
}
|
|
1707
|
-
;
|
|
1708
|
-
results[name].push(value);
|
|
1709
|
-
} else {
|
|
1710
|
-
results[name] ??= value;
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
return key ? results[key] : results;
|
|
1714
|
-
};
|
|
1715
|
-
var getQueryParam = _getQueryParam;
|
|
1716
|
-
var getQueryParams = (url, key) => {
|
|
1717
|
-
return _getQueryParam(url, key, true);
|
|
1718
|
-
};
|
|
1719
|
-
var decodeURIComponent_ = decodeURIComponent;
|
|
1720
|
-
|
|
1721
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/request.js
|
|
1722
|
-
var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
|
1723
|
-
var HonoRequest = class {
|
|
1724
|
-
/**
|
|
1725
|
-
* `.raw` can get the raw Request object.
|
|
1726
|
-
*
|
|
1727
|
-
* @see {@link https://hono.dev/docs/api/request#raw}
|
|
1728
|
-
*
|
|
1729
|
-
* @example
|
|
1730
|
-
* ```ts
|
|
1731
|
-
* // For Cloudflare Workers
|
|
1732
|
-
* app.post('/', async (c) => {
|
|
1733
|
-
* const metadata = c.req.raw.cf?.hostMetadata?
|
|
1734
|
-
* ...
|
|
1735
|
-
* })
|
|
1736
|
-
* ```
|
|
1737
|
-
*/
|
|
1738
|
-
raw;
|
|
1739
|
-
#validatedData;
|
|
1740
|
-
// Short name of validatedData
|
|
1741
|
-
#matchResult;
|
|
1742
|
-
routeIndex = 0;
|
|
1743
|
-
/**
|
|
1744
|
-
* `.path` can get the pathname of the request.
|
|
1745
|
-
*
|
|
1746
|
-
* @see {@link https://hono.dev/docs/api/request#path}
|
|
1747
|
-
*
|
|
1748
|
-
* @example
|
|
1749
|
-
* ```ts
|
|
1750
|
-
* app.get('/about/me', (c) => {
|
|
1751
|
-
* const pathname = c.req.path // `/about/me`
|
|
1752
|
-
* })
|
|
1753
|
-
* ```
|
|
1754
|
-
*/
|
|
1755
|
-
path;
|
|
1756
|
-
bodyCache = {};
|
|
1757
|
-
constructor(request, path = "/", matchResult = [[]]) {
|
|
1758
|
-
this.raw = request;
|
|
1759
|
-
this.path = path;
|
|
1760
|
-
this.#matchResult = matchResult;
|
|
1761
|
-
this.#validatedData = {};
|
|
1762
|
-
}
|
|
1763
|
-
param(key) {
|
|
1764
|
-
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
1765
|
-
}
|
|
1766
|
-
#getDecodedParam(key) {
|
|
1767
|
-
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
1768
|
-
const param = this.#getParamValue(paramKey);
|
|
1769
|
-
return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param;
|
|
1770
|
-
}
|
|
1771
|
-
#getAllDecodedParams() {
|
|
1772
|
-
const decoded = {};
|
|
1773
|
-
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
1774
|
-
for (const key of keys) {
|
|
1775
|
-
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
1776
|
-
if (value !== void 0) {
|
|
1777
|
-
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
1778
|
-
}
|
|
1779
|
-
}
|
|
1780
|
-
return decoded;
|
|
1781
|
-
}
|
|
1782
|
-
#getParamValue(paramKey) {
|
|
1783
|
-
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
1784
|
-
}
|
|
1785
|
-
query(key) {
|
|
1786
|
-
return getQueryParam(this.url, key);
|
|
1787
|
-
}
|
|
1788
|
-
queries(key) {
|
|
1789
|
-
return getQueryParams(this.url, key);
|
|
1790
|
-
}
|
|
1791
|
-
header(name) {
|
|
1792
|
-
if (name) {
|
|
1793
|
-
return this.raw.headers.get(name) ?? void 0;
|
|
1794
|
-
}
|
|
1795
|
-
const headerData = {};
|
|
1796
|
-
this.raw.headers.forEach((value, key) => {
|
|
1797
|
-
headerData[key] = value;
|
|
1798
|
-
});
|
|
1799
|
-
return headerData;
|
|
1800
|
-
}
|
|
1801
|
-
async parseBody(options) {
|
|
1802
|
-
return this.bodyCache.parsedBody ??= await parseBody(this, options);
|
|
1803
|
-
}
|
|
1804
|
-
#cachedBody = (key) => {
|
|
1805
|
-
const { bodyCache, raw: raw2 } = this;
|
|
1806
|
-
const cachedBody = bodyCache[key];
|
|
1807
|
-
if (cachedBody) {
|
|
1808
|
-
return cachedBody;
|
|
1809
|
-
}
|
|
1810
|
-
const anyCachedKey = Object.keys(bodyCache)[0];
|
|
1811
|
-
if (anyCachedKey) {
|
|
1812
|
-
return bodyCache[anyCachedKey].then((body) => {
|
|
1813
|
-
if (anyCachedKey === "json") {
|
|
1814
|
-
body = JSON.stringify(body);
|
|
1815
|
-
}
|
|
1816
|
-
return new Response(body)[key]();
|
|
1817
|
-
});
|
|
1818
|
-
}
|
|
1819
|
-
return bodyCache[key] = raw2[key]();
|
|
1820
|
-
};
|
|
1821
|
-
/**
|
|
1822
|
-
* `.json()` can parse Request body of type `application/json`
|
|
1823
|
-
*
|
|
1824
|
-
* @see {@link https://hono.dev/docs/api/request#json}
|
|
1825
|
-
*
|
|
1826
|
-
* @example
|
|
1827
|
-
* ```ts
|
|
1828
|
-
* app.post('/entry', async (c) => {
|
|
1829
|
-
* const body = await c.req.json()
|
|
1830
|
-
* })
|
|
1831
|
-
* ```
|
|
1832
|
-
*/
|
|
1833
|
-
json() {
|
|
1834
|
-
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
1835
|
-
}
|
|
1836
|
-
/**
|
|
1837
|
-
* `.text()` can parse Request body of type `text/plain`
|
|
1838
|
-
*
|
|
1839
|
-
* @see {@link https://hono.dev/docs/api/request#text}
|
|
1840
|
-
*
|
|
1841
|
-
* @example
|
|
1842
|
-
* ```ts
|
|
1843
|
-
* app.post('/entry', async (c) => {
|
|
1844
|
-
* const body = await c.req.text()
|
|
1845
|
-
* })
|
|
1846
|
-
* ```
|
|
1847
|
-
*/
|
|
1848
|
-
text() {
|
|
1849
|
-
return this.#cachedBody("text");
|
|
1850
|
-
}
|
|
1851
|
-
/**
|
|
1852
|
-
* `.arrayBuffer()` parse Request body as an `ArrayBuffer`
|
|
1853
|
-
*
|
|
1854
|
-
* @see {@link https://hono.dev/docs/api/request#arraybuffer}
|
|
1855
|
-
*
|
|
1856
|
-
* @example
|
|
1857
|
-
* ```ts
|
|
1858
|
-
* app.post('/entry', async (c) => {
|
|
1859
|
-
* const body = await c.req.arrayBuffer()
|
|
1860
|
-
* })
|
|
1861
|
-
* ```
|
|
1862
|
-
*/
|
|
1863
|
-
arrayBuffer() {
|
|
1864
|
-
return this.#cachedBody("arrayBuffer");
|
|
1865
|
-
}
|
|
1866
|
-
/**
|
|
1867
|
-
* Parses the request body as a `Blob`.
|
|
1868
|
-
* @example
|
|
1869
|
-
* ```ts
|
|
1870
|
-
* app.post('/entry', async (c) => {
|
|
1871
|
-
* const body = await c.req.blob();
|
|
1872
|
-
* });
|
|
1873
|
-
* ```
|
|
1874
|
-
* @see https://hono.dev/docs/api/request#blob
|
|
1875
|
-
*/
|
|
1876
|
-
blob() {
|
|
1877
|
-
return this.#cachedBody("blob");
|
|
1878
|
-
}
|
|
1879
|
-
/**
|
|
1880
|
-
* Parses the request body as `FormData`.
|
|
1881
|
-
* @example
|
|
1882
|
-
* ```ts
|
|
1883
|
-
* app.post('/entry', async (c) => {
|
|
1884
|
-
* const body = await c.req.formData();
|
|
1885
|
-
* });
|
|
1886
|
-
* ```
|
|
1887
|
-
* @see https://hono.dev/docs/api/request#formdata
|
|
1888
|
-
*/
|
|
1889
|
-
formData() {
|
|
1890
|
-
return this.#cachedBody("formData");
|
|
1891
|
-
}
|
|
1892
|
-
/**
|
|
1893
|
-
* Adds validated data to the request.
|
|
1894
|
-
*
|
|
1895
|
-
* @param target - The target of the validation.
|
|
1896
|
-
* @param data - The validated data to add.
|
|
1897
|
-
*/
|
|
1898
|
-
addValidatedData(target, data) {
|
|
1899
|
-
this.#validatedData[target] = data;
|
|
1900
|
-
}
|
|
1901
|
-
valid(target) {
|
|
1902
|
-
return this.#validatedData[target];
|
|
1903
|
-
}
|
|
1904
|
-
/**
|
|
1905
|
-
* `.url()` can get the request url strings.
|
|
1906
|
-
*
|
|
1907
|
-
* @see {@link https://hono.dev/docs/api/request#url}
|
|
1908
|
-
*
|
|
1909
|
-
* @example
|
|
1910
|
-
* ```ts
|
|
1911
|
-
* app.get('/about/me', (c) => {
|
|
1912
|
-
* const url = c.req.url // `http://localhost:8787/about/me`
|
|
1913
|
-
* ...
|
|
1914
|
-
* })
|
|
1915
|
-
* ```
|
|
1916
|
-
*/
|
|
1917
|
-
get url() {
|
|
1918
|
-
return this.raw.url;
|
|
1919
|
-
}
|
|
1920
|
-
/**
|
|
1921
|
-
* `.method()` can get the method name of the request.
|
|
1922
|
-
*
|
|
1923
|
-
* @see {@link https://hono.dev/docs/api/request#method}
|
|
1924
|
-
*
|
|
1925
|
-
* @example
|
|
1926
|
-
* ```ts
|
|
1927
|
-
* app.get('/about/me', (c) => {
|
|
1928
|
-
* const method = c.req.method // `GET`
|
|
1929
|
-
* })
|
|
1930
|
-
* ```
|
|
1931
|
-
*/
|
|
1932
|
-
get method() {
|
|
1933
|
-
return this.raw.method;
|
|
1934
|
-
}
|
|
1935
|
-
get [GET_MATCH_RESULT]() {
|
|
1936
|
-
return this.#matchResult;
|
|
1937
|
-
}
|
|
1938
|
-
/**
|
|
1939
|
-
* `.matchedRoutes()` can return a matched route in the handler
|
|
1940
|
-
*
|
|
1941
|
-
* @deprecated
|
|
1942
|
-
*
|
|
1943
|
-
* Use matchedRoutes helper defined in "hono/route" instead.
|
|
1944
|
-
*
|
|
1945
|
-
* @see {@link https://hono.dev/docs/api/request#matchedroutes}
|
|
1946
|
-
*
|
|
1947
|
-
* @example
|
|
1948
|
-
* ```ts
|
|
1949
|
-
* app.use('*', async function logger(c, next) {
|
|
1950
|
-
* await next()
|
|
1951
|
-
* c.req.matchedRoutes.forEach(({ handler, method, path }, i) => {
|
|
1952
|
-
* const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]')
|
|
1953
|
-
* console.log(
|
|
1954
|
-
* method,
|
|
1955
|
-
* ' ',
|
|
1956
|
-
* path,
|
|
1957
|
-
* ' '.repeat(Math.max(10 - path.length, 0)),
|
|
1958
|
-
* name,
|
|
1959
|
-
* i === c.req.routeIndex ? '<- respond from here' : ''
|
|
1960
|
-
* )
|
|
1961
|
-
* })
|
|
1962
|
-
* })
|
|
1963
|
-
* ```
|
|
1964
|
-
*/
|
|
1965
|
-
get matchedRoutes() {
|
|
1966
|
-
return this.#matchResult[0].map(([[, route]]) => route);
|
|
1967
|
-
}
|
|
1968
|
-
/**
|
|
1969
|
-
* `routePath()` can retrieve the path registered within the handler
|
|
1970
|
-
*
|
|
1971
|
-
* @deprecated
|
|
1972
|
-
*
|
|
1973
|
-
* Use routePath helper defined in "hono/route" instead.
|
|
1974
|
-
*
|
|
1975
|
-
* @see {@link https://hono.dev/docs/api/request#routepath}
|
|
1976
|
-
*
|
|
1977
|
-
* @example
|
|
1978
|
-
* ```ts
|
|
1979
|
-
* app.get('/posts/:id', (c) => {
|
|
1980
|
-
* return c.json({ path: c.req.routePath })
|
|
1981
|
-
* })
|
|
1982
|
-
* ```
|
|
1983
|
-
*/
|
|
1984
|
-
get routePath() {
|
|
1985
|
-
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
1986
|
-
}
|
|
1987
|
-
};
|
|
1988
|
-
|
|
1989
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/utils/html.js
|
|
1990
|
-
var HtmlEscapedCallbackPhase = {
|
|
1991
|
-
Stringify: 1,
|
|
1992
|
-
BeforeStream: 2,
|
|
1993
|
-
Stream: 3
|
|
1994
|
-
};
|
|
1995
|
-
var raw = (value, callbacks) => {
|
|
1996
|
-
const escapedString = new String(value);
|
|
1997
|
-
escapedString.isEscaped = true;
|
|
1998
|
-
escapedString.callbacks = callbacks;
|
|
1999
|
-
return escapedString;
|
|
2000
|
-
};
|
|
2001
|
-
var resolveCallback = async (str, phase, preserveCallbacks, context, buffer) => {
|
|
2002
|
-
if (typeof str === "object" && !(str instanceof String)) {
|
|
2003
|
-
if (!(str instanceof Promise)) {
|
|
2004
|
-
str = str.toString();
|
|
2005
|
-
}
|
|
2006
|
-
if (str instanceof Promise) {
|
|
2007
|
-
str = await str;
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
const callbacks = str.callbacks;
|
|
2011
|
-
if (!callbacks?.length) {
|
|
2012
|
-
return Promise.resolve(str);
|
|
2013
|
-
}
|
|
2014
|
-
if (buffer) {
|
|
2015
|
-
buffer[0] += str;
|
|
2016
|
-
} else {
|
|
2017
|
-
buffer = [str];
|
|
2018
|
-
}
|
|
2019
|
-
const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then(
|
|
2020
|
-
(res) => Promise.all(
|
|
2021
|
-
res.filter(Boolean).map((str2) => resolveCallback(str2, phase, false, context, buffer))
|
|
2022
|
-
).then(() => buffer[0])
|
|
2023
|
-
);
|
|
2024
|
-
if (preserveCallbacks) {
|
|
2025
|
-
return raw(await resStr, callbacks);
|
|
2026
|
-
} else {
|
|
2027
|
-
return resStr;
|
|
2028
|
-
}
|
|
2029
|
-
};
|
|
2030
|
-
|
|
2031
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/context.js
|
|
2032
|
-
var TEXT_PLAIN = "text/plain; charset=UTF-8";
|
|
2033
|
-
var setDefaultContentType = (contentType, headers) => {
|
|
2034
|
-
return {
|
|
2035
|
-
"Content-Type": contentType,
|
|
2036
|
-
...headers
|
|
2037
|
-
};
|
|
2038
|
-
};
|
|
2039
|
-
var Context = class {
|
|
2040
|
-
#rawRequest;
|
|
2041
|
-
#req;
|
|
2042
|
-
/**
|
|
2043
|
-
* `.env` can get bindings (environment variables, secrets, KV namespaces, D1 database, R2 bucket etc.) in Cloudflare Workers.
|
|
2044
|
-
*
|
|
2045
|
-
* @see {@link https://hono.dev/docs/api/context#env}
|
|
2046
|
-
*
|
|
2047
|
-
* @example
|
|
2048
|
-
* ```ts
|
|
2049
|
-
* // Environment object for Cloudflare Workers
|
|
2050
|
-
* app.get('*', async c => {
|
|
2051
|
-
* const counter = c.env.COUNTER
|
|
2052
|
-
* })
|
|
2053
|
-
* ```
|
|
2054
|
-
*/
|
|
2055
|
-
env = {};
|
|
2056
|
-
#var;
|
|
2057
|
-
finalized = false;
|
|
2058
|
-
/**
|
|
2059
|
-
* `.error` can get the error object from the middleware if the Handler throws an error.
|
|
2060
|
-
*
|
|
2061
|
-
* @see {@link https://hono.dev/docs/api/context#error}
|
|
2062
|
-
*
|
|
2063
|
-
* @example
|
|
2064
|
-
* ```ts
|
|
2065
|
-
* app.use('*', async (c, next) => {
|
|
2066
|
-
* await next()
|
|
2067
|
-
* if (c.error) {
|
|
2068
|
-
* // do something...
|
|
2069
|
-
* }
|
|
2070
|
-
* })
|
|
2071
|
-
* ```
|
|
2072
|
-
*/
|
|
2073
|
-
error;
|
|
2074
|
-
#status;
|
|
2075
|
-
#executionCtx;
|
|
2076
|
-
#res;
|
|
2077
|
-
#layout;
|
|
2078
|
-
#renderer;
|
|
2079
|
-
#notFoundHandler;
|
|
2080
|
-
#preparedHeaders;
|
|
2081
|
-
#matchResult;
|
|
2082
|
-
#path;
|
|
2083
|
-
/**
|
|
2084
|
-
* Creates an instance of the Context class.
|
|
2085
|
-
*
|
|
2086
|
-
* @param req - The Request object.
|
|
2087
|
-
* @param options - Optional configuration options for the context.
|
|
2088
|
-
*/
|
|
2089
|
-
constructor(req, options) {
|
|
2090
|
-
this.#rawRequest = req;
|
|
2091
|
-
if (options) {
|
|
2092
|
-
this.#executionCtx = options.executionCtx;
|
|
2093
|
-
this.env = options.env;
|
|
2094
|
-
this.#notFoundHandler = options.notFoundHandler;
|
|
2095
|
-
this.#path = options.path;
|
|
2096
|
-
this.#matchResult = options.matchResult;
|
|
2097
|
-
}
|
|
2098
|
-
}
|
|
2099
|
-
/**
|
|
2100
|
-
* `.req` is the instance of {@link HonoRequest}.
|
|
2101
|
-
*/
|
|
2102
|
-
get req() {
|
|
2103
|
-
this.#req ??= new HonoRequest(this.#rawRequest, this.#path, this.#matchResult);
|
|
2104
|
-
return this.#req;
|
|
2105
|
-
}
|
|
2106
|
-
/**
|
|
2107
|
-
* @see {@link https://hono.dev/docs/api/context#event}
|
|
2108
|
-
* The FetchEvent associated with the current request.
|
|
2109
|
-
*
|
|
2110
|
-
* @throws Will throw an error if the context does not have a FetchEvent.
|
|
2111
|
-
*/
|
|
2112
|
-
get event() {
|
|
2113
|
-
if (this.#executionCtx && "respondWith" in this.#executionCtx) {
|
|
2114
|
-
return this.#executionCtx;
|
|
2115
|
-
} else {
|
|
2116
|
-
throw Error("This context has no FetchEvent");
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
/**
|
|
2120
|
-
* @see {@link https://hono.dev/docs/api/context#executionctx}
|
|
2121
|
-
* The ExecutionContext associated with the current request.
|
|
2122
|
-
*
|
|
2123
|
-
* @throws Will throw an error if the context does not have an ExecutionContext.
|
|
2124
|
-
*/
|
|
2125
|
-
get executionCtx() {
|
|
2126
|
-
if (this.#executionCtx) {
|
|
2127
|
-
return this.#executionCtx;
|
|
2128
|
-
} else {
|
|
2129
|
-
throw Error("This context has no ExecutionContext");
|
|
2130
|
-
}
|
|
2131
|
-
}
|
|
2132
|
-
/**
|
|
2133
|
-
* @see {@link https://hono.dev/docs/api/context#res}
|
|
2134
|
-
* The Response object for the current request.
|
|
2135
|
-
*/
|
|
2136
|
-
get res() {
|
|
2137
|
-
return this.#res ||= new Response(null, {
|
|
2138
|
-
headers: this.#preparedHeaders ??= new Headers()
|
|
2139
|
-
});
|
|
2140
|
-
}
|
|
2141
|
-
/**
|
|
2142
|
-
* Sets the Response object for the current request.
|
|
2143
|
-
*
|
|
2144
|
-
* @param _res - The Response object to set.
|
|
2145
|
-
*/
|
|
2146
|
-
set res(_res) {
|
|
2147
|
-
if (this.#res && _res) {
|
|
2148
|
-
_res = new Response(_res.body, _res);
|
|
2149
|
-
for (const [k, v] of this.#res.headers.entries()) {
|
|
2150
|
-
if (k === "content-type") {
|
|
2151
|
-
continue;
|
|
2152
|
-
}
|
|
2153
|
-
if (k === "set-cookie") {
|
|
2154
|
-
const cookies = this.#res.headers.getSetCookie();
|
|
2155
|
-
_res.headers.delete("set-cookie");
|
|
2156
|
-
for (const cookie of cookies) {
|
|
2157
|
-
_res.headers.append("set-cookie", cookie);
|
|
2158
|
-
}
|
|
2159
|
-
} else {
|
|
2160
|
-
_res.headers.set(k, v);
|
|
2161
|
-
}
|
|
2162
|
-
}
|
|
2163
|
-
}
|
|
2164
|
-
this.#res = _res;
|
|
2165
|
-
this.finalized = true;
|
|
2166
|
-
}
|
|
2167
|
-
/**
|
|
2168
|
-
* `.render()` can create a response within a layout.
|
|
2169
|
-
*
|
|
2170
|
-
* @see {@link https://hono.dev/docs/api/context#render-setrenderer}
|
|
2171
|
-
*
|
|
2172
|
-
* @example
|
|
2173
|
-
* ```ts
|
|
2174
|
-
* app.get('/', (c) => {
|
|
2175
|
-
* return c.render('Hello!')
|
|
2176
|
-
* })
|
|
2177
|
-
* ```
|
|
2178
|
-
*/
|
|
2179
|
-
render = (...args) => {
|
|
2180
|
-
this.#renderer ??= (content) => this.html(content);
|
|
2181
|
-
return this.#renderer(...args);
|
|
2182
|
-
};
|
|
2183
|
-
/**
|
|
2184
|
-
* Sets the layout for the response.
|
|
2185
|
-
*
|
|
2186
|
-
* @param layout - The layout to set.
|
|
2187
|
-
* @returns The layout function.
|
|
2188
|
-
*/
|
|
2189
|
-
setLayout = (layout) => this.#layout = layout;
|
|
2190
|
-
/**
|
|
2191
|
-
* Gets the current layout for the response.
|
|
2192
|
-
*
|
|
2193
|
-
* @returns The current layout function.
|
|
2194
|
-
*/
|
|
2195
|
-
getLayout = () => this.#layout;
|
|
2196
|
-
/**
|
|
2197
|
-
* `.setRenderer()` can set the layout in the custom middleware.
|
|
2198
|
-
*
|
|
2199
|
-
* @see {@link https://hono.dev/docs/api/context#render-setrenderer}
|
|
2200
|
-
*
|
|
2201
|
-
* @example
|
|
2202
|
-
* ```tsx
|
|
2203
|
-
* app.use('*', async (c, next) => {
|
|
2204
|
-
* c.setRenderer((content) => {
|
|
2205
|
-
* return c.html(
|
|
2206
|
-
* <html>
|
|
2207
|
-
* <body>
|
|
2208
|
-
* <p>{content}</p>
|
|
2209
|
-
* </body>
|
|
2210
|
-
* </html>
|
|
2211
|
-
* )
|
|
2212
|
-
* })
|
|
2213
|
-
* await next()
|
|
2214
|
-
* })
|
|
2215
|
-
* ```
|
|
2216
|
-
*/
|
|
2217
|
-
setRenderer = (renderer) => {
|
|
2218
|
-
this.#renderer = renderer;
|
|
2219
|
-
};
|
|
2220
|
-
/**
|
|
2221
|
-
* `.header()` can set headers.
|
|
2222
|
-
*
|
|
2223
|
-
* @see {@link https://hono.dev/docs/api/context#header}
|
|
2224
|
-
*
|
|
2225
|
-
* @example
|
|
2226
|
-
* ```ts
|
|
2227
|
-
* app.get('/welcome', (c) => {
|
|
2228
|
-
* // Set headers
|
|
2229
|
-
* c.header('X-Message', 'Hello!')
|
|
2230
|
-
* c.header('Content-Type', 'text/plain')
|
|
2231
|
-
*
|
|
2232
|
-
* return c.body('Thank you for coming')
|
|
2233
|
-
* })
|
|
2234
|
-
* ```
|
|
2235
|
-
*/
|
|
2236
|
-
header = (name, value, options) => {
|
|
2237
|
-
if (this.finalized) {
|
|
2238
|
-
this.#res = new Response(this.#res.body, this.#res);
|
|
2239
|
-
}
|
|
2240
|
-
const headers = this.#res ? this.#res.headers : this.#preparedHeaders ??= new Headers();
|
|
2241
|
-
if (value === void 0) {
|
|
2242
|
-
headers.delete(name);
|
|
2243
|
-
} else if (options?.append) {
|
|
2244
|
-
headers.append(name, value);
|
|
2245
|
-
} else {
|
|
2246
|
-
headers.set(name, value);
|
|
2247
|
-
}
|
|
2248
|
-
};
|
|
2249
|
-
status = (status) => {
|
|
2250
|
-
this.#status = status;
|
|
2251
|
-
};
|
|
2252
|
-
/**
|
|
2253
|
-
* `.set()` can set the value specified by the key.
|
|
2254
|
-
*
|
|
2255
|
-
* @see {@link https://hono.dev/docs/api/context#set-get}
|
|
2256
|
-
*
|
|
2257
|
-
* @example
|
|
2258
|
-
* ```ts
|
|
2259
|
-
* app.use('*', async (c, next) => {
|
|
2260
|
-
* c.set('message', 'Hono is hot!!')
|
|
2261
|
-
* await next()
|
|
2262
|
-
* })
|
|
2263
|
-
* ```
|
|
2264
|
-
*/
|
|
2265
|
-
set = (key, value) => {
|
|
2266
|
-
this.#var ??= /* @__PURE__ */ new Map();
|
|
2267
|
-
this.#var.set(key, value);
|
|
2268
|
-
};
|
|
2269
|
-
/**
|
|
2270
|
-
* `.get()` can use the value specified by the key.
|
|
2271
|
-
*
|
|
2272
|
-
* @see {@link https://hono.dev/docs/api/context#set-get}
|
|
2273
|
-
*
|
|
2274
|
-
* @example
|
|
2275
|
-
* ```ts
|
|
2276
|
-
* app.get('/', (c) => {
|
|
2277
|
-
* const message = c.get('message')
|
|
2278
|
-
* return c.text(`The message is "${message}"`)
|
|
2279
|
-
* })
|
|
2280
|
-
* ```
|
|
2281
|
-
*/
|
|
2282
|
-
get = (key) => {
|
|
2283
|
-
return this.#var ? this.#var.get(key) : void 0;
|
|
2284
|
-
};
|
|
2285
|
-
/**
|
|
2286
|
-
* `.var` can access the value of a variable.
|
|
2287
|
-
*
|
|
2288
|
-
* @see {@link https://hono.dev/docs/api/context#var}
|
|
2289
|
-
*
|
|
2290
|
-
* @example
|
|
2291
|
-
* ```ts
|
|
2292
|
-
* const result = c.var.client.oneMethod()
|
|
2293
|
-
* ```
|
|
2294
|
-
*/
|
|
2295
|
-
// c.var.propName is a read-only
|
|
2296
|
-
get var() {
|
|
2297
|
-
if (!this.#var) {
|
|
2298
|
-
return {};
|
|
2299
|
-
}
|
|
2300
|
-
return Object.fromEntries(this.#var);
|
|
2301
|
-
}
|
|
2302
|
-
#newResponse(data, arg, headers) {
|
|
2303
|
-
const responseHeaders = this.#res ? new Headers(this.#res.headers) : this.#preparedHeaders ?? new Headers();
|
|
2304
|
-
if (typeof arg === "object" && "headers" in arg) {
|
|
2305
|
-
const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers);
|
|
2306
|
-
for (const [key, value] of argHeaders) {
|
|
2307
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
2308
|
-
responseHeaders.append(key, value);
|
|
2309
|
-
} else {
|
|
2310
|
-
responseHeaders.set(key, value);
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
|
-
}
|
|
2314
|
-
if (headers) {
|
|
2315
|
-
for (const [k, v] of Object.entries(headers)) {
|
|
2316
|
-
if (typeof v === "string") {
|
|
2317
|
-
responseHeaders.set(k, v);
|
|
2318
|
-
} else {
|
|
2319
|
-
responseHeaders.delete(k);
|
|
2320
|
-
for (const v2 of v) {
|
|
2321
|
-
responseHeaders.append(k, v2);
|
|
2322
|
-
}
|
|
2323
|
-
}
|
|
2324
|
-
}
|
|
2325
|
-
}
|
|
2326
|
-
const status = typeof arg === "number" ? arg : arg?.status ?? this.#status;
|
|
2327
|
-
return new Response(data, { status, headers: responseHeaders });
|
|
2328
|
-
}
|
|
2329
|
-
newResponse = (...args) => this.#newResponse(...args);
|
|
2330
|
-
/**
|
|
2331
|
-
* `.body()` can return the HTTP response.
|
|
2332
|
-
* You can set headers with `.header()` and set HTTP status code with `.status`.
|
|
2333
|
-
* This can also be set in `.text()`, `.json()` and so on.
|
|
2334
|
-
*
|
|
2335
|
-
* @see {@link https://hono.dev/docs/api/context#body}
|
|
2336
|
-
*
|
|
2337
|
-
* @example
|
|
2338
|
-
* ```ts
|
|
2339
|
-
* app.get('/welcome', (c) => {
|
|
2340
|
-
* // Set headers
|
|
2341
|
-
* c.header('X-Message', 'Hello!')
|
|
2342
|
-
* c.header('Content-Type', 'text/plain')
|
|
2343
|
-
* // Set HTTP status code
|
|
2344
|
-
* c.status(201)
|
|
2345
|
-
*
|
|
2346
|
-
* // Return the response body
|
|
2347
|
-
* return c.body('Thank you for coming')
|
|
2348
|
-
* })
|
|
2349
|
-
* ```
|
|
2350
|
-
*/
|
|
2351
|
-
body = (data, arg, headers) => this.#newResponse(data, arg, headers);
|
|
2352
|
-
/**
|
|
2353
|
-
* `.text()` can render text as `Content-Type:text/plain`.
|
|
2354
|
-
*
|
|
2355
|
-
* @see {@link https://hono.dev/docs/api/context#text}
|
|
2356
|
-
*
|
|
2357
|
-
* @example
|
|
2358
|
-
* ```ts
|
|
2359
|
-
* app.get('/say', (c) => {
|
|
2360
|
-
* return c.text('Hello!')
|
|
2361
|
-
* })
|
|
2362
|
-
* ```
|
|
2363
|
-
*/
|
|
2364
|
-
text = (text, arg, headers) => {
|
|
2365
|
-
return !this.#preparedHeaders && !this.#status && !arg && !headers && !this.finalized ? new Response(text) : this.#newResponse(
|
|
2366
|
-
text,
|
|
2367
|
-
arg,
|
|
2368
|
-
setDefaultContentType(TEXT_PLAIN, headers)
|
|
2369
|
-
);
|
|
2370
|
-
};
|
|
2371
|
-
/**
|
|
2372
|
-
* `.json()` can render JSON as `Content-Type:application/json`.
|
|
2373
|
-
*
|
|
2374
|
-
* @see {@link https://hono.dev/docs/api/context#json}
|
|
2375
|
-
*
|
|
2376
|
-
* @example
|
|
2377
|
-
* ```ts
|
|
2378
|
-
* app.get('/api', (c) => {
|
|
2379
|
-
* return c.json({ message: 'Hello!' })
|
|
2380
|
-
* })
|
|
2381
|
-
* ```
|
|
2382
|
-
*/
|
|
2383
|
-
json = (object, arg, headers) => {
|
|
2384
|
-
return this.#newResponse(
|
|
2385
|
-
JSON.stringify(object),
|
|
2386
|
-
arg,
|
|
2387
|
-
setDefaultContentType("application/json", headers)
|
|
2388
|
-
);
|
|
2389
|
-
};
|
|
2390
|
-
html = (html, arg, headers) => {
|
|
2391
|
-
const res = (html2) => this.#newResponse(html2, arg, setDefaultContentType("text/html; charset=UTF-8", headers));
|
|
2392
|
-
return typeof html === "object" ? resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then(res) : res(html);
|
|
2393
|
-
};
|
|
2394
|
-
/**
|
|
2395
|
-
* `.redirect()` can Redirect, default status code is 302.
|
|
2396
|
-
*
|
|
2397
|
-
* @see {@link https://hono.dev/docs/api/context#redirect}
|
|
2398
|
-
*
|
|
2399
|
-
* @example
|
|
2400
|
-
* ```ts
|
|
2401
|
-
* app.get('/redirect', (c) => {
|
|
2402
|
-
* return c.redirect('/')
|
|
2403
|
-
* })
|
|
2404
|
-
* app.get('/redirect-permanently', (c) => {
|
|
2405
|
-
* return c.redirect('/', 301)
|
|
2406
|
-
* })
|
|
2407
|
-
* ```
|
|
2408
|
-
*/
|
|
2409
|
-
redirect = (location, status) => {
|
|
2410
|
-
const locationString = String(location);
|
|
2411
|
-
this.header(
|
|
2412
|
-
"Location",
|
|
2413
|
-
// Multibyes should be encoded
|
|
2414
|
-
// eslint-disable-next-line no-control-regex
|
|
2415
|
-
!/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString)
|
|
2416
|
-
);
|
|
2417
|
-
return this.newResponse(null, status ?? 302);
|
|
2418
|
-
};
|
|
2419
|
-
/**
|
|
2420
|
-
* `.notFound()` can return the Not Found Response.
|
|
2421
|
-
*
|
|
2422
|
-
* @see {@link https://hono.dev/docs/api/context#notfound}
|
|
2423
|
-
*
|
|
2424
|
-
* @example
|
|
2425
|
-
* ```ts
|
|
2426
|
-
* app.get('/notfound', (c) => {
|
|
2427
|
-
* return c.notFound()
|
|
2428
|
-
* })
|
|
2429
|
-
* ```
|
|
2430
|
-
*/
|
|
2431
|
-
notFound = () => {
|
|
2432
|
-
this.#notFoundHandler ??= () => new Response();
|
|
2433
|
-
return this.#notFoundHandler(this);
|
|
2434
|
-
};
|
|
2435
|
-
};
|
|
2436
|
-
|
|
2437
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router.js
|
|
2438
|
-
var METHOD_NAME_ALL = "ALL";
|
|
2439
|
-
var METHOD_NAME_ALL_LOWERCASE = "all";
|
|
2440
|
-
var METHODS = ["get", "post", "put", "delete", "options", "patch"];
|
|
2441
|
-
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
|
2442
|
-
var UnsupportedPathError = class extends Error {
|
|
2443
|
-
};
|
|
2444
|
-
|
|
2445
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/utils/constants.js
|
|
2446
|
-
var COMPOSED_HANDLER = "__COMPOSED_HANDLER";
|
|
2447
|
-
|
|
2448
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/hono-base.js
|
|
2449
|
-
var notFoundHandler = (c) => {
|
|
2450
|
-
return c.text("404 Not Found", 404);
|
|
2451
|
-
};
|
|
2452
|
-
var errorHandler = (err, c) => {
|
|
2453
|
-
if ("getResponse" in err) {
|
|
2454
|
-
const res = err.getResponse();
|
|
2455
|
-
return c.newResponse(res.body, res);
|
|
2456
|
-
}
|
|
2457
|
-
console.error(err);
|
|
2458
|
-
return c.text("Internal Server Error", 500);
|
|
2459
|
-
};
|
|
2460
|
-
var Hono = class _Hono {
|
|
2461
|
-
get;
|
|
2462
|
-
post;
|
|
2463
|
-
put;
|
|
2464
|
-
delete;
|
|
2465
|
-
options;
|
|
2466
|
-
patch;
|
|
2467
|
-
all;
|
|
2468
|
-
on;
|
|
2469
|
-
use;
|
|
2470
|
-
/*
|
|
2471
|
-
This class is like an abstract class and does not have a router.
|
|
2472
|
-
To use it, inherit the class and implement router in the constructor.
|
|
2473
|
-
*/
|
|
2474
|
-
router;
|
|
2475
|
-
getPath;
|
|
2476
|
-
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
2477
|
-
_basePath = "/";
|
|
2478
|
-
#path = "/";
|
|
2479
|
-
routes = [];
|
|
2480
|
-
constructor(options = {}) {
|
|
2481
|
-
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE];
|
|
2482
|
-
allMethods.forEach((method) => {
|
|
2483
|
-
this[method] = (args1, ...args) => {
|
|
2484
|
-
if (typeof args1 === "string") {
|
|
2485
|
-
this.#path = args1;
|
|
2486
|
-
} else {
|
|
2487
|
-
this.#addRoute(method, this.#path, args1);
|
|
2488
|
-
}
|
|
2489
|
-
args.forEach((handler) => {
|
|
2490
|
-
this.#addRoute(method, this.#path, handler);
|
|
2491
|
-
});
|
|
2492
|
-
return this;
|
|
2493
|
-
};
|
|
2494
|
-
});
|
|
2495
|
-
this.on = (method, path, ...handlers) => {
|
|
2496
|
-
for (const p of [path].flat()) {
|
|
2497
|
-
this.#path = p;
|
|
2498
|
-
for (const m of [method].flat()) {
|
|
2499
|
-
handlers.map((handler) => {
|
|
2500
|
-
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
2501
|
-
});
|
|
2502
|
-
}
|
|
2503
|
-
}
|
|
2504
|
-
return this;
|
|
2505
|
-
};
|
|
2506
|
-
this.use = (arg1, ...handlers) => {
|
|
2507
|
-
if (typeof arg1 === "string") {
|
|
2508
|
-
this.#path = arg1;
|
|
2509
|
-
} else {
|
|
2510
|
-
this.#path = "*";
|
|
2511
|
-
handlers.unshift(arg1);
|
|
2512
|
-
}
|
|
2513
|
-
handlers.forEach((handler) => {
|
|
2514
|
-
this.#addRoute(METHOD_NAME_ALL, this.#path, handler);
|
|
2515
|
-
});
|
|
2516
|
-
return this;
|
|
2517
|
-
};
|
|
2518
|
-
const { strict, ...optionsWithoutStrict } = options;
|
|
2519
|
-
Object.assign(this, optionsWithoutStrict);
|
|
2520
|
-
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
2521
|
-
}
|
|
2522
|
-
#clone() {
|
|
2523
|
-
const clone = new _Hono({
|
|
2524
|
-
router: this.router,
|
|
2525
|
-
getPath: this.getPath
|
|
2526
|
-
});
|
|
2527
|
-
clone.errorHandler = this.errorHandler;
|
|
2528
|
-
clone.#notFoundHandler = this.#notFoundHandler;
|
|
2529
|
-
clone.routes = this.routes;
|
|
2530
|
-
return clone;
|
|
2531
|
-
}
|
|
2532
|
-
#notFoundHandler = notFoundHandler;
|
|
2533
|
-
// Cannot use `#` because it requires visibility at JavaScript runtime.
|
|
2534
|
-
errorHandler = errorHandler;
|
|
2535
|
-
/**
|
|
2536
|
-
* `.route()` allows grouping other Hono instance in routes.
|
|
2537
|
-
*
|
|
2538
|
-
* @see {@link https://hono.dev/docs/api/routing#grouping}
|
|
2539
|
-
*
|
|
2540
|
-
* @param {string} path - base Path
|
|
2541
|
-
* @param {Hono} app - other Hono instance
|
|
2542
|
-
* @returns {Hono} routed Hono instance
|
|
2543
|
-
*
|
|
2544
|
-
* @example
|
|
2545
|
-
* ```ts
|
|
2546
|
-
* const app = new Hono()
|
|
2547
|
-
* const app2 = new Hono()
|
|
2548
|
-
*
|
|
2549
|
-
* app2.get("/user", (c) => c.text("user"))
|
|
2550
|
-
* app.route("/api", app2) // GET /api/user
|
|
2551
|
-
* ```
|
|
2552
|
-
*/
|
|
2553
|
-
route(path, app) {
|
|
2554
|
-
const subApp = this.basePath(path);
|
|
2555
|
-
app.routes.map((r) => {
|
|
2556
|
-
let handler;
|
|
2557
|
-
if (app.errorHandler === errorHandler) {
|
|
2558
|
-
handler = r.handler;
|
|
2559
|
-
} else {
|
|
2560
|
-
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
2561
|
-
handler[COMPOSED_HANDLER] = r.handler;
|
|
2562
|
-
}
|
|
2563
|
-
subApp.#addRoute(r.method, r.path, handler);
|
|
2564
|
-
});
|
|
2565
|
-
return this;
|
|
2566
|
-
}
|
|
2567
|
-
/**
|
|
2568
|
-
* `.basePath()` allows base paths to be specified.
|
|
2569
|
-
*
|
|
2570
|
-
* @see {@link https://hono.dev/docs/api/routing#base-path}
|
|
2571
|
-
*
|
|
2572
|
-
* @param {string} path - base Path
|
|
2573
|
-
* @returns {Hono} changed Hono instance
|
|
2574
|
-
*
|
|
2575
|
-
* @example
|
|
2576
|
-
* ```ts
|
|
2577
|
-
* const api = new Hono().basePath('/api')
|
|
2578
|
-
* ```
|
|
2579
|
-
*/
|
|
2580
|
-
basePath(path) {
|
|
2581
|
-
const subApp = this.#clone();
|
|
2582
|
-
subApp._basePath = mergePath(this._basePath, path);
|
|
2583
|
-
return subApp;
|
|
2584
|
-
}
|
|
2585
|
-
/**
|
|
2586
|
-
* `.onError()` handles an error and returns a customized Response.
|
|
2587
|
-
*
|
|
2588
|
-
* @see {@link https://hono.dev/docs/api/hono#error-handling}
|
|
2589
|
-
*
|
|
2590
|
-
* @param {ErrorHandler} handler - request Handler for error
|
|
2591
|
-
* @returns {Hono} changed Hono instance
|
|
2592
|
-
*
|
|
2593
|
-
* @example
|
|
2594
|
-
* ```ts
|
|
2595
|
-
* app.onError((err, c) => {
|
|
2596
|
-
* console.error(`${err}`)
|
|
2597
|
-
* return c.text('Custom Error Message', 500)
|
|
2598
|
-
* })
|
|
2599
|
-
* ```
|
|
2600
|
-
*/
|
|
2601
|
-
onError = (handler) => {
|
|
2602
|
-
this.errorHandler = handler;
|
|
2603
|
-
return this;
|
|
2604
|
-
};
|
|
2605
|
-
/**
|
|
2606
|
-
* `.notFound()` allows you to customize a Not Found Response.
|
|
2607
|
-
*
|
|
2608
|
-
* @see {@link https://hono.dev/docs/api/hono#not-found}
|
|
2609
|
-
*
|
|
2610
|
-
* @param {NotFoundHandler} handler - request handler for not-found
|
|
2611
|
-
* @returns {Hono} changed Hono instance
|
|
2612
|
-
*
|
|
2613
|
-
* @example
|
|
2614
|
-
* ```ts
|
|
2615
|
-
* app.notFound((c) => {
|
|
2616
|
-
* return c.text('Custom 404 Message', 404)
|
|
2617
|
-
* })
|
|
2618
|
-
* ```
|
|
2619
|
-
*/
|
|
2620
|
-
notFound = (handler) => {
|
|
2621
|
-
this.#notFoundHandler = handler;
|
|
2622
|
-
return this;
|
|
2623
|
-
};
|
|
2624
|
-
/**
|
|
2625
|
-
* `.mount()` allows you to mount applications built with other frameworks into your Hono application.
|
|
2626
|
-
*
|
|
2627
|
-
* @see {@link https://hono.dev/docs/api/hono#mount}
|
|
2628
|
-
*
|
|
2629
|
-
* @param {string} path - base Path
|
|
2630
|
-
* @param {Function} applicationHandler - other Request Handler
|
|
2631
|
-
* @param {MountOptions} [options] - options of `.mount()`
|
|
2632
|
-
* @returns {Hono} mounted Hono instance
|
|
2633
|
-
*
|
|
2634
|
-
* @example
|
|
2635
|
-
* ```ts
|
|
2636
|
-
* import { Router as IttyRouter } from 'itty-router'
|
|
2637
|
-
* import { Hono } from 'hono'
|
|
2638
|
-
* // Create itty-router application
|
|
2639
|
-
* const ittyRouter = IttyRouter()
|
|
2640
|
-
* // GET /itty-router/hello
|
|
2641
|
-
* ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
|
|
2642
|
-
*
|
|
2643
|
-
* const app = new Hono()
|
|
2644
|
-
* app.mount('/itty-router', ittyRouter.handle)
|
|
2645
|
-
* ```
|
|
2646
|
-
*
|
|
2647
|
-
* @example
|
|
2648
|
-
* ```ts
|
|
2649
|
-
* const app = new Hono()
|
|
2650
|
-
* // Send the request to another application without modification.
|
|
2651
|
-
* app.mount('/app', anotherApp, {
|
|
2652
|
-
* replaceRequest: (req) => req,
|
|
2653
|
-
* })
|
|
2654
|
-
* ```
|
|
2655
|
-
*/
|
|
2656
|
-
mount(path, applicationHandler, options) {
|
|
2657
|
-
let replaceRequest;
|
|
2658
|
-
let optionHandler;
|
|
2659
|
-
if (options) {
|
|
2660
|
-
if (typeof options === "function") {
|
|
2661
|
-
optionHandler = options;
|
|
2662
|
-
} else {
|
|
2663
|
-
optionHandler = options.optionHandler;
|
|
2664
|
-
if (options.replaceRequest === false) {
|
|
2665
|
-
replaceRequest = (request) => request;
|
|
2666
|
-
} else {
|
|
2667
|
-
replaceRequest = options.replaceRequest;
|
|
2668
|
-
}
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
const getOptions = optionHandler ? (c) => {
|
|
2672
|
-
const options2 = optionHandler(c);
|
|
2673
|
-
return Array.isArray(options2) ? options2 : [options2];
|
|
2674
|
-
} : (c) => {
|
|
2675
|
-
let executionContext = void 0;
|
|
2676
|
-
try {
|
|
2677
|
-
executionContext = c.executionCtx;
|
|
2678
|
-
} catch {
|
|
2679
|
-
}
|
|
2680
|
-
return [c.env, executionContext];
|
|
2681
|
-
};
|
|
2682
|
-
replaceRequest ||= (() => {
|
|
2683
|
-
const mergedPath = mergePath(this._basePath, path);
|
|
2684
|
-
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
2685
|
-
return (request) => {
|
|
2686
|
-
const url = new URL(request.url);
|
|
2687
|
-
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
2688
|
-
return new Request(url, request);
|
|
2689
|
-
};
|
|
2690
|
-
})();
|
|
2691
|
-
const handler = async (c, next) => {
|
|
2692
|
-
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
2693
|
-
if (res) {
|
|
2694
|
-
return res;
|
|
2695
|
-
}
|
|
2696
|
-
await next();
|
|
2697
|
-
};
|
|
2698
|
-
this.#addRoute(METHOD_NAME_ALL, mergePath(path, "*"), handler);
|
|
2699
|
-
return this;
|
|
2700
|
-
}
|
|
2701
|
-
#addRoute(method, path, handler) {
|
|
2702
|
-
method = method.toUpperCase();
|
|
2703
|
-
path = mergePath(this._basePath, path);
|
|
2704
|
-
const r = { basePath: this._basePath, path, method, handler };
|
|
2705
|
-
this.router.add(method, path, [handler, r]);
|
|
2706
|
-
this.routes.push(r);
|
|
2707
|
-
}
|
|
2708
|
-
#handleError(err, c) {
|
|
2709
|
-
if (err instanceof Error) {
|
|
2710
|
-
return this.errorHandler(err, c);
|
|
2711
|
-
}
|
|
2712
|
-
throw err;
|
|
2713
|
-
}
|
|
2714
|
-
#dispatch(request, executionCtx, env2, method) {
|
|
2715
|
-
if (method === "HEAD") {
|
|
2716
|
-
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env2, "GET")))();
|
|
2717
|
-
}
|
|
2718
|
-
const path = this.getPath(request, { env: env2 });
|
|
2719
|
-
const matchResult = this.router.match(method, path);
|
|
2720
|
-
const c = new Context(request, {
|
|
2721
|
-
path,
|
|
2722
|
-
matchResult,
|
|
2723
|
-
env: env2,
|
|
2724
|
-
executionCtx,
|
|
2725
|
-
notFoundHandler: this.#notFoundHandler
|
|
2726
|
-
});
|
|
2727
|
-
if (matchResult[0].length === 1) {
|
|
2728
|
-
let res;
|
|
2729
|
-
try {
|
|
2730
|
-
res = matchResult[0][0][0][0](c, async () => {
|
|
2731
|
-
c.res = await this.#notFoundHandler(c);
|
|
2732
|
-
});
|
|
2733
|
-
} catch (err) {
|
|
2734
|
-
return this.#handleError(err, c);
|
|
2735
|
-
}
|
|
2736
|
-
return res instanceof Promise ? res.then(
|
|
2737
|
-
(resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
|
|
2738
|
-
).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
2739
|
-
}
|
|
2740
|
-
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
2741
|
-
return (async () => {
|
|
2742
|
-
try {
|
|
2743
|
-
const context = await composed(c);
|
|
2744
|
-
if (!context.finalized) {
|
|
2745
|
-
throw new Error(
|
|
2746
|
-
"Context is not finalized. Did you forget to return a Response object or `await next()`?"
|
|
2747
|
-
);
|
|
2748
|
-
}
|
|
2749
|
-
return context.res;
|
|
2750
|
-
} catch (err) {
|
|
2751
|
-
return this.#handleError(err, c);
|
|
2752
|
-
}
|
|
2753
|
-
})();
|
|
2754
|
-
}
|
|
2755
|
-
/**
|
|
2756
|
-
* `.fetch()` will be entry point of your app.
|
|
2757
|
-
*
|
|
2758
|
-
* @see {@link https://hono.dev/docs/api/hono#fetch}
|
|
2759
|
-
*
|
|
2760
|
-
* @param {Request} request - request Object of request
|
|
2761
|
-
* @param {Env} Env - env Object
|
|
2762
|
-
* @param {ExecutionContext} - context of execution
|
|
2763
|
-
* @returns {Response | Promise<Response>} response of request
|
|
2764
|
-
*
|
|
2765
|
-
*/
|
|
2766
|
-
fetch = (request, ...rest) => {
|
|
2767
|
-
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
2768
|
-
};
|
|
2769
|
-
/**
|
|
2770
|
-
* `.request()` is a useful method for testing.
|
|
2771
|
-
* You can pass a URL or pathname to send a GET request.
|
|
2772
|
-
* app will return a Response object.
|
|
2773
|
-
* ```ts
|
|
2774
|
-
* test('GET /hello is ok', async () => {
|
|
2775
|
-
* const res = await app.request('/hello')
|
|
2776
|
-
* expect(res.status).toBe(200)
|
|
2777
|
-
* })
|
|
2778
|
-
* ```
|
|
2779
|
-
* @see https://hono.dev/docs/api/hono#request
|
|
2780
|
-
*/
|
|
2781
|
-
request = (input, requestInit, Env, executionCtx) => {
|
|
2782
|
-
if (input instanceof Request) {
|
|
2783
|
-
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
2784
|
-
}
|
|
2785
|
-
input = input.toString();
|
|
2786
|
-
return this.fetch(
|
|
2787
|
-
new Request(
|
|
2788
|
-
/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`,
|
|
2789
|
-
requestInit
|
|
2790
|
-
),
|
|
2791
|
-
Env,
|
|
2792
|
-
executionCtx
|
|
2793
|
-
);
|
|
2794
|
-
};
|
|
2795
|
-
/**
|
|
2796
|
-
* `.fire()` automatically adds a global fetch event listener.
|
|
2797
|
-
* This can be useful for environments that adhere to the Service Worker API, such as non-ES module Cloudflare Workers.
|
|
2798
|
-
* @deprecated
|
|
2799
|
-
* Use `fire` from `hono/service-worker` instead.
|
|
2800
|
-
* ```ts
|
|
2801
|
-
* import { Hono } from 'hono'
|
|
2802
|
-
* import { fire } from 'hono/service-worker'
|
|
2803
|
-
*
|
|
2804
|
-
* const app = new Hono()
|
|
2805
|
-
* // ...
|
|
2806
|
-
* fire(app)
|
|
2807
|
-
* ```
|
|
2808
|
-
* @see https://hono.dev/docs/api/hono#fire
|
|
2809
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
|
|
2810
|
-
* @see https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
|
|
2811
|
-
*/
|
|
2812
|
-
fire = () => {
|
|
2813
|
-
addEventListener("fetch", (event) => {
|
|
2814
|
-
event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
|
|
2815
|
-
});
|
|
2816
|
-
};
|
|
2817
|
-
};
|
|
2818
|
-
|
|
2819
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/reg-exp-router/matcher.js
|
|
2820
|
-
var emptyParam = [];
|
|
2821
|
-
function match(method, path) {
|
|
2822
|
-
const matchers = this.buildAllMatchers();
|
|
2823
|
-
const match2 = ((method2, path2) => {
|
|
2824
|
-
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
2825
|
-
const staticMatch = matcher[2][path2];
|
|
2826
|
-
if (staticMatch) {
|
|
2827
|
-
return staticMatch;
|
|
2828
|
-
}
|
|
2829
|
-
const match3 = path2.match(matcher[0]);
|
|
2830
|
-
if (!match3) {
|
|
2831
|
-
return [[], emptyParam];
|
|
2832
|
-
}
|
|
2833
|
-
const index = match3.indexOf("", 1);
|
|
2834
|
-
return [matcher[1][index], match3];
|
|
2835
|
-
});
|
|
2836
|
-
this.match = match2;
|
|
2837
|
-
return match2(method, path);
|
|
2838
|
-
}
|
|
2839
|
-
|
|
2840
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/reg-exp-router/node.js
|
|
2841
|
-
var LABEL_REG_EXP_STR = "[^/]+";
|
|
2842
|
-
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
2843
|
-
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
2844
|
-
var PATH_ERROR = /* @__PURE__ */ Symbol();
|
|
2845
|
-
var regExpMetaChars = new Set(".\\+*[^]$()");
|
|
2846
|
-
function compareKey(a, b) {
|
|
2847
|
-
if (a.length === 1) {
|
|
2848
|
-
return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
2849
|
-
}
|
|
2850
|
-
if (b.length === 1) {
|
|
2851
|
-
return 1;
|
|
2852
|
-
}
|
|
2853
|
-
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
|
|
2854
|
-
return 1;
|
|
2855
|
-
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
|
|
2856
|
-
return -1;
|
|
2857
|
-
}
|
|
2858
|
-
if (a === LABEL_REG_EXP_STR) {
|
|
2859
|
-
return 1;
|
|
2860
|
-
} else if (b === LABEL_REG_EXP_STR) {
|
|
2861
|
-
return -1;
|
|
2862
|
-
}
|
|
2863
|
-
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
2864
|
-
}
|
|
2865
|
-
var Node = class _Node {
|
|
2866
|
-
#index;
|
|
2867
|
-
#varIndex;
|
|
2868
|
-
#children = /* @__PURE__ */ Object.create(null);
|
|
2869
|
-
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
2870
|
-
if (tokens.length === 0) {
|
|
2871
|
-
if (this.#index !== void 0) {
|
|
2872
|
-
throw PATH_ERROR;
|
|
2873
|
-
}
|
|
2874
|
-
if (pathErrorCheckOnly) {
|
|
2875
|
-
return;
|
|
2876
|
-
}
|
|
2877
|
-
this.#index = index;
|
|
2878
|
-
return;
|
|
2879
|
-
}
|
|
2880
|
-
const [token, ...restTokens] = tokens;
|
|
2881
|
-
const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
2882
|
-
let node;
|
|
2883
|
-
if (pattern) {
|
|
2884
|
-
const name = pattern[1];
|
|
2885
|
-
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
|
2886
|
-
if (name && pattern[2]) {
|
|
2887
|
-
if (regexpStr === ".*") {
|
|
2888
|
-
throw PATH_ERROR;
|
|
2889
|
-
}
|
|
2890
|
-
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
|
2891
|
-
if (/\((?!\?:)/.test(regexpStr)) {
|
|
2892
|
-
throw PATH_ERROR;
|
|
2893
|
-
}
|
|
2894
|
-
}
|
|
2895
|
-
node = this.#children[regexpStr];
|
|
2896
|
-
if (!node) {
|
|
2897
|
-
if (Object.keys(this.#children).some(
|
|
2898
|
-
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
2899
|
-
)) {
|
|
2900
|
-
throw PATH_ERROR;
|
|
2901
|
-
}
|
|
2902
|
-
if (pathErrorCheckOnly) {
|
|
2903
|
-
return;
|
|
2904
|
-
}
|
|
2905
|
-
node = this.#children[regexpStr] = new _Node();
|
|
2906
|
-
if (name !== "") {
|
|
2907
|
-
node.#varIndex = context.varIndex++;
|
|
2908
|
-
}
|
|
2909
|
-
}
|
|
2910
|
-
if (!pathErrorCheckOnly && name !== "") {
|
|
2911
|
-
paramMap.push([name, node.#varIndex]);
|
|
2912
|
-
}
|
|
2913
|
-
} else {
|
|
2914
|
-
node = this.#children[token];
|
|
2915
|
-
if (!node) {
|
|
2916
|
-
if (Object.keys(this.#children).some(
|
|
2917
|
-
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
2918
|
-
)) {
|
|
2919
|
-
throw PATH_ERROR;
|
|
2920
|
-
}
|
|
2921
|
-
if (pathErrorCheckOnly) {
|
|
2922
|
-
return;
|
|
2923
|
-
}
|
|
2924
|
-
node = this.#children[token] = new _Node();
|
|
2925
|
-
}
|
|
2926
|
-
}
|
|
2927
|
-
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
2928
|
-
}
|
|
2929
|
-
buildRegExpStr() {
|
|
2930
|
-
const childKeys = Object.keys(this.#children).sort(compareKey);
|
|
2931
|
-
const strList = childKeys.map((k) => {
|
|
2932
|
-
const c = this.#children[k];
|
|
2933
|
-
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
2934
|
-
});
|
|
2935
|
-
if (typeof this.#index === "number") {
|
|
2936
|
-
strList.unshift(`#${this.#index}`);
|
|
2937
|
-
}
|
|
2938
|
-
if (strList.length === 0) {
|
|
2939
|
-
return "";
|
|
2940
|
-
}
|
|
2941
|
-
if (strList.length === 1) {
|
|
2942
|
-
return strList[0];
|
|
2943
|
-
}
|
|
2944
|
-
return "(?:" + strList.join("|") + ")";
|
|
2945
|
-
}
|
|
2946
|
-
};
|
|
2947
|
-
|
|
2948
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/reg-exp-router/trie.js
|
|
2949
|
-
var Trie = class {
|
|
2950
|
-
#context = { varIndex: 0 };
|
|
2951
|
-
#root = new Node();
|
|
2952
|
-
insert(path, index, pathErrorCheckOnly) {
|
|
2953
|
-
const paramAssoc = [];
|
|
2954
|
-
const groups = [];
|
|
2955
|
-
for (let i = 0; ; ) {
|
|
2956
|
-
let replaced = false;
|
|
2957
|
-
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
2958
|
-
const mark = `@\\${i}`;
|
|
2959
|
-
groups[i] = [mark, m];
|
|
2960
|
-
i++;
|
|
2961
|
-
replaced = true;
|
|
2962
|
-
return mark;
|
|
2963
|
-
});
|
|
2964
|
-
if (!replaced) {
|
|
2965
|
-
break;
|
|
2966
|
-
}
|
|
2967
|
-
}
|
|
2968
|
-
const tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
2969
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
2970
|
-
const [mark] = groups[i];
|
|
2971
|
-
for (let j = tokens.length - 1; j >= 0; j--) {
|
|
2972
|
-
if (tokens[j].indexOf(mark) !== -1) {
|
|
2973
|
-
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
2974
|
-
break;
|
|
2975
|
-
}
|
|
2976
|
-
}
|
|
2977
|
-
}
|
|
2978
|
-
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
2979
|
-
return paramAssoc;
|
|
2980
|
-
}
|
|
2981
|
-
buildRegExp() {
|
|
2982
|
-
let regexp = this.#root.buildRegExpStr();
|
|
2983
|
-
if (regexp === "") {
|
|
2984
|
-
return [/^$/, [], []];
|
|
2985
|
-
}
|
|
2986
|
-
let captureIndex = 0;
|
|
2987
|
-
const indexReplacementMap = [];
|
|
2988
|
-
const paramReplacementMap = [];
|
|
2989
|
-
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
2990
|
-
if (handlerIndex !== void 0) {
|
|
2991
|
-
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
2992
|
-
return "$()";
|
|
2993
|
-
}
|
|
2994
|
-
if (paramIndex !== void 0) {
|
|
2995
|
-
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
2996
|
-
return "";
|
|
2997
|
-
}
|
|
2998
|
-
return "";
|
|
2999
|
-
});
|
|
3000
|
-
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
|
|
3001
|
-
}
|
|
3002
|
-
};
|
|
3003
|
-
|
|
3004
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/reg-exp-router/router.js
|
|
3005
|
-
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
3006
|
-
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
3007
|
-
function buildWildcardRegExp(path) {
|
|
3008
|
-
return wildcardRegExpCache[path] ??= new RegExp(
|
|
3009
|
-
path === "*" ? "" : `^${path.replace(
|
|
3010
|
-
/\/\*$|([.\\+*[^\]$()])/g,
|
|
3011
|
-
(_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)"
|
|
3012
|
-
)}$`
|
|
3013
|
-
);
|
|
3014
|
-
}
|
|
3015
|
-
function clearWildcardRegExpCache() {
|
|
3016
|
-
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
3017
|
-
}
|
|
3018
|
-
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
3019
|
-
const trie = new Trie();
|
|
3020
|
-
const handlerData = [];
|
|
3021
|
-
if (routes.length === 0) {
|
|
3022
|
-
return nullMatcher;
|
|
3023
|
-
}
|
|
3024
|
-
const routesWithStaticPathFlag = routes.map(
|
|
3025
|
-
(route) => [!/\*|\/:/.test(route[0]), ...route]
|
|
3026
|
-
).sort(
|
|
3027
|
-
([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length
|
|
3028
|
-
);
|
|
3029
|
-
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
3030
|
-
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
3031
|
-
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
3032
|
-
if (pathErrorCheckOnly) {
|
|
3033
|
-
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
3034
|
-
} else {
|
|
3035
|
-
j++;
|
|
3036
|
-
}
|
|
3037
|
-
let paramAssoc;
|
|
3038
|
-
try {
|
|
3039
|
-
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
|
3040
|
-
} catch (e) {
|
|
3041
|
-
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
|
3042
|
-
}
|
|
3043
|
-
if (pathErrorCheckOnly) {
|
|
3044
|
-
continue;
|
|
3045
|
-
}
|
|
3046
|
-
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
3047
|
-
const paramIndexMap = /* @__PURE__ */ Object.create(null);
|
|
3048
|
-
paramCount -= 1;
|
|
3049
|
-
for (; paramCount >= 0; paramCount--) {
|
|
3050
|
-
const [key, value] = paramAssoc[paramCount];
|
|
3051
|
-
paramIndexMap[key] = value;
|
|
3052
|
-
}
|
|
3053
|
-
return [h, paramIndexMap];
|
|
3054
|
-
});
|
|
3055
|
-
}
|
|
3056
|
-
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
3057
|
-
for (let i = 0, len = handlerData.length; i < len; i++) {
|
|
3058
|
-
for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
|
|
3059
|
-
const map = handlerData[i][j]?.[1];
|
|
3060
|
-
if (!map) {
|
|
3061
|
-
continue;
|
|
3062
|
-
}
|
|
3063
|
-
const keys = Object.keys(map);
|
|
3064
|
-
for (let k = 0, len3 = keys.length; k < len3; k++) {
|
|
3065
|
-
map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
3066
|
-
}
|
|
3067
|
-
}
|
|
3068
|
-
}
|
|
3069
|
-
const handlerMap = [];
|
|
3070
|
-
for (const i in indexReplacementMap) {
|
|
3071
|
-
handlerMap[i] = handlerData[indexReplacementMap[i]];
|
|
3072
|
-
}
|
|
3073
|
-
return [regexp, handlerMap, staticMap];
|
|
3074
|
-
}
|
|
3075
|
-
function findMiddleware(middleware, path) {
|
|
3076
|
-
if (!middleware) {
|
|
3077
|
-
return void 0;
|
|
3078
|
-
}
|
|
3079
|
-
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
3080
|
-
if (buildWildcardRegExp(k).test(path)) {
|
|
3081
|
-
return [...middleware[k]];
|
|
3082
|
-
}
|
|
3083
|
-
}
|
|
3084
|
-
return void 0;
|
|
3085
|
-
}
|
|
3086
|
-
var RegExpRouter = class {
|
|
3087
|
-
name = "RegExpRouter";
|
|
3088
|
-
#middleware;
|
|
3089
|
-
#routes;
|
|
3090
|
-
constructor() {
|
|
3091
|
-
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
3092
|
-
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
3093
|
-
}
|
|
3094
|
-
add(method, path, handler) {
|
|
3095
|
-
const middleware = this.#middleware;
|
|
3096
|
-
const routes = this.#routes;
|
|
3097
|
-
if (!middleware || !routes) {
|
|
3098
|
-
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
3099
|
-
}
|
|
3100
|
-
if (!middleware[method]) {
|
|
3101
|
-
;
|
|
3102
|
-
[middleware, routes].forEach((handlerMap) => {
|
|
3103
|
-
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
|
3104
|
-
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
3105
|
-
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
|
3106
|
-
});
|
|
3107
|
-
});
|
|
3108
|
-
}
|
|
3109
|
-
if (path === "/*") {
|
|
3110
|
-
path = "*";
|
|
3111
|
-
}
|
|
3112
|
-
const paramCount = (path.match(/\/:/g) || []).length;
|
|
3113
|
-
if (/\*$/.test(path)) {
|
|
3114
|
-
const re = buildWildcardRegExp(path);
|
|
3115
|
-
if (method === METHOD_NAME_ALL) {
|
|
3116
|
-
Object.keys(middleware).forEach((m) => {
|
|
3117
|
-
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
3118
|
-
});
|
|
3119
|
-
} else {
|
|
3120
|
-
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
3121
|
-
}
|
|
3122
|
-
Object.keys(middleware).forEach((m) => {
|
|
3123
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
3124
|
-
Object.keys(middleware[m]).forEach((p) => {
|
|
3125
|
-
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
3126
|
-
});
|
|
3127
|
-
}
|
|
3128
|
-
});
|
|
3129
|
-
Object.keys(routes).forEach((m) => {
|
|
3130
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
3131
|
-
Object.keys(routes[m]).forEach(
|
|
3132
|
-
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
|
|
3133
|
-
);
|
|
3134
|
-
}
|
|
3135
|
-
});
|
|
3136
|
-
return;
|
|
3137
|
-
}
|
|
3138
|
-
const paths = checkOptionalParameter(path) || [path];
|
|
3139
|
-
for (let i = 0, len = paths.length; i < len; i++) {
|
|
3140
|
-
const path2 = paths[i];
|
|
3141
|
-
Object.keys(routes).forEach((m) => {
|
|
3142
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
3143
|
-
routes[m][path2] ||= [
|
|
3144
|
-
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
|
3145
|
-
];
|
|
3146
|
-
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
|
3147
|
-
}
|
|
3148
|
-
});
|
|
3149
|
-
}
|
|
3150
|
-
}
|
|
3151
|
-
match = match;
|
|
3152
|
-
buildAllMatchers() {
|
|
3153
|
-
const matchers = /* @__PURE__ */ Object.create(null);
|
|
3154
|
-
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
3155
|
-
matchers[method] ||= this.#buildMatcher(method);
|
|
3156
|
-
});
|
|
3157
|
-
this.#middleware = this.#routes = void 0;
|
|
3158
|
-
clearWildcardRegExpCache();
|
|
3159
|
-
return matchers;
|
|
3160
|
-
}
|
|
3161
|
-
#buildMatcher(method) {
|
|
3162
|
-
const routes = [];
|
|
3163
|
-
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
3164
|
-
[this.#middleware, this.#routes].forEach((r) => {
|
|
3165
|
-
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
3166
|
-
if (ownRoute.length !== 0) {
|
|
3167
|
-
hasOwnRoute ||= true;
|
|
3168
|
-
routes.push(...ownRoute);
|
|
3169
|
-
} else if (method !== METHOD_NAME_ALL) {
|
|
3170
|
-
routes.push(
|
|
3171
|
-
...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]])
|
|
3172
|
-
);
|
|
3173
|
-
}
|
|
3174
|
-
});
|
|
3175
|
-
if (!hasOwnRoute) {
|
|
3176
|
-
return null;
|
|
3177
|
-
} else {
|
|
3178
|
-
return buildMatcherFromPreprocessedRoutes(routes);
|
|
3179
|
-
}
|
|
3180
|
-
}
|
|
3181
|
-
};
|
|
3182
|
-
|
|
3183
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/smart-router/router.js
|
|
3184
|
-
var SmartRouter = class {
|
|
3185
|
-
name = "SmartRouter";
|
|
3186
|
-
#routers = [];
|
|
3187
|
-
#routes = [];
|
|
3188
|
-
constructor(init) {
|
|
3189
|
-
this.#routers = init.routers;
|
|
3190
|
-
}
|
|
3191
|
-
add(method, path, handler) {
|
|
3192
|
-
if (!this.#routes) {
|
|
3193
|
-
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
3194
|
-
}
|
|
3195
|
-
this.#routes.push([method, path, handler]);
|
|
3196
|
-
}
|
|
3197
|
-
match(method, path) {
|
|
3198
|
-
if (!this.#routes) {
|
|
3199
|
-
throw new Error("Fatal error");
|
|
3200
|
-
}
|
|
3201
|
-
const routers = this.#routers;
|
|
3202
|
-
const routes = this.#routes;
|
|
3203
|
-
const len = routers.length;
|
|
3204
|
-
let i = 0;
|
|
3205
|
-
let res;
|
|
3206
|
-
for (; i < len; i++) {
|
|
3207
|
-
const router = routers[i];
|
|
3208
|
-
try {
|
|
3209
|
-
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
|
|
3210
|
-
router.add(...routes[i2]);
|
|
3211
|
-
}
|
|
3212
|
-
res = router.match(method, path);
|
|
3213
|
-
} catch (e) {
|
|
3214
|
-
if (e instanceof UnsupportedPathError) {
|
|
3215
|
-
continue;
|
|
3216
|
-
}
|
|
3217
|
-
throw e;
|
|
3218
|
-
}
|
|
3219
|
-
this.match = router.match.bind(router);
|
|
3220
|
-
this.#routers = [router];
|
|
3221
|
-
this.#routes = void 0;
|
|
3222
|
-
break;
|
|
3223
|
-
}
|
|
3224
|
-
if (i === len) {
|
|
3225
|
-
throw new Error("Fatal error");
|
|
3226
|
-
}
|
|
3227
|
-
this.name = `SmartRouter + ${this.activeRouter.name}`;
|
|
3228
|
-
return res;
|
|
3229
|
-
}
|
|
3230
|
-
get activeRouter() {
|
|
3231
|
-
if (this.#routes || this.#routers.length !== 1) {
|
|
3232
|
-
throw new Error("No active router has been determined yet.");
|
|
3233
|
-
}
|
|
3234
|
-
return this.#routers[0];
|
|
3235
|
-
}
|
|
3236
|
-
};
|
|
3237
|
-
|
|
3238
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/trie-router/node.js
|
|
3239
|
-
var emptyParams = /* @__PURE__ */ Object.create(null);
|
|
3240
|
-
var Node2 = class _Node2 {
|
|
3241
|
-
#methods;
|
|
3242
|
-
#children;
|
|
3243
|
-
#patterns;
|
|
3244
|
-
#order = 0;
|
|
3245
|
-
#params = emptyParams;
|
|
3246
|
-
constructor(method, handler, children) {
|
|
3247
|
-
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
3248
|
-
this.#methods = [];
|
|
3249
|
-
if (method && handler) {
|
|
3250
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
3251
|
-
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
3252
|
-
this.#methods = [m];
|
|
3253
|
-
}
|
|
3254
|
-
this.#patterns = [];
|
|
3255
|
-
}
|
|
3256
|
-
insert(method, path, handler) {
|
|
3257
|
-
this.#order = ++this.#order;
|
|
3258
|
-
let curNode = this;
|
|
3259
|
-
const parts = splitRoutingPath(path);
|
|
3260
|
-
const possibleKeys = [];
|
|
3261
|
-
for (let i = 0, len = parts.length; i < len; i++) {
|
|
3262
|
-
const p = parts[i];
|
|
3263
|
-
const nextP = parts[i + 1];
|
|
3264
|
-
const pattern = getPattern(p, nextP);
|
|
3265
|
-
const key = Array.isArray(pattern) ? pattern[0] : p;
|
|
3266
|
-
if (key in curNode.#children) {
|
|
3267
|
-
curNode = curNode.#children[key];
|
|
3268
|
-
if (pattern) {
|
|
3269
|
-
possibleKeys.push(pattern[1]);
|
|
3270
|
-
}
|
|
3271
|
-
continue;
|
|
3272
|
-
}
|
|
3273
|
-
curNode.#children[key] = new _Node2();
|
|
3274
|
-
if (pattern) {
|
|
3275
|
-
curNode.#patterns.push(pattern);
|
|
3276
|
-
possibleKeys.push(pattern[1]);
|
|
3277
|
-
}
|
|
3278
|
-
curNode = curNode.#children[key];
|
|
3279
|
-
}
|
|
3280
|
-
curNode.#methods.push({
|
|
3281
|
-
[method]: {
|
|
3282
|
-
handler,
|
|
3283
|
-
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
3284
|
-
score: this.#order
|
|
3285
|
-
}
|
|
3286
|
-
});
|
|
3287
|
-
return curNode;
|
|
3288
|
-
}
|
|
3289
|
-
#getHandlerSets(node, method, nodeParams, params) {
|
|
3290
|
-
const handlerSets = [];
|
|
3291
|
-
for (let i = 0, len = node.#methods.length; i < len; i++) {
|
|
3292
|
-
const m = node.#methods[i];
|
|
3293
|
-
const handlerSet = m[method] || m[METHOD_NAME_ALL];
|
|
3294
|
-
const processedSet = {};
|
|
3295
|
-
if (handlerSet !== void 0) {
|
|
3296
|
-
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
3297
|
-
handlerSets.push(handlerSet);
|
|
3298
|
-
if (nodeParams !== emptyParams || params && params !== emptyParams) {
|
|
3299
|
-
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
3300
|
-
const key = handlerSet.possibleKeys[i2];
|
|
3301
|
-
const processed = processedSet[handlerSet.score];
|
|
3302
|
-
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
|
|
3303
|
-
processedSet[handlerSet.score] = true;
|
|
3304
|
-
}
|
|
3305
|
-
}
|
|
3306
|
-
}
|
|
3307
|
-
}
|
|
3308
|
-
return handlerSets;
|
|
3309
|
-
}
|
|
3310
|
-
search(method, path) {
|
|
3311
|
-
const handlerSets = [];
|
|
3312
|
-
this.#params = emptyParams;
|
|
3313
|
-
const curNode = this;
|
|
3314
|
-
let curNodes = [curNode];
|
|
3315
|
-
const parts = splitPath(path);
|
|
3316
|
-
const curNodesQueue = [];
|
|
3317
|
-
for (let i = 0, len = parts.length; i < len; i++) {
|
|
3318
|
-
const part = parts[i];
|
|
3319
|
-
const isLast = i === len - 1;
|
|
3320
|
-
const tempNodes = [];
|
|
3321
|
-
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
3322
|
-
const node = curNodes[j];
|
|
3323
|
-
const nextNode = node.#children[part];
|
|
3324
|
-
if (nextNode) {
|
|
3325
|
-
nextNode.#params = node.#params;
|
|
3326
|
-
if (isLast) {
|
|
3327
|
-
if (nextNode.#children["*"]) {
|
|
3328
|
-
handlerSets.push(
|
|
3329
|
-
...this.#getHandlerSets(nextNode.#children["*"], method, node.#params)
|
|
3330
|
-
);
|
|
3331
|
-
}
|
|
3332
|
-
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params));
|
|
3333
|
-
} else {
|
|
3334
|
-
tempNodes.push(nextNode);
|
|
3335
|
-
}
|
|
3336
|
-
}
|
|
3337
|
-
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
|
|
3338
|
-
const pattern = node.#patterns[k];
|
|
3339
|
-
const params = node.#params === emptyParams ? {} : { ...node.#params };
|
|
3340
|
-
if (pattern === "*") {
|
|
3341
|
-
const astNode = node.#children["*"];
|
|
3342
|
-
if (astNode) {
|
|
3343
|
-
handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params));
|
|
3344
|
-
astNode.#params = params;
|
|
3345
|
-
tempNodes.push(astNode);
|
|
3346
|
-
}
|
|
3347
|
-
continue;
|
|
3348
|
-
}
|
|
3349
|
-
const [key, name, matcher] = pattern;
|
|
3350
|
-
if (!part && !(matcher instanceof RegExp)) {
|
|
3351
|
-
continue;
|
|
3352
|
-
}
|
|
3353
|
-
const child = node.#children[key];
|
|
3354
|
-
const restPathString = parts.slice(i).join("/");
|
|
3355
|
-
if (matcher instanceof RegExp) {
|
|
3356
|
-
const m = matcher.exec(restPathString);
|
|
3357
|
-
if (m) {
|
|
3358
|
-
params[name] = m[0];
|
|
3359
|
-
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
3360
|
-
if (Object.keys(child.#children).length) {
|
|
3361
|
-
child.#params = params;
|
|
3362
|
-
const componentCount = m[0].match(/\//)?.length ?? 0;
|
|
3363
|
-
const targetCurNodes = curNodesQueue[componentCount] ||= [];
|
|
3364
|
-
targetCurNodes.push(child);
|
|
3365
|
-
}
|
|
3366
|
-
continue;
|
|
3367
|
-
}
|
|
3368
|
-
}
|
|
3369
|
-
if (matcher === true || matcher.test(part)) {
|
|
3370
|
-
params[name] = part;
|
|
3371
|
-
if (isLast) {
|
|
3372
|
-
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
3373
|
-
if (child.#children["*"]) {
|
|
3374
|
-
handlerSets.push(
|
|
3375
|
-
...this.#getHandlerSets(child.#children["*"], method, params, node.#params)
|
|
3376
|
-
);
|
|
3377
|
-
}
|
|
3378
|
-
} else {
|
|
3379
|
-
child.#params = params;
|
|
3380
|
-
tempNodes.push(child);
|
|
3381
|
-
}
|
|
3382
|
-
}
|
|
3383
|
-
}
|
|
3384
|
-
}
|
|
3385
|
-
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
|
|
3386
|
-
}
|
|
3387
|
-
if (handlerSets.length > 1) {
|
|
3388
|
-
handlerSets.sort((a, b) => {
|
|
3389
|
-
return a.score - b.score;
|
|
3390
|
-
});
|
|
3391
|
-
}
|
|
3392
|
-
return [handlerSets.map(({ handler, params }) => [handler, params])];
|
|
3393
|
-
}
|
|
3394
|
-
};
|
|
3395
|
-
|
|
3396
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/router/trie-router/router.js
|
|
3397
|
-
var TrieRouter = class {
|
|
3398
|
-
name = "TrieRouter";
|
|
3399
|
-
#node;
|
|
3400
|
-
constructor() {
|
|
3401
|
-
this.#node = new Node2();
|
|
3402
|
-
}
|
|
3403
|
-
add(method, path, handler) {
|
|
3404
|
-
const results = checkOptionalParameter(path);
|
|
3405
|
-
if (results) {
|
|
3406
|
-
for (let i = 0, len = results.length; i < len; i++) {
|
|
3407
|
-
this.#node.insert(method, results[i], handler);
|
|
3408
|
-
}
|
|
3409
|
-
return;
|
|
3410
|
-
}
|
|
3411
|
-
this.#node.insert(method, path, handler);
|
|
3412
|
-
}
|
|
3413
|
-
match(method, path) {
|
|
3414
|
-
return this.#node.search(method, path);
|
|
3415
|
-
}
|
|
3416
|
-
};
|
|
3417
|
-
|
|
3418
|
-
// ../../node_modules/.deno/hono@4.11.3/node_modules/hono/dist/hono.js
|
|
3419
|
-
var Hono2 = class extends Hono {
|
|
3420
|
-
/**
|
|
3421
|
-
* Creates an instance of the Hono class.
|
|
3422
|
-
*
|
|
3423
|
-
* @param options - Optional configuration options for the Hono instance.
|
|
3424
|
-
*/
|
|
3425
|
-
constructor(options = {}) {
|
|
3426
|
-
super(options);
|
|
3427
|
-
this.router = options.router ?? new SmartRouter({
|
|
3428
|
-
routers: [new RegExpRouter(), new TrieRouter()]
|
|
3429
|
-
});
|
|
3430
|
-
}
|
|
3431
|
-
};
|
|
3432
|
-
|
|
3433
|
-
// src/server.ts
|
|
3434
|
-
async function handleError2(context, pluginOptions, error) {
|
|
3435
|
-
console.error(error);
|
|
3436
|
-
const loggerError = transformError(context, error);
|
|
3437
|
-
if (pluginOptions.postCommentOnError && loggerError) {
|
|
3438
|
-
await context.commentHandler.postComment(context, loggerError);
|
|
3439
|
-
}
|
|
3440
|
-
throw new HTTPException(500, { message: "Unexpected error" });
|
|
3441
|
-
}
|
|
3442
|
-
function createPlugin(handler, manifest, options) {
|
|
3443
|
-
const pluginOptions = getPluginOptions(options);
|
|
3444
|
-
const app = new Hono2();
|
|
3445
|
-
app.get("/manifest.json", (ctx) => {
|
|
3446
|
-
return ctx.json(manifest);
|
|
3447
|
-
});
|
|
3448
|
-
app.post("/", async function appPost(ctx) {
|
|
3449
|
-
if (ctx.req.header("content-type") !== "application/json") {
|
|
3450
|
-
throw new HTTPException(400, { message: "Content-Type must be application/json" });
|
|
3451
|
-
}
|
|
3452
|
-
const body = await ctx.req.json();
|
|
3453
|
-
const inputSchemaErrors = [...import_value4.Value.Errors(inputSchema, body)];
|
|
3454
|
-
if (inputSchemaErrors.length) {
|
|
3455
|
-
console.dir(inputSchemaErrors, { depth: null });
|
|
3456
|
-
throw new HTTPException(400, { message: "Invalid body" });
|
|
3457
|
-
}
|
|
3458
|
-
const signature = body.signature;
|
|
3459
|
-
if (!pluginOptions.bypassSignatureVerification && !await verifySignature(pluginOptions.kernelPublicKey, body, signature)) {
|
|
3460
|
-
throw new HTTPException(400, { message: "Invalid signature" });
|
|
3461
|
-
}
|
|
3462
|
-
const inputs = import_value4.Value.Decode(inputSchema, body);
|
|
3463
|
-
let config2;
|
|
3464
|
-
if (pluginOptions.settingsSchema) {
|
|
3465
|
-
try {
|
|
3466
|
-
config2 = import_value4.Value.Decode(pluginOptions.settingsSchema, import_value4.Value.Default(pluginOptions.settingsSchema, inputs.settings));
|
|
3467
|
-
} catch (e) {
|
|
3468
|
-
console.dir(...import_value4.Value.Errors(pluginOptions.settingsSchema, inputs.settings), { depth: null });
|
|
3469
|
-
throw e;
|
|
3470
|
-
}
|
|
3471
|
-
} else {
|
|
3472
|
-
config2 = inputs.settings;
|
|
3473
|
-
}
|
|
3474
|
-
let env2;
|
|
3475
|
-
const honoEnvironment = env(ctx);
|
|
3476
|
-
if (pluginOptions.envSchema) {
|
|
3477
|
-
try {
|
|
3478
|
-
env2 = import_value4.Value.Decode(pluginOptions.envSchema, import_value4.Value.Default(pluginOptions.envSchema, honoEnvironment));
|
|
3479
|
-
} catch (e) {
|
|
3480
|
-
console.dir(...import_value4.Value.Errors(pluginOptions.envSchema, honoEnvironment), { depth: null });
|
|
3481
|
-
throw e;
|
|
3482
|
-
}
|
|
3483
|
-
} else {
|
|
3484
|
-
env2 = ctx.env;
|
|
3485
|
-
}
|
|
3486
|
-
const workerName = new URL(inputs.ref).hostname.split(".")[0];
|
|
3487
|
-
PluginRuntimeInfo.getInstance({ ...env2, CLOUDFLARE_WORKER_NAME: workerName });
|
|
3488
|
-
const command = getCommand(inputs, pluginOptions);
|
|
3489
|
-
const context = {
|
|
3490
|
-
eventName: inputs.eventName,
|
|
3491
|
-
payload: inputs.eventPayload,
|
|
3492
|
-
command,
|
|
3493
|
-
authToken: inputs.authToken,
|
|
3494
|
-
ubiquityKernelToken: inputs.ubiquityKernelToken,
|
|
3495
|
-
octokit: new customOctokit({ auth: inputs.authToken }),
|
|
3496
|
-
config: config2,
|
|
3497
|
-
env: env2,
|
|
3498
|
-
logger: new Logs(pluginOptions.logLevel),
|
|
3499
|
-
commentHandler: new CommentHandler()
|
|
3500
|
-
};
|
|
3501
|
-
try {
|
|
3502
|
-
const result = await handler(context);
|
|
3503
|
-
return ctx.json({ stateId: inputs.stateId, output: result ?? {} });
|
|
3504
|
-
} catch (error) {
|
|
3505
|
-
await handleError2(context, pluginOptions, error);
|
|
3506
|
-
}
|
|
3507
|
-
});
|
|
3508
|
-
return app;
|
|
3509
|
-
}
|
|
3510
|
-
|
|
3511
|
-
// src/llm/index.ts
|
|
3512
|
-
var EMPTY_STRING = "";
|
|
3513
|
-
function normalizeBaseUrl(baseUrl) {
|
|
3514
|
-
let normalized = baseUrl.trim();
|
|
3515
|
-
while (normalized.endsWith("/")) {
|
|
3516
|
-
normalized = normalized.slice(0, -1);
|
|
770
|
+
// src/llm/index.ts
|
|
771
|
+
var EMPTY_STRING = "";
|
|
772
|
+
function normalizeBaseUrl(baseUrl) {
|
|
773
|
+
let normalized = baseUrl.trim();
|
|
774
|
+
while (normalized.endsWith("/")) {
|
|
775
|
+
normalized = normalized.slice(0, -1);
|
|
3517
776
|
}
|
|
3518
777
|
return normalized;
|
|
3519
778
|
}
|
|
@@ -3608,6 +867,10 @@ async function fetchWithRetry(url, options, maxRetries) {
|
|
|
3608
867
|
throw error;
|
|
3609
868
|
} catch (error) {
|
|
3610
869
|
lastError = error;
|
|
870
|
+
const status = typeof error.status === "number" ? error.status : void 0;
|
|
871
|
+
if (typeof status === "number" && status < 500) {
|
|
872
|
+
throw error;
|
|
873
|
+
}
|
|
3611
874
|
if (attempt >= maxRetries) throw error;
|
|
3612
875
|
await sleep(getRetryDelayMs(attempt));
|
|
3613
876
|
attempt += 1;
|
|
@@ -3683,6 +946,13 @@ function parseEventData(data) {
|
|
|
3683
946
|
try {
|
|
3684
947
|
return JSON.parse(data);
|
|
3685
948
|
} catch (error) {
|
|
949
|
+
if (data.includes("\n")) {
|
|
950
|
+
const collapsed = data.replace(/\n/g, EMPTY_STRING);
|
|
951
|
+
try {
|
|
952
|
+
return JSON.parse(collapsed);
|
|
953
|
+
} catch {
|
|
954
|
+
}
|
|
955
|
+
}
|
|
3686
956
|
const message = error instanceof Error ? error.message : String(error);
|
|
3687
957
|
const preview = data.length > 200 ? `${data.slice(0, 200)}...` : data;
|
|
3688
958
|
throw new Error(`LLM stream parse error: ${message}. Data: ${preview}`);
|