@orkestrel/contract 0.0.16 → 0.0.18

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 CHANGED
@@ -1,9 +1,15 @@
1
1
  # @orkestrel/contract
2
2
 
3
- The zero-dependency contract toolkit — runtime type guards, guard combinators, coerce-and-extract
4
- parsers, and a shape DSL that compiles once into a JSON Schema, a guard, a parser, a strict audit, a
5
- parse report, and a generator, every one of them derived from a single owned snapshot of the
6
- declaration. The foundation package of the `@orkestrel` line.
3
+ > The zero-dependency contract toolkit — runtime type guards, guard combinators,
4
+ > coerce-and-extract parsers, and a shape DSL that compiles one declaration into a JSON
5
+ > Schema, a guard, a parser, a strict audit, a parse report, and a generator, every one of
6
+ > them derived from a single owned snapshot of that declaration.
7
+
8
+ Declare a value's shape once with the shape builders, compile it with the `createContract`
9
+ function, and take the member each call site needs: `is` to narrow, `parse` to coerce, `audit` and
10
+ `explain` to diagnose, `schema` to publish, and `generate` to seed a fixture. Where a whole
11
+ declaration is more than the job needs, take a bare guard, combinator, or parser instead. The
12
+ foundation package of the `@orkestrel` line.
7
13
 
8
14
  ## Install
9
15
 
@@ -287,7 +287,7 @@ var CONTRACT_CODES = Object.freeze([
287
287
  "expansion"
288
288
  ]);
289
289
  /**
290
- * Caps the number of {@link Fault} / {@link AuditFault} entries a single
290
+ * Caps at `64` the number of {@link Fault} / {@link AuditFault} entries a single
291
291
  * `explain` or `audit` report ever returns, frozen.
292
292
  *
293
293
  * @remarks
@@ -301,7 +301,7 @@ var CONTRACT_CODES = Object.freeze([
301
301
  */
302
302
  var FAULT_LIMIT = 64;
303
303
  /**
304
- * Caps the character length of a {@link preview}-rendered string, frozen.
304
+ * Caps at `64` the character length of a {@link preview}-rendered string, frozen.
305
305
  *
306
306
  * @remarks
307
307
  * A previewed string longer than this is clipped with a trailing `…` so a
@@ -326,7 +326,7 @@ var PREVIEW_LIMIT = 64;
326
326
  */
327
327
  var GUARD_DEPTH_LIMIT = 512;
328
328
  /**
329
- * Caps the supported nesting depth of a compiled contract shape, frozen.
329
+ * Caps at `512` the supported nesting depth of a compiled contract shape, frozen.
330
330
  *
331
331
  * @remarks
332
332
  * {@link validateShape} rejects the next level
@@ -341,7 +341,8 @@ var GUARD_DEPTH_LIMIT = 512;
341
341
  */
342
342
  var COMPILE_DEPTH_LIMIT = 512;
343
343
  /**
344
- * Caps the number of nodes a compiled artifact may expand a shape into, frozen.
344
+ * Caps at `16384` the number of nodes a compiled artifact may expand a shape into,
345
+ * frozen.
345
346
  *
346
347
  * @remarks
347
348
  * A shape graph is a DAG; every compiled artifact is a TREE. A declaration may
@@ -376,7 +377,7 @@ var COMPILE_DEPTH_LIMIT = 512;
376
377
  */
377
378
  var COMPILE_NODE_LIMIT = 16384;
378
379
  /**
379
- * Caps the number of object keys one compiled presence mask carries, frozen.
380
+ * Caps at `31` the number of object keys one compiled presence mask carries, frozen.
380
381
  *
381
382
  * @remarks
382
383
  * A compiled object plan decides which declared keys a value carries. The keys
@@ -400,7 +401,7 @@ var COMPILE_NODE_LIMIT = 16384;
400
401
  */
401
402
  var PRESENCE_MASK_LIMIT = 31;
402
403
  /**
403
- * Caps the number of nodes one JSON snapshot may produce, frozen.
404
+ * Caps at `262144` the number of nodes one JSON snapshot may produce, frozen.
404
405
  *
405
406
  * @remarks
406
407
  * JSON persistence is a TREE, so `cloneJSONValue` / `cloneJSONRecord`
@@ -427,8 +428,8 @@ var PRESENCE_MASK_LIMIT = 31;
427
428
  */
