Refactor the stream sender

This commit is contained in:
NI
2019-09-19 15:04:20 +08:00
parent 2736a3db9d
commit 2d47cd004c
6 changed files with 309 additions and 123 deletions

View File

@@ -46,3 +46,32 @@ export function getRands(n, min, max) {
return r;
}
/**
* Separate given buffer to multiple ones based on input max length
*
* @param {Uint8Array} buf Buffer to separate
* @param {number} max Max length of each buffer
*
* @returns {Array<Uint8Array>} Separated buffers
*
*/
export function separateBuffer(buf, max) {
let start = 0,
result = [];
while (start < buf.length) {
let remain = buf.length - start;
if (remain <= max) {
result.push(buf.slice(start, start + remain));
return result;
}
remain = max;
result.push(buf.slice(start, start + remain));
start += remain;
}
}