File tree Expand file tree Collapse file tree 7 files changed +121
-1
lines changed Expand file tree Collapse file tree 7 files changed +121
-1
lines changed Original file line number Diff line number Diff line change
1
+ ## Description
2
+
3
+ * ` framework.js ` - small piece of the framework, just to demonstrate wrapper
4
+ * ` application.js ` - small piece of the application for wrapper demonstration
5
+
6
+ ## How to execute
7
+
8
+ From the command line, type: ` node application ` then ` node framework ` , compare
9
+ output, read code in ` framework.js ` and ` application.js ` , try to understand
10
+ how wrapper works for setTimeout
11
+
12
+ ## Tasks
Original file line number Diff line number Diff line change
1
+ // Print from the global context of application module
2
+ console . log ( 'From application global context' ) ;
3
+
4
+ // Declare function for timer event
5
+ function timerEvent ( ) {
6
+ console . log ( 'From application timer event' ) ;
7
+ }
8
+
9
+ // Set function to timer event for 1sec
10
+ setTimeout ( timerEvent , 1000 ) ;
Original file line number Diff line number Diff line change
1
+ // Example: function wrapper in sandboxed context
2
+
3
+ var fs = require ( 'fs' ) ,
4
+ vm = require ( 'vm' ) ;
5
+
6
+ // Declare dictionary to convert it into sandboxed context
7
+ var context = {
8
+ module : { } ,
9
+ console : console ,
10
+ // Declare wrapper function for setTimeout
11
+ setTimeout : function ( callback , timeout ) {
12
+ // Add some behaviour for setTimeout
13
+ console . log (
14
+ 'Call: setTimeout, ' +
15
+ 'callback function: ' + callback . name + ', ' +
16
+ 'timeout: ' + timeout
17
+ ) ;
18
+ setTimeout ( function ( ) {
19
+ // Add some behaviour on event
20
+ console . log ( 'Event: setTimeout, before callback' ) ;
21
+ // Call of orignal user-defined callback, passed to wrapper
22
+ callback ( ) ;
23
+ console . log ( 'Event: setTimeout, after callback' ) ;
24
+ } , timeout ) ;
25
+ }
26
+ } ;
27
+
28
+ // Convert collection into sandboxed context
29
+ context . global = context ;
30
+ var sandbox = vm . createContext ( context ) ;
31
+
32
+ // Read an application source code from the file
33
+ var fileName = './application.js' ;
34
+ fs . readFile ( fileName , function ( err , src ) {
35
+ // Run an application in sandboxed context
36
+ var script = vm . createScript ( src , fileName ) ;
37
+ script . runInNewContext ( sandbox ) ;
38
+ } ) ;
Original file line number Diff line number Diff line change
1
+ ## Описание
2
+
3
+ * ` framework.js ` - кусочек фреймворка, демонстрирующий обертку (wrapper)
4
+ * ` application.js ` - часть приложения для демонстрации обертки
5
+
6
+ ## Запуск
7
+
8
+ Из командной строки пишем ` node application ` , а потом ` node framework ` ,
9
+ сравниваем вывод, смотрим код ` framework.js ` и ` application.js ` , понимаем
10
+ как работает обертка вокруг setTimeout
11
+
12
+ ## Задания
Original file line number Diff line number Diff line change
1
+ // Вывод из глобального контекста модуля
2
+ console . log ( 'From application global context' ) ;
3
+
4
+ // Объявляем функцию для события таймера
5
+ function timerEvent ( ) {
6
+ console . log ( 'From application timer event' ) ;
7
+ }
8
+
9
+ // Устанавливаем функцию на таймер
10
+ setTimeout ( timerEvent , 1000 ) ;
Original file line number Diff line number Diff line change
1
+ // Пример оборачивания функции в песочнице
2
+
3
+ var fs = require ( 'fs' ) ,
4
+ vm = require ( 'vm' ) ;
5
+
6
+ // Объявляем хеш из которого сделаем контекст-песочницу
7
+ var context = {
8
+ module : { } ,
9
+ console : console ,
10
+ // Оборачиваем функцию setTimeout в песочнице
11
+ setTimeout : function ( callback , timeout ) {
12
+ // Добавляем поведение при вызове setTimeout
13
+ console . log (
14
+ 'Call: setTimeout, ' +
15
+ 'callback function: ' + callback . name + ', ' +
16
+ 'timeout: ' + timeout
17
+ ) ;
18
+ setTimeout ( function ( ) {
19
+ // Добавляем поведение при срабатывании таймера
20
+ console . log ( 'Event: setTimeout, before callback' ) ;
21
+ // Вызываем функцию пользователя на событии таймера
22
+ callback ( ) ;
23
+ console . log ( 'Event: setTimeout, after callback' ) ;
24
+ } , timeout ) ;
25
+ }
26
+ } ;
27
+
28
+ // Преобразовываем хеш в контекст
29
+ context . global = context ;
30
+ var sandbox = vm . createContext ( context ) ;
31
+
32
+ // Читаем исходный код приложения из файла
33
+ var fileName = './application.js' ;
34
+ fs . readFile ( fileName , function ( err , src ) {
35
+ // Запускаем код приложения в песочнице
36
+ var script = vm . createScript ( src , fileName ) ;
37
+ script . runInNewContext ( sandbox ) ;
38
+ } ) ;
Original file line number Diff line number Diff line change 7
7
var fs = require ( 'fs' ) ,
8
8
vm = require ( 'vm' ) ;
9
9
10
- // Чоздаем контекст-песочницу, которая станет глобальным контекстом приложения
10
+ // Создаем контекст-песочницу, которая станет глобальным контекстом приложения
11
11
var context = { module : { } , console : console } ;
12
12
context . global = context ;
13
13
var sandbox = vm . createContext ( context ) ;
You can’t perform that action at this time.
0 commit comments