Skip to content

Commit

Permalink
do not try to patch missing fs functions
Browse files Browse the repository at this point in the history
Deeper fix related to #222 and #230
  • Loading branch information
isaacs committed Apr 1, 2022
1 parent 393b623 commit 2d9d831
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ function patch (fs) {
fs.lstatSync = statFixSync(fs.lstatSync)

// if lchmod/lchown do not exist, then make them no-ops
if (!fs.lchmod) {
if (fs.chmod && !fs.lchmod) {
fs.lchmod = function (path, mode, cb) {
if (cb) process.nextTick(cb)
}
fs.lchmodSync = function () {}
}
if (!fs.lchown) {
if (fs.chown && !fs.lchown) {
fs.lchown = function (path, uid, gid, cb) {
if (cb) process.nextTick(cb)
}
Expand All @@ -94,32 +94,38 @@ function patch (fs) {
// CPU to a busy looping process, which can cause the program causing the lock
// contention to be starved of CPU by node, so the contention doesn't resolve.
if (platform === "win32") {
fs.rename = (function (fs$rename) { return function (from, to, cb) {
var start = Date.now()
var backoff = 0;
fs$rename(from, to, function CB (er) {
if (er
&& (er.code === "EACCES" || er.code === "EPERM")
&& Date.now() - start < 60000) {
setTimeout(function() {
fs.stat(to, function (stater, st) {
if (stater && stater.code === "ENOENT")
fs$rename(from, to, CB);
else
cb(er)
})
}, backoff)
if (backoff < 100)
backoff += 10;
return;
}
if (cb) cb(er)
})
}})(fs.rename)
fs.rename = typeof fs.rename !== 'function' ? fs.rename
: (function (fs$rename) {
function rename (from, to, cb) {
var start = Date.now()
var backoff = 0;
fs$rename(from, to, function CB (er) {
if (er
&& (er.code === "EACCES" || er.code === "EPERM")
&& Date.now() - start < 60000) {
setTimeout(function() {
fs.stat(to, function (stater, st) {
if (stater && stater.code === "ENOENT")
fs$rename(from, to, CB);
else
cb(er)
})
}, backoff)
if (backoff < 100)
backoff += 10;
return;
}
if (cb) cb(er)
})
}
if (Object.setPrototypeOf) Object.setPrototypeOf(rename, fs$rename)
return rename
})(fs.rename)
}

// if read() returns EAGAIN, then just try it again.
fs.read = (function (fs$read) {
fs.read = typeof fs.read !== 'function' ? fs.read
: (function (fs$read) {
function read (fd, buffer, offset, length, position, callback_) {
var callback
if (callback_ && typeof callback_ === 'function') {
Expand All @@ -136,11 +142,12 @@ function patch (fs) {
}

// This ensures `util.promisify` works as it does for native `fs.read`.
if (Object.setPrototypeOf && fs$read != undefined) Object.setPrototypeOf(read, fs$read)
if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read)
return read
})(fs.read)

fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
fs.readSync = typeof fs.readSync !== 'function' ? fs.readSync
: (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
var eagCounter = 0
while (true) {
try {
Expand Down Expand Up @@ -199,7 +206,7 @@ function patch (fs) {
}

function patchLutimes (fs) {
if (constants.hasOwnProperty("O_SYMLINK")) {
if (constants.hasOwnProperty("O_SYMLINK") && fs.futimes) {
fs.lutimes = function (path, at, mt, cb) {
fs.open(path, constants.O_SYMLINK, function (er, fd) {
if (er) {
Expand Down Expand Up @@ -233,7 +240,7 @@ function patch (fs) {
return ret
}

} else {
} else if (fs.futimes) {
fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
fs.lutimesSync = function () {}
}
Expand Down

0 comments on commit 2d9d831

Please sign in to comment.