@@ -288,3 +288,180 @@ describe("import() resolution table", function () {
288288 } ) ;
289289 } ) ;
290290} ) ;
291+
292+ // A file module's query is part of its identity, as in browsers and Node; its
293+ // fragment is not, as for an http(s) module here.
294+ describe ( "file module instances per query" , function ( ) {
295+ var nsModule = require ( "ns:module" ) ;
296+ var dir = "~/esm/query-identity/" ;
297+ var appRoot = __dirname . slice ( 0 , __dirname . length - "/tests" . length ) ;
298+ var urlBase = "file://" + appRoot + "/esm/query-identity/" ;
299+
300+ function evaluations ( url ) {
301+ var counts = globalThis . __queryIdentityEvaluations || { } ;
302+ return counts [ url ] || 0 ;
303+ }
304+
305+ function load ( spec ) {
306+ return import ( dir + "importer.mjs" ) . then ( function ( m ) { return m . load ( spec ) ; } ) ;
307+ }
308+
309+ function fail ( done ) {
310+ return function ( e ) {
311+ expect ( "rejected: " + String ( ( e && e . message ) || e ) ) . toBe ( "resolved" ) ;
312+ done ( ) ;
313+ } ;
314+ }
315+
316+ afterEach ( function ( ) {
317+ nsModule . configureLoader ( { importMap : { imports : { } } } ) ;
318+ } ) ;
319+
320+ it ( "gives a static import and an import() with one query one instance" , function ( done ) {
321+ Promise . all ( [ import ( dir + "static-v1.mjs" ) , load ( "./counted.mjs?v=1" ) ] ) . then ( function ( r ) {
322+ expect ( r [ 0 ] . dep ) . toBe ( r [ 1 ] ) ;
323+ expect ( r [ 1 ] . url ) . toBe ( urlBase + "counted.mjs?v=1" ) ;
324+ expect ( evaluations ( urlBase + "counted.mjs?v=1" ) ) . toBe ( 1 ) ;
325+ done ( ) ;
326+ } , fail ( done ) ) ;
327+ } ) ;
328+
329+ it ( "gives two queries, an empty query and none four instances, each evaluated once" ,
330+ function ( done ) {
331+ Promise . all ( [ load ( "./counted.mjs?v=1" ) , load ( "./counted.mjs?v=2" ) , load ( "./counted.mjs?" ) ,
332+ load ( "./counted.mjs" ) ] ) . then ( function ( r ) {
333+ expect ( r . map ( function ( ns ) { return ns . url ; } ) ) . toEqual ( [
334+ urlBase + "counted.mjs?v=1" ,
335+ urlBase + "counted.mjs?v=2" ,
336+ urlBase + "counted.mjs?" ,
337+ urlBase + "counted.mjs" ,
338+ ] ) ;
339+ expect ( [ r [ 0 ] === r [ 1 ] , r [ 0 ] === r [ 3 ] , r [ 1 ] === r [ 3 ] , r [ 2 ] === r [ 3 ] ] )
340+ . toEqual ( [ false , false , false , false ] ) ;
341+ expect ( r . map ( function ( ns ) { return evaluations ( ns . url ) ; } ) ) . toEqual ( [ 1 , 1 , 1 , 1 ] ) ;
342+ done ( ) ;
343+ } , fail ( done ) ) ;
344+ } ) ;
345+
346+ it ( "keeps the query of an import-map target in the module's identity" , function ( done ) {
347+ nsModule . configureLoader ( {
348+ importMap : { imports : { "ns-query-identity-mapped" : dir + "counted.mjs?v=map" } } ,
349+ } ) ;
350+ Promise . all ( [ import ( dir + "static-mapped.mjs" ) , load ( "ns-query-identity-mapped" ) ,
351+ load ( "./counted.mjs?v=map" ) ] ) . then ( function ( r ) {
352+ expect ( [ r [ 0 ] . dep === r [ 1 ] , r [ 1 ] === r [ 2 ] ] ) . toEqual ( [ true , true ] ) ;
353+ expect ( r [ 1 ] . url ) . toBe ( urlBase + "counted.mjs?v=map" ) ;
354+ expect ( evaluations ( urlBase + "counted.mjs?v=map" ) ) . toBe ( 1 ) ;
355+ done ( ) ;
356+ } , fail ( done ) ) ;
357+ } ) ;
358+
359+ it ( "does not split instances by fragment" , function ( done ) {
360+ Promise . all ( [ load ( "./counted.mjs#a" ) , load ( "./counted.mjs#b" ) , load ( "./counted.mjs" ) ,
361+ load ( "./counted.mjs?v=1#c" ) , load ( "./counted.mjs?v=1" ) ] ) . then ( function ( r ) {
362+ expect ( [ r [ 0 ] === r [ 2 ] , r [ 1 ] === r [ 2 ] , r [ 3 ] === r [ 4 ] ] ) . toEqual ( [ true , true , true ] ) ;
363+ expect ( [ r [ 0 ] . url , r [ 3 ] . url ] ) . toEqual ( [ urlBase + "counted.mjs" , urlBase + "counted.mjs?v=1" ] ) ;
364+ done ( ) ;
365+ } , fail ( done ) ) ;
366+ } ) ;
367+
368+ it ( "gives require() of the file the instance without a query" , function ( done ) {
369+ Promise . all ( [ load ( "./counted.mjs" ) , load ( "./counted.mjs?v=1" ) ] ) . then ( function ( r ) {
370+ var required = require ( "../esm/query-identity/counted.mjs" ) ;
371+ expect ( [ required === r [ 0 ] , required === r [ 1 ] ] ) . toEqual ( [ true , false ] ) ;
372+ done ( ) ;
373+ } , fail ( done ) ) ;
374+ } ) ;
375+
376+ it ( "invalidates the instance a file:// URL's query names and no other" , function ( done ) {
377+ var invalidated = urlBase + "counted.mjs?v=inv" ;
378+ Promise . all ( [ load ( "./counted.mjs?v=inv" ) , load ( "./counted.mjs" ) ] ) . then ( function ( first ) {
379+ nsModule . invalidateModules ( [ invalidated ] ) ;
380+ return Promise . all ( [ load ( "./counted.mjs?v=inv" ) , load ( "./counted.mjs" ) ] ) . then ( function ( second ) {
381+ expect ( [ first [ 0 ] === second [ 0 ] , first [ 1 ] === second [ 1 ] ] ) . toEqual ( [ false , true ] ) ;
382+ expect ( evaluations ( invalidated ) ) . toBe ( 2 ) ;
383+ done ( ) ;
384+ } ) ;
385+ } ) . catch ( fail ( done ) ) ;
386+ } ) ;
387+
388+ describe ( "inside a module imported with a query containing '/', '..' and '://'" , function ( ) {
389+ var query = "?a=/../../y&u=http://h/z" ;
390+ var probe ;
391+
392+ function scopeOnFixtureDir ( ) {
393+ var scopes = { } ;
394+ scopes [ appRoot + "/esm/query-identity/" ] = {
395+ "ns-query-identity-scoped" : dir + "scoped-target.mjs" ,
396+ } ;
397+ nsModule . configureLoader ( { importMap : { imports : { } , scopes : scopes } } ) ;
398+ }
399+
400+ beforeAll ( function ( done ) {
401+ scopeOnFixtureDir ( ) ;
402+ load ( "./probe.mjs" + query ) . then ( function ( ns ) {
403+ probe = ns ;
404+ done ( ) ;
405+ } , function ( e ) {
406+ probe = { error : String ( ( e && e . message ) || e ) } ;
407+ done ( ) ;
408+ } ) ;
409+ } ) ;
410+
411+ it ( "loads" , function ( ) {
412+ expect ( probe . error ) . toBeUndefined ( ) ;
413+ } ) ;
414+
415+ it ( "reports the query in import.meta.url and not in import.meta.dirname" , function ( ) {
416+ expect ( probe . url ) . toBe ( urlBase + "probe.mjs" + query ) ;
417+ expect ( probe . search ) . toBe ( query ) ;
418+ expect ( probe . dirname ) . toBe ( appRoot + "/esm/query-identity" ) ;
419+ } ) ;
420+
421+ it ( "resolves relative imports against the module's directory" , function ( done ) {
422+ expect ( probe . staticSibling ) . toBe ( "sibling" ) ;
423+ expect ( probe . required ) . toBe ( "sibling-cjs" ) ;
424+ probe . dynamicSibling ( ) . then ( function ( name ) {
425+ expect ( name ) . toBe ( "sibling" ) ;
426+ done ( ) ;
427+ } , fail ( done ) ) ;
428+ } ) ;
429+
430+ it ( "matches an import-map scope on the module's directory" , function ( done ) {
431+ scopeOnFixtureDir ( ) ;
432+ probe . scopedImport ( ) . then ( function ( name ) {
433+ expect ( name ) . toBe ( "scoped-target" ) ;
434+ done ( ) ;
435+ } , fail ( done ) ) ;
436+ } ) ;
437+
438+ it ( "starts a relative worker from the module's directory" , function ( done ) {
439+ var worker = probe . startWorker ( ) ;
440+ var timer = setTimeout ( function ( ) {
441+ worker . terminate ( ) ;
442+ expect ( "no message" ) . toBe ( "query-worker-ready" ) ;
443+ done ( ) ;
444+ } , 10000 ) ;
445+ worker . onerror = function ( e ) {
446+ clearTimeout ( timer ) ;
447+ worker . terminate ( ) ;
448+ expect ( "error: " + String ( ( e && e . message ) || e ) ) . toBe ( "query-worker-ready" ) ;
449+ done ( ) ;
450+ } ;
451+ worker . onmessage = function ( msg ) {
452+ clearTimeout ( timer ) ;
453+ worker . terminate ( ) ;
454+ expect ( msg . data ) . toBe ( "query-worker-ready" ) ;
455+ done ( ) ;
456+ } ;
457+ } , 15000 ) ;
458+
459+ it ( "is not listed by getLoadedModuleUrls, like any file module" , function ( ) {
460+ var listed = nsModule . getLoadedModuleUrls ( ) . filter ( function ( u ) {
461+ return u . indexOf ( "query-identity" ) >= 0 ;
462+ } ) ;
463+ expect ( listed ) . toEqual ( [ ] ) ;
464+ } ) ;
465+ } ) ;
466+ } ) ;
467+
0 commit comments