1// Flags: --experimental-json-modules 2'use strict'; 3const common = require('../common'); 4const assert = require('assert'); 5function createURL(mime, body) { 6 return `data:${mime},${body}`; 7} 8 9function createBase64URL(mime, body) { 10 return `data:${mime};base64,${Buffer.from(body).toString('base64')}`; 11} 12(async () => { 13 { 14 const body = 'export default {a:"aaa"};'; 15 const plainESMURL = createURL('text/javascript', body); 16 const ns = await import(plainESMURL); 17 assert.deepStrictEqual(Object.keys(ns), ['default']); 18 assert.deepStrictEqual(ns.default.a, 'aaa'); 19 const importerOfURL = createURL( 20 'text/javascript', 21 `export {default as default} from ${JSON.stringify(plainESMURL)}` 22 ); 23 assert.strictEqual( 24 (await import(importerOfURL)).default, 25 ns.default 26 ); 27 const base64ESMURL = createBase64URL('text/javascript', body); 28 assert.notStrictEqual( 29 await import(base64ESMURL), 30 ns 31 ); 32 } 33 { 34 const body = 'export default import.meta.url;'; 35 const plainESMURL = createURL('text/javascript', body); 36 const ns = await import(plainESMURL); 37 assert.deepStrictEqual(Object.keys(ns), ['default']); 38 assert.deepStrictEqual(ns.default, plainESMURL); 39 } 40 { 41 const body = 'export default import.meta.url;'; 42 const plainESMURL = createURL('text/javascript;charset=UTF-8', body); 43 const ns = await import(plainESMURL); 44 assert.deepStrictEqual(Object.keys(ns), ['default']); 45 assert.deepStrictEqual(ns.default, plainESMURL); 46 } 47 { 48 const body = 'export default import.meta.url;'; 49 const plainESMURL = createURL('text/javascript;charset="UTF-8"', body); 50 const ns = await import(plainESMURL); 51 assert.deepStrictEqual(Object.keys(ns), ['default']); 52 assert.deepStrictEqual(ns.default, plainESMURL); 53 } 54 { 55 const body = 'export default import.meta.url;'; 56 const plainESMURL = createURL('text/javascript;;a=a;b=b;;', body); 57 const ns = await import(plainESMURL); 58 assert.deepStrictEqual(Object.keys(ns), ['default']); 59 assert.deepStrictEqual(ns.default, plainESMURL); 60 } 61 { 62 const ns = await import('data:application/json;foo="test,"this"'); 63 assert.deepStrictEqual(Object.keys(ns), ['default']); 64 assert.deepStrictEqual(ns.default, 'this'); 65 } 66 { 67 const ns = await import(`data:application/json;foo=${ 68 encodeURIComponent('test,') 69 },0`); 70 assert.deepStrictEqual(Object.keys(ns), ['default']); 71 assert.deepStrictEqual(ns.default, 0); 72 } 73 { 74 await assert.rejects(async () => { 75 return import('data:application/json;foo="test,",0'); 76 }, { 77 name: 'SyntaxError', 78 message: /Unexpected end of JSON input/ 79 }); 80 } 81 { 82 const body = '{"x": 1}'; 83 const plainESMURL = createURL('application/json', body); 84 const ns = await import(plainESMURL); 85 assert.deepStrictEqual(Object.keys(ns), ['default']); 86 assert.deepStrictEqual(ns.default.x, 1); 87 } 88 { 89 const body = '{"default": 2}'; 90 const plainESMURL = createURL('application/json', body); 91 const ns = await import(plainESMURL); 92 assert.deepStrictEqual(Object.keys(ns), ['default']); 93 assert.deepStrictEqual(ns.default.default, 2); 94 } 95 { 96 const body = 'null'; 97 const plainESMURL = createURL('invalid', body); 98 try { 99 await import(plainESMURL); 100 common.mustNotCall()(); 101 } catch (e) { 102 assert.strictEqual(e.code, 'ERR_INVALID_MODULE_SPECIFIER'); 103 } 104 } 105 { 106 const plainESMURL = 'data:text/javascript,export%20default%202'; 107 const module = await import(plainESMURL); 108 assert.strictEqual(module.default, 2); 109 } 110})().then(common.mustCall()); 111