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 c6eb2b4

Browse filesBrowse files
jasnelldanielleadams
authored andcommitted
doc: clarify Buffer.from when using ArrayBuffer
Fixes: #31348 Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36785 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ad1d8fb commit c6eb2b4
Copy full SHA for c6eb2b4

File tree

Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/buffer.md‎

Copy file name to clipboardExpand all lines: doc/api/buffer.md
+16-1Lines changed: 16 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ added: v5.10.0
612612
This creates a view of the [`ArrayBuffer`][] without copying the underlying
613613
memory. For example, when passed a reference to the `.buffer` property of a
614614
[`TypedArray`][] instance, the newly created `Buffer` will share the same
615-
allocated memory as the [`TypedArray`][].
615+
allocated memory as the [`TypedArray`][]'s underlying `ArrayBuffer`.
616616

617617
```js
618618
const arr = new Uint16Array(2);
@@ -648,6 +648,21 @@ A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`][] or a
648648
[`SharedArrayBuffer`][] or another type appropriate for `Buffer.from()`
649649
variants.
650650

651+
It is important to remember that a backing `ArrayBuffer` can cover a range
652+
of memory that extends beyond the bounds of a `TypedArray` view. A new
653+
`Buffer` created using the `buffer` property of a `TypedArray` may extend
654+
beyond the range of the `TypedArray`:
655+
656+
```js
657+
const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
658+
const arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
659+
console.log(arrA.buffer === arrB.buffer); // true
660+
661+
const buf = Buffer.from(arrB.buffer);
662+
console.log(buf);
663+
// Prints: <Buffer 63 64 65 66>
664+
```
665+
651666
### Static method: `Buffer.from(buffer)`
652667
<!-- YAML
653668
added: v5.10.0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.