1
+ import { mustCall } from '../common/index.mjs' ;
2
+ import fixtures from '../common/fixtures.js' ;
3
+ import { strictEqual } from 'node:assert' ;
4
+ import { spawn } from 'node:child_process' ;
5
+ import http from 'node:http' ;
6
+ import path from 'node:path' ;
7
+ import { promisify } from 'node:util' ;
8
+
9
+
10
+ const files = {
11
+ 'main.mjs' : 'export * from "./lib.mjs";' ,
12
+ 'lib.mjs' : 'export { sum } from "./sum.mjs";' ,
13
+ 'sum.mjs' : 'export function sum(a, b) { return a + b }' ,
14
+ } ;
15
+
16
+ const requestListener = ( { url } , rsp ) => {
17
+ const filename = path . basename ( url ) ;
18
+ const content = files [ filename ] ;
19
+
20
+ if ( content ) {
21
+ return rsp
22
+ . writeHead ( 200 , { 'Content-Type' : 'application/javascript' } )
23
+ . end ( content ) ;
24
+ }
25
+
26
+ return rsp
27
+ . writeHead ( 404 )
28
+ . end ( ) ;
29
+ } ;
30
+
31
+ const server = http . createServer ( requestListener ) ;
32
+
33
+ await promisify ( server . listen . bind ( server ) ) ( {
34
+ host : '127.0.0.1' ,
35
+ port : 0 ,
36
+ } ) ;
37
+
38
+ const {
39
+ address : host ,
40
+ port,
41
+ } = server . address ( ) ;
42
+
43
+ { // Verify nested HTTP imports work
44
+ const child = spawn ( // ! `spawn` MUST be used (vs `spawnSync`) to avoid blocking the event loop
45
+ process . execPath ,
46
+ [
47
+ '--no-warnings' ,
48
+ '--loader' ,
49
+ fixtures . fileURL ( 'es-module-loaders' , 'http-loader.mjs' ) ,
50
+ '--input-type=module' ,
51
+ '--eval' ,
52
+ `import * as main from 'http://${ host } :${ port } /main.mjs'; console.log(main)` ,
53
+ ]
54
+ ) ;
55
+
56
+ let stderr = '' ;
57
+ let stdout = '' ;
58
+
59
+ child . stderr . setEncoding ( 'utf8' ) ;
60
+ child . stderr . on ( 'data' , ( data ) => stderr += data ) ;
61
+ child . stdout . setEncoding ( 'utf8' ) ;
62
+ child . stdout . on ( 'data' , ( data ) => stdout += data ) ;
63
+
64
+ child . on ( 'close' , mustCall ( ( code , signal ) => {
65
+ strictEqual ( code , 0 ) ;
66
+ strictEqual ( signal , null ) ;
67
+ strictEqual ( stderr , '' ) ;
68
+ strictEqual ( stdout , '[Module: null prototype] { sum: [Function: sum] }\n' ) ;
69
+
70
+ server . close ( ) ;
71
+ } ) ) ;
72
+ }
0 commit comments