-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
fix(fetch): fixup generators and add error handling #8395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+67
−23
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,24 @@ | ||
| import { OPENCOLLECTIVE_MEMBERS_URL } from '#site/next.constants.mjs'; | ||
| import { fetchWithRetry } from '#site/util/fetch'; | ||
|
|
||
| /** | ||
| * Fetches supporters data from Open Collective API, filters active backers, | ||
| * and maps it to the Supporters type. | ||
| * | ||
| * @returns {Promise<Array<import('#site/types/supporters')>>} Array of supporters | ||
| * @returns {Promise<Array<import('#site/types/supporters').OpenCollectiveSupporter>>} Array of supporters | ||
| */ | ||
| async function fetchOpenCollectiveData() { | ||
| const endpoint = 'https://opencollective.com/nodejs/members/all.json'; | ||
|
|
||
| const response = await fetch(endpoint); | ||
|
|
||
| const payload = await response.json(); | ||
|
|
||
| const members = payload | ||
| .filter(({ role, isActive }) => role === 'BACKER' && isActive) | ||
| .sort((a, b) => b.totalAmountDonated - a.totalAmountDonated) | ||
| .map(({ name, website, image, profile }) => ({ | ||
| name, | ||
| image, | ||
| url: website, | ||
| profile, | ||
| source: 'opencollective', | ||
| })); | ||
|
|
||
| return members; | ||
| } | ||
|
|
||
| export default fetchOpenCollectiveData; | ||
| export default () => | ||
| fetchWithRetry(OPENCOLLECTIVE_MEMBERS_URL) | ||
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .then(response => response.json()) | ||
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .then(payload => | ||
| payload | ||
| .filter(({ role, isActive }) => role === 'BACKER' && isActive) | ||
| .sort((a, b) => b.totalAmountDonated - a.totalAmountDonated) | ||
| .map(({ name, website, image, profile }) => ({ | ||
| name, | ||
| image, | ||
| url: website, | ||
| profile, | ||
| source: 'opencollective', | ||
| })) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,93 +1,85 @@ | ||
| import { VULNERABILITIES_URL } from '#site/next.constants.mjs'; | ||
| import { fetchWithRetry } from '#site/util/fetch'; | ||
|
|
||
| const RANGE_REGEX = /([<>]=?)\s*(\d+)(?:\.(\d+))?/; | ||
| const V0_REGEX = /^0\.\d+(\.x)?$/; | ||
| const VER_REGEX = /^\d+\.x$/; | ||
|
|
||
| /** | ||
| * Fetches vulnerability data from the Node.js Security Working Group repository, | ||
| * and returns it grouped by major version. | ||
| * | ||
| * @returns {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities>} Grouped vulnerabilities | ||
| */ | ||
| export default async function generateVulnerabilityData() { | ||
| const response = await fetch(VULNERABILITIES_URL); | ||
| export default () => | ||
| fetchWithRetry(VULNERABILITIES_URL) | ||
| .then(response => response.json()) | ||
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .then(payload => { | ||
| /** @type {Array<import('#site/types/vulnerabilities').RawVulnerability>} */ | ||
| const data = Object.values(payload); | ||
|
|
||
| /** @type {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities> */ | ||
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const grouped = {}; | ||
|
|
||
| // Helper function to add vulnerability to a major version group | ||
| const addToGroup = (majorVersion, vulnerability) => { | ||
| grouped[majorVersion] ??= []; | ||
| grouped[majorVersion].push(vulnerability); | ||
| }; | ||
|
|
||
| // Helper function to process version patterns | ||
| const processVersion = (version, vulnerability) => { | ||
| // Handle 0.X versions (pre-semver) | ||
| if (V0_REGEX.test(version)) { | ||
| addToGroup('0', vulnerability); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Handle simple major.x patterns (e.g., 12.x) | ||
| if (VER_REGEX.test(version)) { | ||
| const majorVersion = version.split('.')[0]; | ||
|
|
||
| /** @type {Array<import('#site/types/vulnerabilities').RawVulnerability>} */ | ||
| const data = Object.values(await response.json()); | ||
|
|
||
| /** @type {Promise<import('#site/types/vulnerabilities').GroupedVulnerabilities> */ | ||
| const grouped = {}; | ||
|
|
||
| // Helper function to add vulnerability to a major version group | ||
| const addToGroup = (majorVersion, vulnerability) => { | ||
| grouped[majorVersion] ??= []; | ||
| grouped[majorVersion].push(vulnerability); | ||
| }; | ||
|
|
||
| // Helper function to process version patterns | ||
| const processVersion = (version, vulnerability) => { | ||
| // Handle 0.X versions (pre-semver) | ||
| if (/^0\.\d+(\.x)?$/.test(version)) { | ||
| addToGroup('0', vulnerability); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Handle simple major.x patterns (e.g., 12.x) | ||
| if (/^\d+\.x$/.test(version)) { | ||
| const majorVersion = version.split('.')[0]; | ||
| addToGroup(majorVersion, vulnerability); | ||
|
|
||
| addToGroup(majorVersion, vulnerability); | ||
| return; | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| // Handle version ranges (>, >=, <, <=) | ||
| const rangeMatch = RANGE_REGEX.exec(version); | ||
|
|
||
| // Handle version ranges (>, >=, <, <=) | ||
| const rangeMatch = RANGE_REGEX.exec(version); | ||
| if (rangeMatch) { | ||
| const [, operator, majorVersion] = rangeMatch; | ||
|
|
||
| if (rangeMatch) { | ||
| const [, operator, majorVersion] = rangeMatch; | ||
| const majorNum = parseInt(majorVersion, 10); | ||
|
|
||
| const majorNum = parseInt(majorVersion, 10); | ||
| switch (operator) { | ||
| case '>=': | ||
| case '>': | ||
| case '<=': | ||
| addToGroup(majorVersion, vulnerability); | ||
|
|
||
| switch (operator) { | ||
| case '>=': | ||
| case '>': | ||
| case '<=': | ||
| addToGroup(majorVersion, vulnerability); | ||
| break; | ||
| case '<': | ||
| // Add to all major versions below the specified version | ||
| for (let i = majorNum - 1; i >= 0; i--) { | ||
| addToGroup(i.toString(), vulnerability); | ||
| } | ||
|
|
||
| break; | ||
| case '<': | ||
| // Add to all major versions below the specified version | ||
| for (let i = majorNum - 1; i >= 0; i--) { | ||
| addToGroup(i.toString(), vulnerability); | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| break; | ||
| for (const { ref, ...vulnerability } of Object.values(data)) { | ||
| vulnerability.url = ref; | ||
| // Process all potential versions from the vulnerable field | ||
| const versions = vulnerability.vulnerable.split(' || ').filter(Boolean); | ||
|
|
||
| for (const version of versions) { | ||
| processVersion(version, vulnerability); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (const vulnerability of Object.values(data)) { | ||
| const parsedVulnerability = { | ||
| cve: vulnerability.cve, | ||
| url: vulnerability.ref, | ||
| vulnerable: vulnerability.vulnerable, | ||
| patched: vulnerability.patched, | ||
| description: vulnerability.description, | ||
| overview: vulnerability.overview, | ||
| affectedEnvironments: vulnerability.affectedEnvironments, | ||
| severity: vulnerability.severity, | ||
| }; | ||
|
|
||
| // Process all potential versions from the vulnerable field | ||
| const versions = parsedVulnerability.vulnerable | ||
| .split(' || ') | ||
| .filter(Boolean); | ||
|
|
||
| for (const version of versions) { | ||
| processVersion(version, parsedVulnerability); | ||
| } | ||
| } | ||
|
|
||
| return grouped; | ||
| } | ||
|
|
||
| return grouped; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export type Supporter<T extends string> = { | ||
| name: string; | ||
| image: string; | ||
| url: string; | ||
| profile: string; | ||
| source: T; | ||
| }; | ||
|
|
||
| export type OpenCollectiveSupporter = Supporter<'opencollective'>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { setTimeout } from 'node:timers/promises'; | ||
|
|
||
| type RetryOptions = RequestInit & { | ||
| maxRetry?: number; | ||
| delay?: number; | ||
| }; | ||
|
|
||
| export const fetchWithRetry = async ( | ||
| url: string, | ||
| { maxRetry = 3, delay = 100, ...options }: RetryOptions = {} | ||
| ) => { | ||
| for (let i = 1; i <= maxRetry; i++) { | ||
| try { | ||
| return fetch(url, options); | ||
| } catch (e) { | ||
| if (i === maxRetry) { | ||
| throw e; | ||
| } | ||
|
|
||
| await setTimeout(delay); | ||
araujogui marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue; | ||
avivkeller marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.