diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c428c03..8748e1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,7 +44,7 @@ jobs: deno-version: ${{ env.DENO_VERSION }} - name: Test run: | - deno test --unstable --allow-read --allow-net + deno test --no-check --unstable --allow-read --allow-net timeout-minutes: 5 typecheck: diff --git a/README.md b/README.md index 464cd97..b05e6c2 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,13 @@ [Deno][deno] module to translate Worker's system of messages into [Reader][reader] and [Writer][writer]. -Note that `WorkerReader` and `WorkerWriter` access `Deno` namespace thus -[Using Deno in worker](https://deno.land/manual@v1.7.5/runtime/workers#using-deno-in-worker) +Note that this package requires +[`Worker.postMessage` supports structured clone +algorithm](https://deno.com/blog/v1.10#worker.postmessage-supports-structured-clone-algorithm) +introduced in Deno v1.10. + +Note that this package accesses `Deno` namespace thus +[Using Deno in worker](https://deno.land/manual@v1.11.0/runtime/workers#using-deno-in-worker) must be enabled. [deno]: https://deno.land/ diff --git a/reader.ts b/reader.ts index 8cf4041..507fc1d 100644 --- a/reader.ts +++ b/reader.ts @@ -16,7 +16,7 @@ export class WorkerReader implements Deno.Reader, Deno.Closer { this.#worker = worker; this.#worker.onmessage = (e) => { if (this.#queue && !this.#closed) { - this.#queue.put_nowait(new Uint8Array(e.data)); + this.#queue.put_nowait(e.data); } }; } @@ -45,8 +45,8 @@ export class WorkerReader implements Deno.Reader, Deno.Closer { private readFromRemain(p: Uint8Array): number { const n = p.byteLength; - const d = this.#remain.slice(0, n); - this.#remain = this.#remain.slice(n); + const d = this.#remain.subarray(0, n); + this.#remain = this.#remain.subarray(n); p.set(d); return d.byteLength; } diff --git a/reader_test.ts b/reader_test.ts index e4ecbb9..5fbdcf8 100644 --- a/reader_test.ts +++ b/reader_test.ts @@ -12,13 +12,13 @@ Deno.test( worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [0, 1, 2, 3, 4], + data: new Uint8Array([0, 1, 2, 3, 4]), }), ); worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [5, 6, 7, 8, 9], + data: new Uint8Array([5, 6, 7, 8, 9]), }), ); let p: Uint8Array; @@ -45,14 +45,14 @@ Deno.test( worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [0, 1, 2, 3, 4], + data: new Uint8Array([0, 1, 2, 3, 4]), }), ); reader.close(); worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [5, 6, 7, 8, 9], + data: new Uint8Array([5, 6, 7, 8, 9]), }), ); let p: Uint8Array; @@ -78,14 +78,14 @@ Deno.test( worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + data: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), }), ); reader.close(); worker.onmessage?.call( worker, new MessageEvent("worker", { - data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + data: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), }), ); let n: number | null; diff --git a/test.ts b/test.ts index a39e1fa..ee6e068 100644 --- a/test.ts +++ b/test.ts @@ -43,8 +43,8 @@ Deno.test({ ]); // Peformance check - const rtThreshold = 3000; - const wtThreshold = 3000; + const rtThreshold = 500; + const wtThreshold = 500; assert(rt < rtThreshold, "Reader is too slow"); assert(wt < wtThreshold, "Writer is too slow"); } finally { @@ -88,8 +88,8 @@ Deno.test({ ]); // Peformance check - const rtThreshold = 5000; - const wtThreshold = 5000; + const rtThreshold = 500; + const wtThreshold = 500; assert(rt < rtThreshold, "Reader is too slow"); assert(wt < wtThreshold, "Writer is too slow"); } finally { @@ -133,8 +133,8 @@ Deno.test({ ]); // Peformance check - const rtThreshold = 30000; - const wtThreshold = 30000; + const rtThreshold = 500; + const wtThreshold = 500; assert(rt < rtThreshold, "Reader is too slow"); assert(wt < wtThreshold, "Writer is too slow"); } finally { diff --git a/types.ts b/types.ts index 19ea774..88b6d40 100644 --- a/types.ts +++ b/types.ts @@ -1,4 +1,4 @@ -type Payload = number[]; +type Payload = Uint8Array; export type WorkerForWorkerReader = { onmessage?: (message: MessageEvent) => void; diff --git a/writer.ts b/writer.ts index 4597582..360c277 100644 --- a/writer.ts +++ b/writer.ts @@ -16,10 +16,7 @@ export class WorkerWriter implements Deno.Writer { * https://github.com/lambdalisue/deno-workerio/issues/5 */ write(p: Uint8Array): Promise { - // XXX - // Send 'p' as-is once the issue below has resolved. - // https://github.com/denoland/deno/issues/3557 - this.#worker.postMessage(Array.from(p)); + this.#worker.postMessage(p); return Promise.resolve(p.length); } } diff --git a/writer_test.ts b/writer_test.ts index 038b7f3..7b4c434 100644 --- a/writer_test.ts +++ b/writer_test.ts @@ -4,7 +4,7 @@ import { WorkerForWorkerWriter, WorkerWriter } from "./mod.ts"; Deno.test( "WorkerWriter invokes internal worker.postMessage when data is written by 'write' method", async () => { - const posted: number[][] = []; + const posted: Uint8Array[] = []; const worker: WorkerForWorkerWriter = { postMessage(message) { posted.push(message); @@ -13,7 +13,7 @@ Deno.test( const writer = new WorkerWriter(worker); await writer.write(new Uint8Array([0, 1, 2, 3, 4])); await writer.write(new Uint8Array([5, 6, 7, 8, 9])); - assertEquals(posted[0], [0, 1, 2, 3, 4]); - assertEquals(posted[1], [5, 6, 7, 8, 9]); + assertEquals(posted[0], new Uint8Array([0, 1, 2, 3, 4])); + assertEquals(posted[1], new Uint8Array([5, 6, 7, 8, 9])); }, );