@@ -40,7 +40,7 @@ const {
4040 makeRequireFunction,
4141 normalizeReferrerURL,
4242 stripBOM,
43- stripShebang
43+ stripShebangOrBOM ,
4444} = require ( 'internal/modules/cjs/helpers' ) ;
4545const { getOptionValue } = require ( 'internal/options' ) ;
4646const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
@@ -59,7 +59,7 @@ const {
5959const { validateString } = require ( 'internal/validators' ) ;
6060const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
6161
62- module . exports = Module ;
62+ module . exports = { wrapSafe , Module } ;
6363
6464let asyncESM ;
6565let ModuleJob ;
@@ -690,22 +690,10 @@ Module.prototype.require = function(id) {
690690var resolvedArgv ;
691691let hasPausedEntry = false ;
692692
693- // Run the file contents in the correct scope or sandbox. Expose
694- // the correct helper variables (require, module, exports) to
695- // the file.
696- // Returns exception, if any.
697- Module . prototype . _compile = function ( content , filename ) {
698- if ( manifest ) {
699- const moduleURL = pathToFileURL ( filename ) ;
700- manifest . assertIntegrity ( moduleURL , content ) ;
701- }
702-
703- content = stripShebang ( content ) ;
704-
705- let compiledWrapper ;
693+ function wrapSafe ( filename , content ) {
706694 if ( patched ) {
707695 const wrapper = Module . wrap ( content ) ;
708- compiledWrapper = vm . runInThisContext ( wrapper , {
696+ return vm . runInThisContext ( wrapper , {
709697 filename,
710698 lineOffset : 0 ,
711699 displayErrors : true ,
@@ -714,35 +702,54 @@ Module.prototype._compile = function(content, filename) {
714702 return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
715703 } : undefined ,
716704 } ) ;
717- } else {
718- compiledWrapper = compileFunction (
719- content ,
720- filename ,
721- 0 ,
722- 0 ,
723- undefined ,
724- false ,
725- undefined ,
726- [ ] ,
727- [
728- 'exports' ,
729- 'require' ,
730- 'module' ,
731- '__filename' ,
732- '__dirname' ,
733- ]
734- ) ;
735- if ( experimentalModules ) {
736- const { callbackMap } = internalBinding ( 'module_wrap' ) ;
737- callbackMap . set ( compiledWrapper , {
738- importModuleDynamically : async ( specifier ) => {
739- const loader = await asyncESM . loaderPromise ;
740- return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
741- }
742- } ) ;
743- }
744705 }
745706
707+ const compiledWrapper = compileFunction (
708+ content ,
709+ filename ,
710+ 0 ,
711+ 0 ,
712+ undefined ,
713+ false ,
714+ undefined ,
715+ [ ] ,
716+ [
717+ 'exports' ,
718+ 'require' ,
719+ 'module' ,
720+ '__filename' ,
721+ '__dirname' ,
722+ ]
723+ ) ;
724+
725+ if ( experimentalModules ) {
726+ const { callbackMap } = internalBinding ( 'module_wrap' ) ;
727+ callbackMap . set ( compiledWrapper , {
728+ importModuleDynamically : async ( specifier ) => {
729+ const loader = await asyncESM . loaderPromise ;
730+ return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
731+ }
732+ } ) ;
733+ }
734+
735+ return compiledWrapper ;
736+ }
737+
738+ // Run the file contents in the correct scope or sandbox. Expose
739+ // the correct helper variables (require, module, exports) to
740+ // the file.
741+ // Returns exception, if any.
742+ Module . prototype . _compile = function ( content , filename ) {
743+ if ( manifest ) {
744+ const moduleURL = pathToFileURL ( filename ) ;
745+ manifest . assertIntegrity ( moduleURL , content ) ;
746+ }
747+
748+ // Strip after manifest integrity check
749+ content = stripShebangOrBOM ( content ) ;
750+
751+ const compiledWrapper = wrapSafe ( filename , content ) ;
752+
746753 var inspectorWrapper = null ;
747754 if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
748755 if ( ! resolvedArgv ) {
@@ -782,7 +789,7 @@ Module.prototype._compile = function(content, filename) {
782789// Native extension for .js
783790Module . _extensions [ '.js' ] = function ( module , filename ) {
784791 const content = fs . readFileSync ( filename , 'utf8' ) ;
785- module . _compile ( stripBOM ( content ) , filename ) ;
792+ module . _compile ( content , filename ) ;
786793} ;
787794
788795
0 commit comments