| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 426ed0d commit 1c50714
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -389,7 +389,7 @@ function initializePolicy() { | |||
| 389 | 389 | } | |
| 390 | 390 | ||
| 391 | 391 | function initializeCJSLoader() { | |
| 392 | - require('internal/modules/cjs/loader')._initPaths(); | ||
| 392 | + require('internal/modules/cjs/loader').Module._initPaths(); | ||
| 393 | 393 | } | |
| 394 | 394 | ||
| 395 | 395 | function initializeESMLoader() { | |
@@ -438,7 +438,9 @@ function loadPreloadModules() { | |||
| 438 | 438 | const preloadModules = getOptionValue('--require'); | |
| 439 | 439 | if (preloadModules && preloadModules.length > 0) { | |
| 440 | 440 | const { | |
| 441 | - _preloadModules | ||
| 441 | + Module: { | ||
| 442 | + _preloadModules | ||
| 443 | + }, | ||
| 442 | 444 | } = require('internal/modules/cjs/loader'); | |
| 443 | 445 | _preloadModules(preloadModules); | |
| 444 | 446 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,14 +13,15 @@ const { | |||
| 13 | 13 | ||
| 14 | 14 | const { pathToFileURL } = require('url'); | |
| 15 | 15 | ||
| 16 | - const vm = require('vm'); | ||
| 17 | 16 | const { | |
| 18 | - stripShebang, stripBOM | ||
| 17 | + stripShebangOrBOM, | ||
| 19 | 18 | } = require('internal/modules/cjs/helpers'); | |
| 20 | 19 | ||
| 21 | 20 | const { | |
| 22 | - _resolveFilename: resolveCJSModuleName, | ||
| 23 | - wrap: wrapCJSModule | ||
| 21 | + Module: { | ||
| 22 | + _resolveFilename: resolveCJSModuleName, | ||
| 23 | + }, | ||
| 24 | + wrapSafe, | ||
| 24 | 25 | } = require('internal/modules/cjs/loader'); | |
| 25 | 26 | ||
| 26 | 27 | // TODO(joyeecheung): not every one of these are necessary | |
@@ -49,9 +50,6 @@ if (process.argv[1] && process.argv[1] !== '-') { | |||
| 49 | 50 | } | |
| 50 | 51 | ||
| 51 | 52 | function checkSyntax(source, filename) { | |
| 52 | - // Remove Shebang. | ||
| 53 | - source = stripShebang(source); | ||
| 54 | - | ||
| 55 | 53 | const { getOptionValue } = require('internal/options'); | |
| 56 | 54 | const experimentalModules = getOptionValue('--experimental-modules'); | |
| 57 | 55 | if (experimentalModules) { | |
@@ -70,10 +68,5 @@ function checkSyntax(source, filename) { | |||
| 70 | 68 | } | |
| 71 | 69 | } | |
| 72 | 70 | ||
| 73 | - // Remove BOM. | ||
| 74 | - source = stripBOM(source); | ||
| 75 | - // Wrap it. | ||
| 76 | - source = wrapCJSModule(source); | ||
| 77 | - // Compile the script, this will throw if it fails. | ||
| 78 | - new vm.Script(source, { displayErrors: true, filename }); | ||
| 71 | + wrapSafe(filename, stripShebangOrBOM(source)); | ||
| 79 | 72 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ const { | |||
| 6 | 6 | ||
| 7 | 7 | prepareMainThreadExecution(true); | |
| 8 | 8 | ||
| 9 | - const CJSModule = require('internal/modules/cjs/loader'); | ||
| 9 | + const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 10 | 10 | ||
| 11 | 11 | markBootstrapComplete(); | |
| 12 | 12 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,6 +131,17 @@ function stripShebang(content) { | |||
| 131 | 131 | return content; | |
| 132 | 132 | } | |
| 133 | 133 | ||
| 134 | + // Strip either the shebang or UTF BOM of a file. | ||
| 135 | + // Note that this only processes one. If both occur in | ||
| 136 | + // either order, the one that comes second is not | ||
| 137 | + // significant. | ||
| 138 | + function stripShebangOrBOM(content) { | ||
| 139 | + if (content.charCodeAt(0) === 0xFEFF) { | ||
| 140 | + return content.slice(1); | ||
| 141 | + } | ||
| 142 | + return stripShebang(content); | ||
| 143 | + } | ||
| 144 | + | ||
| 134 | 145 | const builtinLibs = [ | |
| 135 | 146 | 'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto', | |
| 136 | 147 | 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net', | |
@@ -197,5 +208,6 @@ module.exports = { | |||
| 197 | 208 | makeRequireFunction, | |
| 198 | 209 | normalizeReferrerURL, | |
| 199 | 210 | stripBOM, | |
| 200 | - stripShebang | ||
| 211 | + stripShebang, | ||
| 212 | + stripShebangOrBOM, | ||
| 201 | 213 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,11 +12,9 @@ const { | |||
| 12 | 12 | const { Buffer } = require('buffer'); | |
| 13 | 13 | ||
| 14 | 14 | const { | |
| 15 | - stripShebang, | ||
| 16 | - stripBOM, | ||
| 17 | - loadNativeModule | ||
| 15 | + stripBOM | ||
| 18 | 16 | } = require('internal/modules/cjs/helpers'); | |
| 19 | - const CJSModule = require('internal/modules/cjs/loader'); | ||
| 17 | + const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 20 | 18 | const internalURLModule = require('internal/url'); | |
| 21 | 19 | const createDynamicModule = require( | |
| 22 | 20 | 'internal/modules/esm/create_dynamic_module'); | |
@@ -80,8 +78,9 @@ translators.set('module', async function moduleStrategy(url) { | |||
| 80 | 78 | const source = `${await getSource(url)}`; | |
| 81 | 79 | maybeCacheSourceMap(url, source); | |
| 82 | 80 | debug(`Translating StandardModule ${url}`); | |
| 83 | - const module = new ModuleWrap(stripShebang(source), url); | ||
| 84 | - moduleWrap.callbackMap.set(module, { | ||
| 81 | + const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); | ||
| 82 | + const module = new ModuleWrap(source, url); | ||
| 83 | + callbackMap.set(module, { | ||
| 85 | 84 | initializeImportMeta, | |
| 86 | 85 | importModuleDynamically, | |
| 87 | 86 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,7 @@ function evalModule(source, print) { | |||
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | 55 | function evalScript(name, body, breakFirstLine, print) { | |
| 56 | - const CJSModule = require('internal/modules/cjs/loader'); | ||
| 56 | + const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 57 | 57 | const { kVmBreakFirstLineSymbol } = require('internal/util'); | |
| 58 | 58 | ||
| 59 | 59 | const cwd = tryGetCwd(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,7 @@ function sendInspectorCommand(cb, onError) { | |||
| 24 | 24 | function installConsoleExtensions(commandLineApi) { | |
| 25 | 25 | if (commandLineApi.require) { return; } | |
| 26 | 26 | const { tryGetCwd } = require('internal/process/execution'); | |
| 27 | - const CJSModule = require('internal/modules/cjs/loader'); | ||
| 27 | + const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 28 | 28 | const { makeRequireFunction } = require('internal/modules/cjs/helpers'); | |
| 29 | 29 | const consoleAPIModule = new CJSModule('<inspector console>'); | |
| 30 | 30 | const cwd = tryGetCwd(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,3 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - module.exports = require('internal/modules/cjs/loader'); | ||
| 3 | + module.exports = require('internal/modules/cjs/loader').Module; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,7 +65,7 @@ const path = require('path'); | |||
| 65 | 65 | const fs = require('fs'); | |
| 66 | 66 | const { Interface } = require('readline'); | |
| 67 | 67 | const { Console } = require('console'); | |
| 68 | - const CJSModule = require('internal/modules/cjs/loader'); | ||
| 68 | + const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 69 | 69 | const domain = require('domain'); | |
| 70 | 70 | const debug = require('internal/util/debuglog').debuglog('repl'); | |
| 71 | 71 | const { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments