Open jnv opened 1 month ago
Today node:http2
on bun don't support HTTP/1.1 fallback and when using TLS expects the h2 ALPN Protocol with is not being received in this case, when not using TLS its assumes h2c protocol also know as HTTP/2 over TCP or HTTP/2 over cleartext. The difference in node.js is that node.js can receive HTTP/1.1 fallback and upgrade to HTTP/2 using the Upgrade header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade), in the future HTTP/1.1 fallback will be implemented and this behavior will match node.js until there I recommend the server and client to use ALPN Protocol since is the fastest and most supported way of starting a HTTP/2 connection.
Directly using node:http2
works as expected:
const http2 = require('node:http2');
// Define the URL and path
const client = http2.connect('https://nghttp2.org');
client.on('error', console.error);
// Prepare the request
const options = {
':method': 'POST',
':path': '/httpbin/post',
'content-length': 6
};
const request = client.request(options);
request.on('response', (headers, flags) => {
console.log('statusCode:', headers[':status']);
console.log('headers:', headers);
});
const body = [];
// Collect response data
request.on('data', (chunk) => {
body.push(chunk);
});
request.on('end', () => {
console.log('body:', Buffer.concat(body).toString());
client.close(); // Close the connection
});
// Write data to the request
request.write('123');
request.end('456');
Not to self: after another compatbility check we also need to include the Origin H2 extension: https://datatracker.ietf.org/doc/html/rfc8336 to properly work with http2-wrapper when using h2, add support for http/1.1 fallback and upgrade from http/1.1 -> h2 using headers.
But should work fine for http/1.1 when using .auto
What version of Bun is running?
1.1.34+5e5e7c60f
What platform is your computer?
Darwin 23.6.0 arm64 arm
What steps can reproduce the bug?
(For faster reproduction, here is a repository with the script below and dependencies: https://github.com/jnv/http2-wrapper-bun-repro)
bun add http2-wrapper
Copy the script in usage to js file (e.g.
usage.js
):bun usage.js
What is the expected behavior?
The script exits successfully and writes a HTTP response body.
What do you see instead?
The script crashes with the following error:
Additional information
http2-wrapper is used by got among others.
Looking for the error, I found it in bun's source. Perhaps TLSSocket is behaving differently from Node?