@orkestrel/contract 0.0.14 → 0.0.15

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.
@@ -333,6 +333,30 @@ var COMPILE_DEPTH_LIMIT = 512;
333
333
  */
334
334
  var COMPILE_NODE_LIMIT = 16384;
335
335
  /**
336
+ * The maximum number of object keys one compiled presence mask carries, frozen.
337
+ *
338
+ * @remarks
339
+ * A compiled object plan decides which declared keys a value carries. The keys
340
+ * it asks about are fixed when the plan is built, so the plan assigns each one a
341
+ * bit position once and a call ORs one bit per own key it finds and compares the
342
+ * result against the full mask — no per-call collection, and one integer compare
343
+ * instead of one membership dispatch per declared key.
344
+ *
345
+ * The bound is the width a JavaScript bitwise operand has: `1 << position`
346
+ * evaluates on a 32-bit signed integer, so positions run `0` through `30` and a
347
+ * mask stays positive. A declaration with more keys than this is ordinary
348
+ * authoring rather than an attack, so it is answered rather than refused:
349
+ * {@link ContractCompiler} builds no position record for it and its compiled
350
+ * guard, parser, auditor, and reporter decide presence from a collected key
351
+ * vocabulary instead. Both branches answer identically; only the cost differs.
352
+ *
353
+ * @example
354
+ * ```ts
355
+ * PRESENCE_MASK_LIMIT // 31
356
+ * ```
357
+ */
358
+ var PRESENCE_MASK_LIMIT = 31;
359
+ /**
336
360
  * The maximum number of nodes one JSON snapshot may produce, frozen.
337
361
  *
338
362
  * @remarks
@@ -2397,6 +2421,9 @@ function readArrayEntries(value) {
2397
2421
  const length = value.length;
2398
2422
  if (!INTRINSICS.safe(length) || length < 0 || length > 2 ** 32 - 1) throw new INTRINSICS.error("Array length is outside the native array domain");
2399
2423
  const collected = [];
2424
+ const keys = [];
2425
+ let ascending = true;
2426
+ let previous = -1;
2400
2427
  const members = INTRINSICS.members(value);
2401
2428
  for (let position = 0; position < members.length; position += 1) {
2402
2429
  const key = members[position];
@@ -2404,16 +2431,19 @@ function readArrayEntries(value) {
2404
2431
  const index = INTRINSICS.numeric(key);
2405
2432
  if (INTRINSICS.integer(index) && index >= 0 && index < 2 ** 32 - 1 && INTRINSICS.text(index) === key) {
2406
2433
  if (index >= length) throw new INTRINSICS.error("Array index views disagree");
2434
+ if (index <= previous) ascending = false;
2435
+ previous = index;
2407
2436
  collected[collected.length] = index;
2437
+ keys[keys.length] = key;
2408
2438
  }
2409
2439
  }
2410
- const indices = sortValues(collected);
2440
+ const indices = ascending ? collected : sortValues(collected);
2411
2441
  const entries = new INTRINSICS.list(length);
2412
2442
  for (let position = 0; position < indices.length; position += 1) {
2413
2443
  const index = indices[position];
2414
2444
  if (index === void 0) continue;
2415
- const key = INTRINSICS.text(index);
2416
- if (!INTRINSICS.own(value, key)) throw new INTRINSICS.error("Array index views disagree");
2445
+ const key = ascending ? keys[position] : INTRINSICS.text(index);
2446
+ if (key === void 0 || !INTRINSICS.own(value, key)) throw new INTRINSICS.error("Array index views disagree");
2417
2447
  entries[index] = value[index];
2418
2448
  }
2419
2449
  return INTRINSICS.freeze({
@@ -6832,6 +6862,8 @@ var ContractCompiler = class ContractCompiler {
6832
6862
  #trackGuard(plan) {
6833
6863
  let filled = 0;
6834
6864
  let memo;
6865
+ let slot;
6866
+ let kept = false;
6835
6867
  return (value) => {
6836
6868
  if (!isObject(value)) return plan(value);
6837
6869
  const opened = ContractCompiler.#scope === 0;
@@ -6841,14 +6873,28 @@ var ContractCompiler = class ContractCompiler {
6841
6873
  }
6842
6874
  try {
6843
6875
  const scope = ContractCompiler.#scope;
6844
- if (memo === void 0 || filled !== scope) {
6845
- memo = new ContractCompiler.#weakMap();
6876
+ if (filled !== scope) {
6877
+ memo = void 0;
6878
+ slot = void 0;
6879
+ kept = false;
6846
6880
  filled = scope;
6847
6881
  }
6848
- const recalled = INTRINSICS.apply(INTRINSICS.recall, memo, [value]);
6849
- if (recalled !== void 0) return recalled;
6882
+ if (slot === value) return kept;
6883
+ if (memo !== void 0) {
6884
+ const recalled = INTRINSICS.apply(INTRINSICS.recall, memo, [value]);
6885
+ if (recalled !== void 0) return recalled;
6886
+ }
6850
6887
  const answer = plan(value);
6851
- INTRINSICS.apply(INTRINSICS.retain, memo, [value, answer]);
6888
+ if (slot === void 0) {
6889
+ slot = value;
6890
+ kept = answer;
6891
+ } else {
6892
+ if (memo === void 0) {
6893
+ memo = new ContractCompiler.#weakMap();
6894
+ INTRINSICS.apply(INTRINSICS.retain, memo, [slot, kept]);
6895
+ }
6896
+ INTRINSICS.apply(INTRINSICS.retain, memo, [value, answer]);
6897
+ }
6852
6898
  return answer;
6853
6899
  } finally {
6854
6900
  if (opened) ContractCompiler.#scope = 0;
@@ -6858,6 +6904,8 @@ var ContractCompiler = class ContractCompiler {
6858
6904
  #trackFaults(plan) {
6859
6905
  let filled = 0;
6860
6906
  let memo;
6907
+ let slot;
6908
+ let kept;
6861
6909
  return (value, path) => {
6862
6910
  if (!isObject(value)) return plan(value, path);
6863
6911
  const opened = ContractCompiler.#scope === 0;
@@ -6867,14 +6915,28 @@ var ContractCompiler = class ContractCompiler {
6867
6915
  }
6868
6916
  try {
6869
6917
  const scope = ContractCompiler.#scope;
6870
- if (memo === void 0 || filled !== scope) {
6871
- memo = new ContractCompiler.#weakMap();
6918
+ if (filled !== scope) {
6919
+ memo = void 0;
6920
+ slot = void 0;
6921
+ kept = void 0;
6872
6922
  filled = scope;
6873
6923
  }
6874
- const recalled = INTRINSICS.apply(INTRINSICS.recall, memo, [value]);
6875
- if (recalled !== void 0) return recalled;
6924
+ if (slot === value && kept !== void 0) return kept;
6925
+ if (memo !== void 0) {
6926
+ const recalled = INTRINSICS.apply(INTRINSICS.recall, memo, [value]);
6927
+ if (recalled !== void 0) return recalled;
6928
+ }
6876
6929
  const answer = plan(value, path);
6877
- if (answer.length === 0) INTRINSICS.apply(INTRINSICS.retain, memo, [value, answer]);
6930
+ if (slot === void 0) {
6931
+ slot = value;
6932
+ kept = answer.length === 0 ? answer : void 0;
6933
+ } else if (answer.length === 0) {
6934
+ if (memo === void 0) {
6935
+ memo = new ContractCompiler.#weakMap();
6936
+ if (kept !== void 0) INTRINSICS.apply(INTRINSICS.retain, memo, [slot, kept]);
6937
+ }
6938
+ INTRINSICS.apply(INTRINSICS.retain, memo, [value, answer]);
6939
+ }
6878
6940
  return answer;
6879
6941
  } finally {
6880
6942
  if (opened) ContractCompiler.#scope = 0;
@@ -7069,16 +7131,37 @@ var ContractCompiler = class ContractCompiler {
7069
7131
  for (let optionalIndex = 0; optionalIndex < optionalKeys.length; optionalIndex += 1) if (optionalKeys[optionalIndex] === key) optional = true;
7070
7132
  if (!optional) required[required.length] = key;
7071
7133
  }
7134
+ const maskable = required.length <= 31;
7135
+ const positions = INTRINSICS.create(null);
7136
+ let full = 0;
7137
+ if (maskable) for (let keyIndex = 0; keyIndex < required.length; keyIndex += 1) {
7138
+ const key = required[keyIndex];
7139
+ if (key === void 0) continue;
7140
+ positions[key] = keyIndex;
7141
+ full |= 1 << keyIndex;
7142
+ }
7072
7143
  return (value) => {
7073
7144
  if (!isRecord(value)) return false;
7074
7145
  const keys = enumerableKeys(value);
7075
7146
  if (keys === void 0) return false;
7076
7147
  const outcome = attempt(() => {
7077
- const present = collectMembers(keys);
7078
- for (let keyIndex = 0; keyIndex < required.length; keyIndex += 1) {
7079
- const key = required[keyIndex];
7080
- if (key === void 0) continue;
7081
- if (!matchesMember(present, key)) return false;
7148
+ if (maskable) {
7149
+ let seen = 0;
7150
+ for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
7151
+ const key = keys[keyIndex];
7152
+ if (key === void 0) continue;
7153
+ if (!INTRINSICS.own(positions, key)) continue;
7154
+ const position = positions[key];
7155
+ if (position !== void 0) seen |= 1 << position;
7156
+ }
7157
+ if (seen !== full) return false;
7158
+ } else {
7159
+ const present = collectMembers(keys);
7160
+ for (let keyIndex = 0; keyIndex < required.length; keyIndex += 1) {
7161
+ const key = required[keyIndex];
7162
+ if (key === void 0) continue;
7163
+ if (!matchesMember(present, key)) return false;
7164
+ }
7082
7165
  }
7083
7166
  for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
7084
7167
  const key = keys[keyIndex];
@@ -7235,6 +7318,13 @@ var ContractCompiler = class ContractCompiler {
7235
7318
  subject: "object",
7236
7319
  context: { shape: "object" }
7237
7320
  });
7321
+ const maskable = entries.length <= 31;
7322
+ const positions = INTRINSICS.create(null);
7323
+ if (maskable) for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7324
+ const entry = entries[entryIndex];
7325
+ if (entry === void 0) continue;
7326
+ positions[entry.key] = entryIndex;
7327
+ }
7238
7328
  const extra = owned.additionalProperties;
7239
7329
  const additional = extra === void 0 || extra === false || extra === true ? void 0 : this.#parserAt(extra);
7240
7330
  const open = extra === true || additional !== void 0;
@@ -7244,12 +7334,20 @@ var ContractCompiler = class ContractCompiler {
7244
7334
  const keys = enumerableKeys(record);
7245
7335
  if (keys === void 0) return void 0;
7246
7336
  const outcome = attempt(() => {
7247
- const present = collectMembers(keys);
7337
+ const present = maskable ? void 0 : collectMembers(keys);
7338
+ let seen = 0;
7339
+ if (present === void 0) for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
7340
+ const key = keys[keyIndex];
7341
+ if (key === void 0) continue;
7342
+ if (!INTRINSICS.own(positions, key)) continue;
7343
+ const position = positions[key];
7344
+ if (position !== void 0) seen |= 1 << position;
7345
+ }
7248
7346
  const result = INTRINSICS.create(null);
7249
7347
  for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7250
7348
  const entry = entries[entryIndex];
7251
7349
  if (entry === void 0) continue;
7252
- if (!matchesMember(present, entry.key)) {
7350
+ if (present === void 0 ? (seen & 1 << entryIndex) === 0 : !matchesMember(present, entry.key)) {
7253
7351
  if (entry.optional) continue;
7254
7352
  return;
7255
7353
  }
@@ -7364,6 +7462,7 @@ var ContractCompiler = class ContractCompiler {
7364
7462
  switch (owned.type) {
7365
7463
  case "string": {
7366
7464
  const node = owned;
7465
+ const refined = owned.min !== void 0 || owned.max !== void 0 || owned.pattern !== void 0;
7367
7466
  return (value, path) => {
7368
7467
  if (!isString(value)) return [{
7369
7468
  reason: "type",
@@ -7371,12 +7470,13 @@ var ContractCompiler = class ContractCompiler {
7371
7470
  expected: "string",
7372
7471
  received: preview(value)
7373
7472
  }];
7374
- return createStringFaults(node, value, path);
7473
+ return refined ? createStringFaults(node, value, path) : [];
7375
7474
  };
7376
7475
  }
7377
7476
  case "number": {
7378
7477
  const node = owned;
7379
7478
  const kind = owned.integer === true ? "integer" : "number";
7479
+ const refined = owned.integer === true || owned.min !== void 0 || owned.max !== void 0;
7380
7480
  return (value, path) => {
7381
7481
  if (!isFiniteNumber(value)) return [{
7382
7482
  reason: "type",
@@ -7384,7 +7484,7 @@ var ContractCompiler = class ContractCompiler {
7384
7484
  expected: kind,
7385
7485
  received: preview(value)
7386
7486
  }];
7387
- return createNumberFaults(node, value, path);
7487
+ return refined ? createNumberFaults(node, value, path) : [];
7388
7488
  };
7389
7489
  }
7390
7490
  case "boolean": return (value, path) => isBoolean(value) ? [] : [{
@@ -7470,6 +7570,15 @@ var ContractCompiler = class ContractCompiler {
7470
7570
  const extra = owned.additionalProperties;
7471
7571
  const closed = extra === void 0 || extra === false;
7472
7572
  const additional = closed || extra === true ? void 0 : this.#auditAt(extra);
7573
+ const planned = attempt(() => collectMembers(declaredKeys));
7574
+ const declared = planned.success ? planned.value : void 0;
7575
+ const maskable = entries.length <= 31;
7576
+ const positions = INTRINSICS.create(null);
7577
+ if (maskable) for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7578
+ const entry = entries[entryIndex];
7579
+ if (entry === void 0) continue;
7580
+ positions[entry.key] = entryIndex;
7581
+ }
7473
7582
  return (value, path) => {
7474
7583
  if (isObject(value)) readValue(() => INTRINSICS.parent(value), "compileAuditor", {
7475
7584
  subject: "object",
@@ -7494,13 +7603,21 @@ var ContractCompiler = class ContractCompiler {
7494
7603
  });
7495
7604
  const faults = [];
7496
7605
  const outcome = attempt(() => {
7497
- const present = collectMembers(keys);
7498
- const declared = collectMembers(declaredKeys);
7606
+ const present = maskable ? void 0 : collectMembers(keys);
7607
+ const vocabulary = declared ?? collectMembers(declaredKeys);
7608
+ let seen = 0;
7609
+ if (present === void 0) for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
7610
+ const key = keys[keyIndex];
7611
+ if (key === void 0) continue;
7612
+ if (!INTRINSICS.own(positions, key)) continue;
7613
+ const position = positions[key];
7614
+ if (position !== void 0) seen |= 1 << position;
7615
+ }
7499
7616
  for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7500
7617
  const entry = entries[entryIndex];
7501
7618
  if (entry === void 0) continue;
7502
7619
  if (faults.length >= 64) return;
7503
- if (!matchesMember(present, entry.key)) {
7620
+ if (present === void 0 ? (seen & 1 << entryIndex) === 0 : !matchesMember(present, entry.key)) {
7504
7621
  if (!entry.optional) faults[faults.length] = {
7505
7622
  reason: "missing",
7506
7623
  path: pathOf(path, entry.key),
@@ -7514,7 +7631,7 @@ var ContractCompiler = class ContractCompiler {
7514
7631
  const key = keys[keyIndex];
7515
7632
  if (key === void 0) continue;
7516
7633
  if (faults.length >= 64) return;
7517
- if (matchesMember(declared, key)) continue;
7634
+ if (matchesMember(vocabulary, key)) continue;
7518
7635
  if (closed) {
7519
7636
  faults[faults.length] = {
7520
7637
  reason: "extra",
@@ -7557,8 +7674,11 @@ var ContractCompiler = class ContractCompiler {
7557
7674
  const plan = plans[index2];
7558
7675
  if (plan === void 0) continue;
7559
7676
  const variantFaults = plan(value, path);
7677
+ if (variantFaults.length === 0) {
7678
+ if (!exclusive) return [];
7679
+ matched += 1;
7680
+ }
7560
7681
  perVariant[perVariant.length] = variantFaults;
7561
- if (variantFaults.length === 0) matched += 1;
7562
7682
  }
7563
7683
  if (exclusive) {
7564
7684
  if (matched === 1) return [];
@@ -7567,7 +7687,7 @@ var ContractCompiler = class ContractCompiler {
7567
7687
  path,
7568
7688
  matched
7569
7689
  }];
7570
- } else if (matched > 0) return [];
7690
+ }
7571
7691
  const closest = selectClosestFaults(perVariant);
7572
7692
  const report = [exclusive ? {
7573
7693
  reason: "oneOf",
@@ -7632,6 +7752,7 @@ var ContractCompiler = class ContractCompiler {
7632
7752
  switch (owned.type) {
7633
7753
  case "string": {
7634
7754
  const node = owned;
7755
+ const refined = owned.min !== void 0 || owned.max !== void 0 || owned.pattern !== void 0;
7635
7756
  return (value, path) => {
7636
7757
  const parsed = parseString(value);
7637
7758
  if (parsed === void 0) return [{
@@ -7640,12 +7761,13 @@ var ContractCompiler = class ContractCompiler {
7640
7761
  expected: "string",
7641
7762
  received: preview(value)
7642
7763
  }];
7643
- return createStringFaults(node, parsed, path);
7764
+ return refined ? createStringFaults(node, parsed, path) : [];
7644
7765
  };
7645
7766
  }
7646
7767
  case "number": {
7647
7768
  const node = owned;
7648
7769
  const kind = owned.integer === true ? "integer" : "number";
7770
+ const refined = owned.integer === true || owned.min !== void 0 || owned.max !== void 0;
7649
7771
  return (value, path) => {
7650
7772
  const parsed = parseNumber(value);
7651
7773
  if (parsed === void 0) return [{
@@ -7654,7 +7776,7 @@ var ContractCompiler = class ContractCompiler {
7654
7776
  expected: kind,
7655
7777
  received: preview(value)
7656
7778
  }];
7657
- return createNumberFaults(node, parsed, path);
7779
+ return refined ? createNumberFaults(node, parsed, path) : [];
7658
7780
  };
7659
7781
  }
7660
7782
  case "boolean": return (value, path) => parseBoolean(value) === void 0 ? [{
@@ -7712,6 +7834,7 @@ var ContractCompiler = class ContractCompiler {
7712
7834
  }
7713
7835
  case "object": {
7714
7836
  const entries = [];
7837
+ const names = [];
7715
7838
  const keyList = INTRINSICS.keys(owned.properties);
7716
7839
  for (let keyIndex = 0; keyIndex < keyList.length; keyIndex += 1) {
7717
7840
  const key = keyList[keyIndex];
@@ -7726,10 +7849,20 @@ var ContractCompiler = class ContractCompiler {
7726
7849
  optional,
7727
7850
  kind: shapeToKind(inner)
7728
7851
  };
7852
+ names[names.length] = key;
7729
7853
  }
7730
7854
  const extra = owned.additionalProperties;
7731
7855
  const tail = extra === void 0 || extra === true || extra === false ? void 0 : this.#reportAt(extra);
7732
7856
  const open = extra === true || tail !== void 0;
7857
+ const planned = attempt(() => collectMembers(names));
7858
+ const known = planned.success ? planned.value : void 0;
7859
+ const maskable = entries.length <= 31;
7860
+ const positions = INTRINSICS.create(null);
7861
+ if (maskable) for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7862
+ const entry = entries[entryIndex];
7863
+ if (entry === void 0) continue;
7864
+ positions[entry.key] = entryIndex;
7865
+ }
7733
7866
  return (value, path) => {
7734
7867
  if (!isRecord(value)) return [{
7735
7868
  reason: "type",
@@ -7747,14 +7880,21 @@ var ContractCompiler = class ContractCompiler {
7747
7880
  }];
7748
7881
  const faults = [];
7749
7882
  if (!attempt(() => {
7750
- const present = collectMembers(keys);
7751
- const known = collectMembers([]);
7883
+ const present = maskable ? void 0 : collectMembers(keys);
7884
+ const vocabulary = known ?? collectMembers(names);
7885
+ let seen = 0;
7886
+ if (present === void 0) for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
7887
+ const key = keys[keyIndex];
7888
+ if (key === void 0) continue;
7889
+ if (!INTRINSICS.own(positions, key)) continue;
7890
+ const position = positions[key];
7891
+ if (position !== void 0) seen |= 1 << position;
7892
+ }
7752
7893
  for (let entryIndex = 0; entryIndex < entries.length; entryIndex += 1) {
7753
7894
  const entry = entries[entryIndex];
7754
7895
  if (entry === void 0) continue;
7755
7896
  if (faults.length >= 64) return;
7756
- admitMember(known, entry.key);
7757
- if (!matchesMember(present, entry.key)) {
7897
+ if (present === void 0 ? (seen & 1 << entryIndex) === 0 : !matchesMember(present, entry.key)) {
7758
7898
  if (!entry.optional) faults[faults.length] = {
7759
7899
  reason: "missing",
7760
7900
  path: pathOf(path, entry.key),
@@ -7777,7 +7917,7 @@ var ContractCompiler = class ContractCompiler {
7777
7917
  const key = keys[keyIndex];
7778
7918
  if (key === void 0) continue;
7779
7919
  if (faults.length >= 64) return;
7780
- if (matchesMember(known, key)) continue;
7920
+ if (matchesMember(vocabulary, key)) continue;
7781
7921
  const observed = record[key];
7782
7922
  if (tail !== void 0) appendEntries(faults, tail(observed, pathOf(path, key)));
7783
7923
  }
@@ -7806,7 +7946,9 @@ var ContractCompiler = class ContractCompiler {
7806
7946
  for (let index2 = 0; index2 < plans.length; index2 += 1) {
7807
7947
  const plan = plans[index2];
7808
7948
  if (plan === void 0) continue;
7809
- perVariant[perVariant.length] = plan(value, path);
7949
+ const variantFaults = plan(value, path);
7950
+ if (!exclusive && variantFaults.length === 0) return [];
7951
+ perVariant[perVariant.length] = variantFaults;
7810
7952
  }
7811
7953
  const closest = selectClosestFaults(perVariant);
7812
7954
  if (exclusive) {
@@ -7831,7 +7973,6 @@ var ContractCompiler = class ContractCompiler {
7831
7973
  matched
7832
7974
  }];
7833
7975
  }
7834
- for (let index2 = 0; index2 < perVariant.length; index2 += 1) if (perVariant[index2]?.length === 0) return [];
7835
7976
  const report = [{
7836
7977
  reason: "variant",
7837
7978
  path,
@@ -10391,6 +10532,7 @@ exports.INFER_ENUM_LIMIT = INFER_ENUM_LIMIT;
10391
10532
  exports.INTRINSICS = INTRINSICS;
10392
10533
  exports.JSONCloner = JSONCloner;
10393
10534
  exports.JSON_SCHEMA_TYPES = JSON_SCHEMA_TYPES;
10535
+ exports.PRESENCE_MASK_LIMIT = PRESENCE_MASK_LIMIT;
10394
10536
  exports.PREVIEW_LIMIT = PREVIEW_LIMIT;
10395
10537
  exports.SchemaCloner = SchemaCloner;
10396
10538
  exports.ShapeCloner = ShapeCloner;