fs: unexpose internal constants

`EXTENSIONLESS_FORMAT_JAVASCRIPT` and `EXTENSIONLESS_FORMAT_WASM` are
only used internally through binding `getFormatOfExtensionlessFile`.
They should not be exposed publicly.

PR-URL: https://github.com/nodejs/node/pull/58327
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Chengzhong Wu 2025-05-16 20:40:17 +01:00 committed by GitHub
parent b1d598744c
commit a436f7b492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 94 additions and 13 deletions

View File

@ -7,7 +7,7 @@ const {
const { getOptionValue } = require('internal/options');
const { getValidatedPath } = require('internal/fs/utils');
const fsBindings = internalBinding('fs');
const { fs: fsConstants } = internalBinding('constants');
const { internal: internalConstants } = internalBinding('constants');
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
const experimentalAddonModules = getOptionValue('--experimental-addon-modules');
@ -60,7 +60,7 @@ function getFormatOfExtensionlessFile(url) {
if (!experimentalWasmModules) { return 'module'; }
const path = getValidatedPath(url);
switch (fsBindings.getFormatOfExtensionlessFile(path)) {
case fsConstants.EXTENSIONLESS_FORMAT_WASM:
case internalConstants.EXTENSIONLESS_FORMAT_WASM:
return 'wasm';
default:
return 'module';

View File

@ -1041,7 +1041,7 @@ void DefineCryptoConstants(Local<Object> target) {
#endif
}
void DefineSystemConstants(Local<Object> target) {
void DefineFsConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_DIR);
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_JUNCTION);
// file access modes
@ -1059,10 +1059,6 @@ void DefineSystemConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, UV_DIRENT_CHAR);
NODE_DEFINE_CONSTANT(target, UV_DIRENT_BLOCK);
// Define module specific constants
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
NODE_DEFINE_CONSTANT(target, S_IFMT);
NODE_DEFINE_CONSTANT(target, S_IFREG);
NODE_DEFINE_CONSTANT(target, S_IFDIR);
@ -1250,6 +1246,12 @@ void DefineDLOpenConstants(Local<Object> target) {
#endif
}
void DefineInternalConstants(Local<Object> target) {
// Define module specific constants
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
}
void DefineTraceConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_BEGIN);
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_END);
@ -1307,16 +1309,19 @@ void CreatePerContextProperties(Local<Object> target,
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
Local<Object> trace_constants =
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
Local<Object> internal_constants =
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
DefineErrnoConstants(err_constants);
DefineWindowsErrorConstants(err_constants);
DefineSignalConstants(sig_constants);
DefinePriorityConstants(priority_constants);
DefineSystemConstants(fs_constants);
DefineFsConstants(fs_constants);
DefineCryptoConstants(crypto_constants);
DefineZlibConstants(zlib_constants);
DefineDLOpenConstants(dlopen_constants);
DefineTraceConstants(trace_constants);
DefineInternalConstants(internal_constants);
// Define libuv constants.
NODE_DEFINE_CONSTANT(os_constants, UV_UDP_REUSEADDR);
@ -1362,6 +1367,11 @@ void CreatePerContextProperties(Local<Object> target,
FIXED_ONE_BYTE_STRING(isolate, "trace"),
trace_constants)
.Check();
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(isolate, "internal"),
internal_constants)
.Check();
}
} // namespace constants

View File

