TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
872 stars 229 forks source link

Feature request: support undici (native fetch) #239

Open idranme opened 10 months ago

idranme commented 10 months ago
node:internal/deps/undici/undici:11576
    Error.captureStackTrace(err, this);
          ^

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at async file:///D:/develop/fetch-proxy/index.mjs:5:13 {
  cause: TypeError: agent.dispatch is not a function
      at node:internal/deps/undici/undici:11453:55
      at new Promise (<anonymous>)
      at dispatch (node:internal/deps/undici/undici:11453:16)
      at httpNetworkFetch (node:internal/deps/undici/undici:11354:73)
      at httpNetworkOrCacheFetch (node:internal/deps/undici/undici:11256:39)
      at httpFetch (node:internal/deps/undici/undici:11105:43)
      at schemeFetch (node:internal/deps/undici/undici:11033:24)
      at node:internal/deps/undici/undici:10909:26
      at mainFetch (node:internal/deps/undici/undici:10926:11)
      at fetching (node:internal/deps/undici/undici:10883:7)
}

It will report error:

import { ProxyAgent } from 'proxy-agent'

const client = new ProxyAgent('http://127.0.0.1:7890/')

const res = await fetch('https://google.com', {
  dispatcher: client
})
const text = await res.text()
console.log(text)

It won't report errors:

import { ProxyAgent } from 'undici'

const client = new ProxyAgent('http://127.0.0.1:7890/')

const res = await fetch('https://google.com', {
  dispatcher: client
})
const text = await res.text()
console.log(text)

API Reference: https://github.com/nodejs/node/blob/feb5b0fef8def15c801e4b950d662d76f41ed52c/deps/undici/src/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler

Reference implementation: https://github.com/nodejs/node/blob/da197d189021b34db1164ec68f123c885877b2ee/deps/undici/src/lib/proxy-agent.js#L123