@@ -40,7 +40,7 @@ const {
40
40
makeRequireFunction,
41
41
normalizeReferrerURL,
42
42
stripBOM,
43
- stripShebang
43
+ stripShebangOrBOM ,
44
44
} = require ( 'internal/modules/cjs/helpers' ) ;
45
45
const { getOptionValue } = require ( 'internal/options' ) ;
46
46
const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
@@ -59,7 +59,7 @@ const {
59
59
const { validateString } = require ( 'internal/validators' ) ;
60
60
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
61
61
62
- module . exports = Module ;
62
+ module . exports = { wrapSafe , Module } ;
63
63
64
64
let asyncESM ;
65
65
let ModuleJob ;
@@ -690,22 +690,10 @@ Module.prototype.require = function(id) {
690
690
var resolvedArgv ;
691
691
let hasPausedEntry = false ;
692
692
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 ) {
706
694
if ( patched ) {
707
695
const wrapper = Module . wrap ( content ) ;
708
- compiledWrapper = vm . runInThisContext ( wrapper , {
696
+ return vm . runInThisContext ( wrapper , {
709
697
filename,
710
698
lineOffset : 0 ,
711
699
displayErrors : true ,
@@ -714,35 +702,54 @@ Module.prototype._compile = function(content, filename) {
714
702
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
715
703
} : undefined ,
716
704
} ) ;
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
- }
744
705
}
745
706
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
+
746
753
var inspectorWrapper = null ;
747
754
if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
748
755
if ( ! resolvedArgv ) {
@@ -782,7 +789,7 @@ Module.prototype._compile = function(content, filename) {
782
789
// Native extension for .js
783
790
Module . _extensions [ '.js' ] = function ( module , filename ) {
784
791
const content = fs . readFileSync ( filename , 'utf8' ) ;
785
- module . _compile ( stripBOM ( content ) , filename ) ;
792
+ module . _compile ( content , filename ) ;
786
793
} ;
787
794
788
795
0 commit comments