/** * A wire-format strategy used by websocket clients to encode outgoing * envelopes and decode incoming ones. Codecs are picked per-client at * handshake time via the Sec-WebSocket-Protocol header. */ export type Codec = { /** * - the Sec-WebSocket-Protocol identifier. */ name: string; /** * - whether encode() produces a binary payload. */ binary: boolean; /** * - whether this codec's runtime is installed. * Lazy/optional codecs (like MessagePack) may be unavailable without breaking imports. */ available: boolean; /** * - serialize an envelope/object. */ encode: (obj: any) => (string | Buffer); /** * - deserialize a received envelope. */ decode: (data: Buffer | string) => any; /** * - cheap check on incoming binary * bytes: returns true if they *might* be a control envelope, false if they are * definitely raw binary data. JSON codec always returns false (its envelopes * never travel on binary frames). */ sniff: (data: Buffer) => boolean; }; import json = require("./json"); import msgpack = require("./msgpack"); /** * A wire-format strategy used by websocket clients to encode outgoing * envelopes and decode incoming ones. Codecs are picked per-client at * handshake time via the Sec-WebSocket-Protocol header. * @typedef Codec * @property {string} name - the Sec-WebSocket-Protocol identifier. * @property {boolean} binary - whether encode() produces a binary payload. * @property {boolean} available - whether this codec's runtime is installed. * Lazy/optional codecs (like MessagePack) may be unavailable without breaking imports. * @property {(obj: any) => (string|Buffer)} encode - serialize an envelope/object. * @property {(data: Buffer|string) => any} decode - deserialize a received envelope. * @property {(data: Buffer) => boolean} sniff - cheap check on incoming binary * bytes: returns true if they *might* be a control envelope, false if they are * definitely raw binary data. JSON codec always returns false (its envelopes * never travel on binary frames). */ /** @type {Object.} */ export const codecs: { [x: string]: Codec; }; /** * Resolves a subprotocol string to its codec, or null if unsupported or * unavailable. An empty / falsy input resolves to the default JSON codec, * matching "no Sec-WebSocket-Protocol header" client behavior. * @param {string} [subprotocol] * @returns {Codec|null} */ export function resolve(subprotocol?: string): Codec | null; export { json, msgpack };