diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js index 5271c6a0fe0a02..f436bc2a1a1d84 100644 --- a/lib/internal/modules/esm/default_resolve.js +++ b/lib/internal/modules/esm/default_resolve.js @@ -53,7 +53,7 @@ function resolve(specifier, parentURL) { try { const parsed = new URL(specifier); if (parsed.protocol === 'data:') { - const [ , mime ] = /^([^/]+\/[^;,]+)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ]; + const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ]; const format = ({ '__proto__': null, 'text/javascript': 'module', diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 34a9a140dd7732..76bd3fe04e320d 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -39,7 +39,7 @@ const debug = debuglog('esm'); const translators = new SafeMap(); exports.translators = translators; -const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(;base64)?,([\s\S]*)$/; +const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; function getSource(url) { const parsed = new URL(url); if (parsed.protocol === 'file:') { diff --git a/test/es-module/test-esm-data-urls.js b/test/es-module/test-esm-data-urls.js index f2f166b47ee3d8..7efe7a1199c5e6 100644 --- a/test/es-module/test-esm-data-urls.js +++ b/test/es-module/test-esm-data-urls.js @@ -35,6 +35,47 @@ function createBase64URL(mime, body) { assert.deepStrictEqual(Object.keys(ns), ['default']); assert.deepStrictEqual(ns.default, plainESMURL); } + { + const body = 'export default import.meta.url;'; + const plainESMURL = createURL('text/javascript;charset=UTF-8', body); + const ns = await import(plainESMURL); + assert.deepStrictEqual(Object.keys(ns), ['default']); + assert.deepStrictEqual(ns.default, plainESMURL); + } + { + const body = 'export default import.meta.url;'; + const plainESMURL = createURL('text/javascript;charset="UTF-8"', body); + const ns = await import(plainESMURL); + assert.deepStrictEqual(Object.keys(ns), ['default']); + assert.deepStrictEqual(ns.default, plainESMURL); + } + { + const body = 'export default import.meta.url;'; + const plainESMURL = createURL('text/javascript;;a=a;b=b;;', body); + const ns = await import(plainESMURL); + assert.deepStrictEqual(Object.keys(ns), ['default']); + assert.deepStrictEqual(ns.default, plainESMURL); + } + { + const ns = await import('data:application/json;foo="test,"this"'); + assert.deepStrictEqual(Object.keys(ns), ['default']); + assert.deepStrictEqual(ns.default, 'this'); + } + { + const ns = await import(`data:application/json;foo=${ + encodeURIComponent('test,') + },0`); + assert.deepStrictEqual(Object.keys(ns), ['default']); + assert.deepStrictEqual(ns.default, 0); + } + { + await assert.rejects(async () => { + return import('data:application/json;foo="test,",0'); + }, { + name: 'SyntaxError', + message: /Unexpected end of JSON input/ + }); + } { const body = '{"x": 1}'; const plainESMURL = createURL('application/json', body);