0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/lib/internal/fs/sync_write_stream.js
Matteo Collina abafd38c8d 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>
2019-03-18 10:30:07 +01:00

40 lines
920 B
JavaScript

'use strict';
const { Writable } = require('stream');
const { closeSync, writeSync } = require('fs');
function SyncWriteStream(fd, options) {
Writable.call(this, { autoDestroy: true });
options = options || {};
this.fd = fd;
this.readable = false;
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
}
Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
Object.setPrototypeOf(SyncWriteStream, Writable);
SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
writeSync(this.fd, chunk, 0, chunk.length);
cb();
return true;
};
SyncWriteStream.prototype._destroy = function(err, cb) {
if (this.fd === null) // already destroy()ed
return cb(err);
if (this.autoClose)
closeSync(this.fd);
this.fd = null;
cb(err);
};
SyncWriteStream.prototype.destroySoon =
SyncWriteStream.prototype.destroy;
module.exports = SyncWriteStream;