From 45707aee7c307bbaef27f26893628325eb0d1f20 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Wed, 31 May 2023 17:30:36 -0400 Subject: [PATCH 1/3] 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: https://github.com/nodejs/node/issues/28725 --- doc/api/buffer.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 981ef7aa2d4071..02580dcfe0a17a 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1118,6 +1118,12 @@ const { Buffer } = require('node:buffer'); const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); ``` +Note that if `array` is an `Array`-like object (that is, one with a `length` +property of type `number`) it is treated as if it is an array, unless it is a +`Buffer` or a `Uint8Array`. This means all other `TypedArray` variants get +treated as `Array`. To create a `Buffer` from the underlying `ArrayBuffer`, use +the `.buffer` property directly. + A `TypeError` will be thrown if `array` is not an `Array` or another type appropriate for `Buffer.from()` variants. From 48769eca32dda58a3cecddcf1160246d4a671a51 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Wed, 31 May 2023 21:22:32 -0400 Subject: [PATCH 2/3] fixup! doc: clarify array args to Buffer.from() --- doc/api/buffer.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 02580dcfe0a17a..08b33f3486520f 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1118,11 +1118,11 @@ const { Buffer } = require('node:buffer'); const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); ``` -Note that if `array` is an `Array`-like object (that is, one with a `length` -property of type `number`) it is treated as if it is an array, unless it is a -`Buffer` or a `Uint8Array`. This means all other `TypedArray` variants get -treated as `Array`. To create a `Buffer` from the underlying `ArrayBuffer`, use -the `.buffer` property directly. +If `array` is an `Array`-like object (that is, one with a `length` property of +type `number`), it is treated as if it is an array, unless it is a `Buffer` or a +`Uint8Array`. This means all other `TypedArray` variants get treated as an +`Array`. To create a `Buffer` from the underlying `ArrayBuffer`, use the +`.buffer` property directly. A `TypeError` will be thrown if `array` is not an `Array` or another type appropriate for `Buffer.from()` variants. From 2a6c6e02484116afb0a94d1dcc14cf82b908d4d3 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Thu, 1 Jun 2023 09:06:08 -0400 Subject: [PATCH 3/3] fixup! fixup! doc: clarify array args to Buffer.from() --- doc/api/buffer.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 08b33f3486520f..3a46599649d025 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1119,10 +1119,10 @@ const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); ``` If `array` is an `Array`-like object (that is, one with a `length` property of -type `number`), it is treated as if it is an array, unless it is a `Buffer` or a -`Uint8Array`. This means all other `TypedArray` variants get treated as an -`Array`. To create a `Buffer` from the underlying `ArrayBuffer`, use the -`.buffer` property directly. +type `number`), it is treated as if it is an array, unless it is a `Buffer` or +a `Uint8Array`. This means all other `TypedArray` variants get treated as an +`Array`. To create a `Buffer` from the bytes backing a `TypedArray`, use +[`Buffer.copyBytesFrom()`][]. A `TypeError` will be thrown if `array` is not an `Array` or another type appropriate for `Buffer.from()` variants. @@ -5496,6 +5496,7 @@ introducing security vulnerabilities into an application. [`Buffer.allocUnsafe()`]: #static-method-bufferallocunsafesize [`Buffer.allocUnsafeSlow()`]: #static-method-bufferallocunsafeslowsize [`Buffer.concat()`]: #static-method-bufferconcatlist-totallength +[`Buffer.copyBytesFrom()`]: #static-method-buffercopybytesfromview-offset-length [`Buffer.from(array)`]: #static-method-bufferfromarray [`Buffer.from(arrayBuf)`]: #static-method-bufferfromarraybuffer-byteoffset-length [`Buffer.from(buffer)`]: #static-method-bufferfrombuffer