import { core, primordials } from "ext:core/mod.js";
const {
FunctionPrototypeCall,
ObjectSetPrototypeOf,
} = primordials;
const lazyStream = core.createLazyLoader("node:stream");
const lazyFs = core.createLazyLoader("node:fs");
let _initialized = false;
function SyncWriteStream(fd, options) {
if (!_initialized) {
_initialize();
}
FunctionPrototypeCall(lazyStream().Writable, this, { autoDestroy: true });
options = options || {};
this.fd = fd;
this.readable = false;
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
}
function _initialize() {
const { Writable } = lazyStream();
ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
ObjectSetPrototypeOf(SyncWriteStream, Writable);
_initialized = true;
}
SyncWriteStream.prototype._write = function (chunk, _encoding, cb) {
try {
lazyFs().writeSync(this.fd, chunk, 0, chunk.length);
} catch (e) {
cb(e);
return;
}
cb();
};
SyncWriteStream.prototype._destroy = function (err, cb) {
if (this.fd === null) {
return cb(err);
}
if (this.autoClose) {
lazyFs().closeSync(this.fd);
}
this.fd = null;
cb(err);
};
SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;
export default SyncWriteStream;