Skip to content

Commit e228d6f

Browse files
committed
fs: unexpose internal constants
`EXTENSIONLESS_FORMAT_JAVASCRIPT` and `EXTENSIONLESS_FORMAT_WASM` are only used internally through binding `getFormatOfExtensionlessFile`. They should not be exposed publicly.
1 parent b197355 commit e228d6f

File tree

6 files changed

+94
-13
lines changed

6 files changed

+94
-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
const experimentalAddonModules = getOptionValue('--experimental-addon-modules');
@@ -60,7 +60,7 @@ function getFormatOfExtensionlessFile(url) {
6060
if (!experimentalWasmModules) { return 'module'; }
6161
const path = getValidatedPath(url);
6262
switch (fsBindings.getFormatOfExtensionlessFile(path)) {
63-
case fsConstants.EXTENSIONLESS_FORMAT_WASM:
63+
case internalConstants.EXTENSIONLESS_FORMAT_WASM:
6464
return 'wasm';
6565
default:
6666
return 'module';

src/node_constants.cc

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

1044-
void DefineSystemConstants(Local<Object> target) {
1044+
void DefineFsConstants(Local<Object> target) {
10451045
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_DIR);
10461046
NODE_DEFINE_CONSTANT(target, UV_FS_SYMLINK_JUNCTION);
10471047
// file access modes
@@ -1059,10 +1059,6 @@ void DefineSystemConstants(Local<Object> target) {
10591059
NODE_DEFINE_CONSTANT(target, UV_DIRENT_CHAR);
10601060
NODE_DEFINE_CONSTANT(target, UV_DIRENT_BLOCK);
10611061

1062-
// Define module specific constants
1063-
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
1064-
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
1065-
10661062
NODE_DEFINE_CONSTANT(target, S_IFMT);
10671063
NODE_DEFINE_CONSTANT(target, S_IFREG);
10681064
NODE_DEFINE_CONSTANT(target, S_IFDIR);
@@ -1250,6 +1246,12 @@ void DefineDLOpenConstants(Local<Object> target) {
12501246
#endif
12511247
}
12521248

1249+
void DefineInternalConstants(Local<Object> target) {
1250+
// Define module specific constants
1251+
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_JAVASCRIPT);
1252+
NODE_DEFINE_CONSTANT(target, EXTENSIONLESS_FORMAT_WASM);
1253+
}
1254+
12531255
void DefineTraceConstants(Local<Object> target) {
12541256
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_BEGIN);
12551257
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_END);
@@ -1307,16 +1309,19 @@ void CreatePerContextProperties(Local<Object> target,
13071309
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
13081310
Local<Object> trace_constants =
13091311
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
1312+
Local<Object> internal_constants =
1313+
Object::New(isolate, Null(isolate), nullptr, nullptr, 0);
13101314

13111315
DefineErrnoConstants(err_constants);
13121316
DefineWindowsErrorConstants(err_constants);
13131317
DefineSignalConstants(sig_constants);
13141318
DefinePriorityConstants(priority_constants);
1315-
DefineSystemConstants(fs_constants);
1319+
DefineFsConstants(fs_constants);
13161320
DefineCryptoConstants(crypto_constants);
13171321
DefineZlibConstants(zlib_constants);
13181322
DefineDLOpenConstants(dlopen_constants);
13191323
DefineTraceConstants(trace_constants);
1324+
DefineInternalConstants(internal_constants);
13201325

13211326
// Define libuv constants.
13221327
NODE_DEFINE_CONSTANT(os_constants, UV_UDP_REUSEADDR);
@@ -1362,6 +1367,11 @@ void CreatePerContextProperties(Local<Object> target,
13621367
FIXED_ONE_BYTE_STRING(isolate, "trace"),
13631368
trace_constants)
13641369
.Check();
1370+
target
1371+
->Set(env->context(),
1372+
FIXED_ONE_BYTE_STRING(isolate, "internal"),
1373+
internal_constants)
1374+
.Check();
13651375
}
13661376

13671377
} // 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)