Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cbor

Package Overview
Dependencies
Maintainers
2
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cbor - npm Package Compare versions

Comparing version 0.0.1 to 0.3.0

.codoopts

147

lib/cbor.js

@@ -1,144 +0,21 @@

var Int64 = require('node-int64');
var _ = require('lodash');
// Generated by CoffeeScript 1.7.1
(function() {
exports.Diagnose = require('./diagnose');
function isInteger(value) {
return parseFloat(value) == parseInt(value, 10) && !isNaN(value);
};
exports.Decoder = require('./decoder');
function encodeCbor(value, writableStream, done) {
writeValue(value, writableStream);
//TODO: use 'drain' event
if (done) {
done();
}
};
exports.Encoder = require('./encoder');
function encodeSemanticCbor(tag, value, writableStream, done) {
writableStream.write(new Buffer([tag | 6 << 5]));
writeValue(value, writableStream);
//TODO: use 'drain' event
if (done) {
done();
}
};
exports.Simple = require('./simple');
var simpleValues = {
'undefined': 0xf7,
'null': 0xf6,
'true': 0xf5,
'false': 0xf4,
};
exports.Tagged = require('./tagged');
function writeValue(value, writableStream) {
if (_.isUndefined(value)) {
writableStream.write(new Buffer([simpleValues['undefined']]));
} else if (_.isNull(value)) {
writableStream.write(new Buffer([simpleValues['null']]));
} else if (_.isBoolean(value)) {
writeBoolean(value, writableStream);
} else if (_.isNumber(value)) {
writeNumber(value, writableStream);
} else if (_.isString(value)) {
writeString(value, writableStream);
} else if (Buffer.isBuffer(value)) {
writeByteString(value, writableStream);
} else if (_.isArray(value)) {
writeArray(value, writableStream);
} else {
writeObject(value, writableStream);
}
};
exports.Evented = require('./evented');
function writeBoolean(value, writableStream) {
if (value) {
writableStream.write(new Buffer([simpleValues['true']]));
} else {
writableStream.write(new Buffer([simpleValues['false']]));
}
};
exports.encode = exports.Encoder.encode;
function writeNumber(value, writableStream) {
if (isInteger(value)) {
if (value >= 0) {
writeInteger(value, 0, writableStream);
} else {
writeInteger(Math.abs(value) - 1, 1 << 5, writableStream);
}
} else {
writeFloat(value, writableStream);
}
};
exports.decode = exports.Decoder.decode;
function writeInteger(value, majorType, writableStream) {
if (value >= 0 && value < 24) {
writableStream.write(new Buffer([value | majorType]));
} else {
var buffer = new Buffer(0);
if (value <= 0xff) {
buffer = new Buffer(2);
buffer.writeUInt8(24 | majorType, 0);
buffer.writeUInt8(value, 1);
} else if (value <= 0xffff) {
buffer = new Buffer(3);
buffer.writeUInt8(25 | majorType, 0);
buffer.writeUInt16BE(value, 1);
} else if (value <= 0xffffffff) {
buffer = new Buffer(5);
buffer.writeUInt8(26 | majorType, 0);
buffer.writeUInt32BE(value, 1);
} else {
var int64 = new Int64(value);
buffer = new Buffer(9);
buffer.writeUInt8(27 | majorType, 0);
int64.buffer.copy(buffer, 1);
}
writableStream.write(buffer);
}
};
exports.diagnose = exports.Diagnose.diagnose;
function writeFloat(value, writableStream) {
//TODO: use appopriate precision
var buffer = new Buffer(9);
buffer.writeUInt8(27 | 7 << 5, 0);
buffer.writeDoubleBE(value, 1);
writableStream.write(buffer);
};
function writeByteString(buffer, writableStream) {
writeInteger(buffer.length, 2 << 5, writableStream);
writableStream.write(buffer);
};
function writeString(value, writableStream) {
var buffer = new Buffer(value);
writeInteger(buffer.length, 3 << 5, writableStream);
writableStream.write(buffer);
};
function writeArray(value, writableStream) {
writeInteger(value.length, 4 << 5, writableStream);
_.forEach(value, function (item) {
writeValue(item, writableStream);
})
};
function writeObject(value, writableStream) {
var keys = _.filter(_.keys(value), function (key) {
return !_.isFunction(value[key]);
});
keys = _.sortBy(keys);
writeInteger(keys.length, 5 << 5, writableStream);
_.forEach(keys, function (key) {
writeValue(key, writableStream);
writeValue(value[key], writableStream);
})
};
function decodeCbor(input, callback) {
callback(null, null);
};
module.exports.cbor = {
encode: encodeCbor,
encodeSemantic: encodeSemanticCbor
};
}).call(this);
{
"name": "cbor",
"version": "0.0.1",
"description": "NPM package for encoding and decoding CBOR (Concise Binary Object Representation) data.",
"main": "index.js",
"version": "0.3.0",
"description": "Encode and parse CBOR documents.",
"main": "./lib/cbor.js",
"repository" :
{
"type" : "git",
"url" : "http://github.com/hildjj/node-cbor.git"
},
"homepage": "http://hildjj.github.io/node-cbor/",
"directories": {
"lib": "lib"
},
"bin": {
"json2cbor": "./bin/json2cbor",
"cbor2json": "./bin/cbor2json"
},
"scripts": {
"test": "mocha test"
"test": "./node_modules/.bin/nodeunit test",
"prepublish": "coffee -c -o lib src"
},
"repository": "https://github.com/dotCypress/cbor",
"keywords": [
"CBOR"
"coap",
"cbor",
"json"
],
"author": {
"name": "Vitaly Domnikov",
"email": "dotcypress@gmail.com"
"name": "Joe Hildebrand",
"email": "joe-github@cursive.net"
},
"devDependencies": {
"coffee-script": "*",
"nodeunit": "*",
"covershot": "*",
"temp": "*",
"node-jsmeter": "git://github.com/hildjj/node-jsmeter.git"
},
"license": "MIT",
"readmeFilename": "README.md",
"devDependencies": {
"mocha": "~1.13.0",
"memorystream": "~0.2.0"
},
"dependencies": {
"lodash": "~2.4.1",
"node-int64": "~0.3.0"
"async": "~0.2.9",
"bignumber.js": "~1.0.1"
}
}

