@@ -51,8 +51,7 @@ const {
51
51
makeRequireFunction,
52
52
normalizeReferrerURL,
53
53
stripBOM,
54
- stripShebang,
55
- loadNativeModule
54
+ stripShebangOrBOM,
56
55
} = require ( 'internal/modules/cjs/helpers' ) ;
57
56
const { getOptionValue } = require ( 'internal/options' ) ;
58
57
const enableSourceMaps = getOptionValue ( '--enable-source-maps' ) ;
@@ -73,7 +72,7 @@ const {
73
72
const { validateString } = require ( 'internal/validators' ) ;
74
73
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
75
74
76
- module . exports = Module ;
75
+ module . exports = { wrapSafe , Module } ;
77
76
78
77
let asyncESM , ModuleJob , ModuleWrap , kInstantiated ;
79
78
@@ -948,26 +947,10 @@ Module.prototype.require = function(id) {
948
947
let resolvedArgv ;
949
948
let hasPausedEntry = false ;
950
949
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 ) {
968
951
if ( patched ) {
969
952
const wrapper = Module . wrap ( content ) ;
970
- compiledWrapper = vm . runInThisContext ( wrapper , {
953
+ return vm . runInThisContext ( wrapper , {
971
954
filename,
972
955
lineOffset : 0 ,
973
956
displayErrors : true ,
@@ -976,46 +959,61 @@ Module.prototype._compile = function(content, filename) {
976
959
return loader . import ( specifier , normalizeReferrerURL ( filename ) ) ;
977
960
} : undefined ,
978
961
} ) ;
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 ) ) ;
1002
994
}
1003
- throw err ;
1004
- }
995
+ } ) ;
996
+ }
1005
997
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 ) ;
1016
1009
}
1017
1010
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 ;
1019
1017
if ( getOptionValue ( '--inspect-brk' ) && process . _eval == null ) {
1020
1018
if ( ! resolvedArgv ) {
1021
1019
// We enter the repl if we're not given a filename argument.
@@ -1079,7 +1077,7 @@ Module._extensions['.js'] = function(module, filename) {
1079
1077
}
1080
1078
}
1081
1079
const content = fs . readFileSync ( filename , 'utf8' ) ;
1082
- module . _compile ( stripBOM ( content ) , filename ) ;
1080
+ module . _compile ( content , filename ) ;
1083
1081
} ;
1084
1082
1085
1083
0 commit comments