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, 04:45 UTC
| 1 | import * as crypto from "node:crypto"; |
| 2 | import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Predicate from "effect/Predicate"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | |
| 15 | export class BookmarkUnexpectedResponse extends Data.TaggedError("BookmarkUnexpectedResponse")<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export type BookmarkProps = { |
| 21 | /** |
| 22 | * The name of the bookmark application shown in the App Launcher. Used as |
| 23 | * a stable identifier so the provider can locate the bookmark during |
| 24 | * adoption / state recovery. If omitted, a unique name is generated from |
| 25 | * the stack/stage/logical id. |
| 26 | * |
| 27 | * @default ${app}-${stage}-${id} |
| 28 | */ |
| 29 | name?: string; |
| 30 | /** |
| 31 | * The domain the bookmark links to, e.g. `example.com` or |
| 32 | * `wiki.example.com/path`. |
| 33 | */ |
| 34 | domain: string; |
| 35 | /** |
| 36 | * The image URL for the logo shown in the App Launcher dashboard. |
| 37 | */ |
| 38 | logoUrl?: string; |
| 39 | /** |
| 40 | * Whether to display the bookmark in the App Launcher. |
| 41 | * |
| 42 | * @default true |
| 43 | */ |
| 44 | appLauncherVisible?: boolean; |
| 45 | }; |
| 46 | |
| 47 | export type Bookmark = Resource< |
| 48 | "Cloudflare.Access.Bookmark", |
| 49 | BookmarkProps, |
| 50 | { |
| 51 | /** UUID of the bookmark application. */ |
| 52 | bookmarkId: string; |
| 53 | /** Cloudflare account that owns the bookmark. */ |
| 54 | accountId: string; |
| 55 | /** Display name reported by Cloudflare. */ |
| 56 | name: string; |
| 57 | /** The domain the bookmark links to. */ |
| 58 | domain: string; |
| 59 | /** The logo URL shown in the App Launcher. */ |
| 60 | logoUrl: string | undefined; |
| 61 | /** Whether the bookmark is displayed in the App Launcher. */ |
| 62 | appLauncherVisible: boolean; |
| 63 | }, |
| 64 | never, |
| 65 | Providers |
| 66 | >; |
| 67 | |
| 68 | /** |
| 69 | * A Cloudflare Zero Trust Access bookmark application — an unprotected link |
| 70 | * shown in the App Launcher. |
| 71 | * @deprecated **Legacy resource.** Cloudflare has deprecated the dedicated |
| 72 | * bookmarks API in favor of Access applications with `type: "bookmark"` — |
| 73 | * prefer {@link Application} for new configurations. This resource is |
| 74 | * provided for managing pre-existing bookmark records. |
| 75 | * |
| 76 | * ### Creating a Bookmark |
| 77 | * **Example:** Basic bookmark |
| 78 | * ```typescript |
| 79 | * const bookmark = yield* Cloudflare.Access.Bookmark("Wiki", { |
| 80 | * domain: "wiki.example.com", |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * **Example:** Bookmark with a logo, hidden from the App Launcher |
| 85 | * ```typescript |
| 86 | * const bookmark = yield* Cloudflare.Access.Bookmark("Wiki", { |
| 87 | * name: "internal-wiki", |
| 88 | * domain: "wiki.example.com", |
| 89 | * logoUrl: "https://example.com/logo.png", |
| 90 | * appLauncherVisible: false, |
| 91 | * }); |
| 92 | * ``` |
| 93 | * |
| 94 | * ### Preferred Alternative |
| 95 | * **Example:** Bookmark-type Access application (non-legacy) |
| 96 | * ```typescript |
| 97 | * const app = yield* Cloudflare.Access.Application("Wiki", { |
| 98 | * type: "bookmark", |
| 99 | * domain: "wiki.example.com", |
| 100 | * }); |
| 101 | * ``` |
| 102 | * |
| 103 | * @resource |
| 104 | * @product Access |
| 105 | * @category Cloudflare One (Zero Trust) |
| 106 | */ |
| 107 | export const Bookmark = Resource<Bookmark>("Cloudflare.Access.Bookmark"); |
| 108 | |
| 109 | export const isBookmark = (value: unknown): value is Bookmark => |
| 110 | Predicate.hasProperty(value, "Type") && value.Type === "Cloudflare.Access.Bookmark"; |
| 111 | |
| 112 | export const BookmarkProvider = () => |
| 113 | Provider.succeed(Bookmark, { |
| 114 | stables: ["bookmarkId", "accountId"], |
| 115 | diff: Effect.fn(function* ({ news, output }) { |
| 116 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 117 | if (!isResolved(news)) return undefined; |
| 118 | if ((output?.accountId ?? accountId) !== accountId) { |
| 119 | return { action: "replace" } as const; |
| 120 | } |
| 121 | // name/domain/logoUrl/appLauncherVisible converge via PUT. |
| 122 | }), |
| 123 | read: Effect.fn(function* ({ id, output, olds }) { |
| 124 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 125 | const acct = output?.accountId ?? accountId; |
| 126 | if (output?.bookmarkId) { |
| 127 | const direct = yield* getBookmark(acct, output.bookmarkId); |
| 128 | if (direct && direct.id) return toAttrs(direct, acct); |
| 129 | } |
| 130 | const name = yield* createBookmarkName(id, output?.name ?? olds?.name); |
| 131 | const existing = yield* findBookmarkByName(acct, name); |
| 132 | if (!existing || !existing.id) return undefined; |
| 133 | return toAttrs(existing, acct); |
| 134 | }), |
| 135 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 136 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 137 | const acct = output?.accountId ?? accountId; |
| 138 | const name = yield* createBookmarkName(id, news.name); |
| 139 | const desiredVisible = news.appLauncherVisible ?? true; |
| 140 | |
| 141 | // Observe — prefer the cached id, fall back to a name lookup so we |
| 142 | // recover from out-of-band deletes and state-persistence failures. |
| 143 | let observed: ObservedBookmark | undefined; |
| 144 | if (output?.bookmarkId) { |
| 145 | observed = yield* getBookmark(acct, output.bookmarkId); |
| 146 | } |
| 147 | if (!observed || !observed.id) { |
| 148 | observed = yield* findBookmarkByName(acct, name); |
| 149 | } |
| 150 | |
| 151 | // Ensure — create when missing. The legacy API requires the new |
| 152 | // bookmark's UUID in the path, so derive a deterministic one from the |
| 153 | // name: a retry after a partial failure converges on the same record. |
| 154 | if (!observed || !observed.id) { |
| 155 | const bookmarkId = yield* deterministicUuid(`${acct}:${name}`); |
| 156 | const created = yield* zeroTrust |
| 157 | .createAccessBookmark({ |
| 158 | accountId: acct, |
| 159 | bookmarkId, |
| 160 | name, |
| 161 | domain: news.domain, |
| 162 | logoUrl: news.logoUrl, |
| 163 | appLauncherVisible: desiredVisible, |
| 164 | }) |
| 165 | .pipe( |
| 166 | Effect.catch((err) => |
| 167 | Effect.gen(function* () { |
| 168 | const existing = yield* getBookmark(acct, bookmarkId); |
| 169 | if (existing && existing.id) return existing; |
| 170 | return yield* err; |
| 171 | }), |
| 172 | ), |
| 173 | ); |
| 174 | if (!created.id) { |
| 175 | return yield* new BookmarkUnexpectedResponse({ |
| 176 | message: "Bookmark: created bookmark missing id", |
| 177 | }); |
| 178 | } |
| 179 | return toAttrs(created, acct); |
| 180 | } |
| 181 | |
| 182 | // Sync — PUT the full desired shape only on a real delta. |
| 183 | if ( |
| 184 | observed.name !== name || |
| 185 | observed.domain !== news.domain || |
| 186 | (observed.logoUrl ?? undefined) !== news.logoUrl || |
| 187 | (observed.appLauncherVisible ?? true) !== desiredVisible |
| 188 | ) { |
| 189 | const updated = yield* zeroTrust.updateAccessBookmark({ |
| 190 | accountId: acct, |
| 191 | bookmarkId: observed.id, |
| 192 | name, |
| 193 | domain: news.domain, |
| 194 | logoUrl: news.logoUrl, |
| 195 | appLauncherVisible: desiredVisible, |
| 196 | }); |
| 197 | observed = { |
| 198 | id: updated.id ?? observed.id, |
| 199 | name: updated.name ?? name, |
| 200 | domain: updated.domain ?? news.domain, |
| 201 | logoUrl: updated.logoUrl ?? news.logoUrl, |
| 202 | appLauncherVisible: updated.appLauncherVisible ?? desiredVisible, |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | return toAttrs(observed, acct); |
| 207 | }), |
| 208 | delete: Effect.fn(function* ({ output }) { |
| 209 | yield* zeroTrust |
| 210 | .deleteAccessBookmark({ |
| 211 | accountId: output.accountId, |
| 212 | bookmarkId: output.bookmarkId, |
| 213 | }) |
| 214 | .pipe(Effect.catchTag("AccessBookmarkNotFound", () => Effect.void)); |
| 215 | }), |
| 216 | list: Effect.fn(function* () { |
| 217 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 218 | return yield* zeroTrust.listAccessBookmarks.pages({ accountId }).pipe( |
| 219 | Stream.runCollect, |
| 220 | Effect.map((chunk) => |
| 221 | Array.from(chunk).flatMap((page) => |
| 222 | (page.result ?? []) |
| 223 | .filter((b): b is ObservedBookmark & { id: string } => Predicate.isNotNullish(b.id)) |
| 224 | .map((b) => toAttrs(b, accountId)), |
| 225 | ), |
| 226 | ), |
| 227 | ); |
| 228 | }), |
| 229 | }); |
| 230 | |
| 231 | const createBookmarkName = (id: string, name: string | undefined) => |
| 232 | Effect.gen(function* () { |
| 233 | if (name) return name; |
| 234 | return yield* createPhysicalName({ id }); |
| 235 | }); |
| 236 | |
| 237 | const getBookmark = (acct: string, bookmarkId: string) => |
| 238 | zeroTrust |
| 239 | .getAccessBookmark({ accountId: acct, bookmarkId }) |
| 240 | .pipe(Effect.catchTag("AccessBookmarkNotFound", () => Effect.undefined)); |
| 241 | |
| 242 | const findBookmarkByName = (acct: string, name: string) => |
| 243 | zeroTrust.listAccessBookmarks.items({ accountId: acct }).pipe( |
| 244 | Stream.filter((b): b is ObservedBookmark => b.name === name), |
| 245 | Stream.runHead, |
| 246 | Effect.map(Option.getOrUndefined), |
| 247 | Effect.orElseSucceed(() => undefined), |
| 248 | ); |
| 249 | |
| 250 | /** RFC-4122-shaped v4 UUID derived deterministically from a seed string. */ |
| 251 | const deterministicUuid = (seed: string) => |
| 252 | Effect.sync(() => { |
| 253 | const hex = crypto.createHash("sha256").update(seed).digest("hex"); |
| 254 | const bytes = hex.slice(0, 32).split(""); |
| 255 | bytes[12] = "4"; |
| 256 | bytes[16] = ((parseInt(bytes[16]!, 16) & 0x3) | 0x8).toString(16); |
| 257 | const s = bytes.join(""); |
| 258 | return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20, 32)}`; |
| 259 | }); |
| 260 | |
| 261 | const toAttrs = (observed: ObservedBookmark, accountId: string) => ({ |
| 262 | bookmarkId: observed.id!, |
| 263 | accountId, |
| 264 | name: observed.name ?? "", |
| 265 | domain: observed.domain ?? "", |
| 266 | logoUrl: observed.logoUrl ?? undefined, |
| 267 | appLauncherVisible: observed.appLauncherVisible ?? true, |
| 268 | }); |
| 269 | |
| 270 | type ObservedBookmark = { |
| 271 | id?: string; |
| 272 | name?: string | null; |
| 273 | domain?: string | null; |
| 274 | logoUrl?: string | null; |
| 275 | appLauncherVisible?: boolean | null; |
| 276 | }; |