The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as stream from "@distilled.cloud/cloudflare/stream"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 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 Stream from "effect/Stream"; |
| 8 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | const TypeId = "Cloudflare.Stream.LiveInputOutput" as const; |
| 12 | type TypeId = typeof TypeId; |
| 13 | |
| 14 | export type LiveInputOutputProps = { |
| 15 | /** |
| 16 | * The unique identifier (`uid`) of the live input the output restreams |
| 17 | * from. Usually a reference to a `LiveInput`'s `liveInputId` |
| 18 | * attribute. |
| 19 | * |
| 20 | * Immutable — an output belongs to exactly one live input, so changing |
| 21 | * it triggers a replacement. |
| 22 | */ |
| 23 | liveInputId: string; |
| 24 | /** |
| 25 | * The URL the output restreams to, e.g. |
| 26 | * `rtmps://a.rtmps.youtube.com/live2`. |
| 27 | * |
| 28 | * Immutable — Cloudflare's update endpoint only toggles `enabled`, so |
| 29 | * changing the URL triggers a replacement. |
| 30 | */ |
| 31 | url: string; |
| 32 | /** |
| 33 | * The streamKey used to authenticate against the output's target. |
| 34 | * |
| 35 | * Immutable — Cloudflare's update endpoint only toggles `enabled`, so |
| 36 | * changing the streamKey triggers a replacement. |
| 37 | */ |
| 38 | streamKey: string; |
| 39 | /** |
| 40 | * When enabled, live video streamed to the associated live input is |
| 41 | * sent to the output URL. When disabled, live video is not sent to the |
| 42 | * output URL, even while streaming to the associated live input. |
| 43 | * Mutable. |
| 44 | * @default true |
| 45 | */ |
| 46 | enabled?: boolean; |
| 47 | }; |
| 48 | |
| 49 | export type LiveInputOutputAttributes = { |
| 50 | /** |
| 51 | * The unique identifier for the output (Cloudflare `uid`). |
| 52 | */ |
| 53 | outputId: string; |
| 54 | /** |
| 55 | * The unique identifier of the live input the output belongs to. |
| 56 | */ |
| 57 | liveInputId: string; |
| 58 | /** |
| 59 | * The Cloudflare account the output belongs to. |
| 60 | */ |
| 61 | accountId: string; |
| 62 | /** |
| 63 | * The URL the output restreams to. |
| 64 | */ |
| 65 | url: string; |
| 66 | /** |
| 67 | * The streamKey used to authenticate against the output's target. |
| 68 | */ |
| 69 | streamKey: string; |
| 70 | /** |
| 71 | * Whether live video is sent to the output URL while streaming to the |
| 72 | * associated live input. |
| 73 | */ |
| 74 | enabled: boolean; |
| 75 | }; |
| 76 | |
| 77 | export type LiveInputOutput = Resource< |
| 78 | TypeId, |
| 79 | LiveInputOutputProps, |
| 80 | LiveInputOutputAttributes, |
| 81 | never, |
| 82 | Providers |
| 83 | >; |
| 84 | |
| 85 | /** |
| 86 | * A Cloudflare Stream live input output — restreams (simulcasts) live |
| 87 | * video received by a `LiveInput` to another RTMP(S) destination |
| 88 | * such as YouTube Live or Twitch. |
| 89 | * |
| 90 | * The destination (`url` + `streamKey`) is immutable: Cloudflare's update |
| 91 | * endpoint only toggles `enabled`, so changing the destination replaces |
| 92 | * the output. Toggling `enabled` updates the output in place. |
| 93 | * |
| 94 | * Requires the Stream subscription to be enabled on the account. |
| 95 | * ### Creating an output |
| 96 | * **Example:** Restream a live input to YouTube |
| 97 | * ```typescript |
| 98 | * const input = yield* Cloudflare.Stream.LiveInput("Broadcast", {}); |
| 99 | * |
| 100 | * const youtube = yield* Cloudflare.Stream.LiveInputOutput("YouTube", { |
| 101 | * liveInputId: input.liveInputId, |
| 102 | * url: "rtmps://a.rtmps.youtube.com/live2", |
| 103 | * streamKey: youtubeStreamKey, |
| 104 | * }); |
| 105 | * ``` |
| 106 | * |
| 107 | * ### Managing an output |
| 108 | * **Example:** Pause restreaming without deleting the output |
| 109 | * ```typescript |
| 110 | * const youtube = yield* Cloudflare.Stream.LiveInputOutput("YouTube", { |
| 111 | * liveInputId: input.liveInputId, |
| 112 | * url: "rtmps://a.rtmps.youtube.com/live2", |
| 113 | * streamKey: youtubeStreamKey, |
| 114 | * enabled: false, |
| 115 | * }); |
| 116 | * ``` |
| 117 | * |
| 118 | * @see https://developers.cloudflare.com/stream/stream-live/simulcasting/ |
| 119 | * |
| 120 | * @resource |
| 121 | * @product Stream |
| 122 | * @category Media |
| 123 | */ |
| 124 | export const LiveInputOutput = Resource<LiveInputOutput>(TypeId); |
| 125 | |
| 126 | /** |
| 127 | * Returns true if the given value is a LiveInputOutput resource. |
| 128 | */ |
| 129 | export const isLiveInputOutput = (value: unknown): value is LiveInputOutput => |
| 130 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 131 | |
| 132 | export const LiveInputOutputProvider = () => |
| 133 | Provider.succeed(LiveInputOutput, { |
| 134 | stables: ["outputId", "liveInputId", "accountId", "url", "streamKey"], |
| 135 | |
| 136 | diff: Effect.fn(function* ({ olds, news, output }) { |
| 137 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 138 | if (!isResolved(news)) return undefined; |
| 139 | if (output !== undefined && output.accountId !== accountId) { |
| 140 | return { action: "replace" } as const; |
| 141 | } |
| 142 | // An output belongs to exactly one live input. |
| 143 | const oldLiveInput = output?.liveInputId ?? olds?.liveInputId; |
| 144 | if ( |
| 145 | typeof oldLiveInput === "string" && |
| 146 | typeof news.liveInputId === "string" && |
| 147 | oldLiveInput !== news.liveInputId |
| 148 | ) { |
| 149 | return { action: "replace" } as const; |
| 150 | } |
| 151 | // The destination is immutable — only `enabled` can be updated. |
| 152 | const oldUrl = output?.url ?? olds?.url; |
| 153 | if (oldUrl !== undefined && oldUrl !== news.url) { |
| 154 | return { action: "replace" } as const; |
| 155 | } |
| 156 | const oldStreamKey = output?.streamKey ?? olds?.streamKey; |
| 157 | if (oldStreamKey !== undefined && oldStreamKey !== news.streamKey) { |
| 158 | return { action: "replace" } as const; |
| 159 | } |
| 160 | // `enabled` is mutable via PUT — default engine update logic. |
| 161 | return undefined; |
| 162 | }), |
| 163 | |
| 164 | read: Effect.fn(function* ({ output }) { |
| 165 | // Outputs have no deterministic name or tags, so a cold read (state |
| 166 | // lost before the uid was persisted) cannot recover the resource — |
| 167 | // rely on the cached uid. |
| 168 | if (output?.outputId === undefined) return undefined; |
| 169 | const observed = yield* findOutput(output.accountId, output.liveInputId, output.outputId); |
| 170 | return observed ? toAttributes(observed, output.accountId, output.liveInputId) : undefined; |
| 171 | }), |
| 172 | |
| 173 | reconcile: Effect.fn(function* ({ news, output }) { |
| 174 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 175 | // Inputs have been resolved to concrete strings by Plan. |
| 176 | const liveInputId = news.liveInputId as string; |
| 177 | |
| 178 | // Observe — the uid cached on `output` is a hint, not a guarantee: |
| 179 | // a missing output (or live input) falls through to "missing" and |
| 180 | // we recreate. |
| 181 | const observed = output?.outputId |
| 182 | ? yield* findOutput(output.accountId ?? accountId, liveInputId, output.outputId) |
| 183 | : undefined; |
| 184 | |
| 185 | if (!observed) { |
| 186 | // Ensure — greenfield (or out-of-band delete): create with the |
| 187 | // full desired body. uids are auto-assigned so there is no |
| 188 | // AlreadyExists race to tolerate. |
| 189 | const created = yield* stream.createLiveInputOutput({ |
| 190 | accountId, |
| 191 | liveInputIdentifier: liveInputId, |
| 192 | url: news.url, |
| 193 | streamKey: news.streamKey, |
| 194 | enabled: news.enabled, |
| 195 | }); |
| 196 | return toAttributes(created, accountId, liveInputId); |
| 197 | } |
| 198 | |
| 199 | // Sync — `enabled` is the only mutable aspect; diff observed cloud |
| 200 | // state against desired and skip the PUT on a no-op. |
| 201 | const observedAccount = output?.accountId ?? accountId; |
| 202 | const desiredEnabled = news.enabled ?? true; |
| 203 | if (desiredEnabled === (observed.enabled ?? true)) { |
| 204 | return toAttributes(observed, observedAccount, liveInputId); |
| 205 | } |
| 206 | |
| 207 | const updated = yield* stream.updateLiveInputOutput({ |
| 208 | accountId: observedAccount, |
| 209 | liveInputIdentifier: liveInputId, |
| 210 | outputIdentifier: observed.uid ?? output!.outputId, |
| 211 | enabled: desiredEnabled, |
| 212 | }); |
| 213 | return toAttributes(updated, observedAccount, liveInputId); |
| 214 | }), |
| 215 | |
| 216 | list: Effect.fn(function* () { |
| 217 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 218 | // Parent fan-out: outputs are sub-resources of a live input and |
| 219 | // have no account-wide enumeration endpoint. Enumerate every live |
| 220 | // input on the account, then list each input's outputs. |
| 221 | const inputs = yield* stream.listLiveInputs({ accountId }); |
| 222 | const liveInputIds = inputs |
| 223 | .map((input) => input.uid) |
| 224 | .filter((uid): uid is string => typeof uid === "string"); |
| 225 | |
| 226 | const rows = yield* Effect.forEach( |
| 227 | liveInputIds, |
| 228 | (liveInputId) => |
| 229 | stream.listLiveInputOutputs |
| 230 | .pages({ |
| 231 | accountId, |
| 232 | liveInputIdentifier: liveInputId, |
| 233 | }) |
| 234 | .pipe( |
| 235 | Stream.runCollect, |
| 236 | Effect.map((chunk) => |
| 237 | Array.from(chunk).flatMap((page) => |
| 238 | page.result.map((observed) => toAttributes(observed, accountId, liveInputId)), |
| 239 | ), |
| 240 | ), |
| 241 | // A live input deleted between enumeration and listing its |
| 242 | // outputs counts as having no outputs. |
| 243 | Effect.catchTag("LiveInputNotFound", () => Effect.succeed([])), |
| 244 | ), |
| 245 | { concurrency: 10 }, |
| 246 | ); |
| 247 | return rows.flat(); |
| 248 | }), |
| 249 | |
| 250 | delete: Effect.fn(function* ({ output }) { |
| 251 | // Idempotent — an output (or its parent live input) already deleted |
| 252 | // out-of-band surfaces as `OutputNotFound` (Cloudflare error code |
| 253 | // 10003). |
| 254 | yield* stream |
| 255 | .deleteLiveInputOutput({ |
| 256 | accountId: output.accountId, |
| 257 | liveInputIdentifier: output.liveInputId, |
| 258 | outputIdentifier: output.outputId, |
| 259 | }) |
| 260 | .pipe(Effect.catchTag("OutputNotFound", () => Effect.void)); |
| 261 | }), |
| 262 | }); |
| 263 | |
| 264 | type ObservedOutput = { |
| 265 | enabled?: boolean | null; |
| 266 | streamKey?: string | null; |
| 267 | uid?: string | null; |
| 268 | url?: string | null; |
| 269 | }; |
| 270 | |
| 271 | /** |
| 272 | * Find an output by uid under a live input, mapping "gone" — either the |
| 273 | * output missing from the list or the parent live input deleted |
| 274 | * (`LiveInputNotFound`, Cloudflare error code 10003) — to `undefined`. |
| 275 | * There is no get-by-uid endpoint, so observation goes through the list. |
| 276 | */ |
| 277 | const findOutput = (accountId: string, liveInputId: string, outputId: string) => |
| 278 | stream.listLiveInputOutputs({ accountId, liveInputIdentifier: liveInputId }).pipe( |
| 279 | Effect.map((page) => page.result.find((candidate) => candidate.uid === outputId)), |
| 280 | Effect.catchTag("LiveInputNotFound", () => Effect.undefined), |
| 281 | ); |
| 282 | |
| 283 | const toAttributes = ( |
| 284 | output: |
| 285 | | stream.CreateLiveInputOutputResponse |
| 286 | | stream.UpdateLiveInputOutputResponse |
| 287 | | ObservedOutput, |
| 288 | accountId: string, |
| 289 | liveInputId: string, |
| 290 | ): LiveInputOutputAttributes => ({ |
| 291 | outputId: output.uid ?? "", |
| 292 | liveInputId, |
| 293 | accountId, |
| 294 | url: output.url ?? "", |
| 295 | streamKey: output.streamKey ?? "", |
| 296 | enabled: output.enabled ?? true, |
| 297 | }); |