Skip to content

Commit

Permalink
Merge pull request #31 from Milly/allow-worker-env
Browse files Browse the repository at this point in the history
feat: allows `WorkerLike`
  • Loading branch information
lambdalisue authored May 31, 2024
2 parents 7d9d89c + 11857b0 commit 7d215d9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ for await (const data of reader) {
#### Worker

```typescript
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />

import {
readableStreamFromWorker,
writableStreamFromWorker,
Expand All @@ -55,9 +58,8 @@ 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 reader = readableStreamFromWorker(self);
const writer = writableStreamFromWorker(self);
const w = writer.getWriter();

for await (const data of reader) {
Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"exports": {
".": "./mod.ts",
"./readable_stream": "./readable_stream.ts",
"./writable_stream": "./writable_stream.ts"
"./writable_stream": "./writable_stream.ts",
"./types": "./types.ts"
},
"publish": {
"exclude": [
Expand Down
9 changes: 6 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
* #### Worker
*
* ```typescript
* /// <reference no-default-lib="true" />
* /// <reference lib="deno.worker" />
*
* import {
* readableStreamFromWorker,
* writableStreamFromWorker,
Expand All @@ -50,9 +53,8 @@
* 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 reader = readableStreamFromWorker(self);
* const writer = writableStreamFromWorker(self);
* const w = writer.getWriter();
*
* for await (const data of reader) {
Expand All @@ -69,3 +71,4 @@
*/
export * from "./readable_stream.ts";
export * from "./writable_stream.ts";
export type * from "./types.ts";
4 changes: 3 additions & 1 deletion readable_stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { WorkerLike } from "./types.ts";

/**
* Options for creating a readable stream from a worker.
*/
Expand All @@ -16,7 +18,7 @@ export type ReadableStreamFromWorkerOptions = {
* @returns A readable stream that can be used to read the data.
*/
export function readableStreamFromWorker(
worker: Worker,
worker: WorkerLike,
options: ReadableStreamFromWorkerOptions = {},
): ReadableStream<Uint8Array> {
const {
Expand Down
8 changes: 5 additions & 3 deletions test_echo_server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const worker = self as unknown as Worker;
worker.onmessage = (ev) => {
worker.postMessage(ev.data);
/// <reference no-default-lib="true" />
/// <reference lib="deno.worker" />

self.onmessage = (ev) => {
self.postMessage(ev.data);
};
7 changes: 7 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Worker like object. */
export type WorkerLike = {
// deno-lint-ignore no-explicit-any
onmessage: ((ev: MessageEvent) => any) | null;
// deno-lint-ignore no-explicit-any
postMessage(message: any, transfer?: Transferable[]): void;
};
4 changes: 3 additions & 1 deletion writable_stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { WorkerLike } from "./types.ts";

/**
* Options for creating a writable stream from a worker.
*/
Expand All @@ -15,7 +17,7 @@ export type WritableStreamFromWorkerOptions = {
* @returns A new `WritableStream` instance.
*/
export function writableStreamFromWorker(
worker: Worker,
worker: WorkerLike,
options: WritableStreamFromWorkerOptions = {},
): WritableStream<Uint8Array> {
const {
Expand Down

0 comments on commit 7d215d9

Please sign in to comment.