@orkestrel/contract 0.0.12 → 0.0.14

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.
@@ -1,6 +1,17 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#region src/core/constants.ts
3
3
  /**
4
+ * The registry-global key used to recognize {@link ContractError} values across
5
+ * package copies.
6
+ *
7
+ * @remarks
8
+ * The descriptor stores the branded value itself. Recognition compares that
9
+ * identity, so a transparent proxy cannot forward its target's brand as its
10
+ * own. The registry makes the key discoverable; it is a recognition mechanism,
11
+ * not an unforgeable provenance marker.
12
+ */
13
+ var CONTRACT_ERROR_BRAND = Symbol.for("@orkestrel/contract.error");
14
+ /**
4
15
  * Every host operation this package dispatches through, captured while this
5
16
  * module evaluates.
6
17
  *
@@ -447,11 +458,9 @@ var FORMAT_PATTERNS = Object.freeze({
447
458
  * ```
448
459
  */
449
460
  var ContractError = class ContractError extends Error {
450
- static #hasOwn = Object.hasOwn;
451
461
  name = "ContractError";
452
462
  code;
453
463
  context;
454
- #brand = true;
455
464
  /**
456
465
  * Create a contract error.
457
466
  *
@@ -459,117 +468,44 @@ var ContractError = class ContractError extends Error {
459
468
  * @param options - Machine-readable category, optional context, and optional cause
460
469
  */
461
470
  constructor(message, options) {
462
- super(message, ContractError.#hasOwn(options, "cause") ? { cause: options.cause } : void 0);
471
+ super(message, INTRINSICS.own(options, "cause") ? { cause: options.cause } : void 0);
472
+ INTRINSICS.define(this, CONTRACT_ERROR_BRAND, {
473
+ value: this,
474
+ configurable: true
475
+ });
463
476
  this.code = options.code;
464
- this.context = ContractError.#hasOwn(options, "context") ? options.context : void 0;
465
- }
466
- /**
467
- * Hand the module the branded recognition test.
468
- *
469
- * @remarks
470
- * The bootstrap seam, and deliberately NOT a spelling of the recognition
471
- * question. Private-name membership is legal only inside this class body, so
472
- * something must bridge the class to the module — and the previous bridge was
473
- * a static that ANSWERED the question, `ContractError.owns(value)`. That
474
- * reintroduced, verbatim, the poisonable surface the private brand was chosen
475
- * to remove: one assignment made the package fail to recognize its own errors,
476
- * so an engine rewrapped an error it had authored as an unreadable failure,
477
- * and a throwing assignment put the caller's raw value through fifteen public
478
- * doors.
479
- *
480
- * This hands over the CLOSURE instead: {@link isContractError} calls it and
481
- * applies the result, so the only member on the recognition path hands the
482
- * test over rather than answering with it. That per-call read is not a seam,
483
- * because the static block below pins this member as a non-writable,
484
- * non-configurable own data property.
485
- *
486
- * An earlier round argued the pin needed no further defence, because a static
487
- * block runs during class definition and no order — this module's, an
488
- * importer's, or a module that evaluated first — reaches `ContractError.guard`
489
- * before the block runs. That is true and it answers the wrong question.
490
- * INSTALLING IS NOT READING. The concern was never that something reads the
491
- * member unpinned; it was whether the pin installs at all, and the pin is one
492
- * live dispatch through `Object.defineProperty`. A module that evaluated
493
- * first can replace it with a selective no-op, after which the block runs,
494
- * returns, installs nothing, and every later read finds a writable member
495
- * again — the whole `owns` defect restored, invisibly, in a realm where every
496
- * shipped test still passes.
497
- *
498
- * So the block VERIFIES its own work and refuses to define the class when the
499
- * pin did not take. A silent failure becomes a loud one at import. The
500
- * residual is named rather than denied: an adversary who also answers the
501
- * verifying descriptor read defeats it, and that adversary already chose what
502
- * every capture in this package holds.
503
- *
504
- * @returns The total recognition guard `isContractError` publishes
505
- *
506
- * @example
507
- * ```ts
508
- * ContractError.guard()(new ContractError('x', { code: 'clone' })) // true
509
- * ```
510
- */
511
- static guard() {
512
- return ContractError.#recognizes;
513
- }
514
- static #recognizes(value) {
515
- if (typeof value !== "object" || value === null) return false;
516
- try {
517
- return #brand in value && value.#brand === true;
518
- } catch {
519
- return false;
520
- }
477
+ this.context = INTRINSICS.own(options, "context") ? options.context : void 0;
521
478
  }
522
479
  static {
523
- Object.defineProperty(this, "guard", {
524
- writable: false,
525
- configurable: false
526
- });
527
- const guard = Object.getOwnPropertyDescriptor(this, "guard");
528
- if (guard?.writable !== false || guard.configurable !== false) throw new ContractError("ContractError: the recognition pin could not be installed", { code: "structure" });
529
- const members = Reflect.ownKeys(this.prototype);
480
+ const members = INTRINSICS.members(this.prototype);
530
481
  for (let index = 0; index < members.length; index += 1) {
531
482
  const key = members[index];
532
483
  if (key === void 0) continue;
533
- Object.defineProperty(this.prototype, key, {
484
+ INTRINSICS.define(this.prototype, key, {
534
485
  writable: false,
535
486
  configurable: false
536
487
  });
537
- const pinned = Object.getOwnPropertyDescriptor(this.prototype, key);
488
+ const pinned = INTRINSICS.describe(this.prototype, key);
538
489
  if (pinned?.writable !== false || pinned.configurable !== false) throw new ContractError("ContractError: a prototype member could not be pinned", { code: "structure" });
539
490
  }
540
491
  }
541
492
  };
542
493
  /**
543
- * Determine whether an unknown value is a {@link ContractError}.
544
- *
545
- * @remarks
546
- * The ONLY spelling of the recognition question this package publishes. The test
547
- * itself is a private static of the class, because private-name membership is
548
- * legal only inside a class body; the one member on the path,
549
- * {@link ContractError.guard}, hands that test over and is pinned non-writable
550
- * and non-configurable while the class is defined. So there is no property a
551
- * caller can point somewhere else, and no member that ANSWERS the recognition
552
- * question — which is exactly what the static that preceded it did.
553
- *
554
- * A function declaration rather than a `const` holding the retrieved closure.
555
- * The retrieved form was tried and was wrong twice over: a module-scope binding
556
- * bound to a computed value is module DATA, which belongs in a data kind file
557
- * and not in the file that owns the error classes and their guards; and the
558
- * single read it bought guarded a window that does not exist, since the pin is
559
- * installed during class definition and no code can observe this member before
560
- * it. Reading a permanently pinned own data property per call buys the caller
561
- * nothing, so the guard is spelled as what it is.
562
- *
563
- * Recognition is realm-local and identity-based: a value from another copy of
564
- * this module is not this class's error, exactly as `instanceof` reported; a
565
- * `Proxy` over a genuine error is refused, because a proxy holds no private
566
- * field of its target; and `Reflect.construct(ContractError, args, Foreign)` IS
567
- * recognized, because that construction genuinely ran this constructor and only
568
- * the prototype was the caller's choice. That last consequence is a widening
569
- * rather than a narrowing, and it is named rather than left to be discovered.
494
+ * Checks whether an unknown value is a {@link ContractError}.
495
+ *
496
+ * @remarks
497
+ * Recognition combines a global own-property brand with the native `Error`
498
+ * base, a subclass prototype, the fixed name, and a declared contract code.
499
+ * The brand stores the error itself and recognition requires that exact
500
+ * identity. A transparent proxy is therefore refused because its forwarded
501
+ * descriptor still stores the target, not the proxy.
502
+ * The brand is recognized across duplicate installations and ESM/CommonJS
503
+ * module copies at 0.0.13 or later. A copy earlier than 0.0.13 stamps no brand,
504
+ * so an error it throws stays outside the type, and so does a plain or
505
+ * property-only lookalike.
570
506
  *
571
507
  * @param value - The value to inspect
572
- * @returns `true` only for a value this class's constructor branded
508
+ * @returns True only for a `ContractError` instance; false otherwise
573
509
  *
574
510
  * @example
575
511
  * ```ts
@@ -578,7 +514,16 @@ var ContractError = class ContractError extends Error {
578
514
  * ```
579
515
  */
580
516
  function isContractError(value) {
581
- return ContractError.guard()(value);
517
+ if (typeof value !== "object" || value === null) return false;
518
+ try {
519
+ if (!(value instanceof INTRINSICS.error) || INTRINSICS.prototype(value) === INTRINSICS.error.prototype) return false;
520
+ if (value.name !== "ContractError" || !("code" in value)) return false;
521
+ if (INTRINSICS.describe(value, CONTRACT_ERROR_BRAND)?.value !== value) return false;
522
+ const code = value.code;
523
+ return code === "bound" || code === "range" || code === "empty" || code === "placement" || code === "structure" || code === "literal" || code === "cycle" || code === "pattern" || code === "generate" || code === "random" || code === "clone" || code === "depth" || code === "expansion";
524
+ } catch {
525
+ return false;
526
+ }
582
527
  }
583
528
  //#endregion
584
529
  //#region src/core/validators.ts
@@ -6558,28 +6503,38 @@ var ContractCompiler = class ContractCompiler {
6558
6503
  static #weakMap = WeakMap;
6559
6504
  static #visits = 0;
6560
6505
  static #scope = 0;
6506
+ static #emptyStack = [];
6507
+ static #emptyNodes = [];
6508
+ static #emptyOrder = [];
6509
+ static #emptySchemas = [];
6510
+ static #emptyGuards = [];
6511
+ static #emptyParsers = [];
6512
+ static #emptyAudits = [];
6513
+ static #emptyReports = [];
6514
+ static #emptySeeds = [];
6515
+ static {
6516
+ INTRINSICS.freeze(ContractCompiler.#emptyStack);
6517
+ INTRINSICS.freeze(ContractCompiler.#emptyNodes);
6518
+ INTRINSICS.freeze(ContractCompiler.#emptyOrder);
6519
+ INTRINSICS.freeze(ContractCompiler.#emptySchemas);
6520
+ INTRINSICS.freeze(ContractCompiler.#emptyGuards);
6521
+ INTRINSICS.freeze(ContractCompiler.#emptyParsers);
6522
+ INTRINSICS.freeze(ContractCompiler.#emptyAudits);
6523
+ INTRINSICS.freeze(ContractCompiler.#emptyReports);
6524
+ INTRINSICS.freeze(ContractCompiler.#emptySeeds);
6525
+ }
6561
6526
  #source;
6562
6527
  #state;
6563
6528
  #stack;
6564
- #emptyStack;
6565
6529
  #nodes;
6566
- #emptyNodes;
6567
6530
  #index;
6568
- #emptyIndex;
6569
6531
  #order;
6570
- #emptyOrder;
6571
6532
  #schemas;
6572
- #emptySchemas;
6573
6533
  #guards;
6574
- #emptyGuards;
6575
6534
  #parsers;
6576
- #emptyParsers;
6577
6535
  #audits;
6578
- #emptyAudits;
6579
6536
  #reports;
6580
- #emptyReports;
6581
6537
  #seeds;
6582
- #emptySeeds;
6583
6538
  #schema;
6584
6539
  #guard;
6585
6540
  #parser;
@@ -6596,25 +6551,15 @@ var ContractCompiler = class ContractCompiler {
6596
6551
  this.#source = shape;
6597
6552
  this.#state = { phase: "ready" };
6598
6553
  this.#stack = [];
6599
- this.#emptyStack = [];
6600
6554
  this.#nodes = [];
6601
- this.#emptyNodes = [];
6602
6555
  this.#index = new ContractCompiler.#weakMap();
6603
- this.#emptyIndex = new ContractCompiler.#weakMap();
6604
6556
  this.#order = [];
6605
- this.#emptyOrder = [];
6606
6557
  this.#schemas = [];
6607
- this.#emptySchemas = [];
6608
6558
  this.#guards = [];
6609
- this.#emptyGuards = [];
6610
6559
  this.#parsers = [];
6611
- this.#emptyParsers = [];
6612
6560
  this.#audits = [];
6613
- this.#emptyAudits = [];
6614
6561
  this.#reports = [];
6615
- this.#emptyReports = [];
6616
6562
  this.#seeds = [];
6617
- this.#emptySeeds = [];
6618
6563
  this.#schema = void 0;
6619
6564
  this.#guard = void 0;
6620
6565
  this.#parser = void 0;
@@ -6736,16 +6681,16 @@ var ContractCompiler = class ContractCompiler {
6736
6681
  }
6737
6682
  #release() {
6738
6683
  this.#source = void 0;
6739
- this.#stack = this.#emptyStack;
6740
- this.#nodes = this.#emptyNodes;
6741
- this.#index = this.#emptyIndex;
6742
- this.#order = this.#emptyOrder;
6743
- this.#schemas = this.#emptySchemas;
6744
- this.#guards = this.#emptyGuards;
6745
- this.#parsers = this.#emptyParsers;
6746
- this.#audits = this.#emptyAudits;
6747
- this.#reports = this.#emptyReports;
6748
- this.#seeds = this.#emptySeeds;
6684
+ this.#stack = ContractCompiler.#emptyStack;
6685
+ this.#nodes = ContractCompiler.#emptyNodes;
6686
+ this.#index = void 0;
6687
+ this.#order = ContractCompiler.#emptyOrder;
6688
+ this.#schemas = ContractCompiler.#emptySchemas;
6689
+ this.#guards = ContractCompiler.#emptyGuards;
6690
+ this.#parsers = ContractCompiler.#emptyParsers;
6691
+ this.#audits = ContractCompiler.#emptyAudits;
6692
+ this.#reports = ContractCompiler.#emptyReports;
6693
+ this.#seeds = ContractCompiler.#emptySeeds;
6749
6694
  }
6750
6695
  #prepare() {
6751
6696
  if (this.#nodes.length > 0) return;
@@ -6765,6 +6710,14 @@ var ContractCompiler = class ContractCompiler {
6765
6710
  this.#source = void 0;
6766
6711
  }
6767
6712
  #discover(root) {
6713
+ const known = this.#index;
6714
+ if (known === void 0) throw new ContractError("ContractCompiler: the prepared index is unavailable", {
6715
+ code: "structure",
6716
+ context: {
6717
+ path: [],
6718
+ shape: "contract"
6719
+ }
6720
+ });
6768
6721
  this.#stack[this.#stack.length] = {
6769
6722
  operation: "enter",
6770
6723
  shape: root
@@ -6779,10 +6732,10 @@ var ContractCompiler = class ContractCompiler {
6779
6732
  continue;
6780
6733
  }
6781
6734
  const shape = frame.shape;
6782
- if (INTRINSICS.apply(INTRINSICS.recall, this.#index, [shape]) !== void 0) continue;
6735
+ if (INTRINSICS.apply(INTRINSICS.recall, known, [shape]) !== void 0) continue;
6783
6736
  const index = this.#nodes.length;
6784
6737
  this.#nodes[index] = shape;
6785
- INTRINSICS.apply(INTRINSICS.retain, this.#index, [shape, index]);
6738
+ INTRINSICS.apply(INTRINSICS.retain, known, [shape, index]);
6786
6739
  this.#stack[this.#stack.length] = {
6787
6740
  operation: "exit",
6788
6741
  index
@@ -6838,7 +6791,15 @@ var ContractCompiler = class ContractCompiler {
6838
6791
  }
6839
6792
  }
6840
6793
  #locate(shape) {
6841
- const index = INTRINSICS.apply(INTRINSICS.recall, this.#index, [shape]);
6794
+ const known = this.#index;
6795
+ if (known === void 0) throw new ContractError("ContractCompiler: the prepared index is unavailable", {
6796
+ code: "structure",
6797
+ context: {
6798
+ path: [],
6799
+ shape: "contract"
6800
+ }
6801
+ });
6802
+ const index = INTRINSICS.apply(INTRINSICS.recall, known, [shape]);
6842
6803
  if (index === void 0) throw new ContractError("ContractCompiler: a structural child is not in the prepared index", {
6843
6804
  code: "structure",
6844
6805
  context: {
@@ -6870,7 +6831,7 @@ var ContractCompiler = class ContractCompiler {
6870
6831
  }
6871
6832
  #trackGuard(plan) {
6872
6833
  let filled = 0;
6873
- let memo = new ContractCompiler.#weakMap();
6834
+ let memo;
6874
6835
  return (value) => {
6875
6836
  if (!isObject(value)) return plan(value);
6876
6837
  const opened = ContractCompiler.#scope === 0;
@@ -6880,7 +6841,7 @@ var ContractCompiler = class ContractCompiler {
6880
6841
  }
6881
6842
  try {
6882
6843
  const scope = ContractCompiler.#scope;
6883
- if (filled !== scope) {
6844
+ if (memo === void 0 || filled !== scope) {
6884
6845
  memo = new ContractCompiler.#weakMap();
6885
6846
  filled = scope;
6886
6847
  }
@@ -6896,7 +6857,7 @@ var ContractCompiler = class ContractCompiler {
6896
6857
  }
6897
6858
  #trackFaults(plan) {
6898
6859
  let filled = 0;
6899
- let memo = new ContractCompiler.#weakMap();
6860
+ let memo;
6900
6861
  return (value, path) => {
6901
6862
  if (!isObject(value)) return plan(value, path);
6902
6863
  const opened = ContractCompiler.#scope === 0;
@@ -6906,7 +6867,7 @@ var ContractCompiler = class ContractCompiler {
6906
6867
  }
6907
6868
  try {
6908
6869
  const scope = ContractCompiler.#scope;
6909
- if (filled !== scope) {
6870
+ if (memo === void 0 || filled !== scope) {
6910
6871
  memo = new ContractCompiler.#weakMap();
6911
6872
  filled = scope;
6912
6873
  }
@@ -10416,6 +10377,7 @@ function schemaToShape(schema) {
10416
10377
  exports.CLONE_NODE_LIMIT = CLONE_NODE_LIMIT;
10417
10378
  exports.COMPILE_DEPTH_LIMIT = COMPILE_DEPTH_LIMIT;
10418
10379
  exports.COMPILE_NODE_LIMIT = COMPILE_NODE_LIMIT;
10380
+ exports.CONTRACT_ERROR_BRAND = CONTRACT_ERROR_BRAND;
10419
10381
  exports.ContractCompiler = ContractCompiler;
10420
10382
  exports.ContractError = ContractError;
10421
10383
  exports.FAULT_LIMIT = FAULT_LIMIT;