This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,80 @@ | ||
import { serve } from "../../deps/std/http.ts" | ||
import { TimedMemo } from "../../util/memo.ts" | ||
import { f, handleErrors } from "../mod.ts" | ||
|
||
serve(() => new Response("delegator")) | ||
serve(handleErrors(handler)) | ||
|
||
const delegateeProjectId = "70eddd08-c9b0-4cb3-b100-8c6facf52f1e" | ||
|
||
const shaAbbrevLength = 8 | ||
|
||
const GITHUB_API_REPO = "https://api.github.com/repos/paritytech/capi" | ||
|
||
const rTagVersion = /^v(\d+\.\d+\.\d+(?:-.+)?)$/ | ||
const rPrVersion = /^pr:(\d+)$/ | ||
|
||
const rVersionedUrl = /^(?:\/@(.+?))?(\/.*)?$/ | ||
|
||
const { signal } = new AbortController() | ||
|
||
async function handler(request: Request) { | ||
const url = new URL(request.url) | ||
const [, version, path = "/"] = rVersionedUrl.exec(url.pathname)! | ||
if (!version) throw f.notFound() | ||
const sha = await getSha(version) | ||
let normalizedVersion | ||
if (rTagVersion.test(version)) { | ||
normalizedVersion = version.replace(rTagVersion, "v$1") | ||
} else { | ||
normalizedVersion = sha.slice(0, shaAbbrevLength) | ||
} | ||
if (version !== normalizedVersion) { | ||
return f.redirect(`/@${normalizedVersion}${path}`) | ||
} | ||
const deploymentUrl = await getDeployment(sha) | ||
return await fetch(new URL(path, deploymentUrl)) | ||
} | ||
|
||
const shaTtl = 60_000 | ||
const shaMemo = new TimedMemo<string, string>(shaTtl, signal) | ||
async function getSha(version: string): Promise<string> { | ||
const ref = version | ||
.replace(rPrVersion, "pull/$1/head") | ||
.replace(rTagVersion, "v$1") | ||
.replace(/:/g, "/") | ||
return await shaMemo.run( | ||
ref, | ||
async () => (await json(`${GITHUB_API_REPO}/commits/${ref}`)).sha, | ||
) | ||
} | ||
|
||
const deploymentTtl = 60_000 | ||
const deploymentMemo = new TimedMemo<string, string>(deploymentTtl, signal) | ||
async function getDeployment(sha: string) { | ||
return await deploymentMemo.run(sha, async () => { | ||
const deployments: GithubDeployment[] = await json( | ||
`${GITHUB_API_REPO}/deployments?sha=${sha}`, | ||
) | ||
const deployment = deployments.find((x) => x.payload.project_id === delegateeProjectId) | ||
if (!deployment) throw f.notFound() | ||
const statuses: GithubStatus[] = await json(deployment.statuses_url) | ||
const url = statuses.map((x) => x.environment_url).find((x) => x) | ||
if (!url) throw f.notFound() | ||
return url | ||
}) | ||
} | ||
|
||
async function json(url: string) { | ||
const response = await fetch(url) | ||
if (!response.ok) throw new Error(`${url}: invalid response`) | ||
return await response.json() | ||
} | ||
|
||
interface GithubDeployment { | ||
statuses_url: string | ||
payload: { project_id: string } | ||
} | ||
|
||
interface GithubStatus { | ||
environment_url?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./Env.ts" | ||
export * as f from "./factories.ts" | ||
export * from "./handler.ts" | ||
export * from "./PathInfo.ts" | ||
export * from "./Provider.ts" |