diff --git a/README.md b/README.md index 542d7ef..475cece 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,79 @@ [![Test](https://github.com/lambdalisue/deno-workerio/actions/workflows/test.yml/badge.svg)](https://github.com/lambdalisue/deno-workerio/actions/workflows/test.yml) [Deno][deno] module to translate Worker's system of messages into -[Reader][reader] and [Writer][writer]. - -This module supports Deno v1.28.0 or later. +[`ReadableStream`][readablestream] and +[`WritableStream`][writablestream] or [`Deno.Reader`][reader] and +[`Deno.Writer`][writer]. [deno]: https://deno.land/ [reader]: https://doc.deno.land/builtin/stable#Deno.Reader [writer]: https://doc.deno.land/builtin/stable#Deno.Writer +[ReadableStream]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream +[WritableStream]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream ## Example -### Server +### ReadableStream/WritableStream + +#### Server + +```typescript +import { + readableStreamFromWorker, + writableStreamFromWorker, +} from "https://deno.land/x/workerio/mod.ts"; + +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); + +const worker = new Worker(new URL("./worker.ts", import.meta.url).href, { + type: "module", +}); + +const reader = readableStreamFromWorker(worker); +const writer = writableStreamFromWorker(worker); +const w = writer.getWriter(); + +await w.write(encoder.encode("Hello")); +await w.write(encoder.encode("World")); +w.releaseLock(); + +for await (const data of reader) { + const text = decoder.decode(data); + console.log(text); +} +``` + +#### Worker + +```typescript +import { + readableStreamFromWorker, + writableStreamFromWorker, +} from "https://deno.land/x/workerio/mod.ts"; + +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); + +async function main(): Promise { + const worker = self as unknown as Worker; + const reader = readableStreamFromWorker(worker); + const writer = writableStreamFromWorker(worker); + const w = writer.getWriter(); + + for await (const data of reader) { + const text = decoder.decode(data); + await w.write(encoder.encode(`!!! ${text} !!!`)); + } + w.releaseLock(); +} + +main().catch((e) => console.error(e)); +``` + +### Deno.Reader/Deno.Writer + +#### Server ```typescript import { @@ -42,7 +104,7 @@ for await (const data of Deno.iter(reader)) { } ``` -### Worker +#### Worker ```typescript import { @@ -54,8 +116,7 @@ const decoder = new TextDecoder(); const encoder = new TextEncoder(); async function main(): Promise { - // deno-lint-ignore no-explicit-any - const worker = self as any; + const worker = self as unknown as Worker; const reader = new WorkerReader(worker); const writer = new WorkerWriter(worker); @@ -68,52 +129,6 @@ async function main(): Promise { main().catch((e) => console.error(e)); ``` -## Benchmark - -You can run benchmark of `WorkerReader` and `WorkerWriter` with the following -command: - -``` -$ deno run --no-check --allow-read --allow-net ./benchmark/benchmark.ts -=========================================================== -Transfer: 1 MiB -N: 5 times -=========================================================== -Relaxing 1 sec ... -Start benchmark -1 Reader: 9 [ms] Writer: 0 [ms] -2 Reader: 7 [ms] Writer: 0 [ms] -3 Reader: 6 [ms] Writer: 1 [ms] -4 Reader: 5 [ms] Writer: 1 [ms] -5 Reader: 6 [ms] Writer: 0 [ms] -=========================================================== -Reader: Avg. 6.6000 msec (1271.0 Mbps) -Writer: Avg. 0.40000 msec (20972 Mbps) -=========================================================== -``` - -Use `-n` to change the number of tries and `-size` to the size of the buffer (in -MB) like: - -``` -$ deno run --no-check --allow-read --allow-net ./benchmark/benchmark.ts -n 3 --size 8 -=========================================================== -Transfer: 8 MiB -N: 3 times -=========================================================== -Relaxing 1 sec ... -Start benchmark -1 Reader: 53 [ms] Writer: 7 [ms] -2 Reader: 43 [ms] Writer: 1 [ms] -3 Reader: 40 [ms] Writer: 1 [ms] -=========================================================== -Reader: Avg. 45.333 msec (1480.3 Mbps) -Writer: Avg. 3.0000 msec (22370 Mbps) -=========================================================== -``` - -See [Benchmark](./wiki/Benchmark) for various benchmarks. - ## License The code follows MIT license written in [LICENSE](./LICENSE). Contributors need diff --git a/example/server.ts b/example/server.ts index 40c76bd..b761cef 100644 --- a/example/server.ts +++ b/example/server.ts @@ -1,4 +1,4 @@ -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; +import * as streams from "https://deno.land/std@0.186.0/streams/mod.ts"; import { WorkerReader, WorkerWriter } from "../mod.ts"; const decoder = new TextDecoder(); diff --git a/example/worker.ts b/example/worker.ts index a9f9c16..cf587a8 100644 --- a/example/worker.ts +++ b/example/worker.ts @@ -1,4 +1,4 @@ -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; +import * as streams from "https://deno.land/std@0.186.0/streams/mod.ts"; import { WorkerReader, WorkerWriter } from "../mod.ts"; const decoder = new TextDecoder(); diff --git a/readable_stream.ts b/readable_stream.ts index cae2d9b..8e74e9a 100644 --- a/readable_stream.ts +++ b/readable_stream.ts @@ -1,40 +1,18 @@ -const DEFAULT_CHUNK_SIZE = 16 * 1024; - /** * Options for creating a readable stream from a worker. */ -export interface ReadableStreamFromWorkerOptions { - /** - * The size of chunks to allocate to read. The default is ~16KiB, which is - * the maximum size that Deno operations can currently support. - */ - chunkSize?: number; - +export type ReadableStreamFromWorkerOptions = { /** * The queuing strategy to create the `ReadableStream` with. */ - strategy?: { - /** - * A number representing the total number of bytes that can be stored in the - * stream's internal queue before backpressure is applied. - * - * If not specified, it defaults to 1. - */ - highWaterMark?: number | undefined; - /** - * This value should always be left undefined as the stream's underlying - * source is the worker. - */ - size?: undefined; - }; -} + strategy?: QueuingStrategy; +}; /** * Creates a readable stream that reads data from a worker's `postMessage` event. * * @param worker The worker to read data from. - * @param options The options to configure the behavior of the stream. Defaults to - * 16 KiB chunk size and a queuing strategy with highWaterMark of 1 and undefined size. + * @param options Options for creating the readable stream. * @returns A readable stream that can be used to read the data. */ export function readableStreamFromWorker( @@ -42,33 +20,23 @@ export function readableStreamFromWorker( options: ReadableStreamFromWorkerOptions = {}, ): ReadableStream { const { - chunkSize = DEFAULT_CHUNK_SIZE, - strategy, + strategy = new ByteLengthQueuingStrategy({ highWaterMark: 1024 }), } = options; - let onmessage: (e: MessageEvent) => void; return new ReadableStream({ start(controller) { - onmessage = (ev) => { + worker.onmessage = (ev) => { if (ev.data === null) { - worker.removeEventListener("message", onmessage); controller.close(); - return; + worker.onmessage = undefined; } else if (ev.data instanceof Uint8Array) { - const data = ev.data; - let offset = 0; - while (offset < data.length) { - const end = offset + chunkSize; - controller.enqueue(data.subarray(offset, end)); - offset = end; - } + controller.enqueue(ev.data); } else { throw new Error("Unexpected data posted"); } }; - worker.addEventListener("message", onmessage); }, cancel() { - worker.removeEventListener("message", onmessage); + worker.onmessage = undefined; }, }, strategy); } diff --git a/readable_stream_bench.ts b/readable_stream_bench.ts index 54789ed..fd4c9df 100644 --- a/readable_stream_bench.ts +++ b/readable_stream_bench.ts @@ -1,6 +1,4 @@ -import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; +import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts"; import { readableStreamFromWorker, WorkerReader } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; @@ -14,8 +12,6 @@ const sizes = [ ]; for (const size of sizes) { - const data = new Uint8Array(size); - Deno.bench( `readableStreamFromWorker (${ size.toString().padStart(2) @@ -28,15 +24,15 @@ for (const size of sizes) { const worker = new MockWorker(); const rstream = readableStreamFromWorker(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); worker.postMessage(data); } worker.postMessage(null); - const chunks = []; + let total = 0; for await (const chunk of rstream) { - chunks.push(chunk); + total += chunk.length; } - const content = concat(...chunks); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); @@ -49,11 +45,18 @@ for (const size of sizes) { const worker = new MockWorker(); const reader = new WorkerReader(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); worker.postMessage(data); } worker.postMessage(null); - const content = await streams.readAll(reader); - assertEquals(content.length, size * count); + let total = 0; + while (true) { + const p = new Uint8Array(1024); + const n = await reader.read(p); + if (n === null) break; + total += n; + } + assertEquals(total, size * count); }, ); } diff --git a/readable_stream_test.ts b/readable_stream_test.ts index bea854d..ef32965 100644 --- a/readable_stream_test.ts +++ b/readable_stream_test.ts @@ -1,5 +1,5 @@ -import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; +import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { concat } from "https://deno.land/std@0.186.0/bytes/mod.ts"; import { readableStreamFromWorker } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; diff --git a/reader_bench.ts b/reader_bench.ts index 435fd58..0e10001 100644 --- a/reader_bench.ts +++ b/reader_bench.ts @@ -1,7 +1,6 @@ -import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; -import { WorkerReader as WorkerReaderV2 } from "https://deno.land/x/workerio@v2.0.1/mod.ts"; -import { WorkerReader as WorkerReaderV1 } from "https://deno.land/x/workerio@v1.4.4/mod.ts"; +import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { WorkerReader as WorkerReaderV2 } from "https://deno.land/x/workerio@v3.0.1/mod.ts"; +import { WorkerReader as WorkerReaderV1 } from "https://deno.land/x/workerio@v3.0.1/mod.ts"; import { WorkerReader } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; @@ -15,8 +14,6 @@ const sizes = [ ]; for (const size of sizes) { - const data = new Uint8Array(size); - Deno.bench(`WorkerReader (${size.toString().padStart(2)} Bytes x ${count})`, { group: size.toString(), baseline: true, @@ -24,11 +21,18 @@ for (const size of sizes) { const worker = new MockWorker(); const reader = new WorkerReader(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); worker.postMessage(data); } reader.close(); - const content = await streams.readAll(reader); - assertEquals(content.length, size * count); + let total = 0; + while (true) { + const p = new Uint8Array(1024); + const n = await reader.read(p); + if (n === null) break; + total += n; + } + assertEquals(total, size * count); }); Deno.bench( @@ -40,11 +44,18 @@ for (const size of sizes) { const worker = new MockWorker(); const reader = new WorkerReaderV2(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); worker.postMessage(data); } reader.close(); - const content = await streams.readAll(reader); - assertEquals(content.length, size * count); + let total = 0; + while (true) { + const p = new Uint8Array(1024); + const n = await reader.read(p); + if (n === null) break; + total += n; + } + assertEquals(total, size * count); }, ); @@ -57,11 +68,18 @@ for (const size of sizes) { const worker = new MockWorker(); const reader = new WorkerReaderV1(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); worker.postMessage(data); } reader.close(); - const content = await streams.readAll(reader); - assertEquals(content.length, size * count); + let total = 0; + while (true) { + const p = new Uint8Array(1024); + const n = await reader.read(p); + if (n === null) break; + total += n; + } + assertEquals(total, size * count); }, ); } diff --git a/reader_test.ts b/reader_test.ts index 60a0be6..c101abf 100644 --- a/reader_test.ts +++ b/reader_test.ts @@ -1,6 +1,6 @@ -import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { delay } from "https://deno.land/std@0.185.0/async/mod.ts"; -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; +import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { delay } from "https://deno.land/std@0.186.0/async/mod.ts"; +import * as streams from "https://deno.land/std@0.186.0/streams/mod.ts"; import { WorkerReader } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; diff --git a/test_bench.ts b/test_bench.ts index 8e115e2..d42947d 100644 --- a/test_bench.ts +++ b/test_bench.ts @@ -1,23 +1,12 @@ -import { assertEquals } from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; -import * as streams from "https://deno.land/std@0.185.0/streams/mod.ts"; -import { - WorkerReader as WorkerReaderV2, - WorkerWriter as WorkerWriterV2, -} from "https://deno.land/x/workerio@v2.0.1/mod.ts"; -import { - WorkerReader as WorkerReaderV1, - WorkerWriter as WorkerWriterV1, -} from "https://deno.land/x/workerio@v1.4.4/mod.ts"; +import { assertEquals } from "https://deno.land/std@0.186.0/testing/asserts.ts"; import { readableStreamFromWorker, WorkerReader, WorkerWriter, writableStreamFromWorker, } from "./mod.ts"; -import { MockWorker } from "./test_util.ts"; -const count = 100; +const count = 1000; const sizes = [ 64, 128, @@ -26,9 +15,12 @@ const sizes = [ 1024, ]; -for (const size of sizes) { - const data = new Uint8Array(size); +const worker = new Worker( + new URL("./test_echo_server.ts", import.meta.url).href, + { type: "module" }, +); +for (const size of sizes) { Deno.bench( `Streams API (${size.toString().padStart(2)} Bytes x ${count})`, { @@ -36,31 +28,31 @@ for (const size of sizes) { baseline: true, }, async () => { - const worker = new MockWorker(); const rstream = readableStreamFromWorker(worker); const wstream = writableStreamFromWorker(worker); const producer = async () => { const writer = wstream.getWriter(); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } writer.close(); }; const consumer = async () => { - const chunks: Uint8Array[] = []; + let total = 0; for await (const chunk of rstream) { - chunks.push(chunk); + total += chunk.length; } - return concat(...chunks); + return total; }; - const [_, content] = await Promise.all([ + const [_, total] = await Promise.all([ producer(), consumer(), ]); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); @@ -70,84 +62,33 @@ for (const size of sizes) { group: size.toString(), }, async () => { - const worker = new MockWorker(); const reader = new WorkerReader(worker); const writer = new WorkerWriter(worker); const producer = async () => { for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } writer.close(); }; - const consumer = () => { - return streams.readAll(reader); - }; - - const [_, content] = await Promise.all([ - producer(), - consumer(), - ]); - assertEquals(content.length, size * count); - }, - ); - - Deno.bench( - `Reader/Writer API v2 (${size.toString().padStart(2)} Bytes x ${count})`, - { - group: size.toString(), - }, - async () => { - const worker = new MockWorker(); - const reader = new WorkerReaderV2(worker); - const writer = new WorkerWriterV2(worker); - - const producer = async () => { - for (let i = 0; i < count; i++) { - await writer.write(data); - } - reader.close(); - }; - - const consumer = () => { - return streams.readAll(reader); - }; - - const [_, content] = await Promise.all([ - producer(), - consumer(), - ]); - assertEquals(content.length, size * count); - }, - ); - - Deno.bench( - `Reader/Writer API v1 (${size.toString().padStart(2)} Bytes x ${count})`, - { - group: size.toString(), - }, - async () => { - const worker = new MockWorker(); - const reader = new WorkerReaderV1(worker); - const writer = new WorkerWriterV1(worker); - - const producer = async () => { - for (let i = 0; i < count; i++) { - await writer.write(data); + const consumer = async () => { + let total = 0; + while (true) { + const p = new Uint8Array(1024); + const n = await reader.read(p); + if (n === null) break; + total += n; } - reader.close(); - }; - - const consumer = () => { - return streams.readAll(reader); + return total; }; - const [_, content] = await Promise.all([ + const [_, total] = await Promise.all([ producer(), consumer(), ]); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); } diff --git a/test_echo_server.ts b/test_echo_server.ts new file mode 100644 index 0000000..5e411f2 --- /dev/null +++ b/test_echo_server.ts @@ -0,0 +1,4 @@ +const worker = self as unknown as Worker; +worker.onmessage = (ev) => { + worker.postMessage(ev.data); +}; diff --git a/writable_stream.ts b/writable_stream.ts index d768f14..b7031c2 100644 --- a/writable_stream.ts +++ b/writable_stream.ts @@ -1,3 +1,13 @@ +/** + * Options for creating a writable stream from a worker. + */ +export type WritableStreamFromWorkerOptions = { + /** + * The queuing strategy to create the `WritableStream` with. + */ + strategy?: QueuingStrategy; +}; + /** * Creates a `WritableStream` that writes data to a `Worker` through `Worker.postMessage`. * @@ -6,7 +16,11 @@ */ export function writableStreamFromWorker( worker: Worker, + options: WritableStreamFromWorkerOptions = {}, ): WritableStream { + const { + strategy = new ByteLengthQueuingStrategy({ highWaterMark: 1024 }), + } = options; return new WritableStream({ /** * Writes a chunk of data to the `Worker` through `Worker.postMessage`. @@ -15,7 +29,7 @@ export function writableStreamFromWorker( */ write(chunk, controller) { try { - worker.postMessage(chunk); + worker.postMessage(chunk, [chunk.buffer]); } catch (e) { controller.error(e); } @@ -26,5 +40,5 @@ export function writableStreamFromWorker( close() { worker.postMessage(null); }, - }); + }, strategy); } diff --git a/writable_stream_bench.ts b/writable_stream_bench.ts index d77f66f..459b9ca 100644 --- a/writable_stream_bench.ts +++ b/writable_stream_bench.ts @@ -1,8 +1,7 @@ import { assertEquals, assertInstanceOf, -} from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; +} from "https://deno.land/std@0.186.0/testing/asserts.ts"; import { WorkerWriter, writableStreamFromWorker } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; @@ -16,8 +15,6 @@ const sizes = [ ]; for (const size of sizes) { - const data = new Uint8Array(size); - Deno.bench( `writableStreamFromWorker (${ size.toString().padStart(2) @@ -28,18 +25,19 @@ for (const size of sizes) { }, async () => { const worker = new MockWorker(); - const chunks: Uint8Array[] = []; + let total = 0; worker.addEventListener("message", (ev) => { assertInstanceOf(ev, MessageEvent); - chunks.push(ev.data); + total += ev.data.length; }); const wstream = writableStreamFromWorker(worker); const writer = wstream.getWriter(); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } - const content = concat(...chunks); - assertEquals(content.length, size * count); + writer.releaseLock(); + assertEquals(total, size * count); }, ); @@ -50,17 +48,17 @@ for (const size of sizes) { }, async () => { const worker = new MockWorker(); - const chunks: Uint8Array[] = []; + let total = 0; worker.addEventListener("message", (ev) => { assertInstanceOf(ev, MessageEvent); - chunks.push(ev.data); + total += ev.data.length; }); const writer = new WorkerWriter(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } - const content = concat(...chunks); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); } diff --git a/writable_stream_test.ts b/writable_stream_test.ts index faf5fd8..7520707 100644 --- a/writable_stream_test.ts +++ b/writable_stream_test.ts @@ -1,8 +1,8 @@ import { assertEquals, assertInstanceOf, -} from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; +} from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { concat } from "https://deno.land/std@0.186.0/bytes/mod.ts"; import { writableStreamFromWorker } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; diff --git a/writer.ts b/writer.ts index 7dae943..b886b28 100644 --- a/writer.ts +++ b/writer.ts @@ -26,7 +26,7 @@ export class WorkerWriter implements Deno.Writer, Deno.Closer { * @returns A promise resolving to the number of bytes written. */ write(p: Uint8Array): Promise { - this.#worker.postMessage(p); + this.#worker.postMessage(p, [p.buffer]); return Promise.resolve(p.length); } diff --git a/writer_bench.ts b/writer_bench.ts index 3e31b45..8b4c4dc 100644 --- a/writer_bench.ts +++ b/writer_bench.ts @@ -1,10 +1,9 @@ import { assertEquals, assertInstanceOf, -} from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; -import { WorkerWriter as WorkerWriterV2 } from "https://deno.land/x/workerio@v2.0.1/mod.ts"; -import { WorkerWriter as WorkerWriterV1 } from "https://deno.land/x/workerio@v1.4.4/mod.ts"; +} from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { WorkerWriter as WorkerWriterV2 } from "https://deno.land/x/workerio@v3.0.1/mod.ts"; +import { WorkerWriter as WorkerWriterV1 } from "https://deno.land/x/workerio@v3.0.1/mod.ts"; import { WorkerWriter } from "./mod.ts"; import { MockWorker } from "./test_util.ts"; @@ -18,8 +17,6 @@ const sizes = [ ]; for (const size of sizes) { - const data = new Uint8Array(size); - Deno.bench( `WorkerWriter (${size.toString().padStart(2)} Bytes x ${count})`, { @@ -28,17 +25,17 @@ for (const size of sizes) { }, async () => { const worker = new MockWorker(); - const chunks: Uint8Array[] = []; + let total = 0; worker.addEventListener("message", (ev) => { assertInstanceOf(ev, MessageEvent); - chunks.push(ev.data); + total += ev.data.length; }); const writer = new WorkerWriter(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } - const content = concat(...chunks); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); @@ -49,17 +46,17 @@ for (const size of sizes) { }, async () => { const worker = new MockWorker(); - const chunks: Uint8Array[] = []; + let total = 0; worker.addEventListener("message", (ev) => { assertInstanceOf(ev, MessageEvent); - chunks.push(ev.data); + total += ev.data.length; }); const writer = new WorkerWriterV2(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } - const content = concat(...chunks); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); @@ -70,17 +67,17 @@ for (const size of sizes) { }, async () => { const worker = new MockWorker(); - const chunks: Uint8Array[] = []; + let total = 0; worker.addEventListener("message", (ev) => { assertInstanceOf(ev, MessageEvent); - chunks.push(ev.data); + total += ev.data.length; }); const writer = new WorkerWriterV1(worker); for (let i = 0; i < count; i++) { + const data = new Uint8Array(size); await writer.write(data); } - const content = concat(...chunks); - assertEquals(content.length, size * count); + assertEquals(total, size * count); }, ); } diff --git a/writer_test.ts b/writer_test.ts index a7ac570..8593a14 100644 --- a/writer_test.ts +++ b/writer_test.ts @@ -1,8 +1,8 @@ import { assertEquals, assertInstanceOf, -} from "https://deno.land/std@0.185.0/testing/asserts.ts"; -import { concat } from "https://deno.land/std@0.185.0/bytes/mod.ts"; +} from "https://deno.land/std@0.186.0/testing/asserts.ts"; +import { concat } from "https://deno.land/std@0.186.0/bytes/mod.ts"; import { WorkerWriter } from "./mod.ts"; import { MockWorker } from "./test_util.ts";