Return promise for duplex stream
npm install promise-duplex




This module allows to convertDuplex stream
into its promisified version, which returnsPromise
object fulfilled when stream's events occurred.
The module combinespromise-readable andpromise- in one.
This module requires Node >= 16.
``shell`
npm install promise-duplex
`js`
import PromiseDuplex from "promise-duplex"
`js`
const promiseDuplex = new PromiseDuplex(stream)
PromiseDuplex object requires Duplex object to work.
_Example:_
`js
import net from "node:net"
import PromiseDuplex from "promise-duplex"
const stream = new net.Socket()
const promiseDuplex = new PromiseDuplex(stream)
`
`js`
const stream = promiseDuplex.stream
Original stream object.
_Example:_
`js`
console.log(promiseDuplex.stream.localAddress)
`js`
const chunk = await promiseDuplex.read(chunkSize)
Check
PromiseReadable.read
for details.
`js`
const content = await promiseDuplex.readAll()
Check
PromiseReadable.readAll
for details.
`js`
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
Check
PromiseReadable.iterate
for details.
`js`
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
Check
[PromiseReadable[Symbol.asyncIterator]](https://www.npmjs.com/package/promise-readable#symbolasynciterator)
for details.
`js`
promiseDuplex = await promiseDuplex.setEncoding(encoding)
Check
PromiseReadable.setEncoding
for details.
`js`
await promiseDuplex.write(chunk)
Check
PromiseWritable.write
for details.
`js`
await promiseDuplex.writeAll(content, chunkSize)
Check
PromiseWritable.writeAll
for details.
`js`
await promiseDuplex.end()
Check
PromiseWritable.once
for details.
`js`
const result = await promiseDuplex.once(event)
Check
PromiseReadable.once
and
PromiseWritable.once
for details.
`js`
promiseDuplex = promiseDuplex.destroy()
This method calls destroy method on stream and cleans up all own handlers.
The method returns this object.
PromiseReadable,
PromiseWritable,
PromiseSocket,
PromisePiping`.
Copyright (c) 2017-2024 Piotr Roszatycki