~james/core

core/web/src/lib/server/github/env.ts -rw-r--r-- 1.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// server-only github app config, read from worker bindings or $app/env/private
// (declared in src/env.ts).
// GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET and GITHUB_APP_SLUG identify the app, and the
// oauth callback url is <web origin>/_internal/github/callback.
// GITHUB_SESSION_SECRET seals the connection cookie; at least 32 random bytes, and
// rotating it signs every existing connection out. never prefix these with VITE_/PUBLIC_.
import * as privateEnv from "$app/env/private";

export interface GitHubEnv {
	clientId: string;
	clientSecret: string;
	secret: string;
	appSlug: string;
	publicOrigin: string;
	apiOrigin: string;
	avatarsOrigin: string;
}

// workers provide bindings via platform.env while $app/env/private is empty
export const githubEnv = (platform?: App.Platform): GitHubEnv => {
	const bound = platform?.env as Record<string, string | undefined> | undefined;
	const read = (key: string) =>
		privateEnv[key as keyof typeof privateEnv]?.trim() || bound?.[key]?.trim() || "";
	const origin = (key: string, fallback: string) => (read(key) || fallback).replace(/\/+$/, "");
	return {
		clientId: read("GITHUB_CLIENT_ID"),
		clientSecret: read("GITHUB_CLIENT_SECRET"),
		secret: read("GITHUB_SESSION_SECRET"),
		appSlug: read("GITHUB_APP_SLUG"),
		publicOrigin: origin("GITHUB_PUBLIC_ORIGIN", "https://github.com"),
		apiOrigin: origin("GITHUB_API_ORIGIN", "https://api.github.com"),
		avatarsOrigin: origin("GITHUB_AVATARS_ORIGIN", "https://avatars.githubusercontent.com")
	};
};

export const hostOf = (origin: string) => new URL(origin).hostname;