Skip to content

Commit abafd38

Browse files
committed
fs: use proper .destroy() implementation for SyncWriteStream
PR-URL: nodejs#26690 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3610155 commit abafd38

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/internal/fs/sync_write_stream.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const { Writable } = require('stream');
44
const { closeSync, writeSync } = require('fs');
55

66
function SyncWriteStream(fd, options) {
7-
Writable.call(this);
7+
Writable.call(this, { autoDestroy: true });
88

99
options = options || {};
1010

1111
this.fd = fd;
1212
this.readable = false;
1313
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
14-
15-
this.on('end', () => this._destroy());
1614
}
1715

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

27-
SyncWriteStream.prototype._destroy = function() {
25+
SyncWriteStream.prototype._destroy = function(err, cb) {
2826
if (this.fd === null) // already destroy()ed
29-
return;
27+
return cb(err);
3028

3129
if (this.autoClose)
3230
closeSync(this.fd);
3331

3432
this.fd = null;
35-
return true;
33+
cb(err);
3634
};
3735

3836
SyncWriteStream.prototype.destroySoon =
39-
SyncWriteStream.prototype.destroy = function() {
40-
this._destroy();
41-
this.emit('close');
42-
return true;
43-
};
37+
SyncWriteStream.prototype.destroy;
4438

4539
module.exports = SyncWriteStream;

test/parallel/test-internal-fs-syncwritestream.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
1919
assert.strictEqual(stream.fd, 1);
2020
assert.strictEqual(stream.readable, false);
2121
assert.strictEqual(stream.autoClose, true);
22-
assert.strictEqual(stream.listenerCount('end'), 1);
2322
}
2423

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

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

50-
stream.on('close', common.mustCall(3));
48+
stream.on('close', common.mustCall());
49+
assert.strictEqual(stream.destroy(), stream);
50+
assert.strictEqual(stream.fd, null);
51+
}
52+
53+
// Verify that the stream will unset the fd after destroySoon().
54+
{
55+
const fd = fs.openSync(filename, 'w');
56+
const stream = new SyncWriteStream(fd);
5157

52-
assert.strictEqual(stream.destroy(), true);
58+
stream.on('close', common.mustCall());
59+
assert.strictEqual(stream.destroySoon(), stream);
5360
assert.strictEqual(stream.fd, null);
54-
assert.strictEqual(stream.destroy(), true);
55-
assert.strictEqual(stream.destroySoon(), true);
5661
}
5762

58-
// Verify that the 'end' event listener will also destroy the stream.
63+
// Verify that calling end() will also destroy the stream.
5964
{
6065
const fd = fs.openSync(filename, 'w');
6166
const stream = new SyncWriteStream(fd);
6267

6368
assert.strictEqual(stream.fd, fd);
6469

65-
stream.emit('end');
70+
stream.end();
6671
assert.strictEqual(stream.fd, null);
6772
}

0 commit comments

Comments
 (0)