11cb0ef41Sopenharmony_ci// This is expected to be used by test-esm-loader-hooks.mjs via: 21cb0ef41Sopenharmony_ci// node --loader ./test/fixtures/es-module-loaders/hooks-input.mjs ./test/fixtures/es-modules/json-modules.mjs 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciimport assert from 'assert'; 51cb0ef41Sopenharmony_ciimport { writeSync } from 'fs'; 61cb0ef41Sopenharmony_ciimport { readFile } from 'fs/promises'; 71cb0ef41Sopenharmony_ciimport { fileURLToPath } from 'url'; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cilet resolveCalls = 0; 111cb0ef41Sopenharmony_cilet loadCalls = 0; 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciexport async function resolve(specifier, context, next) { 141cb0ef41Sopenharmony_ci resolveCalls++; 151cb0ef41Sopenharmony_ci let url; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci if (resolveCalls === 1) { 181cb0ef41Sopenharmony_ci url = new URL(specifier).href; 191cb0ef41Sopenharmony_ci assert.match(specifier, /json-modules\.mjs$/); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci if (!(/\[eval\d*\]$/).test(context.parentURL)) { 221cb0ef41Sopenharmony_ci assert.strictEqual(context.parentURL, undefined); 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci assert.deepStrictEqual(context.importAttributes, {}); 261cb0ef41Sopenharmony_ci } else if (resolveCalls === 2) { 271cb0ef41Sopenharmony_ci url = new URL(specifier, context.parentURL).href; 281cb0ef41Sopenharmony_ci assert.match(specifier, /experimental\.json$/); 291cb0ef41Sopenharmony_ci assert.match(context.parentURL, /json-modules\.mjs$/); 301cb0ef41Sopenharmony_ci assert.deepStrictEqual(context.importAttributes, { 311cb0ef41Sopenharmony_ci type: 'json', 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // Ensure `context` has all and only the properties it's supposed to 361cb0ef41Sopenharmony_ci assert.deepStrictEqual(Reflect.ownKeys(context), [ 371cb0ef41Sopenharmony_ci 'conditions', 381cb0ef41Sopenharmony_ci 'importAttributes', 391cb0ef41Sopenharmony_ci 'parentURL', 401cb0ef41Sopenharmony_ci ]); 411cb0ef41Sopenharmony_ci assert.ok(Array.isArray(context.conditions)); 421cb0ef41Sopenharmony_ci assert.strictEqual(typeof next, 'function'); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci const returnValue = { 451cb0ef41Sopenharmony_ci url, 461cb0ef41Sopenharmony_ci format: 'test', 471cb0ef41Sopenharmony_ci shortCircuit: true, 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci return returnValue; 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciexport async function load(url, context, next) { 561cb0ef41Sopenharmony_ci loadCalls++; 571cb0ef41Sopenharmony_ci const source = await readFile(fileURLToPath(url)); 581cb0ef41Sopenharmony_ci let format; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci if (loadCalls === 1) { 611cb0ef41Sopenharmony_ci assert.match(url, /json-modules\.mjs$/); 621cb0ef41Sopenharmony_ci assert.deepStrictEqual(context.importAttributes, {}); 631cb0ef41Sopenharmony_ci format = 'module'; 641cb0ef41Sopenharmony_ci } else if (loadCalls === 2) { 651cb0ef41Sopenharmony_ci assert.match(url, /experimental\.json$/); 661cb0ef41Sopenharmony_ci assert.deepStrictEqual(context.importAttributes, { 671cb0ef41Sopenharmony_ci type: 'json', 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci format = 'json'; 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci assert.ok(new URL(url)); 731cb0ef41Sopenharmony_ci // Ensure `context` has all and only the properties it's supposed to 741cb0ef41Sopenharmony_ci assert.deepStrictEqual(Object.keys(context), [ 751cb0ef41Sopenharmony_ci 'format', 761cb0ef41Sopenharmony_ci 'importAttributes', 771cb0ef41Sopenharmony_ci ]); 781cb0ef41Sopenharmony_ci assert.strictEqual(context.format, 'test'); 791cb0ef41Sopenharmony_ci assert.strictEqual(typeof next, 'function'); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci const returnValue = { 821cb0ef41Sopenharmony_ci source, 831cb0ef41Sopenharmony_ci format, 841cb0ef41Sopenharmony_ci shortCircuit: true, 851cb0ef41Sopenharmony_ci }; 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci return returnValue; 901cb0ef41Sopenharmony_ci} 91