-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check if SharedArrayBuffer is supported
- Loading branch information
Showing
1 changed file
with
10 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
function view(buffer, shared = false) { | ||
if (buffer instanceof ArrayBuffer) return new Uint8Array(buffer); | ||
if (shared && buffer instanceof SharedArrayBuffer) return new Uint8Array(buffer); | ||
if (typeof SharedArrayBuffer !== 'undefined' && shared && buffer instanceof SharedArrayBuffer) | ||
return new Uint8Array(buffer); | ||
if (ArrayBuffer.isView(buffer)) return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
|
||
throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
} | ||
|
||
function from_parts(buffers, shared = false) { | ||
let length = 0; | ||
let offset = 0; | ||
buffers.forEach(buffer => length += (null == buffer.byteLength ? buffer.length : buffer.byteLength)); | ||
|
||
const u8 = new Uint8Array(shared ? new SharedArrayBuffer(length) : length); | ||
|
||
buffers.forEach(buffer => { | ||
const ref = Array.isArray(buffer) ? buffer : view(buffer, true); | ||
|
||
u8.set(ref, offset); | ||
offset += ref.length; | ||
}); | ||
|
||
return u8; | ||
} | ||
module.exports = { view, from_parts }; | ||
|
||
module.exports = { view, from_parts }; |