Skip to content

fix: lock source to ensure no double reads happen #4

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var fs = require('fs');
var Decoder = require('pull-utf8-decoder')
var Lock = require('lock')
/**
# pull-file

Expand All @@ -25,6 +26,7 @@ module.exports = function(filename, opts) {

var ended, closeNext, busy;
var _buffer = new Buffer(bufferSize)
var lock = Lock()
var live = opts && !!opts.live
var liveCb, closeCb
var watcher
Expand Down Expand Up @@ -78,10 +80,12 @@ module.exports = function(filename, opts) {
return cb(err);
}

if(count === buffer.length) {
if(count === toRead && count === buffer.length) {
cb(null, buffer);
} else if(count === 0 && live) {
liveCb = cb; closeNext = true
} else if (count === 0) {
cb(ended = true)
} else {
closeNext = true;
cb(null, buffer.slice(0, count));
Expand Down Expand Up @@ -161,11 +165,16 @@ module.exports = function(filename, opts) {
readNext(cb);
};

var lockedSource = function (end, cb) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the locking causes the abort only to be propagated after the previous read is done. I would appreciate some input on how to best solve this.

what happens if a termination signal skips the lock?

something like:

var lockedSource = function (end, cb) {
  if (end) return source(end, cb)
  lock('source', function (release) {
    source(null, release(cb))
  })
}

lock('source', function (release) {
source(end, release(cb))
})
}

//read directly to text
if(opts && opts.encoding)
return Decoder(opts.encoding)(source)
return Decoder(opts.encoding)(lockedSource)

return source
return lockedSource

};

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"homepage": "https://github.com/DamonOehlman/pull-file",
"dependencies": {
"lock": "^0.1.3",
"pull-utf8-decoder": "^1.0.2"
}
}
}
35 changes: 35 additions & 0 deletions test/largefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,43 @@ test('large file as ascii strings', function(t) {
);
});

test('large file fast reads', function(t) {
var big = crypto.pseudoRandomBytes(10*1024*1024)
fs.writeFileSync(tmpfile, big)

var read = file(tmpfile, {bufferSize: 1024 * 1024})
var items = []

read(null, checkRead)
read(null, checkRead)
read(null, checkRead)
read(null, checkRead)
read(null, checkRead)

setTimeout(function () {
read(null, checkRead)
read(null, checkRead)
read(null, checkRead)
read(null, checkRead)
read(null, checkRead)
read(null, function (err, data) {
t.equal(err, true)
t.equal(data, undefined)
next()
})
}, 10)

function checkRead (err, data) {
t.equal(data.length, 1024 * 1024)
items.push(data)
next()
}

var i = 0
function next () {
if (++i === 11) {
t.equal(hash(big), hash(Buffer.concat(items)))
t.end()
}
}
})