@orkestrel/contract 0.0.16 → 0.0.17
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/README.md +10 -4
- package/dist/src/core/index.cjs +112 -20
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +297 -35
- package/dist/src/core/index.d.ts +297 -35
- package/dist/src/core/index.js +112 -20
- package/dist/src/core/index.js.map +1 -1
- package/package.json +7 -8
package/dist/src/core/index.js
CHANGED
|
@@ -286,7 +286,7 @@ var CONTRACT_CODES = Object.freeze([
|
|
|
286
286
|
"expansion"
|
|
287
287
|
]);
|
|
288
288
|
/**
|
|
289
|
-
* Caps the number of {@link Fault} / {@link AuditFault} entries a single
|
|
289
|
+
* Caps at `64` the number of {@link Fault} / {@link AuditFault} entries a single
|
|
290
290
|
* `explain` or `audit` report ever returns, frozen.
|
|
291
291
|
*
|
|
292
292
|
* @remarks
|
|
@@ -300,7 +300,7 @@ var CONTRACT_CODES = Object.freeze([
|
|
|
300
300
|
*/
|
|
301
301
|
var FAULT_LIMIT = 64;
|
|
302
302
|
/**
|
|
303
|
-
* Caps the character length of a {@link preview}-rendered string, frozen.
|
|
303
|
+
* Caps at `64` the character length of a {@link preview}-rendered string, frozen.
|
|
304
304
|
*
|
|
305
305
|
* @remarks
|
|
306
306
|
* A previewed string longer than this is clipped with a trailing `…` so a
|
|
@@ -325,7 +325,7 @@ var PREVIEW_LIMIT = 64;
|
|
|
325
325
|
*/
|
|
326
326
|
var GUARD_DEPTH_LIMIT = 512;
|
|
327
327
|
/**
|
|
328
|
-
* Caps the supported nesting depth of a compiled contract shape, frozen.
|
|
328
|
+
* Caps at `512` the supported nesting depth of a compiled contract shape, frozen.
|
|
329
329
|
*
|
|
330
330
|
* @remarks
|
|
331
331
|
* {@link validateShape} rejects the next level
|
|
@@ -340,7 +340,8 @@ var GUARD_DEPTH_LIMIT = 512;
|
|
|
340
340
|
*/
|
|
341
341
|
var COMPILE_DEPTH_LIMIT = 512;
|
|
342
342
|
/**
|
|
343
|
-
* Caps the number of nodes a compiled artifact may expand a shape into,
|
|
343
|
+
* Caps at `16384` the number of nodes a compiled artifact may expand a shape into,
|
|
344
|
+
* frozen.
|
|
344
345
|
*
|
|
345
346
|
* @remarks
|
|
346
347
|
* A shape graph is a DAG; every compiled artifact is a TREE. A declaration may
|
|
@@ -375,7 +376,7 @@ var COMPILE_DEPTH_LIMIT = 512;
|
|
|
375
376
|
*/
|
|
376
377
|
var COMPILE_NODE_LIMIT = 16384;
|
|
377
378
|
/**
|
|
378
|
-
* Caps the number of object keys one compiled presence mask carries, frozen.
|
|
379
|
+
* Caps at `31` the number of object keys one compiled presence mask carries, frozen.
|
|
379
380
|
*
|
|
380
381
|
* @remarks
|
|
381
382
|
* A compiled object plan decides which declared keys a value carries. The keys
|
|
@@ -399,7 +400,7 @@ var COMPILE_NODE_LIMIT = 16384;
|
|
|
399
400
|
*/
|
|
400
401
|
var PRESENCE_MASK_LIMIT = 31;
|
|
401
402
|
/**
|
|
402
|
-
* Caps the number of nodes one JSON snapshot may produce, frozen.
|
|
403
|
+
* Caps at `262144` the number of nodes one JSON snapshot may produce, frozen.
|
|
403
404
|
*
|
|
404
405
|
* @remarks
|
|
405
406
|
* JSON persistence is a TREE, so `cloneJSONValue` / `cloneJSONRecord`
|
|
@@ -426,8 +427,8 @@ var PRESENCE_MASK_LIMIT = 31;
|
|
|
426
427
|
*/
|
|
427
428
|
var CLONE_NODE_LIMIT = 262144;
|
|
428
429
|
/**
|
|
429
|
-
* Caps the number of candidate-generation attempts for a constrained
|
|
430
|
-
*
|
|
430
|
+
* Caps at `32` the number of candidate-generation attempts for a constrained generated
|
|
431
|
+
* value, frozen.
|
|
431
432
|
*
|
|
432
433
|
* @remarks
|
|
433
434
|
* Provides one deterministic work bound for generators that must retry a
|
|
@@ -440,7 +441,7 @@ var CLONE_NODE_LIMIT = 262144;
|
|
|
440
441
|
*/
|
|
441
442
|
var GENERATION_ATTEMPT_LIMIT = 32;
|
|
442
443
|
/**
|
|
443
|
-
* Caps the object/array nesting depth {@link valueToSchema} walks, frozen.
|
|
444
|
+
* Caps at `32` the object/array nesting depth {@link valueToSchema} walks, frozen.
|
|
444
445
|
*
|
|
445
446
|
* @remarks
|
|
446
447
|
* Bounds inference against adversarial or cyclic runtime input — once the
|
|
@@ -458,8 +459,8 @@ var GENERATION_ATTEMPT_LIMIT = 32;
|
|
|
458
459
|
*/
|
|
459
460
|
var INFER_DEPTH_LIMIT = 32;
|
|
460
461
|
/**
|
|
461
|
-
* Caps by default the number of object properties / array elements
|
|
462
|
-
*
|
|
462
|
+
* Caps by default at `256` the number of object properties / array elements {@link
|
|
463
|
+
* valueToSchema} samples per container, frozen.
|
|
463
464
|
*
|
|
464
465
|
* @remarks
|
|
465
466
|
* Bounds the work (and the emitted schema's size) against a wide record or a
|
|
@@ -468,7 +469,7 @@ var INFER_DEPTH_LIMIT = 32;
|
|
|
468
469
|
*/
|
|
469
470
|
var INFER_BREADTH_LIMIT = 256;
|
|
470
471
|
/**
|
|
471
|
-
* Caps by default the number of distinct values a multi-sample slot may hold
|
|
472
|
+
* Caps by default at `12` the number of distinct values a multi-sample slot may hold
|
|
472
473
|
* before enum inference gives up and falls back to a bare `type`, frozen.
|
|
473
474
|
*
|
|
474
475
|
* @remarks
|
|
@@ -479,7 +480,7 @@ var INFER_BREADTH_LIMIT = 256;
|
|
|
479
480
|
*/
|
|
480
481
|
var INFER_ENUM_LIMIT = 12;
|
|
481
482
|
/**
|
|
482
|
-
* Caps the string length {@link stringToFormat} attempts to classify, frozen.
|
|
483
|
+
* Caps at `128` the string length {@link stringToFormat} attempts to classify, frozen.
|
|
483
484
|
*
|
|
484
485
|
* @remarks
|
|
485
486
|
* Bounds per-string format-detection work: a value longer than this returns
|
|
@@ -507,11 +508,14 @@ var FORMAT_PATTERNS = Object.freeze({
|
|
|
507
508
|
//#endregion
|
|
508
509
|
//#region src/core/errors.ts
|
|
509
510
|
/**
|
|
510
|
-
* Carries a machine-readable contract category, optional context, and an exact
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
511
|
+
* Carries a machine-readable contract category, optional context, and an exact optional
|
|
512
|
+
* cause.
|
|
513
|
+
*
|
|
514
|
+
* @remarks
|
|
515
|
+
* Omitting `cause` omits the own property; explicitly supplying `cause: undefined`
|
|
516
|
+
* retains an own property with that value. Both optional options are read as OWN
|
|
517
|
+
* properties, so a construction never consults the caller-writable prototype chain of
|
|
518
|
+
* the container it was handed.
|
|
515
519
|
*
|
|
516
520
|
* @example
|
|
517
521
|
* ```ts
|
|
@@ -932,6 +936,11 @@ function isDate(value) {
|
|
|
932
936
|
}
|
|
933
937
|
/** Determines whether a value is a `RegExp`.
|
|
934
938
|
*
|
|
939
|
+
* @remarks
|
|
940
|
+
* A total native internal-slot brand check rather than a property read: it accepts a
|
|
941
|
+
* genuine pattern from this realm and from another, and refuses a proxy or a hand-built
|
|
942
|
+
* forgery without reading an advertised field.
|
|
943
|
+
*
|
|
935
944
|
* @param value - The value to inspect
|
|
936
945
|
* @returns True if the value is a `RegExp`; false otherwise
|
|
937
946
|
*
|
|
@@ -1177,6 +1186,10 @@ function isWeakSet(value) {
|
|
|
1177
1186
|
}
|
|
1178
1187
|
/** Determines whether a value is an array.
|
|
1179
1188
|
*
|
|
1189
|
+
* @remarks
|
|
1190
|
+
* Checks the container alone — no element is inspected. Use {@link arrayOf} to check
|
|
1191
|
+
* every element against a guard.
|
|
1192
|
+
*
|
|
1180
1193
|
* @param value - The value to inspect
|
|
1181
1194
|
* @returns True if the value is an array; false otherwise
|
|
1182
1195
|
*
|
|
@@ -1950,6 +1963,13 @@ function collectMembers(values) {
|
|
|
1950
1963
|
* Determines whether a value is a member of a collected vocabulary, by
|
|
1951
1964
|
* SameValueZero.
|
|
1952
1965
|
*
|
|
1966
|
+
* @remarks
|
|
1967
|
+
* Dispatched through the captured `Set.prototype.has` onto a collection no caller
|
|
1968
|
+
* holds, and asked as a module binding rather than as a property: `set.has(value)` asks
|
|
1969
|
+
* a member every caller can rewrite, and moving that read onto an exported class's
|
|
1970
|
+
* `has` method reproduced the identical defect one prototype higher, because every
|
|
1971
|
+
* public class method is dispatched through a reachable prototype.
|
|
1972
|
+
*
|
|
1953
1973
|
* @param members - The vocabulary to ask, built by {@link collectMembers}
|
|
1954
1974
|
* @param value - The value to test for membership
|
|
1955
1975
|
* @returns True if the value was collected; false otherwise
|
|
@@ -1965,6 +1985,10 @@ function matchesMember(members, value) {
|
|
|
1965
1985
|
/**
|
|
1966
1986
|
* Collects one more member into a vocabulary that grows as a walk proceeds.
|
|
1967
1987
|
*
|
|
1988
|
+
* @remarks
|
|
1989
|
+
* Collects through the captured adder, for a vocabulary that grows as a walk proceeds —
|
|
1990
|
+
* a uniqueness gate, a de-duplicating key population.
|
|
1991
|
+
*
|
|
1968
1992
|
* @param members - The vocabulary to extend
|
|
1969
1993
|
* @param value - The value to admit
|
|
1970
1994
|
*
|
|
@@ -2007,6 +2031,10 @@ function matchesVisited(visited, value) {
|
|
|
2007
2031
|
/**
|
|
2008
2032
|
* Records an object as entered on a traversal's active path.
|
|
2009
2033
|
*
|
|
2034
|
+
* @remarks
|
|
2035
|
+
* Records through the captured `WeakSet.prototype.add`, on the same terms {@link
|
|
2036
|
+
* matchesVisited} states for the membership answer.
|
|
2037
|
+
*
|
|
2010
2038
|
* @param visited - The active-path set this traversal owns
|
|
2011
2039
|
* @param value - The object being entered
|
|
2012
2040
|
*
|
|
@@ -2021,6 +2049,10 @@ function admitVisited(visited, value) {
|
|
|
2021
2049
|
/**
|
|
2022
2050
|
* Records an object as exited from a traversal's active path.
|
|
2023
2051
|
*
|
|
2052
|
+
* @remarks
|
|
2053
|
+
* Records through the captured `WeakSet.prototype.delete`, on the same terms {@link
|
|
2054
|
+
* matchesVisited} states for the membership answer.
|
|
2055
|
+
*
|
|
2024
2056
|
* @param visited - The active-path set this traversal owns
|
|
2025
2057
|
* @param value - The object being exited
|
|
2026
2058
|
*
|
|
@@ -2233,6 +2265,10 @@ function readPatternSource(pattern) {
|
|
|
2233
2265
|
/**
|
|
2234
2266
|
* Reads a regular expression's flag text through the captured accessor.
|
|
2235
2267
|
*
|
|
2268
|
+
* @remarks
|
|
2269
|
+
* The flags half of {@link readPatternSource}, on the same captured-accessor terms and
|
|
2270
|
+
* for the same reason.
|
|
2271
|
+
*
|
|
2236
2272
|
* @param pattern - The candidate regular expression to read
|
|
2237
2273
|
* @returns The pattern's flag text, or `undefined` when it cannot be read as a string
|
|
2238
2274
|
* @throws The exact value the captured accessor throws for a receiver that is not a pattern
|
|
@@ -2567,6 +2603,11 @@ function contain(callback, door, options) {
|
|
|
2567
2603
|
/**
|
|
2568
2604
|
* Invokes a predicate through the sanctioned never-throw boundary.
|
|
2569
2605
|
*
|
|
2606
|
+
* @remarks
|
|
2607
|
+
* Backs the `instanceof`-based validators and every container combinator's element
|
|
2608
|
+
* walk. The answer is `true` only for the boolean literal `true`; a throw and every
|
|
2609
|
+
* other return are `false`.
|
|
2610
|
+
*
|
|
2570
2611
|
* @param callback - The predicate to invoke with no arguments
|
|
2571
2612
|
* @returns True if the callback returns the boolean value `true`; false otherwise
|
|
2572
2613
|
*
|
|
@@ -2898,6 +2939,15 @@ function readOptions(source, keys, builder, shape) {
|
|
|
2898
2939
|
/**
|
|
2899
2940
|
* Draws and validates one generator random sample.
|
|
2900
2941
|
*
|
|
2942
|
+
* @remarks
|
|
2943
|
+
* A broken source is the source's fault rather than the shape's, so the refusal
|
|
2944
|
+
* carries code `random` and never the `generate` code, and {@link compileGenerator}
|
|
2945
|
+
* rethrows it at whatever draw depth it happened instead of rotating to the next union
|
|
2946
|
+
* variant. The diagnostic names the consuming shape category, the `[0, 1)` limit, and a
|
|
2947
|
+
* total non-coercing {@link preview} of the offending sample (or `threw`), so a hostile
|
|
2948
|
+
* object runs no conversion hook while the diagnostic is built and a primitive symbol
|
|
2949
|
+
* renders without consulting mutable `Symbol.prototype.toString`.
|
|
2950
|
+
*
|
|
2901
2951
|
* @param random - The caller-supplied random source
|
|
2902
2952
|
* @param shape - The shape category consuming the sample
|
|
2903
2953
|
* @returns A finite sample in `[0, 1)`
|
|
@@ -3643,7 +3693,7 @@ function sanitizeDepth(value) {
|
|
|
3643
3693
|
return INTRINSICS.min(sanitizeBudget(value, 32), 32);
|
|
3644
3694
|
}
|
|
3645
3695
|
/**
|
|
3646
|
-
* Renders a short, safe, TOTAL
|
|
3696
|
+
* Renders an unknown value as a short, safe, TOTAL string for a {@link Fault}'s
|
|
3647
3697
|
* `received` field.
|
|
3648
3698
|
*
|
|
3649
3699
|
* @remarks
|
|
@@ -4534,7 +4584,10 @@ var ShapeValidator = class ShapeValidator {
|
|
|
4534
4584
|
const outcome = attempt(() => this.#execute());
|
|
4535
4585
|
const poison = this.#state.phase === "active" ? this.#state.poison : void 0;
|
|
4536
4586
|
this.#clear();
|
|
4537
|
-
if (poison !== void 0)
|
|
4587
|
+
if (poison !== void 0) {
|
|
4588
|
+
this.#expansion = void 0;
|
|
4589
|
+
throw poison;
|
|
4590
|
+
}
|
|
4538
4591
|
if (outcome.success) return;
|
|
4539
4592
|
if (isContractError(outcome.error)) throw outcome.error;
|
|
4540
4593
|
throw new ContractError("validateShape: shape reflection failed", {
|
|
@@ -6310,6 +6363,11 @@ function literalOf(...literals) {
|
|
|
6310
6363
|
* Verifies that `ctor` is a real constructor (through {@link isConstructor}) first,
|
|
6311
6364
|
* so passing an arrow function does not silently produce a broken guard.
|
|
6312
6365
|
*
|
|
6366
|
+
* A callable instance passes. The exclusion is a bad constructor, never a callable
|
|
6367
|
+
* value: an `isObject` pre-filter made `instanceOf(Function)(() => {})` answer `false`
|
|
6368
|
+
* while {@link isInstance}, the helper this is built on, answered `true` for the same
|
|
6369
|
+
* pair.
|
|
6370
|
+
*
|
|
6313
6371
|
* @param ctor - The constructor whose instances the guard accepts
|
|
6314
6372
|
* @returns A guard narrowing to that constructor's instance type
|
|
6315
6373
|
*
|
|
@@ -6327,6 +6385,10 @@ function instanceOf(ctor) {
|
|
|
6327
6385
|
* Builds a guard from a native `enum` or any object whose values are strings or
|
|
6328
6386
|
* numbers.
|
|
6329
6387
|
*
|
|
6388
|
+
* @remarks
|
|
6389
|
+
* An unreadable enumeration is refused at factory time with the shared `structure` read
|
|
6390
|
+
* refusal; the returned guard stays total.
|
|
6391
|
+
*
|
|
6330
6392
|
* @param enumeration - The readable enumeration whose values the guard accepts
|
|
6331
6393
|
* @returns A guard accepting one enumeration value
|
|
6332
6394
|
* @throws {ContractError} When the enumeration cannot be read
|
|
@@ -6439,6 +6501,10 @@ function keyOf(value) {
|
|
|
6439
6501
|
* Builds a new guard shape by keeping only the listed keys — the structural
|
|
6440
6502
|
* equivalent of `Pick<T, K>`. Produces a shape for {@link recordOf}, not a guard.
|
|
6441
6503
|
*
|
|
6504
|
+
* @remarks
|
|
6505
|
+
* An unreadable `keys` list and an unreadable `shape` are refused separately, each
|
|
6506
|
+
* under its own name, so a caller reads which argument failed.
|
|
6507
|
+
*
|
|
6442
6508
|
* @param shape - The guard shape to narrow
|
|
6443
6509
|
* @param keys - The keys to keep
|
|
6444
6510
|
* @returns A guard shape carrying only the kept keys
|
|
@@ -6478,6 +6544,10 @@ function pickOf(shape, keys) {
|
|
|
6478
6544
|
* Builds a new guard shape by removing the listed keys — the structural
|
|
6479
6545
|
* equivalent of `Omit<T, K>`. Produces a shape for {@link recordOf}, not a guard.
|
|
6480
6546
|
*
|
|
6547
|
+
* @remarks
|
|
6548
|
+
* An unreadable `keys` list and an unreadable `shape` are refused separately, each
|
|
6549
|
+
* under its own name, for the reason {@link pickOf} states.
|
|
6550
|
+
*
|
|
6481
6551
|
* @param shape - The guard shape to narrow
|
|
6482
6552
|
* @param keys - The keys to remove
|
|
6483
6553
|
* @returns A guard shape carrying every key except the removed ones
|
|
@@ -6526,6 +6596,10 @@ function orOf(left, right) {
|
|
|
6526
6596
|
* Typed as `Guard<unknown>` because `Exclude<unknown, T>` is not useful; use
|
|
6527
6597
|
* {@link complementOf} when you need the narrowed `Exclude<TBase, TExcluded>`.
|
|
6528
6598
|
*
|
|
6599
|
+
* The negation applies to the contained verdict, so a throwing guard is a non-match and
|
|
6600
|
+
* its negation passes. Containing the negation instead made a guard and its `notOf`
|
|
6601
|
+
* both reject the same value, which broke `orOf(g, notOf(g))` as a tautology.
|
|
6602
|
+
*
|
|
6529
6603
|
* @param guard - The guard or predicate to negate
|
|
6530
6604
|
* @returns A guard accepting exactly the values `guard` rejects
|
|
6531
6605
|
*
|
|
@@ -6541,6 +6615,10 @@ function notOf(guard) {
|
|
|
6541
6615
|
* Builds a guard for `Exclude<TBase, TExcluded>` — accepts values that pass
|
|
6542
6616
|
* `base` but not `excluded`.
|
|
6543
6617
|
*
|
|
6618
|
+
* @remarks
|
|
6619
|
+
* The base and the exclusion are contained separately, for the reason {@link notOf}
|
|
6620
|
+
* states: a throwing exclusion is a non-match, so the complement passes.
|
|
6621
|
+
*
|
|
6544
6622
|
* @param base - The guard establishing the accepted domain
|
|
6545
6623
|
* @param excluded - The guard whose accepted values are removed from that domain
|
|
6546
6624
|
* @returns A guard accepting a value `base` accepts and `excluded` rejects
|
|
@@ -6873,6 +6951,12 @@ function parseNull(value) {
|
|
|
6873
6951
|
/**
|
|
6874
6952
|
* Parses an unknown value to a plain record — the input reference, never cloned.
|
|
6875
6953
|
*
|
|
6954
|
+
* @remarks
|
|
6955
|
+
* Probes the record's own enumerable values before returning the reference, so a
|
|
6956
|
+
* hostile getter is met here rather than at the caller. A failed probe throws
|
|
6957
|
+
* `ContractError { code: 'structure' }` carrying `parseRecord: value could not be
|
|
6958
|
+
* read`; a readable non-record answers `undefined`.
|
|
6959
|
+
*
|
|
6876
6960
|
* @param value - The value to parse
|
|
6877
6961
|
* @returns The record, or `undefined`
|
|
6878
6962
|
* @throws {ContractError} When an object value cannot be read
|
|
@@ -9943,6 +10027,11 @@ function stringShape(options) {
|
|
|
9943
10027
|
/**
|
|
9944
10028
|
* Builds a numeric {@link NumberShape}.
|
|
9945
10029
|
*
|
|
10030
|
+
* @remarks
|
|
10031
|
+
* A present `min` or `max` must be finite. `NaN` and `±Infinity` throw a `bound` {@link
|
|
10032
|
+
* ContractError} at construction, because no finite value satisfies them and no JSON
|
|
10033
|
+
* Schema keyword expresses them.
|
|
10034
|
+
*
|
|
9946
10035
|
* @param options - Optional bounds (`min` / `max`), `integer`, and `description`
|
|
9947
10036
|
* @returns A number shape
|
|
9948
10037
|
* @throws {ContractError} When a present bound is not finite
|
|
@@ -10390,6 +10479,9 @@ function nullableShape(inner) {
|
|
|
10390
10479
|
* the compiled guard accepts — `NaN`, a `Map`, and a class instance all satisfy
|
|
10391
10480
|
* `{}` and all fail `isJSONValue`.
|
|
10392
10481
|
*
|
|
10482
|
+
* This is the shape an author writes to mean "any JSON value". Inference never produces
|
|
10483
|
+
* it: {@link schemaToShape} widening always lands on {@link RawShape} instead.
|
|
10484
|
+
*
|
|
10393
10485
|
* @param options - Optional `description`
|
|
10394
10486
|
* @returns A JSON passthrough shape
|
|
10395
10487
|
*
|