Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions __tests__/cbor.tests.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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', () => {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/cbor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down