Skip to content

Instantly share code, notes, and snippets.

@allain
Created June 27, 2018 15:29
Show Gist options
  • Select an option

  • Save allain/d553deee5038e09935ad51c87af23cc1 to your computer and use it in GitHub Desktop.

Select an option

Save allain/d553deee5038e09935ad51c87af23cc1 to your computer and use it in GitHub Desktop.
const vm = require('vm')
const repl = require('repl')
const globals = {
Date,
JSON
}
const state = {}
const environment = new Proxy(state, {
get (target, key) {
if (key === 'context') return state
return globals[key] || target[key]
},
set (target, key, value) {
target[key] = value
}
})
const ctx = vm.createContext(environment)
repl.start({
prompt: '> ',
eval: (command, context, filename, cb) =>
Promise.resolve(vm.runInContext(command, ctx)).then(result => cb(null, result))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment