@orkestrel/contract 0.0.11 → 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.
- package/dist/src/core/index.cjs +145 -126
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +71 -85
- package/dist/src/core/index.d.ts +71 -85
- package/dist/src/core/index.js +143 -127
- package/dist/src/core/index.js.map +1 -1
- package/package.json +14 -11
package/dist/src/core/index.cjs
CHANGED
|
@@ -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,
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
484
|
+
INTRINSICS.define(this.prototype, key, {
|
|
534
485
|
writable: false,
|
|
535
486
|
configurable: false
|
|
536
487
|
});
|
|
537
|
-
const pinned =
|
|
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
|
-
*
|
|
544
|
-
*
|
|
545
|
-
* @remarks
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
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
|
|
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
|
-
|
|
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
|
|
@@ -2478,6 +2423,61 @@ function readArrayEntries(value) {
|
|
|
2478
2423
|
});
|
|
2479
2424
|
}
|
|
2480
2425
|
/**
|
|
2426
|
+
* Snapshot a guard shape and its optional-key mode for a shape combinator.
|
|
2427
|
+
*
|
|
2428
|
+
* @remarks
|
|
2429
|
+
* A null-prototype record plus its own key list is used instead of a `Map`.
|
|
2430
|
+
* The declared-key population decides the guard's answer, and
|
|
2431
|
+
* `Map.prototype.has`, `Map.prototype.get`, and map iteration are three
|
|
2432
|
+
* caller-writable members on that path. An own data key read by index
|
|
2433
|
+
* dispatches through nothing.
|
|
2434
|
+
*
|
|
2435
|
+
* @param shape - The guard shape whose own string declarations to snapshot
|
|
2436
|
+
* @param optional - The optional-key list, `true` for every key, or `undefined`
|
|
2437
|
+
* @param reader - The public combinator name used in read refusals
|
|
2438
|
+
* @returns The owned guards and names plus the collected optional-key membership
|
|
2439
|
+
* @throws {ContractError} When the shape or optional-key list cannot be read
|
|
2440
|
+
*
|
|
2441
|
+
* @example
|
|
2442
|
+
* ```ts
|
|
2443
|
+
* readGuardShape({ id: isString }, undefined, 'recordOf')
|
|
2444
|
+
* ```
|
|
2445
|
+
*/
|
|
2446
|
+
function readGuardShape(shape, optional, reader) {
|
|
2447
|
+
const declared = readValue(() => {
|
|
2448
|
+
const members = INTRINSICS.members(shape);
|
|
2449
|
+
const guards = INTRINSICS.create(null);
|
|
2450
|
+
const names = [];
|
|
2451
|
+
for (let index = 0; index < members.length; index += 1) {
|
|
2452
|
+
const key = members[index];
|
|
2453
|
+
if (!isString(key)) continue;
|
|
2454
|
+
if (!INTRINSICS.own(guards, key)) names[names.length] = key;
|
|
2455
|
+
guards[key] = shape[key];
|
|
2456
|
+
}
|
|
2457
|
+
return {
|
|
2458
|
+
guards,
|
|
2459
|
+
names,
|
|
2460
|
+
vocabulary: collectMembers(names)
|
|
2461
|
+
};
|
|
2462
|
+
}, reader, { subject: "shape" });
|
|
2463
|
+
const optionalKeys = readValue(() => {
|
|
2464
|
+
if (optional === true) return collectMembers(declared.names);
|
|
2465
|
+
if (!INTRINSICS.array(optional)) return collectMembers([]);
|
|
2466
|
+
const entries = readArrayEntries(optional);
|
|
2467
|
+
if (!entries.success) throw entries.error;
|
|
2468
|
+
if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
|
|
2469
|
+
const keys = collectMembers([]);
|
|
2470
|
+
for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
|
|
2471
|
+
return keys;
|
|
2472
|
+
}, reader, { subject: "optional" });
|
|
2473
|
+
return {
|
|
2474
|
+
guards: declared.guards,
|
|
2475
|
+
names: declared.names,
|
|
2476
|
+
optional: optionalKeys,
|
|
2477
|
+
vocabulary: declared.vocabulary
|
|
2478
|
+
};
|
|
2479
|
+
}
|
|
2480
|
+
/**
|
|
2481
2481
|
* Snapshot an object's own enumerable string keys through a total boundary.
|
|
2482
2482
|
*
|
|
2483
2483
|
* @remarks
|
|
@@ -5704,32 +5704,7 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
5704
5704
|
*/
|
|
5705
5705
|
function recordOf(shape, optional) {
|
|
5706
5706
|
return contain(() => {
|
|
5707
|
-
const declared =
|
|
5708
|
-
const members = INTRINSICS.members(shape);
|
|
5709
|
-
const collected = INTRINSICS.create(null);
|
|
5710
|
-
const names = [];
|
|
5711
|
-
for (let index = 0; index < members.length; index += 1) {
|
|
5712
|
-
const key = members[index];
|
|
5713
|
-
if (!isString(key)) continue;
|
|
5714
|
-
if (!INTRINSICS.own(collected, key)) names[names.length] = key;
|
|
5715
|
-
collected[key] = shape[key];
|
|
5716
|
-
}
|
|
5717
|
-
return {
|
|
5718
|
-
collected,
|
|
5719
|
-
names,
|
|
5720
|
-
vocabulary: collectMembers(names)
|
|
5721
|
-
};
|
|
5722
|
-
}, "recordOf", { subject: "shape" });
|
|
5723
|
-
const optionalKeys = readValue(() => {
|
|
5724
|
-
if (optional === true) return collectMembers(declared.names);
|
|
5725
|
-
if (!INTRINSICS.array(optional)) return collectMembers([]);
|
|
5726
|
-
const entries = readArrayEntries(optional);
|
|
5727
|
-
if (!entries.success) throw entries.error;
|
|
5728
|
-
if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
|
|
5729
|
-
const keys = collectMembers([]);
|
|
5730
|
-
for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
|
|
5731
|
-
return keys;
|
|
5732
|
-
}, "recordOf", { subject: "optional" });
|
|
5707
|
+
const declared = readGuardShape(shape, optional, "recordOf");
|
|
5733
5708
|
return (value) => holds(() => {
|
|
5734
5709
|
if (!isRecord(value)) return false;
|
|
5735
5710
|
const members = INTRINSICS.members(value);
|
|
@@ -5740,9 +5715,9 @@ function recordOf(shape, optional) {
|
|
|
5740
5715
|
for (let index = 0; index < declared.names.length; index += 1) {
|
|
5741
5716
|
const key = declared.names[index];
|
|
5742
5717
|
if (key === void 0) continue;
|
|
5743
|
-
const guard = declared.
|
|
5718
|
+
const guard = declared.guards[key];
|
|
5744
5719
|
const present = INTRINSICS.own(value, key);
|
|
5745
|
-
if (!matchesMember(
|
|
5720
|
+
if (!matchesMember(declared.optional, key) && !present) return false;
|
|
5746
5721
|
if (present) {
|
|
5747
5722
|
if (guard === void 0 || !guard(value[key])) return false;
|
|
5748
5723
|
}
|
|
@@ -5752,6 +5727,47 @@ function recordOf(shape, optional) {
|
|
|
5752
5727
|
}, "recordOf");
|
|
5753
5728
|
}
|
|
5754
5729
|
/**
|
|
5730
|
+
* Build a guard that accepts non-array objects matching an open guard shape.
|
|
5731
|
+
*
|
|
5732
|
+
* @remarks
|
|
5733
|
+
* Three calling modes mirror {@link recordOf}:
|
|
5734
|
+
* - **No `optional`** — all shape keys required; unknown members admitted.
|
|
5735
|
+
* - **`optional: K[]`** — the listed keys are optional; all others required.
|
|
5736
|
+
* - **`optional: true`** — every shape key is optional.
|
|
5737
|
+
*
|
|
5738
|
+
* Each declared member is read through `Reflect.get`, so inherited data and
|
|
5739
|
+
* prototype accessors can satisfy the shape. An optional member passes when its
|
|
5740
|
+
* read value is `undefined`; otherwise its guard must accept the value. Unknown
|
|
5741
|
+
* members are never enumerated or inspected. Member-carrying functions are
|
|
5742
|
+
* accepted. Arrays, `null`, and primitives are rejected. Hostile reads return
|
|
5743
|
+
* `false` rather than throwing.
|
|
5744
|
+
*
|
|
5745
|
+
* @example
|
|
5746
|
+
* ```ts
|
|
5747
|
+
* const isResult = objectOf({ conclusion: isBoolean })
|
|
5748
|
+
* isResult({ conclusion: true, metadata: 'retained' }) // true
|
|
5749
|
+
* isResult([]) // false
|
|
5750
|
+
* ```
|
|
5751
|
+
*/
|
|
5752
|
+
function objectOf(shape, optional) {
|
|
5753
|
+
return contain(() => {
|
|
5754
|
+
const declared = readGuardShape(shape, optional, "objectOf");
|
|
5755
|
+
return (value) => holds(() => {
|
|
5756
|
+
if (!isObject(value) && !isFunction(value) || INTRINSICS.array(value)) return false;
|
|
5757
|
+
for (let index = 0; index < declared.names.length; index += 1) {
|
|
5758
|
+
const key = declared.names[index];
|
|
5759
|
+
if (key === void 0) continue;
|
|
5760
|
+
const guard = declared.guards[key];
|
|
5761
|
+
const member = INTRINSICS.read(value, key);
|
|
5762
|
+
if (matchesMember(declared.optional, key)) {
|
|
5763
|
+
if (member !== void 0 && (guard === void 0 || !guard(member))) return false;
|
|
5764
|
+
} else if (guard === void 0 || !guard(member)) return false;
|
|
5765
|
+
}
|
|
5766
|
+
return true;
|
|
5767
|
+
});
|
|
5768
|
+
}, "objectOf");
|
|
5769
|
+
}
|
|
5770
|
+
/**
|
|
5755
5771
|
* Build a guard that accepts values that are own keys of the provided object.
|
|
5756
5772
|
*
|
|
5757
5773
|
* @remarks
|
|
@@ -10345,6 +10361,7 @@ function schemaToShape(schema) {
|
|
|
10345
10361
|
exports.CLONE_NODE_LIMIT = CLONE_NODE_LIMIT;
|
|
10346
10362
|
exports.COMPILE_DEPTH_LIMIT = COMPILE_DEPTH_LIMIT;
|
|
10347
10363
|
exports.COMPILE_NODE_LIMIT = COMPILE_NODE_LIMIT;
|
|
10364
|
+
exports.CONTRACT_ERROR_BRAND = CONTRACT_ERROR_BRAND;
|
|
10348
10365
|
exports.ContractCompiler = ContractCompiler;
|
|
10349
10366
|
exports.ContractError = ContractError;
|
|
10350
10367
|
exports.FAULT_LIMIT = FAULT_LIMIT;
|
|
@@ -10505,6 +10522,7 @@ exports.nullShape = nullShape;
|
|
|
10505
10522
|
exports.nullableOf = nullableOf;
|
|
10506
10523
|
exports.nullableShape = nullableShape;
|
|
10507
10524
|
exports.numberShape = numberShape;
|
|
10525
|
+
exports.objectOf = objectOf;
|
|
10508
10526
|
exports.objectShape = objectShape;
|
|
10509
10527
|
exports.omitOf = omitOf;
|
|
10510
10528
|
exports.omitVisited = omitVisited;
|
|
@@ -10539,6 +10557,7 @@ exports.pinMembers = pinMembers;
|
|
|
10539
10557
|
exports.preview = preview;
|
|
10540
10558
|
exports.rawShape = rawShape;
|
|
10541
10559
|
exports.readArrayEntries = readArrayEntries;
|
|
10560
|
+
exports.readGuardShape = readGuardShape;
|
|
10542
10561
|
exports.readMapEntries = readMapEntries;
|
|
10543
10562
|
exports.readOptions = readOptions;
|
|
10544
10563
|
exports.readPattern = readPattern;
|