From 9e7a9017be792a4cf44ecb2161fc0b2176a5f64c Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 22 Apr 2024 17:29:22 +0200 Subject: [PATCH 1/3] Expose EnvHttpProxyAgent to Node.js core bundle, so it can be turned on by an experimental flag Signed-off-by: Matteo Collina --- index-fetch.js | 4 ++++ test-me.mjs | 9 +++++++++ test/fetch/export-env-proxy-agent.js | 9 +++++++++ 3 files changed, 22 insertions(+) create mode 100644 test-me.mjs create mode 100644 test/fetch/export-env-proxy-agent.js diff --git a/index-fetch.js b/index-fetch.js index dc6c0d05e3e..aac622cc04c 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -1,5 +1,6 @@ 'use strict' +const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent') const fetchImpl = require('./lib/web/fetch').fetch module.exports.fetch = function fetch (resource, init = undefined) { @@ -19,3 +20,6 @@ module.exports.WebSocket = require('./lib/web/websocket/websocket').WebSocket module.exports.MessageEvent = require('./lib/web/websocket/events').MessageEvent module.exports.EventSource = require('./lib/web/eventsource/eventsource').EventSource + +// Expose the fetch implementation to be enabled in Node.js core via a flag +module.exports.EnvHttpProxyAgent = EnvHttpProxyAgent diff --git a/test-me.mjs b/test-me.mjs new file mode 100644 index 00000000000..7c4b48a76e1 --- /dev/null +++ b/test-me.mjs @@ -0,0 +1,9 @@ +import { fetch } from './index.js' + +console.time('fetch') +for (let i = 0; i < 100000; i++) { + const res = await fetch('http://localhost:3000') + // console.log(await res.text()) + await res.text() +} +console.timeEnd('fetch') diff --git a/test/fetch/export-env-proxy-agent.js b/test/fetch/export-env-proxy-agent.js new file mode 100644 index 00000000000..9e15d733ff8 --- /dev/null +++ b/test/fetch/export-env-proxy-agent.js @@ -0,0 +1,9 @@ +'use strict' + +const { test } = require('node:test') +const assert = require('node:assert') +const undiciFetch = require('../../undici-fetch') + +test('EnvHttpProxyAgent should be part of Node.js bundle', () => { + assert.strictEqual(typeof undiciFetch.EnvHttpProxyAgent, 'function') +}) From 7119e817c022c3681d11282c6611c1cf7e3cb660 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 22 Apr 2024 17:29:56 +0200 Subject: [PATCH 2/3] fixup Signed-off-by: Matteo Collina --- test-me.mjs | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 test-me.mjs diff --git a/test-me.mjs b/test-me.mjs deleted file mode 100644 index 7c4b48a76e1..00000000000 --- a/test-me.mjs +++ /dev/null @@ -1,9 +0,0 @@ -import { fetch } from './index.js' - -console.time('fetch') -for (let i = 0; i < 100000; i++) { - const res = await fetch('http://localhost:3000') - // console.log(await res.text()) - await res.text() -} -console.timeEnd('fetch') From 7b70e6ec5d7d7c798308e6f5b7e5ce09f3447169 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 23 Apr 2024 12:32:52 +0200 Subject: [PATCH 3/3] added setGlobalDispatcher and getGlobalDispatcher to the node.js bundle Signed-off-by: Matteo Collina --- index-fetch.js | 3 +++ test/fetch/export-env-proxy-agent.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/index-fetch.js b/index-fetch.js index aac622cc04c..fc8e557ce84 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -1,5 +1,6 @@ 'use strict' +const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global') const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent') const fetchImpl = require('./lib/web/fetch').fetch @@ -23,3 +24,5 @@ module.exports.EventSource = require('./lib/web/eventsource/eventsource').EventS // Expose the fetch implementation to be enabled in Node.js core via a flag module.exports.EnvHttpProxyAgent = EnvHttpProxyAgent +module.exports.getGlobalDispatcher = getGlobalDispatcher +module.exports.setGlobalDispatcher = setGlobalDispatcher diff --git a/test/fetch/export-env-proxy-agent.js b/test/fetch/export-env-proxy-agent.js index 9e15d733ff8..933a6bb530d 100644 --- a/test/fetch/export-env-proxy-agent.js +++ b/test/fetch/export-env-proxy-agent.js @@ -6,4 +6,10 @@ const undiciFetch = require('../../undici-fetch') test('EnvHttpProxyAgent should be part of Node.js bundle', () => { assert.strictEqual(typeof undiciFetch.EnvHttpProxyAgent, 'function') + assert.strictEqual(typeof undiciFetch.getGlobalDispatcher, 'function') + assert.strictEqual(typeof undiciFetch.setGlobalDispatcher, 'function') + + const agent = new undiciFetch.EnvHttpProxyAgent() + undiciFetch.setGlobalDispatcher(agent) + assert.strictEqual(undiciFetch.getGlobalDispatcher(), agent) })