Add some more argument validation

This commit is contained in:
David Payr
2024-12-29 02:21:54 +01:00
parent b7d8c526da
commit cc87dbfcf9
12 changed files with 340 additions and 39 deletions
+14 -6
View File
@@ -15,24 +15,28 @@ export abstract class Source {
}
/** @public */
export class ArrayBufferSource extends Source {
export class BufferSource extends Source {
/** @internal */
_buffer: ArrayBuffer;
_bytes: Uint8Array;
constructor(buffer: ArrayBuffer | Uint8Array) {
if (!(buffer instanceof ArrayBuffer) && !(buffer instanceof Uint8Array)) {
throw new TypeError('buffer must be an ArrayBuffer or Uint8Array.');
}
constructor(buffer: ArrayBuffer) {
super();
this._buffer = buffer;
this._bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
}
/** @internal */
override async _read(start: number, end: number) {
return new Uint8Array(this._buffer, start, end - start);
return this._bytes.subarray(start, end);
}
/** @internal */
override async _retrieveSize() {
return this._buffer.byteLength;
return this._bytes.byteLength;
}
}
@@ -42,6 +46,10 @@ export class BlobSource extends Source {
_blob: Blob;
constructor(blob: Blob) {
if (!(blob instanceof Blob)) {
throw new TypeError('blob must be a Blob.');
}
super();
this._blob = blob;