@cbortech/cbor 0.27.1 → 0.27.2

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 (51) hide show
  1. package/README.ja.md +27 -0
  2. package/README.md +28 -0
  3. package/dist/ast/CborItem.d.ts +17 -2
  4. package/dist/ast/CborTag.d.ts +22 -0
  5. package/dist/ast/CborUnresolvedAppExt.d.ts +9 -0
  6. package/dist/ast/index.cjs +1 -1
  7. package/dist/ast/index.js +2 -2
  8. package/dist/cbor/tagLabels.d.ts +26 -0
  9. package/dist/cddl/eRefScope.d.ts +241 -0
  10. package/dist/cddl/eref.d.ts +64 -0
  11. package/dist/cddl/implicitTags.d.ts +28 -0
  12. package/dist/cddl/index.cjs +2 -2
  13. package/dist/cddl/index.cjs.map +1 -1
  14. package/dist/cddl/index.d.ts +2 -0
  15. package/dist/cddl/index.js +26 -25
  16. package/dist/cddl/index.js.map +1 -1
  17. package/dist/cddl/validator.d.ts +98 -2
  18. package/dist/cdn/index.cjs +1 -1
  19. package/dist/cdn/index.js +1 -1
  20. package/dist/cdn/serialize-utils.d.ts +20 -13
  21. package/dist/extensions/eref.d.ts +135 -0
  22. package/dist/index.cjs +7 -7
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +190 -81
  26. package/dist/index.js.map +1 -1
  27. package/dist/js/fromJS.d.ts +23 -1
  28. package/dist/mapEntries-8zshEr-t.js +7085 -0
  29. package/dist/mapEntries-8zshEr-t.js.map +1 -0
  30. package/dist/mapEntries-DwHPAzCN.cjs +68 -0
  31. package/dist/mapEntries-DwHPAzCN.cjs.map +1 -0
  32. package/dist/schema-7r2xjpMK.js +345 -0
  33. package/dist/schema-7r2xjpMK.js.map +1 -0
  34. package/dist/schema-YiigSWjR.cjs +8 -0
  35. package/dist/schema-YiigSWjR.cjs.map +1 -0
  36. package/dist/{serialize-utils-h-CVB9rg.js → serialize-utils-BQtutOo6.js} +14 -6
  37. package/dist/serialize-utils-BQtutOo6.js.map +1 -0
  38. package/dist/{serialize-utils-DhlW61ZX.cjs → serialize-utils-DQ8T3Mzw.cjs} +5 -5
  39. package/dist/serialize-utils-DQ8T3Mzw.cjs.map +1 -0
  40. package/dist/types.d.ts +153 -4
  41. package/package.json +8 -8
  42. package/dist/mapEntries-CCLaJSaJ.js +0 -4127
  43. package/dist/mapEntries-CCLaJSaJ.js.map +0 -1
  44. package/dist/mapEntries-CZZJScaj.cjs +0 -13
  45. package/dist/mapEntries-CZZJScaj.cjs.map +0 -1
  46. package/dist/schema-DN9inJny.js +0 -1977
  47. package/dist/schema-DN9inJny.js.map +0 -1
  48. package/dist/schema-zsg5yCPK.cjs +0 -63
  49. package/dist/schema-zsg5yCPK.cjs.map +0 -1
  50. package/dist/serialize-utils-DhlW61ZX.cjs.map +0 -1
  51. package/dist/serialize-utils-h-CVB9rg.js.map +0 -1
