node/lib/internal/fs/sync_write_stream.js
Livia Medeiros 46dc5d7c9f
lib: prefer call() over apply() if argument list is not array
PR-URL: https://github.com/nodejs/node/pull/60796
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2025-11-23 08:45:46 +00:00

50 lines
1.1 KiB
JavaScript

'use strict';
const {
FunctionPrototypeCall,
ObjectSetPrototypeOf,
} = primordials;
const { kEmptyObject } = require('internal/util');
const { Writable } = require('stream');
const { closeSync, writeSync } = require('fs');
function SyncWriteStream(fd, options) {
FunctionPrototypeCall(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;