@voxgig/apidef 8.29.0 → 8.30.1
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 +18 -0
- package/dist/builder/entity/entity.js +1 -1
- 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/guide/graphql01.js +6 -3
- package/dist/guide/graphql01.js.map +1 -1
- package/dist/guide/guide.js +2 -10
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.js +94 -27
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/parse.d.ts +2 -1
- package/dist/parse.js +65 -18
- package/dist/parse.js.map +1 -1
- package/dist/transform/entity.js +3 -3
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/field.d.ts +2 -1
- package/dist/transform/field.js +10 -1
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +13 -24
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/top.js +8 -0
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +9 -2
- package/dist/utility.js +161 -25
- package/dist/utility.js.map +1 -1
- package/dist/yamlalias.d.ts +3 -0
- package/dist/yamlalias.js +269 -0
- package/dist/yamlalias.js.map +1 -0
- package/package.json +1 -1
- package/src/builder/entity/entity.ts +1 -1
- package/src/builder/entity/info.ts +1 -1
- package/src/guide/graphql01.ts +9 -3
- package/src/guide/guide.ts +2 -10
- package/src/guide/heuristic01.ts +105 -28
- package/src/parse.ts +68 -26
- package/src/transform/entity.ts +5 -5
- package/src/transform/field.ts +12 -1
- package/src/transform/operation.ts +15 -27
- package/src/transform/top.ts +9 -0
- package/src/utility.ts +186 -25
- package/src/yamlalias.ts +295 -0
package/src/transform/top.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from '../utility'
|
|
9
9
|
|
|
10
10
|
import { METHODS } from '../resolved'
|
|
11
|
+
import { IGNORED_HEADERS_KEY } from '../parse'
|
|
11
12
|
|
|
12
13
|
import type { TransformResult } from '../transform'
|
|
13
14
|
|
|
@@ -134,6 +135,14 @@ const topTransform = async function(
|
|
|
134
135
|
}
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
for (const ignored of def[IGNORED_HEADERS_KEY] ?? []) {
|
|
139
|
+
ctx.warn?.({
|
|
140
|
+
note: 'header parameter `' + ignored.name + '` of ' +
|
|
141
|
+
(null == ignored.method ? '' : ignored.method + ' ') + ignored.path +
|
|
142
|
+
' ignored: OpenAPI ignores Accept, Content-Type and Authorization header parameters',
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
137
146
|
const summary = resolveSummary(def)
|
|
138
147
|
if (null != summary) {
|
|
139
148
|
kit.info.summary = summary
|
package/src/utility.ts
CHANGED
|
@@ -29,11 +29,6 @@ const sortedKeys = (obj: any): string[] => Object.keys(obj ?? {}).sort()
|
|
|
29
29
|
const sortedEntries = (obj: any): [string, any][] =>
|
|
30
30
|
Object.entries(obj ?? {}).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
|
|
31
31
|
|
|
32
|
-
// Pre-compiled regex patterns for formatJsonSrc to avoid recompilation per call.
|
|
33
|
-
const RE_JSON_KEY = /"([a-zA-Z_][a-zA-Z_0-9]*)": /g
|
|
34
|
-
const RE_JSON_TRAILING_BRACE = /},/g
|
|
35
|
-
const RE_JSON_COMMENT = /\n(\s*)([a-zA-Z_][a-zA-Z_0-9]*)_COMMENT:\s*"(.*)",/g
|
|
36
|
-
|
|
37
32
|
const RE_BARE_KEY = /^[A-Za-z_][_A-Za-z0-9]*$/
|
|
38
33
|
const isBareKey = (k: string) => RE_BARE_KEY.test(k)
|
|
39
34
|
const quoteKey = (k: string) => (isBareKey(k) ? k : JSON.stringify(k))
|
|
@@ -128,11 +123,38 @@ function loadFile(path: string, what: string, fs: FsUtil, log: Log) {
|
|
|
128
123
|
}
|
|
129
124
|
|
|
130
125
|
|
|
126
|
+
// Unquotes bare keys and breaks the line after `},`, only outside strings, so
|
|
127
|
+
// no string value is rewritten.
|
|
131
128
|
function formatJsonSrc(jsonsrc: string) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
let out = ''
|
|
130
|
+
let i = 0
|
|
131
|
+
while (i < jsonsrc.length) {
|
|
132
|
+
const ch = jsonsrc[i]
|
|
133
|
+
if ('"' === ch) {
|
|
134
|
+
let end = i + 1
|
|
135
|
+
while (end < jsonsrc.length && '"' !== jsonsrc[end]) {
|
|
136
|
+
end += '\\' === jsonsrc[end] ? 2 : 1
|
|
137
|
+
}
|
|
138
|
+
const body = jsonsrc.substring(i + 1, end)
|
|
139
|
+
if (': ' === jsonsrc.substring(end + 1, end + 3) && isBareKey(body)) {
|
|
140
|
+
out += body + ': '
|
|
141
|
+
i = end + 3
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
out += jsonsrc.substring(i, end + 1)
|
|
145
|
+
i = end + 1
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if ('}' === ch && ',' === jsonsrc[i + 1]) {
|
|
149
|
+
out += '}\n'
|
|
150
|
+
i += 2
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
out += ch
|
|
154
|
+
i++
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return out
|
|
136
158
|
}
|
|
137
159
|
|
|
138
160
|
|
|
@@ -722,6 +744,7 @@ function formatJSONIC(
|
|
|
722
744
|
color?: boolean
|
|
723
745
|
maxlines?: number,
|
|
724
746
|
exclude?: string[],
|
|
747
|
+
comments?: boolean,
|
|
725
748
|
}): string {
|
|
726
749
|
|
|
727
750
|
if (undefined === val) return ''
|
|
@@ -731,6 +754,7 @@ function formatJSONIC(
|
|
|
731
754
|
const useColor = opts?.color ?? false
|
|
732
755
|
const maxlines = opts?.maxlines ?? Number.MAX_VALUE
|
|
733
756
|
const exclude = opts?.exclude ?? []
|
|
757
|
+
const comments = opts?.comments ?? true
|
|
734
758
|
|
|
735
759
|
// ANSI color codes
|
|
736
760
|
const colors = {
|
|
@@ -751,13 +775,8 @@ function formatJSONIC(
|
|
|
751
775
|
if (v === null) return c('null', 'null')
|
|
752
776
|
const t = typeof v
|
|
753
777
|
switch (t) {
|
|
754
|
-
case 'string': return c('string',
|
|
755
|
-
'
|
|
756
|
-
.substring(1)
|
|
757
|
-
.replace(/\\n/g, '\n')
|
|
758
|
-
.replace(/\\"/g, '"')
|
|
759
|
-
.replace(/`/g, '\\`')
|
|
760
|
-
.replace(/"$/, '`'))
|
|
778
|
+
case 'string': return c('string',
|
|
779
|
+
!v.includes('\n') ? JSON.stringify(v) : backtickString(v))
|
|
761
780
|
case 'number': return c('number', Number.isFinite(v) ? String(v) : 'null')
|
|
762
781
|
case 'boolean': return c('boolean', v ? 'true' : 'false')
|
|
763
782
|
case 'bigint': return c('string', JSON.stringify(v.toString()))
|
|
@@ -769,15 +788,44 @@ function formatJSONIC(
|
|
|
769
788
|
}
|
|
770
789
|
}
|
|
771
790
|
|
|
772
|
-
const
|
|
791
|
+
const commentText = (c: any): string | null => {
|
|
773
792
|
if (c == null) return null
|
|
774
793
|
if (Array.isArray(c) && c.every(x => typeof x === 'string')) return c.join('; ')
|
|
775
794
|
if (typeof c === 'string') return c
|
|
776
795
|
try { return JSON.stringify(c) } catch { return String(c) }
|
|
777
796
|
}
|
|
778
797
|
|
|
798
|
+
// A comment runs to the end of its line, so a line break in it would start
|
|
799
|
+
// source.
|
|
800
|
+
const renderComment = (c: any): string | null => {
|
|
801
|
+
const text = comments ? commentText(c) : null
|
|
802
|
+
return null == text ? null : text.replace(RE_LINE_BREAKS, ' ')
|
|
803
|
+
}
|
|
804
|
+
|
|
779
805
|
return renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c,
|
|
780
|
-
renderPrimitive, renderComment, false)
|
|
806
|
+
renderPrimitive, renderComment, comments, false)
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
const RE_LINE_BREAKS = /[\r\n\u2028\u2029]+/g
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
// Its JSON escapes, read in order so an escaped backslash never starts
|
|
814
|
+
// another, with a newline and a quote written as themselves.
|
|
815
|
+
function backtickString(s: string): string {
|
|
816
|
+
const json = JSON.stringify(s)
|
|
817
|
+
let out = '`'
|
|
818
|
+
for (let i = 1; i < json.length - 1; i++) {
|
|
819
|
+
const ch = json[i]
|
|
820
|
+
if ('\\' === ch) {
|
|
821
|
+
const esc = json[++i]
|
|
822
|
+
out += 'n' === esc ? '\n' : '"' === esc ? '"' : '\\' + esc
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
out += '`' === ch ? '\\`' : ch
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return out + '`'
|
|
781
829
|
}
|
|
782
830
|
|
|
783
831
|
|
|
@@ -791,6 +839,7 @@ function renderJSONIC(
|
|
|
791
839
|
c: (color: any, text: string) => string,
|
|
792
840
|
renderPrimitive: (v: any) => string,
|
|
793
841
|
renderComment: (c: any) => string | null,
|
|
842
|
+
comments: boolean,
|
|
794
843
|
decircularized: boolean,
|
|
795
844
|
): string {
|
|
796
845
|
|
|
@@ -874,7 +923,7 @@ function renderJSONIC(
|
|
|
874
923
|
}
|
|
875
924
|
if (undefined !== flat) {
|
|
876
925
|
return renderJSONIC(flat, hsepd, showd, useColor, maxlines, exclude, c,
|
|
877
|
-
renderPrimitive, renderComment, true)
|
|
926
|
+
renderPrimitive, renderComment, comments, true)
|
|
878
927
|
}
|
|
879
928
|
}
|
|
880
929
|
// decircular did not resolve it, so render a marker rather than
|
|
@@ -919,7 +968,7 @@ function renderJSONIC(
|
|
|
919
968
|
keys.unshift('name', 'message', 'stack')
|
|
920
969
|
}
|
|
921
970
|
|
|
922
|
-
const printableKeys = keys.filter(k => !k.endsWith('_COMMENT') &&
|
|
971
|
+
const printableKeys = keys.filter(k => !(comments && k.endsWith('_COMMENT')) &&
|
|
923
972
|
(showd || !k.endsWith('$')))
|
|
924
973
|
|
|
925
974
|
if (printableKeys.length === 0) {
|
|
@@ -1130,6 +1179,49 @@ function foldName(name: string): string {
|
|
|
1130
1179
|
const FIRST_LETTER_RE = /[a-zA-Z]/
|
|
1131
1180
|
|
|
1132
1181
|
|
|
1182
|
+
// Formats a route segment may name beside its resource, or alone.
|
|
1183
|
+
const FORMAT_EXTENSIONS = new Set([
|
|
1184
|
+
'json', 'xml', 'csv', 'tsv', 'yaml', 'yml', 'txt', 'html', 'htm', 'rss',
|
|
1185
|
+
'atom', 'jsonl', 'ndjson', 'geojson',
|
|
1186
|
+
])
|
|
1187
|
+
|
|
1188
|
+
// A route segment as the guide reads it: {id}.json is {id}, repos.json and
|
|
1189
|
+
// data.{format} are repos and data, and a format alone (json) names nothing.
|
|
1190
|
+
function guideSegment(seg: string): string | null {
|
|
1191
|
+
const held = seg.match(/^(\{[^}]+\})\.(?:[A-Za-z0-9]+|\{[^}]+\})$/)
|
|
1192
|
+
if (null != held) return held[1]
|
|
1193
|
+
const named = seg.match(/^([^{}.]+)\.([A-Za-z0-9]+|\{[^}]+\})$/)
|
|
1194
|
+
if (null != named && ('{' === named[2][0] || FORMAT_EXTENSIONS.has(named[2].toLowerCase()))) {
|
|
1195
|
+
return named[1]
|
|
1196
|
+
}
|
|
1197
|
+
return FORMAT_EXTENSIONS.has(seg.toLowerCase()) ? null : seg
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// The guide's parts of a route; a route of formats alone keeps them.
|
|
1201
|
+
function guideParts(path: string): string[] {
|
|
1202
|
+
const parts = path.split('/').filter((p: string) => '' !== p)
|
|
1203
|
+
const kept = parts.map(guideSegment).filter((p): p is string => null != p)
|
|
1204
|
+
return 0 === kept.length ? parts : kept
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
function guidePath(path: string): string {
|
|
1208
|
+
return '/' + guideParts(path).join('/')
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
// aontu cannot unify a spread with a key Object.prototype owns, so no entity
|
|
1213
|
+
// is given one of these names. Listed, not read, so both ports agree.
|
|
1214
|
+
const PROTOTYPE_NAMES = new Set([
|
|
1215
|
+
'constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty',
|
|
1216
|
+
'__lookupGetter__', '__lookupSetter__', 'isPrototypeOf',
|
|
1217
|
+
'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString',
|
|
1218
|
+
])
|
|
1219
|
+
|
|
1220
|
+
function guardPrototypeName(s: string): string {
|
|
1221
|
+
return PROTOTYPE_NAMES.has(s) ? s + '_item' : s
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
|
|
1133
1225
|
function prefixLeadingDigit(s: string): string {
|
|
1134
1226
|
if (null == s || '' === s) return s
|
|
1135
1227
|
const first = s.charCodeAt(0)
|
|
@@ -1262,7 +1354,7 @@ function ensureMinEntityName(
|
|
|
1262
1354
|
padded = truncated || parts[0].substring(0, MAX_ENTITY_NAME_LEN)
|
|
1263
1355
|
}
|
|
1264
1356
|
|
|
1265
|
-
padded = prefixLeadingDigit(padded)
|
|
1357
|
+
padded = guardPrototypeName(prefixLeadingDigit(padded))
|
|
1266
1358
|
if (padded.length < MIN_ENTITY_NAME_LEN) {
|
|
1267
1359
|
const padding = 'nt'.substring(0, MIN_ENTITY_NAME_LEN - padded.length)
|
|
1268
1360
|
padded = padded + padding
|
|
@@ -1289,15 +1381,33 @@ function ensureMinEntityName(
|
|
|
1289
1381
|
// Unconditional suffixes: framework noise, always stripped.
|
|
1290
1382
|
const CMP_SUFFIXES = ['_rest_controller', '_controller', '_response', '_request']
|
|
1291
1383
|
|
|
1292
|
-
|
|
1384
|
+
// Longest first: the first suffix a name ends with is the only one tried.
|
|
1385
|
+
const CMP_GUARDED_SUFFIXES = [
|
|
1386
|
+
'_create_response', '_update_response', '_page_response',
|
|
1387
|
+
'_response_collection', '_response_single', '_response_dto',
|
|
1388
|
+
'_collection', '_single', '_dto', '_page',
|
|
1389
|
+
]
|
|
1293
1390
|
|
|
1294
1391
|
const CMP_RESULT_VERBS = [
|
|
1295
1392
|
'list', 'create', 'show', 'update', 'delete', 'remove',
|
|
1296
|
-
'get', 'edit', 'resolve', 'rotate', 'search',
|
|
1393
|
+
'get', 'edit', 'resolve', 'rotate', 'search', 'add',
|
|
1297
1394
|
]
|
|
1298
1395
|
|
|
1299
1396
|
const CMP_PREFIXES = ['get_', 'post_', 'put_', 'delete_', 'patch_']
|
|
1300
1397
|
|
|
1398
|
+
const OPERATION_VERBS = [
|
|
1399
|
+
...CMP_RESULT_VERBS, 'post', 'put', 'patch', 'fetch', 'find', 'retrieve', 'read', 'add',
|
|
1400
|
+
]
|
|
1401
|
+
|
|
1402
|
+
// An operationId leads with its action, so get_service_status names
|
|
1403
|
+
// service_status. The verb stays when what follows is too short to name one.
|
|
1404
|
+
function stripOperationVerb(opid: string): string {
|
|
1405
|
+
const cut = opid.indexOf('_')
|
|
1406
|
+
if (cut < 0) return opid
|
|
1407
|
+
const rest = opid.substring(cut + 1)
|
|
1408
|
+
return OPERATION_VERBS.includes(opid.substring(0, cut)) && 3 <= rest.length ? rest : opid
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1301
1411
|
// A guide node (entity, path or op) is active unless guide.aontu explicitly
|
|
1302
1412
|
// says otherwise. `active` has always been declared in the guide model and
|
|
1303
1413
|
// documented as the escape hatch for a misclassified entity; this is the
|
|
@@ -1451,7 +1561,7 @@ function cleanComponentName(
|
|
|
1451
1561
|
// (CallRouteAllowedCaller), so the namespace is tried singular too.
|
|
1452
1562
|
const cands = after
|
|
1453
1563
|
? [canonize(depluralize(after)), canonize(depluralize(before + '_' + after)),
|
|
1454
|
-
canonize(depluralize(before) + '_' + depluralize(after))]
|
|
1564
|
+
canonize(depluralize(before) + '_' + depluralize(after)), canonize(depluralize(before))]
|
|
1455
1565
|
: [canonize(depluralize(before))]
|
|
1456
1566
|
for (const cand of cands) {
|
|
1457
1567
|
if (cand.length >= 3 && knownWithVersion(cand, version)) {
|
|
@@ -1487,6 +1597,15 @@ function cleanComponentName(
|
|
|
1487
1597
|
}
|
|
1488
1598
|
}
|
|
1489
1599
|
|
|
1600
|
+
// A list wrapper names its records: list_asset is asset, when asset is known.
|
|
1601
|
+
if (null != isKnownCmp && cleaned.startsWith('list_')) {
|
|
1602
|
+
const remainder = canonize(cleaned.substring('list_'.length))
|
|
1603
|
+
if (remainder.length >= 3 && knownWithVersion(remainder, version)) {
|
|
1604
|
+
cleaned = remainder
|
|
1605
|
+
stripped = true
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1490
1609
|
// Nothing stripped it: restore the version suffix removed above.
|
|
1491
1610
|
if (!stripped && cleaned === preVersion.replace(/_v\d+$/, '') && null != version) {
|
|
1492
1611
|
cleaned = preVersion
|
|
@@ -1815,7 +1934,43 @@ function itemEnvelopeOf(res: any): string | null {
|
|
|
1815
1934
|
}
|
|
1816
1935
|
|
|
1817
1936
|
|
|
1818
|
-
|
|
1937
|
+
// A list over an object reads its records, never the object: its one envelope,
|
|
1938
|
+
// else its one list of records beside other data, else the first of several.
|
|
1939
|
+
function envelopeProp(resprops: any, opname: string, example?: any): string | null {
|
|
1940
|
+
const key = singleEnvelope(resprops, opname)
|
|
1941
|
+
return null == key && 'list' === opname ? listedRecords(resprops, example) : key
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
function recordLists(resprops: any): string[] {
|
|
1946
|
+
return keysof(resprops).filter((k: string) => isRecordList(resprops[k]))
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
|
|
1950
|
+
// Lists of one item schema are categories of one record: the one the example
|
|
1951
|
+
// fills is read, as the example is counted. Otherwise the first by name is,
|
|
1952
|
+
// past one holding errors or messages.
|
|
1953
|
+
function listedRecords(resprops: any, example?: any): string | null {
|
|
1954
|
+
const lists = recordLists(resprops)
|
|
1955
|
+
if (lists.length < 2) {
|
|
1956
|
+
return lists[0] ?? null
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
const refs = new Set(lists.map((k: string) => resprops[k]?.items?.['x-ref']))
|
|
1960
|
+
if (1 === refs.size && null != [...refs][0]) {
|
|
1961
|
+
const filled = lists.filter((k: string) =>
|
|
1962
|
+
Array.isArray(example?.[k]) && 0 < example[k].length)
|
|
1963
|
+
if (1 === filled.length) {
|
|
1964
|
+
return filled[0]
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
const status = (k: string) => isEnvelopeStatusProp(k) || isEnvelopeStatusProp(k.replace(/s$/i, ''))
|
|
1969
|
+
return lists.find((k: string) => !isPageMetaProp(k) && !status(k)) ?? lists[0]
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
function singleEnvelope(resprops: any, opname: string): string | null {
|
|
1819
1974
|
const keys = keysof(resprops)
|
|
1820
1975
|
if (0 === keys.length) {
|
|
1821
1976
|
return null
|
|
@@ -2318,6 +2473,10 @@ export {
|
|
|
2318
2473
|
humanTitle,
|
|
2319
2474
|
foldName,
|
|
2320
2475
|
prefixLeadingDigit,
|
|
2476
|
+
guardPrototypeName,
|
|
2477
|
+
guideParts,
|
|
2478
|
+
guidePath,
|
|
2479
|
+
stripOperationVerb,
|
|
2321
2480
|
debugpath,
|
|
2322
2481
|
debugpathOn,
|
|
2323
2482
|
findPathsWithPrefix,
|
|
@@ -2334,6 +2493,8 @@ export {
|
|
|
2334
2493
|
itemEnvelopeTransform,
|
|
2335
2494
|
itemEnvelopeOf,
|
|
2336
2495
|
envelopeProp,
|
|
2496
|
+
recordLists,
|
|
2497
|
+
singleEnvelope,
|
|
2337
2498
|
envelopeItemRef,
|
|
2338
2499
|
composedEnvelopeProp,
|
|
2339
2500
|
pageRecords,
|
package/src/yamlalias.ts
ADDED
|
@@ -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
|
+
}
|