Refactored Sandbox to be singleton

no-issue
This commit is contained in:
Fabien O'Carroll 2019-04-15 12:13:50 +02:00
parent fd9fc92dd5
commit ad9d142174
2 changed files with 5 additions and 16 deletions

View File

@ -4,7 +4,7 @@ const Promise = require('bluebird');
const config = require('../../config');
const common = require('../../lib/common');
const AppProxy = require('./proxy');
const AppSandbox = require('./sandbox');
const Sandbox = require('./sandbox');
const AppDependencies = require('./dependencies');
const AppPermissions = require('./permissions');
@ -27,9 +27,7 @@ function getAppRelativePath(name, relativeTo = __dirname) {
// Load apps through a pseudo sandbox
function loadApp(appPath) {
const sandbox = new AppSandbox();
return sandbox.loadApp(appPath);
return Sandbox.loadApp(appPath);
}
function getAppByName(name, permissions) {

View File

@ -1,22 +1,13 @@
const Module = require('module');
function AppSandbox(opts) {
this.opts = opts;
}
AppSandbox.prototype.loadApp = function loadAppSandboxed(appPath) {
// Set loaded modules parent to this
const parentModulePath = this.opts.parent || module.parent;
module.exports.loadApp = function loadAppSandboxed(appPath) {
// Resolve the modules path
const resolvedModulePath = Module._resolveFilename(appPath, parentModulePath);
const resolvedModulePath = Module._resolveFilename(appPath, module.parent);
// Instantiate a Node Module class
const currentModule = new Module(resolvedModulePath, parentModulePath);
const currentModule = new Module(resolvedModulePath, module.parent);
currentModule.load(currentModule.id);
return currentModule.exports;
};
module.exports = AppSandbox;