Skip to content

Std.Tcp

error HostNotFound(host: String)

The name could not be resolved.

  • host: String
error ConnectionRefused(host: String, port: Int)

Nothing accepted the connection.

  • host: String
  • port: Int
error ConnectionClosed

The connection was closed by close, by the release of the handler, or by the peer resetting it, or the peer finished sending before read_exact got its bytes.

error TlsFailure(message: String)

The TLS handshake failed, including a certificate that could not be verified.

  • message: String
error TcpFailure(message: String)

Any other failure of the host. message is the host’s description.

  • message: String
type Connection

An open connection, or one that was closed. Held by the host.

enum Roots

The certificates trusted as roots.

  • SystemRoots

    The roots that the host trusts.

  • Pem(text: String)

    One or more CA certificates in PEM, which are then the only roots trusted.

enum Verify

How much of the peer’s certificate upgrade_tls checks.

  • Full(roots: Roots)

    Verifies the chain up to roots, and that the certificate is for the server name.

  • ChainOnly(roots: Roots)

    Verifies the chain up to roots only.

  • NoVerification

    Checks nothing: the connection is encrypted, but the peer is not authenticated.

effect Tcp

A TCP client.

  • suspend fn connect(host: String, port: Int) -> Connection fails HostNotFound | ConnectionRefused | TcpFailure

    Resolves host (a name or an IP address) and connects to port.

  • suspend fn read(conn: Connection, max: Int) -> Option[Bytes] fails ConnectionClosed | TcpFailure

    At least 1 and at most max bytes, as many as have arrived, or None after the peer has finished sending.

  • suspend fn write(conn: Connection, data: Bytes) -> () fails ConnectionClosed | TcpFailure

    Returns after all of data has been handed to the host.

  • suspend fn upgrade_tls(conn: Connection, server_name: String, verify: Verify) -> () fails ConnectionClosed | TlsFailure | TcpFailure

    Performs a TLS handshake as the client, and turns the same connection into a TLS connection.

  • suspend fn close(conn: Connection) -> ()

    Closes the connection. Closing a closed connection does nothing.

handler SystemTcp(registry: Registry): Tcp
uses { Async, ProcessHost }

The host’s TCP client: node:net and node:tls on JS. Its release closes the connections it opened that are still open.

  • registry: Registry
const VERIFY_FULL: Verify

The usual check: the chain up to the roots that the host trusts, and the server name.

fn read_exact(conn: Connection, n: Int) -> Bytes fails ConnectionClosed | TcpFailure uses { Tcp }

Exactly n bytes, read with read as many times as needed. ConnectionClosed if the peer finishes sending first. An n of 0 reads nothing; a negative n is a defect.