Comments (14)
HTTP/2 support in undici is experimental and not enabled by default. Note that we do not support H2, but only HTTP/2 (over TLS), due the necessary protocol selection.
To enable:
import { createServer, constants } from 'node:http2';
import { once } from 'node:events';
import { setGlobalDispatcher, Agent } from './index.js';
const {
HTTP2_HEADER_STATUS,
HTTP2_HEADER_CONTENT_TYPE,
} = constants;
setGlobalDispatcher(new Agent({
allowH2: true
}));
fetch('https://api.sandbox.push.apple.com').then(r => r.text()).then(console.log)
I think we could enable it in the next major and see how it goes. Unfortunately it might be breaking in case of bugs.
from undici.
I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.
Good Idea! Repeating it for everybody:
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
from undici.
Duplicate of #399. AFAICT it has already been implemented.
from undici.
@aduh95 Can you elaborate? Does nodeJs use undici? If yes, in what version is this supported? I'm using Node 20, which is latest LTE version and fetch there doesn't support H2.
from undici.
Does nodeJs use undici?
Correct (it is also maintained by us) https://github.com/nodejs/node/blob/109ab0a89cdeff5717a2a16edd27bafecc104cf6/doc/contributing/maintaining/maintaining-http.md#L54-L56
You can check what version of Undici your build is using in process.versions.undici
.
I'm using Node 20, which is latest LTE version and fetch there doesn't support H2.
Any chance you could send a repro? So we can reopen this issue and transfer it to the Undici repo.
from undici.
I don't know much about HTTP/2, but here's an example that works in a browser (do it from the website itself to avoid CORS issue), while it fails in Node.js:
fetch('https://api.sandbox.push.apple.com').then(r => r.text()).then(console.log)
It's not easy to find a public URL that only works with HTTP/2.
from undici.
Created a minimal repro:
import { createServer, constants } from 'node:http2';
import { once } from 'node:events';
const {
HTTP2_HEADER_STATUS,
HTTP2_HEADER_CONTENT_TYPE,
} = constants;
const server = createServer();
server.on('stream', (stream, headers) => {
stream.respond({
[HTTP2_HEADER_STATUS]: 200,
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8',
'Access-Control-Allow-Origin': '*',
});
stream.write('hello world');
stream.end();
});
server.listen(3000);
await once(server, 'listening');
const response = await fetch('http://localhost:3000/');
console.assert(response.ok, response.status);
console.log(await response.text());
server.close();
I can confirm the above code works with Deno and fails with the latest main
.
from undici.
I believe it should be fine as soon as we keep it as experimental
though enabled by default when chosen by the server
.
from undici.
@mcolina Just to clraify: I'm using Http2SecureServer with allowHttp1: true
to return an error on unexpectedProtocol
. So sounds like my server would always return the error in this proposal.
Can this behavior be controlled by a flag?!
from undici.
@mctrafik you can already turn on http2 with:
import { setGlobalDispatcher, Agent } from 'undici';
setGlobalDispatcher(new Agent({
allowH2: true
}));
from undici.
I believe it should be fine as soon as we keep it as experimental though enabled by default when chosen by the server.
Good Idea! Repeating it for everybody:
We want to enable HTTP/2 if it is the only protocol that the server advertises in the TLS exchange, but prefer HTTP/1.1 for everything else. 100% agreed.PR?
SGTM 👍
from undici.
If anyone is looking for a way to enable H2 without requiring undici
(which is currently not exposed as a node built-in), here's the trick:
global[Symbol.for("undici.globalDispatcher.1")] = new global[Symbol.for("undici.globalDispatcher.1")].constructor({
allowH2: true,
});
Cheers
from undici.
Note that we do not support H2, but only HTTP/2
What does this mean?
From RFC 9113:
The string "h2" identifies the protocol where HTTP/2 uses Transport Layer Security (TLS)
I am trying to understand the nuance of not supporting H2, but only HTTP/2.
from undici.
I am trying to understand the nuance of not supporting H2, but only HTTP/2.
I meant H2C, which is the non-tls variant.
from undici.
Related Issues (20)
- autobahn workflow is always 1 commit behind HOT 1
- Nightly tests are failing HOT 3
- (regression?) timeouts while reading a body no longer abort with a TimeoutError HOT 2
- Body timeout not working HOT 5
- Retrieve the URL when errored HOT 1
- SocketError: other side closed (with repro example) HOT 1
- Fetch failed with UND_ERR_CONNECT_TIMEOUT undici error on Next.js serverless function HOT 1
- 'interceptors' export doesn't work like in the docs HOT 1
- Nightly tests are failing HOT 4
- publish autobahn test results on the docs site HOT 7
- Nightly tests are failing HOT 2
- Improve support for pre-shared sessions (PSK) HOT 3
- Request constructor: Failed to parse URL from ___ HOT 2
- Broken links in docs webpage HOT 2
- Failing JSON parsing in Node 22 HOT 13
- undici/types/index.d.ts:21:8: Type error TS1192: Module '"[email protected]/node_modules/undici/types/interceptors"' has no default export HOT 6
- Node 22.3.0 regression: undici sends invalid origin header in some cases HOT 2
- fetch arrayBuffer() leaks random data from process memory HOT 18
- build: husky install is deprecated HOT 1
- Ability to externalize WASM was broken HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from undici.