Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lambdalisue/deno-workerio

🦕 Deno module to translate Worker's system of messages into ReadableStream and WritableStream
https://github.com/lambdalisue/deno-workerio

deno worker

Last synced: 2 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# workerio

[![JSR](https://jsr.io/badges/@lambdalisue/workerio)](https://jsr.io/@lambdalisue/workerio)
[![Test](https://github.com/lambdalisue/deno-workerio/actions/workflows/test.yml/badge.svg)](https://github.com/lambdalisue/deno-workerio/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/lambdalisue/deno-workerio/graph/badge.svg?token=6Q1iOBg2Ka)](https://codecov.io/gh/lambdalisue/deno-workerio)

[Deno][deno] module to translate Worker's system of messages into
[`ReadableStream`][readablestream] and
[`WritableStream`][writablestream].

[deno]: https://deno.land/
[ReadableStream]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
[WritableStream]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream

## Example

#### Server

```typescript
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

```typescript
///
///

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

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

async function main(): Promise {
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](./LICENSE). Contributors need
to agree that any modifications sent in this repository follow the license.