@mokup/server 1.2.0 → 1.2.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/dist/connect.cjs +1 -1
- package/dist/connect.mjs +1 -1
- package/dist/express.cjs +1 -1
- package/dist/express.mjs +1 -1
- package/dist/fastify.cjs +1 -1
- package/dist/fastify.mjs +1 -1
- package/dist/fetch-server.cjs +76 -562
- package/dist/fetch-server.mjs +71 -553
- package/dist/fetch.cjs +1 -1
- package/dist/fetch.mjs +1 -1
- package/dist/hono.cjs +1 -1
- package/dist/hono.mjs +1 -1
- package/dist/index.cjs +8 -160
- package/dist/index.d.cts +4 -41
- package/dist/index.d.mts +4 -41
- package/dist/index.d.ts +4 -41
- package/dist/index.mjs +8 -160
- package/dist/koa.cjs +1 -1
- package/dist/koa.mjs +1 -1
- package/dist/node.cjs +10 -6
- package/dist/node.mjs +10 -6
- package/dist/shared/{server.LbftO9Jh.mjs → server.B5ThEHd_.mjs} +1 -1
- package/dist/shared/{server.aaygIV2Q.cjs → server.D0X6h49r.cjs} +1 -1
- package/dist/worker-node.cjs +1 -1
- package/dist/worker-node.mjs +1 -1
- package/dist/worker.cjs +1 -1
- package/dist/worker.mjs +1 -1
- package/package.json +6 -11
package/dist/fetch-server.mjs
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { join, normalize, extname
|
|
1
|
+
import { join, normalize, extname } from '@mokup/shared/pathe';
|
|
2
2
|
import { promises } from 'node:fs';
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { resolveRouteGroup, formatRouteFile, resolveGroupRoot, resolveGroups } from '@mokup/shared/playground-grouping';
|
|
5
5
|
import { compareRouteScore, parseRouteTemplate } from '@mokup/runtime';
|
|
6
|
-
import {
|
|
6
|
+
import { createRouteUtils } from '@mokup/shared/route-utils';
|
|
7
|
+
import { resolveDirectoryConfig as resolveDirectoryConfig$1, isPromise } from '@mokup/shared/config-utils';
|
|
8
|
+
import { loadModule } from '@mokup/shared/module-loader';
|
|
9
|
+
import { configExtensions as configExtensions$1 } from '@mokup/shared/route-constants';
|
|
10
|
+
import { collectFiles, isConfigFile, isSupportedFile } from '@mokup/shared/mock-files';
|
|
11
|
+
import { loadRules as loadRules$1 } from '@mokup/shared/load-rules';
|
|
12
|
+
import { resolveDirs as resolveDirs$1, normalizeIgnorePrefix as normalizeIgnorePrefix$1 } from '@mokup/shared/scan-utils';
|
|
13
|
+
import { hasIgnoredPrefix, matchesFilter, isInDirs } from '@mokup/shared/path-utils';
|
|
7
14
|
import { delay, createDebouncer } from '@mokup/shared/timing';
|
|
8
|
-
import { Buffer } from 'node:buffer';
|
|
9
|
-
import { pathToFileURL } from 'node:url';
|
|
10
|
-
import { build } from '@mokup/shared/esbuild';
|
|
11
|
-
import process, { cwd } from 'node:process';
|
|
12
|
-
import { parse } from '@mokup/shared/jsonc-parser';
|
|
13
15
|
import { Hono, PatternRouter } from '@mokup/shared/hono';
|
|
16
|
+
import { cwd } from 'node:process';
|
|
14
17
|
|
|
15
18
|
const silentLogger$1 = {
|
|
16
19
|
info: () => {
|
|
@@ -267,278 +270,51 @@ function registerPlaygroundRoutes(params) {
|
|
|
267
270
|
});
|
|
268
271
|
}
|
|
269
272
|
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
"PATCH",
|
|
275
|
-
"DELETE",
|
|
276
|
-
"OPTIONS",
|
|
277
|
-
"HEAD"
|
|
278
|
-
]);
|
|
279
|
-
const methodSuffixSet = new Set(
|
|
280
|
-
Array.from(methodSet, (method) => method.toLowerCase())
|
|
281
|
-
);
|
|
282
|
-
const supportedExtensions = /* @__PURE__ */ new Set([
|
|
283
|
-
".json",
|
|
284
|
-
".jsonc",
|
|
285
|
-
".ts",
|
|
286
|
-
".js",
|
|
287
|
-
".mjs",
|
|
288
|
-
".cjs"
|
|
289
|
-
]);
|
|
290
|
-
const configExtensions = [".ts", ".js", ".mjs", ".cjs"];
|
|
291
|
-
|
|
292
|
-
function normalizePrefix(prefix) {
|
|
293
|
-
if (!prefix) {
|
|
294
|
-
return "";
|
|
295
|
-
}
|
|
296
|
-
const normalized = prefix.startsWith("/") ? prefix : `/${prefix}`;
|
|
297
|
-
return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
298
|
-
}
|
|
299
|
-
function resolveDirs(dir, root) {
|
|
300
|
-
const raw = typeof dir === "function" ? dir(root) : dir;
|
|
301
|
-
const resolved = Array.isArray(raw) ? raw : raw ? [raw] : ["mock"];
|
|
302
|
-
const normalized = resolved.map(
|
|
303
|
-
(entry) => isAbsolute(entry) ? entry : resolve(root, entry)
|
|
304
|
-
);
|
|
305
|
-
return Array.from(new Set(normalized));
|
|
306
|
-
}
|
|
307
|
-
function normalizeIgnorePrefix(value, fallback = ["."]) {
|
|
308
|
-
const list = typeof value === "undefined" ? fallback : Array.isArray(value) ? value : [value];
|
|
309
|
-
return list.filter((entry) => typeof entry === "string" && entry.length > 0);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const jsonExtensions = /* @__PURE__ */ new Set([".json", ".jsonc"]);
|
|
313
|
-
function resolveTemplate(template, prefix) {
|
|
314
|
-
const normalized = template.startsWith("/") ? template : `/${template}`;
|
|
315
|
-
if (!prefix) {
|
|
316
|
-
return normalized;
|
|
317
|
-
}
|
|
318
|
-
const normalizedPrefix = normalizePrefix(prefix);
|
|
319
|
-
if (!normalizedPrefix) {
|
|
320
|
-
return normalized;
|
|
321
|
-
}
|
|
322
|
-
if (normalized === normalizedPrefix || normalized.startsWith(`${normalizedPrefix}/`)) {
|
|
323
|
-
return normalized;
|
|
324
|
-
}
|
|
325
|
-
if (normalized === "/") {
|
|
326
|
-
return `${normalizedPrefix}/`;
|
|
327
|
-
}
|
|
328
|
-
return `${normalizedPrefix}${normalized}`;
|
|
329
|
-
}
|
|
330
|
-
function stripMethodSuffix(base) {
|
|
331
|
-
const segments = base.split(".");
|
|
332
|
-
const last = segments.at(-1);
|
|
333
|
-
if (last && methodSuffixSet.has(last.toLowerCase())) {
|
|
334
|
-
segments.pop();
|
|
335
|
-
return {
|
|
336
|
-
name: segments.join("."),
|
|
337
|
-
method: last.toUpperCase()
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
return {
|
|
341
|
-
name: base,
|
|
342
|
-
method: void 0
|
|
343
|
-
};
|
|
344
|
-
}
|
|
273
|
+
const routeUtils = createRouteUtils({
|
|
274
|
+
parseRouteTemplate,
|
|
275
|
+
compareRouteScore
|
|
276
|
+
});
|
|
345
277
|
function deriveRouteFromFile(file, rootDir, logger) {
|
|
346
|
-
|
|
347
|
-
const ext = extname(rel);
|
|
348
|
-
const withoutExt = rel.slice(0, rel.length - ext.length);
|
|
349
|
-
const dir = dirname(withoutExt);
|
|
350
|
-
const base = basename(withoutExt);
|
|
351
|
-
const { name, method } = stripMethodSuffix(base);
|
|
352
|
-
const resolvedMethod = method ?? (jsonExtensions.has(ext) ? "GET" : void 0);
|
|
353
|
-
if (!resolvedMethod) {
|
|
354
|
-
logger.warn(`Skip mock without method suffix: ${file}`);
|
|
355
|
-
return null;
|
|
356
|
-
}
|
|
357
|
-
if (!name) {
|
|
358
|
-
logger.warn(`Skip mock with empty route name: ${file}`);
|
|
359
|
-
return null;
|
|
360
|
-
}
|
|
361
|
-
const joined = dir === "." ? name : join(dir, name);
|
|
362
|
-
const segments = toPosix(joined).split("/");
|
|
363
|
-
if (segments.at(-1) === "index") {
|
|
364
|
-
segments.pop();
|
|
365
|
-
}
|
|
366
|
-
const template = segments.length === 0 ? "/" : `/${segments.join("/")}`;
|
|
367
|
-
const parsed = parseRouteTemplate(template);
|
|
368
|
-
if (parsed.errors.length > 0) {
|
|
369
|
-
for (const error of parsed.errors) {
|
|
370
|
-
logger.warn(`${error} in ${file}`);
|
|
371
|
-
}
|
|
372
|
-
return null;
|
|
373
|
-
}
|
|
374
|
-
for (const warning of parsed.warnings) {
|
|
375
|
-
logger.warn(`${warning} in ${file}`);
|
|
376
|
-
}
|
|
377
|
-
return {
|
|
378
|
-
template: parsed.template,
|
|
379
|
-
method: resolvedMethod,
|
|
380
|
-
tokens: parsed.tokens,
|
|
381
|
-
score: parsed.score
|
|
382
|
-
};
|
|
278
|
+
return routeUtils.deriveRouteFromFile(file, rootDir, (message) => logger.warn(message));
|
|
383
279
|
}
|
|
384
280
|
function resolveRule(params) {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
const template = resolveTemplate(params.derivedTemplate, params.prefix);
|
|
391
|
-
const parsed = parseRouteTemplate(template);
|
|
392
|
-
if (parsed.errors.length > 0) {
|
|
393
|
-
for (const error of parsed.errors) {
|
|
394
|
-
params.logger.warn(`${error} in ${params.file}`);
|
|
395
|
-
}
|
|
396
|
-
return null;
|
|
397
|
-
}
|
|
398
|
-
for (const warning of parsed.warnings) {
|
|
399
|
-
params.logger.warn(`${warning} in ${params.file}`);
|
|
400
|
-
}
|
|
401
|
-
const route = {
|
|
281
|
+
return routeUtils.resolveRule({
|
|
282
|
+
rule: params.rule,
|
|
283
|
+
derivedTemplate: params.derivedTemplate,
|
|
284
|
+
derivedMethod: params.derivedMethod,
|
|
285
|
+
prefix: params.prefix,
|
|
402
286
|
file: params.file,
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
if (typeof params.rule.delay === "number") {
|
|
416
|
-
route.delay = params.rule.delay;
|
|
417
|
-
}
|
|
418
|
-
return route;
|
|
419
|
-
}
|
|
420
|
-
function sortRoutes(routes) {
|
|
421
|
-
return routes.sort((a, b) => {
|
|
422
|
-
if (a.method !== b.method) {
|
|
423
|
-
return a.method.localeCompare(b.method);
|
|
424
|
-
}
|
|
425
|
-
const scoreCompare = compareRouteScore(a.score, b.score);
|
|
426
|
-
if (scoreCompare !== 0) {
|
|
427
|
-
return scoreCompare;
|
|
428
|
-
}
|
|
429
|
-
return a.template.localeCompare(b.template);
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
let registerPromise = null;
|
|
434
|
-
let hasLoggedFailure = false;
|
|
435
|
-
async function ensureTsxRegister(logger) {
|
|
436
|
-
if (registerPromise) {
|
|
437
|
-
return registerPromise;
|
|
438
|
-
}
|
|
439
|
-
registerPromise = (async () => {
|
|
440
|
-
try {
|
|
441
|
-
const mod = await import('tsx/esm/api');
|
|
442
|
-
const setSourceMapsEnabled = process.setSourceMapsEnabled;
|
|
443
|
-
if (typeof setSourceMapsEnabled === "function") {
|
|
444
|
-
setSourceMapsEnabled(true);
|
|
445
|
-
}
|
|
446
|
-
if (typeof mod.register === "function") {
|
|
447
|
-
mod.register();
|
|
287
|
+
warn: (message) => params.logger.warn(message),
|
|
288
|
+
build: (base, rule) => {
|
|
289
|
+
const route = {
|
|
290
|
+
file: params.file,
|
|
291
|
+
template: base.template,
|
|
292
|
+
method: base.method,
|
|
293
|
+
tokens: base.tokens,
|
|
294
|
+
score: base.score,
|
|
295
|
+
handler: rule.handler
|
|
296
|
+
};
|
|
297
|
+
if (typeof rule.status === "number") {
|
|
298
|
+
route.status = rule.status;
|
|
448
299
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
if (!hasLoggedFailure && logger) {
|
|
452
|
-
logger.warn(
|
|
453
|
-
"Failed to register tsx loader; falling back to bundled TS loader.",
|
|
454
|
-
error
|
|
455
|
-
);
|
|
456
|
-
hasLoggedFailure = true;
|
|
300
|
+
if (rule.headers) {
|
|
301
|
+
route.headers = rule.headers;
|
|
457
302
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
})();
|
|
461
|
-
return registerPromise;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
const middlewareSymbol = Symbol.for("mokup.config.middlewares");
|
|
465
|
-
function isUnknownFileExtensionError$1(error) {
|
|
466
|
-
if (!error || typeof error !== "object") {
|
|
467
|
-
return false;
|
|
468
|
-
}
|
|
469
|
-
const code = error.code;
|
|
470
|
-
if (code === "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
471
|
-
return true;
|
|
472
|
-
}
|
|
473
|
-
const message = error.message;
|
|
474
|
-
return typeof message === "string" && message.includes("Unknown file extension");
|
|
475
|
-
}
|
|
476
|
-
async function loadTsModule$1(file, logger) {
|
|
477
|
-
const cacheBust = Date.now();
|
|
478
|
-
const fileUrl = `${pathToFileURL(file).href}?t=${cacheBust}`;
|
|
479
|
-
const registered = await ensureTsxRegister(logger);
|
|
480
|
-
if (registered) {
|
|
481
|
-
try {
|
|
482
|
-
return await import(fileUrl);
|
|
483
|
-
} catch (error) {
|
|
484
|
-
if (!isUnknownFileExtensionError$1(error)) {
|
|
485
|
-
throw error;
|
|
303
|
+
if (typeof rule.delay === "number") {
|
|
304
|
+
route.delay = rule.delay;
|
|
486
305
|
}
|
|
306
|
+
return route;
|
|
487
307
|
}
|
|
488
|
-
}
|
|
489
|
-
const result = await build({
|
|
490
|
-
entryPoints: [file],
|
|
491
|
-
bundle: true,
|
|
492
|
-
format: "esm",
|
|
493
|
-
platform: "node",
|
|
494
|
-
sourcemap: "inline",
|
|
495
|
-
target: "es2020",
|
|
496
|
-
write: false
|
|
497
308
|
});
|
|
498
|
-
const output = result.outputFiles[0];
|
|
499
|
-
const code = output?.text ?? "";
|
|
500
|
-
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString(
|
|
501
|
-
"base64"
|
|
502
|
-
)}`;
|
|
503
|
-
return import(`${dataUrl}#${cacheBust}`);
|
|
504
|
-
}
|
|
505
|
-
async function loadModule$1(file, logger) {
|
|
506
|
-
const ext = configExtensions.find((extension) => file.endsWith(extension));
|
|
507
|
-
if (ext === ".cjs") {
|
|
508
|
-
const require = createRequire(import.meta.url);
|
|
509
|
-
delete require.cache[file];
|
|
510
|
-
return require(file);
|
|
511
|
-
}
|
|
512
|
-
if (ext === ".js" || ext === ".mjs") {
|
|
513
|
-
return import(`${pathToFileURL(file).href}?t=${Date.now()}`);
|
|
514
|
-
}
|
|
515
|
-
if (ext === ".ts") {
|
|
516
|
-
return loadTsModule$1(file, logger);
|
|
517
|
-
}
|
|
518
|
-
return null;
|
|
519
|
-
}
|
|
520
|
-
function getConfigFileCandidates(dir) {
|
|
521
|
-
return configExtensions.map((extension) => join(dir, `index.config${extension}`));
|
|
522
309
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (cached !== void 0) {
|
|
526
|
-
return cached;
|
|
527
|
-
}
|
|
528
|
-
for (const candidate of getConfigFileCandidates(dir)) {
|
|
529
|
-
try {
|
|
530
|
-
await promises.stat(candidate);
|
|
531
|
-
cache.set(dir, candidate);
|
|
532
|
-
return candidate;
|
|
533
|
-
} catch {
|
|
534
|
-
continue;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
cache.set(dir, null);
|
|
538
|
-
return null;
|
|
310
|
+
function sortRoutes(routes) {
|
|
311
|
+
return routeUtils.sortRoutes(routes);
|
|
539
312
|
}
|
|
540
|
-
|
|
541
|
-
|
|
313
|
+
|
|
314
|
+
const configExtensions = configExtensions$1;
|
|
315
|
+
|
|
316
|
+
async function loadConfig(file) {
|
|
317
|
+
const mod = await loadModule(file);
|
|
542
318
|
if (!mod) {
|
|
543
319
|
return null;
|
|
544
320
|
}
|
|
@@ -549,301 +325,43 @@ async function loadConfig(file, logger) {
|
|
|
549
325
|
}
|
|
550
326
|
return value;
|
|
551
327
|
}
|
|
552
|
-
function
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
continue;
|
|
565
|
-
}
|
|
566
|
-
middlewares.push({
|
|
567
|
-
handle: entry,
|
|
328
|
+
async function resolveDirectoryConfig(params) {
|
|
329
|
+
const { file, rootDir, logger, configCache, fileCache } = params;
|
|
330
|
+
const resolved = await resolveDirectoryConfig$1({
|
|
331
|
+
file,
|
|
332
|
+
rootDir,
|
|
333
|
+
configExtensions,
|
|
334
|
+
configCache,
|
|
335
|
+
fileCache,
|
|
336
|
+
loadConfig,
|
|
337
|
+
warn: (message) => logger.warn(message),
|
|
338
|
+
mapMiddleware: (handler, index, position, source) => ({
|
|
339
|
+
handle: handler,
|
|
568
340
|
source,
|
|
569
341
|
index,
|
|
570
342
|
position
|
|
571
|
-
})
|
|
572
|
-
}
|
|
573
|
-
return middlewares;
|
|
574
|
-
}
|
|
575
|
-
function readMiddlewareMeta(config) {
|
|
576
|
-
const value = config[middlewareSymbol];
|
|
577
|
-
if (!value || typeof value !== "object") {
|
|
578
|
-
return null;
|
|
579
|
-
}
|
|
580
|
-
const meta = value;
|
|
581
|
-
return {
|
|
582
|
-
pre: Array.isArray(meta.pre) ? meta.pre : [],
|
|
583
|
-
normal: Array.isArray(meta.normal) ? meta.normal : [],
|
|
584
|
-
post: Array.isArray(meta.post) ? meta.post : []
|
|
585
|
-
};
|
|
586
|
-
}
|
|
587
|
-
async function resolveDirectoryConfig(params) {
|
|
588
|
-
const { file, rootDir, logger, configCache, fileCache } = params;
|
|
589
|
-
const resolvedRoot = normalize(rootDir);
|
|
590
|
-
const resolvedFileDir = normalize(dirname(file));
|
|
591
|
-
const chain = [];
|
|
592
|
-
let current = resolvedFileDir;
|
|
593
|
-
while (true) {
|
|
594
|
-
chain.push(current);
|
|
595
|
-
if (current === resolvedRoot) {
|
|
596
|
-
break;
|
|
597
|
-
}
|
|
598
|
-
const parent = dirname(current);
|
|
599
|
-
if (parent === current) {
|
|
600
|
-
break;
|
|
601
|
-
}
|
|
602
|
-
current = parent;
|
|
603
|
-
}
|
|
604
|
-
chain.reverse();
|
|
605
|
-
const merged = {};
|
|
606
|
-
const preMiddlewares = [];
|
|
607
|
-
const normalMiddlewares = [];
|
|
608
|
-
const postMiddlewares = [];
|
|
609
|
-
for (const dir of chain) {
|
|
610
|
-
const configPath = await findConfigFile(dir, fileCache);
|
|
611
|
-
if (!configPath) {
|
|
612
|
-
continue;
|
|
613
|
-
}
|
|
614
|
-
let config = configCache.get(configPath);
|
|
615
|
-
if (config === void 0) {
|
|
616
|
-
config = await loadConfig(configPath, logger);
|
|
617
|
-
configCache.set(configPath, config);
|
|
618
|
-
}
|
|
619
|
-
if (!config) {
|
|
620
|
-
logger.warn(`Invalid config in ${configPath}`);
|
|
621
|
-
continue;
|
|
622
|
-
}
|
|
623
|
-
if (config.headers) {
|
|
624
|
-
merged.headers = { ...merged.headers ?? {}, ...config.headers };
|
|
625
|
-
}
|
|
626
|
-
if (typeof config.status === "number") {
|
|
627
|
-
merged.status = config.status;
|
|
628
|
-
}
|
|
629
|
-
if (typeof config.delay === "number") {
|
|
630
|
-
merged.delay = config.delay;
|
|
631
|
-
}
|
|
632
|
-
if (typeof config.enabled === "boolean") {
|
|
633
|
-
merged.enabled = config.enabled;
|
|
634
|
-
}
|
|
635
|
-
if (typeof config.ignorePrefix !== "undefined") {
|
|
636
|
-
merged.ignorePrefix = config.ignorePrefix;
|
|
637
|
-
}
|
|
638
|
-
if (typeof config.include !== "undefined") {
|
|
639
|
-
merged.include = config.include;
|
|
640
|
-
}
|
|
641
|
-
if (typeof config.exclude !== "undefined") {
|
|
642
|
-
merged.exclude = config.exclude;
|
|
643
|
-
}
|
|
644
|
-
const meta = readMiddlewareMeta(config);
|
|
645
|
-
const normalizedPre = normalizeMiddlewares(
|
|
646
|
-
meta?.pre,
|
|
647
|
-
configPath,
|
|
648
|
-
logger,
|
|
649
|
-
"pre"
|
|
650
|
-
);
|
|
651
|
-
const normalizedNormal = normalizeMiddlewares(
|
|
652
|
-
meta?.normal,
|
|
653
|
-
configPath,
|
|
654
|
-
logger,
|
|
655
|
-
"normal"
|
|
656
|
-
);
|
|
657
|
-
const normalizedLegacy = normalizeMiddlewares(
|
|
658
|
-
config.middleware,
|
|
659
|
-
configPath,
|
|
660
|
-
logger,
|
|
661
|
-
"normal"
|
|
662
|
-
);
|
|
663
|
-
const normalizedPost = normalizeMiddlewares(
|
|
664
|
-
meta?.post,
|
|
665
|
-
configPath,
|
|
666
|
-
logger,
|
|
667
|
-
"post"
|
|
668
|
-
);
|
|
669
|
-
if (normalizedPre.length > 0) {
|
|
670
|
-
preMiddlewares.push(...normalizedPre);
|
|
671
|
-
}
|
|
672
|
-
if (normalizedNormal.length > 0) {
|
|
673
|
-
normalMiddlewares.push(...normalizedNormal);
|
|
674
|
-
}
|
|
675
|
-
if (normalizedLegacy.length > 0) {
|
|
676
|
-
normalMiddlewares.push(...normalizedLegacy);
|
|
677
|
-
}
|
|
678
|
-
if (normalizedPost.length > 0) {
|
|
679
|
-
postMiddlewares.push(...normalizedPost);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
343
|
+
})
|
|
344
|
+
});
|
|
682
345
|
return {
|
|
683
|
-
|
|
684
|
-
|
|
346
|
+
headers: resolved.headers,
|
|
347
|
+
status: resolved.status,
|
|
348
|
+
delay: resolved.delay,
|
|
349
|
+
enabled: resolved.enabled,
|
|
350
|
+
ignorePrefix: resolved.ignorePrefix,
|
|
351
|
+
include: resolved.include,
|
|
352
|
+
exclude: resolved.exclude,
|
|
353
|
+
middlewares: resolved.middlewares
|
|
685
354
|
};
|
|
686
355
|
}
|
|
687
356
|
|
|
688
|
-
async function
|
|
689
|
-
|
|
690
|
-
for (const entry of entries) {
|
|
691
|
-
if (entry.name === "node_modules" || entry.name === ".git") {
|
|
692
|
-
continue;
|
|
693
|
-
}
|
|
694
|
-
const fullPath = join(dir, entry.name);
|
|
695
|
-
if (entry.isDirectory()) {
|
|
696
|
-
await walkDir(fullPath, rootDir, files);
|
|
697
|
-
continue;
|
|
698
|
-
}
|
|
699
|
-
if (entry.isFile()) {
|
|
700
|
-
files.push({ file: fullPath, rootDir });
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
async function exists(path) {
|
|
705
|
-
try {
|
|
706
|
-
await promises.stat(path);
|
|
707
|
-
return true;
|
|
708
|
-
} catch {
|
|
709
|
-
return false;
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
async function collectFiles(dirs) {
|
|
713
|
-
const files = [];
|
|
714
|
-
for (const dir of dirs) {
|
|
715
|
-
if (!await exists(dir)) {
|
|
716
|
-
continue;
|
|
717
|
-
}
|
|
718
|
-
await walkDir(dir, dir, files);
|
|
719
|
-
}
|
|
720
|
-
return files;
|
|
721
|
-
}
|
|
722
|
-
function isConfigFile(file) {
|
|
723
|
-
if (file.endsWith(".d.ts")) {
|
|
724
|
-
return false;
|
|
725
|
-
}
|
|
726
|
-
const base = basename(file);
|
|
727
|
-
if (!base.startsWith("index.config.")) {
|
|
728
|
-
return false;
|
|
729
|
-
}
|
|
730
|
-
const ext = extname(file).toLowerCase();
|
|
731
|
-
return configExtensions.includes(ext);
|
|
732
|
-
}
|
|
733
|
-
function isSupportedFile(file) {
|
|
734
|
-
if (file.endsWith(".d.ts")) {
|
|
735
|
-
return false;
|
|
736
|
-
}
|
|
737
|
-
if (isConfigFile(file)) {
|
|
738
|
-
return false;
|
|
739
|
-
}
|
|
740
|
-
const ext = extname(file).toLowerCase();
|
|
741
|
-
return supportedExtensions.has(ext);
|
|
357
|
+
async function loadRules(file, logger) {
|
|
358
|
+
return loadRules$1(file, { loadModule, logger });
|
|
742
359
|
}
|
|
743
360
|
|
|
744
|
-
function
|
|
745
|
-
|
|
746
|
-
return false;
|
|
747
|
-
}
|
|
748
|
-
const code = error.code;
|
|
749
|
-
if (code === "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
750
|
-
return true;
|
|
751
|
-
}
|
|
752
|
-
const message = error.message;
|
|
753
|
-
return typeof message === "string" && message.includes("Unknown file extension");
|
|
754
|
-
}
|
|
755
|
-
async function loadTsModule(file, logger) {
|
|
756
|
-
const cacheBust = Date.now();
|
|
757
|
-
const fileUrl = `${pathToFileURL(file).href}?t=${cacheBust}`;
|
|
758
|
-
const registered = await ensureTsxRegister(logger);
|
|
759
|
-
if (registered) {
|
|
760
|
-
try {
|
|
761
|
-
return await import(fileUrl);
|
|
762
|
-
} catch (error) {
|
|
763
|
-
if (!isUnknownFileExtensionError(error)) {
|
|
764
|
-
throw error;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
const result = await build({
|
|
769
|
-
entryPoints: [file],
|
|
770
|
-
bundle: true,
|
|
771
|
-
format: "esm",
|
|
772
|
-
platform: "node",
|
|
773
|
-
sourcemap: "inline",
|
|
774
|
-
target: "es2020",
|
|
775
|
-
write: false
|
|
776
|
-
});
|
|
777
|
-
const output = result.outputFiles[0];
|
|
778
|
-
const code = output?.text ?? "";
|
|
779
|
-
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString(
|
|
780
|
-
"base64"
|
|
781
|
-
)}`;
|
|
782
|
-
return import(`${dataUrl}#${cacheBust}`);
|
|
783
|
-
}
|
|
784
|
-
async function loadModule(file, logger) {
|
|
785
|
-
const ext = extname(file).toLowerCase();
|
|
786
|
-
if (ext === ".cjs") {
|
|
787
|
-
const require = createRequire(import.meta.url);
|
|
788
|
-
delete require.cache[file];
|
|
789
|
-
return require(file);
|
|
790
|
-
}
|
|
791
|
-
if (ext === ".js" || ext === ".mjs") {
|
|
792
|
-
return import(`${pathToFileURL(file).href}?t=${Date.now()}`);
|
|
793
|
-
}
|
|
794
|
-
if (ext === ".ts") {
|
|
795
|
-
return loadTsModule(file, logger);
|
|
796
|
-
}
|
|
797
|
-
return null;
|
|
798
|
-
}
|
|
799
|
-
async function readJsonFile(file, logger) {
|
|
800
|
-
try {
|
|
801
|
-
const content = await promises.readFile(file, "utf8");
|
|
802
|
-
const errors = [];
|
|
803
|
-
const data = parse(content, errors, {
|
|
804
|
-
allowTrailingComma: true,
|
|
805
|
-
disallowComments: false
|
|
806
|
-
});
|
|
807
|
-
if (errors.length > 0) {
|
|
808
|
-
logger.warn(`Invalid JSONC in ${file}`);
|
|
809
|
-
return void 0;
|
|
810
|
-
}
|
|
811
|
-
return data;
|
|
812
|
-
} catch (error) {
|
|
813
|
-
logger.warn(`Failed to read ${file}: ${String(error)}`);
|
|
814
|
-
return void 0;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
async function loadRules(file, logger) {
|
|
818
|
-
const ext = extname(file).toLowerCase();
|
|
819
|
-
if (ext === ".json" || ext === ".jsonc") {
|
|
820
|
-
const json = await readJsonFile(file, logger);
|
|
821
|
-
if (typeof json === "undefined") {
|
|
822
|
-
return [];
|
|
823
|
-
}
|
|
824
|
-
return [
|
|
825
|
-
{
|
|
826
|
-
handler: json
|
|
827
|
-
}
|
|
828
|
-
];
|
|
829
|
-
}
|
|
830
|
-
const mod = await loadModule(file, logger);
|
|
831
|
-
const value = mod?.default ?? mod;
|
|
832
|
-
if (!value) {
|
|
833
|
-
return [];
|
|
834
|
-
}
|
|
835
|
-
if (Array.isArray(value)) {
|
|
836
|
-
return value;
|
|
837
|
-
}
|
|
838
|
-
if (typeof value === "function") {
|
|
839
|
-
return [
|
|
840
|
-
{
|
|
841
|
-
handler: value
|
|
842
|
-
}
|
|
843
|
-
];
|
|
844
|
-
}
|
|
845
|
-
return [value];
|
|
361
|
+
function resolveDirs(dir, root) {
|
|
362
|
+
return resolveDirs$1(dir, root);
|
|
846
363
|
}
|
|
364
|
+
const normalizeIgnorePrefix = normalizeIgnorePrefix$1;
|
|
847
365
|
|
|
848
366
|
const silentLogger = {
|
|
849
367
|
info: () => {
|
package/dist/fetch.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const runtime = require('@mokup/runtime');
|
|
4
|
-
const runtime$1 = require('./shared/server.
|
|
4
|
+
const runtime$1 = require('./shared/server.D0X6h49r.cjs');
|
|
5
5
|
|
|
6
6
|
function createFetchHandler(options) {
|
|
7
7
|
const runtime$2 = runtime.createRuntime(runtime$1.toRuntimeOptions(options));
|
package/dist/fetch.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRuntime } from '@mokup/runtime';
|
|
2
|
-
import { t as toRuntimeOptions, d as toRuntimeRequestFromFetch, e as toArrayBuffer } from './shared/server.
|
|
2
|
+
import { t as toRuntimeOptions, d as toRuntimeRequestFromFetch, e as toArrayBuffer } from './shared/server.B5ThEHd_.mjs';
|
|
3
3
|
|
|
4
4
|
function createFetchHandler(options) {
|
|
5
5
|
const runtime = createRuntime(toRuntimeOptions(options));
|
package/dist/hono.cjs
CHANGED
package/dist/hono.mjs
CHANGED