UNPKG

thorish

Version:

This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.

38 lines (37 loc) 1.31 kB
import type { WebSocket as NodeWebSocket } from 'ws'; import { Listener } from './queue.js'; /** * Simple abstract connection between two endpoints. Just sends messages between the endpoints, * does not do any request/response stuff. */ export type Conn<In = any, Out = any> = { /** * Listener for messages from the connection. */ listener: Listener<In>; /** * Sends the message. Queues if not yet connected. */ send(raw: Out): void; /** * Shutdown the connection. If cause is passed, an error is assumed. */ close(cause?: any): void; /** * Aborts when this disconnects. Also look for `undefined` from `next()`. */ signal: AbortSignal; /** * Done when socket closes. Throws on error, possibly with the `cause` via {@link Conn#close}. */ done: Promise<void>; }; /** * Builds an abstract {@link Conn} for the given socket, from Node or the browser. * * This sends and reads the packets encoded as JSON. This silently drops invalid JSON. * * The socket doesn't need to be open yet; if not, then sends are buffered until it is. There's no * signal passed here, and this will stay open until {@link Conn#close} is called. */ export declare function connForSocket<In, Out>(ws: WebSocket | NodeWebSocket): Conn<In, Out>;