NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.96k stars 352 forks source link

Add: Support for cloudflare workers #240

Open Bartek20 opened 1 year ago

Bartek20 commented 1 year ago

I think some users will like this idea as it will help to eliminate eval() and new Function() restrictions in workers.

NeilFraser commented 1 year ago

What would be involved in supporting this?

Bartek20 commented 1 year ago

The point is that in cloudflare workers libraries are compiled into a single script and then they are executed. Unfortunately, when compiling this library, errors like Interpreter() is not a function or Interpreter() is not a class are generated. I would like to add that adding support would allow the execution of functions downloaded from an external api, which is that in workers there is a top-down restriction on the eval() and new Function() functions, therefore currently running functions downloaded from the api is not physically possible and this library is however, it does not currently work with workers properly.

NeilFraser commented 1 year ago

We don't have any experience with Cloudflare, so you'd have to do the work. If you need assistance from the JS-Interpreter side, do let us know. Once you get something running we'd certainly be interested in seeing it. Then we can discuss whether something like this could be added to the demo directory, or as documentation, or whether there are changes that should be made to the core library.

yorinasub17 commented 11 months ago

Not sure if you are still looking for a way to do this, but I found a way to integrate JS-Interpreter into Cloudflare Workers since I needed this as well. Here is a minimal hello world demo: https://github.com/yorinasub17/workers-js-interpreter-demo

The gist is that you need to load acorn into globalThis manually since it will be exported as a module instead of being loaded into the globalThis object like the browser environment.