Skip to content

Commit 46cae1d

Browse files
committed
convert Sandbox and VMSandbox to es6 class
1 parent b524a03 commit 46cae1d

File tree

3 files changed

+99
-96
lines changed

3 files changed

+99
-96
lines changed

src/sandbox.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
1+
'use strict';
2+
13
var chalk = require('chalk');
24

3-
function Sandbox() {
4-
}
5+
class Sandbox {
56

6-
Sandbox.prototype.init = function(options) {
7-
this.globals = options.globals;
8-
this.sandbox = this.buildSandbox();
9-
};
10-
11-
Sandbox.prototype.buildSandbox = function() {
12-
var console = this.buildWrappedConsole();
13-
var sourceMapSupport = require('./install-source-map-support');
14-
var URL = require('url');
15-
var globals = this.globals;
16-
17-
var sandbox = {
18-
sourceMapSupport: sourceMapSupport,
19-
console: console,
20-
setTimeout: setTimeout,
21-
clearTimeout: clearTimeout,
22-
URL: URL,
23-
24-
// Convince jQuery not to assume it's in a browser
25-
module: { exports: {} }
26-
};
27-
28-
for (var key in globals) {
29-
sandbox[key] = globals[key];
7+
constructor(options) {
8+
this.globals = options.globals;
9+
this.sandbox = this.buildSandbox();
3010
}
3111

32-
// Set the global as `window`.
33-
sandbox.window = sandbox;
34-
sandbox.window.self = sandbox;
12+
buildSandbox() {
13+
var console = this.buildWrappedConsole();
14+
var sourceMapSupport = require('./install-source-map-support');
15+
var URL = require('url');
16+
var globals = this.globals;
17+
18+
var sandbox = {
19+
sourceMapSupport: sourceMapSupport,
20+
console: console,
21+
setTimeout: setTimeout,
22+
clearTimeout: clearTimeout,
23+
URL: URL,
3524

36-
return sandbox;
37-
};
25+
// Convince jQuery not to assume it's in a browser
26+
module: { exports: {} }
27+
};
3828

39-
Sandbox.prototype.buildWrappedConsole = function() {
40-
var wrappedConsole = Object.create(console);
41-
wrappedConsole.error = function() {
42-
console.error.apply(console, Array.prototype.map.call(arguments, function(a) {
43-
return typeof a === 'string' ? chalk.red(a) : a;
44-
}));
45-
};
29+
for (var key in globals) {
30+
sandbox[key] = globals[key];
31+
}
4632

47-
return wrappedConsole;
48-
};
33+
// Set the global as `window`.
34+
sandbox.window = sandbox;
35+
sandbox.window.self = sandbox;
36+
37+
return sandbox;
38+
}
39+
40+
buildWrappedConsole() {
41+
var wrappedConsole = Object.create(console);
42+
wrappedConsole.error = function() {
43+
console.error.apply(console, Array.prototype.map.call(arguments, function(a) {
44+
return typeof a === 'string' ? chalk.red(a) : a;
45+
}));
46+
};
47+
48+
return wrappedConsole;
49+
}
50+
}
4951

5052
module.exports = Sandbox;

src/vm-sandbox.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
'use strict';
2+
13
var vm = require('vm');
24
var Sandbox = require('./sandbox');
35

4-
function VMSandbox(options) {
5-
this.init(options);
6-
vm.createContext(this.sandbox);
7-
}
6+
class VMSandbox extends Sandbox {
7+
constructor(options) {
8+
super(options);
9+
vm.createContext(this.sandbox);
10+
}
811

9-
VMSandbox.prototype = Object.create(Sandbox.prototype);
10-
VMSandbox.prototype.constructor = Sandbox;
12+
eval(source, filePath) {
13+
var fileScript = new vm.Script(source, { filename: filePath });
14+
fileScript.runInContext(this.sandbox);
15+
}
1116

12-
VMSandbox.prototype.eval = function(source, filePath) {
13-
var fileScript = new vm.Script(source, { filename: filePath });
14-
fileScript.runInContext(this.sandbox);
15-
};
17+
run(cb) {
18+
return cb.call(this.sandbox, this.sandbox);
19+
}
1620

17-
VMSandbox.prototype.run = function(cb) {
18-
return cb.call(this.sandbox, this.sandbox);
19-
};
21+
}
2022

2123
module.exports = VMSandbox;

test/fixtures/custom-sandbox/custom-sandbox.js

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,47 @@ const Sandbox = require('./../../../src/sandbox');
1111
* from fastboot's Sandbox class
1212
*
1313
*/
14-
function CustomSandbox(options) {
15-
this.init(options);
16-
vm.createContext(this.sandbox);
17-
}
18-
19-
CustomSandbox.prototype = Object.create(Sandbox.prototype);
20-
CustomSandbox.prototype.constructor = Sandbox;
21-
22-
/**
23-
* Evals the file in the sandbox.
24-
*
25-
*/
26-
CustomSandbox.prototype.eval = function(source, filePath) {
27-
var fileScript = new vm.Script(source, { filename: filePath });
28-
fileScript.runInContext(this.sandbox);
29-
};
30-
31-
/**
32-
* Runs a given function in the sandbox with providing the context and the sandbox object.
33-
* Can be used to run functions outside of the sandbox in the sandbox.
34-
*
35-
* @todo: use this when we create app factory from addon
36-
*/
37-
CustomSandbox.prototype.run = function(cb) {
38-
return cb.call(this.sandbox, this.sandbox);
39-
};
40-
41-
/**
42-
* Wraps console.error with sending the error over the channel as well so that
43-
* it is reported by EKG.
44-
* // TODO: change this function to send error over the channel instead.
45-
*
46-
*/
47-
CustomSandbox.prototype.buildWrappedConsole = function() {
48-
var wrappedConsole = Object.create(console);
49-
wrappedConsole.error = function() {
50-
console.error.apply(console, Array.prototype.map.call(arguments, function(a) {
51-
return a;
52-
}));
53-
};
54-
55-
return wrappedConsole;
56-
};
14+
class CustomSandbox extends Sandbox {
15+
constructor(options) {
16+
super(options);
17+
vm.createContext(this.sandbox);
18+
}
19+
20+
/**
21+
* Evals the file in the sandbox.
22+
*
23+
*/
24+
eval(source, filePath) {
25+
var fileScript = new vm.Script(source, { filename: filePath });
26+
fileScript.runInContext(this.sandbox);
27+
}
28+
29+
/**
30+
* Runs a given function in the sandbox with providing the context and the sandbox object.
31+
* Can be used to run functions outside of the sandbox in the sandbox.
32+
*
33+
* @todo: use this when we create app factory from addon
34+
*/
35+
run(cb) {
36+
return cb.call(this.sandbox, this.sandbox);
37+
}
38+
39+
/**
40+
* Wraps console.error with sending the error over the channel as well so that
41+
* it is reported by EKG.
42+
* // TODO: change this function to send error over the channel instead.
43+
*
44+
*/
45+
buildWrappedConsole() {
46+
var wrappedConsole = Object.create(console);
47+
wrappedConsole.error = function() {
48+
console.error.apply(console, Array.prototype.map.call(arguments, function(a) {
49+
return a;
50+
}));
51+
};
52+
53+
return wrappedConsole;
54+
}
5755

56+
}
5857
module.exports = CustomSandbox;

0 commit comments

Comments
 (0)