The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import { unpackEnvValue } from "alchemy/RuntimeContext"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import type { CacheCluster } from "./CacheCluster.ts"; |
| 7 | import { |
| 8 | ConnectCacheCluster, |
| 9 | cacheClusterConnectEnvPrefix, |
| 10 | type ConnectCacheClusterOptions, |
| 11 | } from "./ConnectCacheCluster.ts"; |
| 12 | |
| 13 | export const ConnectCacheClusterHttp = Layer.effect( |
| 14 | ConnectCacheCluster, |
| 15 | Effect.gen(function* () { |
| 16 | const bind = yield* AWSBindings.Bind; |
| 17 | return Effect.fn(function* (cluster: CacheCluster, options?: ConnectCacheClusterOptions) { |
| 18 | const prefix = cacheClusterConnectEnvPrefix(cluster.LogicalId); |
| 19 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 20 | yield* bind`AWS.ElastiCache.ConnectCacheCluster(${cluster})`({ |
| 21 | env: { |
| 22 | [`${prefix}_ENDPOINTS`]: Output.map(cluster.endpoints, JSON.stringify), |
| 23 | [`${prefix}_TLS`]: Output.map(cluster.transitEncryptionEnabled, (tls) => |
| 24 | String(tls ?? false), |
| 25 | ), |
| 26 | }, |
| 27 | }); |
| 28 | if (options?.subnetIds || options?.securityGroupIds) { |
| 29 | yield* AWSBindings.bindVpc`AWS.ElastiCache.ConnectCacheCluster(${cluster})`({ |
| 30 | subnetIds: options?.subnetIds ?? [], |
| 31 | securityGroupIds: options?.securityGroupIds ?? [], |
| 32 | }); |
| 33 | } |
| 34 | } |
| 35 | return Effect.gen(function* () { |
| 36 | const endpoints = unpackEnvValue<Array<{ address: string; port: number }>>( |
| 37 | process.env[`${prefix}_ENDPOINTS`], |
| 38 | ); |
| 39 | if (endpoints !== undefined) { |
| 40 | if (!endpoints.length) { |
| 41 | return yield* Effect.die( |
| 42 | `Memcached endpoints for '${cluster.LogicalId}' are not available`, |
| 43 | ); |
| 44 | } |
| 45 | return { |
| 46 | endpoints, |
| 47 | tls: unpackEnvValue<boolean>(process.env[`${prefix}_TLS`]) ?? false, |
| 48 | }; |
| 49 | } |
| 50 | const clusterEndpoints = yield* yield* cluster.endpoints; |
| 51 | if (!clusterEndpoints.length) { |
| 52 | return yield* Effect.die( |
| 53 | `Memcached endpoints for '${cluster.LogicalId}' are not available yet`, |
| 54 | ); |
| 55 | } |
| 56 | return { |
| 57 | endpoints: clusterEndpoints, |
| 58 | tls: yield* yield* cluster.transitEncryptionEnabled, |
| 59 | }; |
| 60 | }); |
| 61 | }); |
| 62 | }), |
| 63 | ); |