From cbcd1a23064ce607ca3247377641525a4721342e Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Fri, 9 Aug 2013 00:09:32 +0100 Subject: [PATCH] Don't embded settings in HTML, serve them as a separate JSON file --- client/assets/src/build.js | 52 ++-------- client/assets/src/index.html.tmpl | 107 ++++----------------- server/httphandler.js | 152 +++++++++++++++++++++++++++++- 3 files changed, 177 insertions(+), 134 deletions(-) diff --git a/client/assets/src/build.js b/client/assets/src/build.js index d06089b..6aa0611 100644 --- a/client/assets/src/build.js +++ b/client/assets/src/build.js @@ -130,47 +130,13 @@ translation_files.forEach(function (file) { * Build the index.html file */ -var index_src = fs.readFileSync(__dirname + '/index.html.tmpl', FILE_ENCODING); -var vars = { - base_path: config.get().http_base_path || '/kiwi', - cache_buster: Math.ceil(Math.random() * 9000).toString(), - server_settings: {}, - client_plugins: [] -}; - -// Any restricted server mode set? -if (config.get().restrict_server) { - vars.server_settings = { - connection: { - server: config.get().restrict_server, - port: config.get().restrict_server_port || 6667, - ssl: config.get().restrict_server_ssl, - channel: config.get().restrict_server_channel, - nick: config.get().restrict_server_nick, - allow_change: false - } - }; -} - -// Any client default settings? -if (config.get().client) { - vars.server_settings.client = config.get().client; -} - -// Any client plugins? -if (config.get().client_plugins && config.get().client_plugins.length > 0) { - vars.client_plugins = config.get().client_plugins; -} - -// Translations -vars.translations = translations; - -_.each(vars, function(value, key) { - if (typeof value === 'object') value = JSON.stringify(value); - index_src = index_src.replace(new RegExp('<%' + key + '%>', 'g'), value); +var index_src = fs.readFileSync(__dirname + '/index.html.tmpl', FILE_ENCODING) + .replace(new RegExp('<%base_path%>', 'g'), config.get().http_base_path || '/kiwi'); + +fs.writeFile(__dirname + '/../../index.html', index_src, { encoding: FILE_ENCODING }, function (err) { + if (!err) { + console.log('index.html built'); + } else { + console.log('Error building index.html'); + } }); - -fs.writeFileSync(__dirname + '/../../index.html', index_src, FILE_ENCODING); - - -console.log('index.html built'); diff --git a/client/assets/src/index.html.tmpl b/client/assets/src/index.html.tmpl index 470814c..e5fab30 100644 --- a/client/assets/src/index.html.tmpl +++ b/client/assets/src/index.html.tmpl @@ -303,13 +303,12 @@ + diff --git a/server/httphandler.js b/server/httphandler.js index 045dd2f..6a25aab 100644 --- a/server/httphandler.js +++ b/server/httphandler.js @@ -1,7 +1,9 @@ var url = require('url'), fs = require('fs'), + crypto = require('crypto'), node_static = require('node-static'), - _ = require('lodash'); + _ = require('lodash'), + config = require('./configuration.js'); @@ -43,6 +45,8 @@ HttpHandler.prototype.serve = function (request, response) { } else { response.setHeader('Content-Language', request.url.substr(16, request.url.indexOf('.') - 16)); } + } else if (request.url.substr(0, 21) === '/assets/settings.json') { + return serveSettings.call(this, request, response); } this.file_server.serve(request, response, function (err) { @@ -101,3 +105,149 @@ var serveFallbackLocale = function (request, response) { //en-gb is our default language, so we serve this as the last possible answer for everything this.file_server.serveFile('/assets/locales/en-gb.json', 200, {Vary: 'Accept-Language', 'Content-Language': 'en-gb'}, request, response); }; + +function serveSettings(request, response) { + var vars = { + server_settings: {}, + client_plugins: [], + translations: [], + scripts: [ + [ + 'libs/lodash.min.js' + ], + 'libs/backbone.min.js', + 'libs/jed.js' + ] + }, + referrer_url; + + if (request.headers['referer']) { + referrer_url = url.parse(request.headers['referer'], true); + } + if (referrer_url && referrer_url.query && referrer_url.query.debug) { + vars.scripts = vars.scripts.concat([ + 'src/app.js', + [ + 'src/models/application.js', + 'src/models/gateway.js' + ], + [ + 'src/models/newconnection.js', + 'src/models/panellist.js', + 'src/models/networkpanellist.js', + 'src/models/panel.js', + 'src/models/member.js', + 'src/models/memberlist.js', + 'src/models/network.js' + ], + + [ + 'src/models/query.js', + 'src/models/channel.js', + 'src/models/server.js', + 'src/models/applet.js' + ], + + [ + 'src/applets/settings.js', + 'src/applets/chanlist.js', + 'src/applets/scripteditor.js' + ], + + [ + 'src/models/pluginmanager.js', + 'src/models/datastore.js', + 'src/helpers/utils.js' + ], + + // Some views extend these, so make sure they're loaded beforehand + [ + 'src/views/panel.js' + ], + + [ + 'src/views/channel.js', + 'src/views/applet.js', + 'src/views/application.js', + 'src/views/apptoolbar.js', + 'src/views/controlbox.js', + 'src/views/favicon.js', + 'src/views/mediamessage.js', + 'src/views/member.js', + 'src/views/memberlist.js', + 'src/views/menubox.js', + 'src/views/networktabs.js', + 'src/views/nickchangebox.js', + 'src/views/resizehandler.js', + 'src/views/serverselect.js', + 'src/views/statusmessage.js', + 'src/views/tabs.js', + 'src/views/topicbar.js', + 'src/views/userbox.js' + ] + ]); + } else { + vars.scripts.push('kiwi.min.js'); + } + + // Any restricted server mode set? + if (config.get().restrict_server) { + vars.server_settings = { + connection: { + server: config.get().restrict_server, + port: config.get().restrict_server_port || 6667, + ssl: config.get().restrict_server_ssl, + channel: config.get().restrict_server_channel, + nick: config.get().restrict_server_nick, + allow_change: false + } + }; + } + + // Any client default settings? + if (config.get().client) { + vars.server_settings.client = config.get().client; + } + + // Any client plugins? + if (config.get().client_plugins && config.get().client_plugins.length > 0) { + vars.client_plugins = config.get().client_plugins; + } + + fs.readFile(__dirname + '/../client/assets/src/translations/translations.json', function (err, translations) { + if (err) { + response.statusCode = 500; + return response.end(); + } + + var translation_files; + translations = JSON.parse(translations); + fs.readdir(__dirname + '/../client/assets/src/translations/', function (err, pofiles) { + var hash; + if (err) { + response.statusCode = 500; + return response.end(); + } + + pofiles.forEach(function (file) { + var locale = file.slice(0, -3); + if ((file.slice(-3) === '.po') && (locale !== 'template')) { + vars.translations.push({tag: locale, language: translations[locale]}); + } + }); + + hash = crypto.createHash('md5').update(JSON.stringify(vars)).digest('hex'); + + if (request.headers['if-none-match'] && request.headers['if-none-match'] === hash) { + response.writeHead(304, 'Not Modified'); + return response.end(); + } + + response.writeHead(200, { + 'ETag': hash, + 'Content-Type': 'application/json' + }); + response.end(JSON.stringify(vars)); + }); + }); +} -- 2.25.1