Bruno Weber
Bruno Weber

Reputation: 21

How to create an array with node stream and then access the data

I need to access a directory and I will use stream with generator, and for each file I will push to myArray array. How do I return the filled array, in which case it is returning empty, even after iterating through the stream?

const { Readable, Transform, Writable } = require('node:stream')

const myArray = []

class ReadStream extends Readable {
  i = 0

  _read() {
    if (this.i >= 10) {
      this.push(null)
    } else {
      this.i++
      const str = String(this.i)
      const buf = Buffer.from(str, 'ascii')
      console.clear()
      this.push(buf)
    }
  }
}

class TransformStream extends Transform {
  _transform(chunk, encoding, callback) {
    const transformed = chunk.toString().toLowerCase()

    callback(null, Buffer.from(String(transformed)))
  }
}

class WriteStream extends Writable {
  _write(chunk, encoding, callback) {
    const write = chunk.toString()

    myArray.push(write)

    callback()
  }
}

new ReadStream().pipe(new TransformStream()).pipe(new WriteStream())

How would I return the filled myArray array in this case?

Upvotes: 2

Views: 456

Answers (1)

Hojat
Hojat

Reputation: 41

If you have to use the write stream, message me again, Try this:

const { Readable, Transform, Writable } = require('node:stream')

const myArray = []

class ReadStream extends Readable {

  i = 0

  _read() {
    if (this.i >= 10) {
      this.push(null)
    } else {
      this.i++
      const str = String(this.i)
      const buf = Buffer.from(str, 'ascii')
      this.push(buf)
    }
  }
}

class TransformStream extends Transform {
  _transform(chunk, encoding, callback) {
    const transformed = chunk.toString().toLowerCase()
    
    callback(null, Buffer.from(String(transformed)))
  }
}

new ReadStream().pipe(new TransformStream())
.on("data", function (chunk) {
  const write = chunk.toString()
  myArray.push(write)
})
.on('end',function write() {
  
  // console.clear()
  console.log(myArray);
  })

Edit:

const { Readable, Transform } = require('node:stream')

const myArray = []

class ReadStream extends Readable {

  i = 0

  _read() {
    if (this.i >= 10) {
      this.push(null)
    } else {
      this.i++
      const str = String(this.i)
      const buf = Buffer.from(str, 'ascii')
      this.push(buf)
    }
  }
}

class TransformStream extends Transform {
  _transform(chunk, encoding, callback) {
    const transformed = chunk.toString().toLowerCase()

    callback(null, Buffer.from(String(transformed)))
  }
}

function loadMyArray(params) {
  return new Promise((resolve, reject) => {
    new ReadStream().pipe(new TransformStream())
      .on("data", function (chunk) {
        const write = chunk.toString()
        myArray.push(write)
      })
      .on('end', function write() {
        // console.clear()
        resolve()
      })
      .on('error', function (err) {
        reject(err)
      })
  })
}
start()

async function start() {
  await loadMyArray()
  console.log(myArray);
}

Upvotes: 2

Related Questions