2019-07-09 21:03:07 +00:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
2022-04-29 14:04:12 +00:00
|
|
|
const fixtures = require('../common/fixtures');
|
2019-07-09 21:03:07 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
function createURL(mime, body) {
|
|
|
|
return `data:${mime},${body}`;
|
|
|
|
}
|
2019-11-28 06:56:21 +00:00
|
|
|
|
2019-07-09 21:03:07 +00:00
|
|
|
function createBase64URL(mime, body) {
|
|
|
|
return `data:${mime};base64,${Buffer.from(body).toString('base64')}`;
|
|
|
|
}
|
|
|
|
(async () => {
|
|
|
|
{
|
|
|
|
const body = 'export default {a:"aaa"};';
|
|
|
|
const plainESMURL = createURL('text/javascript', body);
|
|
|
|
const ns = await import(plainESMURL);
|
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default.a, 'aaa');
|
2019-07-09 21:03:07 +00:00
|
|
|
const importerOfURL = createURL(
|
|
|
|
'text/javascript',
|
|
|
|
`export {default as default} from ${JSON.stringify(plainESMURL)}`
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
(await import(importerOfURL)).default,
|
|
|
|
ns.default
|
|
|
|
);
|
|
|
|
const base64ESMURL = createBase64URL('text/javascript', body);
|
|
|
|
assert.notStrictEqual(
|
|
|
|
await import(base64ESMURL),
|
|
|
|
ns
|
|
|
|
);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = 'export default import.meta.url;';
|
|
|
|
const plainESMURL = createURL('text/javascript', body);
|
|
|
|
const ns = await import(plainESMURL);
|
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, plainESMURL);
|
2019-07-09 21:03:07 +00:00
|
|
|
}
|
2019-11-22 16:34:31 +00:00
|
|
|
{
|
|
|
|
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']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, plainESMURL);
|
2019-11-22 16:34:31 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
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']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, plainESMURL);
|
2019-11-22 16:34:31 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
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']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, plainESMURL);
|
2019-11-22 16:34:31 +00:00
|
|
|
}
|
|
|
|
{
|
2021-08-27 23:47:49 +00:00
|
|
|
const ns = await import('data:application/json;foo="test,"this"',
|
2023-10-14 03:52:38 +00:00
|
|
|
{ with: { type: 'json' } });
|
2019-11-22 16:34:31 +00:00
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, 'this');
|
2019-11-22 16:34:31 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const ns = await import(`data:application/json;foo=${
|
|
|
|
encodeURIComponent('test,')
|
2023-10-14 03:52:38 +00:00
|
|
|
},0`, { with: { type: 'json' } });
|
2019-11-22 16:34:31 +00:00
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default, 0);
|
2019-11-22 16:34:31 +00:00
|
|
|
}
|
|
|
|
{
|
2021-08-27 23:47:49 +00:00
|
|
|
await assert.rejects(async () =>
|
|
|
|
import('data:application/json;foo="test,",0',
|
2023-10-14 03:52:38 +00:00
|
|
|
{ with: { type: 'json' } }), {
|
2019-11-22 16:34:31 +00:00
|
|
|
name: 'SyntaxError',
|
2022-06-18 07:02:31 +00:00
|
|
|
message: /Unterminated string in JSON at position 3/
|
2019-11-22 16:34:31 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-09 21:03:07 +00:00
|
|
|
{
|
|
|
|
const body = '{"x": 1}';
|
|
|
|
const plainESMURL = createURL('application/json', body);
|
2023-10-14 03:52:38 +00:00
|
|
|
const ns = await import(plainESMURL, { with: { type: 'json' } });
|
2019-07-09 21:03:07 +00:00
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default.x, 1);
|
2019-07-09 21:03:07 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = '{"default": 2}';
|
|
|
|
const plainESMURL = createURL('application/json', body);
|
2023-10-14 03:52:38 +00:00
|
|
|
const ns = await import(plainESMURL, { with: { type: 'json' } });
|
2019-07-09 21:03:07 +00:00
|
|
|
assert.deepStrictEqual(Object.keys(ns), ['default']);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(ns.default.default, 2);
|
2019-07-09 21:03:07 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = 'null';
|
|
|
|
const plainESMURL = createURL('invalid', body);
|
2024-09-07 08:22:03 +00:00
|
|
|
await assert.rejects(import(plainESMURL), { code: 'ERR_UNKNOWN_MODULE_FORMAT' });
|
2019-07-09 21:03:07 +00:00
|
|
|
}
|
2021-02-16 13:22:18 +00:00
|
|
|
{
|
|
|
|
const plainESMURL = 'data:text/javascript,export%20default%202';
|
|
|
|
const module = await import(plainESMURL);
|
|
|
|
assert.strictEqual(module.default, 2);
|
|
|
|
}
|
2022-04-29 14:04:12 +00:00
|
|
|
{
|
|
|
|
const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`;
|
|
|
|
await import(plainESMURL);
|
|
|
|
}
|
2024-09-07 08:22:03 +00:00
|
|
|
{
|
|
|
|
const plainESMURL = 'data:text/javascript,var x = "hello world?"';
|
|
|
|
await import(plainESMURL);
|
|
|
|
}
|
2019-07-09 21:03:07 +00:00
|
|
|
})().then(common.mustCall());
|