-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: fix http(s) import via custom loader
PR-URL: #43130 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
1 parent
cb18e8e
commit b0805e3
Showing
5 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { mustCall } from '../common/index.mjs'; | ||
import fixtures from '../common/fixtures.js'; | ||
import { strictEqual } from 'node:assert'; | ||
import { spawn } from 'node:child_process'; | ||
import http from 'node:http'; | ||
import path from 'node:path'; | ||
import { promisify } from 'node:util'; | ||
|
||
|
||
const files = { | ||
'main.mjs': 'export * from "./lib.mjs";', | ||
'lib.mjs': 'export { sum } from "./sum.mjs";', | ||
'sum.mjs': 'export function sum(a, b) { return a + b }', | ||
}; | ||
|
||
const requestListener = ({ url }, rsp) => { | ||
const filename = path.basename(url); | ||
const content = files[filename]; | ||
|
||
if (content) { | ||
return rsp | ||
.writeHead(200, { 'Content-Type': 'application/javascript' }) | ||
.end(content); | ||
} | ||
|
||
return rsp | ||
.writeHead(404) | ||
.end(); | ||
}; | ||
|
||
const server = http.createServer(requestListener); | ||
|
||
await promisify(server.listen.bind(server))({ | ||
host: '127.0.0.1', | ||
port: 0, | ||
}); | ||
|
||
const { | ||
address: host, | ||
port, | ||
} = server.address(); | ||
|
||
{ // Verify nested HTTP imports work | ||
const child = spawn( // ! `spawn` MUST be used (vs `spawnSync`) to avoid blocking the event loop | ||
process.execPath, | ||
[ | ||
'--no-warnings', | ||
'--loader', | ||
fixtures.fileURL('es-module-loaders', 'http-loader.mjs'), | ||
'--input-type=module', | ||
'--eval', | ||
`import * as main from 'http://${host}:${port}/main.mjs'; console.log(main)`, | ||
] | ||
); | ||
|
||
let stderr = ''; | ||
let stdout = ''; | ||
|
||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => stderr += data); | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => stdout += data); | ||
|
||
child.on('close', mustCall((code, signal) => { | ||
strictEqual(stderr, ''); | ||
strictEqual(stdout, '[Module: null prototype] { sum: [Function: sum] }\n'); | ||
strictEqual(code, 0); | ||
strictEqual(signal, null); | ||
|
||
server.close(); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { get } from 'http'; | ||
|
||
export function resolve(specifier, context, nextResolve) { | ||
const { parentURL = null } = context; | ||
|
||
if (specifier.startsWith('http://')) { | ||
return { | ||
shortCircuit: true, | ||
url: specifier, | ||
}; | ||
} else if (parentURL?.startsWith('http://')) { | ||
return { | ||
shortCircuit: true, | ||
url: new URL(specifier, parentURL).href, | ||
}; | ||
} | ||
|
||
return nextResolve(specifier, context); | ||
} | ||
|
||
export function load(url, context, nextLoad) { | ||
if (url.startsWith('http://')) { | ||
return new Promise((resolve, reject) => { | ||
get(url, (rsp) => { | ||
let data = ''; | ||
rsp.on('data', (chunk) => data += chunk); | ||
rsp.on('end', () => { | ||
resolve({ | ||
format: 'module', | ||
shortCircuit: true, | ||
source: data, | ||
}); | ||
}); | ||
}) | ||
.on('error', reject); | ||
}); | ||
} | ||
|
||
return nextLoad(url, context); | ||
} |