Skip to content
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

esm: use undici/fetch data url parser #54748

Merged
merged 8 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,13 +1525,17 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
E('ERR_INVALID_URI', 'URI malformed', URIError);
E('ERR_INVALID_URL', function(input, base = null) {
E('ERR_INVALID_URL', function(input, base = null, cause = undefined) {
this.input = input;

if (base != null) {
this.base = base;
}

if (cause !== undefined) {
this.cause = cause;
}

// Don't include URL in message.
// (See https://github.com/nodejs/node/pull/38614)
return 'Invalid URL';
Expand Down
16 changes: 11 additions & 5 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const {

const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;

let fetch;
function lazyFetch(url, input = undefined) {
fetch ??= require('internal/deps/undici/undici').fetch;
return fetch(url, input);
}

/**
* @param {URL} url URL to the module
* @param {ESModuleContext} context used to decorate error messages
Expand All @@ -42,12 +48,12 @@ async function getSource(url, context) {
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
source = await readFileAsync(url);
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
if (!match) {
throw new ERR_INVALID_URL(responseURL);
try {
const response = await lazyFetch(responseURL);
source = BufferFrom(await response.arrayBuffer());
} catch (e) {
throw new ERR_INVALID_URL(responseURL, null, e);
}
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(url, supportedSchemes);
Expand Down
11 changes: 5 additions & 6 deletions test/es-module/test-esm-data-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ function createBase64URL(mime, body) {
{
const body = 'null';
const plainESMURL = createURL('invalid', body);
try {
await import(plainESMURL);
common.mustNotCall()();
} catch (e) {
assert.strictEqual(e.code, 'ERR_INVALID_URL');
}
await assert.rejects(import(plainESMURL), { code: 'ERR_UNKNOWN_MODULE_FORMAT' })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data:invalid,null is a valid data url afaik

}
{
const plainESMURL = 'data:text/javascript,export%20default%202';
Expand All @@ -112,4 +107,8 @@ function createBase64URL(mime, body) {
const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`;
await import(plainESMURL);
}
{
const plainESMURL = 'data:text/javascript,var x = "hello world?"';
await import(plainESMURL);
}
})().then(common.mustCall());
Loading