From 750cbb6457fe088af380f4f593991d00ad502eef Mon Sep 17 00:00:00 2001 From: Sergey Sharpov Date: Sun, 15 Dec 2024 18:13:43 +0200 Subject: [PATCH] allow shared array buffer for reading, block for writing --- src/byte-stream-writer.ts | 28 ++++++++++++++++++++++++---- src/byte-stream.ts | 33 +++++++++++++++++---------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/byte-stream-writer.ts b/src/byte-stream-writer.ts index 223998d..414871c 100644 --- a/src/byte-stream-writer.ts +++ b/src/byte-stream-writer.ts @@ -82,11 +82,24 @@ const defaultOptions: ResizeOptions = { increment: 256 }; +const validateBuffer = (buffer: ArrayBufferLike): ArrayBuffer => { + if (SharedArrayBuffer && buffer instanceof SharedArrayBuffer) { + throw new TypeError('SharedArrayBuffer writing is not supported'); + } + + return buffer as ArrayBuffer; +}; + /** * A class that provides methods for writing binary data to a buffer. * @group Encoding */ export class ByteStreamWriter extends ByteStream { + /** + * The underlying buffer. + */ + protected override _buffer: ArrayBuffer; + /** * The options for buffer resizing. */ @@ -110,7 +123,7 @@ export class ByteStreamWriter extends ByteStream { * @param buffer - The buffer to write to * @throws {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError | RangeError} if the initial buffer size is invalid or the maxByteLength is less than the initial buffer size */ - public constructor(buffer: ArrayBuffer); + public constructor(buffer: ArrayBufferLike); /** * Creates a new buffer writer. @@ -119,7 +132,7 @@ export class ByteStreamWriter extends ByteStream { */ public constructor(typedArray: TypedArray); - public constructor(target: number | ArrayBuffer | TypedArray, options: Partial = {}) { + public constructor(target: number | ArrayBufferLike | TypedArray, options: Partial = {}) { const targetOptions = { ...defaultOptions, ...options }; if (typeof target === 'number') { @@ -133,9 +146,9 @@ export class ByteStreamWriter extends ByteStream { super(buffer); } else if (ArrayBuffer.isView(target)) { - super(target as TypedArray); + super(validateBuffer(target.buffer)); } else { - super(target as ArrayBuffer); + super(validateBuffer(target)); } this._options = targetOptions; @@ -164,6 +177,13 @@ export class ByteStreamWriter extends ByteStream { } }; + /** + * Returns the underlying buffer + */ + public override get buffer(): ArrayBuffer { + return this._buffer; + } + /** * The current capacity of the buffer. */ diff --git a/src/byte-stream.ts b/src/byte-stream.ts index 463f223..d8aa094 100644 --- a/src/byte-stream.ts +++ b/src/byte-stream.ts @@ -1,15 +1,16 @@ export type TypedArray = - | Int8Array - | Uint8Array - | Uint8ClampedArray - | Int16Array - | Uint16Array - | Int32Array - | Uint32Array - | Float32Array - | Float64Array - | BigInt64Array - | BigUint64Array; + | Int8Array + | Uint8Array + | Uint8ClampedArray + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Float32Array + | Float64Array + | BigInt64Array + | BigUint64Array; + /** * Base class for reading and writing binary data. @@ -19,7 +20,7 @@ export class ByteStream { /** * The underlying buffer. */ - protected _buffer: ArrayBuffer; + protected _buffer: ArrayBufferLike; /** * The DataView instance to read data from the buffer. @@ -38,9 +39,9 @@ export class ByteStream { /** * Creates a new buffer view. - * @param buffer - The ArrayBuffer to read/write data from/to + * @param buffer - The ArrayBuffer/SharedArrayBuffer to read/write data from/to */ - public constructor(buffer: ArrayBuffer); + public constructor(buffer: ArrayBufferLike); /** * Creates a new buffer view. @@ -48,7 +49,7 @@ export class ByteStream { */ public constructor(typedArray: TypedArray); - public constructor(target: ArrayBuffer | TypedArray) { + public constructor(target: ArrayBufferLike | TypedArray) { if (ArrayBuffer.isView(target)) { this._buffer = target.buffer; this._offset = target.byteOffset; @@ -99,7 +100,7 @@ export class ByteStream { /** * Returns the underlying buffer */ - public get buffer(): ArrayBuffer { + public get buffer(): ArrayBufferLike { return this._buffer; }