duplexify
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
end-of-stream | 1.4.4 | 2.33 kB | MIT | prod | |
inherits | 2.0.4 | 1.98 kB | ISC | prod | |
readable-stream | 2.3.8 | 25.14 kB | MIT | prod | |
stream-shift | 1.0.3 | 2 kB | MIT | prod |
Duplexify is a utility that converts a writable and readable stream into a unified streams2 duplex stream. It is particularly useful because it supports both streams2 and streams1 inputs. Additionally, duplexify allows for the asynchronous setting of the readable and writable parts via the 'setReadable(stream)' and 'setWritable(stream)' functions.
To use duplexify, you can follow the steps below:
Firstly, install the duplexify package with npm:
npm install duplexify
Once you have the package, you can implement it into your JavaScript file. Here is a basic usage example:
var duplexify = require('duplexify')
// turn writableStream and readableStream into a single duplex stream
var dup = duplexify(writableStream, readableStream)
dup.write('hello world') // will write to writableStream
dup.on('data', function(data) {
// will read from readableStream
})
You can also use duplexify to set the readable and writable parts asynchronously:
var dup = duplexify()
dup.write('hello world') // write will buffer until the writable
// part has been set
// wait a bit ...
dup.setReadable(readableStream)
// maybe wait some more?
dup.setWritable(writableStream)
If either the readable or writable streams emit an error or close, duplexify will destroy both streams and surface the event. You can manually destroy the streams by calling 'dup.destroy()'.
Documentation for duplexify can be found directly on its GitHub page. This includes information on usage, examples, and even a guide on turning a core http request into a duplex stream.