Skip to content

Instantly share code, notes, and snippets.

@jimmywarting
Created March 7, 2025 21:19
Show Gist options
  • Save jimmywarting/a80ec13283e8cd887bd6dd504062f4ea to your computer and use it in GitHub Desktop.
Save jimmywarting/a80ec13283e8cd887bd6dd504062f4ea to your computer and use it in GitHub Desktop.
Allow http/2 (h2) with native fetch in node
// https://github.com/nodejs/undici/issues/2750#issuecomment-2707472553

// undici global symbol are lazy loaded, so we need to trigger it first
try { fetch('data:;base64,') } catch (e) { }

// Get the global agent
const unidiciGlobalDispatcherSymbol = Symbol.for('undici.globalDispatcher.1');
const agent = globalThis[unidiciGlobalDispatcherSymbol]

// get the agent internal options
const symbols = Object.getOwnPropertySymbols(agent)
const [ options ] = symbols.filter(predicate => predicate.toString().includes('options'))
const agentOptions = agent[options]

// enable h2
agentOptions.allowH2 = true

And then make your sweet loving h2 request without any trouble

fetch(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment