Suppose I have the following snippet to create a worker in Deno and start it.
const worker = new Worker("./worker.ts", { type: "module", deno: true });
worker.postMessage({ command: "START" });
Is there a way to pipe stdout/stderr from the worker to a file, similarly to how Deno.run
works? For example, is something like this possible?
const file = await Deno.open('/path/to/file.log', { write: true });
const worker = new Worker("./worker.ts", {
type: "module",
deno: true,
stdout: file.rid,
stderr: file.rid
});
worker.postMessage({ command: "START" });
Or, is it possible to use a stream?
import { WritableStream } from "https://denopkg.com/keroxp/deno-streams/writable_stream.ts"
const stream = new WritableStream<number>({
write: chunk => {
...
}
})
const worker = new Worker("./worker.ts", {
type: "module",
deno: true,
stdout: stream,
stderr: stream
});
worker.postMessage({ command: "START" });
Currently transfering streams is not supported:
https://github.com/whatwg/streams/blob/main/transferable-streams-explainer.md
Once it is, you'll be able to transfer a
WritableStream
/ReadableStream
/TransformStream
to the worker using the following syntax.As a temporary solution, until it's supported, you can use
MessageChannel
to representstdout
&stderr
main
worker