@voxgig/apidef 8.28.1 → 8.30.0

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 (49) hide show
  1. package/README.md +18 -0
  2. package/dist/builder/entity/entity.js +1 -1
  3. package/dist/builder/entity/entity.js.map +1 -1
  4. package/dist/builder/entity/info.js +1 -1
  5. package/dist/builder/entity/info.js.map +1 -1
  6. package/dist/guide/graphql01.js +6 -3
  7. package/dist/guide/graphql01.js.map +1 -1
  8. package/dist/guide/guide.js +2 -10
  9. package/dist/guide/guide.js.map +1 -1
  10. package/dist/guide/heuristic01.js +87 -18
  11. package/dist/guide/heuristic01.js.map +1 -1
  12. package/dist/model.d.ts +1 -0
  13. package/dist/parse.d.ts +2 -1
  14. package/dist/parse.js +65 -18
  15. package/dist/parse.js.map +1 -1
  16. package/dist/transform/entity.js +3 -3
  17. package/dist/transform/entity.js.map +1 -1
  18. package/dist/transform/field.d.ts +2 -1
  19. package/dist/transform/field.js +33 -21
  20. package/dist/transform/field.js.map +1 -1
  21. package/dist/transform/flowstep.js +21 -1
  22. package/dist/transform/flowstep.js.map +1 -1
  23. package/dist/transform/operation.js +13 -24
  24. package/dist/transform/operation.js.map +1 -1
  25. package/dist/transform/top.js +8 -0
  26. package/dist/transform/top.js.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/dist/utility.d.ts +10 -2
  29. package/dist/utility.js +177 -28
  30. package/dist/utility.js.map +1 -1
  31. package/dist/yamlalias.d.ts +3 -0
  32. package/dist/yamlalias.js +269 -0
  33. package/dist/yamlalias.js.map +1 -0
  34. package/model/apidef.aontu +1 -0
  35. package/package.json +1 -1
  36. package/src/builder/entity/entity.ts +1 -1
  37. package/src/builder/entity/info.ts +1 -1
  38. package/src/guide/graphql01.ts +9 -3
  39. package/src/guide/guide.ts +2 -10
  40. package/src/guide/heuristic01.ts +101 -20
  41. package/src/model.ts +1 -0
  42. package/src/parse.ts +68 -26
  43. package/src/transform/entity.ts +5 -5
  44. package/src/transform/field.ts +39 -21
  45. package/src/transform/flowstep.ts +24 -1
  46. package/src/transform/operation.ts +15 -27
  47. package/src/transform/top.ts +9 -0
  48. package/src/utility.ts +204 -28
  49. package/src/yamlalias.ts +295 -0
