The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as S3 from "@alchemy.run/aws/S3"; |
| 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 * as Schema from "effect/Schema"; |
| 9 | |
| 10 | export class S3VersionedObjectLockFunction extends Lambda.Function<S3VersionedObjectLockFunction>()( |
| 11 | "S3VersionedObjectLockFunction", |
| 12 | ) {} |
| 13 | |
| 14 | const versionRequest = Schema.Struct({ |
| 15 | Key: Schema.String, |
| 16 | VersionId: Schema.optional(Schema.String), |
| 17 | }); |
| 18 | |
| 19 | export default S3VersionedObjectLockFunction.make( |
| 20 | { |
| 21 | main: import.meta.url, |
| 22 | functionUrl: true, |
| 23 | timeout: Duration.seconds(30), |
| 24 | memorySize: 512, |
| 25 | }, |
| 26 | Effect.gen(function* () { |
| 27 | const props = { |
| 28 | versioning: "Enabled", |
| 29 | objectLockEnabled: true, |
| 30 | forceDestroy: true, |
| 31 | } as const; |
| 32 | const buckets = { |
| 33 | GetObjectRetention: yield* S3.Bucket("VersionedObjectLockGetRetentionBucket", props), |
| 34 | PutObjectRetention: yield* S3.Bucket("VersionedObjectLockPutRetentionBucket", props), |
| 35 | GetObjectLegalHold: yield* S3.Bucket("VersionedObjectLockGetHoldBucket", props), |
| 36 | PutObjectLegalHold: yield* S3.Bucket("VersionedObjectLockPutHoldBucket", props), |
| 37 | RestoreObject: yield* S3.Bucket("VersionedObjectLockRestoreBucket", { |
| 38 | versioning: "Enabled", |
| 39 | forceDestroy: true, |
| 40 | }), |
| 41 | }; |
| 42 | const getRetention = yield* S3.GetObjectRetention(buckets.GetObjectRetention); |
| 43 | const putRetention = yield* S3.PutObjectRetention(buckets.PutObjectRetention); |
| 44 | const getHold = yield* S3.GetObjectLegalHold(buckets.GetObjectLegalHold); |
| 45 | const putHold = yield* S3.PutObjectLegalHold(buckets.PutObjectLegalHold); |
| 46 | const restore = yield* S3.RestoreObject(buckets.RestoreObject); |
| 47 | const info = yield* Effect.forEach(Object.entries(buckets), ([binding, bucket]) => |
| 48 | Effect.gen(function* () { |
| 49 | return { |
| 50 | binding, |
| 51 | bucketName: yield* bucket.bucketName, |
| 52 | bucketArn: yield* bucket.bucketArn, |
| 53 | }; |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | return { |
| 58 | fetch: Effect.gen(function* () { |
| 59 | const request = yield* HttpServerRequest; |
| 60 | const url = yield* Effect.sync(() => new URL(request.originalUrl)); |
| 61 | if (url.pathname === "/info") { |
| 62 | const resolved = yield* Effect.forEach(info, (bucket) => |
| 63 | Effect.gen(function* () { |
| 64 | return { |
| 65 | binding: bucket.binding, |
| 66 | bucketName: yield* bucket.bucketName, |
| 67 | bucketArn: yield* bucket.bucketArn, |
| 68 | }; |
| 69 | }), |
| 70 | ); |
| 71 | if (resolved.some((bucket) => !bucket.bucketName || !bucket.bucketArn)) { |
| 72 | return HttpServerResponse.text("Outputs not hydrated yet", { |
| 73 | status: 503, |
| 74 | }); |
| 75 | } |
| 76 | return yield* HttpServerResponse.json(resolved); |
| 77 | } |
| 78 | if (request.method !== "POST") { |
| 79 | return HttpServerResponse.text("Use POST", { status: 405 }); |
| 80 | } |
| 81 | const body = yield* request.json; |
| 82 | switch (url.pathname) { |
| 83 | case "/get-retention": { |
| 84 | const input = yield* Schema.decodeUnknownEffect(versionRequest)(body); |
| 85 | return yield* HttpServerResponse.json(yield* getRetention(input)); |
| 86 | } |
| 87 | case "/put-retention": { |
| 88 | const input = yield* Schema.decodeUnknownEffect( |
| 89 | Schema.Struct({ |
| 90 | Key: Schema.String, |
| 91 | VersionId: Schema.String, |
| 92 | retainUntil: Schema.String, |
| 93 | }), |
| 94 | )(body); |
| 95 | const RetainUntilDate = yield* Effect.sync(() => new Date(input.retainUntil)); |
| 96 | yield* putRetention({ |
| 97 | Key: input.Key, |
| 98 | VersionId: input.VersionId, |
| 99 | Retention: { Mode: "GOVERNANCE", RetainUntilDate }, |
| 100 | }); |
| 101 | return yield* HttpServerResponse.json({ retained: true }); |
| 102 | } |
| 103 | case "/get-hold": { |
| 104 | const input = yield* Schema.decodeUnknownEffect(versionRequest)(body); |
| 105 | return yield* HttpServerResponse.json(yield* getHold(input)); |
| 106 | } |
| 107 | case "/put-hold": { |
| 108 | const input = yield* Schema.decodeUnknownEffect( |
| 109 | Schema.Struct({ |
| 110 | Key: Schema.String, |
| 111 | VersionId: Schema.String, |
| 112 | Status: Schema.Literals(["ON", "OFF"]), |
| 113 | }), |
| 114 | )(body); |
| 115 | yield* putHold({ |
| 116 | Key: input.Key, |
| 117 | VersionId: input.VersionId, |
| 118 | LegalHold: { Status: input.Status }, |
| 119 | }); |
| 120 | return yield* HttpServerResponse.json({ updated: true }); |
| 121 | } |
| 122 | case "/restore": { |
| 123 | const input = yield* Schema.decodeUnknownEffect(versionRequest)(body); |
| 124 | const result = yield* restore({ |
| 125 | ...input, |
| 126 | RestoreRequest: { |
| 127 | Days: 1, |
| 128 | GlacierJobParameters: { Tier: "Standard" }, |
| 129 | }, |
| 130 | }).pipe( |
| 131 | Effect.as({ tag: "accepted", versionId: input.VersionId }), |
| 132 | Effect.catchTag("InvalidObjectState", () => |
| 133 | Effect.succeed({ tag: "InvalidObjectState" }), |
| 134 | ), |
| 135 | ); |
| 136 | return yield* HttpServerResponse.json(result); |
| 137 | } |
| 138 | default: |
| 139 | return HttpServerResponse.text("Not found", { status: 404 }); |
| 140 | } |
| 141 | }).pipe( |
| 142 | Effect.catchTag( |
| 143 | [ |
| 144 | "NoSuchVersion", |
| 145 | "NoSuchKey", |
| 146 | "MethodNotAllowed", |
| 147 | "InvalidRequest", |
| 148 | "AccessDeniedException", |
| 149 | ], |
| 150 | (error) => |
| 151 | HttpServerResponse.json( |
| 152 | { tag: error._tag }, |
| 153 | { |
| 154 | status: |
| 155 | error._tag === "MethodNotAllowed" |
| 156 | ? 405 |
| 157 | : error._tag === "InvalidRequest" |
| 158 | ? 400 |
| 159 | : error._tag === "AccessDeniedException" |
| 160 | ? 403 |
| 161 | : 404, |
| 162 | }, |
| 163 | ), |
| 164 | ), |
| 165 | Effect.orDie, |
| 166 | ), |
| 167 | }; |
| 168 | }).pipe( |
| 169 | Effect.provide( |
| 170 | Layer.mergeAll( |
| 171 | S3.GetObjectRetentionHttp, |
| 172 | S3.PutObjectRetentionHttp, |
| 173 | S3.GetObjectLegalHoldHttp, |
| 174 | S3.PutObjectLegalHoldHttp, |
| 175 | S3.RestoreObjectHttp, |
| 176 | ), |
| 177 | ), |
| 178 | ), |
| 179 | ); |