Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).
npm install cbor-transpiledcbor
====
Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).
Installation:
------------
``bash`
$ npm install --save cbor
NOTE
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:
`bash`
npm install 'hildjj/node-cbor#node0' --save
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:
`javascript
var cbor = require('cbor');
var assert = require('assert');
var encoded = cbor.encode(true); // returns
cbor.decodeFirst(encoded, function(error, obj) {
// error != null if there was an error
// obj is the unpacked object
assert.ok(obj === true);
});
// Use integers as keys?
var m = new Map();
m.set(1, 2);
encoded = cbor.encode(m); //
`
Allows streaming as well:
`javascript
var cbor = require('cbor');
var fs = require('fs');
var d = new cbor.Decoder();
d.on('data', function(obj){
console.log(obj);
});
var s = fs.createReadStream('foo');
s.pipe(d);
var d2 = new cbor.Decoder({input: '00', encoding: 'hex'});
d.on('data', function(obj){
console.log(obj);
});
`
There is also support for synchronous decodes:
`javascript`
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.
The following types are supported for encoding:
* boolean
* number (including -0, NaN, and ±Infinity)
* string
* Array, Set (encoded as Array)
* Object (including null), Map
* undefined
* Buffer
* Date,
* RegExp
* url.URL
* bignumber
Decoding supports the above types, including the following CBOR tag numbers:
| Tag | Generated Type |
|-----|----------------|
| 0 | Date |
| 1 | Date |
| 2 | bignumber |
| 3 | bignumber |
| 4 | bignumber |
| 5 | bignumber |
| 32 | url.URL |
| 35 | RegExp |
There are several ways to add a new encoder:
This is the easiest approach, if you can modify the class being encoded. Add an
encodeCBOR method to your class, which takes a single parameter of the encodertrue
currently being used. Your method should return on success, else false.encoder.push(buffer)
Your method may call or encoder.pushAny(any) as needed.
For example:
`javascript`
class Foo {
constructor () {
this.one = 1
this.two = 2
}
encodeCBOR (encoder) {
const tagged = new Tagged(64000, [this.one, this.two])
return encoder.pushAny(tagged)
}
}
You can also modify an existing type by monkey-patching an encodeCBOR function
onto its prototype, but this isn't recommended.
Sometimes, you want to support an existing type without modification to that
type. In this case, call addSemanticType(type, encodeFunction) on an existingEncoder instance. The encodeFunction takes an encoder and an object to
encode, for example:
`javascript`
class Bar {
constructor () {
this.three = 3
}
}
const enc = new Encoder()
enc.addSemanticType(Bar, (encoder, b) => {
encoder.pushAny(b.three)
})
Most of the time, you will want to add support for decoding a new tag type. If
the Decoder class encounters a tag it doesn't support, it will generate a Taggedtags
instance that you can handle or ignore as needed. To have a specific type
generated instead, pass a option to the Decoder's constructor, consistingFoo
of an object with tag number keys and function values. The function will be
passed the decoded value associated with the tag, and should return the decoded
value. For the example above, this might look like:
`javascript`
const d = new Decoder({tags: { 64000: (val) => {
// check val to make sure it's an Array as expected, etc.
const foo = new Foo()
foo.one = val[0]
foo.two = val[1]
return foo
}}})
Developers
----------
Get a list of build steps with npm run. I use npm run dev, which rebuilds,.coffee
runs tests, and refreshes a browser window with coverage metrics every time I
save a file. If you don't want to run the fuzz tests every time, setNO_GARBAGE
a environment variable:
```
env NO_GARBAGE=1 npm run dev


