mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 00:30:06 +00:00

PR-URL: https://github.com/nodejs/node/pull/20764 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
46 lines
954 B
JavaScript
46 lines
954 B
JavaScript
'use strict';
|
|
|
|
const { Writable } = require('stream');
|
|
const { inherits } = require('util');
|
|
const { closeSync, writeSync } = require('fs');
|
|
|
|
function SyncWriteStream(fd, options) {
|
|
Writable.call(this);
|
|
|
|
options = options || {};
|
|
|
|
this.fd = fd;
|
|
this.readable = false;
|
|
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
|
|
|
this.on('end', () => this._destroy());
|
|
}
|
|
|
|
inherits(SyncWriteStream, Writable);
|
|
|
|
SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
|
|
writeSync(this.fd, chunk, 0, chunk.length);
|
|
cb();
|
|
return true;
|
|
};
|
|
|
|
SyncWriteStream.prototype._destroy = function() {
|
|
if (this.fd === null) // already destroy()ed
|
|
return;
|
|
|
|
if (this.autoClose)
|
|
closeSync(this.fd);
|
|
|
|
this.fd = null;
|
|
return true;
|
|
};
|
|
|
|
SyncWriteStream.prototype.destroySoon =
|
|
SyncWriteStream.prototype.destroy = function() {
|
|
this._destroy();
|
|
this.emit('close');
|
|
return true;
|
|
};
|
|
|
|
module.exports = SyncWriteStream;
|