mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
abafd38c8d
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>
40 lines
920 B
JavaScript
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;
|