Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sourceControlHostSecrets.ts

45 lines1.8 KB

1import { secretSettingsFields } from "@t3tools/contracts";
2​
3import { sourceControlDefinitions } from "./sourceControlDefinitions.ts";
4​
5export const SOURCE_CONTROL_HOST_SECRET_FIELDS: ReadonlyMap<
6 string,
7 ReadonlyArray<string>
8> = new Map(
9 sourceControlDefinitions.definitions.flatMap((definition) => {
10 const fields = definition.settings ? secretSettingsFields(definition.settings) : [];
11 return fields.length > 0 ? [[definition.kind, fields] as const] : [];
12 }),
13);
14​
15export interface SourceControlHostSecretSlot {
16 readonly kind: string;
17 readonly field: string;
18 readonly serverHost: string | null;
20​
21/** The secret store entry for one slot; names stay stable across releases. */
22export function sourceControlHostSecretName(slot: SourceControlHostSecretSlot): string {
23 const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");
24 return slot.serverHost === null
25 ? `source-control-${encode(slot.kind)}-${encode(slot.field)}`
26 : `source-control-${encode(slot.kind)}-${encode(slot.field)}-${encode(slot.serverHost.trim().toLowerCase())}`;
28​
29export function readSecretFieldValues(
30 value: unknown,
31): ReadonlyArray<readonly [serverHost: string | null, value: string]> {
32 if (typeof value === "string") return [[null, value]];
33 if (value === null || typeof value !== "object" || Array.isArray(value)) return [];
34 return Object.entries(value).flatMap(([serverHost, entry]) =>
35 typeof entry === "string" ? [[serverHost.trim().toLowerCase(), entry] as const] : [],
36 );
38​
39export function writeSecretFieldValues(
40 original: unknown,
41 values: ReadonlyArray<readonly [serverHost: string | null, value: string]>,
42): unknown {
43 if (typeof original === "string") return values[0]?.[1] ?? "";
44 return Object.fromEntries(values.map(([serverHost, value]) => [serverHost ?? "", value]));