bin-protocol
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -27,5 +27,3 @@ 'use strict'; | ||
function Protocol(options) { | ||
this.options = options || {}; | ||
} | ||
function Protocol() {} | ||
@@ -59,11 +57,11 @@ Protocol.Reader_ = Reader; | ||
function _Reader() { | ||
_Reader.super_.apply(this, arguments); | ||
Reader.apply(this, arguments); | ||
} | ||
function _Writer() { | ||
_Writer.super_.apply(this, arguments); | ||
Writer.apply(this, arguments); | ||
} | ||
function _P(options) { | ||
_P.super_.call(this, options); | ||
this.options = options || {}; | ||
@@ -85,2 +83,5 @@ this.reader = new _Reader(this.options); | ||
_Reader.prototype.__methods = _SuperProtocol.Reader_.prototype.__methods.slice(); | ||
_Writer.prototype.__methods = _SuperProtocol.Writer_.prototype.__methods.slice(); | ||
_P.define = function (name, config, namespace) { | ||
@@ -100,5 +101,2 @@ if (config.read) { | ||
module.exports = createProtocol(Protocol); | ||
module.exports.createProtobufProtocol = require('./protobuf'); | ||
primitives.forEach(function (p) { | ||
@@ -270,1 +268,4 @@ define(p[0], { | ||
}); | ||
module.exports = createProtocol(Protocol); | ||
module.exports.createProtobufProtocol = require('./protobuf'); |
@@ -9,4 +9,4 @@ 'use strict'; | ||
if (self._unbound_methods) { | ||
self._unbound_methods.forEach(function (opts) { | ||
if (self.__methods) { | ||
self.__methods.forEach(function (opts) { | ||
self.define.apply(self, opts); | ||
@@ -63,13 +63,8 @@ }); | ||
Reader.define = function (name, read, namespace, _proto) { | ||
var fname = namespace ? namespace + '.' + name : name; | ||
_proto = _proto || Reader; | ||
if (fname.indexOf('.') !== -1) { | ||
if (!_proto.prototype._unbound_methods) { | ||
_proto.prototype._unbound_methods = []; | ||
} | ||
_proto.prototype._unbound_methods.push([name, read, namespace]); | ||
} else { | ||
_.set(_proto.prototype, fname, _.partial(_read, name, read)); | ||
if (!_proto.prototype.__methods) { | ||
_proto.prototype.__methods = []; | ||
} | ||
_proto.prototype.__methods.push([name, read, namespace]); | ||
}; | ||
@@ -76,0 +71,0 @@ |
@@ -8,2 +8,4 @@ 'use strict'; | ||
self.id = _.uniqueId('writer_'); | ||
self.options = _.defaults(options || {}, { | ||
@@ -17,4 +19,4 @@ bufferSize: 8 * 1024, | ||
if (self._unbound_methods) { | ||
self._unbound_methods.forEach(function (opts) { | ||
if (self.__methods) { | ||
self.__methods.forEach(function (opts) { | ||
self.define.apply(self, opts); | ||
@@ -33,13 +35,8 @@ }); | ||
Writer.define = function (name, write, namespace, _proto) { | ||
var fname = namespace ? namespace + '.' + name : name; | ||
_proto = _proto || Writer; | ||
if (fname.indexOf('.') !== -1) { | ||
if (!_proto.prototype._unbound_methods) { | ||
_proto.prototype._unbound_methods = []; | ||
} | ||
_proto.prototype._unbound_methods.push([name, write, namespace]); | ||
} else { | ||
_.set(_proto.prototype, fname, _.partial(_write, name, write)); | ||
if (!_proto.prototype.__methods) { | ||
_proto.prototype.__methods = []; | ||
} | ||
_proto.prototype.__methods.push([name, write, namespace]); | ||
}; | ||
@@ -46,0 +43,0 @@ |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": ["buffer", "raw", "binary", "protocol", "parser", "reader", "builder"], |
@@ -7,107 +7,93 @@ 'use strict'; | ||
describe('custom protocols', function () { | ||
it('should be able to define new protocol methods on the global prototypes', function () { | ||
var protocol1 = new Protocol(); | ||
var protocol2 = new Protocol(); | ||
describe('Protocol definitions', function () { | ||
it('should be able to define new methods', function () { | ||
var protocol; | ||
protocol1.read().should.not.respondTo('gtest1'); | ||
protocol2.read().should.not.respondTo('gtest1'); | ||
protocol1.read().should.not.have.ownProperty('gtest1'); | ||
protocol2.read().should.not.have.ownProperty('gtest1'); | ||
Protocol.define('gtest1', { | ||
read: function () { | ||
this.Int8(); | ||
} | ||
read: function () {} | ||
}); | ||
protocol1.read().should.respondTo('gtest1'); | ||
protocol2.read().should.respondTo('gtest1'); | ||
protocol1.read().should.not.have.ownProperty('gtest1'); | ||
protocol2.read().should.not.have.ownProperty('gtest1'); | ||
protocol = new Protocol(); | ||
protocol.read().should.respondTo('gtest1'); | ||
}); | ||
it('should be able to define new protocol methods on the current instance only', function () { | ||
var protocol1 = new Protocol(); | ||
var protocol2 = new Protocol(); | ||
var buffer = new Buffer([0xff]); | ||
it('createProtocol() should call supplied function in constructor', function () { | ||
var protocol, | ||
Protocol1 = Protocol.createProtocol(function () { | ||
this.test = 'passed'; | ||
}); | ||
protocol1.read().should.not.respondTo('gtest2'); | ||
protocol2.read().should.not.respondTo('gtest2'); | ||
protocol1.read().should.not.have.ownProperty('gtest2'); | ||
protocol2.read().should.not.have.ownProperty('gtest2'); | ||
protocol = new Protocol1(); | ||
protocol1.define('gtest2', { | ||
read: function () { | ||
this.Int8(); | ||
} | ||
}); | ||
protocol.should.have.property('test', 'passed'); | ||
}); | ||
protocol2.define('gtest2', { | ||
read: function () { | ||
this.UInt8(); | ||
} | ||
it('new methods should be inherited with .createProtocol()', function () { | ||
var Protocol1, protocol; | ||
Protocol.define('gtest2', { | ||
read: function () {} | ||
}); | ||
protocol1.read().should.respondTo('gtest2'); | ||
protocol2.read().should.respondTo('gtest2'); | ||
protocol1.read().should.have.ownProperty('gtest2'); | ||
protocol2.read().should.have.ownProperty('gtest2'); | ||
Protocol1 = Protocol.createProtocol(); | ||
protocol1.read(buffer).gtest2().result.should.be.eql(-1); | ||
protocol2.read(buffer).gtest2().result.should.be.eql(255); | ||
protocol = new Protocol1(); | ||
protocol.read().should.respondTo('gtest2'); | ||
}); | ||
it('should be able to create new protocol type', function () { | ||
var MyProtocol = Protocol.createProtocol(); | ||
it('child methods should not be available in parent protocol', function () { | ||
var Protocol1, protocol1, protocol; | ||
MyProtocol.should.be.a('function'); | ||
Protocol1 = Protocol.createProtocol(); | ||
MyProtocol.should.respondTo('define'); | ||
Protocol1.define('gtest3', { | ||
read: function () {} | ||
}); | ||
protocol1 = new Protocol1(); | ||
protocol = new Protocol(); | ||
protocol1.read().should.respondTo('gtest3'); | ||
protocol.read().should.not.respondTo('gtest3'); | ||
}); | ||
it('should call contructor function with this binding in actual constructor', function () { | ||
var myConstructor = function () { | ||
this.myProperty = 'value'; | ||
}; | ||
it('methods with namespaces', function () { | ||
var Protocol1, protocol; | ||
var MyProtocol = Protocol.createProtocol(myConstructor); | ||
var myProtocol = new MyProtocol; | ||
Protocol1 = Protocol.createProtocol(); | ||
myProtocol.should.have.ownProperty('myProperty', 'value'); | ||
Protocol1.define('test1', { | ||
read: function () {} | ||
}, 'namespace'); | ||
protocol = new Protocol1(); | ||
protocol.read().namespace.should.respondTo('test1'); | ||
}); | ||
it('new protocol type - prototype methods', function () { | ||
var MyProtocol = Protocol.createProtocol(); | ||
var myprotocol1 = new MyProtocol(); | ||
var myprotocol2 = new MyProtocol(); | ||
var protocol = new Protocol(); | ||
it('child protocols should not share own methods', function () { | ||
var Protocol1, Protocol2, protocol1, protocol2; | ||
myprotocol1.read().should.not.respondTo('gtest3'); | ||
myprotocol2.read().should.not.respondTo('gtest3'); | ||
myprotocol1.read().should.not.have.ownProperty('gtest3'); | ||
myprotocol2.read().should.not.have.ownProperty('gtest3'); | ||
Protocol1 = Protocol.createProtocol(); | ||
Protocol2 = Protocol.createProtocol(); | ||
// should not have local methods of Protocol instances | ||
myprotocol1.read().should.not.respondTo('gtest2'); | ||
myprotocol1.read().should.not.have.ownProperty('gtest2'); | ||
Protocol1.define('test1', { | ||
read: function () {} | ||
}, 'test'); | ||
// it still should have methods defined on parent Protocol | ||
myprotocol1.read().should.respondTo('gtest1'); | ||
myprotocol1.read().should.not.have.ownProperty('gtest1'); | ||
Protocol2.define('test2', { | ||
read: function () {} | ||
}, 'test'); | ||
MyProtocol.define('gtest3', { | ||
read: function () { | ||
this.Int8('mp'); | ||
} | ||
}); | ||
protocol1 = new Protocol1(); | ||
protocol2 = new Protocol2(); | ||
myprotocol1.read().should.respondTo('gtest3'); | ||
myprotocol2.read().should.respondTo('gtest3'); | ||
myprotocol1.read().should.not.have.ownProperty('gtest3'); | ||
myprotocol2.read().should.not.have.ownProperty('gtest3'); | ||
protocol1.read().test.should.respondTo('test1'); | ||
protocol1.read().test.should.not.respondTo('test2'); | ||
protocol.read().should.not.respondTo('gtest3'); | ||
protocol.read().should.not.have.ownProperty('gtest3'); | ||
protocol2.read().test.should.respondTo('test2'); | ||
protocol2.read().test.should.not.respondTo('test1'); | ||
}); | ||
}); |
16
test2.js
'use strict'; | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Protocol = require('./lib/index'); | ||
var TestProtocol = Protocol.createProtobufProtocol(fs.readFileSync(path.join(__dirname, 'test/proto/basic.proto'))); | ||
var proto = fs.readFileSync('/Users/oleksiy/src/binary-protocol/test/proto/complex.proto'); | ||
var protocol = new TestProtocol(); | ||
var GameProtocol = Protocol.createProtobufProtocol(proto); | ||
// encode message | ||
var encoded = protocol.write().basic.Test({ | ||
string: 'hello' | ||
}).result; | ||
// decode message | ||
var decoded = protocol.read(encoded).basic.Test().result; | ||
console.log(decoded); | ||
module.exports = GameProtocol; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36
135310
2068