When you create a new repl instance in code it automatically has access to anything in global scope. You can modify the repl context to expose some custom variables in local scope so that the repl can access them, but I don't see an easy way to eliminate access to the global scope. I wish I could just give the repl a new blank global scope.
Here is an example repl instance:
var repl = require('repl'),
msg = "Hello world!";
repl.start('> ').context.msg = msg;
In that repl I typed out the following:
for (var key in global) {
console.log(key);
}
Which resulted in the following list:
- ArrayBuffer
- Int8Array
- Uint8Array
- Uint8ClampedArray
- Int16Array
- Uint16Array
- Int32Array
- Uint32Array
- Float32Array
- Float64Array
- DataView
- DTRACE_NET_SERVER_CONNECTION
- DTRACE_NET_STREAM_END
- DTRACE_NET_SOCKET_READ
- DTRACE_NET_SOCKET_WRITE
- DTRACE_HTTP_SERVER_REQUEST
- DTRACE_HTTP_SERVER_RESPONSE
- DTRACE_HTTP_CLIENT_REQUEST
- DTRACE_HTTP_CLIENT_RESPONSE
- COUNTER_NET_SERVER_CONNECTION
- COUNTER_NET_SERVER_CONNECTION_CLOSE
- COUNTER_HTTP_SERVER_REQUEST
- COUNTER_HTTP_SERVER_RESPONSE
- COUNTER_HTTP_CLIENT_REQUEST
- COUNTER_HTTP_CLIENT_RESPONSE
- global
- process
- GLOBAL
- root
- Buffer
- setTimeout
- setInterval
- clearTimeout
- clearInterval
- setImmediate
- clearImmediate
- console
- module
- require
- msg
- _
- key
You can see that our msg
variable was added in there, which is great, but there are many global variables I do not want to expose. I want to expose some of the less harmful ones, such as setTimeout
, console
, etc, but definitely not things like require
, process
, etc. Does anyone know how I might overcome this without spawning a totally new child process?