Skip to content

🦕 Deno module to translate Worker's system of messages into ReadableStream and WritableStream

License

Notifications You must be signed in to change notification settings

lambdalisue/deno-workerio

Repository files navigation

workerio

JSR Test codecov

Deno module to translate Worker's system of messages into ReadableStream<Uint8Array> and WritableStream<Uint8Array>.

Example

Server

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);
}

Worker

/// <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));

License

The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.

About

🦕 Deno module to translate Worker's system of messages into ReadableStream and WritableStream

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •