-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathframework.js
More file actions
54 lines (47 loc) · 1.41 KB
/
Copy pathframework.js
File metadata and controls
54 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
// Example showing us how the framework creates an environment (sandbox) for
// appication runtime, load an application code and passes a sandbox into app
// as a global context and receives exported application interface
const PARSING_TIMEOUT = 1000;
const EXECUTION_TIMEOUT = 5000;
// The framework can require core libraries
const fs = require('fs');
const vm = require('vm');
const sfs = require('sandboxed-fs');
// Create a hash and turn it into the sandboxed context which will be
// the global context of an application
const context = {
module: {},
console,
require: (name) => {
if (name === 'fs') return sfs.bind('./');
let exported = execute('./node_modules/' + name + '/index.js');
if (!exported) exported = require(name);
}
};
context.global = context;
const sandbox = vm.createContext(context);
function execute(fileName) {
console.log(fileName);
fs.readFile(fileName, (err, src) => {
console.log(src);
let script;
try {
script = new vm.Script(src, { timeout: PARSING_TIMEOUT });
console.dir({ script });
} catch (e) {
console.dir(e);
process.exit(1);
}
try {
script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT });
const exported = sandbox.module.exports;
console.dir({ exported });
return exported;
} catch (e) {
console.dir(e);
process.exit(1);
}
});
}
execute('./application.js');