| 1 | import * as sqs from "@distilled.cloud/aws/sqs"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import * as Output from "../../Output.ts"; |
| 6 | import { isInstance } from "../EC2/Instance.ts"; |
| 7 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 8 | import type { Queue } from "./Queue.ts"; |
| 9 | import { |
| 10 | StartMessageMoveTask, |
| 11 | type StartMessageMoveTaskRequest, |
| 12 | } from "./StartMessageMoveTask.ts"; |
| 13 | |
| 14 | // Bespoke (not the shared scaffold): redrive spans TWO queues. The source |
| 15 | // (DLQ) needs the start/receive/delete/get-attributes actions, and the |
| 16 | // destination needs send/get-attributes — on `*` when no explicit |
| 17 | // destination is given, because "redrive to original source queues" targets |
| 18 | // queues unknowable at deploy time. |
| 19 | export const StartMessageMoveTaskHttp = Layer.effect( |
| 20 | StartMessageMoveTask, |
| 21 | Effect.gen(function* () { |
| 22 | const startMessageMoveTask = yield* sqs.startMessageMoveTask; |
| 23 | |
| 24 | return Effect.fn(function* ( |
| 25 | source: Queue, |
| 26 | options?: { destination?: Queue }, |
| 27 | ) { |
| 28 | const SourceArn = yield* source.queueArn; |
| 29 | const destination = options?.destination; |
| 30 | const DestinationArn = destination |
| 31 | ? yield* destination.queueArn |
| 32 | : undefined; |
| 33 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 34 | const host = yield* Binding.Host; |
| 35 | if (isBindingHost(host) || isInstance(host)) { |
| 36 | yield* host.bind`Allow(${host}, AWS.SQS.StartMessageMoveTask(${source}))`( |
| 37 | { |
| 38 | policyStatements: [ |
| 39 | { |
| 40 | Effect: "Allow", |
| 41 | Action: [ |
| 42 | "sqs:StartMessageMoveTask", |
| 43 | "sqs:ReceiveMessage", |
| 44 | "sqs:DeleteMessage", |
| 45 | "sqs:GetQueueAttributes", |
| 46 | ], |
| 47 | Resource: [Output.interpolate`${source.queueArn}`], |
| 48 | }, |
| 49 | { |
| 50 | Effect: "Allow", |
| 51 | Action: ["sqs:SendMessage", "sqs:GetQueueAttributes"], |
| 52 | Resource: [ |
| 53 | destination |
| 54 | ? Output.interpolate`${destination.queueArn}` |
| 55 | : "*", |
| 56 | ], |
| 57 | }, |
| 58 | ], |
| 59 | }, |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | return Effect.fn(`AWS.SQS.StartMessageMoveTask(${source.LogicalId})`)( |
| 64 | function* (request?: StartMessageMoveTaskRequest) { |
| 65 | return yield* startMessageMoveTask({ |
| 66 | ...request, |
| 67 | SourceArn: yield* SourceArn, |
| 68 | ...(DestinationArn !== undefined |
| 69 | ? { DestinationArn: yield* DestinationArn } |
| 70 | : {}), |
| 71 | }); |
| 72 | }, |
| 73 | ); |
| 74 | }); |
| 75 | }), |
| 76 | ); |