Skip to content

Commit

Permalink
doc: add SharedArrayBuffer to Buffer documentation
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
ThomasdenH authored and gibfahn committed Dec 19, 2017
1 parent 797e33b commit a0bd1c0
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ differently based on what arguments are provided:
a fast-but-uninitialized `Buffer` versus creating a slower-but-safer `Buffer`.
* Passing a string, array, or `Buffer` as the first argument copies the
passed object's data into the `Buffer`.
* Passing an [`ArrayBuffer`] returns a `Buffer` that shares allocated memory with
the given [`ArrayBuffer`].
* Passing an [`ArrayBuffer`] or a [`SharedArrayBuffer`] returns a `Buffer` that
shares allocated memory with the given array buffer.

Because the behavior of `new Buffer()` changes significantly based on the type
of value passed as the first argument, applications that do not properly
Expand Down Expand Up @@ -350,16 +350,16 @@ deprecated: v6.0.0
> [`Buffer.from(arrayBuffer[, byteOffset [, length]])`][`Buffer.from(arrayBuffer)`]
> instead.
* `arrayBuffer` {ArrayBuffer} An [`ArrayBuffer`] or the `.buffer` property of a
[`TypedArray`].
* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`],
[`SharedArrayBuffer`] or the `.buffer` property of a [`TypedArray`].
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
* `length` {integer} Number of bytes to expose.
**Default:** `arrayBuffer.length - byteOffset`

This creates a view of the [`ArrayBuffer`] without copying the underlying
memory. For example, when passed a reference to the `.buffer` property of a
[`TypedArray`] instance, the newly created `Buffer` will share the same
allocated memory as the [`TypedArray`].
This creates a view of the [`ArrayBuffer`] or [`SharedArrayBuffer`] without
copying the underlying memory. For example, when passed a reference to the
`.buffer` property of a [`TypedArray`] instance, the newly created `Buffer` will
share the same allocated memory as the [`TypedArray`].

The optional `byteOffset` and `length` arguments specify a memory range within
the `arrayBuffer` that will be shared by the `Buffer`.
Expand Down Expand Up @@ -611,8 +611,8 @@ A `TypeError` will be thrown if `size` is not a number.
added: v0.1.90
-->

* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer} A value to
calculate the length of.
* `string` {string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} A
value to calculate the length of.
* `encoding` {string} If `string` is a string, this is its encoding.
**Default:** `'utf8'`
* Returns: {integer} The number of bytes contained within `string`.
Expand All @@ -635,8 +635,8 @@ console.log(`${str}: ${str.length} characters, ` +
`${Buffer.byteLength(str, 'utf8')} bytes`);
```

When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`], the
actual byte length is returned.
When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`]/
[`SharedArrayBuffer`], the actual byte length is returned.

Otherwise, converts to `String` and returns the byte length of string.

Expand Down Expand Up @@ -733,8 +733,8 @@ A `TypeError` will be thrown if `array` is not an `Array`.
added: v5.10.0
-->

* `arrayBuffer` {ArrayBuffer} An [`ArrayBuffer`] or the `.buffer` property of a
[`TypedArray`].
* `arrayBuffer` {ArrayBuffer|SharedArrayBuffer} An [`ArrayBuffer`],
[`SharedArrayBuffer`], or the `.buffer` property of a [`TypedArray`].
* `byteOffset` {integer} Index of first byte to expose. **Default:** `0`
* `length` {integer} Number of bytes to expose.
**Default:** `arrayBuffer.length - byteOffset`
Expand Down Expand Up @@ -778,7 +778,8 @@ const buf = Buffer.from(ab, 0, 2);
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)
<!-- YAML
Expand Down Expand Up @@ -2544,6 +2545,7 @@ console.log(buf);
[RFC1345]: https://tools.ietf.org/html/rfc1345
[RFC4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
[`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#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
Expand Down

0 comments on commit a0bd1c0

Please sign in to comment.