JS-Interpreter icon indicating copy to clipboard operation
JS-Interpreter copied to clipboard

Add: Support for cloudflare workers

Open Bartek20 opened this issue 2 years ago • 4 comments

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

Bartek20 avatar Jan 11 '23 18:01 Bartek20

What would be involved in supporting this?

NeilFraser avatar Jan 11 '23 19:01 NeilFraser

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.

Bartek20 avatar Jan 17 '23 18:01 Bartek20

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.

NeilFraser avatar Jan 17 '23 19:01 NeilFraser

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.

yorinasub17 avatar Aug 04 '23 21:08 yorinasub17