From d5b465c6690076c36746c62ed37cb3711feced3c Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sun, 17 Sep 2023 20:30:14 +0800 Subject: [PATCH 1/5] fs: runtime deprecate `fs.F_OK`, `fs.R_OK`, `fs.W_OK`, `fs.X_OK` --- doc/api/deprecations.md | 5 ++++- lib/fs.js | 49 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 38e1b6efc9aafd..613ad3671f109b 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 9db60a4f89a3d9..976fad6a1741e7 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', + 'DEP0175', + ), + }, + 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', + 'DEP0175', + ), + }, + 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', + 'DEP0175', + ), + }, + 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', + 'DEP0175', + ), + }, constants: { __proto__: null, configurable: false, From 5369c71132dd13928938f8780d7b2a79e286b770 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sun, 3 Nov 2024 13:29:16 +0800 Subject: [PATCH 2/5] squash: fix DEP number --- lib/fs.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 976fad6a1741e7..af513ae55e1a1a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -3283,7 +3283,7 @@ ObjectDefineProperties(fs, { return F_OK || 0; }, 'fs.F_OK is deprecated, use fs.constants.F_OK instead', - 'DEP0175', + 'DEP0176', ), }, R_OK: { @@ -3294,7 +3294,7 @@ ObjectDefineProperties(fs, { return R_OK || 0; }, 'fs.R_OK is deprecated, use fs.constants.R_OK instead', - 'DEP0175', + 'DEP0176', ), }, W_OK: { @@ -3305,7 +3305,7 @@ ObjectDefineProperties(fs, { return W_OK || 0; }, 'fs.W_OK is deprecated, use fs.constants.W_OK instead', - 'DEP0175', + 'DEP0176', ), }, X_OK: { @@ -3316,7 +3316,7 @@ ObjectDefineProperties(fs, { return X_OK || 0; }, 'fs.X_OK is deprecated, use fs.constants.X_OK instead', - 'DEP0175', + 'DEP0176', ), }, constants: { From 5d01b37e704da2a8cc3a72137bb6b83c7ba40a47 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 5 Nov 2024 22:44:31 +0800 Subject: [PATCH 3/5] squash: add test --- test/parallel/test-fs-constants.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js index 49bcabd80873b4..9d665ac6443800 100644 --- a/test/parallel/test-fs-constants.js +++ b/test/parallel/test-fs-constants.js @@ -1,8 +1,18 @@ '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); + +expectWarning( + 'DeprecationWarning', + 'fs.F_OK is deprecated, use fs.constants.F_OK instead', + 'DEP0176' +); +fs.F_OK; +fs.R_OK; +fs.W_OK; +fs.X_OK; From 37546ec993a11c96b9d6772f1efd179d4ae83756 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 5 Nov 2024 22:55:28 +0800 Subject: [PATCH 4/5] squash: adjust test --- test/parallel/test-fs-constants.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js index 9d665ac6443800..6e785cbc0ea3e3 100644 --- a/test/parallel/test-fs-constants.js +++ b/test/parallel/test-fs-constants.js @@ -7,12 +7,21 @@ const assert = require('assert'); 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' ); -fs.F_OK; -fs.R_OK; -fs.W_OK; -fs.X_OK; + +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); From 7000c66039c77eae7663b1bf68b529b5aaef9e4c Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 5 Nov 2024 22:59:14 +0800 Subject: [PATCH 5/5] squash: lint --- test/parallel/test-fs-constants.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js index 6e785cbc0ea3e3..056ee637febf4a 100644 --- a/test/parallel/test-fs-constants.js +++ b/test/parallel/test-fs-constants.js @@ -10,10 +10,10 @@ 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' }); +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',