buffer-peek-stream
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -31,3 +31,2 @@ var stream = require('stream'); | ||
BufferPeekStream.prototype._transform = function _transform(chunk, enc, callback) { | ||
@@ -60,32 +59,39 @@ // buffer incoming chunks until we have enough for our peek | ||
//TODO: find out if this stream can still get data after unpiping under any circumstance | ||
var buffer = Buffer.concat(this.__buffer); | ||
var source = this.__src; | ||
// push exactly the number of bytes we wanted to peek | ||
this.push(buffer.slice(0, this.__peekBytes)); | ||
this.push(null); | ||
if (source._readableState.ended) { | ||
if (this.__src._readableState.ended) { | ||
// if the source has ended then we need to modify its state so it'll start flowing again when we | ||
// unshift the data back on. these settings were naively obtained by creating a file read | ||
// stream of a tiny file then leaving it hang for a timeout of 5s and dumping the state | ||
source.readable = true; | ||
source._readableState.ended = true; | ||
source._readableState.endEmitted = false; | ||
source._readableState.ranOut = false; | ||
source._readableState.reading = false; | ||
source._readableState.calledRead = true; | ||
source._readableState.sync = false; | ||
source._readableState.needReadable = false; | ||
source._readableState.emittedReadable = true; | ||
source._readableState.readableListening = false; | ||
source._readableState.readingMore = false; | ||
this.__src.readable = true; | ||
this.__src._readableState.ended = false; | ||
this.__src._readableState.endEmitted = false; | ||
this.__src._readableState.ranOut = false; | ||
this.__src._readableState.reading = false; | ||
this.__src._readableState.calledRead = true; | ||
this.__src._readableState.sync = false; | ||
this.__src._readableState.needReadable = false; | ||
this.__src._readableState.emittedReadable = true; | ||
this.__src._readableState.readableListening = false; | ||
this.__src._readableState.readingMore = false; | ||
// flush before end is called on this stream | ||
flush.call(this); | ||
} else { | ||
// flush once we've emptied this streams buffer through the transform function | ||
this.once('drain', flush.bind(this)); | ||
} | ||
source.unshift(buffer); | ||
function flush() { | ||
var buffer = Buffer.concat(this.__buffer); | ||
// we don't need to keep the original buffers | ||
this.__buffer = null; | ||
// push exactly the number of bytes we wanted to peek | ||
this.push(buffer.slice(0, this.__peekBytes)); | ||
// put the whole buffer back onto the start of the origin stream | ||
this.__src.unshift(buffer); | ||
this.__src = null; | ||
this.__buffer = null; | ||
this.push(null); | ||
} | ||
}; |
@@ -10,3 +10,3 @@ var BufferPeekStream = require('./buffer-peek-stream'); | ||
cb = bytes; | ||
bytes = 65536; // 64K | ||
bytes = 16384; // 16K | ||
} | ||
@@ -19,3 +19,9 @@ | ||
_.once('data', function (data) { | ||
var data; | ||
_.once('data', function (d) { | ||
data = d; | ||
}); | ||
_.once('end', function () { | ||
cb(null, data); | ||
@@ -22,0 +28,0 @@ }); |
{ | ||
"name": "buffer-peek-stream", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Transform stream that lets you inspect the start of a ReadStream before deciding what to do with it", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha test/buffer-peek-stream.js" | ||
"test": "mocha test.js" | ||
}, | ||
@@ -25,3 +25,8 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/seangarner/node-buffer-peek-stream" | ||
"homepage": "https://github.com/seangarner/node-buffer-peek-stream", | ||
"devDependencies": { | ||
"chai": "^1.9.2", | ||
"mocha": "^2.0.1", | ||
"randomstring": "^1.0.3" | ||
} | ||
} |
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
7906
101
3