Skip to content

feature: support initializing Buffer from an ArrayBuffer #106

Closed
@feross

Description

@feross

I propose we make the Buffer constructor also accept ArrayBuffer.

Currently this is what happens:

var u = new Uint8Array([1, 2, 3, 4]);
var ab = u.buffer;
var b = new Buffer(ab);
console.log(b); // <Buffer >

This is what should happen:

console.log(b); // <Buffer 01 02 03 04>

When writing isomorphic code (i.e. code that runs on the server and in the browser), it's often the case that you'll get an ArrayBuffer from a DOM API (xhr, websockets, webrtc, etc.) and need to convert it to a Buffer to work with modules in the npm ecosystem. Users often expect that new Buffer(arraybuffer) will work and they open issues when it doesn't.

We have the buffer npm module which gives us the same Buffer API in the browser (and is used by browserify), however it tracks the node.js/io.js buffer exactly, so we can't add support for new Buffer(arraybuffer) unless core does too.

I know the Buffer constructor already takes a million different argument types, so it couldn't hurt to add one more, right? Curious to see what the community thinks about this. If there's interest, I can send a PR.

Metadata

Metadata

Assignees

Labels

bufferIssues and PRs related to the buffer subsystem.feature requestIssues that request new features to be added to Node.js.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions