packet-reader
Advanced tools
Comparing version 0.0.0 to 0.1.0
20
index.js
var assert = require('assert') | ||
var Reader = module.exports = function(codeSize) { | ||
var Reader = module.exports = function(headerSize) { | ||
this.offset = 0 | ||
this.lastChunk = false | ||
this.chunk = null | ||
//TODO - support code length > 1 | ||
this.codeSize = codeSize || 0 | ||
assert(this.codeSize < 2, 'pre-length code >1 not currently supported') | ||
this.headerSize = headerSize || 0 | ||
this.header = null | ||
assert(this.headerSize < 2, 'pre-length header of more than 1 byte length not currently supported') | ||
} | ||
@@ -30,15 +30,15 @@ | ||
Reader.prototype.read = function() { | ||
if(this.chunk.length < (this.codeSize + 4 + this.offset)) { | ||
if(this.chunk.length < (this.headerSize + 4 + this.offset)) { | ||
return this._save() | ||
} | ||
if(this.codeSize) { | ||
var code = this.chunk[this.offset] | ||
if(this.headerSize) { | ||
this.header = this.chunk[this.offset] | ||
} | ||
//read length of next item | ||
var length = this.chunk.readUInt32BE(this.offset + this.codeSize) | ||
var length = this.chunk.readUInt32BE(this.offset + this.headerSize) | ||
//next item spans more chunks than we have | ||
var remaining = this.chunk.length - (this.offset + 4 + this.codeSize) | ||
var remaining = this.chunk.length - (this.offset + 4 + this.headerSize) | ||
if(length > remaining) { | ||
@@ -48,3 +48,3 @@ return this._save() | ||
this.offset += (this.codeSize + 4) | ||
this.offset += (this.headerSize + 4) | ||
var result = this.chunk.slice(this.offset, this.offset + length) | ||
@@ -51,0 +51,0 @@ this.offset += length |
{ | ||
"name": "packet-reader", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Read binary packets...", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -94,1 +94,23 @@ var assert = require('assert') | ||
}) | ||
describe('1 length code', function() { | ||
beforeEach(function() { | ||
this.reader = new Reader(1) | ||
}) | ||
it('reads code', function() { | ||
this.reader.addChunk(Buffer([9, 0, 0, 0, 1, 1])) | ||
var result = this.reader.read() | ||
assert(result) | ||
assert.equal(this.reader.header, 9) | ||
assert.equal(result.length, 1) | ||
assert.equal(result[0], 1) | ||
}) | ||
it('is set on uncompleted read', function() { | ||
assert.equal(this.reader.header, null) | ||
this.reader.addChunk(Buffer([2, 0, 0, 0, 1])) | ||
assert.strictEqual(this.reader.read(), false) | ||
assert.equal(this.reader.header, 2) | ||
}) | ||
}) |
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
5680
146