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

Function wrapSafe

lib/internal/modules/cjs/loader.js:1834–1884  ·  view source on GitHub ↗

* Wraps the given content in a script and runs it in a new context. * @param {string} filename The name of the file being loaded * @param {string} content The content of the file being loaded * @param {Module|undefined} cjsModuleInstance The CommonJS loader instance * @param {'commonjs'|undefine

(filename, content, cjsModuleInstance, format)

Source from the content-addressed store, hash-verified

1832 * @returns {object}
1833 */
1834function wrapSafe(filename, content, cjsModuleInstance, format) {
1835 assert(format !== 'module', 'ESM should be handled in loadESMFromCJS()');
1836 const hostDefinedOptionId = vm_dynamic_import_default_internal;
1837 const importModuleDynamically = vm_dynamic_import_default_internal;
1838 if (patched) {
1839 const wrapped = Module.wrap(content);
1840 const script = makeContextifyScript(
1841 wrapped, // code
1842 filename, // filename
1843 0, // lineOffset
1844 0, // columnOffset
1845 undefined, // cachedData
1846 false, // produceCachedData
1847 undefined, // parsingContext
1848 hostDefinedOptionId, // hostDefinedOptionId
1849 importModuleDynamically, // importModuleDynamically
1850 );
1851
1852 // Cache the source map for the module if present.
1853 const { sourceMapURL, sourceURL } = script;
1854 if (sourceMapURL) {
1855 maybeCacheSourceMap(filename, content, cjsModuleInstance, false, sourceURL, sourceMapURL);
1856 }
1857
1858 return {
1859 __proto__: null,
1860 function: runScriptInThisContext(script, true, false),
1861 sourceMapURL,
1862 };
1863 }
1864
1865 let shouldDetectModule = false;
1866 if (format !== 'commonjs') {
1867 if (cjsModuleInstance?.[kIsMainSymbol]) {
1868 // For entry points, format detection is used unless explicitly disabled.
1869 shouldDetectModule = getOptionValue('--experimental-detect-module');
1870 } else {
1871 // For modules being loaded by `require()`, if require(esm) is disabled,
1872 // don't try to reparse to detect format and just throw for ESM syntax.
1873 shouldDetectModule = getOptionValue('--require-module');
1874 }
1875 }
1876 const result = compileFunctionForCJSLoader(content, filename, false /* is_sea_main */, shouldDetectModule);
1877
1878 // Cache the source map for the module if present.
1879 if (result.sourceMapURL) {
1880 maybeCacheSourceMap(filename, content, cjsModuleInstance, false, result.sourceURL, result.sourceMapURL);
1881 }
1882
1883 return result;
1884}
1885
1886/**
1887 * Run the file contents in the correct scope or sandbox. Expose the correct helper variables (`require`, `module`,

Callers 2

loader.jsFile · 0.85
checkSyntaxFunction · 0.85

Calls 6

makeContextifyScriptFunction · 0.85
maybeCacheSourceMapFunction · 0.85
runScriptInThisContextFunction · 0.85
getOptionValueFunction · 0.85
assertFunction · 0.50
wrapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…