Skip to content

Commit

Permalink
Fix decoding of empty object.
Browse files Browse the repository at this point in the history
- Enable test to decode empty object.
- Map.entries doesn't take argument.
- Remove instanceof Map check since API requires a Map.
- Normalize data to a Buffer since 'cbor' library requires one.
- Normalize `cbor.decode` result value to a Map. It returns just an
  object when decoding an empty object. Simplifies later Map vs object
  usage.
  • Loading branch information
davidlehn committed Aug 5, 2020
1 parent 04b54cb commit 499759e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @digitalbazaar/cborld ChangeLog

## 1.0.1 - 2020-xx-xx

### Fixed
- Handle decoding of encoded empty object.

## 1.0.0 - 2020-07-23

### Note
Expand Down
4 changes: 2 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export function getJsonldContextUrls({jsonldDocument}) {
export function getCborldContextUrls({cborMap, appContextMap}) {
const contextUrls = [];

for(const [key, value] of cborMap.entries(cborMap)) {
if(cborMap instanceof Map && key === 1) {
for(const [key, value] of cborMap.entries()) {
if(key === 1) {
const contexts = Array.isArray(value) ? value : [value];
contexts.forEach(element => {
const encodedContext = new ContextCodec();
Expand Down
9 changes: 8 additions & 1 deletion lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ import util from 'util';
*/
export async function fromCborld({
cborldBytes, documentLoader, appContextMap, diagnose}) {
const cborMap = cbor.decode(cborldBytes);
// normalize bytes to a Buffer for 'cbor'
const _cborldBytes =
cborldBytes instanceof Buffer ? cborldBytes : Buffer.from(cborldBytes);
const cborMap = cbor.decode(_cborldBytes);
// normalize value to a Map
if(!(cborMap.value instanceof Map)) {
cborMap.value = new Map(Object.entries(cborMap.value));
}

let jsonldDocument = {};
if(cborMap.tag === 0x0500) {
Expand Down
14 changes: 8 additions & 6 deletions tests/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ global.should = chai.should();

import {
encode,
decode
decode,
documentLoader
} from '..';

describe('cborld', () => {
describe('encode', () => {
it('should encode an empty JSON-LD Document', async () => {
const jsonldDocument = {};
const cborldBytes = await encode({jsonldDocument});
expect(cborldBytes).to.equalBytes('d90501a0');
const cborldBytes = await encode({jsonldDocument, documentLoader});
expect(cborldBytes).instanceof(Uint8Array);
expect(cborldBytes).equalBytes('d90501a0');
});
it.skip('should encode a simple JSON-LD Document', async () => {
});
});
describe('decode', () => {
it.skip('should decode empty document CBOR-LD bytes', async () => {
it('should decode empty document CBOR-LD bytes', async () => {
const cborldBytes = new Uint8Array([0xd9, 0x05, 0x01, 0xa0]);
const jsonldDocument = await decode({cborldBytes});
expect(jsonldDocument).to.deepEqual({});
const jsonldDocument = await decode({cborldBytes, documentLoader});
expect(jsonldDocument).deep.equal({});
});
it.skip('should decode simple CBOR-LD bytes', async () => {
});
Expand Down

0 comments on commit 499759e

Please sign in to comment.