Skip to content

Commit

Permalink
esm: align sync and async load implementations
Browse files Browse the repository at this point in the history
Refs: #48272
PR-URL: #49152
Refs: #47999
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
  • Loading branch information
aduh95 authored and UlisesGascon committed Sep 10, 2023
1 parent 910378f commit a6cfea3
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,30 @@ async function getSource(url, context) {
return { __proto__: null, responseURL, source };
}

/**
* @param {URL} url URL to the module
* @param {ESModuleContext} context used to decorate error messages
* @returns {{ responseURL: string, source: string | BufferView }}
*/
function getSourceSync(url, context) {
const parsed = new URL(url);
const responseURL = url;
const { protocol, href } = url;
const responseURL = href;
let source;
if (parsed.protocol === 'file:') {
source = readFileSync(parsed);
} else if (parsed.protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
if (protocol === 'file:') {
source = readFileSync(url);
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
if (!match) {
throw new ERR_INVALID_URL(url);
throw new ERR_INVALID_URL(responseURL);
}
const { 1: base64, 2: body } = match;
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
} else {
const supportedSchemes = ['file', 'data'];
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
}
if (policy?.manifest) {
policy.manifest.assertIntegrity(parsed, source);
policy.manifest.assertIntegrity(url, source);
}
return { __proto__: null, responseURL, source };
}
Expand Down Expand Up @@ -159,14 +164,18 @@ function defaultLoadSync(url, context = kEmptyObject) {
source,
} = context;

format ??= defaultGetFormat(new URL(url), context);
const urlInstance = new URL(url);

throwIfUnsupportedURLScheme(urlInstance, false);

format ??= defaultGetFormat(urlInstance, context);

validateAssertions(url, format, importAssertions);

if (format === 'builtin') {
source = null;
} else if (source == null) {
({ responseURL, source } = getSourceSync(url, context));
({ responseURL, source } = getSourceSync(urlInstance, context));
}

return {
Expand Down

0 comments on commit a6cfea3

Please sign in to comment.