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 9
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
10 changed files
with
143 additions
and
42 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { serve } from "../../deps/std/http.ts" | ||
import { WssProvider } from "../../providers/frame/wss.ts" | ||
import { S3Cache } from "../../util/cache/mod.ts" | ||
import { handler } from "../handler.ts" | ||
import { Env } from "../mod.ts" | ||
|
||
// this is only used by the dev providers, which should target the local server | ||
const href = "http://localhost:4646/" | ||
|
||
const controller = new AbortController() | ||
const { signal } = controller | ||
|
||
const cache = new S3Cache(Deno.env.get("DENO_DEPLOYMENT_ID")!, { | ||
accessKeyID: Deno.env.get("S3_ACCESS_KEY")!, | ||
secretKey: Deno.env.get("S3_SECRET_KEY")!, | ||
region: Deno.env.get("S3_REGION")!, | ||
bucket: Deno.env.get("S3_BUCKET")!, | ||
}, signal) | ||
|
||
const env = new Env(href, cache, signal, (env) => ({ | ||
frame: { | ||
wss: new WssProvider(env), | ||
}, | ||
})) | ||
|
||
serve(handler(env)) |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { serve } from "../../deps/std/http.ts" | ||
import { TimedMemo } from "../../util/memo.ts" | ||
import { f, handleErrors } from "../mod.ts" | ||
|
||
serve(handleErrors(handler)) | ||
|
||
const ttl = 60_000 | ||
const shaAbbrevLength = 8 | ||
|
||
const rTagVersion = /^v(\d+\.\d+\.\d+(?:-.+)?)$/ | ||
const rPrVersion = /^pr:(\d+)$/ | ||
const rVersionedUrl = /^(?:\/@(.+?))?(\/.*)?$/ | ||
|
||
const delegateeProjectId = "70eddd08-c9b0-4cb3-b100-8c6facf52f1e" | ||
const githubApiBase = "https://api.github.com/repos/paritytech/capi/" | ||
|
||
const { signal } = new AbortController() | ||
|
||
async function handler(request: Request) { | ||
const url = new URL(request.url) | ||
const [, version, path = "/"] = rVersionedUrl.exec(url.pathname)! | ||
if (!version) return f.redirect(`/@${await defaultVersion()}${path}`) | ||
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), { | ||
method: request.method, | ||
headers: request.headers, | ||
body: request.body, | ||
}) | ||
} | ||
|
||
const defaultVersionMemo = new TimedMemo<void, string>(ttl, signal) | ||
async function defaultVersion() { | ||
return await defaultVersionMemo.run(undefined, async () => { | ||
const release = await github<GithubRelease>(`releases/latest`).catch(async () => | ||
(await github<GithubRelease[]>(`releases`))[0]! | ||
) | ||
return release.tag_name | ||
}) | ||
} | ||
|
||
const shaMemo = new TimedMemo<string, string>(ttl, 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 github<GithubCommit>(`commits/${ref}`)).sha, | ||
) | ||
} | ||
|
||
const deploymentMemo = new TimedMemo<string, string>(ttl, signal) | ||
async function getDeployment(sha: string) { | ||
return await deploymentMemo.run(sha, async () => { | ||
const deployments = await github<GithubDeployment[]>(`deployments?sha=${sha}`) | ||
const deployment = deployments.find((x) => x.payload.project_id === delegateeProjectId) | ||
if (!deployment) throw f.notFound() | ||
const statuses = await github<GithubStatus[]>(deployment.statuses_url) | ||
const url = statuses.map((x) => x.environment_url).find((x) => x) | ||
if (!url) throw f.notFound() | ||
return url | ||
}) | ||
} | ||
|
||
async function github<T>(url: string): Promise<T> { | ||
const response = await fetch(new URL(url, githubApiBase)) | ||
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 | ||
} | ||
|
||
interface GithubRelease { | ||
tag_name: string | ||
} | ||
|
||
interface GithubCommit { | ||
sha: 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
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" |
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
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