Skip to content

Commit

Permalink
fix: check if SharedArrayBuffer is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
matmen committed Nov 23, 2023
1 parent 9dcc205 commit 8c3b682
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions utils/buffer.js
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 };

0 comments on commit 8c3b682

Please sign in to comment.