mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
71785889c8
PR-URL: https://github.com/nodejs/node/pull/55044 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
ObjectSetPrototypeOf,
|
|
ReflectApply,
|
|
} = primordials;
|
|
const { kEmptyObject } = require('internal/util');
|
|
|
|
const { Writable } = require('stream');
|
|
const { closeSync, writeSync } = require('fs');
|
|
|
|
function SyncWriteStream(fd, options) {
|
|
ReflectApply(Writable, this, [{ autoDestroy: true }]);
|
|
|
|
options ||= kEmptyObject;
|
|
|
|
this.fd = fd;
|
|
this.readable = false;
|
|
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
|
}
|
|
|
|
ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
|
|
ObjectSetPrototypeOf(SyncWriteStream, Writable);
|
|
|
|
SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
|
|
try {
|
|
writeSync(this.fd, chunk, 0, chunk.length);
|
|
} catch (e) {
|
|
cb(e);
|
|
return;
|
|
}
|
|
cb();
|
|
};
|
|
|
|
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;
|