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 Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as MediaTailor from "@alchemy.run/aws/MediaTailor"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | // Channel-assembly names are runtime request parameters; these nonexistent |
| 13 | // names drive the typed not-found paths (proving the IAM grants reached the |
| 14 | // API — a missing grant would surface AccessDeniedException instead). |
| 15 | export const NONEXISTENT_CHANNEL = "alchemy-nonexistent-mediatailor-channel"; |
| 16 | export const NONEXISTENT_PROGRAM = "alchemy-nonexistent-mediatailor-program"; |
| 17 | |
| 18 | export class MediaTailorTestFunction extends Lambda.Function<Lambda.Function>()( |
| 19 | "MediaTailorTestFunction", |
| 20 | ) {} |
| 21 | |
| 22 | export default MediaTailorTestFunction.make( |
| 23 | { |
| 24 | main, |
| 25 | functionUrl: true, |
| 26 | // MediaTailor control-plane calls routinely take a few seconds; AWS's |
| 27 | // default 3s Lambda timeout is too tight under cold starts. |
| 28 | timeout: Duration.seconds(30), |
| 29 | }, |
| 30 | Effect.gen(function* () { |
| 31 | const config = yield* MediaTailor.PlaybackConfiguration("BindingsConfig", { |
| 32 | adDecisionServerUrl: "https://ads.example.com/vast?ip=[client_ip]", |
| 33 | videoContentSourceUrl: "https://origin.example.com/live", |
| 34 | }); |
| 35 | |
| 36 | const createPrefetchSchedule = yield* MediaTailor.CreatePrefetchSchedule(config); |
| 37 | const getPrefetchSchedule = yield* MediaTailor.GetPrefetchSchedule(config); |
| 38 | const deletePrefetchSchedule = yield* MediaTailor.DeletePrefetchSchedule(config); |
| 39 | const listPrefetchSchedules = yield* MediaTailor.ListPrefetchSchedules(config); |
| 40 | const listAlerts = yield* MediaTailor.ListAlerts(); |
| 41 | const getChannelSchedule = yield* MediaTailor.GetChannelSchedule(); |
| 42 | const startChannel = yield* MediaTailor.StartChannel(); |
| 43 | const stopChannel = yield* MediaTailor.StopChannel(); |
| 44 | const createProgram = yield* MediaTailor.CreateProgram(); |
| 45 | const describeProgram = yield* MediaTailor.DescribeProgram(); |
| 46 | const updateProgram = yield* MediaTailor.UpdateProgram(); |
| 47 | const deleteProgram = yield* MediaTailor.DeleteProgram(); |
| 48 | |
| 49 | return { |
| 50 | fetch: Effect.gen(function* () { |
| 51 | const request = yield* HttpServerRequest; |
| 52 | const url = new URL(request.originalUrl); |
| 53 | const pathname = url.pathname; |
| 54 | |
| 55 | if (request.method === "GET" && pathname === "/health") { |
| 56 | return yield* HttpServerResponse.json({ ok: true }); |
| 57 | } |
| 58 | |
| 59 | if (request.method === "POST" && pathname === "/prefetch/create") { |
| 60 | const body = (yield* request.json) as unknown as { name: string }; |
| 61 | const now = yield* Effect.sync(() => Date.now()); |
| 62 | const result = yield* createPrefetchSchedule({ |
| 63 | Name: body.name, |
| 64 | Retrieval: { EndTime: new Date(now + 60 * 60 * 1000) }, |
| 65 | Consumption: { EndTime: new Date(now + 2 * 60 * 60 * 1000) }, |
| 66 | }).pipe( |
| 67 | Effect.map((r) => ({ |
| 68 | arn: r.Arn, |
| 69 | error: undefined, |
| 70 | detail: undefined, |
| 71 | })), |
| 72 | Effect.catch((e) => |
| 73 | Effect.succeed({ |
| 74 | arn: undefined, |
| 75 | error: e._tag, |
| 76 | detail: String(e), |
| 77 | }), |
| 78 | ), |
| 79 | ); |
| 80 | return yield* HttpServerResponse.json(result); |
| 81 | } |
| 82 | |
| 83 | if (request.method === "GET" && pathname === "/prefetch/get") { |
| 84 | const name = url.searchParams.get("name")!; |
| 85 | const result = yield* getPrefetchSchedule({ Name: name }).pipe( |
| 86 | Effect.map((r) => ({ |
| 87 | name: r.Name, |
| 88 | error: undefined, |
| 89 | detail: undefined, |
| 90 | })), |
| 91 | Effect.catch((e) => |
| 92 | Effect.succeed({ |
| 93 | name: undefined, |
| 94 | error: e._tag, |
| 95 | detail: String(e), |
| 96 | }), |
| 97 | ), |
| 98 | ); |
| 99 | return yield* HttpServerResponse.json(result); |
| 100 | } |
| 101 | |
| 102 | if (request.method === "GET" && pathname === "/prefetch/list") { |
| 103 | const result = yield* listPrefetchSchedules({}).pipe( |
| 104 | Effect.map((r) => ({ |
| 105 | names: (r.Items ?? []).map((item) => item.Name), |
| 106 | error: undefined, |
| 107 | detail: undefined, |
| 108 | })), |
| 109 | Effect.catch((e) => |
| 110 | Effect.succeed({ |
| 111 | names: [] as string[], |
| 112 | error: e._tag, |
| 113 | detail: String(e), |
| 114 | }), |
| 115 | ), |
| 116 | ); |
| 117 | return yield* HttpServerResponse.json(result); |
| 118 | } |
| 119 | |
| 120 | if (request.method === "POST" && pathname === "/prefetch/delete") { |
| 121 | const body = (yield* request.json) as unknown as { name: string }; |
| 122 | const result = yield* deletePrefetchSchedule({ |
| 123 | Name: body.name, |
| 124 | }).pipe( |
| 125 | Effect.map(() => ({ |
| 126 | deleted: true, |
| 127 | error: undefined, |
| 128 | detail: undefined, |
| 129 | })), |
| 130 | Effect.catch((e) => |
| 131 | Effect.succeed({ |
| 132 | deleted: false, |
| 133 | error: e._tag, |
| 134 | detail: String(e), |
| 135 | }), |
| 136 | ), |
| 137 | ); |
| 138 | return yield* HttpServerResponse.json(result); |
| 139 | } |
| 140 | |
| 141 | if (request.method === "GET" && pathname === "/alerts") { |
| 142 | const arn = url.searchParams.get("arn")!; |
| 143 | const result = yield* listAlerts({ ResourceArn: arn }).pipe( |
| 144 | Effect.map((r) => ({ |
| 145 | count: r.Items?.length ?? 0, |
| 146 | error: undefined, |
| 147 | detail: undefined, |
| 148 | })), |
| 149 | Effect.catch((e) => Effect.succeed({ count: 0, error: e._tag, detail: String(e) })), |
| 150 | ); |
| 151 | return yield* HttpServerResponse.json(result); |
| 152 | } |
| 153 | |
| 154 | if (request.method === "GET" && pathname === "/channel/schedule") { |
| 155 | const name = url.searchParams.get("name")!; |
| 156 | const result = yield* getChannelSchedule({ ChannelName: name }).pipe( |
| 157 | Effect.map((r) => ({ |
| 158 | count: r.Items?.length ?? 0, |
| 159 | error: undefined, |
| 160 | detail: undefined, |
| 161 | })), |
| 162 | Effect.catch((e) => Effect.succeed({ count: 0, error: e._tag, detail: String(e) })), |
| 163 | ); |
| 164 | return yield* HttpServerResponse.json(result); |
| 165 | } |
| 166 | |
| 167 | if (request.method === "POST" && pathname === "/channel/start") { |
| 168 | const body = (yield* request.json) as unknown as { name: string }; |
| 169 | const result = yield* startChannel({ ChannelName: body.name }).pipe( |
| 170 | Effect.map(() => ({ |
| 171 | started: true, |
| 172 | error: undefined, |
| 173 | detail: undefined, |
| 174 | })), |
| 175 | Effect.catch((e) => |
| 176 | Effect.succeed({ |
| 177 | started: false, |
| 178 | error: e._tag, |
| 179 | detail: String(e), |
| 180 | }), |
| 181 | ), |
| 182 | ); |
| 183 | return yield* HttpServerResponse.json(result); |
| 184 | } |
| 185 | |
| 186 | if (request.method === "POST" && pathname === "/channel/stop") { |
| 187 | const body = (yield* request.json) as unknown as { name: string }; |
| 188 | const result = yield* stopChannel({ ChannelName: body.name }).pipe( |
| 189 | Effect.map(() => ({ |
| 190 | stopped: true, |
| 191 | error: undefined, |
| 192 | detail: undefined, |
| 193 | })), |
| 194 | Effect.catch((e) => |
| 195 | Effect.succeed({ |
| 196 | stopped: false, |
| 197 | error: e._tag, |
| 198 | detail: String(e), |
| 199 | }), |
| 200 | ), |
| 201 | ); |
| 202 | return yield* HttpServerResponse.json(result); |
| 203 | } |
| 204 | |
| 205 | if (request.method === "POST" && pathname === "/program/create") { |
| 206 | const result = yield* createProgram({ |
| 207 | ChannelName: NONEXISTENT_CHANNEL, |
| 208 | ProgramName: NONEXISTENT_PROGRAM, |
| 209 | SourceLocationName: "alchemy-nonexistent-source-location", |
| 210 | VodSourceName: "alchemy-nonexistent-vod-source", |
| 211 | ScheduleConfiguration: { |
| 212 | Transition: { |
| 213 | Type: "RELATIVE", |
| 214 | RelativePosition: "AFTER_PROGRAM", |
| 215 | }, |
| 216 | }, |
| 217 | }).pipe( |
| 218 | Effect.map(() => ({ |
| 219 | created: true, |
| 220 | error: undefined, |
| 221 | detail: undefined, |
| 222 | })), |
| 223 | Effect.catch((e) => |
| 224 | Effect.succeed({ |
| 225 | created: false, |
| 226 | error: e._tag, |
| 227 | detail: String(e), |
| 228 | }), |
| 229 | ), |
| 230 | ); |
| 231 | return yield* HttpServerResponse.json(result); |
| 232 | } |
| 233 | |
| 234 | if (request.method === "GET" && pathname === "/program") { |
| 235 | const result = yield* describeProgram({ |
| 236 | ChannelName: NONEXISTENT_CHANNEL, |
| 237 | ProgramName: NONEXISTENT_PROGRAM, |
| 238 | }).pipe( |
| 239 | Effect.map((r) => ({ |
| 240 | name: r.ProgramName, |
| 241 | error: undefined, |
| 242 | detail: undefined, |
| 243 | })), |
| 244 | Effect.catch((e) => |
| 245 | Effect.succeed({ |
| 246 | name: undefined, |
| 247 | error: e._tag, |
| 248 | detail: String(e), |
| 249 | }), |
| 250 | ), |
| 251 | ); |
| 252 | return yield* HttpServerResponse.json(result); |
| 253 | } |
| 254 | |
| 255 | if (request.method === "POST" && pathname === "/program/update") { |
| 256 | const result = yield* updateProgram({ |
| 257 | ChannelName: NONEXISTENT_CHANNEL, |
| 258 | ProgramName: NONEXISTENT_PROGRAM, |
| 259 | ScheduleConfiguration: {}, |
| 260 | }).pipe( |
| 261 | Effect.map(() => ({ |
| 262 | updated: true, |
| 263 | error: undefined, |
| 264 | detail: undefined, |
| 265 | })), |
| 266 | Effect.catch((e) => |
| 267 | Effect.succeed({ |
| 268 | updated: false, |
| 269 | error: e._tag, |
| 270 | detail: String(e), |
| 271 | }), |
| 272 | ), |
| 273 | ); |
| 274 | return yield* HttpServerResponse.json(result); |
| 275 | } |
| 276 | |
| 277 | if (request.method === "POST" && pathname === "/program/delete") { |
| 278 | const result = yield* deleteProgram({ |
| 279 | ChannelName: NONEXISTENT_CHANNEL, |
| 280 | ProgramName: NONEXISTENT_PROGRAM, |
| 281 | }).pipe( |
| 282 | Effect.map(() => ({ |
| 283 | deleted: true, |
| 284 | error: undefined, |
| 285 | detail: undefined, |
| 286 | })), |
| 287 | Effect.catch((e) => |
| 288 | Effect.succeed({ |
| 289 | deleted: false, |
| 290 | error: e._tag, |
| 291 | detail: String(e), |
| 292 | }), |
| 293 | ), |
| 294 | ); |
| 295 | return yield* HttpServerResponse.json(result); |
| 296 | } |
| 297 | |
| 298 | return yield* HttpServerResponse.json( |
| 299 | { error: "Not found", method: request.method, pathname }, |
| 300 | { status: 404 }, |
| 301 | ); |
| 302 | }).pipe(Effect.orDie), |
| 303 | }; |
| 304 | }).pipe( |
| 305 | Effect.provide( |
| 306 | Layer.mergeAll( |
| 307 | MediaTailor.CreatePrefetchScheduleHttp, |
| 308 | MediaTailor.GetPrefetchScheduleHttp, |
| 309 | MediaTailor.DeletePrefetchScheduleHttp, |
| 310 | MediaTailor.ListPrefetchSchedulesHttp, |
| 311 | MediaTailor.ListAlertsHttp, |
| 312 | MediaTailor.GetChannelScheduleHttp, |
| 313 | MediaTailor.StartChannelHttp, |
| 314 | MediaTailor.StopChannelHttp, |
| 315 | MediaTailor.CreateProgramHttp, |
| 316 | MediaTailor.DescribeProgramHttp, |
| 317 | MediaTailor.UpdateProgramHttp, |
| 318 | MediaTailor.DeleteProgramHttp, |
| 319 | ), |
| 320 | ), |
| 321 | ), |
| 322 | ); |