Skip to content

Commit

Permalink
refactor: new QOIEncoderStream() to have better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackAsLight committed Nov 9, 2024
1 parent ced3e42 commit bef304f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
18 changes: 5 additions & 13 deletions src/encoder_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,13 @@ import type { QOIOptions } from "./types.ts";
*
* @module
*/
export class QOIEncoderStream
implements TransformStream<Uint8Array, Uint8Array> {
#width: number;
#height: number;
#isRGB: boolean;
#colorspace: 0 | 1;
#source: ReadableStreamBYOBReader;
#readable: ReadableStream<Uint8Array>;
#writable: WritableStream<Uint8Array>;
export class QOIEncoderStream extends TransformStream<Uint8Array, Uint8Array> {
constructor(options: QOIOptions) {
if (options.width < 0) {
throw new RangeError("Width cannot be a negative number");
if (options.width < 0 || Number.isNaN(options.width)) {
throw new RangeError("Width cannot be a negative number or NaN");
}
if (options.height < 0) {
throw new RangeError("Height cannot be a negative number");
if (options.height < 0 || Number.isNaN(options.height)) {
throw new RangeError("Height cannot be a negative number or NaN");
}
this.#width = options.width;
this.#height = options.height;
Expand Down
4 changes: 2 additions & 2 deletions src/encoder_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Deno.test("QOIEncoderStream() correctly encoding header", async () => {
assertEquals(
(await toBytes(
ReadableStream
.from([new Uint8Array([0, 0, 0, 255, 0, 0, 0, 255])])
.from([new Uint8Array([0, 0]), new Uint8Array([0, 255, 0, 0, 0, 255])])
.pipeThrough(
new QOIEncoderStream({
width: 2,
Expand Down Expand Up @@ -184,7 +184,7 @@ Deno.test("QOIEncoderStream() rejecting due to unexpected number of bytes", asyn
);
},
RangeError,
"Unexpected number of bytes from readable stream",
"Unexpected number of bytes from stream",
);
});

Expand Down

0 comments on commit bef304f

Please sign in to comment.