Don't allocate buffer twice#1403
Conversation
* getBuffer returns a buffer of concatenated response chunks. * Its result is stored in rawBody and passed to parseBody. * Then it meets Buffer.from(rawBody) and triggers a new buffer allocation: https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_buffer Now a new Buffer created without copying the underlying memory. https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
|
Tests are failing. |
|
Yeah, I know. Just need time to get familiar with the project and tests. |
|
|
|
@sindresorhus Although we could add an environment variable to disable this. if (responseType === 'buffer') {
return process.env.GOT_MUTABLE_BODY ? rawBody : Buffer.from(rawBody);
}or should we just return the original buffer? If so, then this could happen: const promise = got('https://somesitehere.com'); //=> 123
const buffer = await promise.buffer();
buffer.write(49, 0);
const text = await promise.text(); //=> 223, not 123!or we could warn users in the docs... If we decide to return the original buffer then it will use 2x less RAM for people who do |
That seems extremely unlikely though. Why would they write to a buffer they got from a method call. But maybe we could just override the Also, upvote and comment on this issue: nodejs/node#27080 |
@szmarczak Thoughts on this? |
That wouldn't work as you'd still be able to do |
|
We could |
|
|
@szmarczak Maybe we could at least wrap the TS type in |

getBufferreturns a buffer of concatenated response chunks.rawBodyand passed toparseBody.Buffer.from(rawBody)and triggers a new buffer allocation:https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_buffer
Now a new Buffer created without copying the underlying memory.
https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
Checklist