@basd/pipe
v0.0.4
Published
A simple and versatile utility class for handling streams in both Node.js and the browser.
Downloads
33
Maintainers
Readme
@basd/pipe
put that in your pipe and stream it
A simple and versatile utility class for handling streams in both Node.js and the browser. The Pipe class provides a convenient way to work with various stream types, including Readable, Writable, Duplex, Transform, and PassThrough streams. You can also collect readable streams into arrays, buffers and strings. Or pipe streams together to create a pipeline.
Installation
npm install @basd/pipe
Usage
First, require the Pipe
class from the module:
const Pipe = require('@basd/pipe')
You can create different types of streams (Readable, Writable, Duplex, PassThrough) by using the Pipe class constructor and providing the appropriate options:
// Create a duplex stream (as a string)
const myDuplex = new Pipe('duplex')
// Create a readable stream (with an options object)
const myReadable = new Pipe({ type: 'readable' })
// Create a writable stream (without the new)
const myWritable = Pipe('writable')
// Create a pass-through stream (defaults to pass-through)
const myPassThrough = new Pipe()
You're basically creating a stream proxy that let's you pass a function and options into the constructor. If the type is a writable stream, the function becomes the write() method; if it's a readable stream, the function becomes the read() method.
const writer = new Pipe(data => console.log(`got data:`, data))
const reader = new Pipe()
const pipeline = Pipe.line(reader, writer)
pipeline.push('Hello World!') // => `got data: Hello World!`
The Pipe class can also be used to set up custom write and read handlers:
const myDuplex = new Pipe(
'duplex',
{
write: (data, encoding, callback) => {
console.log('Writing:', data)
callback()
},
read: (size) => {
console.log('Reading:', size)
}
}
)
You can pipe other Pipe instances or functions into a Pipe instance:
const myDuplex = new Pipe('duplex')
const myReadable = new Pipe('readable')
myDuplex.pipe(myReadable)
The line
method allows you to pipe multiple streams one after the other:
const myDuplex = new Pipe('duplex')
const myReadable = new Pipe('readable')
const myWritable = new Pipe('writable')
myDuplex.line(myReadable, myWritable)
You can collect data from a stream:
const myReadable = new Pipe('readable')
myReadable.collect().then((data) => {
console.log('Collected data:', data)
})
And write data to a stream:
const myWritable = new Pipe('writable')
Pipe.writeStream(myWritable, 'Hello, world!').then(() => {
console.log('Data written.')
})
Or read data from a stream:
const myReadable = new Pipe('readable')
Pipe.readStream(myReadable).then((data) => {
console.log('Read data:', data)
})
Documentation
Pipe Class Methods
The Pipe class provides several static methods and properties to work with different stream types:
Pipe.Duplex
Pipe.Readable
Pipe.Writable
Pipe.Transform
Pipe.PassThrough
Pipe.line(...)
Pipe.collect(stream, format = 'array', bytes = 32)
Pipe.readStream(reader)
Pipe.writeStream(writer, data)
Pipe Instance Methods
The Pipe class instances expose a set of methods to interact with the underlying stream:
pipe.end(...)
pipe.removeListener(...)
pipe.on(...)
pipe.once(...)
pipe.off(...)
pipe.emit(...)
pipe.write(...)
pipe.read(...)
pipe.push(...)
pipe.pipe(pipe)
pipe.line(...)
pipe.collect()
API Reference
API Reference, also see the inline comments in the Pipe class for more detailed information about each method.
Tests
In order to run the test suite, simply clone the repository and install its dependencies:
git clone https://gitlab.com/frenware/core/basd/pipe.git
cd basd
npm install
To run the tests:
npm test
Contributing
Thank you! Please see our contributing guidelines for details.
Donations
If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!
Bitcoin (BTC):
1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF
Monero (XMR):
46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ
License
basd is MIT licensed.