This module allows to convert
Duplex
stream
into its promisified version, which returns
Promise
object fulfilled when stream's events occurred.
The module combines
promise-readable
and
promise-
in one.
This module requires Node >= 16.
npm install promise-duplex
import PromiseDuplex from "promise-duplex"
const promiseDuplex = new PromiseDuplex(stream)
PromiseDuplex
object requires Duplex
object to work.
Example:
import net from "node:net"
import PromiseDuplex from "promise-duplex"
const stream = new net.Socket()
const promiseDuplex = new PromiseDuplex(stream)
const stream = promiseDuplex.stream
Original stream object.
Example:
console.log(promiseDuplex.stream.localAddress)
const chunk = await promiseDuplex.read(chunkSize)
Check
PromiseReadable.read
for details.
const content = await promiseDuplex.readAll()
Check
PromiseReadable.readAll
for details.
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
Check
PromiseReadable.iterate
for details.
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
Check
PromiseReadable[Symbol.asyncIterator]
for details.
promiseDuplex = await promiseDuplex.setEncoding(encoding)
Check
PromiseReadable.setEncoding
for details.
await promiseDuplex.write(chunk)
Check
PromiseWritable.write
for details.
await promiseDuplex.writeAll(content, chunkSize)
Check
PromiseWritable.writeAll
for details.
await promiseDuplex.end()
Check
PromiseWritable.once
for details.
const result = await promiseDuplex.once(event)
Check
PromiseReadable.once
and
PromiseWritable.once
for details.
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 [email protected]