0
\$\begingroup\$

I need to collect all the data from the stream and resolve it as promise.

I created this stream:

class CollectToPromise extends Writable {
  constructor(options) {
    super();
    this._result = ""

    this.result = new Promise((resolve, reject) => {
      this.on('finish', resolve):w
      this.on('error', reject)
    }).then(() => this._result)

  }

  _write(chunk, encoding, callback) {
    this._result += chunk
    callback()
  }

  _writev(chunks, callback) {
    chunks.forEach(c => {
      this._result += chunk
    })
    callback()
  }

  asPromise() {
    return this.result
  }
}

and I use it like this:

var r = fs.createReadStream('secrets');
const c = new CollectToPromise()
const encrypt = crypto.createCipher(algorithm, password);
r.pipe(encrypt).pipe(c).asPromise().then((res) => console.log(res)

My questions are:

  1. What do you think about the solution, including the naming?
  2. How can I avoid the warning: (node:32386) Warning: Use Cipheriv for counter mode of aes-256-ctr?
\$\endgroup\$
5
  • \$\begingroup\$ asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application. \$\endgroup\$
    – t.niese
    Commented Dec 6, 2018 at 12:23
  • \$\begingroup\$ >then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise \$\endgroup\$ Commented Dec 6, 2018 at 12:43
  • 1
    \$\begingroup\$ Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation. \$\endgroup\$
    – t.niese
    Commented Dec 6, 2018 at 12:50
  • \$\begingroup\$ Ok, now I see your point, thx \$\endgroup\$ Commented Dec 6, 2018 at 12:53
  • \$\begingroup\$ Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result? \$\endgroup\$ Commented Jan 29, 2019 at 8:41

1 Answer 1

2
\$\begingroup\$

I am not sure I understand why you are attempting to extend Writable here.

At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.

Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation

\$\endgroup\$
1
  • \$\begingroup\$ Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it \$\endgroup\$ Commented Jan 28, 2019 at 16:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.