-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.6 KB
/
index.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
import http from "node:http";
import { execFile } from "node:child_process";
import { argv, exit } from "node:process";
import { JSDOM } from "jsdom";
const {
HOST = "0.0.0.0",
PORT = 3000,
UPDATE_INTERVAL = 86400,
ON_CHANGE
} = process.env;
const changelogURL =
"https://developers.facebook.com/docs/graph-api/changelog?locale=en_US";
let state, expires;
const onChange = async ({ version, updatedAt }) => {
console.info(`${updatedAt.toISOString()} - New API version is ${version}`);
if (!ON_CHANGE) return;
if (URL.canParse(ON_CHANGE))
return fetch(ON_CHANGE, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ version, updatedAt })
});
try {
execFile(ON_CHANGE, [version, updatedAt.toISOString()]);
} catch {}
};
const init = async () => {
const dom = await JSDOM.fromURL(changelogURL);
const version =
dom.window.document.querySelector("h1 + table code").textContent;
const updatedAt = new Date();
state && state.version !== version && onChange({ version, updatedAt });
state = { version, updatedAt };
expires = new Date(
new Date(updatedAt).setSeconds(updatedAt.getSeconds() + UPDATE_INTERVAL)
);
setInterval(init, UPDATE_INTERVAL * 1000);
};
await init();
if (argv[2] === "--cli") {
console.info(state.version);
exit();
}
const server = http.createServer((_req, res) => {
res.setHeader("Content-Type", "application/json");
res.setHeader("Expires", expires.toUTCString());
res.end(JSON.stringify(state));
});
server.listen(PORT, HOST, () =>
console.log(`Listening on http://${HOST}:${PORT}`)
);