@cbortech/cbor 0.26.5 → 0.26.7

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 (52) hide show
  1. package/README.ja.md +239 -34
  2. package/README.md +242 -38
  3. package/dist/ast/CborByteString.d.ts +14 -0
  4. package/dist/ast/CborEllipsis.d.ts +94 -2
  5. package/dist/ast/CborFloat.d.ts +10 -0
  6. package/dist/ast/CborItem.d.ts +93 -4
  7. package/dist/ast/CborNint.d.ts +9 -0
  8. package/dist/ast/CborSimple.d.ts +11 -2
  9. package/dist/ast/CborTag.d.ts +8 -0
  10. package/dist/ast/CborTextString.d.ts +20 -0
  11. package/dist/ast/CborUint.d.ts +8 -0
  12. package/dist/ast/index.cjs +1 -1
  13. package/dist/ast/index.js +2 -2
  14. package/dist/cbor.d.ts +8 -0
  15. package/dist/cddl/ast.d.ts +196 -0
  16. package/dist/cddl/controls.d.ts +28 -0
  17. package/dist/cddl/equal.d.ts +19 -0
  18. package/dist/cddl/errors.d.ts +91 -0
  19. package/dist/cddl/index.cjs +3 -0
  20. package/dist/cddl/index.cjs.map +1 -0
  21. package/dist/cddl/index.d.ts +52 -0
  22. package/dist/cddl/index.js +67 -0
  23. package/dist/cddl/index.js.map +1 -0
  24. package/dist/cddl/parser.d.ts +13 -0
  25. package/dist/cddl/position.d.ts +13 -0
  26. package/dist/cddl/prelude.d.ts +5 -0
  27. package/dist/cddl/schema.d.ts +90 -0
  28. package/dist/cddl/tokenizer.d.ts +138 -0
  29. package/dist/cddl/validator.d.ts +30 -0
  30. package/dist/cddl/writer.d.ts +23 -0
  31. package/dist/cdn/serialize-utils.d.ts +186 -8
  32. package/dist/extensions/dt.d.ts +3 -0
  33. package/dist/extensions/types.d.ts +31 -9
  34. package/dist/index.cjs +4 -4
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.ts +2 -0
  37. package/dist/index.js +97 -107
  38. package/dist/index.js.map +1 -1
  39. package/dist/mapEntries-D2NyeCX3.cjs +17 -0
  40. package/dist/mapEntries-D2NyeCX3.cjs.map +1 -0
  41. package/dist/{mapEntries-Clr-oNtQ.js → mapEntries-DxrDre2P.js} +1498 -991
  42. package/dist/mapEntries-DxrDre2P.js.map +1 -0
  43. package/dist/schema-Bofmsptw.js +1977 -0
  44. package/dist/schema-Bofmsptw.js.map +1 -0
  45. package/dist/schema-t_bdPk8_.cjs +63 -0
  46. package/dist/schema-t_bdPk8_.cjs.map +1 -0
  47. package/dist/types.d.ts +258 -7
  48. package/dist/utils/base64.d.ts +12 -0
  49. package/package.json +24 -7
  50. package/dist/mapEntries-6hy7UgeN.cjs +0 -15
  51. package/dist/mapEntries-6hy7UgeN.cjs.map +0 -1
  52. package/dist/mapEntries-Clr-oNtQ.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,68 +1,48 @@
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-Clr-oNtQ.js";
3
- //#region src/extensions/b32.ts
4
- var w = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", T = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
5
- function E(e) {
6
- let t = e.length;
7
- for (; t > 0 && e.charCodeAt(t - 1) === 61;) t--;
8
- return e.slice(0, t);
9
- }
10
- function D(e, t, n) {
11
- let r = E(e).toUpperCase(), i = r.length % 8;
12
- if (i === 1 || i === 3 || i === 6) throw SyntaxError(`invalid base32 length: ${r.length} characters`);
13
- let a = (/* @__PURE__ */ new Uint8Array(128)).fill(255);
14
- for (let e = 0; e < t.length; e++) a[t.charCodeAt(e)] = e;
15
- let o = new Uint8Array(Math.floor(r.length * 5 / 8)), s = 0, c = 0, l = 0;
16
- for (let e of r) {
17
- let t = e.charCodeAt(0), n = t < 128 ? a[t] : 255;
18
- if (n === 255) throw SyntaxError(`invalid character in byte string: ${JSON.stringify(e)}`);
19
- s = s << 5 | n, c += 5, c >= 8 && (c -= 8, o[l++] = s >> c & 255);
20
- }
21
- if (c > 0 && s & (1 << c) - 1) {
22
- let e = "non-zero trailing bits in base32 input";
23
- if (n) n(e);
24
- else throw SyntaxError(e);
25
- }
26
- return o;
27
- }
28
- var O = {
29
- appStringPrefixes: ["b32"],
30
- parseAppString(e, n, r) {
31
- return new t(D(v(n), w, r), { ednEncoding: "base32" });
32
- }
33
- }, k = {
34
- appStringPrefixes: ["h32"],
35
- parseAppString(e, n, r) {
36
- return new t(D(v(n), T, r), { ednEncoding: "base32hex" });
37
- }
38
- };
39
- //#endregion
2
+ import { n as t, s as n } from "./schema-Bofmsptw.js";
3
+ import { B as r, C as i, H as a, R as o, S as s, U as c, V as l, a as u, c as d, d as f, f as p, g as m, i 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-DxrDre2P.js";
40
4
  //#region src/extensions/same.ts
41
- function A(e, t) {
5
+ function E(e, t) {
42
6
  if (e.length !== t.length) return !1;
43
7
  for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
44
8
  return !0;
45
9
  }
46
- var j = {
10
+ var D = {
47
11
  appStringPrefixes: ["same"],
48
12
  preserveAppSeqSource: !0,
49
13
  parseAppSequence(e, t, n) {
50
14
  if (t.length === 0) throw SyntaxError("same<<...>> requires at least one item");
51
15
  let r = t[0], i = r.toCBOR();
52
- for (let e = 1; e < t.length; e++) if (!A(i, t[e].toCBOR())) {
16
+ for (let e = 1; e < t.length; e++) if (!E(i, t[e].toCBOR())) {
53
17
  let t = `same<<...>>: item ${e} produces different CBOR bytes than item 0`;
54
18
  if (n) n(t);
55
19
  else throw SyntaxError(t);
56
20
  }
57
21
  return r;
58
22
  }
59
- }, M = class t {
60
- static OMIT = o;
61
- static TAG = s;
62
- static Tag = r;
63
- static Simple = a;
64
- static MapEntries = S;
65
- static dt_as_Date = p;
23
+ }, O = /* @__PURE__ */ new Map(), k = 64;
24
+ function A(e) {
25
+ if (typeof e != "string") return e;
26
+ let n = O.get(e);
27
+ return n || (n = t(e), O.size >= k && O.delete(O.keys().next().value), O.set(e, n)), n;
28
+ }
29
+ function j(e, t, r) {
30
+ if (t) {
31
+ let i = t.validate(e, r);
32
+ if (!i.valid) throw new n(i.errors, i.warnings);
33
+ }
34
+ return e;
35
+ }
36
+ function M(e, t) {
37
+ return j(e, A(t?.cddl), t?.cddlValidationOptions);
38
+ }
39
+ var N = class t {
40
+ static OMIT = T;
41
+ static TAG = r;
42
+ static Tag = a;
43
+ static Simple = o;
44
+ static MapEntries = C;
45
+ static dt_as_Date = _;
66
46
  #e;
67
47
  constructor(e) {
68
48
  this.#e = e ?? {};
@@ -153,7 +133,7 @@ var j = {
153
133
  stringify(e, n, r) {
154
134
  if (typeof n == "function" || Array.isArray(n) || n === null || n === void 0 && r !== void 0) {
155
135
  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);
136
+ return n === null ? i.replacer = void 0 : (typeof n == "function" || Array.isArray(n)) && (i.replacer = n), r !== void 0 && (i.indent = B(r)), t.stringify(e, i);
157
137
  }
158
138
  return t.stringify(e, this.#t(n ?? void 0));
159
139
  }
@@ -161,73 +141,73 @@ var j = {
161
141
  return t.format(e, this.#t(n));
162
142
  }
163
143
  static fromCBOR(e, t) {
164
- return d(e, t);
144
+ return M(w(e, t), t);
165
145
  }
166
146
  static fromCDN(e, t) {
167
- return c(e, t);
147
+ return M(m(e, t), t);
168
148
  }
169
149
  static fromEDN(e, n) {
170
150
  return t.fromCDN(e, n);
171
151
  }
172
152
  static *fromHexDumpSeq(e, n) {
173
- let r = [], i = N(e).trim().split(/\s+/).filter(Boolean);
153
+ let r = [], i = P(e).trim().split(/\s+/).filter(Boolean);
174
154
  for (let e of i) if (/^[0-9A-Fa-f]{2}$/.test(e)) r.push(parseInt(e, 16));
175
155
  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
156
  else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);
177
157
  yield* t.fromCBORSeq(new Uint8Array(r), n);
178
158
  }
179
159
  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, {
160
+ let n = A(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;
161
+ for (; i < r.byteLength;) {
162
+ let e = w(r, {
183
163
  ...t,
184
- offset: r,
164
+ offset: i,
185
165
  allowTrailing: !0
186
166
  });
187
- yield e, r = e.end;
167
+ yield j(e, n, t?.cddlValidationOptions), i = e.end;
188
168
  }
189
169
  }
190
170
  static *fromCDNSeq(t, n) {
191
- let r = !!n?.preserveComments, i = 0, a = !0;
171
+ let r = A(n?.cddl), i = !!n?.preserveComments, a = 0, o = !0;
192
172
  for (;;) {
193
- let { offset: o, hadSeparator: s, commaOffset: l } = R(t, i, n, r ? a ? "all" : "after-newline" : "none");
194
- if (a && l >= 0) {
173
+ let { offset: s, hadSeparator: c, commaOffset: l } = z(t, a, n, i ? o ? "all" : "after-newline" : "none");
174
+ if (o && l >= 0) {
195
175
  let e = "leading comma in CDN sequence";
196
176
  if (n?.strict !== !1) throw SyntaxError(e);
197
- I(e, l, n);
177
+ L(e, l, n);
198
178
  }
199
- if (o >= t.length || r && L(t, o) && R(t, o, n).offset >= t.length) break;
200
- if (!a && !s) {
179
+ if (s >= t.length || i && R(t, s) && z(t, s, n).offset >= t.length) break;
180
+ if (!o && !c) {
201
181
  let e = "CDN sequence items must be separated by whitespace, comma, or comment";
202
182
  if (n?.strict !== !1) throw SyntaxError(e);
203
- I(e, o, n);
183
+ L(e, s, n);
204
184
  }
205
- i = o;
185
+ a = s;
206
186
  let u;
207
187
  try {
208
- u = c(t, {
188
+ u = m(t, {
209
189
  ...n,
210
- offset: i,
190
+ offset: a,
211
191
  allowTrailing: !0,
212
192
  _skipRS: !0
213
193
  });
214
194
  } catch (t) {
215
195
  if (n?.strict !== !1) throw t;
216
- I(t instanceof Error ? t.message : String(t), i, n, !0, t instanceof e ? t : void 0);
196
+ L(t instanceof Error ? t.message : String(t), a, n, !0, t instanceof e ? t : void 0);
217
197
  break;
218
198
  }
219
- yield u, i = u.end, a = !1;
199
+ yield j(u, r, n?.cddlValidationOptions), a = u.end, o = !1;
220
200
  }
221
201
  }
222
202
  static fromJS(e, t) {
223
- return b(e, t);
203
+ return M(x(e, t), t);
224
204
  }
225
205
  static fromHexDump(e, t) {
226
- let n = [], r = N(e).trim().split(/\s+/).filter(Boolean);
206
+ let n = [], r = P(e).trim().split(/\s+/).filter(Boolean);
227
207
  for (let e of r) if (/^[0-9A-Fa-f]{2}$/.test(e)) n.push(parseInt(e, 16));
228
208
  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
209
  else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);
230
- return d(new Uint8Array(n), t);
210
+ return M(w(new Uint8Array(n), t), t);
231
211
  }
232
212
  static decode(e, n) {
233
213
  return t.fromCBOR(e, n).toJS(n);
@@ -273,7 +253,14 @@ var j = {
273
253
  }
274
254
  i.push(e);
275
255
  }
276
- }, c = 0;
256
+ }, c = A(r?.cddl), l = [], u = [], d = (e) => {
257
+ if (!c) return;
258
+ let t = c.validate(e, r?.cddlValidationOptions);
259
+ l.push(...t.errors), t.warnings && u.push(...t.warnings);
260
+ }, f = c ? {
261
+ cddlErrors: l,
262
+ cddlWarnings: u
263
+ } : {}, p = 0;
277
264
  try {
278
265
  let e = r?.type ?? "cbor";
279
266
  if (e === "cdn") {
@@ -281,33 +268,36 @@ var j = {
281
268
  ...s,
282
269
  unresolvedExtension: r?.unresolvedExtension
283
270
  };
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++;
271
+ for (let r of t.fromCDNSeq(n, e)) p++, d(r);
272
+ } else if (e === "hex") for (let e of t.fromHexDumpSeq(n, s)) p++, d(e);
273
+ else for (let e of t.fromCBORSeq(n, s)) p++, d(e);
287
274
  } catch (e) {
288
275
  return {
289
276
  valid: !1,
290
- count: c,
277
+ count: p,
291
278
  warnings: i,
292
279
  hints: a,
293
- error: e instanceof Error ? e : Error(String(e))
280
+ error: e instanceof Error ? e : Error(String(e)),
281
+ ...f
294
282
  };
295
283
  }
296
284
  if (o) {
297
285
  let t = o.cause instanceof Error ? o.cause : new e(o.message, { offset: o.offset });
298
286
  return {
299
287
  valid: !1,
300
- count: c,
288
+ count: p,
301
289
  warnings: i,
302
290
  hints: a,
303
- error: t
291
+ error: t,
292
+ ...f
304
293
  };
305
294
  }
306
295
  return {
307
- valid: i.length === 0,
308
- count: c,
296
+ valid: i.length === 0 && l.length === 0,
297
+ count: p,
309
298
  warnings: i,
310
- hints: a
299
+ hints: a,
300
+ ...f
311
301
  };
312
302
  }
313
303
  static cborToCdn(e, n) {
@@ -327,81 +317,81 @@ var j = {
327
317
  }
328
318
  static stringify(e, t, n) {
329
319
  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);
320
+ let r = typeof t == "function" || Array.isArray(t) ? t : void 0, i = B(n);
331
321
  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 });
322
+ let t = v(e, r);
323
+ return t === void 0 || t === T ? void 0 : x(t).toCDN(i === void 0 ? void 0 : { indent: i });
334
324
  }
335
- return b(e).toCDN(i === void 0 ? void 0 : { indent: i });
325
+ return x(e).toCDN(i === void 0 ? void 0 : { indent: i });
336
326
  }
337
327
  let r = t;
338
328
  if (r?.replacer) {
339
- let t = _(e, r.replacer, r.extensions, r.undefinedOmits, r.builtinExtensions);
340
- if (t === void 0 || t === o) return;
329
+ let t = v(e, r.replacer, r.extensions, r.undefinedOmits, r.builtinExtensions);
330
+ if (t === void 0 || t === T) return;
341
331
  let { replacer: n, ...i } = r;
342
- return b(t, Object.keys(i).length > 0 ? i : void 0).toCDN(r);
332
+ return M(x(t, Object.keys(i).length > 0 ? i : void 0), r).toCDN(r);
343
333
  }
344
- return b(e, r).toCDN(r);
334
+ return M(x(e, r), r).toCDN(r);
345
335
  }
346
336
  static format(e, n) {
347
337
  return t.fromCDN(e, n).toCDN(n);
348
338
  }
349
339
  };
350
- function N(e) {
340
+ function P(e) {
351
341
  let t = "", n = 0;
352
342
  for (; n < e.length;) {
353
343
  let r = e[n], i = e[n + 1] ?? "";
354
344
  if (r === "-" && i === "-") {
355
- n = P(e, n + 2), t += " ";
345
+ n = F(e, n + 2), t += " ";
356
346
  continue;
357
347
  }
358
348
  if (r === "—") {
359
- n = P(e, n + 1), t += " ";
349
+ n = F(e, n + 1), t += " ";
360
350
  continue;
361
351
  }
362
352
  if (r === "#") {
363
- n = P(e, n + 1), t += " ";
353
+ n = F(e, n + 1), t += " ";
364
354
  continue;
365
355
  }
366
356
  if (r === "/" && i === "/") {
367
- n = P(e, n + 2), t += " ";
357
+ n = F(e, n + 2), t += " ";
368
358
  continue;
369
359
  }
370
360
  if (r === "/" && i === "*") {
371
361
  let r = e.indexOf("*/", n + 2);
372
362
  if (r < 0) throw SyntaxError("Unterminated comment in hex dump");
373
- t += F(e.slice(n, r + 2)), n = r + 2;
363
+ t += I(e.slice(n, r + 2)), n = r + 2;
374
364
  continue;
375
365
  }
376
366
  if (r === "/") {
377
367
  let r = e.indexOf("/", n + 1);
378
368
  if (r < 0) throw SyntaxError("Unterminated comment in hex dump");
379
- t += F(e.slice(n, r + 1)), n = r + 1;
369
+ t += I(e.slice(n, r + 1)), n = r + 1;
380
370
  continue;
381
371
  }
382
372
  t += r, n++;
383
373
  }
384
374
  return t;
385
375
  }
386
- function P(e, t) {
376
+ function F(e, t) {
387
377
  let n = e.indexOf("\n", t);
388
378
  return n < 0 ? e.length : n;
389
379
  }
390
- function F(e) {
380
+ function I(e) {
391
381
  return e.replace(/[^\r\n]/g, " ");
392
382
  }
393
- function I(e, t, n, r, i) {
383
+ function L(e, t, n, r, i) {
394
384
  let a = i?.offset ?? t, o = {
395
385
  message: e,
396
386
  offset: a
397
387
  };
398
388
  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
389
  }
400
- function L(e, t) {
390
+ function R(e, t) {
401
391
  let n = e[t];
402
392
  return n === "#" || n === "/";
403
393
  }
404
- function R(e, t, n, r = "none") {
394
+ function z(e, t, n, r = "none") {
405
395
  let i = t, a = !1, o = !1, s = !1, c = -1, l = () => r === "all" || r === "after-newline" && s;
406
396
  for (; i < e.length;) {
407
397
  let t = e[i];
@@ -431,7 +421,7 @@ function R(e, t, n, r = "none") {
431
421
  if (t < 0) {
432
422
  let t = "unterminated /* comment in CDN sequence";
433
423
  if (n?.strict !== !1) throw SyntaxError(t);
434
- I(t, i, n, !0), i = e.length;
424
+ L(t, i, n, !0), i = e.length;
435
425
  } else e.slice(i, t).includes("\n") && (s = !0), i = t + 2;
436
426
  continue;
437
427
  }
@@ -441,7 +431,7 @@ function R(e, t, n, r = "none") {
441
431
  if (t < 0) {
442
432
  let t = "unterminated / comment in CDN sequence";
443
433
  if (n?.strict !== !1) throw SyntaxError(t);
444
- I(t, i, n, !0), i = e.length;
434
+ L(t, i, n, !0), i = e.length;
445
435
  } else e.slice(i, t).includes("\n") && (s = !0), i = t + 1;
446
436
  continue;
447
437
  }
@@ -457,7 +447,7 @@ function R(e, t, n, r = "none") {
457
447
  commaOffset: c
458
448
  };
459
449
  }
460
- function z(e) {
450
+ function B(e) {
461
451
  if (typeof e == "number") {
462
452
  let t = Math.floor(Math.min(10, Math.max(0, e)));
463
453
  return t === 0 ? void 0 : t;
@@ -465,6 +455,6 @@ function z(e) {
465
455
  if (typeof e == "string") return e.slice(0, 10) || void 0;
466
456
  }
467
457
  //#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 };
458
+ export { h as BUILTIN_EXTENSIONS, N as CBOR, N as default, T as CBOR_OMIT, r as CBOR_TAG, n as CddlMismatchError, e as CdnSyntaxError, C as MapEntries, l as Null, o as Simple, a as Tag, c as Undefined, d as b1, s as b32, f as cri, b as dt, _ as dt_as_Date, u as float, i as h32, y as ilbs, S as ilts, p as ip, D as same, g as t1 };
469
459
 
470
460
  //# sourceMappingURL=index.js.map