Skip to content

Commit 50e51a0

Browse files
authored
Merge pull request #58 from auth0-lab/variable_map_size
fix(cbor): added `variableMapSize: true` option for cbor-x defaults
2 parents b7a2c4b + c8b5a36 commit 50e51a0

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

__tests__/cbor.tests.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hex } from 'buffer-tag';
22

3-
import { cborDecode, cborEncode, DataItem, getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from '../src/cbor';
3+
import { cborDecode, cborEncode, DataItem } from '../src/cbor';
44

55
describe('cbor', () => {
66
it('should properly decode a nested map', () => {
@@ -11,23 +11,25 @@ describe('cbor', () => {
1111
expect(decoded.data.get('foo')?.data.get('bar')).toBe('baz');
1212
});
1313

14-
it('should properly encoded and decoded maps', () => {
15-
const encoded = cborEncode(DataItem.fromData({ foo: 'baz' }));
14+
it('should properly encoded and decoded maps (length <= 23)', () => {
15+
const length = 23;
16+
const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i]));
17+
const encoded = cborEncode(DataItem.fromData(obj));
1618
const decoded = cborDecode(encoded);
1719
const reEncode = cborEncode(decoded);
1820
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
19-
expect(encoded[3].toString(16)).toBe('b9'); // Large Map
21+
expect(encoded[4].toString(16)).toBe((0xA0 + length).toString(16));
2022
});
2123

22-
it('should properly encoded and decoded maps using variableMapSize=true', () => {
23-
const options = getCborEncodeDecodeOptions();
24-
options.variableMapSize = true;
25-
setCborEncodeDecodeOptions(options);
26-
const encoded = cborEncode(DataItem.fromData({ foo: 'baz' }));
24+
it('should properly encoded and decoded maps (length > 23)', () => {
25+
const length = 24;
26+
const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i]));
27+
const encoded = cborEncode(DataItem.fromData(obj));
2728
const decoded = cborDecode(encoded);
2829
const reEncode = cborEncode(decoded);
2930
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
30-
expect(encoded[3].toString(16)).toBe('a1'); // Map with one item
31+
expect(encoded[4].toString(16)).toBe('b8');
32+
expect(encoded[5].toString(16)).toBe(length.toString(16));
3133
});
3234

3335
it('should properly encoded and decoded with arrays', () => {

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@panva/hkdf": "^1.1.1",
6969
"@peculiar/x509": "^1.9.5",
7070
"buffer": "^6.0.3",
71-
"cbor-x": "^1.5.9",
71+
"cbor-x": "^1.6.0",
7272
"compare-versions": "^6.0.0",
7373
"cose-kit": "^1.7.1",
7474
"debug": "^4.3.4",

src/cbor/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let encoderDefaults: Options = {
3939
mapsAsObjects: false,
4040
// @ts-ignore
4141
useTag259ForMaps: false,
42+
variableMapSize: true,
4243
};
4344

4445
// tdate data item shall contain a date-time string as specified in RFC 3339 (with no fraction of seconds)

0 commit comments

Comments
 (0)