package/dist/types.d.ts CHANGED
@@ -146,11 +146,19 @@ export interface ToJSOptions {
146
146
  integerAs?: 'auto' | 'number' | 'bigint';
147
147
  /**
148
148
  * How to represent CBOR map values when converting to JavaScript.
149
- * - `'auto'`: text-string-only keys → `Record<string, unknown>`,
150
- * other key types `Map<unknown, unknown>`.
151
- * Duplicate keys are silently overwritten (last value wins).
149
+ * - `'auto'`: `Record<string, unknown>` when every key is a text string,
150
+ * or (with a CDDL `cddl` schema and `eRefKeys` not `false`) an e-ref
151
+ * annotated integer key that resolves to its name string — `Map<unknown,
152
+ * unknown>` for any other key type. Duplicate *text-string* keys are
153
+ * silently overwritten (last value wins), same as `'object'` below —
154
+ * but if a text-string key and an e-ref name key resolve to the same
155
+ * property string (e.g. `&(title: -1)` alongside a literal `"title"`
156
+ * key in the same map), `'auto'` falls back to `MapEntries` instead of
157
+ * silently dropping one of them.
152
158
  * - `'object'`: always `Record<string, unknown>` — non-string keys are
153
- * converted via `String()`. Duplicate keys are overwritten (last wins).
159
+ * converted via `String()`. Duplicate keys (including a text/e-ref
160
+ * collision as above) are overwritten (last wins) — unlike `'auto'`,
161
+ * `'object'` never falls back, since it was explicitly requested.
154
162
  * - `'entries'`: always `MapEntries` (a typed `Array` subclass) — preserves all
155
163
  * entries including duplicate keys (§2.4.2 of draft-ietf-cbor-edn-literals-27).
156
164
  * `fromJS()` recognises `MapEntries` instances and converts them back to `CborMap`.
@@ -167,6 +175,30 @@ export interface ToJSOptions {
167
175
  * @default false
168
176
  */
169
177
  stripTags?: boolean;
178
+ /**
179
+ * Whether a tag the CDDL schema itself implies is left off the JavaScript
180
+ * value — e.g. with `{ t: time }` (prelude `time = #6.1(number)`), the
181
+ * CBOR `{"t": 1(-14159024)}` (CDN `{"t": DT'1969-07-21T02:56:16Z'}`)
182
+ * converts to `{ t: -14159024 }` (a plain `number`) rather than a
183
+ * `Number` object carrying `Tag.symbol`.
184
+ *
185
+ * A tag counts as implied only where `FromJSOptions.implicitTags` would
186
+ * add it back when converting the result with the same schema, so
187
+ * `toJS()` → `fromJS()` still round-trips: it must be matched by a
188
+ * `#6.N(type)` with a literal tag number, and the untagged content must
189
+ * not be acceptable at that position on its own — under `time / number`
190
+ * a tagged `1(5)` keeps its tag, since plain `5` would stay untagged.
191
+ * Tags the schema doesn't require (`any`, `#6.<uint>(…)`, …) are always
192
+ * kept.
193
+ *
194
+ * Only takes effect for an item validated via `FromCBOROptions.cddl` /
195
+ * `FromCDNOptions.cddl` / `FromJSOptions.cddl`; without a schema, every
196
+ * tag is kept as before. Pass `false` to keep every tag even with one.
197
+ * `stripTags: true` removes all tags regardless.
198
+ *
199
+ * @default true
200
+ */
201
+ implicitTags?: boolean;
170
202
  /**
171
203
  * Post-conversion reviver function, applied bottom-up after the CBOR value
172
204
  * has been converted to JavaScript.
@@ -249,6 +281,50 @@ export interface ToJSOptions {
249
281
  * });
250
282
  */
251
283
  itemOptions?: (item: CborItem, ctx: ItemContext) => Partial<ToJSNodeOptions> | undefined;
284
+ /**
285
+ * Whether a map key annotated by CDDL `e'...'` external-reference
286
+ * resolution (draft-ietf-cbor-edn-e-ref) — an integer key the `cddl`
287
+ * schema names via a `&(name: value)` group entry at a map-member-key
288
+ * position, e.g. `? &(title: -1) => oltext` — converts to a plain-object
289
+ * key spelled as that name (`"title"`) instead of the integer itself. A
290
+ * general CDDL constant rule (`title = -1`) is not enough on its own to
291
+ * annotate a key this way — see `cddl/eref.ts`'s module doc — only
292
+ * `e'title'` written explicitly in CDN source resolves via a plain
293
+ * constant rule.
294
+ *
295
+ * Defaults to `true`: such a key exists at all only because some earlier
296
+ * `cddl`-validated decode/parse (or an `eRefKeys`-driven `fromJS()`, see
297
+ * `FromJSOptions.eRefKeys`) already named it, so leaving this option unset
298
+ * uses that name — pass `false` explicitly to fall back to the plain
299
+ * integer key instead (e.g. for code that reads `obj[-1]` and doesn't
300
+ * want it silently renamed to `obj.title`). A map with no CDDL-named keys
301
+ * at all is entirely unaffected either way — `mapAs: 'auto'`'s own choice
302
+ * between a plain object and `MapEntries` only ever treats a key as
303
+ * object-eligible via this option when the key is actually annotated;
304
+ * this is not a general "coerce integer keys to strings" switch.
305
+ *
306
+ * Only takes effect for a key CDDL validation has already annotated (see
307
+ * `FromCBOROptions.cddl` / `FromCDNOptions.cddl`); without a schema, or for
308
+ * a key the schema does not name this way, the key converts as usual.
309
+ * Annotation is unambiguous by construction (a name mapped to more than
310
+ * one value, or a value named more than once, in the same schema is
311
+ * excluded from annotation entirely — and a name ambiguous in either
312
+ * direction is excluded from *both*), so this never introduces a name
313
+ * collision on its own — though a name can still collide with an
314
+ * unrelated map's own genuine text-string key of the same spelling
315
+ * (e.g. `&(title: -1)` alongside a literal `"title"` key in the same
316
+ * map). `mapAs: 'auto'` treats that case differently from a plain
317
+ * duplicate text-string key: it detects the collision and falls back to
318
+ * `MapEntries` rather than silently dropping one side — see
319
+ * `ToJSOptions.mapAs`. An explicit `mapAs: 'object'` still overwrites
320
+ * (last value wins), the same as it does for any other non-injective key
321
+ * conversion. Only integer-valued names are annotated (see
322
+ * `cddl/eref.ts`'s Scope note) — a float, text, or byte-string map key is
323
+ * never affected.
324
+ *
325
+ * @default true
326
+ */
327
+ eRefKeys?: boolean;
252
328
  }
