-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
esm: do not lazy instantiate loaders to avoid infinite loop #47599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
init, run it from main/ files
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,16 +1,56 @@ | ||||
| 'use strict'; | ||||
|
|
||||
| const { | ||||
| ArrayPrototypePush, | ||||
| PromisePrototypeThen, | ||||
| ReflectApply, | ||||
| SafeMap, | ||||
| } = primordials; | ||||
|
|
||||
| const assert = require('internal/assert'); | ||||
| const { createModuleLoader } = require('internal/modules/esm/loader'); | ||||
| const { getOptionValue } = require('internal/options'); | ||||
| const { | ||||
| hasUncaughtExceptionCaptureCallback, | ||||
| } = require('internal/process/execution'); | ||||
| const { pathToFileURL } = require('internal/url'); | ||||
| const { kEmptyObject } = require('internal/util'); | ||||
| const { kEmptyObject, createDeferredPromise } = require('internal/util'); | ||||
|
|
||||
| let esmLoader; | ||||
| let init = false; | ||||
|
|
||||
| module.exports = { | ||||
| esmLoader: undefined, | ||||
| setESMLoader(loader) { | ||||
| module.exports.esmLoader = loader; | ||||
| get esmLoader() { | ||||
| return esmLoader ??= { __proto__: null, cjsCache: new SafeMap(), import() { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| const { promise, resolve, reject } = createDeferredPromise(); | ||||
| ArrayPrototypePush(this.importRequests, { arguments: arguments, resolve, reject }); | ||||
| return promise; | ||||
| }, importRequests: [] }; | ||||
| }, | ||||
| initIfNeeded() { | ||||
| // TODO: we could try to avoid loading ESM loader on CJS-only codebase | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should initialize the ESM loader in CJS-only code base. At the very least the ESM internals must be snapshotable before we consider this (I suspect if we somehow make that happen, e.g. #45828, which requires resolving some of the the problematic circular dependencies, the bug here already might go away..)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to, because dynamic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We have already been doing it in the main branch: node/lib/internal/modules/cjs/loader.js Line 1196 in 2ec4ef7
|
||||
| return module.exports.init(); | ||||
| }, | ||||
| init(loader = undefined) { | ||||
|
Comment on lines
+30
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit convoluted, and not clear when which pieces should be called. Could it be baked into the |
||||
| assert(!init); | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't this throw? (I would think |
||||
| init = true; | ||||
|
|
||||
| loader ??= createModuleLoader(true); | ||||
|
|
||||
| if (esmLoader != null) { | ||||
| for (const { 0: key, 1: value } of esmLoader.cjsCache) { | ||||
| // Getting back the values from the mocked loader. | ||||
| loader.cjsCache.set(key, value); | ||||
| } | ||||
| for (let i = 0; i < esmLoader.importRequests.length; i++) { | ||||
| PromisePrototypeThen( | ||||
| ReflectApply(loader.import, loader, esmLoader.importRequests[i].arguments), | ||||
| esmLoader.importRequests[i].resolve, | ||||
| esmLoader.importRequests[i].reject, | ||||
| ); | ||||
| } | ||||
| } | ||||
| esmLoader = loader; | ||||
| }, | ||||
| async loadESM(callback) { | ||||
| const { esmLoader } = module.exports; | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.