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

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts