-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In an attempt to bundle only a subset of Undici code, we forgot to take some side-effect into account.
- Loading branch information
Showing
5 changed files
with
72 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import {createHash} from 'node:crypto'; | ||
import {once} from 'node:events'; | ||
import {createServer} from 'node:http'; | ||
import {connect} from 'node:net'; | ||
import {gzipSync} from 'node:zlib'; | ||
|
||
function createSimpleTarArchive(fileName, fileContent, mode = 0o644) { | ||
|
@@ -110,12 +111,47 @@ const server = createServer((req, res) => { | |
res.writeHead(500).end(`Internal Error`); | ||
throw new Error(`unsupported request`, {cause: {url: req.url, packageName}}); | ||
} | ||
}).listen(0, `localhost`); | ||
}); | ||
|
||
if (process.env.AUTH_TYPE === `PROXY`) { | ||
const proxy = createServer((req, res) => { | ||
res.writeHead(200, {[`Content-Type`]: `text/plain`}); | ||
res.end(`okay`); | ||
}); | ||
proxy.on(`connect`, (req, clientSocket, head) => { | ||
if (req.url !== `example.com:80`) { | ||
// Reject all requests except those to `example.com` | ||
clientSocket.end(`HTTP/1.1 404 Not Found\r\n\r\n`); | ||
return; | ||
} | ||
const {address, port} = server.address(); | ||
const serverSocket = connect(port, address, () => { | ||
clientSocket.write(`HTTP/1.1 200 Connection Established\r\n` + | ||
`Proxy-agent: Node.js-Proxy\r\n` + | ||
`\r\n`); | ||
serverSocket.write(head); | ||
serverSocket.pipe(clientSocket); | ||
clientSocket.pipe(serverSocket); | ||
}); | ||
}); | ||
proxy.listen(0, `localhost`); | ||
await once(proxy, `listening`); | ||
const {address, port} = proxy.address(); | ||
process.env.ALL_PROXY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`; | ||
|
||
proxy.unref(); | ||
} | ||
|
||
server.listen(0, `localhost`); | ||
await once(server, `listening`); | ||
|
||
const {address, port} = server.address(); | ||
switch (process.env.AUTH_TYPE) { | ||
case `PROXY`: | ||
// The proxy set up above will redirect all requests to our custom registry, | ||
process.env.COREPACK_NPM_REGISTRY = `http://user:[email protected]`; | ||
break; | ||
|
||
case `COREPACK_NPM_REGISTRY`: | ||
process.env.COREPACK_NPM_REGISTRY = `http://user:pass@${address.includes(`:`) ? `[${address}]` : address}:${port}`; | ||
break; | ||
|
@@ -137,8 +173,11 @@ switch (process.env.AUTH_TYPE) { | |
if (process.env.NOCK_ENV === `replay`) { | ||
const originalFetch = globalThis.fetch; | ||
globalThis.fetch = function fetch(i) { | ||
if (!`${i}`.startsWith(`http://${address.includes(`:`) ? `[${address}]` : address}:${port}`)) | ||
throw new Error; | ||
if (!`${i}`.startsWith( | ||
process.env.AUTH_TYPE === `PROXY` ? | ||
`http://example.com` : | ||
`http://${address.includes(`:`) ? `[${address}]` : address}:${port}`)) | ||
throw new Error(`Unexpected request to ${i}`); | ||
|
||
return Reflect.apply(originalFetch, this, arguments); | ||
}; | ||
|
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