|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const Buffer = require('buffer').Buffer;
|
4 |
| -const Stream = require('stream').Stream; |
| 4 | +const Writable = require('stream').Writable; |
5 | 5 | const fs = require('fs');
|
6 | 6 | const util = require('util');
|
7 | 7 | const constants = process.binding('constants').fs;
|
@@ -58,65 +58,41 @@ exports.stringToFlags = stringToFlags;
|
58 | 58 |
|
59 | 59 | // Temporary hack for process.stdout and process.stderr when piped to files.
|
60 | 60 | function SyncWriteStream(fd, options) {
|
61 |
| - Stream.call(this); |
| 61 | + Writable.call(this); |
62 | 62 |
|
63 | 63 | options = options || {};
|
64 | 64 |
|
65 | 65 | this.fd = fd;
|
66 |
| - this.writable = true; |
67 | 66 | this.readable = false;
|
68 | 67 | this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
69 |
| -} |
70 |
| - |
71 |
| -util.inherits(SyncWriteStream, Stream); |
72 |
| - |
73 |
| -SyncWriteStream.prototype.write = function(data, arg1, arg2) { |
74 |
| - var encoding, cb; |
75 |
| - |
76 |
| - // parse arguments |
77 |
| - if (arg1) { |
78 |
| - if (typeof arg1 === 'string') { |
79 |
| - encoding = arg1; |
80 |
| - cb = arg2; |
81 |
| - } else if (typeof arg1 === 'function') { |
82 |
| - cb = arg1; |
83 |
| - } else { |
84 |
| - throw new Error('Bad arguments'); |
85 |
| - } |
86 |
| - } |
87 |
| - assertEncoding(encoding); |
88 |
| - |
89 |
| - // Change strings to buffers. SLOW |
90 |
| - if (typeof data === 'string') { |
91 |
| - data = Buffer.from(data, encoding); |
92 |
| - } |
93 | 68 |
|
94 |
| - fs.writeSync(this.fd, data, 0, data.length); |
| 69 | + this.on('end', () => this._destroy()); |
| 70 | +} |
95 | 71 |
|
96 |
| - if (cb) { |
97 |
| - process.nextTick(cb); |
98 |
| - } |
| 72 | +util.inherits(SyncWriteStream, Writable); |
99 | 73 |
|
| 74 | +SyncWriteStream.prototype._write = function(chunk, encoding, cb) { |
| 75 | + fs.writeSync(this.fd, chunk, 0, chunk.length); |
| 76 | + cb(); |
100 | 77 | return true;
|
101 | 78 | };
|
102 | 79 |
|
| 80 | +SyncWriteStream.prototype._destroy = function() { |
| 81 | + if (this.fd === null) // already destroy()ed |
| 82 | + return; |
103 | 83 |
|
104 |
| -SyncWriteStream.prototype.end = function(data, arg1, arg2) { |
105 |
| - if (data) { |
106 |
| - this.write(data, arg1, arg2); |
107 |
| - } |
108 |
| - this.destroy(); |
109 |
| -}; |
110 |
| - |
111 |
| - |
112 |
| -SyncWriteStream.prototype.destroy = function() { |
113 | 84 | if (this.autoClose)
|
114 | 85 | fs.closeSync(this.fd);
|
| 86 | + |
115 | 87 | this.fd = null;
|
116 |
| - this.emit('close'); |
117 | 88 | return true;
|
118 | 89 | };
|
119 | 90 |
|
120 |
| -SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy; |
| 91 | +SyncWriteStream.prototype.destroySoon = |
| 92 | +SyncWriteStream.prototype.destroy = function() { |
| 93 | + this._destroy(); |
| 94 | + this.emit('close'); |
| 95 | + return true; |
| 96 | +}; |
121 | 97 |
|
122 | 98 | exports.SyncWriteStream = SyncWriteStream;
|
0 commit comments