Skip to content

Commit

Permalink
refactor: change a proxy domain and do octocat test 3 times
Browse files Browse the repository at this point in the history
  • Loading branch information
3Shain committed Mar 8, 2023
1 parent c347009 commit d54f913
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { log, timeout } from "./utils";

const END_POINTS = ["", "https://gh.3ss.workers.dev/"];
const END_POINTS = ["", "https://ghp.3shain.uk/"];

export async function createGithubEndpoint() {
await log(`Checking github endpoints`);
const fastest = await Promise.race(
[...END_POINTS.map((prefix) =>
fetch(`${prefix}https://api.github.com/octocat`).then(x=>x.text()).then(x=>prefix)
),timeout(1000)]
);
const fastest = await Promise.race([
...END_POINTS.map((prefix) =>
fetch(`${prefix}https://api.github.com/octocat`)
.then((x) => x.text())
.then(() => fetch(`${prefix}https://api.github.com/octocat`))
.then((x) => x.text())
.then(() => fetch(`${prefix}https://api.github.com/octocat`))
.then((x) => x.text())
.then((x) => prefix)
),
timeout(20000),
]);

fastest == "" || (await log(`Using github proxy ${fastest}`));

function api(path: `/${string}`): Promise<any> {
return fetch(`${fastest}https://api.github.com${path}`).then(x=>{
if(x.status==200||x.status==301||x.status==302) {
return x.json()
return fetch(`${fastest}https://api.github.com${path}`).then((x) => {
if (x.status == 200 || x.status == 301 || x.status == 302) {
return x.json();
}
return Promise.reject(new Error(`Request failed: ${x.status} ${x.statusText} (${x.url})`));
})
return Promise.reject(
new Error(`Request failed: ${x.status} ${x.statusText} (${x.url})`)
);
});
}

function acceleratedPath(path: string) {
Expand All @@ -27,7 +36,7 @@ export async function createGithubEndpoint() {

return {
api,
acceleratedPath
acceleratedPath,
};
}

Expand Down

0 comments on commit d54f913

Please sign in to comment.