import axios from "axios"; import type { Source } from "./source.ts"; export interface GitLabSourceOptions { timeout?: number; } export class GitLabSource implements Source { public readonly displayName = "GitLab"; protected readonly network: string; protected readonly timeout: number; constructor(network: string, options?: GitLabSourceOptions) { this.network = network; this.timeout = options?.timeout ?? 30_000; } public async getPublicKeyJwk(keyId: string): Promise { const path = `pubkeys/${keyId}.jwk`; const url = this.url(path); try { const response = await axios.get(url, { timeout: this.timeout, transitional: { forcedJSONParsing: false, // Prevent Axios from trying to parse the response }, }); return response.data; } catch (error) { throw new Error( `Error when fetching ${url}: ${error instanceof Error ? error.message : "Unknown error"}`, { cause: error }, ); } } public async getTnlJwt(version?: number): Promise { const path = version ? `lists/${this.network}/archive/tnl-${version.toString()}.jwt` // Fetch an archived TNL : `lists/${this.network}/tnl.jwt`; // Fetch latest TNL const url = this.url(path); try { const response = await axios.get(url, { timeout: this.timeout, transitional: { forcedJSONParsing: false, // Prevent Axios from trying to parse the response }, }); return response.data; } catch (error) { throw new Error( `Error when fetching ${url}: ${error instanceof Error ? error.message : "Unknown error"}`, { cause: error }, ); } } protected url(path: string): string { const branch = "main"; return `https://gitlab.com/api/v4/projects/77640068/repository/files/${encodeURIComponent(path)}/raw?ref=${branch}`; } }