Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f202c9

Browse filesBrowse 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
Copy full SHA for 2f202c9

File tree

Expand file treeCollapse file tree

1 file changed

+7
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+7
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/buffer.md‎

Copy file name to clipboardExpand all lines: doc/api/buffer.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.