streamToBuffer.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * stream 转 buffer
 *
 * @since 0.0.1
 * @param stream stream 对象
 * @returns buffer 对象
 * @example
 *
 * const stream = fs.createReadStream('./text.txt');
 * const buf = await streamToBuffer(stream);
 */
function streamToBuffer(stream) {
    return new Promise((resolve, reject) => {
        const buffers = [];
        stream.on('error', reject);
        stream.on('data', (data) => buffers.push(data));
        stream.on('end', () => resolve(Buffer.concat(buffers)));
    });
}
exports.default = streamToBuffer;