Sha256: 02d5c5db3376afd2c396f9fbc3496cabcbd06a973dc1e23760d2ec4a9bc3e2cb
Contents?: true
Size: 1.37 KB
Versions: 26
Compression:
Stored size: 1.37 KB
Contents
import net from 'node:net' import { normalizeSocketWriteArgs, type WriteArgs, type WriteCallback, } from './utils/normalizeSocketWriteArgs' export interface MockSocketOptions { write: ( chunk: Buffer | string, encoding: BufferEncoding | undefined, callback?: WriteCallback ) => void read: (chunk: Buffer, encoding: BufferEncoding | undefined) => void } export class MockSocket extends net.Socket { public connecting: boolean constructor(protected readonly options: MockSocketOptions) { super() this.connecting = false this.connect() this._final = (callback) => { callback(null) } } public connect() { // The connection will remain pending until // the consumer decides to handle it. this.connecting = true return this } public write(...args: Array<unknown>): boolean { const [chunk, encoding, callback] = normalizeSocketWriteArgs( args as WriteArgs ) this.options.write(chunk, encoding, callback) return true } public end(...args: Array<unknown>) { const [chunk, encoding, callback] = normalizeSocketWriteArgs( args as WriteArgs ) this.options.write(chunk, encoding, callback) return super.end.apply(this, args as any) } public push(chunk: any, encoding?: BufferEncoding): boolean { this.options.read(chunk, encoding) return super.push(chunk, encoding) } }
Version data entries
26 entries across 26 versions & 1 rubygems