Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae7789a

Browse filesBrowse files
committed
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>
1 parent c574b57 commit ae7789a
Copy full SHA for ae7789a

File tree

Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ translators.set('json', async function jsonStrategy(url) {
124124
});
125125
}
126126
const content = await readFileAsync(pathname, 'utf-8');
127+
// A require call could have been called on the same file during loading and
128+
// that resolves synchronously. To make sure we always return the identical
129+
// export, we have to check again if the module already exists or not.
130+
module = CJSModule._cache[modulePath];
131+
if (module && module.loaded) {
132+
const exports = module.exports;
133+
return createDynamicModule(['default'], url, (reflect) => {
134+
reflect.exports.default.set(exports);
135+
});
136+
}
127137
try {
128138
const exports = JsonParse(stripBOM(content));
129139
module = {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.