@brainervirus/workit-pi 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/worker.js +0 -1639
- package/dist/workit.js +500 -1735
- package/package.json +1 -1
- package/skills/workit-fanout/SKILL.md +4 -4
- package/skills/workit-fanout/references/brief.md +17 -11
- package/skills/workit-fanout/references/orchestration.md +6 -6
- package/skills/workit-implement/SKILL.md +2 -0
package/dist/workit.js
CHANGED
|
@@ -963,8 +963,6 @@ var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/
|
|
|
963
963
|
var base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
964
964
|
var httpProtocol = /^https?$/;
|
|
965
965
|
var e164 = /^\+[1-9]\d{6,14}$/;
|
|
966
|
-
var creditCard = /^\d(?:[ -]?\d){11,18}$/;
|
|
967
|
-
var iban = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z0-9]{11,30}$/;
|
|
968
966
|
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])))`;
|
|
969
967
|
function anchor(source) {
|
|
970
968
|
return new RegExp(`^${source}$`);
|
|
@@ -1810,41 +1808,6 @@ var $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (inst, def) => {
|
|
|
1810
1808
|
def.pattern ?? (def.pattern = e164);
|
|
1811
1809
|
$ZodStringFormat.init(inst, def);
|
|
1812
1810
|
});
|
|
1813
|
-
var CC_SANITIZE = /[- ]/g;
|
|
1814
|
-
function isLuhnAlgo(digits) {
|
|
1815
|
-
let length = digits.length;
|
|
1816
|
-
let bit = 1;
|
|
1817
|
-
let sum = 0;
|
|
1818
|
-
while (length) {
|
|
1819
|
-
const value = digits.charCodeAt(--length) - 48;
|
|
1820
|
-
bit ^= 1;
|
|
1821
|
-
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
1822
|
-
}
|
|
1823
|
-
return sum % 10 === 0;
|
|
1824
|
-
}
|
|
1825
|
-
function isValidCreditCard(input) {
|
|
1826
|
-
if (!creditCard.test(input))
|
|
1827
|
-
return false;
|
|
1828
|
-
return isLuhnAlgo(input.replace(CC_SANITIZE, ""));
|
|
1829
|
-
}
|
|
1830
|
-
function isIso7064Mod97(iban2) {
|
|
1831
|
-
let remainder = 0;
|
|
1832
|
-
const len = iban2.length;
|
|
1833
|
-
for (let i = 4;i < len; i++) {
|
|
1834
|
-
const code = iban2.charCodeAt(i);
|
|
1835
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1836
|
-
}
|
|
1837
|
-
for (let i = 0;i < 4; i++) {
|
|
1838
|
-
const code = iban2.charCodeAt(i);
|
|
1839
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1840
|
-
}
|
|
1841
|
-
return remainder === 1;
|
|
1842
|
-
}
|
|
1843
|
-
function isValidIBAN(input) {
|
|
1844
|
-
if (!iban.test(input))
|
|
1845
|
-
return false;
|
|
1846
|
-
return isIso7064Mod97(input);
|
|
1847
|
-
}
|
|
1848
1811
|
function isValidJWT(token, algorithm = null) {
|
|
1849
1812
|
try {
|
|
1850
1813
|
const tokensParts = token.split(".");
|
|
@@ -3157,9 +3120,6 @@ function settle(inst, answer) {
|
|
|
3157
3120
|
recursive.set(inst, answer === PROVEN);
|
|
3158
3121
|
return answer;
|
|
3159
3122
|
}
|
|
3160
|
-
function isRecursiveSchema(inst) {
|
|
3161
|
-
return isRecursive(inst, new Set, true) !== NONE;
|
|
3162
|
-
}
|
|
3163
3123
|
function bucketFor(state, inst) {
|
|
3164
3124
|
let bucket = state.buckets.get(inst);
|
|
3165
3125
|
if (!bucket) {
|
|
@@ -3389,1661 +3349,63 @@ var error = () => {
|
|
|
3389
3349
|
return "Invalid input";
|
|
3390
3350
|
case "invalid_element":
|
|
3391
3351
|
return `Invalid value in ${issue.origin}`;
|
|
3392
|
-
default:
|
|
3393
|
-
return `Invalid input`;
|
|
3394
|
-
}
|
|
3395
|
-
};
|
|
3396
|
-
};
|
|
3397
|
-
function en_default() {
|
|
3398
|
-
return {
|
|
3399
|
-
localeError: error()
|
|
3400
|
-
};
|
|
3401
|
-
}
|
|
3402
|
-
// node_modules/zod/v4/core/registries.js
|
|
3403
|
-
var _a2;
|
|
3404
|
-
class $ZodRegistry {
|
|
3405
|
-
constructor() {
|
|
3406
|
-
this._map = new WeakMap;
|
|
3407
|
-
this._idmap = new Map;
|
|
3408
|
-
}
|
|
3409
|
-
add(schema, ..._meta) {
|
|
3410
|
-
const meta = _meta[0];
|
|
3411
|
-
this._map.set(schema, meta);
|
|
3412
|
-
if (meta && typeof meta === "object" && "id" in meta) {
|
|
3413
|
-
this._idmap.set(meta.id, schema);
|
|
3414
|
-
}
|
|
3415
|
-
return this;
|
|
3416
|
-
}
|
|
3417
|
-
clear() {
|
|
3418
|
-
this._map = new WeakMap;
|
|
3419
|
-
this._idmap = new Map;
|
|
3420
|
-
return this;
|
|
3421
|
-
}
|
|
3422
|
-
remove(schema) {
|
|
3423
|
-
const meta = this._map.get(schema);
|
|
3424
|
-
if (meta && typeof meta === "object" && "id" in meta) {
|
|
3425
|
-
this._idmap.delete(meta.id);
|
|
3426
|
-
}
|
|
3427
|
-
this._map.delete(schema);
|
|
3428
|
-
return this;
|
|
3429
|
-
}
|
|
3430
|
-
get(schema) {
|
|
3431
|
-
const p = schema._zod.parent;
|
|
3432
|
-
if (p) {
|
|
3433
|
-
const pm = { ...this.get(p) ?? {} };
|
|
3434
|
-
delete pm.id;
|
|
3435
|
-
const f = { ...pm, ...this._map.get(schema) };
|
|
3436
|
-
return Object.keys(f).length ? f : undefined;
|
|
3437
|
-
}
|
|
3438
|
-
return this._map.get(schema);
|
|
3439
|
-
}
|
|
3440
|
-
has(schema) {
|
|
3441
|
-
return this._map.has(schema);
|
|
3442
|
-
}
|
|
3443
|
-
}
|
|
3444
|
-
function registry() {
|
|
3445
|
-
return new $ZodRegistry;
|
|
3446
|
-
}
|
|
3447
|
-
(_a2 = globalThis).__zod_globalRegistry ?? (_a2.__zod_globalRegistry = registry());
|
|
3448
|
-
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
3449
|
-
// node_modules/zod/v4/core/compile.js
|
|
3450
|
-
var INVALID = Symbol.for("zod.compile.invalid");
|
|
3451
|
-
var FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
|
|
3452
|
-
|
|
3453
|
-
class ZodCompileAsyncError extends Error {
|
|
3454
|
-
constructor(message = "z.compile does not support async refinements, transforms, or checks") {
|
|
3455
|
-
super(message);
|
|
3456
|
-
this.name = "ZodCompileAsyncError";
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
|
-
|
|
3460
|
-
class ZodCompileUnsupportedError extends Error {
|
|
3461
|
-
constructor(feature, islandable = true) {
|
|
3462
|
-
super(`z.compile does not support ${feature}; this schema must use the runtime parser`);
|
|
3463
|
-
this.name = "ZodCompileUnsupportedError";
|
|
3464
|
-
this.islandable = islandable;
|
|
3465
|
-
}
|
|
3466
|
-
}
|
|
3467
|
-
function compileValidator(schema, parser) {
|
|
3468
|
-
try {
|
|
3469
|
-
return compileFn(schema, { assertOnly: true });
|
|
3470
|
-
} catch {
|
|
3471
|
-
return parser;
|
|
3472
|
-
}
|
|
3473
|
-
}
|
|
3474
|
-
function compile(schema, options) {
|
|
3475
|
-
try {
|
|
3476
|
-
const parser = compileFn(schema);
|
|
3477
|
-
const clone = withParser(schema, parser);
|
|
3478
|
-
clone._zod.bag.validator = compileValidator(schema, parser);
|
|
3479
|
-
return clone;
|
|
3480
|
-
} catch (err) {
|
|
3481
|
-
if (options?.strict)
|
|
3482
|
-
throw err;
|
|
3483
|
-
return schema;
|
|
3484
|
-
}
|
|
3485
|
-
}
|
|
3486
|
-
function withParser(schema, parser) {
|
|
3487
|
-
if (isRecursiveSchema(schema)) {
|
|
3488
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3489
|
-
}
|
|
3490
|
-
const clone2 = clone(schema);
|
|
3491
|
-
const liveRun = schema._zod.run;
|
|
3492
|
-
const originalRun = liveRun.__originalRun ?? liveRun;
|
|
3493
|
-
const wrapped = (payload, ctx) => {
|
|
3494
|
-
if (ctx?.async || ctx?.direction === "backward" || ctx?.skipChecks || ctx?.[FALLBACK_FLAG]) {
|
|
3495
|
-
return originalRun(payload, ctx);
|
|
3496
|
-
}
|
|
3497
|
-
if (ctx && isBackEdge(ctx, payload.value)) {
|
|
3498
|
-
return originalRun(payload, ctx);
|
|
3499
|
-
}
|
|
3500
|
-
const out = parser(payload.value);
|
|
3501
|
-
if (out !== INVALID) {
|
|
3502
|
-
payload.value = out;
|
|
3503
|
-
return payload;
|
|
3504
|
-
}
|
|
3505
|
-
if (ctx)
|
|
3506
|
-
ctx[FALLBACK_FLAG] = true;
|
|
3507
|
-
return originalRun(payload, ctx);
|
|
3508
|
-
};
|
|
3509
|
-
wrapped.__originalRun = originalRun;
|
|
3510
|
-
clone2._zod.bag.fallbackRun = originalRun;
|
|
3511
|
-
clone2._zod.bag.validator = parser;
|
|
3512
|
-
clone2._zod.run = wrapped;
|
|
3513
|
-
if (!liveRun.__originalRun)
|
|
3514
|
-
installCompiledUserMethods(clone2, schema, parser);
|
|
3515
|
-
return clone2;
|
|
3516
|
-
}
|
|
3517
|
-
function installCompiledUserMethods(target, source, parser) {
|
|
3518
|
-
const targetAny = target;
|
|
3519
|
-
const sourceAny = source;
|
|
3520
|
-
if (typeof sourceAny.safeParse === "function") {
|
|
3521
|
-
const originalSafeParse = sourceAny.safeParse;
|
|
3522
|
-
targetAny.safeParse = (data, params) => {
|
|
3523
|
-
const out = parser(data);
|
|
3524
|
-
if (out !== INVALID) {
|
|
3525
|
-
return { success: true, data: out };
|
|
3526
|
-
}
|
|
3527
|
-
return originalSafeParse(data, params);
|
|
3528
|
-
};
|
|
3529
|
-
}
|
|
3530
|
-
if (typeof sourceAny.parse === "function") {
|
|
3531
|
-
const originalParse = sourceAny.parse;
|
|
3532
|
-
targetAny.parse = (data, params) => {
|
|
3533
|
-
const out = parser(data);
|
|
3534
|
-
if (out !== INVALID) {
|
|
3535
|
-
return out;
|
|
3536
|
-
}
|
|
3537
|
-
return originalParse(data, params);
|
|
3538
|
-
};
|
|
3539
|
-
}
|
|
3540
|
-
}
|
|
3541
|
-
function compileFn(schema, options) {
|
|
3542
|
-
let recursive = true;
|
|
3543
|
-
try {
|
|
3544
|
-
recursive = isRecursiveSchema(schema);
|
|
3545
|
-
} catch {}
|
|
3546
|
-
if (recursive) {
|
|
3547
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3548
|
-
}
|
|
3549
|
-
const ctx = {
|
|
3550
|
-
constants: new Map,
|
|
3551
|
-
constantCounter: 0,
|
|
3552
|
-
varCounter: 0,
|
|
3553
|
-
definite: true
|
|
3554
|
-
};
|
|
3555
|
-
const doc = new Doc(["input"]);
|
|
3556
|
-
const outputAccessor = generateCheck(doc, ctx, schema, "input", !options?.assertOnly);
|
|
3557
|
-
doc.write(outputAccessor === null ? `return true;` : `return ${outputAccessor};`);
|
|
3558
|
-
const constantNames = ["INVALID", ...ctx.constants.keys()];
|
|
3559
|
-
const constantValues = [INVALID, ...ctx.constants.values()];
|
|
3560
|
-
const code = doc.content.join(`
|
|
3561
|
-
`);
|
|
3562
|
-
const fullCode = options?.debug ? constantNames.length > 0 ? `// Constants: ${constantNames.join(", ")}
|
|
3563
|
-
${code}` : code : "";
|
|
3564
|
-
const F = Function;
|
|
3565
|
-
const factoryCode = `return (input) => {
|
|
3566
|
-
${code}
|
|
3567
|
-
}`;
|
|
3568
|
-
let fn;
|
|
3569
|
-
try {
|
|
3570
|
-
const factory = new F(...constantNames, factoryCode);
|
|
3571
|
-
fn = factory(...constantValues);
|
|
3572
|
-
} catch (err) {
|
|
3573
|
-
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${err.message})`);
|
|
3574
|
-
}
|
|
3575
|
-
if (options?.debug) {
|
|
3576
|
-
fn.code = fullCode;
|
|
3577
|
-
}
|
|
3578
|
-
fn.definite = ctx.definite;
|
|
3579
|
-
return fn;
|
|
3580
|
-
}
|
|
3581
|
-
function addConstant(ctx, value) {
|
|
3582
|
-
for (const [name, v] of ctx.constants) {
|
|
3583
|
-
if (v === value)
|
|
3584
|
-
return name;
|
|
3585
|
-
}
|
|
3586
|
-
const name = `c${ctx.constantCounter++}`;
|
|
3587
|
-
ctx.constants.set(name, value);
|
|
3588
|
-
return name;
|
|
3589
|
-
}
|
|
3590
|
-
function addUserConstant(ctx, fn) {
|
|
3591
|
-
ctx.definite = false;
|
|
3592
|
-
return addConstant(ctx, fn);
|
|
3593
|
-
}
|
|
3594
|
-
function newVar(ctx) {
|
|
3595
|
-
return `v${ctx.varCounter++}`;
|
|
3596
|
-
}
|
|
3597
|
-
function runtimeRun(schema, value) {
|
|
3598
|
-
const result = schema._zod.run({ value, issues: [] }, {});
|
|
3599
|
-
if (result && typeof result.then === "function")
|
|
3600
|
-
return INVALID;
|
|
3601
|
-
const r = result;
|
|
3602
|
-
return r.issues.length === 0 ? r.value : INVALID;
|
|
3603
|
-
}
|
|
3604
|
-
function compileChild(doc, ctx, schema, accessor, needsValue = true) {
|
|
3605
|
-
const contentLen = doc.content.length;
|
|
3606
|
-
const constantCount = ctx.constants.size;
|
|
3607
|
-
const constantCounter = ctx.constantCounter;
|
|
3608
|
-
const varCounter = ctx.varCounter;
|
|
3609
|
-
try {
|
|
3610
|
-
return generateCheck(doc, ctx, schema, accessor, needsValue);
|
|
3611
|
-
} catch (err) {
|
|
3612
|
-
if (!(err instanceof ZodCompileUnsupportedError) || !err.islandable)
|
|
3613
|
-
throw err;
|
|
3614
|
-
doc.content.length = contentLen;
|
|
3615
|
-
if (ctx.constants.size > constantCount) {
|
|
3616
|
-
const trailing = Array.from(ctx.constants.keys()).slice(constantCount);
|
|
3617
|
-
for (const k of trailing)
|
|
3618
|
-
ctx.constants.delete(k);
|
|
3619
|
-
}
|
|
3620
|
-
ctx.constantCounter = constantCounter;
|
|
3621
|
-
ctx.varCounter = varCounter;
|
|
3622
|
-
return emitRuntimeIsland(doc, ctx, schema, accessor);
|
|
3623
|
-
}
|
|
3624
|
-
}
|
|
3625
|
-
function emitRuntimeIsland(doc, ctx, schema, accessor) {
|
|
3626
|
-
ctx.definite = false;
|
|
3627
|
-
const schemaConst = addConstant(ctx, schema);
|
|
3628
|
-
const runConst = addConstant(ctx, runtimeRun);
|
|
3629
|
-
const outVar = newVar(ctx);
|
|
3630
|
-
doc.write(`const ${outVar} = ${runConst}(${schemaConst}, ${accessor});`);
|
|
3631
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3632
|
-
return outVar;
|
|
3633
|
-
}
|
|
3634
|
-
var WHEN_DEFAULTED_CHECKS = new Set([
|
|
3635
|
-
"max_size",
|
|
3636
|
-
"min_size",
|
|
3637
|
-
"size_equals",
|
|
3638
|
-
"max_length",
|
|
3639
|
-
"min_length",
|
|
3640
|
-
"length_equals"
|
|
3641
|
-
]);
|
|
3642
|
-
function generateChecks(doc, ctx, schema, accessor) {
|
|
3643
|
-
const schemaChecks = schema._zod.def.checks;
|
|
3644
|
-
if (!schemaChecks || schemaChecks.length === 0)
|
|
3645
|
-
return accessor;
|
|
3646
|
-
let currentAccessor = accessor;
|
|
3647
|
-
for (const check of schemaChecks) {
|
|
3648
|
-
const def = check._zod.def;
|
|
3649
|
-
if (def.when && !WHEN_DEFAULTED_CHECKS.has(def.check)) {
|
|
3650
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3651
|
-
}
|
|
3652
|
-
switch (def.check) {
|
|
3653
|
-
case "greater_than":
|
|
3654
|
-
generateGreaterThanCheck(doc, ctx, def, currentAccessor);
|
|
3655
|
-
break;
|
|
3656
|
-
case "less_than":
|
|
3657
|
-
generateLessThanCheck(doc, ctx, def, currentAccessor);
|
|
3658
|
-
break;
|
|
3659
|
-
case "multiple_of":
|
|
3660
|
-
generateMultipleOfCheck(doc, ctx, def, currentAccessor);
|
|
3661
|
-
break;
|
|
3662
|
-
case "number_format":
|
|
3663
|
-
generateNumberFormatCheck(doc, def, currentAccessor);
|
|
3664
|
-
break;
|
|
3665
|
-
case "min_length": {
|
|
3666
|
-
const min = numericOperand(def.minimum, "min_length");
|
|
3667
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${min} && ${currentAccessor}.length < ${def.minimum * 2}`);
|
|
3668
|
-
doc.write(`if (${len} < ${min}) return INVALID;`);
|
|
3669
|
-
break;
|
|
3670
|
-
}
|
|
3671
|
-
case "max_length": {
|
|
3672
|
-
const max = numericOperand(def.maximum, "max_length");
|
|
3673
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length > ${max}`);
|
|
3674
|
-
doc.write(`if (${len} > ${max}) return INVALID;`);
|
|
3675
|
-
break;
|
|
3676
|
-
}
|
|
3677
|
-
case "length_equals": {
|
|
3678
|
-
const exact = numericOperand(def.length, "length_equals");
|
|
3679
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${exact} && ${currentAccessor}.length <= ${def.length * 2}`);
|
|
3680
|
-
doc.write(`if (${len} !== ${exact}) return INVALID;`);
|
|
3681
|
-
break;
|
|
3682
|
-
}
|
|
3683
|
-
case "min_size":
|
|
3684
|
-
doc.write(`if (${currentAccessor}.size < ${numericOperand(def.minimum, "min_size")}) return INVALID;`);
|
|
3685
|
-
break;
|
|
3686
|
-
case "max_size":
|
|
3687
|
-
doc.write(`if (${currentAccessor}.size > ${numericOperand(def.maximum, "max_size")}) return INVALID;`);
|
|
3688
|
-
break;
|
|
3689
|
-
case "size_equals":
|
|
3690
|
-
doc.write(`if (${currentAccessor}.size !== ${numericOperand(def.size, "size_equals")}) return INVALID;`);
|
|
3691
|
-
break;
|
|
3692
|
-
case "string_format":
|
|
3693
|
-
currentAccessor = generateStringFormatCheck(doc, ctx, def, currentAccessor);
|
|
3694
|
-
break;
|
|
3695
|
-
case "custom":
|
|
3696
|
-
currentAccessor = generateCustomRefineCheck(doc, ctx, check, currentAccessor);
|
|
3697
|
-
break;
|
|
3698
|
-
case "bigint_format":
|
|
3699
|
-
generateBigIntFormatCheck(doc, def, currentAccessor);
|
|
3700
|
-
break;
|
|
3701
|
-
case "mime_type":
|
|
3702
|
-
generateMimeTypeCheck(doc, ctx, def, currentAccessor);
|
|
3703
|
-
break;
|
|
3704
|
-
case "property":
|
|
3705
|
-
generatePropertyCheck(doc, ctx, def, currentAccessor);
|
|
3706
|
-
break;
|
|
3707
|
-
case "properties":
|
|
3708
|
-
generatePropertiesChecks(doc, ctx, def, currentAccessor);
|
|
3709
|
-
break;
|
|
3710
|
-
case "overwrite": {
|
|
3711
|
-
const newAccessor = newVar(ctx);
|
|
3712
|
-
generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor);
|
|
3713
|
-
currentAccessor = newAccessor;
|
|
3714
|
-
break;
|
|
3715
|
-
}
|
|
3716
|
-
default: {
|
|
3717
|
-
throw new ZodCompileUnsupportedError(`check type ${def.check}`);
|
|
3718
|
-
}
|
|
3719
|
-
}
|
|
3720
|
-
}
|
|
3721
|
-
return currentAccessor;
|
|
3722
|
-
}
|
|
3723
|
-
function codePointLengthVar(doc, ctx, accessor, inDoubt) {
|
|
3724
|
-
const cpLen = addConstant(ctx, codePointLength);
|
|
3725
|
-
const v = newVar(ctx);
|
|
3726
|
-
doc.write(`const ${v} = typeof ${accessor} === "string" && ${inDoubt} ? ${cpLen}(${accessor}) : ${accessor}.length;`);
|
|
3727
|
-
return v;
|
|
3728
|
-
}
|
|
3729
|
-
function numericOperand(value, label) {
|
|
3730
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
3731
|
-
throw new ZodCompileUnsupportedError(`${label} bound of type ${typeof value}`);
|
|
3732
|
-
}
|
|
3733
|
-
return `${value}`;
|
|
3734
|
-
}
|
|
3735
|
-
function comparisonOperand(ctx, value) {
|
|
3736
|
-
if (typeof value === "bigint")
|
|
3737
|
-
return `${value}n`;
|
|
3738
|
-
if (typeof value === "number") {
|
|
3739
|
-
if (Number.isNaN(value))
|
|
3740
|
-
throw new ZodCompileUnsupportedError("comparison check with NaN bound");
|
|
3741
|
-
return `${value}`;
|
|
3742
|
-
}
|
|
3743
|
-
if (value instanceof Date) {
|
|
3744
|
-
if (Number.isNaN(value.getTime())) {
|
|
3745
|
-
throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
|
|
3746
|
-
}
|
|
3747
|
-
return addConstant(ctx, value);
|
|
3748
|
-
}
|
|
3749
|
-
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof value}`);
|
|
3750
|
-
}
|
|
3751
|
-
function generateGreaterThanCheck(doc, ctx, def, accessor) {
|
|
3752
|
-
const op = def.inclusive ? "<" : "<=";
|
|
3753
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3754
|
-
}
|
|
3755
|
-
function generateLessThanCheck(doc, ctx, def, accessor) {
|
|
3756
|
-
const op = def.inclusive ? ">" : ">=";
|
|
3757
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
3758
|
-
}
|
|
3759
|
-
function generateMultipleOfCheck(doc, ctx, def, accessor) {
|
|
3760
|
-
if (typeof def.value === "bigint") {
|
|
3761
|
-
if (def.value === BigInt(0))
|
|
3762
|
-
throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
|
|
3763
|
-
doc.write(`if (${accessor} % ${def.value}n !== 0n) return INVALID;`);
|
|
3764
|
-
} else {
|
|
3765
|
-
const remainder = addConstant(ctx, floatSafeRemainder);
|
|
3766
|
-
doc.write(`if (${remainder}(${accessor}, ${numericOperand(def.value, "multiple_of")}) !== 0) return INVALID;`);
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
function generateNumberFormatCheck(doc, def, accessor) {
|
|
3770
|
-
const format = def.format;
|
|
3771
|
-
switch (format) {
|
|
3772
|
-
case "safeint":
|
|
3773
|
-
doc.write(`if (!Number.isSafeInteger(${accessor})) return INVALID;`);
|
|
3774
|
-
break;
|
|
3775
|
-
case "int32":
|
|
3776
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < -2147483648 || ${accessor} > 2147483647) return INVALID;`);
|
|
3777
|
-
break;
|
|
3778
|
-
case "uint32":
|
|
3779
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < 0 || ${accessor} > 4294967295) return INVALID;`);
|
|
3780
|
-
break;
|
|
3781
|
-
case "float32":
|
|
3782
|
-
doc.write(`if (!Number.isFinite(${accessor}) || ${accessor} < -3.4028234663852886e38 || ${accessor} > 3.4028234663852886e38) return INVALID;`);
|
|
3783
|
-
break;
|
|
3784
|
-
case "float64":
|
|
3785
|
-
doc.write(`if (!Number.isFinite(${accessor})) return INVALID;`);
|
|
3786
|
-
break;
|
|
3787
|
-
default: {
|
|
3788
|
-
throw new ZodCompileUnsupportedError(`number format ${format}`);
|
|
3789
|
-
}
|
|
3790
|
-
}
|
|
3791
|
-
}
|
|
3792
|
-
function generateBigIntFormatCheck(doc, def, accessor) {
|
|
3793
|
-
const format = def.format;
|
|
3794
|
-
if (!format)
|
|
3795
|
-
return;
|
|
3796
|
-
switch (format) {
|
|
3797
|
-
case "int64":
|
|
3798
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
3799
|
-
break;
|
|
3800
|
-
case "uint64":
|
|
3801
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
3802
|
-
break;
|
|
3803
|
-
default: {
|
|
3804
|
-
throw new ZodCompileUnsupportedError(`bigint format ${format}`);
|
|
3805
|
-
}
|
|
3806
|
-
}
|
|
3807
|
-
}
|
|
3808
|
-
function generateMimeTypeCheck(doc, ctx, def, accessor) {
|
|
3809
|
-
const mimeTypes = def.mime;
|
|
3810
|
-
if (mimeTypes && mimeTypes.length > 0) {
|
|
3811
|
-
const mimeSet = addConstant(ctx, new Set(mimeTypes));
|
|
3812
|
-
doc.write(`if (!${mimeSet}.has(${accessor}.type)) return INVALID;`);
|
|
3813
|
-
}
|
|
3814
|
-
}
|
|
3815
|
-
function generatePropertiesChecks(doc, ctx, def, accessor) {
|
|
3816
|
-
if (def.when) {
|
|
3817
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
3818
|
-
}
|
|
3819
|
-
doc.write(`if (${accessor} == null) return INVALID;`);
|
|
3820
|
-
const shape = def.shape;
|
|
3821
|
-
for (const key of Reflect.ownKeys(shape)) {
|
|
3822
|
-
const keyExpr = typeof key === "symbol" ? addConstant(ctx, key) : esc(key);
|
|
3823
|
-
const inputVar = newVar(ctx);
|
|
3824
|
-
doc.write(`const ${inputVar} = ${accessor}[${keyExpr}];`);
|
|
3825
|
-
compileChild(doc, ctx, shape[key], inputVar, false);
|
|
3826
|
-
}
|
|
3827
|
-
}
|
|
3828
|
-
function generatePropertyCheck(doc, ctx, def, accessor) {
|
|
3829
|
-
const propAccessor = `${accessor}[${JSON.stringify(def.property)}]`;
|
|
3830
|
-
generateCheck(doc, ctx, def.schema, propAccessor);
|
|
3831
|
-
}
|
|
3832
|
-
function generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor) {
|
|
3833
|
-
const tx = check._zod.def.tx;
|
|
3834
|
-
if (!tx) {
|
|
3835
|
-
throw new ZodCompileUnsupportedError("overwrite check without a transform function");
|
|
3836
|
-
}
|
|
3837
|
-
if (isAsyncFunction(tx)) {
|
|
3838
|
-
throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
|
|
3839
|
-
}
|
|
3840
|
-
const txConst = addConstant(ctx, tx);
|
|
3841
|
-
doc.write(`const ${newAccessor} = ${txConst}(${currentAccessor});`);
|
|
3842
|
-
}
|
|
3843
|
-
function throwAsync() {
|
|
3844
|
-
throw new $ZodAsyncError;
|
|
3845
|
-
}
|
|
3846
|
-
function pushIssue(issue) {
|
|
3847
|
-
this.issues.push(issue);
|
|
3848
|
-
}
|
|
3849
|
-
function generateCustomRefineCheck(doc, ctx, check, accessor) {
|
|
3850
|
-
const def = check._zod.def;
|
|
3851
|
-
if (def.fn) {
|
|
3852
|
-
if (isAsyncFunction(def.fn)) {
|
|
3853
|
-
throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
|
|
3854
|
-
}
|
|
3855
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
3856
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
3857
|
-
const resVar = newVar(ctx);
|
|
3858
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
3859
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
3860
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
3861
|
-
return accessor;
|
|
3862
|
-
}
|
|
3863
|
-
if (check._zod.check) {
|
|
3864
|
-
if (isAsyncFunction(check._zod.check)) {
|
|
3865
|
-
throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
|
|
3866
|
-
}
|
|
3867
|
-
const checkFn = check._zod.check;
|
|
3868
|
-
const helperFn = (value) => {
|
|
3869
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
3870
|
-
const result = checkFn(fakePayload);
|
|
3871
|
-
if (result instanceof Promise)
|
|
3872
|
-
throwAsync();
|
|
3873
|
-
return fakePayload.issues.length === 0 ? fakePayload.value : INVALID;
|
|
3874
|
-
};
|
|
3875
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
3876
|
-
const outVar = newVar(ctx);
|
|
3877
|
-
doc.write(`const ${outVar} = ${helperConst}(${accessor});`);
|
|
3878
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
3879
|
-
return outVar;
|
|
3880
|
-
}
|
|
3881
|
-
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
|
|
3882
|
-
}
|
|
3883
|
-
var PATTERN_IS_COMPLETE = new Set([
|
|
3884
|
-
"cidrv4",
|
|
3885
|
-
"cuid",
|
|
3886
|
-
"cuid2",
|
|
3887
|
-
"date",
|
|
3888
|
-
"datetime",
|
|
3889
|
-
"duration",
|
|
3890
|
-
"e164",
|
|
3891
|
-
"email",
|
|
3892
|
-
"emoji",
|
|
3893
|
-
"ends_with",
|
|
3894
|
-
"guid",
|
|
3895
|
-
"includes",
|
|
3896
|
-
"ipv4",
|
|
3897
|
-
"ksuid",
|
|
3898
|
-
"lowercase",
|
|
3899
|
-
"mac",
|
|
3900
|
-
"nanoid",
|
|
3901
|
-
"regex",
|
|
3902
|
-
"starts_with",
|
|
3903
|
-
"time",
|
|
3904
|
-
"ulid",
|
|
3905
|
-
"uppercase",
|
|
3906
|
-
"uuid",
|
|
3907
|
-
"xid"
|
|
3908
|
-
]);
|
|
3909
|
-
function generateStringFormatCheck(doc, ctx, def, accessor, needsValue = true) {
|
|
3910
|
-
const fmt = def.format;
|
|
3911
|
-
if (fmt === "base64") {
|
|
3912
|
-
const validator = addConstant(ctx, isValidBase64);
|
|
3913
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3914
|
-
return accessor;
|
|
3915
|
-
}
|
|
3916
|
-
if (fmt === "base64url") {
|
|
3917
|
-
const validator = addConstant(ctx, isValidBase64URL);
|
|
3918
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3919
|
-
return accessor;
|
|
3920
|
-
}
|
|
3921
|
-
if (fmt === "jwt") {
|
|
3922
|
-
const validator = addConstant(ctx, isValidJWT);
|
|
3923
|
-
const alg = addConstant(ctx, def.alg ?? null);
|
|
3924
|
-
doc.write(`if (!${validator}(${accessor}, ${alg})) return INVALID;`);
|
|
3925
|
-
return accessor;
|
|
3926
|
-
}
|
|
3927
|
-
if (fmt === "ipv6") {
|
|
3928
|
-
const validator = addConstant(ctx, isValidIPv6);
|
|
3929
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3930
|
-
return accessor;
|
|
3931
|
-
}
|
|
3932
|
-
if (fmt === "cidrv6") {
|
|
3933
|
-
const validator = addConstant(ctx, isValidCIDRv6);
|
|
3934
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3935
|
-
return accessor;
|
|
3936
|
-
}
|
|
3937
|
-
if (fmt === "credit_card") {
|
|
3938
|
-
const validator = addConstant(ctx, isValidCreditCard);
|
|
3939
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3940
|
-
return accessor;
|
|
3941
|
-
}
|
|
3942
|
-
if (fmt === "iban") {
|
|
3943
|
-
const validator = addConstant(ctx, isValidIBAN);
|
|
3944
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
3945
|
-
return accessor;
|
|
3946
|
-
}
|
|
3947
|
-
const formatDef = def;
|
|
3948
|
-
if (fmt === "url" || fmt === "httpurl" || formatDef.normalize || formatDef.hostname !== undefined || formatDef.protocol !== undefined) {
|
|
3949
|
-
const parseConst = addConstant(ctx, validateURL);
|
|
3950
|
-
const defConst = addConstant(ctx, def);
|
|
3951
|
-
const trimVar = newVar(ctx);
|
|
3952
|
-
const urlVar = newVar(ctx);
|
|
3953
|
-
doc.write(`const ${trimVar} = ${accessor}.trim();`);
|
|
3954
|
-
doc.write(`const ${urlVar} = ${parseConst}(${trimVar}, ${defConst});`);
|
|
3955
|
-
doc.write(`if (typeof ${urlVar} === "number") return INVALID;`);
|
|
3956
|
-
if (formatDef.hostname !== undefined) {
|
|
3957
|
-
const hostnameConst = addConstant(ctx, urlHostnameOk);
|
|
3958
|
-
doc.write(`if (!${hostnameConst}(${urlVar}, ${defConst}.hostname)) return INVALID;`);
|
|
3959
|
-
}
|
|
3960
|
-
if (formatDef.protocol !== undefined) {
|
|
3961
|
-
const protocolConst = addConstant(ctx, urlProtocolOk);
|
|
3962
|
-
doc.write(`if (!${protocolConst}(${urlVar}, ${defConst}.protocol)) return INVALID;`);
|
|
3963
|
-
}
|
|
3964
|
-
if (!needsValue)
|
|
3965
|
-
return null;
|
|
3966
|
-
const outputVar = newVar(ctx);
|
|
3967
|
-
const outputExpr = formatDef.normalize ? `${urlVar}.href` : `${addConstant(ctx, stripTabAndNewline)}(${trimVar})`;
|
|
3968
|
-
doc.write(`const ${outputVar} = ${outputExpr};`);
|
|
3969
|
-
return outputVar;
|
|
3970
|
-
}
|
|
3971
|
-
const customFn = def.fn;
|
|
3972
|
-
if (customFn) {
|
|
3973
|
-
if (isAsyncFunction(customFn))
|
|
3974
|
-
throw new ZodCompileUnsupportedError(`async string format ${fmt}`);
|
|
3975
|
-
const fnConst = addConstant(ctx, customFn);
|
|
3976
|
-
doc.write(`if (!${fnConst}(${accessor})) return INVALID;`);
|
|
3977
|
-
return accessor;
|
|
3978
|
-
}
|
|
3979
|
-
if (PATTERN_IS_COMPLETE.has(fmt) && def.pattern) {
|
|
3980
|
-
const patternConst = addConstant(ctx, def.pattern);
|
|
3981
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
3982
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
3983
|
-
return accessor;
|
|
3984
|
-
}
|
|
3985
|
-
const format = def.format;
|
|
3986
|
-
switch (format) {
|
|
3987
|
-
case "regex":
|
|
3988
|
-
throw new ZodCompileUnsupportedError("regex format without a pattern");
|
|
3989
|
-
case "lowercase":
|
|
3990
|
-
doc.write(`if (${accessor} !== ${accessor}.toLowerCase()) return INVALID;`);
|
|
3991
|
-
break;
|
|
3992
|
-
case "uppercase":
|
|
3993
|
-
doc.write(`if (${accessor} !== ${accessor}.toUpperCase()) return INVALID;`);
|
|
3994
|
-
break;
|
|
3995
|
-
case "includes":
|
|
3996
|
-
doc.write(`if (!${accessor}.includes(${esc(def.includes)})) return INVALID;`);
|
|
3997
|
-
break;
|
|
3998
|
-
case "starts_with": {
|
|
3999
|
-
const prefix = def.prefix;
|
|
4000
|
-
doc.write(`if (${accessor}.slice(0, ${prefix.length}) !== ${esc(prefix)}) return INVALID;`);
|
|
4001
|
-
break;
|
|
4002
|
-
}
|
|
4003
|
-
case "ends_with": {
|
|
4004
|
-
const suffix = def.suffix;
|
|
4005
|
-
doc.write(`if (${accessor}.slice(-${suffix.length}) !== ${esc(suffix)}) return INVALID;`);
|
|
4006
|
-
break;
|
|
4007
|
-
}
|
|
4008
|
-
default: {
|
|
4009
|
-
throw new ZodCompileUnsupportedError(`string format ${format}`);
|
|
4010
|
-
}
|
|
4011
|
-
}
|
|
4012
|
-
return accessor;
|
|
4013
|
-
}
|
|
4014
|
-
function generateCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4015
|
-
const def = schema._zod.def;
|
|
4016
|
-
const type = def.type;
|
|
4017
|
-
if (def.coerce) {
|
|
4018
|
-
throw new ZodCompileUnsupportedError(`coercion (z.coerce.${type}())`);
|
|
4019
|
-
}
|
|
4020
|
-
const buildsValue = needsValue || !!def.checks?.length;
|
|
4021
|
-
let typeAccessor;
|
|
4022
|
-
switch (type) {
|
|
4023
|
-
case "string":
|
|
4024
|
-
typeAccessor = generateStringCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4025
|
-
break;
|
|
4026
|
-
case "number":
|
|
4027
|
-
typeAccessor = generateNumberCheck(doc, schema, accessor);
|
|
4028
|
-
break;
|
|
4029
|
-
case "boolean":
|
|
4030
|
-
typeAccessor = generateBooleanCheck(doc, accessor);
|
|
4031
|
-
break;
|
|
4032
|
-
case "bigint":
|
|
4033
|
-
typeAccessor = generateBigIntCheck(doc, schema, accessor);
|
|
4034
|
-
break;
|
|
4035
|
-
case "symbol":
|
|
4036
|
-
typeAccessor = generateSymbolCheck(doc, accessor);
|
|
4037
|
-
break;
|
|
4038
|
-
case "undefined":
|
|
4039
|
-
typeAccessor = generateUndefinedCheck(doc, accessor);
|
|
4040
|
-
break;
|
|
4041
|
-
case "null":
|
|
4042
|
-
typeAccessor = generateNullCheck(doc, accessor);
|
|
4043
|
-
break;
|
|
4044
|
-
case "any":
|
|
4045
|
-
case "unknown":
|
|
4046
|
-
typeAccessor = accessor;
|
|
4047
|
-
break;
|
|
4048
|
-
case "never":
|
|
4049
|
-
doc.write("return INVALID;");
|
|
4050
|
-
typeAccessor = accessor;
|
|
4051
|
-
break;
|
|
4052
|
-
case "void":
|
|
4053
|
-
typeAccessor = generateVoidCheck(doc, accessor);
|
|
4054
|
-
break;
|
|
4055
|
-
case "nan":
|
|
4056
|
-
typeAccessor = generateNaNCheck(doc, accessor);
|
|
4057
|
-
break;
|
|
4058
|
-
case "date":
|
|
4059
|
-
typeAccessor = generateDateCheck(doc, accessor);
|
|
4060
|
-
break;
|
|
4061
|
-
case "object":
|
|
4062
|
-
typeAccessor = generateObjectCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4063
|
-
break;
|
|
4064
|
-
case "optional":
|
|
4065
|
-
typeAccessor = generateOptionalCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4066
|
-
break;
|
|
4067
|
-
case "nullable":
|
|
4068
|
-
typeAccessor = generateNullableCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4069
|
-
break;
|
|
4070
|
-
case "array":
|
|
4071
|
-
typeAccessor = generateArrayCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4072
|
-
break;
|
|
4073
|
-
case "literal":
|
|
4074
|
-
typeAccessor = generateLiteralCheck(doc, ctx, schema, accessor);
|
|
4075
|
-
break;
|
|
4076
|
-
case "enum":
|
|
4077
|
-
typeAccessor = generateEnumCheck(doc, ctx, schema, accessor);
|
|
4078
|
-
break;
|
|
4079
|
-
case "readonly": {
|
|
4080
|
-
const innerOut = generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4081
|
-
const frozenVar = newVar(ctx);
|
|
4082
|
-
doc.write(`const ${frozenVar} = Object.freeze(${innerOut});`);
|
|
4083
|
-
typeAccessor = frozenVar;
|
|
4084
|
-
break;
|
|
4085
|
-
}
|
|
4086
|
-
case "success":
|
|
4087
|
-
generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4088
|
-
typeAccessor = "true";
|
|
4089
|
-
break;
|
|
4090
|
-
case "default":
|
|
4091
|
-
case "prefault":
|
|
4092
|
-
typeAccessor = generateDefaultCheck(doc, ctx, schema, accessor);
|
|
4093
|
-
break;
|
|
4094
|
-
case "nonoptional":
|
|
4095
|
-
typeAccessor = generateNonOptionalCheck(doc, ctx, schema, accessor);
|
|
4096
|
-
break;
|
|
4097
|
-
case "tuple":
|
|
4098
|
-
typeAccessor = generateTupleCheck(doc, ctx, schema, accessor);
|
|
4099
|
-
break;
|
|
4100
|
-
case "union":
|
|
4101
|
-
typeAccessor = generateUnionCheck(doc, ctx, schema, accessor);
|
|
4102
|
-
break;
|
|
4103
|
-
case "intersection":
|
|
4104
|
-
typeAccessor = generateIntersectionCheck(doc, ctx, schema, accessor);
|
|
4105
|
-
break;
|
|
4106
|
-
case "record":
|
|
4107
|
-
typeAccessor = generateRecordCheck(doc, ctx, schema, accessor);
|
|
4108
|
-
break;
|
|
4109
|
-
case "map":
|
|
4110
|
-
typeAccessor = generateMapCheck(doc, ctx, schema, accessor);
|
|
4111
|
-
break;
|
|
4112
|
-
case "set":
|
|
4113
|
-
typeAccessor = generateSetCheck(doc, ctx, schema, accessor);
|
|
4114
|
-
break;
|
|
4115
|
-
case "file":
|
|
4116
|
-
typeAccessor = generateFileCheck(doc, accessor);
|
|
4117
|
-
break;
|
|
4118
|
-
case "template_literal":
|
|
4119
|
-
typeAccessor = generateTemplateLiteralCheck(doc, ctx, schema, accessor);
|
|
4120
|
-
break;
|
|
4121
|
-
case "lazy":
|
|
4122
|
-
typeAccessor = generateLazyCheck(doc, ctx, schema, accessor);
|
|
4123
|
-
break;
|
|
4124
|
-
case "pipe":
|
|
4125
|
-
typeAccessor = generatePipeCheck(doc, ctx, schema, accessor);
|
|
4126
|
-
break;
|
|
4127
|
-
case "custom":
|
|
4128
|
-
typeAccessor = generateCustomCheck(doc, ctx, schema, accessor);
|
|
4129
|
-
break;
|
|
4130
|
-
case "transform":
|
|
4131
|
-
typeAccessor = generateTransformCheck(doc, ctx, schema, accessor);
|
|
4132
|
-
break;
|
|
4133
|
-
case "catch":
|
|
4134
|
-
typeAccessor = generateCatchCheck(doc, ctx, schema, accessor);
|
|
4135
|
-
break;
|
|
4136
|
-
default: {
|
|
4137
|
-
throw new ZodCompileUnsupportedError(`schema type ${type}`);
|
|
4138
|
-
}
|
|
4139
|
-
}
|
|
4140
|
-
if (typeAccessor === null)
|
|
4141
|
-
return null;
|
|
4142
|
-
return generateChecks(doc, ctx, schema, typeAccessor);
|
|
4143
|
-
}
|
|
4144
|
-
function generateStringCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4145
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4146
|
-
const def = schema._zod.def;
|
|
4147
|
-
if (def.format === undefined)
|
|
4148
|
-
return accessor;
|
|
4149
|
-
return generateStringFormatCheck(doc, ctx, def, accessor, needsValue);
|
|
4150
|
-
}
|
|
4151
|
-
function generateNumberCheck(doc, schema, accessor) {
|
|
4152
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isFinite(${accessor})) return INVALID;`);
|
|
4153
|
-
const def = schema._zod.def;
|
|
4154
|
-
if (def.check === "number_format" && def.format) {
|
|
4155
|
-
generateNumberFormatCheck(doc, { format: def.format }, accessor);
|
|
4156
|
-
}
|
|
4157
|
-
return accessor;
|
|
4158
|
-
}
|
|
4159
|
-
function generateBooleanCheck(doc, accessor) {
|
|
4160
|
-
doc.write(`if (typeof ${accessor} !== "boolean") return INVALID;`);
|
|
4161
|
-
return accessor;
|
|
4162
|
-
}
|
|
4163
|
-
function generateBigIntCheck(doc, schema, accessor) {
|
|
4164
|
-
doc.write(`if (typeof ${accessor} !== "bigint") return INVALID;`);
|
|
4165
|
-
const def = schema._zod.def;
|
|
4166
|
-
if (def.format) {
|
|
4167
|
-
switch (def.format) {
|
|
4168
|
-
case "int64":
|
|
4169
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
4170
|
-
break;
|
|
4171
|
-
case "uint64":
|
|
4172
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
4173
|
-
break;
|
|
4174
|
-
}
|
|
4175
|
-
}
|
|
4176
|
-
return accessor;
|
|
4177
|
-
}
|
|
4178
|
-
function generateSymbolCheck(doc, accessor) {
|
|
4179
|
-
doc.write(`if (typeof ${accessor} !== "symbol") return INVALID;`);
|
|
4180
|
-
return accessor;
|
|
4181
|
-
}
|
|
4182
|
-
function generateUndefinedCheck(doc, accessor) {
|
|
4183
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4184
|
-
return accessor;
|
|
4185
|
-
}
|
|
4186
|
-
function generateNullCheck(doc, accessor) {
|
|
4187
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4188
|
-
return accessor;
|
|
4189
|
-
}
|
|
4190
|
-
function generateVoidCheck(doc, accessor) {
|
|
4191
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4192
|
-
return accessor;
|
|
4193
|
-
}
|
|
4194
|
-
function generateNaNCheck(doc, accessor) {
|
|
4195
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isNaN(${accessor})) return INVALID;`);
|
|
4196
|
-
return accessor;
|
|
4197
|
-
}
|
|
4198
|
-
function generateDateCheck(doc, accessor) {
|
|
4199
|
-
doc.write(`if (!(${accessor} instanceof Date) || Number.isNaN(${accessor}.getTime())) return INVALID;`);
|
|
4200
|
-
return accessor;
|
|
4201
|
-
}
|
|
4202
|
-
function generateObjectCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4203
|
-
const def = schema._zod.def;
|
|
4204
|
-
doc.write(`if (typeof ${accessor} !== "object" || ${accessor} === null || Array.isArray(${accessor})) return INVALID;`);
|
|
4205
|
-
const shape = def.shape;
|
|
4206
|
-
const keys = Object.keys(shape);
|
|
4207
|
-
const symbolKeys = Object.getOwnPropertySymbols(shape);
|
|
4208
|
-
const allKeys = symbolKeys.length ? [...keys, ...symbolKeys] : keys;
|
|
4209
|
-
const keyExpr = (k) => typeof k === "symbol" ? addConstant(ctx, k) : esc(k);
|
|
4210
|
-
const propKey = (k) => typeof k === "symbol" ? `[${keyExpr(k)}]` : esc(k);
|
|
4211
|
-
const propShape = shape;
|
|
4212
|
-
if (keys.includes("__proto__")) {
|
|
4213
|
-
throw new ZodCompileUnsupportedError('object shape key "__proto__"');
|
|
4214
|
-
}
|
|
4215
|
-
const propOutputs = new Map;
|
|
4216
|
-
for (const key of allKeys) {
|
|
4217
|
-
const propSchema = propShape[key];
|
|
4218
|
-
const kx = keyExpr(key);
|
|
4219
|
-
const inputVar = newVar(ctx);
|
|
4220
|
-
doc.write(`const ${inputVar} = ${accessor}[${kx}];`);
|
|
4221
|
-
if (propSchema._zod.optin !== undefined) {
|
|
4222
|
-
const outputVar = newVar(ctx);
|
|
4223
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
4224
|
-
doc.indented((d) => {
|
|
4225
|
-
const outputAccessor = compileChild(d, ctx, propSchema, inputVar);
|
|
4226
|
-
d.write(`return ${outputAccessor};`);
|
|
4227
|
-
});
|
|
4228
|
-
doc.write(`})();`);
|
|
4229
|
-
if (propSchema._zod.optout === "optional") {
|
|
4230
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
4231
|
-
doc.indented((d) => {
|
|
4232
|
-
d.write(`if (${kx} in ${accessor}) return INVALID;`);
|
|
4233
|
-
d.write(`${outputVar} = undefined;`);
|
|
4234
|
-
});
|
|
4235
|
-
doc.write(`}`);
|
|
4236
|
-
} else {
|
|
4237
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4238
|
-
}
|
|
4239
|
-
propOutputs.set(key, outputVar);
|
|
4240
|
-
} else {
|
|
4241
|
-
if (requiresPresenceCheck(propSchema)) {
|
|
4242
|
-
doc.write(`if (!(${kx} in ${accessor})) return INVALID;`);
|
|
4243
|
-
}
|
|
4244
|
-
const outputAccessor = compileChild(doc, ctx, propSchema, inputVar, buildsValue);
|
|
4245
|
-
if (outputAccessor !== null)
|
|
4246
|
-
propOutputs.set(key, outputAccessor);
|
|
4247
|
-
}
|
|
4248
|
-
}
|
|
4249
|
-
const catchall = def.catchall;
|
|
4250
|
-
let unknownKeysMode = "none";
|
|
4251
|
-
if (catchall) {
|
|
4252
|
-
const catchallType = catchall._zod.def.type;
|
|
4253
|
-
if (catchallType === "never") {
|
|
4254
|
-
const condition = keys.map((k) => `k !== ${esc(k)}`).join(" && ") || "true";
|
|
4255
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4256
|
-
doc.indented((d) => {
|
|
4257
|
-
d.write(`if (${condition}) return INVALID;`);
|
|
4258
|
-
});
|
|
4259
|
-
doc.write(`}`);
|
|
4260
|
-
} else if ((catchallType === "unknown" || catchallType === "any") && !catchall._zod.def.checks?.length) {
|
|
4261
|
-
unknownKeysMode = "passthrough";
|
|
4262
|
-
} else {
|
|
4263
|
-
unknownKeysMode = "schema";
|
|
4264
|
-
}
|
|
4265
|
-
}
|
|
4266
|
-
const outputVar = newVar(ctx);
|
|
4267
|
-
const hasConditionalKeys = allKeys.some((k) => mayOmitUndefined(propShape[k]) || dropsWhenAbsent(propShape[k]));
|
|
4268
|
-
if (!buildsValue) {
|
|
4269
|
-
if (unknownKeysMode === "schema") {
|
|
4270
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4271
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4272
|
-
doc.indented((d) => {
|
|
4273
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4274
|
-
if (knownSet)
|
|
4275
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4276
|
-
const valVar = newVar(ctx);
|
|
4277
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4278
|
-
compileChild(d, ctx, catchall, valVar, false);
|
|
4279
|
-
});
|
|
4280
|
-
doc.write(`}`);
|
|
4281
|
-
}
|
|
4282
|
-
return null;
|
|
4283
|
-
}
|
|
4284
|
-
if (!hasConditionalKeys) {
|
|
4285
|
-
const propLiterals = allKeys.map((k) => `${propKey(k)}: ${propOutputs.get(k)}`).join(", ");
|
|
4286
|
-
doc.write(`const ${outputVar} = { ${propLiterals} };`);
|
|
4287
|
-
} else {
|
|
4288
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4289
|
-
for (const k of allKeys) {
|
|
4290
|
-
const kx = keyExpr(k);
|
|
4291
|
-
const out = propOutputs.get(k);
|
|
4292
|
-
if (dropsWhenAbsent(propShape[k])) {
|
|
4293
|
-
doc.write(`if (${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4294
|
-
} else if (mayOmitUndefined(propShape[k])) {
|
|
4295
|
-
doc.write(`if (${out} !== undefined || ${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4296
|
-
} else {
|
|
4297
|
-
doc.write(`${outputVar}[${kx}] = ${out};`);
|
|
4298
|
-
}
|
|
4299
|
-
}
|
|
4300
|
-
}
|
|
4301
|
-
if (unknownKeysMode !== "none") {
|
|
4302
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4303
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4304
|
-
doc.indented((d) => {
|
|
4305
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4306
|
-
if (knownSet)
|
|
4307
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4308
|
-
if (unknownKeysMode === "passthrough") {
|
|
4309
|
-
d.write(`${outputVar}[k] = ${accessor}[k];`);
|
|
4310
|
-
} else {
|
|
4311
|
-
const valVar = newVar(ctx);
|
|
4312
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4313
|
-
const catchallOut = compileChild(d, ctx, catchall, valVar);
|
|
4314
|
-
d.write(`${outputVar}[k] = ${catchallOut};`);
|
|
4315
|
-
}
|
|
4316
|
-
});
|
|
4317
|
-
doc.write(`}`);
|
|
4318
|
-
}
|
|
4319
|
-
return outputVar;
|
|
4320
|
-
}
|
|
4321
|
-
function generateOptionalCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4322
|
-
const def = schema._zod.def;
|
|
4323
|
-
if (isExactOptional(schema)) {
|
|
4324
|
-
return generateCheck(doc, ctx, def.innerType, accessor, buildsValue);
|
|
4325
|
-
}
|
|
4326
|
-
if (def.innerType._zod.optin === "defaulted") {
|
|
4327
|
-
const outputVar = newVar(ctx);
|
|
4328
|
-
const branchVar = newVar(ctx);
|
|
4329
|
-
doc.write(`let ${outputVar};`);
|
|
4330
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4331
|
-
doc.indented((d) => {
|
|
4332
|
-
d.write(`const ${branchVar} = (() => {`);
|
|
4333
|
-
d.indented((d2) => {
|
|
4334
|
-
const innerOutput = generateCheck(d2, ctx, def.innerType, accessor);
|
|
4335
|
-
d2.write(`return ${innerOutput};`);
|
|
4336
|
-
});
|
|
4337
|
-
d.write(`})();`);
|
|
4338
|
-
d.write(`if (${branchVar} !== INVALID) ${outputVar} = ${branchVar};`);
|
|
4339
|
-
});
|
|
4340
|
-
doc.write(`} else {`);
|
|
4341
|
-
doc.indented((d) => {
|
|
4342
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4343
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4344
|
-
});
|
|
4345
|
-
doc.write(`}`);
|
|
4346
|
-
return outputVar;
|
|
4347
|
-
}
|
|
4348
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4349
|
-
if (outputVar)
|
|
4350
|
-
doc.write(`let ${outputVar};`);
|
|
4351
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4352
|
-
doc.indented((d) => {
|
|
4353
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4354
|
-
if (outputVar && innerOutput !== null)
|
|
4355
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4356
|
-
});
|
|
4357
|
-
doc.write(`}`);
|
|
4358
|
-
return outputVar;
|
|
4359
|
-
}
|
|
4360
|
-
function isExactOptional(schema) {
|
|
4361
|
-
return schema._zod.traits?.has("$ZodExactOptional") === true;
|
|
4362
|
-
}
|
|
4363
|
-
function requiresPresenceCheck(schema) {
|
|
4364
|
-
return schema._zod.optin === undefined && fastPathAcceptsAbsence(schema);
|
|
4365
|
-
}
|
|
4366
|
-
function fastPathAcceptsAbsence(schema) {
|
|
4367
|
-
if (schema._zod.def.coerce)
|
|
4368
|
-
return true;
|
|
4369
|
-
const def = schema._zod.def;
|
|
4370
|
-
switch (def.type) {
|
|
4371
|
-
case "any":
|
|
4372
|
-
case "unknown":
|
|
4373
|
-
case "undefined":
|
|
4374
|
-
case "void":
|
|
4375
|
-
case "default":
|
|
4376
|
-
case "prefault":
|
|
4377
|
-
case "transform":
|
|
4378
|
-
case "custom":
|
|
4379
|
-
case "lazy":
|
|
4380
|
-
return true;
|
|
4381
|
-
case "string":
|
|
4382
|
-
case "number":
|
|
4383
|
-
case "boolean":
|
|
4384
|
-
case "bigint":
|
|
4385
|
-
case "symbol":
|
|
4386
|
-
case "null":
|
|
4387
|
-
case "never":
|
|
4388
|
-
case "nan":
|
|
4389
|
-
case "date":
|
|
4390
|
-
case "object":
|
|
4391
|
-
case "array":
|
|
4392
|
-
case "tuple":
|
|
4393
|
-
case "record":
|
|
4394
|
-
case "map":
|
|
4395
|
-
case "set":
|
|
4396
|
-
case "file":
|
|
4397
|
-
case "template_literal":
|
|
4398
|
-
return false;
|
|
4399
|
-
case "nonoptional":
|
|
4400
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : false;
|
|
4401
|
-
case "literal":
|
|
4402
|
-
return !!def.values?.includes(undefined);
|
|
4403
|
-
case "enum":
|
|
4404
|
-
return !!schema._zod.values?.has(undefined);
|
|
4405
|
-
case "optional":
|
|
4406
|
-
case "nullable":
|
|
4407
|
-
case "readonly":
|
|
4408
|
-
case "success":
|
|
4409
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : true;
|
|
4410
|
-
case "catch":
|
|
4411
|
-
return true;
|
|
4412
|
-
case "union":
|
|
4413
|
-
return def.options ? def.options.some(fastPathAcceptsAbsence) : true;
|
|
4414
|
-
case "intersection":
|
|
4415
|
-
if (!def.left || !def.right)
|
|
4416
|
-
return true;
|
|
4417
|
-
return fastPathAcceptsAbsence(def.left) && fastPathAcceptsAbsence(def.right);
|
|
4418
|
-
case "pipe":
|
|
4419
|
-
return def.in ? fastPathAcceptsAbsence(def.in) : true;
|
|
4420
|
-
default:
|
|
4421
|
-
return true;
|
|
4422
|
-
}
|
|
4423
|
-
}
|
|
4424
|
-
function dropsWhenAbsent(schema) {
|
|
4425
|
-
return schema._zod.optin === "optional" && schema._zod.optout === "optional";
|
|
4426
|
-
}
|
|
4427
|
-
function mayOmitUndefined(schema) {
|
|
4428
|
-
return (schema._zod.optin !== "defaulted" || schema._zod.optout === "optional") && mayOutputUndefined(schema);
|
|
4429
|
-
}
|
|
4430
|
-
function mayOutputUndefined(schema) {
|
|
4431
|
-
const def = schema._zod.def;
|
|
4432
|
-
switch (def.type) {
|
|
4433
|
-
case "string":
|
|
4434
|
-
case "number":
|
|
4435
|
-
case "boolean":
|
|
4436
|
-
case "bigint":
|
|
4437
|
-
case "symbol":
|
|
4438
|
-
case "null":
|
|
4439
|
-
case "nan":
|
|
4440
|
-
case "date":
|
|
4441
|
-
case "object":
|
|
4442
|
-
case "array":
|
|
4443
|
-
case "tuple":
|
|
4444
|
-
case "record":
|
|
4445
|
-
case "map":
|
|
4446
|
-
case "set":
|
|
4447
|
-
case "file":
|
|
4448
|
-
case "template_literal":
|
|
4449
|
-
case "never":
|
|
4450
|
-
case "success":
|
|
4451
|
-
return false;
|
|
4452
|
-
case "literal":
|
|
4453
|
-
return !!def.values?.includes(undefined);
|
|
4454
|
-
case "enum":
|
|
4455
|
-
return !!schema._zod.values?.has(undefined);
|
|
4456
|
-
case "optional":
|
|
4457
|
-
return true;
|
|
4458
|
-
case "nullable":
|
|
4459
|
-
case "readonly":
|
|
4460
|
-
case "nonoptional":
|
|
4461
|
-
return def.innerType ? mayOutputUndefined(def.innerType) : true;
|
|
4462
|
-
case "union":
|
|
4463
|
-
return def.options ? def.options.some(mayOutputUndefined) : true;
|
|
4464
|
-
case "intersection":
|
|
4465
|
-
return !def.left || !def.right || mayOutputUndefined(def.left) || mayOutputUndefined(def.right);
|
|
4466
|
-
case "pipe":
|
|
4467
|
-
return def.out ? mayOutputUndefined(def.out) : true;
|
|
4468
|
-
default:
|
|
4469
|
-
return true;
|
|
4470
|
-
}
|
|
4471
|
-
}
|
|
4472
|
-
function generateNullableCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4473
|
-
const def = schema._zod.def;
|
|
4474
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4475
|
-
if (outputVar)
|
|
4476
|
-
doc.write(`let ${outputVar} = null;`);
|
|
4477
|
-
doc.write(`if (${accessor} !== null) {`);
|
|
4478
|
-
doc.indented((d) => {
|
|
4479
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4480
|
-
if (outputVar && innerOutput !== null)
|
|
4481
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4482
|
-
});
|
|
4483
|
-
doc.write(`}`);
|
|
4484
|
-
return outputVar;
|
|
4485
|
-
}
|
|
4486
|
-
function generateArrayCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4487
|
-
const def = schema._zod.def;
|
|
4488
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4489
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4490
|
-
const iVar = newVar(ctx);
|
|
4491
|
-
const elemVar = newVar(ctx);
|
|
4492
|
-
if (outputVar)
|
|
4493
|
-
doc.write(`const ${outputVar} = new Array(${accessor}.length);`);
|
|
4494
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4495
|
-
doc.indented((d) => {
|
|
4496
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4497
|
-
const elemOutput = compileChild(d, ctx, def.element, elemVar, buildsValue);
|
|
4498
|
-
if (outputVar && elemOutput !== null)
|
|
4499
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4500
|
-
});
|
|
4501
|
-
doc.write(`}`);
|
|
4502
|
-
return outputVar;
|
|
4503
|
-
}
|
|
4504
|
-
function generateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4505
|
-
const def = schema._zod.def;
|
|
4506
|
-
const values = def.values;
|
|
4507
|
-
if (values.length !== 1) {
|
|
4508
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4509
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4510
|
-
return accessor;
|
|
4511
|
-
}
|
|
4512
|
-
const value = values[0];
|
|
4513
|
-
if (typeof value === "number" && Number.isNaN(value)) {
|
|
4514
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4515
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4516
|
-
return accessor;
|
|
4517
|
-
}
|
|
4518
|
-
if (typeof value === "string") {
|
|
4519
|
-
doc.write(`if (${accessor} !== ${esc(value)}) return INVALID;`);
|
|
4520
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
4521
|
-
doc.write(`if (${accessor} !== ${value}) return INVALID;`);
|
|
4522
|
-
} else if (value === null) {
|
|
4523
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4524
|
-
} else if (value === undefined) {
|
|
4525
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4526
|
-
} else if (typeof value === "bigint") {
|
|
4527
|
-
doc.write(`if (${accessor} !== ${value}n) return INVALID;`);
|
|
4528
|
-
} else {
|
|
4529
|
-
throw new ZodCompileUnsupportedError(`literal type ${typeof value}`);
|
|
4530
|
-
}
|
|
4531
|
-
return accessor;
|
|
4532
|
-
}
|
|
4533
|
-
function generateEnumCheck(doc, ctx, schema, accessor) {
|
|
4534
|
-
const values = schema._zod.values;
|
|
4535
|
-
if (!values) {
|
|
4536
|
-
throw new ZodCompileUnsupportedError("enum schema without enumerated values");
|
|
4537
|
-
}
|
|
4538
|
-
const enumSet = addConstant(ctx, values);
|
|
4539
|
-
doc.write(`if (!${enumSet}.has(${accessor})) return INVALID;`);
|
|
4540
|
-
return accessor;
|
|
4541
|
-
}
|
|
4542
|
-
function generateWrapperCheck(doc, ctx, schema, accessor) {
|
|
4543
|
-
const def = schema._zod.def;
|
|
4544
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4545
|
-
}
|
|
4546
|
-
function generateDefaultCheck(doc, ctx, schema, accessor) {
|
|
4547
|
-
const def = schema._zod.def;
|
|
4548
|
-
const descriptor = Object.getOwnPropertyDescriptor(schema._zod.def, "defaultValue");
|
|
4549
|
-
const defaultGetter = descriptor ? () => schema._zod.def.defaultValue : undefined;
|
|
4550
|
-
if (schema._zod.def.type === "prefault") {
|
|
4551
|
-
if (!defaultGetter) {
|
|
4552
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4553
|
-
}
|
|
4554
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4555
|
-
const inputVar = newVar(ctx);
|
|
4556
|
-
doc.write(`let ${inputVar} = ${accessor};`);
|
|
4557
|
-
doc.write(`if (${accessor} === undefined) ${inputVar} = ${defaultFn}();`);
|
|
4558
|
-
return generateCheck(doc, ctx, def.innerType, inputVar);
|
|
4559
|
-
}
|
|
4560
|
-
const outputVar = newVar(ctx);
|
|
4561
|
-
if (defaultGetter) {
|
|
4562
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4563
|
-
const cloneFn = addConstant(ctx, shallowClone);
|
|
4564
|
-
doc.write(`let ${outputVar};`);
|
|
4565
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4566
|
-
doc.indented((d) => {
|
|
4567
|
-
d.write(`${outputVar} = ${cloneFn}(${defaultFn}());`);
|
|
4568
|
-
});
|
|
4569
|
-
doc.write(`} else {`);
|
|
4570
|
-
doc.indented((d) => {
|
|
4571
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4572
|
-
d.write(`${outputVar} = ${innerOutput} === undefined ? ${cloneFn}(${defaultFn}()) : ${innerOutput};`);
|
|
4573
|
-
});
|
|
4574
|
-
doc.write(`}`);
|
|
4575
|
-
} else {
|
|
4576
|
-
doc.write(`let ${outputVar};`);
|
|
4577
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4578
|
-
doc.indented((d) => {
|
|
4579
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4580
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4581
|
-
});
|
|
4582
|
-
doc.write(`}`);
|
|
4583
|
-
}
|
|
4584
|
-
return outputVar;
|
|
4585
|
-
}
|
|
4586
|
-
function generateNonOptionalCheck(doc, ctx, schema, accessor) {
|
|
4587
|
-
const def = schema._zod.def;
|
|
4588
|
-
const innerOutput = generateCheck(doc, ctx, def.innerType, accessor);
|
|
4589
|
-
const outputVar = newVar(ctx);
|
|
4590
|
-
doc.write(`const ${outputVar} = ${innerOutput};`);
|
|
4591
|
-
doc.write(`if (${outputVar} === undefined) return INVALID;`);
|
|
4592
|
-
return outputVar;
|
|
4593
|
-
}
|
|
4594
|
-
function generateTupleCheck(doc, ctx, schema, accessor) {
|
|
4595
|
-
const def = schema._zod.def;
|
|
4596
|
-
const items = def.items;
|
|
4597
|
-
const rest = def.rest;
|
|
4598
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4599
|
-
const optinStart = getTupleOptStart(items, "optin");
|
|
4600
|
-
const optoutStart = getTupleOptStart(items, "optout");
|
|
4601
|
-
if (rest) {
|
|
4602
|
-
doc.write(`if (${accessor}.length < ${optinStart}) return INVALID;`);
|
|
4603
|
-
} else {
|
|
4604
|
-
doc.write(`if (${accessor}.length < ${optinStart} || ${accessor}.length > ${items.length}) return INVALID;`);
|
|
4605
|
-
}
|
|
4606
|
-
const outputVar = newVar(ctx);
|
|
4607
|
-
doc.write(`const ${outputVar} = [];`);
|
|
4608
|
-
for (let i = 0;i < items.length; i++) {
|
|
4609
|
-
const itemSchema = items[i];
|
|
4610
|
-
if (i >= optoutStart) {
|
|
4611
|
-
doc.write(`if (${outputVar}.length === ${i}) {`);
|
|
4612
|
-
doc.indented((d) => {
|
|
4613
|
-
d.write(`if (${i} < ${accessor}.length) {`);
|
|
4614
|
-
d.indented((d2) => {
|
|
4615
|
-
const elemVar = newVar(ctx);
|
|
4616
|
-
d2.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4617
|
-
const elemOutput = compileChild(d2, ctx, itemSchema, elemVar);
|
|
4618
|
-
d2.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4619
|
-
});
|
|
4620
|
-
d.write(`} else {`);
|
|
4621
|
-
d.indented((d2) => {
|
|
4622
|
-
if (dropsWhenAbsent(itemSchema)) {
|
|
4623
|
-
d2.write(`${outputVar}.length = ${i};`);
|
|
4624
|
-
return;
|
|
4625
|
-
}
|
|
4626
|
-
const elemVar = newVar(ctx);
|
|
4627
|
-
const branchVar = newVar(ctx);
|
|
4628
|
-
d2.write(`const ${elemVar} = undefined;`);
|
|
4629
|
-
d2.write(`const ${branchVar} = (() => {`);
|
|
4630
|
-
d2.indented((d3) => {
|
|
4631
|
-
const elemOutput = compileChild(d3, ctx, itemSchema, elemVar);
|
|
4632
|
-
d3.write(`return ${elemOutput};`);
|
|
4633
|
-
});
|
|
4634
|
-
d2.write(`})();`);
|
|
4635
|
-
d2.write(`if (${branchVar} === INVALID || ${branchVar} === undefined) ${outputVar}.length = ${i};`);
|
|
4636
|
-
d2.write(`else ${outputVar}[${i}] = ${branchVar};`);
|
|
4637
|
-
});
|
|
4638
|
-
d.write(`}`);
|
|
4639
|
-
});
|
|
4640
|
-
doc.write(`}`);
|
|
4641
|
-
} else {
|
|
4642
|
-
const elemVar = newVar(ctx);
|
|
4643
|
-
doc.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4644
|
-
const elemOutput = compileChild(doc, ctx, itemSchema, elemVar);
|
|
4645
|
-
doc.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4646
|
-
}
|
|
4647
|
-
}
|
|
4648
|
-
if (rest) {
|
|
4649
|
-
const iVar = newVar(ctx);
|
|
4650
|
-
const elemVar = newVar(ctx);
|
|
4651
|
-
doc.write(`for (let ${iVar} = ${items.length}; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4652
|
-
doc.indented((d) => {
|
|
4653
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4654
|
-
const elemOutput = compileChild(d, ctx, rest, elemVar);
|
|
4655
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4656
|
-
});
|
|
4657
|
-
doc.write(`}`);
|
|
4658
|
-
}
|
|
4659
|
-
return outputVar;
|
|
4660
|
-
}
|
|
4661
|
-
function getTupleOptStart(items, key) {
|
|
4662
|
-
for (let i = items.length - 1;i >= 0; i--) {
|
|
4663
|
-
const omittable = key === "optin" ? items[i]._zod.optin !== undefined : items[i]._zod.optout === "optional";
|
|
4664
|
-
if (!omittable)
|
|
4665
|
-
return i + 1;
|
|
4666
|
-
}
|
|
4667
|
-
return 0;
|
|
4668
|
-
}
|
|
4669
|
-
function generateUnionCheck(doc, ctx, schema, accessor) {
|
|
4670
|
-
const def = schema._zod.def;
|
|
4671
|
-
const options = def.options;
|
|
4672
|
-
if (def.discriminator) {
|
|
4673
|
-
return generateDiscriminatedUnionCheck(doc, ctx, def, accessor);
|
|
4674
|
-
}
|
|
4675
|
-
if (def.inclusive === false) {
|
|
4676
|
-
throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
|
|
4677
|
-
}
|
|
4678
|
-
if (options.length === 0) {
|
|
4679
|
-
doc.write("return INVALID;");
|
|
4680
|
-
return accessor;
|
|
4681
|
-
}
|
|
4682
|
-
if (options.length === 1) {
|
|
4683
|
-
return generateCheck(doc, ctx, options[0], accessor);
|
|
4684
|
-
}
|
|
4685
|
-
const allLiterals = options.every((opt) => opt._zod.def.type === "literal" && !opt._zod.def.checks?.length);
|
|
4686
|
-
if (allLiterals) {
|
|
4687
|
-
const values = new Set(options.flatMap((opt) => opt._zod.def.values));
|
|
4688
|
-
const valuesConst = addConstant(ctx, values);
|
|
4689
|
-
doc.write(`if (!${valuesConst}.has(${accessor})) return INVALID;`);
|
|
4690
|
-
return accessor;
|
|
4691
|
-
}
|
|
4692
|
-
const outputVar = newVar(ctx);
|
|
4693
|
-
doc.write(`let ${outputVar};`);
|
|
4694
|
-
for (let i = 0;i < options.length; i++) {
|
|
4695
|
-
const opt = options[i];
|
|
4696
|
-
if (i === 0) {
|
|
4697
|
-
doc.write(`${outputVar} = (() => {`);
|
|
4698
|
-
} else {
|
|
4699
|
-
doc.write(`if (${outputVar} === INVALID) ${outputVar} = (() => {`);
|
|
3352
|
+
default:
|
|
3353
|
+
return `Invalid input`;
|
|
4700
3354
|
}
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
}
|
|
4707
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4708
|
-
return outputVar;
|
|
3355
|
+
};
|
|
3356
|
+
};
|
|
3357
|
+
function en_default() {
|
|
3358
|
+
return {
|
|
3359
|
+
localeError: error()
|
|
3360
|
+
};
|
|
4709
3361
|
}
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
return accessor;
|
|
3362
|
+
// node_modules/zod/v4/core/registries.js
|
|
3363
|
+
var _a2;
|
|
3364
|
+
class $ZodRegistry {
|
|
3365
|
+
constructor() {
|
|
3366
|
+
this._map = new WeakMap;
|
|
3367
|
+
this._idmap = new Map;
|
|
4717
3368
|
}
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
const claimed = new Set;
|
|
4724
|
-
for (const option of def.options) {
|
|
4725
|
-
const values = option._zod.propValues?.[def.discriminator];
|
|
4726
|
-
if (!values || values.size === 0) {
|
|
4727
|
-
throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
|
|
3369
|
+
add(schema, ..._meta) {
|
|
3370
|
+
const meta = _meta[0];
|
|
3371
|
+
this._map.set(schema, meta);
|
|
3372
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
3373
|
+
this._idmap.set(meta.id, schema);
|
|
4728
3374
|
}
|
|
4729
|
-
|
|
4730
|
-
if (claimed.has(value)) {
|
|
4731
|
-
throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(value)}`);
|
|
4732
|
-
}
|
|
4733
|
-
claimed.add(value);
|
|
4734
|
-
}
|
|
4735
|
-
const conditions = Array.from(values, (value) => literalEquality(ctx, discVar, value));
|
|
4736
|
-
const prefix = firstBranch ? "if" : "else if";
|
|
4737
|
-
doc.write(`${prefix} (${conditions.join(" || ")}) {`);
|
|
4738
|
-
doc.indented((d) => {
|
|
4739
|
-
const branchOutput = generateCheck(d, ctx, option, accessor);
|
|
4740
|
-
d.write(`${outputVar} = ${branchOutput};`);
|
|
4741
|
-
});
|
|
4742
|
-
doc.write(`}`);
|
|
4743
|
-
firstBranch = false;
|
|
4744
|
-
}
|
|
4745
|
-
doc.write(`else { return INVALID; }`);
|
|
4746
|
-
return outputVar;
|
|
4747
|
-
}
|
|
4748
|
-
function literalEquality(ctx, accessor, value) {
|
|
4749
|
-
if (typeof value === "string")
|
|
4750
|
-
return `${accessor} === ${esc(value)}`;
|
|
4751
|
-
if (typeof value === "number") {
|
|
4752
|
-
if (Number.isNaN(value))
|
|
4753
|
-
return `Number.isNaN(${accessor})`;
|
|
4754
|
-
return `${accessor} === ${value}`;
|
|
3375
|
+
return this;
|
|
4755
3376
|
}
|
|
4756
|
-
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
return
|
|
4760
|
-
if (value === undefined)
|
|
4761
|
-
return `${accessor} === undefined`;
|
|
4762
|
-
if (typeof value === "bigint")
|
|
4763
|
-
return `${accessor} === ${value}n`;
|
|
4764
|
-
if (typeof value === "symbol") {
|
|
4765
|
-
const symbolConst = addConstant(ctx, value);
|
|
4766
|
-
return `${accessor} === ${symbolConst}`;
|
|
3377
|
+
clear() {
|
|
3378
|
+
this._map = new WeakMap;
|
|
3379
|
+
this._idmap = new Map;
|
|
3380
|
+
return this;
|
|
4767
3381
|
}
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
4771
|
-
|
|
4772
|
-
|
|
4773
|
-
|
|
4774
|
-
|
|
4775
|
-
const mergeConst = addConstant(ctx, mergeValues);
|
|
4776
|
-
const mergedVar = newVar(ctx);
|
|
4777
|
-
doc.write(`const ${mergedVar} = ${mergeConst}(${leftOutput}, ${rightOutput});`);
|
|
4778
|
-
doc.write(`if (!${mergedVar}.valid) return INVALID;`);
|
|
4779
|
-
return `${mergedVar}.data`;
|
|
4780
|
-
}
|
|
4781
|
-
function generateRecordCheck(doc, ctx, schema, accessor) {
|
|
4782
|
-
const def = schema._zod.def;
|
|
4783
|
-
const isPlainObjectConst = addConstant(ctx, isPlainObject);
|
|
4784
|
-
doc.write(`if (!${isPlainObjectConst}(${accessor})) return INVALID;`);
|
|
4785
|
-
const outputVar = newVar(ctx);
|
|
4786
|
-
const kVar = newVar(ctx);
|
|
4787
|
-
const valVar = newVar(ctx);
|
|
4788
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4789
|
-
const recordDef = def;
|
|
4790
|
-
const keyValues = recordDef.partial ? undefined : def.keyType._zod.values;
|
|
4791
|
-
if (keyValues) {
|
|
4792
|
-
const inputKeys = [];
|
|
4793
|
-
for (const key of keyValues) {
|
|
4794
|
-
if (!(typeof key === "string" || typeof key === "number" || typeof key === "symbol")) {
|
|
4795
|
-
throw new ZodCompileUnsupportedError(`record key value ${String(key)}`);
|
|
4796
|
-
}
|
|
4797
|
-
const inputKey = typeof key === "number" ? key.toString() : key;
|
|
4798
|
-
if (inputKey === "__proto__") {
|
|
4799
|
-
throw new ZodCompileUnsupportedError('record key "__proto__"');
|
|
4800
|
-
}
|
|
4801
|
-
inputKeys.push(inputKey);
|
|
4802
|
-
const keyConst = addConstant(ctx, key);
|
|
4803
|
-
const outKey = generateCheck(doc, ctx, def.keyType, keyConst);
|
|
4804
|
-
const valueVar = newVar(ctx);
|
|
4805
|
-
doc.write(`const ${valueVar} = ${accessor}[${literalPropertyKey(ctx, inputKey)}];`);
|
|
4806
|
-
const valOutput = compileChild(doc, ctx, def.valueType, valueVar);
|
|
4807
|
-
doc.write(`${outputVar}[${outKey}] = ${valOutput};`);
|
|
4808
|
-
}
|
|
4809
|
-
const knownKeysConst = addConstant(ctx, new Set(inputKeys));
|
|
4810
|
-
doc.write(`for (const ${kVar} in ${accessor}) {`);
|
|
4811
|
-
doc.indented((d) => {
|
|
4812
|
-
d.write(`if (${knownKeysConst}.has(${kVar})) continue;`);
|
|
4813
|
-
if (recordDef.mode === "loose") {
|
|
4814
|
-
d.write(`if (${kVar} !== "__proto__") ${outputVar}[${kVar}] = ${accessor}[${kVar}];`);
|
|
4815
|
-
} else {
|
|
4816
|
-
d.write(`return INVALID;`);
|
|
4817
|
-
}
|
|
4818
|
-
});
|
|
4819
|
-
doc.write(`}`);
|
|
4820
|
-
return outputVar;
|
|
4821
|
-
}
|
|
4822
|
-
const keyDef = def.keyType._zod.def;
|
|
4823
|
-
const keyIsBareString = keyDef.type === "string" && keyDef.format === undefined && !keyDef.coerce && (keyDef.checks?.length ?? 0) === 0;
|
|
4824
|
-
if (!keyIsBareString) {
|
|
4825
|
-
const isLoose = def.mode === "loose";
|
|
4826
|
-
const keyFn = compileFn(def.keyType);
|
|
4827
|
-
if (keyFn.definite === false)
|
|
4828
|
-
ctx.definite = false;
|
|
4829
|
-
const keyFast = addConstant(ctx, keyFn);
|
|
4830
|
-
const numericConst = addConstant(ctx, number);
|
|
4831
|
-
const outKeyVar = newVar(ctx);
|
|
4832
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4833
|
-
d.write(`let ${outKeyVar} = ${keyFast}(${kVar});`);
|
|
4834
|
-
d.write(`if (${outKeyVar} === INVALID && typeof ${kVar} === "string" && ${numericConst}.test(${kVar})) ${outKeyVar} = ${keyFast}(Number(${kVar}));`);
|
|
4835
|
-
if (isLoose) {
|
|
4836
|
-
d.write(`if (${outKeyVar} === INVALID) { ${outputVar}[${kVar}] = ${accessor}[${kVar}]; continue; }`);
|
|
4837
|
-
} else {
|
|
4838
|
-
d.write(`if (${outKeyVar} === INVALID) return INVALID;`);
|
|
4839
|
-
}
|
|
4840
|
-
d.write(`if (${outKeyVar} === "__proto__") continue;`);
|
|
4841
|
-
const valueVar = newVar(ctx);
|
|
4842
|
-
d.write(`const ${valueVar} = ${accessor}[${kVar}];`);
|
|
4843
|
-
const valOutput = compileChild(d, ctx, def.valueType, valueVar);
|
|
4844
|
-
d.write(`${outputVar}[${outKeyVar}] = ${valOutput};`);
|
|
4845
|
-
});
|
|
4846
|
-
return outputVar;
|
|
4847
|
-
}
|
|
4848
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
4849
|
-
d.write(`const ${valVar} = ${accessor}[${kVar}];`);
|
|
4850
|
-
const valOutput = compileChild(d, ctx, def.valueType, valVar);
|
|
4851
|
-
d.write(`${outputVar}[${kVar}] = ${valOutput};`);
|
|
4852
|
-
}, `return INVALID;`);
|
|
4853
|
-
return outputVar;
|
|
4854
|
-
}
|
|
4855
|
-
function emitOwnKeys(doc, ctx, accessor, kVar, body, onSymbol) {
|
|
4856
|
-
const propIsEnumerableConst = addConstant(ctx, Object.prototype.propertyIsEnumerable);
|
|
4857
|
-
const symsVar = newVar(ctx);
|
|
4858
|
-
const keysVar = newVar(ctx);
|
|
4859
|
-
const iVar = newVar(ctx);
|
|
4860
|
-
doc.write(`const ${symsVar} = Object.getOwnPropertySymbols(${accessor});`);
|
|
4861
|
-
doc.write(`const ${keysVar} = Object.getOwnPropertyNames(${accessor});`);
|
|
4862
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${keysVar}.length; ${iVar}++) {`);
|
|
4863
|
-
doc.indented((d) => {
|
|
4864
|
-
d.write(`const ${kVar} = ${keysVar}[${iVar}];`);
|
|
4865
|
-
d.write(`if (${kVar} === "__proto__" || !${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4866
|
-
body(d);
|
|
4867
|
-
});
|
|
4868
|
-
doc.write(`}`);
|
|
4869
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${symsVar}.length; ${iVar}++) {`);
|
|
4870
|
-
doc.indented((d) => {
|
|
4871
|
-
d.write(`const ${kVar} = ${symsVar}[${iVar}];`);
|
|
4872
|
-
d.write(`if (!${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
4873
|
-
if (onSymbol)
|
|
4874
|
-
d.write(onSymbol);
|
|
4875
|
-
else
|
|
4876
|
-
body(d);
|
|
4877
|
-
});
|
|
4878
|
-
doc.write(`}`);
|
|
4879
|
-
}
|
|
4880
|
-
function literalPropertyKey(ctx, key) {
|
|
4881
|
-
if (typeof key === "string")
|
|
4882
|
-
return esc(key);
|
|
4883
|
-
return addConstant(ctx, key);
|
|
4884
|
-
}
|
|
4885
|
-
function generateMapCheck(doc, ctx, schema, accessor) {
|
|
4886
|
-
const def = schema._zod.def;
|
|
4887
|
-
doc.write(`if (!(${accessor} instanceof Map)) return INVALID;`);
|
|
4888
|
-
const outputVar = newVar(ctx);
|
|
4889
|
-
const kVar = newVar(ctx);
|
|
4890
|
-
const valVar = newVar(ctx);
|
|
4891
|
-
doc.write(`const ${outputVar} = new Map();`);
|
|
4892
|
-
doc.write(`for (const [${kVar}, ${valVar}] of ${accessor}) {`);
|
|
4893
|
-
doc.indented((d) => {
|
|
4894
|
-
const keyOutput = generateCheck(d, ctx, def.keyType, kVar);
|
|
4895
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4896
|
-
d.write(`${outputVar}.set(${keyOutput}, ${valOutput});`);
|
|
4897
|
-
});
|
|
4898
|
-
doc.write(`}`);
|
|
4899
|
-
return outputVar;
|
|
4900
|
-
}
|
|
4901
|
-
function generateSetCheck(doc, ctx, schema, accessor) {
|
|
4902
|
-
const def = schema._zod.def;
|
|
4903
|
-
doc.write(`if (!(${accessor} instanceof Set)) return INVALID;`);
|
|
4904
|
-
const outputVar = newVar(ctx);
|
|
4905
|
-
const valVar = newVar(ctx);
|
|
4906
|
-
doc.write(`const ${outputVar} = new Set();`);
|
|
4907
|
-
doc.write(`for (const ${valVar} of ${accessor}) {`);
|
|
4908
|
-
doc.indented((d) => {
|
|
4909
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
4910
|
-
d.write(`${outputVar}.add(${valOutput});`);
|
|
4911
|
-
});
|
|
4912
|
-
doc.write(`}`);
|
|
4913
|
-
return outputVar;
|
|
4914
|
-
}
|
|
4915
|
-
function generateFileCheck(doc, accessor) {
|
|
4916
|
-
doc.write(`if (!(${accessor} instanceof File)) return INVALID;`);
|
|
4917
|
-
return accessor;
|
|
4918
|
-
}
|
|
4919
|
-
function generateTemplateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4920
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4921
|
-
const pattern = schema._zod.pattern;
|
|
4922
|
-
if (pattern) {
|
|
4923
|
-
const patternConst = addConstant(ctx, pattern);
|
|
4924
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
4925
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
3382
|
+
remove(schema) {
|
|
3383
|
+
const meta = this._map.get(schema);
|
|
3384
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
3385
|
+
this._idmap.delete(meta.id);
|
|
3386
|
+
}
|
|
3387
|
+
this._map.delete(schema);
|
|
3388
|
+
return this;
|
|
4926
3389
|
}
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4935
|
-
|
|
4936
|
-
d.write(`${cacheConst}.parser = function(input) {`);
|
|
4937
|
-
d.indented((d2) => {
|
|
4938
|
-
d2.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
|
|
4939
|
-
d2.write(`return result.issues.length === 0 ? result.value : INVALID;`);
|
|
4940
|
-
});
|
|
4941
|
-
d.write(`};`);
|
|
4942
|
-
});
|
|
4943
|
-
doc.write(`}`);
|
|
4944
|
-
const outputVar = newVar(ctx);
|
|
4945
|
-
doc.write(`const ${outputVar} = ${cacheConst}.parser(${accessor});`);
|
|
4946
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4947
|
-
return outputVar;
|
|
4948
|
-
}
|
|
4949
|
-
function generatePipeCheck(doc, ctx, schema, accessor) {
|
|
4950
|
-
const def = schema._zod.def;
|
|
4951
|
-
const inputOutput = generateCheck(doc, ctx, def.in, accessor);
|
|
4952
|
-
if (def.transform) {
|
|
4953
|
-
if (isAsyncFunction(def.transform)) {
|
|
4954
|
-
throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
|
|
4955
|
-
}
|
|
4956
|
-
const transformFn = def.transform;
|
|
4957
|
-
const helperFn = (value) => {
|
|
4958
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
4959
|
-
const result = transformFn(value, fakePayload);
|
|
4960
|
-
if (result instanceof Promise)
|
|
4961
|
-
return INVALID;
|
|
4962
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
4963
|
-
};
|
|
4964
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
4965
|
-
const transformedVar = newVar(ctx);
|
|
4966
|
-
doc.write(`const ${transformedVar} = ${helperConst}(${inputOutput});`);
|
|
4967
|
-
doc.write(`if (${transformedVar} === INVALID) return INVALID;`);
|
|
4968
|
-
return generateCheck(doc, ctx, def.out, transformedVar);
|
|
4969
|
-
} else {
|
|
4970
|
-
return generateCheck(doc, ctx, def.out, inputOutput);
|
|
3390
|
+
get(schema) {
|
|
3391
|
+
const p = schema._zod.parent;
|
|
3392
|
+
if (p) {
|
|
3393
|
+
const pm = { ...this.get(p) ?? {} };
|
|
3394
|
+
delete pm.id;
|
|
3395
|
+
const f = { ...pm, ...this._map.get(schema) };
|
|
3396
|
+
return Object.keys(f).length ? f : undefined;
|
|
3397
|
+
}
|
|
3398
|
+
return this._map.get(schema);
|
|
4971
3399
|
}
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
return typeof fn === "function" && (fn.constructor.name === "AsyncFunction" || fn[Symbol.toStringTag] === "AsyncFunction");
|
|
4975
|
-
}
|
|
4976
|
-
function generateCustomCheck(doc, ctx, schema, accessor) {
|
|
4977
|
-
const def = schema._zod.def;
|
|
4978
|
-
if (def.fn) {
|
|
4979
|
-
if (isAsyncFunction(def.fn)) {
|
|
4980
|
-
throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
|
|
4981
|
-
}
|
|
4982
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
4983
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
4984
|
-
const resVar = newVar(ctx);
|
|
4985
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
4986
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
4987
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
4988
|
-
} else {
|
|
4989
|
-
throw new ZodCompileUnsupportedError("custom schema without a predicate function");
|
|
3400
|
+
has(schema) {
|
|
3401
|
+
return this._map.has(schema);
|
|
4990
3402
|
}
|
|
4991
|
-
return accessor;
|
|
4992
|
-
}
|
|
4993
|
-
function runtimeCatch(innerSchema, catchValue, value) {
|
|
4994
|
-
const result = innerSchema._zod.run({ value, issues: [] }, {});
|
|
4995
|
-
if (result && typeof result.then === "function")
|
|
4996
|
-
return INVALID;
|
|
4997
|
-
const r = result;
|
|
4998
|
-
if (r.issues.length === 0)
|
|
4999
|
-
return r.value;
|
|
5000
|
-
return catchValue();
|
|
5001
|
-
}
|
|
5002
|
-
function generateCatchCheck(doc, ctx, schema, accessor) {
|
|
5003
|
-
const def = schema._zod.def;
|
|
5004
|
-
if (!def.catchValue[CONSTANT_CATCH]) {
|
|
5005
|
-
throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
|
|
5006
|
-
}
|
|
5007
|
-
const outputVar = newVar(ctx);
|
|
5008
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
5009
|
-
doc.indented((d) => {
|
|
5010
|
-
const innerOut = compileChild(d, ctx, def.innerType, accessor);
|
|
5011
|
-
d.write(`return ${innerOut};`);
|
|
5012
|
-
});
|
|
5013
|
-
doc.write(`})();`);
|
|
5014
|
-
const innerConst = addConstant(ctx, def.innerType);
|
|
5015
|
-
const catchConst = addUserConstant(ctx, def.catchValue);
|
|
5016
|
-
const catchHelperConst = addConstant(ctx, runtimeCatch);
|
|
5017
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
5018
|
-
doc.indented((d) => {
|
|
5019
|
-
d.write(`${outputVar} = ${catchHelperConst}(${innerConst}, ${catchConst}, ${accessor});`);
|
|
5020
|
-
d.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5021
|
-
});
|
|
5022
|
-
doc.write(`}`);
|
|
5023
|
-
return outputVar;
|
|
5024
3403
|
}
|
|
5025
|
-
function
|
|
5026
|
-
|
|
5027
|
-
if (def.transform) {
|
|
5028
|
-
if (isAsyncFunction(def.transform)) {
|
|
5029
|
-
throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
|
|
5030
|
-
}
|
|
5031
|
-
const transformFn = def.transform;
|
|
5032
|
-
const helperFn = (value) => {
|
|
5033
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
5034
|
-
const result = transformFn(value, fakePayload);
|
|
5035
|
-
if (result instanceof Promise)
|
|
5036
|
-
return INVALID;
|
|
5037
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
5038
|
-
};
|
|
5039
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
5040
|
-
const outputVar = newVar(ctx);
|
|
5041
|
-
doc.write(`const ${outputVar} = ${helperConst}(${accessor});`);
|
|
5042
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5043
|
-
return outputVar;
|
|
5044
|
-
}
|
|
5045
|
-
return accessor;
|
|
3404
|
+
function registry() {
|
|
3405
|
+
return new $ZodRegistry;
|
|
5046
3406
|
}
|
|
3407
|
+
(_a2 = globalThis).__zod_globalRegistry ?? (_a2.__zod_globalRegistry = registry());
|
|
3408
|
+
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
5047
3409
|
// node_modules/zod/v4/core/api.js
|
|
5048
3410
|
function snapshotChecks(def) {
|
|
5049
3411
|
if (def.checks)
|
|
@@ -8486,7 +6848,6 @@ var advertisedOperationSchemas = {
|
|
|
8486
6848
|
worker: advertised(workerOperations),
|
|
8487
6849
|
state: advertised(stateOperations)
|
|
8488
6850
|
};
|
|
8489
|
-
var compiledOperationSchemas = Object.fromEntries(OPERATION_FAMILIES.map((family) => [family, compile(operationSchemas[family])]));
|
|
8490
6851
|
var success = (revisionValue, workspaceRevisionValue, data) => ({
|
|
8491
6852
|
ok: true,
|
|
8492
6853
|
schemaVersion: 1,
|
|
@@ -8508,8 +6869,7 @@ function resolveIdRef(kind, ref, ids) {
|
|
|
8508
6869
|
function parseOperation(family, input) {
|
|
8509
6870
|
if (typeof input === "object" && input !== null && "schemaVersion" in input && input.schemaVersion !== SCHEMA_VERSION)
|
|
8510
6871
|
return failure2("unsupported_version", "unsupported schema version", { operation: family });
|
|
8511
|
-
const
|
|
8512
|
-
const parsed = compiled.success ? operationSchemas[family].safeParse(input) : compiled;
|
|
6872
|
+
const parsed = operationSchemas[family].safeParse(input);
|
|
8513
6873
|
if (parsed.success)
|
|
8514
6874
|
return success(null, null, parsed.data);
|
|
8515
6875
|
const fields = parsed.error.issues.map((issue) => ({
|
|
@@ -17840,6 +16200,24 @@ function workitInvocations(command, dialect = "posix", depth = 0) {
|
|
|
17840
16200
|
}
|
|
17841
16201
|
return found;
|
|
17842
16202
|
}
|
|
16203
|
+
var WORKIT_GLOBAL = /^--(?:cwd|session|token|as)(=|$)/;
|
|
16204
|
+
function skipWorkitGlobals(args) {
|
|
16205
|
+
let cwd = null;
|
|
16206
|
+
let at = 0;
|
|
16207
|
+
for (;at < args.length; at++) {
|
|
16208
|
+
const match = WORKIT_GLOBAL.exec(args[at]);
|
|
16209
|
+
if (match?.[1] === "=") {
|
|
16210
|
+
if (args[at].startsWith("--cwd="))
|
|
16211
|
+
cwd = args[at].slice(6);
|
|
16212
|
+
} else if (match && args[at + 1] !== undefined) {
|
|
16213
|
+
if (args[at] === "--cwd")
|
|
16214
|
+
cwd = args[at + 1];
|
|
16215
|
+
at++;
|
|
16216
|
+
} else if (args[at] !== "--json")
|
|
16217
|
+
break;
|
|
16218
|
+
}
|
|
16219
|
+
return { rest: args.slice(at), cwd };
|
|
16220
|
+
}
|
|
17843
16221
|
var PUSH_VALUE_OPTIONS = new Set(["-o", "--push-option", "--receive-pack", "--exec", "--repo"]);
|
|
17844
16222
|
var CURRENT_BRANCH = /^\$\(\s*git\s+(?:branch\s+--show-current|rev-parse\s+--abbrev-ref\s+HEAD|symbolic-ref\s+--short\s+HEAD)\s*\)$/;
|
|
17845
16223
|
var pushDestination = (destination) => {
|
|
@@ -23012,7 +21390,7 @@ var splitDenial = (plan) => ({
|
|
|
23012
21390
|
unblock: plan.unblock
|
|
23013
21391
|
});
|
|
23014
21392
|
// packages/workit-core/src/hooks/write-gate.ts
|
|
23015
|
-
import { realpathSync as realpathSync6 } from "node:fs";
|
|
21393
|
+
import { existsSync as existsSync9, realpathSync as realpathSync6 } from "node:fs";
|
|
23016
21394
|
import os10 from "node:os";
|
|
23017
21395
|
import path31 from "node:path";
|
|
23018
21396
|
|
|
@@ -23241,7 +21619,7 @@ var exempt = (cwd, root, file, plans) => {
|
|
|
23241
21619
|
const segments = posix.split("/");
|
|
23242
21620
|
return /\.mdx?$/i.test(posix) || segments[0] === "docs" || segments.includes("plans") || [...plans].some((plan) => fold(plan) === posix);
|
|
23243
21621
|
};
|
|
23244
|
-
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs"]);
|
|
21622
|
+
var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs", "busybox"]);
|
|
23245
21623
|
var operands = (words) => words.filter((item) => !item.startsWith("-"));
|
|
23246
21624
|
var SCRIPT_SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
|
|
23247
21625
|
var wrappedScript = (input) => {
|
|
@@ -23253,9 +21631,9 @@ var wrappedScript = (input) => {
|
|
|
23253
21631
|
return null;
|
|
23254
21632
|
if ("script" in tail)
|
|
23255
21633
|
return tail.script;
|
|
23256
|
-
const
|
|
23257
|
-
if (SCRIPT_SHELLS.has((
|
|
23258
|
-
return script
|
|
21634
|
+
const script = shellScript2(tail.argv);
|
|
21635
|
+
if (SCRIPT_SHELLS.has((tail.argv[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase()) && script)
|
|
21636
|
+
return script;
|
|
23259
21637
|
return tail.argv.map(quoteWord2).join(" ");
|
|
23260
21638
|
};
|
|
23261
21639
|
var editsOf = (input) => {
|
|
@@ -23334,6 +21712,94 @@ var editsOf = (input) => {
|
|
|
23334
21712
|
return [];
|
|
23335
21713
|
}
|
|
23336
21714
|
};
|
|
21715
|
+
var shellScript2 = (words) => {
|
|
21716
|
+
if (!SCRIPT_SHELLS.has((words[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase()))
|
|
21717
|
+
return null;
|
|
21718
|
+
for (let index = 1;index < words.length; index++) {
|
|
21719
|
+
const word = words[index];
|
|
21720
|
+
if (/^-[a-z]*c$/.test(word))
|
|
21721
|
+
return words[index + 1] ?? null;
|
|
21722
|
+
if (/^[-+][oO]$/.test(word))
|
|
21723
|
+
index++;
|
|
21724
|
+
else if (!/^[-+]/.test(word))
|
|
21725
|
+
return null;
|
|
21726
|
+
}
|
|
21727
|
+
return null;
|
|
21728
|
+
};
|
|
21729
|
+
var XARGS_VALUE = /^-[IinLPdsEa]$/;
|
|
21730
|
+
var runsWriter = (words) => {
|
|
21731
|
+
const filled = [...words.filter((word) => word !== "{}" && word !== ";" && word !== "+"), "x"];
|
|
21732
|
+
const edits = editsOf(filled);
|
|
21733
|
+
return edits === null || edits.length > 0 || opaqueWrites(filled) !== null;
|
|
21734
|
+
};
|
|
21735
|
+
var opaqueWrites = (input) => {
|
|
21736
|
+
let words = input;
|
|
21737
|
+
while (words.length && (PREFIXES2.has(words[0]) || /^\w+=/.test(words[0]))) {
|
|
21738
|
+
if (words[0] === "xargs") {
|
|
21739
|
+
let at = 1;
|
|
21740
|
+
while (words[at]?.startsWith("-"))
|
|
21741
|
+
at += XARGS_VALUE.test(words[at]) ? 2 : 1;
|
|
21742
|
+
return runsWriter(words.slice(at)) ? ["."] : null;
|
|
21743
|
+
}
|
|
21744
|
+
words = words.slice(1);
|
|
21745
|
+
}
|
|
21746
|
+
const [verb, ...args] = words;
|
|
21747
|
+
const name = (verb?.split(/[\\/]/).at(-1) ?? "").toLowerCase();
|
|
21748
|
+
const files = operands(args);
|
|
21749
|
+
const has = (pattern) => args.some((arg) => pattern.test(arg));
|
|
21750
|
+
const listed = (named) => named.length ? named : ["."];
|
|
21751
|
+
if (name === "unlink" || name === "shred")
|
|
21752
|
+
return listed(files);
|
|
21753
|
+
if (/^(?:chmod|chown|chgrp|chattr)$/.test(name))
|
|
21754
|
+
return listed(has(/^--reference/) ? files : files.slice(1));
|
|
21755
|
+
if (name === "rsync") {
|
|
21756
|
+
const dest = files.at(-1);
|
|
21757
|
+
const remote = /^[\w.@-]*:/.test(dest ?? "") && !/^[A-Za-z]:[\\/]/.test(dest ?? "");
|
|
21758
|
+
return dest === undefined ? ["."] : remote ? [] : [dest];
|
|
21759
|
+
}
|
|
21760
|
+
if (name === "find") {
|
|
21761
|
+
const exec = args.findIndex((arg) => /^-(?:exec|execdir|ok|okdir)$/.test(arg));
|
|
21762
|
+
const writes = has(/^-(?:delete|fprint\w*|fls)$/) || exec >= 0 && runsWriter(args.slice(exec + 1));
|
|
21763
|
+
const end = args.findIndex((arg) => /^[-(!]/.test(arg));
|
|
21764
|
+
return writes ? listed(end < 0 ? args : args.slice(0, end)) : null;
|
|
21765
|
+
}
|
|
21766
|
+
if (/^g?awk$/.test(name)) {
|
|
21767
|
+
if (!has(/^-i|^--include/))
|
|
21768
|
+
return null;
|
|
21769
|
+
const named = [];
|
|
21770
|
+
let program = !has(/^-f$|^--file/);
|
|
21771
|
+
for (let index = 0;index < args.length; index++)
|
|
21772
|
+
if (/^(?:-[ifvF]|--(?:include|file|assign|field-separator))$/.test(args[index]))
|
|
21773
|
+
index++;
|
|
21774
|
+
else if (args[index].startsWith("-"))
|
|
21775
|
+
continue;
|
|
21776
|
+
else if (program)
|
|
21777
|
+
program = false;
|
|
21778
|
+
else
|
|
21779
|
+
named.push(args[index]);
|
|
21780
|
+
return listed(named);
|
|
21781
|
+
}
|
|
21782
|
+
if (name === "perl")
|
|
21783
|
+
return has(/^-[a-zA-Z]*[eE]/) ? ["."] : null;
|
|
21784
|
+
if (/^(?:python[\d.]*|node|bun|deno|ruby|php)$/.test(name))
|
|
21785
|
+
return has(/^-[a-zA-Z]*[ce]$|^--eval$|^-p$/) ? ["."] : null;
|
|
21786
|
+
return shellScript2(words) === null ? null : ["."];
|
|
21787
|
+
};
|
|
21788
|
+
var linkSources = (args, at) => {
|
|
21789
|
+
const names = operands([...args]);
|
|
21790
|
+
const into = args.indexOf("-t");
|
|
21791
|
+
const dest = into >= 0 ? args[into + 1] : names.length > 1 ? names.at(-1) : ".";
|
|
21792
|
+
const sources = names.filter((name, index) => into >= 0 ? name !== dest : index < names.length - 1 || names.length === 1);
|
|
21793
|
+
const target = dest === undefined ? null : at(dest);
|
|
21794
|
+
const symbolic = args.some((arg) => /^-[a-zA-Z]*s|^--symbolic$/.test(arg));
|
|
21795
|
+
const base = target === null ? null : into >= 0 || sources.length > 1 || existsSync9(target) || /[\\/]$/.test(dest ?? "") ? target : path31.dirname(target);
|
|
21796
|
+
return sources.flatMap((source) => {
|
|
21797
|
+
if (symbolic && base !== null && !path31.isAbsolute(homeExpanded(source)))
|
|
21798
|
+
return [path31.resolve(base, source)];
|
|
21799
|
+
const resolved = at(source);
|
|
21800
|
+
return resolved ? [resolved] : [];
|
|
21801
|
+
});
|
|
21802
|
+
};
|
|
23337
21803
|
var shellWrites = (command, depth = 0) => {
|
|
23338
21804
|
const targets = [];
|
|
23339
21805
|
let unknown = false;
|
|
@@ -23443,11 +21909,15 @@ var gitMutationDirs = (cwd, command, dialect) => {
|
|
|
23443
21909
|
var CD2 = new Set(["cd", "chdir", "set-location", "sl"]);
|
|
23444
21910
|
var PUSHD2 = new Set(["pushd", "push-location"]);
|
|
23445
21911
|
var POPD2 = new Set(["popd", "pop-location"]);
|
|
23446
|
-
var
|
|
21912
|
+
var UNKNOWN_DIR = path31.resolve(path31.parse(process.cwd()).root, ".workit-unknown-dir");
|
|
21913
|
+
var cdInto = (dir, words, strict = false) => {
|
|
21914
|
+
const lost = strict ? UNKNOWN_DIR : undefined;
|
|
23447
21915
|
const named = words.slice(1).find((word) => !word.startsWith("-") || word === "-");
|
|
23448
21916
|
const target = named === undefined ? undefined : homeExpanded(named);
|
|
23449
|
-
if (
|
|
23450
|
-
return;
|
|
21917
|
+
if (target === "-" || target && /^~|[$`*?]/.test(target))
|
|
21918
|
+
return lost;
|
|
21919
|
+
if (dir === undefined)
|
|
21920
|
+
return lost;
|
|
23451
21921
|
return target === undefined ? os10.homedir() : path31.resolve(dir, target);
|
|
23452
21922
|
};
|
|
23453
21923
|
var quoteWord2 = (word) => /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", "'\\''")}'`;
|
|
@@ -23455,7 +21925,7 @@ var homeExpanded = (file) => {
|
|
|
23455
21925
|
const match = /^(?:~|\$HOME|\$\{HOME\})(?=\/|$)/.exec(file);
|
|
23456
21926
|
return match ? (process.env.HOME || os10.homedir()) + file.slice(match[0].length) : file;
|
|
23457
21927
|
};
|
|
23458
|
-
var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
21928
|
+
var segmentTargets = (cwd, command, dialect, depth = 0, strict = false) => {
|
|
23459
21929
|
const targets = [];
|
|
23460
21930
|
let dir = cwd;
|
|
23461
21931
|
let stack = [];
|
|
@@ -23479,21 +21949,35 @@ var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
|
23479
21949
|
const words = lead < 0 ? [] : segment.words.slice(lead);
|
|
23480
21950
|
const name = (words[0]?.split(/[\\/]/).at(-1) ?? "").toLowerCase();
|
|
23481
21951
|
if (CD2.has(name)) {
|
|
23482
|
-
dir = cdInto(dir, words);
|
|
21952
|
+
dir = cdInto(dir, words, strict);
|
|
23483
21953
|
continue;
|
|
23484
21954
|
}
|
|
23485
21955
|
if (PUSHD2.has(name)) {
|
|
23486
21956
|
stack.push(dir);
|
|
23487
|
-
dir = cdInto(dir, words);
|
|
21957
|
+
dir = cdInto(dir, words, strict);
|
|
23488
21958
|
continue;
|
|
23489
21959
|
}
|
|
23490
21960
|
if (POPD2.has(name)) {
|
|
23491
|
-
dir = stack.length ? stack.pop() : undefined;
|
|
21961
|
+
dir = stack.length ? stack.pop() : strict ? UNKNOWN_DIR : undefined;
|
|
23492
21962
|
continue;
|
|
23493
21963
|
}
|
|
23494
|
-
const inner = depth < 4 ? wrappedScript(words) : null;
|
|
21964
|
+
const inner = depth < 4 ? wrappedScript(words) ?? (strict ? shellScript2(words) : null) : null;
|
|
23495
21965
|
if (inner !== null && dir !== undefined)
|
|
23496
|
-
targets.push(...segmentTargets(dir, inner, "posix", depth + 1));
|
|
21966
|
+
targets.push(...segmentTargets(dir, inner, "posix", depth + 1, strict));
|
|
21967
|
+
if (strict && dir !== undefined) {
|
|
21968
|
+
const runIn = dir;
|
|
21969
|
+
for (const sub of depth < 4 ? segment.subs ?? [] : [])
|
|
21970
|
+
targets.push(...segmentTargets(runIn, sub, dialect, depth + 1, true));
|
|
21971
|
+
const opaque = segment.unanalyzable ? ["."] : inner === null ? opaqueWrites(words) : null;
|
|
21972
|
+
for (const file of opaque ?? []) {
|
|
21973
|
+
const resolved = at(file);
|
|
21974
|
+
if (resolved)
|
|
21975
|
+
targets.push(file === "." ? path31.join(resolved, DIR_TARGET) : resolved);
|
|
21976
|
+
}
|
|
21977
|
+
const ln = words.findIndex((word) => /^ln$/i.test(word.split(/[\\/]/).at(-1) ?? ""));
|
|
21978
|
+
if (ln >= 0 && words.slice(0, ln).every((word) => PREFIXES2.has(word)))
|
|
21979
|
+
targets.push(...linkSources(words.slice(ln + 1), at));
|
|
21980
|
+
}
|
|
23497
21981
|
for (const file of segment.redirects)
|
|
23498
21982
|
if (!/^\/dev\/(?:null|stdout|stderr|tty)$/.test(file)) {
|
|
23499
21983
|
const resolved = at(file);
|
|
@@ -23510,11 +21994,29 @@ var segmentTargets = (cwd, command, dialect, depth = 0) => {
|
|
|
23510
21994
|
if (resolved)
|
|
23511
21995
|
targets.push(resolved);
|
|
23512
21996
|
}
|
|
23513
|
-
if (dir !== undefined
|
|
23514
|
-
|
|
21997
|
+
if (dir !== undefined)
|
|
21998
|
+
for (const invocation of workitInvocations(segment.words.map(quoteWord2).join(" "), "posix")) {
|
|
21999
|
+
const { at: runsIn, args } = workitCwd(dir, invocation);
|
|
22000
|
+
if (args[0] === "git" && MUTATING_WORKIT_GIT.has(args[1] ?? "") && !inert(args.slice(2), ""))
|
|
22001
|
+
targets.push(path31.join(runsIn, DIR_TARGET));
|
|
22002
|
+
}
|
|
23515
22003
|
}
|
|
23516
22004
|
return targets;
|
|
23517
22005
|
};
|
|
22006
|
+
var workitCwd = (dir, args) => {
|
|
22007
|
+
const { rest, cwd } = skipWorkitGlobals(args);
|
|
22008
|
+
return { at: cwd === null ? dir : path31.resolve(dir, homeExpanded(cwd)), args: rest };
|
|
22009
|
+
};
|
|
22010
|
+
var shellMutationTargets = (cwd, command, dialect = "posix", strict = false) => {
|
|
22011
|
+
try {
|
|
22012
|
+
return [
|
|
22013
|
+
...segmentTargets(cwd, command, dialect, 0, strict),
|
|
22014
|
+
...gitMutationDirs(cwd, command, dialect).map((dir) => path31.join(dir, DIR_TARGET))
|
|
22015
|
+
];
|
|
22016
|
+
} catch {
|
|
22017
|
+
return [];
|
|
22018
|
+
}
|
|
22019
|
+
};
|
|
23518
22020
|
var overridden = (command, dialect) => {
|
|
23519
22021
|
const segments = segmentsOf(command, dialect);
|
|
23520
22022
|
if (segments.length !== 1 || segments[0].group || segments[0].subs?.length)
|
|
@@ -23620,16 +22122,8 @@ function shellGate(cwd, command, owner, dialect = "posix", deps) {
|
|
|
23620
22122
|
} catch {}
|
|
23621
22123
|
const writes = shellWrites(command);
|
|
23622
22124
|
let targets = [];
|
|
23623
|
-
if (owner)
|
|
23624
|
-
|
|
23625
|
-
targets = [
|
|
23626
|
-
...segmentTargets(cwd, command, dialect),
|
|
23627
|
-
...gitMutationDirs(cwd, command, dialect).map((dir) => path31.join(dir, DIR_TARGET))
|
|
23628
|
-
];
|
|
23629
|
-
} catch {
|
|
23630
|
-
targets = [];
|
|
23631
|
-
}
|
|
23632
|
-
}
|
|
22125
|
+
if (owner)
|
|
22126
|
+
targets = shellMutationTargets(cwd, command, dialect);
|
|
23633
22127
|
if (owner && targets.length) {
|
|
23634
22128
|
const live = liveGate(owner, targets, deps);
|
|
23635
22129
|
if (live.kind !== "none") {
|
|
@@ -24268,9 +22762,266 @@ var cwdRewrite = (command) => {
|
|
|
24268
22762
|
return `workit --cwd ${dir} ${rest.slice("workit".length).trimStart()}`.trimEnd();
|
|
24269
22763
|
};
|
|
24270
22764
|
|
|
22765
|
+
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
22766
|
+
import { createHash as createHash17 } from "node:crypto";
|
|
22767
|
+
import fs25 from "node:fs";
|
|
22768
|
+
import path33 from "node:path";
|
|
22769
|
+
var FANOUT_TOKEN_FILE = "workit-fanout-token";
|
|
22770
|
+
var NONE5 = { kind: "none" };
|
|
22771
|
+
var CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
|
|
22772
|
+
var canonical3 = (file) => {
|
|
22773
|
+
let rest = "";
|
|
22774
|
+
for (let dir = path33.resolve(file);; dir = path33.dirname(dir)) {
|
|
22775
|
+
try {
|
|
22776
|
+
return path33.join(fs25.realpathSync.native(dir), rest);
|
|
22777
|
+
} catch {
|
|
22778
|
+
if (path33.dirname(dir) === dir)
|
|
22779
|
+
return path33.resolve(file);
|
|
22780
|
+
rest = rest ? path33.join(path33.basename(dir), rest) : path33.basename(dir);
|
|
22781
|
+
}
|
|
22782
|
+
}
|
|
22783
|
+
};
|
|
22784
|
+
var within = (child, parent) => {
|
|
22785
|
+
const fold = (value) => CASE_INSENSITIVE2 ? value.toLowerCase() : value;
|
|
22786
|
+
const relative = path33.relative(fold(parent), fold(child));
|
|
22787
|
+
return relative === "" || !relative.startsWith("..") && !path33.isAbsolute(relative);
|
|
22788
|
+
};
|
|
22789
|
+
var checkoutOf2 = (file) => {
|
|
22790
|
+
for (let dir = canonical3(file);; dir = path33.dirname(dir)) {
|
|
22791
|
+
const dotGit = path33.join(dir, ".git");
|
|
22792
|
+
if (fs25.existsSync(dotGit)) {
|
|
22793
|
+
if (!fs25.statSync(dotGit).isFile())
|
|
22794
|
+
return { root: dir, workit: false };
|
|
22795
|
+
const admin = /^gitdir:\s*(.+)$/mu.exec(fs25.readFileSync(dotGit, "utf8"))?.[1]?.trim();
|
|
22796
|
+
return {
|
|
22797
|
+
root: dir,
|
|
22798
|
+
workit: Boolean(admin) && fs25.existsSync(path33.join(path33.resolve(dir, admin), FANOUT_TOKEN_FILE))
|
|
22799
|
+
};
|
|
22800
|
+
}
|
|
22801
|
+
if (path33.dirname(dir) === dir)
|
|
22802
|
+
return null;
|
|
22803
|
+
}
|
|
22804
|
+
};
|
|
22805
|
+
var workitWorktreeOf = (file) => {
|
|
22806
|
+
try {
|
|
22807
|
+
const checkout = checkoutOf2(file);
|
|
22808
|
+
return checkout?.workit ? checkout.root : null;
|
|
22809
|
+
} catch {
|
|
22810
|
+
return null;
|
|
22811
|
+
}
|
|
22812
|
+
};
|
|
22813
|
+
var bindingDir = () => path33.join(resolveStateDir(), "worktree-bind");
|
|
22814
|
+
var bindingFile = (session, agentId) => path33.join(bindingDir(), `${createHash17("sha256").update(`${session}\x00${agentId}`).digest("hex").slice(0, 32)}.json`);
|
|
22815
|
+
var readBinding = (file) => {
|
|
22816
|
+
try {
|
|
22817
|
+
const value = JSON.parse(fs25.readFileSync(file, "utf8"));
|
|
22818
|
+
return typeof value.worktree === "string" ? value : null;
|
|
22819
|
+
} catch {
|
|
22820
|
+
return null;
|
|
22821
|
+
}
|
|
22822
|
+
};
|
|
22823
|
+
var boundWorktree = (input) => {
|
|
22824
|
+
const agentId = input.session.agentId;
|
|
22825
|
+
if (!agentId)
|
|
22826
|
+
return null;
|
|
22827
|
+
const binding = readBinding(bindingFile(input.session.id, agentId));
|
|
22828
|
+
return binding && workitWorktreeOf(binding.worktree) === binding.worktree ? binding.worktree : null;
|
|
22829
|
+
};
|
|
22830
|
+
var bind = (input, worktree) => {
|
|
22831
|
+
const agentId = input.session.agentId;
|
|
22832
|
+
const file = bindingFile(input.session.id, agentId);
|
|
22833
|
+
const binding = {
|
|
22834
|
+
worktree,
|
|
22835
|
+
session: input.session.id,
|
|
22836
|
+
agentId,
|
|
22837
|
+
at: new Date().toISOString()
|
|
22838
|
+
};
|
|
22839
|
+
fs25.mkdirSync(path33.dirname(file), { recursive: true });
|
|
22840
|
+
fs25.writeFileSync(file, JSON.stringify(binding));
|
|
22841
|
+
try {
|
|
22842
|
+
if (inWorkitWorkspace(input.cwd))
|
|
22843
|
+
appendHookObserved(input.cwd, {
|
|
22844
|
+
type: "worktree.bound",
|
|
22845
|
+
actor: { host: input.host, session: input.session.id, agentId },
|
|
22846
|
+
agentType: input.session.agentType,
|
|
22847
|
+
worktree
|
|
22848
|
+
});
|
|
22849
|
+
} catch {}
|
|
22850
|
+
};
|
|
22851
|
+
var hostIsolated = (cwd, agentId) => {
|
|
22852
|
+
try {
|
|
22853
|
+
if (agentId === null)
|
|
22854
|
+
return false;
|
|
22855
|
+
const checkout = checkoutOf2(cwd);
|
|
22856
|
+
if (!checkout)
|
|
22857
|
+
return false;
|
|
22858
|
+
const dotGit = path33.join(checkout.root, ".git");
|
|
22859
|
+
if (!fs25.statSync(dotGit).isFile())
|
|
22860
|
+
return false;
|
|
22861
|
+
const admin = /^gitdir:\s*(.+)$/mu.exec(fs25.readFileSync(dotGit, "utf8"))?.[1]?.trim();
|
|
22862
|
+
if (!admin)
|
|
22863
|
+
return false;
|
|
22864
|
+
const adminDir = path33.resolve(checkout.root, admin);
|
|
22865
|
+
const back = fs25.readFileSync(path33.join(adminDir, "gitdir"), "utf8").trim();
|
|
22866
|
+
if (path33.relative(canonical3(path33.resolve(adminDir, back)), canonical3(dotGit)) !== "")
|
|
22867
|
+
return false;
|
|
22868
|
+
const common = path33.join(adminDir, "commondir");
|
|
22869
|
+
const gitDir = fs25.existsSync(common) ? path33.resolve(adminDir, fs25.readFileSync(common, "utf8").trim()) : adminDir;
|
|
22870
|
+
const expected = path33.join(path33.dirname(canonical3(gitDir)), ".claude", "worktrees", `agent-${agentId}`);
|
|
22871
|
+
return path33.relative(canonical3(expected), checkout.root) === "";
|
|
22872
|
+
} catch {
|
|
22873
|
+
return false;
|
|
22874
|
+
}
|
|
22875
|
+
};
|
|
22876
|
+
var shown = (target) => path33.basename(target) === ".workit-live-target" ? path33.dirname(target) : target;
|
|
22877
|
+
var unboundDeny = (input, target) => {
|
|
22878
|
+
const unblock = "Work only in the worktree your brief names, which the lead made with `workit fanout worktree create <slice>`: edit files under it and run one verb per call against it (`workit --cwd <worktree> git commit -m … -- <paths>`, `workit --cwd <worktree> check <name>`). If your brief names no worktree, stop and report that to the lead.";
|
|
22879
|
+
return {
|
|
22880
|
+
kind: "deny",
|
|
22881
|
+
reason: `workit worktree confinement: implementer ${input.session.agentId} is not working in a Workit worktree yet, and ${shown(target)} is outside every worktree \`workit fanout worktree create\` made, so it would change the lead's checkout. ${unblock}`,
|
|
22882
|
+
unblock
|
|
22883
|
+
};
|
|
22884
|
+
};
|
|
22885
|
+
var outsideDeny = (input, worktree, target) => {
|
|
22886
|
+
const unblock = `Edit only under ${worktree} and run git there, one verb per call: \`workit --cwd ${worktree} git commit -m … -- <paths>\`, \`workit --cwd ${worktree} check <name>\` (or \`git -C ${worktree} …\`); a command whose writes Workit cannot see is judged by the directory it runs in. Anything outside it is a follow-up for your report.`;
|
|
22887
|
+
return {
|
|
22888
|
+
kind: "deny",
|
|
22889
|
+
reason: `workit worktree confinement: implementer ${input.session.agentId} is bound to the worktree ${worktree}, and ${shown(target)} is outside it. ${unblock}`,
|
|
22890
|
+
unblock
|
|
22891
|
+
};
|
|
22892
|
+
};
|
|
22893
|
+
var unknownDirDeny = (input, worktree) => {
|
|
22894
|
+
const wt = worktree ?? "<worktree>";
|
|
22895
|
+
const unblock = `Workit cannot resolve \`cd -\`, \`cd "$X"\` or \`popd\`: write by absolute path, \`git -C ${wt}\` or \`workit --cwd ${wt}\`.`;
|
|
22896
|
+
return {
|
|
22897
|
+
kind: "deny",
|
|
22898
|
+
reason: `workit worktree confinement: implementer ${input.session.agentId} writes after an unknown directory change. ${unblock}`,
|
|
22899
|
+
unblock
|
|
22900
|
+
};
|
|
22901
|
+
};
|
|
22902
|
+
function confineImplementer(input, targets) {
|
|
22903
|
+
try {
|
|
22904
|
+
if (!input.session.agentId || hostIsolated(input.cwd, input.session.agentId))
|
|
22905
|
+
return NONE5;
|
|
22906
|
+
if (targets.some((target) => within(path33.resolve(target), UNKNOWN_DIR)))
|
|
22907
|
+
return unknownDirDeny(input, boundWorktree(input));
|
|
22908
|
+
const bindings = canonical3(bindingDir());
|
|
22909
|
+
const judged = targets.map((named) => ({ named, real: canonical3(named) })).filter(({ real }) => within(real, bindings) || checkoutOf2(real) !== null);
|
|
22910
|
+
if (judged.length === 0)
|
|
22911
|
+
return NONE5;
|
|
22912
|
+
let worktree = boundWorktree(input);
|
|
22913
|
+
if (!worktree) {
|
|
22914
|
+
for (const { real } of judged) {
|
|
22915
|
+
worktree = within(real, bindings) ? null : workitWorktreeOf(real);
|
|
22916
|
+
if (worktree)
|
|
22917
|
+
break;
|
|
22918
|
+
}
|
|
22919
|
+
if (!worktree)
|
|
22920
|
+
return unboundDeny(input, judged[0].named);
|
|
22921
|
+
bind(input, worktree);
|
|
22922
|
+
}
|
|
22923
|
+
const root = canonical3(worktree);
|
|
22924
|
+
const outside = judged.find(({ real }) => !within(real, root) || within(real, bindings));
|
|
22925
|
+
return outside ? outsideDeny(input, worktree, outside.named) : NONE5;
|
|
22926
|
+
} catch {
|
|
22927
|
+
return NONE5;
|
|
22928
|
+
}
|
|
22929
|
+
}
|
|
22930
|
+
var MUTATING_REMOTE = new Set([
|
|
22931
|
+
"add",
|
|
22932
|
+
"remove",
|
|
22933
|
+
"rm",
|
|
22934
|
+
"rename",
|
|
22935
|
+
"set-url",
|
|
22936
|
+
"set-head",
|
|
22937
|
+
"set-branches",
|
|
22938
|
+
"prune"
|
|
22939
|
+
]);
|
|
22940
|
+
var CONFIG_READ = /^(?:--get(?:-all|-regexp|-urlmatch)?|--list|-l|get|list)$/;
|
|
22941
|
+
var CONFIG_WRITE = /^(?:--(?:unset(?:-all)?|add|replace-all|rename-section|remove-section|edit)|-e|set|unset|rename-section|remove-section|edit)$/;
|
|
22942
|
+
var CONFIG_VALUE = /^(?:--file|-f|--blob|--type|--default|--comment)$/;
|
|
22943
|
+
var leadOwnedGit = (args) => {
|
|
22944
|
+
const [sub = "", ...rest] = args;
|
|
22945
|
+
if (rest.some((arg) => arg === "--help" || arg === "-h"))
|
|
22946
|
+
return false;
|
|
22947
|
+
const names = rest.filter((arg) => !arg.startsWith("-"));
|
|
22948
|
+
const flag = (pattern) => rest.some((arg) => pattern.test(arg));
|
|
22949
|
+
switch (sub) {
|
|
22950
|
+
case "worktree":
|
|
22951
|
+
return names[0] !== undefined && names[0] !== "list";
|
|
22952
|
+
case "update-ref":
|
|
22953
|
+
return true;
|
|
22954
|
+
case "symbolic-ref":
|
|
22955
|
+
return names.length > 1 || flag(/^(?:-d|--delete)$/);
|
|
22956
|
+
case "branch":
|
|
22957
|
+
return flag(/^-[a-zA-Z]*[dDfmMcC]|^--(?:delete|force|move|copy)$/);
|
|
22958
|
+
case "tag":
|
|
22959
|
+
return flag(/^-[a-zA-Z]*[df]|^--(?:delete|force)$/);
|
|
22960
|
+
case "push": {
|
|
22961
|
+
if (flag(/^(?:-d|--delete|--mirror|--prune)$/))
|
|
22962
|
+
return true;
|
|
22963
|
+
const refspecs = names.slice(1);
|
|
22964
|
+
return refspecs.some((name) => name.includes(":") || name.startsWith("+")) || refspecs.length > 0 && flag(/^-[a-zA-Z]*f|^--force/);
|
|
22965
|
+
}
|
|
22966
|
+
case "replace":
|
|
22967
|
+
case "filter-branch":
|
|
22968
|
+
case "gc":
|
|
22969
|
+
case "prune":
|
|
22970
|
+
case "repack":
|
|
22971
|
+
case "sparse-checkout":
|
|
22972
|
+
return true;
|
|
22973
|
+
case "notes":
|
|
22974
|
+
case "reflog":
|
|
22975
|
+
case "submodule":
|
|
22976
|
+
return !/^(?:list|show|status|exists)$/.test(names[0] ?? "list");
|
|
22977
|
+
case "stash":
|
|
22978
|
+
return names[0] === "drop" || names[0] === "clear";
|
|
22979
|
+
case "remote":
|
|
22980
|
+
return MUTATING_REMOTE.has(names[0] ?? "");
|
|
22981
|
+
case "config": {
|
|
22982
|
+
if (rest.some((arg) => CONFIG_READ.test(arg)))
|
|
22983
|
+
return false;
|
|
22984
|
+
if (rest.some((arg) => CONFIG_WRITE.test(arg)))
|
|
22985
|
+
return true;
|
|
22986
|
+
let operands = 0;
|
|
22987
|
+
for (let index = 0;index < rest.length; index++)
|
|
22988
|
+
if (CONFIG_VALUE.test(rest[index]))
|
|
22989
|
+
index++;
|
|
22990
|
+
else if (!rest[index].startsWith("-"))
|
|
22991
|
+
operands++;
|
|
22992
|
+
return operands > 1;
|
|
22993
|
+
}
|
|
22994
|
+
default:
|
|
22995
|
+
return false;
|
|
22996
|
+
}
|
|
22997
|
+
};
|
|
22998
|
+
var leadOwnedWorkit = (args) => !args.includes("--help") && args.some((_, start) => {
|
|
22999
|
+
if (start > 0 && !/(?:^|[\\/])workit(?:\.exe|\.cmd)?$/.test(args[start - 1]))
|
|
23000
|
+
return false;
|
|
23001
|
+
const [verb, sub] = skipWorkitGlobals(args.slice(start)).rest;
|
|
23002
|
+
return verb === "fanout" && /^(?:plan|worktree)$/.test(sub ?? "");
|
|
23003
|
+
});
|
|
23004
|
+
var leadOwnedDeny = (input, command) => {
|
|
23005
|
+
const unblock = "Worktrees, branches other than your own, refs, tags, remotes, git config, the shared stash and the fan-out plan belong to the lead: report what you need changed as a follow-up instead.";
|
|
23006
|
+
return {
|
|
23007
|
+
kind: "deny",
|
|
23008
|
+
reason: `workit worktree confinement: implementer ${input.session.agentId} may not run \`${command.slice(0, 160)}\`. ${unblock}`,
|
|
23009
|
+
unblock
|
|
23010
|
+
};
|
|
23011
|
+
};
|
|
23012
|
+
function confineImplementerShell(input, command, dialect = "posix") {
|
|
23013
|
+
try {
|
|
23014
|
+
if (!input.session.agentId || hostIsolated(input.cwd, input.session.agentId))
|
|
23015
|
+
return NONE5;
|
|
23016
|
+
if (rawInvocations(command, dialect).some((invocation) => invocation.tool === "git" && leadOwnedGit(invocation.args)) || workitInvocations(command, dialect).some(leadOwnedWorkit))
|
|
23017
|
+
return leadOwnedDeny(input, command);
|
|
23018
|
+
} catch {}
|
|
23019
|
+
return confineImplementer(input, shellMutationTargets(input.cwd, command, dialect, true));
|
|
23020
|
+
}
|
|
23021
|
+
|
|
24271
23022
|
// packages/workit-core/src/hooks/handle.ts
|
|
24272
23023
|
var usable2 = (value) => value === "native" || value === "partial";
|
|
24273
|
-
var
|
|
23024
|
+
var NONE6 = { kind: "none" };
|
|
24274
23025
|
var resendsOnChange = (descriptor) => descriptor.context.turnResend === "on-change" && usable2(descriptor.context.perTurn);
|
|
24275
23026
|
var offered = new Set;
|
|
24276
23027
|
var ROLES = {
|
|
@@ -24283,6 +23034,7 @@ var workitRole = (descriptor, agentType) => {
|
|
|
24283
23034
|
const name = agentType.startsWith(prefix) ? agentType.slice(prefix.length) : "";
|
|
24284
23035
|
return Object.hasOwn(ROLES, name) ? ROLES[name] : null;
|
|
24285
23036
|
};
|
|
23037
|
+
var implementerAgent = (input, descriptor) => Boolean(input.session.agentId) && input.session.agentType !== null && workitRole(descriptor, input.session.agentType) === "implementer";
|
|
24286
23038
|
var subagentSession = (session, agentId) => [session, agentId].filter((part) => Boolean(part)).join(":").replace(/[^A-Za-z0-9_.:@/+-]/g, "-").slice(0, 128);
|
|
24287
23039
|
var CLAUDE_WORKTREE = /[\\/]\.claude[\\/]worktrees[\\/]/;
|
|
24288
23040
|
var isolatedGit = (cwd) => `Run git as plain, separate commands inside your worktree (\`git switch -c <branch> <base>\`, ${workspaceTrailers(cwd).enabled ? '`git commit --trailer "Workit-Session=<your session>"`, `git push`; `workit doctor --fix` can install a prepare-commit-msg hook that adds the trailer' : "`git commit` (this workspace sets trailers: false: add no Workit-Session trailer), `git push`"}; add Co-Authored-By with \`git commit --trailer\` in the same commit, never by amending), never \`workit git …\`: Claude Code's worktree isolation refuses a command that wraps git and cannot be turned off (https://code.claude.com/docs/en/worktrees#how-claude-code-enforces-isolation). Never dodge that check by renaming or re-wrapping git. Use workit for the non-git verbs (\`workit check\`, \`workit ledger\`, \`workit pr\`).`;
|
|
@@ -24290,7 +23042,7 @@ var subagentStartText = (input, descriptor, event) => {
|
|
|
24290
23042
|
const session = input.session.id ?? null;
|
|
24291
23043
|
const role = workitRole(descriptor, event.agentType);
|
|
24292
23044
|
const who = `${descriptor.label} subagent ${event.agentId} (${event.agentType})`;
|
|
24293
|
-
const isolated = descriptor.subagents.worktreeIsolation === "native" && (role === "implementer"
|
|
23045
|
+
const isolated = descriptor.subagents.worktreeIsolation === "native" && (role === "implementer" ? hostIsolated(input.cwd, event.agentId) : CLAUDE_WORKTREE.test(input.cwd));
|
|
24294
23046
|
const guide = role === "implementer" ? branchGuidance(input.cwd) : null;
|
|
24295
23047
|
if (role === "implementer" && isolated)
|
|
24296
23048
|
return `Workit observed ${who} working in its own git worktree: it may edit and commit there, within its brief's scope. ${isolatedGit(input.cwd)} Plain git is expected here, and the branch name is still policy-checked: a raw \`git switch -c\`, \`git checkout -b\`, \`git branch\` or \`git worktree add -b\` onto a protected or non-compliant name is denied, so use a policy-compliant branch.${guide ? ` ${guide}` : " (e.g. feature/<slug>)"} Never record a verdict on your own work. Never push, open a PR, or merge unless the brief asks for it.`;
|
|
@@ -24298,7 +23050,7 @@ var subagentStartText = (input, descriptor, event) => {
|
|
|
24298
23050
|
return `${isolatedGit(input.cwd)}
|
|
24299
23051
|
${subagentStartText({ ...input, cwd: "" }, descriptor, event)}`;
|
|
24300
23052
|
if (role === "implementer")
|
|
24301
|
-
return `Workit observed ${who} as an implementer: it edits and commits only in its own git worktree (the lead makes
|
|
23053
|
+
return `Workit observed ${who} as an implementer: it edits and commits only in its own git worktree, the one its brief names (the lead makes it with \`workit fanout worktree create <slice>\` before spawning), never in the lead's checkout, within its brief's scope. Workit binds it to the first Workit worktree it changes: from then on an edit or a mutating git command outside that worktree is denied, and before then so is one outside every Workit worktree. Run Workit against the worktree one verb per call, never \`cd … &&\` chains or \`$VAR\` paths: \`workit --cwd <worktree> git commit -m "<message>" -- <paths>\`, \`workit --cwd <worktree> check <name>\`. The worktree is on the brief's branch once the lead made it; otherwise switch with \`workit --cwd <worktree> git branch <branch> --base <base>\` (or \`--kind <kind> --slug <slug>\`, which picks the base); branch policy hooks still deny protected or non-compliant branches, raw git included.${guide ? ` ${guide}` : " (e.g. feature/<slug>)"} Never record a verdict on your own work. Never push, open a PR, or merge unless the brief asks for it.`;
|
|
24302
23054
|
const readOnly = `Workit observed ${who} as read-only/agent-guided.`;
|
|
24303
23055
|
if (role !== "judge" || !event.agentId)
|
|
24304
23056
|
return readOnly;
|
|
@@ -24329,7 +23081,7 @@ function decide(input, deps) {
|
|
|
24329
23081
|
switch (event.kind) {
|
|
24330
23082
|
case "session.start": {
|
|
24331
23083
|
if (!usable2(descriptor.context.sessionStart))
|
|
24332
|
-
return
|
|
23084
|
+
return NONE6;
|
|
24333
23085
|
const key = input.session.id ? `${input.host}\x00${input.session.id}` : null;
|
|
24334
23086
|
const offer = event.source === "startup" && (key === null || !offered.has(key));
|
|
24335
23087
|
if (offer && key !== null)
|
|
@@ -24344,26 +23096,31 @@ function decide(input, deps) {
|
|
|
24344
23096
|
}
|
|
24345
23097
|
case "context.turn": {
|
|
24346
23098
|
if (!usable2(descriptor.context.perTurn))
|
|
24347
|
-
return
|
|
23099
|
+
return NONE6;
|
|
24348
23100
|
const current = turnContextText(input, descriptor);
|
|
24349
23101
|
const text = resendsOnChange(descriptor) ? changedTurnContext(input, current) : current;
|
|
24350
|
-
return withContextLine(text ? { kind: "context", text } :
|
|
23102
|
+
return withContextLine(text ? { kind: "context", text } : NONE6, promptNudge(input, event.prompt));
|
|
24351
23103
|
}
|
|
24352
23104
|
case "shell.pre": {
|
|
24353
23105
|
const skillRead = skillReadIn(event.command, event.dialect);
|
|
24354
23106
|
if (skillRead)
|
|
24355
23107
|
recordSkillLoad(input, skillRead, "read");
|
|
24356
23108
|
const canDeny = usable2(descriptor.shellPolicy.deny);
|
|
24357
|
-
const policy = canDeny ? shellPolicy(input.cwd, event.command, event.dialect) :
|
|
23109
|
+
const policy = canDeny ? shellPolicy(input.cwd, event.command, event.dialect) : NONE6;
|
|
24358
23110
|
if (policy.kind !== "none")
|
|
24359
23111
|
return policy;
|
|
24360
23112
|
const postTool = usable2(descriptor.events["shell.post"].support);
|
|
24361
23113
|
if (!postTool)
|
|
24362
23114
|
settlePendingCommit(input);
|
|
24363
|
-
const raw = canDeny ? rawGitPre(input, event.command, { nudge: false }) :
|
|
23115
|
+
const raw = canDeny ? rawGitPre(input, event.command, { nudge: false }) : NONE6;
|
|
24364
23116
|
if (raw.kind === "deny")
|
|
24365
23117
|
return raw;
|
|
24366
|
-
|
|
23118
|
+
if (canDeny && implementerAgent(input, descriptor)) {
|
|
23119
|
+
const confined = confineImplementerShell(input, event.command, event.dialect);
|
|
23120
|
+
if (confined.kind === "deny")
|
|
23121
|
+
return confined;
|
|
23122
|
+
}
|
|
23123
|
+
const gate = usable2(descriptor.events["write.pre"].support) ? shellGate(input.cwd, event.command, liveOwner(input, descriptor.transport), event.dialect) : NONE6;
|
|
24367
23124
|
if (gate.kind === "deny")
|
|
24368
23125
|
return gate;
|
|
24369
23126
|
const plan = heavyGate(input.cwd, event.command, {
|
|
@@ -24386,10 +23143,10 @@ function decide(input, deps) {
|
|
|
24386
23143
|
command: rewrittenTo,
|
|
24387
23144
|
input: event.toolInput ? { ...event.toolInput, command: rewrittenTo } : null,
|
|
24388
23145
|
text
|
|
24389
|
-
} :
|
|
23146
|
+
} : NONE6;
|
|
24390
23147
|
if (descriptor.transport !== "hook-process")
|
|
24391
23148
|
return rewritten(null);
|
|
24392
|
-
const nudged = withContextLine(withContextLine(canDeny ? rawGitPre(input, event.command) :
|
|
23149
|
+
const nudged = withContextLine(withContextLine(canDeny ? rawGitPre(input, event.command) : NONE6, aside), shipNudge(input, event.command));
|
|
24393
23150
|
if (!lease)
|
|
24394
23151
|
return cwdCommand && nudged.kind !== "deny" ? rewritten(nudged.kind === "context" ? nudged.text : null) : nudged;
|
|
24395
23152
|
return rewritten([rewriteNote(lease), nudged.kind === "context" ? nudged.text : null].filter(Boolean).join(`
|
|
@@ -24398,9 +23155,17 @@ function decide(input, deps) {
|
|
|
24398
23155
|
case "shell.post":
|
|
24399
23156
|
if (usable2(descriptor.events["shell.post"].support))
|
|
24400
23157
|
rawGitPost(input, event.command, callKey(event.toolUseId, event.command));
|
|
24401
|
-
return
|
|
24402
|
-
case "write.pre":
|
|
24403
|
-
|
|
23158
|
+
return NONE6;
|
|
23159
|
+
case "write.pre": {
|
|
23160
|
+
if (!usable2(descriptor.events["write.pre"].support))
|
|
23161
|
+
return NONE6;
|
|
23162
|
+
if (implementerAgent(input, descriptor)) {
|
|
23163
|
+
const confined = confineImplementer(input, liveTargets(input.cwd, event.paths));
|
|
23164
|
+
if (confined.kind === "deny")
|
|
23165
|
+
return confined;
|
|
23166
|
+
}
|
|
23167
|
+
return writeGate(input.cwd, event.paths, liveOwner(input, descriptor.transport));
|
|
23168
|
+
}
|
|
24404
23169
|
case "subagent.start":
|
|
24405
23170
|
if (workitRole(descriptor, event.agentType) === "judge")
|
|
24406
23171
|
trackJudge(input, event.agentId, true);
|
|
@@ -24417,14 +23182,14 @@ function decide(input, deps) {
|
|
|
24417
23182
|
case "tool.pre":
|
|
24418
23183
|
if (event.skill)
|
|
24419
23184
|
recordSkillLoad(input, event.skill, "tool");
|
|
24420
|
-
return
|
|
23185
|
+
return NONE6;
|
|
24421
23186
|
case "stop":
|
|
24422
|
-
return usable2(descriptor.events.stop.support) ? stopDecision(input, descriptor) :
|
|
23187
|
+
return usable2(descriptor.events.stop.support) ? stopDecision(input, descriptor) : NONE6;
|
|
24423
23188
|
case "subagent.stop":
|
|
24424
23189
|
trackJudge(input, event.agentId, false);
|
|
24425
|
-
return
|
|
23190
|
+
return NONE6;
|
|
24426
23191
|
case "prompt.submit":
|
|
24427
|
-
return
|
|
23192
|
+
return NONE6;
|
|
24428
23193
|
}
|
|
24429
23194
|
}
|
|
24430
23195
|
// packages/workit-core/src/hooks/deadline.ts
|
|
@@ -24733,12 +23498,12 @@ var skillPromptNudge = (prompt, ctx) => process.env.WORKIT_PI_WORKER_SESSION ? n
|
|
|
24733
23498
|
// packages/workit-pi/src/launches.ts
|
|
24734
23499
|
import { mkdirSync as mkdirSync3, readFileSync as readFileSync6, readdirSync as readdirSync5, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
|
|
24735
23500
|
import os11 from "node:os";
|
|
24736
|
-
import
|
|
23501
|
+
import path34 from "node:path";
|
|
24737
23502
|
var launchDir = (root) => {
|
|
24738
23503
|
const location = resolveStore(root);
|
|
24739
|
-
return location instanceof Error ? null :
|
|
23504
|
+
return location instanceof Error ? null : path34.join(location.dir, "pi-launches");
|
|
24740
23505
|
};
|
|
24741
|
-
var fileFor = (dir, workerId) =>
|
|
23506
|
+
var fileFor = (dir, workerId) => path34.join(dir, `${workerId.replace(/[^A-Za-z0-9-]/g, "_")}.json`);
|
|
24742
23507
|
var recordLaunch = (root, attempt) => {
|
|
24743
23508
|
const dir = launchDir(root);
|
|
24744
23509
|
if (!dir)
|
|
@@ -24784,7 +23549,7 @@ var abandonedLaunches = (root) => {
|
|
|
24784
23549
|
const found = [];
|
|
24785
23550
|
for (const name of names) {
|
|
24786
23551
|
try {
|
|
24787
|
-
const attempt = JSON.parse(readFileSync6(
|
|
23552
|
+
const attempt = JSON.parse(readFileSync6(path34.join(dir, name), "utf8"));
|
|
24788
23553
|
if (typeof attempt.workerId === "string" && typeof attempt.session === "string") {
|
|
24789
23554
|
if (!alive(attempt))
|
|
24790
23555
|
found.push(attempt);
|