mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
Refactored app proxy module to not use Constructor
no-issue Rather than creating a whole instance, we can replace it with a helper method - his is less memory intensive and a little easier to parse for something this small.
This commit is contained in:
parent
ca8c5c4907
commit
4696d70de0
@ -3,7 +3,7 @@ const _ = require('lodash');
|
||||
const Promise = require('bluebird');
|
||||
const config = require('../../config');
|
||||
const common = require('../../lib/common');
|
||||
const AppProxy = require('./proxy');
|
||||
const Proxy = require('./proxy');
|
||||
const Sandbox = require('./sandbox');
|
||||
|
||||
// Get the full path to an app by name
|
||||
@ -23,17 +23,10 @@ function getAppRelativePath(name, relativeTo = __dirname) {
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
// Load apps through a pseudo sandbox
|
||||
function loadApp(appPath) {
|
||||
return Sandbox.loadApp(appPath);
|
||||
}
|
||||
|
||||
function getAppByName(name) {
|
||||
// Grab the app class to instantiate
|
||||
const AppClass = loadApp(getAppRelativePath(name));
|
||||
const proxy = new AppProxy({
|
||||
name
|
||||
});
|
||||
const AppClass = Sandbox.loadApp(getAppRelativePath(name));
|
||||
const proxy = Proxy.getInstance(name);
|
||||
|
||||
// Check for an actual class, otherwise just use whatever was returned
|
||||
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
|
||||
|
@ -5,81 +5,60 @@ const filters = require('../../filters');
|
||||
const common = require('../../lib/common');
|
||||
const routingService = require('../routing');
|
||||
|
||||
let generateProxyFunctions;
|
||||
module.exports.getInstance = function getInstance(name) {
|
||||
if (!name) {
|
||||
throw new Error(common.i18n.t('errors.apps.mustProvideAppName.error'));
|
||||
}
|
||||
|
||||
generateProxyFunctions = function (name) {
|
||||
const appRouter = routingService.registry.getRouter('appRouter');
|
||||
|
||||
var runIfPermissionToMethod = function (perm, method, wrappedFunc, context, args) {
|
||||
// internal apps get all permissions
|
||||
return wrappedFunc.apply(context, args);
|
||||
},
|
||||
checkRegisterPermissions = function (perm, registerMethod) {
|
||||
return _.wrap(registerMethod, function (origRegister, name) {
|
||||
return runIfPermissionToMethod(perm, name, origRegister, this, _.toArray(arguments).slice(1));
|
||||
});
|
||||
},
|
||||
passThruAppContextToApi = function (perm, apiMethods) {
|
||||
var appContext = {
|
||||
app: name
|
||||
const passThruAppContextToApi = (apiMethods) => {
|
||||
const appContext = {
|
||||
app: name
|
||||
};
|
||||
|
||||
return _.reduce(apiMethods, function (memo, apiMethod, methodName) {
|
||||
memo[methodName] = function (...args) {
|
||||
const options = args[args.length - 1];
|
||||
|
||||
if (_.isObject(options)) {
|
||||
options.context = _.clone(appContext);
|
||||
}
|
||||
return apiMethod.apply({}, args);
|
||||
};
|
||||
|
||||
return _.reduce(apiMethods, function (memo, apiMethod, methodName) {
|
||||
memo[methodName] = function () {
|
||||
var args = _.toArray(arguments),
|
||||
options = args[args.length - 1];
|
||||
return memo;
|
||||
}, {});
|
||||
};
|
||||
|
||||
if (_.isObject(options)) {
|
||||
options.context = _.clone(appContext);
|
||||
}
|
||||
return apiMethod.apply({}, args);
|
||||
};
|
||||
|
||||
return memo;
|
||||
}, {});
|
||||
},
|
||||
proxy;
|
||||
|
||||
proxy = {
|
||||
return {
|
||||
filters: {
|
||||
register: checkRegisterPermissions('filters', filters.registerFilter.bind(filters)),
|
||||
deregister: checkRegisterPermissions('filters', filters.deregisterFilter.bind(filters))
|
||||
register: filters.registerFilter.bind(filters),
|
||||
deregister: filters.deregisterFilter.bind(filters)
|
||||
},
|
||||
helpers: {
|
||||
register: checkRegisterPermissions('helpers', helpers.registerThemeHelper.bind(helpers)),
|
||||
registerAsync: checkRegisterPermissions('helpers', helpers.registerAsyncThemeHelper.bind(helpers))
|
||||
register: helpers.registerThemeHelper.bind(helpers),
|
||||
registerAsync: helpers.registerAsyncThemeHelper.bind(helpers)
|
||||
},
|
||||
// Expose the route service...
|
||||
routeService: {
|
||||
// This allows for mounting an entirely new Router at a path...
|
||||
registerRouter: checkRegisterPermissions('routes', appRouter.mountRouter.bind(appRouter))
|
||||
registerRouter: appRouter.mountRouter.bind(appRouter)
|
||||
},
|
||||
// Mini proxy to the API - needs review
|
||||
api: {
|
||||
posts: passThruAppContextToApi('posts',
|
||||
posts: passThruAppContextToApi(
|
||||
_.pick(api.posts, 'browse', 'read', 'edit', 'add', 'destroy')
|
||||
),
|
||||
tags: passThruAppContextToApi('tags',
|
||||
tags: passThruAppContextToApi(
|
||||
_.pick(api.tags, 'browse')
|
||||
),
|
||||
notifications: passThruAppContextToApi('notifications',
|
||||
notifications: passThruAppContextToApi(
|
||||
_.pick(api.notifications, 'browse', 'add', 'destroy')
|
||||
),
|
||||
settings: passThruAppContextToApi('settings',
|
||||
settings: passThruAppContextToApi(
|
||||
_.pick(api.settings, 'browse', 'read', 'edit')
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
return proxy;
|
||||
};
|
||||
|
||||
function AppProxy(options) {
|
||||
if (!options.name) {
|
||||
throw new Error(common.i18n.t('errors.apps.mustProvideAppName.error'));
|
||||
}
|
||||
|
||||
_.extend(this, generateProxyFunctions(options.name));
|
||||
}
|
||||
|
||||
module.exports = AppProxy;
|
||||
|
Loading…
Reference in New Issue
Block a user