@kubb/ast 5.0.0-beta.30 → 5.0.0-beta.32
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/index.cjs +452 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +220 -101
- package/dist/index.js +450 -56
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/dedupe.ts +200 -0
- package/src/guards.ts +1 -46
- package/src/index.ts +2 -0
- package/src/refs.ts +0 -49
- package/src/signature.ts +232 -0
- package/src/types.ts +1 -0
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/ast",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.32",
|
|
4
4
|
"description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ast",
|
package/src/dedupe.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createSchema } from './factory.ts'
|
|
2
|
+
import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'
|
|
3
|
+
import { signatureOf } from './signature.ts'
|
|
4
|
+
import { collectLazy, transform } from './visitor.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A canonical destination for a deduplicated shape: the shared schema name and
|
|
8
|
+
* the synthetic `$ref` path that points at it.
|
|
9
|
+
*/
|
|
10
|
+
export type DedupeCanonical = {
|
|
11
|
+
/**
|
|
12
|
+
* Canonical schema name every duplicate occurrence refers to.
|
|
13
|
+
*/
|
|
14
|
+
name: string
|
|
15
|
+
/**
|
|
16
|
+
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
|
|
17
|
+
*/
|
|
18
|
+
ref: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
|
|
23
|
+
* canonical target, plus the freshly hoisted definitions that must be added to
|
|
24
|
+
* the schema list.
|
|
25
|
+
*/
|
|
26
|
+
export type DedupePlan = {
|
|
27
|
+
/**
|
|
28
|
+
* Maps a structural signature to the canonical schema that represents it.
|
|
29
|
+
*/
|
|
30
|
+
canonicalBySignature: Map<string, DedupeCanonical>
|
|
31
|
+
/**
|
|
32
|
+
* New top-level schema definitions created for inline shapes that had no existing
|
|
33
|
+
* named component. Nested duplicates inside each definition are already collapsed.
|
|
34
|
+
*/
|
|
35
|
+
hoisted: Array<SchemaNode>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
|
|
40
|
+
* The mechanics (grouping, counting, rewriting) live here; the policy lives in the caller.
|
|
41
|
+
*/
|
|
42
|
+
export type BuildDedupePlanOptions = {
|
|
43
|
+
/**
|
|
44
|
+
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
|
|
45
|
+
* reject both ineligible kinds (return `false` for anything other than, say, enums and
|
|
46
|
+
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
|
|
47
|
+
*/
|
|
48
|
+
isCandidate: (node: SchemaNode) => boolean
|
|
49
|
+
/**
|
|
50
|
+
* Produces the canonical name for an inline shape with no existing named component.
|
|
51
|
+
* Return `null` to leave the shape inline (for example when no contextual name exists).
|
|
52
|
+
*/
|
|
53
|
+
nameFor: (node: SchemaNode, signature: string) => string | null
|
|
54
|
+
/**
|
|
55
|
+
* Builds the `$ref` path for a canonical name.
|
|
56
|
+
*/
|
|
57
|
+
refFor: (name: string) => string
|
|
58
|
+
/**
|
|
59
|
+
* Minimum number of occurrences before a shape is deduplicated.
|
|
60
|
+
*
|
|
61
|
+
* @default 2
|
|
62
|
+
*/
|
|
63
|
+
minOccurrences?: number
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds the shared `ref` replacement for a duplicate occurrence, carrying the
|
|
68
|
+
* usage-slot and documentation fields that are not part of the canonical type.
|
|
69
|
+
*/
|
|
70
|
+
function createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {
|
|
71
|
+
return createSchema({
|
|
72
|
+
type: 'ref',
|
|
73
|
+
name: canonical.name,
|
|
74
|
+
ref: canonical.ref,
|
|
75
|
+
optional: node.optional,
|
|
76
|
+
nullish: node.nullish,
|
|
77
|
+
readOnly: node.readOnly,
|
|
78
|
+
writeOnly: node.writeOnly,
|
|
79
|
+
deprecated: node.deprecated,
|
|
80
|
+
description: node.description,
|
|
81
|
+
default: node.default,
|
|
82
|
+
example: node.example,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
|
|
88
|
+
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
|
|
89
|
+
* so nested duplicates inside a replaced shape are not visited again.
|
|
90
|
+
*
|
|
91
|
+
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
|
|
92
|
+
* turned into a reference to itself; nested duplicates are still collapsed.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const next = applyDedupe(operationNode, plan.canonicalBySignature)
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function applyDedupe(node: SchemaNode, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch?: boolean): SchemaNode
|
|
100
|
+
export function applyDedupe(node: OperationNode, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch?: boolean): OperationNode
|
|
101
|
+
export function applyDedupe(node: Node, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch = false): Node {
|
|
102
|
+
if (canonicalBySignature.size === 0) return node
|
|
103
|
+
|
|
104
|
+
const root = node
|
|
105
|
+
|
|
106
|
+
return transform(node, {
|
|
107
|
+
schema(schemaNode) {
|
|
108
|
+
const signature = signatureOf(schemaNode)
|
|
109
|
+
if (skipRootMatch && schemaNode === root) return undefined
|
|
110
|
+
|
|
111
|
+
const canonical = canonicalBySignature.get(signature)
|
|
112
|
+
if (!canonical) return undefined
|
|
113
|
+
|
|
114
|
+
return createRefNode(schemaNode, canonical)
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Strips usage-slot flags from a hoisted definition and applies its canonical name.
|
|
121
|
+
* A standalone definition is never optional, so `optional`/`nullish` are cleared.
|
|
122
|
+
*/
|
|
123
|
+
function cleanDefinition(node: SchemaNode, name: string): SchemaNode {
|
|
124
|
+
return { ...node, name, optional: undefined, nullish: undefined }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
|
|
129
|
+
*
|
|
130
|
+
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
|
|
131
|
+
* is a named top-level schema, that name becomes the canonical (so other top-level duplicates
|
|
132
|
+
* and inline copies turn into references to it); otherwise a new definition is hoisted using
|
|
133
|
+
* `nameFor`. The plan is then applied per node with {@link applyDedupe}.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
|
|
138
|
+
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
|
|
139
|
+
* nameFor: (node) => node.name ?? null,
|
|
140
|
+
* refFor: (name) => `#/components/schemas/${name}`,
|
|
141
|
+
* })
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {
|
|
145
|
+
const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options
|
|
146
|
+
|
|
147
|
+
const topLevelNodes = new Set<SchemaNode>()
|
|
148
|
+
|
|
149
|
+
type Group = {
|
|
150
|
+
count: number
|
|
151
|
+
representative: SchemaNode
|
|
152
|
+
topLevelName?: string
|
|
153
|
+
}
|
|
154
|
+
const groups = new Map<string, Group>()
|
|
155
|
+
|
|
156
|
+
function record(schemaNode: SchemaNode): void {
|
|
157
|
+
const signature = signatureOf(schemaNode)
|
|
158
|
+
if (!isCandidate(schemaNode)) return
|
|
159
|
+
|
|
160
|
+
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name
|
|
161
|
+
const group = groups.get(signature)
|
|
162
|
+
if (group) {
|
|
163
|
+
group.count++
|
|
164
|
+
if (isTopLevel && !group.topLevelName) group.topLevelName = schemaNode.name!
|
|
165
|
+
} else {
|
|
166
|
+
groups.set(signature, { count: 1, representative: schemaNode, topLevelName: isTopLevel ? schemaNode.name! : undefined })
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
for (const root of roots) {
|
|
171
|
+
if (root.kind === 'Schema') topLevelNodes.add(root)
|
|
172
|
+
for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {
|
|
173
|
+
record(schemaNode)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const canonicalBySignature = new Map<string, DedupeCanonical>()
|
|
178
|
+
const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []
|
|
179
|
+
|
|
180
|
+
for (const [signature, group] of groups) {
|
|
181
|
+
if (group.count < minOccurrences) continue
|
|
182
|
+
|
|
183
|
+
if (group.topLevelName) {
|
|
184
|
+
canonicalBySignature.set(signature, { name: group.topLevelName, ref: refFor(group.topLevelName) })
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const name = nameFor(group.representative, signature)
|
|
189
|
+
if (!name) continue
|
|
190
|
+
|
|
191
|
+
canonicalBySignature.set(signature, { name, ref: refFor(name) })
|
|
192
|
+
pendingHoists.push({ name, representative: group.representative })
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Build hoisted definitions only after every canonical name is known, so nested
|
|
196
|
+
// duplicates inside a definition also resolve to refs.
|
|
197
|
+
const hoisted = pendingHoists.map(({ name, representative }) => cleanDefinition(applyDedupe(representative, canonicalBySignature, true), name))
|
|
198
|
+
|
|
199
|
+
return { canonicalBySignature, hoisted }
|
|
200
|
+
}
|
package/src/guards.ts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
FunctionParameterNode,
|
|
3
|
-
FunctionParametersNode,
|
|
4
|
-
HttpOperationNode,
|
|
5
|
-
InputNode,
|
|
6
|
-
Node,
|
|
7
|
-
NodeKind,
|
|
8
|
-
OperationNode,
|
|
9
|
-
OutputNode,
|
|
10
|
-
ParameterGroupNode,
|
|
11
|
-
ParameterNode,
|
|
12
|
-
PropertyNode,
|
|
13
|
-
ResponseNode,
|
|
14
|
-
SchemaNode,
|
|
15
|
-
SchemaNodeByType,
|
|
16
|
-
} from './nodes/index.ts'
|
|
1
|
+
import type { HttpOperationNode, InputNode, Node, NodeKind, OperationNode, OutputNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'
|
|
17
2
|
|
|
18
3
|
/**
|
|
19
4
|
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
@@ -93,33 +78,3 @@ export function isHttpOperationNode(node: OperationNode): node is HttpOperationN
|
|
|
93
78
|
* ```
|
|
94
79
|
*/
|
|
95
80
|
export const isSchemaNode = isKind<SchemaNode>('Schema')
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Returns `true` when the input is a `PropertyNode`.
|
|
99
|
-
*/
|
|
100
|
-
export const isPropertyNode = isKind<PropertyNode>('Property')
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Returns `true` when the input is a `ParameterNode`.
|
|
104
|
-
*/
|
|
105
|
-
export const isParameterNode = isKind<ParameterNode>('Parameter')
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Returns `true` when the input is a `ResponseNode`.
|
|
109
|
-
*/
|
|
110
|
-
export const isResponseNode = isKind<ResponseNode>('Response')
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Returns `true` when the input is a `FunctionParameterNode`.
|
|
114
|
-
*/
|
|
115
|
-
export const isFunctionParameterNode = isKind<FunctionParameterNode>('FunctionParameter')
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Returns `true` when the input is a `ParameterGroupNode`.
|
|
119
|
-
*/
|
|
120
|
-
export const isParameterGroupNode = isKind<ParameterGroupNode>('ParameterGroup')
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Returns `true` when the input is a `FunctionParametersNode`.
|
|
124
|
-
*/
|
|
125
|
-
export const isFunctionParametersNode = isKind<FunctionParametersNode>('FunctionParameters')
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { httpMethods, isScalarPrimitive, mediaTypes, nodeKinds, schemaTypes } from './constants.ts'
|
|
2
|
+
export { applyDedupe, buildDedupePlan } from './dedupe.ts'
|
|
2
3
|
export { defineSchemaDialect } from './dialect.ts'
|
|
3
4
|
export { dispatch } from './dispatch.ts'
|
|
4
5
|
export {
|
|
@@ -34,6 +35,7 @@ export { isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isSche
|
|
|
34
35
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
35
36
|
export { extractRefName } from './refs.ts'
|
|
36
37
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
38
|
+
export { isSchemaEqual, schemaSignature } from './signature.ts'
|
|
37
39
|
export { mergeAdjacentObjects, mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
38
40
|
export type * from './types.ts'
|
|
39
41
|
export {
|
package/src/refs.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { InputNode } from './nodes/root.ts'
|
|
2
1
|
import type { SchemaNode } from './nodes/schema.ts'
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -19,51 +18,3 @@ export type RefMap = Map<string, SchemaNode>
|
|
|
19
18
|
export function extractRefName(ref: string): string {
|
|
20
19
|
return ref.split('/').at(-1) ?? ref
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Builds a `RefMap` from `input.schemas` using each schema's `name`.
|
|
25
|
-
*
|
|
26
|
-
* Unnamed schemas are skipped.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* const refMap = buildRefMap(input)
|
|
31
|
-
* const pet = refMap.get('Pet')
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export function buildRefMap(input: InputNode): RefMap {
|
|
35
|
-
const map: RefMap = new Map()
|
|
36
|
-
|
|
37
|
-
for (const schema of input.schemas) {
|
|
38
|
-
if (schema.name) {
|
|
39
|
-
map.set(schema.name, schema)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return map
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Resolves a schema by name from a `RefMap`.
|
|
47
|
-
*
|
|
48
|
-
* Returns `null` when the ref is not found.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* const petSchema = resolveRef(refMap, 'Pet')
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export function resolveRef(refMap: RefMap, ref: string): SchemaNode | null {
|
|
56
|
-
return refMap.get(ref) ?? null
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Converts a `RefMap` into a plain object.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const refsObject = refMapToObject(refMap)
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
|
|
68
|
-
return Object.fromEntries(refMap)
|
|
69
|
-
}
|
package/src/signature.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import type { SchemaNode } from './nodes/index.ts'
|
|
3
|
+
import { extractRefName } from './refs.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The shape-affecting flags shared by every node kind: base primitive, format, and `nullable`.
|
|
7
|
+
* Documentation and usage-slot flags (`optional`/`nullish`/`readOnly`/`writeOnly`) are
|
|
8
|
+
* intentionally excluded — they describe the property slot, not the type.
|
|
9
|
+
*/
|
|
10
|
+
function flagsDescriptor(node: SchemaNode): string {
|
|
11
|
+
return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {
|
|
15
|
+
if (node.ref) return extractRefName(node.ref)
|
|
16
|
+
return node.name ?? ''
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type ScalarField = { kind: 'scalar'; key: string; prefix: string }
|
|
20
|
+
type BoolField = { kind: 'bool'; key: string; prefix: string }
|
|
21
|
+
type ChildField = { kind: 'child'; key: string; prefix: string }
|
|
22
|
+
type ChildrenField = { kind: 'children'; key: string; prefix: string }
|
|
23
|
+
type ObjectPropsField = { kind: 'objectProps' }
|
|
24
|
+
type AdditionalPropsField = { kind: 'additionalProps' }
|
|
25
|
+
type PatternPropsField = { kind: 'patternProps' }
|
|
26
|
+
type EnumValuesField = { kind: 'enumValues' }
|
|
27
|
+
type RefTargetField = { kind: 'refTarget' }
|
|
28
|
+
|
|
29
|
+
type ShapeField =
|
|
30
|
+
| ScalarField
|
|
31
|
+
| BoolField
|
|
32
|
+
| ChildField
|
|
33
|
+
| ChildrenField
|
|
34
|
+
| ObjectPropsField
|
|
35
|
+
| AdditionalPropsField
|
|
36
|
+
| PatternPropsField
|
|
37
|
+
| EnumValuesField
|
|
38
|
+
| RefTargetField
|
|
39
|
+
|
|
40
|
+
const arrayTupleFields: ReadonlyArray<ShapeField> = [
|
|
41
|
+
{ kind: 'children', key: 'items', prefix: 'i' },
|
|
42
|
+
{ kind: 'child', key: 'rest', prefix: 'r' },
|
|
43
|
+
{ kind: 'scalar', key: 'min', prefix: 'mn' },
|
|
44
|
+
{ kind: 'scalar', key: 'max', prefix: 'mx' },
|
|
45
|
+
{ kind: 'bool', key: 'unique', prefix: 'u' },
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
const numericFields: ReadonlyArray<ShapeField> = [
|
|
49
|
+
{ kind: 'scalar', key: 'min', prefix: 'mn' },
|
|
50
|
+
{ kind: 'scalar', key: 'max', prefix: 'mx' },
|
|
51
|
+
{ kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },
|
|
52
|
+
{ kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },
|
|
53
|
+
{ kind: 'scalar', key: 'multipleOf', prefix: 'mo' },
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
const rangeFields: ReadonlyArray<ShapeField> = [
|
|
57
|
+
{ kind: 'scalar', key: 'min', prefix: 'mn' },
|
|
58
|
+
{ kind: 'scalar', key: 'max', prefix: 'mx' },
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Maps each schema node `type` to the ordered list of shape-contributing fields.
|
|
63
|
+
* Node types absent from this map (scalar types like boolean, null, any, etc.) fall
|
|
64
|
+
* back to `${type}|${flags}` with no additional fields.
|
|
65
|
+
*/
|
|
66
|
+
const SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {
|
|
67
|
+
object: [
|
|
68
|
+
{ kind: 'objectProps' },
|
|
69
|
+
{ kind: 'additionalProps' },
|
|
70
|
+
{ kind: 'patternProps' },
|
|
71
|
+
{ kind: 'scalar', key: 'minProperties', prefix: 'mn' },
|
|
72
|
+
{ kind: 'scalar', key: 'maxProperties', prefix: 'mx' },
|
|
73
|
+
],
|
|
74
|
+
array: arrayTupleFields,
|
|
75
|
+
tuple: arrayTupleFields,
|
|
76
|
+
union: [
|
|
77
|
+
{ kind: 'scalar', key: 'strategy', prefix: 's' },
|
|
78
|
+
{ kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },
|
|
79
|
+
{ kind: 'children', key: 'members', prefix: 'm' },
|
|
80
|
+
],
|
|
81
|
+
intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],
|
|
82
|
+
enum: [{ kind: 'enumValues' }],
|
|
83
|
+
ref: [{ kind: 'refTarget' }],
|
|
84
|
+
string: [
|
|
85
|
+
{ kind: 'scalar', key: 'min', prefix: 'mn' },
|
|
86
|
+
{ kind: 'scalar', key: 'max', prefix: 'mx' },
|
|
87
|
+
{ kind: 'scalar', key: 'pattern', prefix: 'pt' },
|
|
88
|
+
],
|
|
89
|
+
number: numericFields,
|
|
90
|
+
integer: numericFields,
|
|
91
|
+
bigint: numericFields,
|
|
92
|
+
url: [
|
|
93
|
+
{ kind: 'scalar', key: 'path', prefix: 'path' },
|
|
94
|
+
{ kind: 'scalar', key: 'min', prefix: 'mn' },
|
|
95
|
+
{ kind: 'scalar', key: 'max', prefix: 'mx' },
|
|
96
|
+
],
|
|
97
|
+
uuid: rangeFields,
|
|
98
|
+
email: rangeFields,
|
|
99
|
+
datetime: [
|
|
100
|
+
{ kind: 'bool', key: 'offset', prefix: 'o' },
|
|
101
|
+
{ kind: 'bool', key: 'local', prefix: 'l' },
|
|
102
|
+
],
|
|
103
|
+
date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],
|
|
104
|
+
time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {
|
|
108
|
+
switch (field.kind) {
|
|
109
|
+
case 'scalar':
|
|
110
|
+
return `${field.prefix}:${record[field.key] ?? ''}`
|
|
111
|
+
case 'bool':
|
|
112
|
+
return `${field.prefix}:${record[field.key] ? 1 : 0}`
|
|
113
|
+
case 'child': {
|
|
114
|
+
const child = record[field.key] as SchemaNode | undefined
|
|
115
|
+
return `${field.prefix}:${child ? signatureOf(child) : ''}`
|
|
116
|
+
}
|
|
117
|
+
case 'children': {
|
|
118
|
+
const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []
|
|
119
|
+
return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`
|
|
120
|
+
}
|
|
121
|
+
case 'objectProps': {
|
|
122
|
+
const obj = node as Extract<SchemaNode, { type: 'object' }>
|
|
123
|
+
const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')
|
|
124
|
+
return `p[${props}]`
|
|
125
|
+
}
|
|
126
|
+
case 'additionalProps': {
|
|
127
|
+
const obj = node as Extract<SchemaNode, { type: 'object' }>
|
|
128
|
+
if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`
|
|
129
|
+
if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`
|
|
130
|
+
return ''
|
|
131
|
+
}
|
|
132
|
+
case 'patternProps': {
|
|
133
|
+
const obj = node as Extract<SchemaNode, { type: 'object' }>
|
|
134
|
+
const pattern = obj.patternProperties
|
|
135
|
+
? Object.keys(obj.patternProperties)
|
|
136
|
+
.sort()
|
|
137
|
+
.map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)
|
|
138
|
+
.join(',')
|
|
139
|
+
: ''
|
|
140
|
+
return `pp[${pattern}]`
|
|
141
|
+
}
|
|
142
|
+
case 'enumValues': {
|
|
143
|
+
const en = node as Extract<SchemaNode, { type: 'enum' }>
|
|
144
|
+
let values = ''
|
|
145
|
+
if (en.namedEnumValues?.length) {
|
|
146
|
+
values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')
|
|
147
|
+
} else if (en.enumValues?.length) {
|
|
148
|
+
values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')
|
|
149
|
+
}
|
|
150
|
+
return `v[${values}]`
|
|
151
|
+
}
|
|
152
|
+
case 'refTarget': {
|
|
153
|
+
return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
|
|
160
|
+
* children's signatures. {@link signatureOf} hashes this string; children contribute their
|
|
161
|
+
* fixed-length signature rather than their own full descriptor, which keeps the result bounded.
|
|
162
|
+
*/
|
|
163
|
+
function describeShape(node: SchemaNode): string {
|
|
164
|
+
const flags = flagsDescriptor(node)
|
|
165
|
+
const fields = SHAPE_KEYS[node.type]
|
|
166
|
+
if (!fields) return `${node.type}|${flags}`
|
|
167
|
+
|
|
168
|
+
const record = node as unknown as Record<string, unknown>
|
|
169
|
+
const parts: Array<string> = [`${node.type}|${flags}`]
|
|
170
|
+
for (const field of fields) {
|
|
171
|
+
parts.push(serializeShapeField(field, node, record))
|
|
172
|
+
}
|
|
173
|
+
return parts.join('|')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Persistent hash-consing cache: `SchemaNode` → signature digest, keyed by node identity.
|
|
178
|
+
*
|
|
179
|
+
* A `WeakMap` so entries are released once the node is garbage-collected, and so a node hashed
|
|
180
|
+
* during dedupe planning is not re-hashed when the same tree is rewritten during streaming
|
|
181
|
+
* (where `schemaSignature` and `applyDedupe` would otherwise each walk it from scratch). Reuse
|
|
182
|
+
* across calls is sound because a signature depends only on a node's content, and schema nodes
|
|
183
|
+
* are immutable once created — transforms allocate new objects rather than mutating in place.
|
|
184
|
+
*/
|
|
185
|
+
const signatureCache = new WeakMap<SchemaNode, string>()
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
|
|
189
|
+
* children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
|
|
190
|
+
* full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
|
|
191
|
+
* digest is identical across calls because it depends only on content — never on traversal
|
|
192
|
+
* order. This keeps the keys built during planning consistent with the ones recomputed later
|
|
193
|
+
* during streaming. {@link signatureCache} memoizes node → digest across every computation.
|
|
194
|
+
*/
|
|
195
|
+
export function signatureOf(node: SchemaNode): string {
|
|
196
|
+
const cached = signatureCache.get(node)
|
|
197
|
+
if (cached !== undefined) return cached
|
|
198
|
+
const signature = createHash('sha256').update(describeShape(node)).digest('hex')
|
|
199
|
+
signatureCache.set(node, signature)
|
|
200
|
+
return signature
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Computes a deterministic, shape-only signature (a fixed-length content hash) for a schema node.
|
|
205
|
+
*
|
|
206
|
+
* Two schemas share a signature when they are structurally identical, ignoring
|
|
207
|
+
* documentation (`name`, `title`, `description`, `example`, `default`, `deprecated`)
|
|
208
|
+
* and usage-slot flags (`optional`, `nullish`, `readOnly`, `writeOnly`). `nullable`
|
|
209
|
+
* is kept because it changes the produced type. `ref` nodes compare by target name,
|
|
210
|
+
* which also keeps the algorithm terminating on circular shapes.
|
|
211
|
+
*
|
|
212
|
+
* @example Two enums with different descriptions share a signature
|
|
213
|
+
* ```ts
|
|
214
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
|
|
215
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function schemaSignature(node: SchemaNode): string {
|
|
219
|
+
return signatureOf(node)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns `true` when two schema nodes are structurally identical under shape-only equality.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* isSchemaEqual(a, b) // a and b produce the same TypeScript type
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {
|
|
231
|
+
return schemaSignature(a) === schemaSignature(b)
|
|
232
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type { VisitorDepth } from './constants.ts'
|
|
2
|
+
export type { BuildDedupePlanOptions, DedupeCanonical, DedupePlan } from './dedupe.ts'
|
|
2
3
|
export type { SchemaDialect } from './dialect.ts'
|
|
3
4
|
export type { DispatchRule } from './dispatch.ts'
|
|
4
5
|
export type { DistributiveOmit } from './factory.ts'
|
|
File without changes
|