Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/site/next-data/generators/majorNodeReleases.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import nodevu from '@nodevu/core';

import { fetchWithRetry } from '#site/util/fetch';

/**
* Filters Node.js release data to return only major releases with documented support.
*/
export default async function getMajorNodeReleases() {
const nodevuData = await nodevu({ fetch });
const nodevuData = await nodevu({ fetch: fetchWithRetry });

return Object.entries(nodevuData).filter(([version, { support }]) => {
// Filter out those without documented support
Expand Down
19 changes: 9 additions & 10 deletions apps/site/util/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { setTimeout } from 'node:timers/promises';

type RetryOptions = RequestInit & {
maxRetry?: number;
delay?: number;
};

type FetchError = {
cause: {
code: string;
};
};
const isTimeoutError = (e: unknown): boolean =>
e instanceof Error &&
typeof e.cause === 'object' &&
e.cause !== null &&
'code' in e.cause &&
e.cause.code === 'ETIMEDOUT';

export const fetchWithRetry = async (
url: string,
{ maxRetry = 3, delay = 100, ...options }: RetryOptions = {}
) => {
for (let i = 1; i <= maxRetry; i++) {
try {
return fetch(url, options);
return await fetch(url, options);
} catch (e) {
console.debug(
`fetch of ${url} failed at ${Date.now()}, attempt ${i}/${maxRetry}`,
e
);

if (i === maxRetry || (e as FetchError).cause.code !== 'ETIMEDOUT') {
if (i === maxRetry || !isTimeoutError(e)) {
throw e;
}

await setTimeout(delay * i);
await new Promise(resolve => setTimeout(resolve, delay * i));
}
}
};
Loading