nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
44 lines (32 loc) • 1.13 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/fs/sync_write_stream.js
import { kEmptyObject } from "nstdlib/lib/internal/util";
import { Writable } from "nstdlib/lib/stream";
import { closeSync, writeSync } from "nstdlib/lib/fs";
function SyncWriteStream(fd, options) {
ReflectApply(Writable, this, [{ autoDestroy: true }]);
options = options || kEmptyObject;
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) {
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;
export default SyncWriteStream;