Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2020-08-06 03:56:17 +0300
committerAnna Henningsen <anna@addaleax.net>2020-08-08 21:33:46 +0300
commite948ef351b4111d78ae8ba08f29eb138af22fa0b (patch)
treea380cea63740bd768ebd56640d6a00ad16cd4388 /lib/internal/modules
parentf32114441c4aca6fc432897f92fc26135bf490a0 (diff)
module: handle Top-Level Await non-fulfills better
Handle situations in which the main `Promise` from a TLA module is not fulfilled better: - When not resolving the `Promise` at all, set a non-zero exit code (unless another one has been requested explicitly) to distinguish the result from a successful completion. - When rejecting the `Promise`, always treat it like an uncaught exception. In particular, this also ensures a non-zero exit code. Refs: https://github.com/nodejs/node/pull/34558 PR-URL: https://github.com/nodejs/node/pull/34640 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r--lib/internal/modules/run_main.js19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js
index 0967ef539ca..d7b0ee56a1e 100644
--- a/lib/internal/modules/run_main.js
+++ b/lib/internal/modules/run_main.js
@@ -40,11 +40,23 @@ function shouldUseESMLoader(mainPath) {
function runMainESM(mainPath) {
const esmLoader = require('internal/process/esm_loader');
const { pathToFileURL } = require('internal/url');
- esmLoader.loadESM((ESMLoader) => {
+ handleMainPromise(esmLoader.loadESM((ESMLoader) => {
const main = path.isAbsolute(mainPath) ?
pathToFileURL(mainPath).href : mainPath;
return ESMLoader.import(main);
- });
+ }));
+}
+
+function handleMainPromise(promise) {
+ // Handle a Promise from running code that potentially does Top-Level Await.
+ // In that case, it makes sense to set the exit code to a specific non-zero
+ // value if the main code never finishes running.
+ function handler() {
+ if (process.exitCode === undefined)
+ process.exitCode = 13;
+ }
+ process.on('exit', handler);
+ return promise.finally(() => process.off('exit', handler));
}
// For backwards compatibility, we have to run a bunch of
@@ -62,5 +74,6 @@ function executeUserEntryPoint(main = process.argv[1]) {
}
module.exports = {
- executeUserEntryPoint
+ executeUserEntryPoint,
+ handleMainPromise,
};