summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-05-13 14:19:28 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2019-06-15 11:23:03 +0200
commit9939762322de29224fcbc2f08c4a6a4bc08d6ccd (patch)
tree3a8bf102cab01f12db943bacad4c802becbae751 /lib
parent52255868c523145c7bc0fc7e2f7ef53aceba3e0d (diff)
downloadandroid-node-v8-9939762322de29224fcbc2f08c4a6a4bc08d6ccd.tar.gz
android-node-v8-9939762322de29224fcbc2f08c4a6a4bc08d6ccd.tar.bz2
android-node-v8-9939762322de29224fcbc2f08c4a6a4bc08d6ccd.zip
module: prevent race condition while combining import and require
This checks if any require calls have happened to the same file during the file read. If that was the case, it'll return the same module instead of creating a new instance. PR-URL: https://github.com/nodejs/node/pull/27674 Reviewed-By: Guy Bedford <guybedford@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/modules/esm/translators.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
index 9526e05263..b1f2c9f259 100644
--- a/lib/internal/modules/esm/translators.js
+++ b/lib/internal/modules/esm/translators.js
@@ -123,6 +123,16 @@ translators.set('json', async function jsonStrategy(url) {
});
}
const content = await readFileAsync(pathname, 'utf-8');
+ // A require call could have been called on the same file during loading and
+ // that resolves synchronously. To make sure we always return the identical
+ // export, we have to check again if the module already exists or not.
+ module = CJSModule._cache[modulePath];
+ if (module && module.loaded) {
+ const exports = module.exports;
+ return createDynamicModule(['default'], url, (reflect) => {
+ reflect.exports.default.set(exports);
+ });
+ }
try {
const exports = JsonParse(stripBOM(content));
module = {