unjs / giget

✨ Download templates and git repositories with pleasure!
MIT License
446 stars 37 forks source link

Does not use http proxy on node 20 #120

Closed Genteure closed 8 months ago

Genteure commented 10 months ago

Environment

node 20.6.1 giget 1.1.3

Reproduction

  1. Start any proxy server with logging:

    // https://github.com/berstend/straightforward
    $ npx straightforward
    
          straightforward forward-proxy running on localhost:9191
  2. Set environment variable

    $env:HTTPS_PROXY="http://127.0.0.1:9191"
    export HTTPS_PROXY="http://127.0.0.1:9191"
  3. Run curl -x http://127.0.0.1:9191 https://example.com to confirm the proxy is working and is printing proxy connections:

             CONNECT         example.com:443
  4. Run npx giget github:withastro/astro/examples/basics#latest ./output --force-clean --verbose.

No connection is made through the proxy.

Describe the bug

giget should use the proxy set in environment variable.

https://github.com/unjs/node-fetch-native/blob/47e92fdae1356d3970e40dfafa5614d2c0f68a87/src/index.ts#L22-L29 giget by default is using node's built in fetch in node 20.

The built-in fetch is the same as undici.fetch. https://github.com/nodejs/node/blob/3e14cfbbcfcedbb18235ffd5cc28983ac5d5ca52/lib/internal/process/pre_execution.js#L314-L316

undici does not support http.Agent but uses a different API: https://undici.nodejs.org/#/docs/api/ProxyAgent

Additional context

Workaround: set the environment variable FORCE_NODE_FETCH.

Logs

No response

MarvinXu commented 9 months ago

Same here. I can't download template in create-astro

YadongChen commented 9 months ago

The solution provided is useful, set FORCE_NODE_FETCH=1

for powershell, as follows: $ENV:FORCE_NODE_FETCH=1

pi0 commented 8 months ago

Proxy support is added in the latest version of giget for Node.js versions with native fetch (undici) (read more)

You can get the feature by upgrading your lockfile.