promise-socket
This module allows to convert
net.Socket
stream into
its promisified version, which returns
Promise
object fulfilled when stream's events occurred.
Requirements
This module requires Node >= 4.
Installation
npm install promise-socket
Usage
const PromiseSocket = require('promise-socket')
Typescript:
import PromiseSocket from 'promise-socket'
constructor
const promiseSocket = new PromiseSocket(socket)
PromiseSocket
object requires socket
object to work. New
net.Socket
object is created if socket
argument is missing.
Example:
const net = require('net')
const PromiseSocket = require('promise-socket')
const socket = new net.Socket()
const promiseSocket = new PromiseSocket(socket)
Typescript:
import net from 'net'
import PromiseSocket from 'promise-socket'
const socket = new net.Socket()
const promiseSocket = new PromiseSocket(socket)
stream
const socket = promiseSocket.stream
Original socket object.
Example:
console.log(promiseSocket.stream.localAddress)
connect
await connect(port, host)
await connect(path)
await connect(options)
Initiate a connection on a given socket. Promise if fulfilled when connect
event is emitted. Check
socket.connect
for
arguments.
Example:
await connect(80, 'localhost')
await connect({ port: 80, host: 'localhost'})
setTimeout
socket.setTimeout(ms)
Set the timeout for idle socket and after this timeout the socket will be ended.
It means that read
or write
methods will be also fulfilled.
Example:
socket.setTimeout(1000)
await socket.readAll()
read
const chunk = await promiseSocket.read(chunkSize)
Check
PromiseReadable.read
for details.
readAll
const content = await promiseSocket.readAll()
Check
PromiseReadable.readAll
for details.
write
await promiseSocket.write(chunk)
Check
PromiseWritable.write
for details.
writeAll
await promiseSocket.writeAll(content, chunkSize)
Check
PromiseWritable.writeAll
for details.
end
await promiseSocket.end()
Check
PromiseWritable.once
for details.
once
const result = await promiseSocket.once(event)
Check
PromiseReadable.once
and
PromiseWritable.once
for details.
destroy
promiseSocket.destroy()
This method calls destroy method on stream and cleans up all own handlers.
See also
PromiseReadable
,
PromiseWritable
,
PromiseDuplex
,
PromisePiping
.
License
Copyright (c) 2017-2018 Piotr Roszatycki mailto:piotr.roszatycki@gmail.com
MIT