diff --git a/__tests__/cbor.tests.ts b/__tests__/cbor.tests.ts index c794473..0329d82 100644 --- a/__tests__/cbor.tests.ts +++ b/__tests__/cbor.tests.ts @@ -1,6 +1,6 @@ import { hex } from 'buffer-tag'; -import { cborDecode, cborEncode, DataItem, getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from '../src/cbor'; +import { cborDecode, cborEncode, DataItem } from '../src/cbor'; describe('cbor', () => { it('should properly decode a nested map', () => { @@ -11,23 +11,25 @@ describe('cbor', () => { expect(decoded.data.get('foo')?.data.get('bar')).toBe('baz'); }); - it('should properly encoded and decoded maps', () => { - const encoded = cborEncode(DataItem.fromData({ foo: 'baz' })); + it('should properly encoded and decoded maps (length <= 23)', () => { + const length = 23; + const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i])); + const encoded = cborEncode(DataItem.fromData(obj)); 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 + expect(encoded[4].toString(16)).toBe((0xA0 + length).toString(16)); }); - 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' })); + it('should properly encoded and decoded maps (length > 23)', () => { + const length = 24; + const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i])); + const encoded = cborEncode(DataItem.fromData(obj)); 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 + expect(encoded[4].toString(16)).toBe('b8'); + expect(encoded[5].toString(16)).toBe(length.toString(16)); }); it('should properly encoded and decoded with arrays', () => { diff --git a/package-lock.json b/package-lock.json index e771a42..0518aad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@panva/hkdf": "^1.1.1", "@peculiar/x509": "^1.9.5", "buffer": "^6.0.3", - "cbor-x": "^1.5.9", + "cbor-x": "^1.6.0", "compare-versions": "^6.0.0", "cose-kit": "^1.7.1", "debug": "^4.3.4", @@ -3391,9 +3391,9 @@ } }, "node_modules/cbor-x": { - "version": "1.5.9", - "resolved": "https://registry.npmjs.org/cbor-x/-/cbor-x-1.5.9.tgz", - "integrity": "sha512-OEI5rEu3MeR0WWNUXuIGkxmbXVhABP+VtgAXzm48c9ulkrsvxshjjk94XSOGphyAKeNGLPfAxxzEtgQ6rEVpYQ==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/cbor-x/-/cbor-x-1.6.0.tgz", + "integrity": "sha512-0kareyRwHSkL6ws5VXHEf8uY1liitysCVJjlmhaLG+IXLqhSaOO+t63coaso7yjwEzWZzLy8fJo06gZDVQM9Qg==", "license": "MIT", "optionalDependencies": { "cbor-extract": "^2.2.0" @@ -10664,9 +10664,9 @@ } }, "cbor-x": { - "version": "1.5.9", - "resolved": "https://registry.npmjs.org/cbor-x/-/cbor-x-1.5.9.tgz", - "integrity": "sha512-OEI5rEu3MeR0WWNUXuIGkxmbXVhABP+VtgAXzm48c9ulkrsvxshjjk94XSOGphyAKeNGLPfAxxzEtgQ6rEVpYQ==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/cbor-x/-/cbor-x-1.6.0.tgz", + "integrity": "sha512-0kareyRwHSkL6ws5VXHEf8uY1liitysCVJjlmhaLG+IXLqhSaOO+t63coaso7yjwEzWZzLy8fJo06gZDVQM9Qg==", "requires": { "cbor-extract": "^2.2.0" } diff --git a/package.json b/package.json index 9e61a36..f402c26 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "@panva/hkdf": "^1.1.1", "@peculiar/x509": "^1.9.5", "buffer": "^6.0.3", - "cbor-x": "^1.5.9", + "cbor-x": "^1.6.0", "compare-versions": "^6.0.0", "cose-kit": "^1.7.1", "debug": "^4.3.4", diff --git a/src/cbor/index.ts b/src/cbor/index.ts index 18fcc03..31d361e 100644 --- a/src/cbor/index.ts +++ b/src/cbor/index.ts @@ -39,6 +39,7 @@ let encoderDefaults: Options = { mapsAsObjects: false, // @ts-ignore useTag259ForMaps: false, + variableMapSize: true, }; // tdate data item shall contain a date-time string as specified in RFC 3339 (with no fraction of seconds)