Skip to content

Commit

Permalink
Use built-in fetch (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Feb 14, 2024
1 parent 056c6e3 commit 6273302
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 130 deletions.
127 changes: 0 additions & 127 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,6 @@
"cheerio": "^1.0.0-rc.3",
"lodash-es": "^4.17.21",
"markdown-it": "^13.0.1",
"node-fetch": "^3.2.9",
"parse-github-url": "^1.0.2"
},
"devDependencies": {
Expand Down
16 changes: 14 additions & 2 deletions src/fetch/proposals.ts
@@ -1,12 +1,24 @@
import { Endpoints } from '@octokit/types';
import _ from 'lodash-es';
import fetch from 'node-fetch';
import parseGithubURL from 'parse-github-url';
import { github } from './github.js';
import { readAllProposals } from './proposal-markdown.js';
import { getTC39Repos } from './repos.js';
import { BundleProposals } from '../types/bundle.js';

const fetchWithRetry = async (url: RequestInfo, init: RequestInit, retryCount = 3) => {
for (let i = 0; i < retryCount; i++) {
try {
return await fetch(url, init);
} catch (error) {
if (i === retryCount - 1) throw error;
}
// exponential backoff: 100ms, 400ms, 900ms
await new Promise((resolve) => setTimeout(resolve, (i + 1) ** 2 * 100));
}
throw new Error('Unreachable');
};

export async function getProposals() {
const repos = await getTC39Repos();
const records: BundleProposals = [];
Expand Down Expand Up @@ -36,7 +48,7 @@ export async function getProposals() {
? `https://tc39.es/${data.name}/`
: `https://${data?.owner?.login}.github.io/${data?.name}/`;

const response = await fetch(specURL, { redirect: 'manual' });
const response = await fetchWithRetry(specURL, { redirect: 'manual' });

// This is the default spec text at https://github.com/tc39/template-for-proposals/blob/HEAD/spec.emu
if (response.status !== 200) spec = undefined;
Expand Down

0 comments on commit 6273302

Please sign in to comment.