JSE is a small web service built with ExpressJS intended to run JavaScript, CoffeeScript and TypeScript code using vm (NodeJS) module.
- security. I didn't spent time on making it secure, it's not intended to run on production, it's a toy.
context
is limited in types it can contain, currently its limited to same types as JSON, that means, functions will be omitted from request and response objects.
Installing project dependencies:
$ npm install
Installing modules that will be available for use when code is running:
$ cd modules
$ npm install
to see how to install and include new modules, see modules/README.md.
$ node index.js
or $ npm start
$ npm run test-unit
$ npm run test-api
Endpoint | Description |
---|---|
/ | Execute JavaScript code |
/modules | Get information about installed and whitelisted node modules |
Execute JavaScript code with given context and modules.
Name | Required? | Type | Description |
---|---|---|---|
code |
required | string | code to be executed |
context |
required | object | context |
language |
required | string | type of language: javascript, typescript, coffeescript |
modules |
optional | list of strings | modules that code can have access to when executed |
{
"code": "list = _.map(list, function(e) { return e + 1; })",
"context": {
"list": [1, 2, 3]
},
"language": "javascript",
"modules": [
"underscore"
]
}
{
"context": {
"list": [
2,
3,
4
]
}
}
code
square = (x) -> x * x
list = [square(n) for n in list]
curl
curl -H "Content-Type: application/json" -X POST -d '{"code": "square = (x) -> x * x\nlist = [square(n) for n in list]", "context": {"list": [1, 2, 3]}, "language": "coffeescript"}' http://localhost:8000/
response
{"context":{"list":[[1,4,9]],"n":3}}
code
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
greeter = new Greeter("world");
curl
curl -H "Content-Type: application/json" -X POST -d '{"code": "class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return \"Hello, \" + this.greeting; } } greeter = new Greeter(\"world\");", "context": {"greeter": {}}, "language": "typescript"}' http://localhost:8000/
response
{"context":{"greeter":{"greeting":"world"}}}
NOTE:
greet()
method was omitted from response.
Get information about installed and whitelisted node modules
curl -H "Content-Type: application/json" -X GET http://localhost:8000/modules
{
"installed": {
"faker": "^3.1.0",
"underscore": "^1.8.3"
},
"whitelisted": {
"faker": "faker",
"underscore": "_"
}
}