Skip to content

Commit

Permalink
doc: clarify array args to Buffer.from()
Browse files Browse the repository at this point in the history
The code for Buffer.from() treats non-Buffer and non-Uint8Array
Array-likes as Arrays. This creates some confusion when passing various
TypedArrays to Buffer.from(). The documentation now reflects the actual
behavior.

Fixes: nodejs#28725
  • Loading branch information
bengl committed May 31, 2023
1 parent 5e98a74 commit 45707ae
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,12 @@ const { Buffer } = require('node:buffer');
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
```

Note that if `array` is an `Array`-like object (that is, one with a `length`
property of type `number`) it is treated as if it is an array, unless it is a
`Buffer` or a `Uint8Array`. This means all other `TypedArray` variants get
treated as `Array`. To create a `Buffer` from the underlying `ArrayBuffer`, use
the `.buffer` property directly.

A `TypeError` will be thrown if `array` is not an `Array` or another type
appropriate for `Buffer.from()` variants.

Expand Down

0 comments on commit 45707ae

Please sign in to comment.