@@ -51,8 +51,7 @@ const {
5151 makeRequireFunction,
5252 normalizeReferrerURL,
5353 stripBOM,
54- stripShebang,
55- loadNativeModule
54+ stripShebangOrBOM,
5655} = require ( 'internal/modules/cjs/helpers' ) ;
5756const { getOptionValue } = require ( 'internal/options' ) ;
5857const enableSourceMaps = getOptionValue ( '--enable-source-maps' ) ;
@@ -73,7 +72,7 @@ const {
7372const { validateString } = require ( 'internal/validators' ) ;
7473const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
7574
76- module . exports = Module ;
75+ module . exports = { wrapSafe , Module } ;
7776
7877let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
7978
@@ -948,26 +947,10 @@ Module.prototype.require = function(id) {
948947let resolvedArgv ;
949948let hasPausedEntry = false ;
950949
951- // Run the file contents in the correct scope or sandbox. Expose
952- // the correct helper variables (require, module, exports) to
953- // the file.
954- // Returns exception, if any.
955- Module . prototype . _compile = function ( content , filename ) {
956- let moduleURL ;
957- let redirects ;
958- if ( manifest ) {
959- moduleURL = pathToFileURL ( filename ) ;
960- redirects = manifest . getRedirector ( moduleURL ) ;
961- manifest . assertIntegrity ( moduleURL , content ) ;
962- }
963-
964- content = stripShebang ( content ) ;
965- maybeCacheSourceMap ( filename , content , this ) ;
966-
967- let compiledWrapper ;
950+ function wrapSafe ( filename , content ) {
968951 if ( patched ) {
969952 const wrapper = Module . wrap ( content ) ;
970- compiledWrapper = vm . runInThisContext ( wrapper , {
953+ return vm . runInThisContext ( wrapper , {
971954 filename,
972955 lineOffset : 0 ,
973956 displayErrors : true ,
@@ -976,46 +959,61 @@ Module.prototype._compile = function(content, filename) {
976959 return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
977960 } : undefined ,
978961 } ) ;
979- } else {
980- let compiled ;
981- try {
982- compiled = compileFunction (
983- content ,
984- filename ,
985- 0 ,
986- 0 ,
987- undefined ,
988- false ,
989- undefined ,
990- [ ] ,
991- [
992- 'exports' ,
993- 'require' ,
994- 'module' ,
995- '__filename' ,
996- '__dirname' ,
997- ]
998- ) ;
999- } catch ( err ) {
1000- if ( experimentalModules ) {
1001- enrichCJSError ( err ) ;
962+ }
963+
964+ let compiledWrapper ;
965+ try {
966+ compiledWrapper = compileFunction (
967+ content ,
968+ filename ,
969+ 0 ,
970+ 0 ,
971+ undefined ,
972+ false ,
973+ undefined ,
974+ [ ] ,
975+ [
976+ 'exports' ,
977+ 'require' ,
978+ 'module' ,
979+ '__filename' ,
980+ '__dirname' ,
981+ ]
982+ ) ;
983+ } catch ( err ) {
984+ enrichCJSError ( err ) ;
985+ throw err ;
986+ }
987+
988+ if ( experimentalModules ) {
989+ const { callbackMap } = internalBinding ( 'module_wrap' ) ;
990+ callbackMap . set ( compiledWrapper , {
991+ importModuleDynamically : async ( specifier ) => {
992+ const loader = await asyncESM . loaderPromise ;
993+ return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
1002994 }
1003- throw err ;
1004- }
995+ } ) ;
996+ }
1005997
1006- if ( experimentalModules ) {
1007- const { callbackMap } = internalBinding ( 'module_wrap' ) ;
1008- callbackMap . set ( compiled . cacheKey , {
1009- importModuleDynamically : async ( specifier ) => {
1010- const loader = await asyncESM . loaderPromise ;
1011- return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
1012- }
1013- } ) ;
1014- }
1015- compiledWrapper = compiled . function ;
998+ return compiledWrapper ;
999+ }
1000+
1001+ // Run the file contents in the correct scope or sandbox. Expose
1002+ // the correct helper variables (require, module, exports) to
1003+ // the file.
1004+ // Returns exception, if any.
1005+ Module . prototype . _compile = function ( content , filename ) {
1006+ if ( manifest ) {
1007+ const moduleURL = pathToFileURL ( filename ) ;
1008+ manifest . assertIntegrity ( moduleURL , content ) ;
10161009 }
10171010
1018- let inspectorWrapper = null ;
1011+ // Strip after manifest integrity check
1012+ content = stripShebangOrBOM ( content ) ;
1013+
1014+ const compiledWrapper = wrapSafe ( filename , content ) ;
1015+
1016+ var inspectorWrapper = null ;
10191017 if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
10201018 if ( ! resolvedArgv ) {
10211019 // We enter the repl if we're not given a filename argument.
@@ -1079,7 +1077,7 @@ Module._extensions['.js'] = function(module, filename) {
10791077 }
10801078 }
10811079 const content = fs . readFileSync ( filename , 'utf8' ) ;
1082- module . _compile ( stripBOM ( content ) , filename ) ;
1080+ module . _compile ( content , filename ) ;
10831081} ;
10841082
10851083
0 commit comments