Minifcation of all javascript, plus caching of the minifies js and main HTML. Also...
authorJack Allnutt <m2ys4u@Gmail.com>
Mon, 15 Aug 2011 21:02:52 +0000 (22:02 +0100)
committerJack Allnutt <m2ys4u@Gmail.com>
Mon, 15 Aug 2011 21:02:52 +0000 (22:02 +0100)
node/client/index.html.jade
node/kiwi.js

index a4fd68a82efbf1c13041244e21400eb3883b086f..c196c7fdec2cc327f60cf863219427ba978ad3db 100644 (file)
@@ -17,10 +17,11 @@ html(xmlns="http://www.w3.org/1999/xhtml", lang="en-gb")
         script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")
         script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js")
         script(type="text/javascript", src="js/jquery.json-2.2.min.js")
-        script(type="text/javascript", src="js/util.js")
-        script(type="text/javascript", src="js/gateway.js")
-        script(type="text/javascript", src="js/front.js")
-        script(type="text/javascript", src="js/iscroll.js")
+        //script(type="text/javascript", src="js/util.js")
+        //script(type="text/javascript", src="js/gateway.js")
+        //script(type="text/javascript", src="js/front.js")
+        //script(type="text/javascript", src="js/iscroll.js")
+        script(type="text/javascript", src="js/all.js")
 
         - if (touchscreen)
             script(type="text/javascript", src="js/touchscreen_tweaks.js")
index 2982a38f43937720136795e3dc0203a49f31fd84..b1d939e7c20477f84a694804bc5dc81245bdcee7 100644 (file)
@@ -7,7 +7,10 @@ var tls = require('tls'),
     fs = require('fs'),
     url = require('url'),
     dns = require('dns'),
+    crypto = require('crypto'),
     ws = require('socket.io'),
+    jsp = require("uglify-js").parser,
+    pro = require("uglify-js").uglify,
     _ = require('./lib/underscore.min.js'),
     starttls = require('./lib/starttls.js');
 
@@ -447,14 +450,37 @@ var ircSocketDataHandler = function (data, websocket, ircSocket) {
 if (config.handle_http) {
     var fileServer = new (require('node-static').Server)(__dirname + config.public_http);
     var jade = require('jade');
+    var cache = {alljs: '', html: []};
 }
 
 var httpHandler = function (request, response) {
-    var uri, subs, useragent, agent, server_set, server, nick, debug, touchscreen;
+    var uri, subs, useragent, agent, server_set, server, nick, debug, touchscreen, hash;
+    var min = {};
     if (config.handle_http) {
         uri = url.parse(request.url);
         subs = uri.pathname.substr(0, 4);
-        if ((subs === '/js/') || (subs === '/css') || (subs === '/img')) {
+        if (uri.pathname === '/js/all.js') {
+            if (cache.alljs === '') {
+                min.util = fs.readFileSync(config.public_http + 'js/util.js');
+                min.gateway = fs.readFileSync(config.public_http + 'js/gateway.js');
+                min.front = fs.readFileSync(config.public_http + 'js/front.js');
+                min.iscroll = fs.readFileSync(config.public_http + 'js/iscroll.js');
+                min.ast = jsp.parse(min.util + min.gateway + min.front + min.iscroll);
+                min.ast = pro.ast_mangle(min.ast);
+                min.ast = pro.ast_squeeze(min.ast);
+                min.final_code = pro.gen_code(min.ast);
+                cache.alljs = min.final_code;
+                hash = crypto.createHash('md5').update(cache.alljs);
+                cache.alljs_hash = hash.digest('base64');
+            }
+            if (request.headers['if-none-match'] === cache.alljs_hash) {
+                response.statusCode = 304;
+            } else {
+                response.setHeader('ETag', cache.alljs_hash);
+                response.write(cache.alljs);
+            }
+            response.end();
+        } else if ((subs === '/js/') || (subs === '/css') || (subs === '/img')) {
             request.addListener('end', function () {
                 fileServer.serve(request, response);
             });
@@ -485,16 +511,33 @@ var httpHandler = function (request, response) {
                 server = 'irc.anonnet.org';
                 nick = '';
             }
-            response.setHeader('Connection', 'close');
             response.setHeader('X-Generated-By', 'KiwiIRC');
-            jade.renderFile(__dirname + '/client/index.html.jade', { locals: { "touchscreen": touchscreen, "debug": debug, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": config }}, function (err, html) {
-                if (!err) {
-                    response.write(html);
+            hash = crypto.createHash('md5').update(touchscreen?'t':'f').update(debug?'t':'f').update(server_set?'t':'f').update(server).update(nick).update(agent).update(JSON.stringify(config)).digest('base64');
+            if (cache.html[hash]) {
+                if (request.headers['if-none-match'] === cache.html[hash].hash) {
+                    response.statusCode = 304;
                 } else {
-                    response.statusCode = 500;
+                    response.setHeader('Etag', cache.html[hash].hash);
+                    response.write(cache.html[hash].html);
                 }
                 response.end();
-            });
+            } else {
+                jade.renderFile(__dirname + '/client/index.html.jade', { locals: { "touchscreen": touchscreen, "debug": debug, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": config }}, function (err, html) {
+                    if (!err) {
+                        var hash2 = crypto.createHash('md5').update(html).digest('base64');
+                        cache.html[hash] = {"html": html, "hash": hash2};
+                        if (request.headers['if-none-match'] === hash2) {
+                            response.statusCode = 304;
+                        } else {
+                            response.setHeader('Etag', hash2);
+                            response.write(html);
+                        }
+                    } else {
+                        response.statusCode = 500;
+                    }
+                    response.end();
+                });
+            }
         } else if (uri.pathname.substr(0, 10) === '/socket.io') {
             return;
         } else {
@@ -515,6 +558,15 @@ if (config.listen_ssl) {
     httpServer.listen(config.port, config.bind_address);
 }
 io.set('log level', 1);
+io.enable('browser client minification');
+io.enable('browser client etag');
+io.set('transports', [
+    'websocket',
+    'flashsocket',
+    'htmlfile',
+    'xhr-polling',
+    'jsonp-polling'
+]);
 
 // Now we're listening on the network, set our UID/GIDs if required
 changeUser();