@cbortech/cbor 0.26.4 → 0.26.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.ja.md +103 -38
  2. package/README.md +108 -42
  3. package/dist/ast/CborAppSeqResult.d.ts +3 -0
  4. package/dist/ast/index.cjs +1 -1
  5. package/dist/ast/index.js +2 -2
  6. package/dist/cbor.d.ts +8 -0
  7. package/dist/cddl/ast.d.ts +196 -0
  8. package/dist/cddl/controls.d.ts +28 -0
  9. package/dist/cddl/equal.d.ts +19 -0
  10. package/dist/cddl/errors.d.ts +91 -0
  11. package/dist/cddl/index.cjs +3 -0
  12. package/dist/cddl/index.cjs.map +1 -0
  13. package/dist/cddl/index.d.ts +52 -0
  14. package/dist/cddl/index.js +67 -0
  15. package/dist/cddl/index.js.map +1 -0
  16. package/dist/cddl/parser.d.ts +13 -0
  17. package/dist/cddl/position.d.ts +13 -0
  18. package/dist/cddl/prelude.d.ts +5 -0
  19. package/dist/cddl/schema.d.ts +90 -0
  20. package/dist/cddl/tokenizer.d.ts +138 -0
  21. package/dist/cddl/validator.d.ts +30 -0
  22. package/dist/cddl/writer.d.ts +23 -0
  23. package/dist/cdn/serialize-utils.d.ts +3 -2
  24. package/dist/extensions/types.d.ts +3 -1
  25. package/dist/index.cjs +7 -7
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +104 -77
  29. package/dist/index.js.map +1 -1
  30. package/dist/mapEntries-BhMlCwYo.cjs +15 -0
  31. package/dist/mapEntries-BhMlCwYo.cjs.map +1 -0
  32. package/dist/{mapEntries-hyNVtz5Z.js → mapEntries-Czxt-cmd.js} +458 -456
  33. package/dist/mapEntries-Czxt-cmd.js.map +1 -0
  34. package/dist/schema-BmGsaEaW.cjs +63 -0
  35. package/dist/schema-BmGsaEaW.cjs.map +1 -0
  36. package/dist/schema-BxkgvUY6.js +1977 -0
  37. package/dist/schema-BxkgvUY6.js.map +1 -0
  38. package/dist/types.d.ts +107 -4
  39. package/dist/utils/base64.d.ts +12 -0
  40. package/package.json +23 -6
  41. package/dist/mapEntries-DDJxbotH.cjs +0 -15
  42. package/dist/mapEntries-DDJxbotH.cjs.map +0 -1
  43. package/dist/mapEntries-hyNVtz5Z.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,14 +1,15 @@
1
1
  import { o as e } from "./tokenizer-CeuixxXi.js";
2
- import { E as t, F as n, I as r, L as i, M as a, N as o, P as s, _ as c, a as l, c as u, d, f, h as p, i as m, l as h, m as g, n as _, o as v, p as y, r as b, s as x, t as S, u as C } from "./mapEntries-hyNVtz5Z.js";
2
+ import { n as t, s as n } from "./schema-BxkgvUY6.js";
3
+ import { B as r, H as i, L as a, R as o, V as s, _ as c, a as l, c as u, d, f, h as p, i as m, k as h, l as g, m as _, n as v, o as y, p as b, r as x, s as S, t as C, u as w, z as T } from "./mapEntries-Czxt-cmd.js";
3
4
  //#region src/extensions/b32.ts
