Permalink
Browse files

replace tunnel middleware with refactored version

- Remove trailing slashes from tunnel/static prefixes.
- Add flag and flag check to determine when to stop processing the request.
  • Loading branch information...
1 parent 9112208 commit 31295f4cda6cae19a3187a00f6a9f7c452fe6c2c @ekashida ekashida committed Mar 23, 2013
@@ -5,31 +5,31 @@
*/
/*global require, module*/
-/*jslint sloppy:true, nomen:true*/
-
-
-var liburl = require('url'),
- libpath = require('path');
-
+/*jslint sloppy:true, nomen:true, white:true*/
+var RE_TRAILING_SLASHES = /\/+$/;
/**
- * Export a function which can create the handler.
- * @param {Object} config Data to configure the handler.
- * @return {Object} The newly constructed handler.
+ * Export a function which can parse tunnel requests.
+ * @param {Object} config The configuration.
+ * @return {Object} The parser.
*/
module.exports = function (config) {
- var appConfig = config.store.getAppConfig({}) || {},
+ var liburl = require('url'),
+ libpath = require('path'),
+ appConfig = config.store.getAppConfig({}) || {},
staticPrefix,
tunnelPrefix;
staticPrefix = appConfig.staticHandling && appConfig.staticHandling.prefix;
tunnelPrefix = appConfig.tunnelPrefix;
if (staticPrefix) {
+ staticPrefix = staticPrefix.replace(RE_TRAILING_SLASHES, '');
staticPrefix = '/' + staticPrefix;
}
if (tunnelPrefix) {
+ tunnelPrefix = tunnelPrefix.replace(RE_TRAILING_SLASHES, '');
tunnelPrefix = '/' + tunnelPrefix;
}
@@ -50,8 +50,6 @@ module.exports = function (config) {
return next();
}
- req._tunnel = {};
-
/**
Tunnel examples
@@ -86,27 +84,23 @@ module.exports = function (config) {
// Get the mojit type.
type = parts[1];
- // Spec tunnel
+ // "Spec" tunnel request
if (parts[parts.length - 2] === 'specs') {
req._tunnel.specsReq = {
type: type,
name: name
};
- return next();
}
- // Type tunnel
- if (name === 'definition') {
+ // "Type" tunnel request
+ else if (name === 'definition') {
req._tunnel.typeReq = {
type: type
};
- return next();
}
}
-
- // RPC tunnel
- if (req.url === tunnelPrefix && req.method === 'POST') {
+ // "RPC" tunnel request
+ else if (req.url === tunnelPrefix && req.method === 'POST') {
req._tunnel.rpcReq = {};
- return next();
}
return next();
@@ -37,7 +37,6 @@ module.exports = function (config) {
}
command = req.body;
- instance = command.instance;
command.context = command.context || {};
// When switching from the client context to the server context, we
@@ -48,7 +47,7 @@ module.exports = function (config) {
// 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.instance,
command.context,
function (err, instance) {
// Replace with the expanded instance.
@@ -7,19 +7,6 @@
/*global module*/
/*jslint sloppy:true, nomen:true*/
-
-function sendData(res, data, code) {
- res.writeHead((code || 200), {
- 'content-type': 'application/json; charset="utf-8"'
- });
- res.end(JSON.stringify(data, null, 4));
-}
-
-function sendError(res, msg, code) {
- sendData(res, {error: msg}, (code || 500));
-}
-
-
/**
* Exports a middleware factory that can handle spec tunnel requests.
*
@@ -29,6 +16,7 @@ function sendError(res, msg, code) {
module.exports = function (config) {
return function (req, res, next) {
var specsReq = req._tunnel && req._tunnel.specsReq,
+ instance,
type,
name;
@@ -40,20 +28,31 @@ module.exports = function (config) {
name = specsReq.name;
if (!type || !name) {
- return sendError(res, 'Not found: ' + req.url, 404);
+ return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
+ }
+
+ instance = {
+ base: type
+ };
+
+ if (name !== 'default') {
+ instance.base += ':' + name;
}
- 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
- );
+ config.store.expandInstanceForEnv(
+ 'client',
+ instance,
+ req.context,
+ function (err, data) {
+ if (err) {
+ return req._tunnel.sendError(
+ res,
+ 'Error opening: ' + req.url + '\n' + err,
+ 500
+ );
+ }
+ return req._tunnel.sendData(res, data);
}
- return sendData(res, data);
- });
+ );
};
};
@@ -7,19 +7,6 @@
/*global module*/
/*jslint sloppy:true, nomen:true*/
-
-function sendData(res, data, code) {
- res.writeHead((code || 200), {
- 'content-type': 'application/json; charset="utf-8"'
- });
- res.end(JSON.stringify(data, null, 4));
-}
-
-function sendError(res, msg, code) {
- sendData(res, {error: msg}, (code || 500));
-}
-
-
/**
* Exports a middleware factory that can handle type tunnel requests.
*
@@ -28,27 +15,35 @@ function sendError(res, msg, code) {
*/
module.exports = function (config) {
return function (req, res, next) {
- var typeReq = req._tunnel && req._tunnel.typeReq;
+ var typeReq = req._tunnel && req._tunnel.typeReq,
+ instance;
if (!typeReq) {
return next();
}
if (!typeReq.type) {
- return sendError(res, 'Not found: ' + req.url, 404);
+ return req._tunnel.sendError(res, 'Not found: ' + req.url, 404);
}
- config.store.expandInstanceForEnv('client', {
+ instance = {
type: typeReq.type
- }, req.context, function (err, data) {
- if (err) {
- return sendError(
- res,
- 'Error opening: ' + req.url + '\n' + err,
- 500
- );
+ };
+
+ config.store.expandInstanceForEnv(
+ 'client',
+ instance,
+ req.context,
+ function (err, data) {
+ if (err) {
+ return req._tunnel.sendError(
+ res,
+ 'Error opening: ' + req.url + '\n' + err,
+ 500
+ );
+ }
+ return req._tunnel.sendData(res, data);
}
- return sendData(res, data);
- });
+ );
};
};
Oops, something went wrong.

0 comments on commit 31295f4

Please sign in to comment.