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
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 * as net 'net'
import { PromiseSocket } as '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)
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.
socket.setTimeout(1000)
await socket.readAll()
stream
const stream = promiseDuplex.stream
Original stream object.
Example:
console.log(promiseDuplex.stream.localAddress)
read
const chunk = await promiseDuplex.read(chunkSize)
Check
PromiseReadable.read
for details.
readAll
const content = await promiseDuplex.readAll()
Check
PromiseReadable.readAll
for details.
write
await promiseDuplex.write(chunk)
Check
PromiseWritable.write
for details.
writeAll
await promiseDuplex.writeAll(content, chunkSize)
Check
PromiseWritable.writeAll
for details.
end
await promiseDuplex.end()
Check
PromiseWritable.once
for details.
once
const result = await promiseDuplex.once(event)
Check
PromiseReadable.once
and
PromiseWritable.once
for details.
Promise
This module uses any-promise and
any ES6 Promise library or polyfill is supported.
Ie. bluebird can be used as Promise
library for this module, if it is registered before.
require('any-promise/register/bluebird')
const PromiseReadable = require('promise-socket')
License
Copyright (c) 2017 Piotr Roszatycki piotr.roszatycki@gmail.com
MIT