@orkestrel/contract 0.0.12 → 0.0.13

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
@@ -10416,6 +10361,7 @@ function schemaToShape(schema) {
10416
10361
  exports.CLONE_NODE_LIMIT = CLONE_NODE_LIMIT;
10417
10362
  exports.COMPILE_DEPTH_LIMIT = COMPILE_DEPTH_LIMIT;
10418
10363
  exports.COMPILE_NODE_LIMIT = COMPILE_NODE_LIMIT;
10364
+ exports.CONTRACT_ERROR_BRAND = CONTRACT_ERROR_BRAND;
10419
10365
  exports.ContractCompiler = ContractCompiler;
10420
10366
  exports.ContractError = ContractError;
10421
10367
  exports.FAULT_LIMIT = FAULT_LIMIT;