JSPM

  • Created
  • Published
  • Downloads 1204553
  • Score
    100M100P100Q204307F
  • License MIT

Encode and parse CBOR documents.

Package Exports

  • cbor
  • cbor/lib/tagged
  • cbor/lib/utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cbor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Encode and parse CBOR documents.

Installation:

$ npm install cbor

Documentation:

See the full API documentation.

From the command line:

$ bin/json2cbor package.json > package.cbor
$ bin/cbor2json package.cbor
$ bin/cbor2diag package.cbor

Example:

var cbor = require('cbor');
var encoded = cbor.encode(true); // returns <Buffer d9>
cbor.decode(encoded, function(error, obj) {
  // error != null if there was an error
  // obj is the unpacked object
  assert.ok(obj === true);
});

Allows streaming as well:

var cbor = require('cbor');
var fs = require('fs');

var d = new cbor.Decoder();
d.on('complete', function(obj){
  console.log(obj);
});

var s = fs.createReadStream('foo');
s.pipe(d);

var d2 = new cbor.Decoder({input: '00', encoding: 'hex'});
d.on('complete', function(obj){
  console.log(obj);
});
d2.start(); // needed when you don't use the stream interface

And also a SAX-type mode (which the streaming mode wraps):

var cbor = require('cbor');
var fs = require('fs');

var parser = new cbor.Evented();

// `kind` is one of the following strings:
// 'value': an atomic value was detected
// 'array-first': the first element of an array
// 'array': an item after the first in an array
// 'key-first': the first key in a map
// 'key': a key other than the first in a map
// 'stream-first': the first item in an indefinite encoding
// 'stream': an item other than the first in an indefinite encoding
// null: the end of a top-level CBOR item

parser.on('value',function(val,tags,kind) {
  // An atomic item (not a map or array) was detected
  // `val`: the value
  // `tags`: an array of tags that preceded the list
  // `kind`: see above
  console.log(val);
});
parser.on('array-start', function(count,tags,kind) {
  // `count`: The number of items in the array.  -1 if indefinite length.
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('array-stop', function(count,tags,kind) {
  // `count`: The actual number of items in the array.
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('map-start', function(count,tags,kind) {
  // `count`: The number of pairs in the map.  -1 if indefinite length.
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('map-stop', function(count,tags,kind) {
  // `count`: The actual number of pairs in the map.
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('stream-start', function(mt,tags,kind) {
  // The start of a CBOR indefinite length bytestring or utf8-string.
  // `mt`: The major type for all of the items
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('stream-stop', function(count,mt,tags,kind) {
  // We got to the end of a CBOR indefinite length bytestring or utf8-string.
  // `count`: The number of constituent items
  // `mt`: The major type for all of the items
  // `tags`: An array of tags that preceded the list
  // `kind`: see above
});
parser.on('end', function() {
  // the end of the input
});
parser.on('error', function(er) {
  // parse error such as invalid input
});

var s = fs.createReadStream('foo');
s.pipe(parser);

Developers

For the moment, you'll need to manually install istanbul, nodeunit, and grunt-cli:

$ npm install -g grunt-cli nodeunit istanbul
$ grunt
Running "coffee:compile" (coffee) task

Running "nodeunit:all" (nodeunit) task
Testing BufferStream.test...............OK
Testing decoder.test.....OK
Testing diagnose.test...OK
Testing encoder.test.......OK
Testing evented.test....OK
Testing simple.test.OK
Testing tagged.test..OK
Testing utils.test.......OK
>> 459 assertions passed (129ms)

Done, without errors.

Build Status Coverage Status Dependency status Dev Dependency Status