denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.71k stars 5.38k forks source link

Expose console primitives to Workers #11844

Open jfancher opened 3 years ago

jfancher commented 3 years ago

This is a probably a pretty niche use case, but maybe there's some useful general feature to arise from it.

I have the following situation:

I tried to copy/paste the guts of the console implementation, but in addition to being quite complex, it's not possible to replicate fully without some internal hooks, notably to inspect Promise values (and maybe other stuff).

Other approaches I tried or considered:

I got stuck at this point, and ended up forking Deno to solve this. (I added a runtime/js script that exports the Console class, and do console = new Console(myInterceptorFunc) in the Worker module). This was quite a simple change and ended up with exactly the behavior I want, but naturally I'd rather not have to maintain a fork.

Would any of these features be considered for Deno (in order of how useful they'd be to me)?

Open to any other alternatives that achieve the same result as well!

kitsonk commented 3 years ago

I am not a fan of this suggestion, as it leaks a lot of "private" non-web standard stuff, no matter which way you look at it, and it would dramatically increase the "contracted" API surface.

I had a similar problem of wanting to "sandbox" a workers console output and did this: https://github.com/kitsonk/dectyl/blob/main/runtime/console.ts along with this https://github.com/kitsonk/dectyl/blob/main/runtime/inspect.ts. It is effectively a re-implentation of the Deno runtime code for console and inspect and then post a message back to the main worker for additional processing: https://github.com/kitsonk/dectyl/blob/main/runtime/main.ts#L612-L618

pawelmhm commented 2 years ago

This is a probably a pretty niche use case

It is probably not that niche, because users asking about this on StackOverflow https://stackoverflow.com/questions/64362869/how-to-pipe-stdout-stderr-to-file-from-worker-in-deno and I have same problem and @kitsonk also faces the need to do this.

If you consider workers are good idea for running code you don't really fully trust it seems logical you don't want to have standard output or error of this process in your parent process logs.

Providing an option in the Worker constructor to redirect its output/error streams

:+1:

Something like this could be nice

const worker = new Worker("./worker.ts", {
  type: "module",
  deno: true,
  stdout: file.rid,
  stderr: file.rid
});

non-web standard stuf

It is certainly not typical web standard stuff, but maybe it could be done with web platform in mind, e.g. by providing stderr or stdout via transferable streams? https://github.com/whatwg/streams/blob/main/transferable-streams-explainer.md?

You could then create a worker with stdin, stdout and stderr set to some web streams

bartlomieju commented 1 year ago

We discussed this during our team meeting and we're feeling we should add Console constructor to console object, that would allow users to override the global console instance and direct logs anywhere user wishes.