doc: add SharedArrayBuffer to Buffer documentation
PR-URL: https://github.com/nodejs/node/pull/15489 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
b08c7321bd
commit
bd0e36dbc6
@ -66,8 +66,8 @@ differently based on what arguments are provided:
|
|||||||
memory.
|
memory.
|
||||||
* Passing a string, array, or `Buffer` as the first argument copies the
|
* Passing a string, array, or `Buffer` as the first argument copies the
|
||||||
passed object's data into the `Buffer`.
|
passed object's data into the `Buffer`.
|
||||||
* Passing an [`ArrayBuffer`] returns a `Buffer` that shares allocated memory with
|
* Passing an [`ArrayBuffer`] or a [`SharedArrayBuffer`] returns a `Buffer` that
|
||||||
the given [`ArrayBuffer`].
|
shares allocated memory with the given array buffer.
|
||||||
|
|
||||||
Because the behavior of `new Buffer()` changes significantly based on the type
|
Because the behavior of `new Buffer()` changes significantly based on the type
|
||||||
of value passed as the first argument, applications that do not properly
|
of value passed as the first argument, applications that do not properly
|
||||||
@ -361,16 +361,16 @@ changes:
|
|||||||
> [`Buffer.from(arrayBuffer[, byteOffset [, length]])`][`Buffer.from(arrayBuffer)`]
|
> [`Buffer.from(arrayBuffer[, byteOffset [, length]])`][`Buffer.from(arrayBuffer)`]
|
||||||
> instead.
|
> instead.
|
||||||
|
|
||||||
* `arrayBuffer` {ArrayBuffer} An [`ArrayBuffer`] or the `.buffer` property of a
|
* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`],
|
||||||
[`TypedArray`].
|
[`SharedArrayBuffer`] or the `.buffer` property of a [`TypedArray`].
|
||||||
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
|
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
|
||||||
* `length` {integer} Number of bytes to expose.
|
* `length` {integer} Number of bytes to expose.
|
||||||
**Default:** `arrayBuffer.length - byteOffset`
|
**Default:** `arrayBuffer.length - byteOffset`
|
||||||
|
|
||||||
This creates a view of the [`ArrayBuffer`] without copying the underlying
|
This creates a view of the [`ArrayBuffer`] or [`SharedArrayBuffer`] without
|
||||||
memory. For example, when passed a reference to the `.buffer` property of a
|
copying the underlying memory. For example, when passed a reference to the
|
||||||
[`TypedArray`] instance, the newly created `Buffer` will share the same
|
`.buffer` property of a [`TypedArray`] instance, the newly created `Buffer` will
|
||||||
allocated memory as the [`TypedArray`].
|
share the same allocated memory as the [`TypedArray`].
|
||||||
|
|
||||||
The optional `byteOffset` and `length` arguments specify a memory range within
|
The optional `byteOffset` and `length` arguments specify a memory range within
|
||||||
the `arrayBuffer` that will be shared by the `Buffer`.
|
the `arrayBuffer` that will be shared by the `Buffer`.
|
||||||
@ -679,8 +679,8 @@ changes:
|
|||||||
or `ArrayBuffer`.
|
or `ArrayBuffer`.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer} A value to
|
* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A
|
||||||
calculate the length of.
|
value to calculate the length of.
|
||||||
* `encoding` {string} If `string` is a string, this is its encoding.
|
* `encoding` {string} If `string` is a string, this is its encoding.
|
||||||
**Default:** `'utf8'`
|
**Default:** `'utf8'`
|
||||||
* Returns: {integer} The number of bytes contained within `string`.
|
* Returns: {integer} The number of bytes contained within `string`.
|
||||||
@ -703,8 +703,8 @@ console.log(`${str}: ${str.length} characters, ` +
|
|||||||
`${Buffer.byteLength(str, 'utf8')} bytes`);
|
`${Buffer.byteLength(str, 'utf8')} bytes`);
|
||||||
```
|
```
|
||||||
|
|
||||||
When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`], the
|
When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`]/
|
||||||
actual byte length is returned.
|
[`SharedArrayBuffer`], the actual byte length is returned.
|
||||||
|
|
||||||
### Class Method: Buffer.compare(buf1, buf2)
|
### Class Method: Buffer.compare(buf1, buf2)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
@ -807,8 +807,8 @@ A `TypeError` will be thrown if `array` is not an `Array`.
|
|||||||
added: v5.10.0
|
added: v5.10.0
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `arrayBuffer` {ArrayBuffer} An [`ArrayBuffer`] or the `.buffer` property of a
|
* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`],
|
||||||
[`TypedArray`].
|
[`SharedArrayBuffer`], or the `.buffer` property of a [`TypedArray`].
|
||||||
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
|
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
|
||||||
* `length` {integer} Number of bytes to expose.
|
* `length` {integer} Number of bytes to expose.
|
||||||
**Default:** `arrayBuffer.length - byteOffset`
|
**Default:** `arrayBuffer.length - byteOffset`
|
||||||
@ -852,7 +852,8 @@ const buf = Buffer.from(ab, 0, 2);
|
|||||||
console.log(buf.length);
|
console.log(buf.length);
|
||||||
```
|
```
|
||||||
|
|
||||||
A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`].
|
A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`] or a
|
||||||
|
[`SharedArrayBuffer`].
|
||||||
|
|
||||||
### Class Method: Buffer.from(buffer)
|
### Class Method: Buffer.from(buffer)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
@ -2712,6 +2713,7 @@ This value may depend on the JS engine that is being used.
|
|||||||
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
||||||
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
|
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
|
||||||
[`RangeError`]: errors.html#errors_class_rangeerror
|
[`RangeError`]: errors.html#errors_class_rangeerror
|
||||||
|
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
|
||||||
[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
|
[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
|
||||||
[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
|
[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
|
||||||
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
|
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
|
||||||
|
Loading…
x
Reference in New Issue
Block a user