@brainervirus/workit-opencode 8.22.2 → 8.23.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/dist/plugin.js
CHANGED
|
@@ -964,8 +964,6 @@ var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/
|
|
|
964
964
|
var base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
965
965
|
var httpProtocol = /^https?$/;
|
|
966
966
|
var e164 = /^\+[1-9]\d{6,14}$/;
|
|
967
|
-
var creditCard = /^\d(?:[ -]?\d){11,18}$/;
|
|
968
|
-
var iban = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z0-9]{11,30}$/;
|
|
969
967
|
var 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])))`;
|
|
970
968
|
function anchor(source) {
|
|
971
969
|
return new RegExp(`^${source}$`);
|
|
@@ -1811,41 +1809,6 @@ var $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
|
|
|
1811
1809
|
def.pattern ?? (def.pattern = e164);
|
|
1812
1810
|
$ZodStringFormat.init(inst, def);
|
|
1813
1811
|
});
|
|
1814
|
-
var CC_SANITIZE = /[- ]/g;
|
|
1815
|
-
function isLuhnAlgo(digits) {
|
|
1816
|
-
let length = digits.length;
|
|
1817
|
-
let bit = 1;
|
|
1818
|
-
let sum = 0;
|
|
1819
|
-
while (length) {
|
|
1820
|
-
const value = digits.charCodeAt(--length) - 48;
|
|
1821
|
-
bit ^= 1;
|
|
1822
|
-
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
1823
|
-
}
|
|
1824
|
-
return sum % 10 === 0;
|
|
1825
|
-
}
|
|
1826
|
-
function isValidCreditCard(input) {
|
|
1827
|
-
if (!creditCard.test(input))
|
|
1828
|
-
return false;
|
|
1829
|
-
return isLuhnAlgo(input.replace(CC_SANITIZE, ""));
|
|
1830
|
-
}
|
|
1831
|
-
function isIso7064Mod97(iban2) {
|
|
1832
|
-
let remainder = 0;
|
|
1833
|
-
const len = iban2.length;
|
|
1834
|
-
for (let i = 4;i < len; i++) {
|
|
1835
|
-
const code = iban2.charCodeAt(i);
|
|
1836
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1837
|
-
}
|
|
1838
|
-
for (let i = 0;i < 4; i++) {
|
|
1839
|
-
const code = iban2.charCodeAt(i);
|
|
1840
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1841
|
-
}
|
|
1842
|
-
return remainder === 1;
|
|
1843
|
-
}
|
|
1844
|
-
function isValidIBAN(input) {
|
|
1845
|
-
if (!iban.test(input))
|
|
1846
|
-
return false;
|
|
1847
|
-
return isIso7064Mod97(input);
|
|
1848
|
-
}
|
|
1849
1812
|
function isValidJWT(token, algorithm = null) {
|
|
1850
1813
|
try {
|
|
1851
1814
|
const tokensParts = token.split(".");
|
|
@@ -3158,9 +3121,6 @@ function settle(inst, answer) {
|
|
|
3158
3121
|
recursive.set(inst, answer === PROVEN);
|
|
3159
3122
|
return answer;
|
|
3160
3123
|
}
|
|
3161
|
-
function isRecursiveSchema(inst) {
|
|
3162
|
-
return isRecursive(inst, new Set, true) !== NONE;
|
|
3163
|
-
}
|
|
3164
3124
|
function bucketFor(state, inst) {
|
|
3165
3125
|
let bucket = state.buckets.get(inst);
|
|
3166
3126
|
if (!bucket) {
|
|
@@ -3447,1604 +3407,6 @@ function registry() {
|
|
|
3447
3407
|
}
|
|
3448
3408
|
(_a2 = globalThis).__zod_globalRegistry ?? (_a2.__zod_globalRegistry = registry());
|
|
3449
3409
|
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
3450
|
-
// node_modules/zod/v4/core/compile.js
|
|
3451
|
-
var INVALID = Symbol.for("zod.compile.invalid");
|
|
3452
|
-
var FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
|
|
3453
|
-
|
|
3454
|
-
class ZodCompileAsyncError extends Error {
|
|
3455
|
-
constructor(message = "z.compile does not support async refinements, transforms, or checks") {
|
|
3456
|
-
super(message);
|
|
3457
|
-
this.name = "ZodCompileAsyncError";
|
|
3458
|
-
}
|
|
3459
|
-
}
|
|
3460
|
-
|
|
3461
|
-
class ZodCompileUnsupportedError extends Error {
|
|
3462
|
-
constructor(feature, islandable = true) {
|
|
3463
|
-
super(`z.compile does not support ${feature}; this schema must use the runtime parser`);
|
|
3464
|
-
this.name = "ZodCompileUnsupportedError";
|
|
3465
|
-
this.islandable = islandable;
|
|
3466
|
-
}
|
|
3467
|
-
}
|
|
3468
|
-
function compileValidator(schema, parser) {
|
|
3469
|
-
try {
|
|
3470
|
-
return compileFn(schema, { assertOnly: true });
|
|
3471
|
-
} catch {
|
|
3472
|
-
return parser;
|
|
3473
|
-
}
|
|
3474
|
-
}
|
|
3475
|
-
function compile(schema, options) {
|
|
3476
|
-
try {
|
|
3477
|
-
const parser = compileFn(schema);
|
|
3478
|
-
const clone = withParser(schema, parser);
|
|
3479
|
-
clone._zod.bag.validator = compileValidator(schema, parser);
|
|
3480
|
-
return clone;
|
|
3481
|
-
} catch (err) {
|
|
3482
|
-
if (options?.strict)
|
|
3483
|
-
throw err;
|
|
3484
|
-
return schema;
|
|
3485
|
-
}
|
|
3486
|
-
}
|
|
3487
|
-
function withParser(schema, parser) {
|
|
3488
|
-
if (isRecursiveSchema(schema)) {
|
|
3489
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3490
|
-
}
|
|
3491
|
-
const clone2 = clone(schema);
|
|
3492
|
-
const liveRun = schema._zod.run;
|
|
3493
|
-
const originalRun = liveRun.__originalRun ?? liveRun;
|
|
3494
|
-
const wrapped = (payload, ctx) => {
|
|
3495
|
-
if (ctx?.async || ctx?.direction === "backward" || ctx?.skipChecks || ctx?.[FALLBACK_FLAG]) {
|
|
3496
|
-
return originalRun(payload, ctx);
|
|
3497
|
-
}
|
|
3498
|
-
if (ctx && isBackEdge(ctx, payload.value)) {
|
|
3499
|
-
return originalRun(payload, ctx);
|
|
3500
|
-
}
|
|
3501
|
-
const out = parser(payload.value);
|
|
3502
|
-
if (out !== INVALID) {
|
|
3503
|
-
payload.value = out;
|
|
3504
|
-
return payload;
|
|
3505
|
-
}
|
|
3506
|
-
if (ctx)
|
|
3507
|
-
ctx[FALLBACK_FLAG] = true;
|
|
3508
|
-
return originalRun(payload, ctx);
|
|
3509
|
-
};
|
|
3510
|
-
wrapped.__originalRun = originalRun;
|
|
3511
|
-
clone2._zod.bag.fallbackRun = originalRun;
|
|
3512
|
-
clone2._zod.bag.validator = parser;
|
|
3513
|
-
clone2._zod.run = wrapped;
|
|
3514
|
-
if (!liveRun.__originalRun)
|
|
3515
|
-
installCompiledUserMethods(clone2, schema, parser);
|
|
3516
|
-
return clone2;
|
|
3517
|
-
}
|
|
3518
|
-
function installCompiledUserMethods(target, source, parser) {
|
|
3519
|
-
const targetAny = target;
|
|
3520
|
-
const sourceAny = source;
|
|
3521
|
-
if (typeof sourceAny.safeParse === "function") {
|
|
3522
|
-
const originalSafeParse = sourceAny.safeParse;
|
|
3523
|
-
targetAny.safeParse = (data, params) => {
|
|
3524
|
-
const out = parser(data);
|
|
3525
|
-
if (out !== INVALID) {
|
|
3526
|
-
return { success: true, data: out };
|
|
3527
|
-
}
|
|
3528
|
-
return originalSafeParse(data, params);
|
|
3529
|
-
};
|
|
3530
|
-
}
|
|
3531
|
-
if (typeof sourceAny.parse === "function") {
|
|
3532
|
-
const originalParse = sourceAny.parse;
|
|
3533
|
-
targetAny.parse = (data, params) => {
|
|
3534
|
-
const out = parser(data);
|
|
3535
|
-
if (out !== INVALID) {
|
|
3536
|
-
return out;
|
|
3537
|
-
}
|
|
3538
|
-
return originalParse(data, params);
|
|
3539
|
-
};
|
|
3540
|
-
}
|
|
3541
|
-
}
|
|
3542
|
-
function compileFn(schema, options) {
|
|
3543
|
-
let recursive = true;
|
|
3544
|
-
try {
|
|
3545
|
-
recursive = isRecursiveSchema(schema);
|
|
3546
|
-
} catch {}
|
|
3547
|
-
if (recursive) {
|
|
3548
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3549
|
-
}
|
|
3550
|
-
const ctx = {
|
|
3551
|
-
constants: new Map,
|
|
3552
|
-
constantCounter: 0,
|
|
3553
|
-
varCounter: 0,
|
|
3554
|
-
definite: true
|
|
3555
|
-
};
|
|
3556
|
-
const doc = new Doc(["input"]);
|
|
3557
|
-
const outputAccessor = generateCheck(doc, ctx, schema, "input", !options?.assertOnly);
|
|
3558
|
-
doc.write(outputAccessor === null ? `return true;` : `return ${outputAccessor};`);
|
|
3559
|
-
const constantNames = ["INVALID", ...ctx.constants.keys()];
|
|
3560
|
-
const constantValues = [INVALID, ...ctx.constants.values()];
|
|
3561
|
-
const code = doc.content.join(`
|
|
3562
|
-
`);
|
|
3563
|
-
const fullCode = options?.debug ? constantNames.length > 0 ? `// Constants: ${constantNames.join(", ")}
|
|
3564
|
-
${code}` : code : "";
|
|
3565
|
-
const F = Function;
|
|
3566
|
-
const factoryCode = `return (input) => {
|
|
3567
|
-
${code}
|
|
3568
|
-
}`;
|
|
3569
|
-
let fn;
|
|
3570
|
-
try {
|
|
3571
|
-
const factory = new F(...constantNames, factoryCode);
|
|
3572
|
-
fn = factory(...constantValues);
|
|
3573
|
-
} catch (err) {
|
|
3574
|
-
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${err.message})`);
|
|
3575
|
-
}
|
|
3576
|
-
if (options?.debug) {
|
|
3577
|
-
fn.code = fullCode;
|
|
3578
|
-
}
|
|
3579
|
-
fn.definite = ctx.definite;
|
|
3580
|
-
return fn;
|
|
3581
|
-
}
|
|
3582
|
-
function addConstant(ctx, value) {
|
|
3583
|
-
for (const [name, v] of ctx.constants) {
|
|
3584
|
-
if (v === value)
|
|
3585
|
-
return name;
|
|
3586
|
-
}
|
|
3587
|
-
const name = `c${ctx.constantCounter++}`;
|
|
3588
|
-
ctx.constants.set(name, value);
|
|
3589
|
-
return name;
|
|
3590
|
-
}
|
|
3591
|
-
function addUserConstant(ctx, fn) {
|
|
3592
|
-
ctx.definite = false;
|
|
3593
|
-
return addConstant(ctx, fn);
|
|
3594
|
-
}
|
|
3595
|
-
function newVar(ctx) {
|
|
3596
|
-
return `v${ctx.varCounter++}`;
|
|
3597
|
-
}
|
|
3598
|
-
function runtimeRun(schema, value) {
|
|
3599
|
-
const result = schema._zod.run({ value, issues: [] }, {});
|
|
3600
|
-
if (result && typeof result.then === "function")
|
|
3601
|
-
return INVALID;
|
|
3602
|
-
const r = result;
|
|
3603
|
-
return r.issues.length === 0 ? r.value : INVALID;
|
|
3604
|
-
}
|
|
3605
|
-
function compileChild(doc, ctx, schema, accessor, needsValue = true) {
|
|
3606
|
-
const contentLen = doc.content.length;
|
|
3607
|
-
const constantCount = ctx.constants.size;
|
|
3608
|
-
const constantCounter = ctx.constantCounter;
|
|
3609
|
-
const varCounter = ctx.varCounter;
|
|
3610
|
-
try {
|
|
3611
|
-
return generateCheck(doc, ctx, schema, accessor, needsValue);
|
|
3612
|
-
} catch (err) {
|
|
3613
|
-
if (!(err instanceof ZodCompileUnsupportedError) || !err.islandable)
|
|
3614
|
-
throw err;
|
|
3615
|
-
doc.content.length = contentLen;
|
|
3616
|
-
if (ctx.constants.size > constantCount) {
|
|
3617
|
-
const trailing = Array.from(ctx.constants.keys()).slice(constantCount);
|
|
3618
|
-
for (const k of trailing)
|
|
3619
|
-
ctx.constants.delete(k);
|
|
3620
|
-
}
|
|
3621
|
-
ctx.constantCounter = constantCounter;
|
|
3622
|
-
ctx.varCounter = varCounter;
|
|
3623
|
-
return emitRuntimeIsland(doc, ctx, schema, accessor);
|
|
3624
|
-
}
|
|
3625
|
-
}
|
|
3626
|
-
function emitRuntimeIsland(doc, ctx, schema, accessor) {
|
|
3627
|
-
ctx.definite = false;
|
|
3628
|
-
const schemaConst = addConstant(ctx, schema);
|
|
3629
|
-
const runConst = addConstant(ctx, runtimeRun);
|
|
3630
|
-
const outVar = newVar(ctx);
|
|
3631
|
-
doc.write(`const ${outVar} = ${runConst}(${schemaConst}, ${accessor});`);
|
|
3632
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3633
|
-
return outVar;
|
|
3634
|
-
}
|
|
3635
|
-
var WHEN_DEFAULTED_CHECKS = new Set([
|
|
3636
|
-
"max_size",
|
|
3637
|
-
"min_size",
|
|
3638
|
-
"size_equals",
|
|
3639
|
-
"max_length",
|
|
3640
|
-
"min_length",
|
|
3641
|
-
"length_equals"
|
|
3642
|
-
]);
|
|
3643
|
-
function generateChecks(doc, ctx, schema, accessor) {
|
|
3644
|
-
const schemaChecks = schema._zod.def.checks;
|
|
3645
|
-
if (!schemaChecks || schemaChecks.length === 0)
|
|
3646
|
-
return accessor;
|
|
3647
|
-
let currentAccessor = accessor;
|
|
3648
|
-
for (const check of schemaChecks) {
|
|
3649
|
-
const def = check._zod.def;
|
|
3650
|
-
if (def.when && !WHEN_DEFAULTED_CHECKS.has(def.check)) {
|
|
3651
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3652
|
-
}
|
|
3653
|
-
switch (def.check) {
|
|
3654
|
-
case "greater_than":
|
|
3655
|
-
generateGreaterThanCheck(doc, ctx, def, currentAccessor);
|
|
3656
|
-
break;
|
|
3657
|
-
case "less_than":
|
|
3658
|
-
generateLessThanCheck(doc, ctx, def, currentAccessor);
|
|
3659
|
-
break;
|
|
3660
|
-
case "multiple_of":
|
|
3661
|
-
generateMultipleOfCheck(doc, ctx, def, currentAccessor);
|
|
3662
|
-
break;
|
|
3663
|
-
case "number_format":
|
|
3664
|
-
generateNumberFormatCheck(doc, def, currentAccessor);
|
|
3665
|
-
break;
|
|
3666
|
-
case "min_length": {
|
|
3667
|
-
const min = numericOperand(def.minimum, "min_length");
|
|
3668
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${min} && ${currentAccessor}.length < ${def.minimum * 2}`);
|
|
3669
|
-
doc.write(`if (${len} < ${min}) return INVALID;`);
|
|
3670
|
-
break;
|
|
3671
|
-
}
|
|
3672
|
-
case "max_length": {
|
|
3673
|
-
const max = numericOperand(def.maximum, "max_length");
|
|
3674
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length > ${max}`);
|
|
3675
|
-
doc.write(`if (${len} > ${max}) return INVALID;`);
|
|
3676
|
-
break;
|
|
3677
|
-
}
|
|
3678
|
-
case "length_equals": {
|
|
3679
|
-
const exact = numericOperand(def.length, "length_equals");
|
|
3680
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${exact} && ${currentAccessor}.length <= ${def.length * 2}`);
|
|
3681
|
-
doc.write(`if (${len} !== ${exact}) return INVALID;`);
|
|
3682
|
-
break;
|
|
3683
|
-
}
|
|
3684
|
-
case "min_size":
|
|
3685
|
-
doc.write(`if (${currentAccessor}.size < ${numericOperand(def.minimum, "min_size")}) return INVALID;`);
|
|
3686
|
-
break;
|
|
3687
|
-
case "max_size":
|
|
3688
|
-
doc.write(`if (${currentAccessor}.size > ${numericOperand(def.maximum, "max_size")}) return INVALID;`);
|
|
3689
|
-
break;
|
|
3690
|
-
case "size_equals":
|
|
3691
|
-
doc.write(`if (${currentAccessor}.size !== ${numericOperand(def.size, "size_equals")}) return INVALID;`);
|
|
3692
|
-
break;
|
|
3693
|
-
case "string_format":
|
|
3694
|
-
currentAccessor = generateStringFormatCheck(doc, ctx, def, currentAccessor);
|
|
3695
|
-
break;
|
|
3696
|
-
case "custom":
|
|
3697
|
-
currentAccessor = generateCustomRefineCheck(doc, ctx, check, currentAccessor);
|
|
3698
|
-
break;
|
|
3699
|
-
case "bigint_format":
|
|
3700
|
-
generateBigIntFormatCheck(doc, def, currentAccessor);
|
|
3701
|
-
break;
|
|
3702
|
-
case "mime_type":
|
|
3703
|
-
generateMimeTypeCheck(doc, ctx, def, currentAccessor);
|
|
3704
|
-
break;
|
|
3705
|
-
case "property":
|
|
3706
|
-
generatePropertyCheck(doc, ctx, def, currentAccessor);
|
|
3707
|
-
break;
|
|
3708
|
-
case "properties":
|
|
3709
|
-
generatePropertiesChecks(doc, ctx, def, currentAccessor);
|
|
3710
|
-
break;
|
|
3711
|
-
case "overwrite": {
|
|
3712
|
-
const newAccessor = newVar(ctx);
|
|
3713
|
-
generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor);
|
|
3714
|
-
currentAccessor = newAccessor;
|
|
3715
|
-
break;
|
|
3716
|
-
}
|
|
3717
|
-
default: {
|
|
3718
|
-
throw new ZodCompileUnsupportedError(`check type ${def.check}`);
|
|
3719
|
-
}
|
|
3720
|
-
}
|
|
3721
|
-
}
|
|
3722
|
-
return currentAccessor;
|
|
3723
|
-
}
|
|
3724
|
-
function codePointLengthVar(doc, ctx, accessor, inDoubt) {
|
|
3725
|
-
const cpLen = addConstant(ctx, codePointLength);
|
|
3726
|
-
const v = newVar(ctx);
|
|
3727
|
-
doc.write(`const ${v} = typeof ${accessor} === "string" && ${inDoubt} ? ${cpLen}(${accessor}) : ${accessor}.length;`);
|
|
3728
|
-
return v;
|
|
3729
|
-
}
|
|
3730
|
-
function numericOperand(value, label) {
|
|
3731
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
3732
|
-
throw new ZodCompileUnsupportedError(`${label} bound of type ${typeof value}`);
|
|
3733
|
-
}
|
|
3734
|
-
return `${value}`;
|
|
3735
|
-
}
|
|
3736
|
-
function comparisonOperand(ctx, value) {
|
|
3737
|
-
if (typeof value === "bigint")
|
|
3738
|
-
return `${value}n`;
|
|
3739
|
-
if (typeof value === "number") {
|
|
3740
|
-
if (Number.isNaN(value))
|
|
3741
|
-
throw new ZodCompileUnsupportedError("comparison check with NaN bound");
|
|
3742
|
-
return `${value}`;
|
|
3743
|
-
}
|
|
3744
|
-
if (value instanceof Date) {
|
|
3745
|
-
if (Number.isNaN(value.getTime())) {
|
|
3746
|
-
throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
|
|
3747
|
-
}
|
|
3748
|
-
return addConstant(ctx, value);
|
|
3749
|
-
}
|
|
3750
|
-
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof value}`);
|
|
3751
|
-
}
|
|
3752
|
-
function generateGreaterThanCheck(doc, ctx, def, accessor) {
|
|
3753
|
-
const op = def.inclusive ? "<" : "<=";
|
|
3754
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3755
|
-
}
|
|
3756
|
-
function generateLessThanCheck(doc, ctx, def, accessor) {
|
|
3757
|
-
const op = def.inclusive ? ">" : ">=";
|
|
3758
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3759
|
-
}
|
|
3760
|
-
function generateMultipleOfCheck(doc, ctx, def, accessor) {
|
|
3761
|
-
if (typeof def.value === "bigint") {
|
|
3762
|
-
if (def.value === BigInt(0))
|
|
3763
|
-
throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
|
|
3764
|
-
doc.write(`if (${accessor} % ${def.value}n !== 0n) return INVALID;`);
|
|
3765
|
-
} else {
|
|
3766
|
-
const remainder = addConstant(ctx, floatSafeRemainder);
|
|
3767
|
-
doc.write(`if (${remainder}(${accessor}, ${numericOperand(def.value, "multiple_of")}) !== 0) return INVALID;`);
|
|
3768
|
-
}
|
|
3769
|
-
}
|
|
3770
|
-
function generateNumberFormatCheck(doc, def, accessor) {
|
|
3771
|
-
const format = def.format;
|
|
3772
|
-
switch (format) {
|
|
3773
|
-
case "safeint":
|
|
3774
|
-
doc.write(`if (!Number.isSafeInteger(${accessor})) return INVALID;`);
|
|
3775
|
-
break;
|
|
3776
|
-
case "int32":
|
|
3777
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < -2147483648 || ${accessor} > 2147483647) return INVALID;`);
|
|
3778
|
-
break;
|
|
3779
|
-
case "uint32":
|
|
3780
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < 0 || ${accessor} > 4294967295) return INVALID;`);
|
|
3781
|
-
break;
|
|
3782
|
-
case "float32":
|
|
3783
|
-
doc.write(`if (!Number.isFinite(${accessor}) || ${accessor} < -3.4028234663852886e38 || ${accessor} > 3.4028234663852886e38) return INVALID;`);
|
|
3784
|
-
break;
|
|
3785
|
-
case "float64":
|
|
3786
|
-
doc.write(`if (!Number.isFinite(${accessor})) return INVALID;`);
|
|
3787
|
-
break;
|
|
3788
|
-
default: {
|
|
3789
|
-
throw new ZodCompileUnsupportedError(`number format ${format}`);
|
|
3790
|
-
}
|
|
3791
|
-
}
|
|
3792
|
-
}
|
|
3793
|
-
function generateBigIntFormatCheck(doc, def, accessor) {
|
|
3794
|
-
const format = def.format;
|
|
3795
|
-
if (!format)
|
|
3796
|
-
return;
|
|
3797
|
-
switch (format) {
|
|
3798
|
-
case "int64":
|
|
3799
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
3800
|
-
break;
|
|
3801
|
-
case "uint64":
|
|
3802
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
3803
|
-
break;
|
|
3804
|
-
default: {
|
|
3805
|
-
throw new ZodCompileUnsupportedError(`bigint format ${format}`);
|
|
3806
|
-
}
|
|
3807
|
-
}
|
|
3808
|
-
}
|
|
3809
|
-
function generateMimeTypeCheck(doc, ctx, def, accessor) {
|
|
3810
|
-
const mimeTypes = def.mime;
|
|
3811
|
-
if (mimeTypes && mimeTypes.length > 0) {
|
|
3812
|
-
const mimeSet = addConstant(ctx, new Set(mimeTypes));
|
|
3813
|
-
doc.write(`if (!${mimeSet}.has(${accessor}.type)) return INVALID;`);
|
|
3814
|
-
}
|
|
3815
|
-
}
|
|
3816
|
-
function generatePropertiesChecks(doc, ctx, def, accessor) {
|
|
3817
|
-
if (def.when) {
|
|
3818
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3819
|
-
}
|
|
3820
|
-
doc.write(`if (${accessor} == null) return INVALID;`);
|
|
3821
|
-
const shape = def.shape;
|
|
3822
|
-
for (const key of Reflect.ownKeys(shape)) {
|
|
3823
|
-
const keyExpr = typeof key === "symbol" ? addConstant(ctx, key) : esc(key);
|
|
3824
|
-
const inputVar = newVar(ctx);
|
|
3825
|
-
doc.write(`const ${inputVar} = ${accessor}[${keyExpr}];`);
|
|
3826
|
-
compileChild(doc, ctx, shape[key], inputVar, false);
|
|
3827
|
-
}
|
|
3828
|
-
}
|
|
3829
|
-
function generatePropertyCheck(doc, ctx, def, accessor) {
|
|
3830
|
-
const propAccessor = `${accessor}[${JSON.stringify(def.property)}]`;
|
|
3831
|
-
generateCheck(doc, ctx, def.schema, propAccessor);
|
|
3832
|
-
}
|
|
3833
|
-
function generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor) {
|
|
3834
|
-
const tx = check._zod.def.tx;
|
|
3835
|
-
if (!tx) {
|
|
3836
|
-
throw new ZodCompileUnsupportedError("overwrite check without a transform function");
|
|
3837
|
-
}
|
|
3838
|
-
if (isAsyncFunction(tx)) {
|
|
3839
|
-
throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
|
|
3840
|
-
}
|
|
3841
|
-
const txConst = addConstant(ctx, tx);
|
|
3842
|
-
doc.write(`const ${newAccessor} = ${txConst}(${currentAccessor});`);
|
|
3843
|
-
}
|
|
3844
|
-
function throwAsync() {
|
|
3845
|
-
throw new $ZodAsyncError;
|
|
3846
|
-
}
|
|
3847
|
-
function pushIssue(issue) {
|
|
3848
|
-
this.issues.push(issue);
|
|
3849
|
-
}
|
|
3850
|
-
function generateCustomRefineCheck(doc, ctx, check, accessor) {
|
|
3851
|
-
const def = check._zod.def;
|
|
3852
|
-
if (def.fn) {
|
|
3853
|
-
if (isAsyncFunction(def.fn)) {
|
|
3854
|
-
throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
|
|
3855
|
-
}
|
|
3856
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
3857
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
3858
|
-
const resVar = newVar(ctx);
|
|
3859
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
3860
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
3861
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
3862
|
-
return accessor;
|
|
3863
|
-
}
|
|
3864
|
-
if (check._zod.check) {
|
|
3865
|
-
if (isAsyncFunction(check._zod.check)) {
|
|
3866
|
-
throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
|
|
3867
|
-
}
|
|
3868
|
-
const checkFn = check._zod.check;
|
|
3869
|
-
const helperFn = (value) => {
|
|
3870
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
3871
|
-
const result = checkFn(fakePayload);
|
|
3872
|
-
if (result instanceof Promise)
|
|
3873
|
-
throwAsync();
|
|
3874
|
-
return fakePayload.issues.length === 0 ? fakePayload.value : INVALID;
|
|
3875
|
-
};
|
|
3876
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
3877
|
-
const outVar = newVar(ctx);
|
|
3878
|
-
doc.write(`const ${outVar} = ${helperConst}(${accessor});`);
|
|
3879
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3880
|
-
return outVar;
|
|
3881
|
-
}
|
|
3882
|
-
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
|
|
3883
|
-
}
|
|
3884
|
-
var PATTERN_IS_COMPLETE = new Set([
|
|
3885
|
-
"cidrv4",
|
|
3886
|
-
"cuid",
|
|
3887
|
-
"cuid2",
|
|
3888
|
-
"date",
|
|
3889
|
-
"datetime",
|
|
3890
|
-
"duration",
|
|
3891
|
-
"e164",
|
|
3892
|
-
"email",
|
|
3893
|
-
"emoji",
|
|
3894
|
-
"ends_with",
|
|
3895
|
-
"guid",
|
|
3896
|
-
"includes",
|
|
3897
|
-
"ipv4",
|
|
3898
|
-
"ksuid",
|
|
3899
|
-
"lowercase",
|
|
3900
|
-
"mac",
|
|
3901
|
-
"nanoid",
|
|
3902
|
-
"regex",
|
|
3903
|
-
"starts_with",
|
|
3904
|
-
"time",
|
|
3905
|
-
"ulid",
|
|
3906
|
-
"uppercase",
|
|
3907
|
-
"uuid",
|
|
3908
|
-
"xid"
|
|
3909
|
-
]);
|
|
3910
|
-
function generateStringFormatCheck(doc, ctx, def, accessor, needsValue = true) {
|
|
3911
|
-
const fmt = def.format;
|
|
3912
|
-
if (fmt === "base64") {
|
|
3913
|
-
const validator = addConstant(ctx, isValidBase64);
|
|
3914
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3915
|
-
return accessor;
|
|
3916
|
-
}
|
|
3917
|
-
if (fmt === "base64url") {
|
|
3918
|
-
const validator = addConstant(ctx, isValidBase64URL);
|
|
3919
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3920
|
-
return accessor;
|
|
3921
|
-
}
|
|
3922
|
-
if (fmt === "jwt") {
|
|
3923
|
-
const validator = addConstant(ctx, isValidJWT);
|
|
3924
|
-
const alg = addConstant(ctx, def.alg ?? null);
|
|
3925
|
-
doc.write(`if (!${validator}(${accessor}, ${alg})) return INVALID;`);
|
|
3926
|
-
return accessor;
|
|
3927
|
-
}
|
|
3928
|
-
if (fmt === "ipv6") {
|
|
3929
|
-
const validator = addConstant(ctx, isValidIPv6);
|
|
3930
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3931
|
-
return accessor;
|
|
3932
|
-
}
|
|
3933
|
-
if (fmt === "cidrv6") {
|
|
3934
|
-
const validator = addConstant(ctx, isValidCIDRv6);
|
|
3935
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3936
|
-
return accessor;
|
|
3937
|
-
}
|
|
3938
|
-
if (fmt === "credit_card") {
|
|
3939
|
-
const validator = addConstant(ctx, isValidCreditCard);
|
|
3940
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3941
|
-
return accessor;
|
|
3942
|
-
}
|
|
3943
|
-
if (fmt === "iban") {
|
|
3944
|
-
const validator = addConstant(ctx, isValidIBAN);
|
|
3945
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3946
|
-
return accessor;
|
|
3947
|
-
}
|
|
3948
|
-
const formatDef = def;
|
|
3949
|
-
if (fmt === "url" || fmt === "httpurl" || formatDef.normalize || formatDef.hostname !== undefined || formatDef.protocol !== undefined) {
|
|
3950
|
-
const parseConst = addConstant(ctx, validateURL);
|
|
3951
|
-
const defConst = addConstant(ctx, def);
|
|
3952
|
-
const trimVar = newVar(ctx);
|
|
3953
|
-
const urlVar = newVar(ctx);
|
|
3954
|
-
doc.write(`const ${trimVar} = ${accessor}.trim();`);
|
|
3955
|
-
doc.write(`const ${urlVar} = ${parseConst}(${trimVar}, ${defConst});`);
|
|
3956
|
-
doc.write(`if (typeof ${urlVar} === "number") return INVALID;`);
|
|
3957
|
-
if (formatDef.hostname !== undefined) {
|
|
3958
|
-
const hostnameConst = addConstant(ctx, urlHostnameOk);
|
|
3959
|
-
doc.write(`if (!${hostnameConst}(${urlVar}, ${defConst}.hostname)) return INVALID;`);
|
|
3960
|
-
}
|
|
3961
|
-
if (formatDef.protocol !== undefined) {
|
|
3962
|
-
const protocolConst = addConstant(ctx, urlProtocolOk);
|
|
3963
|
-
doc.write(`if (!${protocolConst}(${urlVar}, ${defConst}.protocol)) return INVALID;`);
|
|
3964
|
-
}
|
|
3965
|
-
if (!needsValue)
|
|
3966
|
-
return null;
|
|
3967
|
-
const outputVar = newVar(ctx);
|
|
3968
|
-
const outputExpr = formatDef.normalize ? `${urlVar}.href` : `${addConstant(ctx, stripTabAndNewline)}(${trimVar})`;
|
|
3969
|
-
doc.write(`const ${outputVar} = ${outputExpr};`);
|
|
3970
|
-
return outputVar;
|
|
3971
|
-
}
|
|
3972
|
-
const customFn = def.fn;
|
|
3973
|
-
if (customFn) {
|
|
3974
|
-
if (isAsyncFunction(customFn))
|
|
3975
|
-
throw new ZodCompileUnsupportedError(`async string format ${fmt}`);
|
|
3976
|
-
const fnConst = addConstant(ctx, customFn);
|
|
3977
|
-
doc.write(`if (!${fnConst}(${accessor})) return INVALID;`);
|
|
3978
|
-
return accessor;
|
|
3979
|
-
}
|
|
3980
|
-
if (PATTERN_IS_COMPLETE.has(fmt) && def.pattern) {
|
|
3981
|
-
const patternConst = addConstant(ctx, def.pattern);
|
|
3982
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
3983
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
3984
|
-
return accessor;
|
|
3985
|
-
}
|
|
3986
|
-
const format = def.format;
|
|
3987
|
-
switch (format) {
|
|
3988
|
-
case "regex":
|
|
3989
|
-
throw new ZodCompileUnsupportedError("regex format without a pattern");
|
|
3990
|
-
case "lowercase":
|
|
3991
|
-
doc.write(`if (${accessor} !== ${accessor}.toLowerCase()) return INVALID;`);
|
|
3992
|
-
break;
|
|
3993
|
-
case "uppercase":
|
|
3994
|
-
doc.write(`if (${accessor} !== ${accessor}.toUpperCase()) return INVALID;`);
|
|
3995
|
-
break;
|
|
3996
|
-
case "includes":
|
|
3997
|
-
doc.write(`if (!${accessor}.includes(${esc(def.includes)})) return INVALID;`);
|
|
3998
|
-
break;
|
|
3999
|
-
case "starts_with": {
|
|
4000
|
-
const prefix = def.prefix;
|
|
4001
|
-
doc.write(`if (${accessor}.slice(0, ${prefix.length}) !== ${esc(prefix)}) return INVALID;`);
|
|
4002
|
-
break;
|
|
4003
|
-
}
|
|
4004
|
-
case "ends_with": {
|
|
4005
|
-
const suffix = def.suffix;
|
|
4006
|
-
doc.write(`if (${accessor}.slice(-${suffix.length}) !== ${esc(suffix)}) return INVALID;`);
|
|
4007
|
-
break;
|
|
4008
|
-
}
|
|
4009
|
-
default: {
|
|
4010
|
-
throw new ZodCompileUnsupportedError(`string format ${format}`);
|
|
4011
|
-
}
|
|
4012
|
-
}
|
|
4013
|
-
return accessor;
|
|
4014
|
-
}
|
|
4015
|
-
function generateCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4016
|
-
const def = schema._zod.def;
|
|
4017
|
-
const type = def.type;
|
|
4018
|
-
if (def.coerce) {
|
|
4019
|
-
throw new ZodCompileUnsupportedError(`coercion (z.coerce.${type}())`);
|
|
4020
|
-
}
|
|
4021
|
-
const buildsValue = needsValue || !!def.checks?.length;
|
|
4022
|
-
let typeAccessor;
|
|
4023
|
-
switch (type) {
|
|
4024
|
-
case "string":
|
|
4025
|
-
typeAccessor = generateStringCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4026
|
-
break;
|
|
4027
|
-
case "number":
|
|
4028
|
-
typeAccessor = generateNumberCheck(doc, schema, accessor);
|
|
4029
|
-
break;
|
|
4030
|
-
case "boolean":
|
|
4031
|
-
typeAccessor = generateBooleanCheck(doc, accessor);
|
|
4032
|
-
break;
|
|
4033
|
-
case "bigint":
|
|
4034
|
-
typeAccessor = generateBigIntCheck(doc, schema, accessor);
|
|
4035
|
-
break;
|
|
4036
|
-
case "symbol":
|
|
4037
|
-
typeAccessor = generateSymbolCheck(doc, accessor);
|
|
4038
|
-
break;
|
|
4039
|
-
case "undefined":
|
|
4040
|
-
typeAccessor = generateUndefinedCheck(doc, accessor);
|
|
4041
|
-
break;
|
|
4042
|
-
case "null":
|
|
4043
|
-
typeAccessor = generateNullCheck(doc, accessor);
|
|
4044
|
-
break;
|
|
4045
|
-
case "any":
|
|
4046
|
-
case "unknown":
|
|
4047
|
-
typeAccessor = accessor;
|
|
4048
|
-
break;
|
|
4049
|
-
case "never":
|
|
4050
|
-
doc.write("return INVALID;");
|
|
4051
|
-
typeAccessor = accessor;
|
|
4052
|
-
break;
|
|
4053
|
-
case "void":
|
|
4054
|
-
typeAccessor = generateVoidCheck(doc, accessor);
|
|
4055
|
-
break;
|
|
4056
|
-
case "nan":
|
|
4057
|
-
typeAccessor = generateNaNCheck(doc, accessor);
|
|
4058
|
-
break;
|
|
4059
|
-
case "date":
|
|
4060
|
-
typeAccessor = generateDateCheck(doc, accessor);
|
|
4061
|
-
break;
|
|
4062
|
-
case "object":
|
|
4063
|
-
typeAccessor = generateObjectCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4064
|
-
break;
|
|
4065
|
-
case "optional":
|
|
4066
|
-
typeAccessor = generateOptionalCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4067
|
-
break;
|
|
4068
|
-
case "nullable":
|
|
4069
|
-
typeAccessor = generateNullableCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4070
|
-
break;
|
|
4071
|
-
case "array":
|
|
4072
|
-
typeAccessor = generateArrayCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4073
|
-
break;
|
|
4074
|
-
case "literal":
|
|
4075
|
-
typeAccessor = generateLiteralCheck(doc, ctx, schema, accessor);
|
|
4076
|
-
break;
|
|
4077
|
-
case "enum":
|
|
4078
|
-
typeAccessor = generateEnumCheck(doc, ctx, schema, accessor);
|
|
4079
|
-
break;
|
|
4080
|
-
case "readonly": {
|
|
4081
|
-
const innerOut = generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4082
|
-
const frozenVar = newVar(ctx);
|
|
4083
|
-
doc.write(`const ${frozenVar} = Object.freeze(${innerOut});`);
|
|
4084
|
-
typeAccessor = frozenVar;
|
|
4085
|
-
break;
|
|
4086
|
-
}
|
|
4087
|
-
case "success":
|
|
4088
|
-
generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4089
|
-
typeAccessor = "true";
|
|
4090
|
-
break;
|
|
4091
|
-
case "default":
|
|
4092
|
-
case "prefault":
|
|
4093
|
-
typeAccessor = generateDefaultCheck(doc, ctx, schema, accessor);
|
|
4094
|
-
break;
|
|
4095
|
-
case "nonoptional":
|
|
4096
|
-
typeAccessor = generateNonOptionalCheck(doc, ctx, schema, accessor);
|
|
4097
|
-
break;
|
|
4098
|
-
case "tuple":
|
|
4099
|
-
typeAccessor = generateTupleCheck(doc, ctx, schema, accessor);
|
|
4100
|
-
break;
|
|
4101
|
-
case "union":
|
|
4102
|
-
typeAccessor = generateUnionCheck(doc, ctx, schema, accessor);
|
|
4103
|
-
break;
|
|
4104
|
-
case "intersection":
|
|
4105
|
-
typeAccessor = generateIntersectionCheck(doc, ctx, schema, accessor);
|
|
4106
|
-
break;
|
|
4107
|
-
case "record":
|
|
4108
|
-
typeAccessor = generateRecordCheck(doc, ctx, schema, accessor);
|
|
4109
|
-
break;
|
|
4110
|
-
case "map":
|
|
4111
|
-
typeAccessor = generateMapCheck(doc, ctx, schema, accessor);
|
|
4112
|
-
break;
|
|
4113
|
-
case "set":
|
|
4114
|
-
typeAccessor = generateSetCheck(doc, ctx, schema, accessor);
|
|
4115
|
-
break;
|
|
4116
|
-
case "file":
|
|
4117
|
-
typeAccessor = generateFileCheck(doc, accessor);
|
|
4118
|
-
break;
|
|
4119
|
-
case "template_literal":
|
|
4120
|
-
typeAccessor = generateTemplateLiteralCheck(doc, ctx, schema, accessor);
|
|
4121
|
-
break;
|
|
4122
|
-
case "lazy":
|
|
4123
|
-
typeAccessor = generateLazyCheck(doc, ctx, schema, accessor);
|
|
4124
|
-
break;
|
|
4125
|
-
case "pipe":
|
|
4126
|
-
typeAccessor = generatePipeCheck(doc, ctx, schema, accessor);
|
|
4127
|
-
break;
|
|
4128
|
-
case "custom":
|
|
4129
|
-
typeAccessor = generateCustomCheck(doc, ctx, schema, accessor);
|
|
4130
|
-
break;
|
|
4131
|
-
case "transform":
|
|
4132
|
-
typeAccessor = generateTransformCheck(doc, ctx, schema, accessor);
|
|
4133
|
-
break;
|
|
4134
|
-
case "catch":
|
|
4135
|
-
typeAccessor = generateCatchCheck(doc, ctx, schema, accessor);
|
|
4136
|
-
break;
|
|
4137
|
-
default: {
|
|
4138
|
-
throw new ZodCompileUnsupportedError(`schema type ${type}`);
|
|
4139
|
-
}
|
|
4140
|
-
}
|
|
4141
|
-
if (typeAccessor === null)
|
|
4142
|
-
return null;
|
|
4143
|
-
return generateChecks(doc, ctx, schema, typeAccessor);
|
|
4144
|
-
}
|
|
4145
|
-
function generateStringCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4146
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4147
|
-
const def = schema._zod.def;
|
|
4148
|
-
if (def.format === undefined)
|
|
4149
|
-
return accessor;
|
|
4150
|
-
return generateStringFormatCheck(doc, ctx, def, accessor, needsValue);
|
|
4151
|
-
}
|
|
4152
|
-
function generateNumberCheck(doc, schema, accessor) {
|
|
4153
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isFinite(${accessor})) return INVALID;`);
|
|
4154
|
-
const def = schema._zod.def;
|
|
4155
|
-
if (def.check === "number_format" && def.format) {
|
|
4156
|
-
generateNumberFormatCheck(doc, { format: def.format }, accessor);
|
|
4157
|
-
}
|
|
4158
|
-
return accessor;
|
|
4159
|
-
}
|
|
4160
|
-
function generateBooleanCheck(doc, accessor) {
|
|
4161
|
-
doc.write(`if (typeof ${accessor} !== "boolean") return INVALID;`);
|
|
4162
|
-
return accessor;
|
|
4163
|
-
}
|
|
4164
|
-
function generateBigIntCheck(doc, schema, accessor) {
|
|
4165
|
-
doc.write(`if (typeof ${accessor} !== "bigint") return INVALID;`);
|
|
4166
|
-
const def = schema._zod.def;
|
|
4167
|
-
if (def.format) {
|
|
4168
|
-
switch (def.format) {
|
|
4169
|
-
case "int64":
|
|
4170
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
4171
|
-
break;
|
|
4172
|
-
case "uint64":
|
|
4173
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
4174
|
-
break;
|
|
4175
|
-
}
|
|
4176
|
-
}
|
|
4177
|
-
return accessor;
|
|
4178
|
-
}
|
|
4179
|
-
function generateSymbolCheck(doc, accessor) {
|
|
4180
|
-
doc.write(`if (typeof ${accessor} !== "symbol") return INVALID;`);
|
|
4181
|
-
return accessor;
|
|
4182
|
-
}
|
|
4183
|
-
function generateUndefinedCheck(doc, accessor) {
|
|
4184
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4185
|
-
return accessor;
|
|
4186
|
-
}
|
|
4187
|
-
function generateNullCheck(doc, accessor) {
|
|
4188
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4189
|
-
return accessor;
|
|
4190
|
-
}
|
|
4191
|
-
function generateVoidCheck(doc, accessor) {
|
|
4192
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4193
|
-
return accessor;
|
|
4194
|
-
}
|
|
4195
|
-
function generateNaNCheck(doc, accessor) {
|
|
4196
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isNaN(${accessor})) return INVALID;`);
|
|
4197
|
-
return accessor;
|
|
4198
|
-
}
|
|
4199
|
-
function generateDateCheck(doc, accessor) {
|
|
4200
|
-
doc.write(`if (!(${accessor} instanceof Date) || Number.isNaN(${accessor}.getTime())) return INVALID;`);
|
|
4201
|
-
return accessor;
|
|
4202
|
-
}
|
|
4203
|
-
function generateObjectCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4204
|
-
const def = schema._zod.def;
|
|
4205
|
-
doc.write(`if (typeof ${accessor} !== "object" || ${accessor} === null || Array.isArray(${accessor})) return INVALID;`);
|
|
4206
|
-
const shape = def.shape;
|
|
4207
|
-
const keys = Object.keys(shape);
|
|
4208
|
-
const symbolKeys = Object.getOwnPropertySymbols(shape);
|
|
4209
|
-
const allKeys = symbolKeys.length ? [...keys, ...symbolKeys] : keys;
|
|
4210
|
-
const keyExpr = (k) => typeof k === "symbol" ? addConstant(ctx, k) : esc(k);
|
|
4211
|
-
const propKey = (k) => typeof k === "symbol" ? `[${keyExpr(k)}]` : esc(k);
|
|
4212
|
-
const propShape = shape;
|
|
4213
|
-
if (keys.includes("__proto__")) {
|
|
4214
|
-
throw new ZodCompileUnsupportedError('object shape key "__proto__"');
|
|
4215
|
-
}
|
|
4216
|
-
const propOutputs = new Map;
|
|
4217
|
-
for (const key of allKeys) {
|
|
4218
|
-
const propSchema = propShape[key];
|
|
4219
|
-
const kx = keyExpr(key);
|
|
4220
|
-
const inputVar = newVar(ctx);
|
|
4221
|
-
doc.write(`const ${inputVar} = ${accessor}[${kx}];`);
|
|
4222
|
-
if (propSchema._zod.optin !== undefined) {
|
|
4223
|
-
const outputVar = newVar(ctx);
|
|
4224
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
4225
|
-
doc.indented((d) => {
|
|
4226
|
-
const outputAccessor = compileChild(d, ctx, propSchema, inputVar);
|
|
4227
|
-
d.write(`return ${outputAccessor};`);
|
|
4228
|
-
});
|
|
4229
|
-
doc.write(`})();`);
|
|
4230
|
-
if (propSchema._zod.optout === "optional") {
|
|
4231
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
4232
|
-
doc.indented((d) => {
|
|
4233
|
-
d.write(`if (${kx} in ${accessor}) return INVALID;`);
|
|
4234
|
-
d.write(`${outputVar} = undefined;`);
|
|
4235
|
-
});
|
|
4236
|
-
doc.write(`}`);
|
|
4237
|
-
} else {
|
|
4238
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4239
|
-
}
|
|
4240
|
-
propOutputs.set(key, outputVar);
|
|
4241
|
-
} else {
|
|
4242
|
-
if (requiresPresenceCheck(propSchema)) {
|
|
4243
|
-
doc.write(`if (!(${kx} in ${accessor})) return INVALID;`);
|
|
4244
|
-
}
|
|
4245
|
-
const outputAccessor = compileChild(doc, ctx, propSchema, inputVar, buildsValue);
|
|
4246
|
-
if (outputAccessor !== null)
|
|
4247
|
-
propOutputs.set(key, outputAccessor);
|
|
4248
|
-
}
|
|
4249
|
-
}
|
|
4250
|
-
const catchall = def.catchall;
|
|
4251
|
-
let unknownKeysMode = "none";
|
|
4252
|
-
if (catchall) {
|
|
4253
|
-
const catchallType = catchall._zod.def.type;
|
|
4254
|
-
if (catchallType === "never") {
|
|
4255
|
-
const condition = keys.map((k) => `k !== ${esc(k)}`).join(" && ") || "true";
|
|
4256
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4257
|
-
doc.indented((d) => {
|
|
4258
|
-
d.write(`if (${condition}) return INVALID;`);
|
|
4259
|
-
});
|
|
4260
|
-
doc.write(`}`);
|
|
4261
|
-
} else if ((catchallType === "unknown" || catchallType === "any") && !catchall._zod.def.checks?.length) {
|
|
4262
|
-
unknownKeysMode = "passthrough";
|
|
4263
|
-
} else {
|
|
4264
|
-
unknownKeysMode = "schema";
|
|
4265
|
-
}
|
|
4266
|
-
}
|
|
4267
|
-
const outputVar = newVar(ctx);
|
|
4268
|
-
const hasConditionalKeys = allKeys.some((k) => mayOmitUndefined(propShape[k]) || dropsWhenAbsent(propShape[k]));
|
|
4269
|
-
if (!buildsValue) {
|
|
4270
|
-
if (unknownKeysMode === "schema") {
|
|
4271
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4272
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4273
|
-
doc.indented((d) => {
|
|
4274
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4275
|
-
if (knownSet)
|
|
4276
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4277
|
-
const valVar = newVar(ctx);
|
|
4278
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4279
|
-
compileChild(d, ctx, catchall, valVar, false);
|
|
4280
|
-
});
|
|
4281
|
-
doc.write(`}`);
|
|
4282
|
-
}
|
|
4283
|
-
return null;
|
|
4284
|
-
}
|
|
4285
|
-
if (!hasConditionalKeys) {
|
|
4286
|
-
const propLiterals = allKeys.map((k) => `${propKey(k)}: ${propOutputs.get(k)}`).join(", ");
|
|
4287
|
-
doc.write(`const ${outputVar} = { ${propLiterals} };`);
|
|
4288
|
-
} else {
|
|
4289
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4290
|
-
for (const k of allKeys) {
|
|
4291
|
-
const kx = keyExpr(k);
|
|
4292
|
-
const out = propOutputs.get(k);
|
|
4293
|
-
if (dropsWhenAbsent(propShape[k])) {
|
|
4294
|
-
doc.write(`if (${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4295
|
-
} else if (mayOmitUndefined(propShape[k])) {
|
|
4296
|
-
doc.write(`if (${out} !== undefined || ${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4297
|
-
} else {
|
|
4298
|
-
doc.write(`${outputVar}[${kx}] = ${out};`);
|
|
4299
|
-
}
|
|
4300
|
-
}
|
|
4301
|
-
}
|
|
4302
|
-
if (unknownKeysMode !== "none") {
|
|
4303
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4304
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4305
|
-
doc.indented((d) => {
|
|
4306
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4307
|
-
if (knownSet)
|
|
4308
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4309
|
-
if (unknownKeysMode === "passthrough") {
|
|
4310
|
-
d.write(`${outputVar}[k] = ${accessor}[k];`);
|
|
4311
|
-
} else {
|
|
4312
|
-
const valVar = newVar(ctx);
|
|
4313
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4314
|
-
const catchallOut = compileChild(d, ctx, catchall, valVar);
|
|
4315
|
-
d.write(`${outputVar}[k] = ${catchallOut};`);
|
|
4316
|
-
}
|
|
4317
|
-
});
|
|
4318
|
-
doc.write(`}`);
|
|
4319
|
-
}
|
|
4320
|
-
return outputVar;
|
|
4321
|
-
}
|
|
4322
|
-
function generateOptionalCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4323
|
-
const def = schema._zod.def;
|
|
4324
|
-
if (isExactOptional(schema)) {
|
|
4325
|
-
return generateCheck(doc, ctx, def.innerType, accessor, buildsValue);
|
|
4326
|
-
}
|
|
4327
|
-
if (def.innerType._zod.optin === "defaulted") {
|
|
4328
|
-
const outputVar = newVar(ctx);
|
|
4329
|
-
const branchVar = newVar(ctx);
|
|
4330
|
-
doc.write(`let ${outputVar};`);
|
|
4331
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4332
|
-
doc.indented((d) => {
|
|
4333
|
-
d.write(`const ${branchVar} = (() => {`);
|
|
4334
|
-
d.indented((d2) => {
|
|
4335
|
-
const innerOutput = generateCheck(d2, ctx, def.innerType, accessor);
|
|
4336
|
-
d2.write(`return ${innerOutput};`);
|
|
4337
|
-
});
|
|
4338
|
-
d.write(`})();`);
|
|
4339
|
-
d.write(`if (${branchVar} !== INVALID) ${outputVar} = ${branchVar};`);
|
|
4340
|
-
});
|
|
4341
|
-
doc.write(`} else {`);
|
|
4342
|
-
doc.indented((d) => {
|
|
4343
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4344
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4345
|
-
});
|
|
4346
|
-
doc.write(`}`);
|
|
4347
|
-
return outputVar;
|
|
4348
|
-
}
|
|
4349
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4350
|
-
if (outputVar)
|
|
4351
|
-
doc.write(`let ${outputVar};`);
|
|
4352
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4353
|
-
doc.indented((d) => {
|
|
4354
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4355
|
-
if (outputVar && innerOutput !== null)
|
|
4356
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4357
|
-
});
|
|
4358
|
-
doc.write(`}`);
|
|
4359
|
-
return outputVar;
|
|
4360
|
-
}
|
|
4361
|
-
function isExactOptional(schema) {
|
|
4362
|
-
return schema._zod.traits?.has("$ZodExactOptional") === true;
|
|
4363
|
-
}
|
|
4364
|
-
function requiresPresenceCheck(schema) {
|
|
4365
|
-
return schema._zod.optin === undefined && fastPathAcceptsAbsence(schema);
|
|
4366
|
-
}
|
|
4367
|
-
function fastPathAcceptsAbsence(schema) {
|
|
4368
|
-
if (schema._zod.def.coerce)
|
|
4369
|
-
return true;
|
|
4370
|
-
const def = schema._zod.def;
|
|
4371
|
-
switch (def.type) {
|
|
4372
|
-
case "any":
|
|
4373
|
-
case "unknown":
|
|
4374
|
-
case "undefined":
|
|
4375
|
-
case "void":
|
|
4376
|
-
case "default":
|
|
4377
|
-
case "prefault":
|
|
4378
|
-
case "transform":
|
|
4379
|
-
case "custom":
|
|
4380
|
-
case "lazy":
|
|
4381
|
-
return true;
|
|
4382
|
-
case "string":
|
|
4383
|
-
case "number":
|
|
4384
|
-
case "boolean":
|
|
4385
|
-
case "bigint":
|
|
4386
|
-
case "symbol":
|
|
4387
|
-
case "null":
|
|
4388
|
-
case "never":
|
|
4389
|
-
case "nan":
|
|
4390
|
-
case "date":
|
|
4391
|
-
case "object":
|
|
4392
|
-
case "array":
|
|
4393
|
-
case "tuple":
|
|
4394
|
-
case "record":
|
|
4395
|
-
case "map":
|
|
4396
|
-
case "set":
|
|
4397
|
-
case "file":
|
|
4398
|
-
case "template_literal":
|
|
4399
|
-
return false;
|
|
4400
|
-
case "nonoptional":
|
|
4401
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : false;
|
|
4402
|
-
case "literal":
|
|
4403
|
-
return !!def.values?.includes(undefined);
|
|
4404
|
-
case "enum":
|
|
4405
|
-
return !!schema._zod.values?.has(undefined);
|
|
4406
|
-
case "optional":
|
|
4407
|
-
case "nullable":
|
|
4408
|
-
case "readonly":
|
|
4409
|
-
case "success":
|
|
4410
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : true;
|
|
4411
|
-
case "catch":
|
|
4412
|
-
return true;
|
|
4413
|
-
case "union":
|
|
4414
|
-
return def.options ? def.options.some(fastPathAcceptsAbsence) : true;
|
|
4415
|
-
case "intersection":
|
|
4416
|
-
if (!def.left || !def.right)
|
|
4417
|
-
return true;
|
|
4418
|
-
return fastPathAcceptsAbsence(def.left) && fastPathAcceptsAbsence(def.right);
|
|
4419
|
-
case "pipe":
|
|
4420
|
-
return def.in ? fastPathAcceptsAbsence(def.in) : true;
|
|
4421
|
-
default:
|
|
4422
|
-
return true;
|
|
4423
|
-
}
|
|
4424
|
-
}
|
|
4425
|
-
function dropsWhenAbsent(schema) {
|
|
4426
|
-
return schema._zod.optin === "optional" && schema._zod.optout === "optional";
|
|
4427
|
-
}
|
|
4428
|
-
function mayOmitUndefined(schema) {
|
|
4429
|
-
return (schema._zod.optin !== "defaulted" || schema._zod.optout === "optional") && mayOutputUndefined(schema);
|
|
4430
|
-
}
|
|
4431
|
-
function mayOutputUndefined(schema) {
|
|
4432
|
-
const def = schema._zod.def;
|
|
4433
|
-
switch (def.type) {
|
|
4434
|
-
case "string":
|
|
4435
|
-
case "number":
|
|
4436
|
-
case "boolean":
|
|
4437
|
-
case "bigint":
|
|
4438
|
-
case "symbol":
|
|
4439
|
-
case "null":
|
|
4440
|
-
case "nan":
|
|
4441
|
-
case "date":
|
|
4442
|
-
case "object":
|
|
4443
|
-
case "array":
|
|
4444
|
-
case "tuple":
|
|
4445
|
-
case "record":
|
|
4446
|
-
case "map":
|
|
4447
|
-
case "set":
|
|
4448
|
-
case "file":
|
|
4449
|
-
case "template_literal":
|
|
4450
|
-
case "never":
|
|
4451
|
-
case "success":
|
|
4452
|
-
return false;
|
|
4453
|
-
case "literal":
|
|
4454
|
-
return !!def.values?.includes(undefined);
|
|
4455
|
-
case "enum":
|
|
4456
|
-
return !!schema._zod.values?.has(undefined);
|
|
4457
|
-
case "optional":
|
|
4458
|
-
return true;
|
|
4459
|
-
case "nullable":
|
|
4460
|
-
case "readonly":
|
|
4461
|
-
case "nonoptional":
|
|
4462
|
-
return def.innerType ? mayOutputUndefined(def.innerType) : true;
|
|
4463
|
-
case "union":
|
|
4464
|
-
return def.options ? def.options.some(mayOutputUndefined) : true;
|
|
4465
|
-
case "intersection":
|
|
4466
|
-
return !def.left || !def.right || mayOutputUndefined(def.left) || mayOutputUndefined(def.right);
|
|
4467
|
-
case "pipe":
|
|
4468
|
-
return def.out ? mayOutputUndefined(def.out) : true;
|
|
4469
|
-
default:
|
|
4470
|
-
return true;
|
|
4471
|
-
}
|
|
4472
|
-
}
|
|
4473
|
-
function generateNullableCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4474
|
-
const def = schema._zod.def;
|
|
4475
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4476
|
-
if (outputVar)
|
|
4477
|
-
doc.write(`let ${outputVar} = null;`);
|
|
4478
|
-
doc.write(`if (${accessor} !== null) {`);
|
|
4479
|
-
doc.indented((d) => {
|
|
4480
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4481
|
-
if (outputVar && innerOutput !== null)
|
|
4482
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4483
|
-
});
|
|
4484
|
-
doc.write(`}`);
|
|
4485
|
-
return outputVar;
|
|
4486
|
-
}
|
|
4487
|
-
function generateArrayCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4488
|
-
const def = schema._zod.def;
|
|
4489
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4490
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4491
|
-
const iVar = newVar(ctx);
|
|
4492
|
-
const elemVar = newVar(ctx);
|
|
4493
|
-
if (outputVar)
|
|
4494
|
-
doc.write(`const ${outputVar} = new Array(${accessor}.length);`);
|
|
4495
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4496
|
-
doc.indented((d) => {
|
|
4497
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4498
|
-
const elemOutput = compileChild(d, ctx, def.element, elemVar, buildsValue);
|
|
4499
|
-
if (outputVar && elemOutput !== null)
|
|
4500
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4501
|
-
});
|
|
4502
|
-
doc.write(`}`);
|
|
4503
|
-
return outputVar;
|
|
4504
|
-
}
|
|
4505
|
-
function generateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4506
|
-
const def = schema._zod.def;
|
|
4507
|
-
const values = def.values;
|
|
4508
|
-
if (values.length !== 1) {
|
|
4509
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4510
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4511
|
-
return accessor;
|
|
4512
|
-
}
|
|
4513
|
-
const value = values[0];
|
|
4514
|
-
if (typeof value === "number" && Number.isNaN(value)) {
|
|
4515
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4516
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4517
|
-
return accessor;
|
|
4518
|
-
}
|
|
4519
|
-
if (typeof value === "string") {
|
|
4520
|
-
doc.write(`if (${accessor} !== ${esc(value)}) return INVALID;`);
|
|
4521
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
4522
|
-
doc.write(`if (${accessor} !== ${value}) return INVALID;`);
|
|
4523
|
-
} else if (value === null) {
|
|
4524
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4525
|
-
} else if (value === undefined) {
|
|
4526
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4527
|
-
} else if (typeof value === "bigint") {
|
|
4528
|
-
doc.write(`if (${accessor} !== ${value}n) return INVALID;`);
|
|
4529
|
-
} else {
|
|
4530
|
-
throw new ZodCompileUnsupportedError(`literal type ${typeof value}`);
|
|
4531
|
-
}
|
|
4532
|
-
return accessor;
|
|
4533
|
-
}
|
|
4534
|
-
function generateEnumCheck(doc, ctx, schema, accessor) {
|
|
4535
|
-
const values = schema._zod.values;
|
|
4536
|
-
if (!values) {
|
|
4537
|
-
throw new ZodCompileUnsupportedError("enum schema without enumerated values");
|
|
4538
|
-
}
|
|
4539
|
-
const enumSet = addConstant(ctx, values);
|
|
4540
|
-
doc.write(`if (!${enumSet}.has(${accessor})) return INVALID;`);
|
|
4541
|
-
return accessor;
|
|
4542
|
-
}
|
|
4543
|
-
function generateWrapperCheck(doc, ctx, schema, accessor) {
|
|
4544
|
-
const def = schema._zod.def;
|
|
4545
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4546
|
-
}
|
|
4547
|
-
function generateDefaultCheck(doc, ctx, schema, accessor) {
|
|
4548
|
-
const def = schema._zod.def;
|
|
4549
|
-
const descriptor = Object.getOwnPropertyDescriptor(schema._zod.def, "defaultValue");
|
|
4550
|
-
const defaultGetter = descriptor ? () => schema._zod.def.defaultValue : undefined;
|
|
4551
|
-
if (schema._zod.def.type === "prefault") {
|
|
4552
|
-
if (!defaultGetter) {
|
|
4553
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4554
|
-
}
|
|
4555
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4556
|
-
const inputVar = newVar(ctx);
|
|
4557
|
-
doc.write(`let ${inputVar} = ${accessor};`);
|
|
4558
|
-
doc.write(`if (${accessor} === undefined) ${inputVar} = ${defaultFn}();`);
|
|
4559
|
-
return generateCheck(doc, ctx, def.innerType, inputVar);
|
|
4560
|
-
}
|
|
4561
|
-
const outputVar = newVar(ctx);
|
|
4562
|
-
if (defaultGetter) {
|
|
4563
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4564
|
-
const cloneFn = addConstant(ctx, shallowClone);
|
|
4565
|
-
doc.write(`let ${outputVar};`);
|
|
4566
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4567
|
-
doc.indented((d) => {
|
|
4568
|
-
d.write(`${outputVar} = ${cloneFn}(${defaultFn}());`);
|
|
4569
|
-
});
|
|
4570
|
-
doc.write(`} else {`);
|
|
4571
|
-
doc.indented((d) => {
|
|
4572
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4573
|
-
d.write(`${outputVar} = ${innerOutput} === undefined ? ${cloneFn}(${defaultFn}()) : ${innerOutput};`);
|
|
4574
|
-
});
|
|
4575
|
-
doc.write(`}`);
|
|
4576
|
-
} else {
|
|
4577
|
-
doc.write(`let ${outputVar};`);
|
|
4578
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4579
|
-
doc.indented((d) => {
|
|
4580
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4581
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4582
|
-
});
|
|
4583
|
-
doc.write(`}`);
|
|
4584
|
-
}
|
|
4585
|
-
return outputVar;
|
|
4586
|
-
}
|
|
4587
|
-
function generateNonOptionalCheck(doc, ctx, schema, accessor) {
|
|
4588
|
-
const def = schema._zod.def;
|
|
4589
|
-
const innerOutput = generateCheck(doc, ctx, def.innerType, accessor);
|
|
4590
|
-
const outputVar = newVar(ctx);
|
|
4591
|
-
doc.write(`const ${outputVar} = ${innerOutput};`);
|
|
4592
|
-
doc.write(`if (${outputVar} === undefined) return INVALID;`);
|
|
4593
|
-
return outputVar;
|
|
4594
|
-
}
|
|
4595
|
-
function generateTupleCheck(doc, ctx, schema, accessor) {
|
|
4596
|
-
const def = schema._zod.def;
|
|
4597
|
-
const items = def.items;
|
|
4598
|
-
const rest = def.rest;
|
|
4599
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4600
|
-
const optinStart = getTupleOptStart(items, "optin");
|
|
4601
|
-
const optoutStart = getTupleOptStart(items, "optout");
|
|
4602
|
-
if (rest) {
|
|
4603
|
-
doc.write(`if (${accessor}.length < ${optinStart}) return INVALID;`);
|
|
4604
|
-
} else {
|
|
4605
|
-
doc.write(`if (${accessor}.length < ${optinStart} || ${accessor}.length > ${items.length}) return INVALID;`);
|
|
4606
|
-
}
|
|
4607
|
-
const outputVar = newVar(ctx);
|
|
4608
|
-
doc.write(`const ${outputVar} = [];`);
|
|
4609
|
-
for (let i = 0;i < items.length; i++) {
|
|
4610
|
-
const itemSchema = items[i];
|
|
4611
|
-
if (i >= optoutStart) {
|
|
4612
|
-
doc.write(`if (${outputVar}.length === ${i}) {`);
|
|
4613
|
-
doc.indented((d) => {
|
|
4614
|
-
d.write(`if (${i} < ${accessor}.length) {`);
|
|
4615
|
-
d.indented((d2) => {
|
|
4616
|
-
const elemVar = newVar(ctx);
|
|
4617
|
-
d2.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4618
|
-
const elemOutput = compileChild(d2, ctx, itemSchema, elemVar);
|
|
4619
|
-
d2.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4620
|
-
});
|
|
4621
|
-
d.write(`} else {`);
|
|
4622
|
-
d.indented((d2) => {
|
|
4623
|
-
if (dropsWhenAbsent(itemSchema)) {
|
|
4624
|
-
d2.write(`${outputVar}.length = ${i};`);
|
|
4625
|
-
return;
|
|
4626
|
-
}
|
|
4627
|
-
const elemVar = newVar(ctx);
|
|
4628
|
-
const branchVar = newVar(ctx);
|
|
4629
|
-
d2.write(`const ${elemVar} = undefined;`);
|
|
4630
|
-
d2.write(`const ${branchVar} = (() => {`);
|
|
4631
|
-
d2.indented((d3) => {
|
|
4632
|
-
const elemOutput = compileChild(d3, ctx, itemSchema, elemVar);
|
|
4633
|
-
d3.write(`return ${elemOutput};`);
|
|
4634
|
-
});
|
|
4635
|
-
d2.write(`})();`);
|
|
4636
|
-
d2.write(`if (${branchVar} === INVALID || ${branchVar} === undefined) ${outputVar}.length = ${i};`);
|
|
4637
|
-
d2.write(`else ${outputVar}[${i}] = ${branchVar};`);
|
|
4638
|
-
});
|
|
4639
|
-
d.write(`}`);
|
|
4640
|
-
});
|
|
4641
|
-
doc.write(`}`);
|
|
4642
|
-
} else {
|
|
4643
|
-
const elemVar = newVar(ctx);
|
|
4644
|
-
doc.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4645
|
-
const elemOutput = compileChild(doc, ctx, itemSchema, elemVar);
|
|
4646
|
-
doc.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4647
|
-
}
|
|
4648
|
-
}
|
|
4649
|
-
if (rest) {
|
|
4650
|
-
const iVar = newVar(ctx);
|
|
4651
|
-
const elemVar = newVar(ctx);
|
|
4652
|
-
doc.write(`for (let ${iVar} = ${items.length}; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4653
|
-
doc.indented((d) => {
|
|
4654
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4655
|
-
const elemOutput = compileChild(d, ctx, rest, elemVar);
|
|
4656
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4657
|
-
});
|
|
4658
|
-
doc.write(`}`);
|
|
4659
|
-
}
|
|
4660
|
-
return outputVar;
|
|
4661
|
-
}
|
|
4662
|
-
function getTupleOptStart(items, key) {
|
|
4663
|
-
for (let i = items.length - 1;i >= 0; i--) {
|
|
4664
|
-
const omittable = key === "optin" ? items[i]._zod.optin !== undefined : items[i]._zod.optout === "optional";
|
|
4665
|
-
if (!omittable)
|
|
4666
|
-
return i + 1;
|
|
4667
|
-
}
|
|
4668
|
-
return 0;
|
|
4669
|
-
}
|
|
4670
|
-
function generateUnionCheck(doc, ctx, schema, accessor) {
|
|
4671
|
-
const def = schema._zod.def;
|
|
4672
|
-
const options = def.options;
|
|
4673
|
-
if (def.discriminator) {
|
|
4674
|
-
return generateDiscriminatedUnionCheck(doc, ctx, def, accessor);
|
|
4675
|
-
}
|
|
4676
|
-
if (def.inclusive === false) {
|
|
4677
|
-
throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
|
|
4678
|
-
}
|
|
4679
|
-
if (options.length === 0) {
|
|
4680
|
-
doc.write("return INVALID;");
|
|
4681
|
-
return accessor;
|
|
4682
|
-
}
|
|
4683
|
-
if (options.length === 1) {
|
|
4684
|
-
return generateCheck(doc, ctx, options[0], accessor);
|
|
4685
|
-
}
|
|
4686
|
-
const allLiterals = options.every((opt) => opt._zod.def.type === "literal" && !opt._zod.def.checks?.length);
|
|
4687
|
-
if (allLiterals) {
|
|
4688
|
-
const values = new Set(options.flatMap((opt) => opt._zod.def.values));
|
|
4689
|
-
const valuesConst = addConstant(ctx, values);
|
|
4690
|
-
doc.write(`if (!${valuesConst}.has(${accessor})) return INVALID;`);
|
|
4691
|
-
return accessor;
|
|
4692
|
-
}
|
|
4693
|
-
const outputVar = newVar(ctx);
|
|
4694
|
-
doc.write(`let ${outputVar};`);
|
|
4695
|
-
for (let i = 0;i < options.length; i++) {
|
|
4696
|
-
const opt = options[i];
|
|
4697
|
-
if (i === 0) {
|
|
4698
|
-
doc.write(`${outputVar} = (() => {`);
|
|
4699
|
-
} else {
|
|
4700
|
-
doc.write(`if (${outputVar} === INVALID) ${outputVar} = (() => {`);
|
|
4701
|
-
}
|
|
4702
|
-
doc.indented((d) => {
|
|
4703
|
-
const branchOutput = generateCheck(d, ctx, opt, accessor);
|
|
4704
|
-
d.write(`return ${branchOutput};`);
|
|
4705
|
-
});
|
|
4706
|
-
doc.write(`})();`);
|
|
4707
|
-
}
|
|
4708
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4709
|
-
return outputVar;
|
|
4710
|
-
}
|
|
4711
|
-
function generateDiscriminatedUnionCheck(doc, ctx, def, accessor) {
|
|
4712
|
-
if (def.unionFallback) {
|
|
4713
|
-
throw new ZodCompileUnsupportedError("discriminated union with unionFallback");
|
|
4714
|
-
}
|
|
4715
|
-
if (def.options.length === 0) {
|
|
4716
|
-
doc.write("return INVALID;");
|
|
4717
|
-
return accessor;
|
|
4718
|
-
}
|
|
4719
|
-
const discVar = newVar(ctx);
|
|
4720
|
-
const outputVar = newVar(ctx);
|
|
4721
|
-
doc.write(`const ${discVar} = ${accessor}?.[${esc(def.discriminator)}];`);
|
|
4722
|
-
doc.write(`let ${outputVar};`);
|
|
4723
|
-
let firstBranch = true;
|
|
4724
|
-
const claimed = new Set;
|
|
4725
|
-
for (const option of def.options) {
|
|
4726
|
-
const values = option._zod.propValues?.[def.discriminator];
|
|
4727
|
-
if (!values || values.size === 0) {
|
|
4728
|
-
throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
|
|
4729
|
-
}
|
|
4730
|
-
for (const value of values) {
|
|
4731
|
-
if (claimed.has(value)) {
|
|
4732
|
-
throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(value)}`);
|
|
4733
|
-
}
|
|
4734
|
-
claimed.add(value);
|
|
4735
|
-
}
|
|
4736
|
-
const conditions = Array.from(values, (value) => literalEquality(ctx, discVar, value));
|
|
4737
|
-
const prefix = firstBranch ? "if" : "else if";
|
|
4738
|
-
doc.write(`${prefix} (${conditions.join(" || ")}) {`);
|
|
4739
|
-
doc.indented((d) => {
|
|
4740
|
-
const branchOutput = generateCheck(d, ctx, option, accessor);
|
|
4741
|
-
d.write(`${outputVar} = ${branchOutput};`);
|
|
4742
|
-
});
|
|
4743
|
-
doc.write(`}`);
|
|
4744
|
-
firstBranch = false;
|
|
4745
|
-
}
|
|
4746
|
-
doc.write(`else { return INVALID; }`);
|
|
4747
|
-
return outputVar;
|
|
4748
|
-
}
|
|
4749
|
-
function literalEquality(ctx, accessor, value) {
|
|
4750
|
-
if (typeof value === "string")
|
|
4751
|
-
return `${accessor} === ${esc(value)}`;
|
|
4752
|
-
if (typeof value === "number") {
|
|
4753
|
-
if (Number.isNaN(value))
|
|
4754
|
-
return `Number.isNaN(${accessor})`;
|
|
4755
|
-
return `${accessor} === ${value}`;
|
|
4756
|
-
}
|
|
4757
|
-
if (typeof value === "boolean")
|
|
4758
|
-
return `${accessor} === ${value}`;
|
|
4759
|
-
if (value === null)
|
|
4760
|
-
return `${accessor} === null`;
|
|
4761
|
-
if (value === undefined)
|
|
4762
|
-
return `${accessor} === undefined`;
|
|
4763
|
-
if (typeof value === "bigint")
|
|
4764
|
-
return `${accessor} === ${value}n`;
|
|
4765
|
-
if (typeof value === "symbol") {
|
|
4766
|
-
const symbolConst = addConstant(ctx, value);
|
|
4767
|
-
return `${accessor} === ${symbolConst}`;
|
|
4768
|
-
}
|
|
4769
|
-
throw new ZodCompileUnsupportedError(`literal discriminator value ${String(value)}`);
|
|
4770
|
-
}
|
|
4771
|
-
function generateIntersectionCheck(doc, ctx, schema, accessor) {
|
|
4772
|
-
const def = schema._zod.def;
|
|
4773
|
-
ctx.definite = false;
|
|
4774
|
-
const leftOutput = compileChild(doc, ctx, def.left, accessor);
|
|
4775
|
-
const rightOutput = compileChild(doc, ctx, def.right, accessor);
|
|
4776
|
-
const mergeConst = addConstant(ctx, mergeValues);
|
|
4777
|
-
const mergedVar = newVar(ctx);
|
|
4778
|
-
doc.write(`const ${mergedVar} = ${mergeConst}(${leftOutput}, ${rightOutput});`);
|
|
4779
|
-
doc.write(`if (!${mergedVar}.valid) return INVALID;`);
|
|
4780
|
-
return `${mergedVar}.data`;
|
|
4781
|
-
}
|
|
4782
|
-
function generateRecordCheck(doc, ctx, schema, accessor) {
|
|
4783
|
-
const def = schema._zod.def;
|
|
4784
|
-
const isPlainObjectConst = addConstant(ctx, isPlainObject);
|
|
4785
|
-
doc.write(`if (!${isPlainObjectConst}(${accessor})) return INVALID;`);
|
|
4786
|
-
const outputVar = newVar(ctx);
|
|
4787
|
-
const kVar = newVar(ctx);
|
|
4788
|
-
const valVar = newVar(ctx);
|
|
4789
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4790
|
-
const recordDef = def;
|
|
4791
|
-
const keyValues = recordDef.partial ? undefined : def.keyType._zod.values;
|
|
4792
|
-
if (keyValues) {
|
|
4793
|
-
const inputKeys = [];
|
|
4794
|
-
for (const key of keyValues) {
|
|
4795
|
-
if (!(typeof key === "string" || typeof key === "number" || typeof key === "symbol")) {
|
|
4796
|
-
throw new ZodCompileUnsupportedError(`record key value ${String(key)}`);
|
|
4797
|
-
}
|
|
4798
|
-
const inputKey = typeof key === "number" ? key.toString() : key;
|
|
4799
|
-
if (inputKey === "__proto__") {
|
|
4800
|
-
throw new ZodCompileUnsupportedError('record key "__proto__"');
|
|
4801
|
-
}
|
|
4802
|
-
inputKeys.push(inputKey);
|
|
4803
|
-
const keyConst = addConstant(ctx, key);
|
|
4804
|
-
const outKey = generateCheck(doc, ctx, def.keyType, keyConst);
|
|
4805
|
-
const valueVar = newVar(ctx);
|
|
4806
|
-
doc.write(`const ${valueVar} = ${accessor}[${literalPropertyKey(ctx, inputKey)}];`);
|
|
4807
|
-
const valOutput = compileChild(doc, ctx, def.valueType, valueVar);
|
|
4808
|
-
doc.write(`${outputVar}[${outKey}] = ${valOutput};`);
|
|
4809
|
-
}
|
|
4810
|
-
const knownKeysConst = addConstant(ctx, new Set(inputKeys));
|
|
4811
|
-
doc.write(`for (const ${kVar} in ${accessor}) {`);
|
|
4812
|
-
doc.indented((d) => {
|
|
4813
|
-
d.write(`if (${knownKeysConst}.has(${kVar})) continue;`);
|
|
4814
|
-
if (recordDef.mode === "loose") {
|
|
4815
|
-
d.write(`if (${kVar} !== "__proto__") ${outputVar}[${kVar}] = ${accessor}[${kVar}];`);
|
|
4816
|
-
} else {
|
|
4817
|
-
d.write(`return INVALID;`);
|
|
4818
|
-
}
|
|
4819
|
-
});
|
|
4820
|
-
doc.write(`}`);
|
|
4821
|
-
return outputVar;
|
|
4822
|
-
}
|
|
4823
|
-
const keyDef = def.keyType._zod.def;
|
|
4824
|
-
const keyIsBareString = keyDef.type === "string" && keyDef.format === undefined && !keyDef.coerce && (keyDef.checks?.length ?? 0) === 0;
|
|
4825
|
-
if (!keyIsBareString) {
|
|
4826
|
-
const isLoose = def.mode === "loose";
|
|
4827
|
-
const keyFn = compileFn(def.keyType);
|
|
4828
|
-
if (keyFn.definite === false)
|
|
4829
|
-
ctx.definite = false;
|
|
4830
|
-
const keyFast = addConstant(ctx, keyFn);
|
|
4831
|
-
const numericConst = addConstant(ctx, number);
|
|
4832
|
-
const outKeyVar = newVar(ctx);
|
|
4833
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4834
|
-
d.write(`let ${outKeyVar} = ${keyFast}(${kVar});`);
|
|
4835
|
-
d.write(`if (${outKeyVar} === INVALID && typeof ${kVar} === "string" && ${numericConst}.test(${kVar})) ${outKeyVar} = ${keyFast}(Number(${kVar}));`);
|
|
4836
|
-
if (isLoose) {
|
|
4837
|
-
d.write(`if (${outKeyVar} === INVALID) { ${outputVar}[${kVar}] = ${accessor}[${kVar}]; continue; }`);
|
|
4838
|
-
} else {
|
|
4839
|
-
d.write(`if (${outKeyVar} === INVALID) return INVALID;`);
|
|
4840
|
-
}
|
|
4841
|
-
d.write(`if (${outKeyVar} === "__proto__") continue;`);
|
|
4842
|
-
const valueVar = newVar(ctx);
|
|
4843
|
-
d.write(`const ${valueVar} = ${accessor}[${kVar}];`);
|
|
4844
|
-
const valOutput = compileChild(d, ctx, def.valueType, valueVar);
|
|
4845
|
-
d.write(`${outputVar}[${outKeyVar}] = ${valOutput};`);
|
|
4846
|
-
});
|
|
4847
|
-
return outputVar;
|
|
4848
|
-
}
|
|
4849
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4850
|
-
d.write(`const ${valVar} = ${accessor}[${kVar}];`);
|
|
4851
|
-
const valOutput = compileChild(d, ctx, def.valueType, valVar);
|
|
4852
|
-
d.write(`${outputVar}[${kVar}] = ${valOutput};`);
|
|
4853
|
-
}, `return INVALID;`);
|
|
4854
|
-
return outputVar;
|
|
4855
|
-
}
|
|
4856
|
-
function emitOwnKeys(doc, ctx, accessor, kVar, body, onSymbol) {
|
|
4857
|
-
const propIsEnumerableConst = addConstant(ctx, Object.prototype.propertyIsEnumerable);
|
|
4858
|
-
const symsVar = newVar(ctx);
|
|
4859
|
-
const keysVar = newVar(ctx);
|
|
4860
|
-
const iVar = newVar(ctx);
|
|
4861
|
-
doc.write(`const ${symsVar} = Object.getOwnPropertySymbols(${accessor});`);
|
|
4862
|
-
doc.write(`const ${keysVar} = Object.getOwnPropertyNames(${accessor});`);
|
|
4863
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${keysVar}.length; ${iVar}++) {`);
|
|
4864
|
-
doc.indented((d) => {
|
|
4865
|
-
d.write(`const ${kVar} = ${keysVar}[${iVar}];`);
|
|
4866
|
-
d.write(`if (${kVar} === "__proto__" || !${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4867
|
-
body(d);
|
|
4868
|
-
});
|
|
4869
|
-
doc.write(`}`);
|
|
4870
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${symsVar}.length; ${iVar}++) {`);
|
|
4871
|
-
doc.indented((d) => {
|
|
4872
|
-
d.write(`const ${kVar} = ${symsVar}[${iVar}];`);
|
|
4873
|
-
d.write(`if (!${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4874
|
-
if (onSymbol)
|
|
4875
|
-
d.write(onSymbol);
|
|
4876
|
-
else
|
|
4877
|
-
body(d);
|
|
4878
|
-
});
|
|
4879
|
-
doc.write(`}`);
|
|
4880
|
-
}
|
|
4881
|
-
function literalPropertyKey(ctx, key) {
|
|
4882
|
-
if (typeof key === "string")
|
|
4883
|
-
return esc(key);
|
|
4884
|
-
return addConstant(ctx, key);
|
|
4885
|
-
}
|
|
4886
|
-
function generateMapCheck(doc, ctx, schema, accessor) {
|
|
4887
|
-
const def = schema._zod.def;
|
|
4888
|
-
doc.write(`if (!(${accessor} instanceof Map)) return INVALID;`);
|
|
4889
|
-
const outputVar = newVar(ctx);
|
|
4890
|
-
const kVar = newVar(ctx);
|
|
4891
|
-
const valVar = newVar(ctx);
|
|
4892
|
-
doc.write(`const ${outputVar} = new Map();`);
|
|
4893
|
-
doc.write(`for (const [${kVar}, ${valVar}] of ${accessor}) {`);
|
|
4894
|
-
doc.indented((d) => {
|
|
4895
|
-
const keyOutput = generateCheck(d, ctx, def.keyType, kVar);
|
|
4896
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4897
|
-
d.write(`${outputVar}.set(${keyOutput}, ${valOutput});`);
|
|
4898
|
-
});
|
|
4899
|
-
doc.write(`}`);
|
|
4900
|
-
return outputVar;
|
|
4901
|
-
}
|
|
4902
|
-
function generateSetCheck(doc, ctx, schema, accessor) {
|
|
4903
|
-
const def = schema._zod.def;
|
|
4904
|
-
doc.write(`if (!(${accessor} instanceof Set)) return INVALID;`);
|
|
4905
|
-
const outputVar = newVar(ctx);
|
|
4906
|
-
const valVar = newVar(ctx);
|
|
4907
|
-
doc.write(`const ${outputVar} = new Set();`);
|
|
4908
|
-
doc.write(`for (const ${valVar} of ${accessor}) {`);
|
|
4909
|
-
doc.indented((d) => {
|
|
4910
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4911
|
-
d.write(`${outputVar}.add(${valOutput});`);
|
|
4912
|
-
});
|
|
4913
|
-
doc.write(`}`);
|
|
4914
|
-
return outputVar;
|
|
4915
|
-
}
|
|
4916
|
-
function generateFileCheck(doc, accessor) {
|
|
4917
|
-
doc.write(`if (!(${accessor} instanceof File)) return INVALID;`);
|
|
4918
|
-
return accessor;
|
|
4919
|
-
}
|
|
4920
|
-
function generateTemplateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4921
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4922
|
-
const pattern = schema._zod.pattern;
|
|
4923
|
-
if (pattern) {
|
|
4924
|
-
const patternConst = addConstant(ctx, pattern);
|
|
4925
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
4926
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
4927
|
-
}
|
|
4928
|
-
return accessor;
|
|
4929
|
-
}
|
|
4930
|
-
function generateLazyCheck(doc, ctx, schema, accessor) {
|
|
4931
|
-
const def = schema._zod.def;
|
|
4932
|
-
const getterConst = addUserConstant(ctx, def.getter);
|
|
4933
|
-
const cacheConst = addConstant(ctx, { parser: null });
|
|
4934
|
-
doc.write(`if (!${cacheConst}.parser) {`);
|
|
4935
|
-
doc.indented((d) => {
|
|
4936
|
-
d.write(`const inner = ${getterConst}();`);
|
|
4937
|
-
d.write(`${cacheConst}.parser = function(input) {`);
|
|
4938
|
-
d.indented((d2) => {
|
|
4939
|
-
d2.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
|
|
4940
|
-
d2.write(`return result.issues.length === 0 ? result.value : INVALID;`);
|
|
4941
|
-
});
|
|
4942
|
-
d.write(`};`);
|
|
4943
|
-
});
|
|
4944
|
-
doc.write(`}`);
|
|
4945
|
-
const outputVar = newVar(ctx);
|
|
4946
|
-
doc.write(`const ${outputVar} = ${cacheConst}.parser(${accessor});`);
|
|
4947
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4948
|
-
return outputVar;
|
|
4949
|
-
}
|
|
4950
|
-
function generatePipeCheck(doc, ctx, schema, accessor) {
|
|
4951
|
-
const def = schema._zod.def;
|
|
4952
|
-
const inputOutput = generateCheck(doc, ctx, def.in, accessor);
|
|
4953
|
-
if (def.transform) {
|
|
4954
|
-
if (isAsyncFunction(def.transform)) {
|
|
4955
|
-
throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
|
|
4956
|
-
}
|
|
4957
|
-
const transformFn = def.transform;
|
|
4958
|
-
const helperFn = (value) => {
|
|
4959
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
4960
|
-
const result = transformFn(value, fakePayload);
|
|
4961
|
-
if (result instanceof Promise)
|
|
4962
|
-
return INVALID;
|
|
4963
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
4964
|
-
};
|
|
4965
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
4966
|
-
const transformedVar = newVar(ctx);
|
|
4967
|
-
doc.write(`const ${transformedVar} = ${helperConst}(${inputOutput});`);
|
|
4968
|
-
doc.write(`if (${transformedVar} === INVALID) return INVALID;`);
|
|
4969
|
-
return generateCheck(doc, ctx, def.out, transformedVar);
|
|
4970
|
-
} else {
|
|
4971
|
-
return generateCheck(doc, ctx, def.out, inputOutput);
|
|
4972
|
-
}
|
|
4973
|
-
}
|
|
4974
|
-
function isAsyncFunction(fn) {
|
|
4975
|
-
return typeof fn === "function" && (fn.constructor.name === "AsyncFunction" || fn[Symbol.toStringTag] === "AsyncFunction");
|
|
4976
|
-
}
|
|
4977
|
-
function generateCustomCheck(doc, ctx, schema, accessor) {
|
|
4978
|
-
const def = schema._zod.def;
|
|
4979
|
-
if (def.fn) {
|
|
4980
|
-
if (isAsyncFunction(def.fn)) {
|
|
4981
|
-
throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
|
|
4982
|
-
}
|
|
4983
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
4984
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
4985
|
-
const resVar = newVar(ctx);
|
|
4986
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
4987
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
4988
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
4989
|
-
} else {
|
|
4990
|
-
throw new ZodCompileUnsupportedError("custom schema without a predicate function");
|
|
4991
|
-
}
|
|
4992
|
-
return accessor;
|
|
4993
|
-
}
|
|
4994
|
-
function runtimeCatch(innerSchema, catchValue, value) {
|
|
4995
|
-
const result = innerSchema._zod.run({ value, issues: [] }, {});
|
|
4996
|
-
if (result && typeof result.then === "function")
|
|
4997
|
-
return INVALID;
|
|
4998
|
-
const r = result;
|
|
4999
|
-
if (r.issues.length === 0)
|
|
5000
|
-
return r.value;
|
|
5001
|
-
return catchValue();
|
|
5002
|
-
}
|
|
5003
|
-
function generateCatchCheck(doc, ctx, schema, accessor) {
|
|
5004
|
-
const def = schema._zod.def;
|
|
5005
|
-
if (!def.catchValue[CONSTANT_CATCH]) {
|
|
5006
|
-
throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
|
|
5007
|
-
}
|
|
5008
|
-
const outputVar = newVar(ctx);
|
|
5009
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
5010
|
-
doc.indented((d) => {
|
|
5011
|
-
const innerOut = compileChild(d, ctx, def.innerType, accessor);
|
|
5012
|
-
d.write(`return ${innerOut};`);
|
|
5013
|
-
});
|
|
5014
|
-
doc.write(`})();`);
|
|
5015
|
-
const innerConst = addConstant(ctx, def.innerType);
|
|
5016
|
-
const catchConst = addUserConstant(ctx, def.catchValue);
|
|
5017
|
-
const catchHelperConst = addConstant(ctx, runtimeCatch);
|
|
5018
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
5019
|
-
doc.indented((d) => {
|
|
5020
|
-
d.write(`${outputVar} = ${catchHelperConst}(${innerConst}, ${catchConst}, ${accessor});`);
|
|
5021
|
-
d.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5022
|
-
});
|
|
5023
|
-
doc.write(`}`);
|
|
5024
|
-
return outputVar;
|
|
5025
|
-
}
|
|
5026
|
-
function generateTransformCheck(doc, ctx, schema, accessor) {
|
|
5027
|
-
const def = schema._zod.def;
|
|
5028
|
-
if (def.transform) {
|
|
5029
|
-
if (isAsyncFunction(def.transform)) {
|
|
5030
|
-
throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
|
|
5031
|
-
}
|
|
5032
|
-
const transformFn = def.transform;
|
|
5033
|
-
const helperFn = (value) => {
|
|
5034
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
5035
|
-
const result = transformFn(value, fakePayload);
|
|
5036
|
-
if (result instanceof Promise)
|
|
5037
|
-
return INVALID;
|
|
5038
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
5039
|
-
};
|
|
5040
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
5041
|
-
const outputVar = newVar(ctx);
|
|
5042
|
-
doc.write(`const ${outputVar} = ${helperConst}(${accessor});`);
|
|
5043
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5044
|
-
return outputVar;
|
|
5045
|
-
}
|
|
5046
|
-
return accessor;
|
|
5047
|
-
}
|
|
5048
3410
|
// node_modules/zod/v4/core/api.js
|
|
5049
3411
|
function snapshotChecks(def) {
|
|
5050
3412
|
if (def.checks)
|
|
@@ -8487,7 +6849,6 @@ var advertisedOperationSchemas = {
|
|
|
8487
6849
|
worker: advertised(workerOperations),
|
|
8488
6850
|
state: advertised(stateOperations)
|
|
8489
6851
|
};
|
|
8490
|
-
var compiledOperationSchemas = Object.fromEntries(OPERATION_FAMILIES.map((family) => [family, compile(operationSchemas[family])]));
|
|
8491
6852
|
var success = (revisionValue, workspaceRevisionValue, data) => ({
|
|
8492
6853
|
ok: true,
|
|
8493
6854
|
schemaVersion: 1,
|
|
@@ -8509,8 +6870,7 @@ function resolveIdRef(kind, ref, ids) {
|
|
|
8509
6870
|
function parseOperation(family, input) {
|
|
8510
6871
|
if (typeof input === "object" && input !== null && "schemaVersion" in input && input.schemaVersion !== SCHEMA_VERSION)
|
|
8511
6872
|
return failure2("unsupported_version", "unsupported schema version", { operation: family });
|
|
8512
|
-
const
|
|
8513
|
-
const parsed = compiled.success ? operationSchemas[family].safeParse(input) : compiled;
|
|
6873
|
+
const parsed = operationSchemas[family].safeParse(input);
|
|
8514
6874
|
if (parsed.success)
|
|
8515
6875
|
return success(null, null, parsed.data);
|
|
8516
6876
|
const fields = parsed.error.issues.map((issue) => ({
|
|
@@ -17859,6 +16219,24 @@ function workitInvocations(command, dialect = "posix", depth = 0) {
|
|
|
17859
16219
|
}
|
|
17860
16220
|
return found;
|
|
17861
16221
|
}
|
|
16222
|
+
var WORKIT_GLOBAL = /^--(?:cwd|session|token|as)(=|$)/;
|
|
16223
|
+
function skipWorkitGlobals(args) {
|
|
16224
|
+
let cwd = null;
|
|
16225
|
+
let at = 0;
|
|
16226
|
+
for (;at < args.length; at++) {
|
|
16227
|
+
const match = WORKIT_GLOBAL.exec(args[at]);
|
|
16228
|
+
if (match?.[1] === "=") {
|
|
16229
|
+
if (args[at].startsWith("--cwd="))
|
|
16230
|
+
cwd = args[at].slice(6);
|
|
16231
|
+
} else if (match && args[at + 1] !== undefined) {
|
|
16232
|
+
if (args[at] === "--cwd")
|
|
16233
|
+
cwd = args[at + 1];
|
|
16234
|
+
at++;
|
|
16235
|
+
} else if (args[at] !== "--json")
|
|
16236
|
+
break;
|
|
16237
|
+
}
|
|
16238
|
+
return { rest: args.slice(at), cwd };
|
|
16239
|
+
}
|
|
17862
16240
|
var PUSH_VALUE_OPTIONS = new Set(["-o", "--push-option", "--receive-pack", "--exec", "--repo"]);
|
|
17863
16241
|
var CURRENT_BRANCH = /^\$\(\s*git\s+(?:branch\s+--show-current|rev-parse\s+--abbrev-ref\s+HEAD|symbolic-ref\s+--short\s+HEAD)\s*\)$/;
|
|
17864
16242
|
var pushDestination = (destination) => {
|
|
@@ -22989,7 +21367,7 @@ function heavyGate(cwd, command, options) {
|
|
|
22989
21367
|
}
|
|
22990
21368
|
}
|
|
22991
21369
|
// packages/workit-core/src/hooks/write-gate.ts
|
|
22992
|
-
import { realpathSync as realpathSync6 } from "node:fs";
|
|
21370
|
+
import { existsSync as existsSync9, realpathSync as realpathSync6 } from "node:fs";
|
|
22993
21371
|
import os10 from "node:os";
|
|
22994
21372
|
import path31 from "node:path";
|
|
22995
21373
|
|
|
@@ -23218,7 +21596,7 @@ var exempt = (cwd, root, file, plans) => {
|
|
|
23218
21596
|
const segments = posix.split("/");
|
|
23219
21597
|
return /\.mdx?$/i.test(posix) || segments[0] === "docs" || segments.includes("plans") || [...plans].some((plan) => fold(plan) === posix);
|
|
23220
21598
|
};
|
|
23221
|
-
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs"]);
|
|
21599
|
+
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs", "busybox"]);
|
|
23222
21600
|
var operands = (words) => words.filter((item) => !item.startsWith("-"));
|
|
23223
21601
|
var SCRIPT_SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
|
|
23224
21602
|
var wrappedScript = (input) => {
|
|
@@ -23230,9 +21608,9 @@ var wrappedScript = (input) => {
|
|
|
23230
21608
|
return null;
|
|
23231
21609
|
if ("script" in tail)
|
|
23232
21610
|
return tail.script;
|
|
23233
|
-
const
|
|
23234
|
-
if (SCRIPT_SHELLS.has((
|
|
23235
|
-
return script
|
|
21611
|
+
const script = shellScript2(tail.argv);
|
|
21612
|
+
if (SCRIPT_SHELLS.has((tail.argv[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase()) && script)
|
|
21613
|
+
return script;
|
|
23236
21614
|
return tail.argv.map(quoteWord2).join(" ");
|
|
23237
21615
|
};
|
|
23238
21616
|
var editsOf = (input) => {
|
|
@@ -23311,6 +21689,94 @@ var editsOf = (input) => {
|
|
|
23311
21689
|
return [];
|
|
23312
21690
|
}
|
|
23313
21691
|
};
|
|
21692
|
+
var shellScript2 = (words) => {
|
|
21693
|
+
if (!SCRIPT_SHELLS.has((words[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase()))
|
|
21694
|
+
return null;
|
|
21695
|
+
for (let index = 1;index < words.length; index++) {
|
|
21696
|
+
const word = words[index];
|
|
21697
|
+
if (/^-[a-z]*c$/.test(word))
|
|
21698
|
+
return words[index + 1] ?? null;
|
|
21699
|
+
if (/^[-+][oO]$/.test(word))
|
|
21700
|
+
index++;
|
|
21701
|
+
else if (!/^[-+]/.test(word))
|
|
21702
|
+
return null;
|
|
21703
|
+
}
|
|
21704
|
+
return null;
|
|
21705
|
+
};
|
|
21706
|
+
var XARGS_VALUE = /^-[IinLPdsEa]$/;
|
|
21707
|
+
var runsWriter = (words) => {
|
|
21708
|
+
const filled = [...words.filter((word) => word !== "{}" && word !== ";" && word !== "+"), "x"];
|
|
21709
|
+
const edits = editsOf(filled);
|
|
21710
|
+
return edits === null || edits.length > 0 || opaqueWrites(filled) !== null;
|
|
21711
|
+
};
|
|
21712
|
+
var opaqueWrites = (input) => {
|
|
21713
|
+
let words = input;
|
|
21714
|
+
while (words.length && (PREFIXES2.has(words[0]) || /^\w+=/.test(words[0]))) {
|
|
21715
|
+
if (words[0] === "xargs") {
|
|
21716
|
+
let at = 1;
|
|
21717
|
+
while (words[at]?.startsWith("-"))
|
|
21718
|
+
at += XARGS_VALUE.test(words[at]) ? 2 : 1;
|
|
21719
|
+
return runsWriter(words.slice(at)) ? ["."] : null;
|
|
21720
|
+
}
|
|
21721
|
+
words = words.slice(1);
|
|
21722
|
+
}
|
|
21723
|
+
const [verb, ...args] = words;
|
|
21724
|
+
const name = (verb?.split(/[\\/]/).at(-1) ?? "").toLowerCase();
|
|
21725
|
+
const files = operands(args);
|
|
21726
|
+
const has = (pattern) => args.some((arg) => pattern.test(arg));
|
|
21727
|
+
const listed = (named) => named.length ? named : ["."];
|
|
21728
|
+
if (name === "unlink" || name === "shred")
|
|
21729
|
+
return listed(files);
|
|
21730
|
+
if (/^(?:chmod|chown|chgrp|chattr)$/.test(name))
|
|
21731
|
+
return listed(has(/^--reference/) ? files : files.slice(1));
|
|
21732
|
+
if (name === "rsync") {
|
|
21733
|
+
const dest = files.at(-1);
|
|
21734
|
+
const remote = /^[\w.@-]*:/.test(dest ?? "") && !/^[A-Za-z]:[\\/]/.test(dest ?? "");
|
|
21735
|
+
return dest === undefined ? ["."] : remote ? [] : [dest];
|
|
21736
|
+
}
|
|
21737
|
+
if (name === "find") {
|
|
21738
|
+
const exec = args.findIndex((arg) => /^-(?:exec|execdir|ok|okdir)$/.test(arg));
|
|
21739
|
+
const writes = has(/^-(?:delete|fprint\w*|fls)$/) || exec >= 0 && runsWriter(args.slice(exec + 1));
|
|
21740
|
+
const end = args.findIndex((arg) => /^[-(!]/.test(arg));
|
|
21741
|
+
return writes ? listed(end < 0 ? args : args.slice(0, end)) : null;
|
|
21742
|
+
}
|
|
21743
|
+
if (/^g?awk$/.test(name)) {
|
|
21744
|
+
if (!has(/^-i|^--include/))
|
|
21745
|
+
return null;
|
|
21746
|
+
const named = [];
|
|
21747
|
+
let program = !has(/^-f$|^--file/);
|
|
21748
|
+
for (let index = 0;index < args.length; index++)
|
|
21749
|
+
if (/^(?:-[ifvF]|--(?:include|file|assign|field-separator))$/.test(args[index]))
|
|
21750
|
+
index++;
|
|
21751
|
+
else if (args[index].startsWith("-"))
|
|
21752
|
+
continue;
|
|
21753
|
+
else if (program)
|
|
21754
|
+
program = false;
|
|
21755
|
+
else
|
|
21756
|
+
named.push(args[index]);
|
|
21757
|
+
return listed(named);
|
|
21758
|
+
}
|
|
21759
|
+
if (name === "perl")
|
|
21760
|
+
return has(/^-[a-zA-Z]*[eE]/) ? ["."] : null;
|
|
21761
|
+
if (/^(?:python[\d.]*|node|bun|deno|ruby|php)$/.test(name))
|
|
21762
|
+
return has(/^-[a-zA-Z]*[ce]$|^--eval$|^-p$/) ? ["."] : null;
|
|
21763
|
+
return shellScript2(words) === null ? null : ["."];
|
|
21764
|
+
};
|
|
21765
|
+
var linkSources = (args, at) => {
|
|
21766
|
+
const names = operands([...args]);
|
|
21767
|
+
const into = args.indexOf("-t");
|
|
21768
|
+
const dest = into >= 0 ? args[into + 1] : names.length > 1 ? names.at(-1) : ".";
|
|
21769
|
+
const sources = names.filter((name, index) => into >= 0 ? name !== dest : index < names.length - 1 || names.length === 1);
|
|
21770
|
+
const target = dest === undefined ? null : at(dest);
|
|
21771
|
+
const symbolic = args.some((arg) => /^-[a-zA-Z]*s|^--symbolic$/.test(arg));
|
|
21772
|
+
const base = target === null ? null : into >= 0 || sources.length > 1 || existsSync9(target) || /[\\/]$/.test(dest ?? "") ? target : path31.dirname(target);
|
|
21773
|
+
return sources.flatMap((source) => {
|
|
21774
|
+
if (symbolic && base !== null && !path31.isAbsolute(homeExpanded(source)))
|
|
21775
|
+
return [path31.resolve(base, source)];
|
|
21776
|
+
const resolved = at(source);
|
|
21777
|
+
return resolved ? [resolved] : [];
|
|
21778
|
+
});
|
|
21779
|
+
};
|
|
23314
21780
|
var shellWrites = (command, depth = 0) => {
|
|
23315
21781
|
const targets = [];
|
|
23316
21782
|
let unknown = false;
|
|
@@ -23420,11 +21886,15 @@ var gitMutationDirs = (cwd, command, dialect) => {
|
|
|
23420
21886
|
var CD2 = new Set(["cd", "chdir", "set-location", "sl"]);
|
|
23421
21887
|
var PUSHD2 = new Set(["pushd", "push-location"]);
|
|
23422
21888
|
var POPD2 = new Set(["popd", "pop-location"]);
|
|
23423
|
-
var
|
|
21889
|
+
var UNKNOWN_DIR = path31.resolve(path31.parse(process.cwd()).root, ".workit-unknown-dir");
|
|
21890
|
+
var cdInto = (dir, words, strict = false) => {
|
|
21891
|
+
const lost = strict ? UNKNOWN_DIR : undefined;
|
|
23424
21892
|
const named = words.slice(1).find((word) => !word.startsWith("-") || word === "-");
|
|
23425
21893
|
const target = named === undefined ? undefined : homeExpanded(named);
|
|
23426
|
-
if (
|
|
23427
|
-
return;
|
|
21894
|
+
if (target === "-" || target && /^~|[$`*?]/.test(target))
|
|
21895
|
+
return lost;
|
|
21896
|
+
if (dir === undefined)
|
|
21897
|
+
return lost;
|
|
23428
21898
|
return target === undefined ? os10.homedir() : path31.resolve(dir, target);
|
|
23429
21899
|
};
|
|
23430
21900
|
var quoteWord2 = (word) => /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", "'\\''")}'`;
|
|
@@ -23432,7 +21902,7 @@ var homeExpanded = (file) => {
|
|
|
23432
21902
|
const match = /^(?:~|\$HOME|\$\{HOME\})(?=\/|$)/.exec(file);
|
|
23433
21903
|
return match ? (process.env.HOME || os10.homedir()) + file.slice(match[0].length) : file;
|
|
23434
21904
|
};
|
|
23435
|
-
var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
21905
|
+
var segmentTargets = (cwd, command, dialect, depth = 0, strict = false) => {
|
|
23436
21906
|
const targets = [];
|
|
23437
21907
|
let dir = cwd;
|
|
23438
21908
|
let stack = [];
|
|
@@ -23456,21 +21926,35 @@ var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
|
23456
21926
|
const words = lead < 0 ? [] : segment.words.slice(lead);
|
|
23457
21927
|
const name = (words[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase();
|
|
23458
21928
|
if (CD2.has(name)) {
|
|
23459
|
-
dir = cdInto(dir, words);
|
|
21929
|
+
dir = cdInto(dir, words, strict);
|
|
23460
21930
|
continue;
|
|
23461
21931
|
}
|
|
23462
21932
|
if (PUSHD2.has(name)) {
|
|
23463
21933
|
stack.push(dir);
|
|
23464
|
-
dir = cdInto(dir, words);
|
|
21934
|
+
dir = cdInto(dir, words, strict);
|
|
23465
21935
|
continue;
|
|
23466
21936
|
}
|
|
23467
21937
|
if (POPD2.has(name)) {
|
|
23468
|
-
dir = stack.length ? stack.pop() : undefined;
|
|
21938
|
+
dir = stack.length ? stack.pop() : strict ? UNKNOWN_DIR : undefined;
|
|
23469
21939
|
continue;
|
|
23470
21940
|
}
|
|
23471
|
-
const inner = depth < 4 ? wrappedScript(words) : null;
|
|
21941
|
+
const inner = depth < 4 ? wrappedScript(words) ?? (strict ? shellScript2(words) : null) : null;
|
|
23472
21942
|
if (inner !== null && dir !== undefined)
|
|
23473
|
-
targets.push(...segmentTargets(dir, inner, "posix", depth + 1));
|
|
21943
|
+
targets.push(...segmentTargets(dir, inner, "posix", depth + 1, strict));
|
|
21944
|
+
if (strict && dir !== undefined) {
|
|
21945
|
+
const runIn = dir;
|
|
21946
|
+
for (const sub of depth < 4 ? segment.subs ?? [] : [])
|
|
21947
|
+
targets.push(...segmentTargets(runIn, sub, dialect, depth + 1, true));
|
|
21948
|
+
const opaque = segment.unanalyzable ? ["."] : inner === null ? opaqueWrites(words) : null;
|
|
21949
|
+
for (const file of opaque ?? []) {
|
|
21950
|
+
const resolved = at(file);
|
|
21951
|
+
if (resolved)
|
|
21952
|
+
targets.push(file === "." ? path31.join(resolved, DIR_TARGET) : resolved);
|
|
21953
|
+
}
|
|
21954
|
+
const ln = words.findIndex((word) => /^ln$/i.test(word.split(/[\\/]/).at(-1) ?? ""));
|
|
21955
|
+
if (ln >= 0 && words.slice(0, ln).every((word) => PREFIXES2.has(word)))
|
|
21956
|
+
targets.push(...linkSources(words.slice(ln + 1), at));
|
|
21957
|
+
}
|
|
23474
21958
|
for (const file of segment.redirects)
|
|
23475
21959
|
if (!/^\/dev\/(?:null|stdout|stderr|tty)$/.test(file)) {
|
|
23476
21960
|
const resolved = at(file);
|
|
@@ -23487,11 +21971,29 @@ var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
|
23487
21971
|
if (resolved)
|
|
23488
21972
|
targets.push(resolved);
|
|
23489
21973
|
}
|
|
23490
|
-
if (dir !== undefined
|
|
23491
|
-
|
|
21974
|
+
if (dir !== undefined)
|
|
21975
|
+
for (const invocation of workitInvocations(segment.words.map(quoteWord2).join(" "), "posix")) {
|
|
21976
|
+
const { at: runsIn, args } = workitCwd(dir, invocation);
|
|
21977
|
+
if (args[0] === "git" && MUTATING_WORKIT_GIT.has(args[1] ?? "") && !inert(args.slice(2), ""))
|
|
21978
|
+
targets.push(path31.join(runsIn, DIR_TARGET));
|
|
21979
|
+
}
|
|
23492
21980
|
}
|
|
23493
21981
|
return targets;
|
|
23494
21982
|
};
|
|
21983
|
+
var workitCwd = (dir, args) => {
|
|
21984
|
+
const { rest, cwd } = skipWorkitGlobals(args);
|
|
21985
|
+
return { at: cwd === null ? dir : path31.resolve(dir, homeExpanded(cwd)), args: rest };
|
|
21986
|
+
};
|
|
21987
|
+
var shellMutationTargets = (cwd, command, dialect = "posix", strict = false) => {
|
|
21988
|
+
try {
|
|
21989
|
+
return [
|
|
21990
|
+
...segmentTargets(cwd, command, dialect, 0, strict),
|
|
21991
|
+
...gitMutationDirs(cwd, command, dialect).map((dir) => path31.join(dir, DIR_TARGET))
|
|
21992
|
+
];
|
|
21993
|
+
} catch {
|
|
21994
|
+
return [];
|
|
21995
|
+
}
|
|
21996
|
+
};
|
|
23495
21997
|
var overridden = (command, dialect) => {
|
|
23496
21998
|
const segments = segmentsOf(command, dialect);
|
|
23497
21999
|
if (segments.length !== 1 || segments[0].group || segments[0].subs?.length)
|
|
@@ -23597,16 +22099,8 @@ function shellGate(cwd, command, owner, dialect = "posix", deps) {
|
|
|
23597
22099
|
} catch {}
|
|
23598
22100
|
const writes = shellWrites(command);
|
|
23599
22101
|
let targets = [];
|
|
23600
|
-
if (owner)
|
|
23601
|
-
|
|
23602
|
-
targets = [
|
|
23603
|
-
...segmentTargets(cwd, command, dialect),
|
|
23604
|
-
...gitMutationDirs(cwd, command, dialect).map((dir) => path31.join(dir, DIR_TARGET))
|
|
23605
|
-
];
|
|
23606
|
-
} catch {
|
|
23607
|
-
targets = [];
|
|
23608
|
-
}
|
|
23609
|
-
}
|
|
22102
|
+
if (owner)
|
|
22103
|
+
targets = shellMutationTargets(cwd, command, dialect);
|
|
23610
22104
|
if (owner && targets.length) {
|
|
23611
22105
|
const live = liveGate(owner, targets, deps);
|
|
23612
22106
|
if (live.kind !== "none") {
|
|
@@ -24184,6 +22678,19 @@ var standingSinceStart = (input) => {
|
|
|
24184
22678
|
// packages/workit-core/src/hooks/cwd-normalize.ts
|
|
24185
22679
|
var FILTERS = new Set(["tail", "head", "grep"]);
|
|
24186
22680
|
|
|
22681
|
+
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
22682
|
+
var CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
|
|
22683
|
+
var MUTATING_REMOTE = new Set([
|
|
22684
|
+
"add",
|
|
22685
|
+
"remove",
|
|
22686
|
+
"rm",
|
|
22687
|
+
"rename",
|
|
22688
|
+
"set-url",
|
|
22689
|
+
"set-head",
|
|
22690
|
+
"set-branches",
|
|
22691
|
+
"prune"
|
|
22692
|
+
]);
|
|
22693
|
+
|
|
24187
22694
|
// packages/workit-core/src/hooks/handle.ts
|
|
24188
22695
|
var offered = new Set;
|
|
24189
22696
|
var PRE_TOOL = new Set(["shell.pre", "write.pre", "tool.pre"]);
|
|
@@ -24292,7 +22799,7 @@ var OPENCODE_DESCRIPTOR = {
|
|
|
24292
22799
|
import path37 from "node:path";
|
|
24293
22800
|
|
|
24294
22801
|
// packages/workit-core/src/core/gitignore.ts
|
|
24295
|
-
import { existsSync as
|
|
22802
|
+
import { existsSync as existsSync10, readFileSync as readFileSync6, writeFileSync as writeFileSync4 } from "node:fs";
|
|
24296
22803
|
import path33 from "node:path";
|
|
24297
22804
|
var GITIGNORE_ENTRIES = [
|
|
24298
22805
|
"# workit: SDD working state (never commit)",
|
|
@@ -24314,7 +22821,7 @@ var ensureProjectGitignore = (workspaceRoot, confirmed) => {
|
|
|
24314
22821
|
if (!confirmed)
|
|
24315
22822
|
return { ok: false, error: "confirmed: true required" };
|
|
24316
22823
|
const file = path33.join(workspaceRoot, ".gitignore");
|
|
24317
|
-
const existing =
|
|
22824
|
+
const existing = existsSync10(file) ? readFileSync6(file, "utf8") : "";
|
|
24318
22825
|
const existingLines = new Set(existing.split(`
|
|
24319
22826
|
`).map((l) => l.trim()).filter(Boolean));
|
|
24320
22827
|
const added = [];
|
|
@@ -24333,19 +22840,19 @@ var ensureProjectGitignore = (workspaceRoot, confirmed) => {
|
|
|
24333
22840
|
` : "") + append.join(`
|
|
24334
22841
|
`) + `
|
|
24335
22842
|
`, "utf8");
|
|
24336
|
-
} else if (!
|
|
22843
|
+
} else if (!existsSync10(file)) {
|
|
24337
22844
|
writeFileSync4(file, "", "utf8");
|
|
24338
22845
|
}
|
|
24339
22846
|
return { ok: true, path: file, added };
|
|
24340
22847
|
};
|
|
24341
22848
|
|
|
24342
22849
|
// packages/workit-core/src/core/hygiene.ts
|
|
24343
|
-
import { existsSync as
|
|
22850
|
+
import { existsSync as existsSync11, readFileSync as readFileSync7, readdirSync as readdirSync5, writeFileSync as writeFileSync5 } from "node:fs";
|
|
24344
22851
|
import path34 from "node:path";
|
|
24345
22852
|
var repoRoot3 = assetRoot();
|
|
24346
22853
|
var templatesDir = () => path34.join(repoRoot3, "templates", "hygiene");
|
|
24347
22854
|
var packageJson = (root) => {
|
|
24348
|
-
if (!
|
|
22855
|
+
if (!existsSync11(path34.join(root, "package.json")))
|
|
24349
22856
|
return null;
|
|
24350
22857
|
try {
|
|
24351
22858
|
return JSON.parse(readFileSync7(path34.join(root, "package.json"), "utf8"));
|
|
@@ -24354,7 +22861,7 @@ var packageJson = (root) => {
|
|
|
24354
22861
|
}
|
|
24355
22862
|
};
|
|
24356
22863
|
var isOpenSource = (root) => {
|
|
24357
|
-
if (
|
|
22864
|
+
if (existsSync11(path34.join(root, "LICENSE")))
|
|
24358
22865
|
return true;
|
|
24359
22866
|
const pkg = packageJson(root);
|
|
24360
22867
|
if (pkg && !pkg.private)
|
|
@@ -24382,10 +22889,10 @@ var planHygieneFiles = (root, opts = {}) => {
|
|
|
24382
22889
|
files.push("LICENSE", "CONTRIBUTING.md");
|
|
24383
22890
|
const planned = [];
|
|
24384
22891
|
for (const file of files) {
|
|
24385
|
-
if (
|
|
22892
|
+
if (existsSync11(path34.join(root, file)))
|
|
24386
22893
|
continue;
|
|
24387
22894
|
const tpl = path34.join(templatesDir(), file);
|
|
24388
|
-
if (!
|
|
22895
|
+
if (!existsSync11(tpl))
|
|
24389
22896
|
continue;
|
|
24390
22897
|
const content = readFileSync7(tpl, "utf8").replace(/<PROJECT>/g, path34.basename(root)).replace(/<YEAR>/g, String(new Date().getFullYear())).replace(/<HOLDER>\s*/g, licenseHolder(root));
|
|
24391
22898
|
planned.push({ path: path34.join(root, file), content });
|
|
@@ -24930,7 +23437,7 @@ import path41 from "node:path";
|
|
|
24930
23437
|
|
|
24931
23438
|
// packages/workit-core/src/git/ops.ts
|
|
24932
23439
|
import { spawnSync as spawnSync20 } from "node:child_process";
|
|
24933
|
-
import { existsSync as
|
|
23440
|
+
import { existsSync as existsSync12, readFileSync as readFileSync9 } from "node:fs";
|
|
24934
23441
|
import path40 from "node:path";
|
|
24935
23442
|
|
|
24936
23443
|
// packages/workit-core/src/forge/redact.ts
|
|
@@ -24979,11 +23486,11 @@ function operationInProgress(cwd) {
|
|
|
24979
23486
|
const conflicted = () => git5(cwd, ["ls-files", "-u"]).stdout.trim() !== "";
|
|
24980
23487
|
for (const [index, [, operation]] of markers.entries()) {
|
|
24981
23488
|
const marker = found[index];
|
|
24982
|
-
if (!marker || !
|
|
23489
|
+
if (!marker || !existsSync12(marker))
|
|
24983
23490
|
continue;
|
|
24984
|
-
if (index === 1 &&
|
|
23491
|
+
if (index === 1 && existsSync12(path40.join(marker, "applying")))
|
|
24985
23492
|
return { operation: "am", commitAllowed: false, branch: null };
|
|
24986
|
-
if (index === 0 &&
|
|
23493
|
+
if (index === 0 && existsSync12(path40.join(marker, "amend")) && !conflicted()) {
|
|
24987
23494
|
let headName = "";
|
|
24988
23495
|
try {
|
|
24989
23496
|
headName = readFileSync9(path40.join(marker, "head-name"), "utf8").trim();
|
|
@@ -26278,11 +24785,11 @@ var createStopControl = (root, port) => {
|
|
|
26278
24785
|
};
|
|
26279
24786
|
|
|
26280
24787
|
// packages/workit-opencode/src/v2/registry.ts
|
|
26281
|
-
import { existsSync as
|
|
24788
|
+
import { existsSync as existsSync14, readFileSync as readFileSync10 } from "node:fs";
|
|
26282
24789
|
import path45 from "node:path";
|
|
26283
24790
|
|
|
26284
24791
|
// packages/workit-opencode/src/shared/assets.ts
|
|
26285
|
-
import { existsSync as
|
|
24792
|
+
import { existsSync as existsSync13, realpathSync as realpathSync9 } from "node:fs";
|
|
26286
24793
|
import path44 from "node:path";
|
|
26287
24794
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
26288
24795
|
var moduleDir = () => {
|
|
@@ -26299,7 +24806,7 @@ var packageRoot2 = () => {
|
|
|
26299
24806
|
path44.resolve(dir, "..", ".."),
|
|
26300
24807
|
path44.resolve(dir, "..", "..", "..")
|
|
26301
24808
|
]) {
|
|
26302
|
-
if (
|
|
24809
|
+
if (existsSync13(path44.join(candidate, "package.json")))
|
|
26303
24810
|
return candidate;
|
|
26304
24811
|
}
|
|
26305
24812
|
return path44.resolve(dir, "..");
|
|
@@ -26307,7 +24814,7 @@ var packageRoot2 = () => {
|
|
|
26307
24814
|
var skillsRoot = () => {
|
|
26308
24815
|
const root = packageRoot2();
|
|
26309
24816
|
const canonical = path44.resolve(root, "..", "workit-core", "skills");
|
|
26310
|
-
if (
|
|
24817
|
+
if (existsSync13(path44.join(root, "src")) && existsSync13(canonical))
|
|
26311
24818
|
return canonical;
|
|
26312
24819
|
return path44.join(root, "assets", "skills");
|
|
26313
24820
|
};
|
|
@@ -26333,7 +24840,7 @@ var parseFrontmatter = (markdown) => {
|
|
|
26333
24840
|
};
|
|
26334
24841
|
var registerSkills = async (ctx) => {
|
|
26335
24842
|
const skillsDir = skillsRoot();
|
|
26336
|
-
if (!
|
|
24843
|
+
if (!existsSync14(skillsDir))
|
|
26337
24844
|
return new Set;
|
|
26338
24845
|
const listed = await ctx.skill.list().catch(() => ({ data: [] }));
|
|
26339
24846
|
const own = (skill) => typeof skill.path === "string" && path45.resolve(skill.path).startsWith(skillsDir + path45.sep);
|
|
@@ -26341,7 +24848,7 @@ var registerSkills = async (ctx) => {
|
|
|
26341
24848
|
const packaged = new Map;
|
|
26342
24849
|
for (const id of WORKIT_METHOD_SKILLS) {
|
|
26343
24850
|
const file = path45.join(skillsDir, id, "SKILL.md");
|
|
26344
|
-
if (!userSkills.has(id) &&
|
|
24851
|
+
if (!userSkills.has(id) && existsSync14(file))
|
|
26345
24852
|
packaged.set(id, file);
|
|
26346
24853
|
}
|
|
26347
24854
|
await ctx.skill.transform((editor) => {
|
|
@@ -26387,11 +24894,11 @@ var registerCommands = async (ctx, registeredSkills) => {
|
|
|
26387
24894
|
};
|
|
26388
24895
|
|
|
26389
24896
|
// packages/workit-opencode/src/stale-sources.ts
|
|
26390
|
-
import { existsSync as
|
|
24897
|
+
import { existsSync as existsSync15, readdirSync as readdirSync6 } from "node:fs";
|
|
26391
24898
|
import path46 from "node:path";
|
|
26392
24899
|
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
26393
24900
|
var typescriptFiles = (directory) => {
|
|
26394
|
-
if (!
|
|
24901
|
+
if (!existsSync15(directory))
|
|
26395
24902
|
return [];
|
|
26396
24903
|
return readdirSync6(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
26397
24904
|
const file = path46.join(directory, entry.name);
|
|
@@ -26405,7 +24912,7 @@ var pluginSourceFiles = [
|
|
|
26405
24912
|
path46.join(packageRoot2(), "..", "workit-core", "src", "core.ts"),
|
|
26406
24913
|
...typescriptFiles(path46.join(packageRoot2(), "..", "workit-core", "src", "core"))
|
|
26407
24914
|
])
|
|
26408
|
-
].filter((file) =>
|
|
24915
|
+
].filter((file) => existsSync15(file)).toSorted();
|
|
26409
24916
|
|
|
26410
24917
|
// packages/workit-opencode/src/v2/plugin.ts
|
|
26411
24918
|
var sessionFacts = async (ctx, sessionID) => {
|