@ -7,7 +7,7 @@ const constants = internalBinding('constants');
const assert = require('assert');
assert.deepStrictEqual(
Object.keys(constants).sort(), ['crypto', 'fs', 'os', 'trace', 'zlib']
Object.keys(constants).sort(), ['crypto', 'fs', 'internal', 'os', 'trace', 'zlib']
);
assert.deepStrictEqual(
@ -28,6 +28,6 @@ function test(obj) {
}
[
constants, constants.crypto, constants.fs, constants.os, constants.trace,
constants, constants.crypto, constants.fs, constants.internal, constants.os, constants.trace,
constants.zlib, constants.os.dlopen, constants.os.errno, constants.os.signals,
].forEach(test);

View File

@ -6,3 +6,72 @@ 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 null prototype.
assert.strictEqual(Object.getPrototypeOf(fs.constants), null);
const knownFsConstantNames = [
'UV_FS_SYMLINK_DIR',
'UV_FS_SYMLINK_JUNCTION',
'O_RDONLY',
'O_WRONLY',
'O_RDWR',
'UV_DIRENT_UNKNOWN',
'UV_DIRENT_FILE',
'UV_DIRENT_DIR',
'UV_DIRENT_LINK',
'UV_DIRENT_FIFO',
'UV_DIRENT_SOCKET',
'UV_DIRENT_CHAR',
'UV_DIRENT_BLOCK',
'S_IFMT',
'S_IFREG',
'S_IFDIR',
'S_IFCHR',
'S_IFBLK',
'S_IFIFO',
'S_IFLNK',
'S_IFSOCK',
'O_CREAT',
'O_EXCL',
'UV_FS_O_FILEMAP',
'O_NOCTTY',
'O_TRUNC',
'O_APPEND',
'O_DIRECTORY',
'O_EXCL',
'O_NOATIME',
'O_NOFOLLOW',
'O_SYNC',
'O_DSYNC',
'O_SYMLINK',
'O_DIRECT',
'O_NONBLOCK',
'S_IRWXU',
'S_IRUSR',
'S_IWUSR',
'S_IXUSR',
'S_IRWXG',
'S_IRGRP',
'S_IWGRP',
'S_IXGRP',
'S_IRWXO',
'S_IROTH',
'S_IWOTH',
'S_IXOTH',
'F_OK',
'R_OK',
'W_OK',
'X_OK',
'UV_FS_COPYFILE_EXCL',
'COPYFILE_EXCL',
'UV_FS_COPYFILE_FICLONE',
'COPYFILE_FICLONE',
'UV_FS_COPYFILE_FICLONE_FORCE',
'COPYFILE_FICLONE_FORCE',
];
const fsConstantNames = Object.keys(fs.constants);
const unknownFsConstantNames = fsConstantNames.filter((constant) => {
return !knownFsConstantNames.includes(constant);
});
assert.deepStrictEqual(unknownFsConstantNames, [], `Unknown fs.constants: ${unknownFsConstantNames.join(', ')}`);

View File

@ -191,8 +191,6 @@ export interface ConstantsBinding {
COPYFILE_FICLONE: 2;
UV_FS_COPYFILE_FICLONE_FORCE: 4;
COPYFILE_FICLONE_FORCE: 4;
EXTENSIONLESS_FORMAT_JAVASCRIPT: 0;
EXTENSIONLESS_FORMAT_WASM: 1;
};
crypto: {
OPENSSL_VERSION_NUMBER: 269488319;
@ -389,4 +387,8 @@ export interface ConstantsBinding {
TRACE_EVENT_PHASE_LEAVE_CONTEXT: 41;
TRACE_EVENT_PHASE_LINK_IDS: 61;
};
internal: {
EXTENSIONLESS_FORMAT_JAVASCRIPT: 0;
EXTENSIONLESS_FORMAT_WASM: 1;
};
}

View File

@ -235,7 +235,7 @@ declare namespace InternalFSBinding {
function writeString(fd: number, value: string, pos: unknown, encoding: unknown, req: undefined, ctx: FSSyncContext): number;
function writeString(fd: number, value: string, pos: unknown, encoding: unknown, usePromises: typeof kUsePromises): Promise<number>;
function getFormatOfExtensionlessFile(url: string): ConstantsBinding['fs'];
function getFormatOfExtensionlessFile(url: string): ConstantsBinding['internal'];
function writeFileUtf8(path: string, data: string, flag: number, mode: number): void;
function writeFileUtf8(fd: number, data: string, flag: number, mode: number): void;