-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
esm: non file baseurls and fix cache key collision #42392
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
Open
bmeck
wants to merge
8
commits into
nodejs:main
Choose a base branch
from
bmeck:esm-non-file-baseurls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0cdd803
esm: fix baseURL for non-files
bmeck cfdd468
fixup: make import.meta properly init
bmeck ff4b7c0
fixup: oopsie
bmeck d80ab5b
fixup: oopsie
bmeck 1513e3c
fixup: lint
bmeck b6b2a91
fixup: Update lib/internal/modules/esm/loader.js
bmeck 3c518c2
fixup: review nits
bmeck 933139e
fixup: lint
bmeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const { | |
PromiseAll, | ||
RegExpPrototypeExec, | ||
SafeArrayIterator, | ||
SafeMap, | ||
SafeWeakMap, | ||
StringPrototypeStartsWith, | ||
globalThis, | ||
|
@@ -30,7 +31,7 @@ const { | |
ERR_INVALID_RETURN_VALUE, | ||
ERR_UNKNOWN_MODULE_FORMAT | ||
} = require('internal/errors').codes; | ||
const { pathToFileURL, isURLInstance, URL } = require('internal/url'); | ||
const { isURLInstance, URL } = require('internal/url'); | ||
const { | ||
isAnyArrayBuffer, | ||
isArrayBufferView, | ||
|
@@ -92,10 +93,6 @@ class ESMLoader { | |
*/ | ||
cjsCache = new SafeWeakMap(); | ||
|
||
/** | ||
* The index for assigning unique URLs to anonymous module evaluation | ||
*/ | ||
evalIndex = 0; | ||
|
||
/** | ||
* Registry of loaded modules, akin to `require.cache` | ||
|
@@ -207,8 +204,10 @@ class ESMLoader { | |
|
||
async eval( | ||
source, | ||
url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href | ||
bmeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url, | ||
baseURL | ||
) { | ||
this.baseURLs.set(url, baseURL); | ||
const evalInstance = (url) => { | ||
const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); | ||
const module = new ModuleWrap(url, undefined, source, 0, 0); | ||
|
@@ -217,6 +216,9 @@ class ESMLoader { | |
return this.import(specifier, | ||
this.getBaseURL(url), | ||
importAssertions); | ||
}, | ||
initializeImportMeta: (meta, { url }) => { | ||
this.importMetaInitialize(meta, { url }); | ||
} | ||
}); | ||
|
||
|
@@ -232,6 +234,7 @@ class ESMLoader { | |
}; | ||
} | ||
|
||
baseURLs = new SafeMap(); | ||
/** | ||
* Returns the url to use for the resolution of a given cache key url | ||
* These are not guaranteed to be the same. | ||
|
@@ -253,20 +256,29 @@ class ESMLoader { | |
* @returns {string} | ||
*/ | ||
getBaseURL(url) { | ||
let baseURL = this.baseURLs.get(url); | ||
// This will only be a string if it exists in the Map otherwise, it | ||
// doesn't have a baseURL or needs to compute the baseURL | ||
if (typeof baseURL === 'string') { | ||
return baseURL; | ||
} | ||
if ( | ||
StringPrototypeStartsWith(url, 'http:') || | ||
StringPrototypeStartsWith(url, 'https:') | ||
) { | ||
// The request & response have already settled, so they are in | ||
// fetchModule's cache, in which case, fetchModule returns | ||
// immediately and synchronously | ||
url = fetchModule(new URL(url), { parentURL: url }).resolvedHREF; | ||
baseURL = fetchModule(new URL(url), { parentURL: url }).resolvedHREF; | ||
// This should only occur if the module hasn't been fetched yet | ||
if (typeof url !== 'string') { | ||
if (typeof baseURL !== 'string') { | ||
throw new ERR_INTERNAL_ASSERTION(`Base url for module ${url} not loaded.`); | ||
} | ||
this.baseURLs.set(url, baseURL); | ||
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. why we are mutating |
||
} else { | ||
baseURL = url; | ||
} | ||
return url; | ||
return baseURL; | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
CJS is using
[stdin]
instead ofnode:stdin
for this, should we chosenode:[stdin]
instead? I don't feel strongly about this at all, but if there are no drawbacks maybe we should aim for consistency.On another note, I've noticed that this PR makes the following work:
While it doesn't seem to work at all on
master
: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.
wrapping both in
[]
seems good for consistency, then we could also use[eval]
like CJS.I'm fine removing the relative importing for these modules as long as it gives a little more consistency with CJS where you can use
require()
to get relative files from[stdin]
.