4
- var w = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", T = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
5
- function E(e) {
5
+ var E = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", D = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
6
+ function O(e) {
6
7
  let t = e.length;
7
8
  for (; t > 0 && e.charCodeAt(t - 1) === 61;) t--;
8
9
  return e.slice(0, t);
9
10
  }
10
- function D(e, t, n) {
11
- let r = E(e).toUpperCase(), i = r.length % 8;
11
+ function k(e, t, n) {
12
+ let r = O(e).toUpperCase(), i = r.length % 8;
12
13
  if (i === 1 || i === 3 || i === 6) throw SyntaxError(`invalid base32 length: ${r.length} characters`);
13
14
  let a = (/* @__PURE__ */ new Uint8Array(128)).fill(255);
14
15
  for (let e = 0; e < t.length; e++) a[t.charCodeAt(e)] = e;
@@ -25,43 +26,59 @@ function D(e, t, n) {
25
26
  }
26
27
  return o;
27
28
  }
28
- var O = {
29
+ var A = {
29
30
  appStringPrefixes: ["b32"],
30
- parseAppString(e, n, r) {
31
- return new t(D(v(n), w, r), { ednEncoding: "base32" });
31
+ parseAppString(e, t, n) {
32
+ return new h(k(y(t), E, n), { ednEncoding: "base32" });
32
33
  }
33
- }, k = {
34
+ }, j = {
34
35
  appStringPrefixes: ["h32"],
35
- parseAppString(e, n, r) {
36
- return new t(D(v(n), T, r), { ednEncoding: "base32hex" });
36
+ parseAppString(e, t, n) {
37
+ return new h(k(y(t), D, n), { ednEncoding: "base32hex" });
37
38
  }
38
39
  };
39
40
  //#endregion
40
41
  //#region src/extensions/same.ts
41
- function A(e, t) {
42
+ function M(e, t) {
42
43
  if (e.length !== t.length) return !1;
43
44
  for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
44
45
  return !0;
45
46
  }
46
- var j = {
47
+ var N = {
47
48
  appStringPrefixes: ["same"],
48
49
  preserveAppSeqSource: !0,
49
50
  parseAppSequence(e, t, n) {
50
51
  if (t.length === 0) throw SyntaxError("same<<...>> requires at least one item");
51
52
  let r = t[0], i = r.toCBOR();
52
- for (let e = 1; e < t.length; e++) if (!A(i, t[e].toCBOR())) {
53
+ for (let e = 1; e < t.length; e++) if (!M(i, t[e].toCBOR())) {
53
54
  let t = `same<<...>>: item ${e} produces different CBOR bytes than item 0`;
54
55
  if (n) n(t);
55
56
  else throw SyntaxError(t);
56
57
  }
57
58
  return r;
58
59
  }
59
- }, M = class t {
60
+ }, P = /* @__PURE__ */ new Map(), F = 64;
61
+ function I(e) {
62
+ if (typeof e != "string") return e;
63
+ let n = P.get(e);
64
+ return n || (n = t(e), P.size >= F && P.delete(P.keys().next().value), P.set(e, n)), n;
65
+ }
66
+ function L(e, t, r) {
67
+ if (t) {
68
+ let i = t.validate(e, r);
69
+ if (!i.valid) throw new n(i.errors, i.warnings);
70
+ }
71
+ return e;
72
+ }
73
+ function R(e, t) {
74
+ return L(e, I(t?.cddl), t?.cddlValidationOptions);
75
+ }
76
+ var z = class t {
60
77
  static OMIT = o;
61
- static TAG = s;
62
- static Tag = r;
78
+ static TAG = T;
79
+ static Tag = s;
63
80
  static Simple = a;
64
- static MapEntries = S;
81
+ static MapEntries = C;
65
82
  static dt_as_Date = p;
66
83
  #e;
67
84
  constructor(e) {
@@ -153,7 +170,7 @@ var j = {
153
170
  stringify(e, n, r) {
154
171
  if (typeof n == "function" || Array.isArray(n) || n === null || n === void 0 && r !== void 0) {
155
172
  let i = { ...this.#e };
156
- return n === null ? i.replacer = void 0 : (typeof n == "function" || Array.isArray(n)) && (i.replacer = n), r !== void 0 && (i.indent = z(r)), t.stringify(e, i);
173
+ return n === null ? i.replacer = void 0 : (typeof n == "function" || Array.isArray(n)) && (i.replacer = n), r !== void 0 && (i.indent = K(r)), t.stringify(e, i);
157
174
  }
158
175
  return t.stringify(e, this.#t(n ?? void 0));
159
176
  }
@@ -161,73 +178,73 @@ var j = {
161
178
  return t.format(e, this.#t(n));
162
179
  }
163
180
  static fromCBOR(e, t) {
164
- return d(e, t);
181
+ return R(d(e, t), t);
165
182
  }
166
183
  static fromCDN(e, t) {
167
- return c(e, t);
184
+ return R(c(e, t), t);
168
185
  }
169
186
  static fromEDN(e, n) {
170
187
  return t.fromCDN(e, n);
171
188
  }
172
189
  static *fromHexDumpSeq(e, n) {
173
- let r = [], i = N(e).trim().split(/\s+/).filter(Boolean);
190
+ let r = [], i = B(e).trim().split(/\s+/).filter(Boolean);
174
191
  for (let e of i) if (/^[0-9A-Fa-f]{2}$/.test(e)) r.push(parseInt(e, 16));
175
192
  else if (/^[0-9A-Fa-f]+$/.test(e) && e.length % 2 == 0) for (let t = 0; t < e.length; t += 2) r.push(parseInt(e.slice(t, t + 2), 16));
176
193
  else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);
177
194
  yield* t.fromCBORSeq(new Uint8Array(r), n);
178
195
  }
179
196
  static *fromCBORSeq(e, t) {
180
- let n = e instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && e instanceof SharedArrayBuffer ? new Uint8Array(e) : new Uint8Array(e.buffer, e.byteOffset, e.byteLength), r = 0;
181
- for (; r < n.byteLength;) {
182
- let e = d(n, {
197
+ let n = I(t?.cddl), r = e instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && e instanceof SharedArrayBuffer ? new Uint8Array(e) : new Uint8Array(e.buffer, e.byteOffset, e.byteLength), i = 0;
198
+ for (; i < r.byteLength;) {
199
+ let e = d(r, {
183
200
  ...t,
184
- offset: r,
201
+ offset: i,
185
202
  allowTrailing: !0
186
203
  });
187
- yield e, r = e.end;
204
+ yield L(e, n, t?.cddlValidationOptions), i = e.end;
188
205
  }
189
206
  }
190
207
  static *fromCDNSeq(t, n) {
191
- let r = !!n?.preserveComments, i = 0, a = !0;
208
+ let r = I(n?.cddl), i = !!n?.preserveComments, a = 0, o = !0;
192
209
  for (;;) {
193
- let { offset: o, hadSeparator: s, commaOffset: l } = R(t, i, n, r ? a ? "all" : "after-newline" : "none");
194
- if (a && l >= 0) {
210
+ let { offset: s, hadSeparator: l, commaOffset: u } = G(t, a, n, i ? o ? "all" : "after-newline" : "none");
211
+ if (o && u >= 0) {
195
212
  let e = "leading comma in CDN sequence";
196
213
  if (n?.strict !== !1) throw SyntaxError(e);
197
- I(e, l, n);
214
+ U(e, u, n);
198
215
  }
199
- if (o >= t.length || r && L(t, o) && R(t, o, n).offset >= t.length) break;
200
- if (!a && !s) {
216
+ if (s >= t.length || i && W(t, s) && G(t, s, n).offset >= t.length) break;
217
+ if (!o && !l) {
201
218
  let e = "CDN sequence items must be separated by whitespace, comma, or comment";
202
219
  if (n?.strict !== !1) throw SyntaxError(e);
203
- I(e, o, n);
220
+ U(e, s, n);
204
221
  }
205
- i = o;
206
- let u;
222
+ a = s;
223
+ let d;
207
224
  try {
208
- u = c(t, {
225
+ d = c(t, {
209
226
  ...n,
210
- offset: i,
227
+ offset: a,
211
228
  allowTrailing: !0,
212
229
  _skipRS: !0
213
230
  });
214
231
  } catch (t) {
215
232
  if (n?.strict !== !1) throw t;
216
- I(t instanceof Error ? t.message : String(t), i, n, !0, t instanceof e ? t : void 0);
233
+ U(t instanceof Error ? t.message : String(t), a, n, !0, t instanceof e ? t : void 0);
217
234
  break;
218
235
  }
219
- yield u, i = u.end, a = !1;
236
+ yield L(d, r, n?.cddlValidationOptions), a = d.end, o = !1;
220
237
  }
221
238
  }
222
239
  static fromJS(e, t) {
223
- return b(e, t);
240
+ return R(x(e, t), t);
224
241
  }
225
242
  static fromHexDump(e, t) {
226
- let n = [], r = N(e).trim().split(/\s+/).filter(Boolean);
243
+ let n = [], r = B(e).trim().split(/\s+/).filter(Boolean);
227
244
  for (let e of r) if (/^[0-9A-Fa-f]{2}$/.test(e)) n.push(parseInt(e, 16));
228
245
  else if (/^[0-9A-Fa-f]+$/.test(e) && e.length % 2 == 0) for (let t = 0; t < e.length; t += 2) n.push(parseInt(e.slice(t, t + 2), 16));
229
246
  else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);
230
- return d(new Uint8Array(n), t);
247
+ return R(d(new Uint8Array(n), t), t);
231
248
  }
232
249
  static decode(e, n) {
233
250
  return t.fromCBOR(e, n).toJS(n);
@@ -273,7 +290,14 @@ var j = {
273
290
  }
274
291
  i.push(e);
275
292
  }
276
- }, c = 0;
293
+ }, c = I(r?.cddl), l = [], u = [], d = (e) => {
294
+ if (!c) return;
295
+ let t = c.validate(e, r?.cddlValidationOptions);
296
+ l.push(...t.errors), t.warnings && u.push(...t.warnings);
297
+ }, f = c ? {
298
+ cddlErrors: l,
299
+ cddlWarnings: u
300
+ } : {}, p = 0;
277
301
  try {
278
302
  let e = r?.type ?? "cbor";
279
303
  if (e === "cdn") {
@@ -281,33 +305,36 @@ var j = {
281
305
  ...s,
282
306
  unresolvedExtension: r?.unresolvedExtension
283
307
  };
284
- for (let r of t.fromCDNSeq(n, e)) c++;
285
- } else if (e === "hex") for (let e of t.fromHexDumpSeq(n, s)) c++;
286
- else for (let e of t.fromCBORSeq(n, s)) c++;
308
+ for (let r of t.fromCDNSeq(n, e)) p++, d(r);
309
+ } else if (e === "hex") for (let e of t.fromHexDumpSeq(n, s)) p++, d(e);
310
+ else for (let e of t.fromCBORSeq(n, s)) p++, d(e);
287
311
  } catch (e) {
288
312
  return {
289
313
  valid: !1,
290
- count: c,
314
+ count: p,
291
315
  warnings: i,
292
316
  hints: a,
293
- error: e instanceof Error ? e : Error(String(e))
317
+ error: e instanceof Error ? e : Error(String(e)),
318
+ ...f
294
319
  };
295
320
  }
296
321
  if (o) {
297
322
  let t = o.cause instanceof Error ? o.cause : new e(o.message, { offset: o.offset });
298
323
  return {
299
324
  valid: !1,
300
- count: c,
325
+ count: p,
301
326
  warnings: i,
302
327
  hints: a,
303
- error: t
328
+ error: t,
329
+ ...f
304
330
  };
305
331
  }
306
332
  return {
307
- valid: i.length === 0,
308
- count: c,
333
+ valid: i.length === 0 && l.length === 0,
334
+ count: p,
309
335
  warnings: i,
310
- hints: a
336
+ hints: a,
337
+ ...f
311
338
  };
312
339
  }
313
340
  static cborToCdn(e, n) {
@@ -327,81 +354,81 @@ var j = {
327
354
  }
328
355
  static stringify(e, t, n) {
329
356
  if (typeof t == "function" || Array.isArray(t) || t === null || t === void 0 && n !== void 0) {
330
- let r = typeof t == "function" || Array.isArray(t) ? t : void 0, i = z(n);
357
+ let r = typeof t == "function" || Array.isArray(t) ? t : void 0, i = K(n);
331
358
  if (r) {
332
- let t = _(e, r);
333
- return t === void 0 || t === o ? void 0 : b(t).toCDN(i === void 0 ? void 0 : { indent: i });
359
+ let t = v(e, r);
360
+ return t === void 0 || t === o ? void 0 : x(t).toCDN(i === void 0 ? void 0 : { indent: i });
334
361
  }
335
- return b(e).toCDN(i === void 0 ? void 0 : { indent: i });
362
+ return x(e).toCDN(i === void 0 ? void 0 : { indent: i });
336
363
  }
337
364
  let r = t;
338
365
  if (r?.replacer) {
339
- let t = _(e, r.replacer, r.extensions, r.undefinedOmits, r.builtinExtensions);
366
+ let t = v(e, r.replacer, r.extensions, r.undefinedOmits, r.builtinExtensions);
340
367
  if (t === void 0 || t === o) return;
341
368
  let { replacer: n, ...i } = r;
342
- return b(t, Object.keys(i).length > 0 ? i : void 0).toCDN(r);
369
+ return R(x(t, Object.keys(i).length > 0 ? i : void 0), r).toCDN(r);
343
370
  }
344
- return b(e, r).toCDN(r);
371
+ return R(x(e, r), r).toCDN(r);
345
372
  }
346
373
  static format(e, n) {
347
374
  return t.fromCDN(e, n).toCDN(n);
348
375
  }
349
376
  };
350
- function N(e) {
377
+ function B(e) {
351
378
  let t = "", n = 0;
352
379
  for (; n < e.length;) {
353
380
  let r = e[n], i = e[n + 1] ?? "";
354
381
  if (r === "-" && i === "-") {
355
- n = P(e, n + 2), t += " ";
382
+ n = V(e, n + 2), t += " ";
356
383
  continue;
357
384
  }
358
385
  if (r === "—") {
359
- n = P(e, n + 1), t += " ";
386
+ n = V(e, n + 1), t += " ";
360
387
  continue;
361
388
  }
362
389
  if (r === "#") {
363
- n = P(e, n + 1), t += " ";
390
+ n = V(e, n + 1), t += " ";
364
391
  continue;
365
392
  }
366
393
  if (r === "/" && i === "/") {
367
- n = P(e, n + 2), t += " ";
394
+ n = V(e, n + 2), t += " ";
368
395
  continue;
369
396
  }
370
397
  if (r === "/" && i === "*") {
371
398
  let r = e.indexOf("*/", n + 2);
372
399
  if (r < 0) throw SyntaxError("Unterminated comment in hex dump");
373
- t += F(e.slice(n, r + 2)), n = r + 2;
400
+ t += H(e.slice(n, r + 2)), n = r + 2;
374
401
  continue;
375
402
  }
376
403
  if (r === "/") {
377
404
  let r = e.indexOf("/", n + 1);
378
405
  if (r < 0) throw SyntaxError("Unterminated comment in hex dump");
379
- t += F(e.slice(n, r + 1)), n = r + 1;
406
+ t += H(e.slice(n, r + 1)), n = r + 1;
380
407
  continue;
381
408
  }
382
409
  t += r, n++;
383
410
  }
384
411
  return t;
385
412
  }
386
- function P(e, t) {
413
+ function V(e, t) {
387
414
  let n = e.indexOf("\n", t);
388
415
  return n < 0 ? e.length : n;
389
416
  }
390
- function F(e) {
417
+ function H(e) {
391
418
  return e.replace(/[^\r\n]/g, " ");
392
419
  }
393
- function I(e, t, n, r, i) {
420
+ function U(e, t, n, r, i) {
394
421
  let a = i?.offset ?? t, o = {
395
422
  message: e,
396
423
  offset: a
397
424
  };
398
425
  r && (o.fatal = !0), i && (o.cause = i), i?.offset !== void 0 && (o.line = i.line, o.column = i.column, o.endOffset = i.endOffset), n?.onWarning ? n.onWarning(o) : n?.silent || console.warn(`CDN sequence warning at offset ${a}: ${e}`);
399
426
  }
400
- function L(e, t) {
427
+ function W(e, t) {
401
428
  let n = e[t];
402
429
  return n === "#" || n === "/";
403
430
  }
404
- function R(e, t, n, r = "none") {
431
+ function G(e, t, n, r = "none") {
405
432
  let i = t, a = !1, o = !1, s = !1, c = -1, l = () => r === "all" || r === "after-newline" && s;
406
433
  for (; i < e.length;) {
407
434
  let t = e[i];
@@ -431,7 +458,7 @@ function R(e, t, n, r = "none") {
431
458
  if (t < 0) {
432
459
  let t = "unterminated /* comment in CDN sequence";
433
460
  if (n?.strict !== !1) throw SyntaxError(t);
434
- I(t, i, n, !0), i = e.length;
461
+ U(t, i, n, !0), i = e.length;
435
462
  } else e.slice(i, t).includes("\n") && (s = !0), i = t + 2;
436
463
  continue;
437
464
  }
@@ -441,7 +468,7 @@ function R(e, t, n, r = "none") {
441
468
  if (t < 0) {
442
469
  let t = "unterminated / comment in CDN sequence";
443
470
  if (n?.strict !== !1) throw SyntaxError(t);
444
- I(t, i, n, !0), i = e.length;
471
+ U(t, i, n, !0), i = e.length;
445
472
  } else e.slice(i, t).includes("\n") && (s = !0), i = t + 1;
446
473
  continue;
447
474
  }
@@ -457,7 +484,7 @@ function R(e, t, n, r = "none") {
457
484
  commaOffset: c
458
485
  };
459
486
  }
460
- function z(e) {
487
+ function K(e) {
461
488
  if (typeof e == "number") {
462
489
  let t = Math.floor(Math.min(10, Math.max(0, e)));
463
490
  return t === 0 ? void 0 : t;
@@ -465,6 +492,6 @@ function z(e) {
465
492
  if (typeof e == "string") return e.slice(0, 10) || void 0;
466
493
  }
467
494
  //#endregion
468
- export { m as BUILTIN_EXTENSIONS, M as CBOR, M as default, o as CBOR_OMIT, s as CBOR_TAG, e as CdnSyntaxError, S as MapEntries, n as Null, a as Simple, r as Tag, i as Undefined, h as b1, O as b32, f as cri, g as dt, p as dt_as_Date, l as float, k as h32, x as ilbs, u as ilts, y as ip, j as same, C as t1 };
495
+ export { m as BUILTIN_EXTENSIONS, z as CBOR, z as default, o as CBOR_OMIT, T as CBOR_TAG, n as CddlMismatchError, e as CdnSyntaxError, C as MapEntries, r as Null, a as Simple, s as Tag, i as Undefined, g as b1, A as b32, f as cri, _ as dt, p as dt_as_Date, l as float, j as h32, S as ilbs, u as ilts, b as ip, N as same, w as t1 };
469
496
 
470
497
  //# sourceMappingURL=index.js.map