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 serverless from "@distilled.cloud/aws/redshift-serverless"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { toWireSeconds } from "alchemy/Util/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Redacted from "effect/Redacted"; |
| 7 | import * as AWSBindings from "../AWSBindings.ts"; |
| 8 | import { formatSqlConnectionUrl } from "../Connection/internal.ts"; |
| 9 | import { Connect, connectEnvPrefix, type ConnectOptions } from "./Connect.ts"; |
| 10 | import type { Workgroup } from "./Workgroup.ts"; |
| 11 | |
| 12 | const DEFAULT_PORT = 5439; |
| 13 | const DEFAULT_DATABASE = "dev"; |
| 14 | |
| 15 | const unwrap = (value: string | Redacted.Redacted<string> | undefined): string | undefined => |
| 16 | value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 17 | |
| 18 | /** |
| 19 | * SDK-backed implementation of {@link Connect}. Deploy half attaches the |
| 20 | * `redshift-serverless:GetCredentials` policy on the workgroup ARN and |
| 21 | * publishes `REDSHIFT_SERVERLESS_{LOGICAL_ID}_{HOST,PORT}` on the host |
| 22 | * Function; runtime half mints temporary credentials via |
| 23 | * `redshift-serverless:GetCredentials` and formats a pgwire connection URL. |
| 24 | */ |
| 25 | export const ConnectHttp = Layer.effect( |
| 26 | Connect, |
| 27 | Effect.gen(function* () { |
| 28 | const bind = yield* AWSBindings.Bind; |
| 29 | const getCredentials = yield* AWSBindings.withCredentials(serverless.getCredentials); |
| 30 | |
| 31 | return Effect.fn(function* (workgroup: Workgroup, options: ConnectOptions = {}) { |
| 32 | const WorkgroupName = yield* workgroup.workgroupName; |
| 33 | const Host = yield* workgroup.endpointAddress; |
| 34 | const Port = yield* workgroup.endpointPort; |
| 35 | |
| 36 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 37 | const prefix = connectEnvPrefix(workgroup.LogicalId); |
| 38 | yield* bind`AWS.RedshiftServerless.Connect(${workgroup})`({ |
| 39 | policyStatements: [ |
| 40 | { |
| 41 | Effect: "Allow", |
| 42 | Action: ["redshift-serverless:GetCredentials"], |
| 43 | Resource: [workgroup.workgroupArn], |
| 44 | }, |
| 45 | ], |
| 46 | env: { |
| 47 | [`${prefix}_HOST`]: workgroup.endpointAddress, |
| 48 | // Lambda environment variables are strings — stringify the port. |
| 49 | [`${prefix}_PORT`]: Output.interpolate`${workgroup.endpointPort}`, |
| 50 | }, |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | const database = options.database ?? DEFAULT_DATABASE; |
| 55 | |
| 56 | return Effect.gen(function* () { |
| 57 | const workgroupName = yield* WorkgroupName; |
| 58 | const host = yield* Host; |
| 59 | const port = (yield* Port) ?? DEFAULT_PORT; |
| 60 | if (!host) { |
| 61 | return yield* Effect.die( |
| 62 | `Redshift Serverless workgroup endpoint for '${workgroup.LogicalId}' is not available yet`, |
| 63 | ); |
| 64 | } |
| 65 | const credentials = yield* getCredentials({ |
| 66 | workgroupName, |
| 67 | dbName: database, |
| 68 | durationSeconds: toWireSeconds(options.duration), |
| 69 | }); |
| 70 | const username = unwrap(credentials.dbUser); |
| 71 | const rawPassword = unwrap(credentials.dbPassword); |
| 72 | const password = rawPassword === undefined ? undefined : Redacted.make(rawPassword); |
| 73 | return { |
| 74 | host, |
| 75 | port, |
| 76 | database, |
| 77 | username, |
| 78 | password, |
| 79 | // Redshift Serverless endpoints require TLS. |
| 80 | ssl: true, |
| 81 | expiration: credentials.expiration, |
| 82 | url: formatSqlConnectionUrl({ |
| 83 | host, |
| 84 | port, |
| 85 | database, |
| 86 | username, |
| 87 | password, |
| 88 | ssl: true, |
| 89 | }), |
| 90 | }; |
| 91 | }); |
| 92 | }); |
| 93 | }), |
| 94 | ); |