What is memory-streams?
The memory-streams npm package provides in-memory streams for Node.js, allowing you to create readable and writable streams that store data in memory. This can be useful for testing, buffering data, or manipulating streams without relying on file system or network I/O.
What are memory-streams's main functionalities?
WritableStream
Creates a writable stream that stores data in memory. You can write data to it and then convert it to a string.
const MemoryStream = require('memory-streams');
const writableStream = new MemoryStream.WritableStream();
writableStream.write('Hello, ');
writableStream.write('world!');
writableStream.end();
console.log(writableStream.toString()); // Outputs: 'Hello, world!'
ReadableStream
Creates a readable stream from a string. You can read data from it as you would with any other readable stream.
const MemoryStream = require('memory-streams');
const readableStream = new MemoryStream.ReadableStream('Hello, world!');
readableStream.on('data', (chunk) => {
console.log(chunk.toString()); // Outputs: 'Hello, world!'
});
DuplexStream
Creates a duplex stream that can be both readable and writable. You can write data to it and read data from it.
const MemoryStream = require('memory-streams');
const duplexStream = new MemoryStream.DuplexStream();
duplexStream.write('Hello, ');
duplexStream.write('world!');
duplexStream.end();
duplexStream.on('data', (chunk) => {
console.log(chunk.toString()); // Outputs: 'Hello, world!'
});
Other packages similar to memory-streams
stream-buffers
The stream-buffers package provides similar functionality by offering in-memory readable and writable streams. It allows you to create streams that store data in buffers, which can be useful for testing and buffering data. Compared to memory-streams, stream-buffers offers more control over buffer sizes and behavior.
memorystream
The memorystream package is another alternative that provides in-memory streams. It is designed to be simple and lightweight, offering basic readable and writable stream functionality. It is similar to memory-streams but may have fewer features and less flexibility.
mock-stream
The mock-stream package is designed for testing purposes, providing in-memory streams that can be used to mock readable and writable streams. It offers additional features for testing, such as simulating errors and controlling stream behavior. It is more focused on testing compared to memory-streams.
Memory Streams JS
Memory Streams JS is a light-weight implementation of the Stream.Readable
and Stream.Writable
abstract classes from node.js. You can use the classes provided to store the result of reading and writing streams in memory. This can be useful when you need pipe your test output for later inspection or to stream files from the web into memory without have to use temporary files on disk.
Forked from https://github.com/paulja/memory-streams-js to be modified
so that .end()
calls emit a finish
event.
Installation
Install with:
npm install memory-streams --save
Usage
Sample usage, using the ReadableStream
class and piping:
var streams = require('memory-streams');
var reader = new streams.ReadableStream('Hello World\n');
reader.pipe(process.stdout);
reader.append('Hello Universe\n');
Using the ReadableStream
class and reading manually:
var streams = require('memory-streams');
var reader = new streams.ReadableStream('Hello World\n');
reader.append('Hello Universe\n');
console.log(reader.read().toString());
Using the WritableStream
class and piping the contents of a file:
var streams = require('memory-streams')
, fs = require('fs');
var reader = fs.createReadStream('index.js');
var writer = new streams.WritableStream();
reader.pipe(writer);
reader.on('readable', function() {
console.log(writer.toString());
console.log(writer.toBuffer());
});
You can also call the write
method directly to store data to the stream:
var streams = require('memory-streams');
var writer = new streams.WritableStream();
writer.write('Hello World\n');
console.log(writer.toString());
For more examples you can look at the tests for the module.
License
MIT
Copyright (c) 2017 Paul Jackson
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.