-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleMemoryStream.js
69 lines (56 loc) · 1.73 KB
/
simpleMemoryStream.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
const stream = require('stream');
const Duplex = stream.Duplex;
const util = require('util');
function simpleMemoryStream(options) {
if (!(this instanceof simpleMemoryStream)) {
return new simpleMemoryStream(options);
}
Duplex.call(this, options);
this.buffer = null;
this.waiting = false;
}
util.inherits(simpleMemoryStream, Duplex);
simpleMemoryStream.prototype._write = function (chunk, enc, cb) {
let chunkBuffer = null;
if (chunk instanceof Buffer) {
chunkBuffer = chunk;
} else {
chunkBuffer = new Buffer(chunk, enc);
}
if (this.buffer === null) {
this.buffer = chunkBuffer;
} else {
this.buffer = Buffer.concat([this.buffer, chunkBuffer]);
}
if (this.waiting) {
const prevBuffer = this.buffer;
this.buffer = null;
const keepPushing = this.push(prevBuffer); // may cause a read() now
this.waiting = keepPushing;
}
cb();
};
simpleMemoryStream.prototype._read = function readBytes(n) {
if (this.buffer === null) {
this.waiting = true;
return;
}
const chunkSize = Math.min(n, this.buffer.byteLength);
if (chunkSize > 0) {
const pushData = this.buffer.slice(0, chunkSize);
this.buffer = this.buffer.slice(chunkSize); // throw away what was read
const keepPushing = this.push(pushData);
this.waiting = keepPushing;
} else {
this.waiting = true;
}
};
simpleMemoryStream.prototype.finish = function () {
if (this.buffer !== null) {
this.push(this.buffer);
}
this.push(null);
this.waiting = false;
this.buffer = null;
}
module.exports = simpleMemoryStream;