forked from pusuktv/links.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-registry.js
108 lines (94 loc) · 3.17 KB
/
check-registry.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const fs = require("fs");
const yaml = require("yaml");
const axios = require("axios");
const crypto = require("crypto");
const checkDuplicateGithubUserNames = require("./util/duplicate-check-gh-username");
const getNewUsers = require("./util/get-new-users");
const downloadRegistry = require("./util/download-registry");
const getRestrictedUsernames = require("./util/get-restricted-usernames");
const getRegistry = require("./util/get-registry");
const {
validateGitHubUsernameExistsOnRegistry,
validateUsernameIsNotRestricted,
validatePage,
} = require("./util/validation");
const { productionRegistryUri } = require("./util/constant");
const hashedContents = new Set();
function getHash(pageJsonContent) {
return crypto
.createHash("sha256")
.update(JSON.stringify(pageJsonContent))
.digest("hex");
}
async function validateRegistry() {
const restrictedUsernames = getRestrictedUsernames();
const registry = getRegistry();
await checkDuplicateGithubUserNames(registry.users);
await addFatihToTheRegistryForCheckingPageDuplication();
console.log("Validating " + Object.keys(registry.users).length + " users..");
for (const username in registry.users) {
const user = registry.users[username];
validateGitHubUsernameExistsOnRegistry(user);
validateUsernameIsNotRestricted(restrictedUsernames, user);
const pageJsonUrl = `https://raw.githubusercontent.com/${user.github_username}/my-links/main/page.json`;
console.log("Trying to retrieve " + pageJsonUrl);
const pageJsonResponse = await axios.get(pageJsonUrl);
console.log("Retrieved " + pageJsonUrl);
const pageJsonContent = validatePage(pageJsonResponse, user);
const hash = getHash(pageJsonContent);
if (hashedContents.has(hash)) {
console.error(
`The page.json file for user "${user}" is a duplicate ${pageJsonUrl}`
);
process.exit(1);
}
hashedContents.add(hash);
try {
const imageUrlResponse = await axios.get(pageJsonContent.image_url, {
responseType: "arraybuffer",
});
if (imageUrlResponse.status !== 200) {
throw new Error(
`Invalid image_url for user "${username}". HTTP status code: ${imageUrlResponse.status}`
);
}
} catch (error) {
if (error.response) {
console.error(
`Invalid image_url for user "${username}". HTTP status code: ${error.response.status}`
);
} else {
console.error(
`Invalid image_url for user "${username}". ${error.message}`
);
}
process.exit(1);
}
}
console.log("Registry validation successful.");
async function addFatihToTheRegistryForCheckingPageDuplication() {
registry.users = await getNewUsers(
registry.users,
await downloadRegistry(productionRegistryUri)
);
users = {
fatih: {
github_username: "fatih-yavuz",
}
};
for (const username in registry.users) {
const user = registry.users[username];
users[user.github_username] = user;
}
registry.users = users;
}
}
async function main() {
try {
await validateRegistry();
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
main();