A TCP or Unix domain stream socket, created by hs.socket.create().
A socket can either connect to a server (connect()/connectUnix()) or listen for incoming
connections (listen()/listenUnix()). Each connection a listening socket accepts is handed
to its callback as a new HSSocket, which inherits the listening socket's callback.
Reading is request-driven: queue a read with readUntil(), readBytes() or
readAvailable(), and the callback receives a "data" event once that read can be
satisfied. Data that arrives before you ask for it is buffered. Reads can be queued before
the socket connects, and each one carries an optional numeric tag that is passed back to the
callback, so a single callback can tell a protocol's reads apart (e.g. a header and a body).
Received data is delivered as a Uint8Array. Use hs.socket.decodeText() to turn it into a
string.
Do not instantiate HSSocket directly - use hs.socket.create().
The local port of the connection, or the port a listening socket is listening on. 0 if
neither applies, including while a listener is still starting up.
How long, in seconds, connecting may take, including any TLS handshake. When it expires,
the callback receives an `"error"` event and the socket disconnects. 0 (or any negative
number) means no timeout. Defaults to `hs.socket.connectTimeout`.
How long, in seconds, a queued read may wait with no data arriving. The timer runs only
while a read is queued, and restarts whenever data arrives, so a slow but steady transfer
doesn't time out, and nor does a connection that's quiet while no read is queued. When it
expires, the callback receives an `"error"` event and the socket disconnects. 0 (or any
negative number) means no timeout. Defaults to `hs.socket.readTimeout`.
Sets the callback that receives this socket's events.
`HSSocket`, which has the same callback. Queue a read on it to start receiving.
read's tag.
connection, a `"disconnected"` event follows.
reason, including calling `disconnect()`. Data received that no read had asked for yet
is discarded.
An `"error"` event on its own never changes the socket's state: errors such as calling
`connect()` on a socket that is already connecting, or writing to an idle socket, leave it
as it was. Every connection that ends, and every listening socket that stops, gets a
`"disconnected"` event, after the `"error"` that caused it, if any. So reconnecting belongs
in the `"disconnected"` handler, not the `"error"` one.
When the peer closes the connection, data already received for the first queued read is
delivered to it in a last `"data"` event, before `"disconnected"`, even though the read
isn't satisfied: it may be missing its delimiter, or be shorter than asked for. This is
how to receive the end of a stream that the peer ends by closing, such as an HTTP/1.0
response body.
Listens for incoming TCP connections. The callback receives `"listening"` once the socket
is ready, then `"connection"` for each client that connects.
By default the socket listens on all network interfaces. Pass one of this Mac's IP
addresses to accept connections only on that address (and so only on the interface that
has it), e.g. `"127.0.0.1"` to accept connections only from this Mac. Use
`hs.network.addresses()` to find an interface's addresses.
If the interface's address can change (e.g. it comes from DHCP), watch
`State:/Network/Interface/<name>/IPv4` with `hs.network.configurationWatcher()` and listen again
when it does.
Declaration
listen(port, address) -> HSSocket
Parameters
Name
Type
Description
port
number
The TCP port to listen on, or 0 to let the system choose one (see `localPort`).
address
string
A local IPv4 or IPv6 address to listen on. Defaults to all addresses.
Returns
HSSocket
The socket, for chaining.
Notes
A listening socket, and its callback, stay alive until it stops listening, even if you don't keep a reference to it
Example
hs.socket.create().setCallback((event, sock, data) => {
if (event === "connection") data.write("Hello!\n").readUntil("\n")
if (event === "data") sock.write(data)
}).listen(9000)
// Only reachable from this Mac
hs.socket.create().setCallback(handler).listen(9001, "127.0.0.1")
Listens for incoming connections on a Unix domain socket. A stale socket file left at
`path` is replaced, and the file is removed when the socket stops listening.
Declaration
listenUnix(path) -> HSSocket
Parameters
Name
Type
Description
path
string
The path to create the Unix domain socket at.
Returns
HSSocket
The socket, for chaining.
Notes
A listening socket, and its callback, stay alive until it stops listening, even if you don't keep a reference to it
Makes the next `connect()` use TLS. The server's certificate is checked against the
system's trusted certificates and against `peerName`, or the host passed to `connect()`.
Declaration
enableTLS(peerName, allowUntrusted) -> HSSocket
Parameters
Name
Type
Description
peerName
string
The name the server's certificate must match, if it differs from the host you connect to.
allowUntrusted
boolean
`true` to accept any certificate, e.g. a self-signed one. This makes the connection vulnerable to interception, so only use it for testing.
Makes the next `listen()` use TLS, presenting the certificate and private key stored in a
PKCS#12 (`.p12`) file. If the file can't be loaded, `listen()` reports an error instead of
listening without encryption.
A PKCS#12 file can be exported from Keychain Access, or made from a certificate and key
with `openssl pkcs12 -export -in cert.pem -inkey key.pem -out server.p12`.
Declaration
enableServerTLS(path, passphrase) -> HSSocket
Parameters
Name
Type
Description
path
string
The path to the PKCS#12 file.
passphrase
string
The passphrase protecting the file. Defaults to an empty passphrase.
Returns
HSSocket
The socket, for chaining.
Notes
TLS isn't supported on Unix domain sockets, so `listenUnix()` reports an error if this has been called
Makes the next `listen()` use TLS, presenting a certificate and private key (an identity)
from the keychain. The identity is found by its certificate's name, as Keychain Access
shows it (usually the certificate's common name). If no identity has that name, `listen()`
reports an error instead of listening without encryption.
Declaration
enableServerTLSFromKeychain(name) -> HSSocket
Parameters
Name
Type
Description
name
string
The name of the certificate, e.g. `"myhost.local"`.
Returns
HSSocket
The socket, for chaining.
Notes
macOS may ask for permission to use the private key the first time a client connects
TLS isn't supported on Unix domain sockets, so `listenUnix()` reports an error if this has been called
Closes the connection after sending any data already written, or stops listening. A
listening socket also disconnects every client it accepted. Queued reads, and data received
that no read has asked for yet, are discarded.
The socket can be connected or listen again afterwards.
Declaration
disconnect() -> HSSocket
Returns
HSSocket
The socket, for chaining.
Notes
The callback receives `"disconnected"` (for this socket, and for each client of a listening socket) before `disconnect()` returns, so update any state the handler relies on before calling it