@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 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
@@ -933,6 +937,11 @@ function isDate(value) {
933
937
  }
934
938
  /** Determines whether a value is a `RegExp`.
935
939
  *
940
+ * @remarks
941
+ * A total native internal-slot brand check rather than a property read: it accepts a
942
+ * genuine pattern from this realm and from another, and refuses a proxy or a hand-built
943
+ * forgery without reading an advertised field.
944
+ *
936
945
  * @param value - The value to inspect
937
946
  * @returns True if the value is a `RegExp`; false otherwise
938
947
  *
@@ -1178,6 +1187,10 @@ function isWeakSet(value) {
1178
1187
  }
1179
1188
  /** Determines whether a value is an array.
1180
1189
  *
1190
+ * @remarks
1191
+ * Checks the container alone — no element is inspected. Use {@link arrayOf} to check
1192
+ * every element against a guard.
1193
+ *
1181
1194
  * @param value - The value to inspect
1182
1195
  * @returns True if the value is an array; false otherwise
1183
1196
  *
@@ -1951,6 +1964,13 @@ function collectMembers(values) {
1951
1964
  * Determines whether a value is a member of a collected vocabulary, by
1952
1965
  * SameValueZero.
1953
1966
  *
1967
+ * @remarks
1968
+ * Dispatched through the captured `Set.prototype.has` onto a collection no caller
1969
+ * holds, and asked as a module binding rather than as a property: `set.has(value)` asks
1970
+ * a member every caller can rewrite, and moving that read onto an exported class's
1971
+ * `has` method reproduced the identical defect one prototype higher, because every
1972
+ * public class method is dispatched through a reachable prototype.
1973
+ *
1954
1974
  * @param members - The vocabulary to ask, built by {@link collectMembers}
1955
1975
  * @param value - The value to test for membership
1956
1976
  * @returns True if the value was collected; false otherwise
@@ -1966,6 +1986,10 @@ function matchesMember(members, value) {
1966
1986
  /**
1967
1987
  * Collects one more member into a vocabulary that grows as a walk proceeds.
1968
1988
  *
1989
+ * @remarks
1990
+ * Collects through the captured adder, for a vocabulary that grows as a walk proceeds —
1991
+ * a uniqueness gate, a de-duplicating key population.
1992
+ *
1969
1993
  * @param members - The vocabulary to extend
1970
1994
  * @param value - The value to admit
1971
1995
  *
@@ -2008,6 +2032,10 @@ function matchesVisited(visited, value) {
2008
2032
  /**
2009
2033
  * Records an object as entered on a traversal's active path.
2010
2034
  *
2035
+ * @remarks
2036
+ * Records through the captured `WeakSet.prototype.add`, on the same terms {@link
2037
+ * matchesVisited} states for the membership answer.
2038
+ *
2011
2039
  * @param visited - The active-path set this traversal owns
2012
2040
  * @param value - The object being entered
2013
2041
  *
@@ -2022,6 +2050,10 @@ function admitVisited(visited, value) {
2022
2050
  /**
2023
2051
  * Records an object as exited from a traversal's active path.
2024
2052
  *
2053
+ * @remarks
2054
+ * Records through the captured `WeakSet.prototype.delete`, on the same terms {@link
2055
+ * matchesVisited} states for the membership answer.
2056
+ *
2025
2057
  * @param visited - The active-path set this traversal owns
2026
2058
  * @param value - The object being exited
2027
2059
  *
@@ -2234,6 +2266,10 @@ function readPatternSource(pattern) {
2234
2266
  /**
2235
2267
  * Reads a regular expression's flag text through the captured accessor.
2236
2268
  *
2269
+ * @remarks
2270
+ * The flags half of {@link readPatternSource}, on the same captured-accessor terms and
2271
+ * for the same reason.
2272
+ *
2237
2273
  * @param pattern - The candidate regular expression to read
2238
2274
  * @returns The pattern's flag text, or `undefined` when it cannot be read as a string
2239
2275
  * @throws The exact value the captured accessor throws for a receiver that is not a pattern
@@ -2568,6 +2604,11 @@ function contain(callback, door, options) {
2568
2604
  /**
2569
2605
  * Invokes a predicate through the sanctioned never-throw boundary.
2570
2606
  *
2607
+ * @remarks
2608
+ * Backs the `instanceof`-based validators and every container combinator's element
2609
+ * walk. The answer is `true` only for the boolean literal `true`; a throw and every
2610
+ * other return are `false`.
2611
+ *
2571
2612
  * @param callback - The predicate to invoke with no arguments
2572
2613
  * @returns True if the callback returns the boolean value `true`; false otherwise
2573
2614
  *
@@ -2899,6 +2940,15 @@ function readOptions(source, keys, builder, shape) {
2899
2940
  /**
2900
2941
  * Draws and validates one generator random sample.
2901
2942
  *
2943
+ * @remarks
2944
+ * A broken source is the source's fault rather than the shape's, so the refusal
2945
+ * carries code `random` and never the `generate` code, and {@link compileGenerator}
2946
+ * rethrows it at whatever draw depth it happened instead of rotating to the next union
2947
+ * variant. The diagnostic names the consuming shape category, the `[0, 1)` limit, and a
2948
+ * total non-coercing {@link preview} of the offending sample (or `threw`), so a hostile
2949
+ * object runs no conversion hook while the diagnostic is built and a primitive symbol
2950
+ * renders without consulting mutable `Symbol.prototype.toString`.
2951
+ *
2902
2952
  * @param random - The caller-supplied random source
2903
2953
  * @param shape - The shape category consuming the sample
2904
2954
  * @returns A finite sample in `[0, 1)`
@@ -3644,7 +3694,7 @@ function sanitizeDepth(value) {
3644
3694
  return INTRINSICS.min(sanitizeBudget(value, 32), 32);
3645
3695
  }
3646
3696
  /**
3647
- * Renders a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
3697
+ * Renders an unknown value as a short, safe, TOTAL string for a {@link Fault}'s
3648
3698
  * `received` field.
3649
3699
  *
3650
3700
  * @remarks
@@ -4535,7 +4585,10 @@ var ShapeValidator = class ShapeValidator {
4535
4585
  const outcome = attempt(() => this.#execute());
4536
4586
  const poison = this.#state.phase === "active" ? this.#state.poison : void 0;
4537
4587
  this.#clear();
4538
- if (poison !== void 0) throw poison;
4588
+ if (poison !== void 0) {
4589
+ this.#expansion = void 0;
4590
+ throw poison;
4591
+ }
4539
4592
  if (outcome.success) return;
4540
4593
  if (isContractError(outcome.error)) throw outcome.error;
4541
4594
  throw new ContractError("validateShape: shape reflection failed", {
@@ -6311,6 +6364,11 @@ function literalOf(...literals) {
6311
6364
  * Verifies that `ctor` is a real constructor (through {@link isConstructor}) first,
6312
6365
  * so passing an arrow function does not silently produce a broken guard.
6313
6366
  *
6367
+ * A callable instance passes. The exclusion is a bad constructor, never a callable
6368
+ * value: an `isObject` pre-filter made `instanceOf(Function)(() => {})` answer `false`
6369
+ * while {@link isInstance}, the helper this is built on, answered `true` for the same
6370
+ * pair.
6371
+ *
6314
6372
  * @param ctor - The constructor whose instances the guard accepts
6315
6373
  * @returns A guard narrowing to that constructor's instance type
6316
6374
  *
@@ -6328,6 +6386,10 @@ function instanceOf(ctor) {
6328
6386
  * Builds a guard from a native `enum` or any object whose values are strings or
6329
6387
  * numbers.
6330
6388
  *
6389
+ * @remarks
6390
+ * An unreadable enumeration is refused at factory time with the shared `structure` read
6391
+ * refusal; the returned guard stays total.
6392
+ *
6331
6393
  * @param enumeration - The readable enumeration whose values the guard accepts
6332
6394
  * @returns A guard accepting one enumeration value
6333
6395
  * @throws {ContractError} When the enumeration cannot be read
@@ -6440,6 +6502,10 @@ function keyOf(value) {
6440
6502
  * Builds a new guard shape by keeping only the listed keys — the structural
6441
6503
  * equivalent of `Pick<T, K>`. Produces a shape for {@link recordOf}, not a guard.
6442
6504
  *
6505
+ * @remarks
6506
+ * An unreadable `keys` list and an unreadable `shape` are refused separately, each
6507
+ * under its own name, so a caller reads which argument failed.
6508
+ *
6443
6509
  * @param shape - The guard shape to narrow
6444
6510
  * @param keys - The keys to keep
6445
6511
  * @returns A guard shape carrying only the kept keys
@@ -6479,6 +6545,10 @@ function pickOf(shape, keys) {
6479
6545
  * Builds a new guard shape by removing the listed keys — the structural
6480
6546
  * equivalent of `Omit<T, K>`. Produces a shape for {@link recordOf}, not a guard.
6481
6547
  *
6548
+ * @remarks
6549
+ * An unreadable `keys` list and an unreadable `shape` are refused separately, each
6550
+ * under its own name, for the reason {@link pickOf} states.
6551
+ *
6482
6552
  * @param shape - The guard shape to narrow
6483
6553
  * @param keys - The keys to remove
6484
6554
  * @returns A guard shape carrying every key except the removed ones
@@ -6527,6 +6597,10 @@ function orOf(left, right) {
6527
6597
  * Typed as `Guard<unknown>` because `Exclude<unknown, T>` is not useful; use
6528
6598
  * {@link complementOf} when you need the narrowed `Exclude<TBase, TExcluded>`.
6529
6599
  *
6600
+ * The negation applies to the contained verdict, so a throwing guard is a non-match and
6601
+ * its negation passes. Containing the negation instead made a guard and its `notOf`
6602
+ * both reject the same value, which broke `orOf(g, notOf(g))` as a tautology.
6603
+ *
6530
6604
  * @param guard - The guard or predicate to negate
6531
6605
  * @returns A guard accepting exactly the values `guard` rejects
6532
6606
  *
@@ -6542,6 +6616,10 @@ function notOf(guard) {
6542
6616
  * Builds a guard for `Exclude<TBase, TExcluded>` — accepts values that pass
6543
6617
  * `base` but not `excluded`.
6544
6618
  *
6619
+ * @remarks
6620
+ * The base and the exclusion are contained separately, for the reason {@link notOf}
6621
+ * states: a throwing exclusion is a non-match, so the complement passes.
6622
+ *
6545
6623
  * @param base - The guard establishing the accepted domain
6546
6624
  * @param excluded - The guard whose accepted values are removed from that domain
6547
6625
  * @returns A guard accepting a value `base` accepts and `excluded` rejects
@@ -6874,6 +6952,12 @@ function parseNull(value) {
6874
6952
  /**
6875
6953
  * Parses an unknown value to a plain record — the input reference, never cloned.
6876
6954
  *
6955
+ * @remarks
6956
+ * Probes the record's own enumerable values before returning the reference, so a
6957
+ * hostile getter is met here rather than at the caller. A failed probe throws
6958
+ * `ContractError { code: 'structure' }` carrying `parseRecord: value could not be
6959
+ * read`; a readable non-record answers `undefined`.
6960
+ *
6877
6961
  * @param value - The value to parse
6878
6962
  * @returns The record, or `undefined`
6879
6963
  * @throws {ContractError} When an object value cannot be read
@@ -9944,6 +10028,11 @@ function stringShape(options) {
9944
10028
  /**
9945
10029
  * Builds a numeric {@link NumberShape}.
9946
10030
  *
10031
+ * @remarks
10032
+ * A present `min` or `max` must be finite. `NaN` and `±Infinity` throw a `bound` {@link
10033
+ * ContractError} at construction, because no finite value satisfies them and no JSON
10034
+ * Schema keyword expresses them.
10035
+ *
9947
10036
  * @param options - Optional bounds (`min` / `max`), `integer`, and `description`
9948
10037
  * @returns A number shape
9949
10038
  * @throws {ContractError} When a present bound is not finite
@@ -10391,6 +10480,9 @@ function nullableShape(inner) {
10391
10480
  * the compiled guard accepts — `NaN`, a `Map`, and a class instance all satisfy
10392
10481
  * `{}` and all fail `isJSONValue`.
10393
10482
  *
10483
+ * This is the shape an author writes to mean "any JSON value". Inference never produces
10484
+ * it: {@link schemaToShape} widening always lands on {@link RawShape} instead.
10485
+ *
10394
10486
  * @param options - Optional `description`
10395
10487
  * @returns A JSON passthrough shape
10396
10488
  *