Constructor
new Uniscope(optionsopt, importsopt)
Create a scope object which accepts a JS code as string and then ensures that the accessible globals are limited to a specified subset.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Uniscope.options |
<optional> |
Options to configure the script executions inside the scope |
imports |
Object |
<optional> |
Import variables in the context with the given value |
Methods
(static) create(optionsopt, importsopt) → {Uniscope}
Create a new scope.
This is a handy helper function for those who do not prefer the new
operator!
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Uniscope.options |
<optional> |
Options to configure the script executions inside the scope |
imports |
Object |
<optional> |
Import variables in the context with the given value |
Returns:
- Type
- Uniscope
Example
let Scope = require('uniscope'), // use browserify or requireJS in browser!
myscope;
// create a new scope
myscope = Scope.create(globals, { // `globals` will be `window` in browser
strict: true, // specify whether to run the script in strict mode
}, { // provide an object with globals to be made available to the scripts
myGlobalVarName: "sample"
});
// set a specific variable as global
myscope.set('logger', function (msg) {
console.log(msg);
});
// now run a script
myscope.exec('logger(myGlobalVarName)', function (err) {
err ? console.error(err.stack || err) : console.log('execution complete');
});
exec(code, asyncopt, callback)
Executes a string within a protected Uniscope of controlled set of globals.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
code |
String | A string representing a JavaScript expression, statement, or sequence of statements |
||
async |
Boolean |
<optional> |
false | When set to true, callback will be called when |
callback |
function | Callback function to be called at the end of execution |
import(variables)
Replace a set of variables in the Uniscope's context with values provided as parameter.
Parameters:
Name | Type | Description |
---|---|---|
variables |
Object | Import variables in the context with the given value |
reset(localsopt, contextopt)
Resets the scope.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
locals |
Boolean |
<optional> |
Reset locals |
context |
Boolean |
<optional> |
Reset context |
set(varname, value)
Replace a particular global variable in the Uniscope with one of your own.
Parameters:
Name | Type | Description |
---|---|---|
varname |
String | Variable name or identifier |
value |
* | Value which will be assigned to the variable |
unset(varname)
Removes a global variable that has been custom set previously
Parameters:
Name | Type | Description |
---|---|---|
varname |
String | Variable name or identifier |
Type Definitions
options
Configuration options for Uniscope.
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
eval |
Boolean |
<optional> |
false | Specify whether eval is available inside sandbox |
strict |
Boolean |
<optional> |
false | Specify whether to run the script in strict mode |
console |
Boolean |
<optional> |
false | When set to true, the native |
ignore |
Array.<String> |
<optional> |
Specify a set of global variables that will not be allowed to trickle in |
|
blocked |
Array.<String> |
<optional> |
Specify a set of global variables that will not be allowed to trickle in |