Skip to content

Commit b02cd41

Browse files
fs: runtime deprecate fs.F_OK, fs.R_OK, fs.W_OK, fs.X_OK
PR-URL: #49686 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7a461ed commit b02cd41

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3577,12 +3577,15 @@ The [`util.toUSVString()`][] API is deprecated. Please use
35773577

35783578
<!-- YAML
35793579
changes:
3580+
- version: REPLACEME
3581+
pr-url: https://github.com/nodejs/node/pull/49686
3582+
description: Runtime deprecation.
35803583
- version: v20.8.0
35813584
pr-url: https://github.com/nodejs/node/pull/49683
35823585
description: Documentation-only deprecation.
35833586
-->
35843587

3585-
Type: Documentation-only
3588+
Type: Runtime
35863589

35873590
`F_OK`, `R_OK`, `W_OK` and `X_OK` getters exposed directly on `node:fs` are
35883591
deprecated. Get them from `fs.constants` or `fs.promises.constants` instead.

lib/fs.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const {
8787
const { toPathIfFileURL } = require('internal/url');
8888
const {
8989
customPromisifyArgs: kCustomPromisifyArgsSymbol,
90+
deprecate,
9091
emitExperimentalWarning,
9192
getLazy,
9293
kEmptyObject,
@@ -3274,10 +3275,50 @@ defineLazyProperties(
32743275
);
32753276

32763277
ObjectDefineProperties(fs, {
3277-
F_OK: { __proto__: null, enumerable: true, value: F_OK || 0 },
3278-
R_OK: { __proto__: null, enumerable: true, value: R_OK || 0 },
3279-
W_OK: { __proto__: null, enumerable: true, value: W_OK || 0 },
3280-
X_OK: { __proto__: null, enumerable: true, value: X_OK || 0 },
3278+
F_OK: {
3279+
__proto__: null,
3280+
enumerable: false,
3281+
get: deprecate(
3282+
function get() {
3283+
return F_OK || 0;
3284+
},
3285+
'fs.F_OK is deprecated, use fs.constants.F_OK instead',
3286+
'DEP0176',
3287+
),
3288+
},
3289+
R_OK: {
3290+
__proto__: null,
3291+
enumerable: false,
3292+
get: deprecate(
3293+
function get() {
3294+
return R_OK || 0;
3295+
},
3296+
'fs.R_OK is deprecated, use fs.constants.R_OK instead',
3297+
'DEP0176',
3298+
),
3299+
},
3300+
W_OK: {
3301+
__proto__: null,
3302+
enumerable: false,
3303+
get: deprecate(
3304+
function get() {
3305+
return W_OK || 0;
3306+
},
3307+
'fs.W_OK is deprecated, use fs.constants.W_OK instead',
3308+
'DEP0176',
3309+
),
3310+
},
3311+
X_OK: {
3312+
__proto__: null,
3313+
enumerable: false,
3314+
get: deprecate(
3315+
function get() {
3316+
return X_OK || 0;
3317+
},
3318+
'fs.X_OK is deprecated, use fs.constants.X_OK instead',
3319+
'DEP0176',
3320+
),
3321+
},
32813322
constants: {
32823323
__proto__: null,
32833324
configurable: false,

test/parallel/test-fs-constants.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
'use strict';
2-
require('../common');
2+
const { expectWarning } = require('../common');
33
const fs = require('fs');
44
const assert = require('assert');
55

66
// Check if the two constants accepted by chmod() on Windows are defined.
77
assert.notStrictEqual(fs.constants.S_IRUSR, undefined);
88
assert.notStrictEqual(fs.constants.S_IWUSR, undefined);
9+
10+
// Check for runtime deprecation warning, there should be no setter
11+
const { F_OK, R_OK, W_OK, X_OK } = fs.constants;
12+
13+
assert.throws(() => { fs.F_OK = 'overwritten'; }, { name: 'TypeError' });
14+
assert.throws(() => { fs.R_OK = 'overwritten'; }, { name: 'TypeError' });
15+
assert.throws(() => { fs.W_OK = 'overwritten'; }, { name: 'TypeError' });
16+
assert.throws(() => { fs.X_OK = 'overwritten'; }, { name: 'TypeError' });
17+
18+
expectWarning(
19+
'DeprecationWarning',
20+
'fs.F_OK is deprecated, use fs.constants.F_OK instead',
21+
'DEP0176'
22+
);
23+
24+
assert.strictEqual(fs.F_OK, F_OK);
25+
assert.strictEqual(fs.R_OK, R_OK);
26+
assert.strictEqual(fs.W_OK, W_OK);
27+
assert.strictEqual(fs.X_OK, X_OK);

0 commit comments

Comments
 (0)