@kubb/ast 5.0.0-beta.31 → 5.0.0-beta.33
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 +2 -3
- package/dist/index.cjs +230 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -3728
- package/dist/index.js +232 -136
- package/dist/index.js.map +1 -1
- package/dist/types-CE8VJ5_y.d.ts +3605 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +5 -1
- package/src/constants.ts +0 -50
- package/src/dedupe.ts +2 -4
- package/src/guards.ts +1 -46
- package/src/index.ts +5 -9
- package/src/infer.ts +0 -9
- package/src/refs.ts +0 -56
- package/src/signature.ts +161 -64
- package/src/types.ts +1 -2
- package/src/visitor.ts +8 -4
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
package/dist/types.cjs
ADDED
|
File without changes
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { $t as ScalarSchemaType, At as FunctionParameterNode, Bt as DateSchemaNode, C as PrinterFactoryOptions, Ct as HttpStatusCode, D as DistributiveOmit, Dt as ParameterNode, Et as ParameterLocation, Ft as FileNode, Gt as IntersectionSchemaNode, Ht as EnumSchemaNode, It as ImportNode, Jt as NumberSchemaNode, Kt as Ipv4SchemaNode, Lt as SourceNode, Mt as ParameterGroupNode, Nt as ParamsTypeNode, O as UserFileNode, Ot as FunctionNodeType, Pt as ExportNode, Qt as ScalarSchemaNode, Rt as ArraySchemaNode, S as Printer, Sn as VisitorDepth, St as ResponseNode, Tt as StatusCode, Ut as EnumValueNode, Vt as DatetimeSchemaNode, Wt as FormatStringSchemaNode, Xt as PrimitiveSchemaType, Yt as ObjectSchemaNode, Zt as RefSchemaNode, _ as VisitorContext, _n as TypeDeclarationNode, _t as HttpMethod, an as TimeSchemaNode, bn as NodeKind, bt as OperationNodeBase, cn as PropertyNode, ct as DedupePlan, d as AsyncVisitor, dn as CodeNode, dt as Node, en as SchemaNode, et as InferSchemaNode, f as CollectOptions, fn as ConstNode, ft as InputMeta, g as Visitor, gn as TextNode, gt as GenericOperationNode, h as TransformOptions, hn as JsxNode, ht as OutputNode, in as StringSchemaNode, it as SchemaDialect, jt as FunctionParametersNode, kt as FunctionParamNode, ln as ArrowFunctionNode, m as ParentOf, mn as JSDocNode, mt as InputStreamNode, nn as SchemaType, nt as DispatchRule, on as UnionSchemaNode, ot as BuildDedupePlanOptions, p as CollectVisitor, pn as FunctionNode, pt as InputNode, qt as Ipv6SchemaNode, rn as SpecialSchemaType, sn as UrlSchemaNode, st as DedupeCanonical, t as OperationParamsResolver, tn as SchemaNodeByType, tt as ParserOptions, un as BreakNode, v as WalkOptions, vn as TypeNode, vt as HttpOperationNode, w as PrinterPartial, wt as MediaType, xn as ScalarPrimitive, xt as OperationProtocol, yn as BaseNode, yt as OperationNode, zt as ComplexSchemaType } from "./types-CE8VJ5_y.js";
|
|
2
|
+
export type { ArraySchemaNode, ArrowFunctionNode, AsyncVisitor, BaseNode, BreakNode, BuildDedupePlanOptions, CodeNode, CollectOptions, CollectVisitor, ComplexSchemaType, ConstNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupePlan, DispatchRule, DistributiveOmit, EnumSchemaNode, EnumValueNode, ExportNode, FileNode, FormatStringSchemaNode, FunctionNode, FunctionNodeType, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, GenericOperationNode, HttpMethod, HttpOperationNode, HttpStatusCode, ImportNode, InferSchemaNode, InputMeta, InputNode, InputStreamNode, IntersectionSchemaNode, Ipv4SchemaNode, Ipv6SchemaNode, JSDocNode, JsxNode, MediaType, Node, NodeKind, NumberSchemaNode, ObjectSchemaNode, OperationNode, OperationNodeBase, OperationParamsResolver, OperationProtocol, OutputNode, ParameterGroupNode, ParameterLocation, ParameterNode, ParamsTypeNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, ResponseNode, ScalarPrimitive, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, SpecialSchemaType, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TransformOptions, TypeDeclarationNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
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.33",
|
|
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",
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
"import": "./dist/index.js",
|
|
33
33
|
"require": "./dist/index.cjs"
|
|
34
34
|
},
|
|
35
|
+
"./types": {
|
|
36
|
+
"import": "./dist/types.js",
|
|
37
|
+
"require": "./dist/types.cjs"
|
|
38
|
+
},
|
|
35
39
|
"./package.json": "./package.json"
|
|
36
40
|
},
|
|
37
41
|
"publishConfig": {
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { NodeKind } from './nodes/base.ts'
|
|
2
|
-
import type { MediaType } from './nodes/http.ts'
|
|
3
1
|
import type { HttpMethod } from './nodes/operation.ts'
|
|
4
2
|
import type { SchemaType } from './nodes/schema.ts'
|
|
5
3
|
|
|
@@ -16,26 +14,6 @@ export const visitorDepths = {
|
|
|
16
14
|
deep: 'deep',
|
|
17
15
|
} as const satisfies Record<VisitorDepth, VisitorDepth>
|
|
18
16
|
|
|
19
|
-
export const nodeKinds = {
|
|
20
|
-
input: 'Input',
|
|
21
|
-
output: 'Output',
|
|
22
|
-
operation: 'Operation',
|
|
23
|
-
schema: 'Schema',
|
|
24
|
-
property: 'Property',
|
|
25
|
-
parameter: 'Parameter',
|
|
26
|
-
response: 'Response',
|
|
27
|
-
functionParameter: 'FunctionParameter',
|
|
28
|
-
parameterGroup: 'ParameterGroup',
|
|
29
|
-
functionParameters: 'FunctionParameters',
|
|
30
|
-
type: 'Type',
|
|
31
|
-
file: 'File',
|
|
32
|
-
import: 'Import',
|
|
33
|
-
export: 'Export',
|
|
34
|
-
source: 'Source',
|
|
35
|
-
text: 'Text',
|
|
36
|
-
break: 'Break',
|
|
37
|
-
} as const satisfies Record<string, NodeKind>
|
|
38
|
-
|
|
39
17
|
/**
|
|
40
18
|
* Schema type discriminators used by all AST schema nodes.
|
|
41
19
|
*
|
|
@@ -198,31 +176,3 @@ export const httpMethods = {
|
|
|
198
176
|
* ```
|
|
199
177
|
*/
|
|
200
178
|
export const WALK_CONCURRENCY = 30
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Common MIME types used in request/response content negotiation.
|
|
204
|
-
*
|
|
205
|
-
* Covers JSON, XML, form data, PDFs, images, audio, and video formats.
|
|
206
|
-
* Use these as keys when serializing request/response bodies.
|
|
207
|
-
*/
|
|
208
|
-
export const mediaTypes = {
|
|
209
|
-
applicationJson: 'application/json',
|
|
210
|
-
applicationXml: 'application/xml',
|
|
211
|
-
applicationFormUrlEncoded: 'application/x-www-form-urlencoded',
|
|
212
|
-
applicationOctetStream: 'application/octet-stream',
|
|
213
|
-
applicationPdf: 'application/pdf',
|
|
214
|
-
applicationZip: 'application/zip',
|
|
215
|
-
applicationGraphql: 'application/graphql',
|
|
216
|
-
multipartFormData: 'multipart/form-data',
|
|
217
|
-
textPlain: 'text/plain',
|
|
218
|
-
textHtml: 'text/html',
|
|
219
|
-
textCsv: 'text/csv',
|
|
220
|
-
textXml: 'text/xml',
|
|
221
|
-
imagePng: 'image/png',
|
|
222
|
-
imageJpeg: 'image/jpeg',
|
|
223
|
-
imageGif: 'image/gif',
|
|
224
|
-
imageWebp: 'image/webp',
|
|
225
|
-
imageSvgXml: 'image/svg+xml',
|
|
226
|
-
audioMpeg: 'audio/mpeg',
|
|
227
|
-
videoMp4: 'video/mp4',
|
|
228
|
-
} as const satisfies Record<string, MediaType>
|
package/src/dedupe.ts
CHANGED
|
@@ -101,12 +101,11 @@ export function applyDedupe(node: OperationNode, canonicalBySignature: ReadonlyM
|
|
|
101
101
|
export function applyDedupe(node: Node, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch = false): Node {
|
|
102
102
|
if (canonicalBySignature.size === 0) return node
|
|
103
103
|
|
|
104
|
-
const signatures = new Map<SchemaNode, string>()
|
|
105
104
|
const root = node
|
|
106
105
|
|
|
107
106
|
return transform(node, {
|
|
108
107
|
schema(schemaNode) {
|
|
109
|
-
const signature = signatureOf(schemaNode
|
|
108
|
+
const signature = signatureOf(schemaNode)
|
|
110
109
|
if (skipRootMatch && schemaNode === root) return undefined
|
|
111
110
|
|
|
112
111
|
const canonical = canonicalBySignature.get(signature)
|
|
@@ -145,7 +144,6 @@ function cleanDefinition(node: SchemaNode, name: string): SchemaNode {
|
|
|
145
144
|
export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {
|
|
146
145
|
const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options
|
|
147
146
|
|
|
148
|
-
const signatures = new Map<SchemaNode, string>()
|
|
149
147
|
const topLevelNodes = new Set<SchemaNode>()
|
|
150
148
|
|
|
151
149
|
type Group = {
|
|
@@ -156,7 +154,7 @@ export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupe
|
|
|
156
154
|
const groups = new Map<string, Group>()
|
|
157
155
|
|
|
158
156
|
function record(schemaNode: SchemaNode): void {
|
|
159
|
-
const signature = signatureOf(schemaNode
|
|
157
|
+
const signature = signatureOf(schemaNode)
|
|
160
158
|
if (!isCandidate(schemaNode)) return
|
|
161
159
|
|
|
162
160
|
const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name
|
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,4 @@
|
|
|
1
|
-
export { httpMethods,
|
|
1
|
+
export { httpMethods, schemaTypes } from './constants.ts'
|
|
2
2
|
export { applyDedupe, buildDedupePlan } from './dedupe.ts'
|
|
3
3
|
export { defineSchemaDialect } from './dialect.ts'
|
|
4
4
|
export { dispatch } from './dispatch.ts'
|
|
@@ -6,7 +6,6 @@ export {
|
|
|
6
6
|
createArrowFunction,
|
|
7
7
|
createBreak,
|
|
8
8
|
createConst,
|
|
9
|
-
createContent,
|
|
10
9
|
createExport,
|
|
11
10
|
createFile,
|
|
12
11
|
createFunction,
|
|
@@ -22,7 +21,6 @@ export {
|
|
|
22
21
|
createParameterGroup,
|
|
23
22
|
createParamsType,
|
|
24
23
|
createProperty,
|
|
25
|
-
createRequestBody,
|
|
26
24
|
createResponse,
|
|
27
25
|
createSchema,
|
|
28
26
|
createSource,
|
|
@@ -31,16 +29,15 @@ export {
|
|
|
31
29
|
syncOptionality,
|
|
32
30
|
update,
|
|
33
31
|
} from './factory.ts'
|
|
34
|
-
export { isHttpOperationNode,
|
|
32
|
+
export { isHttpOperationNode, isOperationNode, isSchemaNode, narrowSchema } from './guards.ts'
|
|
35
33
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
36
34
|
export { extractRefName } from './refs.ts'
|
|
37
35
|
export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
|
|
38
|
-
export {
|
|
39
|
-
export {
|
|
36
|
+
export { schemaSignature } from './signature.ts'
|
|
37
|
+
export { mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
40
38
|
export type * from './types.ts'
|
|
41
39
|
export {
|
|
42
40
|
caseParams,
|
|
43
|
-
collectReferencedSchemaNames,
|
|
44
41
|
collectUsedSchemaNames,
|
|
45
42
|
containsCircularRef,
|
|
46
43
|
createDiscriminantNode,
|
|
@@ -48,7 +45,6 @@ export {
|
|
|
48
45
|
extractStringsFromNodes,
|
|
49
46
|
findCircularSchemas,
|
|
50
47
|
isStringType,
|
|
51
|
-
resolveRefName,
|
|
52
48
|
syncSchemaRef,
|
|
53
49
|
} from './utils.ts'
|
|
54
|
-
export { collect,
|
|
50
|
+
export { collect, transform, walk } from './visitor.ts'
|
package/src/infer.ts
CHANGED
|
@@ -130,12 +130,3 @@ export type InferSchemaNode<
|
|
|
130
130
|
? TEntry[1]
|
|
131
131
|
: InferSchemaNode<TSchema, TDateType, TRest>
|
|
132
132
|
: SchemaNode
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Backward-compatible alias for `InferSchemaNode`.
|
|
136
|
-
*/
|
|
137
|
-
export type InferSchema<
|
|
138
|
-
TSchema extends object,
|
|
139
|
-
TDateType extends ParserOptions['dateType'] = 'string',
|
|
140
|
-
TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>,
|
|
141
|
-
> = InferSchemaNode<TSchema, TDateType, TEntries>
|
package/src/refs.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
import type { InputNode } from './nodes/root.ts'
|
|
2
|
-
import type { SchemaNode } from './nodes/schema.ts'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Lookup map from schema name to `SchemaNode`.
|
|
6
|
-
*/
|
|
7
|
-
export type RefMap = Map<string, SchemaNode>
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
2
|
* Returns the last path segment of a reference string.
|
|
11
3
|
*
|
|
@@ -19,51 +11,3 @@ export type RefMap = Map<string, SchemaNode>
|
|
|
19
11
|
export function extractRefName(ref: string): string {
|
|
20
12
|
return ref.split('/').at(-1) ?? ref
|
|
21
13
|
}
|
|
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
CHANGED
|
@@ -16,90 +16,187 @@ function refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {
|
|
|
16
16
|
return node.name ?? ''
|
|
17
17
|
}
|
|
18
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
|
+
|
|
19
61
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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.
|
|
23
65
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
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
|
+
}
|
|
26
106
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
const pattern = node.patternProperties
|
|
37
|
-
? Object.keys(node.patternProperties)
|
|
38
|
-
.sort()
|
|
39
|
-
.map((key) => `${key}=${signatureOf(node.patternProperties![key]!, signatures)}`)
|
|
40
|
-
.join(',')
|
|
41
|
-
: ''
|
|
42
|
-
return `object|${flags}|p[${props}]|${additional}|pp[${pattern}]|mn:${node.minProperties ?? ''}|mx:${node.maxProperties ?? ''}`
|
|
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) : ''}`
|
|
43
116
|
}
|
|
44
|
-
case '
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const rest = node.rest ? signatureOf(node.rest, signatures) : ''
|
|
48
|
-
return `${node.type}|${flags}|i[${items}]|r:${rest}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|u:${node.unique ? 1 : 0}`
|
|
117
|
+
case 'children': {
|
|
118
|
+
const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []
|
|
119
|
+
return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`
|
|
49
120
|
}
|
|
50
|
-
case '
|
|
51
|
-
const
|
|
52
|
-
|
|
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}]`
|
|
53
125
|
}
|
|
54
|
-
case '
|
|
55
|
-
const
|
|
56
|
-
return `
|
|
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 ''
|
|
57
131
|
}
|
|
58
|
-
case '
|
|
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' }>
|
|
59
144
|
let values = ''
|
|
60
|
-
if (
|
|
61
|
-
values =
|
|
62
|
-
} else if (
|
|
63
|
-
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(',')
|
|
64
149
|
}
|
|
65
|
-
return `
|
|
150
|
+
return `v[${values}]`
|
|
151
|
+
}
|
|
152
|
+
case 'refTarget': {
|
|
153
|
+
return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`
|
|
66
154
|
}
|
|
67
|
-
case 'ref':
|
|
68
|
-
return `ref|${flags}|->${refTargetName(node)}`
|
|
69
|
-
case 'string':
|
|
70
|
-
return `string|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|pt:${node.pattern ?? ''}`
|
|
71
|
-
case 'number':
|
|
72
|
-
case 'integer':
|
|
73
|
-
case 'bigint':
|
|
74
|
-
return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|emn:${node.exclusiveMinimum ?? ''}|emx:${node.exclusiveMaximum ?? ''}|mo:${node.multipleOf ?? ''}`
|
|
75
|
-
case 'url':
|
|
76
|
-
return `url|${flags}|path:${node.path ?? ''}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
|
|
77
|
-
case 'uuid':
|
|
78
|
-
case 'email':
|
|
79
|
-
return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
|
|
80
|
-
case 'datetime':
|
|
81
|
-
return `datetime|${flags}|o:${node.offset ? 1 : 0}|l:${node.local ? 1 : 0}`
|
|
82
|
-
case 'date':
|
|
83
|
-
case 'time':
|
|
84
|
-
return `${node.type}|${flags}|rep:${node.representation}`
|
|
85
|
-
default:
|
|
86
|
-
return `${node.type}|${flags}`
|
|
87
155
|
}
|
|
88
156
|
}
|
|
89
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
|
+
|
|
90
187
|
/**
|
|
91
188
|
* Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
|
|
92
189
|
* children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
|
|
93
190
|
* full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
|
|
94
191
|
* digest is identical across calls because it depends only on content — never on traversal
|
|
95
192
|
* order. This keeps the keys built during planning consistent with the ones recomputed later
|
|
96
|
-
* during streaming.
|
|
193
|
+
* during streaming. {@link signatureCache} memoizes node → digest across every computation.
|
|
97
194
|
*/
|
|
98
|
-
export function signatureOf(node: SchemaNode
|
|
99
|
-
const cached =
|
|
195
|
+
export function signatureOf(node: SchemaNode): string {
|
|
196
|
+
const cached = signatureCache.get(node)
|
|
100
197
|
if (cached !== undefined) return cached
|
|
101
|
-
const signature = createHash('sha256').update(describeShape(node
|
|
102
|
-
|
|
198
|
+
const signature = createHash('sha256').update(describeShape(node)).digest('hex')
|
|
199
|
+
signatureCache.set(node, signature)
|
|
103
200
|
return signature
|
|
104
201
|
}
|
|
105
202
|
|
|
@@ -119,7 +216,7 @@ export function signatureOf(node: SchemaNode, signatures: Map<SchemaNode, string
|
|
|
119
216
|
* ```
|
|
120
217
|
*/
|
|
121
218
|
export function schemaSignature(node: SchemaNode): string {
|
|
122
|
-
return signatureOf(node
|
|
219
|
+
return signatureOf(node)
|
|
123
220
|
}
|
|
124
221
|
|
|
125
222
|
/**
|
package/src/types.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type { BuildDedupePlanOptions, DedupeCanonical, DedupePlan } from './dedu
|
|
|
3
3
|
export type { SchemaDialect } from './dialect.ts'
|
|
4
4
|
export type { DispatchRule } from './dispatch.ts'
|
|
5
5
|
export type { DistributiveOmit } from './factory.ts'
|
|
6
|
-
export type {
|
|
6
|
+
export type { InferSchemaNode, ParserOptions } from './infer.ts'
|
|
7
7
|
export type {
|
|
8
8
|
ArraySchemaNode,
|
|
9
9
|
ArrowFunctionNode,
|
|
@@ -70,7 +70,6 @@ export type {
|
|
|
70
70
|
UnionSchemaNode,
|
|
71
71
|
UrlSchemaNode,
|
|
72
72
|
} from './nodes/index.ts'
|
|
73
|
-
export type { RefMap } from './refs.ts'
|
|
74
73
|
export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
|
|
75
74
|
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
|
|
76
75
|
export type { ScalarPrimitive } from './constants.ts'
|
package/src/visitor.ts
CHANGED
|
@@ -401,10 +401,14 @@ export async function walk(node: Node, options: WalkOptions): Promise<void> {
|
|
|
401
401
|
async function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {
|
|
402
402
|
await limit(() => applyVisitor(node, visitor, parent))
|
|
403
403
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
404
|
+
// Visit siblings concurrently and let the shared `limit` cap how many callbacks
|
|
405
|
+
// run at once. Awaiting each child sequentially here would serialize the whole
|
|
406
|
+
// traversal and make `concurrency` inert — every visitor callback would run one
|
|
407
|
+
// at a time regardless of the limit.
|
|
408
|
+
const children = Array.from(getChildren(node, recurse))
|
|
409
|
+
if (children.length === 0) return
|
|
410
|
+
|
|
411
|
+
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))
|
|
408
412
|
}
|
|
409
413
|
|
|
410
414
|
/**
|
|
File without changes
|