+// The timer used to touch the file every 30 seconds if nobody
+// else does it for us. This makes sure we don't have clients
+// hanging indefinitely (which might have them return errors).
+var touch_timer = undefined;
+
+// If we are behind Varnish, we can't count the number of clients
+// ourselves, so we need to get it from parsing varnishncsa.
+var viewer_count_override = undefined;
+
+var replace_json = function(new_json_contents, mtime) {
+ // Generate the list of diffs from the last five versions.
+ if (json !== undefined) {
+ // If two versions have the same mtime, clients could have either.
+ // Note the fact, so that we never insert it.
+ if (json.last_modified == mtime) {
+ json.invalid_base = true;
+ }
+ if (!json.invalid_base) {
+ historic_json.push(json);
+ if (historic_json.length > HISTORY_TO_KEEP) {
+ historic_json.shift();
+ }
+ }
+ }
+
+ var new_json = {
+ parsed: JSON.parse(new_json_contents),
+ plain: new_json_contents,
+ last_modified: mtime
+ };
+ create_json_historic_diff(new_json, historic_json.slice(0), {}, function(new_diff_json) {
+ // gzip the new version (non-delta), and put it into place.
+ zlib.gzip(new_json_contents, function(err, buffer) {
+ if (err) throw err;
+
+ new_json.gzip = buffer;
+ json = new_json;
+ diff_json = new_diff_json;
+ json_lock = 0;
+
+ // Finally, wake up any sleeping clients.
+ possibly_wakeup_clients();
+ });
+ });
+}
+
+var create_json_historic_diff = function(new_json, history_left, new_diff_json, cb) {
+ if (history_left.length == 0) {
+ cb(new_diff_json);
+ return;
+ }
+
+ var histobj = history_left.shift();
+ var diff = delta.JSON_delta.diff(histobj.parsed, new_json.parsed);
+ var diff_text = JSON.stringify(diff);
+ zlib.gzip(diff_text, function(err, buffer) {
+ if (err) throw err;
+ new_diff_json[histobj.last_modified] = {
+ parsed: diff,
+ plain: diff_text,
+ gzip: buffer,
+ last_modified: new_json.last_modified,
+ };
+ create_json_historic_diff(new_json, history_left, new_diff_json, cb);
+ });
+}
+