memory-stream.js
819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
var Stream = require('stream');
var util = require('util');
module.exports = WritableStream;
util.inherits(WritableStream, Stream.Writable);
function WritableStream(options) {
Stream.Writable.call(this, options);
}
WritableStream.prototype.write = function () {
var ret = Stream.Writable.prototype.write.apply(this, arguments);
if (!ret) {
this.emit('drain');
}
return ret;
};
WritableStream.prototype._write = function (chunk, encoding, callback) {
this.write(chunk, encoding, callback);
};
WritableStream.prototype.toString = function () {
return this.toBuffer().toString();
};
WritableStream.prototype.toBuffer = function () {
var buffers = [];
this._writableState.buffer.forEach(function (data) {
buffers.push(data.chunk);
});
return Buffer.concat(buffers);
};