]> git.sesse.net Git - remoteglot/blobdiff - server/serve-analysis.js
Tiny simplification in serve-analysis.js.
[remoteglot] / server / serve-analysis.js
index ed11e10a06edd810bc6d09cd99963c2fc615efbb..4c9a24e9a2abf922e59a273c425a9f5cb3d32337 100644 (file)
@@ -134,6 +134,17 @@ var create_json_historic_diff = function(new_json, history_left, new_diff_json,
        var histobj = history_left.shift();
        var diff = delta.JSON_delta.diff(histobj.parsed, new_json.parsed);
        var diff_text = JSON.stringify(diff);
+
+       // Verify that the delta is correct
+       var base = JSON.parse(histobj.plain);
+       delta.JSON_delta.patch(base, diff);
+       var correct_pv = JSON.stringify(base['pv']);
+       var wrong_pv = JSON.stringify(new_json.parsed['pv']);
+       if (correct_pv !== wrong_pv) {
+               console.log("Patch went wrong:", histobj.plain, new_json.plain);
+               exit();
+       }
+
        zlib.gzip(diff_text, function(err, buffer) {
                if (err) throw err;
                new_diff_json[histobj.last_modified] = {
@@ -146,6 +157,23 @@ var create_json_historic_diff = function(new_json, history_left, new_diff_json,
        });
 }
 
+function read_entire_file(filename, callback) {
+       fs.open(filename, 'r', function(err, fd) {
+               if (err) throw err;
+               fs.fstat(fd, function(err, st) {
+                       if (err) throw err;
+                       var buffer = new Buffer(1048576);
+                       fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
+                               if (err) throw err;
+                               fs.close(fd, function() {
+                                       var contents = buffer.toString('utf8', 0, bytesRead);
+                                       callback(contents, st.mtime.getTime());
+                               });
+                       });
+               });
+       });
+}
+
 var reread_file = function(event, filename) {
        if (filename != path.basename(json_filename)) {
                return;
@@ -162,19 +190,8 @@ var reread_file = function(event, filename) {
        json_lock = 1;
 
        console.log("Rereading " + json_filename);
-       fs.open(json_filename, 'r', function(err, fd) {
-               if (err) throw err;
-               fs.fstat(fd, function(err, st) {
-                       if (err) throw err;
-                       var buffer = new Buffer(1048576);
-                       fs.read(fd, buffer, 0, 1048576, 0, function(err, bytesRead, buffer) {
-                               if (err) throw err;
-                               fs.close(fd, function() {
-                                       var new_json_contents = buffer.toString('utf8', 0, bytesRead);
-                                       replace_json(new_json_contents, st.mtime.getTime());
-                               });
-                       });
-               });
+       read_entire_file(json_filename, function(new_json_contents, mtime) {
+               replace_json(new_json_contents, mtime);
        });
 
        if (touch_timer !== undefined) {
@@ -183,7 +200,7 @@ var reread_file = function(event, filename) {
        touch_timer = setTimeout(function() {
                console.log("Touching analysis.json due to no other activity");
                var now = Date.now() / 1000;
-               fs.utimes(json_filename, now, now);
+               fs.utimes(json_filename, now, now, function() {});
        }, 30000);
 }
 var possibly_wakeup_clients = function() {
@@ -357,12 +374,7 @@ server.on('request', function(request, response) {
        mark_recently_seen(unique);
 
        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;
-       }
+       let accept_gzip = (accept_encoding !== undefined && accept_encoding.match(/\bgzip\b/));
 
        // If we already have something newer than what the user has,
        // just send it out and be done with it.