Nodejs でストリームをバッファに変換する方法

Nodejs では、ネットワーク経由での転送やディスクからの読み取りなどの非テキスト ファイル (画像など) の IO 操作を処理するときに、コンテンツを stream.Readable として受け取る可能性が高く、バイト サイズや画像のサイズを計算するなど、メモリ内の完全なデータを処理する前に、streambuffer に保存する必要があります。その方法はいくつかあります。

with for await to iterate over the async iterable stream

const chunks = [];
for await (const chunk of stream) {
  chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);

with event listener to listen to data and event events from the stream

const chunks = [];
stream.on('data', (chunk) => {
  chunks.push(chunk);
});
stream.on('end', () => {
  const buffer = Buffer.concat(chunks);
});

There is a very good article with more details and information: Understanding Streams in Node.js

nodejs javascript