Skip to content

Commit

Permalink
Fix util.promisify for patched read function
Browse files Browse the repository at this point in the history
Fixes #125

PR-URL: #165
Credit: @coreyfarrell
Close: #165
Reviewed-by: @isaacs
  • Loading branch information
coreyfarrell authored and isaacs committed Aug 4, 2019
1 parent a4520b4 commit 1139b6f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,26 @@ function patch (fs) {
}

// if read() returns EAGAIN, then just try it again.
fs.read = (function (fs$read) { return function (fd, buffer, offset, length, position, callback_) {
var callback
if (callback_ && typeof callback_ === 'function') {
var eagCounter = 0
callback = function (er, _, __) {
if (er && er.code === 'EAGAIN' && eagCounter < 10) {
eagCounter ++
return fs$read.call(fs, fd, buffer, offset, length, position, callback)
fs.read = (function (fs$read) {
function read (fd, buffer, offset, length, position, callback_) {
var callback
if (callback_ && typeof callback_ === 'function') {
var eagCounter = 0
callback = function (er, _, __) {
if (er && er.code === 'EAGAIN' && eagCounter < 10) {
eagCounter ++
return fs$read.call(fs, fd, buffer, offset, length, position, callback)
}
callback_.apply(this, arguments)
}
callback_.apply(this, arguments)
}
return fs$read.call(fs, fd, buffer, offset, length, position, callback)
}
return fs$read.call(fs, fd, buffer, offset, length, position, callback)
}})(fs.read)

// This ensures `util.promisify` works as it does for native `fs.read`.
read.__proto__ = fs$read
return read
})(fs.read)

fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
var eagCounter = 0
Expand Down

0 comments on commit 1139b6f

Please sign in to comment.