Skip to content

Commit

Permalink
module: improve support of data: URLs
Browse files Browse the repository at this point in the history
Add support for loading modules using percent-encoded URLs.

PR-URL: #37392
Backport-PR-URL: #39669
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Bradley Farias <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
  • Loading branch information
aduh95 authored and MylesBorins committed Aug 31, 2021
1 parent 815d59a commit 98259dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/internal/modules/esm/get_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
RegExpPrototypeExec,
decodeURIComponent,
} = primordials;
const { getOptionValue } = require('internal/options');
// Do not eagerly grab .manifest, it may be in TDZ
Expand Down Expand Up @@ -31,8 +32,8 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
if (!match) {
throw new ERR_INVALID_URL(url);
}
const [ , base64, body ] = match;
source = Buffer.from(body, base64 ? 'base64' : 'utf8');
const { 1: base64, 2: body } = match;
source = Buffer.from(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
} else {
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
}
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-data-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ function createBase64URL(mime, body) {
assert.strictEqual(e.code, 'ERR_INVALID_MODULE_SPECIFIER');
}
}
{
const plainESMURL = 'data:text/javascript,export%20default%202';
const module = await import(plainESMURL);
assert.strictEqual(module.default, 2);
}
})().then(common.mustCall());

0 comments on commit 98259dc

Please sign in to comment.