11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci// Flags: --expose-internals 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_cirequire('../common'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst { strictEqual, throws } = require('assert'); 71cb0ef41Sopenharmony_ciconst { createModuleLoader } = require('internal/modules/esm/loader'); 81cb0ef41Sopenharmony_ciconst { LoadCache, ResolveCache } = require('internal/modules/esm/module_map'); 91cb0ef41Sopenharmony_ciconst ModuleJob = require('internal/modules/esm/module_job'); 101cb0ef41Sopenharmony_ciconst createDynamicModule = require( 111cb0ef41Sopenharmony_ci 'internal/modules/esm/create_dynamic_module'); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst jsModuleDataUrl = 'data:text/javascript,export{}'; 141cb0ef41Sopenharmony_ciconst jsonModuleDataUrl = 'data:application/json,""'; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst stubJsModule = createDynamicModule([], ['default'], jsModuleDataUrl); 171cb0ef41Sopenharmony_ciconst stubJsonModule = createDynamicModule([], ['default'], jsonModuleDataUrl); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst loader = createModuleLoader(false); 201cb0ef41Sopenharmony_ciconst jsModuleJob = new ModuleJob(loader, stubJsModule.module, undefined, 211cb0ef41Sopenharmony_ci () => new Promise(() => {})); 221cb0ef41Sopenharmony_ciconst jsonModuleJob = new ModuleJob(loader, stubJsonModule.module, 231cb0ef41Sopenharmony_ci { type: 'json' }, 241cb0ef41Sopenharmony_ci () => new Promise(() => {})); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci// LoadCache.set and LoadCache.get store and retrieve module jobs for a 281cb0ef41Sopenharmony_ci// specified url/type tuple; LoadCache.has correctly reports whether such jobs 291cb0ef41Sopenharmony_ci// are stored in the map. 301cb0ef41Sopenharmony_ci{ 311cb0ef41Sopenharmony_ci const moduleMap = new LoadCache(); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci moduleMap.set(jsModuleDataUrl, undefined, jsModuleJob); 341cb0ef41Sopenharmony_ci moduleMap.set(jsonModuleDataUrl, 'json', jsonModuleJob); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci strictEqual(moduleMap.get(jsModuleDataUrl), jsModuleJob); 371cb0ef41Sopenharmony_ci strictEqual(moduleMap.get(jsonModuleDataUrl, 'json'), jsonModuleJob); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsModuleDataUrl), true); 401cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsModuleDataUrl, 'javascript'), true); 411cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsonModuleDataUrl, 'json'), true); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci strictEqual(moduleMap.has('unknown'), false); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci // The types must match 461cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsModuleDataUrl, 'json'), false); 471cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsonModuleDataUrl, 'javascript'), false); 481cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsonModuleDataUrl), false); 491cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsModuleDataUrl, 'unknown'), false); 501cb0ef41Sopenharmony_ci strictEqual(moduleMap.has(jsonModuleDataUrl, 'unknown'), false); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci// LoadCache.get, LoadCache.has and LoadCache.set should only accept string 541cb0ef41Sopenharmony_ci// values as url argument. 551cb0ef41Sopenharmony_ci{ 561cb0ef41Sopenharmony_ci const moduleMap = new LoadCache(); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci const errorObj = { 591cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 601cb0ef41Sopenharmony_ci name: 'TypeError', 611cb0ef41Sopenharmony_ci message: /^The "url" argument must be of type string/ 621cb0ef41Sopenharmony_ci }; 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci [{}, [], true, 1].forEach((value) => { 651cb0ef41Sopenharmony_ci throws(() => moduleMap.get(value), errorObj); 661cb0ef41Sopenharmony_ci throws(() => moduleMap.has(value), errorObj); 671cb0ef41Sopenharmony_ci throws(() => moduleMap.set(value, undefined, jsModuleJob), errorObj); 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci// LoadCache.get, LoadCache.has and LoadCache.set should only accept string 721cb0ef41Sopenharmony_ci// values (or the kAssertType symbol) as type argument. 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci const moduleMap = new LoadCache(); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci const errorObj = { 771cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 781cb0ef41Sopenharmony_ci name: 'TypeError', 791cb0ef41Sopenharmony_ci message: /^The "type" argument must be of type string/ 801cb0ef41Sopenharmony_ci }; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci [{}, [], true, 1].forEach((value) => { 831cb0ef41Sopenharmony_ci throws(() => moduleMap.get(jsModuleDataUrl, value), errorObj); 841cb0ef41Sopenharmony_ci throws(() => moduleMap.has(jsModuleDataUrl, value), errorObj); 851cb0ef41Sopenharmony_ci throws(() => moduleMap.set(jsModuleDataUrl, value, jsModuleJob), errorObj); 861cb0ef41Sopenharmony_ci }); 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci// LoadCache.set should only accept ModuleJob values as job argument. 901cb0ef41Sopenharmony_ci{ 911cb0ef41Sopenharmony_ci const moduleMap = new LoadCache(); 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci [{}, [], true, 1].forEach((value) => { 941cb0ef41Sopenharmony_ci throws(() => moduleMap.set('', undefined, value), { 951cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 961cb0ef41Sopenharmony_ci name: 'TypeError', 971cb0ef41Sopenharmony_ci message: /^The "job" argument must be an instance of ModuleJob/ 981cb0ef41Sopenharmony_ci }); 991cb0ef41Sopenharmony_ci }); 1001cb0ef41Sopenharmony_ci} 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci{ 1031cb0ef41Sopenharmony_ci const resolveMap = new ResolveCache(); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci strictEqual(resolveMap.serializeKey('./file', { __proto__: null }), './file::'); 1061cb0ef41Sopenharmony_ci strictEqual(resolveMap.serializeKey('./file', { __proto__: null, type: 'json' }), './file::"type""json"'); 1071cb0ef41Sopenharmony_ci strictEqual(resolveMap.serializeKey('./file::"type""json"', { __proto__: null }), './file::"type""json"::'); 1081cb0ef41Sopenharmony_ci strictEqual(resolveMap.serializeKey('./file', { __proto__: null, c: 'd', a: 'b' }), './file::"a""b","c""d"'); 1091cb0ef41Sopenharmony_ci strictEqual(resolveMap.serializeKey('./s', { __proto__: null, c: 'd', a: 'b', b: 'c' }), './s::"a""b","b""c","c""d"'); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci resolveMap.set('key1', 'parent1', 1); 1121cb0ef41Sopenharmony_ci resolveMap.set('key2', 'parent1', 2); 1131cb0ef41Sopenharmony_ci resolveMap.set('key2', 'parent2', 3); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci strictEqual(resolveMap.get('key1', 'parent1'), 1); 1161cb0ef41Sopenharmony_ci strictEqual(resolveMap.get('key2', 'parent1'), 2); 1171cb0ef41Sopenharmony_ci strictEqual(resolveMap.get('key2', 'parent2'), 3); 1181cb0ef41Sopenharmony_ci} 119