@astrale-os/kernel-dsl 1.0.0-beta.24 → 1.0.0-beta.26

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.
Files changed (113) hide show
  1. package/dist/v1/builder/class/authoring.js +0 -3
  2. package/dist/v1/builder/lowering/policy.js +4 -24
  3. package/dist/v1/builder/lowering/schema/context.js +0 -3
  4. package/dist/v1/builder/lowering/value/compile.js +24 -21
  5. package/dist/v1/builder/lowering/value/zod.js +10 -4
  6. package/dist/v1/builder/schema/diagnostic.d.ts +1 -1
  7. package/dist/v1/bundle/codec.js +50 -67
  8. package/dist/v1/bundle/diagnostic.d.ts +1 -1
  9. package/dist/v1/bundle/structure.js +2 -2
  10. package/dist/v1/compiled/admission/accept.js +6 -6
  11. package/dist/v1/compiled/admission/closure.js +16 -33
  12. package/dist/v1/compiled/admission/diagnostic.d.ts +8 -0
  13. package/dist/v1/compiled/admission/diagnostic.js +33 -0
  14. package/dist/v1/compiled/admission/semantics/class.js +11 -7
  15. package/dist/v1/compiled/admission/semantics/policy/admit.js +4 -19
  16. package/dist/v1/compiled/admission/wire/callable.d.ts +5 -2
  17. package/dist/v1/compiled/admission/wire/callable.js +13 -19
  18. package/dist/v1/compiled/admission/wire/class.js +6 -9
  19. package/dist/v1/compiled/admission/wire/entry.d.ts +1 -1
  20. package/dist/v1/compiled/admission/wire/entry.js +8 -13
  21. package/dist/v1/compiled/admission/wire/policy.js +4 -3
  22. package/dist/v1/compiled/admission/wire/program.d.ts +13 -3
  23. package/dist/v1/compiled/admission/wire/program.js +39 -44
  24. package/dist/v1/compiled/admission/wire/property.d.ts +4 -1
  25. package/dist/v1/compiled/admission/wire/property.js +8 -5
  26. package/dist/v1/compiled/admission/wire/value.d.ts +0 -2
  27. package/dist/v1/compiled/admission/wire/value.js +0 -7
  28. package/dist/v1/compiled/codec/json.js +4 -3
  29. package/dist/v1/compiled/compiler/compile.js +10 -29
  30. package/dist/v1/compiled/diagnostic.d.ts +1 -1
  31. package/dist/v1/compiled/loader/load.js +2 -7
  32. package/dist/v1/domain/model/input.d.ts +2 -2
  33. package/dist/v1/domain/validation/create.d.ts +3 -1
  34. package/dist/v1/domain/validation/create.js +13 -8
  35. package/dist/v1/language/callable/resolved.d.ts +3 -3
  36. package/dist/v1/language/chain.d.ts +7 -0
  37. package/dist/v1/language/chain.js +24 -0
  38. package/dist/v1/language/class/inheritance.js +0 -2
  39. package/dist/v1/language/cycle.d.ts +10 -0
  40. package/dist/v1/language/cycle.js +30 -0
  41. package/dist/v1/language/limits.d.ts +19 -0
  42. package/dist/v1/language/limits.js +17 -0
  43. package/dist/v1/language/policy/analysis/budgets.js +17 -13
  44. package/dist/v1/language/policy/analysis/composition.d.ts +4 -0
  45. package/dist/v1/language/policy/analysis/composition.js +11 -0
  46. package/dist/v1/language/suspended.d.ts +7 -0
  47. package/dist/v1/language/suspended.js +23 -0
  48. package/dist/v1/language/value/compile/fields.js +12 -8
  49. package/dist/v1/language/value/compile/load.d.ts +5 -0
  50. package/dist/v1/language/value/compile/load.js +13 -0
  51. package/dist/v1/language/value/evaluate/assertions.d.ts +5 -1
  52. package/dist/v1/language/value/evaluate/assertions.js +49 -8
  53. package/dist/v1/language/value/evaluate/evaluator.js +107 -105
  54. package/dist/v1/language/value/evaluate/preparation.d.ts +16 -0
  55. package/dist/v1/language/value/evaluate/preparation.js +100 -47
  56. package/dist/v1/language/value/evaluate/program.d.ts +2 -2
  57. package/dist/v1/language/value/evaluate/program.js +5 -5
  58. package/dist/v1/language/value/evaluate/validate.d.ts +3 -3
  59. package/dist/v1/language/value/evaluate/validate.js +9 -9
  60. package/dist/v1/language/value/evaluate/validity.optimization.js +4 -3
  61. package/dist/v1/language/value/inclusion/assertions.js +7 -8
  62. package/dist/v1/language/value/inclusion/equivalence.js +4 -2
  63. package/dist/v1/language/value/inclusion/evidence.js +2 -2
  64. package/dist/v1/language/value/inclusion/includes.d.ts +13 -4
  65. package/dist/v1/language/value/inclusion/includes.js +34 -5
  66. package/dist/v1/language/value/inclusion/location.js +10 -7
  67. package/dist/v1/language/value/ordering.js +62 -70
  68. package/dist/v1/language/value/profile/canonicalize.d.ts +6 -1
  69. package/dist/v1/language/value/profile/canonicalize.js +64 -83
  70. package/dist/v1/language/value/profile/json.validate.d.ts +13 -3
  71. package/dist/v1/language/value/profile/json.validate.js +18 -6
  72. package/dist/v1/language/value/reference/closure.js +3 -2
  73. package/dist/v1/language/value/reference/recursion.js +7 -26
  74. package/dist/v1/language/value/reference/references.d.ts +3 -1
  75. package/dist/v1/language/value/reference/references.js +7 -7
  76. package/dist/v1/language/value/syntax/walk.d.ts +5 -0
  77. package/dist/v1/language/value/syntax/walk.js +14 -0
  78. package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +1 -1
  79. package/dist/v1/schema/admission/accept.d.ts +3 -1
  80. package/dist/v1/schema/admission/accept.js +4 -1
  81. package/dist/v1/schema/analysis/class.d.ts +4 -0
  82. package/dist/v1/schema/analysis/class.js +24 -2
  83. package/dist/v1/schema/canonical/revision.d.ts +4 -1
  84. package/dist/v1/schema/canonical/revision.js +4 -1
  85. package/dist/v1/schema/compatibility/comparators/classes.js +22 -22
  86. package/dist/v1/schema/compatibility/context.d.ts +5 -2
  87. package/dist/v1/schema/compatibility/context.js +8 -6
  88. package/dist/v1/schema/compatibility/meaning/footprint.js +1 -2
  89. package/dist/v1/schema/compatibility/model.d.ts +1 -1
  90. package/dist/v1/schema/compatibility/references.d.ts +0 -3
  91. package/dist/v1/schema/compatibility/references.js +1 -9
  92. package/dist/v1/schema/compatibility/structure/requirements.js +4 -6
  93. package/dist/v1/schema/compatibility/walk.js +2 -2
  94. package/dist/v1/schema/diagnostic.d.ts +1 -1
  95. package/dist/v1/schema/normalization/domain.js +8 -4
  96. package/dist/v1/schema/resolution/context.js +6 -7
  97. package/dist/v1/schema/resolution/domain.js +3 -11
  98. package/dist/v1/schema/rule.js +10 -3
  99. package/dist/v1/schema/validation/class.js +1 -1
  100. package/dist/v1/schema/validation/core.js +16 -8
  101. package/dist/v1/schema/validation/domain.js +20 -3
  102. package/dist/v1/schema/validation/policy/budgets.d.ts +5 -1
  103. package/dist/v1/schema/validation/policy/budgets.js +8 -1
  104. package/dist/v1/schema/validation/policy/references.js +5 -25
  105. package/dist/value/accept.d.ts +3 -3
  106. package/dist/value/canonical.js +2 -2
  107. package/dist/value/equal.d.ts +9 -1
  108. package/dist/value/equal.js +37 -3
  109. package/dist/value/index.d.ts +1 -1
  110. package/dist/value/limits.d.ts +11 -0
  111. package/package.json +1 -1
  112. package/dist/v1/compiled/admission/wire/pattern.d.ts +0 -12
  113. package/dist/v1/compiled/admission/wire/pattern.js +0 -111
