Skip to content

Commit

Permalink
Merge pull request #19 from theevenstarspace/typed-array-fix
Browse files Browse the repository at this point in the history
allow shared array buffer for reading, block for writing
  • Loading branch information
SergiiSharpov authored Dec 15, 2024
2 parents 6bb6501 + 750cbb6 commit 9bdcbcb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
28 changes: 24 additions & 4 deletions src/byte-stream-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 101 in src/byte-stream-writer.ts

View workflow job for this annotation

GitHub Actions / build

Property '_buffer' will overwrite the base property in 'ByteStream'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.

/**
* The options for buffer resizing.
*/
Expand All @@ -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.
Expand All @@ -119,7 +132,7 @@ export class ByteStreamWriter extends ByteStream {
*/
public constructor(typedArray: TypedArray);

public constructor(target: number | ArrayBuffer | TypedArray, options: Partial<ResizeOptions> = {}) {
public constructor(target: number | ArrayBufferLike | TypedArray, options: Partial<ResizeOptions> = {}) {
const targetOptions = { ...defaultOptions, ...options };

if (typeof target === 'number') {
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
33 changes: 17 additions & 16 deletions src/byte-stream.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
export type TypedArray =
| Int8Array<ArrayBuffer>
| Uint8Array<ArrayBuffer>
| Uint8ClampedArray<ArrayBuffer>
| Int16Array<ArrayBuffer>
| Uint16Array<ArrayBuffer>
| Int32Array<ArrayBuffer>
| Uint32Array<ArrayBuffer>
| Float32Array<ArrayBuffer>
| Float64Array<ArrayBuffer>
| BigInt64Array<ArrayBuffer>
| BigUint64Array<ArrayBuffer>;
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;


/**
* Base class for reading and writing binary data.
Expand All @@ -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.
Expand All @@ -38,17 +39,17 @@ 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.
* @param typedArray - The TypedArray to read/write data from/to
*/
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;
Expand Down Expand Up @@ -99,7 +100,7 @@ export class ByteStream {
/**
* Returns the underlying buffer
*/
public get buffer(): ArrayBuffer {
public get buffer(): ArrayBufferLike {
return this._buffer;
}

Expand Down

0 comments on commit 9bdcbcb

Please sign in to comment.