The best way to control Claude Code, Codex and more from any computer or phone

main
ts 90 lines 3.88 KB
Raw
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 {
7 ConnectReplicationGroup,
8 replicationGroupConnectEnvPrefix,
9 type ConnectReplicationGroupOptions,
10 } from "./ConnectReplicationGroup.ts";
11 import type { ReplicationGroup } from "./ReplicationGroup.ts";
12
13 export const ConnectReplicationGroupHttp = Layer.effect(
14 ConnectReplicationGroup,
15 Effect.gen(function* () {
16 const bind = yield* AWSBindings.Bind;
17 return Effect.fn(function* (group: ReplicationGroup, options?: ConnectReplicationGroupOptions) {
18 const prefix = replicationGroupConnectEnvPrefix(group.LogicalId);
19 // Outputs yield a deferred runtime effect. Keep the Output expressions
20 // for deploy-time bindings, where Apply resolves them before Lambda's
21 // API call, and use the deferred values only in the runtime closure.
22 const configurationHost = group.configurationEndpointAddress;
23 const primaryHost = group.primaryEndpointAddress;
24 const configurationPort = group.configurationEndpointPort;
25 const primaryPort = group.primaryEndpointPort;
26 const endpointHost = Output.flatMap(configurationHost, (host) =>
27 host === undefined ? primaryHost : Output.asOutput(host),
28 );
29 const endpointPort = Output.flatMap(configurationPort, (port) =>
30 port === undefined ? primaryPort : Output.asOutput(port),
31 );
32 const readerHost = Output.map(group.readerEndpointAddress, (host) => host ?? "");
33 const readerPort = Output.map(group.readerEndpointPort, (port) =>
34 port === undefined ? "" : String(port),
35 );
36 if (!globalThis.__ALCHEMY_RUNTIME__) {
37 yield* bind`AWS.ElastiCache.ConnectReplicationGroup(${group})`({
38 env: {
39 [`${prefix}_HOST`]: endpointHost,
40 [`${prefix}_PORT`]: Output.interpolate`${endpointPort}`,
41 [`${prefix}_TLS`]: Output.map(group.transitEncryptionEnabled, String),
42 [`${prefix}_READER_HOST`]: readerHost,
43 [`${prefix}_READER_PORT`]: readerPort,
44 },
45 });
46 if (options?.subnetIds || options?.securityGroupIds) {
47 yield* AWSBindings.bindVpc`AWS.ElastiCache.ConnectReplicationGroup(${group})`({
48 subnetIds: options?.subnetIds ?? [],
49 securityGroupIds: options?.securityGroupIds ?? [],
50 });
51 }
52 }
53 return Effect.gen(function* () {
54 const host = unpackEnvValue<string>(process.env[`${prefix}_HOST`]);
55 if (host !== undefined) {
56 const port = unpackEnvValue<number>(process.env[`${prefix}_PORT`]);
57 const tls = unpackEnvValue<boolean>(process.env[`${prefix}_TLS`]);
58 const readerHost = unpackEnvValue<string>(process.env[`${prefix}_READER_HOST`]);
59 const readerPort = unpackEnvValue<number>(process.env[`${prefix}_READER_PORT`]);
60 if (!host || port === undefined) {
61 return yield* Effect.die(
62 `ElastiCache endpoint for '${group.LogicalId}' is not available`,
63 );
64 }
65 return {
66 host,
67 port,
68 readerHost: readerHost || undefined,
69 readerPort: readerHost ? readerPort : undefined,
70 tls: tls ?? true,
71 };
72 }
73 const endpoint = yield* yield* endpointHost;
74 const port = yield* yield* endpointPort;
75 if (!endpoint || port === undefined) {
76 return yield* Effect.die(
77 `ElastiCache endpoint for '${group.LogicalId}' is not available yet`,
78 );
79 }
80 return {
81 host: endpoint,
82 port,
83 readerHost: yield* yield* group.readerEndpointAddress,
84 readerPort: yield* yield* group.readerEndpointPort,
85 tls: yield* yield* group.transitEncryptionEnabled,
86 };
87 });
88 });
89 }),
90 );