@mica-js/document 0.4.0 → 0.6.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.
@@ -0,0 +1,704 @@
1
+ //#region src/fused/diagnostics.ts
2
+ /** JSON Pointer of a field path; segments escape `~` and `/` (RFC 6901). */
3
+ function toPointer(path) {
4
+ if (path.length === 0) return "";
5
+ return `/${path.map((segment) => segment.replace(/~/g, "~0").replace(/\//g, "~1")).join("/")}`;
6
+ }
7
+ /**
8
+ * Thrown when fused device data violates its contract.
9
+ *
10
+ * `diagnostics` carries every problem found, each with an explicit code and JSON Pointer, so a
11
+ * host can report them without re-parsing.
12
+ */
13
+ var FusedDocumentError = class extends Error {
14
+ diagnostics;
15
+ constructor(diagnostics) {
16
+ super(buildMessage(diagnostics));
17
+ this.name = "FusedDocumentError";
18
+ this.diagnostics = diagnostics;
19
+ }
20
+ /** Code of the first diagnostic, for coarse programmatic handling. */
21
+ get code() {
22
+ return this.diagnostics[0]?.code;
23
+ }
24
+ /** Field path of the first diagnostic, for coarse programmatic handling. */
25
+ get pointer() {
26
+ return this.diagnostics[0]?.pointer;
27
+ }
28
+ };
29
+ /** Records one diagnostic for `path` as an error. */
30
+ function report(diagnostics, code, message, path) {
31
+ diagnostics.push({
32
+ severity: "error",
33
+ code,
34
+ message,
35
+ pointer: toPointer(path)
36
+ });
37
+ }
38
+ function buildMessage(diagnostics) {
39
+ const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
40
+ const [first, ...rest] = errors.length > 0 ? errors : diagnostics;
41
+ if (first === void 0) return "Fused device validation failed.";
42
+ const location = first.pointer === "" ? "" : ` at ${first.pointer}`;
43
+ const suffix = rest.length === 0 ? "" : ` (and ${rest.length} more)`;
44
+ return `${first.code}${location}: ${first.message}${suffix}`;
45
+ }
46
+ //#endregion
47
+ //#region src/fused/rules.ts
48
+ /** Lexical rules of device identity, versions, paths and member names (RFC 0020 / RFC 0024). */
49
+ var NAME_SEGMENT = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
50
+ var DEVICE_ID = /^[a-z0-9]+(?:-[a-z0-9]+)*:[a-z0-9]+(?:-[a-z0-9]+)*$/;
51
+ var SEMVER = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
52
+ var INTEGRITY = /^sha256:[0-9a-f]{64}$/;
53
+ var MEMBER_KEY = /^[A-Za-z][A-Za-z0-9]*$/;
54
+ var EVENT_KEY = /^onDid[A-Z][A-Za-z0-9]*$/;
55
+ var PIN_ID = /^[A-Za-z_][A-Za-z0-9_]*$/;
56
+ var CODICON = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
57
+ var DESIGNATOR_PREFIX = /^[A-Za-z][A-Za-z0-9]*$/;
58
+ var EXTENSION = /^\.[A-Za-z0-9]+$/;
59
+ var COLOR = /^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
60
+ var DRIVE_LETTER = /^[A-Za-z]:/;
61
+ var URL_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
62
+ /** True when the text carries C0 or DEL control characters. */
63
+ function hasControlCharacters(value) {
64
+ for (const character of value) {
65
+ const code = character.codePointAt(0) ?? 0;
66
+ if (code < 32 || code === 127) return true;
67
+ }
68
+ return false;
69
+ }
70
+ /** Publisher namespace or device name segment; defaults to `U` for designator prefixes. */
71
+ function isNameSegment(value) {
72
+ return NAME_SEGMENT.test(value);
73
+ }
74
+ /** Full `publisher:name` device id (RFC 0020). */
75
+ function isDeviceId(value) {
76
+ return DEVICE_ID.test(value);
77
+ }
78
+ /** Problem of a `publisher:name` device id, or `undefined` when it is well formed. */
79
+ function deviceIdProblem(value) {
80
+ if (value.length === 0) return "must not be empty";
81
+ return isDeviceId(value) ? void 0 : "must be a \"publisher:name\" pair of lowercase dash-separated segments";
82
+ }
83
+ /** Full SemVer version, including prerelease and build metadata (RFC 0020). */
84
+ function isSemVer(value) {
85
+ return SEMVER.test(value);
86
+ }
87
+ /** Release identity or file digest: `sha256:` plus 64 lowercase hex digits (RFC 0020). */
88
+ function isIntegrity(value) {
89
+ return INTEGRITY.test(value);
90
+ }
91
+ /** Property, method, public type and view id key (RFC 0020). */
92
+ function isMemberKey(value) {
93
+ return MEMBER_KEY.test(value);
94
+ }
95
+ /** Event key in `onDidXxx` form (RFC 0020 / RFC 0021). */
96
+ function isEventKey(value) {
97
+ return EVENT_KEY.test(value);
98
+ }
99
+ /** Model pin name, matching the DDL identifier form (RFC 0021). */
100
+ function isPinId(value) {
101
+ return PIN_ID.test(value);
102
+ }
103
+ /** VS Code Codicons name supplied by the editor (RFC 0020). */
104
+ function isCodiconName(value) {
105
+ return CODICON.test(value);
106
+ }
107
+ /** Reference designator prefix (RFC 0020). */
108
+ function isDesignatorPrefix(value) {
109
+ return DESIGNATOR_PREFIX.test(value);
110
+ }
111
+ /** `binary` extension filter entry, e.g. `.bin` (RFC 0020). */
112
+ function isExtensionFilter(value) {
113
+ return EXTENSION.test(value);
114
+ }
115
+ /** `#RRGGBB` / `#RRGGBBAA` color (RFC 0009). */
116
+ function isColor(value) {
117
+ return COLOR.test(value);
118
+ }
119
+ /**
120
+ * Package-root relative POSIX file path problem, or `undefined` when it is well formed.
121
+ *
122
+ * Case sensitivity, empty segments, `.` / `..`, backslashes and absolute paths are rejected
123
+ * (RFC 0020).
124
+ */
125
+ function packagePathProblem(path) {
126
+ return relativePathProblem(path, "package root");
127
+ }
128
+ /** Project-root relative POSIX file path problem, or `undefined` when it is well formed. */
129
+ function projectPathProblem(path) {
130
+ return relativePathProblem(path, "project root");
131
+ }
132
+ /** `device-data` directory relative POSIX file path problem, or `undefined` when well formed. */
133
+ function deviceDataPathProblem(path) {
134
+ return relativePathProblem(path, "device-data directory");
135
+ }
136
+ function relativePathProblem(path, root) {
137
+ if (path.length === 0) return "must not be empty";
138
+ if (path.startsWith("/")) return `must be relative to the ${root}, not absolute`;
139
+ if (DRIVE_LETTER.test(path)) return "must not carry a drive letter";
140
+ if (URL_SCHEME.test(path)) return "must not be a URL";
141
+ if (path.includes("\\")) return "must use POSIX separators";
142
+ if (hasControlCharacters(path)) return "must not contain control characters";
143
+ const segments = path.split("/");
144
+ if (segments[segments.length - 1] === "") return "must name a file, not a directory";
145
+ for (const segment of segments) {
146
+ if (segment.length === 0) return "must not contain empty segments";
147
+ if (segment === "." || segment === "..") return `must not contain "${segment}" segments`;
148
+ }
149
+ }
150
+ /** Problem of a file name that must end in one of `extensions`, or `undefined` when it does. */
151
+ function extensionProblem(path, extensions) {
152
+ const lower = path.toLowerCase();
153
+ return extensions.map((extension) => extension.toLowerCase()).some((extension) => lower.endsWith(extension)) ? void 0 : `must name a ${extensions.join(" or ")} file`;
154
+ }
155
+ /** `sha256:` digest problem of a release identity or file digest, or `undefined` when valid. */
156
+ function integrityProblem(value) {
157
+ return isIntegrity(value) ? void 0 : "must be \"sha256:\" followed by 64 lowercase hexadecimal digits";
158
+ }
159
+ /** Normalizes `#RRGGBB` / `#RRGGBBAA` to upper case; `undefined` when the text is not a color. */
160
+ function normalizeColor(value) {
161
+ return isColor(value) ? value.toUpperCase() : void 0;
162
+ }
163
+ //#endregion
164
+ //#region src/fused/json.ts
165
+ /**
166
+ * Strict JSON reader for the fused contracts.
167
+ *
168
+ * `JSON.parse` silently keeps the last of two equal keys, which would let a manifest or a `.sch`
169
+ * file carry two different values for one field. RFC 0020 and RFC 0024 require rejecting
170
+ * duplicate keys, so the parsers decode JSON text with this reader instead. Keys are compared
171
+ * after unescaping, so `"a"` and `"\u0061"` are the same key.
172
+ */
173
+ /** Deepest object/array nesting the reader accepts. */
174
+ var STRICT_JSON_MAX_DEPTH = 256;
175
+ /**
176
+ * Decodes JSON text, rejecting duplicate keys and reporting every problem with a JSON Pointer.
177
+ *
178
+ * @throws {FusedDocumentError} when the text is not valid JSON or repeats an object key.
179
+ */
180
+ function parseStrictJson(text) {
181
+ const result = decodeStrictJson(text);
182
+ if (!result.ok) throw new FusedDocumentError(result.diagnostics);
183
+ return result.value;
184
+ }
185
+ /** Non-throwing form of {@link parseStrictJson}; the caller decides how to surface the problems. */
186
+ function decodeStrictJson(text) {
187
+ return new Reader(text).decode();
188
+ }
189
+ var Reader = class {
190
+ #text;
191
+ #diagnostics = [];
192
+ #index = 0;
193
+ #failed = false;
194
+ constructor(text) {
195
+ this.#text = text;
196
+ }
197
+ decode() {
198
+ this.#skipWhitespace();
199
+ const value = this.#readValue([], 0);
200
+ if (this.#failed) return this.#failure();
201
+ this.#skipWhitespace();
202
+ if (this.#index !== this.#text.length) {
203
+ this.#fail("Unexpected content after the JSON value.");
204
+ return this.#failure();
205
+ }
206
+ if (this.#diagnostics.length > 0) return this.#failure();
207
+ return {
208
+ ok: true,
209
+ value
210
+ };
211
+ }
212
+ #failure() {
213
+ return {
214
+ ok: false,
215
+ diagnostics: this.#diagnostics
216
+ };
217
+ }
218
+ #fail(message) {
219
+ if (!this.#failed) report(this.#diagnostics, "json/invalid", `Invalid JSON at offset ${String(this.#index)}: ${message}`, []);
220
+ this.#failed = true;
221
+ }
222
+ #skipWhitespace() {
223
+ while (this.#index < this.#text.length) {
224
+ const code = this.#text.charCodeAt(this.#index);
225
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 13) return;
226
+ this.#index += 1;
227
+ }
228
+ }
229
+ #readValue(path, depth) {
230
+ this.#skipWhitespace();
231
+ if (this.#index >= this.#text.length) return this.#fail("Unexpected end of input; expected a value.");
232
+ if (depth > 256) return this.#fail(`Nesting deeper than ${String(256)} is not supported.`);
233
+ switch (this.#text[this.#index]) {
234
+ case "{": return this.#readObject(path, depth + 1);
235
+ case "[": return this.#readArray(path, depth + 1);
236
+ case "\"": return this.#readString();
237
+ case "t": return this.#readKeyword("true", true);
238
+ case "f": return this.#readKeyword("false", false);
239
+ case "n": return this.#readKeyword("null", null);
240
+ default: return this.#readNumber();
241
+ }
242
+ }
243
+ #readKeyword(keyword, value) {
244
+ if (this.#text.startsWith(keyword, this.#index)) {
245
+ this.#index += keyword.length;
246
+ return value;
247
+ }
248
+ return this.#fail(`Expected ${keyword}.`);
249
+ }
250
+ #readNumber() {
251
+ NUMBER.lastIndex = this.#index;
252
+ const match = NUMBER.exec(this.#text);
253
+ if (match === null) return this.#fail("Expected a value.");
254
+ this.#index = NUMBER.lastIndex;
255
+ return Number(match[0]);
256
+ }
257
+ #readString() {
258
+ this.#index += 1;
259
+ let value = "";
260
+ let chunkStart = this.#index;
261
+ for (;;) {
262
+ if (this.#index >= this.#text.length) return this.#fail("Unterminated string.");
263
+ const code = this.#text.charCodeAt(this.#index);
264
+ if (code === 34) {
265
+ value += this.#text.slice(chunkStart, this.#index);
266
+ this.#index += 1;
267
+ return value;
268
+ }
269
+ if (code === 92) {
270
+ value += this.#text.slice(chunkStart, this.#index);
271
+ this.#index += 1;
272
+ const escape = this.#readEscape();
273
+ if (escape === void 0) return;
274
+ value += escape;
275
+ chunkStart = this.#index;
276
+ continue;
277
+ }
278
+ if (code < 32) return this.#fail("Control characters must be escaped inside strings.");
279
+ this.#index += 1;
280
+ }
281
+ }
282
+ #readEscape() {
283
+ if (this.#index >= this.#text.length) return this.#fail("Unterminated escape sequence.");
284
+ const character = this.#text[this.#index];
285
+ this.#index += 1;
286
+ switch (character) {
287
+ case "\"": return "\"";
288
+ case "\\": return "\\";
289
+ case "/": return "/";
290
+ case "b": return "\b";
291
+ case "f": return "\f";
292
+ case "n": return "\n";
293
+ case "r": return "\r";
294
+ case "t": return " ";
295
+ case "u": return this.#readUnicodeEscape();
296
+ default: return this.#fail(`Unknown escape sequence "\\${String(character)}".`);
297
+ }
298
+ }
299
+ #readUnicodeEscape() {
300
+ const digits = this.#text.slice(this.#index, this.#index + 4);
301
+ if (!/^[0-9a-fA-F]{4}$/.test(digits)) return this.#fail("Unicode escapes need exactly four hexadecimal digits.");
302
+ this.#index += 4;
303
+ return String.fromCharCode(Number.parseInt(digits, 16));
304
+ }
305
+ #readObject(path, depth) {
306
+ this.#index += 1;
307
+ const object = {};
308
+ const keys = /* @__PURE__ */ new Set();
309
+ this.#skipWhitespace();
310
+ if (this.#text[this.#index] === "}") {
311
+ this.#index += 1;
312
+ return object;
313
+ }
314
+ for (;;) {
315
+ this.#skipWhitespace();
316
+ if (this.#text[this.#index] !== "\"") return this.#fail("Expected a property name.");
317
+ const key = this.#readString();
318
+ if (key === void 0) return;
319
+ this.#skipWhitespace();
320
+ if (this.#text[this.#index] !== ":") return this.#fail("Expected \":\" after the property name.");
321
+ this.#index += 1;
322
+ const memberPath = [...path, key];
323
+ const duplicate = keys.has(key);
324
+ if (duplicate) report(this.#diagnostics, "json/duplicate-key", `Duplicate JSON key ${JSON.stringify(key)}; the contract does not allow one key twice.`, memberPath);
325
+ else keys.add(key);
326
+ const value = this.#readValue(memberPath, depth);
327
+ if (this.#failed) return;
328
+ if (!duplicate) Object.defineProperty(object, key, {
329
+ value,
330
+ enumerable: true,
331
+ writable: true,
332
+ configurable: true
333
+ });
334
+ this.#skipWhitespace();
335
+ const next = this.#text[this.#index];
336
+ if (next === ",") {
337
+ this.#index += 1;
338
+ continue;
339
+ }
340
+ if (next === "}") {
341
+ this.#index += 1;
342
+ return object;
343
+ }
344
+ return this.#fail("Expected \",\" or \"}\" in the object.");
345
+ }
346
+ }
347
+ #readArray(path, depth) {
348
+ this.#index += 1;
349
+ const array = [];
350
+ this.#skipWhitespace();
351
+ if (this.#text[this.#index] === "]") {
352
+ this.#index += 1;
353
+ return array;
354
+ }
355
+ for (;;) {
356
+ const item = this.#readValue([...path, String(array.length)], depth);
357
+ if (this.#failed) return;
358
+ array.push(item);
359
+ this.#skipWhitespace();
360
+ const next = this.#text[this.#index];
361
+ if (next === ",") {
362
+ this.#index += 1;
363
+ continue;
364
+ }
365
+ if (next === "]") {
366
+ this.#index += 1;
367
+ return array;
368
+ }
369
+ return this.#fail("Expected \",\" or \"]\" in the array.");
370
+ }
371
+ }
372
+ };
373
+ var NUMBER = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
374
+ //#endregion
375
+ //#region src/fused/read.ts
376
+ /** Structural readers shared by the fused parsers; every failure reports code and field path. */
377
+ function isRecord(value) {
378
+ return typeof value === "object" && value !== null && !Array.isArray(value);
379
+ }
380
+ function hasOwn(value, key) {
381
+ return Object.prototype.hasOwnProperty.call(value, key);
382
+ }
383
+ function hasErrors(diagnostics) {
384
+ return diagnostics.some((diagnostic) => diagnostic.severity === "error");
385
+ }
386
+ function forbidUnknownFields(value, allowed, path, diagnostics) {
387
+ for (const key of Object.keys(value)) if (!allowed.includes(key)) report(diagnostics, "schema/unknown-field", `Unknown field "${key}".`, [...path, key]);
388
+ }
389
+ /**
390
+ * Strings are decoded as strict JSON text, which rejects duplicate keys; any other value is
391
+ * validated as-is so callers that already hold a plain object stay supported.
392
+ */
393
+ function parseJsonInput(input, diagnostics) {
394
+ if (typeof input !== "string") return {
395
+ ok: true,
396
+ value: input
397
+ };
398
+ const decoded = decodeStrictJson(input);
399
+ if (!decoded.ok) {
400
+ diagnostics.push(...decoded.diagnostics);
401
+ return {
402
+ ok: false,
403
+ value: void 0
404
+ };
405
+ }
406
+ return {
407
+ ok: true,
408
+ value: decoded.value
409
+ };
410
+ }
411
+ function readStringField(value, key, path, diagnostics, required = true, options = {}) {
412
+ const fieldPath = [...path, key];
413
+ if (!hasOwn(value, key)) {
414
+ if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
415
+ return;
416
+ }
417
+ const field = value[key];
418
+ if (typeof field !== "string") {
419
+ report(diagnostics, "schema/invalid-type", `Field "${key}" must be a string.`, fieldPath);
420
+ return;
421
+ }
422
+ if (options.nonEmpty === true && field.length === 0) {
423
+ report(diagnostics, "schema/invalid-value", `Field "${key}" must not be empty.`, fieldPath);
424
+ return;
425
+ }
426
+ return field;
427
+ }
428
+ function readNumberField(value, key, path, diagnostics, required = true) {
429
+ const fieldPath = [...path, key];
430
+ if (!hasOwn(value, key)) {
431
+ if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
432
+ return;
433
+ }
434
+ const field = value[key];
435
+ if (typeof field !== "number") {
436
+ report(diagnostics, "schema/invalid-type", `Field "${key}" must be a number.`, fieldPath);
437
+ return;
438
+ }
439
+ if (!Number.isFinite(field)) {
440
+ report(diagnostics, "schema/invalid-value", `Field "${key}" must be a finite number.`, fieldPath);
441
+ return;
442
+ }
443
+ return field;
444
+ }
445
+ function readIntegerField(value, key, path, diagnostics, required = true) {
446
+ const field = readNumberField(value, key, path, diagnostics, required);
447
+ if (field === void 0) return;
448
+ if (!Number.isSafeInteger(field)) {
449
+ report(diagnostics, "schema/invalid-value", `Field "${key}" must be a safe integer.`, [...path, key]);
450
+ return;
451
+ }
452
+ return field;
453
+ }
454
+ function readBooleanField(value, key, path, diagnostics, required = true) {
455
+ const fieldPath = [...path, key];
456
+ if (!hasOwn(value, key)) {
457
+ if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
458
+ return;
459
+ }
460
+ const field = value[key];
461
+ if (typeof field !== "boolean") {
462
+ report(diagnostics, "schema/invalid-type", `Field "${key}" must be a boolean.`, fieldPath);
463
+ return;
464
+ }
465
+ return field;
466
+ }
467
+ function readObjectField(value, key, path, diagnostics, required = true) {
468
+ const fieldPath = [...path, key];
469
+ if (!hasOwn(value, key)) {
470
+ if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
471
+ return;
472
+ }
473
+ const field = value[key];
474
+ if (!isRecord(field)) {
475
+ report(diagnostics, "schema/invalid-type", `Field "${key}" must be an object.`, fieldPath);
476
+ return;
477
+ }
478
+ return field;
479
+ }
480
+ function readArrayField(value, key, path, diagnostics, required = true) {
481
+ const fieldPath = [...path, key];
482
+ if (!hasOwn(value, key)) {
483
+ if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
484
+ return;
485
+ }
486
+ const field = value[key];
487
+ if (!Array.isArray(field)) {
488
+ report(diagnostics, "schema/invalid-type", `Field "${key}" must be an array.`, fieldPath);
489
+ return;
490
+ }
491
+ return field;
492
+ }
493
+ /** Compares two strings by UTF-16 code unit, independent of the host locale. */
494
+ function compareStrings(a, b) {
495
+ if (a === b) return 0;
496
+ return a < b ? -1 : 1;
497
+ }
498
+ /**
499
+ * Records an immutable snapshot of a parsed structure.
500
+ *
501
+ * Parsers only build plain objects and arrays, so freezing them is safe; array-buffer views are
502
+ * left alone because their contents cannot be frozen.
503
+ */
504
+ function deepFreeze(value) {
505
+ if (typeof value !== "object" || value === null || ArrayBuffer.isView(value)) return value;
506
+ if (Object.isFrozen(value)) return value;
507
+ Object.freeze(value);
508
+ for (const entry of Object.values(value)) deepFreeze(entry);
509
+ return value;
510
+ }
511
+ //#endregion
512
+ //#region src/fused/resource.ts
513
+ var RESOURCE_MODES = ["asset", "package"];
514
+ /** Reads one instance property value; `undefined` when it does not match the contract. */
515
+ function readPropertyValue(value, path, diagnostics) {
516
+ if (value === null || typeof value === "string" || typeof value === "boolean") return value;
517
+ if (typeof value === "number") {
518
+ if (!Number.isFinite(value)) {
519
+ report(diagnostics, "schema/invalid-value", "Numeric property values must be finite.", path);
520
+ return;
521
+ }
522
+ return value;
523
+ }
524
+ if (isRecord(value)) return readReference(value, path, diagnostics);
525
+ report(diagnostics, "schema/invalid-type", "Property value must be a scalar, a resource reference, a firmware reference or null.", path);
526
+ }
527
+ function readReference(value, path, diagnostics) {
528
+ const mode = readStringField(value, "mode", path, diagnostics, true, { nonEmpty: true });
529
+ if (mode === void 0) return;
530
+ if (mode === "project") return readProjectReference(value, path, diagnostics);
531
+ if (mode === "device-data") return readDeviceDataReference(value, path, diagnostics);
532
+ if (!RESOURCE_MODES.includes(mode)) {
533
+ report(diagnostics, "schema/invalid-value", "Field \"mode\" must be \"asset\", \"package\", \"device-data\" or \"project\".", [...path, "mode"]);
534
+ return;
535
+ }
536
+ return readResourceReference(value, mode, path, diagnostics);
537
+ }
538
+ function readDeviceDataReference(value, path, diagnostics) {
539
+ forbidUnknownFields(value, ["mode", "path"], path, diagnostics);
540
+ const referencePath = readStringField(value, "path", path, diagnostics, true, { nonEmpty: true });
541
+ if (referencePath === void 0) return;
542
+ const problem = deviceDataPathProblem(referencePath);
543
+ if (problem !== void 0) {
544
+ report(diagnostics, "schema/invalid-value", `Field "path" ${problem}.`, [...path, "path"]);
545
+ return;
546
+ }
547
+ return {
548
+ mode: "device-data",
549
+ path: referencePath
550
+ };
551
+ }
552
+ function readProjectReference(value, path, diagnostics) {
553
+ forbidUnknownFields(value, [
554
+ "mode",
555
+ "projectId",
556
+ "name",
557
+ "addressSpace",
558
+ "loadAddress"
559
+ ], path, diagnostics);
560
+ const projectId = readStringField(value, "projectId", path, diagnostics, true, { nonEmpty: true });
561
+ const overrides = readAddressOverrides(value, path, diagnostics);
562
+ if (projectId === void 0 || overrides === void 0) return;
563
+ const name = readStringField(value, "name", path, diagnostics, false, { nonEmpty: true });
564
+ return {
565
+ mode: "project",
566
+ projectId,
567
+ ...name === void 0 ? {} : { name },
568
+ ...overrides
569
+ };
570
+ }
571
+ function readResourceReference(value, mode, path, diagnostics) {
572
+ const hasOverrides = hasOwn(value, "addressSpace") || hasOwn(value, "loadAddress");
573
+ const fields = mode === "asset" ? [
574
+ "assetId",
575
+ "name",
576
+ "mode"
577
+ ] : ["src", "mode"];
578
+ forbidUnknownFields(value, hasOverrides ? [
579
+ ...fields,
580
+ "addressSpace",
581
+ "loadAddress"
582
+ ] : fields, path, diagnostics);
583
+ if (mode === "asset") {
584
+ const assetId = readStringField(value, "assetId", path, diagnostics, true, { nonEmpty: true });
585
+ const name = readStringField(value, "name", path, diagnostics, true, { nonEmpty: true });
586
+ const overrides = readAddressOverrides(value, path, diagnostics);
587
+ if (assetId === void 0 || name === void 0 || overrides === void 0) return void 0;
588
+ return {
589
+ mode,
590
+ assetId,
591
+ name,
592
+ ...overrides
593
+ };
594
+ }
595
+ const src = readStringField(value, "src", path, diagnostics, true, { nonEmpty: true });
596
+ if (src === void 0) return;
597
+ const srcProblem = resourceSrcProblem(mode, src);
598
+ if (srcProblem !== void 0) {
599
+ report(diagnostics, "schema/invalid-value", `Field "src" ${srcProblem}.`, [...path, "src"]);
600
+ return;
601
+ }
602
+ if (!hasOverrides) return {
603
+ src,
604
+ mode
605
+ };
606
+ const overrides = readAddressOverrides(value, path, diagnostics);
607
+ if (overrides === void 0) return;
608
+ return {
609
+ src,
610
+ mode: "package",
611
+ ...overrides
612
+ };
613
+ }
614
+ /** Reads optional `addressSpace` / `loadAddress` overrides of a firmware reference. */
615
+ function readAddressOverrides(value, path, diagnostics) {
616
+ const addressSpace = readStringField(value, "addressSpace", path, diagnostics, false, { nonEmpty: true });
617
+ const loadAddress = readIntegerField(value, "loadAddress", path, diagnostics, false);
618
+ if (loadAddress !== void 0 && loadAddress < 0) {
619
+ report(diagnostics, "schema/invalid-value", "Field \"loadAddress\" must be a non-negative safe integer.", [...path, "loadAddress"]);
620
+ return;
621
+ }
622
+ if (hasOwn(value, "addressSpace") && addressSpace === void 0) return;
623
+ if (hasOwn(value, "loadAddress") && loadAddress === void 0) return;
624
+ return {
625
+ ...addressSpace === void 0 ? {} : { addressSpace },
626
+ ...loadAddress === void 0 ? {} : { loadAddress }
627
+ };
628
+ }
629
+ /** Payload problem of a resource `src` for its mode, or `undefined` when it is well formed. */
630
+ function resourceSrcProblem(mode, src) {
631
+ switch (mode) {
632
+ case "package": return packagePathProblem(src);
633
+ case "asset": return src.length === 0 ? "must not be empty" : void 0;
634
+ }
635
+ }
636
+ //#endregion
637
+ //#region src/fused/instance-data.ts
638
+ function readDependencies(value, path, diagnostics) {
639
+ if (value === void 0) return;
640
+ const dependencies = Object.create(null);
641
+ let failed = false;
642
+ for (const [deviceId, raw] of Object.entries(value)) {
643
+ const dependencyPath = [...path, deviceId];
644
+ const idProblem = deviceIdProblem(deviceId);
645
+ if (idProblem !== void 0) {
646
+ report(diagnostics, "schema/invalid-value", `Dependency key ${JSON.stringify(deviceId)} ${idProblem}.`, dependencyPath);
647
+ failed = true;
648
+ continue;
649
+ }
650
+ if (!isRecord(raw)) {
651
+ report(diagnostics, "schema/invalid-type", "Dependency must be an object.", dependencyPath);
652
+ failed = true;
653
+ continue;
654
+ }
655
+ forbidUnknownFields(raw, ["version", "integrity"], dependencyPath, diagnostics);
656
+ const version = readStringField(raw, "version", dependencyPath, diagnostics, true, { nonEmpty: true });
657
+ if (version !== void 0 && !isSemVer(version)) {
658
+ report(diagnostics, "schema/invalid-value", "Dependency \"version\" must be a full SemVer version.", [...dependencyPath, "version"]);
659
+ failed = true;
660
+ }
661
+ const integrity = readStringField(raw, "integrity", dependencyPath, diagnostics, true, { nonEmpty: true });
662
+ const integrityIssue = integrity === void 0 ? void 0 : integrityProblem(integrity);
663
+ if (integrityIssue !== void 0) {
664
+ report(diagnostics, "schema/invalid-value", `Dependency "integrity" ${integrityIssue}.`, [...dependencyPath, "integrity"]);
665
+ failed = true;
666
+ }
667
+ if (version === void 0 || integrity === void 0 || integrityIssue !== void 0) {
668
+ failed = true;
669
+ continue;
670
+ }
671
+ dependencies[deviceId] = {
672
+ version,
673
+ integrity
674
+ };
675
+ }
676
+ return failed ? void 0 : dependencies;
677
+ }
678
+ function readProperties(raw, path, diagnostics) {
679
+ if (!isRecord(raw)) {
680
+ report(diagnostics, "schema/invalid-type", "Field \"properties\" must be an object.", path);
681
+ return;
682
+ }
683
+ const properties = Object.create(null);
684
+ let failed = false;
685
+ for (const [key, value] of Object.entries(raw)) {
686
+ const entryPath = [...path, key];
687
+ if (!isMemberKey(key)) {
688
+ report(diagnostics, "schema/invalid-value", `Property key ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, entryPath);
689
+ failed = true;
690
+ continue;
691
+ }
692
+ const property = readPropertyValue(value, entryPath, diagnostics);
693
+ if (property === void 0) {
694
+ failed = true;
695
+ continue;
696
+ }
697
+ properties[key] = property;
698
+ }
699
+ return failed ? void 0 : properties;
700
+ }
701
+ //#endregion
702
+ export { isNameSegment as A, isColor as C, isExtensionFilter as D, isEventKey as E, projectPathProblem as F, FusedDocumentError as I, report as L, isSemVer as M, normalizeColor as N, isIntegrity as O, packagePathProblem as P, toPointer as R, isCodiconName as S, isDeviceId as T, decodeStrictJson as _, forbidUnknownFields as a, deviceIdProblem as b, isRecord as c, readBooleanField as d, readIntegerField as f, STRICT_JSON_MAX_DEPTH as g, readStringField as h, deepFreeze as i, isPinId as j, isMemberKey as k, parseJsonInput as l, readObjectField as m, readProperties as n, hasErrors as o, readNumberField as p, compareStrings as r, hasOwn as s, readDependencies as t, readArrayField as u, parseStrictJson as v, isDesignatorPrefix as w, extensionProblem as x, deviceDataPathProblem as y };
703
+
704
+ //# sourceMappingURL=instance-data-CX-ElHak.js.map