Skip to content

.on('close') for a readable stream stops working when going from node 12.14.0 to 12.16.1 #32023

@JamesSingleton

Description

@JamesSingleton
  • Version: 12.16.1
  • Platform: Windows (64 bit) and MacOS
  • Subsystem:

What steps will reproduce the bug?

The entire script can be found here (https://github.com/americanexpress/one-app/blob/master/scripts/dangers/bundle-sizes.js)

  1. Run the following code on Node 12.14.0 and below.
function getGzipSize(filePath) {
  let bytesWritten = 0;
  const byteCounter = new Writable({
    write(chunk, encoding, callback) {
      // we control the input stream, will always be a Buffer
      bytesWritten += chunk.length;
      callback();
    },
  });
  return new Promise((res, rej) => {
    fs.createReadStream(filePath)
      .pipe(zlib.createGzip({ level: 9 }))
      // writable streams do not fire the 'close' event anymore
      // https://github.com/nodejs/node/issues/21122#issuecomment-414622251
      .on('close', () => res(bytesWritten))
      .pipe(byteCounter)
      .on('error', rej);
  });
}
  1. Switch to Node 12.16.1

How often does it reproduce? Is there a required condition?

Consistently.

What is the expected behavior?

With this certain piece of code, on close of the stream, a markdown file should be printed.

What do you see instead?

Nothing is printed due to it not closing.

Additional information

Switching close to end makes it work. Also, this comes from a discussion I had with @mcollina and @jasnell on Twitter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    zlibIssues and PRs related to the zlib subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions