bin-protocol
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -26,2 +26,12 @@ "use strict"; | ||
exports.read = function (buffer) { | ||
var reader = new exports.Reader(buffer); | ||
return reader; | ||
}; | ||
exports.write = function (size) { | ||
var writer = new exports.Writer(size); | ||
return writer; | ||
}; | ||
var primitives = [ | ||
@@ -28,0 +38,0 @@ ['Int8', 1], |
@@ -72,2 +72,3 @@ "use strict"; | ||
this.offset += bytes; | ||
return this; | ||
}; | ||
@@ -79,2 +80,3 @@ | ||
} | ||
return this; | ||
}; | ||
@@ -91,3 +93,3 @@ | ||
this.path.push(path + '[' + i++ + ']'); | ||
try{ | ||
try { | ||
fn.call(this, end); | ||
@@ -100,3 +102,4 @@ } catch (err) { | ||
} | ||
return this; | ||
}; | ||
@@ -29,2 +29,3 @@ "use strict"; | ||
} | ||
return this; | ||
}; | ||
@@ -34,2 +35,3 @@ | ||
this.offset += bytes; | ||
return this; | ||
}; | ||
@@ -52,2 +54,4 @@ | ||
} | ||
return this; | ||
}; | ||
@@ -57,2 +61,3 @@ | ||
this.offset = 0; | ||
return this; | ||
}; | ||
@@ -59,0 +64,0 @@ |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": ["buffer"], |
@@ -1,6 +0,7 @@ | ||
# binary-protocol | ||
# bin-protocol | ||
[](https://travis-ci.org/oleksiyk/binary-protocol) | ||
binary-protocol is a library for parsing and creating arbitrary byte buffers. It was primary created for Apache Kafka protocol. | ||
bin-protocol is a library for parsing and creating arbitrary byte buffers. It was created for [Apache Kafka client](https://github.com/oleksiyk/kafka). | ||
You can build your own type definitions based on the following methods: | ||
@@ -23,3 +24,3 @@ | ||
Int64 support (using long.js): | ||
Int64 support (using [long.js](https://github.com/dcodeIO/long.js)): | ||
* Int64BE | ||
@@ -59,16 +60,2 @@ * Int64LE | ||
var reader = new protocol.Reader(new Buffer([0, 1, 2, 3])); | ||
reader | ||
.Int8('num1') | ||
.Int8('num2') | ||
.Int8('num3') | ||
.Int8('num4'); | ||
console.log(reader.result); // => { num1: 0, num2: 1, num3: 2, num4: 3 } | ||
``` | ||
```javascript | ||
var protocol = require('bin-protocol'); | ||
protocol.define('char', { | ||
@@ -75,0 +62,0 @@ read: function () { |
20
test.js
"use strict"; | ||
var protocol = require('./lib/index'); | ||
// var _ = require('lodash'); | ||
@@ -13,7 +14,17 @@ protocol.define('char', { | ||
protocol.define('array', { | ||
read: function () { | ||
read: function(primitive) { | ||
this | ||
.Int8('length') | ||
.loop('items', this.char, this.context.length); | ||
.loop('items', this[primitive], this.context.length); | ||
return this.context.items; | ||
}, | ||
write: function(value, primitive) { | ||
console.log(value, primitive); | ||
if (value === null || value === undefined) { | ||
this.Int8(-1); | ||
} else { | ||
this | ||
.Int8(value.length) | ||
.loop(value, this[primitive]); | ||
} | ||
} | ||
@@ -25,5 +36,8 @@ }); | ||
reader | ||
.array('chars'); | ||
.array('chars', 'char'); | ||
console.log(reader.result); // => { chars: [ 'a', 'b', 'c', 'd', 'e' ] } | ||
var writer = new protocol.Writer(); | ||
console.log(writer.array([1, 2, 3], 'Int8').result()); |
@@ -74,4 +74,3 @@ "use strict"; | ||
reader = new protocol.Reader(buffer); | ||
reader.Int32BE('a0').nested('nested').result.should.be.eql({ | ||
protocol.read(buffer).Int32BE('a0').nested('nested').result.should.be.eql({ | ||
a0: 3, | ||
@@ -117,4 +116,3 @@ nested: { | ||
reader = new protocol.Reader(buffer); | ||
reader.loopArray('items').result.should.be.eql({ | ||
protocol.read(buffer).loopArray('items').result.should.be.eql({ | ||
items: [ 2, 3, 4 ] | ||
@@ -140,4 +138,3 @@ }); | ||
reader = new protocol.Reader(buffer); | ||
reader.loopArrayEnd('items').result.should.be.eql({ | ||
protocol.read(buffer).loopArrayEnd('items').result.should.be.eql({ | ||
items: [ 2, 3, 4 ] | ||
@@ -166,3 +163,3 @@ }); | ||
it('should discard last empty context on RangeError', function () { | ||
it('methods should be chainable', function () { | ||
var reader, buffer = new Buffer('abcde'); | ||
@@ -178,11 +175,5 @@ | ||
reader = new protocol.Reader(buffer); | ||
expect(function () { | ||
reader.loop('chars', reader.char, 6); | ||
}).to.throw(RangeError); | ||
reader.result.should.be.eql({ | ||
chars: ['a', 'b', 'c', 'd', 'e'] | ||
}); | ||
reader.skip(1).demand(1).loop('chars', reader.char, 4).result.should.be.eql({chars: ['b', 'c', 'd', 'e']}); | ||
}); | ||
}); |
@@ -29,3 +29,3 @@ "use strict"; | ||
it('should write ' + p[0], function () { | ||
var writer, buffer = new Buffer(2 * p[1]), num1, num2; | ||
var buffer = new Buffer(2 * p[1]), num1, num2; | ||
if(p[0].indexOf('U') !== 0){ // signed | ||
@@ -39,4 +39,3 @@ num1 = -123; num2 = 123; | ||
writer = new protocol.Writer(); | ||
writer[p[0]](num1)[p[0]](num2).result().should.be.eql(buffer); | ||
protocol.write()[p[0]](num1)[p[0]](num2).result().should.be.eql(buffer); | ||
}); | ||
@@ -46,6 +45,5 @@ }); | ||
it('should write raw bytes from buffer', function () { | ||
var writer, buffer = new Buffer([1, 2, 3, 4]); | ||
var buffer = new Buffer([1, 2, 3, 4]); | ||
writer = new protocol.Writer(); | ||
writer.raw(buffer).result().toJSON().should.be.eql([1, 2, 3, 4]); | ||
protocol.write().raw(buffer).result().toJSON().should.be.eql([1, 2, 3, 4]); | ||
}); | ||
@@ -134,2 +132,15 @@ | ||
it('methods should be chainable', function () { | ||
var writer; | ||
protocol.define('char', { | ||
write: function (char) { | ||
this.Int8(char.charCodeAt(0)); | ||
} | ||
}); | ||
writer = new protocol.Writer(); | ||
writer.skip(0).demand(1).loop(['a', 'b', 'c', 'd'], writer.char).result().toJSON().should.be.eql([97, 98, 99, 100]); | ||
}); | ||
}); |
60160
545
141