length-prefixed-stream
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,3 +5,3 @@ var varint = require('varint') | ||
var Decoder = function() { | ||
var Decoder = function () { | ||
if (!(this instanceof Decoder)) return new Decoder() | ||
@@ -18,3 +18,3 @@ stream.Transform.call(this) | ||
Decoder.prototype._push = function(message) { | ||
Decoder.prototype._push = function (message) { | ||
this._ptr = 0 | ||
@@ -26,3 +26,3 @@ this._missing = 0 | ||
Decoder.prototype._parseLength = function(data, offset) { | ||
Decoder.prototype._parseLength = function (data, offset) { | ||
for (offset; offset < data.length; offset++) { | ||
@@ -33,3 +33,3 @@ this._prefix[this._ptr++] = data[offset] | ||
this._ptr = 0 | ||
return offset+1 | ||
return offset + 1 | ||
} | ||
@@ -40,3 +40,3 @@ } | ||
Decoder.prototype._parseMessage = function(data, offset) { | ||
Decoder.prototype._parseMessage = function (data, offset) { | ||
var free = data.length - offset | ||
@@ -47,4 +47,4 @@ var missing = this._missing | ||
if (missing <= free) { // fast track - no copy | ||
this._push(data.slice(offset, offset+missing)) | ||
return offset+missing | ||
this._push(data.slice(offset, offset + missing)) | ||
return offset + missing | ||
} | ||
@@ -54,7 +54,8 @@ this._message = new Buffer(missing) | ||
data.copy(this._message, this._ptr, offset, offset+missing) | ||
// TODO: add opt-in "partial mode" to completely avoid copys | ||
data.copy(this._message, this._ptr, offset, offset + missing) | ||
if (missing <= free) { | ||
this._push(this._message) | ||
return offset+missing | ||
return offset + missing | ||
} | ||
@@ -68,3 +69,3 @@ | ||
Decoder.prototype._transform = function(data, enc, cb) { | ||
Decoder.prototype._transform = function (data, enc, cb) { | ||
var offset = 0 | ||
@@ -80,2 +81,2 @@ | ||
module.exports = Decoder | ||
module.exports = Decoder |
@@ -5,5 +5,6 @@ var varint = require('varint') | ||
var pool = new Buffer(10*1024) | ||
var pool = new Buffer(10 * 1024) | ||
var used = 0 | ||
var Encoder = function() { | ||
var Encoder = function () { | ||
if (!(this instanceof Encoder)) return new Encoder() | ||
@@ -15,13 +16,17 @@ stream.Transform.call(this) | ||
Encoder.prototype._transform = function(data, enc, cb) { | ||
varint.encode(data.length, pool) | ||
this.push(pool.slice(0, varint.encode.bytes)) | ||
Encoder.prototype._transform = function (data, enc, cb) { | ||
varint.encode(data.length, pool, used) | ||
used += varint.encode.bytes | ||
pool = pool.slice(varint.encode.bytes) | ||
if (pool.length < 100) pool = new Buffer(10*1024) | ||
this.push(pool.slice(used - varint.encode.bytes, used)) | ||
this.push(data) | ||
this.push(data) | ||
if (pool.length - used < 100) { | ||
pool = new Buffer(10 * 1024) | ||
used = 0 | ||
} | ||
cb() | ||
} | ||
module.exports = Encoder | ||
module.exports = Encoder |
exports.encode = require('./encode') | ||
exports.decode = require('./decode') | ||
exports.decode = require('./decode') |
{ | ||
"name": "length-prefixed-stream", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Streaming length prefixed buffers", | ||
"main": "index.js", | ||
"dependencies": { | ||
"readable-stream": "^1.0.33", | ||
"readable-stream": "^1.1.13", | ||
"varint": "^3.0.1" | ||
@@ -9,0 +9,0 @@ }, |
57
test.js
@@ -6,7 +6,6 @@ var tape = require('tape') | ||
var chunk = function(ultra) { | ||
return through(function(data, enc, cb) { | ||
var chunk = function (ultra) { | ||
return through(function (data, enc, cb) { | ||
while (data.length) { | ||
var chunk = data.slice(0, ultra ? 1 : 1+((Math.random() * data.length) | 0)) | ||
var chunk = data.slice(0, ultra ? 1 : 1 + ((Math.random() * data.length) | 0)) | ||
this.push(chunk) | ||
@@ -19,7 +18,7 @@ data = data.slice(chunk.length) | ||
tape('encode -> decode', function(t) { | ||
tape('encode -> decode', function (t) { | ||
var e = lpstream.encode() | ||
var d = lpstream.decode() | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), 'hello world') | ||
@@ -33,7 +32,7 @@ t.end() | ||
tape('buffered encode -> buffered decode', function(t) { | ||
tape('buffered encode -> buffered decode', function (t) { | ||
var e = lpstream.encode() | ||
var d = lpstream.decode() | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), 'hello world') | ||
@@ -46,3 +45,3 @@ t.end() | ||
e.pipe(concat(function(data) { | ||
e.pipe(concat(function (data) { | ||
d.end(data) | ||
@@ -52,3 +51,3 @@ })) | ||
tape('encode -> decode twice', function(t) { | ||
tape('encode -> decode twice', function (t) { | ||
t.plan(2) | ||
@@ -61,3 +60,3 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), expects.shift()) | ||
@@ -71,3 +70,3 @@ }) | ||
tape('encode -> decode storm', function(t) { | ||
tape('encode -> decode storm', function (t) { | ||
t.plan(50) | ||
@@ -83,7 +82,7 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data, expects.shift()) | ||
}) | ||
expects.forEach(function(b) { | ||
expects.forEach(function (b) { | ||
e.write(b) | ||
@@ -95,7 +94,7 @@ }) | ||
tape('chunked encode -> decode', function(t) { | ||
tape('chunked encode -> decode', function (t) { | ||
var e = lpstream.encode() | ||
var d = lpstream.decode() | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), 'hello world') | ||
@@ -109,3 +108,3 @@ t.end() | ||
tape('chunked encode -> decode twice', function(t) { | ||
tape('chunked encode -> decode twice', function (t) { | ||
t.plan(2) | ||
@@ -118,3 +117,3 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), expects.shift()) | ||
@@ -128,3 +127,3 @@ }) | ||
tape('chunked encode -> decode storm', function(t) { | ||
tape('chunked encode -> decode storm', function (t) { | ||
t.plan(50) | ||
@@ -140,7 +139,7 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data, expects.shift()) | ||
}) | ||
expects.forEach(function(b) { | ||
expects.forEach(function (b) { | ||
e.write(b) | ||
@@ -152,7 +151,7 @@ }) | ||
tape('ultra chunked encode -> decode', function(t) { | ||
tape('ultra chunked encode -> decode', function (t) { | ||
var e = lpstream.encode() | ||
var d = lpstream.decode() | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), 'hello world') | ||
@@ -166,3 +165,3 @@ t.end() | ||
tape('ultra chunked encode -> decode twice', function(t) { | ||
tape('ultra chunked encode -> decode twice', function (t) { | ||
t.plan(2) | ||
@@ -175,3 +174,3 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data.toString(), expects.shift()) | ||
@@ -185,3 +184,3 @@ }) | ||
tape('ultra chunked encode -> decode storm', function(t) { | ||
tape('ultra chunked encode -> decode storm', function (t) { | ||
t.plan(50) | ||
@@ -197,7 +196,7 @@ | ||
d.on('data', function(data) { | ||
d.on('data', function (data) { | ||
t.same(data, expects.shift()) | ||
}) | ||
expects.forEach(function(b) { | ||
expects.forEach(function (b) { | ||
e.write(b) | ||
@@ -207,2 +206,2 @@ }) | ||
e.pipe(chunk(true)).pipe(d) | ||
}) | ||
}) |
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
9323
10
242
Updatedreadable-stream@^1.1.13