length-prefixed-message
Advanced tools
Comparing version 1.1.0 to 3.0.0
94
index.js
@@ -1,69 +0,45 @@ | ||
var readFn = function(byteLen) { | ||
switch(byteLen) { | ||
case 1: | ||
return Buffer.prototype.readUInt8 | ||
case 2: | ||
return Buffer.prototype.readUInt16LE | ||
case 4: | ||
return Buffer.prototype.readUInt32LE | ||
default: | ||
return null; | ||
} | ||
}; | ||
var varint = require('varint'); | ||
var writeFn = function(byteLen) { | ||
switch(byteLen) { | ||
case 1: | ||
return Buffer.prototype.writeUInt8 | ||
case 2: | ||
return Buffer.prototype.writeUInt16LE | ||
case 4: | ||
return Buffer.prototype.writeUInt32LE | ||
default: | ||
return null; | ||
} | ||
}; | ||
var POOL_SIZE = 100000; | ||
var MINIMUM_POOL_LENGTH = 100; | ||
var pool = new Buffer(POOL_SIZE); | ||
var create = function(opts) { | ||
if (typeof opts === 'number') opts = {length:opts} | ||
opts = opts || {}; | ||
opts.length = opts.length || 4; | ||
if ([1,2,4].indexOf(opts.length) === -1) throw new Error('Invalid message length.'); | ||
exports.read = function(stream, cb) { | ||
var msglen = 0; | ||
var readable = function() { | ||
if (!msglen) { | ||
var buf = stream.read(); | ||
if (!buf) return; | ||
var writefn = writeFn(opts.length); | ||
var readfn = readFn(opts.length); | ||
var that = {}; | ||
that.read = function(stream, cb) { | ||
var msglen = 0; | ||
var readable = function() { | ||
if (!msglen) { | ||
msglen = stream.read(opts.length); | ||
if (!msglen) return; | ||
msglen = readfn.call(msglen, 0); | ||
for (var i = 0; i < buf.length; i++) { | ||
if (!(buf[i] & 0x80)) { | ||
msglen = varint.decode(buf); | ||
break; | ||
} | ||
} | ||
if (!msglen) return; | ||
buf = buf.slice(varint.decode.bytes); | ||
stream.unshift(buf); | ||
} | ||
var chunk = stream.read(msglen); | ||
if (!chunk) return; | ||
var chunk = stream.read(msglen); | ||
if (!chunk) return; | ||
stream.removeListener('readable', readable); | ||
cb(chunk) | ||
}; | ||
stream.on('readable', readable); | ||
readable(); | ||
stream.removeListener('readable', readable); | ||
cb(chunk) | ||
}; | ||
that.write = function(stream, buffer) { | ||
if (typeof buffer === 'string') buffer = new Buffer(buffer); | ||
var buf = new Buffer(buffer.length + opts.length); | ||
writefn.call(buf, buffer.length, 0); | ||
buffer.copy(buf, opts.length); | ||
stream.write(buf); | ||
}; | ||
stream.on('readable', readable); | ||
readable(); | ||
}; | ||
return that; | ||
exports.write = function(stream, msg) { | ||
if (typeof msg === 'string') msg = new Buffer(msg); | ||
varint.encode(msg.length, pool); | ||
var lenBuf = pool.slice(0, varint.encode.bytes); | ||
pool = pool.slice(varint.encode.bytes); | ||
if (pool.length < MINIMUM_POOL_LENGTH) pool = new Buffer(POOL_SIZE); | ||
stream.write(lenBuf); | ||
stream.write(msg); | ||
}; | ||
module.exports = create; |
{ | ||
"name": "length-prefixed-message", | ||
"version": "1.1.0", | ||
"version": "3.0.0", | ||
"description": "Reads and writes binary length prefixed messages. ", | ||
@@ -27,3 +27,6 @@ "main": "index.js", | ||
"concat-stream": "^1.4.6" | ||
}, | ||
"dependencies": { | ||
"varint": "^3.0.1" | ||
} | ||
} |
@@ -17,4 +17,3 @@ #length-prefixed-message | ||
```js | ||
var lengthPrefixedMessage = require('length-prefixed-message'); | ||
var lpm = lengthPrefixedMessage({length: 2}); // the length of the prefix | ||
var lpm = require('length-prefixed-message'); | ||
@@ -21,0 +20,0 @@ lpm.read(someStream, function(msgBuffer) { |
16
test.js
var fs = require('fs'); | ||
var test = require('tape'); | ||
var concat = require('concat-stream'); | ||
var lengthPrefixed = require('./'); | ||
var lpm = require('./'); | ||
var varint = require('varint'); | ||
test('.read', function(t) { | ||
var lpm = lengthPrefixed({length: 2}); | ||
lpm.read(fs.createReadStream('./fixtures'), function(buf) { | ||
@@ -15,8 +15,7 @@ t.equal(buf.toString(), 'When Gregor Samsa woke up one morning from unsettling dreams, he found himself changed \n'); | ||
test('.write', function(t) { | ||
var lpm = lengthPrefixed({length: 2}); | ||
var stream = concat(function(buff) { | ||
var len = buff.readUInt16LE(0); | ||
var len = varint.decode(buff); | ||
t.equal(len, 11); | ||
var str = buff.slice(2, 13).toString(); | ||
var str = buff.slice(varint.decode.bytes).toString(); | ||
t.equal(str, 'Hello world'); | ||
@@ -30,13 +29,12 @@ t.end(); | ||
test('.write string support', function(t) { | ||
var lpm = lengthPrefixed({length: 2}); | ||
var stream = concat(function(buff) { | ||
var len = buff.readUInt16LE(0); | ||
var len = varint.decode(buff); | ||
t.equal(len, 11); | ||
var str = buff.slice(2, 13).toString(); | ||
var str = buff.slice(varint.decode.bytes).toString(); | ||
t.equal(str, 'Hello world'); | ||
t.end(); | ||
}); | ||
lpm.write(stream, 'Hello world'); | ||
lpm.write(stream, new Buffer('Hello world')); | ||
stream.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
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
3596
1
70
28
+ Addedvarint@^3.0.1
+ Addedvarint@3.0.1(transitive)