Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup: expand code docs for ESMLoader::getBaseURL and update return…
… doc
  • Loading branch information
JakobJingleheimer committed May 20, 2022
commit 714f94ef0dfd1d882f89be2c986fd861aa79a1bf
18 changes: 10 additions & 8 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,21 @@ class ESMLoader {
* would have a cache key of https://example.com/foo and baseURL
* of https://example.com/bar
*
* MUST BE SYNCHRONOUS for import.meta initialization
* MUST BE CALLED AFTER receiving the url body due to I/O
* @param {string} url
* @returns {string}
* ! MUST BE SYNCHRONOUS for import.meta initialization
* ! MUST BE CALLED AFTER receiving the url body due to I/O
* @param {URL['href']} url
* @returns {string|Promise<URL['href']>}
*/
getBaseURL(url) {
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
// When using network-imports, the request & response have already settled
// so they are in fetchModule's cache, in which case, fetchModule returns
// immediately and synchronously
// BUT, when a custom loader is used, the cache is bypassed and it hits an
// unsettled promise.
const module = fetchModule(new URL(url), { parentURL: url });

if (typeof module?.resolvedHREF === 'string') { // [2]
Expand All @@ -360,8 +362,8 @@ class ESMLoader {
throw new ERR_INTERNAL_ASSERTION(
`Base url for module ${url} not loaded.`
);
} else {
url = module;
} else { // a custom loader was used instead of network-imports
url = module; // ! this is an unsettled promise
}
}
}
Expand Down