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

main
ts 120 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 { Table } from "./Table.ts";
5 import type { TableBucket } from "./TableBucket.ts";
6
7 /**
8 * Shared scaffolding for the S3 Tables runtime bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, makeS3Tables…HttpBinding({ … }))` over one of the
12 * two builders below. Everything except the operation, the IAM action list,
13 * and the injected identifiers is boilerplate.
14 */
15
16 /**
17 * Build the impl Effect for a table-bucket-scoped S3 Tables operation: the
18 * runtime callable injects the bound {@link TableBucket}'s ARN as
19 * `tableBucketARN` and the deploy-time half grants `actions` on the bucket
20 * ARN (and everything under it, for operations like `ListTables` whose IAM
21 * resource is the namespace/table).
22 */
23 export const makeS3TablesTableBucketHttpBinding = <
24 I extends { tableBucketARN?: string },
25 A,
26 E,
27 R,
28 >(options: {
29 /** Fully-qualified binding tag, e.g. `AWS.S3Tables.ListNamespaces`. */
30 tag: string;
31 /** The distilled operation; `tableBucketARN` is injected from the bucket. */
32 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
33 /** IAM actions granted on the bucket ARN and its children. */
34 actions: readonly string[];
35 }) =>
36 Effect.gen(function* () {
37 const bind = yield* AWSBindings.Bind;
38 const op = yield* AWSBindings.withCredentials(options.operation);
39
40 return Effect.fn(function* (tableBucket: TableBucket) {
41 const tableBucketARN = yield* tableBucket.tableBucketArn;
42 if (!globalThis.__ALCHEMY_RUNTIME__) {
43 yield* bind`${options.tag}(${tableBucket})`({
44 policyStatements: [
45 {
46 Effect: "Allow",
47 Action: [...options.actions],
48 Resource: [
49 Output.interpolate`${tableBucket.tableBucketArn}`,
50 // List operations authorize against the namespaces/tables
51 // under the bucket, e.g. `…:bucket/name/table/*`.
52 Output.interpolate`${tableBucket.tableBucketArn}/*`,
53 ],
54 },
55 ],
56 });
57 }
58 return Effect.fn(`${options.tag}(${tableBucket.LogicalId})`)(function* (
59 request?: Omit<I, "tableBucketARN">,
60 ) {
61 return yield* op({
62 ...request,
63 tableBucketARN: yield* tableBucketARN,
64 } as I);
65 });
66 });
67 });
68
69 /**
70 * Build the impl Effect for a table-scoped S3 Tables operation: the runtime
71 * callable injects the bound {@link Table}'s `tableBucketARN`, `namespace`,
72 * and `name`, and the deploy-time half grants `actions` on the table's ARN.
73 */
74 export const makeS3TablesTableHttpBinding = <
75 I extends { tableBucketARN?: string; namespace?: string; name?: string },
76 A,
77 E,
78 R,
79 >(options: {
80 /** Fully-qualified binding tag, e.g. `AWS.S3Tables.GetTableMetadataLocation`. */
81 tag: string;
82 /**
83 * The distilled operation; `tableBucketARN`, `namespace`, and `name` are
84 * injected from the table.
85 */
86 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
87 /** IAM actions granted on the table ARN. */
88 actions: readonly string[];
89 }) =>
90 Effect.gen(function* () {
91 const bind = yield* AWSBindings.Bind;
92 const op = yield* AWSBindings.withCredentials(options.operation);
93
94 return Effect.fn(function* (table: Table) {
95 const tableBucketARN = yield* table.tableBucketArn;
96 const namespace = yield* table.namespace;
97 const name = yield* table.name;
98 if (!globalThis.__ALCHEMY_RUNTIME__) {
99 yield* bind`${options.tag}(${table})`({
100 policyStatements: [
101 {
102 Effect: "Allow",
103 Action: [...options.actions],
104 Resource: [Output.interpolate`${table.tableArn}`],
105 },
106 ],
107 });
108 }
109 return Effect.fn(`${options.tag}(${table.LogicalId})`)(function* (
110 request?: Omit<I, "tableBucketARN" | "namespace" | "name">,
111 ) {
112 return yield* op({
113 ...request,
114 tableBucketARN: yield* tableBucketARN,
115 namespace: yield* namespace,
116 name: yield* name,
117 } as I);
118 });
119 });
120 });