diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 5239bc8ed883a5..cb1c56cd7ff26f 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -30,7 +30,7 @@ const { ERR_UNSUPPORTED_ESM_URL_SCHEME, } = require('internal/errors').codes; -const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; +const DATA_URL_PATTERN = /^data:[^/#]+\/[^,;#]+(?:[^,#]*?)(;base64)?,([^#]*)/; /** * @param {URL} url URL to the module @@ -45,7 +45,7 @@ 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); + const match = RegExpPrototypeExec(DATA_URL_PATTERN, href); if (!match) { throw new ERR_INVALID_URL(responseURL); } @@ -84,7 +84,7 @@ function getSourceSync(url, context) { if (protocol === 'file:') { source = readFileSync(url); } else if (protocol === 'data:') { - const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname); + const match = RegExpPrototypeExec(DATA_URL_PATTERN, href); if (!match) { throw new ERR_INVALID_URL(responseURL); } diff --git a/test/es-module/test-esm-data-urls.js b/test/es-module/test-esm-data-urls.js index 0817cf179df340..67fa1f69c3eba1 100644 --- a/test/es-module/test-esm-data-urls.js +++ b/test/es-module/test-esm-data-urls.js @@ -112,4 +112,7 @@ function createBase64URL(mime, body) { const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`; await import(plainESMURL); } + { + await import('data:text/javascript,\'?\'#~'); + } })().then(common.mustCall());