Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: use proper .destroy() implementation for SyncWriteStream #26690

Closed
wants to merge 1 commit into from
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
16 changes: 5 additions & 11 deletions lib/internal/fs/sync_write_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ const { Writable } = require('stream');
const { closeSync, writeSync } = require('fs');

function SyncWriteStream(fd, options) {
Writable.call(this);
Writable.call(this, { autoDestroy: true });

options = options || {};

this.fd = fd;
this.readable = false;
this.autoClose = options.autoClose === undefined ? true : options.autoClose;

this.on('end', () => this._destroy());
}

Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
Expand All @@ -24,22 +22,18 @@ SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
return true;
};

SyncWriteStream.prototype._destroy = function() {
SyncWriteStream.prototype._destroy = function(err, cb) {
if (this.fd === null) // already destroy()ed
return;
return cb(err);

if (this.autoClose)
closeSync(this.fd);

this.fd = null;
return true;
cb(err);
};

SyncWriteStream.prototype.destroySoon =
SyncWriteStream.prototype.destroy = function() {
this._destroy();
this.emit('close');
return true;
};
SyncWriteStream.prototype.destroy;

module.exports = SyncWriteStream;
21 changes: 13 additions & 8 deletions test/parallel/test-internal-fs-syncwritestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
assert.strictEqual(stream.fd, 1);
assert.strictEqual(stream.readable, false);
assert.strictEqual(stream.autoClose, true);
assert.strictEqual(stream.listenerCount('end'), 1);
}

// Verify constructing the instance with specified options.
Expand All @@ -29,7 +28,6 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
assert.strictEqual(stream.fd, 1);
assert.strictEqual(stream.readable, false);
assert.strictEqual(stream.autoClose, false);
assert.strictEqual(stream.listenerCount('end'), 1);
}

// Verify that the file will be written synchronously.
Expand All @@ -47,21 +45,28 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
const fd = fs.openSync(filename, 'w');
const stream = new SyncWriteStream(fd);

stream.on('close', common.mustCall(3));
stream.on('close', common.mustCall());
assert.strictEqual(stream.destroy(), stream);
assert.strictEqual(stream.fd, null);
}

// Verify that the stream will unset the fd after destroySoon().
{
const fd = fs.openSync(filename, 'w');
const stream = new SyncWriteStream(fd);

assert.strictEqual(stream.destroy(), true);
stream.on('close', common.mustCall());
assert.strictEqual(stream.destroySoon(), stream);
assert.strictEqual(stream.fd, null);
assert.strictEqual(stream.destroy(), true);
assert.strictEqual(stream.destroySoon(), true);
}

// Verify that the 'end' event listener will also destroy the stream.
// Verify that calling end() will also destroy the stream.
{
const fd = fs.openSync(filename, 'w');
const stream = new SyncWriteStream(fd);

assert.strictEqual(stream.fd, fd);

stream.emit('end');
stream.end();
assert.strictEqual(stream.fd, null);
}