@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 113 lines 4.19 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Namespace } from "./Namespace.ts";
5
6 /**
7 * Shared scaffolding for Amazon Redshift Serverless HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every snapshot/recovery-point/restore
10 * `{Op}Http.ts` in this service is a thin
11 * `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
12 * below. Everything except the operation, the identifier resolver, and the
13 * IAM action list is boilerplate. (`ConnectHttp` stays bespoke — it also
14 * publishes endpoint environment variables and formats connection URLs.)
15 */
16
17 /**
18 * Build the impl Effect for an account-level operation (snapshot
19 * administration, recovery-point reads, table-restore status). The
20 * deploy-time half grants `actions` on `*` — these operations span every
21 * namespace in the account and the identifiers they filter on are runtime
22 * data.
23 */
24 export const makeServerlessAccountHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.RedshiftServerless.GetSnapshot`. */
26 tag: string;
27 /** The distilled operation, invoked with the caller's request as-is. */
28 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
29 /** IAM actions granted on `*`. */
30 actions: readonly string[];
31 }) =>
32 Effect.gen(function* () {
33 const bind = yield* AWSBindings.Bind;
34 const op = yield* AWSBindings.withCredentials(options.operation);
35
36 return Effect.fn(function* () {
37 if (!globalThis.__ALCHEMY_RUNTIME__) {
38 yield* bind`${options.tag}()`({
39 policyStatements: [
40 {
41 Effect: "Allow",
42 Action: [...options.actions],
43 Resource: ["*"],
44 },
45 ],
46 });
47 }
48 return Effect.fn(options.tag)(function* (request?: I) {
49 return yield* op((request ?? {}) as I);
50 });
51 });
52 });
53
54 /**
55 * Build the impl Effect for a namespace-scoped operation: the runtime
56 * callable injects the bound {@link Namespace}'s name as `namespaceName`
57 * and the deploy-time half grants `actions` on the namespace ARN (plus any
58 * `extraResources`, e.g. the `snapshot/*` or `recoverypoint/*` ARN patterns
59 * that snapshot creation and restores also authorize against).
60 */
61 export const makeServerlessNamespaceHttpBinding = <
62 I extends { namespaceName?: string },
63 A,
64 E,
65 R,
66 >(options: {
67 /** Fully-qualified binding tag, e.g. `AWS.RedshiftServerless.CreateSnapshot`. */
68 tag: string;
69 /** The distilled operation; `namespaceName` is injected. */
70 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
71 /** IAM actions granted on the namespace ARN. */
72 actions: readonly string[];
73 /** Additional IAM resource ARNs derived from the namespace ARN. */
74 extraResources?: (namespaceArn: string) => string[];
75 }) =>
76 Effect.gen(function* () {
77 const bind = yield* AWSBindings.Bind;
78 const op = yield* AWSBindings.withCredentials(options.operation);
79
80 return Effect.fn(function* (namespace: Namespace) {
81 const Identifier = yield* namespace.namespaceName;
82 if (!globalThis.__ALCHEMY_RUNTIME__) {
83 const resources = options.extraResources;
84 yield* bind`${options.tag}(${namespace})`({
85 policyStatements: [
86 {
87 Effect: "Allow",
88 Action: [...options.actions],
89 Resource: resources
90 ? Output.map(namespace.namespaceArn, (arn) => [arn, ...resources(arn)])
91 : [Output.interpolate`${namespace.namespaceArn}`],
92 },
93 ],
94 });
95 }
96 return Effect.fn(`${options.tag}(${namespace.LogicalId})`)(function* (
97 request: Omit<I, "namespaceName">,
98 ) {
99 return yield* op({
100 ...request,
101 namespaceName: yield* Identifier,
102 } as I);
103 });
104 });
105 });
106
107 /**
108 * The `arn:…:{account}` prefix of a Redshift Serverless resource ARN —
109 * used to widen a namespace grant to sibling resource types (`snapshot/*`,
110 * `recoverypoint/*`, `workgroup/*`) that snapshot and restore operations
111 * also authorize against.
112 */
113 export const serverlessArnPrefix = (arn: string): string => arn.split(":").slice(0, 5).join(":");