ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

Agent did not support on node latest LTS (v18.14.2)

Open pidanmeng opened this issue 2 years ago • 5 comments

Update

it seems like a bug of node


Environment

ofetch: v1.0.1 node:v18.14.2

Reproduction

here is a minimum recurrence demo

import { getProxySettings } from "get-proxy-settings";
import { ofetch } from "ofetch/node";
import httpAgent from 'http-proxy-agent'
import httpsAgent from 'https-proxy-agent'

let agent
await (async function setSystemProxy() {
  const { http, https } = (await getProxySettings()) || {}
  function agentGenerator(ProxySetting) {
    const { protocol, host, port } = ProxySetting
    const agentUrl = `${protocol}://${host}:${port}`
    if (protocol === 'http') {
      return new httpAgent(agentUrl)
    }
    if (protocol === 'https') {
      return new httpsAgent(agentUrl)
    }
    return false
  }
  if (http) {
    agent = agentGenerator(http)
  }
  if (https) {
    agent = agentGenerator(https)
  }
})().then(async () => {
  const response = await ofetch.raw('https://google.com', { agent })
})

Describe the bug

Nodejs has supported fetch api since v17.5.0

This caused current issue:

export const fetch = globalThis.fetch || createNodeFetch();

I'm going to submit a PR to fix this bug!

Additional context

No response

Logs

No response

pidanmeng avatar Feb 23 '23 04:02 pidanmeng

it seems like a bug of node

pidanmeng avatar Feb 23 '23 05:02 pidanmeng

I bumped into this problem today when using Node 16 the "agent" option works as expected but when using Node 18 the "agent" option does not seem to work. Is this a known issue with ofetch or am I missconfiguring something? :)

everchanger avatar Sep 21 '23 09:09 everchanger

Node.js 18 uses undici for fetch. It seems they use dispatchers (like this). PR for docs is welcome if anyone can investigate further.

pi0 avatar Sep 21 '23 10:09 pi0

Hey @everchanger @pidanmeng, this isn't 100% your situation(s) but here's a stripped-down example of how I solved a localhost self-signed certificate issue in development (source):

import { Agent } from 'undici'

try {
  const response = await $fetch('/my/api/call', {
    ...(process.env.NODE_ENV === 'development' && {
      dispatcher: new Agent({
        connect: {
          rejectUnauthorized: false
        }
      })
    })
  })
  console.log(response)
} catch (e) {
  console.log(e)
}

I hope this helps set you on the right path!

timelytree avatar Oct 05 '23 16:10 timelytree

Agent did not support on node v21.4.0

jsonleex avatar Mar 15 '24 02:03 jsonleex

You should use dispatcher in modern node (https://github.com/unjs/ofetch#%EF%B8%8F-adding-https-agent)

pi0 avatar Aug 28 '24 10:08 pi0