@@ -0,0 +1,24 @@
1
+ import { runSuspended } from './suspended.js';
2
+ /**
3
+ * How many edges the longest chain from each node holds, for every node `nodes` reach: a direct
4
+ * target is at depth 1. A depth limit bounds how long a chain is and not how many chains share a
5
+ * node, so the walk reads each node once, and it holds its own stack. A node read again before its
6
+ * own chain ends sits on a cycle and adds nothing more, so a cycle counts once round.
7
+ */
8
+ export function chainDepths(nodes, targetsOf) {
9
+ const depths = new Map();
10
+ function* depthOf(node) {
11
+ const known = depths.get(node);
12
+ if (known !== undefined)
13
+ return known;
14
+ depths.set(node, 0);
15
+ let depth = 0;
16
+ for (const target of targetsOf(node))
17
+ depth = Math.max(depth, 1 + (yield depthOf(target)));
18
+ depths.set(node, depth);
19
+ return depth;
20
+ }
21
+ for (const node of nodes)
22
+ runSuspended(depthOf(node));
23
+ return depths;
24
+ }
@@ -44,8 +44,6 @@ export function admitsForeignSubclass(declaration) {
44
44
  export function edgeContractRefines(child, parent, satisfies) {
45
45
  if (isGraphRoot(parent))
46
46
  return true;
47
- if (child.orientation !== parent.orientation)
48
- return false;
49
47
  if (parent.constraints.noSelf === true && child.constraints.noSelf !== true)
50
48
  return false;
51
49
  if (parent.constraints.acyclic === true && child.constraints.acyclic !== true)
@@ -0,0 +1,10 @@
1
+ /** An edge a depth-first walk took back to a node it has not finished, and the path that reached it. */
2
+ export interface CycleEdge<Node> {
3
+ readonly path: readonly Node[];
4
+ readonly target: Node;
5
+ }
6
+ /**
7
+ * Each edge closing a cycle in a depth-first walk from `roots`, as the walk meets it. A chain can be
8
+ * as long as a document allows, so the walk holds its own stack, and it enters each node once.
9
+ */
10
+ export declare function cycleEdges<Node>(roots: Iterable<Node>, targetsOf: (node: Node) => Iterable<Node>): Generator<CycleEdge<Node>, void, undefined>;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Each edge closing a cycle in a depth-first walk from `roots`, as the walk meets it. A chain can be
3
+ * as long as a document allows, so the walk holds its own stack, and it enters each node once.
4
+ */
5
+ export function* cycleEdges(roots, targetsOf) {
6
+ const finished = new Set();
7
+ for (const root of roots) {
8
+ if (finished.has(root))
9
+ continue;
10
+ // Only the last node leaves it, so a Set's order is the path.
11
+ const walking = new Set([root]);
12
+ const stack = [{ node: root, targets: targetsOf(root)[Symbol.iterator]() }];
13
+ while (stack.length > 0) {
14
+ const { node, targets } = stack.at(-1);
15
+ const { done, value: target } = targets.next();
16
+ if (done === true) {
17
+ walking.delete(node);
18
+ finished.add(node);
19
+ stack.pop();
20
+ }
21
+ else if (walking.has(target)) {
22
+ yield { path: [...walking], target };
23
+ }
24
+ else if (!finished.has(target)) {
25
+ walking.add(target);
26
+ stack.push({ node: target, targets: targetsOf(target)[Symbol.iterator]() });
27
+ }
28
+ }
29
+ }
30
+ }
@@ -14,6 +14,16 @@ export declare const limits: Readonly<{
14
14
  /** Dependencies on the longest chain from a Domain: a direct dependency is at depth 1. */
15
15
  depth: 32;
16
16
  }>;
17
+ /** Bounds of one document holding a dependency closure whole: a Bundle or a compiled Schema. */
18
+ closure: Readonly<{
19
+ /** Members and items of the whole document, over every Domain it holds. */
20
+ members: 1000000;
21
+ }>;
22
+ /** Bounds of Class inheritance (`DV_INHERITANCE_LIMITS`). */
23
+ inheritance: Readonly<{
24
+ /** Parents on the longest chain from a Class, of any Domain: a direct parent is at depth 1. */
25
+ depth: 64;
26
+ }>;
17
27
  /** Bounds of one I-Regexp in a value schema (`VS_REGEX_PORTABLE`), as RFC 9485 §8 allows. */
18
28
  regexp: Readonly<{
19
29
  /** Code points of one source. */
@@ -58,6 +68,15 @@ export declare const limits: Readonly<{
58
68
  checkLeaves: 8;
59
69
  }>;
60
70
  }>;
71
+ /**
72
+ * What the portable value boundary allows a document holding a closure whole. It may hold more than
73
+ * any one Domain does, and each route it crosses reads this, so what one accepts another can read.
74
+ */
75
+ export declare const closureDocument: Readonly<{
76
+ limits: Readonly<{
77
+ maxMembers: 1000000;
78
+ }>;
79
+ }>;
61
80
  /** A limit by its key in `limits`, `<group>.<name>`: `policy.patternDepth`. */
62
81
  export type LimitKey = {
63
82
  readonly [Group in keyof typeof limits & string]: `${Group}.${keyof (typeof limits)[Group] & string}`;
@@ -14,6 +14,16 @@ export const limits = Object.freeze({
14
14
  /** Dependencies on the longest chain from a Domain: a direct dependency is at depth 1. */
15
15
  depth: 32,
16
16
  }),
17
+ /** Bounds of one document holding a dependency closure whole: a Bundle or a compiled Schema. */
18
+ closure: Object.freeze({
19
+ /** Members and items of the whole document, over every Domain it holds. */
20
+ members: 1_000_000,
21
+ }),
22
+ /** Bounds of Class inheritance (`DV_INHERITANCE_LIMITS`). */
23
+ inheritance: Object.freeze({
24
+ /** Parents on the longest chain from a Class, of any Domain: a direct parent is at depth 1. */
25
+ depth: 64,
26
+ }),
17
27
  /** Bounds of one I-Regexp in a value schema (`VS_REGEX_PORTABLE`), as RFC 9485 §8 allows. */
18
28
  regexp: Object.freeze({
19
29
  /** Code points of one source. */
@@ -58,3 +68,10 @@ export const limits = Object.freeze({
58
68
  checkLeaves: 8,
59
69
  }),
60
70
  });
71
+ /**
72
+ * What the portable value boundary allows a document holding a closure whole. It may hold more than
73
+ * any one Domain does, and each route it crosses reads this, so what one accepts another can read.
74
+ */
75
+ export const closureDocument = Object.freeze({
76
+ limits: Object.freeze({ maxMembers: limits.closure.members }),
77
+ });
@@ -1,4 +1,5 @@
1
1
  import { limits } from '../../limits.js';
2
+ import { runSuspended } from '../../suspended.js';
2
3
  import { policyObjectOrigin, policyVariableClass } from '../model/policy.js';
3
4
  /**
4
5
  * Where a count of Policy branches stops: one past its limit. Each branch count is exact up to its
@@ -22,7 +23,8 @@ export function policyMeasurer(origin, normalized, isLocal) {
22
23
  // completed result by node identity turns path enumeration over the composition DAG into work
23
24
  // linear in the number of nodes while preserving the exact measure.
24
25
  const measures = new Map();
25
- const measurePolicy = (name, visiting) => {
26
+ const visiting = new Set();
27
+ function* measurePolicy(name) {
26
28
  if (visiting.has(name))
27
29
  return undefined;
28
30
  if (measures.has(name))
@@ -30,28 +32,30 @@ export function policyMeasurer(origin, normalized, isLocal) {
30
32
  const declaration = normalized.get(name);
31
33
  if (declaration === undefined)
32
34
  return undefined;
33
- const nested = new Set(visiting);
34
- nested.add(name);
35
- const measure = measureDeclaration(declaration, nested);
35
+ visiting.add(name);
36
+ const measure = yield* measureDeclaration(declaration);
37
+ visiting.delete(name);
36
38
  measures.set(name, measure);
37
39
  return measure;
38
- };
39
- const measureDeclaration = (declaration, nested) => {
40
+ }
41
+ function* measureDeclaration(declaration) {
40
42
  if ('match' in declaration)
41
43
  return measurePattern(declaration.match, origin);
42
44
  const refs = 'allOf' in declaration ? declaration.allOf : declaration.anyOf;
43
- const children = refs.flatMap((ref) => {
45
+ const children = [];
46
+ for (const ref of refs) {
44
47
  if (!isLocal(ref))
45
- return [];
46
- const value = measurePolicy(ref.name, nested);
47
- return value === undefined ? [] : [value];
48
- });
48
+ continue;
49
+ const value = yield measurePolicy(ref.name);
50
+ if (value !== undefined)
51
+ children.push(value);
52
+ }
49
53
  if (children.length !== refs.length || children.length === 0)
50
54
  return undefined;
51
55
  return combineMeasures('allOf' in declaration ? 'allOf' : 'anyOf', children);
52
- };
56
+ }
53
57
  return (name) => {
54
- const measure = measurePolicy(name, new Set());
58
+ const measure = runSuspended(measurePolicy(name));
55
59
  return measure === undefined
56
60
  ? undefined
57
61
  : {
@@ -0,0 +1,4 @@
1
+ import type { CycleEdge } from '../../cycle.js';
2
+ import type { PolicyExpression } from '../model/policy.js';
3
+ /** Each edge closing a cycle among the named Policies of `origin`, walking from `names` in order. */
4
+ export declare function compositionCycles(names: Iterable<string>, expressionOf: (name: string) => PolicyExpression | undefined, origin: string): Generator<CycleEdge<string>, void, undefined>;
@@ -0,0 +1,11 @@
1
+ import { cycleEdges } from '../../cycle.js';
2
+ /** Each edge closing a cycle among the named Policies of `origin`, walking from `names` in order. */
3
+ export function compositionCycles(names, expressionOf, origin) {
4
+ return cycleEdges(names, (name) => {
5
+ const expression = expressionOf(name);
6
+ if (expression === undefined || 'match' in expression)
7
+ return [];
8
+ const refs = 'allOf' in expression ? expression.allOf : expression.anyOf;
9
+ return refs.flatMap((ref) => (ref.origin === origin ? [ref.name] : []));
10
+ });
11
+ }
@@ -0,0 +1,7 @@
1
+ /** A computation that suspends on each computation whose answer it waits for. */
2
+ export type Suspended<Answer, Result = Answer> = Generator<Suspended<Answer>, Result, Answer>;
3
+ /**
4
+ * The answer of a suspended computation. Those waiting on one another sit on a stack of their own,
5
+ * so how many can wait at once does not depend on the call stack.
6
+ */
7
+ export declare function runSuspended<Answer>(start: Suspended<Answer>): Answer;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The answer of a suspended computation. Those waiting on one another sit on a stack of their own,
3
+ * so how many can wait at once does not depend on the call stack.
4
+ */
5
+ export function runSuspended(start) {
6
+ const waiting = [];
7
+ let current = start;
8
+ let step = current.next();
9
+ for (;;) {
10
+ if (step.done === true) {
11
+ const caller = waiting.pop();
12
+ if (caller === undefined)
13
+ return step.value;
14
+ current = caller;
15
+ step = current.next(step.value);
16
+ }
17
+ else {
18
+ waiting.push(current);
19
+ current = step.value;
20
+ step = current.next();
21
+ }
22
+ }
23
+ }
@@ -6,35 +6,40 @@ export function inputFieldNodes(program) {
6
6
  const visited = new Set();
7
7
  let closed = false;
8
8
  let unsupported = false;
9
- const visit = (index) => {
9
+ // A worklist, not recursion: admission runs this on the graph of a received schema, whose chain of
10
+ // references may be longer than the call stack. The result is sorted, so the visiting order does
11
+ // not show.
12
+ const pending = [program.root];
13
+ while (pending.length > 0) {
14
+ const index = pending.pop();
10
15
  if (visited.has(index))
11
- return;
16
+ continue;
12
17
  visited.add(index);
13
18
  const node = program.nodes[index];
14
19
  if (node === undefined ||
15
20
  node.schema === null ||
16
21
  typeof node.schema !== 'object' ||
17
22
  Array.isArray(node.schema))
18
- return;
23
+ continue;
19
24
  const schema = node.schema;
20
25
  if (schema.additionalProperties === false || schema.unevaluatedProperties === false)
21
26
  closed = true;
22
27
  if (schema.anyOf !== undefined || schema.oneOf !== undefined || schema.if !== undefined)
23
28
  unsupported = true;
24
29
  if (node.reference !== undefined)
25
- visit(node.reference);
30
+ pending.push(node.reference);
26
31
  if (Array.isArray(schema.allOf)) {
27
32
  schema.allOf.forEach((_, position) => {
28
33
  const child = node.children[`allOf/${position}`];
29
34
  if (child === undefined)
30
35
  throw new TypeError('Compiled input allOf branch is absent.');
31
- visit(child);
36
+ pending.push(child);
32
37
  });
33
38
  }
34
39
  if (schema.properties === null ||
35
40
  typeof schema.properties !== 'object' ||
36
41
  Array.isArray(schema.properties))
37
- return;
42
+ continue;
38
43
  for (const name of Object.keys(schema.properties)) {
39
44
  const child = node.children[`properties/${escapePointerToken(name)}`];
40
45
  if (child === undefined)
@@ -43,8 +48,7 @@ export function inputFieldNodes(program) {
43
48
  constraints.add(child);
44
49
  fields.set(name, constraints);
45
50
  }
46
- };
47
- visit(program.root);
51
+ }
48
52
  if (!closed || unsupported)
49
53
  return undefined;
50
54
  return Object.freeze(Object.fromEntries([...fields]
@@ -1,5 +1,10 @@
1
1
  import type { CompiledInstanceSchema } from '../evaluate/types.js';
2
2
  import type { IndexedValueProgram, ValueProgram } from './model.js';
3
+ /** Index the programs of one entry against the table of the machines they use, each listed once. */
4
+ export declare function indexValuePrograms(programs: readonly ValueProgram[]): {
5
+ readonly values: readonly IndexedValueProgram[];
6
+ readonly patterns: ValueProgram['patterns'];
7
+ };
3
8
  /** Bind already-admitted entry-local indices without copying any pattern machine. */
4
9
  export declare function bindValueProgram(program: IndexedValueProgram, patterns: ValueProgram['patterns']): ValueProgram;
5
10
  /** Bind admitted portable indices to the canonical instance evaluator. */
@@ -1,6 +1,19 @@
1
1
  import { loadPattern } from '../regexp/matcher.js';
2
2
  import { appendPointer } from '../syntax/pointer.js';
3
3
  const loadedPrograms = new WeakMap();
4
+ /** Index the programs of one entry against the table of the machines they use, each listed once. */
5
+ export function indexValuePrograms(programs) {
6
+ const indices = new Map();
7
+ const indexOf = (pattern) => {
8
+ const known = indices.get(pattern);
9
+ if (known !== undefined)
10
+ return known;
11
+ indices.set(pattern, indices.size);
12
+ return indices.size - 1;
13
+ };
14
+ const values = programs.map((program) => Object.freeze({ ...program, patterns: Object.freeze(program.patterns.map(indexOf)) }));
15
+ return { values: Object.freeze(values), patterns: Object.freeze([...indices.keys()]) };
16
+ }
4
17
  /** Bind already-admitted entry-local indices without copying any pattern machine. */
5
18
  export function bindValueProgram(program, patterns) {
6
19
  return Object.freeze({
@@ -1,8 +1,12 @@
1
1
  import type { Value as JsonValue } from '@astrale-os/kernel-dsl/value';
2
2
  export declare function matchesType(input: string, value: JsonValue): boolean;
3
- export declare function jsonEqual(left: JsonValue, right: JsonValue): boolean;
4
3
  /** Count Unicode code points with string-iterator semantics, without a character array. */
5
4
  export declare function codePointLength(input: string): number;
5
+ /**
6
+ * Whether no two items are the same JSON value, at any size their admission allowed, in time linear
7
+ * in their tokens: an item whose reading adds no node to the trie of the readings before it repeats
8
+ * one of them, since no reading is a prefix of another.
9
+ */
6
10
  export declare function unique(values: readonly JsonValue[]): boolean;
7
11
  /** Compare finite JSON numbers as their exact ECMAScript decimal spellings. */
8
12
  export declare function isMultipleOf(value: number, divisor: number): boolean;
@@ -1,4 +1,3 @@
1
- import { equal, json } from '@astrale-os/kernel-dsl/value';
2
1
  import { isValueObject as isJsonObject } from '../model/object.js';
3
2
  export function matchesType(input, value) {
4
3
  switch (input) {
@@ -18,9 +17,6 @@ export function matchesType(input, value) {
18
17
  return false;
19
18
  }
20
19
  }
21
- export function jsonEqual(left, right) {
22
- return equal(left, right);
23
- }
24
20
  /** Count Unicode code points with string-iterator semantics, without a character array. */
25
21
  export function codePointLength(input) {
26
22
  let length = 0;
@@ -34,16 +30,61 @@ export function codePointLength(input) {
34
30
  }
35
31
  return length;
36
32
  }
33
+ /**
34
+ * Whether no two items are the same JSON value, at any size their admission allowed, in time linear
35
+ * in their tokens: an item whose reading adds no node to the trie of the readings before it repeats
36
+ * one of them, since no reading is a prefix of another.
37
+ */
37
38
  export function unique(values) {
38
- const seen = new Set();
39
+ const readings = new Map();
39
40
  for (const value of values) {
40
- const key = json.serialize(value);
41
- if (seen.has(key))
41
+ let node = readings;
42
+ let repeated = true;
43
+ for (const token of readingOf(value)) {
44
+ let next = node.get(token);
45
+ if (next === undefined) {
46
+ next = new Map();
47
+ node.set(token, next);
48
+ repeated = false;
49
+ }
50
+ node = next;
51
+ }
52
+ if (repeated)
42
53
  return false;
43
- seen.add(key);
44
54
  }
45
55
  return true;
46
56
  }
57
+ const ARRAY = Symbol('array');
58
+ const OBJECT = Symbol('object');
59
+ const END = Symbol('end');
60
+ /**
61
+ * An admitted value as tokens: each scalar and member name, and where each container starts and
62
+ * ends, with member names sorted. Two values have the same reading exactly when they have the same
63
+ * canonical JSON text, and no reading is a prefix of another. Admission bounded the value under the
64
+ * limits of its validation, so a reading checks none of its own, and it holds neither the text,
65
+ * which may outgrow any string, nor a stack frame per level.
66
+ */
67
+ function* readingOf(value) {
68
+ const pending = [value];
69
+ while (pending.length > 0) {
70
+ const next = pending.pop();
71
+ if (next === null || typeof next !== 'object') {
72
+ yield next;
73
+ }
74
+ else if (isJsonObject(next)) {
75
+ yield OBJECT;
76
+ pending.push(END);
77
+ for (const name of Object.keys(next).sort())
78
+ pending.push(next[name], name);
79
+ }
80
+ else {
81
+ yield ARRAY;
82
+ pending.push(END);
83
+ for (const item of next)
84
+ pending.push(item);
85
+ }
86
+ }
87
+ }
47
88
  /** Compare finite JSON numbers as their exact ECMAScript decimal spellings. */
48
89
  export function isMultipleOf(value, divisor) {
49
90
  if (!(divisor > 0))