@@ -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 { validateString } = require('internal/validators');
7372const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
7473const experimentalExports = getOptionValue ( '--experimental-exports' ) ;
7574
76- module . exports = Module ;
75+ module . exports = { wrapSafe , Module } ;
7776
7877let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
7978
@@ -857,26 +856,10 @@ Module.prototype.require = function(id) {
857856let resolvedArgv ;
858857let hasPausedEntry = false ;
859858
860- // Run the file contents in the correct scope or sandbox. Expose
861- // the correct helper variables (require, module, exports) to
862- // the file.
863- // Returns exception, if any.
864- Module . prototype . _compile = function ( content , filename ) {
865- let moduleURL ;
866- let redirects ;
867- if ( manifest ) {
868- moduleURL = pathToFileURL ( filename ) ;
869- redirects = manifest . getRedirector ( moduleURL ) ;
870- manifest . assertIntegrity ( moduleURL , content ) ;
871- }
872-
873- content = stripShebang ( content ) ;
874- maybeCacheSourceMap ( filename , content , this ) ;
875-
876- let compiledWrapper ;
859+ function wrapSafe ( filename , content ) {
877860 if ( patched ) {
878861 const wrapper = Module . wrap ( content ) ;
879- compiledWrapper = vm . runInThisContext ( wrapper , {
862+ return vm . runInThisContext ( wrapper , {
880863 filename,
881864 lineOffset : 0 ,
882865 displayErrors : true ,
@@ -885,46 +868,61 @@ Module.prototype._compile = function(content, filename) {
885868 return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
886869 } : undefined ,
887870 } ) ;
888- } else {
889- let compiled ;
890- try {
891- compiled = compileFunction (
892- content ,
893- filename ,
894- 0 ,
895- 0 ,
896- undefined ,
897- false ,
898- undefined ,
899- [ ] ,
900- [
901- 'exports' ,
902- 'require' ,
903- 'module' ,
904- '__filename' ,
905- '__dirname' ,
906- ]
907- ) ;
908- } catch ( err ) {
909- if ( experimentalModules ) {
910- enrichCJSError ( err ) ;
871+ }
872+
873+ let compiledWrapper ;
874+ try {
875+ compiledWrapper = compileFunction (
876+ content ,
877+ filename ,
878+ 0 ,
879+ 0 ,
880+ undefined ,
881+ false ,
882+ undefined ,
883+ [ ] ,
884+ [
885+ 'exports' ,
886+ 'require' ,
887+ 'module' ,
888+ '__filename' ,
889+ '__dirname' ,
890+ ]
891+ ) ;
892+ } catch ( err ) {
893+ enrichCJSError ( err ) ;
894+ throw err ;
895+ }
896+
897+ if ( experimentalModules ) {
898+ const { callbackMap } = internalBinding ( 'module_wrap' ) ;
899+ callbackMap . set ( compiledWrapper , {
900+ importModuleDynamically : async ( specifier ) => {
901+ const loader = await asyncESM . loaderPromise ;
902+ return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
911903 }
912- throw err ;
913- }
904+ } ) ;
905+ }
914906
915- if ( experimentalModules ) {
916- const { callbackMap } = internalBinding ( 'module_wrap' ) ;
917- callbackMap . set ( compiled . cacheKey , {
918- importModuleDynamically : async ( specifier ) => {
919- const loader = await asyncESM . loaderPromise ;
920- return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
921- }
922- } ) ;
923- }
924- compiledWrapper = compiled . function ;
907+ return compiledWrapper ;
908+ }
909+
910+ // Run the file contents in the correct scope or sandbox. Expose
911+ // the correct helper variables (require, module, exports) to
912+ // the file.
913+ // Returns exception, if any.
914+ Module . prototype . _compile = function ( content , filename ) {
915+ if ( manifest ) {
916+ const moduleURL = pathToFileURL ( filename ) ;
917+ manifest . assertIntegrity ( moduleURL , content ) ;
925918 }
926919
927- let inspectorWrapper = null ;
920+ // Strip after manifest integrity check
921+ content = stripShebangOrBOM ( content ) ;
922+
923+ const compiledWrapper = wrapSafe ( filename , content ) ;
924+
925+ var inspectorWrapper = null ;
928926 if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
929927 if ( ! resolvedArgv ) {
930928 // We enter the repl if we're not given a filename argument.
@@ -988,7 +986,7 @@ Module._extensions['.js'] = function(module, filename) {
988986 }
989987 }
990988 const content = fs . readFileSync ( filename , 'utf8' ) ;
991- module . _compile ( stripBOM ( content ) , filename ) ;
989+ module . _compile ( content , filename ) ;
992990} ;
993991
994992
0 commit comments