Skip to content

Commit a7d80d1

Browse files
committed
fixup!
1 parent 23ef046 commit a7d80d1

File tree

3 files changed

+92
-76
lines changed

3 files changed

+92
-76
lines changed

apps/site/next-data/generators/supportersData.mjs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import { fetchWithRetry } from '#site/util/fetch';
77
*
88
* @returns {Promise<Array<import('#site/types/supporters').OpenCollectiveSupporter>>} Array of supporters
99
*/
10-
export default () =>
11-
fetchWithRetry(OPENCOLLECTIVE_MEMBERS_URL)
12-
.then(response => response.json())
13-
.then(payload =>
14-
payload
15-
.filter(({ role, isActive }) => role === 'BACKER' && isActive)
16-
.sort((a, b) => b.totalAmountDonated - a.totalAmountDonated)
17-
.map(({ name, website, image, profile }) => ({
18-
name,
19-
image,
20-
url: website,
21-
profile,
22-
source: 'opencollective',
23-
}))
24-
);
10+
async function fetchOpenCollectiveData() {
11+
const response = await fetchWithRetry(OPENCOLLECTIVE_MEMBERS_URL);
12+
13+
const payload = await response.json();
14+
15+
const members = payload
16+
.filter(({ role, isActive }) => role === 'BACKER' && isActive)
17+
.sort((a, b) => b.totalAmountDonated - a.totalAmountDonated)
18+
.map(({ name, website, image, profile }) => ({
19+
name,
20+
image,
21+
url: website,
22+
profile,
23+
source: 'opencollective',
24+
}));
25+
26+
return members;
27+
}
28+
29+
export default fetchOpenCollectiveData;

apps/site/next-data/generators/vulnerabilities.mjs

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,75 @@ const VER_REGEX = /^\d+\.x$/;
1111
*
1212
* @returns {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities>} Grouped vulnerabilities
1313
*/
14-
export default () =>
15-
fetchWithRetry(VULNERABILITIES_URL)
16-
.then(response => response.json())
17-
.then(payload => {
18-
/** @type {Array<import('#site/types/vulnerabilities').RawVulnerability>} */
19-
const data = Object.values(payload);
20-
21-
/** @type {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities> */
22-
const grouped = {};
23-
24-
// Helper function to add vulnerability to a major version group
25-
const addToGroup = (majorVersion, vulnerability) => {
26-
grouped[majorVersion] ??= [];
27-
grouped[majorVersion].push(vulnerability);
28-
};
29-
30-
// Helper function to process version patterns
31-
const processVersion = (version, vulnerability) => {
32-
// Handle 0.X versions (pre-semver)
33-
if (V0_REGEX.test(version)) {
34-
addToGroup('0', vulnerability);
35-
36-
return;
37-
}
38-
39-
// Handle simple major.x patterns (e.g., 12.x)
40-
if (VER_REGEX.test(version)) {
41-
const majorVersion = version.split('.')[0];
14+
export default async function generateVulnerabilityData() {
15+
const response = await fetchWithRetry(VULNERABILITIES_URL);
4216

43-
addToGroup(majorVersion, vulnerability);
17+
/** @type {Array<import('#site/types/vulnerabilities').RawVulnerability>} */
18+
const data = Object.values(await response.json());
4419

45-
return;
46-
}
20+
/** @type {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities> */
21+
const grouped = {};
4722

48-
// Handle version ranges (>, >=, <, <=)
49-
const rangeMatch = RANGE_REGEX.exec(version);
23+
// Helper function to add vulnerability to a major version group
24+
const addToGroup = (majorVersion, vulnerability) => {
25+
grouped[majorVersion] ??= [];
26+
grouped[majorVersion].push(vulnerability);
27+
};
5028

51-
if (rangeMatch) {
52-
const [, operator, majorVersion] = rangeMatch;
29+
// Helper function to process version patterns
30+
const processVersion = (version, vulnerability) => {
31+
// Handle 0.X versions (pre-semver)
32+
if (V0_REGEX.test(version)) {
33+
addToGroup('0', vulnerability);
5334

54-
const majorNum = parseInt(majorVersion, 10);
35+
return;
36+
}
5537

56-
switch (operator) {
57-
case '>=':
58-
case '>':
59-
case '<=':
60-
addToGroup(majorVersion, vulnerability);
38+
// Handle simple major.x patterns (e.g., 12.x)
39+
if (VER_REGEX.test(version)) {
40+
const majorVersion = version.split('.')[0];
6141

62-
break;
63-
case '<':
64-
// Add to all major versions below the specified version
65-
for (let i = majorNum - 1; i >= 0; i--) {
66-
addToGroup(i.toString(), vulnerability);
67-
}
42+
addToGroup(majorVersion, vulnerability);
6843

69-
break;
70-
}
71-
}
72-
};
44+
return;
45+
}
46+
47+
// Handle version ranges (>, >=, <, <=)
48+
const rangeMatch = RANGE_REGEX.exec(version);
7349

74-
for (const { ref, ...vulnerability } of Object.values(data)) {
75-
vulnerability.url = ref;
76-
// Process all potential versions from the vulnerable field
77-
const versions = vulnerability.vulnerable.split(' || ').filter(Boolean);
50+
if (rangeMatch) {
51+
const [, operator, majorVersion] = rangeMatch;
52+
53+
const majorNum = parseInt(majorVersion, 10);
54+
55+
switch (operator) {
56+
case '>=':
57+
case '>':
58+
case '<=':
59+
addToGroup(majorVersion, vulnerability);
7860

79-
for (const version of versions) {
80-
processVersion(version, vulnerability);
81-
}
61+
break;
62+
case '<':
63+
// Add to all major versions below the specified version
64+
for (let i = majorNum - 1; i >= 0; i--) {
65+
addToGroup(i.toString(), vulnerability);
66+
}
67+
68+
break;
8269
}
70+
}
71+
};
72+
73+
for (const { ref, ...vulnerability } of Object.values(data)) {
74+
vulnerability.url = ref;
75+
76+
// Process all potential versions from the vulnerable field
77+
const versions = vulnerability.vulnerable.split(' || ').filter(Boolean);
78+
79+
for (const version of versions) {
80+
processVersion(version, vulnerability);
81+
}
82+
}
8383

84-
return grouped;
85-
});
84+
return grouped;
85+
}

apps/site/util/fetch.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ type RetryOptions = RequestInit & {
55
delay?: number;
66
};
77

8+
type FetchError = {
9+
cause: {
10+
code: string;
11+
};
12+
};
13+
814
export const fetchWithRetry = async (
915
url: string,
1016
{ maxRetry = 3, delay = 100, ...options }: RetryOptions = {}
@@ -13,7 +19,12 @@ export const fetchWithRetry = async (
1319
try {
1420
return fetch(url, options);
1521
} catch (e) {
16-
if (i === maxRetry) {
22+
console.debug(
23+
`fetch of ${url} failed at ${Date.now()}, attempt ${i}/${maxRetry}`,
24+
e
25+
);
26+
27+
if (i === maxRetry || (e as FetchError).cause.code !== 'ETIMEDOUT') {
1728
throw e;
1829
}
1930

0 commit comments

Comments
 (0)