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

bin-protocol

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bin-protocol - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

.eslintrc

93

lib/index.js

@@ -1,7 +0,23 @@

"use strict";
'use strict';
var Long = require('long');
var primitives = [
['Int8', 1],
['UInt8', 1],
['Int16LE', 2],
['UInt16LE', 2],
['Int16BE', 2],
['UInt16BE', 2],
['Int32LE', 4],
['Int32BE', 4],
['UInt32LE', 4],
['UInt32BE', 4],
['FloatLE', 4],
['FloatBE', 4],
['DoubleLE', 8],
['DoubleBE', 8]
];
module.exports = (function () {
[

@@ -18,6 +34,6 @@ 'Reader',

exports.define = function (name, config) {
if(config.read){
if (config.read) {
exports.Reader.define(name, config.read);
}
if(config.write){
if (config.write) {
exports.Writer.define(name, config.write);

@@ -37,19 +53,2 @@ }

var primitives = [
['Int8', 1],
['UInt8', 1],
['Int16LE', 2],
['UInt16LE', 2],
['Int16BE', 2],
['UInt16BE', 2],
['Int32LE', 4],
['Int32BE', 4],
['UInt32LE', 4],
['UInt32BE', 4],
['FloatLE', 4],
['FloatBE', 4],
['DoubleLE', 8],
['DoubleBE', 8]
];
primitives.forEach(function (p) {

@@ -63,4 +62,5 @@ exports.define(p[0], {

write: function (value) {
var r;
this.demand(p[1]);
var r = this.buffer['write' + p[0]](value, this.offset);
r = this.buffer['write' + p[0]](value, this.offset);
this.offset += p[1];

@@ -74,4 +74,5 @@ return r;

read: function (bytes) {
var r;
this.demand(bytes);
var r = new Buffer(bytes);
r = new Buffer(bytes);
this.buffer.copy(r, 0, this.offset, this.offset + bytes);

@@ -82,3 +83,3 @@ this.offset += bytes;

write: function (buffer) {
if(typeof buffer === 'string' || Array.isArray(buffer)){
if (typeof buffer === 'string' || Array.isArray(buffer)) {
buffer = new Buffer(buffer);

@@ -100,6 +101,6 @@ }

var long;
if(typeof value === 'number'){
if (typeof value === 'number') {
long = Long.fromNumber(value);
} else {
long = Long.fromString(value + '');
long = Long.fromString(value + '');
}

@@ -120,6 +121,6 @@ this.buffer.writeInt32BE(long.getHighBits(), this.offset);

var long;
if(typeof value === 'number'){
if (typeof value === 'number') {
long = Long.fromNumber(value);
} else {
long = Long.fromString(value + '');
long = Long.fromString(value + '');
}

@@ -132,2 +133,38 @@ this.buffer.writeInt32LE(long.getHighBits(), this.offset + 4);

exports.define('UInt64BE', {
read: function () {
var l = new Long(this.buffer.readUInt32BE(this.offset + 4), this.buffer.readUInt32BE(this.offset), true);
this.offset += 8;
return l;
},
write: function (value) {
var long;
if (typeof value === 'number') {
long = Long.fromNumber(value, true);
} else {
long = Long.fromString(value + '', true);
}
this.buffer.writeUInt32BE(long.getHighBitsUnsigned(), this.offset);
this.buffer.writeUInt32BE(long.getLowBitsUnsigned(), this.offset + 4);
this.offset += 8;
}
});
exports.define('UInt64LE', {
read: function () {
var l = new Long(this.buffer.readUInt32LE(this.offset), this.buffer.readUInt32LE(this.offset + 4), true);
this.offset += 8;
return l;
},
write: function (value) {
var long;
if (typeof value === 'number') {
long = Long.fromNumber(value, true);
} else {
long = Long.fromString(value + '', true);
}
this.buffer.writeUInt32LE(long.getHighBitsUnsigned(), this.offset + 4);
this.buffer.writeUInt32LE(long.getLowBitsUnsigned(), this.offset);
this.offset += 8;
}
});

@@ -1,6 +0,6 @@

"use strict";
'use strict';
var _ = require('lodash');
function Reader (buffer){
function Reader(buffer) {
this.buffer = buffer;

@@ -44,5 +44,5 @@ this.offset = 0;

if(typeof path !== 'string'){ path = undefined }
if (typeof path !== 'string') { path = undefined; }
if(path){
if (path) {
this.path.push(path);

@@ -53,3 +53,3 @@ }

try{
try {
r = read.apply(this, Array.prototype.slice.call(arguments, 1));

@@ -59,3 +59,3 @@ } catch (err) {

} finally {
if(path){
if (path) {
this.path.pop();

@@ -65,3 +65,3 @@ }

if(r !== undefined){
if (r !== undefined) {
_.set(this.result, _path, r);

@@ -74,3 +74,3 @@ }

Reader.prototype.skip = function(bytes) {
Reader.prototype.skip = function (bytes) {
this.offset += bytes;

@@ -80,4 +80,4 @@ return this;

Reader.prototype.demand = function(bytes) {
if(this.offset + bytes > this.buffer.length){
Reader.prototype.demand = function (bytes) {
if (this.offset + bytes > this.buffer.length) {
throw new RangeError('Trying to access beyond buffer length');

@@ -88,3 +88,3 @@ }

Reader.prototype.loop = function(path, fn, iterations) {
Reader.prototype.loop = function (path, fn, iterations) {
var i = 0, _break = false;

@@ -96,3 +96,3 @@

while(!_break && (iterations !== undefined ? iterations-- : true)){
while (!_break && (iterations !== undefined ? iterations-- : true)) {
this.path.push(path + '[' + i++ + ']');

@@ -109,2 +109,1 @@ try {

};

@@ -1,6 +0,6 @@

"use strict";
'use strict';
var _ = require('lodash');
function Writer (bufferSize){
function Writer(bufferSize) {
this.buffer = new Buffer(bufferSize || 1024);

@@ -25,4 +25,4 @@ this.offset = 0;

Writer.prototype.demand = function(bytes) {
if(this.offset + bytes > this.buffer.length){
Writer.prototype.demand = function (bytes) {
if (this.offset + bytes > this.buffer.length) {
this._growBuffer(_.max([this.offset + bytes, this.buffer.length * 0.25]));

@@ -33,3 +33,3 @@ }

Writer.prototype.skip = function(bytes) {
Writer.prototype.skip = function (bytes) {
this.offset += bytes;

@@ -40,3 +40,3 @@ return this;

Writer.prototype.loop = function(values, fn, iterations) {
Writer.prototype.loop = function (values, fn, iterations) {
var _break = false, i = 0;

@@ -48,7 +48,7 @@

if(iterations === undefined){
if (iterations === undefined) {
iterations = values.length;
}
while(!_break && (iterations !== undefined ? iterations-- : true)){
while (!_break && (iterations !== undefined ? iterations-- : true)) {
fn.call(this, values[i++], end);

@@ -60,3 +60,3 @@ }

Writer.prototype.reset = function() {
Writer.prototype.reset = function () {
this.offset = 0;

@@ -66,4 +66,4 @@ return this;

Writer.prototype.result = function() {
Writer.prototype.result = function () {
return this.buffer.slice(0, this.offset);
};

@@ -9,3 +9,3 @@ {

},
"version": "1.0.5",
"version": "1.1.0",
"main": "./lib/index.js",

@@ -19,3 +19,6 @@ "keywords": ["buffer", "raw", "binary", "protocol", "parser", "reader", "builder"],

"mocha": "~=2.3.4",
"chai": "~=3.4.1"
"chai": "~=3.4.1",
"eslint": "^1.10.3",
"eslint-config-airbnb": "^3.1.0",
"istanbul": "^0.4.2"
},

@@ -22,0 +25,0 @@ "bugs": {

@@ -27,2 +27,4 @@ # bin-protocol

* Int64LE
* UInt64BE
* UInt64LE

@@ -29,0 +31,0 @@ Read or write raw bytes:

@@ -1,4 +0,4 @@

"use strict";
'use strict';
global.chai = require("chai");
global.chai = require('chai');

@@ -8,2 +8,1 @@ global.assert = global.chai.assert;

global.should = global.chai.should();

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -6,6 +6,6 @@ /* global expect, before, describe, it */

var protocol = require('../lib/index');
var Long = require('long');
describe('Reader', function() {
describe('primitives', function() {
describe('Reader', function () {
describe('primitives', function () {
var primitives = [

@@ -31,3 +31,3 @@ ['Int8', 1],

var reader, buffer = new Buffer(2 * p[1]), num1, num2;
if(p[0].indexOf('U') !== 0){ // signed
if (p[0].indexOf('U') !== 0) { // signed
num1 = -123; num2 = 123;

@@ -56,2 +56,25 @@ } else { // unsigned

describe('64 bits', function () {
var slong = new Long(0xFFFFFFFF, 0x7FFFFFFF), ulong = new Long(0xFFFFFFFF, 0x7FFFFFFF, true);
it('Int64BE', function () {
var buffer = new Buffer([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
protocol.read(buffer).Int64BE('long').result.long.toString().should.be.eql(slong.toString());
});
it('Int64LE', function () {
var buffer = new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
protocol.read(buffer).Int64LE('long').result.long.toString().should.be.eql(slong.toString());
});
it('UInt64BE', function () {
var buffer = new Buffer([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
protocol.read(buffer).UInt64BE('long').result.long.toString().should.be.eql(ulong.toString());
});
it('UInt64LE', function () {
var buffer = new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]);
protocol.read(buffer).UInt64LE('long').result.long.toString().should.be.eql(ulong.toString());
});
});
describe('nested objects and arrays', function () {

@@ -94,3 +117,3 @@ var reader, buffer = new Buffer(16);

for(i = 0; i<this.context.length; i++){
for (i = 0; i < this.context.length; i++) {
this.Int32BE('items[' + i + ']');

@@ -105,3 +128,3 @@ }

reader.customArray('items').result.should.be.eql({
items: [ 2, 3, 4 ]
items: [2, 3, 4]
});

@@ -121,3 +144,3 @@ });

protocol.read(buffer).loopArray('items').result.should.be.eql({
items: [ 2, 3, 4 ]
items: [2, 3, 4]
});

@@ -133,7 +156,5 @@ });

this.loop('items', function (end) {
this.Int32BE();
if((len -= 1) === 0){
end();
}
});
this.Int32BE();
if ((len -= 1) === 0) { end(); }
});
return this.context.items;

@@ -144,3 +165,3 @@ }

protocol.read(buffer).loopArrayEnd('items').result.should.be.eql({
items: [ 2, 3, 4 ]
items: [2, 3, 4]
});

@@ -179,5 +200,4 @@ });

reader = new protocol.Reader(buffer);
reader.skip(1).demand(1).loop('chars', reader.char, 4).result.should.be.eql({chars: ['b', 'c', 'd', 'e']});
reader.skip(1).demand(1).loop('chars', reader.char, 4).result.should.be.eql({ chars: ['b', 'c', 'd', 'e'] });
});
});

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -6,6 +6,6 @@ /* global describe, it */

var protocol = require('../lib/index');
var Long = require('long');
describe('Writer', function() {
describe('primitives', function() {
describe('Writer', function () {
describe('primitives', function () {
var primitives = [

@@ -31,3 +31,3 @@ ['Int8', 1],

var buffer = new Buffer(2 * p[1]), num1, num2;
if(p[0].indexOf('U') !== 0){ // signed
if (p[0].indexOf('U') !== 0) { // signed
num1 = -123; num2 = 123;

@@ -65,2 +65,48 @@ } else { // unsigned

describe('64 bits', function () {
var slong = new Long(0xFFFFFFFF, 0x7FFFFFFF),
ulong = new Long(0xFFFFFFFF, 0x7FFFFFFF, true),
MAX_NUM = 9007199254740991, MIN_NUM = -9007199254740991, writer;
it('Int64BE', function () {
writer = new protocol.Writer();
writer.Int64BE(slong).result().should.be.eql(new Buffer([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]));
});
it('Int64BE from number', function () {
writer = new protocol.Writer();
writer.Int64BE(MIN_NUM).result().should.be.eql(new Buffer([0XFF, 0XE0, 0X00, 0X00, 0X00, 0X00, 0X00, 0X01]));
});
it('Int64LE', function () {
writer = new protocol.Writer();
writer.Int64LE(slong).result().should.be.eql(new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]));
});
it('Int64LE from number', function () {
writer = new protocol.Writer();
writer.Int64LE(MIN_NUM).result().should.be.eql(new Buffer([0X01, 0X00, 0X00, 0X00, 0X00, 0X00, 0XE0, 0XFF]));
});
it('UInt64BE', function () {
writer = new protocol.Writer();
writer.UInt64BE(ulong).result().should.be.eql(new Buffer([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]));
});
it('UInt64BE from number', function () {
writer = new protocol.Writer();
writer.UInt64BE(MAX_NUM).result().should.be.eql(new Buffer([0X00, 0X1F, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF]));
});
it('UInt64LE', function () {
writer = new protocol.Writer();
writer.UInt64LE(ulong).result().should.be.eql(new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F]));
});
it('UInt64LE from number', function () {
writer = new protocol.Writer();
writer.UInt64LE(MAX_NUM).result().should.be.eql(new Buffer([0XFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF, 0X1F, 0X00]));
});
});
describe('arrays (loop)', function () {

@@ -76,3 +122,3 @@ var writer;

for(i = 0; i<values.length; i++){
for (i = 0; i < values.length; i++) {
this.Int32BE(values[i]);

@@ -121,3 +167,3 @@ }

this.Int32BE(value);
if(i++ === 1){
if (i++ === 1) {
end();

@@ -146,3 +192,2 @@ }

});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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