-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathUpdateCheck.ts
More file actions
72 lines (62 loc) · 1.8 KB
/
Copy pathUpdateCheck.ts
File metadata and controls
72 lines (62 loc) · 1.8 KB
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
68
69
70
71
72
'use strict';
import semver from 'semver';
import settings, {getEpVersion} from './Settings';
const headers = {
'User-Agent': 'Etherpad/' + getEpVersion(),
}
type Infos = {
latestVersion: string
}
const updateInterval = 60 * 60 * 1000; // 1 hour
let infos: Infos;
let lastLoadingTime: number | null = null;
let loggedDisabled = false;
const loadEtherpadInformations = () => {
if (lastLoadingTime !== null && Date.now() - lastLoadingTime < updateInterval) {
return infos;
}
return fetch(`${settings.updateServer}/info.json`, {headers})
.then(async (resp) => {
if (!resp.ok) throw new Error(`HTTP ${resp.status} ${resp.statusText}`);
infos = await resp.json() as Infos;
if (infos === undefined || infos === null) {
await Promise.reject("Could not retrieve current version")
return
}
lastLoadingTime = Date.now();
return infos;
})
.catch(async (err: Error) => {
throw err;
});
}
export const getLatestVersion = () => {
if (!settings.privacy.updateCheck) return undefined;
needsUpdate().catch();
return infos?.latestVersion;
};
const needsUpdate = async (cb?: Function) => {
try {
const info = await loadEtherpadInformations()
if (semver.gt(info!.latestVersion, getEpVersion())) {
if (cb) return cb(true);
}
} catch (err) {
console.error(`Can not perform Etherpad update check: ${err}`);
if (cb) return cb(false);
}
};
export const check = () => {
if (!settings.privacy.updateCheck) {
if (!loggedDisabled) {
console.info('Update check disabled by privacy.updateCheck=false (see PRIVACY.md)');
loggedDisabled = true;
}
return;
}
needsUpdate((needsUpdate: boolean) => {
if (needsUpdate) {
console.warn(`Update available: Download the actual version ${infos.latestVersion}`);
}
}).then(()=>{});
};