feat: update deno-github-mod.ts, add timeout

This commit is contained in:
2025-01-19 17:35:05 +08:00
parent f84b047743
commit 90e421b2ec

View File

@@ -30,9 +30,20 @@ export class SshKey {
}
}
export async function fetchKeys(username: string): Promise<Array<SshKey>> {
export async function fetchKeys(
username: string,
timeout?: number,
): Promise<Array<SshKey>> {
const url = `https://github.com/${username}.keys`;
const response = await fetch(url, getFetchAutoProxyInit());
const fetchTimeout = timeout || 10000;
const abortController = new AbortController();
const timoutHandler = setTimeout(() => {
abortController.abort(`Timout ${fetchTimeout} ms`);
}, fetchTimeout);
const init = getFetchAutoProxyInit() || {};
init.signal = abortController.signal;
const response = await fetch(url, init);
const responseText = await response.text();
clearTimeout(timoutHandler);
return responseText.trim().split("\n").map((k) => SshKey.parseSshKey(k));
}