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 stream from "@distilled.cloud/cloudflare/stream"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Predicate from "effect/Predicate"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | const TypeId = "Cloudflare.Stream.Webhook" as const; |
| 12 | type TypeId = typeof TypeId; |
| 13 | |
| 14 | export type WebhookProps = { |
| 15 | /** |
| 16 | * The URL where Stream webhook notifications (e.g. video ready, |
| 17 | * live input connected/disconnected) are sent. Mutable — updated in |
| 18 | * place via Cloudflare's PUT upsert. |
| 19 | */ |
| 20 | notificationUrl: string; |
| 21 | }; |
| 22 | |
| 23 | export type WebhookAttributes = { |
| 24 | /** |
| 25 | * The Cloudflare account the webhook belongs to. |
| 26 | */ |
| 27 | accountId: string; |
| 28 | /** |
| 29 | * The URL where webhook notifications are sent. |
| 30 | */ |
| 31 | notificationUrl: string; |
| 32 | /** |
| 33 | * The date and time the webhook was last modified. |
| 34 | */ |
| 35 | modified: string | undefined; |
| 36 | /** |
| 37 | * The HMAC secret used to verify webhook request signatures |
| 38 | * (`Webhook-Signature` header). |
| 39 | */ |
| 40 | secret: Redacted.Redacted<string>; |
| 41 | }; |
| 42 | |
| 43 | export type Webhook = Resource<TypeId, WebhookProps, WebhookAttributes, never, Providers>; |
| 44 | |
| 45 | /** |
| 46 | * The Cloudflare Stream webhook — an **account-level singleton** that |
| 47 | * receives notifications when videos finish processing or live inputs |
| 48 | * connect/disconnect. |
| 49 | * |
| 50 | * Each account has at most one Stream webhook, so creating this |
| 51 | * resource takes over the account's webhook slot; an existing webhook |
| 52 | * configured outside Alchemy is only adopted when `--adopt` is set. |
| 53 | * Destroying the resource deletes the webhook configuration. |
| 54 | * |
| 55 | * Requires the Stream subscription to be enabled on the account. |
| 56 | * ### Configuring the webhook |
| 57 | * **Example:** Receive Stream notifications |
| 58 | * ```typescript |
| 59 | * const webhook = yield* Cloudflare.Stream.Webhook("Notifications", { |
| 60 | * notificationUrl: "https://example.com/hooks/stream", |
| 61 | * }); |
| 62 | * |
| 63 | * // Verify the Webhook-Signature header with the HMAC secret: |
| 64 | * const secret = webhook.secret; // Redacted<string> |
| 65 | * ``` |
| 66 | * |
| 67 | * @see https://developers.cloudflare.com/stream/manage-video-library/using-webhooks/ |
| 68 | * |
| 69 | * @resource |
| 70 | * @product Stream |
| 71 | * @category Media |
| 72 | */ |
| 73 | export const Webhook = Resource<Webhook>(TypeId); |
| 74 | |
| 75 | /** |
| 76 | * Returns true if the given value is a Webhook resource. |
| 77 | */ |
| 78 | export const isWebhook = (value: unknown): value is Webhook => |
| 79 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 80 | |
| 81 | export const WebhookProvider = () => |
| 82 | Provider.succeed(Webhook, { |
| 83 | stables: ["accountId"], |
| 84 | |
| 85 | diff: Effect.fn(function* ({ output }) { |
| 86 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 87 | // The webhook is a singleton per account — moving accounts is a |
| 88 | // replacement. |
| 89 | if (output !== undefined && output.accountId !== accountId) { |
| 90 | return { action: "replace" } as const; |
| 91 | } |
| 92 | return undefined; |
| 93 | }), |
| 94 | |
| 95 | read: Effect.fn(function* ({ output }) { |
| 96 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 97 | const acct = output?.accountId ?? accountId; |
| 98 | const observed = yield* getWebhook(acct); |
| 99 | if (observed === undefined) return undefined; |
| 100 | const attrs = toAttributes(observed, acct); |
| 101 | // The webhook is an account singleton with no ownership markers. |
| 102 | // On a cold read (no prior output) an existing webhook was |
| 103 | // configured outside Alchemy — brand it `Unowned` so the engine |
| 104 | // refuses to take over unless `--adopt` is set. |
| 105 | if (output === undefined) return Unowned(attrs); |
| 106 | return attrs; |
| 107 | }), |
| 108 | |
| 109 | list: Effect.fn(function* () { |
| 110 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 111 | // Account-level singleton: the account has at most one Stream |
| 112 | // webhook, so enumerate by reading that single slot — return a |
| 113 | // one-element array when configured, [] when unset. Exactly |
| 114 | // mirrors `read`. |
| 115 | const observed = yield* getWebhook(accountId); |
| 116 | if (observed === undefined) return []; |
| 117 | return [toAttributes(observed, accountId)]; |
| 118 | }), |
| 119 | |
| 120 | reconcile: Effect.fn(function* ({ news, output }) { |
| 121 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 122 | const acct = output?.accountId ?? accountId; |
| 123 | |
| 124 | // Observe — read the live webhook config (`WebhookNotFound` means |
| 125 | // the account has no webhook yet). |
| 126 | const observed = yield* getWebhook(acct); |
| 127 | |
| 128 | // Sync — PUT is a true upsert, so create and update are the same |
| 129 | // call. Skip the API entirely when the observed URL already |
| 130 | // matches the desired one. |
| 131 | if (observed !== undefined && observed.notificationUrl === news.notificationUrl) { |
| 132 | return toAttributes(observed, acct); |
| 133 | } |
| 134 | const updated = yield* stream.putWebhook({ |
| 135 | accountId: acct, |
| 136 | notificationUrl: news.notificationUrl, |
| 137 | }); |
| 138 | return toAttributes(updated, acct); |
| 139 | }), |
| 140 | |
| 141 | delete: Effect.fn(function* ({ output }) { |
| 142 | // DELETE on an already-absent webhook returns success, so the |
| 143 | // operation is naturally idempotent; tolerate `WebhookNotFound` |
| 144 | // anyway. |
| 145 | yield* stream |
| 146 | .deleteWebhook({ accountId: output.accountId }) |
| 147 | .pipe(Effect.catchTag("WebhookNotFound", () => Effect.void)); |
| 148 | }), |
| 149 | }); |
| 150 | |
| 151 | /** |
| 152 | * Read the account's webhook, mapping "not configured" |
| 153 | * (`WebhookNotFound`, Cloudflare error code 10003) to `undefined`. |
| 154 | */ |
| 155 | const getWebhook = (accountId: string) => |
| 156 | stream.getWebhook({ accountId }).pipe(Effect.catchTag("WebhookNotFound", () => Effect.undefined)); |
| 157 | |
| 158 | const toAttributes = ( |
| 159 | webhook: stream.GetWebhookResponse | stream.PutWebhookResponse, |
| 160 | accountId: string, |
| 161 | ): WebhookAttributes => ({ |
| 162 | accountId, |
| 163 | notificationUrl: webhook.notificationUrl ?? "", |
| 164 | modified: webhook.modified ?? undefined, |
| 165 | secret: Redacted.make(webhook.secret ?? ""), |
| 166 | }); |