diff --git a/lib/internal/modules/esm/formats.js b/lib/internal/modules/esm/formats.js index ff0bed228d4e74..fc9f21c909cba1 100644 --- a/lib/internal/modules/esm/formats.js +++ b/lib/internal/modules/esm/formats.js @@ -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'; diff --git a/src/node_constants.cc b/src/node_constants.cc index 906e1f6a632d9b..aebda8fb44b919 100644 --- a/src/node_constants.cc +++ b/src/node_constants.cc @@ -1041,7 +1041,7 @@ void DefineCryptoConstants(Local target) { #endif } -void DefineSystemConstants(Local target) { +void DefineFsConstants(Local 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 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 target) { #endif } +void DefineInternalConstants(Local target) { + // Define module specific constants + NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT); + NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM); +} + void DefineTraceConstants(Local target) { NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_BEGIN); NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_END); @@ -1307,16 +1309,19 @@ void CreatePerContextProperties(Local target, Object::New(isolate, Null(isolate), nullptr, nullptr, 0); Local trace_constants = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); + Local 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 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 diff --git a/test/parallel/test-binding-constants.js b/test/parallel/test-binding-constants.js index 4a96b7c7443fc6..d57899be50642b 100644 --- a/test/parallel/test-binding-constants.js +++ b/test/parallel/test-binding-constants.js @@ -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); diff --git a/test/parallel/test-fs-constants.js b/test/parallel/test-fs-constants.js index 49bcabd80873b4..cdecfe5591631a 100644 --- a/test/parallel/test-fs-constants.js +++ b/test/parallel/test-fs-constants.js @@ -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(', ')}`); diff --git a/typings/internalBinding/constants.d.ts b/typings/internalBinding/constants.d.ts index dc4657080ba54b..2c645d99f17679 100644 --- a/typings/internalBinding/constants.d.ts +++ b/typings/internalBinding/constants.d.ts @@ -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; + }; } diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index 7e3f03de22d026..e666725d39ac77 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -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; - 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;