2

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?

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Did not work. The array returns empty.
please try again the first code
How do I access the myArray array outside the entire scope, for example, to return to another function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.