{ } qjs-modules

sockets

Source: quickjs-sockets.c — module exports Socket, AsyncSocket, SockAddr, re-exports SyscallError, plus a function list and SOCK_*/AF_*/… constants.

BSD-socket bindings. Socket is the blocking API; AsyncSocket exposes the same surface but its I/O methods return promises.

Module functions

FunctionArgsDescription
socketpair(domain, type, protocol, out)4Creates a connected pair of sockets.
select(fds[, timeout])1select(2) over a set of descriptors.
poll(fds[, timeout])1poll(2) over a set of descriptors.

SockAddr

new SockAddr([family, addr, port])   // length 1
MemberArgsKindDescription
family—getterAddress family (enumerable).
addr—getter/setterIP address (writable).
port—getter/setterPort number (writable).
path—getter/setterPath for AF_UNIX sockets.
buffer—getterRaw sockaddr bytes.
byteLength—getterSize of the address structure.
clone()0methodCopies the address.
toString()0methodRenders as addr:port / path.

Socket / AsyncSocket

new Socket(domain, type, protocol)        // length 1
new AsyncSocket(domain, type, protocol)   // length 1

Methods

MethodArgsDescription
ndelay([on])0Toggles non-blocking mode.
bind(addr)1Binds to a local SockAddr.
connect(addr)1Connects to a remote SockAddr.
listen([backlog])0Marks the socket as listening.
accept()0Accepts an incoming connection.
send(data)1Sends data.
sendto(data, addr)2Sends a datagram to addr.
recv(buffer)1Receives into a buffer.
recvfrom(buffer, addr)2Receives a datagram and the sender address.
sendmsg / recvmsg1Scatter/gather message I/O.
sendmmsg / recvmmsg1Multiple-message I/O.
shutdown(how)1Shuts down one or both directions.
close()0Closes the socket.
getsockopt(level, name, out)3Reads a socket option.
setsockopt(level, name, value)3Sets a socket option.
valueOf()0Returns the fd (also [Symbol.toPrimitive]).

On AsyncSocket, accept, send, sendto, recv, recvfrom, sendmsg, recvmsg, sendmmsg, recvmmsg are promise-returning.

Properties

PropertyKindDescription
fdgetterFile descriptor.
local / remotegetterLocal / peer SockAddr.
opengetterWhether the socket is open.
eofgetterWhether the peer closed the connection.
modegetter/setterI/O mode.
nonblockgetter/setterNon-blocking flag.

Static functions

FunctionArgsDescription
adopt(fd)1Wraps an existing descriptor as a Socket.

Constants

Exported SOCK_*, AF_*, SOL_*, SO_*, IPPROTO_*, MSG_*, SHUT_*, POLL* and related defines (see js_sockets_defines).