@@ -1,70 +0,119 @@

# cbor [![Build Status](https://secure.travis-ci.org/dotCypress/cbor.png?branch=master)](https://travis-ci.org/dotCypress/cbor)
Encode and parse [CBOR](http://tools.ietf.org/html/draft-bormann-coap-misc-24#appendix-D.6.2) documents.
NPM package for encoding and decoding CBOR (Concise Binary Object Representation) data. This
package implements CBOR as described in [RFC 7049](http://tools.ietf.org/html/rfc7049).
See the [documentation](http://hildjj.github.io/node-cbor/doc/) and [test results](http://hildjj.github.io/node-cbor/covershot/).
__Not ready for production__
Installation:
## Installation
If you have npm installed, you can simply type:
```
npm install cbor
$ npm install cbor
```
Or you can clone this repository using the git command:
From the command line:
```
git clone git://github.com/dotCypress/cbor.git
$ bin/json2cbor package.json > package.cbor
$ bin/cbor2json package.cbor
$ bin/cbor2diag package.cbor
```
## Usage
Some examples how to use cbor module.
Example:
```javascript
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);
});
```
### Encode
Allows streaming as well:
```js
cbor.encode(value, writableStream, callback);
```
Arguments:
* ```value``` - value for encode
* ```writableStream``` - [instance of writable stream](http://nodejs.org/api/stream.html#stream_class_stream_writable)
* ```callback``` - callback function
```javascript
var cbor = require('cbor');
var fs = require('fs');
#### Semantic encode
var d = new cbor.Decoder();
d.on('complete', function(obj){
console.log(obj);
});
```js
cbor.encodeSemantic(tag, value, writableStream, callback);
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
```
Arguments:
* ```tag``` - semantic tag (integer)
* ```value``` - value for encode
* ```writableStream``` - [instance of writable stream](http://nodejs.org/api/stream.html#stream_class_stream_writable)
* ```callback``` - callback function
And also a SAX-type mode (which the streaming mode wraps):
#### Samples
```javascript
var cbor = require('cbor');
var fs = require('fs');
```js
var cbor = require("cbor");
var writableStream = ...;
var cb = ...;
var parser = new cbor.Evented();
// simple encoding
cbor.encode(1, writableStream, cb); // integers
cbor.encode(-50, writableStream, cb); // integers
cbor.encode(1.1, writableStream, cb); // floats
cbor.encode([123, 567, 'hello'], writableStream, cb); // arrays
cbor.encode({'a': 1, 'b': 2}, writableStream, cb); // hashes
cbor.encode(new Buffer([1,2,3,4]), writableStream, cb); // binary data
// semantic encode
cbor.encodeSemantic(0, "2013-03-21T20:04:00Z", writableStream, cb);
```
// `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
## Known issues
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
});
* All floats will be serialized only as IEEE 754 Double-Precision Float (64 bits follow)
* Encode does't support indefinite-length values.
var s = fs.createReadStream('foo');
s.pipe(parser);
```
## License
MIT
Test coverage is currently above 95%.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc