From 1bced5737a74678b6dd3ed8e73cf14871701ec92 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 8 Nov 2014 15:27:51 +0100 Subject: [PATCH] Add support for gzip in serve-analysis.js. --- www/serve-analysis.js | 44 +++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 80f969e..d1f0bae 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -7,12 +7,14 @@ var fs = require('fs'); var url = require('url'); var querystring = require('querystring'); var path = require('path'); +var zlib = require('zlib'); // Constants. var json_filename = '/srv/analysis.sesse.net/www/analysis.json'; // The current contents of the file to hand out, and its last modified time. var json_contents = undefined; +var json_contents_gz = undefined; var json_last_modified = undefined; // The list of clients that are waiting for new data to show up, @@ -38,9 +40,14 @@ var reread_file = function(event, filename) { fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) { if (err) throw err; fs.close(fd, function() { - json_contents = buffer.toString('utf8', 0, bytesRead); - json_last_modified = st.mtime.getTime(); - possibly_wakeup_clients(); + var new_json_contents = buffer.toString('utf8', 0, bytesRead); + zlib.gzip(new_json_contents, function(err, buffer) { + if (err) throw err; + json_contents = new_json_contents; + json_contents_gz = buffer; + json_last_modified = st.mtime.getTime(); + possibly_wakeup_clients(); + }); }); }); }); @@ -49,7 +56,7 @@ var reread_file = function(event, filename) { var possibly_wakeup_clients = function() { for (var i in sleeping_clients) { clearTimeout(sleeping_clients[i].timer); - send_json(sleeping_clients[i].response); + send_json(sleeping_clients[i].response, sleeping_clients[i].accept_gzip); } sleeping_clients = {}; } @@ -60,20 +67,28 @@ var send_404 = function(response) { response.write('Something went wrong. Sorry.'); response.end(); } -var send_json = function(response) { - response.writeHead(200, { +var send_json = function(response, accept_gzip) { + var headers = { 'Content-Type': 'text/json', 'X-Remoteglot-Last-Modified': json_last_modified, 'X-Remoteglot-Num-Viewers': count_viewers(), 'Access-Control-Allow-Origin': 'http://analysis.sesse.net', 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers', 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC', - }); - response.write(json_contents); + }; + + if (accept_gzip) { + headers['Content-Encoding'] = 'gzip'; + response.writeHead(200, headers); + response.write(json_contents_gz); + } else { + response.writeHead(200, headers); + response.write(json_contents); + } response.end(); } var timeout_client = function(client) { - send_json(client.response); + send_json(client.response, client.accept_gzip); delete sleeping_clients[client.request_id]; } var count_viewers = function() { @@ -114,10 +129,18 @@ http.createServer(function(request, response) { last_seen_clients[unique] = (new Date).getTime(); } + var accept_encoding = request.headers['accept-encoding']; + var accept_gzip; + if (accept_encoding !== undefined && accept_encoding.match(/\bgzip\b/)) { + accept_gzip = true; + } else { + accept_gzip = false; + } + // If we already have something newer than what the user has, // just send it out and be done with it. if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) { - send_json(response); + send_json(response, accept_gzip); return; } @@ -128,5 +151,6 @@ http.createServer(function(request, response) { client.response = response; client.timer = setTimeout(function() { timeout_client(client); }, 30000); client.request_id = request_id; + client.accept_gzip = accept_gzip; sleeping_clients[request_id++] = client; }).listen(5000); -- 2.39.2