Skip to content

[DOC] Buffer.from / new Buffer accept Uint8Array (missing) #14118

Closed

Description

@reviewher

Both can accept a Uint8Array. Buffer.from explicitly checks for Uint8Array

It looks like a documentation omission. If so, I can send a PR.

Activity

  1. Trott commented on Jul 7, 2017

    @Trott
    Member

    @nodejs/documentation

  2. added
    docIssues and PRs related to Node.js documentation.
    bufferIssues and PRs related to the buffer subsystem.
    on Jul 7, 2017
  3. benjamingr commented on Jul 9, 2017

    @benjamingr
    Member

    @reviewher the docs already specify it takes a TypedArray - the fact that the implementation does a shortcut should not be an issue.

    Was the unclear part the fact it's not obvious that a UInt8Array is a TypedArray?

  4. reviewher commented on Jul 9, 2017

    @reviewher
    Author

    @benjamingr The documentation lists 4 signatures for Buffer.from:

    • Buffer.from(array) -- Array of bytes
    • Buffer.from(arrayBuffer[, byteOffset[, length]]) -- ArrayBuffer or the .buffer property of a TypedArray.
    • Buffer.from(buffer) -- Buffer
    • Buffer.from(string[, encoding]) -- string

    None of those cases cover an actual Uint8Array object! We are not talking about "the .buffer property" but rather the actual Uint8Array object. An example would be useful:

    > var x = new Uint8Array([1,2,3,4,5])
    > x
    Uint8Array [ 1, 2, 3, 4, 5 ]
    
    > Buffer.from(x.buffer) // <-- this is from the docs
    <Buffer 01 02 03 04 05>
    > x.buffer instanceof ArrayBuffer // <-- x.buffer is an ArrayBuffer, not a typed array
    true
    > x.buffer instanceof Uint8Array // <-- x.buffer is definitely not a Uint8Array
    false
    
    > Buffer.from(x) // <-- this is the case that works and is not covered in the docs
    <Buffer 01 02 03 04 05>
    > Buffer.isBuffer(x) // <-- x is definitely not a buffer
    false
    > x instanceof ArrayBuffer // <-- x is definitely not an ArrayBuffer
    false
    > x instanceof Uint8Array // <-- x is definitely a Uint8Array
    true

    To contrast with other areas of the documentation:

    @benjamingr it's also possible you took a cursory glance at https://nodejs.org/dist/latest-v8.x/docs/api/buffer.html#buffer_buffers_and_typedarray and assumed it was covered, but alas it is not. The ending of that section does discuss typed arrays, but it is merely contrasting the signatures of Buffer.from and TypedArray.from.

    @Trott to be consistent with the other areas of the documentation, assuming that Buffer.from and the new Buffer constructor were intended to take Uint8Array, the simplest fix would add a few words the Buffer cases in new Buffer and Buffer.from in doc/api/buffer.md :

    -* `buffer` {Buffer} An existing `Buffer` to copy data from
    +* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`] to copy data from
  5. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    good first issueIssues that are suitable for first-time contributors.
    on Apr 11, 2018
  6. apapirovski commented on Apr 11, 2018

    @apapirovski
    Contributor

    @nodejs/documentation This has been around for a while and looks like a valid bug report for the docs.

  7. reviewher commented on Apr 11, 2018

    @reviewher
    Author

    This is a one-line change to /doc/api/buffer.md, included in my comment in this issue. I'll gladly send a PR, but @benjamingr seemed to have some concern that isn't obvious, and it may make sense to wait for a reply before making a doc change

  8. apapirovski commented on Apr 11, 2018

    @apapirovski
    Contributor

    @reviwher I think they had the same first look thought which is that it mentions TypedArray but of course on closer inspection it refers to the .buffer property. Much like you already highlighted above. I think if someone can come up with a sensible PR then this could get fixed quickly.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions