-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
209 lines (168 loc) · 5.98 KB
/
index.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
var STREAM = require('stream'),
UTIL = require('util'),
StringDecoder = require('string_decoder').StringDecoder;
function MemoryReadableStream(data, options) {
if (!(this instanceof MemoryReadableStream))
return new MemoryReadableStream(data, options);
MemoryReadableStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryReadableStream, STREAM.Readable);
function MemoryWritableStream(data, options) {
if (!(this instanceof MemoryWritableStream))
return new MemoryWritableStream(data, options);
MemoryWritableStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryWritableStream, STREAM.Writable);
function MemoryDuplexStream(data, options) {
if (!(this instanceof MemoryDuplexStream))
return new MemoryDuplexStream(data, options);
MemoryDuplexStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryDuplexStream, STREAM.Duplex);
MemoryReadableStream.prototype.init =
MemoryWritableStream.prototype.init =
MemoryDuplexStream.prototype.init = function init (data, options) {
var self = this;
this.queue = [];
if (data) {
if (!Array.isArray(data)) {
data = [ data ];
}
data.forEach(function (chunk) {
if (!(chunk instanceof Buffer)) {
chunk = new Buffer(chunk);
}
self.queue.push(chunk);
});
}
options = options || {};
this.maxbufsize = options.hasOwnProperty('maxbufsize') ? options.maxbufsize
: null;
this.bufoverflow = options.hasOwnProperty('bufoverflow') ? options.bufoverflow
: null;
this.frequence = options.hasOwnProperty('frequence') ? options.frequence
: null;
};
function MemoryStream (data, options) {
if (!(this instanceof MemoryStream))
return new MemoryStream(data, options);
options = options || {};
var readable = options.hasOwnProperty('readable') ? options.readable : true,
writable = options.hasOwnProperty('writable') ? options.writable : true;
if (readable && writable) {
return new MemoryDuplexStream(data, options);
} else if (readable) {
return new MemoryReadableStream(data, options);
} else if (writable) {
return new MemoryWritableStream(data, options);
} else {
throw new Error("Unknown stream type Readable, Writable or Duplex ");
}
}
MemoryStream.createReadStream = function (data, options) {
options = options || {};
options.readable = true;
options.writable = false;
return new MemoryStream(data, options);
};
MemoryStream.createWriteStream = function (data, options) {
options = options || {};
options.readable = false;
options.writable = true;
return new MemoryStream(data, options);
};
MemoryReadableStream.prototype._read =
MemoryDuplexStream.prototype._read = function _read (n) {
var self = this,
frequence = self.frequence || 0,
wait_data = this instanceof STREAM.Duplex && ! this._writableState.finished ? true : false;
if ( ! this.queue.length && ! wait_data) {
this.push(null);// finish stream
} else if (this.queue.length) {
setTimeout(function () {
if (self.queue.length) {
var chunk = self.queue.shift();
if (chunk && ! self._readableState.ended) {
if ( ! self.push(chunk) ) {
self.queue.unshift(chunk);
}
}
}
}, frequence);
}
};
MemoryWritableStream.prototype._write =
MemoryDuplexStream.prototype._write = function _write (chunk, encoding, cb) {
var decoder = null;
try {
decoder = this.decodeStrings && encoding ? new StringDecoder(encoding) : null;
} catch (err){
return cb(err);
}
var decoded_chunk = decoder ? decoder.write(chunk) : chunk,
queue_size = this._getQueueSize(),
chunk_size = decoded_chunk.length;
if (this.maxbufsize && (queue_size + chunk_size) > this.maxbufsize ) {
if (this.bufoverflow) {
return cb("Buffer overflowed (" + this.bufoverflow + "/" + queue_size + ")");
} else {
return cb();
}
}
if (this instanceof STREAM.Duplex) {
while (this.queue.length) {
this.push(this.queue.shift());
}
this.push(decoded_chunk);
} else {
this.queue.push(decoded_chunk);
}
cb();
};
MemoryDuplexStream.prototype.end = function (chunk, encoding, cb) {
var self = this;
return MemoryDuplexStream.super_.prototype.end.call(this, chunk, encoding, function () {
self.push(null);//finish readble stream too
if (cb) cb();
});
};
MemoryReadableStream.prototype._getQueueSize =
MemoryWritableStream.prototype._getQueueSize =
MemoryDuplexStream.prototype._getQueueSize = function () {
var queuesize = 0, i;
for (i = 0; i < this.queue.length; i++) {
queuesize += Array.isArray(this.queue[i]) ? this.queue[i][0].length
: this.queue[i].length;
}
return queuesize;
};
MemoryWritableStream.prototype.toString =
MemoryDuplexStream.prototype.toString =
MemoryReadableStream.prototype.toString =
MemoryWritableStream.prototype.getAll =
MemoryDuplexStream.prototype.getAll =
MemoryReadableStream.prototype.getAll = function () {
var self = this,
ret = '';
this.queue.forEach(function (data) {
ret += data;
});
return ret;
};
MemoryWritableStream.prototype.toBuffer =
MemoryDuplexStream.prototype.toBuffer =
MemoryReadableStream.prototype.toBuffer = function () {
var buffer = new Buffer(this._getQueueSize()),
currentOffset = 0;
this.queue.forEach(function (data) {
var data_buffer = data instanceof Buffer ? data : new Buffer(data);
data_buffer.copy(buffer, currentOffset);
currentOffset += data.length;
});
return buffer;
};
module.exports = MemoryStream;