What is stream?
The 'stream' package in Node.js provides a way to handle streaming data. Streams are objects that let you read data from a source or write data to a destination in a continuous fashion. They are particularly useful for handling large amounts of data, such as reading files or handling HTTP requests and responses.
What are stream's main functionalities?
Readable Stream
A Readable stream is used to read data from a source. In this example, a custom Readable stream is created that pushes 'Hello, World!' and then signals the end of data.
const { Readable } = require('stream');
const readable = new Readable({
read(size) {
this.push('Hello, World!');
this.push(null); // No more data
}
});
readable.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
readable.on('end', () => {
console.log('No more data.');
});
Writable Stream
A Writable stream is used to write data to a destination. In this example, a custom Writable stream is created that logs the data it receives.
const { Writable } = require('stream');
const writable = new Writable({
write(chunk, encoding, callback) {
console.log(`Writing: ${chunk.toString()}`);
callback();
}
});
writable.write('Hello, World!');
writable.end('Goodbye, World!');
Transform Stream
A Transform stream is a type of duplex stream where the output is computed based on the input. In this example, a custom Transform stream is created that converts input data to uppercase.
const { Transform } = require('stream');
const transform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
transform.on('data', (chunk) => {
console.log(`Transformed: ${chunk.toString()}`);
});
transform.write('Hello, World!');
transform.end('Goodbye, World!');
Duplex Stream
A Duplex stream is both Readable and Writable. In this example, a custom Duplex stream is created that reads and writes data.
const { Duplex } = require('stream');
const duplex = new Duplex({
read(size) {
this.push('Hello, World!');
this.push(null); // No more data
},
write(chunk, encoding, callback) {
console.log(`Writing: ${chunk.toString()}`);
callback();
}
});
duplex.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
duplex.write('Hello, World!');
duplex.end('Goodbye, World!');
Other packages similar to stream
through2
The 'through2' package is a tiny wrapper around Node.js streams2 Transform to avoid explicit subclassing noise. It provides a simpler API for creating transform streams. Compared to 'stream', 'through2' is more user-friendly and requires less boilerplate code.
highland
The 'highland' package provides a high-level stream library for Node.js. It allows you to work with synchronous and asynchronous data streams in a more functional way. Compared to 'stream', 'highland' offers a more powerful and flexible API for stream manipulation.
readable-stream
The 'readable-stream' package is a mirror of the streams implementations in Node.js, but it can be used independently of the Node.js core. It provides a consistent and stable stream API across different Node.js versions. Compared to 'stream', 'readable-stream' ensures compatibility and stability across different environments.
stream
Node.js streams in the browser.
Ported straight from the Node.js core and adapted to component/emitter's api.
If you're offended by this, keep in mind that it's old and I don't want to break anyone's build by deleting this.
For docs:
A testsuite for the browser is there too. Just issue npm install
after you've cloned this repo and then open the html file.
Installation
Install with component(1)
$ component install juliangruber/stream
or
$ npm install stream
Usage example
var Stream = require('stream');
var src = new Stream();
src.readable = true;
var dest = new Stream();
dest.writable = true;
dest.write = function(data) {
assert(data == 'test');
};
src.pipe(dest);
src.emit('data', 'test');
License
(MIT)
Copyright (c) 2012 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.