Skip to content
This repository
Browse code

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...
commit 14dc572e19e0928c18c42292eea541fbd42a4a78 1 parent 9ed57eb
Eugene Kashida authored March 21, 2013
36  lib/app/middleware/mojito-handler-tunnel-demux.js
@@ -8,8 +8,8 @@
8 8
 /*jslint sloppy:true, nomen:true*/
9 9
 
10 10
 
11  
-var liburl = require('url'),
12  
-    RE_REPEATING_SLASH = /\/{2,}/g;
  11
+var liburl  = require('url'),
  12
+    libpath = require('path');
13 13
 
14 14
 function trimSlash(str) {
15 15
     if (str.charAt(0) === '/') {
@@ -28,8 +28,7 @@ function trimSlash(str) {
28 28
  * @return {Object} The newly constructed handler.
29 29
  */
30 30
 module.exports = function (config) {
31  
-    var that      = this,
32  
-        appConfig = config.store.getAppConfig({}) || {},
  31
+    var appConfig = config.store.getAppConfig({}) || {},
33 32
         staticPrefix,
34 33
         tunnelPrefix;
35 34
 
@@ -43,15 +42,15 @@ module.exports = function (config) {
43 42
         tunnelPrefix = '/' + trimSlash(tunnelPrefix);
44 43
     }
45 44
 
46  
-    this.staticPrefix = staticPrefix || '/static';
47  
-    this.tunnelPrefix = tunnelPrefix || '/tunnel';
  45
+    staticPrefix = staticPrefix || '/static';
  46
+    tunnelPrefix = tunnelPrefix || '/tunnel';
48 47
 
49 48
     return function (req, res, next) {
50  
-        var hasTunnelPrefix = req.url.indexOf(that.tunnelPrefix) === 0,
  49
+        var hasTunnelPrefix = req.url.indexOf(tunnelPrefix) === 0,
51 50
             hasTunnelHeader = req.headers['x-mojito-header'] === 'tunnel',
52 51
             name,
53 52
             type,
54  
-            url,
  53
+            path,
55 54
             parts;
56 55
 
57 56
         // If we are not tunneling get out of here fast!
@@ -78,18 +77,23 @@ module.exports = function (config) {
78 77
         /tunnel/static/{type}/specs/default.json   // according to a UT
79 78
         **/
80 79
 
81  
-        url = req.url.split('?')[0];
  80
+        path = liburl.parse(req.url).pathname;
  81
+
  82
+        // Replace multiple slashes with a single one.
  83
+        path = libpath.resolve(path);
82 84
 
83 85
         // Normalization step to handle `/{tunnelPrefix}`, `/{staticPrefix}`,
84 86
         // and `/{tunnelPrefix}/{staticPrefix}` URLs.
85  
-        url = url.replace(that.staticPrefix, '')
86  
-                 .replace(that.tunnelPrefix, '')
87  
-                 .replace(RE_REPEATING_SLASH, '/');
  87
+        path = path.replace(staticPrefix, '')
  88
+                   .replace(tunnelPrefix, '');
88 89
 
89  
-        parts = url.split('/');
  90
+        parts = path.split('/');
90 91
 
91 92
         if (parts.length) {
92  
-            name = parts[parts.length - 1];
  93
+            // Get the basename without the .json extension.
  94
+            name = libpath.basename(path, '.json');
  95
+
  96
+            // Get the mojit type.
93 97
             type = parts[1];
94 98
 
95 99
             // Spec tunnel
@@ -101,7 +105,7 @@ module.exports = function (config) {
101 105
                 return next();
102 106
             }
103 107
             // Type tunnel
104  
-            if (name === 'definition.json') {
  108
+            if (name === 'definition') {
105 109
                 req._tunnel.typeReq = {
106 110
                     type: type
107 111
                 };
@@ -110,7 +114,7 @@ module.exports = function (config) {
110 114
         }
111 115
 
112 116
         // RPC tunnel
113  
-        if (req.url === that.tunnelPrefix && req.method === 'POST') {
  117
+        if (req.url === tunnelPrefix && req.method === 'POST') {
114 118
             req._tunnel.rpcReq = {};
115 119
             return next();
116 120
         }
53  lib/app/middleware/mojito-handler-tunnel-rpc.js
@@ -28,15 +28,18 @@ function sendError(res, msg, code) {
28 28
  */
29 29
 module.exports = function (config) {
30 30
     return function (req, res, next) {
31  
-        var command  = req.body,
32  
-            instance = command.instance;
  31
+        var rpcReq = req._tunnel && req._tunnel.rpcReq,
  32
+            command,
  33
+            instance;
33 34
 
34  
-        command.context = command.context || {};
35  
-
36  
-        if (!req._tunnel || !req._tunnel.rpcReq) {
  35
+        if (!rpcReq) {
37 36
             return next();
38 37
         }
39 38
 
  39
+        command         = req.body;
  40
+        instance        = command.instance;
  41
+        command.context = command.context || {};
  42
+
40 43
         // When switching from the client context to the server context, we
41 44
         // have to override the runtime.
42 45
         command.context.runtime = 'server';
@@ -44,23 +47,27 @@ module.exports = function (config) {
44 47
         // All we need to do is expand the instance given within the RPC call
45 48
         // and attach it within a "tunnelCommand", which will be handled by
46 49
         // Mojito instead of looking up a route for it.
47  
-        config.store.expandInstance(instance, command.context, function (err, instance) {
48  
-            // Replace with the expanded instance.
49  
-            command.instance = instance;
50  
-            req.command = {
51  
-                action: command.action,
52  
-                instance: {
53  
-                    // Magic here to delegate to tunnelProxy.
54  
-                    base: 'tunnelProxy'
55  
-                },
56  
-                params: {
57  
-                    body: {
58  
-                        proxyCommand: command
59  
-                    }
60  
-                },
61  
-                context: command.context
62  
-            };
63  
-            return next();
64  
-        });
  50
+        config.store.expandInstance(
  51
+            instance,
  52
+            command.context,
  53
+            function (err, instance) {
  54
+                // Replace with the expanded instance.
  55
+                command.instance = instance;
  56
+                req.command = {
  57
+                    action: command.action,
  58
+                    instance: {
  59
+                        // Magic here to delegate to tunnelProxy.
  60
+                        base: 'tunnelProxy'
  61
+                    },
  62
+                    params: {
  63
+                        body: {
  64
+                            proxyCommand: command
  65
+                        }
  66
+                    },
  67
+                    context: command.context
  68
+                };
  69
+                return next();
  70
+            }
  71
+        );
65 72
     };
66 73
 };
33  lib/app/middleware/mojito-handler-tunnel-specs.js
@@ -29,7 +29,6 @@ function sendError(res, msg, code) {
29 29
 module.exports = function (config) {
30 30
     return function (req, res, next) {
31 31
         var specsReq = req._tunnel && req._tunnel.specsReq,
32  
-            instance = {},
33 32
             type,
34 33
             name;
35 34
 
@@ -39,28 +38,22 @@ module.exports = function (config) {
39 38
 
40 39
         type = specsReq.type;
41 40
         name = specsReq.name;
42  
-        name = name && name.split('.').slice(0, -1).join('.');
43 41
 
44 42
         if (!type || !name) {
45  
-            return sendError(res, 'Not found: ' + req.url, 500);
  43
+            return sendError(res, 'Not found: ' + req.url, 404);
46 44
         }
47 45
 
48  
-        instance.base = type;
49  
-
50  
-        if (name !== 'default') {
51  
-            instance.base += ':' + name;
52  
-        }
53  
-
54  
-        config.store.expandInstanceForEnv('client', instance, req.context,
55  
-            function (err, data) {
56  
-                if (err) {
57  
-                    return sendError(
58  
-                        res,
59  
-                        'Error opening: ' + req.url + '\n' + err,
60  
-                        500
61  
-                    );
62  
-                }
63  
-                return sendData(res, data);
64  
-            });
  46
+        config.store.expandInstanceForEnv('client', {
  47
+            base: (name === 'default') ? type : type + ':' + name
  48
+        }, req.context, function (err, data) {
  49
+            if (err) {
  50
+                return sendError(
  51
+                    res,
  52
+                    'Error opening: ' + req.url + '\n' + err,
  53
+                    500
  54
+                );
  55
+            }
  56
+            return sendData(res, data);
  57
+        });
65 58
     };
66 59
 };
30  lib/app/middleware/mojito-handler-tunnel-type.js
@@ -28,29 +28,27 @@ function sendError(res, msg, code) {
28 28
  */
29 29
 module.exports = function (config) {
30 30
     return function (req, res, next) {
31  
-        var typeReq  = req._tunnel && req._tunnel.typeReq,
32  
-            instance = {};
  31
+        var typeReq = req._tunnel && req._tunnel.typeReq;
33 32
 
34 33
         if (!typeReq) {
35 34
             return next();
36 35
         }
37 36
 
38 37
         if (!typeReq.type) {
39  
-            return sendError(res, 'Not found: ' + req.url, 500);
  38
+            return sendError(res, 'Not found: ' + req.url, 404);
40 39
         }
41 40
 
42  
-        instance.type = typeReq.type;
43  
-
44  
-        config.store.expandInstanceForEnv('client', instance, req.context,
45  
-            function (err, data) {
46  
-                if (err) {
47  
-                    return sendError(
48  
-                        res,
49  
-                        'Error opening: ' + req.url + '\n' + err,
50  
-                        500
51  
-                    );
52  
-                }
53  
-                return sendData(res, data);
54  
-            });
  41
+        config.store.expandInstanceForEnv('client', {
  42
+            type: typeReq.type
  43
+        }, req.context, function (err, data) {
  44
+            if (err) {
  45
+                return sendError(
  46
+                    res,
  47
+                    'Error opening: ' + req.url + '\n' + err,
  48
+                    500
  49
+                );
  50
+            }
  51
+            return sendData(res, data);
  52
+        });
55 53
     };
56 54
 };

0 notes on commit 14dc572

Please sign in to comment.
Something went wrong with that request. Please try again.