2020-03-31 13:49:18 +00:00
|
|
|
/**
|
|
|
|
* Concatenate a list of buffers.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2020-03-29 13:43:52 +00:00
|
|
|
export function concat(chunks: Array<Buffer>): Buffer {
|
|
|
|
const sum = chunks.map((it) => it.length).reduce((p, c) => p + c, 0);
|
|
|
|
return Buffer.concat(chunks, sum);
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:49:18 +00:00
|
|
|
/**
|
|
|
|
* Concatenate then encode a list of buffers.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2020-03-29 13:43:52 +00:00
|
|
|
export function encode(chunks: Array<Buffer>, encoding: string): string {
|
|
|
|
if (chunks.length === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return concat(chunks).toString(encoding);
|
|
|
|
}
|