GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feature: support initializing Buffer from an ArrayBuffer · Issue #106 · nodejs/node

feature: support initializing Buffer from an ArrayBuffer #106

New issue
Copy link
New issue
Copy link
Closed
Closed
feature: support initializing Buffer from an ArrayBuffer#106
Copy link
Assignees
trevnorris
Labels
bufferIssues and PRs related to the buffer subsystem.feature requestIssues requesting new Node.js features.

Description

@feross
feross
opened on Dec 8, 2014
Issue body actions

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.

Activity

Next
  1. caineio
    added
    caine: need info
    on Dec 8, 2014
  2. chrisdickinson commented on Dec 8, 2014

    @chrisdickinson
    chrisdickinson
    on Dec 8, 2014
    Contributor
    More actions

    Curious: would this copy the memory represented by the ArrayBuffer, or act as a view onto it?

  3. vkurchatkin commented on Dec 8, 2014

    @vkurchatkin
    vkurchatkin
    on Dec 8, 2014
    Contributor
    More actions

    @chrisdickinson It should behave the same way as new Buffer(buffer) i.e. copy, but the latter is impossible anyway

  4. caineio
    added
    needs info
    need info
    needs info
    and removed
    need info
    needs info
    needs info
    needs info
    on Dec 8, 2014
  5. 37 remaining items

  6. trevnorris commented on Jun 17, 2015

    @trevnorris
    trevnorris
    on Jun 17, 2015
    Contributor
    More actions

    Fixed by 197ba00.

  7. trevnorris
    closed this as completedon Jun 17, 2015
  8. trevnorris
    added 9 commits that reference this issue on Jul 22, 2015

    buffer: allow ArrayBuffer as Buffer argument

    717c634

    buffer: allow ArrayBuffer as Buffer argument

    653021a

    buffer: allow ArrayBuffer as Buffer argument

    ff3b252

    buffer: allow ArrayBuffer as Buffer argument

    f3ed938

    buffer: allow ArrayBuffer as Buffer argument

    cd4711b

    buffer: allow ArrayBuffer as Buffer argument

    d57350c

    buffer: allow ArrayBuffer as Buffer argument

    4643b8b
  9. Tiagoao99 commented on Sep 27, 2023

    @Tiagoao99
    Tiagoao99
    on Sep 27, 2023
    More actions

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @trevnorris
    trevnorris

Labels

bufferIssues and PRs related to the buffer subsystem.feature requestIssues requesting new Node.js features.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.