diff --git a/README.md b/README.md
index 5b03a23..cd9b9d5 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,9 @@ for await (const data of reader) {
#### Worker
```typescript
+///
+///
+
import {
readableStreamFromWorker,
writableStreamFromWorker,
@@ -55,9 +58,8 @@ const decoder = new TextDecoder();
const encoder = new TextEncoder();
async function main(): Promise {
- 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) {
diff --git a/deno.jsonc b/deno.jsonc
index b780ed6..90ab4d1 100644
--- a/deno.jsonc
+++ b/deno.jsonc
@@ -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": [
diff --git a/mod.ts b/mod.ts
index 20c6f43..676717c 100644
--- a/mod.ts
+++ b/mod.ts
@@ -41,6 +41,9 @@
* #### Worker
*
* ```typescript
+ * ///
+ * ///
+ *
* import {
* readableStreamFromWorker,
* writableStreamFromWorker,
@@ -50,9 +53,8 @@
* const encoder = new TextEncoder();
*
* async function main(): Promise {
- * 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) {
@@ -69,3 +71,4 @@
*/
export * from "./readable_stream.ts";
export * from "./writable_stream.ts";
+export type * from "./types.ts";
diff --git a/readable_stream.ts b/readable_stream.ts
index cdd2ac3..6dc91fd 100644
--- a/readable_stream.ts
+++ b/readable_stream.ts
@@ -1,3 +1,5 @@
+import type { WorkerLike } from "./types.ts";
+
/**
* Options for creating a readable stream from a worker.
*/
@@ -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 {
const {
diff --git a/test_echo_server.ts b/test_echo_server.ts
index 5e411f2..f22f6bf 100644
--- a/test_echo_server.ts
+++ b/test_echo_server.ts
@@ -1,4 +1,6 @@
-const worker = self as unknown as Worker;
-worker.onmessage = (ev) => {
- worker.postMessage(ev.data);
+///
+///
+
+self.onmessage = (ev) => {
+ self.postMessage(ev.data);
};
diff --git a/types.ts b/types.ts
new file mode 100644
index 0000000..97dd65c
--- /dev/null
+++ b/types.ts
@@ -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;
+};
diff --git a/writable_stream.ts b/writable_stream.ts
index b7031c2..568ed82 100644
--- a/writable_stream.ts
+++ b/writable_stream.ts
@@ -1,3 +1,5 @@
+import type { WorkerLike } from "./types.ts";
+
/**
* Options for creating a writable stream from a worker.
*/
@@ -15,7 +17,7 @@ export type WritableStreamFromWorkerOptions = {
* @returns A new `WritableStream` instance.
*/
export function writableStreamFromWorker(
- worker: Worker,
+ worker: WorkerLike,
options: WritableStreamFromWorkerOptions = {},
): WritableStream {
const {