@@ -10,6 +10,8 @@ const EXECUTION_TIMEOUT = 5000;
1010// The framework can require core libraries
1111const fs = require ( 'fs' ) ;
1212const vm = require ( 'vm' ) ;
13+ const timers = require ( 'timers' ) ;
14+ const events = require ( 'events' ) ;
1315
1416// Create a hash and turn it into the sandboxed context which will be
1517// the global context of an application
@@ -27,25 +29,34 @@ const context = {
2729context . global = context ;
2830const sandbox = vm . createContext ( context ) ;
2931
32+ // Prepare lambda context injection
33+ const api = { timers, events } ;
34+
3035// Read an application source code from the file
3136const fileName = './application.js' ;
3237fs . readFile ( fileName , ( err , src ) => {
3338 // We need to handle errors here
3439
40+ // Wrap source to lambda, inject api
41+ src = `api => { ${ src } };` ;
42+
3543 // Run an application in sandboxed context
3644 let script ;
3745 try {
3846 script = new vm . Script ( src , { timeout : PARSING_TIMEOUT } ) ;
3947 } catch ( e ) {
48+ console . dir ( e ) ;
4049 console . log ( 'Parsing timeout' ) ;
4150 process . exit ( 1 ) ;
4251 }
4352
4453 try {
45- script . runInNewContext ( sandbox , { timeout : EXECUTION_TIMEOUT } ) ;
54+ const f = script . runInNewContext ( sandbox , { timeout : EXECUTION_TIMEOUT } ) ;
55+ f ( api ) ;
4656 const exported = sandbox . module . exports ;
4757 console . dir ( { exported } ) ;
4858 } catch ( e ) {
59+ console . dir ( e ) ;
4960 console . log ( 'Execution timeout' ) ;
5061 process . exit ( 1 ) ;
5162 }
0 commit comments