253
329
  export interface ToCBOROptions {
254
330
  }
@@ -694,6 +770,79 @@ export interface FromJSOptions {
694
770
  * Mirrors `FromCBOROptions.cddlValidationOptions`.
695
771
  */
696
772
  cddlValidationOptions?: CddlValidateOptions;
773
+ /**
774
+ * When not `false`, a plain object's property name that the `cddl` schema
775
+ * names — via a `&(name: value)` group entry at a map-member-key
776
+ * position (e.g. `? &(title: -1) => oltext`), or a bare reference to a
777
+ * constant rule at that same position — converts to that integer key
778
+ * instead of a text-string key, so `fromJS({ title: "oops" }, { cddl })`
779
+ * produces the same map `toJS()` on that map would read back as
780
+ * `{ title: "oops" }` (both options default to on, so neither needs
781
+ * spelling out explicitly to get a round-trip). The resulting key
782
+ * round-trips through `toCDN()` as `e'title'` the same way a
783
+ * schema-annotated key produced by `fromCBOR()`/`fromCDN()` does — only
784
+ * an unambiguous, integer-valued name is ever used this way (see
785
+ * `cddl/eref.ts`'s module doc).
786
+ *
787
+ * `ToJSOptions.eRefKeys` only ever renames an integer key some earlier
788
+ * step already labeled (`annotateERefKeys()`, run automatically after a
789
+ * successful `cddl`-validated decode/parse — or `e'name'` written
790
+ * explicitly in CDN source); this direction is different, since it
791
+ * *constructs* the key before any validation happens — so which names are
792
+ * even eligible is resolved **positionally**: only a name bound at a member-key position
793
+ * within the CDDL type actually governing *this* property's own position
794
+ * (starting from the schema's root rule, or `cddlValidationOptions.rule`
795
+ * when set, and descending through nested map types the same way this
796
+ * object's own nesting does) is used, never a name that merely exists
797
+ * *somewhere* in the schema. A property named `title` is therefore left
798
+ * as an ordinary text-string key unless the schema specifically names
799
+ * `title` at the exact position this object is being converted for —
800
+ * even if some unrelated, structurally unreachable rule elsewhere in the
801
+ * same schema happens to name `title` too (see `cddl/eRefScope.ts`'s own
802
+ * module doc, including its Scope note on what "governing type" this can
803
+ * and can't resolve).
804
+ *
805
+ * Only applies to a plain object's own properties (`Object.entries()`), not
806
+ * to a `MapEntries` key, which is preserved exactly as given (a native
807
+ * `Map` is not currently recognized by `fromJS()` at all — see its own
808
+ * doc — independent of `eRefKeys`) — a `MapEntries` entry's *value* still
809
+ * gets a nested scope when its own key is a string naming a known map
810
+ * property, the same way an ordinary property's value does, so a nested
811
+ * object reached this way still converts correctly; an array element
812
+ * never does, regardless. Without a schema, or for a property name the
813
+ * schema does not name this way at this exact position, the key converts as usual (a
814
+ * text-string key). Pass `false` explicitly to keep every property as a
815
+ * plain text-string key even when the schema would otherwise name one —
816
+ * e.g. for code that writes `{ title: "oops" }` and wants that spelled
817
+ * literally rather than resolved to `-1`. Requires `cddl` — has no effect
818
+ * on its own.
819
+ *
820
+ * @default true
821
+ */
822
+ eRefKeys?: boolean;
823
+ /**
824
+ * Whether a tag the CDDL schema requires is added to a value that lacks
825
+ * it — e.g. with `{ t: time }` (prelude `time = #6.1(number)`),
826
+ * `{ t: -14159024 }` (a plain `number`, no `Tag.symbol`) converts to
827
+ * `{"t": 1(-14159024)}` (CDN `{"t": DT'1969-07-21T02:56:16Z'}`). The
828
+ * mirror image of `ToJSOptions.implicitTags`.
829
+ *
830
+ * Inference is a fallback, never a rewrite of valid data: nothing is
831
+ * added when the converted value already matches the schema, and at
832
+ * every choice an alternative the value matches as-is wins — with
833
+ * `time / number`, a plain `5` stays `5`. Only a `#6.N(type)` with a
834
+ * literal tag number is inferred (the tag is created the same way a
835
+ * `Tag.symbol`-annotated value would be, so tag 1 becomes a `DT'…'`
836
+ * node); a value already carrying `Tag.symbol` is used as given. When
837
+ * even inference cannot make the value match, it is left unchanged and
838
+ * the usual `CddlMismatchError` is thrown.
839
+ *
840
+ * Requires `cddl` — has no effect on its own. Pass `false` to require
841
+ * every tag to be spelled out via `Tag.symbol`, as without a schema.
842
+ *
843
+ * @default true
844
+ */
845
+ implicitTags?: boolean;
697
846
  }
