From 02b2c80e9277839934843501eacdc47980b56b94 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Mon, 15 Aug 2011 22:02:52 +0100 Subject: [PATCH] Minifcation of all javascript, plus caching of the minifies js and main HTML. Also enabled socket.io minifcation and caching. Enabled full range of socket.io transports. --- node/client/index.html.jade | 9 ++--- node/kiwi.js | 68 ++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/node/client/index.html.jade b/node/client/index.html.jade index a4fd68a..c196c7f 100644 --- a/node/client/index.html.jade +++ b/node/client/index.html.jade @@ -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") diff --git a/node/kiwi.js b/node/kiwi.js index 2982a38..b1d939e 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -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(); -- 2.25.1