@libo11y/spec 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +84 -2
- package/dist/appregistry.d.ts +61 -0
- package/dist/appregistry.js +152 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +95 -0
- package/dist/generated/goprint.d.ts +5 -0
- package/dist/generated/goprint.js +108 -0
- package/dist/generated/spec.d.ts +218 -0
- package/dist/generated/spec.js +191 -0
- package/dist/generated/tables.d.ts +41 -0
- package/dist/generated/tables.js +176 -0
- package/dist/goquote.d.ts +2 -0
- package/dist/goquote.js +59 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/yaml.d.ts +19 -0
- package/dist/yaml.js +491 -0
- package/dist/yamlv3.d.ts +36 -0
- package/dist/yamlv3.js +335 -0
- package/package.json +36 -4
package/dist/yaml.js
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses an application registry file. This is the only `@libo11y/spec` module that depends on a YAML
|
|
3
|
+
* parser, so `@libo11y/spec` itself stays dependency-free for browsers.
|
|
4
|
+
*
|
|
5
|
+
* Decoding follows Go's appregistry.Parse, which is yaml.v3 with KnownFields:
|
|
6
|
+
* - Scalars read as strings. A plain scalar reads as its text, so `1.0` stays `1.0`, and a null scalar
|
|
7
|
+
* (`~`, `null`, empty) leaves its field empty. A null entry in `values` is dropped, and a null key is
|
|
8
|
+
* skipped.
|
|
9
|
+
* - An unknown key, a duplicate key, or a value of the wrong YAML type is an error. When a document has
|
|
10
|
+
* several of these, a duplicate key outranks an unknown key, which outranks a type error.
|
|
11
|
+
* - Problems in the first document outrank a second document, and a second document is an error even
|
|
12
|
+
* when it is empty, as a trailing `---` makes it.
|
|
13
|
+
* - What yaml.v3's reader, scanner and parser reject is `syntax`: a control character, an escape YAML 1.2
|
|
14
|
+
* added such as `\/`, a tag that does not fit its value, an unknown anchor, a `%YAML` version other
|
|
15
|
+
* than 1.1, and a document that aliases its way into excessive work. Like yaml.v3, it accepts the
|
|
16
|
+
* `\'` escape and reads `!!binary` as base64.
|
|
17
|
+
*/
|
|
18
|
+
import { isAlias, isMap, isPair, isScalar, isSeq, Parser, parseAllDocuments, Scalar, } from "yaml";
|
|
19
|
+
import { checkCharacters, decodeInput, resolveScalar, scanEscapes, shortTag, YamlSyntaxError, } from "./yamlv3.js";
|
|
20
|
+
const MESSAGES = {
|
|
21
|
+
empty: "the file holds no YAML document",
|
|
22
|
+
"second-document": "an application registry holds one YAML document, found a second",
|
|
23
|
+
"duplicate-key": "a mapping repeats a key",
|
|
24
|
+
"unknown-key": "a key the schema does not define",
|
|
25
|
+
type: "a value of the wrong YAML type",
|
|
26
|
+
syntax: "the file is not valid YAML",
|
|
27
|
+
};
|
|
28
|
+
/** Thrown by parseAppRegistry for a file that does not decode. */
|
|
29
|
+
export class AppRegistryParseError extends Error {
|
|
30
|
+
kind;
|
|
31
|
+
constructor(kind, detail) {
|
|
32
|
+
super(`parsing application registry: ${kind}: ${detail ?? MESSAGES[kind]}`);
|
|
33
|
+
this.name = "AppRegistryParseError";
|
|
34
|
+
this.kind = kind;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const OPTIONS = { uniqueKeys: false, keepSourceTokens: true };
|
|
38
|
+
const MERGE_TAGS = new Set(["", "!", "!!merge"]);
|
|
39
|
+
// yaml.v3's alias budget (decode.go allowedAliasRatio): past 1000 decodes and 100 alias-driven ones, the
|
|
40
|
+
// share of decodes driven by alias expansion may not exceed a ratio that falls from 0.99 to 0.10 as the
|
|
41
|
+
// document grows, so a small file cannot expand into an exponential amount of work.
|
|
42
|
+
const ALIAS_RATIO_LOW = 400_000;
|
|
43
|
+
const ALIAS_RATIO_HIGH = 4_000_000;
|
|
44
|
+
function allowedAliasRatio(decodes) {
|
|
45
|
+
if (decodes <= ALIAS_RATIO_LOW)
|
|
46
|
+
return 0.99;
|
|
47
|
+
if (decodes >= ALIAS_RATIO_HIGH)
|
|
48
|
+
return 0.1;
|
|
49
|
+
return 0.99 - 0.89 * ((decodes - ALIAS_RATIO_LOW) / (ALIAS_RATIO_HIGH - ALIAS_RATIO_LOW));
|
|
50
|
+
}
|
|
51
|
+
// isEmpty reports a document body with nothing in it: no value, tag or anchor.
|
|
52
|
+
function isEmpty(node) {
|
|
53
|
+
return (node === null ||
|
|
54
|
+
node === undefined ||
|
|
55
|
+
(isScalar(node) &&
|
|
56
|
+
node.value === null &&
|
|
57
|
+
node.source === "" &&
|
|
58
|
+
node.tag === undefined &&
|
|
59
|
+
node.anchor === undefined));
|
|
60
|
+
}
|
|
61
|
+
// walk calls fn for every node under root in document order, keys before their values. It keeps its own
|
|
62
|
+
// stack: the `yaml` library's visit copies the path at every level, which is quadratic in the nesting.
|
|
63
|
+
function walk(root, fn) {
|
|
64
|
+
const stack = [root];
|
|
65
|
+
while (stack.length > 0) {
|
|
66
|
+
const node = stack.pop();
|
|
67
|
+
if (node === null || node === undefined)
|
|
68
|
+
continue;
|
|
69
|
+
if (isPair(node)) {
|
|
70
|
+
stack.push(node.value, node.key);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
fn(node);
|
|
74
|
+
if (isMap(node) || isSeq(node)) {
|
|
75
|
+
for (let i = node.items.length - 1; i >= 0; i--)
|
|
76
|
+
stack.push(node.items[i]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// isPhantom reports the empty document the `yaml` library makes of an extra `...` end marker, which
|
|
81
|
+
// yaml.v3 skips.
|
|
82
|
+
function isPhantom(doc) {
|
|
83
|
+
return doc.directives.docStart !== true && doc.errors.length === 0 && isEmpty(doc.contents);
|
|
84
|
+
}
|
|
85
|
+
// versionDirectives returns, for each document, the versions its %YAML directives name. They come from
|
|
86
|
+
// the CST because the `yaml` library keeps only the last directive and accepts any version.
|
|
87
|
+
function versionDirectives(text, docs) {
|
|
88
|
+
const out = new Map(docs.map((doc) => [doc, []]));
|
|
89
|
+
for (const token of new Parser().parse(text)) {
|
|
90
|
+
if (token.type !== "directive" || !token.source.startsWith("%YAML"))
|
|
91
|
+
continue;
|
|
92
|
+
const doc = docs.find((d) => d.range[0] >= token.offset);
|
|
93
|
+
out.get(doc ?? docs[docs.length - 1])?.push(token.source.slice(5).trim());
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
// documentAliases checks one document for the problems yaml.v3's scanner and parser report, which Go
|
|
98
|
+
// names `syntax` wherever they occur, and returns what each alias refers to. first says whether this is
|
|
99
|
+
// the stream's first document, which yaml.v3 reads without a `---` marker.
|
|
100
|
+
function documentAliases(doc, first, versions) {
|
|
101
|
+
const [error] = doc.errors;
|
|
102
|
+
if (error !== undefined)
|
|
103
|
+
throw new YamlSyntaxError(error.message);
|
|
104
|
+
// yaml.v3 reads YAML 1.1 only.
|
|
105
|
+
if (versions.length > 1)
|
|
106
|
+
throw new YamlSyntaxError("found duplicate %YAML directive");
|
|
107
|
+
const [version] = versions;
|
|
108
|
+
if (version !== undefined && !/^0*1\.0*1(?:\s|#|$)/.test(version)) {
|
|
109
|
+
throw new YamlSyntaxError("found incompatible YAML document");
|
|
110
|
+
}
|
|
111
|
+
if (doc.directives.docStart !== true && (!first || isEmpty(doc.contents))) {
|
|
112
|
+
throw new YamlSyntaxError(first ? "did not find expected node content" : "did not find expected <document start>");
|
|
113
|
+
}
|
|
114
|
+
// yaml.v3 registers an anchor when its node starts, so an alias can only refer back, and an alias
|
|
115
|
+
// inside its own anchored node refers to that node.
|
|
116
|
+
const anchors = new Map();
|
|
117
|
+
const targets = new Map();
|
|
118
|
+
walk(doc.contents, (node) => {
|
|
119
|
+
if (isAlias(node)) {
|
|
120
|
+
if (!anchors.has(node.source))
|
|
121
|
+
throw new YamlSyntaxError(`unknown anchor '${node.source}' referenced`);
|
|
122
|
+
targets.set(node, anchors.get(node.source));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!isScalar(node) && !isMap(node) && !isSeq(node))
|
|
126
|
+
return;
|
|
127
|
+
if (node.anchor !== undefined)
|
|
128
|
+
anchors.set(node.anchor, node);
|
|
129
|
+
if (isScalar(node) && node.type === Scalar.QUOTE_DOUBLE) {
|
|
130
|
+
const token = node.srcToken;
|
|
131
|
+
if (token !== undefined && "source" in token)
|
|
132
|
+
scanEscapes(token.source);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
return targets;
|
|
136
|
+
}
|
|
137
|
+
// withoutQuoteEscapes drops the backslash of every `\'` escape in a double-quoted scalar. yaml.v3 reads
|
|
138
|
+
// `\'` as a quote, the `yaml` library rejects it, and a bare quote means the same inside double quotes.
|
|
139
|
+
function withoutQuoteEscapes(text, docs) {
|
|
140
|
+
const offsets = [];
|
|
141
|
+
// Only a document the `yaml` library rejected for an escape can hold one. Skipping the rest also keeps
|
|
142
|
+
// a document it rejected for nesting too deeply from being walked.
|
|
143
|
+
for (const doc of docs.filter((d) => d.errors.some((e) => e.code === "BAD_DQ_ESCAPE"))) {
|
|
144
|
+
walk(doc.contents, (node) => {
|
|
145
|
+
if (!isScalar(node) || node.type !== Scalar.QUOTE_DOUBLE)
|
|
146
|
+
return;
|
|
147
|
+
const token = node.srcToken;
|
|
148
|
+
if (token === undefined || !("source" in token))
|
|
149
|
+
return;
|
|
150
|
+
try {
|
|
151
|
+
for (const i of scanEscapes(token.source))
|
|
152
|
+
offsets.push(token.offset + i);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// An escape yaml.v3 rejects is reported when the document is checked.
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (offsets.length === 0)
|
|
160
|
+
return text;
|
|
161
|
+
offsets.sort((a, b) => a - b);
|
|
162
|
+
let out = "";
|
|
163
|
+
let from = 0;
|
|
164
|
+
for (const offset of offsets) {
|
|
165
|
+
out += text.slice(from, offset);
|
|
166
|
+
from = offset + 1;
|
|
167
|
+
}
|
|
168
|
+
return out + text.slice(from);
|
|
169
|
+
}
|
|
170
|
+
// Decoder walks one document the way yaml.v3 decodes it into Go's Registry struct. Like yaml.v3, it
|
|
171
|
+
// collects problems and keeps going instead of stopping at the first. Problems yaml.v3 treats as fatal
|
|
172
|
+
// throw YamlSyntaxError.
|
|
173
|
+
class Decoder {
|
|
174
|
+
#targets;
|
|
175
|
+
#expanding = new Set();
|
|
176
|
+
#decodes = 0;
|
|
177
|
+
#aliasDecodes = 0;
|
|
178
|
+
#aliasDepth = 0;
|
|
179
|
+
duplicate = false;
|
|
180
|
+
unknown = false;
|
|
181
|
+
wrongType = false;
|
|
182
|
+
constructor(targets) {
|
|
183
|
+
this.#targets = targets;
|
|
184
|
+
}
|
|
185
|
+
// count is the bookkeeping at the top of yaml.v3's decoder.unmarshal, once per node decoded.
|
|
186
|
+
count() {
|
|
187
|
+
this.#decodes++;
|
|
188
|
+
if (this.#aliasDepth > 0)
|
|
189
|
+
this.#aliasDecodes++;
|
|
190
|
+
if (this.#aliasDecodes > 100 &&
|
|
191
|
+
this.#decodes > 1000 &&
|
|
192
|
+
this.#aliasDecodes / this.#decodes > allowedAliasRatio(this.#decodes)) {
|
|
193
|
+
throw new YamlSyntaxError("document contains excessive aliasing");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// decode counts raw as one decoded node and passes it to fn, following an alias to its node first.
|
|
197
|
+
#decode(raw, fn) {
|
|
198
|
+
this.count();
|
|
199
|
+
if (!isAlias(raw))
|
|
200
|
+
return fn(raw);
|
|
201
|
+
if (this.#expanding.has(raw))
|
|
202
|
+
throw new YamlSyntaxError(`anchor '${raw.source}' value contains itself`);
|
|
203
|
+
this.#expanding.add(raw);
|
|
204
|
+
this.#aliasDepth++;
|
|
205
|
+
try {
|
|
206
|
+
return this.#decode(this.#targets.get(raw), fn);
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
this.#aliasDepth--;
|
|
210
|
+
this.#expanding.delete(raw);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
#scalar(node) {
|
|
214
|
+
return resolveScalar(node.tag, node.source ?? String(node.value), node.type !== Scalar.PLAIN);
|
|
215
|
+
}
|
|
216
|
+
// isNull reports a node that decodes to null, which leaves a Go map, slice or struct at its zero value.
|
|
217
|
+
// Any other scalar is a type error there.
|
|
218
|
+
#nullOrWrongScalar(node) {
|
|
219
|
+
if (node === null || node === undefined)
|
|
220
|
+
return true;
|
|
221
|
+
if (!isScalar(node))
|
|
222
|
+
return false;
|
|
223
|
+
if (!this.#scalar(node).isNull)
|
|
224
|
+
this.wrongType = true;
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
// identity is what yaml.v3 compares to find a duplicate key: the node kind and its value.
|
|
228
|
+
#identity(node) {
|
|
229
|
+
if (isScalar(node))
|
|
230
|
+
return `scalar:${node.source ?? ""}`;
|
|
231
|
+
if (isAlias(node))
|
|
232
|
+
return `alias:${node.source}`;
|
|
233
|
+
return isMap(node) ? "map:" : "seq:";
|
|
234
|
+
}
|
|
235
|
+
#isMergeKey(node) {
|
|
236
|
+
return (isScalar(node) &&
|
|
237
|
+
node.type === Scalar.PLAIN &&
|
|
238
|
+
node.source === "<<" &&
|
|
239
|
+
MERGE_TAGS.has(shortTag(node.tag)));
|
|
240
|
+
}
|
|
241
|
+
// hasDuplicates checks a mapping before it is decoded, as yaml.v3 does for every mapping it decodes.
|
|
242
|
+
#hasDuplicates(map) {
|
|
243
|
+
const seen = new Set();
|
|
244
|
+
for (const pair of map.items) {
|
|
245
|
+
const id = this.#identity(pair.key);
|
|
246
|
+
if (seen.has(id)) {
|
|
247
|
+
this.duplicate = true;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
seen.add(id);
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
// string decodes a node into a Go string: undefined for null, which leaves the field unset.
|
|
255
|
+
string(raw) {
|
|
256
|
+
return this.#decode(raw, (node) => {
|
|
257
|
+
if (node === null || node === undefined)
|
|
258
|
+
return undefined;
|
|
259
|
+
if (isScalar(node)) {
|
|
260
|
+
const v = this.#scalar(node);
|
|
261
|
+
return v.isNull ? undefined : v.text;
|
|
262
|
+
}
|
|
263
|
+
if (!(isMap(node) && this.#hasDuplicates(node)))
|
|
264
|
+
this.wrongType = true;
|
|
265
|
+
return undefined;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
// strings decodes a node into a Go []string. Null entries are dropped.
|
|
269
|
+
strings(raw) {
|
|
270
|
+
return this.#decode(raw, (node) => {
|
|
271
|
+
if (this.#nullOrWrongScalar(node))
|
|
272
|
+
return undefined;
|
|
273
|
+
if (!isSeq(node)) {
|
|
274
|
+
if (!(isMap(node) && this.#hasDuplicates(node)))
|
|
275
|
+
this.wrongType = true;
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
const out = [];
|
|
279
|
+
for (const item of node.items) {
|
|
280
|
+
const s = this.string(item);
|
|
281
|
+
if (s !== undefined)
|
|
282
|
+
out.push(s);
|
|
283
|
+
}
|
|
284
|
+
return out;
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
// mapping decodes a mapping key by key into a Go struct or map, calling onKey for each key. Keys
|
|
288
|
+
// already in merged, the keys of the mapping that merges this one, are skipped. A merge key (<<) is
|
|
289
|
+
// applied after the mapping's own keys, so they win, and the first merged mapping wins over later ones.
|
|
290
|
+
mapping(raw, onKey, merged) {
|
|
291
|
+
this.#decode(raw, (node) => {
|
|
292
|
+
if (this.#nullOrWrongScalar(node))
|
|
293
|
+
return;
|
|
294
|
+
if (!isMap(node)) {
|
|
295
|
+
this.wrongType = true;
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (this.#hasDuplicates(node))
|
|
299
|
+
return;
|
|
300
|
+
let mergeNode;
|
|
301
|
+
let hasMerge = false;
|
|
302
|
+
for (const pair of node.items) {
|
|
303
|
+
if (this.#isMergeKey(pair.key)) {
|
|
304
|
+
mergeNode = pair.value;
|
|
305
|
+
hasMerge = true;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const name = this.string(pair.key);
|
|
309
|
+
if (name === undefined)
|
|
310
|
+
continue;
|
|
311
|
+
if (merged !== undefined) {
|
|
312
|
+
if (merged.has(name))
|
|
313
|
+
continue;
|
|
314
|
+
merged.add(name);
|
|
315
|
+
}
|
|
316
|
+
onKey(name, pair.value);
|
|
317
|
+
}
|
|
318
|
+
if (hasMerge)
|
|
319
|
+
this.#merge(node, mergeNode, onKey, merged);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
// merge is yaml.v3's decoder.merge. The first merge at a level collects the merging mapping's keys,
|
|
323
|
+
// decoded as a Go interface{} would hold them, so only string keys shadow merged ones.
|
|
324
|
+
#merge(parent, source, onKey, merged) {
|
|
325
|
+
const keys = merged ?? new Set();
|
|
326
|
+
if (merged === undefined) {
|
|
327
|
+
for (const pair of parent.items) {
|
|
328
|
+
const key = this.#decode(pair.key, (node) => {
|
|
329
|
+
if (!isScalar(node))
|
|
330
|
+
return undefined;
|
|
331
|
+
const v = this.#scalar(node);
|
|
332
|
+
return !v.isNull && v.isString ? v.text : undefined;
|
|
333
|
+
});
|
|
334
|
+
if (key !== undefined)
|
|
335
|
+
keys.add(key);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const isMapping = (node) => isMap(isAlias(node) ? this.#targets.get(node) : node);
|
|
339
|
+
const fail = () => {
|
|
340
|
+
throw new YamlSyntaxError("map merge requires map or sequence of maps as the value");
|
|
341
|
+
};
|
|
342
|
+
if (isSeq(source)) {
|
|
343
|
+
for (const item of source.items) {
|
|
344
|
+
if (!isMapping(item))
|
|
345
|
+
fail();
|
|
346
|
+
this.mapping(item, onKey, keys);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (!isMapping(source))
|
|
351
|
+
fail();
|
|
352
|
+
this.mapping(source, onKey, keys);
|
|
353
|
+
}
|
|
354
|
+
// struct decodes a mapping into a Go struct with the given fields.
|
|
355
|
+
struct(raw, fields, set) {
|
|
356
|
+
const done = new Set();
|
|
357
|
+
this.mapping(raw, (name, value) => {
|
|
358
|
+
if (!fields.has(name)) {
|
|
359
|
+
this.unknown = true;
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
// yaml.v3 reports "field already set", a type error, for two keys that decode to one name.
|
|
363
|
+
if (done.has(name)) {
|
|
364
|
+
this.wrongType = true;
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
done.add(name);
|
|
368
|
+
set(name, value);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
attribute(raw) {
|
|
372
|
+
const a = {};
|
|
373
|
+
this.struct(raw, ATTRIBUTE_FIELDS, (field, value) => {
|
|
374
|
+
if (field === "values") {
|
|
375
|
+
const values = this.strings(value);
|
|
376
|
+
if (values !== undefined)
|
|
377
|
+
a.values = values;
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const s = this.string(value);
|
|
381
|
+
if (s !== undefined)
|
|
382
|
+
a[field] = s;
|
|
383
|
+
});
|
|
384
|
+
const { type = "", sensitivity = "", cardinality = "", ...rest } = a;
|
|
385
|
+
return { type, sensitivity, cardinality, ...rest };
|
|
386
|
+
}
|
|
387
|
+
attributes(raw) {
|
|
388
|
+
const out = Object.create(null);
|
|
389
|
+
this.mapping(raw, (name, value) => {
|
|
390
|
+
out[name] = this.attribute(value);
|
|
391
|
+
});
|
|
392
|
+
return out;
|
|
393
|
+
}
|
|
394
|
+
registry(raw) {
|
|
395
|
+
let schema = "";
|
|
396
|
+
let namespace = "";
|
|
397
|
+
let attributes = Object.create(null);
|
|
398
|
+
this.struct(raw, REGISTRY_FIELDS, (field, value) => {
|
|
399
|
+
if (field === "attributes") {
|
|
400
|
+
attributes = this.attributes(value);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const s = this.string(value);
|
|
404
|
+
if (s === undefined)
|
|
405
|
+
return;
|
|
406
|
+
if (field === "schema")
|
|
407
|
+
schema = s;
|
|
408
|
+
else
|
|
409
|
+
namespace = s;
|
|
410
|
+
});
|
|
411
|
+
return { schema, namespace, attributes };
|
|
412
|
+
}
|
|
413
|
+
// problem is the kind Go's conformance test names for the collected problems, if any.
|
|
414
|
+
problem() {
|
|
415
|
+
if (this.duplicate)
|
|
416
|
+
return "duplicate-key";
|
|
417
|
+
if (this.unknown)
|
|
418
|
+
return "unknown-key";
|
|
419
|
+
if (this.wrongType)
|
|
420
|
+
return "type";
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const REGISTRY_FIELDS = new Set(["schema", "namespace", "attributes"]);
|
|
425
|
+
const ATTRIBUTE_FIELDS = new Set([
|
|
426
|
+
"type",
|
|
427
|
+
"sensitivity",
|
|
428
|
+
"cardinality",
|
|
429
|
+
"values",
|
|
430
|
+
"brief",
|
|
431
|
+
]);
|
|
432
|
+
function parse(text) {
|
|
433
|
+
checkCharacters(text);
|
|
434
|
+
let docs = parseAllDocuments(text, OPTIONS);
|
|
435
|
+
const rewritten = withoutQuoteEscapes(text, docs);
|
|
436
|
+
if (rewritten !== text)
|
|
437
|
+
docs = parseAllDocuments(rewritten, OPTIONS);
|
|
438
|
+
const versions = versionDirectives(rewritten, docs);
|
|
439
|
+
const [first, ...rest] = docs;
|
|
440
|
+
if (first === undefined) {
|
|
441
|
+
const [error] = "errors" in docs ? docs.errors : [];
|
|
442
|
+
if (error !== undefined)
|
|
443
|
+
throw new YamlSyntaxError(error.message);
|
|
444
|
+
throw new AppRegistryParseError("empty");
|
|
445
|
+
}
|
|
446
|
+
const decoder = new Decoder(documentAliases(first, true, versions.get(first) ?? []));
|
|
447
|
+
decoder.count(); // yaml.v3 decodes the document node, then its content.
|
|
448
|
+
const reg = decoder.registry(first.contents);
|
|
449
|
+
const problem = decoder.problem();
|
|
450
|
+
if (problem !== undefined)
|
|
451
|
+
throw new AppRegistryParseError(problem);
|
|
452
|
+
const second = rest.find((doc) => !isPhantom(doc));
|
|
453
|
+
if (second !== undefined) {
|
|
454
|
+
documentAliases(second, false, versions.get(second) ?? []);
|
|
455
|
+
throw new AppRegistryParseError("second-document");
|
|
456
|
+
}
|
|
457
|
+
return reg;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Decodes a registry file strictly. It does not apply the policy rules; validateAppRegistry does.
|
|
461
|
+
* Throws AppRegistryParseError when the file does not decode.
|
|
462
|
+
*/
|
|
463
|
+
export function parseAppRegistry(text) {
|
|
464
|
+
try {
|
|
465
|
+
return parse(text);
|
|
466
|
+
}
|
|
467
|
+
catch (err) {
|
|
468
|
+
if (err instanceof YamlSyntaxError)
|
|
469
|
+
throw new AppRegistryParseError("syntax", err.message);
|
|
470
|
+
// A document nested deeply enough to exhaust the stack. yaml.v3 stops at a nesting depth of 10000.
|
|
471
|
+
if (err instanceof RangeError)
|
|
472
|
+
throw new AppRegistryParseError("syntax", "the document nests too deeply");
|
|
473
|
+
throw err;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Decodes registry file bytes: UTF-8, or UTF-16 after a byte order mark, as yaml.v3 reads them. Invalid
|
|
478
|
+
* encoding is a `syntax` error.
|
|
479
|
+
*/
|
|
480
|
+
export function parseAppRegistryBytes(bytes) {
|
|
481
|
+
let text;
|
|
482
|
+
try {
|
|
483
|
+
text = decodeInput(bytes);
|
|
484
|
+
}
|
|
485
|
+
catch (err) {
|
|
486
|
+
if (err instanceof YamlSyntaxError)
|
|
487
|
+
throw new AppRegistryParseError("syntax", err.message);
|
|
488
|
+
throw err;
|
|
489
|
+
}
|
|
490
|
+
return parseAppRegistry(text);
|
|
491
|
+
}
|
package/dist/yamlv3.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ports of the gopkg.in/yaml.v3 behaviours parseAppRegistry must reproduce where the `yaml` library,
|
|
3
|
+
* which follows YAML 1.2, differs: the characters the reader accepts, the double-quoted escapes the
|
|
4
|
+
* scanner accepts, and how an explicitly tagged scalar resolves. Internal to @libo11y/spec.
|
|
5
|
+
*/
|
|
6
|
+
/** A problem yaml.v3 reports as a fatal error, which Go's conformance cases name `syntax`. */
|
|
7
|
+
export declare class YamlSyntaxError extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
/** Throws YamlSyntaxError when text holds a character yaml.v3's reader rejects. */
|
|
11
|
+
export declare function checkCharacters(text: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Checks the escapes in a double-quoted scalar's source, quotes included, against yaml.v3's scanner, and
|
|
14
|
+
* returns the offsets within raw of every `\'`. yaml.v3 accepts `\'` and the `yaml` library, following
|
|
15
|
+
* YAML 1.2, rejects it. YAML 1.2's `\/` is the reverse. Throws YamlSyntaxError on an escape yaml.v3
|
|
16
|
+
* rejects.
|
|
17
|
+
*/
|
|
18
|
+
export declare function scanEscapes(raw: string): number[];
|
|
19
|
+
/** yaml.v3's shortTag: `tag:yaml.org,2002:int` becomes `!!int`. An absent tag is "". */
|
|
20
|
+
export declare function shortTag(tag: string | undefined): string;
|
|
21
|
+
/** Decodes bytes as UTF-8, or as UTF-16 after a UTF-16 byte order mark, as yaml.v3's reader does. */
|
|
22
|
+
export declare function decodeInput(bytes: Uint8Array): string;
|
|
23
|
+
/** What a scalar decodes to: null, or its text and whether a Go interface{} would hold a string. */
|
|
24
|
+
export type ScalarValue = {
|
|
25
|
+
readonly isNull: true;
|
|
26
|
+
} | {
|
|
27
|
+
readonly isNull: false;
|
|
28
|
+
text: string;
|
|
29
|
+
isString: boolean;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Resolves a scalar the way yaml.v3's decoder does before storing it in a Go string. value is yaml.v3's
|
|
33
|
+
* Node.Value, the folded text before tag resolution. quotedOrBlock is true for any non-plain style.
|
|
34
|
+
* Throws YamlSyntaxError when an explicit tag does not fit the value, as yaml.v3 does.
|
|
35
|
+
*/
|
|
36
|
+
export declare function resolveScalar(tag: string | undefined, value: string, quotedOrBlock: boolean): ScalarValue;
|