Node.js: How to read a stream into a buffer?

前端 未结 6 1862
野趣味
野趣味 2020-12-07 12:05

I wrote a pretty simple function that downloads an image from a given URL, resize it and upload to S3 (using \'gm\' and \'knox\'), I have no idea if I\'m doing the reading o

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 12:51

    in ts, [].push(bufferPart) is not compatible;

    so:

    getBufferFromStream(stream: Part | null): Promise {
        if (!stream) {
            throw 'FILE_STREAM_EMPTY';
        }
        return new Promise(
            (r, j) => {
                let buffer = Buffer.from([]);
                stream.on('data', buf => {
                   buffer = Buffer.concat([buffer, buf]);
                });
                stream.on('end', () => r(buffer));
                stream.on('error', j);
            }
        );
    }
    

提交回复
热议问题