Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #636 from caridy/rs-sandbox
Browse files Browse the repository at this point in the history
store sandbox using yui-sandbox

- isolation between store instance and the mojito Y instance
- remove experimental dataprocess
- use JSON stringify/parse as the basic cache mechanism in store
  • Loading branch information
caridy committed Oct 16, 2012
2 parents 312a021 + c1115af commit 8b89ad9
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 114 deletions.
5 changes: 3 additions & 2 deletions lib/app/autoload/action-context.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,11 @@ YUI.add('mojito-action-context', function(Y, NAME) {

var controller = opts.controller,
command = opts.command,
store = opts.store,
actionFunction,
perf = Y.mojito.perf.timeline('mojito', 'ac:init', 'set up AC object', command),
error,
staticAppConfig = YUI.Env.mojito.DataProcess.retrieve('static-app-config') || {},
staticAppConfig = store.getAppConfig(store.getStaticContext()),
my;

my = this;
Expand Down Expand Up @@ -316,7 +317,7 @@ YUI.add('mojito-action-context', function(Y, NAME) {
return my.dispatcher.dispatch(command, adapter);
};

attachActionContextAddons(Y.mojito.addons.ac, command, opts.adapter, this, opts.store);
attachActionContextAddons(Y.mojito.addons.ac, command, opts.adapter, this, store);

// Check if the controller has the requested action
if (!Y.Lang.isFunction(controller[actionFunction])) {
Expand Down
54 changes: 3 additions & 51 deletions lib/app/autoload/mojito.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,10 @@ YUI.add('mojito', function(Y, NAME) {
mark: function () {}
};

// internal mojito framework cache (this is probably legacy)
YUI.namespace('_mojito._cache');

// setting the stage for all data-scopes implementation
YUI.namespace('Env.mojito');

// defining the process data scope bound to YUI.Env, which is
// persistent between requests on the same process on the
// server side and persistent per page on the client side.
// TODO: on the client side we might want to use sessionStorage
// TODO: abstract everything related with data into its own component
// TODO: this structure leaks, should be well documented
YUI.Env.mojito.DataProcess = YUI.Env.mojito.DataProcess || (function () {

var data = {};

function key(obj) {
return Y.Lang.isObject(obj) ? JSON.stringify(obj) : obj;
}

return {

/**
* Adds a new entry to the cache.
*
* @method add
* @param request {Object} Request value.
* @param response {Object} Response value.
* @return {Object} Cached object, or null.
*/
add: function (request, response) {
return (data[key(request)] = response);
},

/**
* Flushes cache.
*
* @method flush
*/
flush: function () {
data = {};
},

/**
* Retrieves cached object for given request, if available.
*
* @method retrieve
* @param request {Object} Request object.
* @return {Object} Cached object, or null.
*/
retrieve: function (request) {
return data[key(request)] || null;
}

};
}());

}, '0.1.0', {requires: []});
12 changes: 2 additions & 10 deletions lib/app/autoload/perf.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ YUI.add('mojito-perf', function (Y, NAME) {

var libfs = require('fs'),

// using the mojito's process data to retrieve and
// store the process process buffer and the config
data = YUI.Env.mojito.DataProcess,
buffer = data.retrieve('perf-buffer'),
config = data.retrieve('perf-config'),
buffer = {},
config = Y.config.perf,

requestId = 0,
colorRed = '\u001b[31m',
Expand Down Expand Up @@ -312,11 +309,6 @@ YUI.add('mojito-perf', function (Y, NAME) {
config = {};
}

if (!buffer) {
// setting up a new buffer when needed
buffer = data.add('perf-buffer', {});
}

}, '0.1.0', {requires: [
'mojito'
]});
92 changes: 64 additions & 28 deletions lib/mojito.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
// ----------------------------------------------------------------------------


var YUI = require('yui').YUI,
express = require('express'), // TODO: [Issue 80] go back to connect?
var express = require('express'), // TODO: [Issue 80] go back to connect?
http = require('http'),
OutputHandler = require('./output-handler.server'),
libpath = require('path'),
libutils = require('./management/utils'),
serverLog = require('./server-log'),
YUIFactory = require('./yui-sandbox.js'),
requestCounter = 0, // used to scope logs per request
Mojito;


// ----------------------------------------------------------------------------
// Mojito Global
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -64,6 +62,7 @@ function MojitoServer(options) {

// TODO: Note we could pass some options to the express server instance.
this._app = express.createServer();
this._configureAppStore(this._app, this._options);
this._configureAppInstance(this._app, this._options);

return this;
Expand Down Expand Up @@ -149,33 +148,21 @@ MojitoServer.prototype._startupTime = null;


/**
* Adds Mojito framework components to the Express application instance.
* @method _configureAppInstance
* @param {Object} app The Express application instance to Mojito-enable.
* Adds Resource Store instance reference to the Express application
* as member `app.store`.
* @method _configureAppStore
* @param {Object} app The Express application instance to configure.
* @param {{port: number,
* dir: string,
* context: Object,
* appConfig: Object,
* verbose: boolean}} options An object containing server options.
*/
MojitoServer.prototype._configureAppInstance = function(app, options) {
MojitoServer.prototype._configureAppStore = function(app, options) {

var store,
Y,
appConfig,
yuiConfig,
logConfig = {},
modules = [],
middleware,
m,
midName,
midBase,
midPath,
midFactory,
hasMojito,
midConfig,
dispatcher,
singleton_dispatcher;
YUI = YUIFactory.getYUI(),
Y;

if (!options) {
options = {};
Expand All @@ -202,7 +189,7 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
}
});

Y.use('mojito', 'mojito-util', 'mojito-resource-store');
Y.use('mojito-resource-store');
store = new Y.mojito.ResourceStore({
root: options.dir,
context: options.context,
Expand All @@ -214,10 +201,55 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
app.store = store;

store.preload();
};

/**
* Adds Mojito framework components to the Express application instance.
* @method _configureAppInstance
* @param {Object} app The Express application instance to Mojito-enable.
* @param {{port: number,
* dir: string,
* context: Object,
* appConfig: Object,
* verbose: boolean}} options An object containing server options.
*/
MojitoServer.prototype._configureAppInstance = function(app, options) {

var store = app.store,
YUI,
Y,
appConfig,
yuiConfig,
logConfig = {},
modules = [],
middleware,
m,
midName,
midBase,
midPath,
midFactory,
hasMojito,
midConfig,
dispatcher,
singleton_dispatcher;

if (!options) {
options = {};
}
if (!options.dir) {
options.dir = process.cwd();
}
if (!options.context) {
options.context = {};
}

appConfig = store.getAppConfig(store.getStaticContext());
yuiConfig = (appConfig.yui && appConfig.yui.config) || {};

YUI.Env.mojito.DataProcess.add('static-app-config', appConfig);
YUI = YUIFactory.getYUI(yuiConfig.filter);
Y = YUI({
useSync: true
});

this._configureLogger(Y, yuiConfig);
this._configureYUI(Y, store, modules);
Expand All @@ -228,11 +260,11 @@ MojitoServer.prototype._configureAppInstance = function(app, options) {
// You can also use the option --perf path/filename.log when
// running mojito start to dump metrics to disk.
if (appConfig.perf) {
appConfig.perf.logFile = options.perf;
// storing appConfig in the process
YUI.Env.mojito.DataProcess.add('perf-config', appConfig.perf);
yuiConfig.perf = appConfig.perf;
yuiConfig.perf.logFile = options.perf;
}


// applying the default configuration from application.json->yui-config
Y.applyConfig(yuiConfig);

Expand Down Expand Up @@ -367,6 +399,10 @@ MojitoServer.prototype._configureLogger = function(Y, yuiConfig) {
}
}

// one more hack: we need to make sure that base is attached
// to be able to listen for Y.on.
Y.use('base');

if (yuiConfig.debug) {

logLevelOrder = yuiConfig.logLevelOrder || logLevelOrder;
Expand Down
12 changes: 6 additions & 6 deletions lib/store.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
key = JSON.stringify(ctx || {});

if (this._appConfigCache[key]) {
return Y.mojito.util.copy(this._appConfigCache[key]);
return JSON.parse(this._appConfigCache[key]);
}

ycb = this.config.readConfigYCB(this._libs.path.join(this._config.root, 'application.json'), ctx);

appConfig = Y.mojito.util.blend(this._fwConfig.appConfigBase, this._config.appConfig);
appConfig = Y.mojito.util.blend(appConfig, ycb);

this._appConfigCache[key] = appConfig;
this._appConfigCache[key] = JSON.stringify(appConfig);

return Y.mojito.util.copy(appConfig);
return appConfig;
},
/**
* Preloads everything in the app, and as well pertinent parts of
Expand Down Expand Up @@ -738,7 +738,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
cachedValue = this._routesCache[key];

if (cachedValue) {
return Y.mojito.util.copy(cachedValue);
return JSON.parse(cachedValue);
}

appConfig = this.getAppConfig(ctx);
Expand Down Expand Up @@ -770,9 +770,9 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
Y.mix(out, this._fwConfig.defaultRoutes, true);
}

this._routesCache[key] = out;
this._routesCache[key] = JSON.stringify(out);

return Y.mojito.util.copy(out);
return out;
},


Expand Down
18 changes: 12 additions & 6 deletions lib/yui-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
var fs = require('fs'),
path = require('path'),
vm = require('vm'),
file = path.join(__dirname, '..', 'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs.js'),
code = fs.readFileSync(file, 'utf8');

code = {
min: fs.readFileSync(path.join(__dirname, '..',
'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs-min.js'), 'utf8'),
raw: fs.readFileSync(path.join(__dirname, '..',
'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs.js'), 'utf8'),
debug: fs.readFileSync(path.join(__dirname, '..',
'node_modules', 'yui', 'yui-nodejs', 'yui-nodejs-debug.js'), 'utf8')
};

/*
This is a hack to get an isolated YUI object. This is EXPERIMENTAL,
Expand All @@ -25,7 +30,7 @@ var fs = require('fs'),
*/

exports.getYUI = function () {
exports.getYUI = function (filter) {
var sandbox = {
console: console,
process: process,
Expand All @@ -34,9 +39,10 @@ exports.getYUI = function () {
setTimeout: setTimeout,
setInterval: setInterval,
__filename: __filename,
__dirname: __dirname,
__dirname: path.join(__dirname, '..', 'node_modules', 'yui', 'yui-nodejs'),
exports: {}
};
vm.runInNewContext(code, sandbox, 'build/yui-new/yui-new.js');
filter = (filter && code.hasOwnProperty(filter)) ? filter : 'raw';
vm.runInNewContext(code[filter], sandbox, 'build/yui-new/yui-new.js');
return sandbox.exports.YUI;
};
5 changes: 0 additions & 5 deletions tests/base/mojito-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ YUI.add('mojito', function(Y, NAME) {
mark: function () {},
timeline: function () { return { done: function() {} }; }
};
YUI.namespace('Env.mojito').DataProcess = {
add: function () {},
flush: function () {},
retrieve: function () {}
};
});

/* AC ADDONS */
Expand Down
Loading

0 comments on commit 8b89ad9

Please sign in to comment.