


import { bridgifyObject, emit, onMethod } from 'yaob'





























































/**
 * Wraps a Node-style socket into an EdgeSocket.
 */
export function makeEdgeSocket(
  socket,
  opts
) {
  const out = {
    on: onMethod,

    async connect() {
      socket.setEncoding('utf8')
      socket.on('close', (hadError) => emit(out, 'close'))
      socket.on('error', (error) => emit(out, 'error', error))
      socket.on('data', (data) => emit(out, 'message', String(data)))
      socket.on('connect', () => emit(out, 'open'))
      socket.connect({ host: opts.host, port: opts.port })
    },

    send(data) {
      socket.write(data, 'utf8')
      return Promise.resolve()
    },

    close() {
      socket.destroy()
      return Promise.resolve()
    }
  }
  bridgifyObject(out)
  return out
}

/**
 * The extra things we need to add to the EdgeIo object.
 */










