Skip to content

Add explicit buffer option #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.

7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -168,4 +168,3 @@ module.exports = function(filename, opts) {
return source

};

33 changes: 33 additions & 0 deletions test/explicit-buffer.js
Original file line number Diff line number Diff line change
@@ -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()
})
);
});