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 DataSync from "@alchemy.run/aws/DataSync"; |
| 2 | import * as IAM from "@alchemy.run/aws/IAM"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as S3 from "@alchemy.run/aws/S3"; |
| 5 | import * as Output from "alchemy/Output"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 9 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import path from "pathe"; |
| 14 | |
| 15 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 16 | |
| 17 | const trustPolicy = { |
| 18 | Version: "2012-10-17" as const, |
| 19 | Statement: [ |
| 20 | { |
| 21 | Effect: "Allow" as const, |
| 22 | Principal: { Service: "datasync.amazonaws.com" }, |
| 23 | Action: ["sts:AssumeRole"], |
| 24 | }, |
| 25 | ], |
| 26 | }; |
| 27 | |
| 28 | const s3Policy = (arn: Output.Output<string>) => ({ |
| 29 | Version: "2012-10-17" as const, |
| 30 | Statement: [ |
| 31 | { |
| 32 | Effect: "Allow" as const, |
| 33 | Action: ["s3:GetBucketLocation", "s3:ListBucket", "s3:ListBucketMultipartUploads"], |
| 34 | Resource: [arn], |
| 35 | }, |
| 36 | { |
| 37 | Effect: "Allow" as const, |
| 38 | Action: [ |
| 39 | "s3:AbortMultipartUpload", |
| 40 | "s3:DeleteObject", |
| 41 | "s3:GetObject", |
| 42 | "s3:GetObjectTagging", |
| 43 | "s3:ListMultipartUploadParts", |
| 44 | "s3:PutObject", |
| 45 | "s3:PutObjectTagging", |
| 46 | ], |
| 47 | Resource: [Output.interpolate`${arn}/*`], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | |
| 52 | export class DataSyncTestFunction extends Lambda.Function<Lambda.Function>()( |
| 53 | "DataSyncTestFunction", |
| 54 | ) {} |
| 55 | |
| 56 | export default DataSyncTestFunction.make( |
| 57 | { |
| 58 | main, |
| 59 | functionUrl: true, |
| 60 | timeout: Duration.seconds(60), |
| 61 | }, |
| 62 | Effect.gen(function* () { |
| 63 | // The transfer under test: two S3 locations over empty buckets, so a |
| 64 | // started execution moves no data and a cancel closes it out quickly. |
| 65 | const src = yield* S3.Bucket("BindingsSrc", { forceDestroy: true }); |
| 66 | const dst = yield* S3.Bucket("BindingsDst", { forceDestroy: true }); |
| 67 | const role = yield* IAM.Role("BindingsRole", { |
| 68 | assumeRolePolicyDocument: trustPolicy, |
| 69 | inlinePolicies: { |
| 70 | Src: s3Policy(src.bucketArn), |
| 71 | Dst: s3Policy(dst.bucketArn), |
| 72 | }, |
| 73 | }); |
| 74 | const source = yield* DataSync.LocationS3("BindingsSrcLoc", { |
| 75 | s3BucketArn: src.bucketArn, |
| 76 | bucketAccessRoleArn: role.roleArn, |
| 77 | }); |
| 78 | const dest = yield* DataSync.LocationS3("BindingsDstLoc", { |
| 79 | s3BucketArn: dst.bucketArn, |
| 80 | bucketAccessRoleArn: role.roleArn, |
| 81 | }); |
| 82 | const task = yield* DataSync.Task("BindingsTask", { |
| 83 | sourceLocationArn: source.locationArn, |
| 84 | destinationLocationArn: dest.locationArn, |
| 85 | }); |
| 86 | |
| 87 | // Event source: subscribe the host to DataSync task/execution state |
| 88 | // changes. The deploy proves the EventBridge rule + invoke permission |
| 89 | // wiring. |
| 90 | yield* DataSync.consumeTaskEvents( |
| 91 | { kinds: ["task-state-change", "task-execution-state-change"] }, |
| 92 | (events) => |
| 93 | Stream.runForEach(events, (event) => |
| 94 | Effect.log(`datasync ${event["detail-type"]}: ${event.detail.State}`), |
| 95 | ), |
| 96 | ); |
| 97 | |
| 98 | const startTaskExecution = yield* DataSync.StartTaskExecution(task); |
| 99 | const describeTask = yield* DataSync.DescribeTask(task); |
| 100 | const listTaskExecutions = yield* DataSync.ListTaskExecutions(task); |
| 101 | const describeTaskExecution = yield* DataSync.DescribeTaskExecution(task); |
| 102 | const cancelTaskExecution = yield* DataSync.CancelTaskExecution(task); |
| 103 | const updateTaskExecution = yield* DataSync.UpdateTaskExecution(task); |
| 104 | |
| 105 | const bound = { |
| 106 | startTaskExecution, |
| 107 | describeTask, |
| 108 | listTaskExecutions, |
| 109 | describeTaskExecution, |
| 110 | cancelTaskExecution, |
| 111 | updateTaskExecution, |
| 112 | }; |
| 113 | |
| 114 | return { |
| 115 | fetch: Effect.gen(function* () { |
| 116 | const request = yield* HttpServerRequest; |
| 117 | const url = new URL(request.originalUrl); |
| 118 | const pathname = url.pathname; |
| 119 | const arn = url.searchParams.get("arn"); |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/bindings") { |
| 122 | return yield* HttpServerResponse.json({ |
| 123 | bound: Object.keys(bound), |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | // Task-scoped read: the TaskArn is injected from the binding. |
| 128 | if (request.method === "GET" && pathname === "/task") { |
| 129 | const detail = yield* describeTask(); |
| 130 | return yield* HttpServerResponse.json({ |
| 131 | taskArn: detail.TaskArn, |
| 132 | status: detail.Status, |
| 133 | name: detail.Name, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | // Start a run. A freshly-attached IAM role can transiently fail |
| 138 | // DataSync's location access test (typed LocationAccessTestFailed); |
| 139 | // retry briefly in-route, then report the typed tag so the test can |
| 140 | // re-poll instead of treating it as a hard failure. |
| 141 | if (request.method === "POST" && pathname === "/start") { |
| 142 | const started = yield* startTaskExecution().pipe( |
| 143 | Effect.retry({ |
| 144 | while: (e): boolean => e._tag === "LocationAccessTestFailed", |
| 145 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(5)]), |
| 146 | }), |
| 147 | Effect.result, |
| 148 | ); |
| 149 | return yield* HttpServerResponse.json( |
| 150 | started._tag === "Success" |
| 151 | ? { ok: true, executionArn: started.success.TaskExecutionArn } |
| 152 | : { ok: false, tag: started.failure._tag }, |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | // List the task's runs: the TaskArn is injected from the binding. |
| 157 | if (request.method === "GET" && pathname === "/executions") { |
| 158 | const { TaskExecutions } = yield* listTaskExecutions(); |
| 159 | return yield* HttpServerResponse.json({ |
| 160 | arns: (TaskExecutions ?? []).map((e) => e.TaskExecutionArn), |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | if (arn === null) { |
| 165 | return yield* HttpServerResponse.json({ error: "missing arn" }, { status: 400 }); |
| 166 | } |
| 167 | |
| 168 | // Execution-addressed reads/writes: the execution ARN comes from |
| 169 | // /start; IAM was granted on the bound task's execution pattern. |
| 170 | if (request.method === "GET" && pathname === "/execution") { |
| 171 | const execution = yield* describeTaskExecution({ |
| 172 | TaskExecutionArn: arn, |
| 173 | }); |
| 174 | return yield* HttpServerResponse.json({ |
| 175 | status: execution.Status, |
| 176 | bytesTransferred: execution.BytesTransferred, |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | // Throttling is only valid while the run is launching/preparing/ |
| 181 | // transferring/verifying — report the typed rejection instead of |
| 182 | // failing so the test can assert the binding round-trips. |
| 183 | if (request.method === "POST" && pathname === "/throttle") { |
| 184 | const updated = yield* updateTaskExecution({ |
| 185 | TaskExecutionArn: arn, |
| 186 | Options: { BytesPerSecond: 1024 * 1024 }, |
| 187 | }).pipe(Effect.result); |
| 188 | return yield* HttpServerResponse.json( |
| 189 | updated._tag === "Success" |
| 190 | ? { updated: true } |
| 191 | : { updated: false, tag: updated.failure._tag }, |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | if (request.method === "POST" && pathname === "/cancel") { |
| 196 | const cancelled = yield* cancelTaskExecution({ |
| 197 | TaskExecutionArn: arn, |
| 198 | }).pipe(Effect.result); |
| 199 | return yield* HttpServerResponse.json( |
| 200 | cancelled._tag === "Success" |
| 201 | ? { cancelled: true } |
| 202 | : { cancelled: false, tag: cancelled.failure._tag }, |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | return yield* HttpServerResponse.json( |
| 207 | { error: "Not found", method: request.method, pathname }, |
| 208 | { status: 404 }, |
| 209 | ); |
| 210 | }).pipe(Effect.orDie), |
| 211 | }; |
| 212 | }).pipe( |
| 213 | Effect.provide( |
| 214 | Layer.mergeAll( |
| 215 | Lambda.EventSource, |
| 216 | DataSync.StartTaskExecutionHttp, |
| 217 | DataSync.DescribeTaskHttp, |
| 218 | DataSync.ListTaskExecutionsHttp, |
| 219 | DataSync.DescribeTaskExecutionHttp, |
| 220 | DataSync.CancelTaskExecutionHttp, |
| 221 | DataSync.UpdateTaskExecutionHttp, |
| 222 | ), |
| 223 | ), |
| 224 | ), |
| 225 | ); |