I need to somehow clone a readstream
in nodeJS.
That is if I have a readStream
I need two copies of it stream1, stream2. Where I can read both the streams stream1
and stream2
individually.
How can I do that cause as far as I know you cant readily copy a stream also even If I try to pipe original readStream
to stream1
, stream2
I cant consume each of them individually
Asked
Active
Viewed 822 times
3

Mayank Patel
- 346
- 3
- 18
-
1check node js docs: https://www.npmjs.com/package/readable-stream-clone – Kayes Fahim Sep 03 '21 at 08:09
-
What is your specific use case? You can pipe to multiple places, but flow control issues will exist no matter what. – Brad Sep 03 '21 at 08:17
-
I need to check the encoding of data in readSteam. I am doing it using the detect-character-encoding module. But it requires me to read the chunks from the readStream to know its encoding, If I try to read the chunks I will lose data(chunks) that were required later in the code – Mayank Patel Sep 03 '21 at 08:24
1 Answers
5
I found this docs in npmjs package docs
const fs = require("fs");
const ReadableStreamClone = require("readable-stream-clone");
const readStream = fs.createReadStream('text.txt');
const readStream1 = new ReadableStreamClone(readStream);
const readStream2 = new ReadableStreamClone(readStream);
const writeStream1 = fs.createWriteStream('sample1.txt');
const writeStream2 = fs.createWriteStream('sample2.txt');
readStream1.pipe(writeStream1)
readStream2.pipe(writeStream2)
try those line of code may be help you

Kayes Fahim
- 672
- 5
- 16
-
`readable-stream-clone` has a memory leak (https://github.com/nodejs/readable-stream/issues/202#issuecomment-620626415). Check out https://www.npmjs.com/package/cloneable-readable – will Farrell Sep 01 '22 at 23:49