Skip to content

fs: unexpose internal constants #58327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down
22 changes: 16 additions & 6 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-binding-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
69 changes: 69 additions & 0 deletions test/parallel/test-fs-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}`);
6 changes: 4 additions & 2 deletions typings/internalBinding/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
}
2 changes: 1 addition & 1 deletion typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading