Skip to content

Commit 2c19f59

Browse files
bengltargos
authored andcommitted
doc: clarify array args to Buffer.from()
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: #28725 PR-URL: #48274 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Harshitha K P <[email protected]>
1 parent 72de4e7 commit 2c19f59

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

doc/api/buffer.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,12 @@ const { Buffer } = require('node:buffer');
11161116
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
11171117
```
11181118

1119+
If `array` is an `Array`-like object (that is, one with a `length` property of
1120+
type `number`), it is treated as if it is an array, unless it is a `Buffer` or
1121+
a `Uint8Array`. This means all other `TypedArray` variants get treated as an
1122+
`Array`. To create a `Buffer` from the bytes backing a `TypedArray`, use
1123+
[`Buffer.copyBytesFrom()`][].
1124+
11191125
A `TypeError` will be thrown if `array` is not an `Array` or another type
11201126
appropriate for `Buffer.from()` variants.
11211127

@@ -5488,6 +5494,7 @@ introducing security vulnerabilities into an application.
54885494
[`Buffer.allocUnsafe()`]: #static-method-bufferallocunsafesize
54895495
[`Buffer.allocUnsafeSlow()`]: #static-method-bufferallocunsafeslowsize
54905496
[`Buffer.concat()`]: #static-method-bufferconcatlist-totallength
5497+
[`Buffer.copyBytesFrom()`]: #static-method-buffercopybytesfromview-offset-length
54915498
[`Buffer.from(array)`]: #static-method-bufferfromarray
54925499
[`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length
54935500
[`Buffer.from(buffer)`]: #static-method-bufferfrombuffer

0 commit comments

Comments
 (0)