fs: use proper .destroy() implementation for SyncWriteStream
PR-URL: https://github.com/nodejs/node/pull/26690 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
36101558fd
commit
abafd38c8d
@ -4,15 +4,13 @@ const { Writable } = require('stream');
|
|||||||
const { closeSync, writeSync } = require('fs');
|
const { closeSync, writeSync } = require('fs');
|
||||||
|
|
||||||
function SyncWriteStream(fd, options) {
|
function SyncWriteStream(fd, options) {
|
||||||
Writable.call(this);
|
Writable.call(this, { autoDestroy: true });
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
this.fd = fd;
|
this.fd = fd;
|
||||||
this.readable = false;
|
this.readable = false;
|
||||||
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
||||||
|
|
||||||
this.on('end', () => this._destroy());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
|
Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
|
||||||
@ -24,22 +22,18 @@ SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncWriteStream.prototype._destroy = function() {
|
SyncWriteStream.prototype._destroy = function(err, cb) {
|
||||||
if (this.fd === null) // already destroy()ed
|
if (this.fd === null) // already destroy()ed
|
||||||
return;
|
return cb(err);
|
||||||
|
|
||||||
if (this.autoClose)
|
if (this.autoClose)
|
||||||
closeSync(this.fd);
|
closeSync(this.fd);
|
||||||
|
|
||||||
this.fd = null;
|
this.fd = null;
|
||||||
return true;
|
cb(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
SyncWriteStream.prototype.destroySoon =
|
SyncWriteStream.prototype.destroySoon =
|
||||||
SyncWriteStream.prototype.destroy = function() {
|
SyncWriteStream.prototype.destroy;
|
||||||
this._destroy();
|
|
||||||
this.emit('close');
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = SyncWriteStream;
|
module.exports = SyncWriteStream;
|
||||||
|
@ -19,7 +19,6 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
|
|||||||
assert.strictEqual(stream.fd, 1);
|
assert.strictEqual(stream.fd, 1);
|
||||||
assert.strictEqual(stream.readable, false);
|
assert.strictEqual(stream.readable, false);
|
||||||
assert.strictEqual(stream.autoClose, true);
|
assert.strictEqual(stream.autoClose, true);
|
||||||
assert.strictEqual(stream.listenerCount('end'), 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify constructing the instance with specified options.
|
// Verify constructing the instance with specified options.
|
||||||
@ -29,7 +28,6 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
|
|||||||
assert.strictEqual(stream.fd, 1);
|
assert.strictEqual(stream.fd, 1);
|
||||||
assert.strictEqual(stream.readable, false);
|
assert.strictEqual(stream.readable, false);
|
||||||
assert.strictEqual(stream.autoClose, false);
|
assert.strictEqual(stream.autoClose, false);
|
||||||
assert.strictEqual(stream.listenerCount('end'), 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the file will be written synchronously.
|
// Verify that the file will be written synchronously.
|
||||||
@ -47,21 +45,28 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
|
|||||||
const fd = fs.openSync(filename, 'w');
|
const fd = fs.openSync(filename, 'w');
|
||||||
const stream = new SyncWriteStream(fd);
|
const stream = new SyncWriteStream(fd);
|
||||||
|
|
||||||
stream.on('close', common.mustCall(3));
|
stream.on('close', common.mustCall());
|
||||||
|
assert.strictEqual(stream.destroy(), stream);
|
||||||
assert.strictEqual(stream.destroy(), true);
|
|
||||||
assert.strictEqual(stream.fd, null);
|
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 the stream will unset the fd after destroySoon().
|
||||||
|
{
|
||||||
|
const fd = fs.openSync(filename, 'w');
|
||||||
|
const stream = new SyncWriteStream(fd);
|
||||||
|
|
||||||
|
stream.on('close', common.mustCall());
|
||||||
|
assert.strictEqual(stream.destroySoon(), stream);
|
||||||
|
assert.strictEqual(stream.fd, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that calling end() will also destroy the stream.
|
||||||
{
|
{
|
||||||
const fd = fs.openSync(filename, 'w');
|
const fd = fs.openSync(filename, 'w');
|
||||||
const stream = new SyncWriteStream(fd);
|
const stream = new SyncWriteStream(fd);
|
||||||
|
|
||||||
assert.strictEqual(stream.fd, fd);
|
assert.strictEqual(stream.fd, fd);
|
||||||
|
|
||||||
stream.emit('end');
|
stream.end();
|
||||||
assert.strictEqual(stream.fd, null);
|
assert.strictEqual(stream.fd, null);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user