bare-net
TCP and IPC servers and clients for JavaScript
bare-net — TCP and IPC servers and clients for JavaScript.
npm i bare-netUsage
const net = require('bare-net')
const server = net.createServer()
server.on('connection', (socket) => socket.on('data', console.log))
server.listen(() => console.log('server is up'))
const { port } = server.address()
const socket = net.createConnection(port)
socket.write('hello world')API
NetSocket
new NetSocket(opts?: NetOptions)
Source
Create a NetSocket.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | NetOptions | — | Options; readBufferSize defaults to 65536, and allowHalfOpen and eagerOpen to false. |
connect(path: string, opts?: PipeConnectOptions, onconnect?: () => void): this
Source
Connect the socket to a path over IPC, or to a port/host over TCP.
Overloads:
connect(path: string, opts?: PipeConnectOptions, onconnect?: () => void): this
connect(path: string, onconnect: () => void): this
connect(port: number, host?: string, opts?: TCPSocketConnectOptions, onconnect?: () => void): this
connect(port: number, host: string, onconnect: () => void): this
connect(port: number, onconnect: () => void): this
connect(opts: NetSocketConnectOptions, onconnect?: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to connect to over IPC. |
opts? | PipeConnectOptions | — | Connection options passed to the underlying socket; path, port, and host may be given here instead of as positional arguments. |
onconnect? | () => void | — | Called once when the socket emits 'connect'. |
connecting: boolean
Source
true while the underlying socket is in the process of connecting.
localAddress: string
Source
Local IP address the socket is connected on, if connected over TCP.
localFamily: string
Source
Local IP address family, if connected over TCP.
localPort: number
Source
Local port the socket is connected on, if connected over TCP.
pending: boolean
Source
true if the socket hasn't been assigned an underlying TCP or IPC socket yet.
readyState: 'open' | 'readOnly' | 'writeOnly' | 'opening'
Source
Current connection state of the socket.
NetSocket.ref(): this
Source
Reference the socket, keeping the event loop alive while it is open.
remoteAddress: string
Source
Remote IP address the socket is connected to, if connected over TCP.
remoteFamily: string
Source
Remote IP address family, if connected over TCP.
remotePort: number
Source
Remote port the socket is connected to, if connected over TCP.
setKeepAlive(enable?: boolean, delay?: number): this
Source
Enable or disable TCP keep-alive on the underlying socket.
Overloads:
setKeepAlive(enable?: boolean, delay?: number): this
setKeepAlive(delay: number): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
enable? | boolean | — | Whether to enable keep-alive. |
delay? | number | — | The initial delay in milliseconds before the first keep-alive probe is sent. |
setNoDelay(enable?: boolean): this
Source
Enable or disable Nagle's algorithm on the underlying socket.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
enable? | boolean | — | When true (the default), data is sent immediately without buffering. |
setTimeout(ms: number, ontimeout?: () => void): this
Source
Set the socket's inactivity timeout.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
ms | number | — | The inactivity timeout in milliseconds; pass 0 to disable the timeout. |
ontimeout? | () => void | — | Called once when the socket emits 'timeout'. |
timeout: number
Source
The socket's current inactivity timeout, in milliseconds.
NetSocket.unref(): this
Source
Unreference the socket, allowing the event loop to exit while it is open.
NetServer
new NetServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void)
Source
Create a NetServer, optionally registering a connection listener.
Overloads:
new NetServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void)
new NetServer(onconnection: (socket: NetSocket) => void)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | NetOptions | — | Options applied to each accepted socket; readBufferSize defaults to 65536, and allowHalfOpen and pauseOnConnect to false. |
onconnection? | (socket: NetSocket) => void | — | Called on each 'connection' event. |
address(): string | TCPSocketAddress | null
Source
Returns the address the server is listening on, or null if it isn't listening.
close(onclose: (err?: Error) => void): this
Source
Stop the server from accepting new connections.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
onclose | (err?: Error) => void | — | Called once when the server emits 'close'. |
listen
listen(path: string, backlog?: number, opts?: PipeServerListenOptions, onlistening?: () => void): thisStart the server listening on a path over IPC, or a port/host over TCP.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to listen on over IPC. |
backlog? | number | — | The maximum length of the queue of pending connections. |
opts? | PipeServerListenOptions | — | — |
onlistening? | () => void | — | Called once when the server emits 'listening'. |
listening: boolean
Source
true if the server is currently listening for connections.
NetServer.ref(): this
Source
Reference the server, keeping the event loop alive while it is listening.
NetServer.unref(): this
Source
Unreference the server, allowing the event loop to exit while it is listening.
Functions
createConnection
createConnection(path: string, opts?: NetOptions & PipeConnectOptions, onconnect?: () => void): NetSocketCreate a NetSocket and connect it over IPC if a path is given, otherwise over TCP.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | — | The path to connect to over IPC. |
opts? | NetOptions & PipeConnectOptions | — | Options for the socket and connection; if path is set the socket connects over IPC, otherwise over TCP. |
onconnect? | () => void | — | Called when the connection is established. |
createServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void): NetServer
Source
Create a NetServer, optionally registering a connection listener.
Overloads:
createServer(opts?: NetOptions, onconnection?: (socket: NetSocket) => void): NetServer
createServer(onconnection: (socket: NetSocket) => void): NetServerParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | NetOptions | — | Options applied to each accepted socket; readBufferSize defaults to 65536, and allowHalfOpen and pauseOnConnect to false. |
onconnection? | (socket: NetSocket) => void | — | Called on each 'connection' event. |
Constants and variables
constants
constants: {
type: { TCP: 1; IPC: 2 }
state: { UNREFED: number }
}Connection type and internal state flags used by sockets and servers.
Types
NetOptions
interface NetOptions {
allowHalfOpen?: boolean
eagerOpen?: boolean
readBufferSize?: number
}Options accepted when constructing a NetSocket or NetServer.
NetSocketEvents
interface NetSocketEvents extends DuplexEvents {
connect: []
}Events emitted by a NetSocket, extending the underlying duplex stream's events.
NetSocketConnectOptions
interface NetSocketConnectOptions extends PipeConnectOptions, TCPSocketConnectOptions {}NetServerEvents
interface NetServerEvents extends EventMap {
close: []
connection: [socket: NetSocket]
error: [err: Error]
listening: []
}Events emitted by a NetServer.
NetServerListenOptions
interface NetServerListenOptions extends PipeServerListenOptions, TCPServerListenOptions {}bare-net/constants
Constants and variables
constants.constants
constants: {
type: { TCP: 1; IPC: 2 }
state: { UNREFED: number }
}Connection type and internal state flags used by sockets and servers.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.