11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs'; 21cb0ef41Sopenharmony_ciimport fixtures from '../common/fixtures.js'; 31cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 41cb0ef41Sopenharmony_ciimport http from 'node:http'; 51cb0ef41Sopenharmony_ciimport path from 'node:path'; 61cb0ef41Sopenharmony_ciimport { execPath } from 'node:process'; 71cb0ef41Sopenharmony_ciimport { promisify } from 'node:util'; 81cb0ef41Sopenharmony_ciimport { describe, it, after } from 'node:test'; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst files = { 121cb0ef41Sopenharmony_ci 'main.mjs': 'export * from "./lib.mjs";', 131cb0ef41Sopenharmony_ci 'lib.mjs': 'export { sum } from "./sum.mjs";', 141cb0ef41Sopenharmony_ci 'sum.mjs': 'export function sum(a, b) { return a + b }', 151cb0ef41Sopenharmony_ci 'console.mjs': ` 161cb0ef41Sopenharmony_ci import { sum } from './sum.mjs'; 171cb0ef41Sopenharmony_ci globalThis.sum = sum; 181cb0ef41Sopenharmony_ci console.log("loaded over http"); 191cb0ef41Sopenharmony_ci `, 201cb0ef41Sopenharmony_ci}; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst requestListener = ({ url }, rsp) => { 231cb0ef41Sopenharmony_ci const filename = path.basename(url); 241cb0ef41Sopenharmony_ci const content = files[filename]; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci if (content) { 271cb0ef41Sopenharmony_ci return rsp 281cb0ef41Sopenharmony_ci .writeHead(200, { 'Content-Type': 'application/javascript' }) 291cb0ef41Sopenharmony_ci .end(content); 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci return rsp 331cb0ef41Sopenharmony_ci .writeHead(404) 341cb0ef41Sopenharmony_ci .end(); 351cb0ef41Sopenharmony_ci}; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciconst server = http.createServer(requestListener); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciawait promisify(server.listen.bind(server))({ 401cb0ef41Sopenharmony_ci host: '127.0.0.1', 411cb0ef41Sopenharmony_ci port: 0, 421cb0ef41Sopenharmony_ci}); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciconst { 451cb0ef41Sopenharmony_ci address: host, 461cb0ef41Sopenharmony_ci port, 471cb0ef41Sopenharmony_ci} = server.address(); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_cidescribe('ESM: http import via loader', { concurrency: true }, () => { 501cb0ef41Sopenharmony_ci it('should load using --import flag', async () => { 511cb0ef41Sopenharmony_ci // ! MUST NOT use spawnSync to avoid blocking the event loop 521cb0ef41Sopenharmony_ci const { code, signal, stderr, stdout } = await spawnPromisified( 531cb0ef41Sopenharmony_ci execPath, 541cb0ef41Sopenharmony_ci [ 551cb0ef41Sopenharmony_ci '--no-warnings', 561cb0ef41Sopenharmony_ci '--loader', 571cb0ef41Sopenharmony_ci fixtures.fileURL('es-module-loaders', 'http-loader.mjs'), 581cb0ef41Sopenharmony_ci '--import', `http://${host}:${port}/console.mjs`, 591cb0ef41Sopenharmony_ci '--eval', 601cb0ef41Sopenharmony_ci 'console.log(sum(1, 2))', 611cb0ef41Sopenharmony_ci ] 621cb0ef41Sopenharmony_ci ); 631cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 641cb0ef41Sopenharmony_ci assert.match(stdout, /loaded over http\r?\n3/); 651cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 661cb0ef41Sopenharmony_ci assert.strictEqual(signal, null); 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci it('should load using import inside --eval code', async () => { 691cb0ef41Sopenharmony_ci // ! MUST NOT use spawnSync to avoid blocking the event loop 701cb0ef41Sopenharmony_ci const { code, signal, stderr, stdout } = await spawnPromisified( 711cb0ef41Sopenharmony_ci execPath, 721cb0ef41Sopenharmony_ci [ 731cb0ef41Sopenharmony_ci '--no-warnings', 741cb0ef41Sopenharmony_ci '--loader', 751cb0ef41Sopenharmony_ci fixtures.fileURL('es-module-loaders', 'http-loader.mjs'), 761cb0ef41Sopenharmony_ci '--input-type=module', 771cb0ef41Sopenharmony_ci '--eval', 781cb0ef41Sopenharmony_ci `import * as main from 'http://${host}:${port}/main.mjs'; console.log(main)`, 791cb0ef41Sopenharmony_ci ] 801cb0ef41Sopenharmony_ci ); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci assert.strictEqual(stderr, ''); 831cb0ef41Sopenharmony_ci assert.strictEqual(stdout, '[Module: null prototype] { sum: [Function: sum] }\n'); 841cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 851cb0ef41Sopenharmony_ci assert.strictEqual(signal, null); 861cb0ef41Sopenharmony_ci }); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci after(() => server.close()); 891cb0ef41Sopenharmony_ci}); 90