diff --git a/JavaScript/application.js b/JavaScript/application.js index 8cf3ab0..58c711a 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -1,19 +1,26 @@ 'use strict'; -// File contains a small piece of the source to demonstrate main module -// of a sample application to be executed in the sandboxed context by -// another pice of code from `framework.js`. Read README.md for tasks. - const fs = require('node:fs'); const net = require('node:net'); -// Print from the global context of application module -console.log('From application global context'); -console.dir({ fs, net }, { depth: 1 }); -console.dir({ global }, { depth: 1 }); -console.dir({ api }, { depth: 2 }); +const printGlobalObj = () => { + for (const [ key, val ] of Object.entries(global)) { + console.log(`${key}: ${typeof val}`); + } +} + +printGlobalObj(); + +// console.log('From application global context'); +// console.dir({ fs, net }, { depth: 1 }); +// console.dir({ global }, { depth: 1 }); +// console.dir({ api }, { depth: 2 }); + +setTimeout(() => {console.log('work')}, 2000); +console.log(util.format('Hello %s, %i', 'world', 1000)); + +const fn = (a, b) => a + b; -module.exports = () => { - // Print from the exported function context - console.log('From application exported function'); +module.exports = { + fn }; diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 94dcf33..2383480 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -1,23 +1,43 @@ '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('node:fs'); const vm = require('node:vm'); const timers = require('node:timers'); const events = require('node:events'); +const util = require('node:util'); + +const fileName = process.argv[2]; + +const editedConsole = Object.assign({}, console); +editedConsole.log = (message) => { + console.log(`${fileName} : ${new Date().toISOString()} : ${message}`); +} + +const compareSandboxes = (beforeSandbox, afterSandbox) => { + for (const key in beforeSandbox) { + if (key in afterSandbox) continue; + else return false; + } + for (const key in afterSandbox) { + if (key in beforeSandbox) continue; + else return false; + } + return true; +} + +const printFnInfo = (fn) => { + console.log(`Function parameter: ${fn.length}, source: ${fn.toString()}`); +} -// Create a hash and turn it into the sandboxed context which will be -// the global context of an application const context = { - module: {}, console, + module: {}, + console: editedConsole, + setTimeout, setImmediate, util, require: (name) => { + console.log(`${new Date().toISOString()} : ${name}`); if (name === 'fs' || name === 'node:fs') { console.log('Module fs is restricted'); return null; @@ -28,19 +48,12 @@ const context = { context.global = context; const sandbox = vm.createContext(context); +const sandboxBefore = Object.assign({}, sandbox); -// Prepare lambda context injection const api = { timers, events }; -// Read an application source code from the file -const fileName = './application.js'; fs.readFile(fileName, 'utf8', (err, src) => { - // We need to handle errors here - - // Wrap source to lambda, inject api src = `api => { ${src} };`; - - // Run an application in sandboxed context let script; try { script = new vm.Script(src, { timeout: PARSING_TIMEOUT }); @@ -53,16 +66,16 @@ fs.readFile(fileName, 'utf8', (err, src) => { try { const f = script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT }); f(api); + const sandboxAfter = Object.assign({}, sandbox); + console.log(compareSandboxes(sandboxBefore, sandboxAfter)); const exported = sandbox.module.exports; console.dir({ exported }); + printFnInfo(exported.fn); } catch (e) { console.dir(e); console.log('Execution timeout'); process.exit(1); } - - // We can access a link to exported interface from sandbox.module.exports - // to execute, save to the cache, print to console, etc. }); process.on('uncaughtException', (err) => { diff --git a/JavaScript/test.js b/JavaScript/test.js new file mode 100644 index 0000000..80d75be --- /dev/null +++ b/JavaScript/test.js @@ -0,0 +1,5 @@ +const name = 'John'; +const age = 30; +const message = util.format('Привіт, мене звати %s і мені %d років.', name, age); + +console.log(message);
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: