From a13cd8ce0fab586387305c4db31d271e96568de8 Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Sun, 3 Nov 2024 00:04:54 -0400 Subject: [PATCH 1/2] docs: add example using proxy with fetch --- docs/examples/proxy/fetch.mjs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 docs/examples/proxy/fetch.mjs diff --git a/docs/examples/proxy/fetch.mjs b/docs/examples/proxy/fetch.mjs new file mode 100644 index 00000000000..f7efcff8dda --- /dev/null +++ b/docs/examples/proxy/fetch.mjs @@ -0,0 +1,39 @@ +import * as http from 'node:http' +import { once } from 'node:events' +import { createProxy } from 'proxy' +import { ProxyAgent } from '../../../index.js' + +const proxyServer = createProxy(http.createServer()) +const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }) + res.end('okay') +}) + +proxyServer.on('request', (req, res) => { + console.log(`Incoming request to ${req.url}`) +}) + +const main = async () => { + await once(proxyServer.listen(0), 'listening') + await once(server.listen(0), 'listening') + + const { port: proxyPort } = proxyServer.address() + const { port } = server.address() + + console.log(`Proxy listening on port ${proxyPort}`) + console.log(`Server listening on port ${port}`) + try { + // undici does a tunneling to the proxy server using CONNECT. + const agent = new ProxyAgent(`http://localhost:${proxyPort}`) + const response = await fetch(`http://localhost:${port}`, { + dispatcher: agent, + method: 'GET' + }) + const data = await response.text() + console.log('Response data:', data) + } catch (e) { + console.log(e) + } +} + +main() From 163d8381461dd84f42e5a92b7cc572e63cc159fb Mon Sep 17 00:00:00 2001 From: "dan.castillo" Date: Sun, 3 Nov 2024 10:22:50 -0500 Subject: [PATCH 2/2] remove main function and use TLA --- docs/examples/proxy/fetch.mjs | 38 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/docs/examples/proxy/fetch.mjs b/docs/examples/proxy/fetch.mjs index f7efcff8dda..56e37728a73 100644 --- a/docs/examples/proxy/fetch.mjs +++ b/docs/examples/proxy/fetch.mjs @@ -13,27 +13,23 @@ proxyServer.on('request', (req, res) => { console.log(`Incoming request to ${req.url}`) }) -const main = async () => { - await once(proxyServer.listen(0), 'listening') - await once(server.listen(0), 'listening') +await once(proxyServer.listen(0), 'listening') +await once(server.listen(0), 'listening') - const { port: proxyPort } = proxyServer.address() - const { port } = server.address() +const { port: proxyPort } = proxyServer.address() +const { port } = server.address() - console.log(`Proxy listening on port ${proxyPort}`) - console.log(`Server listening on port ${port}`) - try { - // undici does a tunneling to the proxy server using CONNECT. - const agent = new ProxyAgent(`http://localhost:${proxyPort}`) - const response = await fetch(`http://localhost:${port}`, { - dispatcher: agent, - method: 'GET' - }) - const data = await response.text() - console.log('Response data:', data) - } catch (e) { - console.log(e) - } +console.log(`Proxy listening on port ${proxyPort}`) +console.log(`Server listening on port ${port}`) +try { + // undici does a tunneling to the proxy server using CONNECT. + const agent = new ProxyAgent(`http://localhost:${proxyPort}`) + const response = await fetch(`http://localhost:${port}`, { + dispatcher: agent, + method: 'GET' + }) + const data = await response.text() + console.log('Response data:', data) +} catch (e) { + console.log(e) } - -main()