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 <duhamelantoine1995@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7a461ed commit b02cd41

3 files changed

Lines changed: 69 additions & 6 deletions

File tree

‎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+
functionget(){
3283+
returnF_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+
functionget(){
3294+
returnR_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+
functionget(){
3305+
returnW_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+
functionget(){
3316+
returnX_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
constfs=require('fs');
44
constassert=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)