forked from C4illin/GeoIP-comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (67 loc) · 2.41 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const axios = require("axios");
const sites = require("./docs/sites.json");
const fs = require("fs");
require("dotenv").config();
async function testAPI(url) {
const start = Date.now();
try {
const response = await axios.get(url);
const end = Date.now();
const data = response.data;
return { time: end - start, data: data };
} catch (error) {
console.error(error);
}
}
async function testAll() {
for (const [api, details] of Object.entries(sites)) {
if (details.client && details.client.length > 0) {
let url = details.client;
if (url.includes("YOUR-APIKEY")) {
url = url.replace("YOUR-APIKEY", process.env[api]);
}
const result = await testAPI(url);
sites[api]["clientTime"] = result.time;
sites[api]["clientData"] = result.data;
}
if (details.server && details.server.length > 0) {
let url = details.server;
if (url.includes("YOUR-APIKEY")) {
url = url.replace("YOUR-APIKEY", process.env[api]);
}
const result = await testAPI(url);
sites[api]["serverTime"] = result.time;
sites[api]["serverData"] = result.data;
}
}
fs.writeFileSync("output.json", JSON.stringify(sites, null, 2));
return sites;
}
function markdownTableForSites(data) {
let markdown =
"## Sites\n| Url | Https | Limit | Clientside | Client lookup delay | Serverside delay |\n";
markdown += "| --- | --- | --- | --- | --- | --- |\n";
for (const [api, details] of Object.entries(data)) {
markdown += `| ${details.homepage} | <ul><li>- ${details.https ? "[x]" : "[ ]"} </li></ul> | ${details.limit} | <ul><li>- ${details.client ? "[x]" : "[ ]"} </li></ul> | ${details.clientTime ? details.clientTime + " ms" : "n/a"} | ${details.serverTime ? details.serverTime + " ms" : ""} |\n`;
}
return markdown;
}
function markdownTableResponses(data) {
let markdown = "## Example response\n| API | Serverside Lookup |\n";
markdown += "| --- | --- |\n";
for (const [api, details] of Object.entries(data)) {
// const clientData = details.clientData ? JSON.stringify(details.clientData, null, 2).replaceAll("\n", "<br>") : '';
const serverData = details.serverData
? JSON.stringify(details.serverData, null, 2).replaceAll("\n", "<br>")
: "";
markdown += `| ${api} | <pre>${serverData}</pre> |\n`;
}
return markdown;
}
testAll().then((data) => {
let header = fs.readFileSync("header.md", "utf8");
fs.writeFileSync(
"readme.md",
header + markdownTableForSites(data) + markdownTableResponses(data),
);
});