@voxgig/apidef 8.15.2 → 8.17.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.
- package/dist/apidef.js +15 -0
- package/dist/apidef.js.map +1 -1
- package/dist/builder/entity/entity.js +8 -5
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/builder/entity/info.js +1 -1
- package/dist/builder/entity/info.js.map +1 -1
- package/dist/builder/flow.js +3 -3
- package/dist/builder/flow.js.map +1 -1
- package/dist/cli.js +8 -10
- package/dist/cli.js.map +1 -1
- package/dist/desc.d.ts +2 -0
- package/dist/guide/guide.d.ts +14 -1
- package/dist/guide/guide.js +106 -62
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +9 -1
- package/dist/guide/heuristic01.js +290 -71
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/parse.d.ts +4 -1
- package/dist/parse.js +183 -133
- package/dist/parse.js.map +1 -1
- package/dist/refcount.d.ts +6 -0
- package/dist/refcount.js +215 -0
- package/dist/refcount.js.map +1 -0
- package/dist/transform/entity.d.ts +1 -1
- package/dist/transform/entity.js +12 -3
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.js +1 -1
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +1 -1
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/top.js +1 -1
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +3 -1
- package/dist/utility.js +85 -6
- package/dist/utility.js.map +1 -1
- package/model/{guide.aon → guide.aontu} +1 -1
- package/package.json +3 -3
- package/src/apidef.ts +20 -1
- package/src/builder/entity/entity.ts +8 -5
- package/src/builder/entity/info.ts +1 -1
- package/src/builder/flow.ts +3 -3
- package/src/cli.ts +8 -10
- package/src/desc.ts +3 -0
- package/src/guide/guide.ts +136 -71
- package/src/guide/heuristic01.ts +358 -90
- package/src/parse.ts +221 -126
- package/src/refcount.ts +244 -0
- package/src/transform/entity.ts +16 -5
- package/src/transform/field.ts +1 -1
- package/src/transform/operation.ts +1 -1
- package/src/transform/top.ts +1 -1
- package/src/types.ts +2 -2
- package/src/utility.ts +102 -6
- /package/model/{apidef.aon → apidef.aontu} +0 -0
package/src/parse.ts
CHANGED
|
@@ -103,35 +103,118 @@ async function parseOpenAPI(source: any, _meta?: any) {
|
|
|
103
103
|
)
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
if (SURROGATE_RE.test(source)) {
|
|
107
|
+
parsed = wellFormed(parsed, new Map())
|
|
108
|
+
}
|
|
109
|
+
|
|
106
110
|
if (null == parsed.components) {
|
|
107
111
|
parsed.components = {}
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// long paths and as a result every downstream split('/') sees a literal
|
|
113
|
-
// quote character at the head/tail of the path, breaking URL substitution.
|
|
114
|
-
if (parsed.paths && 'object' === typeof parsed.paths) {
|
|
115
|
-
const cleaned: Record<string, any> = {}
|
|
116
|
-
for (const [k, v] of Object.entries(parsed.paths)) {
|
|
117
|
-
const stripped = k.replace(/^"+|"+$/g, '')
|
|
118
|
-
cleaned[stripped] = v
|
|
119
|
-
}
|
|
120
|
-
parsed.paths = cleaned
|
|
114
|
+
if (isRecord(parsed.paths)) {
|
|
115
|
+
parsed.paths = renameKeys(parsed.paths, normalizePathKeys(Object.keys(parsed.paths)))
|
|
121
116
|
}
|
|
122
117
|
|
|
123
|
-
// See docs/design/derived-names.md
|
|
124
|
-
normalizeColonPathParams(parsed, _meta)
|
|
125
|
-
|
|
126
118
|
// Single-pass: add x-ref properties and resolve $ref pointers together.
|
|
127
119
|
addXRefsAndResolve(parsed, parsed)
|
|
128
120
|
|
|
121
|
+
// After resolution, so a pointer into `paths` names the key as written and a
|
|
122
|
+
// parameter declared by `$ref` counts. See docs/design/derived-names.md
|
|
123
|
+
normalizeColonPathParams(parsed, _meta)
|
|
124
|
+
|
|
129
125
|
const def = decycle(parsed)
|
|
130
126
|
|
|
131
127
|
return def
|
|
132
128
|
}
|
|
133
129
|
|
|
134
130
|
|
|
131
|
+
// A surrogate escape, or a raw lone surrogate unit.
|
|
132
|
+
const SURROGATE_RE =
|
|
133
|
+
/\\(?:u\{?|U)0*[dD][89a-fA-F][0-9a-fA-F]{2}|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/
|
|
134
|
+
|
|
135
|
+
const LONE_SURROGATE_RE =
|
|
136
|
+
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
function wellFormedString(s: string): string {
|
|
140
|
+
return s.replace(LONE_SURROGATE_RE, '�')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
// The Go parser reads a lone surrogate as U+FFFD, and so does every UTF-8
|
|
145
|
+
// writer. Keys it makes equal merge as the parser merges a repeated key.
|
|
146
|
+
function wellFormed(node: any, done: Map<any, any>): any {
|
|
147
|
+
if ('string' === typeof node) return wellFormedString(node)
|
|
148
|
+
if (null == node || 'object' !== typeof node) return node
|
|
149
|
+
if (done.has(node)) return done.get(node)
|
|
150
|
+
|
|
151
|
+
if (Array.isArray(node)) {
|
|
152
|
+
done.set(node, node)
|
|
153
|
+
for (let i = 0; i < node.length; i++) node[i] = wellFormed(node[i], done)
|
|
154
|
+
return node
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const out = Object.create(Object.getPrototypeOf(node))
|
|
158
|
+
done.set(node, out)
|
|
159
|
+
for (const key of Object.keys(node)) {
|
|
160
|
+
const wkey = wellFormedString(key)
|
|
161
|
+
const val = wellFormed(node[key], done)
|
|
162
|
+
const prev = Object.prototype.hasOwnProperty.call(out, wkey) ? out[wkey] : undefined
|
|
163
|
+
out[wkey] = null == prev ? val : Jsonic.util.deep(prev, val)
|
|
164
|
+
}
|
|
165
|
+
return out
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
function renameKeys(obj: any, keys: string[]): any {
|
|
170
|
+
const out = Object.create(Object.getPrototypeOf(obj))
|
|
171
|
+
Object.keys(obj).forEach((key, i) => { out[keys[i]] = obj[key] })
|
|
172
|
+
return out
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
// @tabnas/yaml keeps the quotes of an explicit key (`? "/a"`, `? '/a'`) in the
|
|
177
|
+
// key. A rename that would collide with another key is not made.
|
|
178
|
+
function normalizePathKeys(keys: string[]): string[] {
|
|
179
|
+
return keepDistinct(keys, keys.map(unquotePathKey))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
function unquotePathKey(key: string): string {
|
|
184
|
+
if (key.length < 2 || key[0] !== key[key.length - 1]) return key
|
|
185
|
+
if ("'" === key[0]) return key.slice(1, -1).replace(/''/g, "'")
|
|
186
|
+
if ('"' !== key[0]) return key
|
|
187
|
+
try {
|
|
188
|
+
const decoded = JSON.parse(key)
|
|
189
|
+
if ('string' === typeof decoded) return wellFormedString(decoded)
|
|
190
|
+
}
|
|
191
|
+
catch (_e: any) {
|
|
192
|
+
// Not a JSON string: an escape only YAML knows, so strip the quotes alone.
|
|
193
|
+
}
|
|
194
|
+
return key.slice(1, -1)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
function keepDistinct(keys: string[], next: string[]): string[] {
|
|
199
|
+
const count = new Map<string, number>()
|
|
200
|
+
keys.forEach((key, i) => {
|
|
201
|
+
count.set(key, (count.get(key) ?? 0) + 1)
|
|
202
|
+
if (next[i] !== key) count.set(next[i], (count.get(next[i]) ?? 0) + 1)
|
|
203
|
+
})
|
|
204
|
+
return keys.map((key, i) => next[i] !== key && 1 < (count.get(next[i]) ?? 0) ? key : next[i])
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
// Edges decycle cut, so the guide can still count through them; a clone has none.
|
|
209
|
+
const DECYCLED = new WeakMap<object, Map<string, object>>()
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
function decycledChild(holder: any, key: string | number): any {
|
|
213
|
+
const cut = DECYCLED.get(holder)?.get(String(key))
|
|
214
|
+
return undefined === cut ? holder[key] : cut
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
135
218
|
function decycle(root: any) {
|
|
136
219
|
// Entry path of each node on the current ancestor chain; presence in this
|
|
137
220
|
// map is what identifies a back-edge. Nodes are removed on the way out, so
|
|
@@ -161,6 +244,12 @@ function decycle(root: any) {
|
|
|
161
244
|
|
|
162
245
|
const cyclePath = onPath.get(val)
|
|
163
246
|
if (undefined !== cyclePath) {
|
|
247
|
+
let cuts = DECYCLED.get(node)
|
|
248
|
+
if (undefined === cuts) {
|
|
249
|
+
cuts = new Map()
|
|
250
|
+
DECYCLED.set(node, cuts)
|
|
251
|
+
}
|
|
252
|
+
cuts.set(String(key), val)
|
|
164
253
|
node[key] = `[Circular *${cyclePath.join('.')}]`
|
|
165
254
|
continue
|
|
166
255
|
}
|
|
@@ -189,109 +278,114 @@ function refSiblings(node: any): any {
|
|
|
189
278
|
}
|
|
190
279
|
|
|
191
280
|
|
|
192
|
-
function addXRefsAndResolve(
|
|
281
|
+
function addXRefsAndResolve(
|
|
282
|
+
obj: any,
|
|
283
|
+
root: any,
|
|
284
|
+
visited: WeakSet<any> = new WeakSet(),
|
|
285
|
+
expanding: Map<any, any> = new Map(),
|
|
286
|
+
) {
|
|
193
287
|
if (!obj || typeof obj !== 'object') return
|
|
194
|
-
if (!visited) visited = new WeakSet()
|
|
195
288
|
if (visited.has(obj)) return
|
|
196
289
|
visited.add(obj)
|
|
197
290
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
} else {
|
|
209
|
-
item['x-ref'] = xref
|
|
210
|
-
addXRefsAndResolve(item, root, visited)
|
|
211
|
-
}
|
|
212
|
-
} else {
|
|
213
|
-
addXRefsAndResolve(item, root, visited)
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
} else {
|
|
218
|
-
for (const key of Object.keys(obj)) {
|
|
219
|
-
const val = obj[key]
|
|
220
|
-
if (val && typeof val === 'object') {
|
|
221
|
-
if (typeof val.$ref === 'string') {
|
|
222
|
-
const xref = val.$ref
|
|
223
|
-
const resolved = resolvePointer(root, xref)
|
|
224
|
-
if (resolved !== undefined) {
|
|
225
|
-
obj[key] = { ...resolved, ...refSiblings(val), 'x-ref': xref }
|
|
226
|
-
addXRefsAndResolve(obj[key], root, visited)
|
|
227
|
-
} else {
|
|
228
|
-
val['x-ref'] = xref
|
|
229
|
-
addXRefsAndResolve(val, root, visited)
|
|
230
|
-
}
|
|
231
|
-
} else {
|
|
232
|
-
addXRefsAndResolve(val, root, visited)
|
|
233
|
-
}
|
|
291
|
+
const keys: (string | number)[] =
|
|
292
|
+
Array.isArray(obj) ? Array.from(obj.keys()) : Object.keys(obj)
|
|
293
|
+
|
|
294
|
+
for (const key of keys) {
|
|
295
|
+
const val = obj[key]
|
|
296
|
+
if (val && typeof val === 'object') {
|
|
297
|
+
if (typeof val.$ref === 'string') {
|
|
298
|
+
resolveRefSite(obj, key, root, visited, expanding)
|
|
299
|
+
} else {
|
|
300
|
+
addXRefsAndResolve(val, root, visited, expanding)
|
|
234
301
|
}
|
|
235
302
|
}
|
|
236
303
|
}
|
|
237
304
|
}
|
|
238
305
|
|
|
239
306
|
|
|
240
|
-
function
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
307
|
+
function resolveRefSite(
|
|
308
|
+
holder: any,
|
|
309
|
+
key: string | number,
|
|
310
|
+
root: any,
|
|
311
|
+
visited: WeakSet<any>,
|
|
312
|
+
expanding: Map<any, any>,
|
|
313
|
+
) {
|
|
314
|
+
const site = holder[key]
|
|
245
315
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
316
|
+
const inProgress = expanding.get(site)
|
|
317
|
+
if (undefined !== inProgress) {
|
|
318
|
+
holder[key] = inProgress
|
|
319
|
+
return
|
|
320
|
+
}
|
|
250
321
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
322
|
+
const xref = site.$ref
|
|
323
|
+
const resolved = resolvePointer(root, xref)
|
|
324
|
+
if (resolved === undefined) {
|
|
325
|
+
site['x-ref'] = xref
|
|
326
|
+
addXRefsAndResolve(site, root, visited, expanding)
|
|
327
|
+
return
|
|
328
|
+
}
|
|
255
329
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
330
|
+
const copy = { ...resolved, ...refSiblings(site), 'x-ref': xref }
|
|
331
|
+
holder[key] = copy
|
|
332
|
+
expanding.set(site, copy)
|
|
333
|
+
addXRefsAndResolve(copy, root, visited, expanding)
|
|
334
|
+
expanding.delete(site)
|
|
335
|
+
}
|
|
261
336
|
|
|
262
|
-
// Landed on another alias: follow it. Anything else is the target.
|
|
263
|
-
if (current != null &&
|
|
264
|
-
'object' === typeof current &&
|
|
265
|
-
!Array.isArray(current) &&
|
|
266
|
-
'string' === typeof current.$ref) {
|
|
267
|
-
const sib: any = {}
|
|
268
|
-
let hasSib = false
|
|
269
|
-
for (const k of Object.keys(current)) {
|
|
270
|
-
if ('$ref' === k) continue
|
|
271
|
-
sib[k] = current[k]
|
|
272
|
-
hasSib = true
|
|
273
|
-
}
|
|
274
|
-
if (hasSib) siblings.push(sib)
|
|
275
|
-
pointer = current.$ref
|
|
276
|
-
continue
|
|
277
|
-
}
|
|
278
337
|
|
|
279
|
-
|
|
280
|
-
// No siblings anywhere on the chain: return the target itself, so
|
|
281
|
-
// multiple references keep sharing one object (see the note on
|
|
282
|
-
// addXRefsAndResolve — a fresh copy per site would defeat that).
|
|
283
|
-
return current
|
|
284
|
-
}
|
|
338
|
+
const INDEX_RE = /^(0|[1-9]\d*)$/
|
|
285
339
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
340
|
+
|
|
341
|
+
function isRecord(node: any): boolean {
|
|
342
|
+
return null != node && 'object' === typeof node && !Array.isArray(node)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
function isAlias(node: any): boolean {
|
|
347
|
+
return isRecord(node) && 'string' === typeof node.$ref
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
// An own key of an object, or an RFC 6901 index of an array.
|
|
352
|
+
function pointerChild(node: any, part: string): any {
|
|
353
|
+
if (Array.isArray(node)) {
|
|
354
|
+
return INDEX_RE.test(part) && Number(part) < node.length ? node[Number(part)] : undefined
|
|
355
|
+
}
|
|
356
|
+
return isRecord(node) && Object.prototype.hasOwnProperty.call(node, part) ?
|
|
357
|
+
node[part] : undefined
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
// The object a pointer names, following every alias met on the way, so the
|
|
362
|
+
// answer does not depend on which aliases the walk has already replaced. A
|
|
363
|
+
// pointer that names no object, or needs itself, names nothing.
|
|
364
|
+
function resolvePointer(root: any, ref: string, active: Set<string> = new Set()): any {
|
|
365
|
+
if (!ref.startsWith('#/') || active.has(ref)) return undefined
|
|
366
|
+
active.add(ref)
|
|
367
|
+
|
|
368
|
+
let node: any = root
|
|
369
|
+
for (const raw of ref.substring(2).split('/')) {
|
|
370
|
+
node = pointerChild(followAlias(root, node, active),
|
|
371
|
+
raw.replace(/~1/g, '/').replace(/~0/g, '~'))
|
|
372
|
+
if (undefined === node) break
|
|
294
373
|
}
|
|
374
|
+
node = followAlias(root, node, active)
|
|
375
|
+
|
|
376
|
+
active.delete(ref)
|
|
377
|
+
return isRecord(node) ? node : undefined
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
// An alias's own keywords win over its target's. Without them the target
|
|
382
|
+
// itself is the answer, so references keep sharing one object.
|
|
383
|
+
function followAlias(root: any, node: any, active: Set<string>): any {
|
|
384
|
+
if (!isAlias(node)) return node
|
|
385
|
+
const target = resolvePointer(root, node.$ref, active)
|
|
386
|
+
if (undefined === target) return undefined
|
|
387
|
+
const sib = refSiblings(node)
|
|
388
|
+
return 0 === Object.keys(sib).length ? target : { ...target, ...sib }
|
|
295
389
|
}
|
|
296
390
|
|
|
297
391
|
|
|
@@ -316,6 +410,9 @@ function validateSource(kind: string, source: any, meta: { file: string }) {
|
|
|
316
410
|
|
|
317
411
|
export {
|
|
318
412
|
parse,
|
|
413
|
+
decycledChild,
|
|
414
|
+
normalizePathKeys,
|
|
415
|
+
colonPathKeys,
|
|
319
416
|
}
|
|
320
417
|
|
|
321
418
|
|
|
@@ -327,19 +424,31 @@ const METHODS = [
|
|
|
327
424
|
// Rewrite `/a/:b/c` to `/a/{b}/c`, for a `:b` the path or one of its
|
|
328
425
|
// operations declares `in: path`. See docs/design/derived-names.md
|
|
329
426
|
function normalizeColonPathParams(parsed: any, meta?: any) {
|
|
330
|
-
if (
|
|
427
|
+
if (!isRecord(parsed.paths)) return
|
|
331
428
|
|
|
332
|
-
const
|
|
333
|
-
const
|
|
429
|
+
const paths = Object.keys(parsed.paths)
|
|
430
|
+
const next = colonPathKeys(parsed.paths)
|
|
431
|
+
parsed.paths = renameKeys(parsed.paths, next)
|
|
334
432
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
433
|
+
const renamed = paths.filter((path, i) => next[i] !== path)
|
|
434
|
+
if (0 < renamed.length && null != meta?.log?.info) {
|
|
435
|
+
meta.log.info({
|
|
436
|
+
point: 'path-colon-params',
|
|
437
|
+
count: renamed.length,
|
|
438
|
+
note: 'rewrote ' + renamed.length + ' colon-style path parameter(s) to' +
|
|
439
|
+
' OpenAPI brace form, e.g. ' + renamed[0]
|
|
440
|
+
})
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
// Every `in: path` name counts: the path item's parameters and each operation's.
|
|
446
|
+
function colonPathKeys(paths: Record<string, any>): string[] {
|
|
447
|
+
const keys = Object.keys(paths)
|
|
448
|
+
return keepDistinct(keys, keys.map((path) => {
|
|
449
|
+
if (!path.includes('/:')) return path
|
|
340
450
|
|
|
341
|
-
|
|
342
|
-
// plus each operation's own.
|
|
451
|
+
const item = paths[path]
|
|
343
452
|
const declared = new Set<string>()
|
|
344
453
|
const collect = (params: any) => {
|
|
345
454
|
if (!Array.isArray(params)) return
|
|
@@ -356,22 +465,8 @@ function normalizeColonPathParams(parsed: any, meta?: any) {
|
|
|
356
465
|
}
|
|
357
466
|
}
|
|
358
467
|
|
|
359
|
-
|
|
468
|
+
return path.split('/').map((seg) =>
|
|
360
469
|
seg.startsWith(':') && declared.has(seg.slice(1)) ? '{' + seg.slice(1) + '}' : seg
|
|
361
470
|
).join('/')
|
|
362
|
-
|
|
363
|
-
if (next !== path) renamed.push(path)
|
|
364
|
-
out[next] = item
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
parsed.paths = out
|
|
368
|
-
|
|
369
|
-
if (0 < renamed.length && null != meta?.log?.info) {
|
|
370
|
-
meta.log.info({
|
|
371
|
-
point: 'path-colon-params',
|
|
372
|
-
count: renamed.length,
|
|
373
|
-
note: 'rewrote ' + renamed.length + ' colon-style path parameter(s) to' +
|
|
374
|
-
' OpenAPI brace form, e.g. ' + renamed[0]
|
|
375
|
-
})
|
|
376
|
-
}
|
|
471
|
+
}))
|
|
377
472
|
}
|
package/src/refcount.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/* Copyright (c) 2026 Voxgig, MIT License */
|
|
2
|
+
|
|
3
|
+
import { decycledChild } from './parse'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const REFCOUNT_CAP = 1_000_000_000
|
|
7
|
+
|
|
8
|
+
const ROOT = '\x01ROOT'
|
|
9
|
+
|
|
10
|
+
const INDEX_RE = /^(0|[1-9]\d*)$/
|
|
11
|
+
|
|
12
|
+
type Body = Map<string, number>
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
function satAdd(a: number, b: number): number {
|
|
16
|
+
return Math.min(a + b, REFCOUNT_CAP)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
function satMul(a: number, w: number): number {
|
|
21
|
+
if (0 === a || 0 === w) return 0
|
|
22
|
+
if (a > Math.floor(REFCOUNT_CAP / w)) return REFCOUNT_CAP
|
|
23
|
+
return a * w
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
function byCodePoint(a: string, b: string): number {
|
|
28
|
+
const n = Math.min(a.length, b.length)
|
|
29
|
+
for (let i = 0; i < n; i++) {
|
|
30
|
+
const x = a.charCodeAt(i)
|
|
31
|
+
const y = b.charCodeAt(i)
|
|
32
|
+
if (x !== y) return codePointRank(x) - codePointRank(y)
|
|
33
|
+
}
|
|
34
|
+
return a.length - b.length
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// Surrogate units sort below U+E000-U+FFFF as UTF-16 but above them by code point.
|
|
39
|
+
function codePointRank(unit: number): number {
|
|
40
|
+
return unit < 0xD800 ? unit : unit < 0xE000 ? unit + 0x2000 : unit - 0x800
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
function isNode(v: any): boolean {
|
|
45
|
+
return null != v && 'object' === typeof v
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
function refLabel(v: any): string | undefined {
|
|
50
|
+
if (!isNode(v) || Array.isArray(v)) return undefined
|
|
51
|
+
if ('string' === typeof v['x-ref']) return v['x-ref']
|
|
52
|
+
if (!Object.prototype.hasOwnProperty.call(v, 'x-ref') && 'string' === typeof v.$ref) {
|
|
53
|
+
return v.$ref
|
|
54
|
+
}
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
function hasKid(node: any, key: string): boolean {
|
|
60
|
+
if (!isNode(node)) return false
|
|
61
|
+
return Array.isArray(node) ?
|
|
62
|
+
INDEX_RE.test(key) && Number(key) < node.length :
|
|
63
|
+
Object.prototype.hasOwnProperty.call(node, key)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
function hasKids(node: any): boolean {
|
|
68
|
+
if (!isNode(node)) return false
|
|
69
|
+
return 0 < (Array.isArray(node) ? node.length : Object.keys(node).length)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
function kidKeys(node: any): string[] {
|
|
74
|
+
return Array.isArray(node) ?
|
|
75
|
+
node.map((_: any, i: number) => String(i)) : Object.keys(node)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
// Parts join on '\0'. Escaping '\0' and '\x01' keeps keys injective and in part
|
|
80
|
+
// order, and an escape never puts 'R' after '\x01', so no key equals ROOT.
|
|
81
|
+
function nodeKey(label: string, over: string[]): string {
|
|
82
|
+
return [label, ...over]
|
|
83
|
+
.map(part => part.replace(/[\0\x01]/g, c => '\0' === c ? '\x01\x01' : '\x01\x02'))
|
|
84
|
+
.join('\0')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
// Occurrences of each reference label per use in the resolved spec, where a $ref
|
|
89
|
+
// chain resolves to its first label.
|
|
90
|
+
function countRefs(def: any): Record<string, number> {
|
|
91
|
+
const targets = new Map<string, any>()
|
|
92
|
+
const target = (label: string): any => {
|
|
93
|
+
if (targets.has(label)) return targets.get(label)
|
|
94
|
+
let t: any = undefined
|
|
95
|
+
if (label.startsWith('#/')) {
|
|
96
|
+
t = def
|
|
97
|
+
for (const raw of label.substring(2).split('/')) {
|
|
98
|
+
const part = raw.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
99
|
+
if (!hasKid(t, part)) {
|
|
100
|
+
t = undefined
|
|
101
|
+
break
|
|
102
|
+
}
|
|
103
|
+
t = decycledChild(t, part)
|
|
104
|
+
}
|
|
105
|
+
if (!isNode(t)) t = undefined
|
|
106
|
+
}
|
|
107
|
+
targets.set(label, t)
|
|
108
|
+
return t
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const same = (a: any, b: any): boolean => {
|
|
112
|
+
if (isNode(a) && a === b) return true
|
|
113
|
+
const label = refLabel(a)
|
|
114
|
+
return undefined !== label && label === refLabel(b)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const labelOf = new Map<string, string>()
|
|
118
|
+
const skipOf = new Map<string, Set<string>>()
|
|
119
|
+
const found: string[] = []
|
|
120
|
+
|
|
121
|
+
const scan = (start: any, skip: Set<string>): Body => {
|
|
122
|
+
const body: Body = new Map()
|
|
123
|
+
const expanded = new Set<any>()
|
|
124
|
+
const stack: any[] = []
|
|
125
|
+
for (const k of kidKeys(start)) {
|
|
126
|
+
if ('x-ref' !== k && '$ref' !== k && !skip.has(k)) {
|
|
127
|
+
stack.push(decycledChild(start, k))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
while (0 < stack.length) {
|
|
132
|
+
const v = stack.pop()
|
|
133
|
+
if (!isNode(v)) continue
|
|
134
|
+
|
|
135
|
+
const label = refLabel(v)
|
|
136
|
+
if (undefined === label) {
|
|
137
|
+
// Expanding a shared plain node once per scan is what ends a cycle of them.
|
|
138
|
+
if (expanded.has(v)) continue
|
|
139
|
+
expanded.add(v)
|
|
140
|
+
for (const k of kidKeys(v)) stack.push(decycledChild(v, k))
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const t = target(label)
|
|
145
|
+
const over: string[] = []
|
|
146
|
+
for (const k of Object.keys(v)) {
|
|
147
|
+
if ('x-ref' === k || '$ref' === k) continue
|
|
148
|
+
const vk = decycledChild(v, k)
|
|
149
|
+
if (hasKid(t, k)) {
|
|
150
|
+
const tk = decycledChild(t, k)
|
|
151
|
+
if (same(vk, tk)) continue
|
|
152
|
+
if (hasKids(tk)) over.push(k)
|
|
153
|
+
}
|
|
154
|
+
stack.push(vk)
|
|
155
|
+
}
|
|
156
|
+
over.sort(byCodePoint)
|
|
157
|
+
|
|
158
|
+
const node = nodeKey(label, over)
|
|
159
|
+
if (!labelOf.has(node)) {
|
|
160
|
+
labelOf.set(node, label)
|
|
161
|
+
skipOf.set(node, new Set(over))
|
|
162
|
+
found.push(node)
|
|
163
|
+
}
|
|
164
|
+
body.set(node, (body.get(node) ?? 0) + 1)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return body
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const bodies = new Map<string, Body>([[ROOT, scan(def, new Set())]])
|
|
171
|
+
for (let i = 0; i < found.length; i++) {
|
|
172
|
+
const node = found[i]
|
|
173
|
+
const t = target(labelOf.get(node) as string)
|
|
174
|
+
bodies.set(node, undefined === t ? new Map() : scan(t, skipOf.get(node) as Set<string>))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const count = countPaths(bodies)
|
|
178
|
+
|
|
179
|
+
const out: Record<string, number> = {}
|
|
180
|
+
for (const node of found) {
|
|
181
|
+
const label = labelOf.get(node) as string
|
|
182
|
+
out[label] = satAdd(out[label] ?? 0, count.get(node) ?? 0)
|
|
183
|
+
}
|
|
184
|
+
return out
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
function countPaths(bodies: Map<string, Body>): Map<string, number> {
|
|
189
|
+
// Code-point order, so both ports cut the same edge of a reference cycle.
|
|
190
|
+
const successors = (u: string) =>
|
|
191
|
+
[...(bodies.get(u) as Body).keys()].sort(byCodePoint)
|
|
192
|
+
|
|
193
|
+
const onStack = new Set<string>([ROOT])
|
|
194
|
+
const finished = new Set<string>()
|
|
195
|
+
const backEdges = new Map<string, Set<string>>()
|
|
196
|
+
const post: string[] = []
|
|
197
|
+
|
|
198
|
+
const stack = [{ node: ROOT, next: successors(ROOT), i: 0 }]
|
|
199
|
+
while (0 < stack.length) {
|
|
200
|
+
const frame = stack[stack.length - 1]
|
|
201
|
+
if (frame.i < frame.next.length) {
|
|
202
|
+
const v = frame.next[frame.i++]
|
|
203
|
+
if (onStack.has(v)) {
|
|
204
|
+
let cut = backEdges.get(frame.node)
|
|
205
|
+
if (undefined === cut) {
|
|
206
|
+
cut = new Set()
|
|
207
|
+
backEdges.set(frame.node, cut)
|
|
208
|
+
}
|
|
209
|
+
cut.add(v)
|
|
210
|
+
}
|
|
211
|
+
else if (!finished.has(v)) {
|
|
212
|
+
onStack.add(v)
|
|
213
|
+
stack.push({ node: v, next: successors(v), i: 0 })
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
stack.pop()
|
|
218
|
+
onStack.delete(frame.node)
|
|
219
|
+
finished.add(frame.node)
|
|
220
|
+
post.push(frame.node)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const count = new Map<string, number>([[ROOT, 1]])
|
|
225
|
+
for (let i = post.length - 1; 0 <= i; i--) {
|
|
226
|
+
const u = post[i]
|
|
227
|
+
const cu = count.get(u) ?? 0
|
|
228
|
+
const cut = backEdges.get(u)
|
|
229
|
+
for (const [v, w] of bodies.get(u) as Body) {
|
|
230
|
+
if (cut?.has(v)) continue
|
|
231
|
+
count.set(v, satAdd(count.get(v) ?? 0, satMul(cu, w)))
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return count
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
export {
|
|
239
|
+
REFCOUNT_CAP,
|
|
240
|
+
byCodePoint,
|
|
241
|
+
countRefs,
|
|
242
|
+
satAdd,
|
|
243
|
+
satMul,
|
|
244
|
+
}
|