Skip to content

Commit

Permalink
Update varint processing to use a tagged array instead of a sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
wes-smith committed Oct 16, 2024
1 parent 39ef6e3 commit b5aae56
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ function _getSuffix({cborldBytes}) {
let index = 3;
if(isModern && tagValue >= 128) {
// FIXME: this assumes tag length <= 31 bytes; throw error if not
const varintArrayLength = cborldBytes[index] % 32;
index += varintArrayLength + 1;
// cborldBytes[index + 1] is the header byte for the varint bytestring
const varintArrayLength = cborldBytes[index + 1] % 32;
// This sets `index` to the index of the first byte of the second
// array element in `cborldBytes`
index += varintArrayLength + 2;
}
const {buffer, byteOffset, length} = cborldBytes;
const suffix = new Uint8Array(buffer, byteOffset + index, length - index);
Expand Down
3 changes: 2 additions & 1 deletion lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ function _getPrefix({isLegacy, compressionMode, registryEntryId}) {
varintTagValue, varintByteValue
} = _getVarintStructure(registryEntryId);
if(varintByteValue) {
return [...varintTagValue, ...varintByteValue];
// define varintByteValue as first element in 2 element array
return [...varintTagValue, ...new Uint8Array([0x82]), ...varintByteValue];
}
return varintTagValue;

Expand Down
4 changes: 2 additions & 2 deletions tests/decode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('cborld decode', () => {
it('should decode empty JSON-LD document bytes with varint >1 byte',
async () => {
const cborldBytes = new Uint8Array(
[0xd9, 0x06, 0x80, 0x81, 0x01, 0xa0]);
[0xd9, 0x06, 0x80, 0x82, 0x41, 0x01, 0xa0]);
const jsonldDocument = await decode({cborldBytes});
expect(jsonldDocument).deep.equal({});
});

it('should decode an empty JSON-LD document with multiple byte varint',
async () => {
const cborldBytes = new Uint8Array(
[0xd9, 0x06, 0x80, 0x44, 0x94, 0xeb, 0xdc, 0x03, 0xa0]);
[0xd9, 0x06, 0x80, 0x82, 0x44, 0x94, 0xeb, 0xdc, 0x03, 0xa0]);
const jsonldDocument = await decode({cborldBytes});
expect(jsonldDocument).deep.equal({});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/encode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('cborld encode', () => {
registryEntryId
});
expect(cborldBytes).instanceof(Uint8Array);
expect(cborldBytes).equalBytes('d906804101a0');
expect(cborldBytes).equalBytes('d90680824101a0');
});

it('should encode an empty JSON-LD document with multiple byte varint',
Expand All @@ -58,7 +58,7 @@ describe('cborld encode', () => {
registryEntryId
});
expect(cborldBytes).instanceof(Uint8Array);
expect(cborldBytes).equalBytes('d906804494ebdc03a0');
expect(cborldBytes).equalBytes('d90680824494ebdc03a0');
});

it('should encode xsd dateTime when using a prefix', async () => {
Expand Down

0 comments on commit b5aae56

Please sign in to comment.