
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
cloneable-readable
Advanced tools
The cloneable-readable npm package allows you to create cloneable versions of Node.js readable streams. This means you can create multiple independent readers from a single readable stream, which can be useful in scenarios where you need to process the same stream data in different ways simultaneously.
Cloning a Readable Stream
This feature allows you to clone a readable stream. The code sample demonstrates how to create a cloneable version of a readable stream and then read data from both the original and cloned streams.
const cloneable = require('cloneable-readable');
const { Readable } = require('stream');
const originalStream = new Readable({
read() {
this.push('data');
this.push(null);
}
});
const clonedStream = cloneable(originalStream);
clonedStream.on('data', (chunk) => {
console.log(`Cloned stream data: ${chunk}`);
});
originalStream.on('data', (chunk) => {
console.log(`Original stream data: ${chunk}`);
});
Multiple Clones
This feature allows you to create multiple clones of a readable stream. The code sample demonstrates how to create two clones from the original stream and read data from both clones.
const cloneable = require('cloneable-readable');
const { Readable } = require('stream');
const originalStream = new Readable({
read() {
this.push('data');
this.push(null);
}
});
const clonedStream1 = cloneable(originalStream);
const clonedStream2 = clonedStream1.clone();
clonedStream1.on('data', (chunk) => {
console.log(`Cloned stream 1 data: ${chunk}`);
});
clonedStream2.on('data', (chunk) => {
console.log(`Cloned stream 2 data: ${chunk}`);
});
The multistream package allows you to combine multiple streams into a single readable stream. While it does not provide cloning functionality, it is useful for merging streams together, which can be an alternative approach depending on the use case.
The through2 package is a tiny wrapper around Node.js streams2 Transform to avoid explicit subclassing noise. While it does not provide cloning functionality, it is useful for creating transform streams that can process data in a pipeline.
Clone a Readable stream, safely.
'use strict'
var cloneable = require('cloneable-readable')
var fs = require('fs')
var pump = require('pump')
var stream = cloneable(fs.createReadStream('./package.json'))
pump(stream.clone(), fs.createWriteStream('./out1'))
// simulate some asynchronicity
setImmediate(function () {
pump(stream, fs.createWriteStream('./out2'))
})
cloneable-readable automatically handles objectMode: true
.
This module comes out of an healthy discussion on the 'right' way to clone a Readable in https://github.com/gulpjs/vinyl/issues/85 and https://github.com/nodejs/readable-stream/issues/202. This is my take.
YOU MUST PIPE ALL CLONES TO START THE FLOW
You can also attach 'data'
and 'readable'
events to them.
Create a Cloneable
stream.
A Cloneable has a clone()
method to create more clones.
All clones must be resumed/piped to start the flow.
Check if stream
needs to be wrapped in a Cloneable
or not.
This project was kindly sponsored by nearForm.
MIT
FAQs
Clone a Readable stream, safely
The npm package cloneable-readable receives a total of 1,972,293 weekly downloads. As such, cloneable-readable popularity was classified as popular.
We found that cloneable-readable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.