GeezerVison is showing github.com — doc: clarify array args to Buffer.from() · nodejs/node@2c19f59
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
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 number | Diff line number | Diff line change |
|---|
@@ -1116,6 +1116,12 @@ const { Buffer } = require('node:buffer'); |
1116 | 1116 | const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); |
1117 | 1117 | ``` |
1118 | 1118 |
|
| 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 | + |
1119 | 1125 | A `TypeError` will be thrown if `array` is not an `Array` or another type |
1120 | 1126 | appropriate for `Buffer.from()` variants. |
1121 | 1127 |
|
@@ -5488,6 +5494,7 @@ introducing security vulnerabilities into an application. |
5488 | 5494 | [`Buffer.allocUnsafe()`]: #static-method-bufferallocunsafesize |
5489 | 5495 | [`Buffer.allocUnsafeSlow()`]: #static-method-bufferallocunsafeslowsize |
5490 | 5496 | [`Buffer.concat()`]: #static-method-bufferconcatlist-totallength |
| 5497 | +[`Buffer.copyBytesFrom()`]: #static-method-buffercopybytesfromview-offset-length |
5491 | 5498 | [`Buffer.from(array)`]: #static-method-bufferfromarray |
5492 | 5499 | [`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length |
5493 | 5500 | [`Buffer.from(buffer)`]: #static-method-bufferfrombuffer |
|