428
429
  var CLONE_NODE_LIMIT = 262144;
429
430
  /**
430
- * Caps the number of candidate-generation attempts for a constrained
431
- * generated value, frozen.
431
+ * Caps at `32` the number of candidate-generation attempts for a constrained generated
432
+ * value, frozen.
432
433
  *
433
434
  * @remarks
434
435
  * Provides one deterministic work bound for generators that must retry a
@@ -441,7 +442,7 @@ var CLONE_NODE_LIMIT = 262144;
441
442
  */
442
443
  var GENERATION_ATTEMPT_LIMIT = 32;
443
444
  /**
444
- * Caps the object/array nesting depth {@link valueToSchema} walks, frozen.
445
+ * Caps at `32` the object/array nesting depth {@link valueToSchema} walks, frozen.
445
446
  *
446
447
  * @remarks
447
448
  * Bounds inference against adversarial or cyclic runtime input — once the
@@ -459,8 +460,8 @@ var GENERATION_ATTEMPT_LIMIT = 32;
459
460
  */
460
461
  var INFER_DEPTH_LIMIT = 32;
461
462
  /**
462
- * Caps by default the number of object properties / array elements
463
- * {@link valueToSchema} samples per container, frozen.
463
+ * Caps by default at `256` the number of object properties / array elements {@link
464
+ * valueToSchema} samples per container, frozen.
464
465
  *
465
466
  * @remarks
466
467
  * Bounds the work (and the emitted schema's size) against a wide record or a
@@ -469,7 +470,7 @@ var INFER_DEPTH_LIMIT = 32;
469
470
  */
470
471
  var INFER_BREADTH_LIMIT = 256;
471
472
  /**
472
- * Caps by default the number of distinct values a multi-sample slot may hold
473
+ * Caps by default at `12` the number of distinct values a multi-sample slot may hold
473
474
  * before enum inference gives up and falls back to a bare `type`, frozen.
474
475
  *
475
476
  * @remarks
@@ -480,7 +481,7 @@ var INFER_BREADTH_LIMIT = 256;
480
481
  */
481
482
  var INFER_ENUM_LIMIT = 12;
