From 625b726af55d200196cd4f122b146dc8b331366d Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 20:38:33 +0300 Subject: [PATCH 1/8] =?UTF-8?q?Completed=20task=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/application.js | 2 ++ JavaScript/framework.js | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/JavaScript/application.js b/JavaScript/application.js index 8cf3ab0..77242f1 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -13,6 +13,8 @@ console.dir({ fs, net }, { depth: 1 }); console.dir({ global }, { depth: 1 }); console.dir({ api }, { depth: 2 }); +setTimeout(() => {console.log('work')}, 2000); + module.exports = () => { // Print from the exported function context console.log('From application exported function'); diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 94dcf33..81815db 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -16,7 +16,7 @@ const events = require('node:events'); // 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, setTimeout, setImmediate, require: (name) => { if (name === 'fs' || name === 'node:fs') { console.log('Module fs is restricted'); @@ -28,6 +28,7 @@ const context = { context.global = context; const sandbox = vm.createContext(context); +console.log(sandbox); // Prepare lambda context injection const api = { timers, events }; @@ -52,6 +53,7 @@ fs.readFile(fileName, 'utf8', (err, src) => { try { const f = script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT }); + console.log("here", f); f(api); const exported = sandbox.module.exports; console.dir({ exported }); From 36f57da6a4552728700809f4a5a7c3d3ca92b62d Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 20:50:26 +0300 Subject: [PATCH 2/8] =?UTF-8?q?Completed=20task=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/application.js | 1 + JavaScript/framework.js | 3 ++- JavaScript/test.js | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 JavaScript/test.js diff --git a/JavaScript/application.js b/JavaScript/application.js index 77242f1..86e047d 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -14,6 +14,7 @@ console.dir({ global }, { depth: 1 }); console.dir({ api }, { depth: 2 }); setTimeout(() => {console.log('work')}, 2000); +console.log(util.format('Hello %s, %i', 'world', 1000)); module.exports = () => { // Print from the exported function context diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 81815db..3d04d3c 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -12,11 +12,12 @@ 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'); // Create a hash and turn it into the sandboxed context which will be // the global context of an application const context = { - module: {}, console, setTimeout, setImmediate, + module: {}, console, setTimeout, setImmediate, util, require: (name) => { if (name === 'fs' || name === 'node:fs') { console.log('Module fs is restricted'); diff --git a/JavaScript/test.js b/JavaScript/test.js new file mode 100644 index 0000000..3ad3fb4 --- /dev/null +++ b/JavaScript/test.js @@ -0,0 +1,7 @@ +const util = require('util'); + +const name = 'John'; +const age = 30; +const message = util.format('Привіт, мене звати %s і мені %d років.', name, age); + +console.log(message); From 70cd46393df90729833d82158acdc39c4c5d9f4a Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 20:55:48 +0300 Subject: [PATCH 3/8] =?UTF-8?q?Completed=20task=20=E2=84=963.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/framework.js | 3 +-- JavaScript/test.js | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 3d04d3c..152c0ed 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -29,13 +29,12 @@ const context = { context.global = context; const sandbox = vm.createContext(context); -console.log(sandbox); // Prepare lambda context injection const api = { timers, events }; // Read an application source code from the file -const fileName = './application.js'; +const fileName = process.argv[2]; fs.readFile(fileName, 'utf8', (err, src) => { // We need to handle errors here diff --git a/JavaScript/test.js b/JavaScript/test.js index 3ad3fb4..80d75be 100644 --- a/JavaScript/test.js +++ b/JavaScript/test.js @@ -1,5 +1,3 @@ -const util = require('util'); - const name = 'John'; const age = 30; const message = util.format('Привіт, мене звати %s і мені %d років.', name, age); From feb2cb4b2b83a306482cb6d3af5b4c325a48a5c2 Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 21:03:44 +0300 Subject: [PATCH 4/8] =?UTF-8?q?Completed=20task=20=E2=84=965.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/framework.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 152c0ed..f9ae0c1 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -1,23 +1,25 @@ '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'); -// Create a hash and turn it into the sandboxed context which will be -// the global context of an application +const fileName = process.argv[2]; + +const editedConsole = Object.assign({}, console); +editedConsole.log = (message) => { + console.log(`${fileName} : ${new Date().toISOString()} : ${message}`); +} + const context = { - module: {}, console, setTimeout, setImmediate, util, + module: {}, + console: editedConsole, + setTimeout, setImmediate, util, require: (name) => { if (name === 'fs' || name === 'node:fs') { console.log('Module fs is restricted'); @@ -33,8 +35,6 @@ const sandbox = vm.createContext(context); // Prepare lambda context injection const api = { timers, events }; -// Read an application source code from the file -const fileName = process.argv[2]; fs.readFile(fileName, 'utf8', (err, src) => { // We need to handle errors here From 2e2b5321856f69a66b85bd92d2a0036c8952576b Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 21:09:32 +0300 Subject: [PATCH 5/8] =?UTF-8?q?Completed=20task=20=E2=84=966.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/framework.js | 1 + 1 file changed, 1 insertion(+) diff --git a/JavaScript/framework.js b/JavaScript/framework.js index f9ae0c1..658621c 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -21,6 +21,7 @@ const context = { 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; From 404975b3905f2f9d748cb568dc444f826becf78c Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 21:20:43 +0300 Subject: [PATCH 6/8] =?UTF-8?q?Completed=20task=20=E2=84=968.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/application.js | 12 ++++-------- JavaScript/framework.js | 16 +++++++--------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/JavaScript/application.js b/JavaScript/application.js index 86e047d..e5462f3 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -1,13 +1,8 @@ '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 }); @@ -16,7 +11,8 @@ console.dir({ api }, { depth: 2 }); setTimeout(() => {console.log('work')}, 2000); console.log(util.format('Hello %s, %i', 'world', 1000)); -module.exports = () => { - // Print from the exported function context - console.log('From application exported function'); +const fn = (a, b) => a + b; + +module.exports = { + fn }; diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 658621c..9307db4 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -16,6 +16,10 @@ editedConsole.log = (message) => { console.log(`${fileName} : ${new Date().toISOString()} : ${message}`); } +const printFnInfo = (fn) => { + console.log(`Function parameter: ${fn.length}, source: ${fn.toString()}`); +} + const context = { module: {}, console: editedConsole, @@ -33,16 +37,10 @@ const context = { context.global = context; const sandbox = vm.createContext(context); -// Prepare lambda context injection const api = { timers, events }; 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 }); @@ -58,16 +56,16 @@ fs.readFile(fileName, 'utf8', (err, src) => { f(api); 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) => { console.log('Unhandled exception: ' + err); }); From d39ccc12bcca8d9e4ecf6ee49896361447b62dac Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 21:42:12 +0300 Subject: [PATCH 7/8] =?UTF-8?q?Completed=20task=20=E2=84=969.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/application.js | 16 ++++++++++++---- JavaScript/framework.js | 1 - 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/JavaScript/application.js b/JavaScript/application.js index e5462f3..58c711a 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -3,10 +3,18 @@ const fs = require('node:fs'); const net = require('node:net'); -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)); diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 9307db4..5bef733 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -52,7 +52,6 @@ fs.readFile(fileName, 'utf8', (err, src) => { try { const f = script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT }); - console.log("here", f); f(api); const exported = sandbox.module.exports; console.dir({ exported }); From 29cab7dff089e9bdadf0c1952874eb99d61f9793 Mon Sep 17 00:00:00 2001 From: TheBladeHit Date: Wed, 11 Oct 2023 21:53:49 +0300 Subject: [PATCH 8/8] =?UTF-8?q?Completed=20task=20=E2=84=9610.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaScript/framework.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 5bef733..2383480 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -16,6 +16,18 @@ 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()}`); } @@ -36,6 +48,7 @@ const context = { context.global = context; const sandbox = vm.createContext(context); +const sandboxBefore = Object.assign({}, sandbox); const api = { timers, events }; @@ -53,6 +66,8 @@ 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); @@ -63,8 +78,6 @@ fs.readFile(fileName, 'utf8', (err, src) => { } }); - - process.on('uncaughtException', (err) => { console.log('Unhandled exception: ' + err); }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy