@@ -10,6 +10,8 @@ const EXECUTION_TIMEOUT = 5000;
10
10
// The framework can require core libraries
11
11
const fs = require ( 'fs' ) ;
12
12
const vm = require ( 'vm' ) ;
13
+ const timers = require ( 'timers' ) ;
14
+ const events = require ( 'events' ) ;
13
15
14
16
// Create a hash and turn it into the sandboxed context which will be
15
17
// the global context of an application
@@ -27,25 +29,34 @@ const context = {
27
29
context . global = context ;
28
30
const sandbox = vm . createContext ( context ) ;
29
31
32
+ // Prepare lambda context injection
33
+ const api = { timers, events } ;
34
+
30
35
// Read an application source code from the file
31
36
const fileName = './application.js' ;
32
37
fs . readFile ( fileName , ( err , src ) => {
33
38
// We need to handle errors here
34
39
40
+ // Wrap source to lambda, inject api
41
+ src = `api => { ${ src } };` ;
42
+
35
43
// Run an application in sandboxed context
36
44
let script ;
37
45
try {
38
46
script = new vm . Script ( src , { timeout : PARSING_TIMEOUT } ) ;
39
47
} catch ( e ) {
48
+ console . dir ( e ) ;
40
49
console . log ( 'Parsing timeout' ) ;
41
50
process . exit ( 1 ) ;
42
51
}
43
52
44
53
try {
45
- script . runInNewContext ( sandbox , { timeout : EXECUTION_TIMEOUT } ) ;
54
+ const f = script . runInNewContext ( sandbox , { timeout : EXECUTION_TIMEOUT } ) ;
55
+ f ( api ) ;
46
56
const exported = sandbox . module . exports ;
47
57
console . dir ( { exported } ) ;
48
58
} catch ( e ) {
59
+ console . dir ( e ) ;
49
60
console . log ( 'Execution timeout' ) ;
50
61
process . exit ( 1 ) ;
51
62
}
0 commit comments