Skip to content
Permalink
Browse files

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: #27674
Reviewed-By: Guy Bedford <guybedford@gmail.com>
  • Loading branch information...
BridgeAR authored and targos committed May 13, 2019
1 parent 5ee6ecd commit 9b248e33de86e33e73c68a8701f26f1b694f13ec
Showing with 10 additions and 0 deletions.
  1. +10 −0 lib/internal/modules/esm/translators.js
@@ -124,6 +124,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 = {

0 comments on commit 9b248e3

Please sign in to comment.
You can’t perform that action at this time.