oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.26k stars 2.69k forks source link

ANY fetch() overriding Agent / proxy ~Axios { proxy: false }~ does not work in bun (works in node) #9264

Open tgrushka opened 6 months ago

tgrushka commented 6 months ago

What version of Bun is running?

1.0.30+1424a196f

What platform is your computer?

linux/amd64

What steps can reproduce the bug?

bun add axios
export HTTPS_PROXY='https://localhost'
bun repl
> process.env.HTTPS_PROXY
'https://localhost'
> import axios from 'axios'
undefined
> const api = axios.create({ proxy: false })
undefined
> await api.get('https://www.google.com')
Uncaught ConnectionRefused: Unable to connect. Is the computer able to access the url?
    at AxiosError (/app/node_modules/axios/lib/core/AxiosError.js:21:4)
    at <anonymous> (/app/node_modules/axios/lib/core/AxiosError.js:92:2)
    at handleRequestError (/app/node_modules/axios/lib/adapters/http.js:613:11)
    at <anonymous> (/app/node_modules/follow-redirects/index.js:39:1)
    at <anonymous> (native)
    at promiseReactionJob (native)
    at processTicksAndRejections (native)
    at <anonymous> (/app/node_modules/axios/lib/core/Axios.js:49:17)
    at asyncFunctionResume (native)
    at promiseReactionJobWithoutPromiseUnwrapAsyncContext (native)
    at promiseReactionJob (native)
    at processTicksAndRejections (native) {
  errno: 0,
  path: 'https://www.google.com/',
  code: 'ConnectionRefused',

Exit REPL and start over, because subsequent calls crash bun REPL!!!

bun repl
> import axios from 'axios'
undefined
> const api = axios.create({ proxy: false })
undefined
> api.interceptors.request.use((config) => ({ ...config, proxy: false }))
0
> await api.get('https://www.google.com')
Uncaught ConnectionRefused: Unable to connect. Is the computer able to access the url?
    at AxiosError (/app/node_modules/axios/lib/core/AxiosError.js:21:4)
    at <anonymous> (/app/node_modules/axios/lib/core/AxiosError.js:92:2)
    at handleRequestError (/app/node_modules/axios/lib/adapters/http.js:613:11)
...

Exit REPL.

One more time...

export HTTPS_PROXY=''
bun repl
> import axios from 'axios'
undefined
> const api = axios.create({ proxy: false })
undefined
> await api.get('https://www.google.com')
{
  status: 200,
  statusText: 'OK',
  headers: Object [AxiosHeaders] {
    date: 'Tue, 05 Mar 2024 20:12:06 GMT',
    expires: '-1',
...

Exit REPL.

export HTTPS_PROXY='https://localhost'
node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> const axios = require('axios')
undefined
> axios.get('https://www.google.com').then(res => console.log(res))
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 283,
  [Symbol(trigger_async_id_symbol)]: 268
}
> Uncaught AxiosError: connect ECONNREFUSED 127.0.0.1:443
    at AxiosError.from (/app/node_modules/axios/dist/node/axios.cjs:837:14)

As expected without proxy: false, but then:

(Gee, we don't even have to restart node REPL.)

> const api = axios.create({ proxy: false })
undefined
> api.get('https://www.google.com').then(res => console.log(res))
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 474,
  [Symbol(trigger_async_id_symbol)]: 462
}
> {
  status: 200,
  statusText: 'OK',
  headers: Object [AxiosHeaders] {
    date: 'Tue, 05 Mar 2024 20:15:46 GMT',

Go figure, bun is very, very weird (and frustrating 😠 when it comes to fetch().

Then I get other weird errors (HTTP 428 - Precondition Required) with bun's implementation of fetch when using my own proxy with self-signed certificate, but that's another issue.

What is the expected behavior?

I expect bun to not override fetch() and not override any client library such as axios that uses it. I expect it to behave the same way as it does in node. If it does not, I expect the ability to override bun's "optimized"??? implementation of fetch and revert to node's version of fetch for the entire environment if I want to, for which there seems to be no documentation.

What do you see instead?

See above reproduction. In summary, bun is doing something internally to override fetch() API and is enforcing my HTTPS_PROXY environment variable, even when I strictly override it with proxy: false on the axios instance.

Additional information

No response

tgrushka commented 6 months ago

Tried fetch() by itself, node-fetch, ... nothing seems to allow me to override the HTTPS_PROXY variable, because bun won't allow me to use a node-compatible fetch. This is a major blocker.

kelvinwop commented 4 weeks ago

i get 407 status code too: #13245