/workers-js-interpreter-demo

A demo of using NeilFraser/JS-Interpreter with Cloudflare workers.

Primary LanguageTypeScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Cloudflare Workers JS-Intepreter demo

This repository is a demo of how to setup NeilFraser/JS-Interpreter to run in Cloudflare Workers.

Note that this is a minimal effort integration and is not the most ideal way to integrate the two. Namely:

  • Interpreter and acorn is added to the global this object.

Quickstart

pnpm install
pnpm start

Hit the endpoint. You should get the response, Yori.

Explanation

JS-Interpreter doesn't have an official NPM package, and the current third party packages are not up to date (see this issue on the current state of JS-Interpreter NPM packages). So instead, we install it through the git integration in NPM/PNPM, e.g:

pnpm add github:NeilFraser/JS-Interpreter

This doesn't work for yarn, since it requires a package.json file and the source repo doesn't have one. You can instead use my minimal fork.

E.g., yarn add 'JS-Interpreter@https://github.com/yorinasub17/JS-Interpreter#20230804'

Note that JS-Interpreter is optimized for the browser environment, and thus doesn't export the objects. Meaning, you can't do

// THIS DOESN'T WORK
import Interpreter from "JS-Interpreter/acorn_interpreter.js";

like you would expect. You must do

// This loads Interpreter into globalThis, making it accessible as "globalThis.Interpreter".
import "JS-Interpreter/interpreter.js";

Additionally, the provided acorn.js file in js-interpreter doesn't automatically load the acorn object into globalThis like the browser environment, and instead exports the object as modules. This breaks the Interpreter object as it expects being able to access acorn from globalThis.acorn.parse. To support this, you should manually load it to the globalThis object:

import "JS-Interpreter/interpreter.js";
import * as acorn from "JS-Interpreter/acorn.js";
globalThis.acorn = acorn;

After this, you should be able to access the Interpreter object using globalThis.Interpreter.