Skip to main content

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

Works with
It is unknown whether this package works with Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Browsers
JSR Score
94%
Published
a week ago (4.0.0)

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

import {
  readableStreamFromWorker,
  writableStreamFromWorker,
} from "@lambdalisue/workerio";

const decoder = new TextDecoder();
const encoder = new TextEncoder();

async function main(): Promise<void> {
  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));
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @lambdalisue/workerio

Import symbol

import * as mod from "@lambdalisue/workerio";

Add Package

npx jsr add @lambdalisue/workerio

Import symbol

import * as mod from "@lambdalisue/workerio";

Add Package

yarn dlx jsr add @lambdalisue/workerio

Import symbol

import * as mod from "@lambdalisue/workerio";

Add Package

pnpm dlx jsr add @lambdalisue/workerio

Import symbol

import * as mod from "@lambdalisue/workerio";

Add Package

bunx jsr add @lambdalisue/workerio

Import symbol

import * as mod from "@lambdalisue/workerio";