#Bun.connect TLS connection is hanging

1 messages · Page 1 of 1 (latest)

wooden summit
#

The following code never returns and TLS connection fails. I've also tried raw connection and it just works as intended. I couldn't find a field for declaring certificates for the operation but I think it uses the system's certificates for that

const socket = Bun.connect({
    hostname: "example.com",
    tls: true,
    port: 443,

    socket: {
        open(socket) {
            socket.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");
        },

        data(socket, data) {
            console.log(`got ${data}`);
            socket.end();
        }
    },
});
red mason
#

sorry nobody got back to you

#

can you file an issue? i'm able to reproduce this

#

we are about to make some changes to TLS handshaking though and that may fix it

#

cc @lone pawn so you see

lone pawn
#

@wooden summit socket.write probably returned 0 as indication that it needs a drain function, the handshake is still incomplete. We created a handshake callback that will be called when the handshake is competed and the certificate error itself (if any) 🤝 so the DX will be better after this changes are done/approved

red mason
#

Ohhh

#

That makes sense

#

We should probably make it so there is a handshake callback but if no callback is defined we only call open after handshaking is complete

#

I think that would match expected behavior

lone pawn
#

Makes sense, in TLS module the connect listener is called after handshake in new version too

red mason
#

We should probably do the same for https client too

lone pawn
#

Yeah this makes sense

wooden summit
#

yes socket.write returns 0 btw

#

hmm, writing the actual request on drain callback actually worked. though have to call socket.write in open callback to make it drain

const socket = Bun.connect({
    hostname: "example.com",
    tls: true,
    port: 443,

    socket: {
        open(socket) {
            socket.write(" ");
        },

        drain(socket) {
            const len = socket.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");
            console.log(len);
        },

        data(socket, data) {
            console.log(`got ${data}`);
            socket.end();
        }
    },
});