Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ function closeFsStream(stream, cb, err) {
er = er || err;
cb(er);
stream.closed = true;
if (!er)
const s = stream._writableState || stream._readableState;
if (!er && !s.emitClose)
stream.emit('close');
});

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-file-write-stream4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

// Test that 'close' emits once and not twice when `emitClose: true` is set.
// Refs: https://github.com/nodejs/node/issues/31366

const common = require('../common');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const filepath = path.join(tmpdir.path, 'write_pos.txt');

const fileReadStream = fs.createReadStream(process.execPath);
const fileWriteStream = fs.createWriteStream(filepath, {
emitClose: true
});

fileReadStream.pipe(fileWriteStream);
fileWriteStream.on('close', common.mustCall());