482
483
  /**
483
- * Caps the string length {@link stringToFormat} attempts to classify, frozen.
484
+ * Caps at `128` the string length {@link stringToFormat} attempts to classify, frozen.
484
485
  *
485
486
  * @remarks
486
487
  * Bounds per-string format-detection work: a value longer than this returns
@@ -508,11 +509,14 @@ var FORMAT_PATTERNS = Object.freeze({
508
509
  //#endregion
509
510
  //#region src/core/errors.ts
510
511
  /**
511
- * Carries a machine-readable contract category, optional context, and an exact
512
- * optional cause. Omitting `cause` omits the own property; explicitly
513
- * supplying `cause: undefined` retains an own property with that value. Both
514
- * optional options are read as OWN properties, so a construction never consults
515
- * the caller-writable prototype chain of the container it was handed.
512
+ * Carries a machine-readable contract category, optional context, and an exact optional
513
+ * cause.
514
+ *
515
+ * @remarks
516
+ * Omitting `cause` omits the own property; explicitly supplying `cause: undefined`
517
+ * retains an own property with that value. Both optional options are read as OWN
518
+ * properties, so a construction never consults the caller-writable prototype chain of
519
+ * the container it was handed.
516
520
  *
517
521
  * @example
518
522
  * ```ts
@@ -902,6 +906,20 @@ function isNullableBoolean(value) {
902
906
  * wraps the check in {@link holds} (see ./helpers.js) so any such throw
903
907
  * yields `false` instead of escaping.
904
908
  *
909
+ * Narrows `value` to `InstanceType<C>`, which reads the constructor's last
910
+ * construct signature. Accepts the same constructors as `instanceOf`: a public
911
+ * or abstract construct signature returning an object (or `any`). A class with a
912
+ * private or protected constructor and the bare `AnyConstructor` are refused at the call.
913
+ * A value typed `Function` cannot be passed because the `Function` interface has
914
+ * no construct signature. The constructor `Function` can be passed through
915
+ * `FunctionConstructor`'s construct signature and narrows to `Function`.
916
+ * The false branch drops each member of the declared union that is a subtype
917
+ * of the instance type and keeps the rest, so a subclass that adds no member
918
+ * narrows its base out as well (and a subclass that adds only an optional member
919
+ * does not). A constructor whose instance type admits
920
+ * primitives structurally, such as `Object`, makes the false branch unsound;
921
+ * use `isObject` for that check.
922
+ *
905
923
  * @param value - The value to test
906
924
  * @param ctor - The constructor to test against
907
925
  * @returns True if `value instanceof ctor`; false otherwise, including on a
@@ -933,6 +951,11 @@ function isDate(value) {
933
951
  }
934
952
  /** Determines whether a value is a `RegExp`.
935
953
  *
954
+ * @remarks
955
+ * A total native internal-slot brand check rather than a property read: it accepts a
956
+ * genuine pattern from this realm and from another, and refuses a proxy or a hand-built
957
+ * forgery without reading an advertised field.
958
+ *
936
959
  * @param value - The value to inspect
937
960
  * @returns True if the value is a `RegExp`; false otherwise
938
961
  *
@@ -1178,6 +1201,10 @@ function isWeakSet(value) {
1178
1201
  }
1179
1202
  /** Determines whether a value is an array.
1180
1203
  *
1204
+ * @remarks
1205
+ * Checks the container alone — no element is inspected. Use {@link arrayOf} to check
1206
+ * every element against a guard.
1207
+ *
1181
1208
  * @param value - The value to inspect
1182
1209
  * @returns True if the value is an array; false otherwise
1183
1210
  *
@@ -1951,6 +1978,13 @@ function collectMembers(values) {
1951
1978
  * Determines whether a value is a member of a collected vocabulary, by
1952
1979
  * SameValueZero.
1953
1980
  *
1981
+ * @remarks
1982
+ * Dispatched through the captured `Set.prototype.has` onto a collection no caller
1983
+ * holds, and asked as a module binding rather than as a property: `set.has(value)` asks
1984
+ * a member every caller can rewrite, and moving that read onto an exported class's
1985
+ * `has` method reproduced the identical defect one prototype higher, because every
1986
+ * public class method is dispatched through a reachable prototype.
1987
+ *
1954
1988
  * @param members - The vocabulary to ask, built by {@link collectMembers}
1955
1989
  * @param value - The value to test for membership
1956
1990
  * @returns True if the value was collected; false otherwise
@@ -1966,6 +2000,10 @@ function matchesMember(members, value) {
1966
2000
  /**
1967
2001
  * Collects one more member into a vocabulary that grows as a walk proceeds.
1968
2002
  *
2003
+ * @remarks
2004
+ * Collects through the captured adder, for a vocabulary that grows as a walk proceeds —
2005
+ * a uniqueness gate, a de-duplicating key population.
2006
+ *
1969
2007
  * @param members - The vocabulary to extend
1970
2008
  * @param value - The value to admit
1971
2009
  *
@@ -2008,6 +2046,10 @@ function matchesVisited(visited, value) {
2008
2046
  /**
2009
2047
  * Records an object as entered on a traversal's active path.
2010
2048
  *
2049
+ * @remarks
2050
+ * Records through the captured `WeakSet.prototype.add`, on the same terms {@link
2051
+ * matchesVisited} states for the membership answer.
2052
+ *
2011
2053
  * @param visited - The active-path set this traversal owns
2012
2054
  * @param value - The object being entered
2013
2055
  *
@@ -2022,6 +2064,10 @@ function admitVisited(visited, value) {
2022
2064
  /**
2023
2065
  * Records an object as exited from a traversal's active path.
2024
2066
  *
2067
+ * @remarks
2068
+ * Records through the captured `WeakSet.prototype.delete`, on the same terms {@link
2069
+ * matchesVisited} states for the membership answer.
2070
+ *
2025
2071
  * @param visited - The active-path set this traversal owns
2026
2072
  * @param value - The object being exited
2027
2073
  *
@@ -2234,6 +2280,10 @@ function readPatternSource(pattern) {
2234
2280
  /**
2235
2281
  * Reads a regular expression's flag text through the captured accessor.
2236
2282
  *
2283
+ * @remarks
2284
+ * The flags half of {@link readPatternSource}, on the same captured-accessor terms and
2285
+ * for the same reason.
2286
+ *
2237
2287
  * @param pattern - The candidate regular expression to read
2238
2288
  * @returns The pattern's flag text, or `undefined` when it cannot be read as a string
2239
2289
  * @throws The exact value the captured accessor throws for a receiver that is not a pattern
@@ -2568,6 +2618,11 @@ function contain(callback, door, options) {
2568
2618
  /**
2569
2619
  * Invokes a predicate through the sanctioned never-throw boundary.
2570
2620
  *
2621
+ * @remarks
2622
+ * Backs the `instanceof`-based validators and every container combinator's element
2623
+ * walk. The answer is `true` only for the boolean literal `true`; a throw and every
2624
+ * other return are `false`.
2625
+ *
2571
2626
  * @param callback - The predicate to invoke with no arguments
2572
2627
  * @returns True if the callback returns the boolean value `true`; false otherwise
2573
2628
  *
@@ -2899,6 +2954,15 @@ function readOptions(source, keys, builder, shape) {
2899
2954
  /**
2900
2955
  * Draws and validates one generator random sample.
2901
2956
  *
2957
+ * @remarks
2958
+ * A broken source is the source's fault rather than the shape's, so the refusal
2959
+ * carries code `random` and never the `generate` code, and {@link compileGenerator}
2960
+ * rethrows it at whatever draw depth it happened instead of rotating to the next union
2961
+ * variant. The diagnostic names the consuming shape category, the `[0, 1)` limit, and a
2962
+ * total non-coercing {@link preview} of the offending sample (or `threw`), so a hostile
2963
+ * object runs no conversion hook while the diagnostic is built and a primitive symbol
2964
+ * renders without consulting mutable `Symbol.prototype.toString`.
2965
+ *
2902
2966
  * @param random - The caller-supplied random source
2903
2967
  * @param shape - The shape category consuming the sample
2904
2968
  * @returns A finite sample in `[0, 1)`
@@ -3644,7 +3708,7 @@ function sanitizeDepth(value) {
3644
3708
  return INTRINSICS.min(sanitizeBudget(value, 32), 32);
3645
3709
  }
3646
3710
  /**
3647
- * Renders a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
3711
+ * Renders an unknown value as a short, safe, TOTAL string for a {@link Fault}'s
3648
3712
  * `received` field.
3649
3713
  *
3650
3714
  * @remarks
@@ -4535,7 +4599,10 @@ var ShapeValidator = class ShapeValidator {
4535
4599
  const outcome = attempt(() => this.#execute());
4536
4600
  const poison = this.#state.phase === "active" ? this.#state.poison : void 0;
4537
4601
  this.#clear();
4538
- if (poison !== void 0) throw poison;
4602
+ if (poison !== void 0) {
4603
+ this.#expansion = void 0;
4604
+ throw poison;
4605
+ }
4539
4606
  if (outcome.success) return;
4540
4607
  if (isContractError(outcome.error)) throw outcome.error;
4541
4608
  throw new ContractError("validateShape: shape reflection failed", {
@@ -6311,6 +6378,11 @@ function literalOf(...literals) {
6311
6378
  * Verifies that `ctor` is a real constructor (through {@link isConstructor}) first,
6312
6379
  * so passing an arrow function does not silently produce a broken guard.
6313
6380
  *
6381
+ * A callable instance passes. The exclusion is a bad constructor, never a callable
6382
+ * value: an `isObject` pre-filter made `instanceOf(Function)(() => {})` answer `false`
6383
+ * while {@link isInstance}, the helper this is built on, answered `true` for the same
6384
+ * pair.
6385
+ *
6314
6386
  * @param ctor - The constructor whose instances the guard accepts
6315
6387
  * @returns A guard narrowing to that constructor's instance type
6316
6388
  *
@@ -6328,6 +6400,10 @@ function instanceOf(ctor) {
6328
6400
  * Builds a guard from a native `enum` or any object whose values are strings or
6329
6401
  * numbers.
6330
6402
  *
6403
+ * @remarks
6404
+ * An unreadable enumeration is refused at factory time with the shared `structure` read
6405
+ * refusal; the returned guard stays total.
6406
+ *
6331
6407
  * @param enumeration - The readable enumeration whose values the guard accepts
6332
6408
  * @returns A guard accepting one enumeration value
6333
6409
  * @throws {ContractError} When the enumeration cannot be read
@@ -6440,6 +6516,10 @@ function keyOf(value) {
6440
6516
  * Builds a new guard shape by keeping only the listed keys — the structural
6441
6517
  * equivalent of `Pick<T, K>`. Produces a shape for {@link recordOf}, not a guard.
6442
6518
  *
6519
+ * @remarks
6520
+ * An unreadable `keys` list and an unreadable `shape` are refused separately, each
6521
+ * under its own name, so a caller reads which argument failed.
6522
+ *
6443
6523
  * @param shape - The guard shape to narrow
6444
6524
  * @param keys - The keys to keep
6445
6525
  * @returns A guard shape carrying only the kept keys
@@ -6479,6 +6559,10 @@ function pickOf(shape, keys) {
6479
6559
  * Builds a new guard shape by removing the listed keys — the structural
6480
6560
  * equivalent of `Omit<T, K>`. Produces a shape for {@link recordOf}, not a guard.
6481
6561
  *
6562
+ * @remarks
6563
+ * An unreadable `keys` list and an unreadable `shape` are refused separately, each
6564
+ * under its own name, for the reason {@link pickOf} states.
6565
+ *
6482
6566
  * @param shape - The guard shape to narrow
6483
6567
  * @param keys - The keys to remove
6484
6568
  * @returns A guard shape carrying every key except the removed ones
@@ -6527,6 +6611,10 @@ function orOf(left, right) {
6527
6611
  * Typed as `Guard<unknown>` because `Exclude<unknown, T>` is not useful; use
6528
6612
  * {@link complementOf} when you need the narrowed `Exclude<TBase, TExcluded>`.
6529
6613
  *
6614
+ * The negation applies to the contained verdict, so a throwing guard is a non-match and
6615
+ * its negation passes. Containing the negation instead made a guard and its `notOf`
6616
+ * both reject the same value, which broke `orOf(g, notOf(g))` as a tautology.
6617
+ *
6530
6618
  * @param guard - The guard or predicate to negate
6531
6619
  * @returns A guard accepting exactly the values `guard` rejects
6532
6620
  *
@@ -6542,6 +6630,10 @@ function notOf(guard) {
6542
6630
  * Builds a guard for `Exclude<TBase, TExcluded>` — accepts values that pass
6543
6631
  * `base` but not `excluded`.
6544
6632
  *
6633
+ * @remarks
6634
+ * The base and the exclusion are contained separately, for the reason {@link notOf}
6635
+ * states: a throwing exclusion is a non-match, so the complement passes.
6636
+ *
6545
6637
  * @param base - The guard establishing the accepted domain
6546
6638
  * @param excluded - The guard whose accepted values are removed from that domain
6547
6639
  * @returns A guard accepting a value `base` accepts and `excluded` rejects
@@ -6874,6 +6966,12 @@ function parseNull(value) {
6874
6966
  /**
6875
6967
  * Parses an unknown value to a plain record — the input reference, never cloned.
6876
6968
  *
6969
+ * @remarks
6970
+ * Probes the record's own enumerable values before returning the reference, so a
6971
+ * hostile getter is met here rather than at the caller. A failed probe throws
6972
+ * `ContractError { code: 'structure' }` carrying `parseRecord: value could not be
6973
+ * read`; a readable non-record answers `undefined`.
6974
+ *
6877
6975
  * @param value - The value to parse
6878
6976
  * @returns The record, or `undefined`
6879
6977
  * @throws {ContractError} When an object value cannot be read
@@ -9944,6 +10042,11 @@ function stringShape(options) {
9944
10042
  /**
9945
10043
  * Builds a numeric {@link NumberShape}.
9946
10044
  *
10045
+ * @remarks
10046
+ * A present `min` or `max` must be finite. `NaN` and `±Infinity` throw a `bound` {@link
10047
+ * ContractError} at construction, because no finite value satisfies them and no JSON
10048
+ * Schema keyword expresses them.
10049
+ *
9947
10050
  * @param options - Optional bounds (`min` / `max`), `integer`, and `description`
9948
10051
  * @returns A number shape
9949
10052
  * @throws {ContractError} When a present bound is not finite
@@ -10391,6 +10494,9 @@ function nullableShape(inner) {
10391
10494
  * the compiled guard accepts — `NaN`, a `Map`, and a class instance all satisfy
10392
10495
  * `{}` and all fail `isJSONValue`.
10393
10496
  *
10497
+ * This is the shape an author writes to mean "any JSON value". Inference never produces
10498
+ * it: {@link schemaToShape} widening always lands on {@link RawShape} instead.
10499
+ *
10394
10500
  * @param options - Optional `description`
10395
10501
  * @returns A JSON passthrough shape
10396
10502
  *