Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).
$ npm install --save cborNOTE This package now requires node.js 4.1 or higher. If you want a version that works with older node.js versions, you can install like this:
npm install 'hildjj/node-cbor#node0' --saveSee the full API documentation.
From the command line:
$ bin/json2cbor package.json > package.cbor
$ bin/cbor2json package.cbor
$ bin/cbor2diag package.cbor
Example:
varcbor=require('cbor');varassert=require('assert');varencoded=cbor.encode(true);// returns <Buffer f5>cbor.decodeFirst(encoded,function(error,obj){// error != null if there was an error// obj is the unpacked objectassert.ok(obj===true);});// Use integers as keys?varm=newMap();m.set(1,2);encoded=cbor.encode(m);// <Buffer a1 01 02>Allows streaming as well:
varcbor=require('cbor');varfs=require('fs');vard=newcbor.Decoder();d.on('data',function(obj){console.log(obj);});vars=fs.createReadStream('foo');s.pipe(d);vard2=newcbor.Decoder({input: '00',encoding: 'hex'});d.on('data',function(obj){console.log(obj);});There is also support for synchronous decodes:
try {
console.log(cbor.decodeFirstSync('02')); // 2
console.log(cbor.decodeAllSync('0202')); // [2, 2]
} catch (e) {
// throws on invalid input
}
The sync encoding and decoding are exported as a
leveldb encoding, as
cbor.leveldb.
Get a list of build steps with npm run. I use npm run dev, which rebuilds,
runs tests, and refreshes a browser window with coverage metrics every time I
save a .coffee file. If you don't want to run the fuzz tests every time, set
a NO_GARBAGE environment variable:
env NO_GARBAGE=1 npm run dev


