Skip to content

Commit e3833bb

Browse files
authored
Merge pull request #53 from auth0-lab/allow-control-over-default-cbor-encode-decode-options
feat: allow optional customization of cbor encode/decode options (author: @warren-gallagher)
2 parents 554bb91 + e64f939 commit e3833bb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

__tests__/cbor.tests.ts

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

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

55
describe('cbor', () => {
66
it('should properly decode a nested map', () => {
@@ -16,6 +16,18 @@ describe('cbor', () => {
1616
const decoded = cborDecode(encoded);
1717
const reEncode = cborEncode(decoded);
1818
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
19+
expect(encoded[3].toString(16)).toBe('b9'); // Large Map
20+
});
21+
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' }));
27+
const decoded = cborDecode(encoded);
28+
const reEncode = cborEncode(decoded);
29+
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
30+
expect(encoded[3].toString(16)).toBe('a1'); // Map with one item
1931
});
2032

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

src/cbor/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DateOnly extends Date {
3333
}
3434
}
3535

36-
const encoderDefaults: Options = {
36+
let encoderDefaults: Options = {
3737
tagUint8Array: false,
3838
useRecords: false,
3939
mapsAsObjects: false,
@@ -59,6 +59,14 @@ addExtension({
5959
decode: (isoStringDate: any): Object => new DateOnly(isoStringDate),
6060
});
6161

62+
export const getCborEncodeDecodeOptions = () : Options => {
63+
return encoderDefaults;
64+
};
65+
66+
export const setCborEncodeDecodeOptions = (options: Options) : void => {
67+
encoderDefaults = options;
68+
};
69+
6270
export const cborDecode = (
6371
input: Buffer | Uint8Array,
6472
options: Options = encoderDefaults,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export { DeviceSignedDocument } from './mdoc/model/DeviceSignedDocument';
99
export { DeviceResponse } from './mdoc/model/DeviceResponse';
1010
export { MDLError, MDLParseError } from './mdoc/errors';
1111
export { VerificationAssessmentId } from './mdoc/checkCallback';
12+
export { getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from './cbor';

0 commit comments

Comments
 (0)