From b02cd411c22d5964f9c8ae52f4be2bd4098626a6 Mon Sep 17 00:00:00 2001 From: Livia Medeiros Date: Fri, 15 Nov 2024 08:34:02 +0900 Subject: [PATCH] fs: runtime deprecate `fs.F_OK`, `fs.R_OK`, `fs.W_OK`, `fs.X_OK` PR-URL: https://github.com/nodejs/node/pull/49686 Reviewed-By: Antoine du Hamel Reviewed-By: Marco Ippolito Reviewed-By: James M Snell --- doc/api/deprecations.md | 5 ++- lib/fs.js | 49 +++++++++++++++++++++++++++--- test/parallel/test-fs-constants.js | 21 ++++++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 692360d243b..5d0940880fe 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3577,12 +3577,15 @@ The [`util.toUSVString()`][] API is deprecated. Please use -Type: Documentation-only +Type: Runtime `F_OK`, `R_OK`, `W_OK` and `X_OK` getters exposed directly on `node:fs` are deprecated. Get them from `fs.constants` or `fs.promises.constants` instead. diff --git a/lib/fs.js b/lib/fs.js index 6a7d2cde463..13aeb30be6c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -87,6 +87,7 @@ const { const { toPathIfFileURL } = require('internal/url'); const { customPromisifyArgs: kCustomPromisifyArgsSymbol, + deprecate, emitExperimentalWarning, getLazy, kEmptyObject, @@ -3274,10 +3275,50 @@ defineLazyProperties( ); ObjectDefineProperties(fs, { - F_OK: { __proto__: null, enumerable: true, value: F_OK || 0 }, - R_OK: { __proto__: null, enumerable: true, value: R_OK || 0 }, - W_OK: { __proto__: null, enumerable: true, value: W_OK || 0 }, - X_OK: { __proto__: null, enumerable: true, value: X_OK || 0 }, + F_OK: { + __proto__: null, + enumerable: false, + get: deprecate( + function get() { + return F_OK || 0; + }, + 'fs.F_OK is deprecated, use fs.constants.F_OK instead', + 'DEP0176', + ), + }, + R_OK: { + __proto__: null, + enumerable: false, + get: deprecate( + function get() { + return R_OK || 0; + }, + 'fs.R_OK is deprecated, use fs.constants.R_OK instead', + 'DEP0176', + ), + }, + W_OK: { + __proto__: null, + enumerable: false, + get: deprecate( + function get() { + return W_OK || 0; + }, + 'fs.W_OK is deprecated, use fs.constants.W_OK instead', + 'DEP0176', + ), + }, + X_OK: { + __proto__: null, + enumerable: false, + get: deprecate( + function get() { + return X_OK || 0; + }, + 'fs.X_OK is deprecated, use fs.constants.X_OK instead', + 'DEP0176', + ), + }, constants: { __proto__: null, configurable: false, diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js index 49bcabd8087..056ee637feb 100644 --- a/test/parallel/test-fs-constants.js +++ b/test/parallel/test-fs-constants.js @@ -1,8 +1,27 @@ 'use strict'; -require('../common'); +const { expectWarning } = require('../common'); const fs = require('fs'); const assert = require('assert'); // Check if the two constants accepted by chmod() on Windows are defined. assert.notStrictEqual(fs.constants.S_IRUSR, undefined); assert.notStrictEqual(fs.constants.S_IWUSR, undefined); + +// Check for runtime deprecation warning, there should be no setter +const { F_OK, R_OK, W_OK, X_OK } = fs.constants; + +assert.throws(() => { fs.F_OK = 'overwritten'; }, { name: 'TypeError' }); +assert.throws(() => { fs.R_OK = 'overwritten'; }, { name: 'TypeError' }); +assert.throws(() => { fs.W_OK = 'overwritten'; }, { name: 'TypeError' }); +assert.throws(() => { fs.X_OK = 'overwritten'; }, { name: 'TypeError' }); + +expectWarning( + 'DeprecationWarning', + 'fs.F_OK is deprecated, use fs.constants.F_OK instead', + 'DEP0176' +); + +assert.strictEqual(fs.F_OK, F_OK); +assert.strictEqual(fs.R_OK, R_OK); +assert.strictEqual(fs.W_OK, W_OK); +assert.strictEqual(fs.X_OK, X_OK);