package/package.json000644 0000001045 3560116604 011546 0ustar00000000 000000 { "name": "packet-reader", "version": "1.0.0", "description": "Read binary packets...", "main": "index.js", "directories": { "test": "test" }, "scripts": { "test": "mocha" }, "repository": { "type": "git", "url": "git://github.com/brianc/node-packet-reader.git" }, "author": "Brian M. Carlson", "license": "MIT", "bugs": { "url": "https://github.com/brianc/node-packet-reader/issues" }, "homepage": "https://github.com/brianc/node-packet-reader", "devDependencies": { "mocha": "~1.21.5" } } package/.travis.yml000644 0000000155 3560116604 011372 0ustar00000000 000000 language: node_js node_js: "10" matrix: include: - node_js: "4" - node_js: "6" - node_js: "8" package/index.js000644 0000003401 3560116604 010723 0ustar00000000 000000 var assert = require('assert') var Reader = module.exports = function(options) { //TODO - remove for version 1.0 if(typeof options == 'number') { options = { headerSize: options } } options = options || {} this.offset = 0 this.lastChunk = false this.chunk = null this.chunkLength = 0 this.headerSize = options.headerSize || 0 this.lengthPadding = options.lengthPadding || 0 this.header = null assert(this.headerSize < 2, 'pre-length header of more than 1 byte length not currently supported') } Reader.prototype.addChunk = function(chunk) { if (!this.chunk || this.offset === this.chunkLength) { this.chunk = chunk this.chunkLength = chunk.length this.offset = 0 return } var newChunkLength = chunk.length var newLength = this.chunkLength + newChunkLength if (newLength > this.chunk.length) { var newBufferLength = this.chunk.length * 2 while (newLength >= newBufferLength) { newBufferLength *= 2 } var newBuffer = Buffer.alloc(newBufferLength) this.chunk.copy(newBuffer) this.chunk = newBuffer } chunk.copy(this.chunk, this.chunkLength) this.chunkLength = newLength } Reader.prototype.read = function() { if(this.chunkLength < (this.headerSize + 4 + this.offset)) { return false } if(this.headerSize) { this.header = this.chunk[this.offset] } //read length of next item var length = this.chunk.readUInt32BE(this.offset + this.headerSize) + this.lengthPadding //next item spans more chunks than we have var remaining = this.chunkLength - (this.offset + 4 + this.headerSize) if(length > remaining) { return false } this.offset += (this.headerSize + 4) var result = this.chunk.slice(this.offset, this.offset + length) this.offset += length return result } package/README.md000644 0000005171 3560116604 010543 0ustar00000000 000000 node-packet-reader ================== Handy little well tested module for reading length-prefixed binary packets. Since buffers come off a socket in randomly sized chunks you can't expect them to cleanly break on packet boundaries. This module allows you to push buffers in and read full packets out the other side, so you can get to parsing right away and not have to manage concatenating partial buffers and searching through them for packets. ## install ` $ npm install packet-reader ` ## example ```js var Reader = require('packet-reader') var reader = new Reader() //assuming you have a socket emitting `data` events socket.on('data', function(buffer) { reader.addChunk(buffer) var packet = reader.read() while(packet) { //do something with fully parsed packet } }) ``` here's a more full featured example: let's assume our "packet" for our protocol is 32-bit Big Endian length-prefixed strings so a "hello world" packet would look something like [length, string] `[0, 0, 0 0x0B, h, e, l, l, o, w, o, r, l, d]` ```js var Transform = require('stream').Transform var Reader = require('packet-reader') var reader = new Reader() var parser = new Transform() parser._transform = function(chunk, encoding, cb) { reader.addChunk(chunk) var packet = reader.read() while(packet) { this.push(packet.toString('utf8')) packet = reader.read() } cb() } var server = net.createServer(function(socket) { socket.pipe(parser).pipe(stdout) }) ``` There are a few config options for setting optional pre-length padding byte. Read the tests for details. ## License MIT Copyright 2015 Brian M. Carlson All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/test/index.js000644 0000011433 3560116604 011706 0ustar00000000 000000 var assert = require('assert') var Reader = require('../') describe('packet-reader', function() { beforeEach(function() { this.reader = new Reader(1) }) it('reads perfect 1 length buffer', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1])) var result = this.reader.read() assert.equal(result.length, 1) assert.equal(result[0], 1) assert.strictEqual(false, this.reader.read()) }) it('reads perfect longer buffer', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 4, 1, 2, 3, 4])) var result = this.reader.read() assert.equal(result.length, 4) assert.strictEqual(false, this.reader.read()) }) it('reads two parts', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1])) var result = this.reader.read() assert.strictEqual(false, result) this.reader.addChunk(Buffer.from([2])) var result = this.reader.read() assert.equal(result.length, 1, 'should return 1 length buffer') assert.equal(result[0], 2) assert.strictEqual(this.reader.read(), false) }) it('reads multi-part', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 16])) assert.equal(false, this.reader.read()) this.reader.addChunk(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8])) assert.equal(false, this.reader.read()) this.reader.addChunk(Buffer.from([9, 10, 11, 12, 13, 14, 15, 16])) var result = this.reader.read() assert.equal(result.length, 16) }) it('resets internal buffer at end of packet', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 16])) this.reader.addChunk(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8])) this.reader.addChunk(Buffer.from([9, 10, 11, 12, 13, 14, 15, 16])) var result = this.reader.read() assert.equal(result.length, 16) var newChunk = Buffer.from([0, 0, 0, 0, 16]) this.reader.addChunk(newChunk) assert.equal(this.reader.offset, 0, 'should have been reset to 0.') assert.strictEqual(this.reader.chunk, newChunk) }) it('reads multiple messages from single chunk', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 2, 1, 2])) var result = this.reader.read() assert.equal(result.length, 1, 'should have 1 length buffer') assert.equal(result[0], 1) var result = this.reader.read() assert.equal(result.length, 2, 'should have 2 length buffer but was ' + result.length) assert.equal(result[0], 1) assert.equal(result[1], 2) assert.strictEqual(false, this.reader.read()) }) it('reads 1 and a split', function() { this.reader.addChunk(Buffer.from([0, 0, 0, 0, 1, 1, 0, 0]))//, 0, 0, 2, 1, 2])) var result = this.reader.read() assert.equal(result.length, 1, 'should have 1 length buffer') assert.equal(result[0], 1) var result = this.reader.read() assert.strictEqual(result, false) this.reader.addChunk(Buffer.from([0, 0, 2, 1, 2])) var result = this.reader.read() assert.equal(result.length, 2, 'should have 2 length buffer but was ' + result.length) assert.equal(result[0], 1) assert.equal(result[1], 2) assert.strictEqual(false, this.reader.read()) }) }) describe('variable length header', function() { beforeEach(function() { this.reader = new Reader() }) it('reads double message buffers', function() { this.reader.addChunk(Buffer.from([ 0, 0, 0, 1, 1, 0, 0, 0, 2, 1, 2])) var result = this.reader.read() assert(result) assert.equal(result.length, 1) assert.equal(result[0], 1) result = this.reader.read() assert(result) assert.equal(result.length, 2) assert.equal(result[0], 1) assert.equal(result[1], 2) assert.strictEqual(this.reader.read(), false) }) }) describe('1 length code', function() { beforeEach(function() { this.reader = new Reader(1) }) it('reads code', function() { this.reader.addChunk(Buffer.from([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.from([2, 0, 0, 0, 1])) assert.strictEqual(this.reader.read(), false) assert.equal(this.reader.header, 2) }) }) describe('postgres style packet', function() { beforeEach(function() { this.reader = new Reader({ headerSize: 1, lengthPadding: -4 }) }) it('reads with padded length', function() { this.reader.addChunk(Buffer.from([1, 0, 0, 0, 8, 0, 0, 2, 0])) var result = this.reader.read() assert(result) assert.equal(result.length, 4) assert.equal(result[0], 0) assert.equal(result[1], 0) assert.equal(result[2], 2) assert.equal(result[3], 0) }) })