node-unix-socket
Version:
node-unix-socket allows you to use SO_REUSEPORT, SOCK_SEQPACKET, SOCK_DGRAM in Node.js.
157 lines (156 loc) • 4.69 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { EventEmitter } from 'events';
export declare type NotifyCb = () => void;
/**
* SeqpacketServer is used to create a SOCK_SEQPACKET server.
* Note that sockets of SOCK_SEQPACKET don't works on MacOS and currently SeqpacketServer doesn't work with `cluster` module, i.e. you can't share a SeqpacketServer across different Node.js processes.
*
* SeqpacketServer is also an `EventEmitter` and will emit events including:
*
* ### Event: `'connection'`:
* - socket `SeqpacketSocket`
* - bindpath `string`
*
* Emitted when a new connection is made.
*
* ### Event: `'error'`
* - error `Error`
*
* Emitted when an error occurs.
*
* ### Event: `'close'`
*
* Emitted when the server closes.
*/
export declare class SeqpacketServer extends EventEmitter {
private closed;
private wrap;
constructor();
private checkClosed;
private onError;
private onConnection;
/**
* Returns the bound address.
* @returns
*/
address(): string;
/**
* Stops the server from accepting new connections and keeps existing connections.
*
* This function is synchronous.
* @returns
*/
close(): void;
/**
* Start a server listening for connections on the given path. This function is synchronous.
* @param bindpath
* @param backlog
*/
listen(bindpath: string, backlog?: number): void;
/**
* Reference the server so that it will prevent Node.js process from exiting automatically.
*/
ref(): void;
/**
* Unreference the server so that it won't prevent Node.js process from exiting automatically.
*/
unref(): void;
}
/**
* SeqpacketSocket is an abstraction of a SOCK_SEQPACKET socket.
*
* SeqpacketSocket is also an `EventEmitter` and will emit events including:
*
* ### Event: `'connect'`
*
* Emitted when a socket connection is successfully established.
*
* ### Event: `'data'`
*
* - buffer `Buffer`
* Emitted when data is received. All message boundaries in incoming datagrams are preserved.
*
* ### Event: `'end'`
* Emitted when the other end of the socket signals the end of transmission, thus ending the readable side of the socket.
*
* ### Event: `'error'`
* - error `Error`
* Emitted when an error occurs. The 'close' event will be called directly following this event.
*
* ### Event: `'close'`
* Emitted once the socket is fully closed.
*/
export declare class SeqpacketSocket extends EventEmitter {
private wrap;
private destroyed;
private connectCb?;
private shutdownCb?;
private shutdown;
private isEnd;
constructor(fd?: number);
private onEnd;
private onShutdown;
private onData;
private checkDestroyed;
private onError;
private onConnect;
private checkClose;
/**
* Initiate a connection on a given socket.
*
* This function is asynchronous. When the connection is established, the 'connect' event will be emitted.
* However, connect() will throw error synchronously if the 'serverPath' is not a valid Seqpacket server.
* @param serverPath
* @param connectCb
*/
connect(serverPath: string, connectCb?: NotifyCb): void;
/**
* Sends data on the socket. The `cb` is called when data is written to operating system.
* @param buf
* @param offset
* @param length
* @param cb
*/
write(buf: Buffer, offset?: number, length?: number, cb?: NotifyCb): void;
/**
* Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
* @param cb
*/
end(cb?: NotifyCb): void;
/**
* Return the size of buffer that SeqpacketSocket uses to receive data. The data will be truncated if the buffer size is not large enough.
*
* Default size is 256KB.
* @returns
*/
getInternalReadBufferSize(): number;
/**
* Set the size of buffer that SeqpacketSocket uses to receive data.
*
* @param size
*/
setInternalReadBufferSize(size: number): void;
/**
* Reference the socket so that it will prevent Node.js process from exiting automatically.
*/
ref(): void;
/**
* Unreference the socket so that it won't prevent Node.js process from exiting automatically.
*/
unref(): void;
/**
* Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection.
*/
destroy(): void;
/**
* Alias of "destory".
*/
close(): void;
/**
* For test only
* @ignore
*/
_state(): number;
}