698
847
  /**
699
848
  * Context passed to a `toCDN()` `itemOptions` callback (see
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cbortech/cbor",
3
- "version": "0.27.1",
3
+ "version": "0.27.2",
4
4
  "description": "Convert between CBOR, CDN (CBOR-EDN), and JavaScript values with CDDL validation",
5
5
  "keywords": [
6
6
  "cbor",
@@ -93,10 +93,10 @@
93
93
  "start": "node dist/index.js",
94
94
  "dev": "npm run build && npm start",
95
95
  "clean": "node -e \"['dist','coverage'].forEach(d=>require('fs').rmSync(d,{recursive:true,force:true}))\"",
96
- "test": "vitest run --exclude src/cdn/edn-test-vectors.test.ts --exclude src/cdn/edn-abnf-vectors.test.ts --exclude src/cddl/cddl-corpus.test.ts --exclude src/cddl/cddl-validation-vectors.test.ts --exclude site/src/playground.browser.test.ts",
96
+ "test": "vitest run --exclude src/cdn/edn-test-vectors.test.ts --exclude src/cdn/edn-abnf-vectors.test.ts --exclude src/cddl/cddl-corpus.test.ts --exclude src/cddl/cddl-validation-vectors.test.ts --exclude 'site/src/**/*.browser.test.ts'",
97
97
  "test:exports": "npm run build && node scripts/check-package-exports.mjs",
98
98
  "fetch-test-vectors": "node scripts/fetch-test-vectors.mjs",
99
- "test:node": "vitest run --exclude src/cdn/edn-test-vectors.test.ts --exclude src/cdn/edn-abnf-vectors.test.ts --exclude src/cddl/cddl-corpus.test.ts --exclude src/cddl/cddl-validation-vectors.test.ts --exclude site/src/playground.browser.test.ts",
99
+ "test:node": "vitest run --exclude src/cdn/edn-test-vectors.test.ts --exclude src/cdn/edn-abnf-vectors.test.ts --exclude src/cddl/cddl-corpus.test.ts --exclude src/cddl/cddl-validation-vectors.test.ts --exclude 'site/src/**/*.browser.test.ts'",
100
100
  "test:edn-vectors": "vitest run src/cdn/edn-test-vectors.test.ts",
101
101
  "test:edn-abnf": "vitest run src/cdn/edn-abnf-vectors.test.ts",
102
102
  "test:cddl-corpus": "vitest run src/cddl/cddl-corpus.test.ts",
@@ -118,18 +118,18 @@
118
118
  "site:test": "npm run build && npm run --workspace site test"
119
119
  },
120
120
  "devDependencies": {
121
- "@types/node": "^24.13.3",
121
+ "@types/node": "^24.13.6",
122
122
  "@typescript/typescript6": "^6.0.2",
123
123
  "@vitest/browser": "^4.1.11",
124
124
  "@vitest/browser-playwright": "^4.1.11",
125
125
  "@vitest/coverage-v8": "^4.1.11",
126
126
  "@vitest/ui": "^4.1.11",
127
127
  "cross-env": "^10.1.0",
128
- "playwright": "^1.62.1",
129
- "prettier": "^3.9.6",
128
+ "playwright": "^1.63.0",
129
+ "prettier": "^3.9.8",
130
130
  "typescript": "^7.0.2",
131
- "vite": "^8.2.2",
132
- "vite-plugin-dts": "^5.0.3",
131
+ "vite": "^8.3.0",
132
+ "vite-plugin-dts": "^5.1.1",
133
133
  "vitest": "^4.1.11"
134
134
  }
135
135
  }