From 3d802096095bde4d7bf40cdfa319f488286046f4 Mon Sep 17 00:00:00 2001 From: Warren Gallagher Date: Fri, 6 Jun 2025 15:22:52 -0400 Subject: [PATCH 1/2] feat: allow optional customization of cbor encode/decode options Add new functions to src/cbor/index that allow getting and setting of default cbor encode/decode options. Enhance cbor tests to demonstrate successful use in how maps get encoded. --- __tests__/cbor.tests.ts | 14 +++++++++++++- src/cbor/index.ts | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/__tests__/cbor.tests.ts b/__tests__/cbor.tests.ts index 8781dc1..c794473 100644 --- a/__tests__/cbor.tests.ts +++ b/__tests__/cbor.tests.ts @@ -1,6 +1,6 @@ import { hex } from 'buffer-tag'; -import { cborDecode, cborEncode, DataItem } from '../src/cbor'; +import { cborDecode, cborEncode, DataItem, getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from '../src/cbor'; describe('cbor', () => { it('should properly decode a nested map', () => { @@ -16,6 +16,18 @@ describe('cbor', () => { const decoded = cborDecode(encoded); const reEncode = cborEncode(decoded); expect(reEncode.toString('hex')).toBe(encoded.toString('hex')); + expect(encoded[3].toString(16)).toBe('b9'); // Large Map + }); + + it('should properly encoded and decoded maps using variableMapSize=true', () => { + const options = getCborEncodeDecodeOptions(); + options.variableMapSize = true; + setCborEncodeDecodeOptions(options); + const encoded = cborEncode(DataItem.fromData({ foo: 'baz' })); + const decoded = cborDecode(encoded); + const reEncode = cborEncode(decoded); + expect(reEncode.toString('hex')).toBe(encoded.toString('hex')); + expect(encoded[3].toString(16)).toBe('a1'); // Map with one item }); it('should properly encoded and decoded with arrays', () => { diff --git a/src/cbor/index.ts b/src/cbor/index.ts index 4cc0f5a..18fcc03 100644 --- a/src/cbor/index.ts +++ b/src/cbor/index.ts @@ -33,7 +33,7 @@ export class DateOnly extends Date { } } -const encoderDefaults: Options = { +let encoderDefaults: Options = { tagUint8Array: false, useRecords: false, mapsAsObjects: false, @@ -59,6 +59,14 @@ addExtension({ decode: (isoStringDate: any): Object => new DateOnly(isoStringDate), }); +export const getCborEncodeDecodeOptions = () : Options => { + return encoderDefaults; +}; + +export const setCborEncodeDecodeOptions = (options: Options) : void => { + encoderDefaults = options; +}; + export const cborDecode = ( input: Buffer | Uint8Array, options: Options = encoderDefaults, From 4b74bb8228e5836a47d3968288fa820780cd15cf Mon Sep 17 00:00:00 2001 From: Warren Gallagher Date: Mon, 9 Jun 2025 07:41:29 -0400 Subject: [PATCH 2/2] feat: add export of functions getCborEncodeDecodeOptions, setCborEncodeDecodeOptions from src/cbor/inde.ts to --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 9299221..0661f48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +export { getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from './cbor'; export { Verifier } from './mdoc/Verifier'; export { parse } from './mdoc/parser'; export { DataItem } from './cbor/DataItem';