Deno module to translate Worker's system of messages into
ReadableStream<Uint8Array>
and
WritableStream<Uint8Array>
.
import {
readableStreamFromWorker,
writableStreamFromWorker,
} from "@lambdalisue/workerio";
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);
}
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />
import {
readableStreamFromWorker,
writableStreamFromWorker,
} from "@lambdalisue/workerio";
const decoder = new TextDecoder();
const encoder = new TextEncoder();
async function main(): Promise<void> {
const reader = readableStreamFromWorker(self);
const writer = writableStreamFromWorker(self);
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));
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.