spin_round_22
spin_round_22

Reputation: 89

Piping a stream with "end" set to "false" resolves before writable stream is finished

I wrote a function that returns a resolved promise after piping a readable stream. There are two pipes. The first pipe is a transform, and the second pipe is a writable stream. I do not want the writable stream to be closed, so I pass {end:false} in the writer/pipe argument. The transform and writable stream work independently (ie one pipe only) but not when I chain them.

As per all examples I've seen, in order to know when the process is complete I added readable.on('end'). It is not working as expected.

pipe(readable, transform, writable){
    return new Promise((resolve, reject) => {
        readable
            .pipe(transform)
            .pipe(writable, {end:false})
            .on('error', (error) => {
                console.error(error)
                reject(error);
            })
        readable.on('end', () => {
                writable.end();
                resolve();
            });
    });
}
  1. If I call writable.end() nothing is written to the file
  2. If I don't call writable.end() it actually does exactly what it is supposed to do, BUT it resolves the promise way too early.. the file is still being written to.
  3. If I don't use {end:false} and just do this normally, it works fine. The problem is I want the writable stream to stay open.

Upvotes: 3

Views: 1664

Answers (1)

spin_round_22
spin_round_22

Reputation: 89

After trying every event, it is the writable.on('unpipe') handler that I needed to indicate writing was done.

pipe(readable, transform, writable){
    return new Promise((resolve, reject) => {
        readable
            .pipe(transform)
            .pipe(writable, {end:false})
            .on('unpipe', () => {
                 resolve();
                 // this must be called somewhere!
                 // writable.end();
             })
            .on('error', (error) => {
                console.error(error)
                reject(error);
            })
    });
}

Upvotes: 1

Related Questions