@@ -0,0 +1,295 @@
1
+ /* Copyright (c) 2026 Voxgig Ltd, MIT License */
2
+
3
+ // The YAML parser copies an anchor's node in full for every alias, so nested
4
+ // aliases grow geometrically. This reads, from the text and before anything is
5
+ // parsed, the size the source reaches once every alias is copied in.
6
+
7
+ const YAML_ALIAS_FACTOR = 10
8
+ const YAML_ALIAS_FLOOR = 1_000_000
9
+
10
+ type Anchor = { name: string, start: number, end: number }
11
+ type Alias = { name: string, at: number, target?: Anchor }
12
+
13
+ const SP = 0x20, TAB = 0x09, LF = 0x0a, CR = 0x0d
14
+ const HASH = 0x23, DQ = 0x22, SQ = 0x27, BSL = 0x5c, COLON = 0x3a, COMMA = 0x2c
15
+ const DASH = 0x2d, QMARK = 0x3f, AMP = 0x26, STAR = 0x2a, BANG = 0x21
16
+ const LSQ = 0x5b, RSQ = 0x5d, LCB = 0x7b, RCB = 0x7d, PIPE = 0x7c, GT = 0x3e
17
+
18
+ const isSpace = (c: number) => SP === c || TAB === c
19
+ const isBreak = (c: number) => LF === c || CR === c
20
+ const isGap = (c: number | undefined) => undefined === c || isSpace(c) || isBreak(c)
21
+
22
+
23
+ function yamlAliasLimit(src: string): number {
24
+ return Math.max(YAML_ALIAS_FLOOR, YAML_ALIAS_FACTOR * utf8(src).length)
25
+ }
26
+
27
+
28
+ // In bytes of UTF-8, which the Go port counts too.
29
+ function yamlAliasedSize(src: string): number {
30
+ const b = utf8(src)
31
+ if (!b.includes(AMP) || !b.includes(STAR)) return b.length
32
+
33
+ const { anchors, aliases } = scanYamlNodes(b)
34
+ if (0 === aliases.length) return b.length
35
+
36
+ // An alias names the last anchor of its name before it.
37
+ const latest = new Map<string, Anchor>()
38
+ let ai = 0
39
+ for (const alias of aliases) {
40
+ while (ai < anchors.length && anchors[ai].start < alias.at) {
41
+ latest.set(anchors[ai].name, anchors[ai])
42
+ ai++
43
+ }
44
+ alias.target = latest.get(alias.name)
45
+ }
46
+
47
+ const sizes = new Map<Anchor, number>()
48
+ const open = new Set<Anchor>()
49
+ const size = (a: Anchor): number => {
50
+ const known = sizes.get(a)
51
+ if (undefined !== known) return known
52
+ if (open.has(a)) return 0
53
+ open.add(a)
54
+ let n = a.end - a.start
55
+ for (let x = firstAt(aliases, a.start); x < aliases.length && aliases[x].at < a.end; x++) {
56
+ const t = aliases[x].target
57
+ if (undefined !== t) n += size(t)
58
+ }
59
+ open.delete(a)
60
+ sizes.set(a, n)
61
+ return n
62
+ }
63
+
64
+ let total = b.length
65
+ for (const alias of aliases) {
66
+ if (undefined !== alias.target) total += size(alias.target)
67
+ }
68
+ return total
69
+ }
70
+
71
+
72
+ function utf8(src: string): Uint8Array {
73
+ return new TextEncoder().encode(src)
74
+ }
75
+
76
+
77
+ // Index of the first alias at or after a byte offset.
78
+ function firstAt(aliases: Alias[], at: number): number {
79
+ let lo = 0, hi = aliases.length
80
+ while (lo < hi) {
81
+ const mid = (lo + hi) >> 1
82
+ if (aliases[mid].at < at) lo = mid + 1
83
+ else hi = mid
84
+ }
85
+ return lo
86
+ }
87
+
88
+
89
+ // Anchors with the byte range of the node each labels, and aliases with where
90
+ // they stand. Comments, quoted strings and block scalars are skipped.
91
+ function scanYamlNodes(b: Uint8Array): { anchors: Anchor[], aliases: Alias[] } {
92
+ const anchors: Anchor[] = []
93
+ const aliases: Alias[] = []
94
+
95
+ // A block node's anchor ends where a later line returns to its indent;
96
+ // others end with their bracket or their line.
97
+ const blockAnchors: { anchor: Anchor, indent: number, afterKey: boolean }[] = []
98
+ const bracketAnchors: { anchor: Anchor, depth: number }[] = []
99
+
100
+ let flow = 0
101
+ let blockIndent = -1
102
+ let lineIndent = 0
103
+ let lineBegin = 0
104
+ let scalarCol = 0
105
+ let keyCol = 0
106
+ let lineStart = true
107
+ let nodeStart = true
108
+ let afterKey = false
109
+ let i = 0
110
+
111
+ while (i < b.length) {
112
+ if (lineStart) {
113
+ lineStart = false
114
+ lineBegin = i
115
+ const j = indentEnd(b, i)
116
+ const blank = j >= b.length || isBreak(b[j]) || HASH === b[j]
117
+ if (0 <= blockIndent && (blank || j - i > blockIndent)) {
118
+ i = endOfLine(b, i)
119
+ continue
120
+ }
121
+ blockIndent = -1
122
+ lineIndent = j - i
123
+ if (0 === flow) {
124
+ nodeStart = true
125
+ afterKey = false
126
+ }
127
+ i = j
128
+ continue
129
+ }
130
+
131
+ const c = b[i]
132
+ if (LF === c) {
133
+ lineStart = true
134
+ i++
135
+ }
136
+ else if (CR === c || isSpace(c)) {
137
+ i++
138
+ }
139
+ else if (HASH === c && isGap(b[i - 1])) {
140
+ i = endOfLine(b, i)
141
+ }
142
+ else if (nodeStart && (DQ === c || SQ === c)) {
143
+ scalarCol = i - lineBegin
144
+ i = endOfQuoted(b, i)
145
+ nodeStart = false
146
+ }
147
+ else if (nodeStart && STAR === c) {
148
+ const end = endOfName(b, i + 1)
149
+ aliases.push({ name: decode(b, i + 1, end), at: i })
150
+ nodeStart = false
151
+ i = end
152
+ }
153
+ else if (nodeStart && AMP === c) {
154
+ const end = endOfName(b, i + 1)
155
+ const anchor: Anchor = { name: decode(b, i + 1, end), start: i, end: b.length }
156
+ anchors.push(anchor)
157
+ let k = end
158
+ while (k < b.length && isSpace(b[k])) k++
159
+ if (k >= b.length || isBreak(b[k]) || HASH === b[k]) {
160
+ blockAnchors.push({ anchor, indent: afterKey ? keyCol : lineIndent, afterKey })
161
+ }
162
+ else if (LSQ === b[k] || LCB === b[k]) {
163
+ bracketAnchors.push({ anchor, depth: flow + 1 })
164
+ }
165
+ else {
166
+ anchor.end = 0 < flow ? endOfFlowScalar(b, k) : endOfLine(b, k)
167
+ }
168
+ i = end
169
+ }
170
+ else if (nodeStart && BANG === c) {
171
+ i = endOfName(b, i + 1)
172
+ }
173
+ else if (nodeStart && 0 === flow && (PIPE === c || GT === c)) {
174
+ blockIndent = lineIndent
175
+ i = endOfLine(b, i)
176
+ }
177
+ else if (LSQ === c || LCB === c) {
178
+ flow++
179
+ nodeStart = true
180
+ i++
181
+ }
182
+ else if (RSQ === c || RCB === c) {
183
+ for (let x = bracketAnchors.length - 1; 0 <= x; x--) {
184
+ if (bracketAnchors[x].depth === flow) {
185
+ bracketAnchors[x].anchor.end = i + 1
186
+ bracketAnchors.splice(x, 1)
187
+ }
188
+ }
189
+ flow = Math.max(0, flow - 1)
190
+ nodeStart = false
191
+ i++
192
+ }
193
+ else if (COMMA === c && 0 < flow) {
194
+ nodeStart = true
195
+ i++
196
+ }
197
+ else if (nodeStart && 0 === flow && (DASH === c || QMARK === c) && isGap(b[i + 1])) {
198
+ i++
199
+ }
200
+ else if (COLON === c && (0 < flow || isGap(b[i + 1]))) {
201
+ nodeStart = true
202
+ afterKey = 0 === flow
203
+ keyCol = scalarCol
204
+ i++
205
+ }
206
+ else {
207
+ if (nodeStart) scalarCol = i - lineBegin
208
+ nodeStart = false
209
+ i++
210
+ }
211
+ }
212
+
213
+ for (const { anchor, indent, afterKey: keyed } of blockAnchors) {
214
+ anchor.end = blockEnd(b, endOfLine(b, anchor.start), indent, keyed)
215
+ }
216
+
217
+ return { anchors, aliases }
218
+ }
219
+
220
+
221
+ // The first line after `from` that returns to `indent`; a compact sequence
222
+ // under a key may sit at the key's own indent.
223
+ function blockEnd(b: Uint8Array, from: number, indent: number, keyed: boolean): number {
224
+ let s = from
225
+ while (s < b.length) {
226
+ s = LF === b[s] ? s + 1 : endOfLine(b, s) + 1
227
+ if (s >= b.length) break
228
+ const j = indentEnd(b, s)
229
+ if (j >= b.length || isBreak(b[j]) || HASH === b[j]) continue
230
+ const d = j - s
231
+ const entry = DASH === b[j] && isGap(b[j + 1])
232
+ if (d < indent || (d === indent && !(keyed && entry))) return s
233
+ }
234
+ return b.length
235
+ }
236
+
237
+
238
+ function indentEnd(b: Uint8Array, i: number): number {
239
+ while (i < b.length && SP === b[i]) i++
240
+ return i
241
+ }
242
+
243
+
244
+ function endOfLine(b: Uint8Array, i: number): number {
245
+ while (i < b.length && LF !== b[i]) i++
246
+ return i
247
+ }
248
+
249
+
250
+ function endOfQuoted(b: Uint8Array, i: number): number {
251
+ const q = b[i]
252
+ i++
253
+ while (i < b.length) {
254
+ if (DQ === q && BSL === b[i]) {
255
+ i += 2
256
+ }
257
+ else if (q === b[i] && SQ === q && SQ === b[i + 1]) {
258
+ i += 2
259
+ }
260
+ else if (q === b[i]) {
261
+ return i + 1
262
+ }
263
+ else {
264
+ i++
265
+ }
266
+ }
267
+ return i
268
+ }
269
+
270
+
271
+ function endOfName(b: Uint8Array, i: number): number {
272
+ while (i < b.length && !isGap(b[i]) && COMMA !== b[i] && LSQ !== b[i] &&
273
+ RSQ !== b[i] && LCB !== b[i] && RCB !== b[i] &&
274
+ !(COLON === b[i] && isGap(b[i + 1]))) {
275
+ i++
276
+ }
277
+ return i
278
+ }
279
+
280
+
281
+ function endOfFlowScalar(b: Uint8Array, i: number): number {
282
+ while (i < b.length && !isBreak(b[i]) && COMMA !== b[i] && RSQ !== b[i] && RCB !== b[i]) i++
283
+ return i
284
+ }
285
+
286
+
287
+ function decode(b: Uint8Array, start: number, end: number): string {
288
+ return new TextDecoder().decode(b.subarray(start, end))
289
+ }
290
+
291
+
292
+ export {
293
+ yamlAliasedSize,
294
+ yamlAliasLimit,
295
+ }