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

protodef

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protodef - npm Package Compare versions

Comparing version 0.2.6 to 0.3.0

2

dist/datatypes/conditional.js

@@ -10,2 +10,3 @@ 'use strict';

var tryDoc = _require.tryDoc;
var PartialReadError = _require.PartialReadError;

@@ -76,2 +77,3 @@ module.exports = {

function readOption(buffer, offset, typeArgs, context) {
if (buffer.length < offset + 1) throw new PartialReadError();
var val = buffer.readUInt8(offset++);

@@ -78,0 +80,0 @@ if (val !== 0) {

10

dist/datatypes/numeric.js

@@ -1,5 +0,9 @@

"use strict";
'use strict';
var _Object$keys = require("babel-runtime/core-js/object/keys")["default"];
var _Object$keys = require('babel-runtime/core-js/object/keys')['default'];
var _require = require('../utils');
var PartialReadError = _require.PartialReadError;
function readLong(buffer, offset) {

@@ -21,3 +25,3 @@ if (offset + 8 > buffer.length) return null;

var reader = function reader(buffer, offset) {
if (offset + size > buffer.length) return null;
if (offset + size > buffer.length) throw new PartialReadError();
var value = buffer[bufferReader](offset);

@@ -24,0 +28,0 @@ return {

@@ -34,3 +34,3 @@ 'use strict';

var c;
if (typeof count === "object") c = evalCount(count, rootNode);else if (typeof count !== "undefined") c = getField(count, rootNode);else if (typeof countType !== "undefined") {
if (typeof count === "number") c = count;else if (typeof count === "object") c = evalCount(count, rootNode);else if (typeof count !== "undefined") c = getField(count, rootNode);else if (typeof countType !== "undefined") {
var _tryDoc = tryDoc(function () {

@@ -37,0 +37,0 @@ return _this.read(buffer, offset, { type: countType, typeArgs: countTypeArgs }, rootNode);

@@ -11,2 +11,3 @@ 'use strict';

var tryDoc = _require.tryDoc;
var PartialReadError = _require.PartialReadError;

@@ -79,3 +80,3 @@ module.exports = {

while (true) {
if (cursor + 1 > buffer.length) return null;
if (cursor + 1 > buffer.length) throw new PartialReadError();
var b = buffer.readUInt8(cursor);

@@ -131,3 +132,3 @@ result |= (b & 0x7f) << shift; // Add the bits to our number, except MSB

var strEnd = cursor + value;
if (strEnd > buffer.length) throw new Error("Missing characters in string, found size is " + buffer.length + " expected size was " + strEnd);
if (strEnd > buffer.length) throw new PartialReadError("Missing characters in string, found size is " + buffer.length + " expected size was " + strEnd);

@@ -168,3 +169,3 @@ return {

function readBool(buffer, offset) {
if (offset + 1 > buffer.length) return null;
if (offset + 1 > buffer.length) throw new PartialReadError();
var value = buffer.readInt8(offset);

@@ -189,3 +190,3 @@ return {

var totalCount;
if (typeof count !== "undefined") totalCount = getField(count, rootNode);else if (typeof countType !== "undefined") {
if (typeof count === "number") totalCount = count;else if (typeof count !== "undefined") totalCount = getField(count, rootNode);else if (typeof countType !== "undefined") {
var _read2 = this.read(buffer, offset, { type: countType, typeArgs: countTypeArgs }, rootNode);

@@ -260,2 +261,3 @@

if (bits == 0) {
if (buffer.length < offset + 1) throw new PartialReadError();
curVal = buffer[offset++];

@@ -313,3 +315,3 @@ bits = 8;

while (offset < buffer.length && buffer[offset] != 0x00) str += buffer[offset++];
if (offset < buffer.length) return null;else return str;
if (offset < buffer.length) throw new PartialReadError();else return str;
}

@@ -316,0 +318,0 @@

@@ -102,13 +102,4 @@ 'use strict';

var typeFunctions = this.types[type];
if (!typeFunctions) {
return {
error: new Error("missing data type: " + type)
};
}
var readResults = typeFunctions[0].call(this, buffer, cursor, typeArgs, rootNodes);
if (readResults == null) {
throw new Error("Reader returned null : " + JSON.stringify({ type: type, typeArgs: typeArgs }));
}
if (readResults && readResults.error) return { error: readResults.error };
return readResults;
if (!typeFunctions) throw new Error("missing data type: " + type);
return typeFunctions[0].call(this, buffer, cursor, typeArgs, rootNodes);
}

@@ -124,7 +115,3 @@ }, {

var typeFunctions = this.types[type];
if (!typeFunctions) {
return {
error: new Error("missing data type: " + type)
};
}
if (!typeFunctions) throw new Error("missing data type: " + type);
return typeFunctions[1].call(this, value, buffer, offset, typeArgs, rootNode);

@@ -131,0 +118,0 @@ }

@@ -13,2 +13,6 @@ "use strict";

var _require = require('./utils');
var PartialReadError = _require.PartialReadError;
var Serializer = (function (_Transform) {

@@ -23,2 +27,3 @@ _inherits(Serializer, _Transform);

this.mainType = mainType;
this.queue = new Buffer(0);
}

@@ -56,2 +61,3 @@

this.mainType = mainType;
this.queue = new Buffer(0);
}

@@ -67,10 +73,13 @@

value: function _transform(chunk, enc, cb) {
var packet;
try {
packet = this.parsePacketBuffer(chunk);
} catch (e) {
return cb(e);
this.queue = Buffer.concat([this.queue, chunk]);
while (true) {
var packet;
try {
packet = this.parsePacketBuffer(this.queue);
this.push(packet);
this.queue = this.queue.slice(packet.metadata.size);
} catch (e) {
if (e instanceof PartialReadError) return cb();else return cb(e);
}
}
this.push(packet);
return cb();
}

@@ -77,0 +86,0 @@ }]);

"use strict";
module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
addErrorField: addErrorField,
tryCatch: tryCatch,
tryDoc: tryDoc
};
var _get = require("babel-runtime/helpers/get")["default"];
var _inherits = require("babel-runtime/helpers/inherits")["default"];
var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
function getField(countField, context) {

@@ -44,2 +42,38 @@ var countFieldArr = countField.split("/");

}
var ExtendableError = (function (_Error) {
_inherits(ExtendableError, _Error);
function ExtendableError(message) {
_classCallCheck(this, ExtendableError);
_get(Object.getPrototypeOf(ExtendableError.prototype), "constructor", this).call(this, message);
this.name = this.constructor.name;
this.message = message;
Error.captureStackTrace(this, this.constructor.name);
}
return ExtendableError;
})(Error);
var PartialReadError = (function (_ExtendableError) {
_inherits(PartialReadError, _ExtendableError);
function PartialReadError(message) {
_classCallCheck(this, PartialReadError);
_get(Object.getPrototypeOf(PartialReadError.prototype), "constructor", this).call(this, message);
}
return PartialReadError;
})(ExtendableError);
module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
addErrorField: addErrorField,
tryCatch: tryCatch,
tryDoc: tryDoc,
PartialReadError: PartialReadError
};
//# sourceMappingURL=maps/utils.js.map
{
"name": "protodef",
"version": "0.2.6",
"version": "0.3.0",
"description": "A simple yet powerful way to define binary protocols",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -114,2 +114,5 @@ # ProtoDef

### 0.3.0
* add partial packet support
### 0.2.6

@@ -116,0 +119,0 @@ * add compareToValue (optional) option to switch

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

var { getField, getFieldInfo, tryDoc} = require('../utils');
var { getField, getFieldInfo, tryDoc, PartialReadError} = require('../utils');

@@ -40,2 +40,4 @@ module.exports = {

function readOption(buffer, offset, typeArgs, context) {
if(buffer.length<offset+1)
throw new PartialReadError();
var val = buffer.readUInt8(offset++);

@@ -42,0 +44,0 @@ if (val !== 0) {

@@ -0,1 +1,3 @@

var { PartialReadError} = require('../utils');
function readLong(buffer, offset) {

@@ -19,3 +21,4 @@ if(offset + 8 > buffer.length) return null;

{
if(offset + size > buffer.length) return null;
if(offset + size > buffer.length)
throw new PartialReadError();
var value = buffer[bufferReader](offset);

@@ -22,0 +25,0 @@ return {

@@ -22,3 +22,5 @@ var { getField, tryDoc } = require("../utils");

var c;
if(typeof count === "object")
if(typeof count === "number")
c = count;
else if(typeof count === "object")
c = evalCount(count, rootNode);

@@ -25,0 +27,0 @@ else if (typeof count !== "undefined")

var assert = require('assert');
var { getField, tryDoc } = require("../utils");
var { getField, tryDoc, PartialReadError } = require("../utils");

@@ -61,3 +61,4 @@ module.exports = {

while(true) {
if(cursor + 1 > buffer.length) return null;
if(cursor + 1 > buffer.length)
throw new PartialReadError();
var b = buffer.readUInt8(cursor);

@@ -102,3 +103,3 @@ result |= ((b & 0x7f) << shift); // Add the bits to our number, except MSB

var strEnd = cursor + value;
if(strEnd > buffer.length) throw new Error("Missing characters in string, found size is "+buffer.length+
if(strEnd > buffer.length) throw new PartialReadError("Missing characters in string, found size is "+buffer.length+
" expected size was "+strEnd);

@@ -127,3 +128,3 @@

function readBool(buffer, offset) {
if(offset + 1 > buffer.length) return null;
if(offset + 1 > buffer.length) throw new PartialReadError();
var value = buffer.readInt8(offset);

@@ -145,3 +146,5 @@ return {

var totalCount;
if (typeof count !== "undefined")
if(typeof count === "number")
totalCount = count;
else if (typeof count !== "undefined")
totalCount = getField(count, rootNode);

@@ -203,2 +206,4 @@ else if (typeof countType !== "undefined") {

if (bits == 0) {
if(buffer.length<offset+1)
throw new PartialReadError();
curVal = buffer[offset++];

@@ -259,3 +264,3 @@ bits = 8;

if (offset < buffer.length)
return null;
throw new PartialReadError();
else

@@ -262,0 +267,0 @@ return str;

@@ -81,13 +81,5 @@ var { getFieldInfo, tryCatch } = require('./utils');

var typeFunctions = this.types[type];
if(!typeFunctions) {
return {
error: new Error("missing data type: " + type)
};
}
var readResults = typeFunctions[0].call(this, buffer, cursor, typeArgs, rootNodes);
if(readResults == null) {
throw new Error("Reader returned null : " + JSON.stringify({type,typeArgs}));
}
if(readResults && readResults.error) return {error: readResults.error};
return readResults;
if(!typeFunctions)
throw new Error("missing data type: " + type);
return typeFunctions[0].call(this, buffer, cursor, typeArgs, rootNodes);
}

@@ -98,7 +90,4 @@

var typeFunctions = this.types[type];
if(!typeFunctions) {
return {
error: new Error("missing data type: " + type)
};
}
if(!typeFunctions)
throw new Error("missing data type: " + type);
return typeFunctions[1].call(this, value, buffer, offset, typeArgs, rootNode);

@@ -105,0 +94,0 @@ }

var Transform = require("readable-stream").Transform;
var { PartialReadError} = require('./utils');

@@ -8,2 +9,3 @@ class Serializer extends Transform {

this.mainType=mainType;
this.queue=new Buffer(0);
}

@@ -31,2 +33,3 @@

this.mainType=mainType;
this.queue=new Buffer(0);
}

@@ -39,10 +42,16 @@

_transform(chunk, enc, cb) {
var packet;
try {
packet = this.parsePacketBuffer(chunk);
} catch (e) {
return cb(e);
this.queue = Buffer.concat([this.queue, chunk]);
while(true) {
var packet;
try {
packet = this.parsePacketBuffer(this.queue);
this.push(packet);
this.queue=this.queue.slice(packet.metadata.size);
} catch (e) {
if (e instanceof PartialReadError)
return cb();
else
return cb(e);
}
}
this.push(packet);
return cb();
}

@@ -49,0 +58,0 @@ }

@@ -1,8 +0,1 @@

module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
addErrorField: addErrorField,
tryCatch: tryCatch,
tryDoc: tryDoc
};

@@ -45,1 +38,25 @@ function getField(countField, context) {

}
class ExtendableError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
this.message = message;
Error.captureStackTrace(this, this.constructor.name)
}
}
class PartialReadError extends ExtendableError {
constructor(message) {
super(message);
}
}
module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
addErrorField: addErrorField,
tryCatch: tryCatch,
tryDoc: tryDoc,
PartialReadError:PartialReadError
};
var expect = require('chai').expect;
var PartialReadError=require("../../").utils.PartialReadError;
var numeric = require('../../dist/datatypes/numeric');

@@ -219,4 +221,12 @@ var getReader = function(dataType) { return dataType[0]; };

});
it('Returns null if not enough data is provided', function() {
expect(reader(new Buffer(0), 0)).to.eql(null);
it('Throw a PartialReadError if not enough data is provided', function() {
try {
reader(new Buffer(0), 0);
}
catch (e) {
if(!e instanceof PartialReadError)
throw e;
return;
}
throw Error("no PartialReadError thrown");
});

@@ -223,0 +233,0 @@ it('Reads positive values', function() {

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

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

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