From 359b050d1d7e2bf582fe7882b304ebaf793e4dc5 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 31 Dec 2019 00:04:22 +0200 Subject: [PATCH] sandbox exercises --- JavaScript/application.js | 54 ++++++++++++----- JavaScript/framework.js | 119 +++++++++++++++++++++----------------- 2 files changed, 107 insertions(+), 66 deletions(-) diff --git a/JavaScript/application.js b/JavaScript/application.js index 8f018b8..3c5e82b 100644 --- a/JavaScript/application.js +++ b/JavaScript/application.js @@ -1,19 +1,47 @@ 'use strict'; +// Tests requires +let api; -// 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. +// Require some modules +require('fs'); +require('vm'); -const fs = require('fs'); -const net = require('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 }); +// One call to util`s function +const o = { a: 5, b: 6 }; +o.self = o; +api.console.log(api.util.inspect(o)); +// Using setTimeout and setInterval +// Exporting a function module.exports = () => { - // Print from the exported function context - console.log('From application exported function'); + api.timers.setTimeout(() => { + // Print from the exported function context + api.console.log('setTimeout from application2 exported function'); + }, 5000); + + api.timers.setInterval(() => { + // Print from the exported function context + api.console.log('setInterval from application2 exported function'); + }, 2000); }; +/* +// Exporting an onject +const obj = { + fn1: () => {}, + arr: Array.of({ length: 5 }), + o: new Object({}), + str: new String('string').valueOf(), + //buffer: Buffer.alloc(10), + bool: new Boolean(false).valueOf(), +}; + +module.exports = obj; +*/ +// List of everything from the global context (application +// sandbox) with the data types specified +const def = obj => + Object.keys(obj).reduce( + (hash, key) => ((hash[key] = typeof obj[key]), hash), + {} + ); +api.console.log(def(global)); diff --git a/JavaScript/framework.js b/JavaScript/framework.js index 6bdafde..aa2d289 100644 --- a/JavaScript/framework.js +++ b/JavaScript/framework.js @@ -1,69 +1,82 @@ '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; +// 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`. // The framework can require core libraries -const fs = require('fs'); -const vm = require('vm'); -const timers = require('timers'); -const events = require('events'); -// 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') { - console.log('Module fs is restricted'); - return null; - } - return require(name); - } -}; +// Tests require to initialize 'api', but with it, code is not running! +let api; +global.api = {}; +api.vm = require('vm'); +api.fs = require('fs'); +api.util = require('util'); -context.global = context; -const sandbox = vm.createContext(context); +// This is a wrapper for 'console.log' to add more info into console output +//in the following format: `