1'use strict';
2
3const {
4  ObjectSetPrototypeOf,
5  ReflectApply,
6} = primordials;
7const { kEmptyObject } = require('internal/util');
8
9const { Writable } = require('stream');
10const { closeSync, writeSync } = require('fs');
11
12function SyncWriteStream(fd, options) {
13  ReflectApply(Writable, this, [{ autoDestroy: true }]);
14
15  options = options || kEmptyObject;
16
17  this.fd = fd;
18  this.readable = false;
19  this.autoClose = options.autoClose === undefined ? true : options.autoClose;
20}
21
22ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
23ObjectSetPrototypeOf(SyncWriteStream, Writable);
24
25SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
26  try {
27    writeSync(this.fd, chunk, 0, chunk.length);
28  } catch (e) {
29    cb(e);
30    return;
31  }
32  cb();
33};
34
35SyncWriteStream.prototype._destroy = function(err, cb) {
36  if (this.fd === null) // already destroy()ed
37    return cb(err);
38
39  if (this.autoClose)
40    closeSync(this.fd);
41
42  this.fd = null;
43  cb(err);
44};
45
46SyncWriteStream.prototype.destroySoon =
47  SyncWriteStream.prototype.destroy;
48
49module.exports = SyncWriteStream;
50