Skip to content

Commit 2d9d831

Browse files
committed
do not try to patch missing fs functions
Deeper fix related to #222 and #230
1 parent 393b623 commit 2d9d831

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

polyfills.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ function patch (fs) {
7171
fs.lstatSync = statFixSync(fs.lstatSync)
7272

7373
// if lchmod/lchown do not exist, then make them no-ops
74-
if (!fs.lchmod) {
74+
if (fs.chmod && !fs.lchmod) {
7575
fs.lchmod = function (path, mode, cb) {
7676
if (cb) process.nextTick(cb)
7777
}
7878
fs.lchmodSync = function () {}
7979
}
80-
if (!fs.lchown) {
80+
if (fs.chown && !fs.lchown) {
8181
fs.lchown = function (path, uid, gid, cb) {
8282
if (cb) process.nextTick(cb)
8383
}
@@ -94,32 +94,38 @@ function patch (fs) {
9494
// CPU to a busy looping process, which can cause the program causing the lock
9595
// contention to be starved of CPU by node, so the contention doesn't resolve.
9696
if (platform === "win32") {
97-
fs.rename = (function (fs$rename) { return function (from, to, cb) {
98-
var start = Date.now()
99-
var backoff = 0;
100-
fs$rename(from, to, function CB (er) {
101-
if (er
102-
&& (er.code === "EACCES" || er.code === "EPERM")
103-
&& Date.now() - start < 60000) {
104-
setTimeout(function() {
105-
fs.stat(to, function (stater, st) {
106-
if (stater && stater.code === "ENOENT")
107-
fs$rename(from, to, CB);
108-
else
109-
cb(er)
110-
})
111-
}, backoff)
112-
if (backoff < 100)
113-
backoff += 10;
114-
return;
115-
}
116-
if (cb) cb(er)
117-
})
118-
}})(fs.rename)
97+
fs.rename = typeof fs.rename !== 'function' ? fs.rename
98+
: (function (fs$rename) {
99+
function rename (from, to, cb) {
100+
var start = Date.now()
101+
var backoff = 0;
102+
fs$rename(from, to, function CB (er) {
103+
if (er
104+
&& (er.code === "EACCES" || er.code === "EPERM")
105+
&& Date.now() - start < 60000) {
106+
setTimeout(function() {
107+
fs.stat(to, function (stater, st) {
108+
if (stater && stater.code === "ENOENT")
109+
fs$rename(from, to, CB);
110+
else
111+
cb(er)
112+
})
113+
}, backoff)
114+
if (backoff < 100)
115+
backoff += 10;
116+
return;
117+
}
118+
if (cb) cb(er)
119+
})
120+
}
121+
if (Object.setPrototypeOf) Object.setPrototypeOf(rename, fs$rename)
122+
return rename
123+
})(fs.rename)
119124
}
120125

121126
// if read() returns EAGAIN, then just try it again.
122-
fs.read = (function (fs$read) {
127+
fs.read = typeof fs.read !== 'function' ? fs.read
128+
: (function (fs$read) {
123129
function read (fd, buffer, offset, length, position, callback_) {
124130
var callback
125131
if (callback_ && typeof callback_ === 'function') {
@@ -136,11 +142,12 @@ function patch (fs) {
136142
}
137143

138144
// This ensures `util.promisify` works as it does for native `fs.read`.
139-
if (Object.setPrototypeOf && fs$read != undefined) Object.setPrototypeOf(read, fs$read)
145+
if (Object.setPrototypeOf) Object.setPrototypeOf(read, fs$read)
140146
return read
141147
})(fs.read)
142148

143-
fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
149+
fs.readSync = typeof fs.readSync !== 'function' ? fs.readSync
150+
: (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
144151
var eagCounter = 0
145152
while (true) {
146153
try {
@@ -199,7 +206,7 @@ function patch (fs) {
199206
}
200207

201208
function patchLutimes (fs) {
202-
if (constants.hasOwnProperty("O_SYMLINK")) {
209+
if (constants.hasOwnProperty("O_SYMLINK") && fs.futimes) {
203210
fs.lutimes = function (path, at, mt, cb) {
204211
fs.open(path, constants.O_SYMLINK, function (er, fd) {
205212
if (er) {
@@ -233,7 +240,7 @@ function patch (fs) {
233240
return ret
234241
}
235242

236-
} else {
243+
} else if (fs.futimes) {
237244
fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
238245
fs.lutimesSync = function () {}
239246
}

0 commit comments

Comments
 (0)