6

Say I have a Buffer:

let b = Buffer.from('');

how can I append to b? Is the only way to create a new Buffer?

b = Buffer.concat([b, z]);

on the same subject, is there a way to create a dynamic sized buffer, or should I use Array instead?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

11

To create a dynamic buffer use an array then concat the array similar to this:

let chunks = []

stream
  .on('data', chunk => chunks.push(chunk))
  .on('close', () => console.log(Buffer.concat(chunks)))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338