/
index.js
40 lines (32 loc) · 847 Bytes
/
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
const { Transform } = require('stream')
class ChopStream extends Transform {
constructor (size, opts = {}) {
super()
if (typeof size === 'object') {
opts = size
size = opts.size
}
this.size = size || 1024
this.padding = opts.padding
this.buffer = Buffer.alloc(0)
}
_transform (chunk, _, next) {
const buf = Buffer.concat([this.buffer, chunk])
let i = 0
for (; i <= buf.length - this.size; i += this.size) {
this.push(buf.slice(i, i + this.size))
}
this.buffer = buf.slice(i)
next()
}
_flush (next) {
if (this.padding && this.buffer.length > 0) {
const zeroes = Buffer.alloc(this.size - this.buffer.length)
this.push(Buffer.concat([this.buffer, zeroes]))
} else {
this.push(this.buffer)
}
next()
}
}
module.exports = ChopStream