Skip to content

Dispatcher is ignored when specified in ky.create #687

Open
@msmirnov-gp

Description

@msmirnov-gp

Our services utilize ky.create to create http client with hooks and tls dispatcher for the undici.
We've encountered that when dispatcher is specified through ky.create, it's being ignored and not used during requests.
Example would be more explanatory:

import ky from 'ky'
import type { Agent } from 'undici-types';

const getCert = async ()  => 'assume this is cert'
const getKey = async () => 'assume this is key'

const getAgentConstructor = async () => {
  const symbol = Symbol.for('undici.globalDispatcher.1') as unknown as keyof typeof global
  
  if (!global[symbol]) {
    await fetch('').catch(() => {})
  }
  
  const globalDispatcher = global[symbol] as Agent
  
  return globalDispatcher.constructor as typeof Agent 
}

const createAgent = async () => {
  const [cert, key, Agent] = await Promise.all([getCert(), getKey(), getAgentConstructor()])
  
  return new Agent({ connect: { cert, key }})
}

// This doesn't work during requests, looks like dispatcher is not injected into request
const createHttpClient1 = async () => {
  return ky.create({
    retry: 0,
    dispatcher: await createAgent()
  })
}

// This works during requests, but here dispatcher is injected by overriding fetch specified to ky.
const createHttpClient2 = async () => {
  return ky.create({
    retry: 0,
    async fetch(input, init) {
      return fetch(input, { dispatcher: await createAgent(), ...init })
    }
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions