MCPcopy Index your code
hub / github.com/nodejs/node / loadCJSModuleWithSpecialRequire

Function loadCJSModuleWithSpecialRequire

lib/internal/modules/esm/translators.js:112–191  ·  view source on GitHub ↗

* This may be eventually removed when module.register() reaches end-of-life. * * Loads a CommonJS module via the ESM Loader sync CommonJS translator. * This translator creates its own version of the `require` function passed into CommonJS modules. * Any monkey patches applied to the CommonJS Loa

(module, source, url, filename, isMain, translateContext)

Source from the content-addressed store, hash-verified

110 * @param {TranslateContext} translateContext Context for the translator
111 */
112function loadCJSModuleWithSpecialRequire(module, source, url, filename, isMain, translateContext) {
113 // Use the full URL as the V8 resource name so that any search params
114 // (e.g. ?node-test-mock) are preserved in coverage reports.
115 const compileResult = compileFunctionForCJSLoader(source, url, false /* is_sea_main */, false);
116
117 const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
118 // Cache the source map for the cjs module if present.
119 if (sourceMapURL) {
120 maybeCacheSourceMap(url, source, module, false, sourceURL, sourceMapURL);
121 }
122 const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
123 const __dirname = dirname(filename);
124 // eslint-disable-next-line func-name-matching,func-style
125 const requireFn = function require(specifier) {
126 let importAttributes = kEmptyObject;
127 if (!StringPrototypeStartsWith(specifier, 'node:') && !BuiltinModule.normalizeRequirableId(specifier)) {
128 // TODO: do not depend on the monkey-patchable CJS loader here.
129 const path = CJSModule._resolveFilename(specifier, module);
130 switch (extname(path)) {
131 case '.json':
132 importAttributes = { __proto__: null, type: 'json' };
133 break;
134 case '.node':
135 // If it gets here in the translators, the hooks must have already been invoked
136 // in the loader. Skip them in the synthetic module evaluation step.
137 return wrapModuleLoad(specifier, module, false, kShouldSkipModuleHooks);
138 default:
139 // fall through
140 }
141 specifier = `${pathToFileURL(path)}`;
142 }
143
144 // NOTE: This re-invented require() is only used on the loader-hook worker thread.
145 // On the main thread, the authentic require() is used instead (fixed by #60380).
146 const request = { specifier, attributes: importAttributes, phase: kEvaluationPhase, __proto__: null };
147 const job = cascadedLoader.getOrCreateModuleJob(url, request, kRequireInImportedCJS);
148 job.runSync();
149 let mod = cjsCache.get(job.url);
150 assert(job.module, `Imported CJS module ${url} failed to load module ${job.url} using require() due to race condition`);
151
152 if (job.module.synthetic) {
153 assert(mod, `Imported CJS module ${url} failed to load module ${job.url} using require() due to missed cache`);
154 return mod.exports;
155 }
156
157 // The module being required is a source text module.
158 if (!mod) {
159 mod = cjsEmplaceModuleCacheEntry(job.url);
160 cjsCache.set(job.url, mod);
161 }
162 // The module has been cached by the re-invented require. Update the exports object
163 // from the namespace object and return the evaluated exports.
164 if (!mod.loaded) {
165 debug('populateCJSExportsFromESM from require(esm) in imported CJS', url, mod, job.module);
166 populateCJSExportsFromESM(mod, job.module, job.module.getNamespace());
167 mod.loaded = true;
168 }
169 return mod.exports;

Callers 1

createCJSModuleWrapFunction · 0.85

Calls 8

maybeCacheSourceMapFunction · 0.85
dirnameFunction · 0.85
setOwnPropertyFunction · 0.85
resolveForCJSWithHooksFunction · 0.85
urlToFilenameFunction · 0.85
requireFunction · 0.50
pathToFileURLFunction · 0.50
resolveSyncMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…