Skip to content

Commit 72a1b06

Browse files
legendecasaduh95
authored andcommitted
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: #58327 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent b9a16e9 commit 72a1b06

File tree

6 files changed

+96
-13
lines changed

6 files changed

+96
-13
lines changed

lib/internal/modules/esm/formats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
const { getOptionValue } = require('internal/options');
88
const { getValidatedPath } = require('internal/fs/utils');
99
const fsBindings = internalBinding('fs');
10-
const { fs: fsConstants } = internalBinding('constants');
10+
const { internal: internalConstants } = internalBinding('constants');
1111

1212
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
1313

@@ -55,7 +55,7 @@ function getFormatOfExtensionlessFile(url) {
5555
if (!experimentalWasmModules) { return 'module'; }
5656
const path = getValidatedPath(url);
5757
switch (fsBindings.getFormatOfExtensionlessFile(path)) {
58-
case fsConstants.EXTENSIONLESS_FORMAT_WASM:
58+
case internalConstants.EXTENSIONLESS_FORMAT_WASM:
5959
return 'wasm';
6060
default:
6161
return 'module';

src/node_constants.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ void DefineCryptoConstants(Local<Object> target) {
10401040
#endif
10411041
}
10421042

1043-
void DefineSystemConstants(Local<Object> target) {
1043+
void DefineFsConstants(Local<Object> target) {
10441044
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_DIR);
10451045
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_JUNCTION);
10461046
// file access modes
@@ -1058,10 +1058,6 @@ void DefineSystemConstants(Local<Object> target) {
10581058
NODE_DEFINE_CONSTANT(target, UV_DIRENT_CHAR);
10591059
NODE_DEFINE_CONSTANT(target, UV_DIRENT_BLOCK);
10601060

1061-
// Define module specific constants
1062-
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
1063-
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
1064-
10651061
NODE_DEFINE_CONSTANT(target, S_IFMT);
10661062
NODE_DEFINE_CONSTANT(target, S_IFREG);
10671063
NODE_DEFINE_CONSTANT(target, S_IFDIR);
@@ -1249,6 +1245,12 @@ void DefineDLOpenConstants(Local<Object> target) {
12491245
#endif
12501246
}
12511247

1248+
void DefineInternalConstants(Local<Object> target) {
1249+
// Define module specific constants
1250+
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
1251+
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
1252+
}
1253+
12521254
void DefineTraceConstants(Local<Object> target) {
12531255
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_BEGIN);
12541256
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_END);
@@ -1323,15 +1325,20 @@ void CreatePerContextProperties(Local<Object> target,
13231325
CHECK(trace_constants->SetPrototype(env->context(),
13241326
Null(env->isolate())).FromJust());
13251327

1328+
Local<Object> internal_constants = Object::New(isolate);
1329+
CHECK(internal_constants->SetPrototype(env->context(),
1330+
Null(env->isolate())).FromJust());
1331+
13261332
DefineErrnoConstants(err_constants);
13271333
DefineWindowsErrorConstants(err_constants);
13281334
DefineSignalConstants(sig_constants);
13291335
DefinePriorityConstants(priority_constants);
1330-
DefineSystemConstants(fs_constants);
1336+
DefineFsConstants(fs_constants);
13311337
DefineCryptoConstants(crypto_constants);
13321338
DefineZlibConstants(zlib_constants);
13331339
DefineDLOpenConstants(dlopen_constants);
13341340
DefineTraceConstants(trace_constants);
1341+
DefineInternalConstants(internal_constants);
13351342

13361343
// Define libuv constants.
13371344
NODE_DEFINE_CONSTANT(os_constants, UV_UDP_REUSEADDR);
@@ -1377,6 +1384,11 @@ void CreatePerContextProperties(Local<Object> target,
13771384
FIXED_ONE_BYTE_STRING(isolate, "trace"),
13781385
trace_constants)
13791386
.Check();
1387+
target
1388+
->Set(env->context(),
1389+
FIXED_ONE_BYTE_STRING(isolate, "internal"),
1390+
internal_constants)
1391+
.Check();
13801392
}
13811393

13821394
} // namespace constants

test/parallel/test-binding-constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const constants = internalBinding('constants');
77
const assert = require('assert');
88

99
assert.deepStrictEqual(
10-
Object.keys(constants).sort(), ['crypto', 'fs', 'os', 'trace', 'zlib']
10+
Object.keys(constants).sort(), ['crypto', 'fs', 'internal', 'os', 'trace', 'zlib']
1111
);
1212

1313
assert.deepStrictEqual(
@@ -28,6 +28,6 @@ function test(obj) {
2828
}
2929

3030
[
31-
constants, constants.crypto, constants.fs, constants.os, constants.trace,
31+
constants, constants.crypto, constants.fs, constants.internal, constants.os, constants.trace,
3232
constants.zlib, constants.os.dlopen, constants.os.errno, constants.os.signals,
3333
].forEach(test);

test/parallel/test-fs-constants.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,72 @@ const assert = require('assert');
66
// Check if the two constants accepted by chmod() on Windows are defined.
77
assert.notStrictEqual(fs.constants.S_IRUSR, undefined);
88
assert.notStrictEqual(fs.constants.S_IWUSR, undefined);
9+
10+
// Check null prototype.
11+
assert.strictEqual(Object.getPrototypeOf(fs.constants), null);
12+
13+
const knownFsConstantNames = [
14+
'UV_FS_SYMLINK_DIR',
15+
'UV_FS_SYMLINK_JUNCTION',
16+
'O_RDONLY',
17+
'O_WRONLY',
18+
'O_RDWR',
19+
'UV_DIRENT_UNKNOWN',
20+
'UV_DIRENT_FILE',
21+
'UV_DIRENT_DIR',
22+
'UV_DIRENT_LINK',
23+
'UV_DIRENT_FIFO',
24+
'UV_DIRENT_SOCKET',
25+
'UV_DIRENT_CHAR',
26+
'UV_DIRENT_BLOCK',
27+
'S_IFMT',
28+
'S_IFREG',
29+
'S_IFDIR',
30+
'S_IFCHR',
31+
'S_IFBLK',
32+
'S_IFIFO',
33+
'S_IFLNK',
34+
'S_IFSOCK',
35+
'O_CREAT',
36+
'O_EXCL',
37+
'UV_FS_O_FILEMAP',
38+
'O_NOCTTY',
39+
'O_TRUNC',
40+
'O_APPEND',
41+
'O_DIRECTORY',
42+
'O_EXCL',
43+
'O_NOATIME',
44+
'O_NOFOLLOW',
45+
'O_SYNC',
46+
'O_DSYNC',
47+
'O_SYMLINK',
48+
'O_DIRECT',
49+
'O_NONBLOCK',
50+
'S_IRWXU',
51+
'S_IRUSR',
52+
'S_IWUSR',
53+
'S_IXUSR',
54+
'S_IRWXG',
55+
'S_IRGRP',
56+
'S_IWGRP',
57+
'S_IXGRP',
58+
'S_IRWXO',
59+
'S_IROTH',
60+
'S_IWOTH',
61+
'S_IXOTH',
62+
'F_OK',
63+
'R_OK',
64+
'W_OK',
65+
'X_OK',
66+
'UV_FS_COPYFILE_EXCL',
67+
'COPYFILE_EXCL',
68+
'UV_FS_COPYFILE_FICLONE',
69+
'COPYFILE_FICLONE',
70+
'UV_FS_COPYFILE_FICLONE_FORCE',
71+
'COPYFILE_FICLONE_FORCE',
72+
];
73+
const fsConstantNames = Object.keys(fs.constants);
74+
const unknownFsConstantNames = fsConstantNames.filter((constant) => {
75+
return !knownFsConstantNames.includes(constant);
76+
});
77+
assert.deepStrictEqual(unknownFsConstantNames, [], `Unknown fs.constants: ${unknownFsConstantNames.join(', ')}`);

typings/internalBinding/constants.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ export interface ConstantsBinding {
191191
COPYFILE_FICLONE: 2;
192192
UV_FS_COPYFILE_FICLONE_FORCE: 4;
193193
COPYFILE_FICLONE_FORCE: 4;
194-
EXTENSIONLESS_FORMAT_JAVASCRIPT: 0;
195-
EXTENSIONLESS_FORMAT_WASM: 1;
196194
};
197195
crypto: {
198196
OPENSSL_VERSION_NUMBER: 269488319;
@@ -389,4 +387,8 @@ export interface ConstantsBinding {
389387
TRACE_EVENT_PHASE_LEAVE_CONTEXT: 41;
390388
TRACE_EVENT_PHASE_LINK_IDS: 61;
391389
};
390+
internal: {
391+
EXTENSIONLESS_FORMAT_JAVASCRIPT: 0;
392+
EXTENSIONLESS_FORMAT_WASM: 1;
393+
};
392394
}

typings/internalBinding/fs.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ declare namespace InternalFSBinding {
235235
function writeString(fd: number, value: string, pos: unknown, encoding: unknown, req: undefined, ctx: FSSyncContext): number;
236236
function writeString(fd: number, value: string, pos: unknown, encoding: unknown, usePromises: typeof kUsePromises): Promise<number>;
237237

238-
function getFormatOfExtensionlessFile(url: string): ConstantsBinding['fs'];
238+
function getFormatOfExtensionlessFile(url: string): ConstantsBinding['internal'];
239239

240240
function writeFileUtf8(path: string, data: string, flag: number, mode: number): void;
241241
function writeFileUtf8(fd: number, data: string, flag: number, mode: number): void;

0 commit comments

Comments
 (0)