-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.js
67 lines (57 loc) · 1.93 KB
/
state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const assert = require('assert').strict;
const fs = require('fs');
const {promisify} = require('util');
const {deepEqual} = require('./data_utils.js');
async function initState(stateFile) {
if (stateFile) {
try {
const json = await promisify(fs.readFile)(stateFile, {encoding: 'utf-8'});
const res = JSON.parse(json);
assert(res.versions);
assert(res.runs);
return res;
} catch(e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
}
return {
// Array of HTML versions {htmlHash, html, jsURLs: Array of URLs, firstSeen, lastSeen}
versions: [],
// Array of objects:
// results: Array of runData (see check function),
// knownVersions: Array of htmlHash values of all known versions at the time of run
runs: [],
// Object, JavaScript URL => {hash, content}
referenceContent: {},
};
}
async function writeState(stateFile, state) {
if (!stateFile) return;
// Write atomically to avoid data corruption
const tmpFile = stateFile + '.tmp';
await promisify(fs.writeFile)(tmpFile, JSON.stringify(state), {encoding: 'utf-8'});
await promisify(fs.rename)(tmpFile, stateFile);
}
function integrateCheckResult(state, results, finishedTimestamp) {
assert.strictEqual(typeof finishedTimestamp, 'number');
const knownVersions = state.versions.map(v => v.htmlHash);
const lastRun = (state.runs.length > 0) && state.runs[state.runs.length - 1];
if (lastRun && deepEqual(lastRun.results, results)) {
lastRun.lastFinished = finishedTimestamp;
lastRun.knownVersions = knownVersions;
return;
}
state.runs.push({
firstFinished: finishedTimestamp,
lastFinished: finishedTimestamp,
results,
knownVersions,
});
}
module.exports = {
initState,
integrateCheckResult,
writeState,
};