Skip to content

Commit 2f202c9

Browse files
benglMoLow
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 <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
1 parent 286c358 commit 2f202c9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

doc/api/buffer.md

+7
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,12 @@ const { Buffer } = require('node:buffer');
11141114
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
11151115
```
11161116

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

@@ -5470,6 +5476,7 @@ introducing security vulnerabilities into an application.
54705476
[`Buffer.allocUnsafe()`]: #static-method-bufferallocunsafesize
54715477
[`Buffer.allocUnsafeSlow()`]: #static-method-bufferallocunsafeslowsize
54725478
[`Buffer.concat()`]: #static-method-bufferconcatlist-totallength
5479+
[`Buffer.copyBytesFrom()`]: #static-method-buffercopybytesfromview-offset-length
54735480
[`Buffer.from(array)`]: #static-method-bufferfromarray
54745481
[`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length
54755482
[`Buffer.from(buffer)`]: #static-method-bufferfrombuffer

0 commit comments

Comments
 (0)