]> git.sesse.net Git - remoteglot/commitdiff
Stick most of the properties of the served object in serve-analysis into one package.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 24 Nov 2014 23:40:58 +0000 (00:40 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 24 Nov 2014 23:40:58 +0000 (00:40 +0100)
www/serve-analysis.js

index 100cd4a6f53e4c2340a02bd5d3ce84d65241b2cd..c970c00ce7b418f01232380bf093d985523675ea 100644 (file)
@@ -13,9 +13,7 @@ var zlib = require('zlib');
 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_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;
+var json = undefined;
 
 // The list of clients that are waiting for new data to show up.
 // Uniquely keyed by request_id so that we can take them out of
 
 // The list of clients that are waiting for new data to show up.
 // Uniquely keyed by request_id so that we can take them out of
@@ -36,6 +34,25 @@ var touch_timer = undefined;
 // ourselves, so some external log-tailing daemon needs to tell us.
 var viewer_count_override = undefined;
 
 // ourselves, so some external log-tailing daemon needs to tell us.
 var viewer_count_override = undefined;
 
+var replace_json = function(new_json_contents, mtime) {
+       var new_json = {
+               parsed: JSON.parse(new_json_contents),
+               plain: new_json_contents,
+               last_modified: mtime
+       };
+
+       // gzip the new version, and put it into place.
+       zlib.gzip(new_json_contents, function(err, buffer) {
+               if (err) throw err;
+
+               new_json.gzip = buffer;
+               json = new_json;
+
+               // Finally, wake up any sleeping clients.
+               possibly_wakeup_clients();
+       });
+}
+
 var reread_file = function(event, filename) {
        if (filename != path.basename(json_filename)) {
                return;
 var reread_file = function(event, filename) {
        if (filename != path.basename(json_filename)) {
                return;
@@ -50,13 +67,7 @@ var reread_file = function(event, filename) {
                                if (err) throw err;
                                fs.close(fd, function() {
                                        var new_json_contents = buffer.toString('utf8', 0, bytesRead);
                                if (err) throw err;
                                fs.close(fd, function() {
                                        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();
-                                       });
+                                       replace_json(new_json_contents, st.mtime.getTime());
                                });
                        });
                });
                                });
                        });
                });
@@ -106,7 +117,7 @@ var handle_viewer_override = function(request, u, response) {
 var send_json = function(response, accept_gzip, num_viewers) {
        var headers = {
                'Content-Type': 'text/json',
 var send_json = function(response, accept_gzip, num_viewers) {
        var headers = {
                'Content-Type': 'text/json',
-               'X-Remoteglot-Last-Modified': json_last_modified,
+               'X-Remoteglot-Last-Modified': json.last_modified,
                'X-Remoteglot-Num-Viewers': num_viewers,
                'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
                'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
                'X-Remoteglot-Num-Viewers': num_viewers,
                'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
                'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
@@ -116,10 +127,10 @@ var send_json = function(response, accept_gzip, num_viewers) {
        if (accept_gzip) {
                headers['Content-Encoding'] = 'gzip';
                response.writeHead(200, headers);
        if (accept_gzip) {
                headers['Content-Encoding'] = 'gzip';
                response.writeHead(200, headers);
-               response.write(json_contents_gz);
+               response.write(json.gzip);
        } else {
                response.writeHead(200, headers);
        } else {
                response.writeHead(200, headers);
-               response.write(json_contents);
+               response.write(json.text);
        }
        response.end();
 }
        }
        response.end();
 }
@@ -191,7 +202,7 @@ server.on('request', function(request, response) {
 
        // If we already have something newer than what the user has,
        // just send it out and be done with it.
 
        // 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)) {
+       if (json !== undefined && (!ims || json.last_modified > ims)) {
                send_json(response, accept_gzip, count_viewers());
                return;
        }
                send_json(response, accept_gzip, count_viewers());
                return;
        }