diff --git a/README.md b/README.md index 604b9bf..306a267 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,9 @@ pull( this supports all the options that node's [fs.createReadStream](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) supports, and _also_ this supports a `live: true` property which will keep the stream open and wait for appends -when it gets to the end. +when it gets to the end and an explicit `buffer` option where your chunks will be read to. +Note that if your downstream operations are async you may run into concurrency +issues with this option. Use at your own risk! ## License(s) @@ -54,4 +56,3 @@ 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. - diff --git a/index.js b/index.js index 62fbcc5..609daff 100644 --- a/index.js +++ b/index.js @@ -18,13 +18,13 @@ var Decoder = require('pull-utf8-decoder') **/ module.exports = function(filename, opts) { var mode = opts && opts.mode || 0x1B6; // 0666 - var bufferSize = opts && opts.bufferSize || 1024*64; + var bufferSize = opts && (opts.bufferSize || (opts.buffer && opts.buffer.length)) || 1024*64; var start = opts && opts.start || 0 var end = opts && opts.end || Number.MAX_SAFE_INTEGER var fd = opts && opts.fd var ended, closeNext, busy; - var _buffer = new Buffer(bufferSize) + var _buffer = opts && opts.buffer || new Buffer(bufferSize) var live = opts && !!opts.live var liveCb, closeCb var watcher @@ -88,7 +88,7 @@ module.exports = function(filename, opts) { } } ); - _buffer = new Buffer(Math.min(end - start, bufferSize)) + _buffer = opts && opts.buffer || new Buffer(Math.min(end - start, bufferSize)) } function open(cb) { @@ -168,4 +168,3 @@ module.exports = function(filename, opts) { return source }; - diff --git a/test/explicit-buffer.js b/test/explicit-buffer.js new file mode 100644 index 0000000..2e7c643 --- /dev/null +++ b/test/explicit-buffer.js @@ -0,0 +1,33 @@ +var test = require('tape'); +var pull = require('pull-stream'); +var file = require('..'); + +var path = require('path'); +var crypto = require('crypto') +var osenv = require('osenv') +var fs = require('fs') + +var tmpfile = path.join(osenv.tmpdir(), 'test_pull-file_big') + +var big = crypto.pseudoRandomBytes(10*1024*1024) +fs.writeFileSync(tmpfile, big) + +function hash (data) { + return crypto.createHash('sha256').update(data).digest('hex') +} + +test('large file in explicit buffer', function(t) { + var buf = new Buffer(65551) // prime close to 1024 * 64 + var h = crypto.createHash('sha256') + + pull( + file(tmpfile, {buffer: buf}), + pull.through(function (chunk) { + h.update(chunk) + }), + pull.onEnd(function(err) { + t.equal(hash(big), h.digest('hex')) + t.end() + }) + ); +});