The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as recaptchaenterprise from "@distilled.cloud/gcp/recaptchaenterprise_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import { tagRecord } from "alchemy/Tags"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | import { hasAlchemyLabels } from "../Labels.ts"; |
| 7 | |
| 8 | export const MAX_DESCRIPTION_LENGTH = 256; |
| 9 | export const MAX_DISPLAY_NAME_LENGTH = 63; |
| 10 | export const MAX_PATH_LENGTH = 200; |
| 11 | |
| 12 | export const lastSegment = (value: string) => { |
| 13 | const trimmed = value.replace(/\/+$/, ""); |
| 14 | const parts = trimmed.split("/"); |
| 15 | return parts[parts.length - 1] || trimmed; |
| 16 | }; |
| 17 | |
| 18 | export const projectParent = (project: string) => `projects/${project}`; |
| 19 | |
| 20 | export const keyNameOf = (project: string, keyId: string) => `projects/${project}/keys/${keyId}`; |
| 21 | |
| 22 | export const firewallNameOf = (project: string, firewallpolicyId: string) => |
| 23 | `projects/${project}/firewallpolicies/${firewallpolicyId}`; |
| 24 | |
| 25 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 26 | (left ?? "") === (right ?? ""); |
| 27 | |
| 28 | export const sameJson = (left: unknown, right: unknown) => |
| 29 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 30 | |
| 31 | export const sortedStrings = (values: readonly string[] | undefined) => |
| 32 | [...(values ?? [])].slice().sort(); |
| 33 | |
| 34 | export const stringList = (values: readonly (string | undefined)[] | null | undefined): string[] => |
| 35 | (values ?? []).filter((value): value is string => typeof value === "string"); |
| 36 | |
| 37 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 38 | fields.filter((field): field is string => field !== undefined).join(","); |
| 39 | |
| 40 | export const toDisplayName = ( |
| 41 | id: string, |
| 42 | requested: string | undefined, |
| 43 | existing: string | undefined, |
| 44 | maxLength = MAX_DISPLAY_NAME_LENGTH, |
| 45 | ) => |
| 46 | Effect.gen(function* () { |
| 47 | if (requested !== undefined && requested.length > 0) return requested; |
| 48 | if (existing !== undefined && existing.length > 0) return existing; |
| 49 | return yield* createPhysicalName({ |
| 50 | id, |
| 51 | maxLength, |
| 52 | lowercase: true, |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | export const toGeneratedPath = (id: string, requested: string | undefined) => |
| 57 | Effect.gen(function* () { |
| 58 | if (requested !== undefined) return requested; |
| 59 | const generated = yield* createPhysicalName({ |
| 60 | id, |
| 61 | maxLength: 40, |
| 62 | lowercase: true, |
| 63 | }); |
| 64 | const path = `/alc/${generated}`.slice(0, MAX_PATH_LENGTH); |
| 65 | return path.length > 0 ? path : "/alc"; |
| 66 | }); |
| 67 | |
| 68 | export const getKey = (name: string) => |
| 69 | name.length === 0 |
| 70 | ? Effect.undefined |
| 71 | : recaptchaenterprise |
| 72 | .getProjectsKeys({ name }) |
| 73 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 74 | |
| 75 | export const getFirewallPolicy = (name: string) => |
| 76 | name.length === 0 |
| 77 | ? Effect.undefined |
| 78 | : recaptchaenterprise |
| 79 | .getProjectsFirewallpolicies({ name }) |
| 80 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 81 | |
| 82 | export const listKeys = (project: string) => |
| 83 | recaptchaenterprise.listProjectsKeys |
| 84 | .pages({ |
| 85 | parent: projectParent(project), |
| 86 | pageSize: 1000, |
| 87 | }) |
| 88 | .pipe( |
| 89 | Stream.flatMap((page) => Stream.fromIterable(page.keys ?? [])), |
| 90 | Stream.runCollect, |
| 91 | Effect.map((chunk) => Array.from(chunk)), |
| 92 | ); |
| 93 | |
| 94 | export const listFirewallPolicies = (project: string) => |
| 95 | recaptchaenterprise.listProjectsFirewallpolicies |
| 96 | .pages({ |
| 97 | parent: projectParent(project), |
| 98 | pageSize: 1000, |
| 99 | }) |
| 100 | .pipe( |
| 101 | Stream.flatMap((page) => Stream.fromIterable(page.firewallPolicies ?? [])), |
| 102 | Stream.runCollect, |
| 103 | Effect.map((chunk) => Array.from(chunk)), |
| 104 | ); |
| 105 | |
| 106 | export const findOwnedKey = (project: string, id: string, name?: string) => |
| 107 | Effect.gen(function* () { |
| 108 | if (name !== undefined && name.length > 0) { |
| 109 | const existing = yield* getKey(name); |
| 110 | if (existing !== undefined) return existing; |
| 111 | } |
| 112 | const keys = yield* listKeys(project); |
| 113 | for (const key of keys) { |
| 114 | if (yield* hasAlchemyLabels(id, tagRecord(key.labels))) { |
| 115 | return key; |
| 116 | } |
| 117 | } |
| 118 | return undefined; |
| 119 | }); |