mcp-apps-inspector 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/cli.js +49 -7
- package/dist/config.d.ts +43 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4725 -3
- package/dist/server/diagnostics.d.ts +10 -0
- package/dist/server/probe.d.ts +2 -0
- package/dist/server/workbench.d.ts +4 -0
- package/dist/types.d.ts +29 -0
- package/dist/web/index.html +1 -1
- package/dist/web/static/css/{index.b3102f16b9.css → index.4d9a7b2b96.css} +1 -1
- package/dist/web/static/js/index.2565a9860f.js +5 -0
- package/package.json +1 -1
- package/dist/web/static/js/index.c1186260b9.js +0 -4
package/dist/index.js
CHANGED
|
@@ -3,11 +3,16 @@ import node_http, { createServer } from "node:http";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import open_0 from "open";
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
|
-
import node_path, { join } from "node:path";
|
|
6
|
+
import node_path, { extname, join, relative, resolve as external_node_path_resolve, sep } from "node:path";
|
|
7
7
|
import { Readable } from "node:stream";
|
|
8
|
-
import { stat } from "node:fs/promises";
|
|
8
|
+
import { readFile, realpath, stat } from "node:fs/promises";
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
10
|
+
import { randomBytes } from "node:crypto";
|
|
11
|
+
import { pipeline } from "node:stream/promises";
|
|
10
12
|
const INSPECTOR_CONFIG_PATH = '/__inspector/config';
|
|
13
|
+
const WORKBENCH_EVENTS_PATH = '/__dbx/events';
|
|
14
|
+
const WORKBENCH_DIAGNOSTICS_PATH = '/__dbx/diagnostics';
|
|
15
|
+
const WORKBENCH_CAPTURE_PATH = '/__dbx/capture';
|
|
11
16
|
const HOST = '127.0.0.1';
|
|
12
17
|
const BROWSER_PROFILE_DIRECTORY = join(homedir(), '.dbx', 'mcp-inspector', 'browser-profile');
|
|
13
18
|
function listen(server, port) {
|
|
@@ -330,4 +335,4721 @@ async function startInspector(options = {}) {
|
|
|
330
335
|
close
|
|
331
336
|
};
|
|
332
337
|
}
|
|
333
|
-
|
|
338
|
+
Object.freeze({
|
|
339
|
+
status: "aborted"
|
|
340
|
+
});
|
|
341
|
+
function $constructor(name, initializer, params) {
|
|
342
|
+
function init(inst, def) {
|
|
343
|
+
if (!inst._zod) Object.defineProperty(inst, "_zod", {
|
|
344
|
+
value: {
|
|
345
|
+
def,
|
|
346
|
+
constr: _,
|
|
347
|
+
traits: new Set()
|
|
348
|
+
},
|
|
349
|
+
enumerable: false
|
|
350
|
+
});
|
|
351
|
+
if (inst._zod.traits.has(name)) return;
|
|
352
|
+
inst._zod.traits.add(name);
|
|
353
|
+
initializer(inst, def);
|
|
354
|
+
const proto = _.prototype;
|
|
355
|
+
const keys = Object.keys(proto);
|
|
356
|
+
for(let i = 0; i < keys.length; i++){
|
|
357
|
+
const k = keys[i];
|
|
358
|
+
if (!(k in inst)) inst[k] = proto[k].bind(inst);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const Parent = params?.Parent ?? Object;
|
|
362
|
+
class Definition extends Parent {
|
|
363
|
+
}
|
|
364
|
+
Object.defineProperty(Definition, "name", {
|
|
365
|
+
value: name
|
|
366
|
+
});
|
|
367
|
+
function _(def) {
|
|
368
|
+
var _a;
|
|
369
|
+
const inst = params?.Parent ? new Definition() : this;
|
|
370
|
+
init(inst, def);
|
|
371
|
+
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
372
|
+
for (const fn of inst._zod.deferred)fn();
|
|
373
|
+
return inst;
|
|
374
|
+
}
|
|
375
|
+
Object.defineProperty(_, "init", {
|
|
376
|
+
value: init
|
|
377
|
+
});
|
|
378
|
+
Object.defineProperty(_, Symbol.hasInstance, {
|
|
379
|
+
value: (inst)=>{
|
|
380
|
+
if (params?.Parent && inst instanceof params.Parent) return true;
|
|
381
|
+
return inst?._zod?.traits?.has(name);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
Object.defineProperty(_, "name", {
|
|
385
|
+
value: name
|
|
386
|
+
});
|
|
387
|
+
return _;
|
|
388
|
+
}
|
|
389
|
+
Symbol("zod_brand");
|
|
390
|
+
class $ZodAsyncError extends Error {
|
|
391
|
+
constructor(){
|
|
392
|
+
super("Encountered Promise during synchronous parse. Use .parseAsync() instead.");
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
class $ZodEncodeError extends Error {
|
|
396
|
+
constructor(name){
|
|
397
|
+
super(`Encountered unidirectional transform during encode: ${name}`);
|
|
398
|
+
this.name = "ZodEncodeError";
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const globalConfig = {};
|
|
402
|
+
function core_config(newConfig) {
|
|
403
|
+
if (newConfig) Object.assign(globalConfig, newConfig);
|
|
404
|
+
return globalConfig;
|
|
405
|
+
}
|
|
406
|
+
function getEnumValues(entries) {
|
|
407
|
+
const numericValues = Object.values(entries).filter((v)=>"number" == typeof v);
|
|
408
|
+
const values = Object.entries(entries).filter(([k, _])=>-1 === numericValues.indexOf(+k)).map(([_, v])=>v);
|
|
409
|
+
return values;
|
|
410
|
+
}
|
|
411
|
+
function joinValues(array, separator = "|") {
|
|
412
|
+
return array.map((val)=>stringifyPrimitive(val)).join(separator);
|
|
413
|
+
}
|
|
414
|
+
function jsonStringifyReplacer(_, value) {
|
|
415
|
+
if ("bigint" == typeof value) return value.toString();
|
|
416
|
+
return value;
|
|
417
|
+
}
|
|
418
|
+
function cached(getter) {
|
|
419
|
+
const set = false;
|
|
420
|
+
return {
|
|
421
|
+
get value () {
|
|
422
|
+
if (!set) {
|
|
423
|
+
const value = getter();
|
|
424
|
+
Object.defineProperty(this, "value", {
|
|
425
|
+
value
|
|
426
|
+
});
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
throw new Error("cached value already set");
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function nullish(input) {
|
|
434
|
+
return null == input;
|
|
435
|
+
}
|
|
436
|
+
function cleanRegex(source) {
|
|
437
|
+
const start = source.startsWith("^") ? 1 : 0;
|
|
438
|
+
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
439
|
+
return source.slice(start, end);
|
|
440
|
+
}
|
|
441
|
+
function floatSafeRemainder(val, step) {
|
|
442
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
443
|
+
const stepString = step.toString();
|
|
444
|
+
let stepDecCount = (stepString.split(".")[1] || "").length;
|
|
445
|
+
if (0 === stepDecCount && /\d?e-\d?/.test(stepString)) {
|
|
446
|
+
const match = stepString.match(/\d?e-(\d?)/);
|
|
447
|
+
if (match?.[1]) stepDecCount = Number.parseInt(match[1]);
|
|
448
|
+
}
|
|
449
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
450
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
451
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
452
|
+
return valInt % stepInt / 10 ** decCount;
|
|
453
|
+
}
|
|
454
|
+
const EVALUATING = Symbol("evaluating");
|
|
455
|
+
function defineLazy(object, key, getter) {
|
|
456
|
+
let value;
|
|
457
|
+
Object.defineProperty(object, key, {
|
|
458
|
+
get () {
|
|
459
|
+
if (value === EVALUATING) return;
|
|
460
|
+
if (void 0 === value) {
|
|
461
|
+
value = EVALUATING;
|
|
462
|
+
value = getter();
|
|
463
|
+
}
|
|
464
|
+
return value;
|
|
465
|
+
},
|
|
466
|
+
set (v) {
|
|
467
|
+
Object.defineProperty(object, key, {
|
|
468
|
+
value: v
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
configurable: true
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
function assignProp(target, prop, value) {
|
|
475
|
+
Object.defineProperty(target, prop, {
|
|
476
|
+
value,
|
|
477
|
+
writable: true,
|
|
478
|
+
enumerable: true,
|
|
479
|
+
configurable: true
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
function mergeDefs(...defs) {
|
|
483
|
+
const mergedDescriptors = {};
|
|
484
|
+
for (const def of defs){
|
|
485
|
+
const descriptors = Object.getOwnPropertyDescriptors(def);
|
|
486
|
+
Object.assign(mergedDescriptors, descriptors);
|
|
487
|
+
}
|
|
488
|
+
return Object.defineProperties({}, mergedDescriptors);
|
|
489
|
+
}
|
|
490
|
+
function esc(str) {
|
|
491
|
+
return JSON.stringify(str);
|
|
492
|
+
}
|
|
493
|
+
function slugify(input) {
|
|
494
|
+
return input.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
495
|
+
}
|
|
496
|
+
const captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args)=>{};
|
|
497
|
+
function util_isObject(data) {
|
|
498
|
+
return "object" == typeof data && null !== data && !Array.isArray(data);
|
|
499
|
+
}
|
|
500
|
+
const util_allowsEval = cached(()=>{
|
|
501
|
+
if ("u" > typeof navigator && navigator?.userAgent?.includes("Cloudflare")) return false;
|
|
502
|
+
try {
|
|
503
|
+
const F = Function;
|
|
504
|
+
new F("");
|
|
505
|
+
return true;
|
|
506
|
+
} catch (_) {
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
function isPlainObject(o) {
|
|
511
|
+
if (false === util_isObject(o)) return false;
|
|
512
|
+
const ctor = o.constructor;
|
|
513
|
+
if (void 0 === ctor) return true;
|
|
514
|
+
if ("function" != typeof ctor) return true;
|
|
515
|
+
const prot = ctor.prototype;
|
|
516
|
+
if (false === util_isObject(prot)) return false;
|
|
517
|
+
if (false === Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf")) return false;
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
function shallowClone(o) {
|
|
521
|
+
if (isPlainObject(o)) return {
|
|
522
|
+
...o
|
|
523
|
+
};
|
|
524
|
+
if (Array.isArray(o)) return [
|
|
525
|
+
...o
|
|
526
|
+
];
|
|
527
|
+
return o;
|
|
528
|
+
}
|
|
529
|
+
const propertyKeyTypes = new Set([
|
|
530
|
+
"string",
|
|
531
|
+
"number",
|
|
532
|
+
"symbol"
|
|
533
|
+
]);
|
|
534
|
+
new Set([
|
|
535
|
+
"string",
|
|
536
|
+
"number",
|
|
537
|
+
"bigint",
|
|
538
|
+
"boolean",
|
|
539
|
+
"symbol",
|
|
540
|
+
"undefined"
|
|
541
|
+
]);
|
|
542
|
+
function escapeRegex(str) {
|
|
543
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
544
|
+
}
|
|
545
|
+
function clone(inst, def, params) {
|
|
546
|
+
const cl = new inst._zod.constr(def ?? inst._zod.def);
|
|
547
|
+
if (!def || params?.parent) cl._zod.parent = inst;
|
|
548
|
+
return cl;
|
|
549
|
+
}
|
|
550
|
+
function normalizeParams(_params) {
|
|
551
|
+
const params = _params;
|
|
552
|
+
if (!params) return {};
|
|
553
|
+
if ("string" == typeof params) return {
|
|
554
|
+
error: ()=>params
|
|
555
|
+
};
|
|
556
|
+
if (params?.message !== void 0) {
|
|
557
|
+
if (params?.error !== void 0) throw new Error("Cannot specify both `message` and `error` params");
|
|
558
|
+
params.error = params.message;
|
|
559
|
+
}
|
|
560
|
+
delete params.message;
|
|
561
|
+
if ("string" == typeof params.error) return {
|
|
562
|
+
...params,
|
|
563
|
+
error: ()=>params.error
|
|
564
|
+
};
|
|
565
|
+
return params;
|
|
566
|
+
}
|
|
567
|
+
function stringifyPrimitive(value) {
|
|
568
|
+
if ("bigint" == typeof value) return value.toString() + "n";
|
|
569
|
+
if ("string" == typeof value) return `"${value}"`;
|
|
570
|
+
return `${value}`;
|
|
571
|
+
}
|
|
572
|
+
function optionalKeys(shape) {
|
|
573
|
+
return Object.keys(shape).filter((k)=>"optional" === shape[k]._zod.optin && "optional" === shape[k]._zod.optout);
|
|
574
|
+
}
|
|
575
|
+
const NUMBER_FORMAT_RANGES = {
|
|
576
|
+
safeint: [
|
|
577
|
+
Number.MIN_SAFE_INTEGER,
|
|
578
|
+
Number.MAX_SAFE_INTEGER
|
|
579
|
+
],
|
|
580
|
+
int32: [
|
|
581
|
+
-2147483648,
|
|
582
|
+
2147483647
|
|
583
|
+
],
|
|
584
|
+
uint32: [
|
|
585
|
+
0,
|
|
586
|
+
4294967295
|
|
587
|
+
],
|
|
588
|
+
float32: [
|
|
589
|
+
-3.4028234663852886e+38,
|
|
590
|
+
3.4028234663852886e38
|
|
591
|
+
],
|
|
592
|
+
float64: [
|
|
593
|
+
-Number.MAX_VALUE,
|
|
594
|
+
Number.MAX_VALUE
|
|
595
|
+
]
|
|
596
|
+
};
|
|
597
|
+
function pick(schema, mask) {
|
|
598
|
+
const currDef = schema._zod.def;
|
|
599
|
+
const def = mergeDefs(schema._zod.def, {
|
|
600
|
+
get shape () {
|
|
601
|
+
const newShape = {};
|
|
602
|
+
for(const key in mask){
|
|
603
|
+
if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
|
|
604
|
+
if (mask[key]) newShape[key] = currDef.shape[key];
|
|
605
|
+
}
|
|
606
|
+
assignProp(this, "shape", newShape);
|
|
607
|
+
return newShape;
|
|
608
|
+
},
|
|
609
|
+
checks: []
|
|
610
|
+
});
|
|
611
|
+
return clone(schema, def);
|
|
612
|
+
}
|
|
613
|
+
function omit(schema, mask) {
|
|
614
|
+
const currDef = schema._zod.def;
|
|
615
|
+
const def = mergeDefs(schema._zod.def, {
|
|
616
|
+
get shape () {
|
|
617
|
+
const newShape = {
|
|
618
|
+
...schema._zod.def.shape
|
|
619
|
+
};
|
|
620
|
+
for(const key in mask){
|
|
621
|
+
if (!(key in currDef.shape)) throw new Error(`Unrecognized key: "${key}"`);
|
|
622
|
+
if (mask[key]) delete newShape[key];
|
|
623
|
+
}
|
|
624
|
+
assignProp(this, "shape", newShape);
|
|
625
|
+
return newShape;
|
|
626
|
+
},
|
|
627
|
+
checks: []
|
|
628
|
+
});
|
|
629
|
+
return clone(schema, def);
|
|
630
|
+
}
|
|
631
|
+
function extend(schema, shape) {
|
|
632
|
+
if (!isPlainObject(shape)) throw new Error("Invalid input to extend: expected a plain object");
|
|
633
|
+
const checks = schema._zod.def.checks;
|
|
634
|
+
const hasChecks = checks && checks.length > 0;
|
|
635
|
+
if (hasChecks) throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
|
|
636
|
+
const def = mergeDefs(schema._zod.def, {
|
|
637
|
+
get shape () {
|
|
638
|
+
const _shape = {
|
|
639
|
+
...schema._zod.def.shape,
|
|
640
|
+
...shape
|
|
641
|
+
};
|
|
642
|
+
assignProp(this, "shape", _shape);
|
|
643
|
+
return _shape;
|
|
644
|
+
},
|
|
645
|
+
checks: []
|
|
646
|
+
});
|
|
647
|
+
return clone(schema, def);
|
|
648
|
+
}
|
|
649
|
+
function safeExtend(schema, shape) {
|
|
650
|
+
if (!isPlainObject(shape)) throw new Error("Invalid input to safeExtend: expected a plain object");
|
|
651
|
+
const def = {
|
|
652
|
+
...schema._zod.def,
|
|
653
|
+
get shape () {
|
|
654
|
+
const _shape = {
|
|
655
|
+
...schema._zod.def.shape,
|
|
656
|
+
...shape
|
|
657
|
+
};
|
|
658
|
+
assignProp(this, "shape", _shape);
|
|
659
|
+
return _shape;
|
|
660
|
+
},
|
|
661
|
+
checks: schema._zod.def.checks
|
|
662
|
+
};
|
|
663
|
+
return clone(schema, def);
|
|
664
|
+
}
|
|
665
|
+
function merge(a, b) {
|
|
666
|
+
const def = mergeDefs(a._zod.def, {
|
|
667
|
+
get shape () {
|
|
668
|
+
const _shape = {
|
|
669
|
+
...a._zod.def.shape,
|
|
670
|
+
...b._zod.def.shape
|
|
671
|
+
};
|
|
672
|
+
assignProp(this, "shape", _shape);
|
|
673
|
+
return _shape;
|
|
674
|
+
},
|
|
675
|
+
get catchall () {
|
|
676
|
+
return b._zod.def.catchall;
|
|
677
|
+
},
|
|
678
|
+
checks: []
|
|
679
|
+
});
|
|
680
|
+
return clone(a, def);
|
|
681
|
+
}
|
|
682
|
+
function partial(Class, schema, mask) {
|
|
683
|
+
const def = mergeDefs(schema._zod.def, {
|
|
684
|
+
get shape () {
|
|
685
|
+
const oldShape = schema._zod.def.shape;
|
|
686
|
+
const shape = {
|
|
687
|
+
...oldShape
|
|
688
|
+
};
|
|
689
|
+
if (mask) for(const key in mask){
|
|
690
|
+
if (!(key in oldShape)) throw new Error(`Unrecognized key: "${key}"`);
|
|
691
|
+
if (mask[key]) shape[key] = Class ? new Class({
|
|
692
|
+
type: "optional",
|
|
693
|
+
innerType: oldShape[key]
|
|
694
|
+
}) : oldShape[key];
|
|
695
|
+
}
|
|
696
|
+
else for(const key in oldShape)shape[key] = Class ? new Class({
|
|
697
|
+
type: "optional",
|
|
698
|
+
innerType: oldShape[key]
|
|
699
|
+
}) : oldShape[key];
|
|
700
|
+
assignProp(this, "shape", shape);
|
|
701
|
+
return shape;
|
|
702
|
+
},
|
|
703
|
+
checks: []
|
|
704
|
+
});
|
|
705
|
+
return clone(schema, def);
|
|
706
|
+
}
|
|
707
|
+
function required(Class, schema, mask) {
|
|
708
|
+
const def = mergeDefs(schema._zod.def, {
|
|
709
|
+
get shape () {
|
|
710
|
+
const oldShape = schema._zod.def.shape;
|
|
711
|
+
const shape = {
|
|
712
|
+
...oldShape
|
|
713
|
+
};
|
|
714
|
+
if (mask) for(const key in mask){
|
|
715
|
+
if (!(key in shape)) throw new Error(`Unrecognized key: "${key}"`);
|
|
716
|
+
if (mask[key]) shape[key] = new Class({
|
|
717
|
+
type: "nonoptional",
|
|
718
|
+
innerType: oldShape[key]
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
else for(const key in oldShape)shape[key] = new Class({
|
|
722
|
+
type: "nonoptional",
|
|
723
|
+
innerType: oldShape[key]
|
|
724
|
+
});
|
|
725
|
+
assignProp(this, "shape", shape);
|
|
726
|
+
return shape;
|
|
727
|
+
},
|
|
728
|
+
checks: []
|
|
729
|
+
});
|
|
730
|
+
return clone(schema, def);
|
|
731
|
+
}
|
|
732
|
+
function aborted(x, startIndex = 0) {
|
|
733
|
+
if (true === x.aborted) return true;
|
|
734
|
+
for(let i = startIndex; i < x.issues.length; i++)if (x.issues[i]?.continue !== true) return true;
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
function prefixIssues(path, issues) {
|
|
738
|
+
return issues.map((iss)=>{
|
|
739
|
+
var _a;
|
|
740
|
+
(_a = iss).path ?? (_a.path = []);
|
|
741
|
+
iss.path.unshift(path);
|
|
742
|
+
return iss;
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
function unwrapMessage(message) {
|
|
746
|
+
return "string" == typeof message ? message : message?.message;
|
|
747
|
+
}
|
|
748
|
+
function finalizeIssue(iss, ctx, config) {
|
|
749
|
+
const full = {
|
|
750
|
+
...iss,
|
|
751
|
+
path: iss.path ?? []
|
|
752
|
+
};
|
|
753
|
+
if (!iss.message) {
|
|
754
|
+
const message = unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config.customError?.(iss)) ?? unwrapMessage(config.localeError?.(iss)) ?? "Invalid input";
|
|
755
|
+
full.message = message;
|
|
756
|
+
}
|
|
757
|
+
delete full.inst;
|
|
758
|
+
delete full.continue;
|
|
759
|
+
if (!ctx?.reportInput) delete full.input;
|
|
760
|
+
return full;
|
|
761
|
+
}
|
|
762
|
+
function getLengthableOrigin(input) {
|
|
763
|
+
if (Array.isArray(input)) return "array";
|
|
764
|
+
if ("string" == typeof input) return "string";
|
|
765
|
+
return "unknown";
|
|
766
|
+
}
|
|
767
|
+
function util_issue(...args) {
|
|
768
|
+
const [iss, input, inst] = args;
|
|
769
|
+
if ("string" == typeof iss) return {
|
|
770
|
+
message: iss,
|
|
771
|
+
code: "custom",
|
|
772
|
+
input,
|
|
773
|
+
inst
|
|
774
|
+
};
|
|
775
|
+
return {
|
|
776
|
+
...iss
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
const errors_initializer = (inst, def)=>{
|
|
780
|
+
inst.name = "$ZodError";
|
|
781
|
+
Object.defineProperty(inst, "_zod", {
|
|
782
|
+
value: inst._zod,
|
|
783
|
+
enumerable: false
|
|
784
|
+
});
|
|
785
|
+
Object.defineProperty(inst, "issues", {
|
|
786
|
+
value: def,
|
|
787
|
+
enumerable: false
|
|
788
|
+
});
|
|
789
|
+
inst.message = JSON.stringify(def, jsonStringifyReplacer, 2);
|
|
790
|
+
Object.defineProperty(inst, "toString", {
|
|
791
|
+
value: ()=>inst.message,
|
|
792
|
+
enumerable: false
|
|
793
|
+
});
|
|
794
|
+
};
|
|
795
|
+
const $ZodError = $constructor("$ZodError", errors_initializer);
|
|
796
|
+
const $ZodRealError = $constructor("$ZodError", errors_initializer, {
|
|
797
|
+
Parent: Error
|
|
798
|
+
});
|
|
799
|
+
function flattenError(error, mapper = (issue)=>issue.message) {
|
|
800
|
+
const fieldErrors = {};
|
|
801
|
+
const formErrors = [];
|
|
802
|
+
for (const sub of error.issues)if (sub.path.length > 0) {
|
|
803
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
804
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
805
|
+
} else formErrors.push(mapper(sub));
|
|
806
|
+
return {
|
|
807
|
+
formErrors,
|
|
808
|
+
fieldErrors
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function formatError(error, mapper = (issue)=>issue.message) {
|
|
812
|
+
const fieldErrors = {
|
|
813
|
+
_errors: []
|
|
814
|
+
};
|
|
815
|
+
const processError = (error)=>{
|
|
816
|
+
for (const issue of error.issues)if ("invalid_union" === issue.code && issue.errors.length) issue.errors.map((issues)=>processError({
|
|
817
|
+
issues
|
|
818
|
+
}));
|
|
819
|
+
else if ("invalid_key" === issue.code) processError({
|
|
820
|
+
issues: issue.issues
|
|
821
|
+
});
|
|
822
|
+
else if ("invalid_element" === issue.code) processError({
|
|
823
|
+
issues: issue.issues
|
|
824
|
+
});
|
|
825
|
+
else if (0 === issue.path.length) fieldErrors._errors.push(mapper(issue));
|
|
826
|
+
else {
|
|
827
|
+
let curr = fieldErrors;
|
|
828
|
+
let i = 0;
|
|
829
|
+
while(i < issue.path.length){
|
|
830
|
+
const el = issue.path[i];
|
|
831
|
+
const terminal = i === issue.path.length - 1;
|
|
832
|
+
if (terminal) {
|
|
833
|
+
curr[el] = curr[el] || {
|
|
834
|
+
_errors: []
|
|
835
|
+
};
|
|
836
|
+
curr[el]._errors.push(mapper(issue));
|
|
837
|
+
} else curr[el] = curr[el] || {
|
|
838
|
+
_errors: []
|
|
839
|
+
};
|
|
840
|
+
curr = curr[el];
|
|
841
|
+
i++;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
processError(error);
|
|
846
|
+
return fieldErrors;
|
|
847
|
+
}
|
|
848
|
+
const _parse = (_Err)=>(schema, value, _ctx, _params)=>{
|
|
849
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
850
|
+
async: false
|
|
851
|
+
}) : {
|
|
852
|
+
async: false
|
|
853
|
+
};
|
|
854
|
+
const result = schema._zod.run({
|
|
855
|
+
value,
|
|
856
|
+
issues: []
|
|
857
|
+
}, ctx);
|
|
858
|
+
if (result instanceof Promise) throw new $ZodAsyncError();
|
|
859
|
+
if (result.issues.length) {
|
|
860
|
+
const e = new (_params?.Err ?? _Err)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config())));
|
|
861
|
+
captureStackTrace(e, _params?.callee);
|
|
862
|
+
throw e;
|
|
863
|
+
}
|
|
864
|
+
return result.value;
|
|
865
|
+
};
|
|
866
|
+
const _parseAsync = (_Err)=>async (schema, value, _ctx, params)=>{
|
|
867
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
868
|
+
async: true
|
|
869
|
+
}) : {
|
|
870
|
+
async: true
|
|
871
|
+
};
|
|
872
|
+
let result = schema._zod.run({
|
|
873
|
+
value,
|
|
874
|
+
issues: []
|
|
875
|
+
}, ctx);
|
|
876
|
+
if (result instanceof Promise) result = await result;
|
|
877
|
+
if (result.issues.length) {
|
|
878
|
+
const e = new (params?.Err ?? _Err)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config())));
|
|
879
|
+
captureStackTrace(e, params?.callee);
|
|
880
|
+
throw e;
|
|
881
|
+
}
|
|
882
|
+
return result.value;
|
|
883
|
+
};
|
|
884
|
+
const _safeParse = (_Err)=>(schema, value, _ctx)=>{
|
|
885
|
+
const ctx = _ctx ? {
|
|
886
|
+
..._ctx,
|
|
887
|
+
async: false
|
|
888
|
+
} : {
|
|
889
|
+
async: false
|
|
890
|
+
};
|
|
891
|
+
const result = schema._zod.run({
|
|
892
|
+
value,
|
|
893
|
+
issues: []
|
|
894
|
+
}, ctx);
|
|
895
|
+
if (result instanceof Promise) throw new $ZodAsyncError();
|
|
896
|
+
return result.issues.length ? {
|
|
897
|
+
success: false,
|
|
898
|
+
error: new (_Err ?? $ZodError)(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config())))
|
|
899
|
+
} : {
|
|
900
|
+
success: true,
|
|
901
|
+
data: result.value
|
|
902
|
+
};
|
|
903
|
+
};
|
|
904
|
+
const safeParse = /* @__PURE__*/ _safeParse($ZodRealError);
|
|
905
|
+
const _safeParseAsync = (_Err)=>async (schema, value, _ctx)=>{
|
|
906
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
907
|
+
async: true
|
|
908
|
+
}) : {
|
|
909
|
+
async: true
|
|
910
|
+
};
|
|
911
|
+
let result = schema._zod.run({
|
|
912
|
+
value,
|
|
913
|
+
issues: []
|
|
914
|
+
}, ctx);
|
|
915
|
+
if (result instanceof Promise) result = await result;
|
|
916
|
+
return result.issues.length ? {
|
|
917
|
+
success: false,
|
|
918
|
+
error: new _Err(result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config())))
|
|
919
|
+
} : {
|
|
920
|
+
success: true,
|
|
921
|
+
data: result.value
|
|
922
|
+
};
|
|
923
|
+
};
|
|
924
|
+
const safeParseAsync = /* @__PURE__*/ _safeParseAsync($ZodRealError);
|
|
925
|
+
const _encode = (_Err)=>(schema, value, _ctx)=>{
|
|
926
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
927
|
+
direction: "backward"
|
|
928
|
+
}) : {
|
|
929
|
+
direction: "backward"
|
|
930
|
+
};
|
|
931
|
+
return _parse(_Err)(schema, value, ctx);
|
|
932
|
+
};
|
|
933
|
+
const _decode = (_Err)=>(schema, value, _ctx)=>_parse(_Err)(schema, value, _ctx);
|
|
934
|
+
const _encodeAsync = (_Err)=>async (schema, value, _ctx)=>{
|
|
935
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
936
|
+
direction: "backward"
|
|
937
|
+
}) : {
|
|
938
|
+
direction: "backward"
|
|
939
|
+
};
|
|
940
|
+
return _parseAsync(_Err)(schema, value, ctx);
|
|
941
|
+
};
|
|
942
|
+
const _decodeAsync = (_Err)=>async (schema, value, _ctx)=>_parseAsync(_Err)(schema, value, _ctx);
|
|
943
|
+
const _safeEncode = (_Err)=>(schema, value, _ctx)=>{
|
|
944
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
945
|
+
direction: "backward"
|
|
946
|
+
}) : {
|
|
947
|
+
direction: "backward"
|
|
948
|
+
};
|
|
949
|
+
return _safeParse(_Err)(schema, value, ctx);
|
|
950
|
+
};
|
|
951
|
+
const _safeDecode = (_Err)=>(schema, value, _ctx)=>_safeParse(_Err)(schema, value, _ctx);
|
|
952
|
+
const _safeEncodeAsync = (_Err)=>async (schema, value, _ctx)=>{
|
|
953
|
+
const ctx = _ctx ? Object.assign(_ctx, {
|
|
954
|
+
direction: "backward"
|
|
955
|
+
}) : {
|
|
956
|
+
direction: "backward"
|
|
957
|
+
};
|
|
958
|
+
return _safeParseAsync(_Err)(schema, value, ctx);
|
|
959
|
+
};
|
|
960
|
+
const _safeDecodeAsync = (_Err)=>async (schema, value, _ctx)=>_safeParseAsync(_Err)(schema, value, _ctx);
|
|
961
|
+
const cuid = /^[cC][^\s-]{8,}$/;
|
|
962
|
+
const cuid2 = /^[0-9a-z]+$/;
|
|
963
|
+
const ulid = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
|
|
964
|
+
const xid = /^[0-9a-vA-V]{20}$/;
|
|
965
|
+
const ksuid = /^[A-Za-z0-9]{27}$/;
|
|
966
|
+
const nanoid = /^[a-zA-Z0-9_-]{21}$/;
|
|
967
|
+
const duration = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
|
|
968
|
+
const guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
|
|
969
|
+
const uuid = (version)=>{
|
|
970
|
+
if (!version) return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
|
|
971
|
+
return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
|
|
972
|
+
};
|
|
973
|
+
const email = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
|
|
974
|
+
const _emoji = "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$";
|
|
975
|
+
function emoji() {
|
|
976
|
+
return new RegExp(_emoji, "u");
|
|
977
|
+
}
|
|
978
|
+
const ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
979
|
+
const ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
|
|
980
|
+
const cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
|
|
981
|
+
const cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
982
|
+
const regexes_base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
|
|
983
|
+
const regexes_base64url = /^[A-Za-z0-9_-]*$/;
|
|
984
|
+
const e164 = /^\+(?:[0-9]){6,14}[0-9]$/;
|
|
985
|
+
const dateSource = "(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))";
|
|
986
|
+
const date = /*@__PURE__*/ new RegExp(`^${dateSource}$`);
|
|
987
|
+
function timeSource(args) {
|
|
988
|
+
const hhmm = "(?:[01]\\d|2[0-3]):[0-5]\\d";
|
|
989
|
+
const regex = "number" == typeof args.precision ? -1 === args.precision ? `${hhmm}` : 0 === args.precision ? `${hhmm}:[0-5]\\d` : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}` : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`;
|
|
990
|
+
return regex;
|
|
991
|
+
}
|
|
992
|
+
function regexes_time(args) {
|
|
993
|
+
return new RegExp(`^${timeSource(args)}$`);
|
|
994
|
+
}
|
|
995
|
+
function datetime(args) {
|
|
996
|
+
const time = timeSource({
|
|
997
|
+
precision: args.precision
|
|
998
|
+
});
|
|
999
|
+
const opts = [
|
|
1000
|
+
"Z"
|
|
1001
|
+
];
|
|
1002
|
+
if (args.local) opts.push("");
|
|
1003
|
+
if (args.offset) opts.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)");
|
|
1004
|
+
const timeRegex = `${time}(?:${opts.join("|")})`;
|
|
1005
|
+
return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
|
|
1006
|
+
}
|
|
1007
|
+
const string = (params)=>{
|
|
1008
|
+
const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : "[\\s\\S]*";
|
|
1009
|
+
return new RegExp(`^${regex}$`);
|
|
1010
|
+
};
|
|
1011
|
+
const integer = /^-?\d+$/;
|
|
1012
|
+
const regexes_number = /^-?\d+(?:\.\d+)?/;
|
|
1013
|
+
const lowercase = /^[^A-Z]*$/;
|
|
1014
|
+
const uppercase = /^[^a-z]*$/;
|
|
1015
|
+
const $ZodCheck = /*@__PURE__*/ $constructor("$ZodCheck", (inst, def)=>{
|
|
1016
|
+
var _a;
|
|
1017
|
+
inst._zod ?? (inst._zod = {});
|
|
1018
|
+
inst._zod.def = def;
|
|
1019
|
+
(_a = inst._zod).onattach ?? (_a.onattach = []);
|
|
1020
|
+
});
|
|
1021
|
+
const numericOriginMap = {
|
|
1022
|
+
number: "number",
|
|
1023
|
+
bigint: "bigint",
|
|
1024
|
+
object: "date"
|
|
1025
|
+
};
|
|
1026
|
+
const $ZodCheckLessThan = /*@__PURE__*/ $constructor("$ZodCheckLessThan", (inst, def)=>{
|
|
1027
|
+
$ZodCheck.init(inst, def);
|
|
1028
|
+
const origin = numericOriginMap[typeof def.value];
|
|
1029
|
+
inst._zod.onattach.push((inst)=>{
|
|
1030
|
+
const bag = inst._zod.bag;
|
|
1031
|
+
const curr = (def.inclusive ? bag.maximum : bag.exclusiveMaximum) ?? 1 / 0;
|
|
1032
|
+
if (def.value < curr) if (def.inclusive) bag.maximum = def.value;
|
|
1033
|
+
else bag.exclusiveMaximum = def.value;
|
|
1034
|
+
});
|
|
1035
|
+
inst._zod.check = (payload)=>{
|
|
1036
|
+
if (def.inclusive ? payload.value <= def.value : payload.value < def.value) return;
|
|
1037
|
+
payload.issues.push({
|
|
1038
|
+
origin,
|
|
1039
|
+
code: "too_big",
|
|
1040
|
+
maximum: def.value,
|
|
1041
|
+
input: payload.value,
|
|
1042
|
+
inclusive: def.inclusive,
|
|
1043
|
+
inst,
|
|
1044
|
+
continue: !def.abort
|
|
1045
|
+
});
|
|
1046
|
+
};
|
|
1047
|
+
});
|
|
1048
|
+
const $ZodCheckGreaterThan = /*@__PURE__*/ $constructor("$ZodCheckGreaterThan", (inst, def)=>{
|
|
1049
|
+
$ZodCheck.init(inst, def);
|
|
1050
|
+
const origin = numericOriginMap[typeof def.value];
|
|
1051
|
+
inst._zod.onattach.push((inst)=>{
|
|
1052
|
+
const bag = inst._zod.bag;
|
|
1053
|
+
const curr = (def.inclusive ? bag.minimum : bag.exclusiveMinimum) ?? -1 / 0;
|
|
1054
|
+
if (def.value > curr) if (def.inclusive) bag.minimum = def.value;
|
|
1055
|
+
else bag.exclusiveMinimum = def.value;
|
|
1056
|
+
});
|
|
1057
|
+
inst._zod.check = (payload)=>{
|
|
1058
|
+
if (def.inclusive ? payload.value >= def.value : payload.value > def.value) return;
|
|
1059
|
+
payload.issues.push({
|
|
1060
|
+
origin,
|
|
1061
|
+
code: "too_small",
|
|
1062
|
+
minimum: def.value,
|
|
1063
|
+
input: payload.value,
|
|
1064
|
+
inclusive: def.inclusive,
|
|
1065
|
+
inst,
|
|
1066
|
+
continue: !def.abort
|
|
1067
|
+
});
|
|
1068
|
+
};
|
|
1069
|
+
});
|
|
1070
|
+
const $ZodCheckMultipleOf = /*@__PURE__*/ $constructor("$ZodCheckMultipleOf", (inst, def)=>{
|
|
1071
|
+
$ZodCheck.init(inst, def);
|
|
1072
|
+
inst._zod.onattach.push((inst)=>{
|
|
1073
|
+
var _a;
|
|
1074
|
+
(_a = inst._zod.bag).multipleOf ?? (_a.multipleOf = def.value);
|
|
1075
|
+
});
|
|
1076
|
+
inst._zod.check = (payload)=>{
|
|
1077
|
+
if (typeof payload.value !== typeof def.value) throw new Error("Cannot mix number and bigint in multiple_of check.");
|
|
1078
|
+
const isMultiple = "bigint" == typeof payload.value ? payload.value % def.value === BigInt(0) : 0 === floatSafeRemainder(payload.value, def.value);
|
|
1079
|
+
if (isMultiple) return;
|
|
1080
|
+
payload.issues.push({
|
|
1081
|
+
origin: typeof payload.value,
|
|
1082
|
+
code: "not_multiple_of",
|
|
1083
|
+
divisor: def.value,
|
|
1084
|
+
input: payload.value,
|
|
1085
|
+
inst,
|
|
1086
|
+
continue: !def.abort
|
|
1087
|
+
});
|
|
1088
|
+
};
|
|
1089
|
+
});
|
|
1090
|
+
const $ZodCheckNumberFormat = /*@__PURE__*/ $constructor("$ZodCheckNumberFormat", (inst, def)=>{
|
|
1091
|
+
$ZodCheck.init(inst, def);
|
|
1092
|
+
def.format = def.format || "float64";
|
|
1093
|
+
const isInt = def.format?.includes("int");
|
|
1094
|
+
const origin = isInt ? "int" : "number";
|
|
1095
|
+
const [minimum, maximum] = NUMBER_FORMAT_RANGES[def.format];
|
|
1096
|
+
inst._zod.onattach.push((inst)=>{
|
|
1097
|
+
const bag = inst._zod.bag;
|
|
1098
|
+
bag.format = def.format;
|
|
1099
|
+
bag.minimum = minimum;
|
|
1100
|
+
bag.maximum = maximum;
|
|
1101
|
+
if (isInt) bag.pattern = integer;
|
|
1102
|
+
});
|
|
1103
|
+
inst._zod.check = (payload)=>{
|
|
1104
|
+
const input = payload.value;
|
|
1105
|
+
if (isInt) {
|
|
1106
|
+
if (!Number.isInteger(input)) return void payload.issues.push({
|
|
1107
|
+
expected: origin,
|
|
1108
|
+
format: def.format,
|
|
1109
|
+
code: "invalid_type",
|
|
1110
|
+
continue: false,
|
|
1111
|
+
input,
|
|
1112
|
+
inst
|
|
1113
|
+
});
|
|
1114
|
+
if (!Number.isSafeInteger(input)) {
|
|
1115
|
+
if (input > 0) payload.issues.push({
|
|
1116
|
+
input,
|
|
1117
|
+
code: "too_big",
|
|
1118
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
1119
|
+
note: "Integers must be within the safe integer range.",
|
|
1120
|
+
inst,
|
|
1121
|
+
origin,
|
|
1122
|
+
continue: !def.abort
|
|
1123
|
+
});
|
|
1124
|
+
else payload.issues.push({
|
|
1125
|
+
input,
|
|
1126
|
+
code: "too_small",
|
|
1127
|
+
minimum: Number.MIN_SAFE_INTEGER,
|
|
1128
|
+
note: "Integers must be within the safe integer range.",
|
|
1129
|
+
inst,
|
|
1130
|
+
origin,
|
|
1131
|
+
continue: !def.abort
|
|
1132
|
+
});
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
if (input < minimum) payload.issues.push({
|
|
1137
|
+
origin: "number",
|
|
1138
|
+
input,
|
|
1139
|
+
code: "too_small",
|
|
1140
|
+
minimum,
|
|
1141
|
+
inclusive: true,
|
|
1142
|
+
inst,
|
|
1143
|
+
continue: !def.abort
|
|
1144
|
+
});
|
|
1145
|
+
if (input > maximum) payload.issues.push({
|
|
1146
|
+
origin: "number",
|
|
1147
|
+
input,
|
|
1148
|
+
code: "too_big",
|
|
1149
|
+
maximum,
|
|
1150
|
+
inst
|
|
1151
|
+
});
|
|
1152
|
+
};
|
|
1153
|
+
});
|
|
1154
|
+
const $ZodCheckMaxLength = /*@__PURE__*/ $constructor("$ZodCheckMaxLength", (inst, def)=>{
|
|
1155
|
+
var _a;
|
|
1156
|
+
$ZodCheck.init(inst, def);
|
|
1157
|
+
(_a = inst._zod.def).when ?? (_a.when = (payload)=>{
|
|
1158
|
+
const val = payload.value;
|
|
1159
|
+
return !nullish(val) && void 0 !== val.length;
|
|
1160
|
+
});
|
|
1161
|
+
inst._zod.onattach.push((inst)=>{
|
|
1162
|
+
const curr = inst._zod.bag.maximum ?? 1 / 0;
|
|
1163
|
+
if (def.maximum < curr) inst._zod.bag.maximum = def.maximum;
|
|
1164
|
+
});
|
|
1165
|
+
inst._zod.check = (payload)=>{
|
|
1166
|
+
const input = payload.value;
|
|
1167
|
+
const length = input.length;
|
|
1168
|
+
if (length <= def.maximum) return;
|
|
1169
|
+
const origin = getLengthableOrigin(input);
|
|
1170
|
+
payload.issues.push({
|
|
1171
|
+
origin,
|
|
1172
|
+
code: "too_big",
|
|
1173
|
+
maximum: def.maximum,
|
|
1174
|
+
inclusive: true,
|
|
1175
|
+
input,
|
|
1176
|
+
inst,
|
|
1177
|
+
continue: !def.abort
|
|
1178
|
+
});
|
|
1179
|
+
};
|
|
1180
|
+
});
|
|
1181
|
+
const $ZodCheckMinLength = /*@__PURE__*/ $constructor("$ZodCheckMinLength", (inst, def)=>{
|
|
1182
|
+
var _a;
|
|
1183
|
+
$ZodCheck.init(inst, def);
|
|
1184
|
+
(_a = inst._zod.def).when ?? (_a.when = (payload)=>{
|
|
1185
|
+
const val = payload.value;
|
|
1186
|
+
return !nullish(val) && void 0 !== val.length;
|
|
1187
|
+
});
|
|
1188
|
+
inst._zod.onattach.push((inst)=>{
|
|
1189
|
+
const curr = inst._zod.bag.minimum ?? -1 / 0;
|
|
1190
|
+
if (def.minimum > curr) inst._zod.bag.minimum = def.minimum;
|
|
1191
|
+
});
|
|
1192
|
+
inst._zod.check = (payload)=>{
|
|
1193
|
+
const input = payload.value;
|
|
1194
|
+
const length = input.length;
|
|
1195
|
+
if (length >= def.minimum) return;
|
|
1196
|
+
const origin = getLengthableOrigin(input);
|
|
1197
|
+
payload.issues.push({
|
|
1198
|
+
origin,
|
|
1199
|
+
code: "too_small",
|
|
1200
|
+
minimum: def.minimum,
|
|
1201
|
+
inclusive: true,
|
|
1202
|
+
input,
|
|
1203
|
+
inst,
|
|
1204
|
+
continue: !def.abort
|
|
1205
|
+
});
|
|
1206
|
+
};
|
|
1207
|
+
});
|
|
1208
|
+
const $ZodCheckLengthEquals = /*@__PURE__*/ $constructor("$ZodCheckLengthEquals", (inst, def)=>{
|
|
1209
|
+
var _a;
|
|
1210
|
+
$ZodCheck.init(inst, def);
|
|
1211
|
+
(_a = inst._zod.def).when ?? (_a.when = (payload)=>{
|
|
1212
|
+
const val = payload.value;
|
|
1213
|
+
return !nullish(val) && void 0 !== val.length;
|
|
1214
|
+
});
|
|
1215
|
+
inst._zod.onattach.push((inst)=>{
|
|
1216
|
+
const bag = inst._zod.bag;
|
|
1217
|
+
bag.minimum = def.length;
|
|
1218
|
+
bag.maximum = def.length;
|
|
1219
|
+
bag.length = def.length;
|
|
1220
|
+
});
|
|
1221
|
+
inst._zod.check = (payload)=>{
|
|
1222
|
+
const input = payload.value;
|
|
1223
|
+
const length = input.length;
|
|
1224
|
+
if (length === def.length) return;
|
|
1225
|
+
const origin = getLengthableOrigin(input);
|
|
1226
|
+
const tooBig = length > def.length;
|
|
1227
|
+
payload.issues.push({
|
|
1228
|
+
origin,
|
|
1229
|
+
...tooBig ? {
|
|
1230
|
+
code: "too_big",
|
|
1231
|
+
maximum: def.length
|
|
1232
|
+
} : {
|
|
1233
|
+
code: "too_small",
|
|
1234
|
+
minimum: def.length
|
|
1235
|
+
},
|
|
1236
|
+
inclusive: true,
|
|
1237
|
+
exact: true,
|
|
1238
|
+
input: payload.value,
|
|
1239
|
+
inst,
|
|
1240
|
+
continue: !def.abort
|
|
1241
|
+
});
|
|
1242
|
+
};
|
|
1243
|
+
});
|
|
1244
|
+
const $ZodCheckStringFormat = /*@__PURE__*/ $constructor("$ZodCheckStringFormat", (inst, def)=>{
|
|
1245
|
+
var _a, _b;
|
|
1246
|
+
$ZodCheck.init(inst, def);
|
|
1247
|
+
inst._zod.onattach.push((inst)=>{
|
|
1248
|
+
const bag = inst._zod.bag;
|
|
1249
|
+
bag.format = def.format;
|
|
1250
|
+
if (def.pattern) {
|
|
1251
|
+
bag.patterns ?? (bag.patterns = new Set());
|
|
1252
|
+
bag.patterns.add(def.pattern);
|
|
1253
|
+
}
|
|
1254
|
+
});
|
|
1255
|
+
if (def.pattern) (_a = inst._zod).check ?? (_a.check = (payload)=>{
|
|
1256
|
+
def.pattern.lastIndex = 0;
|
|
1257
|
+
if (def.pattern.test(payload.value)) return;
|
|
1258
|
+
payload.issues.push({
|
|
1259
|
+
origin: "string",
|
|
1260
|
+
code: "invalid_format",
|
|
1261
|
+
format: def.format,
|
|
1262
|
+
input: payload.value,
|
|
1263
|
+
...def.pattern ? {
|
|
1264
|
+
pattern: def.pattern.toString()
|
|
1265
|
+
} : {},
|
|
1266
|
+
inst,
|
|
1267
|
+
continue: !def.abort
|
|
1268
|
+
});
|
|
1269
|
+
});
|
|
1270
|
+
else (_b = inst._zod).check ?? (_b.check = ()=>{});
|
|
1271
|
+
});
|
|
1272
|
+
const $ZodCheckRegex = /*@__PURE__*/ $constructor("$ZodCheckRegex", (inst, def)=>{
|
|
1273
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1274
|
+
inst._zod.check = (payload)=>{
|
|
1275
|
+
def.pattern.lastIndex = 0;
|
|
1276
|
+
if (def.pattern.test(payload.value)) return;
|
|
1277
|
+
payload.issues.push({
|
|
1278
|
+
origin: "string",
|
|
1279
|
+
code: "invalid_format",
|
|
1280
|
+
format: "regex",
|
|
1281
|
+
input: payload.value,
|
|
1282
|
+
pattern: def.pattern.toString(),
|
|
1283
|
+
inst,
|
|
1284
|
+
continue: !def.abort
|
|
1285
|
+
});
|
|
1286
|
+
};
|
|
1287
|
+
});
|
|
1288
|
+
const $ZodCheckLowerCase = /*@__PURE__*/ $constructor("$ZodCheckLowerCase", (inst, def)=>{
|
|
1289
|
+
def.pattern ?? (def.pattern = lowercase);
|
|
1290
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1291
|
+
});
|
|
1292
|
+
const $ZodCheckUpperCase = /*@__PURE__*/ $constructor("$ZodCheckUpperCase", (inst, def)=>{
|
|
1293
|
+
def.pattern ?? (def.pattern = uppercase);
|
|
1294
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1295
|
+
});
|
|
1296
|
+
const $ZodCheckIncludes = /*@__PURE__*/ $constructor("$ZodCheckIncludes", (inst, def)=>{
|
|
1297
|
+
$ZodCheck.init(inst, def);
|
|
1298
|
+
const escapedRegex = escapeRegex(def.includes);
|
|
1299
|
+
const pattern = new RegExp("number" == typeof def.position ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
|
|
1300
|
+
def.pattern = pattern;
|
|
1301
|
+
inst._zod.onattach.push((inst)=>{
|
|
1302
|
+
const bag = inst._zod.bag;
|
|
1303
|
+
bag.patterns ?? (bag.patterns = new Set());
|
|
1304
|
+
bag.patterns.add(pattern);
|
|
1305
|
+
});
|
|
1306
|
+
inst._zod.check = (payload)=>{
|
|
1307
|
+
if (payload.value.includes(def.includes, def.position)) return;
|
|
1308
|
+
payload.issues.push({
|
|
1309
|
+
origin: "string",
|
|
1310
|
+
code: "invalid_format",
|
|
1311
|
+
format: "includes",
|
|
1312
|
+
includes: def.includes,
|
|
1313
|
+
input: payload.value,
|
|
1314
|
+
inst,
|
|
1315
|
+
continue: !def.abort
|
|
1316
|
+
});
|
|
1317
|
+
};
|
|
1318
|
+
});
|
|
1319
|
+
const $ZodCheckStartsWith = /*@__PURE__*/ $constructor("$ZodCheckStartsWith", (inst, def)=>{
|
|
1320
|
+
$ZodCheck.init(inst, def);
|
|
1321
|
+
const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
|
|
1322
|
+
def.pattern ?? (def.pattern = pattern);
|
|
1323
|
+
inst._zod.onattach.push((inst)=>{
|
|
1324
|
+
const bag = inst._zod.bag;
|
|
1325
|
+
bag.patterns ?? (bag.patterns = new Set());
|
|
1326
|
+
bag.patterns.add(pattern);
|
|
1327
|
+
});
|
|
1328
|
+
inst._zod.check = (payload)=>{
|
|
1329
|
+
if (payload.value.startsWith(def.prefix)) return;
|
|
1330
|
+
payload.issues.push({
|
|
1331
|
+
origin: "string",
|
|
1332
|
+
code: "invalid_format",
|
|
1333
|
+
format: "starts_with",
|
|
1334
|
+
prefix: def.prefix,
|
|
1335
|
+
input: payload.value,
|
|
1336
|
+
inst,
|
|
1337
|
+
continue: !def.abort
|
|
1338
|
+
});
|
|
1339
|
+
};
|
|
1340
|
+
});
|
|
1341
|
+
const $ZodCheckEndsWith = /*@__PURE__*/ $constructor("$ZodCheckEndsWith", (inst, def)=>{
|
|
1342
|
+
$ZodCheck.init(inst, def);
|
|
1343
|
+
const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
|
|
1344
|
+
def.pattern ?? (def.pattern = pattern);
|
|
1345
|
+
inst._zod.onattach.push((inst)=>{
|
|
1346
|
+
const bag = inst._zod.bag;
|
|
1347
|
+
bag.patterns ?? (bag.patterns = new Set());
|
|
1348
|
+
bag.patterns.add(pattern);
|
|
1349
|
+
});
|
|
1350
|
+
inst._zod.check = (payload)=>{
|
|
1351
|
+
if (payload.value.endsWith(def.suffix)) return;
|
|
1352
|
+
payload.issues.push({
|
|
1353
|
+
origin: "string",
|
|
1354
|
+
code: "invalid_format",
|
|
1355
|
+
format: "ends_with",
|
|
1356
|
+
suffix: def.suffix,
|
|
1357
|
+
input: payload.value,
|
|
1358
|
+
inst,
|
|
1359
|
+
continue: !def.abort
|
|
1360
|
+
});
|
|
1361
|
+
};
|
|
1362
|
+
});
|
|
1363
|
+
const $ZodCheckOverwrite = /*@__PURE__*/ $constructor("$ZodCheckOverwrite", (inst, def)=>{
|
|
1364
|
+
$ZodCheck.init(inst, def);
|
|
1365
|
+
inst._zod.check = (payload)=>{
|
|
1366
|
+
payload.value = def.tx(payload.value);
|
|
1367
|
+
};
|
|
1368
|
+
});
|
|
1369
|
+
class Doc {
|
|
1370
|
+
constructor(args = []){
|
|
1371
|
+
this.content = [];
|
|
1372
|
+
this.indent = 0;
|
|
1373
|
+
if (this) this.args = args;
|
|
1374
|
+
}
|
|
1375
|
+
indented(fn) {
|
|
1376
|
+
this.indent += 1;
|
|
1377
|
+
fn(this);
|
|
1378
|
+
this.indent -= 1;
|
|
1379
|
+
}
|
|
1380
|
+
write(arg) {
|
|
1381
|
+
if ("function" == typeof arg) {
|
|
1382
|
+
arg(this, {
|
|
1383
|
+
execution: "sync"
|
|
1384
|
+
});
|
|
1385
|
+
arg(this, {
|
|
1386
|
+
execution: "async"
|
|
1387
|
+
});
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
const content = arg;
|
|
1391
|
+
const lines = content.split("\n").filter((x)=>x);
|
|
1392
|
+
const minIndent = Math.min(...lines.map((x)=>x.length - x.trimStart().length));
|
|
1393
|
+
const dedented = lines.map((x)=>x.slice(minIndent)).map((x)=>" ".repeat(2 * this.indent) + x);
|
|
1394
|
+
for (const line of dedented)this.content.push(line);
|
|
1395
|
+
}
|
|
1396
|
+
compile() {
|
|
1397
|
+
const F = Function;
|
|
1398
|
+
const args = this?.args;
|
|
1399
|
+
const content = this?.content ?? [
|
|
1400
|
+
""
|
|
1401
|
+
];
|
|
1402
|
+
const lines = [
|
|
1403
|
+
...content.map((x)=>` ${x}`)
|
|
1404
|
+
];
|
|
1405
|
+
return new F(...args, lines.join("\n"));
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
const versions_version = {
|
|
1409
|
+
major: 4,
|
|
1410
|
+
minor: 2,
|
|
1411
|
+
patch: 0
|
|
1412
|
+
};
|
|
1413
|
+
const $ZodType = /*@__PURE__*/ $constructor("$ZodType", (inst, def)=>{
|
|
1414
|
+
var _a;
|
|
1415
|
+
inst ?? (inst = {});
|
|
1416
|
+
inst._zod.def = def;
|
|
1417
|
+
inst._zod.bag = inst._zod.bag || {};
|
|
1418
|
+
inst._zod.version = versions_version;
|
|
1419
|
+
const checks = [
|
|
1420
|
+
...inst._zod.def.checks ?? []
|
|
1421
|
+
];
|
|
1422
|
+
if (inst._zod.traits.has("$ZodCheck")) checks.unshift(inst);
|
|
1423
|
+
for (const ch of checks)for (const fn of ch._zod.onattach)fn(inst);
|
|
1424
|
+
if (0 === checks.length) {
|
|
1425
|
+
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
1426
|
+
inst._zod.deferred?.push(()=>{
|
|
1427
|
+
inst._zod.run = inst._zod.parse;
|
|
1428
|
+
});
|
|
1429
|
+
} else {
|
|
1430
|
+
const runChecks = (payload, checks, ctx)=>{
|
|
1431
|
+
let isAborted = aborted(payload);
|
|
1432
|
+
let asyncResult;
|
|
1433
|
+
for (const ch of checks){
|
|
1434
|
+
if (ch._zod.def.when) {
|
|
1435
|
+
const shouldRun = ch._zod.def.when(payload);
|
|
1436
|
+
if (!shouldRun) continue;
|
|
1437
|
+
} else if (isAborted) continue;
|
|
1438
|
+
const currLen = payload.issues.length;
|
|
1439
|
+
const _ = ch._zod.check(payload);
|
|
1440
|
+
if (_ instanceof Promise && ctx?.async === false) throw new $ZodAsyncError();
|
|
1441
|
+
if (asyncResult || _ instanceof Promise) asyncResult = (asyncResult ?? Promise.resolve()).then(async ()=>{
|
|
1442
|
+
await _;
|
|
1443
|
+
const nextLen = payload.issues.length;
|
|
1444
|
+
if (nextLen === currLen) return;
|
|
1445
|
+
if (!isAborted) isAborted = aborted(payload, currLen);
|
|
1446
|
+
});
|
|
1447
|
+
else {
|
|
1448
|
+
const nextLen = payload.issues.length;
|
|
1449
|
+
if (nextLen === currLen) continue;
|
|
1450
|
+
if (!isAborted) isAborted = aborted(payload, currLen);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
if (asyncResult) return asyncResult.then(()=>payload);
|
|
1454
|
+
return payload;
|
|
1455
|
+
};
|
|
1456
|
+
const handleCanaryResult = (canary, payload, ctx)=>{
|
|
1457
|
+
if (aborted(canary)) {
|
|
1458
|
+
canary.aborted = true;
|
|
1459
|
+
return canary;
|
|
1460
|
+
}
|
|
1461
|
+
const checkResult = runChecks(payload, checks, ctx);
|
|
1462
|
+
if (checkResult instanceof Promise) {
|
|
1463
|
+
if (false === ctx.async) throw new $ZodAsyncError();
|
|
1464
|
+
return checkResult.then((checkResult)=>inst._zod.parse(checkResult, ctx));
|
|
1465
|
+
}
|
|
1466
|
+
return inst._zod.parse(checkResult, ctx);
|
|
1467
|
+
};
|
|
1468
|
+
inst._zod.run = (payload, ctx)=>{
|
|
1469
|
+
if (ctx.skipChecks) return inst._zod.parse(payload, ctx);
|
|
1470
|
+
if ("backward" === ctx.direction) {
|
|
1471
|
+
const canary = inst._zod.parse({
|
|
1472
|
+
value: payload.value,
|
|
1473
|
+
issues: []
|
|
1474
|
+
}, {
|
|
1475
|
+
...ctx,
|
|
1476
|
+
skipChecks: true
|
|
1477
|
+
});
|
|
1478
|
+
if (canary instanceof Promise) return canary.then((canary)=>handleCanaryResult(canary, payload, ctx));
|
|
1479
|
+
return handleCanaryResult(canary, payload, ctx);
|
|
1480
|
+
}
|
|
1481
|
+
const result = inst._zod.parse(payload, ctx);
|
|
1482
|
+
if (result instanceof Promise) {
|
|
1483
|
+
if (false === ctx.async) throw new $ZodAsyncError();
|
|
1484
|
+
return result.then((result)=>runChecks(result, checks, ctx));
|
|
1485
|
+
}
|
|
1486
|
+
return runChecks(result, checks, ctx);
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
inst["~standard"] = {
|
|
1490
|
+
validate: (value)=>{
|
|
1491
|
+
try {
|
|
1492
|
+
const r = safeParse(inst, value);
|
|
1493
|
+
return r.success ? {
|
|
1494
|
+
value: r.data
|
|
1495
|
+
} : {
|
|
1496
|
+
issues: r.error?.issues
|
|
1497
|
+
};
|
|
1498
|
+
} catch (_) {
|
|
1499
|
+
return safeParseAsync(inst, value).then((r)=>r.success ? {
|
|
1500
|
+
value: r.data
|
|
1501
|
+
} : {
|
|
1502
|
+
issues: r.error?.issues
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
},
|
|
1506
|
+
vendor: "zod",
|
|
1507
|
+
version: 1
|
|
1508
|
+
};
|
|
1509
|
+
});
|
|
1510
|
+
const $ZodString = /*@__PURE__*/ $constructor("$ZodString", (inst, def)=>{
|
|
1511
|
+
$ZodType.init(inst, def);
|
|
1512
|
+
inst._zod.pattern = [
|
|
1513
|
+
...inst?._zod.bag?.patterns ?? []
|
|
1514
|
+
].pop() ?? string(inst._zod.bag);
|
|
1515
|
+
inst._zod.parse = (payload, _)=>{
|
|
1516
|
+
if (def.coerce) try {
|
|
1517
|
+
payload.value = String(payload.value);
|
|
1518
|
+
} catch (_) {}
|
|
1519
|
+
if ("string" == typeof payload.value) return payload;
|
|
1520
|
+
payload.issues.push({
|
|
1521
|
+
expected: "string",
|
|
1522
|
+
code: "invalid_type",
|
|
1523
|
+
input: payload.value,
|
|
1524
|
+
inst
|
|
1525
|
+
});
|
|
1526
|
+
return payload;
|
|
1527
|
+
};
|
|
1528
|
+
});
|
|
1529
|
+
const $ZodStringFormat = /*@__PURE__*/ $constructor("$ZodStringFormat", (inst, def)=>{
|
|
1530
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1531
|
+
$ZodString.init(inst, def);
|
|
1532
|
+
});
|
|
1533
|
+
const $ZodGUID = /*@__PURE__*/ $constructor("$ZodGUID", (inst, def)=>{
|
|
1534
|
+
def.pattern ?? (def.pattern = guid);
|
|
1535
|
+
$ZodStringFormat.init(inst, def);
|
|
1536
|
+
});
|
|
1537
|
+
const $ZodUUID = /*@__PURE__*/ $constructor("$ZodUUID", (inst, def)=>{
|
|
1538
|
+
if (def.version) {
|
|
1539
|
+
const versionMap = {
|
|
1540
|
+
v1: 1,
|
|
1541
|
+
v2: 2,
|
|
1542
|
+
v3: 3,
|
|
1543
|
+
v4: 4,
|
|
1544
|
+
v5: 5,
|
|
1545
|
+
v6: 6,
|
|
1546
|
+
v7: 7,
|
|
1547
|
+
v8: 8
|
|
1548
|
+
};
|
|
1549
|
+
const v = versionMap[def.version];
|
|
1550
|
+
if (void 0 === v) throw new Error(`Invalid UUID version: "${def.version}"`);
|
|
1551
|
+
def.pattern ?? (def.pattern = uuid(v));
|
|
1552
|
+
} else def.pattern ?? (def.pattern = uuid());
|
|
1553
|
+
$ZodStringFormat.init(inst, def);
|
|
1554
|
+
});
|
|
1555
|
+
const $ZodEmail = /*@__PURE__*/ $constructor("$ZodEmail", (inst, def)=>{
|
|
1556
|
+
def.pattern ?? (def.pattern = email);
|
|
1557
|
+
$ZodStringFormat.init(inst, def);
|
|
1558
|
+
});
|
|
1559
|
+
const $ZodURL = /*@__PURE__*/ $constructor("$ZodURL", (inst, def)=>{
|
|
1560
|
+
$ZodStringFormat.init(inst, def);
|
|
1561
|
+
inst._zod.check = (payload)=>{
|
|
1562
|
+
try {
|
|
1563
|
+
const trimmed = payload.value.trim();
|
|
1564
|
+
const url = new URL(trimmed);
|
|
1565
|
+
if (def.hostname) {
|
|
1566
|
+
def.hostname.lastIndex = 0;
|
|
1567
|
+
if (!def.hostname.test(url.hostname)) payload.issues.push({
|
|
1568
|
+
code: "invalid_format",
|
|
1569
|
+
format: "url",
|
|
1570
|
+
note: "Invalid hostname",
|
|
1571
|
+
pattern: def.hostname.source,
|
|
1572
|
+
input: payload.value,
|
|
1573
|
+
inst,
|
|
1574
|
+
continue: !def.abort
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
if (def.protocol) {
|
|
1578
|
+
def.protocol.lastIndex = 0;
|
|
1579
|
+
if (!def.protocol.test(url.protocol.endsWith(":") ? url.protocol.slice(0, -1) : url.protocol)) payload.issues.push({
|
|
1580
|
+
code: "invalid_format",
|
|
1581
|
+
format: "url",
|
|
1582
|
+
note: "Invalid protocol",
|
|
1583
|
+
pattern: def.protocol.source,
|
|
1584
|
+
input: payload.value,
|
|
1585
|
+
inst,
|
|
1586
|
+
continue: !def.abort
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
if (def.normalize) payload.value = url.href;
|
|
1590
|
+
else payload.value = trimmed;
|
|
1591
|
+
return;
|
|
1592
|
+
} catch (_) {
|
|
1593
|
+
payload.issues.push({
|
|
1594
|
+
code: "invalid_format",
|
|
1595
|
+
format: "url",
|
|
1596
|
+
input: payload.value,
|
|
1597
|
+
inst,
|
|
1598
|
+
continue: !def.abort
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1601
|
+
};
|
|
1602
|
+
});
|
|
1603
|
+
const $ZodEmoji = /*@__PURE__*/ $constructor("$ZodEmoji", (inst, def)=>{
|
|
1604
|
+
def.pattern ?? (def.pattern = emoji());
|
|
1605
|
+
$ZodStringFormat.init(inst, def);
|
|
1606
|
+
});
|
|
1607
|
+
const $ZodNanoID = /*@__PURE__*/ $constructor("$ZodNanoID", (inst, def)=>{
|
|
1608
|
+
def.pattern ?? (def.pattern = nanoid);
|
|
1609
|
+
$ZodStringFormat.init(inst, def);
|
|
1610
|
+
});
|
|
1611
|
+
const $ZodCUID = /*@__PURE__*/ $constructor("$ZodCUID", (inst, def)=>{
|
|
1612
|
+
def.pattern ?? (def.pattern = cuid);
|
|
1613
|
+
$ZodStringFormat.init(inst, def);
|
|
1614
|
+
});
|
|
1615
|
+
const $ZodCUID2 = /*@__PURE__*/ $constructor("$ZodCUID2", (inst, def)=>{
|
|
1616
|
+
def.pattern ?? (def.pattern = cuid2);
|
|
1617
|
+
$ZodStringFormat.init(inst, def);
|
|
1618
|
+
});
|
|
1619
|
+
const $ZodULID = /*@__PURE__*/ $constructor("$ZodULID", (inst, def)=>{
|
|
1620
|
+
def.pattern ?? (def.pattern = ulid);
|
|
1621
|
+
$ZodStringFormat.init(inst, def);
|
|
1622
|
+
});
|
|
1623
|
+
const $ZodXID = /*@__PURE__*/ $constructor("$ZodXID", (inst, def)=>{
|
|
1624
|
+
def.pattern ?? (def.pattern = xid);
|
|
1625
|
+
$ZodStringFormat.init(inst, def);
|
|
1626
|
+
});
|
|
1627
|
+
const $ZodKSUID = /*@__PURE__*/ $constructor("$ZodKSUID", (inst, def)=>{
|
|
1628
|
+
def.pattern ?? (def.pattern = ksuid);
|
|
1629
|
+
$ZodStringFormat.init(inst, def);
|
|
1630
|
+
});
|
|
1631
|
+
const $ZodISODateTime = /*@__PURE__*/ $constructor("$ZodISODateTime", (inst, def)=>{
|
|
1632
|
+
def.pattern ?? (def.pattern = datetime(def));
|
|
1633
|
+
$ZodStringFormat.init(inst, def);
|
|
1634
|
+
});
|
|
1635
|
+
const $ZodISODate = /*@__PURE__*/ $constructor("$ZodISODate", (inst, def)=>{
|
|
1636
|
+
def.pattern ?? (def.pattern = date);
|
|
1637
|
+
$ZodStringFormat.init(inst, def);
|
|
1638
|
+
});
|
|
1639
|
+
const $ZodISOTime = /*@__PURE__*/ $constructor("$ZodISOTime", (inst, def)=>{
|
|
1640
|
+
def.pattern ?? (def.pattern = regexes_time(def));
|
|
1641
|
+
$ZodStringFormat.init(inst, def);
|
|
1642
|
+
});
|
|
1643
|
+
const $ZodISODuration = /*@__PURE__*/ $constructor("$ZodISODuration", (inst, def)=>{
|
|
1644
|
+
def.pattern ?? (def.pattern = duration);
|
|
1645
|
+
$ZodStringFormat.init(inst, def);
|
|
1646
|
+
});
|
|
1647
|
+
const $ZodIPv4 = /*@__PURE__*/ $constructor("$ZodIPv4", (inst, def)=>{
|
|
1648
|
+
def.pattern ?? (def.pattern = ipv4);
|
|
1649
|
+
$ZodStringFormat.init(inst, def);
|
|
1650
|
+
inst._zod.bag.format = "ipv4";
|
|
1651
|
+
});
|
|
1652
|
+
const $ZodIPv6 = /*@__PURE__*/ $constructor("$ZodIPv6", (inst, def)=>{
|
|
1653
|
+
def.pattern ?? (def.pattern = ipv6);
|
|
1654
|
+
$ZodStringFormat.init(inst, def);
|
|
1655
|
+
inst._zod.bag.format = "ipv6";
|
|
1656
|
+
inst._zod.check = (payload)=>{
|
|
1657
|
+
try {
|
|
1658
|
+
new URL(`http://[${payload.value}]`);
|
|
1659
|
+
} catch {
|
|
1660
|
+
payload.issues.push({
|
|
1661
|
+
code: "invalid_format",
|
|
1662
|
+
format: "ipv6",
|
|
1663
|
+
input: payload.value,
|
|
1664
|
+
inst,
|
|
1665
|
+
continue: !def.abort
|
|
1666
|
+
});
|
|
1667
|
+
}
|
|
1668
|
+
};
|
|
1669
|
+
});
|
|
1670
|
+
const $ZodCIDRv4 = /*@__PURE__*/ $constructor("$ZodCIDRv4", (inst, def)=>{
|
|
1671
|
+
def.pattern ?? (def.pattern = cidrv4);
|
|
1672
|
+
$ZodStringFormat.init(inst, def);
|
|
1673
|
+
});
|
|
1674
|
+
const $ZodCIDRv6 = /*@__PURE__*/ $constructor("$ZodCIDRv6", (inst, def)=>{
|
|
1675
|
+
def.pattern ?? (def.pattern = cidrv6);
|
|
1676
|
+
$ZodStringFormat.init(inst, def);
|
|
1677
|
+
inst._zod.check = (payload)=>{
|
|
1678
|
+
const parts = payload.value.split("/");
|
|
1679
|
+
try {
|
|
1680
|
+
if (2 !== parts.length) throw new Error();
|
|
1681
|
+
const [address, prefix] = parts;
|
|
1682
|
+
if (!prefix) throw new Error();
|
|
1683
|
+
const prefixNum = Number(prefix);
|
|
1684
|
+
if (`${prefixNum}` !== prefix) throw new Error();
|
|
1685
|
+
if (prefixNum < 0 || prefixNum > 128) throw new Error();
|
|
1686
|
+
new URL(`http://[${address}]`);
|
|
1687
|
+
} catch {
|
|
1688
|
+
payload.issues.push({
|
|
1689
|
+
code: "invalid_format",
|
|
1690
|
+
format: "cidrv6",
|
|
1691
|
+
input: payload.value,
|
|
1692
|
+
inst,
|
|
1693
|
+
continue: !def.abort
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
});
|
|
1698
|
+
function isValidBase64(data) {
|
|
1699
|
+
if ("" === data) return true;
|
|
1700
|
+
if (data.length % 4 !== 0) return false;
|
|
1701
|
+
try {
|
|
1702
|
+
atob(data);
|
|
1703
|
+
return true;
|
|
1704
|
+
} catch {
|
|
1705
|
+
return false;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
const $ZodBase64 = /*@__PURE__*/ $constructor("$ZodBase64", (inst, def)=>{
|
|
1709
|
+
def.pattern ?? (def.pattern = regexes_base64);
|
|
1710
|
+
$ZodStringFormat.init(inst, def);
|
|
1711
|
+
inst._zod.bag.contentEncoding = "base64";
|
|
1712
|
+
inst._zod.check = (payload)=>{
|
|
1713
|
+
if (isValidBase64(payload.value)) return;
|
|
1714
|
+
payload.issues.push({
|
|
1715
|
+
code: "invalid_format",
|
|
1716
|
+
format: "base64",
|
|
1717
|
+
input: payload.value,
|
|
1718
|
+
inst,
|
|
1719
|
+
continue: !def.abort
|
|
1720
|
+
});
|
|
1721
|
+
};
|
|
1722
|
+
});
|
|
1723
|
+
function isValidBase64URL(data) {
|
|
1724
|
+
if (!regexes_base64url.test(data)) return false;
|
|
1725
|
+
const base64 = data.replace(/[-_]/g, (c)=>"-" === c ? "+" : "/");
|
|
1726
|
+
const padded = base64.padEnd(4 * Math.ceil(base64.length / 4), "=");
|
|
1727
|
+
return isValidBase64(padded);
|
|
1728
|
+
}
|
|
1729
|
+
const $ZodBase64URL = /*@__PURE__*/ $constructor("$ZodBase64URL", (inst, def)=>{
|
|
1730
|
+
def.pattern ?? (def.pattern = regexes_base64url);
|
|
1731
|
+
$ZodStringFormat.init(inst, def);
|
|
1732
|
+
inst._zod.bag.contentEncoding = "base64url";
|
|
1733
|
+
inst._zod.check = (payload)=>{
|
|
1734
|
+
if (isValidBase64URL(payload.value)) return;
|
|
1735
|
+
payload.issues.push({
|
|
1736
|
+
code: "invalid_format",
|
|
1737
|
+
format: "base64url",
|
|
1738
|
+
input: payload.value,
|
|
1739
|
+
inst,
|
|
1740
|
+
continue: !def.abort
|
|
1741
|
+
});
|
|
1742
|
+
};
|
|
1743
|
+
});
|
|
1744
|
+
const $ZodE164 = /*@__PURE__*/ $constructor("$ZodE164", (inst, def)=>{
|
|
1745
|
+
def.pattern ?? (def.pattern = e164);
|
|
1746
|
+
$ZodStringFormat.init(inst, def);
|
|
1747
|
+
});
|
|
1748
|
+
function isValidJWT(token, algorithm = null) {
|
|
1749
|
+
try {
|
|
1750
|
+
const tokensParts = token.split(".");
|
|
1751
|
+
if (3 !== tokensParts.length) return false;
|
|
1752
|
+
const [header] = tokensParts;
|
|
1753
|
+
if (!header) return false;
|
|
1754
|
+
const parsedHeader = JSON.parse(atob(header));
|
|
1755
|
+
if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT") return false;
|
|
1756
|
+
if (!parsedHeader.alg) return false;
|
|
1757
|
+
if (algorithm && (!("alg" in parsedHeader) || parsedHeader.alg !== algorithm)) return false;
|
|
1758
|
+
return true;
|
|
1759
|
+
} catch {
|
|
1760
|
+
return false;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
const $ZodJWT = /*@__PURE__*/ $constructor("$ZodJWT", (inst, def)=>{
|
|
1764
|
+
$ZodStringFormat.init(inst, def);
|
|
1765
|
+
inst._zod.check = (payload)=>{
|
|
1766
|
+
if (isValidJWT(payload.value, def.alg)) return;
|
|
1767
|
+
payload.issues.push({
|
|
1768
|
+
code: "invalid_format",
|
|
1769
|
+
format: "jwt",
|
|
1770
|
+
input: payload.value,
|
|
1771
|
+
inst,
|
|
1772
|
+
continue: !def.abort
|
|
1773
|
+
});
|
|
1774
|
+
};
|
|
1775
|
+
});
|
|
1776
|
+
const $ZodNumber = /*@__PURE__*/ $constructor("$ZodNumber", (inst, def)=>{
|
|
1777
|
+
$ZodType.init(inst, def);
|
|
1778
|
+
inst._zod.pattern = inst._zod.bag.pattern ?? regexes_number;
|
|
1779
|
+
inst._zod.parse = (payload, _ctx)=>{
|
|
1780
|
+
if (def.coerce) try {
|
|
1781
|
+
payload.value = Number(payload.value);
|
|
1782
|
+
} catch (_) {}
|
|
1783
|
+
const input = payload.value;
|
|
1784
|
+
if ("number" == typeof input && !Number.isNaN(input) && Number.isFinite(input)) return payload;
|
|
1785
|
+
const received = "number" == typeof input ? Number.isNaN(input) ? "NaN" : Number.isFinite(input) ? void 0 : "Infinity" : void 0;
|
|
1786
|
+
payload.issues.push({
|
|
1787
|
+
expected: "number",
|
|
1788
|
+
code: "invalid_type",
|
|
1789
|
+
input,
|
|
1790
|
+
inst,
|
|
1791
|
+
...received ? {
|
|
1792
|
+
received
|
|
1793
|
+
} : {}
|
|
1794
|
+
});
|
|
1795
|
+
return payload;
|
|
1796
|
+
};
|
|
1797
|
+
});
|
|
1798
|
+
const $ZodNumberFormat = /*@__PURE__*/ $constructor("$ZodNumberFormat", (inst, def)=>{
|
|
1799
|
+
$ZodCheckNumberFormat.init(inst, def);
|
|
1800
|
+
$ZodNumber.init(inst, def);
|
|
1801
|
+
});
|
|
1802
|
+
const $ZodUnknown = /*@__PURE__*/ $constructor("$ZodUnknown", (inst, def)=>{
|
|
1803
|
+
$ZodType.init(inst, def);
|
|
1804
|
+
inst._zod.parse = (payload)=>payload;
|
|
1805
|
+
});
|
|
1806
|
+
const $ZodNever = /*@__PURE__*/ $constructor("$ZodNever", (inst, def)=>{
|
|
1807
|
+
$ZodType.init(inst, def);
|
|
1808
|
+
inst._zod.parse = (payload, _ctx)=>{
|
|
1809
|
+
payload.issues.push({
|
|
1810
|
+
expected: "never",
|
|
1811
|
+
code: "invalid_type",
|
|
1812
|
+
input: payload.value,
|
|
1813
|
+
inst
|
|
1814
|
+
});
|
|
1815
|
+
return payload;
|
|
1816
|
+
};
|
|
1817
|
+
});
|
|
1818
|
+
function handleArrayResult(result, final, index) {
|
|
1819
|
+
if (result.issues.length) final.issues.push(...prefixIssues(index, result.issues));
|
|
1820
|
+
final.value[index] = result.value;
|
|
1821
|
+
}
|
|
1822
|
+
const $ZodArray = /*@__PURE__*/ $constructor("$ZodArray", (inst, def)=>{
|
|
1823
|
+
$ZodType.init(inst, def);
|
|
1824
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
1825
|
+
const input = payload.value;
|
|
1826
|
+
if (!Array.isArray(input)) {
|
|
1827
|
+
payload.issues.push({
|
|
1828
|
+
expected: "array",
|
|
1829
|
+
code: "invalid_type",
|
|
1830
|
+
input,
|
|
1831
|
+
inst
|
|
1832
|
+
});
|
|
1833
|
+
return payload;
|
|
1834
|
+
}
|
|
1835
|
+
payload.value = Array(input.length);
|
|
1836
|
+
const proms = [];
|
|
1837
|
+
for(let i = 0; i < input.length; i++){
|
|
1838
|
+
const item = input[i];
|
|
1839
|
+
const result = def.element._zod.run({
|
|
1840
|
+
value: item,
|
|
1841
|
+
issues: []
|
|
1842
|
+
}, ctx);
|
|
1843
|
+
if (result instanceof Promise) proms.push(result.then((result)=>handleArrayResult(result, payload, i)));
|
|
1844
|
+
else handleArrayResult(result, payload, i);
|
|
1845
|
+
}
|
|
1846
|
+
if (proms.length) return Promise.all(proms).then(()=>payload);
|
|
1847
|
+
return payload;
|
|
1848
|
+
};
|
|
1849
|
+
});
|
|
1850
|
+
function handlePropertyResult(result, final, key, input) {
|
|
1851
|
+
if (result.issues.length) final.issues.push(...prefixIssues(key, result.issues));
|
|
1852
|
+
if (void 0 === result.value) {
|
|
1853
|
+
if (key in input) final.value[key] = void 0;
|
|
1854
|
+
} else final.value[key] = result.value;
|
|
1855
|
+
}
|
|
1856
|
+
function normalizeDef(def) {
|
|
1857
|
+
const keys = Object.keys(def.shape);
|
|
1858
|
+
for (const k of keys)if (!def.shape?.[k]?._zod?.traits?.has("$ZodType")) throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
|
|
1859
|
+
const okeys = optionalKeys(def.shape);
|
|
1860
|
+
return {
|
|
1861
|
+
...def,
|
|
1862
|
+
keys,
|
|
1863
|
+
keySet: new Set(keys),
|
|
1864
|
+
numKeys: keys.length,
|
|
1865
|
+
optionalKeys: new Set(okeys)
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
function handleCatchall(proms, input, payload, ctx, def, inst) {
|
|
1869
|
+
const unrecognized = [];
|
|
1870
|
+
const keySet = def.keySet;
|
|
1871
|
+
const _catchall = def.catchall._zod;
|
|
1872
|
+
const t = _catchall.def.type;
|
|
1873
|
+
for(const key in input){
|
|
1874
|
+
if (keySet.has(key)) continue;
|
|
1875
|
+
if ("never" === t) {
|
|
1876
|
+
unrecognized.push(key);
|
|
1877
|
+
continue;
|
|
1878
|
+
}
|
|
1879
|
+
const r = _catchall.run({
|
|
1880
|
+
value: input[key],
|
|
1881
|
+
issues: []
|
|
1882
|
+
}, ctx);
|
|
1883
|
+
if (r instanceof Promise) proms.push(r.then((r)=>handlePropertyResult(r, payload, key, input)));
|
|
1884
|
+
else handlePropertyResult(r, payload, key, input);
|
|
1885
|
+
}
|
|
1886
|
+
if (unrecognized.length) payload.issues.push({
|
|
1887
|
+
code: "unrecognized_keys",
|
|
1888
|
+
keys: unrecognized,
|
|
1889
|
+
input,
|
|
1890
|
+
inst
|
|
1891
|
+
});
|
|
1892
|
+
if (!proms.length) return payload;
|
|
1893
|
+
return Promise.all(proms).then(()=>payload);
|
|
1894
|
+
}
|
|
1895
|
+
const $ZodObject = /*@__PURE__*/ $constructor("$ZodObject", (inst, def)=>{
|
|
1896
|
+
$ZodType.init(inst, def);
|
|
1897
|
+
const desc = Object.getOwnPropertyDescriptor(def, "shape");
|
|
1898
|
+
if (!desc?.get) {
|
|
1899
|
+
const sh = def.shape;
|
|
1900
|
+
Object.defineProperty(def, "shape", {
|
|
1901
|
+
get: ()=>{
|
|
1902
|
+
const newSh = {
|
|
1903
|
+
...sh
|
|
1904
|
+
};
|
|
1905
|
+
Object.defineProperty(def, "shape", {
|
|
1906
|
+
value: newSh
|
|
1907
|
+
});
|
|
1908
|
+
return newSh;
|
|
1909
|
+
}
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
const _normalized = cached(()=>normalizeDef(def));
|
|
1913
|
+
defineLazy(inst._zod, "propValues", ()=>{
|
|
1914
|
+
const shape = def.shape;
|
|
1915
|
+
const propValues = {};
|
|
1916
|
+
for(const key in shape){
|
|
1917
|
+
const field = shape[key]._zod;
|
|
1918
|
+
if (field.values) {
|
|
1919
|
+
propValues[key] ?? (propValues[key] = new Set());
|
|
1920
|
+
for (const v of field.values)propValues[key].add(v);
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
return propValues;
|
|
1924
|
+
});
|
|
1925
|
+
const isObject = util_isObject;
|
|
1926
|
+
const catchall = def.catchall;
|
|
1927
|
+
let value;
|
|
1928
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
1929
|
+
value ?? (value = _normalized.value);
|
|
1930
|
+
const input = payload.value;
|
|
1931
|
+
if (!isObject(input)) {
|
|
1932
|
+
payload.issues.push({
|
|
1933
|
+
expected: "object",
|
|
1934
|
+
code: "invalid_type",
|
|
1935
|
+
input,
|
|
1936
|
+
inst
|
|
1937
|
+
});
|
|
1938
|
+
return payload;
|
|
1939
|
+
}
|
|
1940
|
+
payload.value = {};
|
|
1941
|
+
const proms = [];
|
|
1942
|
+
const shape = value.shape;
|
|
1943
|
+
for (const key of value.keys){
|
|
1944
|
+
const el = shape[key];
|
|
1945
|
+
const r = el._zod.run({
|
|
1946
|
+
value: input[key],
|
|
1947
|
+
issues: []
|
|
1948
|
+
}, ctx);
|
|
1949
|
+
if (r instanceof Promise) proms.push(r.then((r)=>handlePropertyResult(r, payload, key, input)));
|
|
1950
|
+
else handlePropertyResult(r, payload, key, input);
|
|
1951
|
+
}
|
|
1952
|
+
if (!catchall) return proms.length ? Promise.all(proms).then(()=>payload) : payload;
|
|
1953
|
+
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
|
|
1954
|
+
};
|
|
1955
|
+
});
|
|
1956
|
+
const $ZodObjectJIT = /*@__PURE__*/ $constructor("$ZodObjectJIT", (inst, def)=>{
|
|
1957
|
+
$ZodObject.init(inst, def);
|
|
1958
|
+
const superParse = inst._zod.parse;
|
|
1959
|
+
const _normalized = cached(()=>normalizeDef(def));
|
|
1960
|
+
const generateFastpass = (shape)=>{
|
|
1961
|
+
const doc = new Doc([
|
|
1962
|
+
"shape",
|
|
1963
|
+
"payload",
|
|
1964
|
+
"ctx"
|
|
1965
|
+
]);
|
|
1966
|
+
const normalized = _normalized.value;
|
|
1967
|
+
const parseStr = (key)=>{
|
|
1968
|
+
const k = esc(key);
|
|
1969
|
+
return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
|
|
1970
|
+
};
|
|
1971
|
+
doc.write("const input = payload.value;");
|
|
1972
|
+
const ids = Object.create(null);
|
|
1973
|
+
let counter = 0;
|
|
1974
|
+
for (const key of normalized.keys)ids[key] = `key_${counter++}`;
|
|
1975
|
+
doc.write("const newResult = {};");
|
|
1976
|
+
for (const key of normalized.keys){
|
|
1977
|
+
const id = ids[key];
|
|
1978
|
+
const k = esc(key);
|
|
1979
|
+
doc.write(`const ${id} = ${parseStr(key)};`);
|
|
1980
|
+
doc.write(`
|
|
1981
|
+
if (${id}.issues.length) {
|
|
1982
|
+
payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
|
|
1983
|
+
...iss,
|
|
1984
|
+
path: iss.path ? [${k}, ...iss.path] : [${k}]
|
|
1985
|
+
})));
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
if (${id}.value === undefined) {
|
|
1990
|
+
if (${k} in input) {
|
|
1991
|
+
newResult[${k}] = undefined;
|
|
1992
|
+
}
|
|
1993
|
+
} else {
|
|
1994
|
+
newResult[${k}] = ${id}.value;
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
`);
|
|
1998
|
+
}
|
|
1999
|
+
doc.write("payload.value = newResult;");
|
|
2000
|
+
doc.write("return payload;");
|
|
2001
|
+
const fn = doc.compile();
|
|
2002
|
+
return (payload, ctx)=>fn(shape, payload, ctx);
|
|
2003
|
+
};
|
|
2004
|
+
let fastpass;
|
|
2005
|
+
const isObject = util_isObject;
|
|
2006
|
+
const jit = !globalConfig.jitless;
|
|
2007
|
+
const allowsEval = util_allowsEval;
|
|
2008
|
+
const fastEnabled = jit && allowsEval.value;
|
|
2009
|
+
const catchall = def.catchall;
|
|
2010
|
+
let value;
|
|
2011
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2012
|
+
value ?? (value = _normalized.value);
|
|
2013
|
+
const input = payload.value;
|
|
2014
|
+
if (!isObject(input)) {
|
|
2015
|
+
payload.issues.push({
|
|
2016
|
+
expected: "object",
|
|
2017
|
+
code: "invalid_type",
|
|
2018
|
+
input,
|
|
2019
|
+
inst
|
|
2020
|
+
});
|
|
2021
|
+
return payload;
|
|
2022
|
+
}
|
|
2023
|
+
if (jit && fastEnabled && ctx?.async === false && true !== ctx.jitless) {
|
|
2024
|
+
if (!fastpass) fastpass = generateFastpass(def.shape);
|
|
2025
|
+
payload = fastpass(payload, ctx);
|
|
2026
|
+
if (!catchall) return payload;
|
|
2027
|
+
return handleCatchall([], input, payload, ctx, value, inst);
|
|
2028
|
+
}
|
|
2029
|
+
return superParse(payload, ctx);
|
|
2030
|
+
};
|
|
2031
|
+
});
|
|
2032
|
+
function handleUnionResults(results, final, inst, ctx) {
|
|
2033
|
+
for (const result of results)if (0 === result.issues.length) {
|
|
2034
|
+
final.value = result.value;
|
|
2035
|
+
return final;
|
|
2036
|
+
}
|
|
2037
|
+
const nonaborted = results.filter((r)=>!aborted(r));
|
|
2038
|
+
if (1 === nonaborted.length) {
|
|
2039
|
+
final.value = nonaborted[0].value;
|
|
2040
|
+
return nonaborted[0];
|
|
2041
|
+
}
|
|
2042
|
+
final.issues.push({
|
|
2043
|
+
code: "invalid_union",
|
|
2044
|
+
input: final.value,
|
|
2045
|
+
inst,
|
|
2046
|
+
errors: results.map((result)=>result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config())))
|
|
2047
|
+
});
|
|
2048
|
+
return final;
|
|
2049
|
+
}
|
|
2050
|
+
const $ZodUnion = /*@__PURE__*/ $constructor("$ZodUnion", (inst, def)=>{
|
|
2051
|
+
$ZodType.init(inst, def);
|
|
2052
|
+
defineLazy(inst._zod, "optin", ()=>def.options.some((o)=>"optional" === o._zod.optin) ? "optional" : void 0);
|
|
2053
|
+
defineLazy(inst._zod, "optout", ()=>def.options.some((o)=>"optional" === o._zod.optout) ? "optional" : void 0);
|
|
2054
|
+
defineLazy(inst._zod, "values", ()=>{
|
|
2055
|
+
if (def.options.every((o)=>o._zod.values)) return new Set(def.options.flatMap((option)=>Array.from(option._zod.values)));
|
|
2056
|
+
});
|
|
2057
|
+
defineLazy(inst._zod, "pattern", ()=>{
|
|
2058
|
+
if (def.options.every((o)=>o._zod.pattern)) {
|
|
2059
|
+
const patterns = def.options.map((o)=>o._zod.pattern);
|
|
2060
|
+
return new RegExp(`^(${patterns.map((p)=>cleanRegex(p.source)).join("|")})$`);
|
|
2061
|
+
}
|
|
2062
|
+
});
|
|
2063
|
+
const single = 1 === def.options.length;
|
|
2064
|
+
const first = def.options[0]._zod.run;
|
|
2065
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2066
|
+
if (single) return first(payload, ctx);
|
|
2067
|
+
let async = false;
|
|
2068
|
+
const results = [];
|
|
2069
|
+
for (const option of def.options){
|
|
2070
|
+
const result = option._zod.run({
|
|
2071
|
+
value: payload.value,
|
|
2072
|
+
issues: []
|
|
2073
|
+
}, ctx);
|
|
2074
|
+
if (result instanceof Promise) {
|
|
2075
|
+
results.push(result);
|
|
2076
|
+
async = true;
|
|
2077
|
+
} else {
|
|
2078
|
+
if (0 === result.issues.length) return result;
|
|
2079
|
+
results.push(result);
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
if (!async) return handleUnionResults(results, payload, inst, ctx);
|
|
2083
|
+
return Promise.all(results).then((results)=>handleUnionResults(results, payload, inst, ctx));
|
|
2084
|
+
};
|
|
2085
|
+
});
|
|
2086
|
+
const $ZodIntersection = /*@__PURE__*/ $constructor("$ZodIntersection", (inst, def)=>{
|
|
2087
|
+
$ZodType.init(inst, def);
|
|
2088
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2089
|
+
const input = payload.value;
|
|
2090
|
+
const left = def.left._zod.run({
|
|
2091
|
+
value: input,
|
|
2092
|
+
issues: []
|
|
2093
|
+
}, ctx);
|
|
2094
|
+
const right = def.right._zod.run({
|
|
2095
|
+
value: input,
|
|
2096
|
+
issues: []
|
|
2097
|
+
}, ctx);
|
|
2098
|
+
const async = left instanceof Promise || right instanceof Promise;
|
|
2099
|
+
if (async) return Promise.all([
|
|
2100
|
+
left,
|
|
2101
|
+
right
|
|
2102
|
+
]).then(([left, right])=>handleIntersectionResults(payload, left, right));
|
|
2103
|
+
return handleIntersectionResults(payload, left, right);
|
|
2104
|
+
};
|
|
2105
|
+
});
|
|
2106
|
+
function mergeValues(a, b) {
|
|
2107
|
+
if (a === b) return {
|
|
2108
|
+
valid: true,
|
|
2109
|
+
data: a
|
|
2110
|
+
};
|
|
2111
|
+
if (a instanceof Date && b instanceof Date && +a === +b) return {
|
|
2112
|
+
valid: true,
|
|
2113
|
+
data: a
|
|
2114
|
+
};
|
|
2115
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
2116
|
+
const bKeys = Object.keys(b);
|
|
2117
|
+
const sharedKeys = Object.keys(a).filter((key)=>-1 !== bKeys.indexOf(key));
|
|
2118
|
+
const newObj = {
|
|
2119
|
+
...a,
|
|
2120
|
+
...b
|
|
2121
|
+
};
|
|
2122
|
+
for (const key of sharedKeys){
|
|
2123
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
2124
|
+
if (!sharedValue.valid) return {
|
|
2125
|
+
valid: false,
|
|
2126
|
+
mergeErrorPath: [
|
|
2127
|
+
key,
|
|
2128
|
+
...sharedValue.mergeErrorPath
|
|
2129
|
+
]
|
|
2130
|
+
};
|
|
2131
|
+
newObj[key] = sharedValue.data;
|
|
2132
|
+
}
|
|
2133
|
+
return {
|
|
2134
|
+
valid: true,
|
|
2135
|
+
data: newObj
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
2139
|
+
if (a.length !== b.length) return {
|
|
2140
|
+
valid: false,
|
|
2141
|
+
mergeErrorPath: []
|
|
2142
|
+
};
|
|
2143
|
+
const newArray = [];
|
|
2144
|
+
for(let index = 0; index < a.length; index++){
|
|
2145
|
+
const itemA = a[index];
|
|
2146
|
+
const itemB = b[index];
|
|
2147
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
2148
|
+
if (!sharedValue.valid) return {
|
|
2149
|
+
valid: false,
|
|
2150
|
+
mergeErrorPath: [
|
|
2151
|
+
index,
|
|
2152
|
+
...sharedValue.mergeErrorPath
|
|
2153
|
+
]
|
|
2154
|
+
};
|
|
2155
|
+
newArray.push(sharedValue.data);
|
|
2156
|
+
}
|
|
2157
|
+
return {
|
|
2158
|
+
valid: true,
|
|
2159
|
+
data: newArray
|
|
2160
|
+
};
|
|
2161
|
+
}
|
|
2162
|
+
return {
|
|
2163
|
+
valid: false,
|
|
2164
|
+
mergeErrorPath: []
|
|
2165
|
+
};
|
|
2166
|
+
}
|
|
2167
|
+
function handleIntersectionResults(result, left, right) {
|
|
2168
|
+
if (left.issues.length) result.issues.push(...left.issues);
|
|
2169
|
+
if (right.issues.length) result.issues.push(...right.issues);
|
|
2170
|
+
if (aborted(result)) return result;
|
|
2171
|
+
const merged = mergeValues(left.value, right.value);
|
|
2172
|
+
if (!merged.valid) throw new Error(`Unmergable intersection. Error path: ${JSON.stringify(merged.mergeErrorPath)}`);
|
|
2173
|
+
result.value = merged.data;
|
|
2174
|
+
return result;
|
|
2175
|
+
}
|
|
2176
|
+
const $ZodTuple = /*@__PURE__*/ $constructor("$ZodTuple", (inst, def)=>{
|
|
2177
|
+
$ZodType.init(inst, def);
|
|
2178
|
+
const items = def.items;
|
|
2179
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2180
|
+
const input = payload.value;
|
|
2181
|
+
if (!Array.isArray(input)) {
|
|
2182
|
+
payload.issues.push({
|
|
2183
|
+
input,
|
|
2184
|
+
inst,
|
|
2185
|
+
expected: "tuple",
|
|
2186
|
+
code: "invalid_type"
|
|
2187
|
+
});
|
|
2188
|
+
return payload;
|
|
2189
|
+
}
|
|
2190
|
+
payload.value = [];
|
|
2191
|
+
const proms = [];
|
|
2192
|
+
const reversedIndex = [
|
|
2193
|
+
...items
|
|
2194
|
+
].reverse().findIndex((item)=>"optional" !== item._zod.optin);
|
|
2195
|
+
const optStart = -1 === reversedIndex ? 0 : items.length - reversedIndex;
|
|
2196
|
+
if (!def.rest) {
|
|
2197
|
+
const tooBig = input.length > items.length;
|
|
2198
|
+
const tooSmall = input.length < optStart - 1;
|
|
2199
|
+
if (tooBig || tooSmall) {
|
|
2200
|
+
payload.issues.push({
|
|
2201
|
+
...tooBig ? {
|
|
2202
|
+
code: "too_big",
|
|
2203
|
+
maximum: items.length
|
|
2204
|
+
} : {
|
|
2205
|
+
code: "too_small",
|
|
2206
|
+
minimum: items.length
|
|
2207
|
+
},
|
|
2208
|
+
input,
|
|
2209
|
+
inst,
|
|
2210
|
+
origin: "array"
|
|
2211
|
+
});
|
|
2212
|
+
return payload;
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
let i = -1;
|
|
2216
|
+
for (const item of items){
|
|
2217
|
+
i++;
|
|
2218
|
+
if (i >= input.length) {
|
|
2219
|
+
if (i >= optStart) continue;
|
|
2220
|
+
}
|
|
2221
|
+
const result = item._zod.run({
|
|
2222
|
+
value: input[i],
|
|
2223
|
+
issues: []
|
|
2224
|
+
}, ctx);
|
|
2225
|
+
if (result instanceof Promise) proms.push(result.then((result)=>handleTupleResult(result, payload, i)));
|
|
2226
|
+
else handleTupleResult(result, payload, i);
|
|
2227
|
+
}
|
|
2228
|
+
if (def.rest) {
|
|
2229
|
+
const rest = input.slice(items.length);
|
|
2230
|
+
for (const el of rest){
|
|
2231
|
+
i++;
|
|
2232
|
+
const result = def.rest._zod.run({
|
|
2233
|
+
value: el,
|
|
2234
|
+
issues: []
|
|
2235
|
+
}, ctx);
|
|
2236
|
+
if (result instanceof Promise) proms.push(result.then((result)=>handleTupleResult(result, payload, i)));
|
|
2237
|
+
else handleTupleResult(result, payload, i);
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
if (proms.length) return Promise.all(proms).then(()=>payload);
|
|
2241
|
+
return payload;
|
|
2242
|
+
};
|
|
2243
|
+
});
|
|
2244
|
+
function handleTupleResult(result, final, index) {
|
|
2245
|
+
if (result.issues.length) final.issues.push(...prefixIssues(index, result.issues));
|
|
2246
|
+
final.value[index] = result.value;
|
|
2247
|
+
}
|
|
2248
|
+
const $ZodEnum = /*@__PURE__*/ $constructor("$ZodEnum", (inst, def)=>{
|
|
2249
|
+
$ZodType.init(inst, def);
|
|
2250
|
+
const values = getEnumValues(def.entries);
|
|
2251
|
+
const valuesSet = new Set(values);
|
|
2252
|
+
inst._zod.values = valuesSet;
|
|
2253
|
+
inst._zod.pattern = new RegExp(`^(${values.filter((k)=>propertyKeyTypes.has(typeof k)).map((o)=>"string" == typeof o ? escapeRegex(o) : o.toString()).join("|")})$`);
|
|
2254
|
+
inst._zod.parse = (payload, _ctx)=>{
|
|
2255
|
+
const input = payload.value;
|
|
2256
|
+
if (valuesSet.has(input)) return payload;
|
|
2257
|
+
payload.issues.push({
|
|
2258
|
+
code: "invalid_value",
|
|
2259
|
+
values,
|
|
2260
|
+
input,
|
|
2261
|
+
inst
|
|
2262
|
+
});
|
|
2263
|
+
return payload;
|
|
2264
|
+
};
|
|
2265
|
+
});
|
|
2266
|
+
const $ZodTransform = /*@__PURE__*/ $constructor("$ZodTransform", (inst, def)=>{
|
|
2267
|
+
$ZodType.init(inst, def);
|
|
2268
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2269
|
+
if ("backward" === ctx.direction) throw new $ZodEncodeError(inst.constructor.name);
|
|
2270
|
+
const _out = def.transform(payload.value, payload);
|
|
2271
|
+
if (ctx.async) {
|
|
2272
|
+
const output = _out instanceof Promise ? _out : Promise.resolve(_out);
|
|
2273
|
+
return output.then((output)=>{
|
|
2274
|
+
payload.value = output;
|
|
2275
|
+
return payload;
|
|
2276
|
+
});
|
|
2277
|
+
}
|
|
2278
|
+
if (_out instanceof Promise) throw new $ZodAsyncError();
|
|
2279
|
+
payload.value = _out;
|
|
2280
|
+
return payload;
|
|
2281
|
+
};
|
|
2282
|
+
});
|
|
2283
|
+
function handleOptionalResult(result, input) {
|
|
2284
|
+
if (result.issues.length && void 0 === input) return {
|
|
2285
|
+
issues: [],
|
|
2286
|
+
value: void 0
|
|
2287
|
+
};
|
|
2288
|
+
return result;
|
|
2289
|
+
}
|
|
2290
|
+
const $ZodOptional = /*@__PURE__*/ $constructor("$ZodOptional", (inst, def)=>{
|
|
2291
|
+
$ZodType.init(inst, def);
|
|
2292
|
+
inst._zod.optin = "optional";
|
|
2293
|
+
inst._zod.optout = "optional";
|
|
2294
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values ? new Set([
|
|
2295
|
+
...def.innerType._zod.values,
|
|
2296
|
+
void 0
|
|
2297
|
+
]) : void 0);
|
|
2298
|
+
defineLazy(inst._zod, "pattern", ()=>{
|
|
2299
|
+
const pattern = def.innerType._zod.pattern;
|
|
2300
|
+
return pattern ? new RegExp(`^(${cleanRegex(pattern.source)})?$`) : void 0;
|
|
2301
|
+
});
|
|
2302
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2303
|
+
if ("optional" === def.innerType._zod.optin) {
|
|
2304
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2305
|
+
if (result instanceof Promise) return result.then((r)=>handleOptionalResult(r, payload.value));
|
|
2306
|
+
return handleOptionalResult(result, payload.value);
|
|
2307
|
+
}
|
|
2308
|
+
if (void 0 === payload.value) return payload;
|
|
2309
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2310
|
+
};
|
|
2311
|
+
});
|
|
2312
|
+
const $ZodNullable = /*@__PURE__*/ $constructor("$ZodNullable", (inst, def)=>{
|
|
2313
|
+
$ZodType.init(inst, def);
|
|
2314
|
+
defineLazy(inst._zod, "optin", ()=>def.innerType._zod.optin);
|
|
2315
|
+
defineLazy(inst._zod, "optout", ()=>def.innerType._zod.optout);
|
|
2316
|
+
defineLazy(inst._zod, "pattern", ()=>{
|
|
2317
|
+
const pattern = def.innerType._zod.pattern;
|
|
2318
|
+
return pattern ? new RegExp(`^(${cleanRegex(pattern.source)}|null)$`) : void 0;
|
|
2319
|
+
});
|
|
2320
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values ? new Set([
|
|
2321
|
+
...def.innerType._zod.values,
|
|
2322
|
+
null
|
|
2323
|
+
]) : void 0);
|
|
2324
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2325
|
+
if (null === payload.value) return payload;
|
|
2326
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2327
|
+
};
|
|
2328
|
+
});
|
|
2329
|
+
const $ZodDefault = /*@__PURE__*/ $constructor("$ZodDefault", (inst, def)=>{
|
|
2330
|
+
$ZodType.init(inst, def);
|
|
2331
|
+
inst._zod.optin = "optional";
|
|
2332
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values);
|
|
2333
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2334
|
+
if ("backward" === ctx.direction) return def.innerType._zod.run(payload, ctx);
|
|
2335
|
+
if (void 0 === payload.value) {
|
|
2336
|
+
payload.value = def.defaultValue;
|
|
2337
|
+
return payload;
|
|
2338
|
+
}
|
|
2339
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2340
|
+
if (result instanceof Promise) return result.then((result)=>handleDefaultResult(result, def));
|
|
2341
|
+
return handleDefaultResult(result, def);
|
|
2342
|
+
};
|
|
2343
|
+
});
|
|
2344
|
+
function handleDefaultResult(payload, def) {
|
|
2345
|
+
if (void 0 === payload.value) payload.value = def.defaultValue;
|
|
2346
|
+
return payload;
|
|
2347
|
+
}
|
|
2348
|
+
const $ZodPrefault = /*@__PURE__*/ $constructor("$ZodPrefault", (inst, def)=>{
|
|
2349
|
+
$ZodType.init(inst, def);
|
|
2350
|
+
inst._zod.optin = "optional";
|
|
2351
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values);
|
|
2352
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2353
|
+
if ("backward" === ctx.direction) return def.innerType._zod.run(payload, ctx);
|
|
2354
|
+
if (void 0 === payload.value) payload.value = def.defaultValue;
|
|
2355
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2356
|
+
};
|
|
2357
|
+
});
|
|
2358
|
+
const $ZodNonOptional = /*@__PURE__*/ $constructor("$ZodNonOptional", (inst, def)=>{
|
|
2359
|
+
$ZodType.init(inst, def);
|
|
2360
|
+
defineLazy(inst._zod, "values", ()=>{
|
|
2361
|
+
const v = def.innerType._zod.values;
|
|
2362
|
+
return v ? new Set([
|
|
2363
|
+
...v
|
|
2364
|
+
].filter((x)=>void 0 !== x)) : void 0;
|
|
2365
|
+
});
|
|
2366
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2367
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2368
|
+
if (result instanceof Promise) return result.then((result)=>handleNonOptionalResult(result, inst));
|
|
2369
|
+
return handleNonOptionalResult(result, inst);
|
|
2370
|
+
};
|
|
2371
|
+
});
|
|
2372
|
+
function handleNonOptionalResult(payload, inst) {
|
|
2373
|
+
if (!payload.issues.length && void 0 === payload.value) payload.issues.push({
|
|
2374
|
+
code: "invalid_type",
|
|
2375
|
+
expected: "nonoptional",
|
|
2376
|
+
input: payload.value,
|
|
2377
|
+
inst
|
|
2378
|
+
});
|
|
2379
|
+
return payload;
|
|
2380
|
+
}
|
|
2381
|
+
const $ZodCatch = /*@__PURE__*/ $constructor("$ZodCatch", (inst, def)=>{
|
|
2382
|
+
$ZodType.init(inst, def);
|
|
2383
|
+
defineLazy(inst._zod, "optin", ()=>def.innerType._zod.optin);
|
|
2384
|
+
defineLazy(inst._zod, "optout", ()=>def.innerType._zod.optout);
|
|
2385
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values);
|
|
2386
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2387
|
+
if ("backward" === ctx.direction) return def.innerType._zod.run(payload, ctx);
|
|
2388
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2389
|
+
if (result instanceof Promise) return result.then((result)=>{
|
|
2390
|
+
payload.value = result.value;
|
|
2391
|
+
if (result.issues.length) {
|
|
2392
|
+
payload.value = def.catchValue({
|
|
2393
|
+
...payload,
|
|
2394
|
+
error: {
|
|
2395
|
+
issues: result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))
|
|
2396
|
+
},
|
|
2397
|
+
input: payload.value
|
|
2398
|
+
});
|
|
2399
|
+
payload.issues = [];
|
|
2400
|
+
}
|
|
2401
|
+
return payload;
|
|
2402
|
+
});
|
|
2403
|
+
payload.value = result.value;
|
|
2404
|
+
if (result.issues.length) {
|
|
2405
|
+
payload.value = def.catchValue({
|
|
2406
|
+
...payload,
|
|
2407
|
+
error: {
|
|
2408
|
+
issues: result.issues.map((iss)=>finalizeIssue(iss, ctx, core_config()))
|
|
2409
|
+
},
|
|
2410
|
+
input: payload.value
|
|
2411
|
+
});
|
|
2412
|
+
payload.issues = [];
|
|
2413
|
+
}
|
|
2414
|
+
return payload;
|
|
2415
|
+
};
|
|
2416
|
+
});
|
|
2417
|
+
const $ZodPipe = /*@__PURE__*/ $constructor("$ZodPipe", (inst, def)=>{
|
|
2418
|
+
$ZodType.init(inst, def);
|
|
2419
|
+
defineLazy(inst._zod, "values", ()=>def.in._zod.values);
|
|
2420
|
+
defineLazy(inst._zod, "optin", ()=>def.in._zod.optin);
|
|
2421
|
+
defineLazy(inst._zod, "optout", ()=>def.out._zod.optout);
|
|
2422
|
+
defineLazy(inst._zod, "propValues", ()=>def.in._zod.propValues);
|
|
2423
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2424
|
+
if ("backward" === ctx.direction) {
|
|
2425
|
+
const right = def.out._zod.run(payload, ctx);
|
|
2426
|
+
if (right instanceof Promise) return right.then((right)=>handlePipeResult(right, def.in, ctx));
|
|
2427
|
+
return handlePipeResult(right, def.in, ctx);
|
|
2428
|
+
}
|
|
2429
|
+
const left = def.in._zod.run(payload, ctx);
|
|
2430
|
+
if (left instanceof Promise) return left.then((left)=>handlePipeResult(left, def.out, ctx));
|
|
2431
|
+
return handlePipeResult(left, def.out, ctx);
|
|
2432
|
+
};
|
|
2433
|
+
});
|
|
2434
|
+
function handlePipeResult(left, next, ctx) {
|
|
2435
|
+
if (left.issues.length) {
|
|
2436
|
+
left.aborted = true;
|
|
2437
|
+
return left;
|
|
2438
|
+
}
|
|
2439
|
+
return next._zod.run({
|
|
2440
|
+
value: left.value,
|
|
2441
|
+
issues: left.issues
|
|
2442
|
+
}, ctx);
|
|
2443
|
+
}
|
|
2444
|
+
const $ZodReadonly = /*@__PURE__*/ $constructor("$ZodReadonly", (inst, def)=>{
|
|
2445
|
+
$ZodType.init(inst, def);
|
|
2446
|
+
defineLazy(inst._zod, "propValues", ()=>def.innerType._zod.propValues);
|
|
2447
|
+
defineLazy(inst._zod, "values", ()=>def.innerType._zod.values);
|
|
2448
|
+
defineLazy(inst._zod, "optin", ()=>def.innerType?._zod?.optin);
|
|
2449
|
+
defineLazy(inst._zod, "optout", ()=>def.innerType?._zod?.optout);
|
|
2450
|
+
inst._zod.parse = (payload, ctx)=>{
|
|
2451
|
+
if ("backward" === ctx.direction) return def.innerType._zod.run(payload, ctx);
|
|
2452
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2453
|
+
if (result instanceof Promise) return result.then(handleReadonlyResult);
|
|
2454
|
+
return handleReadonlyResult(result);
|
|
2455
|
+
};
|
|
2456
|
+
});
|
|
2457
|
+
function handleReadonlyResult(payload) {
|
|
2458
|
+
payload.value = Object.freeze(payload.value);
|
|
2459
|
+
return payload;
|
|
2460
|
+
}
|
|
2461
|
+
const $ZodCustom = /*@__PURE__*/ $constructor("$ZodCustom", (inst, def)=>{
|
|
2462
|
+
$ZodCheck.init(inst, def);
|
|
2463
|
+
$ZodType.init(inst, def);
|
|
2464
|
+
inst._zod.parse = (payload, _)=>payload;
|
|
2465
|
+
inst._zod.check = (payload)=>{
|
|
2466
|
+
const input = payload.value;
|
|
2467
|
+
const r = def.fn(input);
|
|
2468
|
+
if (r instanceof Promise) return r.then((r)=>handleRefineResult(r, payload, input, inst));
|
|
2469
|
+
handleRefineResult(r, payload, input, inst);
|
|
2470
|
+
};
|
|
2471
|
+
});
|
|
2472
|
+
function handleRefineResult(result, payload, input, inst) {
|
|
2473
|
+
if (!result) {
|
|
2474
|
+
const _iss = {
|
|
2475
|
+
code: "custom",
|
|
2476
|
+
input,
|
|
2477
|
+
inst,
|
|
2478
|
+
path: [
|
|
2479
|
+
...inst._zod.def.path ?? []
|
|
2480
|
+
],
|
|
2481
|
+
continue: !inst._zod.def.abort
|
|
2482
|
+
};
|
|
2483
|
+
if (inst._zod.def.params) _iss.params = inst._zod.def.params;
|
|
2484
|
+
payload.issues.push(util_issue(_iss));
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
const en_parsedType = (data)=>{
|
|
2488
|
+
const t = typeof data;
|
|
2489
|
+
switch(t){
|
|
2490
|
+
case "number":
|
|
2491
|
+
return Number.isNaN(data) ? "NaN" : "number";
|
|
2492
|
+
case "object":
|
|
2493
|
+
if (Array.isArray(data)) return "array";
|
|
2494
|
+
if (null === data) return "null";
|
|
2495
|
+
if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) return data.constructor.name;
|
|
2496
|
+
}
|
|
2497
|
+
return t;
|
|
2498
|
+
};
|
|
2499
|
+
const en_error = ()=>{
|
|
2500
|
+
const Sizable = {
|
|
2501
|
+
string: {
|
|
2502
|
+
unit: "characters",
|
|
2503
|
+
verb: "to have"
|
|
2504
|
+
},
|
|
2505
|
+
file: {
|
|
2506
|
+
unit: "bytes",
|
|
2507
|
+
verb: "to have"
|
|
2508
|
+
},
|
|
2509
|
+
array: {
|
|
2510
|
+
unit: "items",
|
|
2511
|
+
verb: "to have"
|
|
2512
|
+
},
|
|
2513
|
+
set: {
|
|
2514
|
+
unit: "items",
|
|
2515
|
+
verb: "to have"
|
|
2516
|
+
}
|
|
2517
|
+
};
|
|
2518
|
+
function getSizing(origin) {
|
|
2519
|
+
return Sizable[origin] ?? null;
|
|
2520
|
+
}
|
|
2521
|
+
const Nouns = {
|
|
2522
|
+
regex: "input",
|
|
2523
|
+
email: "email address",
|
|
2524
|
+
url: "URL",
|
|
2525
|
+
emoji: "emoji",
|
|
2526
|
+
uuid: "UUID",
|
|
2527
|
+
uuidv4: "UUIDv4",
|
|
2528
|
+
uuidv6: "UUIDv6",
|
|
2529
|
+
nanoid: "nanoid",
|
|
2530
|
+
guid: "GUID",
|
|
2531
|
+
cuid: "cuid",
|
|
2532
|
+
cuid2: "cuid2",
|
|
2533
|
+
ulid: "ULID",
|
|
2534
|
+
xid: "XID",
|
|
2535
|
+
ksuid: "KSUID",
|
|
2536
|
+
datetime: "ISO datetime",
|
|
2537
|
+
date: "ISO date",
|
|
2538
|
+
time: "ISO time",
|
|
2539
|
+
duration: "ISO duration",
|
|
2540
|
+
ipv4: "IPv4 address",
|
|
2541
|
+
ipv6: "IPv6 address",
|
|
2542
|
+
mac: "MAC address",
|
|
2543
|
+
cidrv4: "IPv4 range",
|
|
2544
|
+
cidrv6: "IPv6 range",
|
|
2545
|
+
base64: "base64-encoded string",
|
|
2546
|
+
base64url: "base64url-encoded string",
|
|
2547
|
+
json_string: "JSON string",
|
|
2548
|
+
e164: "E.164 number",
|
|
2549
|
+
jwt: "JWT",
|
|
2550
|
+
template_literal: "input"
|
|
2551
|
+
};
|
|
2552
|
+
return (issue)=>{
|
|
2553
|
+
switch(issue.code){
|
|
2554
|
+
case "invalid_type":
|
|
2555
|
+
return `Invalid input: expected ${issue.expected}, received ${en_parsedType(issue.input)}`;
|
|
2556
|
+
case "invalid_value":
|
|
2557
|
+
if (1 === issue.values.length) return `Invalid input: expected ${stringifyPrimitive(issue.values[0])}`;
|
|
2558
|
+
return `Invalid option: expected one of ${joinValues(issue.values, "|")}`;
|
|
2559
|
+
case "too_big":
|
|
2560
|
+
{
|
|
2561
|
+
const adj = issue.inclusive ? "<=" : "<";
|
|
2562
|
+
const sizing = getSizing(issue.origin);
|
|
2563
|
+
if (sizing) return `Too big: expected ${issue.origin ?? "value"} to have ${adj}${issue.maximum.toString()} ${sizing.unit ?? "elements"}`;
|
|
2564
|
+
return `Too big: expected ${issue.origin ?? "value"} to be ${adj}${issue.maximum.toString()}`;
|
|
2565
|
+
}
|
|
2566
|
+
case "too_small":
|
|
2567
|
+
{
|
|
2568
|
+
const adj = issue.inclusive ? ">=" : ">";
|
|
2569
|
+
const sizing = getSizing(issue.origin);
|
|
2570
|
+
if (sizing) return `Too small: expected ${issue.origin} to have ${adj}${issue.minimum.toString()} ${sizing.unit}`;
|
|
2571
|
+
return `Too small: expected ${issue.origin} to be ${adj}${issue.minimum.toString()}`;
|
|
2572
|
+
}
|
|
2573
|
+
case "invalid_format":
|
|
2574
|
+
{
|
|
2575
|
+
const _issue = issue;
|
|
2576
|
+
if ("starts_with" === _issue.format) return `Invalid string: must start with "${_issue.prefix}"`;
|
|
2577
|
+
if ("ends_with" === _issue.format) return `Invalid string: must end with "${_issue.suffix}"`;
|
|
2578
|
+
if ("includes" === _issue.format) return `Invalid string: must include "${_issue.includes}"`;
|
|
2579
|
+
if ("regex" === _issue.format) return `Invalid string: must match pattern ${_issue.pattern}`;
|
|
2580
|
+
return `Invalid ${Nouns[_issue.format] ?? issue.format}`;
|
|
2581
|
+
}
|
|
2582
|
+
case "not_multiple_of":
|
|
2583
|
+
return `Invalid number: must be a multiple of ${issue.divisor}`;
|
|
2584
|
+
case "unrecognized_keys":
|
|
2585
|
+
return `Unrecognized key${issue.keys.length > 1 ? "s" : ""}: ${joinValues(issue.keys, ", ")}`;
|
|
2586
|
+
case "invalid_key":
|
|
2587
|
+
return `Invalid key in ${issue.origin}`;
|
|
2588
|
+
case "invalid_union":
|
|
2589
|
+
return "Invalid input";
|
|
2590
|
+
case "invalid_element":
|
|
2591
|
+
return `Invalid value in ${issue.origin}`;
|
|
2592
|
+
default:
|
|
2593
|
+
return "Invalid input";
|
|
2594
|
+
}
|
|
2595
|
+
};
|
|
2596
|
+
};
|
|
2597
|
+
function en() {
|
|
2598
|
+
return {
|
|
2599
|
+
localeError: en_error()
|
|
2600
|
+
};
|
|
2601
|
+
}
|
|
2602
|
+
var registries_a;
|
|
2603
|
+
Symbol("ZodOutput");
|
|
2604
|
+
Symbol("ZodInput");
|
|
2605
|
+
class $ZodRegistry {
|
|
2606
|
+
constructor(){
|
|
2607
|
+
this._map = new WeakMap();
|
|
2608
|
+
this._idmap = new Map();
|
|
2609
|
+
}
|
|
2610
|
+
add(schema, ..._meta) {
|
|
2611
|
+
const meta = _meta[0];
|
|
2612
|
+
this._map.set(schema, meta);
|
|
2613
|
+
if (meta && "object" == typeof meta && "id" in meta) {
|
|
2614
|
+
if (this._idmap.has(meta.id)) throw new Error(`ID ${meta.id} already exists in the registry`);
|
|
2615
|
+
this._idmap.set(meta.id, schema);
|
|
2616
|
+
}
|
|
2617
|
+
return this;
|
|
2618
|
+
}
|
|
2619
|
+
clear() {
|
|
2620
|
+
this._map = new WeakMap();
|
|
2621
|
+
this._idmap = new Map();
|
|
2622
|
+
return this;
|
|
2623
|
+
}
|
|
2624
|
+
remove(schema) {
|
|
2625
|
+
const meta = this._map.get(schema);
|
|
2626
|
+
if (meta && "object" == typeof meta && "id" in meta) this._idmap.delete(meta.id);
|
|
2627
|
+
this._map.delete(schema);
|
|
2628
|
+
return this;
|
|
2629
|
+
}
|
|
2630
|
+
get(schema) {
|
|
2631
|
+
const p = schema._zod.parent;
|
|
2632
|
+
if (p) {
|
|
2633
|
+
const pm = {
|
|
2634
|
+
...this.get(p) ?? {}
|
|
2635
|
+
};
|
|
2636
|
+
delete pm.id;
|
|
2637
|
+
const f = {
|
|
2638
|
+
...pm,
|
|
2639
|
+
...this._map.get(schema)
|
|
2640
|
+
};
|
|
2641
|
+
return Object.keys(f).length ? f : void 0;
|
|
2642
|
+
}
|
|
2643
|
+
return this._map.get(schema);
|
|
2644
|
+
}
|
|
2645
|
+
has(schema) {
|
|
2646
|
+
return this._map.has(schema);
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
function registries_registry() {
|
|
2650
|
+
return new $ZodRegistry();
|
|
2651
|
+
}
|
|
2652
|
+
(registries_a = globalThis).__zod_globalRegistry ?? (registries_a.__zod_globalRegistry = registries_registry());
|
|
2653
|
+
const globalRegistry = globalThis.__zod_globalRegistry;
|
|
2654
|
+
function _string(Class, params) {
|
|
2655
|
+
return new Class({
|
|
2656
|
+
type: "string",
|
|
2657
|
+
...normalizeParams(params)
|
|
2658
|
+
});
|
|
2659
|
+
}
|
|
2660
|
+
function _email(Class, params) {
|
|
2661
|
+
return new Class({
|
|
2662
|
+
type: "string",
|
|
2663
|
+
format: "email",
|
|
2664
|
+
check: "string_format",
|
|
2665
|
+
abort: false,
|
|
2666
|
+
...normalizeParams(params)
|
|
2667
|
+
});
|
|
2668
|
+
}
|
|
2669
|
+
function _guid(Class, params) {
|
|
2670
|
+
return new Class({
|
|
2671
|
+
type: "string",
|
|
2672
|
+
format: "guid",
|
|
2673
|
+
check: "string_format",
|
|
2674
|
+
abort: false,
|
|
2675
|
+
...normalizeParams(params)
|
|
2676
|
+
});
|
|
2677
|
+
}
|
|
2678
|
+
function _uuid(Class, params) {
|
|
2679
|
+
return new Class({
|
|
2680
|
+
type: "string",
|
|
2681
|
+
format: "uuid",
|
|
2682
|
+
check: "string_format",
|
|
2683
|
+
abort: false,
|
|
2684
|
+
...normalizeParams(params)
|
|
2685
|
+
});
|
|
2686
|
+
}
|
|
2687
|
+
function _uuidv4(Class, params) {
|
|
2688
|
+
return new Class({
|
|
2689
|
+
type: "string",
|
|
2690
|
+
format: "uuid",
|
|
2691
|
+
check: "string_format",
|
|
2692
|
+
abort: false,
|
|
2693
|
+
version: "v4",
|
|
2694
|
+
...normalizeParams(params)
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2697
|
+
function _uuidv6(Class, params) {
|
|
2698
|
+
return new Class({
|
|
2699
|
+
type: "string",
|
|
2700
|
+
format: "uuid",
|
|
2701
|
+
check: "string_format",
|
|
2702
|
+
abort: false,
|
|
2703
|
+
version: "v6",
|
|
2704
|
+
...normalizeParams(params)
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
function _uuidv7(Class, params) {
|
|
2708
|
+
return new Class({
|
|
2709
|
+
type: "string",
|
|
2710
|
+
format: "uuid",
|
|
2711
|
+
check: "string_format",
|
|
2712
|
+
abort: false,
|
|
2713
|
+
version: "v7",
|
|
2714
|
+
...normalizeParams(params)
|
|
2715
|
+
});
|
|
2716
|
+
}
|
|
2717
|
+
function _url(Class, params) {
|
|
2718
|
+
return new Class({
|
|
2719
|
+
type: "string",
|
|
2720
|
+
format: "url",
|
|
2721
|
+
check: "string_format",
|
|
2722
|
+
abort: false,
|
|
2723
|
+
...normalizeParams(params)
|
|
2724
|
+
});
|
|
2725
|
+
}
|
|
2726
|
+
function api_emoji(Class, params) {
|
|
2727
|
+
return new Class({
|
|
2728
|
+
type: "string",
|
|
2729
|
+
format: "emoji",
|
|
2730
|
+
check: "string_format",
|
|
2731
|
+
abort: false,
|
|
2732
|
+
...normalizeParams(params)
|
|
2733
|
+
});
|
|
2734
|
+
}
|
|
2735
|
+
function _nanoid(Class, params) {
|
|
2736
|
+
return new Class({
|
|
2737
|
+
type: "string",
|
|
2738
|
+
format: "nanoid",
|
|
2739
|
+
check: "string_format",
|
|
2740
|
+
abort: false,
|
|
2741
|
+
...normalizeParams(params)
|
|
2742
|
+
});
|
|
2743
|
+
}
|
|
2744
|
+
function _cuid(Class, params) {
|
|
2745
|
+
return new Class({
|
|
2746
|
+
type: "string",
|
|
2747
|
+
format: "cuid",
|
|
2748
|
+
check: "string_format",
|
|
2749
|
+
abort: false,
|
|
2750
|
+
...normalizeParams(params)
|
|
2751
|
+
});
|
|
2752
|
+
}
|
|
2753
|
+
function _cuid2(Class, params) {
|
|
2754
|
+
return new Class({
|
|
2755
|
+
type: "string",
|
|
2756
|
+
format: "cuid2",
|
|
2757
|
+
check: "string_format",
|
|
2758
|
+
abort: false,
|
|
2759
|
+
...normalizeParams(params)
|
|
2760
|
+
});
|
|
2761
|
+
}
|
|
2762
|
+
function _ulid(Class, params) {
|
|
2763
|
+
return new Class({
|
|
2764
|
+
type: "string",
|
|
2765
|
+
format: "ulid",
|
|
2766
|
+
check: "string_format",
|
|
2767
|
+
abort: false,
|
|
2768
|
+
...normalizeParams(params)
|
|
2769
|
+
});
|
|
2770
|
+
}
|
|
2771
|
+
function _xid(Class, params) {
|
|
2772
|
+
return new Class({
|
|
2773
|
+
type: "string",
|
|
2774
|
+
format: "xid",
|
|
2775
|
+
check: "string_format",
|
|
2776
|
+
abort: false,
|
|
2777
|
+
...normalizeParams(params)
|
|
2778
|
+
});
|
|
2779
|
+
}
|
|
2780
|
+
function _ksuid(Class, params) {
|
|
2781
|
+
return new Class({
|
|
2782
|
+
type: "string",
|
|
2783
|
+
format: "ksuid",
|
|
2784
|
+
check: "string_format",
|
|
2785
|
+
abort: false,
|
|
2786
|
+
...normalizeParams(params)
|
|
2787
|
+
});
|
|
2788
|
+
}
|
|
2789
|
+
function _ipv4(Class, params) {
|
|
2790
|
+
return new Class({
|
|
2791
|
+
type: "string",
|
|
2792
|
+
format: "ipv4",
|
|
2793
|
+
check: "string_format",
|
|
2794
|
+
abort: false,
|
|
2795
|
+
...normalizeParams(params)
|
|
2796
|
+
});
|
|
2797
|
+
}
|
|
2798
|
+
function _ipv6(Class, params) {
|
|
2799
|
+
return new Class({
|
|
2800
|
+
type: "string",
|
|
2801
|
+
format: "ipv6",
|
|
2802
|
+
check: "string_format",
|
|
2803
|
+
abort: false,
|
|
2804
|
+
...normalizeParams(params)
|
|
2805
|
+
});
|
|
2806
|
+
}
|
|
2807
|
+
function _cidrv4(Class, params) {
|
|
2808
|
+
return new Class({
|
|
2809
|
+
type: "string",
|
|
2810
|
+
format: "cidrv4",
|
|
2811
|
+
check: "string_format",
|
|
2812
|
+
abort: false,
|
|
2813
|
+
...normalizeParams(params)
|
|
2814
|
+
});
|
|
2815
|
+
}
|
|
2816
|
+
function _cidrv6(Class, params) {
|
|
2817
|
+
return new Class({
|
|
2818
|
+
type: "string",
|
|
2819
|
+
format: "cidrv6",
|
|
2820
|
+
check: "string_format",
|
|
2821
|
+
abort: false,
|
|
2822
|
+
...normalizeParams(params)
|
|
2823
|
+
});
|
|
2824
|
+
}
|
|
2825
|
+
function _base64(Class, params) {
|
|
2826
|
+
return new Class({
|
|
2827
|
+
type: "string",
|
|
2828
|
+
format: "base64",
|
|
2829
|
+
check: "string_format",
|
|
2830
|
+
abort: false,
|
|
2831
|
+
...normalizeParams(params)
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
function _base64url(Class, params) {
|
|
2835
|
+
return new Class({
|
|
2836
|
+
type: "string",
|
|
2837
|
+
format: "base64url",
|
|
2838
|
+
check: "string_format",
|
|
2839
|
+
abort: false,
|
|
2840
|
+
...normalizeParams(params)
|
|
2841
|
+
});
|
|
2842
|
+
}
|
|
2843
|
+
function _e164(Class, params) {
|
|
2844
|
+
return new Class({
|
|
2845
|
+
type: "string",
|
|
2846
|
+
format: "e164",
|
|
2847
|
+
check: "string_format",
|
|
2848
|
+
abort: false,
|
|
2849
|
+
...normalizeParams(params)
|
|
2850
|
+
});
|
|
2851
|
+
}
|
|
2852
|
+
function _jwt(Class, params) {
|
|
2853
|
+
return new Class({
|
|
2854
|
+
type: "string",
|
|
2855
|
+
format: "jwt",
|
|
2856
|
+
check: "string_format",
|
|
2857
|
+
abort: false,
|
|
2858
|
+
...normalizeParams(params)
|
|
2859
|
+
});
|
|
2860
|
+
}
|
|
2861
|
+
function _isoDateTime(Class, params) {
|
|
2862
|
+
return new Class({
|
|
2863
|
+
type: "string",
|
|
2864
|
+
format: "datetime",
|
|
2865
|
+
check: "string_format",
|
|
2866
|
+
offset: false,
|
|
2867
|
+
local: false,
|
|
2868
|
+
precision: null,
|
|
2869
|
+
...normalizeParams(params)
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
function _isoDate(Class, params) {
|
|
2873
|
+
return new Class({
|
|
2874
|
+
type: "string",
|
|
2875
|
+
format: "date",
|
|
2876
|
+
check: "string_format",
|
|
2877
|
+
...normalizeParams(params)
|
|
2878
|
+
});
|
|
2879
|
+
}
|
|
2880
|
+
function _isoTime(Class, params) {
|
|
2881
|
+
return new Class({
|
|
2882
|
+
type: "string",
|
|
2883
|
+
format: "time",
|
|
2884
|
+
check: "string_format",
|
|
2885
|
+
precision: null,
|
|
2886
|
+
...normalizeParams(params)
|
|
2887
|
+
});
|
|
2888
|
+
}
|
|
2889
|
+
function _isoDuration(Class, params) {
|
|
2890
|
+
return new Class({
|
|
2891
|
+
type: "string",
|
|
2892
|
+
format: "duration",
|
|
2893
|
+
check: "string_format",
|
|
2894
|
+
...normalizeParams(params)
|
|
2895
|
+
});
|
|
2896
|
+
}
|
|
2897
|
+
function _number(Class, params) {
|
|
2898
|
+
return new Class({
|
|
2899
|
+
type: "number",
|
|
2900
|
+
checks: [],
|
|
2901
|
+
...normalizeParams(params)
|
|
2902
|
+
});
|
|
2903
|
+
}
|
|
2904
|
+
function _int(Class, params) {
|
|
2905
|
+
return new Class({
|
|
2906
|
+
type: "number",
|
|
2907
|
+
check: "number_format",
|
|
2908
|
+
abort: false,
|
|
2909
|
+
format: "safeint",
|
|
2910
|
+
...normalizeParams(params)
|
|
2911
|
+
});
|
|
2912
|
+
}
|
|
2913
|
+
function _unknown(Class) {
|
|
2914
|
+
return new Class({
|
|
2915
|
+
type: "unknown"
|
|
2916
|
+
});
|
|
2917
|
+
}
|
|
2918
|
+
function _never(Class, params) {
|
|
2919
|
+
return new Class({
|
|
2920
|
+
type: "never",
|
|
2921
|
+
...normalizeParams(params)
|
|
2922
|
+
});
|
|
2923
|
+
}
|
|
2924
|
+
function _lt(value, params) {
|
|
2925
|
+
return new $ZodCheckLessThan({
|
|
2926
|
+
check: "less_than",
|
|
2927
|
+
...normalizeParams(params),
|
|
2928
|
+
value,
|
|
2929
|
+
inclusive: false
|
|
2930
|
+
});
|
|
2931
|
+
}
|
|
2932
|
+
function _lte(value, params) {
|
|
2933
|
+
return new $ZodCheckLessThan({
|
|
2934
|
+
check: "less_than",
|
|
2935
|
+
...normalizeParams(params),
|
|
2936
|
+
value,
|
|
2937
|
+
inclusive: true
|
|
2938
|
+
});
|
|
2939
|
+
}
|
|
2940
|
+
function _gt(value, params) {
|
|
2941
|
+
return new $ZodCheckGreaterThan({
|
|
2942
|
+
check: "greater_than",
|
|
2943
|
+
...normalizeParams(params),
|
|
2944
|
+
value,
|
|
2945
|
+
inclusive: false
|
|
2946
|
+
});
|
|
2947
|
+
}
|
|
2948
|
+
function _gte(value, params) {
|
|
2949
|
+
return new $ZodCheckGreaterThan({
|
|
2950
|
+
check: "greater_than",
|
|
2951
|
+
...normalizeParams(params),
|
|
2952
|
+
value,
|
|
2953
|
+
inclusive: true
|
|
2954
|
+
});
|
|
2955
|
+
}
|
|
2956
|
+
function _multipleOf(value, params) {
|
|
2957
|
+
return new $ZodCheckMultipleOf({
|
|
2958
|
+
check: "multiple_of",
|
|
2959
|
+
...normalizeParams(params),
|
|
2960
|
+
value
|
|
2961
|
+
});
|
|
2962
|
+
}
|
|
2963
|
+
function _maxLength(maximum, params) {
|
|
2964
|
+
const ch = new $ZodCheckMaxLength({
|
|
2965
|
+
check: "max_length",
|
|
2966
|
+
...normalizeParams(params),
|
|
2967
|
+
maximum
|
|
2968
|
+
});
|
|
2969
|
+
return ch;
|
|
2970
|
+
}
|
|
2971
|
+
function _minLength(minimum, params) {
|
|
2972
|
+
return new $ZodCheckMinLength({
|
|
2973
|
+
check: "min_length",
|
|
2974
|
+
...normalizeParams(params),
|
|
2975
|
+
minimum
|
|
2976
|
+
});
|
|
2977
|
+
}
|
|
2978
|
+
function _length(length, params) {
|
|
2979
|
+
return new $ZodCheckLengthEquals({
|
|
2980
|
+
check: "length_equals",
|
|
2981
|
+
...normalizeParams(params),
|
|
2982
|
+
length
|
|
2983
|
+
});
|
|
2984
|
+
}
|
|
2985
|
+
function _regex(pattern, params) {
|
|
2986
|
+
return new $ZodCheckRegex({
|
|
2987
|
+
check: "string_format",
|
|
2988
|
+
format: "regex",
|
|
2989
|
+
...normalizeParams(params),
|
|
2990
|
+
pattern
|
|
2991
|
+
});
|
|
2992
|
+
}
|
|
2993
|
+
function _lowercase(params) {
|
|
2994
|
+
return new $ZodCheckLowerCase({
|
|
2995
|
+
check: "string_format",
|
|
2996
|
+
format: "lowercase",
|
|
2997
|
+
...normalizeParams(params)
|
|
2998
|
+
});
|
|
2999
|
+
}
|
|
3000
|
+
function _uppercase(params) {
|
|
3001
|
+
return new $ZodCheckUpperCase({
|
|
3002
|
+
check: "string_format",
|
|
3003
|
+
format: "uppercase",
|
|
3004
|
+
...normalizeParams(params)
|
|
3005
|
+
});
|
|
3006
|
+
}
|
|
3007
|
+
function _includes(includes, params) {
|
|
3008
|
+
return new $ZodCheckIncludes({
|
|
3009
|
+
check: "string_format",
|
|
3010
|
+
format: "includes",
|
|
3011
|
+
...normalizeParams(params),
|
|
3012
|
+
includes
|
|
3013
|
+
});
|
|
3014
|
+
}
|
|
3015
|
+
function _startsWith(prefix, params) {
|
|
3016
|
+
return new $ZodCheckStartsWith({
|
|
3017
|
+
check: "string_format",
|
|
3018
|
+
format: "starts_with",
|
|
3019
|
+
...normalizeParams(params),
|
|
3020
|
+
prefix
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
function _endsWith(suffix, params) {
|
|
3024
|
+
return new $ZodCheckEndsWith({
|
|
3025
|
+
check: "string_format",
|
|
3026
|
+
format: "ends_with",
|
|
3027
|
+
...normalizeParams(params),
|
|
3028
|
+
suffix
|
|
3029
|
+
});
|
|
3030
|
+
}
|
|
3031
|
+
function _overwrite(tx) {
|
|
3032
|
+
return new $ZodCheckOverwrite({
|
|
3033
|
+
check: "overwrite",
|
|
3034
|
+
tx
|
|
3035
|
+
});
|
|
3036
|
+
}
|
|
3037
|
+
function _normalize(form) {
|
|
3038
|
+
return _overwrite((input)=>input.normalize(form));
|
|
3039
|
+
}
|
|
3040
|
+
function _trim() {
|
|
3041
|
+
return _overwrite((input)=>input.trim());
|
|
3042
|
+
}
|
|
3043
|
+
function _toLowerCase() {
|
|
3044
|
+
return _overwrite((input)=>input.toLowerCase());
|
|
3045
|
+
}
|
|
3046
|
+
function _toUpperCase() {
|
|
3047
|
+
return _overwrite((input)=>input.toUpperCase());
|
|
3048
|
+
}
|
|
3049
|
+
function _slugify() {
|
|
3050
|
+
return _overwrite((input)=>slugify(input));
|
|
3051
|
+
}
|
|
3052
|
+
function _array(Class, element, params) {
|
|
3053
|
+
return new Class({
|
|
3054
|
+
type: "array",
|
|
3055
|
+
element,
|
|
3056
|
+
...normalizeParams(params)
|
|
3057
|
+
});
|
|
3058
|
+
}
|
|
3059
|
+
function _refine(Class, fn, _params) {
|
|
3060
|
+
const schema = new Class({
|
|
3061
|
+
type: "custom",
|
|
3062
|
+
check: "custom",
|
|
3063
|
+
fn: fn,
|
|
3064
|
+
...normalizeParams(_params)
|
|
3065
|
+
});
|
|
3066
|
+
return schema;
|
|
3067
|
+
}
|
|
3068
|
+
function _superRefine(fn) {
|
|
3069
|
+
const ch = _check((payload)=>{
|
|
3070
|
+
payload.addIssue = (issue)=>{
|
|
3071
|
+
if ("string" == typeof issue) payload.issues.push(util_issue(issue, payload.value, ch._zod.def));
|
|
3072
|
+
else {
|
|
3073
|
+
const _issue = issue;
|
|
3074
|
+
if (_issue.fatal) _issue.continue = false;
|
|
3075
|
+
_issue.code ?? (_issue.code = "custom");
|
|
3076
|
+
_issue.input ?? (_issue.input = payload.value);
|
|
3077
|
+
_issue.inst ?? (_issue.inst = ch);
|
|
3078
|
+
_issue.continue ?? (_issue.continue = !ch._zod.def.abort);
|
|
3079
|
+
payload.issues.push(util_issue(_issue));
|
|
3080
|
+
}
|
|
3081
|
+
};
|
|
3082
|
+
return fn(payload.value, payload);
|
|
3083
|
+
});
|
|
3084
|
+
return ch;
|
|
3085
|
+
}
|
|
3086
|
+
function _check(fn, params) {
|
|
3087
|
+
const ch = new $ZodCheck({
|
|
3088
|
+
check: "custom",
|
|
3089
|
+
...normalizeParams(params)
|
|
3090
|
+
});
|
|
3091
|
+
ch._zod.check = fn;
|
|
3092
|
+
return ch;
|
|
3093
|
+
}
|
|
3094
|
+
function to_json_schema_initializeContext(params) {
|
|
3095
|
+
let target = params?.target ?? "draft-2020-12";
|
|
3096
|
+
if ("draft-4" === target) target = "draft-04";
|
|
3097
|
+
if ("draft-7" === target) target = "draft-07";
|
|
3098
|
+
return {
|
|
3099
|
+
processors: params.processors ?? {},
|
|
3100
|
+
metadataRegistry: params?.metadata ?? globalRegistry,
|
|
3101
|
+
target,
|
|
3102
|
+
unrepresentable: params?.unrepresentable ?? "throw",
|
|
3103
|
+
override: params?.override ?? (()=>{}),
|
|
3104
|
+
io: params?.io ?? "output",
|
|
3105
|
+
counter: 0,
|
|
3106
|
+
seen: new Map(),
|
|
3107
|
+
cycles: params?.cycles ?? "ref",
|
|
3108
|
+
reused: params?.reused ?? "inline",
|
|
3109
|
+
external: params?.external ?? void 0
|
|
3110
|
+
};
|
|
3111
|
+
}
|
|
3112
|
+
function to_json_schema_process(schema, ctx, _params = {
|
|
3113
|
+
path: [],
|
|
3114
|
+
schemaPath: []
|
|
3115
|
+
}) {
|
|
3116
|
+
var _a;
|
|
3117
|
+
const def = schema._zod.def;
|
|
3118
|
+
const seen = ctx.seen.get(schema);
|
|
3119
|
+
if (seen) {
|
|
3120
|
+
seen.count++;
|
|
3121
|
+
const isCycle = _params.schemaPath.includes(schema);
|
|
3122
|
+
if (isCycle) seen.cycle = _params.path;
|
|
3123
|
+
return seen.schema;
|
|
3124
|
+
}
|
|
3125
|
+
const result = {
|
|
3126
|
+
schema: {},
|
|
3127
|
+
count: 1,
|
|
3128
|
+
cycle: void 0,
|
|
3129
|
+
path: _params.path
|
|
3130
|
+
};
|
|
3131
|
+
ctx.seen.set(schema, result);
|
|
3132
|
+
const overrideSchema = schema._zod.toJSONSchema?.();
|
|
3133
|
+
if (overrideSchema) result.schema = overrideSchema;
|
|
3134
|
+
else {
|
|
3135
|
+
const params = {
|
|
3136
|
+
..._params,
|
|
3137
|
+
schemaPath: [
|
|
3138
|
+
..._params.schemaPath,
|
|
3139
|
+
schema
|
|
3140
|
+
],
|
|
3141
|
+
path: _params.path
|
|
3142
|
+
};
|
|
3143
|
+
const parent = schema._zod.parent;
|
|
3144
|
+
if (parent) {
|
|
3145
|
+
result.ref = parent;
|
|
3146
|
+
to_json_schema_process(parent, ctx, params);
|
|
3147
|
+
ctx.seen.get(parent).isParent = true;
|
|
3148
|
+
} else if (schema._zod.processJSONSchema) schema._zod.processJSONSchema(ctx, result.schema, params);
|
|
3149
|
+
else {
|
|
3150
|
+
const _json = result.schema;
|
|
3151
|
+
const processor = ctx.processors[def.type];
|
|
3152
|
+
if (!processor) throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
|
|
3153
|
+
processor(schema, ctx, _json, params);
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
const meta = ctx.metadataRegistry.get(schema);
|
|
3157
|
+
if (meta) Object.assign(result.schema, meta);
|
|
3158
|
+
if ("input" === ctx.io && isTransforming(schema)) {
|
|
3159
|
+
delete result.schema.examples;
|
|
3160
|
+
delete result.schema.default;
|
|
3161
|
+
}
|
|
3162
|
+
if ("input" === ctx.io && result.schema._prefault) (_a = result.schema).default ?? (_a.default = result.schema._prefault);
|
|
3163
|
+
delete result.schema._prefault;
|
|
3164
|
+
const _result = ctx.seen.get(schema);
|
|
3165
|
+
return _result.schema;
|
|
3166
|
+
}
|
|
3167
|
+
function to_json_schema_extractDefs(ctx, schema) {
|
|
3168
|
+
const root = ctx.seen.get(schema);
|
|
3169
|
+
if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
3170
|
+
const makeURI = (entry)=>{
|
|
3171
|
+
const defsSegment = "draft-2020-12" === ctx.target ? "$defs" : "definitions";
|
|
3172
|
+
if (ctx.external) {
|
|
3173
|
+
const externalId = ctx.external.registry.get(entry[0])?.id;
|
|
3174
|
+
const uriGenerator = ctx.external.uri ?? ((id)=>id);
|
|
3175
|
+
if (externalId) return {
|
|
3176
|
+
ref: uriGenerator(externalId)
|
|
3177
|
+
};
|
|
3178
|
+
const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
|
|
3179
|
+
entry[1].defId = id;
|
|
3180
|
+
return {
|
|
3181
|
+
defId: id,
|
|
3182
|
+
ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}`
|
|
3183
|
+
};
|
|
3184
|
+
}
|
|
3185
|
+
if (entry[1] === root) return {
|
|
3186
|
+
ref: "#"
|
|
3187
|
+
};
|
|
3188
|
+
const uriPrefix = "#";
|
|
3189
|
+
const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
|
|
3190
|
+
const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
|
|
3191
|
+
return {
|
|
3192
|
+
defId,
|
|
3193
|
+
ref: defUriPrefix + defId
|
|
3194
|
+
};
|
|
3195
|
+
};
|
|
3196
|
+
const extractToDef = (entry)=>{
|
|
3197
|
+
if (entry[1].schema.$ref) return;
|
|
3198
|
+
const seen = entry[1];
|
|
3199
|
+
const { ref, defId } = makeURI(entry);
|
|
3200
|
+
seen.def = {
|
|
3201
|
+
...seen.schema
|
|
3202
|
+
};
|
|
3203
|
+
if (defId) seen.defId = defId;
|
|
3204
|
+
const schema = seen.schema;
|
|
3205
|
+
for(const key in schema)delete schema[key];
|
|
3206
|
+
schema.$ref = ref;
|
|
3207
|
+
};
|
|
3208
|
+
if ("throw" === ctx.cycles) for (const entry of ctx.seen.entries()){
|
|
3209
|
+
const seen = entry[1];
|
|
3210
|
+
if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>\n\nSet the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
|
|
3211
|
+
}
|
|
3212
|
+
for (const entry of ctx.seen.entries()){
|
|
3213
|
+
const seen = entry[1];
|
|
3214
|
+
if (schema === entry[0]) {
|
|
3215
|
+
extractToDef(entry);
|
|
3216
|
+
continue;
|
|
3217
|
+
}
|
|
3218
|
+
if (ctx.external) {
|
|
3219
|
+
const ext = ctx.external.registry.get(entry[0])?.id;
|
|
3220
|
+
if (schema !== entry[0] && ext) {
|
|
3221
|
+
extractToDef(entry);
|
|
3222
|
+
continue;
|
|
3223
|
+
}
|
|
3224
|
+
}
|
|
3225
|
+
const id = ctx.metadataRegistry.get(entry[0])?.id;
|
|
3226
|
+
if (id) {
|
|
3227
|
+
extractToDef(entry);
|
|
3228
|
+
continue;
|
|
3229
|
+
}
|
|
3230
|
+
if (seen.cycle) {
|
|
3231
|
+
extractToDef(entry);
|
|
3232
|
+
continue;
|
|
3233
|
+
}
|
|
3234
|
+
if (seen.count > 1) {
|
|
3235
|
+
if ("ref" === ctx.reused) {
|
|
3236
|
+
extractToDef(entry);
|
|
3237
|
+
continue;
|
|
3238
|
+
}
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3241
|
+
}
|
|
3242
|
+
function to_json_schema_finalize(ctx, schema) {
|
|
3243
|
+
const root = ctx.seen.get(schema);
|
|
3244
|
+
if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
3245
|
+
const flattenRef = (zodSchema)=>{
|
|
3246
|
+
const seen = ctx.seen.get(zodSchema);
|
|
3247
|
+
const schema = seen.def ?? seen.schema;
|
|
3248
|
+
const _cached = {
|
|
3249
|
+
...schema
|
|
3250
|
+
};
|
|
3251
|
+
if (null === seen.ref) return;
|
|
3252
|
+
const ref = seen.ref;
|
|
3253
|
+
seen.ref = null;
|
|
3254
|
+
if (ref) {
|
|
3255
|
+
flattenRef(ref);
|
|
3256
|
+
const refSchema = ctx.seen.get(ref).schema;
|
|
3257
|
+
if (refSchema.$ref && ("draft-07" === ctx.target || "draft-04" === ctx.target || "openapi-3.0" === ctx.target)) {
|
|
3258
|
+
schema.allOf = schema.allOf ?? [];
|
|
3259
|
+
schema.allOf.push(refSchema);
|
|
3260
|
+
} else {
|
|
3261
|
+
Object.assign(schema, refSchema);
|
|
3262
|
+
Object.assign(schema, _cached);
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
3265
|
+
if (!seen.isParent) ctx.override({
|
|
3266
|
+
zodSchema: zodSchema,
|
|
3267
|
+
jsonSchema: schema,
|
|
3268
|
+
path: seen.path ?? []
|
|
3269
|
+
});
|
|
3270
|
+
};
|
|
3271
|
+
for (const entry of [
|
|
3272
|
+
...ctx.seen.entries()
|
|
3273
|
+
].reverse())flattenRef(entry[0]);
|
|
3274
|
+
const result = {};
|
|
3275
|
+
if ("draft-2020-12" === ctx.target) result.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
3276
|
+
else if ("draft-07" === ctx.target) result.$schema = "http://json-schema.org/draft-07/schema#";
|
|
3277
|
+
else if ("draft-04" === ctx.target) result.$schema = "http://json-schema.org/draft-04/schema#";
|
|
3278
|
+
else ctx.target;
|
|
3279
|
+
if (ctx.external?.uri) {
|
|
3280
|
+
const id = ctx.external.registry.get(schema)?.id;
|
|
3281
|
+
if (!id) throw new Error("Schema is missing an `id` property");
|
|
3282
|
+
result.$id = ctx.external.uri(id);
|
|
3283
|
+
}
|
|
3284
|
+
Object.assign(result, root.def ?? root.schema);
|
|
3285
|
+
const defs = ctx.external?.defs ?? {};
|
|
3286
|
+
for (const entry of ctx.seen.entries()){
|
|
3287
|
+
const seen = entry[1];
|
|
3288
|
+
if (seen.def && seen.defId) defs[seen.defId] = seen.def;
|
|
3289
|
+
}
|
|
3290
|
+
if (ctx.external) ;
|
|
3291
|
+
else if (Object.keys(defs).length > 0) if ("draft-2020-12" === ctx.target) result.$defs = defs;
|
|
3292
|
+
else result.definitions = defs;
|
|
3293
|
+
try {
|
|
3294
|
+
const finalized = JSON.parse(JSON.stringify(result));
|
|
3295
|
+
Object.defineProperty(finalized, "~standard", {
|
|
3296
|
+
value: {
|
|
3297
|
+
...schema["~standard"],
|
|
3298
|
+
jsonSchema: {
|
|
3299
|
+
input: createStandardJSONSchemaMethod(schema, "input"),
|
|
3300
|
+
output: createStandardJSONSchemaMethod(schema, "output")
|
|
3301
|
+
}
|
|
3302
|
+
},
|
|
3303
|
+
enumerable: false,
|
|
3304
|
+
writable: false
|
|
3305
|
+
});
|
|
3306
|
+
return finalized;
|
|
3307
|
+
} catch (_err) {
|
|
3308
|
+
throw new Error("Error converting schema to JSON.");
|
|
3309
|
+
}
|
|
3310
|
+
}
|
|
3311
|
+
function isTransforming(_schema, _ctx) {
|
|
3312
|
+
const ctx = _ctx ?? {
|
|
3313
|
+
seen: new Set()
|
|
3314
|
+
};
|
|
3315
|
+
if (ctx.seen.has(_schema)) return false;
|
|
3316
|
+
ctx.seen.add(_schema);
|
|
3317
|
+
const def = _schema._zod.def;
|
|
3318
|
+
if ("transform" === def.type) return true;
|
|
3319
|
+
if ("array" === def.type) return isTransforming(def.element, ctx);
|
|
3320
|
+
if ("set" === def.type) return isTransforming(def.valueType, ctx);
|
|
3321
|
+
if ("lazy" === def.type) return isTransforming(def.getter(), ctx);
|
|
3322
|
+
if ("promise" === def.type || "optional" === def.type || "nonoptional" === def.type || "nullable" === def.type || "readonly" === def.type || "default" === def.type || "prefault" === def.type) return isTransforming(def.innerType, ctx);
|
|
3323
|
+
if ("intersection" === def.type) return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
|
|
3324
|
+
if ("record" === def.type || "map" === def.type) return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
|
|
3325
|
+
if ("pipe" === def.type) return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
|
|
3326
|
+
if ("object" === def.type) {
|
|
3327
|
+
for(const key in def.shape)if (isTransforming(def.shape[key], ctx)) return true;
|
|
3328
|
+
return false;
|
|
3329
|
+
}
|
|
3330
|
+
if ("union" === def.type) {
|
|
3331
|
+
for (const option of def.options)if (isTransforming(option, ctx)) return true;
|
|
3332
|
+
return false;
|
|
3333
|
+
}
|
|
3334
|
+
if ("tuple" === def.type) {
|
|
3335
|
+
for (const item of def.items)if (isTransforming(item, ctx)) return true;
|
|
3336
|
+
if (def.rest && isTransforming(def.rest, ctx)) return true;
|
|
3337
|
+
}
|
|
3338
|
+
return false;
|
|
3339
|
+
}
|
|
3340
|
+
const createToJSONSchemaMethod = (schema, processors = {})=>(params)=>{
|
|
3341
|
+
const ctx = to_json_schema_initializeContext({
|
|
3342
|
+
...params,
|
|
3343
|
+
processors
|
|
3344
|
+
});
|
|
3345
|
+
to_json_schema_process(schema, ctx);
|
|
3346
|
+
to_json_schema_extractDefs(ctx, schema);
|
|
3347
|
+
return to_json_schema_finalize(ctx, schema);
|
|
3348
|
+
};
|
|
3349
|
+
const createStandardJSONSchemaMethod = (schema, io)=>(params)=>{
|
|
3350
|
+
const { libraryOptions, target } = params ?? {};
|
|
3351
|
+
const ctx = to_json_schema_initializeContext({
|
|
3352
|
+
...libraryOptions ?? {},
|
|
3353
|
+
target,
|
|
3354
|
+
io,
|
|
3355
|
+
processors: {}
|
|
3356
|
+
});
|
|
3357
|
+
to_json_schema_process(schema, ctx);
|
|
3358
|
+
to_json_schema_extractDefs(ctx, schema);
|
|
3359
|
+
return to_json_schema_finalize(ctx, schema);
|
|
3360
|
+
};
|
|
3361
|
+
const formatMap = {
|
|
3362
|
+
guid: "uuid",
|
|
3363
|
+
url: "uri",
|
|
3364
|
+
datetime: "date-time",
|
|
3365
|
+
json_string: "json-string",
|
|
3366
|
+
regex: ""
|
|
3367
|
+
};
|
|
3368
|
+
const stringProcessor = (schema, ctx, _json, _params)=>{
|
|
3369
|
+
const json = _json;
|
|
3370
|
+
json.type = "string";
|
|
3371
|
+
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod.bag;
|
|
3372
|
+
if ("number" == typeof minimum) json.minLength = minimum;
|
|
3373
|
+
if ("number" == typeof maximum) json.maxLength = maximum;
|
|
3374
|
+
if (format) {
|
|
3375
|
+
json.format = formatMap[format] ?? format;
|
|
3376
|
+
if ("" === json.format) delete json.format;
|
|
3377
|
+
}
|
|
3378
|
+
if (contentEncoding) json.contentEncoding = contentEncoding;
|
|
3379
|
+
if (patterns && patterns.size > 0) {
|
|
3380
|
+
const regexes = [
|
|
3381
|
+
...patterns
|
|
3382
|
+
];
|
|
3383
|
+
if (1 === regexes.length) json.pattern = regexes[0].source;
|
|
3384
|
+
else if (regexes.length > 1) json.allOf = [
|
|
3385
|
+
...regexes.map((regex)=>({
|
|
3386
|
+
..."draft-07" === ctx.target || "draft-04" === ctx.target || "openapi-3.0" === ctx.target ? {
|
|
3387
|
+
type: "string"
|
|
3388
|
+
} : {},
|
|
3389
|
+
pattern: regex.source
|
|
3390
|
+
}))
|
|
3391
|
+
];
|
|
3392
|
+
}
|
|
3393
|
+
};
|
|
3394
|
+
const numberProcessor = (schema, ctx, _json, _params)=>{
|
|
3395
|
+
const json = _json;
|
|
3396
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
|
|
3397
|
+
if ("string" == typeof format && format.includes("int")) json.type = "integer";
|
|
3398
|
+
else json.type = "number";
|
|
3399
|
+
if ("number" == typeof exclusiveMinimum) if ("draft-04" === ctx.target || "openapi-3.0" === ctx.target) {
|
|
3400
|
+
json.minimum = exclusiveMinimum;
|
|
3401
|
+
json.exclusiveMinimum = true;
|
|
3402
|
+
} else json.exclusiveMinimum = exclusiveMinimum;
|
|
3403
|
+
if ("number" == typeof minimum) {
|
|
3404
|
+
json.minimum = minimum;
|
|
3405
|
+
if ("number" == typeof exclusiveMinimum && "draft-04" !== ctx.target) if (exclusiveMinimum >= minimum) delete json.minimum;
|
|
3406
|
+
else delete json.exclusiveMinimum;
|
|
3407
|
+
}
|
|
3408
|
+
if ("number" == typeof exclusiveMaximum) if ("draft-04" === ctx.target || "openapi-3.0" === ctx.target) {
|
|
3409
|
+
json.maximum = exclusiveMaximum;
|
|
3410
|
+
json.exclusiveMaximum = true;
|
|
3411
|
+
} else json.exclusiveMaximum = exclusiveMaximum;
|
|
3412
|
+
if ("number" == typeof maximum) {
|
|
3413
|
+
json.maximum = maximum;
|
|
3414
|
+
if ("number" == typeof exclusiveMaximum && "draft-04" !== ctx.target) if (exclusiveMaximum <= maximum) delete json.maximum;
|
|
3415
|
+
else delete json.exclusiveMaximum;
|
|
3416
|
+
}
|
|
3417
|
+
if ("number" == typeof multipleOf) json.multipleOf = multipleOf;
|
|
3418
|
+
};
|
|
3419
|
+
const neverProcessor = (_schema, _ctx, json, _params)=>{
|
|
3420
|
+
json.not = {};
|
|
3421
|
+
};
|
|
3422
|
+
const unknownProcessor = (_schema, _ctx, _json, _params)=>{};
|
|
3423
|
+
const enumProcessor = (schema, _ctx, json, _params)=>{
|
|
3424
|
+
const def = schema._zod.def;
|
|
3425
|
+
const values = getEnumValues(def.entries);
|
|
3426
|
+
if (values.every((v)=>"number" == typeof v)) json.type = "number";
|
|
3427
|
+
if (values.every((v)=>"string" == typeof v)) json.type = "string";
|
|
3428
|
+
json.enum = values;
|
|
3429
|
+
};
|
|
3430
|
+
const customProcessor = (_schema, ctx, _json, _params)=>{
|
|
3431
|
+
if ("throw" === ctx.unrepresentable) throw new Error("Custom types cannot be represented in JSON Schema");
|
|
3432
|
+
};
|
|
3433
|
+
const transformProcessor = (_schema, ctx, _json, _params)=>{
|
|
3434
|
+
if ("throw" === ctx.unrepresentable) throw new Error("Transforms cannot be represented in JSON Schema");
|
|
3435
|
+
};
|
|
3436
|
+
const arrayProcessor = (schema, ctx, _json, params)=>{
|
|
3437
|
+
const json = _json;
|
|
3438
|
+
const def = schema._zod.def;
|
|
3439
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
3440
|
+
if ("number" == typeof minimum) json.minItems = minimum;
|
|
3441
|
+
if ("number" == typeof maximum) json.maxItems = maximum;
|
|
3442
|
+
json.type = "array";
|
|
3443
|
+
json.items = to_json_schema_process(def.element, ctx, {
|
|
3444
|
+
...params,
|
|
3445
|
+
path: [
|
|
3446
|
+
...params.path,
|
|
3447
|
+
"items"
|
|
3448
|
+
]
|
|
3449
|
+
});
|
|
3450
|
+
};
|
|
3451
|
+
const objectProcessor = (schema, ctx, _json, params)=>{
|
|
3452
|
+
const json = _json;
|
|
3453
|
+
const def = schema._zod.def;
|
|
3454
|
+
json.type = "object";
|
|
3455
|
+
json.properties = {};
|
|
3456
|
+
const shape = def.shape;
|
|
3457
|
+
for(const key in shape)json.properties[key] = to_json_schema_process(shape[key], ctx, {
|
|
3458
|
+
...params,
|
|
3459
|
+
path: [
|
|
3460
|
+
...params.path,
|
|
3461
|
+
"properties",
|
|
3462
|
+
key
|
|
3463
|
+
]
|
|
3464
|
+
});
|
|
3465
|
+
const allKeys = new Set(Object.keys(shape));
|
|
3466
|
+
const requiredKeys = new Set([
|
|
3467
|
+
...allKeys
|
|
3468
|
+
].filter((key)=>{
|
|
3469
|
+
const v = def.shape[key]._zod;
|
|
3470
|
+
if ("input" === ctx.io) return void 0 === v.optin;
|
|
3471
|
+
return void 0 === v.optout;
|
|
3472
|
+
}));
|
|
3473
|
+
if (requiredKeys.size > 0) json.required = Array.from(requiredKeys);
|
|
3474
|
+
if (def.catchall?._zod.def.type === "never") json.additionalProperties = false;
|
|
3475
|
+
else if (def.catchall) {
|
|
3476
|
+
if (def.catchall) json.additionalProperties = to_json_schema_process(def.catchall, ctx, {
|
|
3477
|
+
...params,
|
|
3478
|
+
path: [
|
|
3479
|
+
...params.path,
|
|
3480
|
+
"additionalProperties"
|
|
3481
|
+
]
|
|
3482
|
+
});
|
|
3483
|
+
} else if ("output" === ctx.io) json.additionalProperties = false;
|
|
3484
|
+
};
|
|
3485
|
+
const unionProcessor = (schema, ctx, json, params)=>{
|
|
3486
|
+
const def = schema._zod.def;
|
|
3487
|
+
const isExclusive = false === def.inclusive;
|
|
3488
|
+
const options = def.options.map((x, i)=>to_json_schema_process(x, ctx, {
|
|
3489
|
+
...params,
|
|
3490
|
+
path: [
|
|
3491
|
+
...params.path,
|
|
3492
|
+
isExclusive ? "oneOf" : "anyOf",
|
|
3493
|
+
i
|
|
3494
|
+
]
|
|
3495
|
+
}));
|
|
3496
|
+
if (isExclusive) json.oneOf = options;
|
|
3497
|
+
else json.anyOf = options;
|
|
3498
|
+
};
|
|
3499
|
+
const intersectionProcessor = (schema, ctx, json, params)=>{
|
|
3500
|
+
const def = schema._zod.def;
|
|
3501
|
+
const a = to_json_schema_process(def.left, ctx, {
|
|
3502
|
+
...params,
|
|
3503
|
+
path: [
|
|
3504
|
+
...params.path,
|
|
3505
|
+
"allOf",
|
|
3506
|
+
0
|
|
3507
|
+
]
|
|
3508
|
+
});
|
|
3509
|
+
const b = to_json_schema_process(def.right, ctx, {
|
|
3510
|
+
...params,
|
|
3511
|
+
path: [
|
|
3512
|
+
...params.path,
|
|
3513
|
+
"allOf",
|
|
3514
|
+
1
|
|
3515
|
+
]
|
|
3516
|
+
});
|
|
3517
|
+
const isSimpleIntersection = (val)=>"allOf" in val && 1 === Object.keys(val).length;
|
|
3518
|
+
const allOf = [
|
|
3519
|
+
...isSimpleIntersection(a) ? a.allOf : [
|
|
3520
|
+
a
|
|
3521
|
+
],
|
|
3522
|
+
...isSimpleIntersection(b) ? b.allOf : [
|
|
3523
|
+
b
|
|
3524
|
+
]
|
|
3525
|
+
];
|
|
3526
|
+
json.allOf = allOf;
|
|
3527
|
+
};
|
|
3528
|
+
const tupleProcessor = (schema, ctx, _json, params)=>{
|
|
3529
|
+
const json = _json;
|
|
3530
|
+
const def = schema._zod.def;
|
|
3531
|
+
json.type = "array";
|
|
3532
|
+
const prefixPath = "draft-2020-12" === ctx.target ? "prefixItems" : "items";
|
|
3533
|
+
const restPath = "draft-2020-12" === ctx.target ? "items" : "openapi-3.0" === ctx.target ? "items" : "additionalItems";
|
|
3534
|
+
const prefixItems = def.items.map((x, i)=>to_json_schema_process(x, ctx, {
|
|
3535
|
+
...params,
|
|
3536
|
+
path: [
|
|
3537
|
+
...params.path,
|
|
3538
|
+
prefixPath,
|
|
3539
|
+
i
|
|
3540
|
+
]
|
|
3541
|
+
}));
|
|
3542
|
+
const rest = def.rest ? to_json_schema_process(def.rest, ctx, {
|
|
3543
|
+
...params,
|
|
3544
|
+
path: [
|
|
3545
|
+
...params.path,
|
|
3546
|
+
restPath,
|
|
3547
|
+
..."openapi-3.0" === ctx.target ? [
|
|
3548
|
+
def.items.length
|
|
3549
|
+
] : []
|
|
3550
|
+
]
|
|
3551
|
+
}) : null;
|
|
3552
|
+
if ("draft-2020-12" === ctx.target) {
|
|
3553
|
+
json.prefixItems = prefixItems;
|
|
3554
|
+
if (rest) json.items = rest;
|
|
3555
|
+
} else if ("openapi-3.0" === ctx.target) {
|
|
3556
|
+
json.items = {
|
|
3557
|
+
anyOf: prefixItems
|
|
3558
|
+
};
|
|
3559
|
+
if (rest) json.items.anyOf.push(rest);
|
|
3560
|
+
json.minItems = prefixItems.length;
|
|
3561
|
+
if (!rest) json.maxItems = prefixItems.length;
|
|
3562
|
+
} else {
|
|
3563
|
+
json.items = prefixItems;
|
|
3564
|
+
if (rest) json.additionalItems = rest;
|
|
3565
|
+
}
|
|
3566
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
3567
|
+
if ("number" == typeof minimum) json.minItems = minimum;
|
|
3568
|
+
if ("number" == typeof maximum) json.maxItems = maximum;
|
|
3569
|
+
};
|
|
3570
|
+
const nullableProcessor = (schema, ctx, json, params)=>{
|
|
3571
|
+
const def = schema._zod.def;
|
|
3572
|
+
const inner = to_json_schema_process(def.innerType, ctx, params);
|
|
3573
|
+
const seen = ctx.seen.get(schema);
|
|
3574
|
+
if ("openapi-3.0" === ctx.target) {
|
|
3575
|
+
seen.ref = def.innerType;
|
|
3576
|
+
json.nullable = true;
|
|
3577
|
+
} else json.anyOf = [
|
|
3578
|
+
inner,
|
|
3579
|
+
{
|
|
3580
|
+
type: "null"
|
|
3581
|
+
}
|
|
3582
|
+
];
|
|
3583
|
+
};
|
|
3584
|
+
const nonoptionalProcessor = (schema, ctx, _json, params)=>{
|
|
3585
|
+
const def = schema._zod.def;
|
|
3586
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3587
|
+
const seen = ctx.seen.get(schema);
|
|
3588
|
+
seen.ref = def.innerType;
|
|
3589
|
+
};
|
|
3590
|
+
const defaultProcessor = (schema, ctx, json, params)=>{
|
|
3591
|
+
const def = schema._zod.def;
|
|
3592
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3593
|
+
const seen = ctx.seen.get(schema);
|
|
3594
|
+
seen.ref = def.innerType;
|
|
3595
|
+
json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
3596
|
+
};
|
|
3597
|
+
const prefaultProcessor = (schema, ctx, json, params)=>{
|
|
3598
|
+
const def = schema._zod.def;
|
|
3599
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3600
|
+
const seen = ctx.seen.get(schema);
|
|
3601
|
+
seen.ref = def.innerType;
|
|
3602
|
+
if ("input" === ctx.io) json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
3603
|
+
};
|
|
3604
|
+
const catchProcessor = (schema, ctx, json, params)=>{
|
|
3605
|
+
const def = schema._zod.def;
|
|
3606
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3607
|
+
const seen = ctx.seen.get(schema);
|
|
3608
|
+
seen.ref = def.innerType;
|
|
3609
|
+
let catchValue;
|
|
3610
|
+
try {
|
|
3611
|
+
catchValue = def.catchValue(void 0);
|
|
3612
|
+
} catch {
|
|
3613
|
+
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
3614
|
+
}
|
|
3615
|
+
json.default = catchValue;
|
|
3616
|
+
};
|
|
3617
|
+
const pipeProcessor = (schema, ctx, _json, params)=>{
|
|
3618
|
+
const def = schema._zod.def;
|
|
3619
|
+
const innerType = "input" === ctx.io ? "transform" === def.in._zod.def.type ? def.out : def.in : def.out;
|
|
3620
|
+
to_json_schema_process(innerType, ctx, params);
|
|
3621
|
+
const seen = ctx.seen.get(schema);
|
|
3622
|
+
seen.ref = innerType;
|
|
3623
|
+
};
|
|
3624
|
+
const readonlyProcessor = (schema, ctx, json, params)=>{
|
|
3625
|
+
const def = schema._zod.def;
|
|
3626
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3627
|
+
const seen = ctx.seen.get(schema);
|
|
3628
|
+
seen.ref = def.innerType;
|
|
3629
|
+
json.readOnly = true;
|
|
3630
|
+
};
|
|
3631
|
+
const optionalProcessor = (schema, ctx, _json, params)=>{
|
|
3632
|
+
const def = schema._zod.def;
|
|
3633
|
+
to_json_schema_process(def.innerType, ctx, params);
|
|
3634
|
+
const seen = ctx.seen.get(schema);
|
|
3635
|
+
seen.ref = def.innerType;
|
|
3636
|
+
};
|
|
3637
|
+
const ZodISODateTime = /*@__PURE__*/ $constructor("ZodISODateTime", (inst, def)=>{
|
|
3638
|
+
$ZodISODateTime.init(inst, def);
|
|
3639
|
+
ZodStringFormat.init(inst, def);
|
|
3640
|
+
});
|
|
3641
|
+
function iso_datetime(params) {
|
|
3642
|
+
return _isoDateTime(ZodISODateTime, params);
|
|
3643
|
+
}
|
|
3644
|
+
const ZodISODate = /*@__PURE__*/ $constructor("ZodISODate", (inst, def)=>{
|
|
3645
|
+
$ZodISODate.init(inst, def);
|
|
3646
|
+
ZodStringFormat.init(inst, def);
|
|
3647
|
+
});
|
|
3648
|
+
function iso_date(params) {
|
|
3649
|
+
return _isoDate(ZodISODate, params);
|
|
3650
|
+
}
|
|
3651
|
+
const ZodISOTime = /*@__PURE__*/ $constructor("ZodISOTime", (inst, def)=>{
|
|
3652
|
+
$ZodISOTime.init(inst, def);
|
|
3653
|
+
ZodStringFormat.init(inst, def);
|
|
3654
|
+
});
|
|
3655
|
+
function iso_time(params) {
|
|
3656
|
+
return _isoTime(ZodISOTime, params);
|
|
3657
|
+
}
|
|
3658
|
+
const ZodISODuration = /*@__PURE__*/ $constructor("ZodISODuration", (inst, def)=>{
|
|
3659
|
+
$ZodISODuration.init(inst, def);
|
|
3660
|
+
ZodStringFormat.init(inst, def);
|
|
3661
|
+
});
|
|
3662
|
+
function iso_duration(params) {
|
|
3663
|
+
return _isoDuration(ZodISODuration, params);
|
|
3664
|
+
}
|
|
3665
|
+
const classic_errors_initializer = (inst, issues)=>{
|
|
3666
|
+
$ZodError.init(inst, issues);
|
|
3667
|
+
inst.name = "ZodError";
|
|
3668
|
+
Object.defineProperties(inst, {
|
|
3669
|
+
format: {
|
|
3670
|
+
value: (mapper)=>formatError(inst, mapper)
|
|
3671
|
+
},
|
|
3672
|
+
flatten: {
|
|
3673
|
+
value: (mapper)=>flattenError(inst, mapper)
|
|
3674
|
+
},
|
|
3675
|
+
addIssue: {
|
|
3676
|
+
value: (issue)=>{
|
|
3677
|
+
inst.issues.push(issue);
|
|
3678
|
+
inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
|
|
3679
|
+
}
|
|
3680
|
+
},
|
|
3681
|
+
addIssues: {
|
|
3682
|
+
value: (issues)=>{
|
|
3683
|
+
inst.issues.push(...issues);
|
|
3684
|
+
inst.message = JSON.stringify(inst.issues, jsonStringifyReplacer, 2);
|
|
3685
|
+
}
|
|
3686
|
+
},
|
|
3687
|
+
isEmpty: {
|
|
3688
|
+
get () {
|
|
3689
|
+
return 0 === inst.issues.length;
|
|
3690
|
+
}
|
|
3691
|
+
}
|
|
3692
|
+
});
|
|
3693
|
+
};
|
|
3694
|
+
$constructor("ZodError", classic_errors_initializer);
|
|
3695
|
+
const ZodRealError = $constructor("ZodError", classic_errors_initializer, {
|
|
3696
|
+
Parent: Error
|
|
3697
|
+
});
|
|
3698
|
+
const classic_parse_parse = /* @__PURE__ */ _parse(ZodRealError);
|
|
3699
|
+
const classic_parse_parseAsync = /* @__PURE__ */ _parseAsync(ZodRealError);
|
|
3700
|
+
const parse_safeParse = /* @__PURE__ */ _safeParse(ZodRealError);
|
|
3701
|
+
const parse_safeParseAsync = /* @__PURE__ */ _safeParseAsync(ZodRealError);
|
|
3702
|
+
const parse_encode = /* @__PURE__ */ _encode(ZodRealError);
|
|
3703
|
+
const parse_decode = /* @__PURE__ */ _decode(ZodRealError);
|
|
3704
|
+
const parse_encodeAsync = /* @__PURE__ */ _encodeAsync(ZodRealError);
|
|
3705
|
+
const parse_decodeAsync = /* @__PURE__ */ _decodeAsync(ZodRealError);
|
|
3706
|
+
const parse_safeEncode = /* @__PURE__ */ _safeEncode(ZodRealError);
|
|
3707
|
+
const parse_safeDecode = /* @__PURE__ */ _safeDecode(ZodRealError);
|
|
3708
|
+
const parse_safeEncodeAsync = /* @__PURE__ */ _safeEncodeAsync(ZodRealError);
|
|
3709
|
+
const parse_safeDecodeAsync = /* @__PURE__ */ _safeDecodeAsync(ZodRealError);
|
|
3710
|
+
const ZodType = /*@__PURE__*/ $constructor("ZodType", (inst, def)=>{
|
|
3711
|
+
$ZodType.init(inst, def);
|
|
3712
|
+
Object.assign(inst["~standard"], {
|
|
3713
|
+
jsonSchema: {
|
|
3714
|
+
input: createStandardJSONSchemaMethod(inst, "input"),
|
|
3715
|
+
output: createStandardJSONSchemaMethod(inst, "output")
|
|
3716
|
+
}
|
|
3717
|
+
});
|
|
3718
|
+
inst.toJSONSchema = createToJSONSchemaMethod(inst, {});
|
|
3719
|
+
inst.def = def;
|
|
3720
|
+
inst.type = def.type;
|
|
3721
|
+
Object.defineProperty(inst, "_def", {
|
|
3722
|
+
value: def
|
|
3723
|
+
});
|
|
3724
|
+
inst.check = (...checks)=>inst.clone(mergeDefs(def, {
|
|
3725
|
+
checks: [
|
|
3726
|
+
...def.checks ?? [],
|
|
3727
|
+
...checks.map((ch)=>"function" == typeof ch ? {
|
|
3728
|
+
_zod: {
|
|
3729
|
+
check: ch,
|
|
3730
|
+
def: {
|
|
3731
|
+
check: "custom"
|
|
3732
|
+
},
|
|
3733
|
+
onattach: []
|
|
3734
|
+
}
|
|
3735
|
+
} : ch)
|
|
3736
|
+
]
|
|
3737
|
+
}));
|
|
3738
|
+
inst.clone = (def, params)=>clone(inst, def, params);
|
|
3739
|
+
inst.brand = ()=>inst;
|
|
3740
|
+
inst.register = (reg, meta)=>{
|
|
3741
|
+
reg.add(inst, meta);
|
|
3742
|
+
return inst;
|
|
3743
|
+
};
|
|
3744
|
+
inst.parse = (data, params)=>classic_parse_parse(inst, data, params, {
|
|
3745
|
+
callee: inst.parse
|
|
3746
|
+
});
|
|
3747
|
+
inst.safeParse = (data, params)=>parse_safeParse(inst, data, params);
|
|
3748
|
+
inst.parseAsync = async (data, params)=>classic_parse_parseAsync(inst, data, params, {
|
|
3749
|
+
callee: inst.parseAsync
|
|
3750
|
+
});
|
|
3751
|
+
inst.safeParseAsync = async (data, params)=>parse_safeParseAsync(inst, data, params);
|
|
3752
|
+
inst.spa = inst.safeParseAsync;
|
|
3753
|
+
inst.encode = (data, params)=>parse_encode(inst, data, params);
|
|
3754
|
+
inst.decode = (data, params)=>parse_decode(inst, data, params);
|
|
3755
|
+
inst.encodeAsync = async (data, params)=>parse_encodeAsync(inst, data, params);
|
|
3756
|
+
inst.decodeAsync = async (data, params)=>parse_decodeAsync(inst, data, params);
|
|
3757
|
+
inst.safeEncode = (data, params)=>parse_safeEncode(inst, data, params);
|
|
3758
|
+
inst.safeDecode = (data, params)=>parse_safeDecode(inst, data, params);
|
|
3759
|
+
inst.safeEncodeAsync = async (data, params)=>parse_safeEncodeAsync(inst, data, params);
|
|
3760
|
+
inst.safeDecodeAsync = async (data, params)=>parse_safeDecodeAsync(inst, data, params);
|
|
3761
|
+
inst.refine = (check, params)=>inst.check(refine(check, params));
|
|
3762
|
+
inst.superRefine = (refinement)=>inst.check(superRefine(refinement));
|
|
3763
|
+
inst.overwrite = (fn)=>inst.check(_overwrite(fn));
|
|
3764
|
+
inst.optional = ()=>optional(inst);
|
|
3765
|
+
inst.nullable = ()=>nullable(inst);
|
|
3766
|
+
inst.nullish = ()=>optional(nullable(inst));
|
|
3767
|
+
inst.nonoptional = (params)=>nonoptional(inst, params);
|
|
3768
|
+
inst.array = ()=>schemas_array(inst);
|
|
3769
|
+
inst.or = (arg)=>union([
|
|
3770
|
+
inst,
|
|
3771
|
+
arg
|
|
3772
|
+
]);
|
|
3773
|
+
inst.and = (arg)=>intersection(inst, arg);
|
|
3774
|
+
inst.transform = (tx)=>pipe(inst, transform(tx));
|
|
3775
|
+
inst.default = (def)=>schemas_default(inst, def);
|
|
3776
|
+
inst.prefault = (def)=>prefault(inst, def);
|
|
3777
|
+
inst.catch = (params)=>schemas_catch(inst, params);
|
|
3778
|
+
inst.pipe = (target)=>pipe(inst, target);
|
|
3779
|
+
inst.readonly = ()=>readonly(inst);
|
|
3780
|
+
inst.describe = (description)=>{
|
|
3781
|
+
const cl = inst.clone();
|
|
3782
|
+
globalRegistry.add(cl, {
|
|
3783
|
+
description
|
|
3784
|
+
});
|
|
3785
|
+
return cl;
|
|
3786
|
+
};
|
|
3787
|
+
Object.defineProperty(inst, "description", {
|
|
3788
|
+
get () {
|
|
3789
|
+
return globalRegistry.get(inst)?.description;
|
|
3790
|
+
},
|
|
3791
|
+
configurable: true
|
|
3792
|
+
});
|
|
3793
|
+
inst.meta = (...args)=>{
|
|
3794
|
+
if (0 === args.length) return globalRegistry.get(inst);
|
|
3795
|
+
const cl = inst.clone();
|
|
3796
|
+
globalRegistry.add(cl, args[0]);
|
|
3797
|
+
return cl;
|
|
3798
|
+
};
|
|
3799
|
+
inst.isOptional = ()=>inst.safeParse(void 0).success;
|
|
3800
|
+
inst.isNullable = ()=>inst.safeParse(null).success;
|
|
3801
|
+
return inst;
|
|
3802
|
+
});
|
|
3803
|
+
const _ZodString = /*@__PURE__*/ $constructor("_ZodString", (inst, def)=>{
|
|
3804
|
+
$ZodString.init(inst, def);
|
|
3805
|
+
ZodType.init(inst, def);
|
|
3806
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>stringProcessor(inst, ctx, json, params);
|
|
3807
|
+
const bag = inst._zod.bag;
|
|
3808
|
+
inst.format = bag.format ?? null;
|
|
3809
|
+
inst.minLength = bag.minimum ?? null;
|
|
3810
|
+
inst.maxLength = bag.maximum ?? null;
|
|
3811
|
+
inst.regex = (...args)=>inst.check(_regex(...args));
|
|
3812
|
+
inst.includes = (...args)=>inst.check(_includes(...args));
|
|
3813
|
+
inst.startsWith = (...args)=>inst.check(_startsWith(...args));
|
|
3814
|
+
inst.endsWith = (...args)=>inst.check(_endsWith(...args));
|
|
3815
|
+
inst.min = (...args)=>inst.check(_minLength(...args));
|
|
3816
|
+
inst.max = (...args)=>inst.check(_maxLength(...args));
|
|
3817
|
+
inst.length = (...args)=>inst.check(_length(...args));
|
|
3818
|
+
inst.nonempty = (...args)=>inst.check(_minLength(1, ...args));
|
|
3819
|
+
inst.lowercase = (params)=>inst.check(_lowercase(params));
|
|
3820
|
+
inst.uppercase = (params)=>inst.check(_uppercase(params));
|
|
3821
|
+
inst.trim = ()=>inst.check(_trim());
|
|
3822
|
+
inst.normalize = (...args)=>inst.check(_normalize(...args));
|
|
3823
|
+
inst.toLowerCase = ()=>inst.check(_toLowerCase());
|
|
3824
|
+
inst.toUpperCase = ()=>inst.check(_toUpperCase());
|
|
3825
|
+
inst.slugify = ()=>inst.check(_slugify());
|
|
3826
|
+
});
|
|
3827
|
+
const ZodString = /*@__PURE__*/ $constructor("ZodString", (inst, def)=>{
|
|
3828
|
+
$ZodString.init(inst, def);
|
|
3829
|
+
_ZodString.init(inst, def);
|
|
3830
|
+
inst.email = (params)=>inst.check(_email(ZodEmail, params));
|
|
3831
|
+
inst.url = (params)=>inst.check(_url(ZodURL, params));
|
|
3832
|
+
inst.jwt = (params)=>inst.check(_jwt(ZodJWT, params));
|
|
3833
|
+
inst.emoji = (params)=>inst.check(api_emoji(ZodEmoji, params));
|
|
3834
|
+
inst.guid = (params)=>inst.check(_guid(ZodGUID, params));
|
|
3835
|
+
inst.uuid = (params)=>inst.check(_uuid(ZodUUID, params));
|
|
3836
|
+
inst.uuidv4 = (params)=>inst.check(_uuidv4(ZodUUID, params));
|
|
3837
|
+
inst.uuidv6 = (params)=>inst.check(_uuidv6(ZodUUID, params));
|
|
3838
|
+
inst.uuidv7 = (params)=>inst.check(_uuidv7(ZodUUID, params));
|
|
3839
|
+
inst.nanoid = (params)=>inst.check(_nanoid(ZodNanoID, params));
|
|
3840
|
+
inst.guid = (params)=>inst.check(_guid(ZodGUID, params));
|
|
3841
|
+
inst.cuid = (params)=>inst.check(_cuid(ZodCUID, params));
|
|
3842
|
+
inst.cuid2 = (params)=>inst.check(_cuid2(ZodCUID2, params));
|
|
3843
|
+
inst.ulid = (params)=>inst.check(_ulid(ZodULID, params));
|
|
3844
|
+
inst.base64 = (params)=>inst.check(_base64(ZodBase64, params));
|
|
3845
|
+
inst.base64url = (params)=>inst.check(_base64url(ZodBase64URL, params));
|
|
3846
|
+
inst.xid = (params)=>inst.check(_xid(ZodXID, params));
|
|
3847
|
+
inst.ksuid = (params)=>inst.check(_ksuid(ZodKSUID, params));
|
|
3848
|
+
inst.ipv4 = (params)=>inst.check(_ipv4(ZodIPv4, params));
|
|
3849
|
+
inst.ipv6 = (params)=>inst.check(_ipv6(ZodIPv6, params));
|
|
3850
|
+
inst.cidrv4 = (params)=>inst.check(_cidrv4(ZodCIDRv4, params));
|
|
3851
|
+
inst.cidrv6 = (params)=>inst.check(_cidrv6(ZodCIDRv6, params));
|
|
3852
|
+
inst.e164 = (params)=>inst.check(_e164(ZodE164, params));
|
|
3853
|
+
inst.datetime = (params)=>inst.check(iso_datetime(params));
|
|
3854
|
+
inst.date = (params)=>inst.check(iso_date(params));
|
|
3855
|
+
inst.time = (params)=>inst.check(iso_time(params));
|
|
3856
|
+
inst.duration = (params)=>inst.check(iso_duration(params));
|
|
3857
|
+
});
|
|
3858
|
+
function schemas_string(params) {
|
|
3859
|
+
return _string(ZodString, params);
|
|
3860
|
+
}
|
|
3861
|
+
const ZodStringFormat = /*@__PURE__*/ $constructor("ZodStringFormat", (inst, def)=>{
|
|
3862
|
+
$ZodStringFormat.init(inst, def);
|
|
3863
|
+
_ZodString.init(inst, def);
|
|
3864
|
+
});
|
|
3865
|
+
const ZodEmail = /*@__PURE__*/ $constructor("ZodEmail", (inst, def)=>{
|
|
3866
|
+
$ZodEmail.init(inst, def);
|
|
3867
|
+
ZodStringFormat.init(inst, def);
|
|
3868
|
+
});
|
|
3869
|
+
const ZodGUID = /*@__PURE__*/ $constructor("ZodGUID", (inst, def)=>{
|
|
3870
|
+
$ZodGUID.init(inst, def);
|
|
3871
|
+
ZodStringFormat.init(inst, def);
|
|
3872
|
+
});
|
|
3873
|
+
const ZodUUID = /*@__PURE__*/ $constructor("ZodUUID", (inst, def)=>{
|
|
3874
|
+
$ZodUUID.init(inst, def);
|
|
3875
|
+
ZodStringFormat.init(inst, def);
|
|
3876
|
+
});
|
|
3877
|
+
const ZodURL = /*@__PURE__*/ $constructor("ZodURL", (inst, def)=>{
|
|
3878
|
+
$ZodURL.init(inst, def);
|
|
3879
|
+
ZodStringFormat.init(inst, def);
|
|
3880
|
+
});
|
|
3881
|
+
const ZodEmoji = /*@__PURE__*/ $constructor("ZodEmoji", (inst, def)=>{
|
|
3882
|
+
$ZodEmoji.init(inst, def);
|
|
3883
|
+
ZodStringFormat.init(inst, def);
|
|
3884
|
+
});
|
|
3885
|
+
const ZodNanoID = /*@__PURE__*/ $constructor("ZodNanoID", (inst, def)=>{
|
|
3886
|
+
$ZodNanoID.init(inst, def);
|
|
3887
|
+
ZodStringFormat.init(inst, def);
|
|
3888
|
+
});
|
|
3889
|
+
const ZodCUID = /*@__PURE__*/ $constructor("ZodCUID", (inst, def)=>{
|
|
3890
|
+
$ZodCUID.init(inst, def);
|
|
3891
|
+
ZodStringFormat.init(inst, def);
|
|
3892
|
+
});
|
|
3893
|
+
const ZodCUID2 = /*@__PURE__*/ $constructor("ZodCUID2", (inst, def)=>{
|
|
3894
|
+
$ZodCUID2.init(inst, def);
|
|
3895
|
+
ZodStringFormat.init(inst, def);
|
|
3896
|
+
});
|
|
3897
|
+
const ZodULID = /*@__PURE__*/ $constructor("ZodULID", (inst, def)=>{
|
|
3898
|
+
$ZodULID.init(inst, def);
|
|
3899
|
+
ZodStringFormat.init(inst, def);
|
|
3900
|
+
});
|
|
3901
|
+
const ZodXID = /*@__PURE__*/ $constructor("ZodXID", (inst, def)=>{
|
|
3902
|
+
$ZodXID.init(inst, def);
|
|
3903
|
+
ZodStringFormat.init(inst, def);
|
|
3904
|
+
});
|
|
3905
|
+
const ZodKSUID = /*@__PURE__*/ $constructor("ZodKSUID", (inst, def)=>{
|
|
3906
|
+
$ZodKSUID.init(inst, def);
|
|
3907
|
+
ZodStringFormat.init(inst, def);
|
|
3908
|
+
});
|
|
3909
|
+
const ZodIPv4 = /*@__PURE__*/ $constructor("ZodIPv4", (inst, def)=>{
|
|
3910
|
+
$ZodIPv4.init(inst, def);
|
|
3911
|
+
ZodStringFormat.init(inst, def);
|
|
3912
|
+
});
|
|
3913
|
+
const ZodIPv6 = /*@__PURE__*/ $constructor("ZodIPv6", (inst, def)=>{
|
|
3914
|
+
$ZodIPv6.init(inst, def);
|
|
3915
|
+
ZodStringFormat.init(inst, def);
|
|
3916
|
+
});
|
|
3917
|
+
const ZodCIDRv4 = /*@__PURE__*/ $constructor("ZodCIDRv4", (inst, def)=>{
|
|
3918
|
+
$ZodCIDRv4.init(inst, def);
|
|
3919
|
+
ZodStringFormat.init(inst, def);
|
|
3920
|
+
});
|
|
3921
|
+
const ZodCIDRv6 = /*@__PURE__*/ $constructor("ZodCIDRv6", (inst, def)=>{
|
|
3922
|
+
$ZodCIDRv6.init(inst, def);
|
|
3923
|
+
ZodStringFormat.init(inst, def);
|
|
3924
|
+
});
|
|
3925
|
+
const ZodBase64 = /*@__PURE__*/ $constructor("ZodBase64", (inst, def)=>{
|
|
3926
|
+
$ZodBase64.init(inst, def);
|
|
3927
|
+
ZodStringFormat.init(inst, def);
|
|
3928
|
+
});
|
|
3929
|
+
const ZodBase64URL = /*@__PURE__*/ $constructor("ZodBase64URL", (inst, def)=>{
|
|
3930
|
+
$ZodBase64URL.init(inst, def);
|
|
3931
|
+
ZodStringFormat.init(inst, def);
|
|
3932
|
+
});
|
|
3933
|
+
const ZodE164 = /*@__PURE__*/ $constructor("ZodE164", (inst, def)=>{
|
|
3934
|
+
$ZodE164.init(inst, def);
|
|
3935
|
+
ZodStringFormat.init(inst, def);
|
|
3936
|
+
});
|
|
3937
|
+
const ZodJWT = /*@__PURE__*/ $constructor("ZodJWT", (inst, def)=>{
|
|
3938
|
+
$ZodJWT.init(inst, def);
|
|
3939
|
+
ZodStringFormat.init(inst, def);
|
|
3940
|
+
});
|
|
3941
|
+
const ZodNumber = /*@__PURE__*/ $constructor("ZodNumber", (inst, def)=>{
|
|
3942
|
+
$ZodNumber.init(inst, def);
|
|
3943
|
+
ZodType.init(inst, def);
|
|
3944
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>numberProcessor(inst, ctx, json, params);
|
|
3945
|
+
inst.gt = (value, params)=>inst.check(_gt(value, params));
|
|
3946
|
+
inst.gte = (value, params)=>inst.check(_gte(value, params));
|
|
3947
|
+
inst.min = (value, params)=>inst.check(_gte(value, params));
|
|
3948
|
+
inst.lt = (value, params)=>inst.check(_lt(value, params));
|
|
3949
|
+
inst.lte = (value, params)=>inst.check(_lte(value, params));
|
|
3950
|
+
inst.max = (value, params)=>inst.check(_lte(value, params));
|
|
3951
|
+
inst.int = (params)=>inst.check(schemas_int(params));
|
|
3952
|
+
inst.safe = (params)=>inst.check(schemas_int(params));
|
|
3953
|
+
inst.positive = (params)=>inst.check(_gt(0, params));
|
|
3954
|
+
inst.nonnegative = (params)=>inst.check(_gte(0, params));
|
|
3955
|
+
inst.negative = (params)=>inst.check(_lt(0, params));
|
|
3956
|
+
inst.nonpositive = (params)=>inst.check(_lte(0, params));
|
|
3957
|
+
inst.multipleOf = (value, params)=>inst.check(_multipleOf(value, params));
|
|
3958
|
+
inst.step = (value, params)=>inst.check(_multipleOf(value, params));
|
|
3959
|
+
inst.finite = ()=>inst;
|
|
3960
|
+
const bag = inst._zod.bag;
|
|
3961
|
+
inst.minValue = Math.max(bag.minimum ?? -1 / 0, bag.exclusiveMinimum ?? -1 / 0) ?? null;
|
|
3962
|
+
inst.maxValue = Math.min(bag.maximum ?? 1 / 0, bag.exclusiveMaximum ?? 1 / 0) ?? null;
|
|
3963
|
+
inst.isInt = (bag.format ?? "").includes("int") || Number.isSafeInteger(bag.multipleOf ?? 0.5);
|
|
3964
|
+
inst.isFinite = true;
|
|
3965
|
+
inst.format = bag.format ?? null;
|
|
3966
|
+
});
|
|
3967
|
+
function schemas_number(params) {
|
|
3968
|
+
return _number(ZodNumber, params);
|
|
3969
|
+
}
|
|
3970
|
+
const ZodNumberFormat = /*@__PURE__*/ $constructor("ZodNumberFormat", (inst, def)=>{
|
|
3971
|
+
$ZodNumberFormat.init(inst, def);
|
|
3972
|
+
ZodNumber.init(inst, def);
|
|
3973
|
+
});
|
|
3974
|
+
function schemas_int(params) {
|
|
3975
|
+
return _int(ZodNumberFormat, params);
|
|
3976
|
+
}
|
|
3977
|
+
const ZodUnknown = /*@__PURE__*/ $constructor("ZodUnknown", (inst, def)=>{
|
|
3978
|
+
$ZodUnknown.init(inst, def);
|
|
3979
|
+
ZodType.init(inst, def);
|
|
3980
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>unknownProcessor(inst, ctx, json, params);
|
|
3981
|
+
});
|
|
3982
|
+
function unknown() {
|
|
3983
|
+
return _unknown(ZodUnknown);
|
|
3984
|
+
}
|
|
3985
|
+
const ZodNever = /*@__PURE__*/ $constructor("ZodNever", (inst, def)=>{
|
|
3986
|
+
$ZodNever.init(inst, def);
|
|
3987
|
+
ZodType.init(inst, def);
|
|
3988
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>neverProcessor(inst, ctx, json, params);
|
|
3989
|
+
});
|
|
3990
|
+
function never(params) {
|
|
3991
|
+
return _never(ZodNever, params);
|
|
3992
|
+
}
|
|
3993
|
+
const ZodArray = /*@__PURE__*/ $constructor("ZodArray", (inst, def)=>{
|
|
3994
|
+
$ZodArray.init(inst, def);
|
|
3995
|
+
ZodType.init(inst, def);
|
|
3996
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>arrayProcessor(inst, ctx, json, params);
|
|
3997
|
+
inst.element = def.element;
|
|
3998
|
+
inst.min = (minLength, params)=>inst.check(_minLength(minLength, params));
|
|
3999
|
+
inst.nonempty = (params)=>inst.check(_minLength(1, params));
|
|
4000
|
+
inst.max = (maxLength, params)=>inst.check(_maxLength(maxLength, params));
|
|
4001
|
+
inst.length = (len, params)=>inst.check(_length(len, params));
|
|
4002
|
+
inst.unwrap = ()=>inst.element;
|
|
4003
|
+
});
|
|
4004
|
+
function schemas_array(element, params) {
|
|
4005
|
+
return _array(ZodArray, element, params);
|
|
4006
|
+
}
|
|
4007
|
+
const ZodObject = /*@__PURE__*/ $constructor("ZodObject", (inst, def)=>{
|
|
4008
|
+
$ZodObjectJIT.init(inst, def);
|
|
4009
|
+
ZodType.init(inst, def);
|
|
4010
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>objectProcessor(inst, ctx, json, params);
|
|
4011
|
+
defineLazy(inst, "shape", ()=>def.shape);
|
|
4012
|
+
inst.keyof = ()=>schemas_enum(Object.keys(inst._zod.def.shape));
|
|
4013
|
+
inst.catchall = (catchall)=>inst.clone({
|
|
4014
|
+
...inst._zod.def,
|
|
4015
|
+
catchall: catchall
|
|
4016
|
+
});
|
|
4017
|
+
inst.passthrough = ()=>inst.clone({
|
|
4018
|
+
...inst._zod.def,
|
|
4019
|
+
catchall: unknown()
|
|
4020
|
+
});
|
|
4021
|
+
inst.loose = ()=>inst.clone({
|
|
4022
|
+
...inst._zod.def,
|
|
4023
|
+
catchall: unknown()
|
|
4024
|
+
});
|
|
4025
|
+
inst.strict = ()=>inst.clone({
|
|
4026
|
+
...inst._zod.def,
|
|
4027
|
+
catchall: never()
|
|
4028
|
+
});
|
|
4029
|
+
inst.strip = ()=>inst.clone({
|
|
4030
|
+
...inst._zod.def,
|
|
4031
|
+
catchall: void 0
|
|
4032
|
+
});
|
|
4033
|
+
inst.extend = (incoming)=>extend(inst, incoming);
|
|
4034
|
+
inst.safeExtend = (incoming)=>safeExtend(inst, incoming);
|
|
4035
|
+
inst.merge = (other)=>merge(inst, other);
|
|
4036
|
+
inst.pick = (mask)=>pick(inst, mask);
|
|
4037
|
+
inst.omit = (mask)=>omit(inst, mask);
|
|
4038
|
+
inst.partial = (...args)=>partial(ZodOptional, inst, args[0]);
|
|
4039
|
+
inst.required = (...args)=>required(ZodNonOptional, inst, args[0]);
|
|
4040
|
+
});
|
|
4041
|
+
function schemas_object(shape, params) {
|
|
4042
|
+
const def = {
|
|
4043
|
+
type: "object",
|
|
4044
|
+
shape: shape ?? {},
|
|
4045
|
+
...normalizeParams(params)
|
|
4046
|
+
};
|
|
4047
|
+
return new ZodObject(def);
|
|
4048
|
+
}
|
|
4049
|
+
const ZodUnion = /*@__PURE__*/ $constructor("ZodUnion", (inst, def)=>{
|
|
4050
|
+
$ZodUnion.init(inst, def);
|
|
4051
|
+
ZodType.init(inst, def);
|
|
4052
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>unionProcessor(inst, ctx, json, params);
|
|
4053
|
+
inst.options = def.options;
|
|
4054
|
+
});
|
|
4055
|
+
function union(options, params) {
|
|
4056
|
+
return new ZodUnion({
|
|
4057
|
+
type: "union",
|
|
4058
|
+
options: options,
|
|
4059
|
+
...normalizeParams(params)
|
|
4060
|
+
});
|
|
4061
|
+
}
|
|
4062
|
+
const ZodIntersection = /*@__PURE__*/ $constructor("ZodIntersection", (inst, def)=>{
|
|
4063
|
+
$ZodIntersection.init(inst, def);
|
|
4064
|
+
ZodType.init(inst, def);
|
|
4065
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>intersectionProcessor(inst, ctx, json, params);
|
|
4066
|
+
});
|
|
4067
|
+
function intersection(left, right) {
|
|
4068
|
+
return new ZodIntersection({
|
|
4069
|
+
type: "intersection",
|
|
4070
|
+
left: left,
|
|
4071
|
+
right: right
|
|
4072
|
+
});
|
|
4073
|
+
}
|
|
4074
|
+
const ZodTuple = /*@__PURE__*/ $constructor("ZodTuple", (inst, def)=>{
|
|
4075
|
+
$ZodTuple.init(inst, def);
|
|
4076
|
+
ZodType.init(inst, def);
|
|
4077
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>tupleProcessor(inst, ctx, json, params);
|
|
4078
|
+
inst.rest = (rest)=>inst.clone({
|
|
4079
|
+
...inst._zod.def,
|
|
4080
|
+
rest: rest
|
|
4081
|
+
});
|
|
4082
|
+
});
|
|
4083
|
+
function tuple(items, _paramsOrRest, _params) {
|
|
4084
|
+
const hasRest = _paramsOrRest instanceof $ZodType;
|
|
4085
|
+
const params = hasRest ? _params : _paramsOrRest;
|
|
4086
|
+
const rest = hasRest ? _paramsOrRest : null;
|
|
4087
|
+
return new ZodTuple({
|
|
4088
|
+
type: "tuple",
|
|
4089
|
+
items: items,
|
|
4090
|
+
rest,
|
|
4091
|
+
...normalizeParams(params)
|
|
4092
|
+
});
|
|
4093
|
+
}
|
|
4094
|
+
const ZodEnum = /*@__PURE__*/ $constructor("ZodEnum", (inst, def)=>{
|
|
4095
|
+
$ZodEnum.init(inst, def);
|
|
4096
|
+
ZodType.init(inst, def);
|
|
4097
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>enumProcessor(inst, ctx, json, params);
|
|
4098
|
+
inst.enum = def.entries;
|
|
4099
|
+
inst.options = Object.values(def.entries);
|
|
4100
|
+
const keys = new Set(Object.keys(def.entries));
|
|
4101
|
+
inst.extract = (values, params)=>{
|
|
4102
|
+
const newEntries = {};
|
|
4103
|
+
for (const value of values)if (keys.has(value)) newEntries[value] = def.entries[value];
|
|
4104
|
+
else throw new Error(`Key ${value} not found in enum`);
|
|
4105
|
+
return new ZodEnum({
|
|
4106
|
+
...def,
|
|
4107
|
+
checks: [],
|
|
4108
|
+
...normalizeParams(params),
|
|
4109
|
+
entries: newEntries
|
|
4110
|
+
});
|
|
4111
|
+
};
|
|
4112
|
+
inst.exclude = (values, params)=>{
|
|
4113
|
+
const newEntries = {
|
|
4114
|
+
...def.entries
|
|
4115
|
+
};
|
|
4116
|
+
for (const value of values)if (keys.has(value)) delete newEntries[value];
|
|
4117
|
+
else throw new Error(`Key ${value} not found in enum`);
|
|
4118
|
+
return new ZodEnum({
|
|
4119
|
+
...def,
|
|
4120
|
+
checks: [],
|
|
4121
|
+
...normalizeParams(params),
|
|
4122
|
+
entries: newEntries
|
|
4123
|
+
});
|
|
4124
|
+
};
|
|
4125
|
+
});
|
|
4126
|
+
function schemas_enum(values, params) {
|
|
4127
|
+
const entries = Array.isArray(values) ? Object.fromEntries(values.map((v)=>[
|
|
4128
|
+
v,
|
|
4129
|
+
v
|
|
4130
|
+
])) : values;
|
|
4131
|
+
return new ZodEnum({
|
|
4132
|
+
type: "enum",
|
|
4133
|
+
entries,
|
|
4134
|
+
...normalizeParams(params)
|
|
4135
|
+
});
|
|
4136
|
+
}
|
|
4137
|
+
const ZodTransform = /*@__PURE__*/ $constructor("ZodTransform", (inst, def)=>{
|
|
4138
|
+
$ZodTransform.init(inst, def);
|
|
4139
|
+
ZodType.init(inst, def);
|
|
4140
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>transformProcessor(inst, ctx, json, params);
|
|
4141
|
+
inst._zod.parse = (payload, _ctx)=>{
|
|
4142
|
+
if ("backward" === _ctx.direction) throw new $ZodEncodeError(inst.constructor.name);
|
|
4143
|
+
payload.addIssue = (issue)=>{
|
|
4144
|
+
if ("string" == typeof issue) payload.issues.push(util_issue(issue, payload.value, def));
|
|
4145
|
+
else {
|
|
4146
|
+
const _issue = issue;
|
|
4147
|
+
if (_issue.fatal) _issue.continue = false;
|
|
4148
|
+
_issue.code ?? (_issue.code = "custom");
|
|
4149
|
+
_issue.input ?? (_issue.input = payload.value);
|
|
4150
|
+
_issue.inst ?? (_issue.inst = inst);
|
|
4151
|
+
payload.issues.push(util_issue(_issue));
|
|
4152
|
+
}
|
|
4153
|
+
};
|
|
4154
|
+
const output = def.transform(payload.value, payload);
|
|
4155
|
+
if (output instanceof Promise) return output.then((output)=>{
|
|
4156
|
+
payload.value = output;
|
|
4157
|
+
return payload;
|
|
4158
|
+
});
|
|
4159
|
+
payload.value = output;
|
|
4160
|
+
return payload;
|
|
4161
|
+
};
|
|
4162
|
+
});
|
|
4163
|
+
function transform(fn) {
|
|
4164
|
+
return new ZodTransform({
|
|
4165
|
+
type: "transform",
|
|
4166
|
+
transform: fn
|
|
4167
|
+
});
|
|
4168
|
+
}
|
|
4169
|
+
const ZodOptional = /*@__PURE__*/ $constructor("ZodOptional", (inst, def)=>{
|
|
4170
|
+
$ZodOptional.init(inst, def);
|
|
4171
|
+
ZodType.init(inst, def);
|
|
4172
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>optionalProcessor(inst, ctx, json, params);
|
|
4173
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4174
|
+
});
|
|
4175
|
+
function optional(innerType) {
|
|
4176
|
+
return new ZodOptional({
|
|
4177
|
+
type: "optional",
|
|
4178
|
+
innerType: innerType
|
|
4179
|
+
});
|
|
4180
|
+
}
|
|
4181
|
+
const ZodNullable = /*@__PURE__*/ $constructor("ZodNullable", (inst, def)=>{
|
|
4182
|
+
$ZodNullable.init(inst, def);
|
|
4183
|
+
ZodType.init(inst, def);
|
|
4184
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>nullableProcessor(inst, ctx, json, params);
|
|
4185
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4186
|
+
});
|
|
4187
|
+
function nullable(innerType) {
|
|
4188
|
+
return new ZodNullable({
|
|
4189
|
+
type: "nullable",
|
|
4190
|
+
innerType: innerType
|
|
4191
|
+
});
|
|
4192
|
+
}
|
|
4193
|
+
const ZodDefault = /*@__PURE__*/ $constructor("ZodDefault", (inst, def)=>{
|
|
4194
|
+
$ZodDefault.init(inst, def);
|
|
4195
|
+
ZodType.init(inst, def);
|
|
4196
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>defaultProcessor(inst, ctx, json, params);
|
|
4197
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4198
|
+
inst.removeDefault = inst.unwrap;
|
|
4199
|
+
});
|
|
4200
|
+
function schemas_default(innerType, defaultValue) {
|
|
4201
|
+
return new ZodDefault({
|
|
4202
|
+
type: "default",
|
|
4203
|
+
innerType: innerType,
|
|
4204
|
+
get defaultValue () {
|
|
4205
|
+
return "function" == typeof defaultValue ? defaultValue() : shallowClone(defaultValue);
|
|
4206
|
+
}
|
|
4207
|
+
});
|
|
4208
|
+
}
|
|
4209
|
+
const ZodPrefault = /*@__PURE__*/ $constructor("ZodPrefault", (inst, def)=>{
|
|
4210
|
+
$ZodPrefault.init(inst, def);
|
|
4211
|
+
ZodType.init(inst, def);
|
|
4212
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>prefaultProcessor(inst, ctx, json, params);
|
|
4213
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4214
|
+
});
|
|
4215
|
+
function prefault(innerType, defaultValue) {
|
|
4216
|
+
return new ZodPrefault({
|
|
4217
|
+
type: "prefault",
|
|
4218
|
+
innerType: innerType,
|
|
4219
|
+
get defaultValue () {
|
|
4220
|
+
return "function" == typeof defaultValue ? defaultValue() : shallowClone(defaultValue);
|
|
4221
|
+
}
|
|
4222
|
+
});
|
|
4223
|
+
}
|
|
4224
|
+
const ZodNonOptional = /*@__PURE__*/ $constructor("ZodNonOptional", (inst, def)=>{
|
|
4225
|
+
$ZodNonOptional.init(inst, def);
|
|
4226
|
+
ZodType.init(inst, def);
|
|
4227
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>nonoptionalProcessor(inst, ctx, json, params);
|
|
4228
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4229
|
+
});
|
|
4230
|
+
function nonoptional(innerType, params) {
|
|
4231
|
+
return new ZodNonOptional({
|
|
4232
|
+
type: "nonoptional",
|
|
4233
|
+
innerType: innerType,
|
|
4234
|
+
...normalizeParams(params)
|
|
4235
|
+
});
|
|
4236
|
+
}
|
|
4237
|
+
const ZodCatch = /*@__PURE__*/ $constructor("ZodCatch", (inst, def)=>{
|
|
4238
|
+
$ZodCatch.init(inst, def);
|
|
4239
|
+
ZodType.init(inst, def);
|
|
4240
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>catchProcessor(inst, ctx, json, params);
|
|
4241
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4242
|
+
inst.removeCatch = inst.unwrap;
|
|
4243
|
+
});
|
|
4244
|
+
function schemas_catch(innerType, catchValue) {
|
|
4245
|
+
return new ZodCatch({
|
|
4246
|
+
type: "catch",
|
|
4247
|
+
innerType: innerType,
|
|
4248
|
+
catchValue: "function" == typeof catchValue ? catchValue : ()=>catchValue
|
|
4249
|
+
});
|
|
4250
|
+
}
|
|
4251
|
+
const ZodPipe = /*@__PURE__*/ $constructor("ZodPipe", (inst, def)=>{
|
|
4252
|
+
$ZodPipe.init(inst, def);
|
|
4253
|
+
ZodType.init(inst, def);
|
|
4254
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>pipeProcessor(inst, ctx, json, params);
|
|
4255
|
+
inst.in = def.in;
|
|
4256
|
+
inst.out = def.out;
|
|
4257
|
+
});
|
|
4258
|
+
function pipe(in_, out) {
|
|
4259
|
+
return new ZodPipe({
|
|
4260
|
+
type: "pipe",
|
|
4261
|
+
in: in_,
|
|
4262
|
+
out: out
|
|
4263
|
+
});
|
|
4264
|
+
}
|
|
4265
|
+
const ZodReadonly = /*@__PURE__*/ $constructor("ZodReadonly", (inst, def)=>{
|
|
4266
|
+
$ZodReadonly.init(inst, def);
|
|
4267
|
+
ZodType.init(inst, def);
|
|
4268
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>readonlyProcessor(inst, ctx, json, params);
|
|
4269
|
+
inst.unwrap = ()=>inst._zod.def.innerType;
|
|
4270
|
+
});
|
|
4271
|
+
function readonly(innerType) {
|
|
4272
|
+
return new ZodReadonly({
|
|
4273
|
+
type: "readonly",
|
|
4274
|
+
innerType: innerType
|
|
4275
|
+
});
|
|
4276
|
+
}
|
|
4277
|
+
const ZodCustom = /*@__PURE__*/ $constructor("ZodCustom", (inst, def)=>{
|
|
4278
|
+
$ZodCustom.init(inst, def);
|
|
4279
|
+
ZodType.init(inst, def);
|
|
4280
|
+
inst._zod.processJSONSchema = (ctx, json, params)=>customProcessor(inst, ctx, json, params);
|
|
4281
|
+
});
|
|
4282
|
+
function refine(fn, _params = {}) {
|
|
4283
|
+
return _refine(ZodCustom, fn, _params);
|
|
4284
|
+
}
|
|
4285
|
+
function superRefine(fn) {
|
|
4286
|
+
return _superRefine(fn);
|
|
4287
|
+
}
|
|
4288
|
+
var compat_ZodFirstPartyTypeKind;
|
|
4289
|
+
compat_ZodFirstPartyTypeKind || (compat_ZodFirstPartyTypeKind = {});
|
|
4290
|
+
core_config(en());
|
|
4291
|
+
const MAX_EVENTS = 500;
|
|
4292
|
+
function diagnosticPath(value) {
|
|
4293
|
+
if (!value) return '';
|
|
4294
|
+
try {
|
|
4295
|
+
return new URL(value, 'http://preview.invalid').pathname;
|
|
4296
|
+
} catch {
|
|
4297
|
+
return value.split(/[?#]/, 1)[0];
|
|
4298
|
+
}
|
|
4299
|
+
}
|
|
4300
|
+
function redactMessage(value) {
|
|
4301
|
+
return value.replace(/(?:https?:\/\/|\/)[^\s"'<>]+[?#][^\s"'<>]*/gi, diagnosticPath).replace(/https?:\/\/[^/\s]+@/gi, '').replace(/\bBearer\s+[^\s,;]+/gi, 'Bearer [redacted]').replace(/\b(token|password|secret|authorization|cookie)\s*[:=]\s*[^\s,;]+/gi, '$1=[redacted]');
|
|
4302
|
+
}
|
|
4303
|
+
const detailFields = schemas_array(tuple([
|
|
4304
|
+
schemas_string().max(128),
|
|
4305
|
+
schemas_string().max(512)
|
|
4306
|
+
])).max(24);
|
|
4307
|
+
const eventSchema = schemas_object({
|
|
4308
|
+
kind: schemas_enum([
|
|
4309
|
+
'error',
|
|
4310
|
+
'console',
|
|
4311
|
+
'unhandledrejection',
|
|
4312
|
+
'network',
|
|
4313
|
+
'resource'
|
|
4314
|
+
]),
|
|
4315
|
+
level: schemas_enum([
|
|
4316
|
+
'log',
|
|
4317
|
+
'info',
|
|
4318
|
+
'warn',
|
|
4319
|
+
'error',
|
|
4320
|
+
'debug'
|
|
4321
|
+
]).optional(),
|
|
4322
|
+
stack: schemas_string().max(4096).transform(redactMessage).optional(),
|
|
4323
|
+
message: schemas_string().max(4096).transform(redactMessage).optional(),
|
|
4324
|
+
path: schemas_string().max(4096).transform(diagnosticPath).optional(),
|
|
4325
|
+
line: schemas_number().int().nonnegative().optional(),
|
|
4326
|
+
column: schemas_number().int().nonnegative().optional(),
|
|
4327
|
+
method: schemas_string().regex(/^[A-Z]{1,24}$/i).optional(),
|
|
4328
|
+
status: schemas_number().int().min(0).max(599).optional(),
|
|
4329
|
+
durationMs: schemas_number().nonnegative().optional(),
|
|
4330
|
+
requestDetails: schemas_object({
|
|
4331
|
+
url: schemas_string().max(4096).optional(),
|
|
4332
|
+
headers: detailFields,
|
|
4333
|
+
query: detailFields.optional(),
|
|
4334
|
+
body: schemas_string().max(4096),
|
|
4335
|
+
responseHeaders: detailFields.optional(),
|
|
4336
|
+
responseBody: schemas_string().max(4096).optional()
|
|
4337
|
+
}).optional()
|
|
4338
|
+
});
|
|
4339
|
+
class PreviewDiagnostics {
|
|
4340
|
+
events = [];
|
|
4341
|
+
cursor = 0;
|
|
4342
|
+
lastProbeReadyAt = null;
|
|
4343
|
+
captureRequestDetails = false;
|
|
4344
|
+
add(input) {
|
|
4345
|
+
if (input && 'object' == typeof input && 'kind' in input && 'preview-ready' === input.kind) {
|
|
4346
|
+
this.lastProbeReadyAt = Date.now();
|
|
4347
|
+
return true;
|
|
4348
|
+
}
|
|
4349
|
+
const result = eventSchema.safeParse(input);
|
|
4350
|
+
if (!result.success) return false;
|
|
4351
|
+
const { requestDetails, ...event } = result.data;
|
|
4352
|
+
this.events.push({
|
|
4353
|
+
...event,
|
|
4354
|
+
...this.captureRequestDetails && ('network' === event.kind || 'resource' === event.kind) && requestDetails ? {
|
|
4355
|
+
requestDetails
|
|
4356
|
+
} : {},
|
|
4357
|
+
seq: ++this.cursor,
|
|
4358
|
+
time: Date.now()
|
|
4359
|
+
});
|
|
4360
|
+
if (this.events.length > MAX_EVENTS) this.events.shift();
|
|
4361
|
+
return true;
|
|
4362
|
+
}
|
|
4363
|
+
read(after) {
|
|
4364
|
+
return {
|
|
4365
|
+
events: this.events.filter(({ seq })=>seq > after),
|
|
4366
|
+
nextCursor: this.cursor,
|
|
4367
|
+
lastProbeReadyAt: this.lastProbeReadyAt
|
|
4368
|
+
};
|
|
4369
|
+
}
|
|
4370
|
+
}
|
|
4371
|
+
function renderPreviewProbe(shellOrigin, captureEnabled = false) {
|
|
4372
|
+
return String.raw`(() => {
|
|
4373
|
+
const host = ${JSON.stringify(shellOrigin)};
|
|
4374
|
+
const report = (event) => parent.postMessage({ type: 'dbx:preview-event', event }, host);
|
|
4375
|
+
let captureDetails = ${true === captureEnabled ? 'true' : 'false'};
|
|
4376
|
+
addEventListener('message', ({ source, origin, data }) => {
|
|
4377
|
+
if (source === parent && origin === host && data?.type === 'dbx:capture-request-details') {
|
|
4378
|
+
captureDetails = data.enabled === true;
|
|
4379
|
+
}
|
|
4380
|
+
});
|
|
4381
|
+
parent.postMessage({ type: 'dbx:probe-ready' }, host);
|
|
4382
|
+
|
|
4383
|
+
const hidden = '[redacted]';
|
|
4384
|
+
const sensitive = /authorization|authentication|cookie|token|secret|password|passwd|passphrase|credential|apikey|privatekey|accesskey|sessionid|signature|sig$|securitytoken|xamzcredential|googleaccessid/i;
|
|
4385
|
+
const isSecret = (key) => sensitive.test(key.replace(/[^a-z0-9]/gi, ''));
|
|
4386
|
+
const clip = (value, limit) => (value.length > limit ? value.slice(0, limit - 1) + '…' : value);
|
|
4387
|
+
const cleanText = (text) =>
|
|
4388
|
+
text
|
|
4389
|
+
.replace(/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]+/gi, '$1 [redacted]')
|
|
4390
|
+
.replace(/\b([\w.-]*(?:token|secret|password|passwd|authorization|cookie|api[-_]?key)[\w.-]*)\s*[:=]\s*[^\s,;&]+/gi, '$1=[redacted]');
|
|
4391
|
+
|
|
4392
|
+
function pathOf(value) {
|
|
4393
|
+
try {
|
|
4394
|
+
const url = new URL(value, location.href);
|
|
4395
|
+
return url.protocol === 'https:' || url.protocol === 'http:' ? url.pathname : '';
|
|
4396
|
+
} catch { return ''; }
|
|
4397
|
+
}
|
|
4398
|
+
function safeRequestUrl(value) {
|
|
4399
|
+
try {
|
|
4400
|
+
const url = new URL(value, location.href);
|
|
4401
|
+
if (url.protocol === 'http:' || url.protocol === 'https:') return clip(url.origin + url.pathname, 4096);
|
|
4402
|
+
} catch {}
|
|
4403
|
+
}
|
|
4404
|
+
function queryFields(value) {
|
|
4405
|
+
try {
|
|
4406
|
+
const url = new URL(value, location.href);
|
|
4407
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.search.length > 16384) return;
|
|
4408
|
+
const fields = [];
|
|
4409
|
+
for (const [key, raw] of url.searchParams) {
|
|
4410
|
+
if (fields.length === 24) break;
|
|
4411
|
+
const shown = isSecret(key) ? hidden : /^\s*[\[{]/.test(raw) ? '[结构化数据]' : cleanText(raw.slice(0, 2048));
|
|
4412
|
+
fields.push([clip(key, 128), clip(shown, 512)]);
|
|
4413
|
+
}
|
|
4414
|
+
return fields;
|
|
4415
|
+
} catch {}
|
|
4416
|
+
}
|
|
4417
|
+
function headerValue(key, value) {
|
|
4418
|
+
if (isSecret(key)) return hidden;
|
|
4419
|
+
if (/^(?:location|content-location|referer)$/i.test(key)) return safeRequestUrl(value) || '[地址已省略]';
|
|
4420
|
+
if (/^(?:link|refresh)$/i.test(key)) return '[地址已省略]';
|
|
4421
|
+
return clip(cleanText(String(value).slice(0, 2048)), 512);
|
|
4422
|
+
}
|
|
4423
|
+
function snapshotHeaders(input) {
|
|
4424
|
+
const headers = [];
|
|
4425
|
+
const add = (key, value) => {
|
|
4426
|
+
if (typeof key === 'string' && ['string', 'number', 'boolean'].includes(typeof value) && headers.length < 24) {
|
|
4427
|
+
headers.push([clip(key, 128), headerValue(key, value)]);
|
|
4428
|
+
}
|
|
4429
|
+
};
|
|
4430
|
+
if (input instanceof Headers) {
|
|
4431
|
+
for (const [k, v] of Headers.prototype.entries.call(input)) {
|
|
4432
|
+
add(k, v);
|
|
4433
|
+
if (headers.length === 24) break;
|
|
4434
|
+
}
|
|
4435
|
+
} else if (Array.isArray(input)) {
|
|
4436
|
+
for (let i = 0; i < Math.min(input.length, 24); i++) {
|
|
4437
|
+
const pair = Object.getOwnPropertyDescriptor(input, i)?.value;
|
|
4438
|
+
if (Array.isArray(pair)) add(Object.getOwnPropertyDescriptor(pair, 0)?.value, Object.getOwnPropertyDescriptor(pair, 1)?.value);
|
|
4439
|
+
}
|
|
4440
|
+
} else if (input && (Object.getPrototypeOf(input) === Object.prototype || Object.getPrototypeOf(input) === null)) {
|
|
4441
|
+
for (const k of Object.keys(input).slice(0, 24)) add(k, Object.getOwnPropertyDescriptor(input, k)?.value);
|
|
4442
|
+
}
|
|
4443
|
+
return headers;
|
|
4444
|
+
}
|
|
4445
|
+
function formText(entries) {
|
|
4446
|
+
const out = [];
|
|
4447
|
+
for (const [key, value] of entries) {
|
|
4448
|
+
if (out.length === 30) { out.push('…'); break; }
|
|
4449
|
+
const shown = isSecret(key) ? hidden : typeof value === 'string' ? clip(cleanText(value.slice(0, 4096)), 1024) : '[文件]';
|
|
4450
|
+
out.push(clip(key, 128) + ': ' + shown);
|
|
4451
|
+
}
|
|
4452
|
+
return clip(out.join('\n'), 4096);
|
|
4453
|
+
}
|
|
4454
|
+
const bodyLimit = 16384;
|
|
4455
|
+
const largeResponse = '响应体较大,已省略';
|
|
4456
|
+
const binaryResponse = '二进制响应,未读取';
|
|
4457
|
+
const unreadableResponse = '无法读取响应体';
|
|
4458
|
+
const textResponse = (type) =>
|
|
4459
|
+
/^(?:text\/|application\/(?:json|[^;\s]+\+json|xml|[^;\s]+\+xml|javascript|x-www-form-urlencoded)(?:\s*;|$))/i.test(type) &&
|
|
4460
|
+
!/^text\/event-stream/i.test(type);
|
|
4461
|
+
function bodyText(body, contentType, label = '请求体') {
|
|
4462
|
+
if (body == null) return '';
|
|
4463
|
+
if (body instanceof URLSearchParams) return formText(URLSearchParams.prototype.entries.call(body));
|
|
4464
|
+
if (body instanceof FormData) return formText(FormData.prototype.entries.call(body));
|
|
4465
|
+
if (body instanceof ReadableStream) return '流式请求体,未读取';
|
|
4466
|
+
if (typeof body !== 'string') return '文件或二进制' + label + ',未读取';
|
|
4467
|
+
if (body.length > bodyLimit) return label + '较大,已省略';
|
|
4468
|
+
if (/json/i.test(contentType) || /^\s*[\[{]/.test(body)) {
|
|
4469
|
+
try {
|
|
4470
|
+
const safe = JSON.stringify(JSON.parse(body), (k, v) => (isSecret(k) ? hidden : typeof v === 'string' ? cleanText(v) : v), 2);
|
|
4471
|
+
return clip(safe, 4096);
|
|
4472
|
+
} catch { return 'JSON 格式无效,已省略' + label; }
|
|
4473
|
+
}
|
|
4474
|
+
if (/application\/x-www-form-urlencoded/i.test(contentType)) return formText(new URLSearchParams(body));
|
|
4475
|
+
return clip(cleanText(body), 4096);
|
|
4476
|
+
}
|
|
4477
|
+
function snapshotRequest(inputHeaders, body) {
|
|
4478
|
+
try {
|
|
4479
|
+
const headers = snapshotHeaders(inputHeaders);
|
|
4480
|
+
const contentType = headers.find(([k]) => k.toLowerCase() === 'content-type')?.[1] || '';
|
|
4481
|
+
return { headers, body: bodyText(body, contentType) };
|
|
4482
|
+
} catch { return { headers: [], body: '未能读取请求详情' }; }
|
|
4483
|
+
}
|
|
4484
|
+
async function readFetchBody(response) {
|
|
4485
|
+
if (!response.body) return '';
|
|
4486
|
+
const type = response.headers.get('content-type') || '';
|
|
4487
|
+
if (/^text\/event-stream/i.test(type)) return '事件流响应,未读取';
|
|
4488
|
+
if (!textResponse(type)) return type ? binaryResponse : '响应类型未知,未读取';
|
|
4489
|
+
if (Number(response.headers.get('content-length')) > bodyLimit) return largeResponse;
|
|
4490
|
+
const reader = response.clone().body.getReader();
|
|
4491
|
+
let timedOut = false;
|
|
4492
|
+
const cancel = () => { void reader.cancel().catch(() => {}); };
|
|
4493
|
+
const timeout = setTimeout(() => { timedOut = true; cancel(); }, 1500);
|
|
4494
|
+
try {
|
|
4495
|
+
const decoder = new TextDecoder();
|
|
4496
|
+
let bytes = 0, text = '';
|
|
4497
|
+
for (;;) {
|
|
4498
|
+
const { value, done } = await reader.read();
|
|
4499
|
+
if (timedOut) return '响应读取超时';
|
|
4500
|
+
if (done) return bodyText(text + decoder.decode(), type, '响应体');
|
|
4501
|
+
bytes += value.byteLength;
|
|
4502
|
+
if (bytes > bodyLimit) { cancel(); return largeResponse; }
|
|
4503
|
+
text += decoder.decode(value, { stream: true });
|
|
4504
|
+
}
|
|
4505
|
+
} finally { clearTimeout(timeout); reader.releaseLock(); }
|
|
4506
|
+
}
|
|
4507
|
+
async function fetchResponse(response, details) {
|
|
4508
|
+
try {
|
|
4509
|
+
const responseHeaders = snapshotHeaders(response.headers);
|
|
4510
|
+
const opaque = response.type === 'opaque' || response.type === 'opaqueredirect';
|
|
4511
|
+
const responseBody = opaque ? '不透明响应,浏览器未提供正文' : await readFetchBody(response);
|
|
4512
|
+
return { ...details, responseHeaders, responseBody };
|
|
4513
|
+
} catch { return { ...details, responseBody: unreadableResponse }; }
|
|
4514
|
+
}
|
|
4515
|
+
function previewJson(value) {
|
|
4516
|
+
let remaining = 512;
|
|
4517
|
+
const copy = (item, depth) => {
|
|
4518
|
+
if (--remaining < 0 || depth > 32) throw new Error();
|
|
4519
|
+
if (item === null || typeof item !== 'object') {
|
|
4520
|
+
if (!['string', 'number', 'boolean'].includes(typeof item) && item !== null) throw new Error();
|
|
4521
|
+
return typeof item === 'string' ? clip(cleanText(item.slice(0, bodyLimit)), 4096) : item;
|
|
4522
|
+
}
|
|
4523
|
+
const arr = Array.isArray(item);
|
|
4524
|
+
if (!arr && Object.getPrototypeOf(item) !== Object.prototype && Object.getPrototypeOf(item) !== null) throw new Error();
|
|
4525
|
+
const out = arr ? [] : Object.create(null);
|
|
4526
|
+
if (arr && item.length > remaining) throw new Error();
|
|
4527
|
+
for (const k in item) {
|
|
4528
|
+
const p = Object.getOwnPropertyDescriptor(item, k);
|
|
4529
|
+
if (!p) continue;
|
|
4530
|
+
if (!('value' in p) || k === 'toJSON' || (arr && !/^\d+$/.test(k))) throw new Error();
|
|
4531
|
+
out[k] = isSecret(k) ? hidden : copy(p.value, depth + 1);
|
|
4532
|
+
}
|
|
4533
|
+
return out;
|
|
4534
|
+
};
|
|
4535
|
+
try { return clip(JSON.stringify(copy(value, 0), null, 2), 4096); }
|
|
4536
|
+
catch { return '响应对象过大或已改变,已省略'; }
|
|
4537
|
+
}
|
|
4538
|
+
function xhrResponse(xhr, details, loaded) {
|
|
4539
|
+
try {
|
|
4540
|
+
const headers = xhr.getAllResponseHeaders().split('\r\n', 24).flatMap((line) => {
|
|
4541
|
+
const i = line.indexOf(':');
|
|
4542
|
+
return i > 0 ? [[line.slice(0, i), line.slice(i + 1).trim()]] : [];
|
|
4543
|
+
});
|
|
4544
|
+
const type = xhr.getResponseHeader('content-type') || '';
|
|
4545
|
+
let responseBody;
|
|
4546
|
+
if (!xhr.status) responseBody = '未取得 HTTP 响应';
|
|
4547
|
+
else if (loaded > bodyLimit || Number(xhr.getResponseHeader('content-length')) > bodyLimit) responseBody = largeResponse;
|
|
4548
|
+
else if (xhr.responseType === 'json') responseBody = xhr.response === null ? (loaded ? 'null(或无效的 JSON)' : '') : previewJson(xhr.response);
|
|
4549
|
+
else if (xhr.responseType && xhr.responseType !== 'text') responseBody = binaryResponse;
|
|
4550
|
+
else if (/^text\/event-stream/i.test(type)) responseBody = '事件流响应,未读取';
|
|
4551
|
+
else if (!textResponse(type)) responseBody = !loaded ? '' : type ? binaryResponse : '响应类型未知,未读取';
|
|
4552
|
+
else responseBody = bodyText(xhr.responseText, type, '响应体');
|
|
4553
|
+
return { ...details, responseHeaders: snapshotHeaders(headers), responseBody };
|
|
4554
|
+
} catch { return { ...details, responseBody: unreadableResponse }; }
|
|
4555
|
+
}
|
|
4556
|
+
function fetchDetails(init, request) {
|
|
4557
|
+
if (!captureDetails) return;
|
|
4558
|
+
try {
|
|
4559
|
+
const own = (key, fallback) => {
|
|
4560
|
+
if (!init || !(key in init)) return fallback;
|
|
4561
|
+
const p = Object.getOwnPropertyDescriptor(init, key);
|
|
4562
|
+
if (!p || !('value' in p)) throw new Error();
|
|
4563
|
+
return p.value ?? fallback;
|
|
4564
|
+
};
|
|
4565
|
+
return snapshotRequest(own('headers', request?.headers), own('body', request?.body));
|
|
4566
|
+
} catch { return { headers: [], body: '未能读取请求详情' }; }
|
|
4567
|
+
}
|
|
4568
|
+
|
|
4569
|
+
const probeUrl = document.currentScript?.src;
|
|
4570
|
+
const NativeError = Error;
|
|
4571
|
+
const nativeStackGetter = Object.getOwnPropertyDescriptor(new NativeError(), 'stack')?.get;
|
|
4572
|
+
const cleanDiagnostic = (text, limit) =>
|
|
4573
|
+
cleanText(
|
|
4574
|
+
text.slice(0, limit * 4)
|
|
4575
|
+
.replace(/((?:https?:\/\/|\/)[^\s"'<>?#)]+)[?#][^\s"'<>)]*/gi, (url, base) => base + (url.match(/:\d+:\d+$/)?.[0] || ''))
|
|
4576
|
+
.replace(/(https?:\/\/)[^/\s]+@/gi, '$1')
|
|
4577
|
+
).slice(0, limit);
|
|
4578
|
+
function describe(value, fallback) {
|
|
4579
|
+
if (typeof value !== 'string' || !value.trim()) return fallback;
|
|
4580
|
+
const trimmed = value.trim();
|
|
4581
|
+
if ((trimmed.startsWith('{') && trimmed.endsWith('}')) || (trimmed.startsWith('[') && trimmed.endsWith(']'))) return fallback;
|
|
4582
|
+
const structured = value.search(/\{\s*"|\[\s*"[^"]*"\s*,/);
|
|
4583
|
+
return cleanDiagnostic(structured < 0 ? value : value.slice(0, structured) + '[结构化数据]', 1000);
|
|
4584
|
+
}
|
|
4585
|
+
function argText(value) {
|
|
4586
|
+
if (value === null) return 'null';
|
|
4587
|
+
if (typeof value === 'string') return describe(value, '[文本]');
|
|
4588
|
+
if (['number', 'boolean', 'bigint', 'undefined', 'symbol'].includes(typeof value)) return String(value);
|
|
4589
|
+
if (typeof value === 'function') return '[函数]';
|
|
4590
|
+
if (value instanceof NativeError) return describe(Object.getOwnPropertyDescriptor(value, 'message')?.value, 'Error');
|
|
4591
|
+
if (Array.isArray(value)) return 'Array(' + Object.getOwnPropertyDescriptor(value, 'length').value + ')';
|
|
4592
|
+
const keys = [];
|
|
4593
|
+
for (const k in value) {
|
|
4594
|
+
if (Object.hasOwn(value, k)) keys.push(clip(k, 40));
|
|
4595
|
+
if (keys.length === 5) { keys.push('…'); break; }
|
|
4596
|
+
}
|
|
4597
|
+
return '{' + keys.join(', ') + '}';
|
|
4598
|
+
}
|
|
4599
|
+
function errorStack(error) {
|
|
4600
|
+
try {
|
|
4601
|
+
if (!(error instanceof NativeError)) return;
|
|
4602
|
+
const d = Object.getOwnPropertyDescriptor(error, 'stack');
|
|
4603
|
+
let stack = d?.value;
|
|
4604
|
+
if (d?.get && d.get === nativeStackGetter) {
|
|
4605
|
+
const fmt = Object.getOwnPropertyDescriptor(NativeError, 'prepareStackTrace');
|
|
4606
|
+
if (fmt?.value || fmt?.get) return;
|
|
4607
|
+
let at = error;
|
|
4608
|
+
for (let depth = 0; at && depth < 12; depth++, at = Object.getPrototypeOf(at)) {
|
|
4609
|
+
if (['name', 'message'].some((k) => {
|
|
4610
|
+
const p = Object.getOwnPropertyDescriptor(at, k);
|
|
4611
|
+
return p && (p.get || (typeof p.value === 'object' && p.value !== null) || typeof p.value === 'function');
|
|
4612
|
+
})) return;
|
|
4613
|
+
}
|
|
4614
|
+
if (at) return;
|
|
4615
|
+
stack = nativeStackGetter.call(error);
|
|
4616
|
+
}
|
|
4617
|
+
if (typeof stack !== 'string') return;
|
|
4618
|
+
const msg = Object.getOwnPropertyDescriptor(error, 'message')?.value;
|
|
4619
|
+
if (typeof msg === 'string' && msg.length) {
|
|
4620
|
+
const start = stack.indexOf(msg);
|
|
4621
|
+
if (start === 0 || (start > 0 && start < 128 && /^[^\r\n]*: $/.test(stack.slice(0, start)))) {
|
|
4622
|
+
stack = stack.slice(start + msg.length);
|
|
4623
|
+
}
|
|
4624
|
+
}
|
|
4625
|
+
const frames = stack.slice(0, 16384).split('\n').filter((line) =>
|
|
4626
|
+
(!probeUrl || !line.includes(probeUrl)) &&
|
|
4627
|
+
(/^\s*at\s+.+(?:\d|\)|<anonymous>|native)$/.test(line) || /^[^@\s]*@(?:[\w-]+:\/\/|\/).+:\d+(?::\d+)?$/.test(line)));
|
|
4628
|
+
return frames.length ? cleanDiagnostic(frames.join('\n'), 4096) : undefined;
|
|
4629
|
+
} catch { return undefined; }
|
|
4630
|
+
}
|
|
4631
|
+
|
|
4632
|
+
addEventListener('error', (event) => {
|
|
4633
|
+
if (event instanceof ErrorEvent) {
|
|
4634
|
+
report({ kind: 'error', message: describe(event.message, '脚本异常'), stack: errorStack(event.error),
|
|
4635
|
+
path: event.filename ? pathOf(event.filename) : location.pathname, line: event.lineno, column: event.colno });
|
|
4636
|
+
return;
|
|
4637
|
+
}
|
|
4638
|
+
const el = event.target;
|
|
4639
|
+
if (!(el instanceof HTMLElement)) return;
|
|
4640
|
+
const src = el.currentSrc || el.src || el.href;
|
|
4641
|
+
if (!src || typeof src !== 'string') return;
|
|
4642
|
+
const path = pathOf(src);
|
|
4643
|
+
if (path && new URL(src, location.href).origin !== location.origin) {
|
|
4644
|
+
report({ kind: 'resource', path, message: el.localName + ' 资源加载失败' });
|
|
4645
|
+
}
|
|
4646
|
+
}, true);
|
|
4647
|
+
addEventListener('securitypolicyviolation', (event) => {
|
|
4648
|
+
const path = /^https?:\/\//i.test(event.blockedURI) ? pathOf(event.blockedURI) : '';
|
|
4649
|
+
const label = event.disposition === 'report' ? 'CSP 报告' : 'CSP 拦截';
|
|
4650
|
+
const target = ['eval', 'inline', 'wasm-eval'].includes(event.blockedURI) ? ' (' + event.blockedURI + ')' : '';
|
|
4651
|
+
report({ kind: 'console', level: 'warn', path: event.sourceFile ? pathOf(event.sourceFile) : location.pathname,
|
|
4652
|
+
line: event.lineNumber, column: event.columnNumber,
|
|
4653
|
+
message: label + ' · ' + event.effectiveDirective + target + (path ? ' · ' + path : '') });
|
|
4654
|
+
});
|
|
4655
|
+
addEventListener('unhandledrejection', (event) => {
|
|
4656
|
+
let message = 'Promise 被拒绝';
|
|
4657
|
+
try { message = argText(event.reason); } catch {}
|
|
4658
|
+
report({ kind: 'unhandledrejection', message: cleanDiagnostic(message, 1000), stack: errorStack(event.reason), path: location.pathname });
|
|
4659
|
+
});
|
|
4660
|
+
for (const level of ['log', 'info', 'debug', 'warn', 'error']) {
|
|
4661
|
+
const original = console[level];
|
|
4662
|
+
if (typeof original !== 'function') continue;
|
|
4663
|
+
console[level] = function (...args) {
|
|
4664
|
+
const event = { kind: 'console', level, message: 'console.' + level, path: location.pathname };
|
|
4665
|
+
try {
|
|
4666
|
+
const preview = args.slice(0, 8);
|
|
4667
|
+
const error = preview.find((a) => a instanceof NativeError);
|
|
4668
|
+
const summary = preview.map((a, i) => (i && typeof args[i - 1] === 'string' && isSecret(args[i - 1]) ? hidden : argText(a))).join(' ');
|
|
4669
|
+
event.message = cleanDiagnostic(summary || event.message, 1000);
|
|
4670
|
+
if (error) event.stack = errorStack(error);
|
|
4671
|
+
else if (level === 'warn' || level === 'error') event.stack = errorStack(new NativeError());
|
|
4672
|
+
} catch {}
|
|
4673
|
+
try { report(event); } catch {}
|
|
4674
|
+
return Reflect.apply(original, this, args);
|
|
4675
|
+
};
|
|
4676
|
+
}
|
|
4677
|
+
|
|
4678
|
+
function recordRequest(method, path, status, durationMs, message, details) {
|
|
4679
|
+
const event = { kind: 'network', method, path, status, durationMs: Math.round(durationMs), message };
|
|
4680
|
+
if (details) {
|
|
4681
|
+
if (!status && details.responseBody === undefined) {
|
|
4682
|
+
details = { ...details, responseHeaders: [], responseBody: '未取得可读取的 HTTP 响应' };
|
|
4683
|
+
}
|
|
4684
|
+
event.requestDetails = details;
|
|
4685
|
+
if (new TextEncoder().encode(JSON.stringify(event)).length > 46000) {
|
|
4686
|
+
const compact = (pairs) => pairs?.slice(0, 8).map(([k, v]) => [clip(k, 48), clip(v, 64)]);
|
|
4687
|
+
event.requestDetails = {
|
|
4688
|
+
...details, query: compact(details.query), headers: compact(details.headers),
|
|
4689
|
+
responseHeaders: compact(details.responseHeaders), body: clip(details.body, 1024),
|
|
4690
|
+
responseBody: details.responseBody === undefined ? undefined : clip(details.responseBody, 1024)
|
|
4691
|
+
};
|
|
4692
|
+
}
|
|
4693
|
+
}
|
|
4694
|
+
report(event);
|
|
4695
|
+
}
|
|
4696
|
+
const originalFetch = window.fetch;
|
|
4697
|
+
window.fetch = async function (input, init) {
|
|
4698
|
+
const started = performance.now();
|
|
4699
|
+
const req = input instanceof Request ? input : undefined;
|
|
4700
|
+
const method = String(init?.method || req?.method || 'GET').toUpperCase();
|
|
4701
|
+
const target = req ? req.url : input;
|
|
4702
|
+
const path = pathOf(target);
|
|
4703
|
+
const details = fetchDetails(init, req);
|
|
4704
|
+
if (details) { details.url = safeRequestUrl(target); details.query = queryFields(target); }
|
|
4705
|
+
let response;
|
|
4706
|
+
try { response = await Reflect.apply(originalFetch, this, arguments); }
|
|
4707
|
+
catch (error) {
|
|
4708
|
+
recordRequest(method, path, 0, performance.now() - started, error?.name === 'AbortError' ? '请求已取消' : '网络请求失败', details);
|
|
4709
|
+
throw error;
|
|
4710
|
+
}
|
|
4711
|
+
const duration = performance.now() - started;
|
|
4712
|
+
const message = response.type === 'opaque' ? '不透明响应' : undefined;
|
|
4713
|
+
if (details) void fetchResponse(response, details).then((done) => recordRequest(method, path, response.status, duration, message, done));
|
|
4714
|
+
else recordRequest(method, path, response.status, duration, message);
|
|
4715
|
+
return response;
|
|
4716
|
+
};
|
|
4717
|
+
const xhrMap = new WeakMap();
|
|
4718
|
+
const { open, send, setRequestHeader } = XMLHttpRequest.prototype;
|
|
4719
|
+
XMLHttpRequest.prototype.open = function (method, url) {
|
|
4720
|
+
const res = Reflect.apply(open, this, arguments);
|
|
4721
|
+
xhrMap.set(this, { method: String(method).toUpperCase(), path: pathOf(url), url: safeRequestUrl(url), query: captureDetails ? queryFields(url) : undefined, headers: [] });
|
|
4722
|
+
return res;
|
|
4723
|
+
};
|
|
4724
|
+
XMLHttpRequest.prototype.setRequestHeader = function (name, value) {
|
|
4725
|
+
const res = Reflect.apply(setRequestHeader, this, arguments);
|
|
4726
|
+
const req = xhrMap.get(this);
|
|
4727
|
+
if (captureDetails && req && req.headers.length < 24 && typeof name === 'string' && typeof value === 'string') {
|
|
4728
|
+
req.headers.push([clip(name, 128), headerValue(name, value)]);
|
|
4729
|
+
}
|
|
4730
|
+
return res;
|
|
4731
|
+
};
|
|
4732
|
+
XMLHttpRequest.prototype.send = function () {
|
|
4733
|
+
const req = xhrMap.get(this);
|
|
4734
|
+
if (!req) return Reflect.apply(send, this, arguments);
|
|
4735
|
+
const started = performance.now();
|
|
4736
|
+
const details = captureDetails ? snapshotRequest(req.headers, arguments[0]) : undefined;
|
|
4737
|
+
if (details) { details.url = req.url; details.query = req.query; }
|
|
4738
|
+
const finished = (event) => {
|
|
4739
|
+
recordRequest(req.method, req.path, this.status, performance.now() - started,
|
|
4740
|
+
this.status === 0 ? '请求未取得 HTTP 响应' : undefined, details && xhrResponse(this, details, event.loaded));
|
|
4741
|
+
};
|
|
4742
|
+
this.addEventListener('loadend', finished, { once: true });
|
|
4743
|
+
try { return Reflect.apply(send, this, arguments); }
|
|
4744
|
+
catch (error) {
|
|
4745
|
+
this.removeEventListener('loadend', finished);
|
|
4746
|
+
recordRequest(req.method, req.path, 0, performance.now() - started, '请求未能发出', details);
|
|
4747
|
+
throw error;
|
|
4748
|
+
}
|
|
4749
|
+
};
|
|
4750
|
+
})();`;
|
|
4751
|
+
}
|
|
4752
|
+
const workbench_WEB_ROOT = fileURLToPath(new URL('./web', import.meta.url));
|
|
4753
|
+
const MAX_EVENT_BYTES = 49152;
|
|
4754
|
+
const PROBE_PATH = '/__dbx/probe.js';
|
|
4755
|
+
const CONTENT_TYPES = {
|
|
4756
|
+
'.html': 'text/html; charset=utf-8',
|
|
4757
|
+
'.htm': 'text/html; charset=utf-8',
|
|
4758
|
+
'.js': "text/javascript; charset=utf-8",
|
|
4759
|
+
'.mjs': "text/javascript; charset=utf-8",
|
|
4760
|
+
'.css': 'text/css; charset=utf-8',
|
|
4761
|
+
'.json': 'application/json',
|
|
4762
|
+
'.svg': 'image/svg+xml',
|
|
4763
|
+
'.png': 'image/png',
|
|
4764
|
+
'.jpg': 'image/jpeg',
|
|
4765
|
+
'.jpeg': 'image/jpeg',
|
|
4766
|
+
'.gif': 'image/gif',
|
|
4767
|
+
'.webp': 'image/webp',
|
|
4768
|
+
'.ico': 'image/x-icon',
|
|
4769
|
+
'.woff': 'font/woff',
|
|
4770
|
+
'.woff2': 'font/woff2',
|
|
4771
|
+
'.ttf': 'font/ttf'
|
|
4772
|
+
};
|
|
4773
|
+
const DOCUMENT_START = /^(?:\uFEFF|\s|<!--[\s\S]*?-->|<!doctype\b[^>]*>|<(?:html|head)\b(?:[^>"']|"[^"]*"|'[^']*')*>)*/i;
|
|
4774
|
+
function cspSources(domains) {
|
|
4775
|
+
return (domains ?? []).map((domain)=>{
|
|
4776
|
+
if (!/^(?:https?|wss?):\/\/(?:\*\.)?(?:[a-z\d.-]+|\[[\da-f:]+\])(?::\d+)?\/?$/i.test(domain)) throw new Error('工作台 CSP 域名须为有效的 HTTP(S) 或 WS(S) 来源。');
|
|
4777
|
+
return new URL(domain).origin;
|
|
4778
|
+
}).join(' ');
|
|
4779
|
+
}
|
|
4780
|
+
function buildGuestPolicy(entry, shellOrigin) {
|
|
4781
|
+
const resources = cspSources(entry.csp?.resourceDomains);
|
|
4782
|
+
return [
|
|
4783
|
+
"default-src 'none'",
|
|
4784
|
+
`script-src 'self' 'unsafe-inline' ${resources}`,
|
|
4785
|
+
`style-src 'self' 'unsafe-inline' ${resources}`,
|
|
4786
|
+
`img-src 'self' data: ${resources}`,
|
|
4787
|
+
`media-src 'self' ${resources}`,
|
|
4788
|
+
`font-src 'self' ${resources}`,
|
|
4789
|
+
`connect-src 'self' ${cspSources(entry.csp?.connectDomains)}`,
|
|
4790
|
+
`frame-src ${cspSources(entry.csp?.frameDomains) || "'none'"}`,
|
|
4791
|
+
`base-uri 'self' ${cspSources(entry.csp?.baseUriDomains)}`,
|
|
4792
|
+
`frame-ancestors ${shellOrigin}`,
|
|
4793
|
+
"object-src 'none'",
|
|
4794
|
+
"form-action 'none'"
|
|
4795
|
+
].join('; ');
|
|
4796
|
+
}
|
|
4797
|
+
function parseEntry(entry) {
|
|
4798
|
+
if (!entry.path.startsWith('web/') || /[?#\\]/.test(entry.path) || Array.from(entry.path).some((ch)=>ch.charCodeAt(0) < 32) || entry.path.split('/').some((segment)=>!segment || '.' === segment || '..' === segment) || !/\.html?$/i.test(entry.path)) throw new Error('工作台首页须为 web/ 目录下的 HTML 文件。');
|
|
4799
|
+
return {
|
|
4800
|
+
decodedPath: `/${entry.path}`,
|
|
4801
|
+
encodedPath: `/${entry.path.split('/').map(encodeURIComponent).join('/')}`
|
|
4802
|
+
};
|
|
4803
|
+
}
|
|
4804
|
+
async function resolveStaticFile(workbenchDir, decodedPath) {
|
|
4805
|
+
if (!decodedPath.startsWith('/web/') || /[\\\0]/.test(decodedPath) || decodedPath.split('/').some((segment)=>'.' === segment || '..' === segment)) return;
|
|
4806
|
+
const webRoot = join(workbenchDir, 'web');
|
|
4807
|
+
if (await realpath(webRoot) !== webRoot) return;
|
|
4808
|
+
const file = await realpath(external_node_path_resolve(workbenchDir, `.${decodedPath}`));
|
|
4809
|
+
const within = relative(webRoot, file);
|
|
4810
|
+
if (!within || '..' === within || within.startsWith(`..${sep}`) || external_node_path_resolve(webRoot, within) !== file) return;
|
|
4811
|
+
return (await stat(file)).isFile() ? file : void 0;
|
|
4812
|
+
}
|
|
4813
|
+
function createLoopbackServer(handler) {
|
|
4814
|
+
return createServer({
|
|
4815
|
+
requestTimeout: 15000
|
|
4816
|
+
}, (req, res)=>{
|
|
4817
|
+
if (req.headers.host !== `${HOST}:${req.socket.localPort}` || !req.url?.startsWith('/') || req.url.startsWith('//') || req.url.includes('\\')) return void res.writeHead(400).end();
|
|
4818
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
4819
|
+
res.setHeader('Referrer-Policy', 'no-referrer');
|
|
4820
|
+
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
4821
|
+
let url;
|
|
4822
|
+
try {
|
|
4823
|
+
url = new URL(req.url, `http://${req.headers.host}`);
|
|
4824
|
+
decodeURIComponent(url.pathname);
|
|
4825
|
+
} catch {
|
|
4826
|
+
res.writeHead(400).end();
|
|
4827
|
+
return;
|
|
4828
|
+
}
|
|
4829
|
+
handler(req, res, url).catch(()=>{
|
|
4830
|
+
if (res.destroyed) return;
|
|
4831
|
+
if (res.headersSent) return void res.destroy();
|
|
4832
|
+
res.writeHead(500);
|
|
4833
|
+
res.end('本地预览请求失败');
|
|
4834
|
+
});
|
|
4835
|
+
});
|
|
4836
|
+
}
|
|
4837
|
+
async function readJsonBody(req) {
|
|
4838
|
+
if (req.headers['content-type']?.split(';')[0].trim().toLowerCase() !== 'application/json') return {
|
|
4839
|
+
ok: false,
|
|
4840
|
+
status: 415
|
|
4841
|
+
};
|
|
4842
|
+
let size = 0;
|
|
4843
|
+
const chunks = [];
|
|
4844
|
+
for await (const chunk of req){
|
|
4845
|
+
size += chunk.length;
|
|
4846
|
+
if (size > MAX_EVENT_BYTES) return {
|
|
4847
|
+
ok: false,
|
|
4848
|
+
status: 413
|
|
4849
|
+
};
|
|
4850
|
+
chunks.push(chunk);
|
|
4851
|
+
}
|
|
4852
|
+
try {
|
|
4853
|
+
return {
|
|
4854
|
+
ok: true,
|
|
4855
|
+
data: JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
4856
|
+
};
|
|
4857
|
+
} catch {
|
|
4858
|
+
return {
|
|
4859
|
+
ok: false,
|
|
4860
|
+
status: 400
|
|
4861
|
+
};
|
|
4862
|
+
}
|
|
4863
|
+
}
|
|
4864
|
+
async function startWorkbenchPreview(options) {
|
|
4865
|
+
const { workbenchDir: rawDir, initialEntry, port = 0, open: shouldOpen = false, signal, log } = options;
|
|
4866
|
+
signal?.throwIfAborted();
|
|
4867
|
+
if (!existsSync(workbench_WEB_ROOT)) throw new Error(`本地预览界面资源缺失:${workbench_WEB_ROOT}`);
|
|
4868
|
+
const workbenchDir = await realpath(rawDir);
|
|
4869
|
+
let entry = initialEntry;
|
|
4870
|
+
let parsed = parseEntry(entry);
|
|
4871
|
+
buildGuestPolicy(entry, `http://${HOST}`);
|
|
4872
|
+
let policy = '';
|
|
4873
|
+
let shellOrigin = '';
|
|
4874
|
+
let guestOrigin = '';
|
|
4875
|
+
const token = randomBytes(32).toString('base64url');
|
|
4876
|
+
const diagnostics = new PreviewDiagnostics();
|
|
4877
|
+
const serveShellStatic = createStaticMiddleware(workbench_WEB_ROOT);
|
|
4878
|
+
const shell = createLoopbackServer(async (req, res, url)=>{
|
|
4879
|
+
if (url.pathname === INSPECTOR_CONFIG_PATH && 'GET' === req.method) {
|
|
4880
|
+
const config = {
|
|
4881
|
+
mode: 'workbench',
|
|
4882
|
+
guestUrl: guestOrigin + parsed.encodedPath,
|
|
4883
|
+
entryPath: entry.path,
|
|
4884
|
+
captureEnabled: diagnostics.captureRequestDetails
|
|
4885
|
+
};
|
|
4886
|
+
res.writeHead(200, {
|
|
4887
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
4888
|
+
}).end(JSON.stringify(config));
|
|
4889
|
+
return;
|
|
4890
|
+
}
|
|
4891
|
+
if (url.pathname === WORKBENCH_EVENTS_PATH || url.pathname === WORKBENCH_CAPTURE_PATH || url.pathname === WORKBENCH_DIAGNOSTICS_PATH) {
|
|
4892
|
+
if (req.headers.authorization !== `Bearer ${token}` || void 0 !== req.headers.origin && req.headers.origin !== shellOrigin) return void res.writeHead(403).end();
|
|
4893
|
+
if (url.pathname === WORKBENCH_EVENTS_PATH && 'POST' === req.method) {
|
|
4894
|
+
const parsedBody = await readJsonBody(req);
|
|
4895
|
+
if (!parsedBody.ok) return void res.writeHead(parsedBody.status).end();
|
|
4896
|
+
res.writeHead(diagnostics.add(parsedBody.data) ? 204 : 400).end();
|
|
4897
|
+
return;
|
|
4898
|
+
}
|
|
4899
|
+
if (url.pathname === WORKBENCH_CAPTURE_PATH && ('GET' === req.method || 'PUT' === req.method)) {
|
|
4900
|
+
if ('PUT' === req.method) {
|
|
4901
|
+
const parsedBody = await readJsonBody(req);
|
|
4902
|
+
if (!parsedBody.ok) return void res.writeHead(parsedBody.status).end();
|
|
4903
|
+
const body = parsedBody.data;
|
|
4904
|
+
if (!body || 'object' != typeof body || !('enabled' in body) || 'boolean' != typeof body.enabled) return void res.writeHead(400).end();
|
|
4905
|
+
diagnostics.captureRequestDetails = body.enabled;
|
|
4906
|
+
}
|
|
4907
|
+
res.writeHead(200, {
|
|
4908
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
4909
|
+
}).end(JSON.stringify({
|
|
4910
|
+
enabled: diagnostics.captureRequestDetails
|
|
4911
|
+
}));
|
|
4912
|
+
return;
|
|
4913
|
+
}
|
|
4914
|
+
if (url.pathname === WORKBENCH_DIAGNOSTICS_PATH && 'GET' === req.method) {
|
|
4915
|
+
const cursor = url.searchParams.get('after') ?? '0';
|
|
4916
|
+
if (!/^\d+$/.test(cursor) || !Number.isSafeInteger(Number(cursor))) return void res.writeHead(400).end();
|
|
4917
|
+
res.writeHead(200, {
|
|
4918
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
4919
|
+
}).end(JSON.stringify(diagnostics.read(Number(cursor))));
|
|
4920
|
+
return;
|
|
4921
|
+
}
|
|
4922
|
+
res.writeHead(405).end();
|
|
4923
|
+
return;
|
|
4924
|
+
}
|
|
4925
|
+
if ('/' !== url.pathname && '/index.html' !== url.pathname && extname(url.pathname)) res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
|
|
4926
|
+
else res.setHeader('Content-Security-Policy', [
|
|
4927
|
+
"default-src 'none'",
|
|
4928
|
+
"script-src 'self'",
|
|
4929
|
+
"style-src 'self' 'unsafe-inline'",
|
|
4930
|
+
"img-src 'self' data:",
|
|
4931
|
+
`frame-src ${guestOrigin}`,
|
|
4932
|
+
"connect-src 'self'",
|
|
4933
|
+
"base-uri 'none'",
|
|
4934
|
+
"frame-ancestors 'none'"
|
|
4935
|
+
].join('; '));
|
|
4936
|
+
await serveShellStatic(req, res, ()=>{
|
|
4937
|
+
res.writeHead(404).end();
|
|
4938
|
+
});
|
|
4939
|
+
});
|
|
4940
|
+
const guest = createLoopbackServer(async (req, res, url)=>{
|
|
4941
|
+
if ('GET' !== req.method && 'HEAD' !== req.method) return void res.writeHead(405, {
|
|
4942
|
+
Allow: 'GET, HEAD'
|
|
4943
|
+
}).end();
|
|
4944
|
+
if ('cross-site' === req.headers['sec-fetch-site']) return void res.writeHead(403).end();
|
|
4945
|
+
res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');
|
|
4946
|
+
res.setHeader('Content-Security-Policy', policy);
|
|
4947
|
+
const decodedPath = decodeURIComponent(url.pathname);
|
|
4948
|
+
if (decodedPath === PROBE_PATH) {
|
|
4949
|
+
res.writeHead(200, {
|
|
4950
|
+
'Content-Type': "text/javascript; charset=utf-8"
|
|
4951
|
+
});
|
|
4952
|
+
res.end('HEAD' === req.method ? void 0 : renderPreviewProbe(shellOrigin, diagnostics.captureRequestDetails));
|
|
4953
|
+
return;
|
|
4954
|
+
}
|
|
4955
|
+
const dest = req.headers['sec-fetch-dest'];
|
|
4956
|
+
const captureHeaders = diagnostics.captureRequestDetails;
|
|
4957
|
+
const started = performance.now();
|
|
4958
|
+
res.once('finish', ()=>{
|
|
4959
|
+
if ('empty' !== dest) diagnostics.add({
|
|
4960
|
+
kind: 'resource',
|
|
4961
|
+
method: req.method,
|
|
4962
|
+
path: decodedPath,
|
|
4963
|
+
status: res.statusCode,
|
|
4964
|
+
requestDetails: captureHeaders ? {
|
|
4965
|
+
headers: [
|
|
4966
|
+
'accept',
|
|
4967
|
+
'sec-fetch-dest',
|
|
4968
|
+
'sec-fetch-mode'
|
|
4969
|
+
].flatMap((name)=>{
|
|
4970
|
+
const value = req.headers[name];
|
|
4971
|
+
return 'string' == typeof value ? [
|
|
4972
|
+
[
|
|
4973
|
+
name,
|
|
4974
|
+
value.slice(0, 512)
|
|
4975
|
+
]
|
|
4976
|
+
] : [];
|
|
4977
|
+
}),
|
|
4978
|
+
responseHeaders: [
|
|
4979
|
+
'content-type',
|
|
4980
|
+
'cache-control',
|
|
4981
|
+
'content-length'
|
|
4982
|
+
].flatMap((name)=>{
|
|
4983
|
+
const value = res.getHeader(name);
|
|
4984
|
+
return void 0 === value ? [] : [
|
|
4985
|
+
[
|
|
4986
|
+
name,
|
|
4987
|
+
String(value).slice(0, 512)
|
|
4988
|
+
]
|
|
4989
|
+
];
|
|
4990
|
+
}),
|
|
4991
|
+
body: ''
|
|
4992
|
+
} : void 0,
|
|
4993
|
+
durationMs: Math.round(performance.now() - started)
|
|
4994
|
+
});
|
|
4995
|
+
});
|
|
4996
|
+
const isEntry = decodedPath === parsed.decodedPath;
|
|
4997
|
+
const file = await resolveStaticFile(workbenchDir, decodedPath).catch((err)=>{
|
|
4998
|
+
if ('ENOENT' !== err.code && 'ENOTDIR' !== err.code && 'ELOOP' !== err.code) throw err;
|
|
4999
|
+
});
|
|
5000
|
+
if (!file) {
|
|
5001
|
+
res.writeHead(404, {
|
|
5002
|
+
'Content-Type': 'text/plain; charset=utf-8'
|
|
5003
|
+
});
|
|
5004
|
+
res.end(isEntry ? '未找到首页 HTML,请检查工作台 Manifest 的入口路径,保存后刷新。' : '未找到预览资源');
|
|
5005
|
+
return;
|
|
5006
|
+
}
|
|
5007
|
+
res.setHeader('Content-Type', CONTENT_TYPES[extname(decodedPath).toLowerCase()] ?? 'application/octet-stream');
|
|
5008
|
+
if ('HEAD' === req.method) res.end();
|
|
5009
|
+
else if (isEntry) {
|
|
5010
|
+
const html = await readFile(file, 'utf8');
|
|
5011
|
+
res.end(html.replace(DOCUMENT_START, (start)=>`${start}<script src="${PROBE_PATH}"></script>`));
|
|
5012
|
+
} else await pipeline(createReadStream(file), res);
|
|
5013
|
+
});
|
|
5014
|
+
let closing;
|
|
5015
|
+
const close = ()=>closing ??= Promise.all([
|
|
5016
|
+
guest.listening ? closeServer(guest) : Promise.resolve(),
|
|
5017
|
+
shell.listening ? closeServer(shell) : Promise.resolve()
|
|
5018
|
+
]).then(()=>void 0);
|
|
5019
|
+
try {
|
|
5020
|
+
const shellPort = await listen(shell, port);
|
|
5021
|
+
shellOrigin = `http://${HOST}:${shellPort}`;
|
|
5022
|
+
policy = buildGuestPolicy(entry, shellOrigin);
|
|
5023
|
+
const guestPort = await listen(guest, 0);
|
|
5024
|
+
guestOrigin = `http://${HOST}:${guestPort}`;
|
|
5025
|
+
} catch (error) {
|
|
5026
|
+
await close();
|
|
5027
|
+
throw error;
|
|
5028
|
+
}
|
|
5029
|
+
if (signal?.aborted) {
|
|
5030
|
+
await close();
|
|
5031
|
+
signal.throwIfAborted();
|
|
5032
|
+
}
|
|
5033
|
+
signal?.addEventListener('abort', ()=>void close(), {
|
|
5034
|
+
once: true
|
|
5035
|
+
});
|
|
5036
|
+
const url = `${shellOrigin}/#token=${token}`;
|
|
5037
|
+
if (shouldOpen) try {
|
|
5038
|
+
const appWindow = await openAppWindow(url);
|
|
5039
|
+
if (!appWindow) await open_0(url);
|
|
5040
|
+
} catch (error) {
|
|
5041
|
+
log?.warn(`未能打开本地预览页面,请手动访问 ${url}:${error instanceof Error ? error.message : String(error)}`);
|
|
5042
|
+
}
|
|
5043
|
+
return {
|
|
5044
|
+
url,
|
|
5045
|
+
setEntry (next) {
|
|
5046
|
+
const nextParsed = parseEntry(next);
|
|
5047
|
+
const nextPolicy = buildGuestPolicy(next, shellOrigin);
|
|
5048
|
+
entry = next;
|
|
5049
|
+
parsed = nextParsed;
|
|
5050
|
+
policy = nextPolicy;
|
|
5051
|
+
},
|
|
5052
|
+
close
|
|
5053
|
+
};
|
|
5054
|
+
}
|
|
5055
|
+
export { startInspector, startWorkbenchPreview };
|