-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (35 loc) · 1.06 KB
/
index.js
File metadata and controls
37 lines (35 loc) · 1.06 KB
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
/* SOURCE FILE - Copyright (c) 2017 bytes-stream - Tanase Laurentiu Iulian - https://github.com/RealTimeCom/bytes-stream */
'use strict';
const Transform = require('stream').Transform;
class bytes extends Transform {
constructor(r) {
super();
this.r = r;
this.t = 0; /*total bytes received*/
}
}
bytes.prototype._transform = function(chunk, enc, cb) {
let s = this.t,
/*save prev value*/
l = chunk.length;
this.t += l;
if (s < this.r[1]) { /*not ended*/
if (s >= this.r[0]) {
if (this.t > this.r[1]) {
this.push(chunk.slice(0, this.r[1] - s));
} else {
this.push(chunk);
}
} else {
if (this.t > this.r[1]) {
this.push(chunk.slice(this.r[0] - s, this.r[1] - s));
} else {
if (this.r[0] - s < l) { /*avoid slice empty, slice(chunk.length)*/
this.push(chunk.slice(this.r[0] - s));
}
}
}
}
cb();
};
module.exports = bytes;