OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-booking / node_modules / bare-net / index.d.ts

99 lines · typescript ref:
import EventEmitter, { EventMap } from 'bare-events'
import { Duplex, DuplexEvents } from 'bare-stream'
import {
  Pipe,
  PipeServer,
  createConnection as createPipeConnection,
  createServer as createPipeServer
} from 'bare-pipe'
import {
  TCPSocket,
  TCPServer,
  createConnection as createTCPConnection,
  createServer as createTCPServer,
  isIP,
  isIPv4,
  isIPv6
} from 'bare-tcp'
import constants from './lib/constants'

export { constants, isIP, isIPv4, isIPv6 }

export interface NetOptions {
  allowHalfOpen?: boolean
  eagerOpen?: boolean
  readBufferSize?: number
}

export interface NetSocketEvents extends DuplexEvents {
  connect: []
}

interface NetSocket<M extends NetSocketEvents = NetSocketEvents> extends Duplex<M> {
  readonly connecting: boolean
  readonly pending: boolean
  readonly timeout?: number
  readonly readyState: 'open' | 'readOnly' | 'writeOnly' | 'opening'
  readonly localAddress?: string
  readonly localPort?: number
  readonly localFamily?: string
  readonly remoteAddress?: string
  readonly remotePort?: number
  readonly remoteFamily?: string

  connect: Pipe['connect'] & TCPSocket['connect']

  setKeepAlive(enable?: boolean, delay?: number): this
  setKeepAlive(delay: number): this

  setNoDelay(enable?: boolean): this

  setTimeout(ms: number, ontimeout?: () => void): this

  ref(): this
  unref(): this
}

declare class NetSocket {
  constructor(opts?: NetOptions)
}

export { type NetSocket, NetSocket as Socket }

export interface NetServerEvents extends EventMap {
  close: []
  connection: [socket: NetSocket]
  error: [err: Error]
  listening: []
}

interface NetServer<M extends NetServerEvents = NetServerEvents> extends EventEmitter<M> {
  readonly listening: boolean

  address(): (PipeServer['address'] & TCPServer['address']) | null

  listen: PipeServer['listen'] & TCPServer['listen']

  close(onclose: (err?: Error) => void): this

  ref(): this
  unref(): this
}

declare class NetServer {
  constructor(opts?: NetOptions, onconnection?: (socket: NetSocket) => void)
  constructor(onconnection: (socket: NetSocket) => void)
}

export { type NetServer, NetServer as Server }

export function createConnection(
  ...args: Parameters<typeof createPipeConnection & typeof createTCPConnection>
): NetSocket

export { createConnection as connect }

export function createServer(
  ...args: Parameters<typeof createPipeServer & typeof createTCPServer>
): NetServer