From a1c347065aa649b52d0276adcf4382f4408a4d3a Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 15:48:46 +0200 Subject: [PATCH 01/13] Task1 done --- sandboxedModule/ru/application.js | 8 +++++++- sandboxedModule/ru/framework.js | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index b66eff8..8b44dc3 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -7,5 +7,11 @@ console.log('From application global context'); module.exports = function() { // Вывод из контекста экспортируемой функции - console.log('From application exported function'); + //console.log('From application exported function'); + setTimeout(() => { + console.log("setTimeout function"); + }, 3000); + setInterval(() => { + console.log("setInterval function"); + }, 1000); }; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 03a0e99..6158e85 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -8,7 +8,7 @@ var fs = require('fs'), vm = require('vm'); // Создаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console }; +var context = { module: {}, console: console, setTimeout: setTimeout, setInterval: setInterval }; context.global = context; var sandbox = vm.createContext(context); @@ -21,6 +21,7 @@ fs.readFile(fileName, function(err, src) { var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); + sandbox.module.exports(); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. }); From 0cd8a4bf0c8bdc872fabf62021a87072422dbe4f Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 16:12:11 +0200 Subject: [PATCH 02/13] Task2 done --- sandboxedModule/ru/application.js | 16 ++++++++++------ sandboxedModule/ru/framework.js | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 8b44dc3..11e3308 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -5,13 +5,17 @@ // Вывод из глобального контекста модуля console.log('From application global context'); +var Country = function (name, capital) { + this.name = name; + this.capital = capital; + } + module.exports = function() { // Вывод из контекста экспортируемой функции //console.log('From application exported function'); - setTimeout(() => { - console.log("setTimeout function"); - }, 3000); - setInterval(() => { - console.log("setInterval function"); - }, 1000); + var newCountry = new Country("Ukraine", "Kyiv"); + console.log(util.format("Call util.inspect() through util.format(): %s", + util.inspect(newCountry)) + ); + }; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 6158e85..3ce2396 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -6,9 +6,10 @@ // Фреймворк может явно зависеть от библиотек через dependency lookup var fs = require('fs'), vm = require('vm'); + util = require ('util'); // Создаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console, setTimeout: setTimeout, setInterval: setInterval }; +var context = { module: {}, console: console, util: util }; context.global = context; var sandbox = vm.createContext(context); From f5a38a6baf67bd75d44abd050989e22f762c50ac Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 16:22:41 +0200 Subject: [PATCH 03/13] Task1 done --- sandboxedModule/ru/framework.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 3ce2396..dfe99e3 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -9,7 +9,8 @@ var fs = require('fs'), util = require ('util'); // Создаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console, util: util }; +var context = { module: {}, console: console, setTimeout: setTimeout, + setInterval: setInterval, util: util }; context.global = context; var sandbox = vm.createContext(context); From 87a9bec5721ef462c178b9f09bd4cccc6ac41c44 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 16:25:21 +0200 Subject: [PATCH 04/13] Tasks 1-2 done --- sandboxedModule/ru/application.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 11e3308..dd641f2 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -13,9 +13,19 @@ var Country = function (name, capital) { module.exports = function() { // Вывод из контекста экспортируемой функции //console.log('From application exported function'); + setTimeout(() => { + console.log("setTimeout function"); + }, 3000); + + setInterval(() => { + console.log("setInterval function"); + }, 1000); + //Задание 1 + var newCountry = new Country("Ukraine", "Kyiv"); console.log(util.format("Call util.inspect() through util.format(): %s", util.inspect(newCountry)) ); + //Задание 2 }; From 75f8e25c16ffa6874b4f148a5133bb0ce44c447a Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 21:56:02 +0200 Subject: [PATCH 05/13] Task 1-3 done --- sandboxedModule/ru/framework.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index dfe99e3..7598aec 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -15,10 +15,12 @@ context.global = context; var sandbox = vm.createContext(context); // Читаем исходный код приложения из файла -var fileName = './application.js'; +var fileName = process.argv[2] == null?'./application.js':process.argv[2]; fs.readFile(fileName, function(err, src) { // Тут нужно обработать ошибки - + if (err) + throw err; + // Запускаем код приложения в песочнице var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); From 4a0ed2432ed86e936805abccf8af62c3a855a8aa Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 22:13:30 +0200 Subject: [PATCH 06/13] Tasks 1-4 done --- sandboxedModule/ru/framework.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 7598aec..1cf7853 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -8,13 +8,20 @@ var fs = require('fs'), vm = require('vm'); util = require ('util'); +// Задание 4 +var newConsole = {}; +newConsole.log = function(message) { + console.log(fileName + " " + new Date() + " " + message); +} + // Создаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console, setTimeout: setTimeout, +var context = { module: {}, console: newConsole, setTimeout: setTimeout, setInterval: setInterval, util: util }; context.global = context; var sandbox = vm.createContext(context); // Читаем исходный код приложения из файла +// Задание 3 var fileName = process.argv[2] == null?'./application.js':process.argv[2]; fs.readFile(fileName, function(err, src) { // Тут нужно обработать ошибки From 6a9b8ee6e1029be8e555592b7ff54fd10fbb453e Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 22:48:19 +0200 Subject: [PATCH 07/13] Tasks 1-5 done --- sandboxedModule/ru/file.txt | 7 +++++++ sandboxedModule/ru/framework.js | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 sandboxedModule/ru/file.txt diff --git a/sandboxedModule/ru/file.txt b/sandboxedModule/ru/file.txt new file mode 100644 index 0000000..cf75722 --- /dev/null +++ b/sandboxedModule/ru/file.txt @@ -0,0 +1,7 @@ +application.js Sun Mar 20 2016 22:47:16 GMT+0200 (EET) From application global context +application.js Sun Mar 20 2016 22:47:16 GMT+0200 (EET) Call util.inspect() through util.format(): { name: 'Ukraine', capital: 'Kyiv' } +application.js Sun Mar 20 2016 22:47:17 GMT+0200 (EET) setInterval function +application.js Sun Mar 20 2016 22:47:18 GMT+0200 (EET) setInterval function +application.js Sun Mar 20 2016 22:47:19 GMT+0200 (EET) setTimeout function +application.js Sun Mar 20 2016 22:47:19 GMT+0200 (EET) setInterval function +application.js Sun Mar 20 2016 22:47:20 GMT+0200 (EET) setInterval function diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 1cf7853..44db024 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -8,9 +8,14 @@ var fs = require('fs'), vm = require('vm'); util = require ('util'); -// Задание 4 +// Задания 4-5 var newConsole = {}; newConsole.log = function(message) { + fs.appendFile('file.txt', fileName + " " + new Date() + " " + message + '\n', + (err) => { + if (err) + throw err; + }); console.log(fileName + " " + new Date() + " " + message); } From 41acd3b0d91f0a79704ef10103f5df229c2ce5fa Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 23:04:19 +0200 Subject: [PATCH 08/13] Tasks 1-5 done --- sandboxedModule/ru/file.txt | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 sandboxedModule/ru/file.txt diff --git a/sandboxedModule/ru/file.txt b/sandboxedModule/ru/file.txt deleted file mode 100644 index cf75722..0000000 --- a/sandboxedModule/ru/file.txt +++ /dev/null @@ -1,7 +0,0 @@ -application.js Sun Mar 20 2016 22:47:16 GMT+0200 (EET) From application global context -application.js Sun Mar 20 2016 22:47:16 GMT+0200 (EET) Call util.inspect() through util.format(): { name: 'Ukraine', capital: 'Kyiv' } -application.js Sun Mar 20 2016 22:47:17 GMT+0200 (EET) setInterval function -application.js Sun Mar 20 2016 22:47:18 GMT+0200 (EET) setInterval function -application.js Sun Mar 20 2016 22:47:19 GMT+0200 (EET) setTimeout function -application.js Sun Mar 20 2016 22:47:19 GMT+0200 (EET) setInterval function -application.js Sun Mar 20 2016 22:47:20 GMT+0200 (EET) setInterval function From 223de677c8a0cebdf8d0c86940de98bd437f9986 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Sun, 20 Mar 2016 23:16:57 +0200 Subject: [PATCH 09/13] Tasks 1-6 done --- sandboxedModule/ru/application.js | 12 ++++++++---- sandboxedModule/ru/framework.js | 12 +++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index dd641f2..4a8d16b 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -2,6 +2,8 @@ // прикладного приложения, загружаемого в песочницу демонстрационным // кусочком фреймворка. Читайте README.md в нем задания. +util = require ('util'); + // Вывод из глобального контекста модуля console.log('From application global context'); @@ -12,20 +14,22 @@ var Country = function (name, capital) { module.exports = function() { // Вывод из контекста экспортируемой функции - //console.log('From application exported function'); - setTimeout(() => { + console.log('From application exported function'); + + //Задание 1 + setTimeout(() => { console.log("setTimeout function"); }, 3000); setInterval(() => { console.log("setInterval function"); }, 1000); - //Задание 1 + //Задание 2 var newCountry = new Country("Ukraine", "Kyiv"); console.log(util.format("Call util.inspect() through util.format(): %s", util.inspect(newCountry)) ); - //Задание 2 + }; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 44db024..b52f09a 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -19,9 +19,19 @@ newConsole.log = function(message) { console.log(fileName + " " + new Date() + " " + message); } +//Задание 6 +var newRequire = function(module) { + fs.appendFile('file.txt', new Date() + " " + module + '\n', + (err) => { + if (err) + throw err; + }); + return require(module); +} + // Создаем контекст-песочницу, которая станет глобальным контекстом приложения var context = { module: {}, console: newConsole, setTimeout: setTimeout, - setInterval: setInterval, util: util }; + setInterval: setInterval, util: util, require: newRequire }; context.global = context; var sandbox = vm.createContext(context); From 2ce45d63a106e1cf79e2056842f736fa3c10e575 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Tue, 22 Mar 2016 22:30:28 +0200 Subject: [PATCH 10/13] Tasks 1-7 done --- sandboxedModule/ru/application.js | 35 ++++++++++++++++++------------- sandboxedModule/ru/framework.js | 6 ++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 4a8d16b..f9716ff 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -15,21 +15,28 @@ var Country = function (name, capital) { module.exports = function() { // Вывод из контекста экспортируемой функции console.log('From application exported function'); + +}; - //Задание 1 - setTimeout(() => { - console.log("setTimeout function"); - }, 3000); +//Задание 7 +module.exports.func = function() { + // Вывод из контекста экспортируемой функции + console.log('From application exported function'); + }; - setInterval(() => { - console.log("setInterval function"); - }, 1000); +module.exports.variable = 100; - //Задание 2 - var newCountry = new Country("Ukraine", "Kyiv"); - console.log(util.format("Call util.inspect() through util.format(): %s", - util.inspect(newCountry)) - ); - +//Задание 1 +setTimeout(() => { + console.log("setTimeout function"); + }, 3000); -}; +setInterval(() => { + console.log("setInterval function"); +}, 1000); + +//Задание 2 +var newCountry = new Country("Ukraine", "Kyiv"); +console.log(util.format("Call util.inspect() through util.format(): %s", + util.inspect(newCountry)) +); \ No newline at end of file diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index b52f09a..b3b660e 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -47,6 +47,12 @@ fs.readFile(fileName, function(err, src) { var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); + //Задание 7 + console.log("--------TASK 7--------"); + for (var tmp in sandbox.module.exports) { + console.log(tmp + " -> " + typeof sandbox.module.exports[tmp]); + } + sandbox.module.exports(); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. From 6d46458b791bd5469364010074333e0da83f5646 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Tue, 22 Mar 2016 22:41:32 +0200 Subject: [PATCH 11/13] Tasks 1-8 are done --- sandboxedModule/ru/application.js | 9 ++++----- sandboxedModule/ru/framework.js | 9 ++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index f9716ff..68e9003 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -18,11 +18,10 @@ module.exports = function() { }; -//Задание 7 -module.exports.func = function() { - // Вывод из контекста экспортируемой функции - console.log('From application exported function'); - }; +//Задания 7-8 +module.exports.func = function(argument, anotherArgum) { + console.log(argument); +}; module.exports.variable = 100; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index b3b660e..fbe209e 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -48,11 +48,18 @@ fs.readFile(fileName, function(err, src) { script.runInNewContext(sandbox); //Задание 7 - console.log("--------TASK 7--------"); + console.log("----------TASK 7----------"); for (var tmp in sandbox.module.exports) { console.log(tmp + " -> " + typeof sandbox.module.exports[tmp]); } + //Задание 8 + console.log("----------TASK 8----------"); + console.log(sandbox.module.exports.func.toString()); + console.log('Amount of arguments: '+ + sandbox.module.exports.func.toString(). + replace(/.+\(/, '').replace(/\)[^]+/, '').split(/, */).length); + sandbox.module.exports(); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. From dcdcccf7b338fac0b02164d6fc34d119231440e1 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Tue, 22 Mar 2016 23:00:31 +0200 Subject: [PATCH 12/13] Tasks 1-9 are done --- sandboxedModule/ru/application.js | 4 +++- sandboxedModule/ru/framework.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 68e9003..b1c4d79 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -15,7 +15,9 @@ var Country = function (name, capital) { module.exports = function() { // Вывод из контекста экспортируемой функции console.log('From application exported function'); - + + //Задание 9 + console.log(inspect(global, {colors: true})); }; //Задания 7-8 diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index fbe209e..15edd4f 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -31,7 +31,8 @@ var newRequire = function(module) { // Создаем контекст-песочницу, которая станет глобальным контекстом приложения var context = { module: {}, console: newConsole, setTimeout: setTimeout, - setInterval: setInterval, util: util, require: newRequire }; + setInterval: setInterval, util: util, require: newRequire, + inspect: util.inspect }; context.global = context; var sandbox = vm.createContext(context); @@ -58,9 +59,11 @@ fs.readFile(fileName, function(err, src) { console.log(sandbox.module.exports.func.toString()); console.log('Amount of arguments: '+ sandbox.module.exports.func.toString(). - replace(/.+\(/, '').replace(/\)[^]+/, '').split(/, */).length); + replace(/.+\(/, '').replace(/\)[^]+/, ''). + split(/, */).length); sandbox.module.exports(); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. + }); From 1a2fd603975328573e16974cc1b396af2d9fe9b6 Mon Sep 17 00:00:00 2001 From: YuriiKrat Date: Tue, 22 Mar 2016 23:18:22 +0200 Subject: [PATCH 13/13] All tasks are done --- sandboxedModule/ru/framework.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 15edd4f..d600ef2 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -36,6 +36,10 @@ var context = { module: {}, console: newConsole, setTimeout: setTimeout, context.global = context; var sandbox = vm.createContext(context); +var keys = {}; +for (var tmp in sandbox) + keys[tmp] = sandbox[tmp]; + // Читаем исходный код приложения из файла // Задание 3 var fileName = process.argv[2] == null?'./application.js':process.argv[2]; @@ -48,10 +52,28 @@ fs.readFile(fileName, function(err, src) { var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); + //Задание 10 + console.log("----------TASK 10----------"); + var newKeys = {}; + for (var tmp in sandbox) + newKeys[tmp] = sandbox[tmp]; + + console.log("New keys:"); + for (var tmp in newKeys) { + if (!(tmp in keys)) + console.log(tmp); + } + + console.log("Deleted keys:"); + for (var tmp in keys) { + if (!(tmp in newKeys)) + console.log(tmp); + } + //Задание 7 console.log("----------TASK 7----------"); - for (var tmp in sandbox.module.exports) { - console.log(tmp + " -> " + typeof sandbox.module.exports[tmp]); + for (var tmp in sandbox.module.exports) { + console.log(tmp + " -> " + typeof sandbox.module.exports[tmp]); } //Задание 8