@brainervirus/workit-cli 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/index.js +279 -1707
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1336,7 +1336,7 @@ var cuid, cuid2, ulid, xid, ksuid, nanoid, duration, guid, uuid = (version) => {
|
|
|
1336
1336
|
if (!version)
|
|
1337
1337
|
return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
|
|
1338
1338
|
return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
|
|
1339
|
-
}, email, _emoji = `^(?=[\\s\\S]*[\\p{Extended_Pictographic}\\p{Regional_Indicator}\\u20E3])[\\p{Extended_Pictographic}\\p{Emoji_Component}]+$`, ipv4, ipv6, cidrv4, cidrv6, base64, base64url, httpProtocol, e164,
|
|
1339
|
+
}, email, _emoji = `^(?=[\\s\\S]*[\\p{Extended_Pictographic}\\p{Regional_Indicator}\\u20E3])[\\p{Extended_Pictographic}\\p{Emoji_Component}]+$`, ipv4, ipv6, cidrv4, cidrv6, base64, base64url, httpProtocol, e164, 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])))`, date, anyString, integer, number, boolean, lowercase, uppercase;
|
|
1340
1340
|
var init_regexes = __esm(() => {
|
|
1341
1341
|
cuid = /^[cC][0-9a-z]{6,}$/;
|
|
1342
1342
|
cuid2 = /^[0-9a-z]+$/;
|
|
@@ -1355,8 +1355,6 @@ var init_regexes = __esm(() => {
|
|
|
1355
1355
|
base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
1356
1356
|
httpProtocol = /^https?$/;
|
|
1357
1357
|
e164 = /^\+[1-9]\d{6,14}$/;
|
|
1358
|
-
creditCard = /^\d(?:[ -]?\d){11,18}$/;
|
|
1359
|
-
iban = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z0-9]{11,30}$/;
|
|
1360
1358
|
date = /* @__PURE__ */ anchor(dateSource);
|
|
1361
1359
|
anyString = /^[\s\S]{0,}$/;
|
|
1362
1360
|
integer = /^-?\d+$/;
|
|
@@ -1841,40 +1839,6 @@ function isValidBase64URL(data) {
|
|
|
1841
1839
|
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
1842
1840
|
return isValidBase64(padded);
|
|
1843
1841
|
}
|
|
1844
|
-
function isLuhnAlgo(digits) {
|
|
1845
|
-
let length = digits.length;
|
|
1846
|
-
let bit = 1;
|
|
1847
|
-
let sum = 0;
|
|
1848
|
-
while (length) {
|
|
1849
|
-
const value = digits.charCodeAt(--length) - 48;
|
|
1850
|
-
bit ^= 1;
|
|
1851
|
-
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
1852
|
-
}
|
|
1853
|
-
return sum % 10 === 0;
|
|
1854
|
-
}
|
|
1855
|
-
function isValidCreditCard(input) {
|
|
1856
|
-
if (!creditCard.test(input))
|
|
1857
|
-
return false;
|
|
1858
|
-
return isLuhnAlgo(input.replace(CC_SANITIZE, ""));
|
|
1859
|
-
}
|
|
1860
|
-
function isIso7064Mod97(iban2) {
|
|
1861
|
-
let remainder = 0;
|
|
1862
|
-
const len = iban2.length;
|
|
1863
|
-
for (let i = 4;i < len; i++) {
|
|
1864
|
-
const code = iban2.charCodeAt(i);
|
|
1865
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1866
|
-
}
|
|
1867
|
-
for (let i = 0;i < 4; i++) {
|
|
1868
|
-
const code = iban2.charCodeAt(i);
|
|
1869
|
-
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
1870
|
-
}
|
|
1871
|
-
return remainder === 1;
|
|
1872
|
-
}
|
|
1873
|
-
function isValidIBAN(input) {
|
|
1874
|
-
if (!iban.test(input))
|
|
1875
|
-
return false;
|
|
1876
|
-
return isIso7064Mod97(input);
|
|
1877
|
-
}
|
|
1878
1842
|
function isValidJWT(token, algorithm = null) {
|
|
1879
1843
|
try {
|
|
1880
1844
|
const tokensParts = token.split(".");
|
|
@@ -2200,7 +2164,7 @@ function handleRefineResult(result, payload, input, inst) {
|
|
|
2200
2164
|
payload.issues.push(issue(_iss));
|
|
2201
2165
|
}
|
|
2202
2166
|
}
|
|
2203
|
-
var $ZodType, toStandardResult = (r, ctx) => r.issues.length ? { issues: r.issues.map((iss) => finalizeIssue(iss, ctx, config())) } : { value: r.value }, $ZodString, $ZodStringFormat, $ZodGUID, $ZodUUID, $ZodEmail, URL_BAD_FORMAT = 1, URL_UNPARSEABLE = 2, asciiTabOrNewline, $ZodURL, $ZodEmoji, $ZodNanoID, $ZodCUID, $ZodCUID2, $ZodULID, $ZodXID, $ZodKSUID, $ZodISODateTime, $ZodISODate, $ZodISOTime, $ZodISODuration, $ZodIPv4, ipv6Alphabet, $ZodIPv6, $ZodCIDRv4, $ZodCIDRv6, base64Charset, $ZodBase64, base64urlCharset, $ZodBase64URL, $ZodE164,
|
|
2167
|
+
var $ZodType, toStandardResult = (r, ctx) => r.issues.length ? { issues: r.issues.map((iss) => finalizeIssue(iss, ctx, config())) } : { value: r.value }, $ZodString, $ZodStringFormat, $ZodGUID, $ZodUUID, $ZodEmail, URL_BAD_FORMAT = 1, URL_UNPARSEABLE = 2, asciiTabOrNewline, $ZodURL, $ZodEmoji, $ZodNanoID, $ZodCUID, $ZodCUID2, $ZodULID, $ZodXID, $ZodKSUID, $ZodISODateTime, $ZodISODate, $ZodISOTime, $ZodISODuration, $ZodIPv4, ipv6Alphabet, $ZodIPv6, $ZodCIDRv4, $ZodCIDRv6, base64Charset, $ZodBase64, base64urlCharset, $ZodBase64URL, $ZodE164, $ZodJWT, $ZodNumber, $ZodNumberFormat, $ZodBoolean, $ZodUnknown, $ZodNever, $ZodArray, NO_SYMBOL_KEYS, $ZodObject, $ZodObjectJIT, $ZodUnion, $ZodDiscriminatedUnion, $ZodIntersection, $ZodRecord, $ZodEnum, $ZodLiteral, $ZodTransform, $ZodOptional, $ZodExactOptional, $ZodNullable, $ZodDefault, $ZodPrefault, $ZodNonOptional, $ZodCatch, $ZodPipe, $ZodReadonly, $ZodCustom;
|
|
2204
2168
|
var init_schemas = __esm(() => {
|
|
2205
2169
|
init_checks();
|
|
2206
2170
|
init_core();
|
|
@@ -2555,7 +2519,6 @@ var init_schemas = __esm(() => {
|
|
|
2555
2519
|
def.pattern ?? (def.pattern = e164);
|
|
2556
2520
|
$ZodStringFormat.init(inst, def);
|
|
2557
2521
|
});
|
|
2558
|
-
CC_SANITIZE = /[- ]/g;
|
|
2559
2522
|
$ZodJWT = /* @__PURE__ */ $constructor("$ZodJWT", (inst, def) => {
|
|
2560
2523
|
$ZodStringFormat.init(inst, def);
|
|
2561
2524
|
inst._zod.check = (payload) => {
|
|
@@ -3533,9 +3496,6 @@ function settle(inst, answer) {
|
|
|
3533
3496
|
recursive.set(inst, answer === PROVEN);
|
|
3534
3497
|
return answer;
|
|
3535
3498
|
}
|
|
3536
|
-
function isRecursiveSchema(inst) {
|
|
3537
|
-
return isRecursive(inst, new Set, true) !== NONE;
|
|
3538
|
-
}
|
|
3539
3499
|
function bucketFor(state, inst) {
|
|
3540
3500
|
let bucket = state.buckets.get(inst);
|
|
3541
3501
|
if (!bucket) {
|
|
@@ -3845,1574 +3805,10 @@ var init_registries = __esm(() => {
|
|
|
3845
3805
|
});
|
|
3846
3806
|
|
|
3847
3807
|
// node_modules/zod/v4/core/compile.js
|
|
3848
|
-
|
|
3849
|
-
try {
|
|
3850
|
-
return compileFn(schema, { assertOnly: true });
|
|
3851
|
-
} catch {
|
|
3852
|
-
return parser;
|
|
3853
|
-
}
|
|
3854
|
-
}
|
|
3855
|
-
function compile(schema, options) {
|
|
3856
|
-
try {
|
|
3857
|
-
const parser = compileFn(schema);
|
|
3858
|
-
const clone = withParser(schema, parser);
|
|
3859
|
-
clone._zod.bag.validator = compileValidator(schema, parser);
|
|
3860
|
-
return clone;
|
|
3861
|
-
} catch (err) {
|
|
3862
|
-
if (options?.strict)
|
|
3863
|
-
throw err;
|
|
3864
|
-
return schema;
|
|
3865
|
-
}
|
|
3866
|
-
}
|
|
3867
|
-
function withParser(schema, parser) {
|
|
3868
|
-
if (isRecursiveSchema(schema)) {
|
|
3869
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3870
|
-
}
|
|
3871
|
-
const clone2 = clone(schema);
|
|
3872
|
-
const liveRun = schema._zod.run;
|
|
3873
|
-
const originalRun = liveRun.__originalRun ?? liveRun;
|
|
3874
|
-
const wrapped = (payload, ctx) => {
|
|
3875
|
-
if (ctx?.async || ctx?.direction === "backward" || ctx?.skipChecks || ctx?.[FALLBACK_FLAG]) {
|
|
3876
|
-
return originalRun(payload, ctx);
|
|
3877
|
-
}
|
|
3878
|
-
if (ctx && isBackEdge(ctx, payload.value)) {
|
|
3879
|
-
return originalRun(payload, ctx);
|
|
3880
|
-
}
|
|
3881
|
-
const out = parser(payload.value);
|
|
3882
|
-
if (out !== INVALID) {
|
|
3883
|
-
payload.value = out;
|
|
3884
|
-
return payload;
|
|
3885
|
-
}
|
|
3886
|
-
if (ctx)
|
|
3887
|
-
ctx[FALLBACK_FLAG] = true;
|
|
3888
|
-
return originalRun(payload, ctx);
|
|
3889
|
-
};
|
|
3890
|
-
wrapped.__originalRun = originalRun;
|
|
3891
|
-
clone2._zod.bag.fallbackRun = originalRun;
|
|
3892
|
-
clone2._zod.bag.validator = parser;
|
|
3893
|
-
clone2._zod.run = wrapped;
|
|
3894
|
-
if (!liveRun.__originalRun)
|
|
3895
|
-
installCompiledUserMethods(clone2, schema, parser);
|
|
3896
|
-
return clone2;
|
|
3897
|
-
}
|
|
3898
|
-
function installCompiledUserMethods(target, source, parser) {
|
|
3899
|
-
const targetAny = target;
|
|
3900
|
-
const sourceAny = source;
|
|
3901
|
-
if (typeof sourceAny.safeParse === "function") {
|
|
3902
|
-
const originalSafeParse = sourceAny.safeParse;
|
|
3903
|
-
targetAny.safeParse = (data, params) => {
|
|
3904
|
-
const out = parser(data);
|
|
3905
|
-
if (out !== INVALID) {
|
|
3906
|
-
return { success: true, data: out };
|
|
3907
|
-
}
|
|
3908
|
-
return originalSafeParse(data, params);
|
|
3909
|
-
};
|
|
3910
|
-
}
|
|
3911
|
-
if (typeof sourceAny.parse === "function") {
|
|
3912
|
-
const originalParse = sourceAny.parse;
|
|
3913
|
-
targetAny.parse = (data, params) => {
|
|
3914
|
-
const out = parser(data);
|
|
3915
|
-
if (out !== INVALID) {
|
|
3916
|
-
return out;
|
|
3917
|
-
}
|
|
3918
|
-
return originalParse(data, params);
|
|
3919
|
-
};
|
|
3920
|
-
}
|
|
3921
|
-
}
|
|
3922
|
-
function compileFn(schema, options) {
|
|
3923
|
-
let recursive = true;
|
|
3924
|
-
try {
|
|
3925
|
-
recursive = isRecursiveSchema(schema);
|
|
3926
|
-
} catch {}
|
|
3927
|
-
if (recursive) {
|
|
3928
|
-
throw new ZodCompileUnsupportedError("a schema whose subtree contains a reference cycle");
|
|
3929
|
-
}
|
|
3930
|
-
const ctx = {
|
|
3931
|
-
constants: new Map,
|
|
3932
|
-
constantCounter: 0,
|
|
3933
|
-
varCounter: 0,
|
|
3934
|
-
definite: true
|
|
3935
|
-
};
|
|
3936
|
-
const doc = new Doc(["input"]);
|
|
3937
|
-
const outputAccessor = generateCheck(doc, ctx, schema, "input", !options?.assertOnly);
|
|
3938
|
-
doc.write(outputAccessor === null ? `return true;` : `return ${outputAccessor};`);
|
|
3939
|
-
const constantNames = ["INVALID", ...ctx.constants.keys()];
|
|
3940
|
-
const constantValues = [INVALID, ...ctx.constants.values()];
|
|
3941
|
-
const code = doc.content.join(`
|
|
3942
|
-
`);
|
|
3943
|
-
const fullCode = options?.debug ? constantNames.length > 0 ? `// Constants: ${constantNames.join(", ")}
|
|
3944
|
-
${code}` : code : "";
|
|
3945
|
-
const F = Function;
|
|
3946
|
-
const factoryCode = `return (input) => {
|
|
3947
|
-
${code}
|
|
3948
|
-
}`;
|
|
3949
|
-
let fn;
|
|
3950
|
-
try {
|
|
3951
|
-
const factory = new F(...constantNames, factoryCode);
|
|
3952
|
-
fn = factory(...constantValues);
|
|
3953
|
-
} catch (err) {
|
|
3954
|
-
throw new ZodCompileUnsupportedError(`this schema (generated code failed to evaluate: ${err.message})`);
|
|
3955
|
-
}
|
|
3956
|
-
if (options?.debug) {
|
|
3957
|
-
fn.code = fullCode;
|
|
3958
|
-
}
|
|
3959
|
-
fn.definite = ctx.definite;
|
|
3960
|
-
return fn;
|
|
3961
|
-
}
|
|
3962
|
-
function addConstant(ctx, value) {
|
|
3963
|
-
for (const [name, v] of ctx.constants) {
|
|
3964
|
-
if (v === value)
|
|
3965
|
-
return name;
|
|
3966
|
-
}
|
|
3967
|
-
const name = `c${ctx.constantCounter++}`;
|
|
3968
|
-
ctx.constants.set(name, value);
|
|
3969
|
-
return name;
|
|
3970
|
-
}
|
|
3971
|
-
function addUserConstant(ctx, fn) {
|
|
3972
|
-
ctx.definite = false;
|
|
3973
|
-
return addConstant(ctx, fn);
|
|
3974
|
-
}
|
|
3975
|
-
function newVar(ctx) {
|
|
3976
|
-
return `v${ctx.varCounter++}`;
|
|
3977
|
-
}
|
|
3978
|
-
function runtimeRun(schema, value) {
|
|
3979
|
-
const result = schema._zod.run({ value, issues: [] }, {});
|
|
3980
|
-
if (result && typeof result.then === "function")
|
|
3981
|
-
return INVALID;
|
|
3982
|
-
const r = result;
|
|
3983
|
-
return r.issues.length === 0 ? r.value : INVALID;
|
|
3984
|
-
}
|
|
3985
|
-
function compileChild(doc, ctx, schema, accessor, needsValue = true) {
|
|
3986
|
-
const contentLen = doc.content.length;
|
|
3987
|
-
const constantCount = ctx.constants.size;
|
|
3988
|
-
const constantCounter = ctx.constantCounter;
|
|
3989
|
-
const varCounter = ctx.varCounter;
|
|
3990
|
-
try {
|
|
3991
|
-
return generateCheck(doc, ctx, schema, accessor, needsValue);
|
|
3992
|
-
} catch (err) {
|
|
3993
|
-
if (!(err instanceof ZodCompileUnsupportedError) || !err.islandable)
|
|
3994
|
-
throw err;
|
|
3995
|
-
doc.content.length = contentLen;
|
|
3996
|
-
if (ctx.constants.size > constantCount) {
|
|
3997
|
-
const trailing = Array.from(ctx.constants.keys()).slice(constantCount);
|
|
3998
|
-
for (const k of trailing)
|
|
3999
|
-
ctx.constants.delete(k);
|
|
4000
|
-
}
|
|
4001
|
-
ctx.constantCounter = constantCounter;
|
|
4002
|
-
ctx.varCounter = varCounter;
|
|
4003
|
-
return emitRuntimeIsland(doc, ctx, schema, accessor);
|
|
4004
|
-
}
|
|
4005
|
-
}
|
|
4006
|
-
function emitRuntimeIsland(doc, ctx, schema, accessor) {
|
|
4007
|
-
ctx.definite = false;
|
|
4008
|
-
const schemaConst = addConstant(ctx, schema);
|
|
4009
|
-
const runConst = addConstant(ctx, runtimeRun);
|
|
4010
|
-
const outVar = newVar(ctx);
|
|
4011
|
-
doc.write(`const ${outVar} = ${runConst}(${schemaConst}, ${accessor});`);
|
|
4012
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
4013
|
-
return outVar;
|
|
4014
|
-
}
|
|
4015
|
-
function generateChecks(doc, ctx, schema, accessor) {
|
|
4016
|
-
const schemaChecks = schema._zod.def.checks;
|
|
4017
|
-
if (!schemaChecks || schemaChecks.length === 0)
|
|
4018
|
-
return accessor;
|
|
4019
|
-
let currentAccessor = accessor;
|
|
4020
|
-
for (const check of schemaChecks) {
|
|
4021
|
-
const def = check._zod.def;
|
|
4022
|
-
if (def.when && !WHEN_DEFAULTED_CHECKS.has(def.check)) {
|
|
4023
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
4024
|
-
}
|
|
4025
|
-
switch (def.check) {
|
|
4026
|
-
case "greater_than":
|
|
4027
|
-
generateGreaterThanCheck(doc, ctx, def, currentAccessor);
|
|
4028
|
-
break;
|
|
4029
|
-
case "less_than":
|
|
4030
|
-
generateLessThanCheck(doc, ctx, def, currentAccessor);
|
|
4031
|
-
break;
|
|
4032
|
-
case "multiple_of":
|
|
4033
|
-
generateMultipleOfCheck(doc, ctx, def, currentAccessor);
|
|
4034
|
-
break;
|
|
4035
|
-
case "number_format":
|
|
4036
|
-
generateNumberFormatCheck(doc, def, currentAccessor);
|
|
4037
|
-
break;
|
|
4038
|
-
case "min_length": {
|
|
4039
|
-
const min = numericOperand(def.minimum, "min_length");
|
|
4040
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${min} && ${currentAccessor}.length < ${def.minimum * 2}`);
|
|
4041
|
-
doc.write(`if (${len} < ${min}) return INVALID;`);
|
|
4042
|
-
break;
|
|
4043
|
-
}
|
|
4044
|
-
case "max_length": {
|
|
4045
|
-
const max = numericOperand(def.maximum, "max_length");
|
|
4046
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length > ${max}`);
|
|
4047
|
-
doc.write(`if (${len} > ${max}) return INVALID;`);
|
|
4048
|
-
break;
|
|
4049
|
-
}
|
|
4050
|
-
case "length_equals": {
|
|
4051
|
-
const exact = numericOperand(def.length, "length_equals");
|
|
4052
|
-
const len = codePointLengthVar(doc, ctx, currentAccessor, `${currentAccessor}.length >= ${exact} && ${currentAccessor}.length <= ${def.length * 2}`);
|
|
4053
|
-
doc.write(`if (${len} !== ${exact}) return INVALID;`);
|
|
4054
|
-
break;
|
|
4055
|
-
}
|
|
4056
|
-
case "min_size":
|
|
4057
|
-
doc.write(`if (${currentAccessor}.size < ${numericOperand(def.minimum, "min_size")}) return INVALID;`);
|
|
4058
|
-
break;
|
|
4059
|
-
case "max_size":
|
|
4060
|
-
doc.write(`if (${currentAccessor}.size > ${numericOperand(def.maximum, "max_size")}) return INVALID;`);
|
|
4061
|
-
break;
|
|
4062
|
-
case "size_equals":
|
|
4063
|
-
doc.write(`if (${currentAccessor}.size !== ${numericOperand(def.size, "size_equals")}) return INVALID;`);
|
|
4064
|
-
break;
|
|
4065
|
-
case "string_format":
|
|
4066
|
-
currentAccessor = generateStringFormatCheck(doc, ctx, def, currentAccessor);
|
|
4067
|
-
break;
|
|
4068
|
-
case "custom":
|
|
4069
|
-
currentAccessor = generateCustomRefineCheck(doc, ctx, check, currentAccessor);
|
|
4070
|
-
break;
|
|
4071
|
-
case "bigint_format":
|
|
4072
|
-
generateBigIntFormatCheck(doc, def, currentAccessor);
|
|
4073
|
-
break;
|
|
4074
|
-
case "mime_type":
|
|
4075
|
-
generateMimeTypeCheck(doc, ctx, def, currentAccessor);
|
|
4076
|
-
break;
|
|
4077
|
-
case "property":
|
|
4078
|
-
generatePropertyCheck(doc, ctx, def, currentAccessor);
|
|
4079
|
-
break;
|
|
4080
|
-
case "properties":
|
|
4081
|
-
generatePropertiesChecks(doc, ctx, def, currentAccessor);
|
|
4082
|
-
break;
|
|
4083
|
-
case "overwrite": {
|
|
4084
|
-
const newAccessor = newVar(ctx);
|
|
4085
|
-
generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor);
|
|
4086
|
-
currentAccessor = newAccessor;
|
|
4087
|
-
break;
|
|
4088
|
-
}
|
|
4089
|
-
default: {
|
|
4090
|
-
throw new ZodCompileUnsupportedError(`check type ${def.check}`);
|
|
4091
|
-
}
|
|
4092
|
-
}
|
|
4093
|
-
}
|
|
4094
|
-
return currentAccessor;
|
|
4095
|
-
}
|
|
4096
|
-
function codePointLengthVar(doc, ctx, accessor, inDoubt) {
|
|
4097
|
-
const cpLen = addConstant(ctx, codePointLength);
|
|
4098
|
-
const v = newVar(ctx);
|
|
4099
|
-
doc.write(`const ${v} = typeof ${accessor} === "string" && ${inDoubt} ? ${cpLen}(${accessor}) : ${accessor}.length;`);
|
|
4100
|
-
return v;
|
|
4101
|
-
}
|
|
4102
|
-
function numericOperand(value, label) {
|
|
4103
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
4104
|
-
throw new ZodCompileUnsupportedError(`${label} bound of type ${typeof value}`);
|
|
4105
|
-
}
|
|
4106
|
-
return `${value}`;
|
|
4107
|
-
}
|
|
4108
|
-
function comparisonOperand(ctx, value) {
|
|
4109
|
-
if (typeof value === "bigint")
|
|
4110
|
-
return `${value}n`;
|
|
4111
|
-
if (typeof value === "number") {
|
|
4112
|
-
if (Number.isNaN(value))
|
|
4113
|
-
throw new ZodCompileUnsupportedError("comparison check with NaN bound");
|
|
4114
|
-
return `${value}`;
|
|
4115
|
-
}
|
|
4116
|
-
if (value instanceof Date) {
|
|
4117
|
-
if (Number.isNaN(value.getTime())) {
|
|
4118
|
-
throw new ZodCompileUnsupportedError("comparison check with Invalid Date bound");
|
|
4119
|
-
}
|
|
4120
|
-
return addConstant(ctx, value);
|
|
4121
|
-
}
|
|
4122
|
-
throw new ZodCompileUnsupportedError(`comparison check bound of type ${typeof value}`);
|
|
4123
|
-
}
|
|
4124
|
-
function generateGreaterThanCheck(doc, ctx, def, accessor) {
|
|
4125
|
-
const op = def.inclusive ? "<" : "<=";
|
|
4126
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
4127
|
-
}
|
|
4128
|
-
function generateLessThanCheck(doc, ctx, def, accessor) {
|
|
4129
|
-
const op = def.inclusive ? ">" : ">=";
|
|
4130
|
-
doc.write(`if (${accessor} ${op} ${comparisonOperand(ctx, def.value)}) return INVALID;`);
|
|
4131
|
-
}
|
|
4132
|
-
function generateMultipleOfCheck(doc, ctx, def, accessor) {
|
|
4133
|
-
if (typeof def.value === "bigint") {
|
|
4134
|
-
if (def.value === BigInt(0))
|
|
4135
|
-
throw new ZodCompileUnsupportedError("multiple_of check with a zero divisor");
|
|
4136
|
-
doc.write(`if (${accessor} % ${def.value}n !== 0n) return INVALID;`);
|
|
4137
|
-
} else {
|
|
4138
|
-
const remainder = addConstant(ctx, floatSafeRemainder);
|
|
4139
|
-
doc.write(`if (${remainder}(${accessor}, ${numericOperand(def.value, "multiple_of")}) !== 0) return INVALID;`);
|
|
4140
|
-
}
|
|
4141
|
-
}
|
|
4142
|
-
function generateNumberFormatCheck(doc, def, accessor) {
|
|
4143
|
-
const format = def.format;
|
|
4144
|
-
switch (format) {
|
|
4145
|
-
case "safeint":
|
|
4146
|
-
doc.write(`if (!Number.isSafeInteger(${accessor})) return INVALID;`);
|
|
4147
|
-
break;
|
|
4148
|
-
case "int32":
|
|
4149
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < -2147483648 || ${accessor} > 2147483647) return INVALID;`);
|
|
4150
|
-
break;
|
|
4151
|
-
case "uint32":
|
|
4152
|
-
doc.write(`if (!Number.isInteger(${accessor}) || ${accessor} < 0 || ${accessor} > 4294967295) return INVALID;`);
|
|
4153
|
-
break;
|
|
4154
|
-
case "float32":
|
|
4155
|
-
doc.write(`if (!Number.isFinite(${accessor}) || ${accessor} < -3.4028234663852886e38 || ${accessor} > 3.4028234663852886e38) return INVALID;`);
|
|
4156
|
-
break;
|
|
4157
|
-
case "float64":
|
|
4158
|
-
doc.write(`if (!Number.isFinite(${accessor})) return INVALID;`);
|
|
4159
|
-
break;
|
|
4160
|
-
default: {
|
|
4161
|
-
throw new ZodCompileUnsupportedError(`number format ${format}`);
|
|
4162
|
-
}
|
|
4163
|
-
}
|
|
4164
|
-
}
|
|
4165
|
-
function generateBigIntFormatCheck(doc, def, accessor) {
|
|
4166
|
-
const format = def.format;
|
|
4167
|
-
if (!format)
|
|
4168
|
-
return;
|
|
4169
|
-
switch (format) {
|
|
4170
|
-
case "int64":
|
|
4171
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
4172
|
-
break;
|
|
4173
|
-
case "uint64":
|
|
4174
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
4175
|
-
break;
|
|
4176
|
-
default: {
|
|
4177
|
-
throw new ZodCompileUnsupportedError(`bigint format ${format}`);
|
|
4178
|
-
}
|
|
4179
|
-
}
|
|
4180
|
-
}
|
|
4181
|
-
function generateMimeTypeCheck(doc, ctx, def, accessor) {
|
|
4182
|
-
const mimeTypes = def.mime;
|
|
4183
|
-
if (mimeTypes && mimeTypes.length > 0) {
|
|
4184
|
-
const mimeSet = addConstant(ctx, new Set(mimeTypes));
|
|
4185
|
-
doc.write(`if (!${mimeSet}.has(${accessor}.type)) return INVALID;`);
|
|
4186
|
-
}
|
|
4187
|
-
}
|
|
4188
|
-
function generatePropertiesChecks(doc, ctx, def, accessor) {
|
|
4189
|
-
if (def.when) {
|
|
4190
|
-
throw new ZodCompileUnsupportedError(`check with a custom "when" condition`);
|
|
4191
|
-
}
|
|
4192
|
-
doc.write(`if (${accessor} == null) return INVALID;`);
|
|
4193
|
-
const shape = def.shape;
|
|
4194
|
-
for (const key of Reflect.ownKeys(shape)) {
|
|
4195
|
-
const keyExpr = typeof key === "symbol" ? addConstant(ctx, key) : esc(key);
|
|
4196
|
-
const inputVar = newVar(ctx);
|
|
4197
|
-
doc.write(`const ${inputVar} = ${accessor}[${keyExpr}];`);
|
|
4198
|
-
compileChild(doc, ctx, shape[key], inputVar, false);
|
|
4199
|
-
}
|
|
4200
|
-
}
|
|
4201
|
-
function generatePropertyCheck(doc, ctx, def, accessor) {
|
|
4202
|
-
const propAccessor = `${accessor}[${JSON.stringify(def.property)}]`;
|
|
4203
|
-
generateCheck(doc, ctx, def.schema, propAccessor);
|
|
4204
|
-
}
|
|
4205
|
-
function generateOverwriteCheck(doc, ctx, check, currentAccessor, newAccessor) {
|
|
4206
|
-
const tx = check._zod.def.tx;
|
|
4207
|
-
if (!tx) {
|
|
4208
|
-
throw new ZodCompileUnsupportedError("overwrite check without a transform function");
|
|
4209
|
-
}
|
|
4210
|
-
if (isAsyncFunction(tx)) {
|
|
4211
|
-
throw new ZodCompileAsyncError("z.compile: async overwrite transforms are not supported");
|
|
4212
|
-
}
|
|
4213
|
-
const txConst = addConstant(ctx, tx);
|
|
4214
|
-
doc.write(`const ${newAccessor} = ${txConst}(${currentAccessor});`);
|
|
4215
|
-
}
|
|
4216
|
-
function throwAsync() {
|
|
4217
|
-
throw new $ZodAsyncError;
|
|
4218
|
-
}
|
|
4219
|
-
function pushIssue(issue) {
|
|
4220
|
-
this.issues.push(issue);
|
|
4221
|
-
}
|
|
4222
|
-
function generateCustomRefineCheck(doc, ctx, check, accessor) {
|
|
4223
|
-
const def = check._zod.def;
|
|
4224
|
-
if (def.fn) {
|
|
4225
|
-
if (isAsyncFunction(def.fn)) {
|
|
4226
|
-
throw new ZodCompileAsyncError("z.compile: async .refine() predicates are not supported");
|
|
4227
|
-
}
|
|
4228
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
4229
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
4230
|
-
const resVar = newVar(ctx);
|
|
4231
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
4232
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
4233
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
4234
|
-
return accessor;
|
|
4235
|
-
}
|
|
4236
|
-
if (check._zod.check) {
|
|
4237
|
-
if (isAsyncFunction(check._zod.check)) {
|
|
4238
|
-
throw new ZodCompileAsyncError("z.compile: async .superRefine() / check functions are not supported");
|
|
4239
|
-
}
|
|
4240
|
-
const checkFn = check._zod.check;
|
|
4241
|
-
const helperFn = (value) => {
|
|
4242
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
4243
|
-
const result = checkFn(fakePayload);
|
|
4244
|
-
if (result instanceof Promise)
|
|
4245
|
-
throwAsync();
|
|
4246
|
-
return fakePayload.issues.length === 0 ? fakePayload.value : INVALID;
|
|
4247
|
-
};
|
|
4248
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
4249
|
-
const outVar = newVar(ctx);
|
|
4250
|
-
doc.write(`const ${outVar} = ${helperConst}(${accessor});`);
|
|
4251
|
-
doc.write(`if (${outVar} === INVALID) return INVALID;`);
|
|
4252
|
-
return outVar;
|
|
4253
|
-
}
|
|
4254
|
-
throw new ZodCompileUnsupportedError("custom check without a predicate or check function");
|
|
4255
|
-
}
|
|
4256
|
-
function generateStringFormatCheck(doc, ctx, def, accessor, needsValue = true) {
|
|
4257
|
-
const fmt = def.format;
|
|
4258
|
-
if (fmt === "base64") {
|
|
4259
|
-
const validator = addConstant(ctx, isValidBase64);
|
|
4260
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4261
|
-
return accessor;
|
|
4262
|
-
}
|
|
4263
|
-
if (fmt === "base64url") {
|
|
4264
|
-
const validator = addConstant(ctx, isValidBase64URL);
|
|
4265
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4266
|
-
return accessor;
|
|
4267
|
-
}
|
|
4268
|
-
if (fmt === "jwt") {
|
|
4269
|
-
const validator = addConstant(ctx, isValidJWT);
|
|
4270
|
-
const alg = addConstant(ctx, def.alg ?? null);
|
|
4271
|
-
doc.write(`if (!${validator}(${accessor}, ${alg})) return INVALID;`);
|
|
4272
|
-
return accessor;
|
|
4273
|
-
}
|
|
4274
|
-
if (fmt === "ipv6") {
|
|
4275
|
-
const validator = addConstant(ctx, isValidIPv6);
|
|
4276
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4277
|
-
return accessor;
|
|
4278
|
-
}
|
|
4279
|
-
if (fmt === "cidrv6") {
|
|
4280
|
-
const validator = addConstant(ctx, isValidCIDRv6);
|
|
4281
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4282
|
-
return accessor;
|
|
4283
|
-
}
|
|
4284
|
-
if (fmt === "credit_card") {
|
|
4285
|
-
const validator = addConstant(ctx, isValidCreditCard);
|
|
4286
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4287
|
-
return accessor;
|
|
4288
|
-
}
|
|
4289
|
-
if (fmt === "iban") {
|
|
4290
|
-
const validator = addConstant(ctx, isValidIBAN);
|
|
4291
|
-
doc.write(`if (!${validator}(${accessor})) return INVALID;`);
|
|
4292
|
-
return accessor;
|
|
4293
|
-
}
|
|
4294
|
-
const formatDef = def;
|
|
4295
|
-
if (fmt === "url" || fmt === "httpurl" || formatDef.normalize || formatDef.hostname !== undefined || formatDef.protocol !== undefined) {
|
|
4296
|
-
const parseConst = addConstant(ctx, validateURL);
|
|
4297
|
-
const defConst = addConstant(ctx, def);
|
|
4298
|
-
const trimVar = newVar(ctx);
|
|
4299
|
-
const urlVar = newVar(ctx);
|
|
4300
|
-
doc.write(`const ${trimVar} = ${accessor}.trim();`);
|
|
4301
|
-
doc.write(`const ${urlVar} = ${parseConst}(${trimVar}, ${defConst});`);
|
|
4302
|
-
doc.write(`if (typeof ${urlVar} === "number") return INVALID;`);
|
|
4303
|
-
if (formatDef.hostname !== undefined) {
|
|
4304
|
-
const hostnameConst = addConstant(ctx, urlHostnameOk);
|
|
4305
|
-
doc.write(`if (!${hostnameConst}(${urlVar}, ${defConst}.hostname)) return INVALID;`);
|
|
4306
|
-
}
|
|
4307
|
-
if (formatDef.protocol !== undefined) {
|
|
4308
|
-
const protocolConst = addConstant(ctx, urlProtocolOk);
|
|
4309
|
-
doc.write(`if (!${protocolConst}(${urlVar}, ${defConst}.protocol)) return INVALID;`);
|
|
4310
|
-
}
|
|
4311
|
-
if (!needsValue)
|
|
4312
|
-
return null;
|
|
4313
|
-
const outputVar = newVar(ctx);
|
|
4314
|
-
const outputExpr = formatDef.normalize ? `${urlVar}.href` : `${addConstant(ctx, stripTabAndNewline)}(${trimVar})`;
|
|
4315
|
-
doc.write(`const ${outputVar} = ${outputExpr};`);
|
|
4316
|
-
return outputVar;
|
|
4317
|
-
}
|
|
4318
|
-
const customFn = def.fn;
|
|
4319
|
-
if (customFn) {
|
|
4320
|
-
if (isAsyncFunction(customFn))
|
|
4321
|
-
throw new ZodCompileUnsupportedError(`async string format ${fmt}`);
|
|
4322
|
-
const fnConst = addConstant(ctx, customFn);
|
|
4323
|
-
doc.write(`if (!${fnConst}(${accessor})) return INVALID;`);
|
|
4324
|
-
return accessor;
|
|
4325
|
-
}
|
|
4326
|
-
if (PATTERN_IS_COMPLETE.has(fmt) && def.pattern) {
|
|
4327
|
-
const patternConst = addConstant(ctx, def.pattern);
|
|
4328
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
4329
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
4330
|
-
return accessor;
|
|
4331
|
-
}
|
|
4332
|
-
const format = def.format;
|
|
4333
|
-
switch (format) {
|
|
4334
|
-
case "regex":
|
|
4335
|
-
throw new ZodCompileUnsupportedError("regex format without a pattern");
|
|
4336
|
-
case "lowercase":
|
|
4337
|
-
doc.write(`if (${accessor} !== ${accessor}.toLowerCase()) return INVALID;`);
|
|
4338
|
-
break;
|
|
4339
|
-
case "uppercase":
|
|
4340
|
-
doc.write(`if (${accessor} !== ${accessor}.toUpperCase()) return INVALID;`);
|
|
4341
|
-
break;
|
|
4342
|
-
case "includes":
|
|
4343
|
-
doc.write(`if (!${accessor}.includes(${esc(def.includes)})) return INVALID;`);
|
|
4344
|
-
break;
|
|
4345
|
-
case "starts_with": {
|
|
4346
|
-
const prefix = def.prefix;
|
|
4347
|
-
doc.write(`if (${accessor}.slice(0, ${prefix.length}) !== ${esc(prefix)}) return INVALID;`);
|
|
4348
|
-
break;
|
|
4349
|
-
}
|
|
4350
|
-
case "ends_with": {
|
|
4351
|
-
const suffix = def.suffix;
|
|
4352
|
-
doc.write(`if (${accessor}.slice(-${suffix.length}) !== ${esc(suffix)}) return INVALID;`);
|
|
4353
|
-
break;
|
|
4354
|
-
}
|
|
4355
|
-
default: {
|
|
4356
|
-
throw new ZodCompileUnsupportedError(`string format ${format}`);
|
|
4357
|
-
}
|
|
4358
|
-
}
|
|
4359
|
-
return accessor;
|
|
4360
|
-
}
|
|
4361
|
-
function generateCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4362
|
-
const def = schema._zod.def;
|
|
4363
|
-
const type = def.type;
|
|
4364
|
-
if (def.coerce) {
|
|
4365
|
-
throw new ZodCompileUnsupportedError(`coercion (z.coerce.${type}())`);
|
|
4366
|
-
}
|
|
4367
|
-
const buildsValue = needsValue || !!def.checks?.length;
|
|
4368
|
-
let typeAccessor;
|
|
4369
|
-
switch (type) {
|
|
4370
|
-
case "string":
|
|
4371
|
-
typeAccessor = generateStringCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4372
|
-
break;
|
|
4373
|
-
case "number":
|
|
4374
|
-
typeAccessor = generateNumberCheck(doc, schema, accessor);
|
|
4375
|
-
break;
|
|
4376
|
-
case "boolean":
|
|
4377
|
-
typeAccessor = generateBooleanCheck(doc, accessor);
|
|
4378
|
-
break;
|
|
4379
|
-
case "bigint":
|
|
4380
|
-
typeAccessor = generateBigIntCheck(doc, schema, accessor);
|
|
4381
|
-
break;
|
|
4382
|
-
case "symbol":
|
|
4383
|
-
typeAccessor = generateSymbolCheck(doc, accessor);
|
|
4384
|
-
break;
|
|
4385
|
-
case "undefined":
|
|
4386
|
-
typeAccessor = generateUndefinedCheck(doc, accessor);
|
|
4387
|
-
break;
|
|
4388
|
-
case "null":
|
|
4389
|
-
typeAccessor = generateNullCheck(doc, accessor);
|
|
4390
|
-
break;
|
|
4391
|
-
case "any":
|
|
4392
|
-
case "unknown":
|
|
4393
|
-
typeAccessor = accessor;
|
|
4394
|
-
break;
|
|
4395
|
-
case "never":
|
|
4396
|
-
doc.write("return INVALID;");
|
|
4397
|
-
typeAccessor = accessor;
|
|
4398
|
-
break;
|
|
4399
|
-
case "void":
|
|
4400
|
-
typeAccessor = generateVoidCheck(doc, accessor);
|
|
4401
|
-
break;
|
|
4402
|
-
case "nan":
|
|
4403
|
-
typeAccessor = generateNaNCheck(doc, accessor);
|
|
4404
|
-
break;
|
|
4405
|
-
case "date":
|
|
4406
|
-
typeAccessor = generateDateCheck(doc, accessor);
|
|
4407
|
-
break;
|
|
4408
|
-
case "object":
|
|
4409
|
-
typeAccessor = generateObjectCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4410
|
-
break;
|
|
4411
|
-
case "optional":
|
|
4412
|
-
typeAccessor = generateOptionalCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4413
|
-
break;
|
|
4414
|
-
case "nullable":
|
|
4415
|
-
typeAccessor = generateNullableCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4416
|
-
break;
|
|
4417
|
-
case "array":
|
|
4418
|
-
typeAccessor = generateArrayCheck(doc, ctx, schema, accessor, buildsValue);
|
|
4419
|
-
break;
|
|
4420
|
-
case "literal":
|
|
4421
|
-
typeAccessor = generateLiteralCheck(doc, ctx, schema, accessor);
|
|
4422
|
-
break;
|
|
4423
|
-
case "enum":
|
|
4424
|
-
typeAccessor = generateEnumCheck(doc, ctx, schema, accessor);
|
|
4425
|
-
break;
|
|
4426
|
-
case "readonly": {
|
|
4427
|
-
const innerOut = generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4428
|
-
const frozenVar = newVar(ctx);
|
|
4429
|
-
doc.write(`const ${frozenVar} = Object.freeze(${innerOut});`);
|
|
4430
|
-
typeAccessor = frozenVar;
|
|
4431
|
-
break;
|
|
4432
|
-
}
|
|
4433
|
-
case "success":
|
|
4434
|
-
generateWrapperCheck(doc, ctx, schema, accessor);
|
|
4435
|
-
typeAccessor = "true";
|
|
4436
|
-
break;
|
|
4437
|
-
case "default":
|
|
4438
|
-
case "prefault":
|
|
4439
|
-
typeAccessor = generateDefaultCheck(doc, ctx, schema, accessor);
|
|
4440
|
-
break;
|
|
4441
|
-
case "nonoptional":
|
|
4442
|
-
typeAccessor = generateNonOptionalCheck(doc, ctx, schema, accessor);
|
|
4443
|
-
break;
|
|
4444
|
-
case "tuple":
|
|
4445
|
-
typeAccessor = generateTupleCheck(doc, ctx, schema, accessor);
|
|
4446
|
-
break;
|
|
4447
|
-
case "union":
|
|
4448
|
-
typeAccessor = generateUnionCheck(doc, ctx, schema, accessor);
|
|
4449
|
-
break;
|
|
4450
|
-
case "intersection":
|
|
4451
|
-
typeAccessor = generateIntersectionCheck(doc, ctx, schema, accessor);
|
|
4452
|
-
break;
|
|
4453
|
-
case "record":
|
|
4454
|
-
typeAccessor = generateRecordCheck(doc, ctx, schema, accessor);
|
|
4455
|
-
break;
|
|
4456
|
-
case "map":
|
|
4457
|
-
typeAccessor = generateMapCheck(doc, ctx, schema, accessor);
|
|
4458
|
-
break;
|
|
4459
|
-
case "set":
|
|
4460
|
-
typeAccessor = generateSetCheck(doc, ctx, schema, accessor);
|
|
4461
|
-
break;
|
|
4462
|
-
case "file":
|
|
4463
|
-
typeAccessor = generateFileCheck(doc, accessor);
|
|
4464
|
-
break;
|
|
4465
|
-
case "template_literal":
|
|
4466
|
-
typeAccessor = generateTemplateLiteralCheck(doc, ctx, schema, accessor);
|
|
4467
|
-
break;
|
|
4468
|
-
case "lazy":
|
|
4469
|
-
typeAccessor = generateLazyCheck(doc, ctx, schema, accessor);
|
|
4470
|
-
break;
|
|
4471
|
-
case "pipe":
|
|
4472
|
-
typeAccessor = generatePipeCheck(doc, ctx, schema, accessor);
|
|
4473
|
-
break;
|
|
4474
|
-
case "custom":
|
|
4475
|
-
typeAccessor = generateCustomCheck(doc, ctx, schema, accessor);
|
|
4476
|
-
break;
|
|
4477
|
-
case "transform":
|
|
4478
|
-
typeAccessor = generateTransformCheck(doc, ctx, schema, accessor);
|
|
4479
|
-
break;
|
|
4480
|
-
case "catch":
|
|
4481
|
-
typeAccessor = generateCatchCheck(doc, ctx, schema, accessor);
|
|
4482
|
-
break;
|
|
4483
|
-
default: {
|
|
4484
|
-
throw new ZodCompileUnsupportedError(`schema type ${type}`);
|
|
4485
|
-
}
|
|
4486
|
-
}
|
|
4487
|
-
if (typeAccessor === null)
|
|
4488
|
-
return null;
|
|
4489
|
-
return generateChecks(doc, ctx, schema, typeAccessor);
|
|
4490
|
-
}
|
|
4491
|
-
function generateStringCheck(doc, ctx, schema, accessor, needsValue = true) {
|
|
4492
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
4493
|
-
const def = schema._zod.def;
|
|
4494
|
-
if (def.format === undefined)
|
|
4495
|
-
return accessor;
|
|
4496
|
-
return generateStringFormatCheck(doc, ctx, def, accessor, needsValue);
|
|
4497
|
-
}
|
|
4498
|
-
function generateNumberCheck(doc, schema, accessor) {
|
|
4499
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isFinite(${accessor})) return INVALID;`);
|
|
4500
|
-
const def = schema._zod.def;
|
|
4501
|
-
if (def.check === "number_format" && def.format) {
|
|
4502
|
-
generateNumberFormatCheck(doc, { format: def.format }, accessor);
|
|
4503
|
-
}
|
|
4504
|
-
return accessor;
|
|
4505
|
-
}
|
|
4506
|
-
function generateBooleanCheck(doc, accessor) {
|
|
4507
|
-
doc.write(`if (typeof ${accessor} !== "boolean") return INVALID;`);
|
|
4508
|
-
return accessor;
|
|
4509
|
-
}
|
|
4510
|
-
function generateBigIntCheck(doc, schema, accessor) {
|
|
4511
|
-
doc.write(`if (typeof ${accessor} !== "bigint") return INVALID;`);
|
|
4512
|
-
const def = schema._zod.def;
|
|
4513
|
-
if (def.format) {
|
|
4514
|
-
switch (def.format) {
|
|
4515
|
-
case "int64":
|
|
4516
|
-
doc.write(`if (${accessor} < -9223372036854775808n || ${accessor} > 9223372036854775807n) return INVALID;`);
|
|
4517
|
-
break;
|
|
4518
|
-
case "uint64":
|
|
4519
|
-
doc.write(`if (${accessor} < 0n || ${accessor} > 18446744073709551615n) return INVALID;`);
|
|
4520
|
-
break;
|
|
4521
|
-
}
|
|
4522
|
-
}
|
|
4523
|
-
return accessor;
|
|
4524
|
-
}
|
|
4525
|
-
function generateSymbolCheck(doc, accessor) {
|
|
4526
|
-
doc.write(`if (typeof ${accessor} !== "symbol") return INVALID;`);
|
|
4527
|
-
return accessor;
|
|
4528
|
-
}
|
|
4529
|
-
function generateUndefinedCheck(doc, accessor) {
|
|
4530
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4531
|
-
return accessor;
|
|
4532
|
-
}
|
|
4533
|
-
function generateNullCheck(doc, accessor) {
|
|
4534
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4535
|
-
return accessor;
|
|
4536
|
-
}
|
|
4537
|
-
function generateVoidCheck(doc, accessor) {
|
|
4538
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4539
|
-
return accessor;
|
|
4540
|
-
}
|
|
4541
|
-
function generateNaNCheck(doc, accessor) {
|
|
4542
|
-
doc.write(`if (typeof ${accessor} !== "number" || !Number.isNaN(${accessor})) return INVALID;`);
|
|
4543
|
-
return accessor;
|
|
4544
|
-
}
|
|
4545
|
-
function generateDateCheck(doc, accessor) {
|
|
4546
|
-
doc.write(`if (!(${accessor} instanceof Date) || Number.isNaN(${accessor}.getTime())) return INVALID;`);
|
|
4547
|
-
return accessor;
|
|
4548
|
-
}
|
|
4549
|
-
function generateObjectCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4550
|
-
const def = schema._zod.def;
|
|
4551
|
-
doc.write(`if (typeof ${accessor} !== "object" || ${accessor} === null || Array.isArray(${accessor})) return INVALID;`);
|
|
4552
|
-
const shape = def.shape;
|
|
4553
|
-
const keys = Object.keys(shape);
|
|
4554
|
-
const symbolKeys = Object.getOwnPropertySymbols(shape);
|
|
4555
|
-
const allKeys = symbolKeys.length ? [...keys, ...symbolKeys] : keys;
|
|
4556
|
-
const keyExpr = (k) => typeof k === "symbol" ? addConstant(ctx, k) : esc(k);
|
|
4557
|
-
const propKey = (k) => typeof k === "symbol" ? `[${keyExpr(k)}]` : esc(k);
|
|
4558
|
-
const propShape = shape;
|
|
4559
|
-
if (keys.includes("__proto__")) {
|
|
4560
|
-
throw new ZodCompileUnsupportedError('object shape key "__proto__"');
|
|
4561
|
-
}
|
|
4562
|
-
const propOutputs = new Map;
|
|
4563
|
-
for (const key of allKeys) {
|
|
4564
|
-
const propSchema = propShape[key];
|
|
4565
|
-
const kx = keyExpr(key);
|
|
4566
|
-
const inputVar = newVar(ctx);
|
|
4567
|
-
doc.write(`const ${inputVar} = ${accessor}[${kx}];`);
|
|
4568
|
-
if (propSchema._zod.optin !== undefined) {
|
|
4569
|
-
const outputVar = newVar(ctx);
|
|
4570
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
4571
|
-
doc.indented((d) => {
|
|
4572
|
-
const outputAccessor = compileChild(d, ctx, propSchema, inputVar);
|
|
4573
|
-
d.write(`return ${outputAccessor};`);
|
|
4574
|
-
});
|
|
4575
|
-
doc.write(`})();`);
|
|
4576
|
-
if (propSchema._zod.optout === "optional") {
|
|
4577
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
4578
|
-
doc.indented((d) => {
|
|
4579
|
-
d.write(`if (${kx} in ${accessor}) return INVALID;`);
|
|
4580
|
-
d.write(`${outputVar} = undefined;`);
|
|
4581
|
-
});
|
|
4582
|
-
doc.write(`}`);
|
|
4583
|
-
} else {
|
|
4584
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
4585
|
-
}
|
|
4586
|
-
propOutputs.set(key, outputVar);
|
|
4587
|
-
} else {
|
|
4588
|
-
if (requiresPresenceCheck(propSchema)) {
|
|
4589
|
-
doc.write(`if (!(${kx} in ${accessor})) return INVALID;`);
|
|
4590
|
-
}
|
|
4591
|
-
const outputAccessor = compileChild(doc, ctx, propSchema, inputVar, buildsValue);
|
|
4592
|
-
if (outputAccessor !== null)
|
|
4593
|
-
propOutputs.set(key, outputAccessor);
|
|
4594
|
-
}
|
|
4595
|
-
}
|
|
4596
|
-
const catchall = def.catchall;
|
|
4597
|
-
let unknownKeysMode = "none";
|
|
4598
|
-
if (catchall) {
|
|
4599
|
-
const catchallType = catchall._zod.def.type;
|
|
4600
|
-
if (catchallType === "never") {
|
|
4601
|
-
const condition = keys.map((k) => `k !== ${esc(k)}`).join(" && ") || "true";
|
|
4602
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4603
|
-
doc.indented((d) => {
|
|
4604
|
-
d.write(`if (${condition}) return INVALID;`);
|
|
4605
|
-
});
|
|
4606
|
-
doc.write(`}`);
|
|
4607
|
-
} else if ((catchallType === "unknown" || catchallType === "any") && !catchall._zod.def.checks?.length) {
|
|
4608
|
-
unknownKeysMode = "passthrough";
|
|
4609
|
-
} else {
|
|
4610
|
-
unknownKeysMode = "schema";
|
|
4611
|
-
}
|
|
4612
|
-
}
|
|
4613
|
-
const outputVar = newVar(ctx);
|
|
4614
|
-
const hasConditionalKeys = allKeys.some((k) => mayOmitUndefined(propShape[k]) || dropsWhenAbsent(propShape[k]));
|
|
4615
|
-
if (!buildsValue) {
|
|
4616
|
-
if (unknownKeysMode === "schema") {
|
|
4617
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4618
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4619
|
-
doc.indented((d) => {
|
|
4620
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4621
|
-
if (knownSet)
|
|
4622
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4623
|
-
const valVar = newVar(ctx);
|
|
4624
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4625
|
-
compileChild(d, ctx, catchall, valVar, false);
|
|
4626
|
-
});
|
|
4627
|
-
doc.write(`}`);
|
|
4628
|
-
}
|
|
4629
|
-
return null;
|
|
4630
|
-
}
|
|
4631
|
-
if (!hasConditionalKeys) {
|
|
4632
|
-
const propLiterals = allKeys.map((k) => `${propKey(k)}: ${propOutputs.get(k)}`).join(", ");
|
|
4633
|
-
doc.write(`const ${outputVar} = { ${propLiterals} };`);
|
|
4634
|
-
} else {
|
|
4635
|
-
doc.write(`const ${outputVar} = {};`);
|
|
4636
|
-
for (const k of allKeys) {
|
|
4637
|
-
const kx = keyExpr(k);
|
|
4638
|
-
const out = propOutputs.get(k);
|
|
4639
|
-
if (dropsWhenAbsent(propShape[k])) {
|
|
4640
|
-
doc.write(`if (${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4641
|
-
} else if (mayOmitUndefined(propShape[k])) {
|
|
4642
|
-
doc.write(`if (${out} !== undefined || ${kx} in ${accessor}) ${outputVar}[${kx}] = ${out};`);
|
|
4643
|
-
} else {
|
|
4644
|
-
doc.write(`${outputVar}[${kx}] = ${out};`);
|
|
4645
|
-
}
|
|
4646
|
-
}
|
|
4647
|
-
}
|
|
4648
|
-
if (unknownKeysMode !== "none") {
|
|
4649
|
-
const knownSet = keys.length > 0 ? addConstant(ctx, new Set(keys)) : null;
|
|
4650
|
-
doc.write(`for (const k in ${accessor}) {`);
|
|
4651
|
-
doc.indented((d) => {
|
|
4652
|
-
d.write(`if (k === "__proto__") continue;`);
|
|
4653
|
-
if (knownSet)
|
|
4654
|
-
d.write(`if (${knownSet}.has(k)) continue;`);
|
|
4655
|
-
if (unknownKeysMode === "passthrough") {
|
|
4656
|
-
d.write(`${outputVar}[k] = ${accessor}[k];`);
|
|
4657
|
-
} else {
|
|
4658
|
-
const valVar = newVar(ctx);
|
|
4659
|
-
d.write(`const ${valVar} = ${accessor}[k];`);
|
|
4660
|
-
const catchallOut = compileChild(d, ctx, catchall, valVar);
|
|
4661
|
-
d.write(`${outputVar}[k] = ${catchallOut};`);
|
|
4662
|
-
}
|
|
4663
|
-
});
|
|
4664
|
-
doc.write(`}`);
|
|
4665
|
-
}
|
|
4666
|
-
return outputVar;
|
|
4667
|
-
}
|
|
4668
|
-
function generateOptionalCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4669
|
-
const def = schema._zod.def;
|
|
4670
|
-
if (isExactOptional(schema)) {
|
|
4671
|
-
return generateCheck(doc, ctx, def.innerType, accessor, buildsValue);
|
|
4672
|
-
}
|
|
4673
|
-
if (def.innerType._zod.optin === "defaulted") {
|
|
4674
|
-
const outputVar = newVar(ctx);
|
|
4675
|
-
const branchVar = newVar(ctx);
|
|
4676
|
-
doc.write(`let ${outputVar};`);
|
|
4677
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4678
|
-
doc.indented((d) => {
|
|
4679
|
-
d.write(`const ${branchVar} = (() => {`);
|
|
4680
|
-
d.indented((d2) => {
|
|
4681
|
-
const innerOutput = generateCheck(d2, ctx, def.innerType, accessor);
|
|
4682
|
-
d2.write(`return ${innerOutput};`);
|
|
4683
|
-
});
|
|
4684
|
-
d.write(`})();`);
|
|
4685
|
-
d.write(`if (${branchVar} !== INVALID) ${outputVar} = ${branchVar};`);
|
|
4686
|
-
});
|
|
4687
|
-
doc.write(`} else {`);
|
|
4688
|
-
doc.indented((d) => {
|
|
4689
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4690
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4691
|
-
});
|
|
4692
|
-
doc.write(`}`);
|
|
4693
|
-
return outputVar;
|
|
4694
|
-
}
|
|
4695
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4696
|
-
if (outputVar)
|
|
4697
|
-
doc.write(`let ${outputVar};`);
|
|
4698
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4699
|
-
doc.indented((d) => {
|
|
4700
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4701
|
-
if (outputVar && innerOutput !== null)
|
|
4702
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4703
|
-
});
|
|
4704
|
-
doc.write(`}`);
|
|
4705
|
-
return outputVar;
|
|
4706
|
-
}
|
|
4707
|
-
function isExactOptional(schema) {
|
|
4708
|
-
return schema._zod.traits?.has("$ZodExactOptional") === true;
|
|
4709
|
-
}
|
|
4710
|
-
function requiresPresenceCheck(schema) {
|
|
4711
|
-
return schema._zod.optin === undefined && fastPathAcceptsAbsence(schema);
|
|
4712
|
-
}
|
|
4713
|
-
function fastPathAcceptsAbsence(schema) {
|
|
4714
|
-
if (schema._zod.def.coerce)
|
|
4715
|
-
return true;
|
|
4716
|
-
const def = schema._zod.def;
|
|
4717
|
-
switch (def.type) {
|
|
4718
|
-
case "any":
|
|
4719
|
-
case "unknown":
|
|
4720
|
-
case "undefined":
|
|
4721
|
-
case "void":
|
|
4722
|
-
case "default":
|
|
4723
|
-
case "prefault":
|
|
4724
|
-
case "transform":
|
|
4725
|
-
case "custom":
|
|
4726
|
-
case "lazy":
|
|
4727
|
-
return true;
|
|
4728
|
-
case "string":
|
|
4729
|
-
case "number":
|
|
4730
|
-
case "boolean":
|
|
4731
|
-
case "bigint":
|
|
4732
|
-
case "symbol":
|
|
4733
|
-
case "null":
|
|
4734
|
-
case "never":
|
|
4735
|
-
case "nan":
|
|
4736
|
-
case "date":
|
|
4737
|
-
case "object":
|
|
4738
|
-
case "array":
|
|
4739
|
-
case "tuple":
|
|
4740
|
-
case "record":
|
|
4741
|
-
case "map":
|
|
4742
|
-
case "set":
|
|
4743
|
-
case "file":
|
|
4744
|
-
case "template_literal":
|
|
4745
|
-
return false;
|
|
4746
|
-
case "nonoptional":
|
|
4747
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : false;
|
|
4748
|
-
case "literal":
|
|
4749
|
-
return !!def.values?.includes(undefined);
|
|
4750
|
-
case "enum":
|
|
4751
|
-
return !!schema._zod.values?.has(undefined);
|
|
4752
|
-
case "optional":
|
|
4753
|
-
case "nullable":
|
|
4754
|
-
case "readonly":
|
|
4755
|
-
case "success":
|
|
4756
|
-
return def.innerType ? fastPathAcceptsAbsence(def.innerType) : true;
|
|
4757
|
-
case "catch":
|
|
4758
|
-
return true;
|
|
4759
|
-
case "union":
|
|
4760
|
-
return def.options ? def.options.some(fastPathAcceptsAbsence) : true;
|
|
4761
|
-
case "intersection":
|
|
4762
|
-
if (!def.left || !def.right)
|
|
4763
|
-
return true;
|
|
4764
|
-
return fastPathAcceptsAbsence(def.left) && fastPathAcceptsAbsence(def.right);
|
|
4765
|
-
case "pipe":
|
|
4766
|
-
return def.in ? fastPathAcceptsAbsence(def.in) : true;
|
|
4767
|
-
default:
|
|
4768
|
-
return true;
|
|
4769
|
-
}
|
|
4770
|
-
}
|
|
4771
|
-
function dropsWhenAbsent(schema) {
|
|
4772
|
-
return schema._zod.optin === "optional" && schema._zod.optout === "optional";
|
|
4773
|
-
}
|
|
4774
|
-
function mayOmitUndefined(schema) {
|
|
4775
|
-
return (schema._zod.optin !== "defaulted" || schema._zod.optout === "optional") && mayOutputUndefined(schema);
|
|
4776
|
-
}
|
|
4777
|
-
function mayOutputUndefined(schema) {
|
|
4778
|
-
const def = schema._zod.def;
|
|
4779
|
-
switch (def.type) {
|
|
4780
|
-
case "string":
|
|
4781
|
-
case "number":
|
|
4782
|
-
case "boolean":
|
|
4783
|
-
case "bigint":
|
|
4784
|
-
case "symbol":
|
|
4785
|
-
case "null":
|
|
4786
|
-
case "nan":
|
|
4787
|
-
case "date":
|
|
4788
|
-
case "object":
|
|
4789
|
-
case "array":
|
|
4790
|
-
case "tuple":
|
|
4791
|
-
case "record":
|
|
4792
|
-
case "map":
|
|
4793
|
-
case "set":
|
|
4794
|
-
case "file":
|
|
4795
|
-
case "template_literal":
|
|
4796
|
-
case "never":
|
|
4797
|
-
case "success":
|
|
4798
|
-
return false;
|
|
4799
|
-
case "literal":
|
|
4800
|
-
return !!def.values?.includes(undefined);
|
|
4801
|
-
case "enum":
|
|
4802
|
-
return !!schema._zod.values?.has(undefined);
|
|
4803
|
-
case "optional":
|
|
4804
|
-
return true;
|
|
4805
|
-
case "nullable":
|
|
4806
|
-
case "readonly":
|
|
4807
|
-
case "nonoptional":
|
|
4808
|
-
return def.innerType ? mayOutputUndefined(def.innerType) : true;
|
|
4809
|
-
case "union":
|
|
4810
|
-
return def.options ? def.options.some(mayOutputUndefined) : true;
|
|
4811
|
-
case "intersection":
|
|
4812
|
-
return !def.left || !def.right || mayOutputUndefined(def.left) || mayOutputUndefined(def.right);
|
|
4813
|
-
case "pipe":
|
|
4814
|
-
return def.out ? mayOutputUndefined(def.out) : true;
|
|
4815
|
-
default:
|
|
4816
|
-
return true;
|
|
4817
|
-
}
|
|
4818
|
-
}
|
|
4819
|
-
function generateNullableCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4820
|
-
const def = schema._zod.def;
|
|
4821
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4822
|
-
if (outputVar)
|
|
4823
|
-
doc.write(`let ${outputVar} = null;`);
|
|
4824
|
-
doc.write(`if (${accessor} !== null) {`);
|
|
4825
|
-
doc.indented((d) => {
|
|
4826
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor, buildsValue);
|
|
4827
|
-
if (outputVar && innerOutput !== null)
|
|
4828
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4829
|
-
});
|
|
4830
|
-
doc.write(`}`);
|
|
4831
|
-
return outputVar;
|
|
4832
|
-
}
|
|
4833
|
-
function generateArrayCheck(doc, ctx, schema, accessor, buildsValue = true) {
|
|
4834
|
-
const def = schema._zod.def;
|
|
4835
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4836
|
-
const outputVar = buildsValue ? newVar(ctx) : null;
|
|
4837
|
-
const iVar = newVar(ctx);
|
|
4838
|
-
const elemVar = newVar(ctx);
|
|
4839
|
-
if (outputVar)
|
|
4840
|
-
doc.write(`const ${outputVar} = new Array(${accessor}.length);`);
|
|
4841
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4842
|
-
doc.indented((d) => {
|
|
4843
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
4844
|
-
const elemOutput = compileChild(d, ctx, def.element, elemVar, buildsValue);
|
|
4845
|
-
if (outputVar && elemOutput !== null)
|
|
4846
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
4847
|
-
});
|
|
4848
|
-
doc.write(`}`);
|
|
4849
|
-
return outputVar;
|
|
4850
|
-
}
|
|
4851
|
-
function generateLiteralCheck(doc, ctx, schema, accessor) {
|
|
4852
|
-
const def = schema._zod.def;
|
|
4853
|
-
const values = def.values;
|
|
4854
|
-
if (values.length !== 1) {
|
|
4855
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4856
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4857
|
-
return accessor;
|
|
4858
|
-
}
|
|
4859
|
-
const value = values[0];
|
|
4860
|
-
if (typeof value === "number" && Number.isNaN(value)) {
|
|
4861
|
-
const literalSet = addConstant(ctx, new Set(values));
|
|
4862
|
-
doc.write(`if (!${literalSet}.has(${accessor})) return INVALID;`);
|
|
4863
|
-
return accessor;
|
|
4864
|
-
}
|
|
4865
|
-
if (typeof value === "string") {
|
|
4866
|
-
doc.write(`if (${accessor} !== ${esc(value)}) return INVALID;`);
|
|
4867
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
4868
|
-
doc.write(`if (${accessor} !== ${value}) return INVALID;`);
|
|
4869
|
-
} else if (value === null) {
|
|
4870
|
-
doc.write(`if (${accessor} !== null) return INVALID;`);
|
|
4871
|
-
} else if (value === undefined) {
|
|
4872
|
-
doc.write(`if (${accessor} !== undefined) return INVALID;`);
|
|
4873
|
-
} else if (typeof value === "bigint") {
|
|
4874
|
-
doc.write(`if (${accessor} !== ${value}n) return INVALID;`);
|
|
4875
|
-
} else {
|
|
4876
|
-
throw new ZodCompileUnsupportedError(`literal type ${typeof value}`);
|
|
4877
|
-
}
|
|
4878
|
-
return accessor;
|
|
4879
|
-
}
|
|
4880
|
-
function generateEnumCheck(doc, ctx, schema, accessor) {
|
|
4881
|
-
const values = schema._zod.values;
|
|
4882
|
-
if (!values) {
|
|
4883
|
-
throw new ZodCompileUnsupportedError("enum schema without enumerated values");
|
|
4884
|
-
}
|
|
4885
|
-
const enumSet = addConstant(ctx, values);
|
|
4886
|
-
doc.write(`if (!${enumSet}.has(${accessor})) return INVALID;`);
|
|
4887
|
-
return accessor;
|
|
4888
|
-
}
|
|
4889
|
-
function generateWrapperCheck(doc, ctx, schema, accessor) {
|
|
4890
|
-
const def = schema._zod.def;
|
|
4891
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4892
|
-
}
|
|
4893
|
-
function generateDefaultCheck(doc, ctx, schema, accessor) {
|
|
4894
|
-
const def = schema._zod.def;
|
|
4895
|
-
const descriptor = Object.getOwnPropertyDescriptor(schema._zod.def, "defaultValue");
|
|
4896
|
-
const defaultGetter = descriptor ? () => schema._zod.def.defaultValue : undefined;
|
|
4897
|
-
if (schema._zod.def.type === "prefault") {
|
|
4898
|
-
if (!defaultGetter) {
|
|
4899
|
-
return generateCheck(doc, ctx, def.innerType, accessor);
|
|
4900
|
-
}
|
|
4901
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4902
|
-
const inputVar = newVar(ctx);
|
|
4903
|
-
doc.write(`let ${inputVar} = ${accessor};`);
|
|
4904
|
-
doc.write(`if (${accessor} === undefined) ${inputVar} = ${defaultFn}();`);
|
|
4905
|
-
return generateCheck(doc, ctx, def.innerType, inputVar);
|
|
4906
|
-
}
|
|
4907
|
-
const outputVar = newVar(ctx);
|
|
4908
|
-
if (defaultGetter) {
|
|
4909
|
-
const defaultFn = addConstant(ctx, defaultGetter);
|
|
4910
|
-
const cloneFn = addConstant(ctx, shallowClone);
|
|
4911
|
-
doc.write(`let ${outputVar};`);
|
|
4912
|
-
doc.write(`if (${accessor} === undefined) {`);
|
|
4913
|
-
doc.indented((d) => {
|
|
4914
|
-
d.write(`${outputVar} = ${cloneFn}(${defaultFn}());`);
|
|
4915
|
-
});
|
|
4916
|
-
doc.write(`} else {`);
|
|
4917
|
-
doc.indented((d) => {
|
|
4918
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4919
|
-
d.write(`${outputVar} = ${innerOutput} === undefined ? ${cloneFn}(${defaultFn}()) : ${innerOutput};`);
|
|
4920
|
-
});
|
|
4921
|
-
doc.write(`}`);
|
|
4922
|
-
} else {
|
|
4923
|
-
doc.write(`let ${outputVar};`);
|
|
4924
|
-
doc.write(`if (${accessor} !== undefined) {`);
|
|
4925
|
-
doc.indented((d) => {
|
|
4926
|
-
const innerOutput = generateCheck(d, ctx, def.innerType, accessor);
|
|
4927
|
-
d.write(`${outputVar} = ${innerOutput};`);
|
|
4928
|
-
});
|
|
4929
|
-
doc.write(`}`);
|
|
4930
|
-
}
|
|
4931
|
-
return outputVar;
|
|
4932
|
-
}
|
|
4933
|
-
function generateNonOptionalCheck(doc, ctx, schema, accessor) {
|
|
4934
|
-
const def = schema._zod.def;
|
|
4935
|
-
const innerOutput = generateCheck(doc, ctx, def.innerType, accessor);
|
|
4936
|
-
const outputVar = newVar(ctx);
|
|
4937
|
-
doc.write(`const ${outputVar} = ${innerOutput};`);
|
|
4938
|
-
doc.write(`if (${outputVar} === undefined) return INVALID;`);
|
|
4939
|
-
return outputVar;
|
|
4940
|
-
}
|
|
4941
|
-
function generateTupleCheck(doc, ctx, schema, accessor) {
|
|
4942
|
-
const def = schema._zod.def;
|
|
4943
|
-
const items = def.items;
|
|
4944
|
-
const rest = def.rest;
|
|
4945
|
-
doc.write(`if (!Array.isArray(${accessor})) return INVALID;`);
|
|
4946
|
-
const optinStart = getTupleOptStart(items, "optin");
|
|
4947
|
-
const optoutStart = getTupleOptStart(items, "optout");
|
|
4948
|
-
if (rest) {
|
|
4949
|
-
doc.write(`if (${accessor}.length < ${optinStart}) return INVALID;`);
|
|
4950
|
-
} else {
|
|
4951
|
-
doc.write(`if (${accessor}.length < ${optinStart} || ${accessor}.length > ${items.length}) return INVALID;`);
|
|
4952
|
-
}
|
|
4953
|
-
const outputVar = newVar(ctx);
|
|
4954
|
-
doc.write(`const ${outputVar} = [];`);
|
|
4955
|
-
for (let i = 0;i < items.length; i++) {
|
|
4956
|
-
const itemSchema = items[i];
|
|
4957
|
-
if (i >= optoutStart) {
|
|
4958
|
-
doc.write(`if (${outputVar}.length === ${i}) {`);
|
|
4959
|
-
doc.indented((d) => {
|
|
4960
|
-
d.write(`if (${i} < ${accessor}.length) {`);
|
|
4961
|
-
d.indented((d2) => {
|
|
4962
|
-
const elemVar = newVar(ctx);
|
|
4963
|
-
d2.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4964
|
-
const elemOutput = compileChild(d2, ctx, itemSchema, elemVar);
|
|
4965
|
-
d2.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4966
|
-
});
|
|
4967
|
-
d.write(`} else {`);
|
|
4968
|
-
d.indented((d2) => {
|
|
4969
|
-
if (dropsWhenAbsent(itemSchema)) {
|
|
4970
|
-
d2.write(`${outputVar}.length = ${i};`);
|
|
4971
|
-
return;
|
|
4972
|
-
}
|
|
4973
|
-
const elemVar = newVar(ctx);
|
|
4974
|
-
const branchVar = newVar(ctx);
|
|
4975
|
-
d2.write(`const ${elemVar} = undefined;`);
|
|
4976
|
-
d2.write(`const ${branchVar} = (() => {`);
|
|
4977
|
-
d2.indented((d3) => {
|
|
4978
|
-
const elemOutput = compileChild(d3, ctx, itemSchema, elemVar);
|
|
4979
|
-
d3.write(`return ${elemOutput};`);
|
|
4980
|
-
});
|
|
4981
|
-
d2.write(`})();`);
|
|
4982
|
-
d2.write(`if (${branchVar} === INVALID || ${branchVar} === undefined) ${outputVar}.length = ${i};`);
|
|
4983
|
-
d2.write(`else ${outputVar}[${i}] = ${branchVar};`);
|
|
4984
|
-
});
|
|
4985
|
-
d.write(`}`);
|
|
4986
|
-
});
|
|
4987
|
-
doc.write(`}`);
|
|
4988
|
-
} else {
|
|
4989
|
-
const elemVar = newVar(ctx);
|
|
4990
|
-
doc.write(`const ${elemVar} = ${accessor}[${i}];`);
|
|
4991
|
-
const elemOutput = compileChild(doc, ctx, itemSchema, elemVar);
|
|
4992
|
-
doc.write(`${outputVar}[${i}] = ${elemOutput};`);
|
|
4993
|
-
}
|
|
4994
|
-
}
|
|
4995
|
-
if (rest) {
|
|
4996
|
-
const iVar = newVar(ctx);
|
|
4997
|
-
const elemVar = newVar(ctx);
|
|
4998
|
-
doc.write(`for (let ${iVar} = ${items.length}; ${iVar} < ${accessor}.length; ${iVar}++) {`);
|
|
4999
|
-
doc.indented((d) => {
|
|
5000
|
-
d.write(`const ${elemVar} = ${accessor}[${iVar}];`);
|
|
5001
|
-
const elemOutput = compileChild(d, ctx, rest, elemVar);
|
|
5002
|
-
d.write(`${outputVar}[${iVar}] = ${elemOutput};`);
|
|
5003
|
-
});
|
|
5004
|
-
doc.write(`}`);
|
|
5005
|
-
}
|
|
5006
|
-
return outputVar;
|
|
5007
|
-
}
|
|
5008
|
-
function getTupleOptStart(items, key) {
|
|
5009
|
-
for (let i = items.length - 1;i >= 0; i--) {
|
|
5010
|
-
const omittable = key === "optin" ? items[i]._zod.optin !== undefined : items[i]._zod.optout === "optional";
|
|
5011
|
-
if (!omittable)
|
|
5012
|
-
return i + 1;
|
|
5013
|
-
}
|
|
5014
|
-
return 0;
|
|
5015
|
-
}
|
|
5016
|
-
function generateUnionCheck(doc, ctx, schema, accessor) {
|
|
5017
|
-
const def = schema._zod.def;
|
|
5018
|
-
const options = def.options;
|
|
5019
|
-
if (def.discriminator) {
|
|
5020
|
-
return generateDiscriminatedUnionCheck(doc, ctx, def, accessor);
|
|
5021
|
-
}
|
|
5022
|
-
if (def.inclusive === false) {
|
|
5023
|
-
throw new ZodCompileUnsupportedError("exclusive unions (z.xor)");
|
|
5024
|
-
}
|
|
5025
|
-
if (options.length === 0) {
|
|
5026
|
-
doc.write("return INVALID;");
|
|
5027
|
-
return accessor;
|
|
5028
|
-
}
|
|
5029
|
-
if (options.length === 1) {
|
|
5030
|
-
return generateCheck(doc, ctx, options[0], accessor);
|
|
5031
|
-
}
|
|
5032
|
-
const allLiterals = options.every((opt) => opt._zod.def.type === "literal" && !opt._zod.def.checks?.length);
|
|
5033
|
-
if (allLiterals) {
|
|
5034
|
-
const values = new Set(options.flatMap((opt) => opt._zod.def.values));
|
|
5035
|
-
const valuesConst = addConstant(ctx, values);
|
|
5036
|
-
doc.write(`if (!${valuesConst}.has(${accessor})) return INVALID;`);
|
|
5037
|
-
return accessor;
|
|
5038
|
-
}
|
|
5039
|
-
const outputVar = newVar(ctx);
|
|
5040
|
-
doc.write(`let ${outputVar};`);
|
|
5041
|
-
for (let i = 0;i < options.length; i++) {
|
|
5042
|
-
const opt = options[i];
|
|
5043
|
-
if (i === 0) {
|
|
5044
|
-
doc.write(`${outputVar} = (() => {`);
|
|
5045
|
-
} else {
|
|
5046
|
-
doc.write(`if (${outputVar} === INVALID) ${outputVar} = (() => {`);
|
|
5047
|
-
}
|
|
5048
|
-
doc.indented((d) => {
|
|
5049
|
-
const branchOutput = generateCheck(d, ctx, opt, accessor);
|
|
5050
|
-
d.write(`return ${branchOutput};`);
|
|
5051
|
-
});
|
|
5052
|
-
doc.write(`})();`);
|
|
5053
|
-
}
|
|
5054
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5055
|
-
return outputVar;
|
|
5056
|
-
}
|
|
5057
|
-
function generateDiscriminatedUnionCheck(doc, ctx, def, accessor) {
|
|
5058
|
-
if (def.unionFallback) {
|
|
5059
|
-
throw new ZodCompileUnsupportedError("discriminated union with unionFallback");
|
|
5060
|
-
}
|
|
5061
|
-
if (def.options.length === 0) {
|
|
5062
|
-
doc.write("return INVALID;");
|
|
5063
|
-
return accessor;
|
|
5064
|
-
}
|
|
5065
|
-
const discVar = newVar(ctx);
|
|
5066
|
-
const outputVar = newVar(ctx);
|
|
5067
|
-
doc.write(`const ${discVar} = ${accessor}?.[${esc(def.discriminator)}];`);
|
|
5068
|
-
doc.write(`let ${outputVar};`);
|
|
5069
|
-
let firstBranch = true;
|
|
5070
|
-
const claimed = new Set;
|
|
5071
|
-
for (const option of def.options) {
|
|
5072
|
-
const values = option._zod.propValues?.[def.discriminator];
|
|
5073
|
-
if (!values || values.size === 0) {
|
|
5074
|
-
throw new ZodCompileUnsupportedError("discriminated union option without static discriminator values");
|
|
5075
|
-
}
|
|
5076
|
-
for (const value of values) {
|
|
5077
|
-
if (claimed.has(value)) {
|
|
5078
|
-
throw new ZodCompileUnsupportedError(`duplicate discriminator value ${String(value)}`);
|
|
5079
|
-
}
|
|
5080
|
-
claimed.add(value);
|
|
5081
|
-
}
|
|
5082
|
-
const conditions = Array.from(values, (value) => literalEquality(ctx, discVar, value));
|
|
5083
|
-
const prefix = firstBranch ? "if" : "else if";
|
|
5084
|
-
doc.write(`${prefix} (${conditions.join(" || ")}) {`);
|
|
5085
|
-
doc.indented((d) => {
|
|
5086
|
-
const branchOutput = generateCheck(d, ctx, option, accessor);
|
|
5087
|
-
d.write(`${outputVar} = ${branchOutput};`);
|
|
5088
|
-
});
|
|
5089
|
-
doc.write(`}`);
|
|
5090
|
-
firstBranch = false;
|
|
5091
|
-
}
|
|
5092
|
-
doc.write(`else { return INVALID; }`);
|
|
5093
|
-
return outputVar;
|
|
5094
|
-
}
|
|
5095
|
-
function literalEquality(ctx, accessor, value) {
|
|
5096
|
-
if (typeof value === "string")
|
|
5097
|
-
return `${accessor} === ${esc(value)}`;
|
|
5098
|
-
if (typeof value === "number") {
|
|
5099
|
-
if (Number.isNaN(value))
|
|
5100
|
-
return `Number.isNaN(${accessor})`;
|
|
5101
|
-
return `${accessor} === ${value}`;
|
|
5102
|
-
}
|
|
5103
|
-
if (typeof value === "boolean")
|
|
5104
|
-
return `${accessor} === ${value}`;
|
|
5105
|
-
if (value === null)
|
|
5106
|
-
return `${accessor} === null`;
|
|
5107
|
-
if (value === undefined)
|
|
5108
|
-
return `${accessor} === undefined`;
|
|
5109
|
-
if (typeof value === "bigint")
|
|
5110
|
-
return `${accessor} === ${value}n`;
|
|
5111
|
-
if (typeof value === "symbol") {
|
|
5112
|
-
const symbolConst = addConstant(ctx, value);
|
|
5113
|
-
return `${accessor} === ${symbolConst}`;
|
|
5114
|
-
}
|
|
5115
|
-
throw new ZodCompileUnsupportedError(`literal discriminator value ${String(value)}`);
|
|
5116
|
-
}
|
|
5117
|
-
function generateIntersectionCheck(doc, ctx, schema, accessor) {
|
|
5118
|
-
const def = schema._zod.def;
|
|
5119
|
-
ctx.definite = false;
|
|
5120
|
-
const leftOutput = compileChild(doc, ctx, def.left, accessor);
|
|
5121
|
-
const rightOutput = compileChild(doc, ctx, def.right, accessor);
|
|
5122
|
-
const mergeConst = addConstant(ctx, mergeValues);
|
|
5123
|
-
const mergedVar = newVar(ctx);
|
|
5124
|
-
doc.write(`const ${mergedVar} = ${mergeConst}(${leftOutput}, ${rightOutput});`);
|
|
5125
|
-
doc.write(`if (!${mergedVar}.valid) return INVALID;`);
|
|
5126
|
-
return `${mergedVar}.data`;
|
|
5127
|
-
}
|
|
5128
|
-
function generateRecordCheck(doc, ctx, schema, accessor) {
|
|
5129
|
-
const def = schema._zod.def;
|
|
5130
|
-
const isPlainObjectConst = addConstant(ctx, isPlainObject);
|
|
5131
|
-
doc.write(`if (!${isPlainObjectConst}(${accessor})) return INVALID;`);
|
|
5132
|
-
const outputVar = newVar(ctx);
|
|
5133
|
-
const kVar = newVar(ctx);
|
|
5134
|
-
const valVar = newVar(ctx);
|
|
5135
|
-
doc.write(`const ${outputVar} = {};`);
|
|
5136
|
-
const recordDef = def;
|
|
5137
|
-
const keyValues = recordDef.partial ? undefined : def.keyType._zod.values;
|
|
5138
|
-
if (keyValues) {
|
|
5139
|
-
const inputKeys = [];
|
|
5140
|
-
for (const key of keyValues) {
|
|
5141
|
-
if (!(typeof key === "string" || typeof key === "number" || typeof key === "symbol")) {
|
|
5142
|
-
throw new ZodCompileUnsupportedError(`record key value ${String(key)}`);
|
|
5143
|
-
}
|
|
5144
|
-
const inputKey = typeof key === "number" ? key.toString() : key;
|
|
5145
|
-
if (inputKey === "__proto__") {
|
|
5146
|
-
throw new ZodCompileUnsupportedError('record key "__proto__"');
|
|
5147
|
-
}
|
|
5148
|
-
inputKeys.push(inputKey);
|
|
5149
|
-
const keyConst = addConstant(ctx, key);
|
|
5150
|
-
const outKey = generateCheck(doc, ctx, def.keyType, keyConst);
|
|
5151
|
-
const valueVar = newVar(ctx);
|
|
5152
|
-
doc.write(`const ${valueVar} = ${accessor}[${literalPropertyKey(ctx, inputKey)}];`);
|
|
5153
|
-
const valOutput = compileChild(doc, ctx, def.valueType, valueVar);
|
|
5154
|
-
doc.write(`${outputVar}[${outKey}] = ${valOutput};`);
|
|
5155
|
-
}
|
|
5156
|
-
const knownKeysConst = addConstant(ctx, new Set(inputKeys));
|
|
5157
|
-
doc.write(`for (const ${kVar} in ${accessor}) {`);
|
|
5158
|
-
doc.indented((d) => {
|
|
5159
|
-
d.write(`if (${knownKeysConst}.has(${kVar})) continue;`);
|
|
5160
|
-
if (recordDef.mode === "loose") {
|
|
5161
|
-
d.write(`if (${kVar} !== "__proto__") ${outputVar}[${kVar}] = ${accessor}[${kVar}];`);
|
|
5162
|
-
} else {
|
|
5163
|
-
d.write(`return INVALID;`);
|
|
5164
|
-
}
|
|
5165
|
-
});
|
|
5166
|
-
doc.write(`}`);
|
|
5167
|
-
return outputVar;
|
|
5168
|
-
}
|
|
5169
|
-
const keyDef = def.keyType._zod.def;
|
|
5170
|
-
const keyIsBareString = keyDef.type === "string" && keyDef.format === undefined && !keyDef.coerce && (keyDef.checks?.length ?? 0) === 0;
|
|
5171
|
-
if (!keyIsBareString) {
|
|
5172
|
-
const isLoose = def.mode === "loose";
|
|
5173
|
-
const keyFn = compileFn(def.keyType);
|
|
5174
|
-
if (keyFn.definite === false)
|
|
5175
|
-
ctx.definite = false;
|
|
5176
|
-
const keyFast = addConstant(ctx, keyFn);
|
|
5177
|
-
const numericConst = addConstant(ctx, number);
|
|
5178
|
-
const outKeyVar = newVar(ctx);
|
|
5179
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
5180
|
-
d.write(`let ${outKeyVar} = ${keyFast}(${kVar});`);
|
|
5181
|
-
d.write(`if (${outKeyVar} === INVALID && typeof ${kVar} === "string" && ${numericConst}.test(${kVar})) ${outKeyVar} = ${keyFast}(Number(${kVar}));`);
|
|
5182
|
-
if (isLoose) {
|
|
5183
|
-
d.write(`if (${outKeyVar} === INVALID) { ${outputVar}[${kVar}] = ${accessor}[${kVar}]; continue; }`);
|
|
5184
|
-
} else {
|
|
5185
|
-
d.write(`if (${outKeyVar} === INVALID) return INVALID;`);
|
|
5186
|
-
}
|
|
5187
|
-
d.write(`if (${outKeyVar} === "__proto__") continue;`);
|
|
5188
|
-
const valueVar = newVar(ctx);
|
|
5189
|
-
d.write(`const ${valueVar} = ${accessor}[${kVar}];`);
|
|
5190
|
-
const valOutput = compileChild(d, ctx, def.valueType, valueVar);
|
|
5191
|
-
d.write(`${outputVar}[${outKeyVar}] = ${valOutput};`);
|
|
5192
|
-
});
|
|
5193
|
-
return outputVar;
|
|
5194
|
-
}
|
|
5195
|
-
emitOwnKeys(doc, ctx, accessor, kVar, (d) => {
|
|
5196
|
-
d.write(`const ${valVar} = ${accessor}[${kVar}];`);
|
|
5197
|
-
const valOutput = compileChild(d, ctx, def.valueType, valVar);
|
|
5198
|
-
d.write(`${outputVar}[${kVar}] = ${valOutput};`);
|
|
5199
|
-
}, `return INVALID;`);
|
|
5200
|
-
return outputVar;
|
|
5201
|
-
}
|
|
5202
|
-
function emitOwnKeys(doc, ctx, accessor, kVar, body, onSymbol) {
|
|
5203
|
-
const propIsEnumerableConst = addConstant(ctx, Object.prototype.propertyIsEnumerable);
|
|
5204
|
-
const symsVar = newVar(ctx);
|
|
5205
|
-
const keysVar = newVar(ctx);
|
|
5206
|
-
const iVar = newVar(ctx);
|
|
5207
|
-
doc.write(`const ${symsVar} = Object.getOwnPropertySymbols(${accessor});`);
|
|
5208
|
-
doc.write(`const ${keysVar} = Object.getOwnPropertyNames(${accessor});`);
|
|
5209
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${keysVar}.length; ${iVar}++) {`);
|
|
5210
|
-
doc.indented((d) => {
|
|
5211
|
-
d.write(`const ${kVar} = ${keysVar}[${iVar}];`);
|
|
5212
|
-
d.write(`if (${kVar} === "__proto__" || !${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
5213
|
-
body(d);
|
|
5214
|
-
});
|
|
5215
|
-
doc.write(`}`);
|
|
5216
|
-
doc.write(`for (let ${iVar} = 0; ${iVar} < ${symsVar}.length; ${iVar}++) {`);
|
|
5217
|
-
doc.indented((d) => {
|
|
5218
|
-
d.write(`const ${kVar} = ${symsVar}[${iVar}];`);
|
|
5219
|
-
d.write(`if (!${propIsEnumerableConst}.call(${accessor}, ${kVar})) continue;`);
|
|
5220
|
-
if (onSymbol)
|
|
5221
|
-
d.write(onSymbol);
|
|
5222
|
-
else
|
|
5223
|
-
body(d);
|
|
5224
|
-
});
|
|
5225
|
-
doc.write(`}`);
|
|
5226
|
-
}
|
|
5227
|
-
function literalPropertyKey(ctx, key) {
|
|
5228
|
-
if (typeof key === "string")
|
|
5229
|
-
return esc(key);
|
|
5230
|
-
return addConstant(ctx, key);
|
|
5231
|
-
}
|
|
5232
|
-
function generateMapCheck(doc, ctx, schema, accessor) {
|
|
5233
|
-
const def = schema._zod.def;
|
|
5234
|
-
doc.write(`if (!(${accessor} instanceof Map)) return INVALID;`);
|
|
5235
|
-
const outputVar = newVar(ctx);
|
|
5236
|
-
const kVar = newVar(ctx);
|
|
5237
|
-
const valVar = newVar(ctx);
|
|
5238
|
-
doc.write(`const ${outputVar} = new Map();`);
|
|
5239
|
-
doc.write(`for (const [${kVar}, ${valVar}] of ${accessor}) {`);
|
|
5240
|
-
doc.indented((d) => {
|
|
5241
|
-
const keyOutput = generateCheck(d, ctx, def.keyType, kVar);
|
|
5242
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
5243
|
-
d.write(`${outputVar}.set(${keyOutput}, ${valOutput});`);
|
|
5244
|
-
});
|
|
5245
|
-
doc.write(`}`);
|
|
5246
|
-
return outputVar;
|
|
5247
|
-
}
|
|
5248
|
-
function generateSetCheck(doc, ctx, schema, accessor) {
|
|
5249
|
-
const def = schema._zod.def;
|
|
5250
|
-
doc.write(`if (!(${accessor} instanceof Set)) return INVALID;`);
|
|
5251
|
-
const outputVar = newVar(ctx);
|
|
5252
|
-
const valVar = newVar(ctx);
|
|
5253
|
-
doc.write(`const ${outputVar} = new Set();`);
|
|
5254
|
-
doc.write(`for (const ${valVar} of ${accessor}) {`);
|
|
5255
|
-
doc.indented((d) => {
|
|
5256
|
-
const valOutput = generateCheck(d, ctx, def.valueType, valVar);
|
|
5257
|
-
d.write(`${outputVar}.add(${valOutput});`);
|
|
5258
|
-
});
|
|
5259
|
-
doc.write(`}`);
|
|
5260
|
-
return outputVar;
|
|
5261
|
-
}
|
|
5262
|
-
function generateFileCheck(doc, accessor) {
|
|
5263
|
-
doc.write(`if (!(${accessor} instanceof File)) return INVALID;`);
|
|
5264
|
-
return accessor;
|
|
5265
|
-
}
|
|
5266
|
-
function generateTemplateLiteralCheck(doc, ctx, schema, accessor) {
|
|
5267
|
-
doc.write(`if (typeof ${accessor} !== "string") return INVALID;`);
|
|
5268
|
-
const pattern = schema._zod.pattern;
|
|
5269
|
-
if (pattern) {
|
|
5270
|
-
const patternConst = addConstant(ctx, pattern);
|
|
5271
|
-
doc.write(`${patternConst}.lastIndex = 0;`);
|
|
5272
|
-
doc.write(`if (!${patternConst}.test(${accessor})) return INVALID;`);
|
|
5273
|
-
}
|
|
5274
|
-
return accessor;
|
|
5275
|
-
}
|
|
5276
|
-
function generateLazyCheck(doc, ctx, schema, accessor) {
|
|
5277
|
-
const def = schema._zod.def;
|
|
5278
|
-
const getterConst = addUserConstant(ctx, def.getter);
|
|
5279
|
-
const cacheConst = addConstant(ctx, { parser: null });
|
|
5280
|
-
doc.write(`if (!${cacheConst}.parser) {`);
|
|
5281
|
-
doc.indented((d) => {
|
|
5282
|
-
d.write(`const inner = ${getterConst}();`);
|
|
5283
|
-
d.write(`${cacheConst}.parser = function(input) {`);
|
|
5284
|
-
d.indented((d2) => {
|
|
5285
|
-
d2.write(`const result = inner._zod.run({ value: input, issues: [] }, {});`);
|
|
5286
|
-
d2.write(`return result.issues.length === 0 ? result.value : INVALID;`);
|
|
5287
|
-
});
|
|
5288
|
-
d.write(`};`);
|
|
5289
|
-
});
|
|
5290
|
-
doc.write(`}`);
|
|
5291
|
-
const outputVar = newVar(ctx);
|
|
5292
|
-
doc.write(`const ${outputVar} = ${cacheConst}.parser(${accessor});`);
|
|
5293
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5294
|
-
return outputVar;
|
|
5295
|
-
}
|
|
5296
|
-
function generatePipeCheck(doc, ctx, schema, accessor) {
|
|
5297
|
-
const def = schema._zod.def;
|
|
5298
|
-
const inputOutput = generateCheck(doc, ctx, def.in, accessor);
|
|
5299
|
-
if (def.transform) {
|
|
5300
|
-
if (isAsyncFunction(def.transform)) {
|
|
5301
|
-
throw new ZodCompileAsyncError("z.compile: async transforms in pipes are not supported");
|
|
5302
|
-
}
|
|
5303
|
-
const transformFn = def.transform;
|
|
5304
|
-
const helperFn = (value) => {
|
|
5305
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
5306
|
-
const result = transformFn(value, fakePayload);
|
|
5307
|
-
if (result instanceof Promise)
|
|
5308
|
-
return INVALID;
|
|
5309
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
5310
|
-
};
|
|
5311
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
5312
|
-
const transformedVar = newVar(ctx);
|
|
5313
|
-
doc.write(`const ${transformedVar} = ${helperConst}(${inputOutput});`);
|
|
5314
|
-
doc.write(`if (${transformedVar} === INVALID) return INVALID;`);
|
|
5315
|
-
return generateCheck(doc, ctx, def.out, transformedVar);
|
|
5316
|
-
} else {
|
|
5317
|
-
return generateCheck(doc, ctx, def.out, inputOutput);
|
|
5318
|
-
}
|
|
5319
|
-
}
|
|
5320
|
-
function isAsyncFunction(fn) {
|
|
5321
|
-
return typeof fn === "function" && (fn.constructor.name === "AsyncFunction" || fn[Symbol.toStringTag] === "AsyncFunction");
|
|
5322
|
-
}
|
|
5323
|
-
function generateCustomCheck(doc, ctx, schema, accessor) {
|
|
5324
|
-
const def = schema._zod.def;
|
|
5325
|
-
if (def.fn) {
|
|
5326
|
-
if (isAsyncFunction(def.fn)) {
|
|
5327
|
-
throw new ZodCompileAsyncError("z.compile: async custom predicates are not supported");
|
|
5328
|
-
}
|
|
5329
|
-
const fnConst = addUserConstant(ctx, def.fn);
|
|
5330
|
-
const throwAsyncConst = addConstant(ctx, throwAsync);
|
|
5331
|
-
const resVar = newVar(ctx);
|
|
5332
|
-
doc.write(`const ${resVar} = ${fnConst}(${accessor});`);
|
|
5333
|
-
doc.write(`if (${resVar} instanceof Promise) ${throwAsyncConst}();`);
|
|
5334
|
-
doc.write(`if (!${resVar}) return INVALID;`);
|
|
5335
|
-
} else {
|
|
5336
|
-
throw new ZodCompileUnsupportedError("custom schema without a predicate function");
|
|
5337
|
-
}
|
|
5338
|
-
return accessor;
|
|
5339
|
-
}
|
|
5340
|
-
function runtimeCatch(innerSchema, catchValue, value) {
|
|
5341
|
-
const result = innerSchema._zod.run({ value, issues: [] }, {});
|
|
5342
|
-
if (result && typeof result.then === "function")
|
|
5343
|
-
return INVALID;
|
|
5344
|
-
const r = result;
|
|
5345
|
-
if (r.issues.length === 0)
|
|
5346
|
-
return r.value;
|
|
5347
|
-
return catchValue();
|
|
5348
|
-
}
|
|
5349
|
-
function generateCatchCheck(doc, ctx, schema, accessor) {
|
|
5350
|
-
const def = schema._zod.def;
|
|
5351
|
-
if (!def.catchValue[CONSTANT_CATCH]) {
|
|
5352
|
-
throw new ZodCompileUnsupportedError("catch with a callback (only a constant catch value compiles)", false);
|
|
5353
|
-
}
|
|
5354
|
-
const outputVar = newVar(ctx);
|
|
5355
|
-
doc.write(`let ${outputVar} = (() => {`);
|
|
5356
|
-
doc.indented((d) => {
|
|
5357
|
-
const innerOut = compileChild(d, ctx, def.innerType, accessor);
|
|
5358
|
-
d.write(`return ${innerOut};`);
|
|
5359
|
-
});
|
|
5360
|
-
doc.write(`})();`);
|
|
5361
|
-
const innerConst = addConstant(ctx, def.innerType);
|
|
5362
|
-
const catchConst = addUserConstant(ctx, def.catchValue);
|
|
5363
|
-
const catchHelperConst = addConstant(ctx, runtimeCatch);
|
|
5364
|
-
doc.write(`if (${outputVar} === INVALID) {`);
|
|
5365
|
-
doc.indented((d) => {
|
|
5366
|
-
d.write(`${outputVar} = ${catchHelperConst}(${innerConst}, ${catchConst}, ${accessor});`);
|
|
5367
|
-
d.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5368
|
-
});
|
|
5369
|
-
doc.write(`}`);
|
|
5370
|
-
return outputVar;
|
|
5371
|
-
}
|
|
5372
|
-
function generateTransformCheck(doc, ctx, schema, accessor) {
|
|
5373
|
-
const def = schema._zod.def;
|
|
5374
|
-
if (def.transform) {
|
|
5375
|
-
if (isAsyncFunction(def.transform)) {
|
|
5376
|
-
throw new ZodCompileAsyncError("z.compile: async transforms are not supported");
|
|
5377
|
-
}
|
|
5378
|
-
const transformFn = def.transform;
|
|
5379
|
-
const helperFn = (value) => {
|
|
5380
|
-
const fakePayload = { value, issues: [], addIssue: pushIssue };
|
|
5381
|
-
const result = transformFn(value, fakePayload);
|
|
5382
|
-
if (result instanceof Promise)
|
|
5383
|
-
return INVALID;
|
|
5384
|
-
return fakePayload.issues.length === 0 ? result : INVALID;
|
|
5385
|
-
};
|
|
5386
|
-
const helperConst = addUserConstant(ctx, helperFn);
|
|
5387
|
-
const outputVar = newVar(ctx);
|
|
5388
|
-
doc.write(`const ${outputVar} = ${helperConst}(${accessor});`);
|
|
5389
|
-
doc.write(`if (${outputVar} === INVALID) return INVALID;`);
|
|
5390
|
-
return outputVar;
|
|
5391
|
-
}
|
|
5392
|
-
return accessor;
|
|
5393
|
-
}
|
|
5394
|
-
var INVALID, FALLBACK_FLAG, ZodCompileAsyncError, ZodCompileUnsupportedError, WHEN_DEFAULTED_CHECKS, PATTERN_IS_COMPLETE;
|
|
3808
|
+
var INVALID, FALLBACK_FLAG, WHEN_DEFAULTED_CHECKS, PATTERN_IS_COMPLETE;
|
|
5395
3809
|
var init_compile = __esm(() => {
|
|
5396
|
-
init_core();
|
|
5397
|
-
init_memoizer();
|
|
5398
|
-
init_regexes();
|
|
5399
|
-
init_schemas();
|
|
5400
|
-
init_util();
|
|
5401
3810
|
INVALID = Symbol.for("zod.compile.invalid");
|
|
5402
3811
|
FALLBACK_FLAG = Symbol.for("zod.compile.fallback");
|
|
5403
|
-
ZodCompileAsyncError = class ZodCompileAsyncError extends Error {
|
|
5404
|
-
constructor(message = "z.compile does not support async refinements, transforms, or checks") {
|
|
5405
|
-
super(message);
|
|
5406
|
-
this.name = "ZodCompileAsyncError";
|
|
5407
|
-
}
|
|
5408
|
-
};
|
|
5409
|
-
ZodCompileUnsupportedError = class ZodCompileUnsupportedError extends Error {
|
|
5410
|
-
constructor(feature, islandable = true) {
|
|
5411
|
-
super(`z.compile does not support ${feature}; this schema must use the runtime parser`);
|
|
5412
|
-
this.name = "ZodCompileUnsupportedError";
|
|
5413
|
-
this.islandable = islandable;
|
|
5414
|
-
}
|
|
5415
|
-
};
|
|
5416
3812
|
WHEN_DEFAULTED_CHECKS = new Set([
|
|
5417
3813
|
"max_size",
|
|
5418
3814
|
"min_size",
|
|
@@ -7973,7 +6369,6 @@ var init_coerce = () => {};
|
|
|
7973
6369
|
|
|
7974
6370
|
// node_modules/zod/v4/classic/external.js
|
|
7975
6371
|
var init_external = __esm(() => {
|
|
7976
|
-
init_core2();
|
|
7977
6372
|
init_core2();
|
|
7978
6373
|
init_locales();
|
|
7979
6374
|
init_iso();
|
|
@@ -10655,7 +9050,7 @@ var package_default;
|
|
|
10655
9050
|
var init_package = __esm(() => {
|
|
10656
9051
|
package_default = {
|
|
10657
9052
|
name: "@brainervirus/workit-cli",
|
|
10658
|
-
version: "8.
|
|
9053
|
+
version: "8.23.0",
|
|
10659
9054
|
private: false,
|
|
10660
9055
|
description: "Workit CLI — setup wizard, doctor, and task control for agentic coding workflows",
|
|
10661
9056
|
keywords: [
|
|
@@ -10693,13 +9088,13 @@ var init_package = __esm(() => {
|
|
|
10693
9088
|
},
|
|
10694
9089
|
dependencies: {
|
|
10695
9090
|
"@babel/parser": "7.29.9",
|
|
10696
|
-
"@brainervirus/workit-cursor": "^8.
|
|
9091
|
+
"@brainervirus/workit-cursor": "^8.23.0",
|
|
10697
9092
|
"@inkjs/ui": "2.0.0",
|
|
10698
9093
|
ink: "7.1.1",
|
|
10699
9094
|
react: "19.2.8"
|
|
10700
9095
|
},
|
|
10701
9096
|
devDependencies: {
|
|
10702
|
-
"@brainervirus/workit-core": "^8.
|
|
9097
|
+
"@brainervirus/workit-core": "^8.23.0"
|
|
10703
9098
|
},
|
|
10704
9099
|
engines: {
|
|
10705
9100
|
node: ">=24"
|
|
@@ -10953,8 +9348,7 @@ function resolveIdRef(kind, ref, ids) {
|
|
|
10953
9348
|
function parseOperation(family, input) {
|
|
10954
9349
|
if (typeof input === "object" && input !== null && "schemaVersion" in input && input.schemaVersion !== SCHEMA_VERSION)
|
|
10955
9350
|
return failure2("unsupported_version", "unsupported schema version", { operation: family });
|
|
10956
|
-
const
|
|
10957
|
-
const parsed = compiled.success ? operationSchemas[family].safeParse(input) : compiled;
|
|
9351
|
+
const parsed = operationSchemas[family].safeParse(input);
|
|
10958
9352
|
if (parsed.success)
|
|
10959
9353
|
return success(null, null, parsed.data);
|
|
10960
9354
|
const fields = parsed.error.issues.map((issue) => ({
|
|
@@ -11202,7 +9596,7 @@ var SCHEMA_VERSION = 1, POLICY_VERSION = "1.0.0", OPERATION_FAMILIES, uuidPatter
|
|
|
11202
9596
|
if (critical.length > 0)
|
|
11203
9597
|
return { ...first, critical };
|
|
11204
9598
|
return { success: true, data: parsed.data, stripped: [...new Set(stripped)] };
|
|
11205
|
-
}, workspaceRecordSchema, capabilitySchema, requirementEvaluationSchema, taskViewSchema, taskSummarySchema, taskListItemSchema, exportBundleSchema, callerSchema, base, revisions, taskId, operation = (shape) => object({ ...base, ...shape }).strict(), taskOperations, rawJudgment, policyOperations, evidenceOperations, findingOperations, decisionOperations, workerOperations, stateOperations, operationSchemas, OPERATION_ACTIONS, withImplicitTask = (options) => Object.values(options).map((option) => ("taskId" in option.shape) ? option.extend({ taskId: id.optional() }) : option), advertised = (options) => discriminatedUnion("action", withImplicitTask(options)), advertisedOperationSchemas,
|
|
9599
|
+
}, workspaceRecordSchema, capabilitySchema, requirementEvaluationSchema, taskViewSchema, taskSummarySchema, taskListItemSchema, exportBundleSchema, callerSchema, base, revisions, taskId, operation = (shape) => object({ ...base, ...shape }).strict(), taskOperations, rawJudgment, policyOperations, evidenceOperations, findingOperations, decisionOperations, workerOperations, stateOperations, operationSchemas, OPERATION_ACTIONS, withImplicitTask = (options) => Object.values(options).map((option) => ("taskId" in option.shape) ? option.extend({ taskId: id.optional() }) : option), advertised = (options) => discriminatedUnion("action", withImplicitTask(options)), advertisedOperationSchemas, success = (revisionValue, workspaceRevisionValue, data) => ({
|
|
11206
9600
|
ok: true,
|
|
11207
9601
|
schemaVersion: 1,
|
|
11208
9602
|
revision: revisionValue,
|
|
@@ -11785,7 +10179,6 @@ var init_task_contract = __esm(() => {
|
|
|
11785
10179
|
worker: advertised(workerOperations),
|
|
11786
10180
|
state: advertised(stateOperations)
|
|
11787
10181
|
};
|
|
11788
|
-
compiledOperationSchemas = Object.fromEntries(OPERATION_FAMILIES.map((family) => [family, compile(operationSchemas[family])]));
|
|
11789
10182
|
});
|
|
11790
10183
|
|
|
11791
10184
|
// packages/workit-core/src/core/operation-input.ts
|
|
@@ -84535,10 +82928,176 @@ var init_stack2 = __esm(() => {
|
|
|
84535
82928
|
METHODS3 = new Set(["squash", "merge", "rebase"]);
|
|
84536
82929
|
});
|
|
84537
82930
|
|
|
82931
|
+
// packages/workit-core/src/core/live-session.ts
|
|
82932
|
+
import { spawnSync as spawnSync27 } from "node:child_process";
|
|
82933
|
+
var LIVE_SESSION_TTL_MS, liveProbe, PIDS;
|
|
82934
|
+
var init_live_session = __esm(() => {
|
|
82935
|
+
init_paths();
|
|
82936
|
+
init_logger();
|
|
82937
|
+
init_store_lock();
|
|
82938
|
+
LIVE_SESSION_TTL_MS = 15 * 60000;
|
|
82939
|
+
liveProbe = {
|
|
82940
|
+
platform: process.platform,
|
|
82941
|
+
run: (args) => spawnSync27("ps", args, { encoding: "utf8", timeout: 2000 }).stdout ?? "",
|
|
82942
|
+
start: (pid) => processStartOf(pid)
|
|
82943
|
+
};
|
|
82944
|
+
PIDS = new Map;
|
|
82945
|
+
});
|
|
82946
|
+
|
|
82947
|
+
// packages/workit-core/src/hooks/write-gate.ts
|
|
82948
|
+
import path55 from "node:path";
|
|
82949
|
+
var CASE_INSENSITIVE, PREFIXES2, SCRIPT_SHELLS, cache3, MUTATING_GIT, DRY_N, MUTATING_WORKIT_GIT, CD2, PUSHD2, POPD2, UNKNOWN_DIR;
|
|
82950
|
+
var init_write_gate = __esm(() => {
|
|
82951
|
+
init_task_store();
|
|
82952
|
+
init_derive();
|
|
82953
|
+
init_task_evaluation();
|
|
82954
|
+
init_live_session();
|
|
82955
|
+
init_logger();
|
|
82956
|
+
init_workspaces();
|
|
82957
|
+
init_ledger();
|
|
82958
|
+
init_raw_git();
|
|
82959
|
+
init_shell_words();
|
|
82960
|
+
CASE_INSENSITIVE = process.platform === "darwin" || process.platform === "win32";
|
|
82961
|
+
PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs", "busybox"]);
|
|
82962
|
+
SCRIPT_SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
|
|
82963
|
+
cache3 = new Map;
|
|
82964
|
+
MUTATING_GIT = new Set([
|
|
82965
|
+
"add",
|
|
82966
|
+
"am",
|
|
82967
|
+
"apply",
|
|
82968
|
+
"checkout",
|
|
82969
|
+
"cherry-pick",
|
|
82970
|
+
"clean",
|
|
82971
|
+
"commit",
|
|
82972
|
+
"merge",
|
|
82973
|
+
"mv",
|
|
82974
|
+
"pull",
|
|
82975
|
+
"rebase",
|
|
82976
|
+
"reset",
|
|
82977
|
+
"restore",
|
|
82978
|
+
"revert",
|
|
82979
|
+
"rm",
|
|
82980
|
+
"stash",
|
|
82981
|
+
"switch"
|
|
82982
|
+
]);
|
|
82983
|
+
DRY_N = new Set(["add", "clean", "rm", "mv"]);
|
|
82984
|
+
MUTATING_WORKIT_GIT = new Set(["commit", "branch"]);
|
|
82985
|
+
CD2 = new Set(["cd", "chdir", "set-location", "sl"]);
|
|
82986
|
+
PUSHD2 = new Set(["pushd", "push-location"]);
|
|
82987
|
+
POPD2 = new Set(["popd", "pop-location"]);
|
|
82988
|
+
UNKNOWN_DIR = path55.resolve(path55.parse(process.cwd()).root, ".workit-unknown-dir");
|
|
82989
|
+
});
|
|
82990
|
+
|
|
82991
|
+
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
82992
|
+
var FANOUT_TOKEN_FILE = "workit-fanout-token", CASE_INSENSITIVE2, MUTATING_REMOTE;
|
|
82993
|
+
var init_worktree_confine = __esm(() => {
|
|
82994
|
+
init_logger();
|
|
82995
|
+
init_ledger();
|
|
82996
|
+
init_raw_git();
|
|
82997
|
+
init_write_gate();
|
|
82998
|
+
CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
|
|
82999
|
+
MUTATING_REMOTE = new Set([
|
|
83000
|
+
"add",
|
|
83001
|
+
"remove",
|
|
83002
|
+
"rm",
|
|
83003
|
+
"rename",
|
|
83004
|
+
"set-url",
|
|
83005
|
+
"set-head",
|
|
83006
|
+
"set-branches",
|
|
83007
|
+
"prune"
|
|
83008
|
+
]);
|
|
83009
|
+
});
|
|
83010
|
+
|
|
83011
|
+
// packages/workit-core/src/worktree-deps.ts
|
|
83012
|
+
import fs31 from "node:fs";
|
|
83013
|
+
import path56 from "node:path";
|
|
83014
|
+
function moduleDirs(main) {
|
|
83015
|
+
const out = [];
|
|
83016
|
+
const walk = (relative, depth) => {
|
|
83017
|
+
let entries;
|
|
83018
|
+
try {
|
|
83019
|
+
entries = fs31.readdirSync(path56.join(main, relative), { withFileTypes: true });
|
|
83020
|
+
} catch {
|
|
83021
|
+
return;
|
|
83022
|
+
}
|
|
83023
|
+
for (const entry of entries) {
|
|
83024
|
+
if (!entry.isDirectory())
|
|
83025
|
+
continue;
|
|
83026
|
+
const child = path56.join(relative, entry.name);
|
|
83027
|
+
if (entry.name === MODULES)
|
|
83028
|
+
out.push(child);
|
|
83029
|
+
else if (depth < MAX_DEPTH2 && !entry.name.startsWith("."))
|
|
83030
|
+
walk(child, depth + 1);
|
|
83031
|
+
}
|
|
83032
|
+
};
|
|
83033
|
+
walk("", 0);
|
|
83034
|
+
return out;
|
|
83035
|
+
}
|
|
83036
|
+
function linkEntry(main, worktree, source, target) {
|
|
83037
|
+
const stat = fs31.lstatSync(source);
|
|
83038
|
+
if (stat.isSymbolicLink()) {
|
|
83039
|
+
const link = fs31.readlinkSync(source);
|
|
83040
|
+
const outside = path56.isAbsolute(link) && within(link, main) && !path56.relative(main, link).split(path56.sep).includes(MODULES);
|
|
83041
|
+
const to = outside ? path56.join(worktree, path56.relative(main, link)) : link;
|
|
83042
|
+
fs31.symlinkSync(to, target, process.platform === "win32" ? "junction" : undefined);
|
|
83043
|
+
return;
|
|
83044
|
+
}
|
|
83045
|
+
fs31.symlinkSync(source, target, process.platform === "win32" ? stat.isDirectory() ? "junction" : "file" : undefined);
|
|
83046
|
+
}
|
|
83047
|
+
function mirror(main, worktree, source, target, top) {
|
|
83048
|
+
fs31.mkdirSync(target);
|
|
83049
|
+
let count = 0;
|
|
83050
|
+
for (const entry of fs31.readdirSync(source, { withFileTypes: true })) {
|
|
83051
|
+
const from = path56.join(source, entry.name);
|
|
83052
|
+
const to = path56.join(target, entry.name);
|
|
83053
|
+
if (top && entry.isDirectory() && (entry.name.startsWith("@") || entry.name === ".bin"))
|
|
83054
|
+
count += mirror(main, worktree, from, to, false);
|
|
83055
|
+
else {
|
|
83056
|
+
linkEntry(main, worktree, from, to);
|
|
83057
|
+
count += 1;
|
|
83058
|
+
}
|
|
83059
|
+
}
|
|
83060
|
+
return count;
|
|
83061
|
+
}
|
|
83062
|
+
function linkWorktreeDependencies(main, worktree) {
|
|
83063
|
+
const started = performance.now();
|
|
83064
|
+
const notes = [];
|
|
83065
|
+
const dirs = [];
|
|
83066
|
+
let entries = 0;
|
|
83067
|
+
for (const relative of moduleDirs(main)) {
|
|
83068
|
+
const target = path56.join(worktree, relative);
|
|
83069
|
+
if (!fs31.existsSync(path56.dirname(target)) || fs31.existsSync(target))
|
|
83070
|
+
continue;
|
|
83071
|
+
if (!gitRun4(worktree, ["check-ignore", "-q", `${relative.split(path56.sep).join("/")}/`]).ok) {
|
|
83072
|
+
notes.push(`${relative.split(path56.sep).join("/")} is not ignored by git, so it was not linked from ${main}; install dependencies in the worktree yourself`);
|
|
83073
|
+
continue;
|
|
83074
|
+
}
|
|
83075
|
+
try {
|
|
83076
|
+
entries += mirror(main, worktree, path56.join(main, relative), target, true);
|
|
83077
|
+
dirs.push(relative.split(path56.sep).join("/"));
|
|
83078
|
+
} catch (error) {
|
|
83079
|
+
fs31.rmSync(target, { recursive: true, force: true });
|
|
83080
|
+
notes.push(`could not link ${relative} from ${main} (${error.message}); install dependencies in the worktree yourself`);
|
|
83081
|
+
}
|
|
83082
|
+
}
|
|
83083
|
+
if (dirs.length === 0)
|
|
83084
|
+
return { linked: null, notes };
|
|
83085
|
+
const ms = Math.round(performance.now() - started);
|
|
83086
|
+
notes.push(`linked ${dirs.join(", ")} from ${main} (${entries} entries, ${ms} ms): workspace packages resolve to this worktree, installed packages are shared`);
|
|
83087
|
+
return { linked: { from: main, dirs, entries, ms }, notes };
|
|
83088
|
+
}
|
|
83089
|
+
var MODULES = "node_modules", MAX_DEPTH2 = 2, within = (child, parent) => {
|
|
83090
|
+
const relative = path56.relative(parent, child);
|
|
83091
|
+
return relative === "" || !relative.startsWith("..") && !path56.isAbsolute(relative);
|
|
83092
|
+
};
|
|
83093
|
+
var init_worktree_deps = __esm(() => {
|
|
83094
|
+
init_fanout();
|
|
83095
|
+
});
|
|
83096
|
+
|
|
84538
83097
|
// packages/workit-core/src/fanout-worktree.ts
|
|
84539
83098
|
import { randomBytes as randomBytes5 } from "node:crypto";
|
|
84540
|
-
import
|
|
84541
|
-
import
|
|
83099
|
+
import fs32 from "node:fs";
|
|
83100
|
+
import path57 from "node:path";
|
|
84542
83101
|
function rowSample(lines) {
|
|
84543
83102
|
const out = [];
|
|
84544
83103
|
let bytes = 0;
|
|
@@ -84598,7 +83157,7 @@ function adminDir(worktree) {
|
|
|
84598
83157
|
const file = dir.stdout.trim();
|
|
84599
83158
|
let token = null;
|
|
84600
83159
|
try {
|
|
84601
|
-
token =
|
|
83160
|
+
token = fs32.readFileSync(path57.join(file, FANOUT_TOKEN_FILE), "utf8").trim() || null;
|
|
84602
83161
|
} catch {
|
|
84603
83162
|
token = null;
|
|
84604
83163
|
}
|
|
@@ -84606,7 +83165,7 @@ function adminDir(worktree) {
|
|
|
84606
83165
|
const stat = worktreeDeps.stat(file);
|
|
84607
83166
|
return {
|
|
84608
83167
|
path: file,
|
|
84609
|
-
id:
|
|
83168
|
+
id: path57.basename(file),
|
|
84610
83169
|
ino: stat.ino.toString(),
|
|
84611
83170
|
token,
|
|
84612
83171
|
birthMs: stat.birthtimeNs > 0n ? Number(stat.birthtimeMs) : null,
|
|
@@ -84622,7 +83181,7 @@ function markAdminDir(worktree) {
|
|
|
84622
83181
|
return null;
|
|
84623
83182
|
const token = randomBytes5(16).toString("hex");
|
|
84624
83183
|
try {
|
|
84625
|
-
|
|
83184
|
+
fs32.writeFileSync(path57.join(admin.path, FANOUT_TOKEN_FILE), `${token}
|
|
84626
83185
|
`);
|
|
84627
83186
|
} catch {
|
|
84628
83187
|
return null;
|
|
@@ -84637,25 +83196,25 @@ function excludeScratch(cwd) {
|
|
|
84637
83196
|
const common = gitCommonDir(cwd);
|
|
84638
83197
|
if (!common)
|
|
84639
83198
|
return;
|
|
84640
|
-
const file =
|
|
83199
|
+
const file = path57.join(common, "info", "exclude");
|
|
84641
83200
|
let text = "";
|
|
84642
83201
|
try {
|
|
84643
|
-
text =
|
|
83202
|
+
text = fs32.readFileSync(file, "utf8");
|
|
84644
83203
|
} catch {
|
|
84645
83204
|
text = "";
|
|
84646
83205
|
}
|
|
84647
83206
|
if (text.split(/\r?\n/u).includes(EXCLUDE_LINE))
|
|
84648
83207
|
return;
|
|
84649
|
-
|
|
84650
|
-
|
|
83208
|
+
fs32.mkdirSync(path57.dirname(file), { recursive: true });
|
|
83209
|
+
fs32.appendFileSync(file, `${text && !text.endsWith(`
|
|
84651
83210
|
`) ? `
|
|
84652
83211
|
` : ""}# workit fanout: per-worker scratch dirs
|
|
84653
83212
|
${EXCLUDE_LINE}
|
|
84654
83213
|
`);
|
|
84655
83214
|
}
|
|
84656
83215
|
function ensureScratch(worktree) {
|
|
84657
|
-
const scratch =
|
|
84658
|
-
|
|
83216
|
+
const scratch = path57.join(worktree, SCRATCH_DIR);
|
|
83217
|
+
fs32.mkdirSync(scratch, { recursive: true });
|
|
84659
83218
|
return scratch;
|
|
84660
83219
|
}
|
|
84661
83220
|
function createSliceWorktree(cwd, plan, input) {
|
|
@@ -84682,6 +83241,7 @@ function createSliceWorktree(cwd, plan, input) {
|
|
|
84682
83241
|
if (!createdRow(cwd, plan, slice, target))
|
|
84683
83242
|
return fanoutFail("blocked", `${target} is a worktree that workit fanout worktree create did not make for slice ${slice.id}`, `remove it yourself (git worktree remove ${target}) once you know what it holds`);
|
|
84684
83243
|
excludeScratch(cwd);
|
|
83244
|
+
const deps = linkWorktreeDependencies(mainCheckout(cwd), target);
|
|
84685
83245
|
return {
|
|
84686
83246
|
ok: true,
|
|
84687
83247
|
data: {
|
|
@@ -84690,7 +83250,11 @@ function createSliceWorktree(cwd, plan, input) {
|
|
|
84690
83250
|
head: existing.head ?? "",
|
|
84691
83251
|
mode: resolveRef(cwd, localRef) ? "resume" : "new",
|
|
84692
83252
|
created: false,
|
|
84693
|
-
|
|
83253
|
+
dependencies: deps.linked,
|
|
83254
|
+
notes: [
|
|
83255
|
+
deps.linked ? "the worktree already exists; only its missing dependencies were linked" : "the worktree already exists; nothing was changed",
|
|
83256
|
+
...deps.notes
|
|
83257
|
+
]
|
|
84694
83258
|
}
|
|
84695
83259
|
};
|
|
84696
83260
|
}
|
|
@@ -84699,8 +83263,8 @@ function createSliceWorktree(cwd, plan, input) {
|
|
|
84699
83263
|
return fanoutFail("blocked", `${slice.branch} is checked out at ${elsewhere.path}: never two live workers on one branch`, `stop that worker, then workit fanout worktree release ${slice.id} (or git worktree remove ${elsewhere.path})`);
|
|
84700
83264
|
let dirExisted = false;
|
|
84701
83265
|
try {
|
|
84702
|
-
const stat =
|
|
84703
|
-
if (!stat.isDirectory() ||
|
|
83266
|
+
const stat = fs32.statSync(target);
|
|
83267
|
+
if (!stat.isDirectory() || fs32.readdirSync(target).length > 0)
|
|
84704
83268
|
return fanoutFail("blocked", `${target} exists and is ${stat.isDirectory() ? "not empty" : "not a directory"} (not a worktree of this repository)`, `move it away, or set slice ${slice.id}'s worktree to another path`);
|
|
84705
83269
|
dirExisted = true;
|
|
84706
83270
|
} catch (error) {
|
|
@@ -84740,6 +83304,8 @@ function createSliceWorktree(cwd, plan, input) {
|
|
|
84740
83304
|
}
|
|
84741
83305
|
excludeScratch(cwd);
|
|
84742
83306
|
const scratch = ensureScratch(target);
|
|
83307
|
+
const deps = linkWorktreeDependencies(mainCheckout(cwd), target);
|
|
83308
|
+
notes.push(...deps.notes);
|
|
84743
83309
|
const head = gitRun4(target, ["rev-parse", "HEAD"]).stdout.trim();
|
|
84744
83310
|
const baseSha = mode === "new" ? head : null;
|
|
84745
83311
|
const admin = markAdminDir(target);
|
|
@@ -84759,7 +83325,10 @@ function createSliceWorktree(cwd, plan, input) {
|
|
|
84759
83325
|
...admin ? { adminId: admin.id, adminIno: admin.ino, adminToken: admin.token } : {},
|
|
84760
83326
|
...baseSha ? { baseSha } : {}
|
|
84761
83327
|
});
|
|
84762
|
-
return {
|
|
83328
|
+
return {
|
|
83329
|
+
ok: true,
|
|
83330
|
+
data: { ...base, scratch, head, mode, created: true, dependencies: deps.linked, notes }
|
|
83331
|
+
};
|
|
84763
83332
|
}
|
|
84764
83333
|
function releaseSliceWorktree(cwd, plan, input) {
|
|
84765
83334
|
const found = findSlice(plan, input.slice);
|
|
@@ -84782,7 +83351,7 @@ function releaseSliceWorktree(cwd, plan, input) {
|
|
|
84782
83351
|
notes: []
|
|
84783
83352
|
};
|
|
84784
83353
|
if (index < 0) {
|
|
84785
|
-
if (
|
|
83354
|
+
if (fs32.existsSync(target))
|
|
84786
83355
|
return fanoutFail("blocked", `${target} is not a worktree of this repository; nothing was removed`, "remove it yourself once you know what it holds");
|
|
84787
83356
|
out.notes.push(`no worktree at ${target}; nothing to release`);
|
|
84788
83357
|
return { ok: true, data: out };
|
|
@@ -84844,7 +83413,7 @@ function releaseSliceWorktree(cwd, plan, input) {
|
|
|
84844
83413
|
`)[0]}`, entry.locked ? `git worktree unlock ${entry.path}, then release again` : undefined, { ...out });
|
|
84845
83414
|
out.removed = true;
|
|
84846
83415
|
if (made.dirExisted === true)
|
|
84847
|
-
|
|
83416
|
+
fs32.mkdirSync(entry.path, { recursive: true });
|
|
84848
83417
|
if (out.unpushed)
|
|
84849
83418
|
out.notes.push(`${slice.branch} is kept with ${out.unpushed} commit(s) not on the remote`);
|
|
84850
83419
|
return { ok: true, data: out };
|
|
@@ -84872,42 +83441,44 @@ function unpushedCount(cwd, branch) {
|
|
|
84872
83441
|
return count.ok ? Number(count.stdout.trim()) : null;
|
|
84873
83442
|
}
|
|
84874
83443
|
function removeScratch(worktree) {
|
|
84875
|
-
const scratch =
|
|
83444
|
+
const scratch = path57.join(worktree, SCRATCH_DIR);
|
|
84876
83445
|
let stat;
|
|
84877
83446
|
try {
|
|
84878
|
-
stat =
|
|
83447
|
+
stat = fs32.lstatSync(scratch);
|
|
84879
83448
|
} catch {
|
|
84880
83449
|
return;
|
|
84881
83450
|
}
|
|
84882
83451
|
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
84883
|
-
|
|
83452
|
+
fs32.unlinkSync(scratch);
|
|
84884
83453
|
else
|
|
84885
|
-
|
|
83454
|
+
fs32.rmSync(scratch, { recursive: true, force: true, maxRetries: 2 });
|
|
84886
83455
|
}
|
|
84887
83456
|
var SLOW, SCRATCH_DIR = ".workit-scratch", EXCLUDE_LINE, DIRTY_ROW_BYTES = 2000, canonical2 = (target) => {
|
|
84888
83457
|
try {
|
|
84889
|
-
return
|
|
83458
|
+
return fs32.realpathSync.native(target);
|
|
84890
83459
|
} catch {
|
|
84891
|
-
return
|
|
83460
|
+
return path57.resolve(target);
|
|
84892
83461
|
}
|
|
84893
83462
|
}, samePath = (a, b) => {
|
|
84894
83463
|
const left = canonical2(a);
|
|
84895
83464
|
const right = canonical2(b);
|
|
84896
83465
|
return process.platform === "win32" || process.platform === "darwin" ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
84897
83466
|
}, inside3 = (child, parent) => {
|
|
84898
|
-
const relative =
|
|
84899
|
-
return relative === "" || !relative.startsWith("..") && !
|
|
84900
|
-
}, slicePath = (cwd, slice) =>
|
|
83467
|
+
const relative = path57.relative(canonical2(parent), canonical2(child));
|
|
83468
|
+
return relative === "" || !relative.startsWith("..") && !path57.isAbsolute(relative);
|
|
83469
|
+
}, slicePath = (cwd, slice) => path57.isAbsolute(slice.worktree) ? slice.worktree : path57.resolve(mainCheckout(cwd), slice.worktree), CLOCK_SLACK_MS = 2000, worktreeDeps, worktreeFailed = (stderr) => fanoutFail("failed", `git worktree add failed: ${stderr.trim().split(`
|
|
84901
83470
|
`)[0] || "unknown error"}`);
|
|
84902
83471
|
var init_fanout_worktree = __esm(() => {
|
|
84903
83472
|
init_ops();
|
|
84904
83473
|
init_rev();
|
|
84905
83474
|
init_ledger();
|
|
83475
|
+
init_worktree_confine();
|
|
83476
|
+
init_worktree_deps();
|
|
84906
83477
|
init_fanout();
|
|
84907
83478
|
SLOW = GIT_TIMEOUTS.worktree;
|
|
84908
83479
|
EXCLUDE_LINE = `/${SCRATCH_DIR}/`;
|
|
84909
83480
|
worktreeDeps = {
|
|
84910
|
-
stat: (file) =>
|
|
83481
|
+
stat: (file) => fs32.statSync(file, { bigint: true })
|
|
84911
83482
|
};
|
|
84912
83483
|
});
|
|
84913
83484
|
|
|
@@ -84985,8 +83556,9 @@ function renderBrief(cwd, plan, input) {
|
|
|
84985
83556
|
...REPORT,
|
|
84986
83557
|
"STANDING:",
|
|
84987
83558
|
...standing.length ? standing.map((order) => ` - ${order.what}`) : [" - none recorded beyond this brief"],
|
|
83559
|
+
exists ? `WORKTREE: ${planned} (made by \`workit fanout worktree create ${slice.id}\`; yours alone)` : `WORKTREE: ${planned} (not made yet: the lead runs \`workit fanout worktree create ${slice.id}\` before spawning, then renders this brief again)`,
|
|
84988
83560
|
...identityLines(identity, minted),
|
|
84989
|
-
...rules(
|
|
83561
|
+
...rules(mode, branch, slice.base, exists ? planned : null)
|
|
84990
83562
|
];
|
|
84991
83563
|
return {
|
|
84992
83564
|
ok: true,
|
|
@@ -85019,25 +83591,25 @@ var DEFAULT_TIMEBOX = "30 minutes", REPORT, identityOf = (host) => {
|
|
|
85019
83591
|
}, identityLines = (identity, session) => {
|
|
85020
83592
|
if (identity === "issued")
|
|
85021
83593
|
return [
|
|
85022
|
-
" Session: your env already has WORKIT_SESSION_ID (the lead's id; SubagentStart issues ids only to verifiers and reviewers). Use it as is
|
|
83594
|
+
" Session: your env already has WORKIT_SESSION_ID (the lead's id; SubagentStart issues ids only to verifiers and reviewers). Use it as is: workit commands read it and `workit git commit` adds the Workit-Session trailer; never export or hand-mint one."
|
|
85023
83595
|
];
|
|
85024
83596
|
if (identity === "derived")
|
|
85025
83597
|
return [
|
|
85026
83598
|
" Session: your host already sets WORKIT_SESSION_ID in your shell; export nothing and never hand-mint one."
|
|
85027
83599
|
];
|
|
85028
83600
|
return [` export WORKIT_SESSION_ID=${session}`];
|
|
85029
|
-
}, rules = (
|
|
85030
|
-
const
|
|
83601
|
+
}, rules = (mode, branch, base, worktree) => {
|
|
83602
|
+
const workit = worktree ? `workit --cwd ${worktree}` : "workit";
|
|
85031
83603
|
let first = ` 1. First command: \`workit git branch ${branch} --base ${base}\` (your worktree may start on another name).`;
|
|
85032
|
-
if (
|
|
83604
|
+
if (worktree)
|
|
83605
|
+
first = ` 1. Work only in WORKTREE, already on ${branch}${mode === "resume" ? " at its head" : ""}: edit files under it and run each git and check command against it, one verb per call (\`${workit} git commit …\`, \`${workit} check <name>\`), never \`cd … &&\` chains or \`$VAR\` paths. An implementer's change outside it is denied.`;
|
|
83606
|
+
else if (mode === "resume")
|
|
85033
83607
|
first = ` 1. First command: \`git switch ${branch}\`, then continue from its head.`;
|
|
85034
|
-
else if (plain)
|
|
85035
|
-
first = ` 1. First command: \`git fetch origin && git switch -c ${branch} origin/${base}\` (plain git: worktree isolation refuses every \`workit git\` form).`;
|
|
85036
83608
|
return [
|
|
85037
83609
|
"RULES:",
|
|
85038
83610
|
first,
|
|
85039
83611
|
' 2. Decide ambiguities yourself: `workit ledger ruling "<what>" --why "<why>" --cost-if-wrong "<cost>"`; stop only for an irreversible, security-sensitive or out-of-worktree action.',
|
|
85040
|
-
|
|
83612
|
+
` 3. Commit with \`${workit} git commit -m <msg> -- <paths in SCOPE>\`; push only if this brief says so.`,
|
|
85041
83613
|
" 4. Never record a verdict on your own work.",
|
|
85042
83614
|
" 5. Run tests, lint, typecheck and builds only through `workit check` (a named check, or `workit check --heavy -- <cmd>`): one machine-wide lease queues every agent, so never a hand-made flock; `workit lock status` shows who holds it.",
|
|
85043
83615
|
" 6. A failed review gets one fix round and one recheck. Still failing: stop and report the open finding, never a third round (the lead spawns a decider). A product choice is the user's: report it, do not decide it."
|
|
@@ -85056,7 +83628,7 @@ var init_fanout_brief = __esm(() => {
|
|
|
85056
83628
|
});
|
|
85057
83629
|
|
|
85058
83630
|
// packages/workit-core/src/fanout-landed.ts
|
|
85059
|
-
import { spawnSync as
|
|
83631
|
+
import { spawnSync as spawnSync28 } from "node:child_process";
|
|
85060
83632
|
function forgeBreaker(resolved) {
|
|
85061
83633
|
if (!resolved)
|
|
85062
83634
|
return { forge: null, down: () => null };
|
|
@@ -85147,7 +83719,7 @@ function trunkPatchIds(ctx, fork) {
|
|
|
85147
83719
|
"--"
|
|
85148
83720
|
]);
|
|
85149
83721
|
if (log.ok && log.stdout.trim()) {
|
|
85150
|
-
const run =
|
|
83722
|
+
const run = spawnSync28("git", ["patch-id", "--stable"], {
|
|
85151
83723
|
cwd: ctx.cwd,
|
|
85152
83724
|
input: log.stdout,
|
|
85153
83725
|
encoding: "utf8",
|
|
@@ -85721,8 +84293,8 @@ var exports_fanout = {};
|
|
|
85721
84293
|
__export(exports_fanout, {
|
|
85722
84294
|
run: () => run18
|
|
85723
84295
|
});
|
|
85724
|
-
import
|
|
85725
|
-
import
|
|
84296
|
+
import fs33 from "node:fs";
|
|
84297
|
+
import path58 from "node:path";
|
|
85726
84298
|
function forgeFor(io, offline) {
|
|
85727
84299
|
if (offline)
|
|
85728
84300
|
return { forge: null, error: "--offline" };
|
|
@@ -85783,10 +84355,10 @@ function plan2(argv, io) {
|
|
|
85783
84355
|
return usage3(io, "pass --trunk or --track, not both (--track picks the trunk)", PLAN_USAGE2);
|
|
85784
84356
|
if (flags.positionals.length !== 1)
|
|
85785
84357
|
return usage3(io, flags.positionals.length ? `unexpected argument ${flags.positionals[1]}` : "missing <plan.json>", PLAN_USAGE2);
|
|
85786
|
-
const file =
|
|
84358
|
+
const file = path58.resolve(io.cwd, flags.positionals[0]);
|
|
85787
84359
|
let raw;
|
|
85788
84360
|
try {
|
|
85789
|
-
raw = JSON.parse(
|
|
84361
|
+
raw = JSON.parse(fs33.readFileSync(file, "utf8"));
|
|
85790
84362
|
} catch (error) {
|
|
85791
84363
|
return usage3(io, `cannot read ${file} as JSON: ${error.message}`, PLAN_USAGE2);
|
|
85792
84364
|
}
|
|
@@ -101317,7 +99889,7 @@ var init_ast = __esm(() => {
|
|
|
101317
99889
|
|
|
101318
99890
|
// packages/workit-cli/src/test-audit/rules.ts
|
|
101319
99891
|
import { builtinModules } from "node:module";
|
|
101320
|
-
import
|
|
99892
|
+
import path59 from "node:path";
|
|
101321
99893
|
function testCallee(call) {
|
|
101322
99894
|
let callee = call.callee;
|
|
101323
99895
|
if (callee.type === "CallExpression")
|
|
@@ -101810,12 +100382,12 @@ function checkConstantsOnly(ctx, emit) {
|
|
|
101810
100382
|
}
|
|
101811
100383
|
function checkOverMocking(ctx, emit) {
|
|
101812
100384
|
const { program } = ctx.parsed;
|
|
101813
|
-
const unit =
|
|
100385
|
+
const unit = path59.basename(ctx.file).replace(/\.(?:[cm]?[jt]sx?)$/, "").replace(/\.(?:test|spec)$/, "");
|
|
101814
100386
|
const mocked = find2(program, (node) => node.type === "CallExpression" && MODULE_MOCK.test(calleeName(node.callee) ?? "") && node.arguments[0]?.type === "StringLiteral");
|
|
101815
100387
|
const internal = mocked.filter((call) => /^(?:\.|@\/|~\/)/.test(call.arguments[0].value));
|
|
101816
100388
|
for (const call of mocked) {
|
|
101817
100389
|
const spec = call.arguments[0].value;
|
|
101818
|
-
const base =
|
|
100390
|
+
const base = path59.basename(spec.replace(/\/index(?:\.[cm]?[jt]sx?)?$/, "")).replace(/\.(?:[cm]?[jt]sx?)$/, "");
|
|
101819
100391
|
if (base === unit)
|
|
101820
100392
|
emit({
|
|
101821
100393
|
rule: "over-mocking",
|
|
@@ -102203,11 +100775,11 @@ var init_rules = __esm(() => {
|
|
|
102203
100775
|
});
|
|
102204
100776
|
|
|
102205
100777
|
// packages/workit-cli/src/test-audit/audit.ts
|
|
102206
|
-
import { spawnSync as
|
|
100778
|
+
import { spawnSync as spawnSync29 } from "node:child_process";
|
|
102207
100779
|
import { existsSync as existsSync20, readdirSync as readdirSync10, readFileSync as readFileSync23, statSync as statSync8 } from "node:fs";
|
|
102208
|
-
import
|
|
100780
|
+
import path60 from "node:path";
|
|
102209
100781
|
function git7(cwd, args) {
|
|
102210
|
-
const run =
|
|
100782
|
+
const run = spawnSync29("git", ["-c", "core.quotepath=off", ...args], {
|
|
102211
100783
|
cwd,
|
|
102212
100784
|
encoding: "utf8",
|
|
102213
100785
|
maxBuffer: 64 * 1024 * 1024
|
|
@@ -102218,7 +100790,7 @@ function walkTests(dir, out) {
|
|
|
102218
100790
|
for (const entry of readdirSync10(dir, { withFileTypes: true })) {
|
|
102219
100791
|
if (entry.name.startsWith(".") && entry.name !== "." && entry.isDirectory())
|
|
102220
100792
|
continue;
|
|
102221
|
-
const full =
|
|
100793
|
+
const full = path60.join(dir, entry.name);
|
|
102222
100794
|
if (entry.isDirectory()) {
|
|
102223
100795
|
if (!SKIP_DIRS.has(entry.name))
|
|
102224
100796
|
walkTests(full, out);
|
|
@@ -102230,7 +100802,7 @@ function expandPaths(cwd, inputs) {
|
|
|
102230
100802
|
const files = [];
|
|
102231
100803
|
const missing = [];
|
|
102232
100804
|
for (const input of inputs.length > 0 ? inputs : ["."]) {
|
|
102233
|
-
const full =
|
|
100805
|
+
const full = path60.resolve(cwd, input);
|
|
102234
100806
|
if (!existsSync20(full))
|
|
102235
100807
|
missing.push(input);
|
|
102236
100808
|
else if (statSync8(full).isDirectory())
|
|
@@ -102267,11 +100839,11 @@ function changedLines(cwd, base) {
|
|
|
102267
100839
|
for (let offset = 0;offset < count; offset += 1)
|
|
102268
100840
|
lines.add(start + offset);
|
|
102269
100841
|
}
|
|
102270
|
-
changed.set(
|
|
100842
|
+
changed.set(path60.join(root, file), lines);
|
|
102271
100843
|
}
|
|
102272
100844
|
const untracked = git7(root, ["ls-files", "--others", "--exclude-standard", "-z"]);
|
|
102273
100845
|
for (const file of untracked.stdout.split("\x00").filter(Boolean))
|
|
102274
|
-
changed.set(
|
|
100846
|
+
changed.set(path60.join(root, file), "all");
|
|
102275
100847
|
return changed;
|
|
102276
100848
|
}
|
|
102277
100849
|
function auditFiles(files, display) {
|
|
@@ -102333,7 +100905,7 @@ function auditFiles(files, display) {
|
|
|
102333
100905
|
durationMs: Math.round(performance.now() - started)
|
|
102334
100906
|
};
|
|
102335
100907
|
}
|
|
102336
|
-
var TEST_FILE, CODE_FILE, SKIP_DIRS, toPosix = (file) => file.split(
|
|
100908
|
+
var TEST_FILE, CODE_FILE, SKIP_DIRS, toPosix = (file) => file.split(path60.sep).join("/"), SEVERITY_ORDER, ruleIds;
|
|
102337
100909
|
var init_audit = __esm(() => {
|
|
102338
100910
|
init_ast();
|
|
102339
100911
|
init_rules();
|
|
@@ -102363,7 +100935,7 @@ import {
|
|
|
102363
100935
|
copyFileSync as copyFileSync6
|
|
102364
100936
|
} from "node:fs";
|
|
102365
100937
|
import os18 from "node:os";
|
|
102366
|
-
import
|
|
100938
|
+
import path61 from "node:path";
|
|
102367
100939
|
function mutantsFor(source, file, lines) {
|
|
102368
100940
|
const parsed = parseSource(source, file);
|
|
102369
100941
|
const out = [];
|
|
@@ -102436,7 +101008,7 @@ function relatedTests(source, testFiles) {
|
|
|
102436
101008
|
for (const match of text.matchAll(SPECIFIER)) {
|
|
102437
101009
|
const spec = match[1];
|
|
102438
101010
|
if (spec.startsWith(".")) {
|
|
102439
|
-
if (stripExt(
|
|
101011
|
+
if (stripExt(path61.posix.join(path61.posix.dirname(file), spec)) === target)
|
|
102440
101012
|
return true;
|
|
102441
101013
|
} else if (spec.includes("/") && stripExt(spec).split("/").slice(-2).join("/") === tail)
|
|
102442
101014
|
return true;
|
|
@@ -102456,12 +101028,12 @@ function removeStaleCopies(tmp = os18.tmpdir()) {
|
|
|
102456
101028
|
for (const name of names) {
|
|
102457
101029
|
if (!name.startsWith(TEMP_PREFIX))
|
|
102458
101030
|
continue;
|
|
102459
|
-
const dir =
|
|
101031
|
+
const dir = path61.join(tmp, name);
|
|
102460
101032
|
try {
|
|
102461
101033
|
const stat = lstatSync8(dir);
|
|
102462
101034
|
if (!stat.isDirectory() || uid !== undefined && stat.uid !== uid)
|
|
102463
101035
|
continue;
|
|
102464
|
-
const pid = Number(readFileSync24(
|
|
101036
|
+
const pid = Number(readFileSync24(path61.join(dir, PID_FILE), "utf8"));
|
|
102465
101037
|
if (Number.isInteger(pid) && pid > 0 && alive(pid))
|
|
102466
101038
|
continue;
|
|
102467
101039
|
} catch (error) {
|
|
@@ -102478,8 +101050,8 @@ function removeStaleCopies(tmp = os18.tmpdir()) {
|
|
|
102478
101050
|
function linkNodeModules(origin, target, repo, copy) {
|
|
102479
101051
|
mkdirSync10(target, { recursive: true });
|
|
102480
101052
|
for (const entry of readdirSync11(origin, { withFileTypes: true })) {
|
|
102481
|
-
const from =
|
|
102482
|
-
const to =
|
|
101053
|
+
const from = path61.join(origin, entry.name);
|
|
101054
|
+
const to = path61.join(target, entry.name);
|
|
102483
101055
|
if (entry.name.startsWith("@") && entry.isDirectory()) {
|
|
102484
101056
|
linkNodeModules(from, to, repo, copy);
|
|
102485
101057
|
continue;
|
|
@@ -102495,9 +101067,9 @@ function linkNodeModules(origin, target, repo, copy) {
|
|
|
102495
101067
|
} catch {
|
|
102496
101068
|
continue;
|
|
102497
101069
|
}
|
|
102498
|
-
const relative =
|
|
102499
|
-
if (!relative.startsWith("..") && !
|
|
102500
|
-
symlinkSync(
|
|
101070
|
+
const relative = path61.relative(repo, real);
|
|
101071
|
+
if (!relative.startsWith("..") && !path61.isAbsolute(relative) && !relative.split(path61.sep).includes("node_modules")) {
|
|
101072
|
+
symlinkSync(path61.join(copy, relative), to, "junction");
|
|
102501
101073
|
continue;
|
|
102502
101074
|
}
|
|
102503
101075
|
}
|
|
@@ -102513,7 +101085,7 @@ function copySymlink(repo, tree, from, to) {
|
|
|
102513
101085
|
}
|
|
102514
101086
|
const realRepo = realpathSync9(repo);
|
|
102515
101087
|
if (real && inside4(realRepo, real)) {
|
|
102516
|
-
symlinkSync(
|
|
101088
|
+
symlinkSync(path61.relative(path61.dirname(to), path61.join(tree, path61.relative(realRepo, real))), to);
|
|
102517
101089
|
return;
|
|
102518
101090
|
}
|
|
102519
101091
|
if (real && statSync9(real).isFile())
|
|
@@ -102522,10 +101094,10 @@ function copySymlink(repo, tree, from, to) {
|
|
|
102522
101094
|
symlinkSync(readlinkSync3(from), to);
|
|
102523
101095
|
}
|
|
102524
101096
|
function writeInside(copy, file, content) {
|
|
102525
|
-
const target =
|
|
101097
|
+
const target = path61.join(copy, file);
|
|
102526
101098
|
try {
|
|
102527
101099
|
const stat = lstatSync8(target);
|
|
102528
|
-
if (!stat.isFile() || !inside4(realpathSync9(copy), realpathSync9(
|
|
101100
|
+
if (!stat.isFile() || !inside4(realpathSync9(copy), realpathSync9(path61.dirname(target))))
|
|
102529
101101
|
return false;
|
|
102530
101102
|
} catch {
|
|
102531
101103
|
return false;
|
|
@@ -102536,9 +101108,9 @@ function writeInside(copy, file, content) {
|
|
|
102536
101108
|
return true;
|
|
102537
101109
|
}
|
|
102538
101110
|
function copyWorkingTree(repo) {
|
|
102539
|
-
const dir = mkdtempSync4(
|
|
102540
|
-
writeFileSync11(
|
|
102541
|
-
const tree =
|
|
101111
|
+
const dir = mkdtempSync4(path61.join(os18.tmpdir(), TEMP_PREFIX));
|
|
101112
|
+
writeFileSync11(path61.join(dir, PID_FILE), String(process.pid));
|
|
101113
|
+
const tree = path61.join(dir, "tree");
|
|
102542
101114
|
const head = git7(repo, ["rev-parse", "--verify", "--quiet", "HEAD"]).stdout.trim();
|
|
102543
101115
|
if (head && git7(dir, ["clone", "-q", "--shared", "--no-checkout", repo, tree]).ok) {
|
|
102544
101116
|
const branch = git7(repo, ["symbolic-ref", "--quiet", "--short", "HEAD"]).stdout.trim();
|
|
@@ -102555,29 +101127,29 @@ function copyWorkingTree(repo) {
|
|
|
102555
101127
|
for (const relative of listed) {
|
|
102556
101128
|
if (!relative)
|
|
102557
101129
|
continue;
|
|
102558
|
-
const from =
|
|
101130
|
+
const from = path61.join(repo, relative);
|
|
102559
101131
|
let stat;
|
|
102560
101132
|
try {
|
|
102561
101133
|
stat = lstatSync8(from);
|
|
102562
101134
|
} catch {
|
|
102563
101135
|
continue;
|
|
102564
101136
|
}
|
|
102565
|
-
const to =
|
|
102566
|
-
mkdirSync10(
|
|
101137
|
+
const to = path61.join(tree, relative);
|
|
101138
|
+
mkdirSync10(path61.dirname(to), { recursive: true });
|
|
102567
101139
|
if (stat.isSymbolicLink())
|
|
102568
101140
|
copySymlink(repo, tree, from, to);
|
|
102569
101141
|
else if (stat.isFile())
|
|
102570
101142
|
copyFileSync6(from, to);
|
|
102571
|
-
let parent =
|
|
101143
|
+
let parent = path61.dirname(relative);
|
|
102572
101144
|
while (parent !== "." && !dirs.has(parent)) {
|
|
102573
101145
|
dirs.add(parent);
|
|
102574
|
-
parent =
|
|
101146
|
+
parent = path61.dirname(parent);
|
|
102575
101147
|
}
|
|
102576
101148
|
}
|
|
102577
101149
|
for (const parent of dirs) {
|
|
102578
|
-
const modules =
|
|
102579
|
-
if (existsSync21(modules) && !existsSync21(
|
|
102580
|
-
linkNodeModules(modules,
|
|
101150
|
+
const modules = path61.join(repo, parent, "node_modules");
|
|
101151
|
+
if (existsSync21(modules) && !existsSync21(path61.join(tree, parent, "node_modules")))
|
|
101152
|
+
linkNodeModules(modules, path61.join(tree, parent, "node_modules"), repo, tree);
|
|
102581
101153
|
}
|
|
102582
101154
|
return { dir, tree };
|
|
102583
101155
|
}
|
|
@@ -102616,14 +101188,14 @@ async function runMutation(options) {
|
|
|
102616
101188
|
const tracked = git7(repo, ["ls-files", "-co", "--exclude-standard", "-z"]).stdout.split("\x00").filter(Boolean);
|
|
102617
101189
|
const testFiles = tracked.filter((file) => TEST_FILE.test(file)).map((file) => {
|
|
102618
101190
|
try {
|
|
102619
|
-
return { file, text: readFileSync24(
|
|
101191
|
+
return { file, text: readFileSync24(path61.join(repo, file), "utf8") };
|
|
102620
101192
|
} catch {
|
|
102621
101193
|
return { file, text: "" };
|
|
102622
101194
|
}
|
|
102623
101195
|
});
|
|
102624
101196
|
const candidates = [];
|
|
102625
101197
|
for (const [absolute, lines] of [...options.changed].toSorted(([a], [b]) => a.localeCompare(b))) {
|
|
102626
|
-
const relative = toPosix(
|
|
101198
|
+
const relative = toPosix(path61.relative(repo, absolute));
|
|
102627
101199
|
if (!CODE_FILE.test(relative) || TEST_FILE.test(relative) || relative.endsWith(".d.ts"))
|
|
102628
101200
|
continue;
|
|
102629
101201
|
if (NOT_SOURCE.test(relative) || !existsSync21(absolute))
|
|
@@ -102637,9 +101209,9 @@ async function runMutation(options) {
|
|
|
102637
101209
|
for (const mutant of mutants)
|
|
102638
101210
|
if (!related.has(mutant.file))
|
|
102639
101211
|
related.set(mutant.file, relatedTests(mutant.file, testFiles));
|
|
102640
|
-
const cwdRelative =
|
|
101212
|
+
const cwdRelative = path61.relative(repo, options.cwd);
|
|
102641
101213
|
const asArg = (file) => {
|
|
102642
|
-
const relative = toPosix(
|
|
101214
|
+
const relative = toPosix(path61.relative(cwdRelative, file));
|
|
102643
101215
|
return shellQuote(relative.startsWith(".") ? relative : `./${relative}`);
|
|
102644
101216
|
};
|
|
102645
101217
|
const commandFor = (tests) => options.command.replaceAll("{files}", () => tests.map(asArg).join(" "));
|
|
@@ -102675,7 +101247,7 @@ async function runMutation(options) {
|
|
|
102675
101247
|
return finish3(options, 0, results, counts, started);
|
|
102676
101248
|
removeStaleCopies();
|
|
102677
101249
|
const { dir: copyDir, tree: copy } = copyWorkingTree(repo);
|
|
102678
|
-
const copyCwd =
|
|
101250
|
+
const copyCwd = path61.join(copy, cwdRelative);
|
|
102679
101251
|
let current = null;
|
|
102680
101252
|
const restore = () => {
|
|
102681
101253
|
if (current)
|
|
@@ -102710,7 +101282,7 @@ async function runMutation(options) {
|
|
|
102710
101282
|
record({ ...mutant, status: "skipped", durationMs: 0, tests, reason: "budget" });
|
|
102711
101283
|
continue;
|
|
102712
101284
|
}
|
|
102713
|
-
const original = readFileSync24(
|
|
101285
|
+
const original = readFileSync24(path61.join(copy, mutant.file), "utf8");
|
|
102714
101286
|
const mutated = original.slice(0, mutant.start) + mutant.replacement + original.slice(mutant.end);
|
|
102715
101287
|
current = { file: mutant.file, original };
|
|
102716
101288
|
if (!writeInside(copy, mutant.file, mutated)) {
|
|
@@ -102767,8 +101339,8 @@ var NOT_SOURCE, FLIPS, isStringy = (node) => node.type === "StringLiteral" || no
|
|
|
102767
101339
|
return error.code === "EPERM";
|
|
102768
101340
|
}
|
|
102769
101341
|
}, inside4 = (root, file) => {
|
|
102770
|
-
const relative =
|
|
102771
|
-
return relative === "" || !relative.startsWith("..") && !
|
|
101342
|
+
const relative = path61.relative(root, file);
|
|
101343
|
+
return relative === "" || !relative.startsWith("..") && !path61.isAbsolute(relative);
|
|
102772
101344
|
}, active, killTree = (child) => {
|
|
102773
101345
|
try {
|
|
102774
101346
|
if (child.pid && process.platform !== "win32")
|
|
@@ -102817,7 +101389,7 @@ __export(exports_test_audit, {
|
|
|
102817
101389
|
run: () => run20
|
|
102818
101390
|
});
|
|
102819
101391
|
import { existsSync as existsSync22 } from "node:fs";
|
|
102820
|
-
import
|
|
101392
|
+
import path62 from "node:path";
|
|
102821
101393
|
function parse7(argv) {
|
|
102822
101394
|
const options = {
|
|
102823
101395
|
paths: [],
|
|
@@ -102922,8 +101494,8 @@ async function run20(argv, io) {
|
|
|
102922
101494
|
return emit(io, fail2("invalid_input", `${options}
|
|
102923
101495
|
usage: ${USAGE12}`));
|
|
102924
101496
|
const top = git7(io.cwd, ["rev-parse", "--show-toplevel"]);
|
|
102925
|
-
const repo = top.ok ?
|
|
102926
|
-
const display = (file) => toPosix(
|
|
101497
|
+
const repo = top.ok ? path62.resolve(top.stdout.trim()) : null;
|
|
101498
|
+
const display = (file) => toPosix(path62.relative(io.cwd, file) || file);
|
|
102927
101499
|
let base = null;
|
|
102928
101500
|
let changed = null;
|
|
102929
101501
|
if (options.diff || options.mutate) {
|
|
@@ -103062,7 +101634,7 @@ __export(exports_youtrack, {
|
|
|
103062
101634
|
run: () => run22
|
|
103063
101635
|
});
|
|
103064
101636
|
import { readFileSync as readFileSync25 } from "node:fs";
|
|
103065
|
-
import
|
|
101637
|
+
import path63 from "node:path";
|
|
103066
101638
|
async function run22(argv, io) {
|
|
103067
101639
|
const [sub, ...rest] = argv;
|
|
103068
101640
|
if (sub !== "note" && sub !== "time" && sub !== "meeting")
|
|
@@ -103108,7 +101680,7 @@ async function run22(argv, io) {
|
|
|
103108
101680
|
let markdown = flags.values.markdown ?? "";
|
|
103109
101681
|
if (flags.values.file)
|
|
103110
101682
|
try {
|
|
103111
|
-
markdown = readFileSync25(
|
|
101683
|
+
markdown = readFileSync25(path63.resolve(io.cwd, flags.values.file), "utf8");
|
|
103112
101684
|
} catch (error) {
|
|
103113
101685
|
return usage3(io, `cannot read ${flags.values.file}: ${String(error)}`, USAGE14);
|
|
103114
101686
|
}
|
|
@@ -103177,7 +101749,7 @@ __export(exports_changelog, {
|
|
|
103177
101749
|
run: () => run23
|
|
103178
101750
|
});
|
|
103179
101751
|
import { readFileSync as readFileSync26 } from "node:fs";
|
|
103180
|
-
import
|
|
101752
|
+
import path64 from "node:path";
|
|
103181
101753
|
async function run23(argv, io) {
|
|
103182
101754
|
const [sub, ...rest] = argv;
|
|
103183
101755
|
if (sub !== "apply")
|
|
@@ -103223,7 +101795,7 @@ async function run23(argv, io) {
|
|
|
103223
101795
|
]);
|
|
103224
101796
|
}
|
|
103225
101797
|
var USAGE15 = "workit changelog apply (--entries <JSON|@file|-> | --normalize-only) [--path CHANGELOG.md] [--preview]", readEntries = (raw, cwd) => {
|
|
103226
|
-
const text = raw === "-" ? readFileSync26(0, "utf8") : raw.startsWith("@") ? readFileSync26(
|
|
101798
|
+
const text = raw === "-" ? readFileSync26(0, "utf8") : raw.startsWith("@") ? readFileSync26(path64.resolve(cwd, raw.slice(1)), "utf8") : raw;
|
|
103227
101799
|
return JSON.parse(text);
|
|
103228
101800
|
};
|
|
103229
101801
|
var init_changelog2 = __esm(() => {
|
|
@@ -103320,7 +101892,7 @@ var init_handoff = __esm(() => {
|
|
|
103320
101892
|
// packages/workit-cli/src/main.ts
|
|
103321
101893
|
init_host_session();
|
|
103322
101894
|
init_package();
|
|
103323
|
-
import
|
|
101895
|
+
import path65 from "node:path";
|
|
103324
101896
|
|
|
103325
101897
|
// packages/workit-cli/src/verbs/registry.ts
|
|
103326
101898
|
var TASK_FAMILY_NAMES = [
|
|
@@ -104158,7 +102730,7 @@ var defaultIo = (json) => ({
|
|
|
104158
102730
|
});
|
|
104159
102731
|
async function main(argv, overrides = {}, options = {}) {
|
|
104160
102732
|
const parsed = parseGlobals(argv);
|
|
104161
|
-
const cwd =
|
|
102733
|
+
const cwd = path65.resolve(overrides.cwd ?? process.cwd(), parsed.cwd ?? ".");
|
|
104162
102734
|
const io = { ...defaultIo(parsed.json), ...overrides, json: parsed.json, cwd };
|
|
104163
102735
|
if (parsed.error)
|
|
104164
102736
|
return emit(io, fail2("invalid_input", parsed.error));
|