Skip to content

Commit 5678595

Browse files
committed
fs: deprecate exists() and existsSync()
These methods don't follow standard conventions, and shouldn't be used anyway. Fixes: #103 PR-URL: #166 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent a553267 commit 5678595

9 files changed

+37
-22
lines changed

benchmark/misc/module-loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function measure(n) {
5656
}
5757

5858
function rmrf(location) {
59-
if (fs.existsSync(location)) {
59+
try {
6060
var things = fs.readdirSync(location);
6161
things.forEach(function(thing) {
6262
var cur = path.join(location, thing),
@@ -68,5 +68,7 @@ function rmrf(location) {
6868
fs.unlinkSync(cur);
6969
});
7070
fs.rmdirSync(location);
71+
} catch (err) {
72+
// Ignore error
7173
}
7274
}

doc/api/fs.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,13 @@ that leaves you vulnerable to race conditions: another process may remove the
656656
file between the calls to `fs.exists()` and `fs.open()`. Just open the file
657657
and handle the error when it's not there.
658658

659-
`fs.exists()` will be deprecated.
659+
`fs.exists()` is **deprecated**.
660660

661661
## fs.existsSync(path)
662662

663663
Synchronous version of `fs.exists`.
664664

665-
`fs.existsSync()` will be deprecated.
665+
`fs.existsSync()` is **deprecated**.
666666

667667
## fs.access(path[, mode], callback)
668668

lib/fs.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,25 @@ fs.accessSync = function(path, mode) {
220220
binding.access(pathModule._makeLong(path), mode);
221221
};
222222

223-
fs.exists = function(path, callback) {
223+
fs.exists = util.deprecate(function(path, callback) {
224224
if (!nullCheck(path, cb)) return;
225225
var req = new FSReqWrap();
226226
req.oncomplete = cb;
227227
binding.stat(pathModule._makeLong(path), req);
228228
function cb(err, stats) {
229229
if (callback) callback(err ? false : true);
230230
}
231-
};
231+
}, 'fs.exists() is deprecated. Use fs.access() instead.');
232232

233-
fs.existsSync = function(path) {
233+
fs.existsSync = util.deprecate(function(path) {
234234
try {
235235
nullCheck(path);
236236
binding.stat(pathModule._makeLong(path));
237237
return true;
238238
} catch (e) {
239239
return false;
240240
}
241-
};
241+
}, 'fs.existsSync() is deprecated. Use fs.accessSync() instead.');
242242

243243
fs.readFile = function(path, options, callback_) {
244244
var callback = maybeCallback(arguments[arguments.length - 1]);

test/common.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ if (process.env.NODE_COMMON_PIPE) {
5858
}
5959
}
6060

61-
if (!fs.existsSync(exports.opensslCli))
61+
try {
62+
fs.accessSync(exports.opensslCli);
63+
} catch (err) {
6264
exports.opensslCli = false;
65+
}
6366

6467
if (process.platform === 'win32') {
6568
exports.faketimeCli = false;
@@ -319,3 +322,12 @@ exports.isValidHostname = function(str) {
319322

320323
return !!str.match(re) && str.length <= 255;
321324
}
325+
326+
exports.fileExists = function(pathname) {
327+
try {
328+
fs.accessSync(pathname);
329+
return true;
330+
} catch (err) {
331+
return false;
332+
}
333+
};

test/parallel/test-child-process-spawn-error.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
var common = require('../common');
2223
var fs = require('fs');
2324
var spawn = require('child_process').spawn;
2425
var assert = require('assert');
2526

2627
var errors = 0;
2728

2829
var enoentPath = 'foo123';
29-
assert.equal(fs.existsSync(enoentPath), false);
30+
assert.equal(common.fileExists(enoentPath), false);
3031

3132
var enoentChild = spawn(enoentPath);
3233
enoentChild.on('error', function (err) {

test/parallel/test-fs-mkdir.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function unlink(pathname) {
3838

3939
fs.mkdir(pathname, function(err) {
4040
assert.equal(err, null);
41-
assert.equal(fs.existsSync(pathname), true);
41+
assert.equal(common.fileExists(pathname), true);
4242
ncalls++;
4343
});
4444

@@ -56,7 +56,7 @@ function unlink(pathname) {
5656

5757
fs.mkdir(pathname, 511 /*=0777*/, function(err) {
5858
assert.equal(err, null);
59-
assert.equal(fs.existsSync(pathname), true);
59+
assert.equal(common.fileExists(pathname), true);
6060
ncalls++;
6161
});
6262

@@ -72,7 +72,7 @@ function unlink(pathname) {
7272
unlink(pathname);
7373
fs.mkdirSync(pathname);
7474

75-
var exists = fs.existsSync(pathname);
75+
var exists = common.fileExists(pathname);
7676
unlink(pathname);
7777

7878
assert.equal(exists, true);

test/parallel/test-fs-symlink-dir-junction.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ fs.symlink(linkData, linkPath, 'junction', function(err) {
5454

5555
fs.unlink(linkPath, function(err) {
5656
if (err) throw err;
57-
assert(!fs.existsSync(linkPath));
58-
assert(fs.existsSync(linkData));
57+
assert(!common.fileExists(linkPath));
58+
assert(common.fileExists(linkData));
5959
completed++;
6060
});
6161
});

test/pummel/test-fs-watch-file-slow.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ fs.watchFile(FILENAME, {interval:TIMEOUT - 250}, function(curr, prev) {
4040
console.log([curr, prev]);
4141
switch (++nevents) {
4242
case 1:
43-
assert.equal(fs.existsSync(FILENAME), false);
43+
assert.equal(common.fileExists(FILENAME), false);
4444
break;
4545
case 2:
4646
case 3:
47-
assert.equal(fs.existsSync(FILENAME), true);
47+
assert.equal(common.fileExists(FILENAME), true);
4848
break;
4949
case 4:
50-
assert.equal(fs.existsSync(FILENAME), false);
50+
assert.equal(common.fileExists(FILENAME), false);
5151
fs.unwatchFile(FILENAME);
5252
break;
5353
default:

test/sequential/test-regress-GH-3739.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ for (var i = 0; i < 50; i++) {
4444
}
4545

4646
// Test existsSync
47-
var r = fs.existsSync(dir);
47+
var r = common.fileExists(dir);
4848
if (r !== true) {
4949
cleanup();
50-
throw new Error('fs.existsSync returned false');
50+
throw new Error('fs.accessSync returned false');
5151
}
5252

5353
// Text exists
54-
fs.exists(dir, function(r) {
54+
fs.access(dir, function(err) {
5555
cleanup();
56-
if (r !== true) {
57-
throw new Error('fs.exists reported false');
56+
if (err) {
57+
throw new Error('fs.access reported false');
5858
}
5959
});
6060

0 commit comments

Comments
 (0)