50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import {
|
|
getFetchAutoProxyInit,
|
|
} from "https://hatter.ink/script/fetch/library/deno-fetch-auto-proxy-mod.ts?202501191421";
|
|
|
|
export class SshKey {
|
|
algorithm: string;
|
|
material: string;
|
|
description?: string;
|
|
|
|
constructor(
|
|
algorithm: string,
|
|
material: string,
|
|
description: string | undefined,
|
|
) {
|
|
this.algorithm = algorithm;
|
|
this.material = material;
|
|
this.description = description;
|
|
}
|
|
|
|
static parseSshKey(key: string): SshKey {
|
|
const keyParts = key.split(/\s+/);
|
|
if (keyParts.length < 2 || keyParts.length > 3) {
|
|
throw `Bad SSH key format ${key}`;
|
|
}
|
|
return new SshKey(
|
|
keyParts[0],
|
|
keyParts[1],
|
|
(keyParts.length > 2) ? keyParts[2] : undefined,
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function fetchKeys(
|
|
username: string,
|
|
timeout?: number,
|
|
): Promise<Array<SshKey>> {
|
|
const url = `https://github.com/${username}.keys`;
|
|
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));
|
|
}
|