Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
akupiec
GitHub Repository: akupiec/mad-proxy
Path: blob/master/src/middlewares/utils/StreamWriter.js
115 views
1
const stream = require('stream');
2
3
module.exports = class StreamWriter extends stream.Writable {
4
constructor(...args) {
5
super(...args);
6
this.writable = true;
7
this.bytes = 0;
8
this.raw = [];
9
}
10
11
write(buf) {
12
if(buf instanceof Buffer) {
13
this.raw.push(buf);
14
this.bytes += buf.length;
15
} else {
16
const chunk = Buffer.from(buf);
17
this.write(chunk);
18
}
19
}
20
21
end(buf) {
22
if (buf) this.write(buf);
23
24
this.writable = false;
25
this.body = Buffer.concat(this.raw);
26
super.end(...arguments);
27
}
28
};
29
30