Skip to content

Commit 551e0e6

Browse files
committed
vfs: align lib/internal/modules/ with upstream main
Restore all files under lib/internal/modules/ to match upstream/main exactly. VFS module loading is fully handled by Module.registerHooks() without any patches to the module loader internals.
1 parent 3f12a95 commit 551e0e6

File tree

8 files changed

+16
-68
lines changed

8 files changed

+16
-68
lines changed

lib/internal/modules/esm/load.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99

1010
const { defaultGetFormat } = require('internal/modules/esm/get_format');
1111
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
12-
const { readFileSync } = require('internal/modules/helpers');
12+
const { readFileSync } = require('fs');
1313

1414
const { Buffer: { from: BufferFrom } } = require('buffer');
1515

lib/internal/modules/esm/loader.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function newLoadCache() {
9999
}
100100

101101
const { translators } = require('internal/modules/esm/translators');
102-
103-
let defaultResolve, defaultLoadSync;
102+
const { defaultResolve } = require('internal/modules/esm/resolve');
103+
const { defaultLoadSync, throwUnknownModuleFormat } = require('internal/modules/esm/load');
104104

105105
/**
106106
* Generate message about potential race condition caused by requiring a cached module that has started
@@ -694,7 +694,6 @@ class ModuleLoader {
694694
return cachedResult;
695695
}
696696

697-
defaultResolve ??= require('internal/modules/esm/resolve').defaultResolve;
698697
const result = defaultResolve(specifier, context);
699698
this.#resolveCache.set(requestKey, parentURL, result);
700699
return result;
@@ -771,7 +770,6 @@ class ModuleLoader {
771770
if (this.#asyncLoaderHooks?.loadSync) {
772771
return this.#asyncLoaderHooks.loadSync(url, context);
773772
}
774-
defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync;
775773
return defaultLoadSync(url, context);
776774
}
777775

@@ -797,7 +795,7 @@ class ModuleLoader {
797795

798796
validateLoadResult(url, format) {
799797
if (format == null) {
800-
require('internal/modules/esm/load').throwUnknownModuleFormat(url, format);
798+
throwUnknownModuleFormat(url, format);
801799
}
802800
}
803801

lib/internal/modules/esm/resolve.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ const { realpathSync } = require('fs');
2929
const { getOptionValue } = require('internal/options');
3030
// Do not eagerly grab .manifest, it may be in TDZ
3131
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
32-
const preserveSymlinks = getOptionValue('--preserve-symlinks');
33-
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
34-
const inputTypeFlag = getOptionValue('--input-type');
3532
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
3633
const { getCWDURL, setOwnProperty } = require('internal/util');
3734
const { canParse: URLCanParse } = internalBinding('url');
@@ -49,8 +46,7 @@ const {
4946
ERR_UNSUPPORTED_DIR_IMPORT,
5047
ERR_UNSUPPORTED_RESOLVE_REQUEST,
5148
} = require('internal/errors').codes;
52-
53-
const { Module: CJSModule } = require('internal/modules/cjs/loader');
49+
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
5450
const { getConditionsSet } = require('internal/modules/esm/utils');
5551
const packageJsonReader = require('internal/modules/package_json_reader');
5652
const internalFsBinding = internalBinding('fs');
@@ -873,6 +869,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
873869
*/
874870
function resolveAsCommonJS(specifier, parentURL) {
875871
try {
872+
const { Module: CJSModule } = require('internal/modules/cjs/loader');
876873
const parent = fileURLToPath(parentURL);
877874
const tmpModule = new CJSModule(parent, null);
878875
tmpModule.paths = CJSModule._nodeModulePaths(parent);
@@ -982,7 +979,7 @@ function defaultResolve(specifier, context = {}) {
982979
// input, to avoid user confusion over how expansive the effect of the
983980
// flag should be (i.e. entry point only, package scope surrounding the
984981
// entry point, etc.).
985-
if (inputTypeFlag) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
982+
if (getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
986983
}
987984

988985
conditions = getConditionsSet(conditions);
@@ -992,7 +989,7 @@ function defaultResolve(specifier, context = {}) {
992989
specifier,
993990
parentURL,
994991
conditions,
995-
isMain ? preserveSymlinksMain : preserveSymlinks,
992+
isMain ? getOptionValue('--preserve-symlinks-main') : getOptionValue('--preserve-symlinks'),
996993
);
997994
} catch (error) {
998995
// Try to give the user a hint of what would have been the
@@ -1046,8 +1043,3 @@ module.exports = {
10461043
packageResolve,
10471044
throwIfInvalidParentURL,
10481045
};
1049-
1050-
// cycle
1051-
const {
1052-
defaultGetFormatWithoutErrors,
1053-
} = require('internal/modules/esm/get_format');

lib/internal/modules/esm/translators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ const {
2222

2323
const { BuiltinModule } = require('internal/bootstrap/realm');
2424
const assert = require('internal/assert');
25+
const { readFileSync } = require('fs');
2526
const { dirname, extname } = require('path');
2627
const {
2728
assertBufferSource,
2829
loadBuiltinModule,
29-
readFileSync,
3030
stringify,
3131
stripBOM,
3232
urlToFilename,

lib/internal/modules/helpers.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
5555
* @type {Map<string, string>}
5656
*/
5757
const realpathCache = new SafeMap();
58-
5958
/**
6059
* Resolves the path of a given `require` specifier, following symlinks.
6160
* @param {string} requestPath The `require` specifier
@@ -67,17 +66,6 @@ function toRealPath(requestPath) {
6766
});
6867
}
6968

70-
/**
71-
* Reads a file from disk via the `fs` module reference, which may be
72-
* monkey-patched by VFS to intercept reads for virtual paths.
73-
* @param {string} path The file path
74-
* @param {string|object} options Read options
75-
* @returns {string|Buffer}
76-
*/
77-
function readFileSync(path, options) {
78-
return fs.readFileSync(path, options);
79-
}
80-
8169
/** @type {Set<string>} */
8270
let cjsConditions;
8371
/** @type {string[]} */
@@ -144,7 +132,7 @@ let isSEABuiltinWarningNeeded_;
144132
function isSEABuiltinWarningNeeded() {
145133
if (isSEABuiltinWarningNeeded_ === undefined) {
146134
const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea');
147-
isSEABuiltinWarningNeeded_ = isSea && isExperimentalSeaWarningNeeded();
135+
isSEABuiltinWarningNeeded_ = isSea() && isExperimentalSeaWarningNeeded();
148136
}
149137
return isSEABuiltinWarningNeeded_;
150138
}
@@ -523,7 +511,6 @@ module.exports = {
523511
loadBuiltinModule,
524512
makeRequireFunction,
525513
normalizeReferrerURL,
526-
readFileSync,
527514
stringify,
528515
stripBOM,
529516
toRealPath,

lib/internal/modules/package_json_reader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const {
2525
} = require('internal/errors');
2626
const { kEmptyObject } = require('internal/util');
2727
const modulesBinding = internalBinding('modules');
28-
const internalFsBinding = internalBinding('fs');
2928
const path = require('path');
3029
const { validateString } = require('internal/validators');
30+
const internalFsBinding = internalBinding('fs');
3131

3232

3333
/**

lib/internal/modules/typescript.js

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ObjectPrototypeHasOwnProperty,
55
} = primordials;
66
const {
7-
validateBoolean,
87
validateOneOf,
98
validateObject,
109
validateString,
@@ -25,17 +24,9 @@ const { Buffer } = require('buffer');
2524
const {
2625
getCompileCacheEntry,
2726
saveCompileCacheEntry,
28-
cachedCodeTypes: { kStrippedTypeScript, kTransformedTypeScript, kTransformedTypeScriptWithSourceMaps },
27+
cachedCodeTypes: { kStrippedTypeScript },
2928
} = internalBinding('modules');
3029

31-
/**
32-
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
33-
* @type {function(): TypeScriptMode}
34-
*/
35-
const getTypeScriptParsingMode = getLazy(() =>
36-
(getOptionValue('--experimental-transform-types') ?
37-
(emitExperimentalWarning('Transform Types'), 'transform') : 'strip-only'),
38-
);
3930

4031
/**
4132
* Load the TypeScript parser.
@@ -109,8 +100,7 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
109100
sourceUrl = '',
110101
} = options;
111102
let { mode = 'strip' } = options;
112-
validateOneOf(mode, 'options.mode', ['strip', 'transform']);
113-
validateBoolean(sourceMap, 'options.sourceMap');
103+
validateOneOf(mode, 'options.mode', ['strip']);
114104
validateString(sourceUrl, 'options.sourceUrl');
115105
if (mode === 'strip') {
116106
validateOneOf(sourceMap, 'options.sourceMap', [false, undefined]);
@@ -122,7 +112,6 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
122112

123113
return processTypeScriptCode(code, {
124114
mode,
125-
sourceMap,
126115
filename: sourceUrl,
127116
});
128117
}
@@ -156,19 +145,6 @@ function processTypeScriptCode(code, options) {
156145
return transformedCode;
157146
}
158147

159-
/**
160-
* Get the type enum used for compile cache.
161-
* @param {TypeScriptMode} mode Mode of transpilation.
162-
* @param {boolean} sourceMap Whether source maps are enabled.
163-
* @returns {number}
164-
*/
165-
function getCachedCodeType(mode, sourceMap) {
166-
if (mode === 'transform') {
167-
if (sourceMap) { return kTransformedTypeScriptWithSourceMaps; }
168-
return kTransformedTypeScript;
169-
}
170-
return kStrippedTypeScript;
171-
}
172148

173149
/**
174150
* Performs type-stripping to TypeScript source code internally.
@@ -184,24 +160,18 @@ function stripTypeScriptModuleTypes(source, filename) {
184160
}
185161
const sourceMap = getOptionValue('--enable-source-maps');
186162

187-
const mode = getTypeScriptParsingMode();
188-
189-
// Instead of caching the compile cache status, just go into C++ to fetch it,
190-
// as checking process.env equally involves calling into C++ anyway, and
191-
// the compile cache can be enabled dynamically.
192-
const type = getCachedCodeType(mode, sourceMap);
193163
// Get a compile cache entry into the native compile cache store,
194164
// keyed by the filename. If the cache can already be loaded on disk,
195165
// cached.transpiled contains the cached string. Otherwise we should do
196166
// the transpilation and save it in the native store later using
197167
// saveCompileCacheEntry().
198-
const cached = (filename ? getCompileCacheEntry(source, filename, type) : undefined);
168+
const cached = (filename ? getCompileCacheEntry(source, filename, kStrippedTypeScript) : undefined);
199169
if (cached?.transpiled) { // TODO(joyeecheung): return Buffer here.
200170
return cached.transpiled;
201171
}
202172

203173
const options = {
204-
mode,
174+
mode: 'strip-only',
205175
sourceMap,
206176
filename,
207177
};

test/parallel/test-bootstrap-modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ if (isMainThread) {
131131
'NativeModule internal/modules/esm/assert',
132132
'NativeModule internal/modules/esm/loader',
133133
'NativeModule internal/modules/esm/load',
134+
'NativeModule internal/modules/esm/resolve',
134135
'NativeModule internal/modules/esm/translators',
135136
'NativeModule url',
136137
].forEach(expected.beforePreExec.add.bind(expected.beforePreExec));

0 commit comments

Comments
 (0)