Permalink
Browse files

implement feedback from pull #1044

- delay object creation for performance reasons
- use core library methods for parsing url/path
- fix http response code (500 => 404)
- eliminate `that = this` usage by closing over prefix variables directly
  • Loading branch information...
ekashida committed Mar 22, 2013
1 parent 9ed57eb commit 14dc572e19e0928c18c42292eea541fbd42a4a78
@@ -8,8 +8,8 @@
/*jslint sloppy:true, nomen:true*/
-var liburl = require('url'),
- RE_REPEATING_SLASH = /\/{2,}/g;
+var liburl = require('url'),
+ libpath = require('path');
function trimSlash(str) {
if (str.charAt(0) === '/') {
@@ -28,8 +28,7 @@ function trimSlash(str) {
* @return {Object} The newly constructed handler.
*/
module.exports = function (config) {
- var that = this,
- appConfig = config.store.getAppConfig({}) || {},
+ var appConfig = config.store.getAppConfig({}) || {},
staticPrefix,
tunnelPrefix;
@@ -43,15 +42,15 @@ module.exports = function (config) {
tunnelPrefix = '/' + trimSlash(tunnelPrefix);
}
- this.staticPrefix = staticPrefix || '/static';
- this.tunnelPrefix = tunnelPrefix || '/tunnel';
+ staticPrefix = staticPrefix || '/static';
+ tunnelPrefix = tunnelPrefix || '/tunnel';
return function (req, res, next) {
- var hasTunnelPrefix = req.url.indexOf(that.tunnelPrefix) === 0,
+ var hasTunnelPrefix = req.url.indexOf(tunnelPrefix) === 0,
hasTunnelHeader = req.headers['x-mojito-header'] === 'tunnel',
name,
type,
- url,
+ path,
parts;
// If we are not tunneling get out of here fast!
@@ -78,18 +77,23 @@ module.exports = function (config) {
/tunnel/static/{type}/specs/default.json // according to a UT
**/
- url = req.url.split('?')[0];
+ path = liburl.parse(req.url).pathname;
+
+ // Replace multiple slashes with a single one.
+ path = libpath.resolve(path);
// Normalization step to handle `/{tunnelPrefix}`, `/{staticPrefix}`,
// and `/{tunnelPrefix}/{staticPrefix}` URLs.
- url = url.replace(that.staticPrefix, '')
- .replace(that.tunnelPrefix, '')
- .replace(RE_REPEATING_SLASH, '/');
+ path = path.replace(staticPrefix, '')
+ .replace(tunnelPrefix, '');
- parts = url.split('/');
+ parts = path.split('/');
if (parts.length) {
- name = parts[parts.length - 1];
+ // Get the basename without the .json extension.
+ name = libpath.basename(path, '.json');
+
+ // Get the mojit type.
type = parts[1];
// Spec tunnel
@@ -101,7 +105,7 @@ module.exports = function (config) {
return next();
}
// Type tunnel
- if (name === 'definition.json') {
+ if (name === 'definition') {
req._tunnel.typeReq = {
type: type
};
@@ -110,7 +114,7 @@ module.exports = function (config) {
}
// RPC tunnel
- if (req.url === that.tunnelPrefix && req.method === 'POST') {
+ if (req.url === tunnelPrefix && req.method === 'POST') {
req._tunnel.rpcReq = {};
return next();
}
@@ -28,39 +28,46 @@ function sendError(res, msg, code) {
*/
module.exports = function (config) {
return function (req, res, next) {
- var command = req.body,
- instance = command.instance;
+ var rpcReq = req._tunnel && req._tunnel.rpcReq,
+ command,
+ instance;
- command.context = command.context || {};
-
- if (!req._tunnel || !req._tunnel.rpcReq) {
+ if (!rpcReq) {
return next();
}
+ command = req.body;
+ instance = command.instance;
+ command.context = command.context || {};
+
// When switching from the client context to the server context, we
// have to override the runtime.
command.context.runtime = 'server';
// All we need to do is expand the instance given within the RPC call
// and attach it within a "tunnelCommand", which will be handled by
// Mojito instead of looking up a route for it.
- config.store.expandInstance(instance, command.context, function (err, instance) {
- // Replace with the expanded instance.
- command.instance = instance;
- req.command = {
- action: command.action,
- instance: {
- // Magic here to delegate to tunnelProxy.
- base: 'tunnelProxy'
- },
- params: {
- body: {
- proxyCommand: command
- }
- },
- context: command.context
- };
- return next();
- });
+ config.store.expandInstance(
+ instance,
+ command.context,
+ function (err, instance) {
+ // Replace with the expanded instance.
+ command.instance = instance;
+ req.command = {
+ action: command.action,
+ instance: {
+ // Magic here to delegate to tunnelProxy.
+ base: 'tunnelProxy'
+ },
+ params: {
+ body: {
+ proxyCommand: command
+ }
+ },
+ context: command.context
+ };
+ return next();
+ }
+ );
};
};
@@ -29,7 +29,6 @@ function sendError(res, msg, code) {
module.exports = function (config) {
return function (req, res, next) {
var specsReq = req._tunnel && req._tunnel.specsReq,
- instance = {},
type,
name;
@@ -39,28 +38,22 @@ module.exports = function (config) {
type = specsReq.type;
name = specsReq.name;
- name = name && name.split('.').slice(0, -1).join('.');
if (!type || !name) {
- return sendError(res, 'Not found: ' + req.url, 500);
+ return sendError(res, 'Not found: ' + req.url, 404);
}
- instance.base = type;
-
- if (name !== 'default') {
- instance.base += ':' + name;
- }
-
- config.store.expandInstanceForEnv('client', instance, req.context,
- function (err, data) {
- if (err) {
- return sendError(
- res,
- 'Error opening: ' + req.url + '\n' + err,
- 500
- );
- }
- return sendData(res, data);
- });
+ config.store.expandInstanceForEnv('client', {
+ base: (name === 'default') ? type : type + ':' + name
+ }, req.context, function (err, data) {
+ if (err) {
+ return sendError(
+ res,
+ 'Error opening: ' + req.url + '\n' + err,
+ 500
+ );
+ }
+ return sendData(res, data);
+ });
};
};
@@ -28,29 +28,27 @@ function sendError(res, msg, code) {
*/
module.exports = function (config) {
return function (req, res, next) {
- var typeReq = req._tunnel && req._tunnel.typeReq,
- instance = {};
+ var typeReq = req._tunnel && req._tunnel.typeReq;
if (!typeReq) {
return next();
}
if (!typeReq.type) {
- return sendError(res, 'Not found: ' + req.url, 500);
+ return sendError(res, 'Not found: ' + req.url, 404);
}
- instance.type = typeReq.type;
-
- config.store.expandInstanceForEnv('client', instance, req.context,
- function (err, data) {
- if (err) {
- return sendError(
- res,
- 'Error opening: ' + req.url + '\n' + err,
- 500
- );
- }
- return sendData(res, data);
- });
+ config.store.expandInstanceForEnv('client', {
+ type: typeReq.type
+ }, req.context, function (err, data) {
+ if (err) {
+ return sendError(
+ res,
+ 'Error opening: ' + req.url + '\n' + err,
+ 500
+ );
+ }
+ return sendData(res, data);
+ });
};
};

0 comments on commit 14dc572

Please sign in to comment.