From 50a34058dd6317a69ae474ea214e0e662cdb35f1 Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Fri, 26 Feb 2016 11:31:03 +0200 Subject: [PATCH 01/10] Test adding setTimeout --- sandboxedModule/ru/application.js | 3 +++ sandboxedModule/ru/framework.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index b66eff8..3a94f37 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -5,6 +5,9 @@ // Вывод из глобального контекста модуля console.log('From application global context'); +setTimeout(function(){ + console.log("Some message"); +}, 1000); module.exports = function() { // Вывод из контекста экспортируемой функции console.log('From application exported function'); diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 76465cc..faaa3c1 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 }; context.global = context; var sandbox = vm.createContext(context); From 6f745725e036ae030987d9429f9c8becf62b09d3 Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Wed, 9 Mar 2016 21:33:25 +0200 Subject: [PATCH 02/10] Added setInterval --- sandboxedModule/ru/application.js | 11 ++++++++++- sandboxedModule/ru/framework.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 3a94f37..84b7698 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -6,8 +6,17 @@ console.log('From application global context'); setTimeout(function(){ - console.log("Some message"); + console.log("Some message1"); }, 1000); + +var intervalID01 = setInterval(function(){ + console.log("Some message2"); +}, 1000); + +setTimeout(function(){ + clearInterval(intervalID01); +}, 10000); + module.exports = function() { // Вывод из контекста экспортируемой функции console.log('From application exported function'); diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index a75efc6..73bcab0 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, setTimeout: setTimeout }; +var context = { module: {}, console: console, setTimeout: setTimeout, setInterval: setInterval, clearInterval: clearInterval}; context.global = context; var sandbox = vm.createContext(context); From 7ea1687d4823bd448154bcaf7d6f71c56cdf5000 Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Thu, 10 Mar 2016 00:35:16 +0200 Subject: [PATCH 03/10] Added util library --- sandboxedModule/ru/application.js | 3 ++- sandboxedModule/ru/framework.js | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 84b7698..6eb5a0e 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -7,10 +7,11 @@ console.log('From application global context'); setTimeout(function(){ console.log("Some message1"); + util.log("Some message2"); }, 1000); var intervalID01 = setInterval(function(){ - console.log("Some message2"); + console.log("Some message3"); }, 1000); setTimeout(function(){ diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 73bcab0..89d55c1 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -5,10 +5,15 @@ // Фреймворк может явно зависеть от библиотек через dependency lookup var fs = require('fs'), - vm = require('vm'); + vm = require('vm') + util = require('util'); // Чоздаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console, setTimeout: setTimeout, setInterval: setInterval, clearInterval: clearInterval}; +var context = { module: {}, console: console, + setTimeout: setTimeout, + setInterval: setInterval, + clearInterval: clearInterval, + util: util}; context.global = context; var sandbox = vm.createContext(context); From 3b2d0a86f7c943afc64672de89311e72bcbe132f Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Thu, 10 Mar 2016 12:42:19 +0200 Subject: [PATCH 04/10] Added custom console.log and multiple applications --- sandboxedModule/ru/application.js | 3 ++- sandboxedModule/ru/framework.js | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 6eb5a0e..deefee1 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -8,6 +8,7 @@ console.log('From application global context'); setTimeout(function(){ console.log("Some message1"); util.log("Some message2"); + util.format('%s:%s', 'foo', 'bar', 'baz'); }, 1000); var intervalID01 = setInterval(function(){ @@ -16,7 +17,7 @@ var intervalID01 = setInterval(function(){ setTimeout(function(){ clearInterval(intervalID01); -}, 10000); +}, 5000); module.exports = function() { // Вывод из контекста экспортируемой функции diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 89d55c1..c6f5247 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -5,11 +5,24 @@ // Фреймворк может явно зависеть от библиотек через dependency lookup var fs = require('fs'), - vm = require('vm') + vm = require('vm'), util = require('util'); +var myConsole = {}; +myConsole.log = function (argument) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(fileName+" "+time+" "+argument); +} +/*var exlog = console.log; +console.log = function(argument) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + exlog(fileName+" "+time+" "+argument); +}*/ // Чоздаем контекст-песочницу, которая станет глобальным контекстом приложения -var context = { module: {}, console: console, +var context = { module: {}, console: myConsole, setTimeout: setTimeout, setInterval: setInterval, clearInterval: clearInterval, @@ -19,7 +32,7 @@ context.global = context; var sandbox = vm.createContext(context); // Читаем исходный код приложения из файла -var fileName = './application.js'; +var fileName = process.argv[2] || './application.js'; fs.readFile(fileName, function(err, src) { // Тут нужно обработать ошибки From c4cdd74c6649495f39855c465b356401603d947d Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Thu, 10 Mar 2016 13:04:43 +0200 Subject: [PATCH 05/10] Added output into a file --- sandboxedModule/ru/console.txt | 6 ++++++ sandboxedModule/ru/framework.js | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 sandboxedModule/ru/console.txt diff --git a/sandboxedModule/ru/console.txt b/sandboxedModule/ru/console.txt new file mode 100644 index 0000000..4f1aa7a --- /dev/null +++ b/sandboxedModule/ru/console.txt @@ -0,0 +1,6 @@ +./application.js 13:1:16 From application global context +./application.js 13:1:17 Some message1 +./application.js 13:1:17 Some message3 +./application.js 13:1:18 Some message3 +./application.js 13:1:19 Some message3 +./application.js 13:1:20 Some message3 diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index c6f5247..027d91c 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -14,6 +14,10 @@ myConsole.log = function (argument) { var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); console.log(fileName+" "+time+" "+argument); + fs.appendFile("console.txt",fileName+" "+time+" "+argument+"\n", + function (err){ + if(err) throw err; + }); } /*var exlog = console.log; console.log = function(argument) { From 2b429869656377177b9a2e0b0f7e3c84946d3ee4 Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Thu, 10 Mar 2016 13:33:54 +0200 Subject: [PATCH 06/10] Added a link to require function to the application --- sandboxedModule/ru/application.js | 1 + sandboxedModule/ru/console.txt | 15 +++++++++++++++ sandboxedModule/ru/framework.js | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index deefee1..6ab8079 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -4,6 +4,7 @@ // Вывод из глобального контекста модуля console.log('From application global context'); +var path = require('path'); setTimeout(function(){ console.log("Some message1"); diff --git a/sandboxedModule/ru/console.txt b/sandboxedModule/ru/console.txt index 4f1aa7a..e724bd3 100644 --- a/sandboxedModule/ru/console.txt +++ b/sandboxedModule/ru/console.txt @@ -4,3 +4,18 @@ ./application.js 13:1:18 Some message3 ./application.js 13:1:19 Some message3 ./application.js 13:1:20 Some message3 +./application.js 13:22:31 From application global context +13:22:31 path is required. +./application.js 13:22:32 Some message1 +./application.js 13:22:32 Some message3 +./application.js 13:22:33 Some message3 +./application.js 13:22:34 Some message3 +./application.js 13:22:35 Some message3 +13:23:32 path is required. +./application.js 13:24:57 From application global context +13:24:57 path is required. +./application.js 13:24:58 Some message1 +./application.js 13:24:58 Some message3 +./application.js 13:24:59 Some message3 +./application.js 13:25:0 Some message3 +./application.js 13:25:1 Some message3 diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 027d91c..227098f 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -11,7 +11,7 @@ var fs = require('fs'), var myConsole = {}; myConsole.log = function (argument) { var date = new Date(); - var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); console.log(fileName+" "+time+" "+argument); fs.appendFile("console.txt",fileName+" "+time+" "+argument+"\n", @@ -32,6 +32,19 @@ var context = { module: {}, console: myConsole, clearInterval: clearInterval, util: util}; +context.require = function (module) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(time+" "+module+" is required."); + fs.appendFile("console.txt",time+" "+module+" is required.\n", + function (err){ + if(err) throw err; + }); + return require(module); +} + + context.global = context; var sandbox = vm.createContext(context); From 64b5fc5eb8d5bab33ef0228a1c466a522b35c01f Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Thu, 10 Mar 2016 15:17:12 +0200 Subject: [PATCH 07/10] Added printing a list of application global context --- sandboxedModule/ru/application.js | 10 +- sandboxedModule/ru/console.txt | 672 ++++++++++++++++++++++++++++++ sandboxedModule/ru/framework.js | 12 +- 3 files changed, 685 insertions(+), 9 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 6ab8079..ef1bbcc 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -4,12 +4,16 @@ // Вывод из глобального контекста модуля console.log('From application global context'); + +for (var i in global){ + console.log(i +" "+ typeof global[i]); +} + var path = require('path'); setTimeout(function(){ console.log("Some message1"); - util.log("Some message2"); - util.format('%s:%s', 'foo', 'bar', 'baz'); + util.log("Some message using util.log"); }, 1000); var intervalID01 = setInterval(function(){ @@ -18,7 +22,7 @@ var intervalID01 = setInterval(function(){ setTimeout(function(){ clearInterval(intervalID01); -}, 5000); +}, 3000); module.exports = function() { // Вывод из контекста экспортируемой функции diff --git a/sandboxedModule/ru/console.txt b/sandboxedModule/ru/console.txt index e724bd3..ca69e51 100644 --- a/sandboxedModule/ru/console.txt +++ b/sandboxedModule/ru/console.txt @@ -19,3 +19,675 @@ ./application.js 13:24:59 Some message3 ./application.js 13:25:0 Some message3 ./application.js 13:25:1 Some message3 +./application.js 14:32:20 From application global context +14:32:20 path is required. +./application.js 14:32:21 Some message1 +./application.js 14:32:21 Some message3 +./application.js 14:32:22 Some message3 +./application.js 14:32:23 Some message3 +./application.js 14:32:24 Some message3 +./application.js 14:34:40 From application global context +14:34:40 path is required. +./application.js 14:34:41 Some message3 +./application.js 14:34:41 Some message1 +./application.js 14:34:42 Some message3 +./application.js 14:34:43 Some message3 +./application.js 14:34:44 Some message3 +./application.js 14:40:6 From application global context +14:40:6 path is required. +./application.js 14:40:6 [object global] +./application.js 14:40:7 Some message1 +./application.js 14:40:7 Some message3 +./application.js 14:40:8 Some message3 +./application.js 14:40:9 Some message3 +./application.js 14:40:10 Some message3 +./application.js 14:49:49 From application global context +14:49:49 path is required. +./application.js 14:49:49 [object global] +./application.js 14:49:50 Some message1 +./application.js 14:49:50 Some message3 +./application.js 14:49:51 Some message3 +./application.js 14:49:52 Some message3 +./application.js 14:49:53 Some message3 +./application.js 14:50:5 From application global context +14:50:5 path is required. +./application.js 14:50:5 [object global] +./application.js 14:50:6 Some message1 +./application.js 14:50:6 Some message3 +./application.js 14:50:7 Some message3 +./application.js 14:50:8 Some message3 +./application.js 14:50:9 Some message3 +./application.js 14:50:19 From application global context +14:50:19 path is required. +./application.js 14:50:19 [object global] +./application.js 14:50:20 Some message1 +./application.js 14:50:20 Some message3 +./application.js 14:50:21 Some message3 +./application.js 14:52:54 From application global context +14:52:54 path is required. +./application.js 14:52:54 [object Object] +./application.js 14:52:54 [object Object] +./application.js 14:52:54 i +./application.js 14:52:54 [object Object] +./application.js 14:52:54 [object Object] +./application.js 14:52:54 function (callback, after) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { + after = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(after); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + // fast cases + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + // slow case + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i++) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = ontimeout; + + if (process.domain) timer.domain = process.domain; + + exports.active(timer); + + return timer; +} +./application.js 14:52:54 function (timer) { + if (timer && timer._repeat) { + timer._repeat = null; + clearTimeout(timer); + } +} +./application.js 14:52:54 function (callback, repeat) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { + repeat = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(repeat); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i += 1) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = wrapper; + timer._repeat = ontimeout; + + if (process.domain) timer.domain = process.domain; + exports.active(timer); + + return timer; + + function wrapper() { + timer._repeat(); + + // Timer might be closed - no point in restarting it + if (!timer._repeat) + return; + + // If timer is unref'd (or was - it's permanently removed from the list.) + if (this._handle) { + this._handle.start(repeat, 0); + } else { + timer._idleTimeout = repeat; + exports.active(timer); + } + } +} +./application.js 14:52:54 function (module) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(time+" "+module+" is required."); + fs.appendFile("console.txt",time+" "+module+" is required.\n", + function (err){ + if(err) throw err; + }); + return require(module); +} +./application.js 14:52:54 [object Object] +./application.js 14:52:54 [object global] +./application.js 14:52:55 Some message1 +./application.js 14:52:55 Some message3 +./application.js 14:52:56 Some message3 +./application.js 14:54:20 From application global context +14:54:20 path is required. +./application.js 14:54:21 Some message3 +./application.js 14:54:21 Some message1 +./application.js 14:54:22 Some message3 +./application.js 14:54:24 [object Object] +./application.js 14:54:24 [object Object] +./application.js 14:54:24 [object Object] +./application.js 14:54:24 function (callback, repeat) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { + repeat = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(repeat); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i += 1) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = wrapper; + timer._repeat = ontimeout; + + if (process.domain) timer.domain = process.domain; + exports.active(timer); + + return timer; + + function wrapper() { + timer._repeat(); + + // Timer might be closed - no point in restarting it + if (!timer._repeat) + return; + + // If timer is unref'd (or was - it's permanently removed from the list.) + if (this._handle) { + this._handle.start(repeat, 0); + } else { + timer._idleTimeout = repeat; + exports.active(timer); + } + } +} +./application.js 14:54:24 function (callback, after) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { + after = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(after); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + // fast cases + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + // slow case + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i++) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = ontimeout; + + if (process.domain) timer.domain = process.domain; + + exports.active(timer); + + return timer; +} +./application.js 14:54:24 [object Object] +./application.js 14:54:24 function (timer) { + if (timer && timer._repeat) { + timer._repeat = null; + clearTimeout(timer); + } +} +./application.js 14:54:24 function (module) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(time+" "+module+" is required."); + fs.appendFile("console.txt",time+" "+module+" is required.\n", + function (err){ + if(err) throw err; + }); + return require(module); +} +./application.js 14:54:24 [object Object] +./application.js 14:54:24 [object global] +./application.js 14:59:9 From application global context +14:59:9 path is required. +./application.js 14:59:10 Some message1 +./application.js 14:59:10 Some message3 +./application.js 14:59:11 Some message3 +./application.js 14:59:13 true +./application.js 14:59:13 4000 +./application.js 14:59:13 null +./application.js 14:59:13 null +./application.js 14:59:13 139 +./application.js 14:59:13 function (){ + for (var i in this){ + console.log(this[i]); + } +} +./application.js 14:59:13 null +./application.js 14:59:13 function () { + if (this._handle) { + this._handle.unref(); + } else if (typeof this._onTimeout === 'function') { + var now = Timer.now(); + if (!this._idleStart) this._idleStart = now; + var delay = this._idleStart + this._idleTimeout - now; + if (delay < 0) delay = 0; + + // Prevent running cb again when unref() is called during the same cb + if (this._called && !this._repeat) { + exports.unenroll(this); + return; + } + + var handle = reuse(this); + + this._handle = handle || new Timer(); + this._handle.owner = this; + this._handle[kOnTimeout] = unrefdHandle; + this._handle.start(delay, 0); + this._handle.domain = this.domain; + this._handle.unref(); + } + return this; +} +./application.js 14:59:13 function () { + if (this._handle) + this._handle.ref(); + return this; +} +./application.js 14:59:13 function () { + this._onTimeout = null; + if (this._handle) { + this._handle[kOnTimeout] = null; + this._handle.close(); + } else { + exports.unenroll(this); + } + return this; +} +./application.js 15:2:28 From application global context +./application.js 15:2:28 i +./application.js 15:2:28 undefined +./application.js 15:2:28 undefined +./application.js 15:2:28 [object Object] +./application.js 15:2:28 [object Object] +./application.js 15:2:28 function (callback, after) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { + after = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(after); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + // fast cases + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + // slow case + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i++) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = ontimeout; + + if (process.domain) timer.domain = process.domain; + + exports.active(timer); + + return timer; +} +./application.js 15:2:28 function (callback, repeat) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { + repeat = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(repeat); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i += 1) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = wrapper; + timer._repeat = ontimeout; + + if (process.domain) timer.domain = process.domain; + exports.active(timer); + + return timer; + + function wrapper() { + timer._repeat(); + + // Timer might be closed - no point in restarting it + if (!timer._repeat) + return; + + // If timer is unref'd (or was - it's permanently removed from the list.) + if (this._handle) { + this._handle.start(repeat, 0); + } else { + timer._idleTimeout = repeat; + exports.active(timer); + } + } +} +./application.js 15:2:28 function (timer) { + if (timer && timer._repeat) { + timer._repeat = null; + clearTimeout(timer); + } +} +./application.js 15:2:28 function (module) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(time+" "+module+" is required."); + fs.appendFile("console.txt",time+" "+module+" is required.\n", + function (err){ + if(err) throw err; + }); + return require(module); +} +./application.js 15:2:28 [object Object] +./application.js 15:2:28 [object global] +15:2:28 path is required. +./application.js 15:2:29 Some message1 +./application.js 15:2:29 Some message3 +./application.js 15:2:30 Some message3 +./application.js 15:6:20 From application global context +./application.js 15:6:20 i +./application.js 15:6:20 path +./application.js 15:6:20 intervalID01 +./application.js 15:6:20 module +./application.js 15:6:20 console +./application.js 15:6:20 setTimeout +./application.js 15:6:20 setInterval +./application.js 15:6:20 clearInterval +./application.js 15:6:20 util +./application.js 15:6:20 require +./application.js 15:6:20 global +15:6:20 path is required. +./application.js 15:6:21 Some message1 +./application.js 15:6:21 Some message3 +./application.js 15:6:22 Some message3 +./application.js 15:7:6 From application global context +./application.js 15:7:6 i string i +./application.js 15:7:6 path undefined undefined +./application.js 15:7:6 intervalID01 undefined undefined +./application.js 15:7:6 module object [object Object] +./application.js 15:7:6 console object [object Object] +./application.js 15:7:6 setTimeout function function (callback, after) { + after *= 1; // coalesce to number or NaN + + if (!(after >= 1 && after <= TIMEOUT_MAX)) { + after = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(after); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + // fast cases + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + // slow case + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i++) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = ontimeout; + + if (process.domain) timer.domain = process.domain; + + exports.active(timer); + + return timer; +} +./application.js 15:7:6 setInterval function function (callback, repeat) { + repeat *= 1; // coalesce to number or NaN + + if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) { + repeat = 1; // schedule on next tick, follows browser behaviour + } + + var timer = new Timeout(repeat); + var length = arguments.length; + var ontimeout = callback; + switch (length) { + case 0: + case 1: + case 2: + break; + case 3: + ontimeout = () => callback.call(timer, arguments[2]); + break; + case 4: + ontimeout = () => callback.call(timer, arguments[2], arguments[3]); + break; + case 5: + ontimeout = + () => callback.call(timer, arguments[2], arguments[3], arguments[4]); + break; + default: + var args = new Array(length - 2); + for (var i = 2; i < length; i += 1) + args[i - 2] = arguments[i]; + ontimeout = () => callback.apply(timer, args); + break; + } + timer._onTimeout = wrapper; + timer._repeat = ontimeout; + + if (process.domain) timer.domain = process.domain; + exports.active(timer); + + return timer; + + function wrapper() { + timer._repeat(); + + // Timer might be closed - no point in restarting it + if (!timer._repeat) + return; + + // If timer is unref'd (or was - it's permanently removed from the list.) + if (this._handle) { + this._handle.start(repeat, 0); + } else { + timer._idleTimeout = repeat; + exports.active(timer); + } + } +} +./application.js 15:7:6 util object [object Object] +./application.js 15:7:6 require function function (module) { + var date = new Date(); + var time = date.getHours()+':'+date.getMinutes()+':'+date.getSeconds(); + + console.log(time+" "+module+" is required."); + fs.appendFile("console.txt",time+" "+module+" is required.\n", + function (err){ + if(err) throw err; + }); + return require(module); +} +./application.js 15:7:6 clearInterval function function (timer) { + if (timer && timer._repeat) { + timer._repeat = null; + clearTimeout(timer); + } +} +./application.js 15:7:6 global object [object global] +15:7:6 path is required. +./application.js 15:7:7 Some message1 +./application.js 15:7:7 Some message3 +./application.js 15:7:8 Some message3 +./application.js 15:8:22 From application global context +./application.js 15:8:22 i string +./application.js 15:8:22 path undefined +./application.js 15:8:22 intervalID01 undefined +./application.js 15:8:22 module object +./application.js 15:8:22 console object +./application.js 15:8:22 setTimeout function +./application.js 15:8:22 setInterval function +./application.js 15:8:22 util object +./application.js 15:8:22 clearInterval function +./application.js 15:8:22 require function +./application.js 15:8:22 global object +15:8:22 path is required. +./application.js 15:8:23 Some message1 +./application.js 15:8:23 Some message3 +./application.js 15:8:24 Some message3 +./application.js 15:10:51 From application global context +./application.js 15:10:51 i string +./application.js 15:10:51 path undefined +./application.js 15:10:51 intervalID01 undefined +./application.js 15:10:51 module object +./application.js 15:10:51 console object +./application.js 15:10:51 setTimeout function +./application.js 15:10:51 setInterval function +./application.js 15:10:51 clearInterval function +./application.js 15:10:51 util object +./application.js 15:10:51 require function +./application.js 15:10:51 global object +15:10:51 path is required. +./application.js 15:10:52 Some message1 +./application.js 15:10:52 Some message3 +./application.js 15:10:53 Some message3 +./application.js 15:11:44 From application global context +./application.js 15:11:44 i string +./application.js 15:11:44 path undefined +./application.js 15:11:44 intervalID01 undefined +./application.js 15:11:44 module object +./application.js 15:11:44 console object +./application.js 15:11:44 setTimeout function +./application.js 15:11:44 setInterval function +./application.js 15:11:44 clearInterval function +./application.js 15:11:44 util object +./application.js 15:11:44 require function +./application.js 15:11:44 global object +15:11:44 path is required. +./application.js 15:11:45 Some message1 +./application.js 15:11:45 Some message3 +./application.js 15:11:46 Some message3 diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 227098f..83dedce 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -5,8 +5,8 @@ // Фреймворк может явно зависеть от библиотек через dependency lookup var fs = require('fs'), - vm = require('vm'), - util = require('util'); +vm = require('vm'), +util = require('util'); var myConsole = {}; myConsole.log = function (argument) { @@ -27,10 +27,10 @@ console.log = function(argument) { }*/ // Чоздаем контекст-песочницу, которая станет глобальным контекстом приложения var context = { module: {}, console: myConsole, - setTimeout: setTimeout, - setInterval: setInterval, - clearInterval: clearInterval, - util: util}; +setTimeout: setTimeout, +setInterval: setInterval, +clearInterval: clearInterval, +util: util}; context.require = function (module) { var date = new Date(); From 4b85f881d880043fa1f352082851a003c787c60f Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Wed, 23 Mar 2016 21:24:04 +0200 Subject: [PATCH 08/10] Added exported hash and function --- sandboxedModule/ru/application.js | 11 ++++++++--- sandboxedModule/ru/framework.js | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index ef1bbcc..7f1c9b9 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -12,19 +12,24 @@ for (var i in global){ var path = require('path'); setTimeout(function(){ - console.log("Some message1"); + console.log("Some message using setTimeout"); util.log("Some message using util.log"); }, 1000); var intervalID01 = setInterval(function(){ - console.log("Some message3"); + console.log("Some message using setInterval"); }, 1000); setTimeout(function(){ clearInterval(intervalID01); }, 3000); +var exportedStr = 'str'; module.exports = function() { // Вывод из контекста экспортируемой функции - console.log('From application exported function'); + module.exports.exportedStr = exportedStr; + module.exports.exportedFunc = function(arg) { + return arguments.length; +}; +console.log('From application exported function'); }; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index 83dedce..dbdfc2e 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -57,6 +57,13 @@ fs.readFile(fileName, function(err, src) { var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); + sandbox.module.exports(); + for (var i in sandbox.module.exports) { + console.log(i + " " + typeof sandbox.module.exports[i]); + } + console.log(sandbox.module.exports.exportedFunc.toString()); + console.log("Number of arguments: "+ + sandbox.module.exports.exportedFunc(2,3,3)); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. -}); +}); \ No newline at end of file From f7922cc690e48b20f9d98b4cac4f2f1ea92a8e24 Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Wed, 23 Mar 2016 22:02:07 +0200 Subject: [PATCH 09/10] Added comparison of application sandbox content --- sandboxedModule/ru/application.js | 6 +++--- sandboxedModule/ru/framework.js | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/sandboxedModule/ru/application.js b/sandboxedModule/ru/application.js index 7f1c9b9..11a8619 100644 --- a/sandboxedModule/ru/application.js +++ b/sandboxedModule/ru/application.js @@ -29,7 +29,7 @@ module.exports = function() { // Вывод из контекста экспортируемой функции module.exports.exportedStr = exportedStr; module.exports.exportedFunc = function(arg) { - return arguments.length; -}; -console.log('From application exported function'); + return arguments.length; + }; + console.log('From application exported function'); }; diff --git a/sandboxedModule/ru/framework.js b/sandboxedModule/ru/framework.js index dbdfc2e..3d936e2 100644 --- a/sandboxedModule/ru/framework.js +++ b/sandboxedModule/ru/framework.js @@ -48,6 +48,8 @@ context.require = function (module) { context.global = context; var sandbox = vm.createContext(context); +var keysBefore = clone(sandbox.global); + // Читаем исходный код приложения из файла var fileName = process.argv[2] || './application.js'; fs.readFile(fileName, function(err, src) { @@ -56,7 +58,9 @@ fs.readFile(fileName, function(err, src) { // Запускаем код приложения в песочнице var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); - + + compareKeys(); + sandbox.module.exports(); for (var i in sandbox.module.exports) { console.log(i + " " + typeof sandbox.module.exports[i]); @@ -66,4 +70,22 @@ fs.readFile(fileName, function(err, src) { sandbox.module.exports.exportedFunc(2,3,3)); // Забираем ссылку из sandbox.module.exports, можем ее исполнить, // сохранить в кеш, вывести на экран исходный код приложения и т.д. -}); \ No newline at end of file +}); + +function clone(obj) { + if (null == obj || "object" != typeof obj) return obj; + var copy = obj.constructor(); + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; + } + return copy; +} + +function compareKeys(){ + for(var i in keysBefore){ + if(!(i in sandbox.global)) console.log("Deleted: "+i); + } + for(var i in sandbox.global){ + if(!(i in keysBefore)) console.log("Added: "+i); + } +} \ No newline at end of file From f8700b684c562ea5ecf0f6b9b93accf6de24294f Mon Sep 17 00:00:00 2001 From: Dreadful0 Date: Wed, 20 Apr 2016 22:28:58 +0300 Subject: [PATCH 10/10] Lab2 --- interfaceWrapper/ru/application.js | 24 ++++++++--- interfaceWrapper/ru/framework.js | 64 +++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/interfaceWrapper/ru/application.js b/interfaceWrapper/ru/application.js index 90d20d9..2585873 100644 --- a/interfaceWrapper/ru/application.js +++ b/interfaceWrapper/ru/application.js @@ -1,10 +1,22 @@ // Вывод из глобального контекста модуля console.log('From application global context'); -// Объявляем функцию для события таймера -function timerEvent() { - console.log('From application timer event'); -} +var fileName = './README.md'; +var fileToWrite = './test.txt'; -// Устанавливаем функцию на таймер -setTimeout(timerEvent, 1000); +console.log('Application going to read ' + fileName); +fs.readFile(fileName, function(err, src) { + console.log('File ' + fileName + ' size ' + src.length); +}); + +var intervalID01 = setInterval(function () { + console.log('Application going to write ' + fileToWrite); + fs.appendFile(fileToWrite, 'some text\n', + function (err){ + if(err) throw err; + }); +}, 1000); + +setTimeout(function(){ + clearInterval(intervalID01); +}, 10000); \ No newline at end of file diff --git a/interfaceWrapper/ru/framework.js b/interfaceWrapper/ru/framework.js index 25b3fce..a16092d 100644 --- a/interfaceWrapper/ru/framework.js +++ b/interfaceWrapper/ru/framework.js @@ -1,32 +1,24 @@ // Пример оборачивания функции в песочнице var fs = require('fs'), - vm = require('vm'); +vm = require('vm'); +var callbacks = 0; // Объявляем хеш из которого сделаем контекст-песочницу var context = { module: {}, console: console, // Помещаем ссылку на fs API в песочницу - fs: fs, - // Оборачиваем функцию setTimeout в песочнице - setTimeout: function(callback, timeout) { - // Добавляем поведение при вызове setTimeout - console.log( - 'Call: setTimeout, ' + - 'callback function: ' + callback.name + ', ' + - 'timeout: ' + timeout - ); - setTimeout(function() { - // Добавляем поведение при срабатывании таймера - console.log('Event: setTimeout, before callback'); - // Вызываем функцию пользователя на событии таймера - callback(); - console.log('Event: setTimeout, after callback'); - }, timeout); - } + fs: wrap(fs), + setInterval: setInterval, + setTimeout: setTimeout, + clearInterval: clearInterval }; +setInterval(function() { + console.log("=========== Callbacks: " + callbacks + "=========="); +}, 5000); + // Преобразовываем хеш в контекст context.global = context; var sandbox = vm.createContext(context); @@ -38,3 +30,39 @@ fs.readFile(fileName, function(err, src) { var script = vm.createScript(src, fileName); script.runInNewContext(sandbox); }); + +function cloneInterface(anInterface) { + var clone = {}; + for (var key in anInterface) { + clone[key] = anInterface[key]; + //clone[key] = wrapFunction(key, anInterface[key]); + } + return clone; +} + +function wrapFunction(fnName, fn) { + return function wrapper() { + var args = []; + Array.prototype.push.apply(args, arguments); + console.log('Call: ' + fnName); + //console.log('Args length: ' + args.length); + console.dir(args); + + if (typeof args[args.length - 1] == "function") { + callbacks++; + args[args.length - 1] = wrapFunction('callback', args[args.length - 1]); + } + + return fn.apply(undefined, args); + } + } + + function wrap(module) { + var wrappedModule = cloneInterface(module); + for(var i in wrappedModule) { + if(typeof wrappedModule[i] === 'function') { + wrappedModule[i] = wrapFunction(i, wrappedModule[i]); + } + } + return wrappedModule; + } \ No newline at end of file