I want to clone a read stream. Currently, I am doing it using readable-stream-clone npm package. Using the code:
const fs = require('fs')
const readStream = fs.createReadStream('smallTextFile.txt', { highWaterMark: 5 })
const ReadableStreamClone = require('readable-stream-clone')
const readStream1 = new ReadableStreamClone(readStream)
const readStream2 = new ReadableStreamClone(readStream)
readStream1.on('data', (chunk) => {
console.log('1', chunk.toString())
})
readStream2.on('data', (chunk) => {
console.log('2', chunk.toString())
})
Output of the code is:
1 This
2 This
1 is a
2 is a
1 small
2 small
1 reco
2 reco
1 rd
2 rd
Content in smallTextFile.txt:
This is a small record
But the problem with using readable-stream-clone
is that it has only 3000 weekly downloads and it is not licensed.
There is another npm package cloneable-readable it is a licensed package. Can anyone help me implement the above code using the cloneable-readable package?
You can use the below code to do the same via cloneable-readable, it is similar to readable-stream-clone, when you wrap a steam in cloneable, it will give you a new stream in output.
const fs = require('fs')
const readStream = fs.createReadStream('smallTextFile.txt', { highWaterMark: 5 })
const cloneable = require('cloneable-readable')
const readStream1 = cloneable(readStream);
const readStream2 = cloneable(readStream);
readStream1.on('data', (chunk) => {
console.log('1', chunk.toString())
})
readStream2.on('data', (chunk) => {
console.log('2', chunk.toString())
})