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

main
ts 112 lines 3.94 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 { FileSystem } from "./FileSystem.ts";
5
6 /**
7 * Shared scaffolding for the EFS runtime bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, makeEfs…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation, the IAM action list, and
12 * (for file-system-scoped operations) the injected `FileSystemId` is
13 * boilerplate.
14 */
15
16 /**
17 * Build the impl Effect for a file-system-scoped EFS operation: the runtime
18 * callable injects the bound {@link FileSystem}'s ID as `FileSystemId` and
19 * the deploy-time half grants `actions` on the file system's ARN.
20 */
21 export const makeEfsFileSystemHttpBinding = <
22 I extends { FileSystemId?: string },
23 A,
24 E,
25 R,
26 >(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.EFS.DescribeMountTargets`. */
28 tag: string;
29 /** The distilled operation; `FileSystemId` is injected from the file system. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted on the file system ARN. */
32 actions: readonly string[];
33 /**
34 * Additional IAM actions granted on `*`. Used for tag-on-create
35 * (`elasticfilesystem:TagResource` authorizes against the ARN of the
36 * resource being created, which is unknowable at deploy time).
37 */
38 wildcardActions?: readonly string[];
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 const op = yield* AWSBindings.withCredentials(options.operation);
43
44 return Effect.fn(function* (fileSystem: FileSystem) {
45 const FileSystemId = yield* fileSystem.fileSystemId;
46 if (!globalThis.__ALCHEMY_RUNTIME__) {
47 yield* bind`${options.tag}(${fileSystem})`({
48 policyStatements: [
49 {
50 Effect: "Allow",
51 Action: [...options.actions],
52 Resource: [Output.interpolate`${fileSystem.fileSystemArn}`],
53 },
54 ...(options.wildcardActions !== undefined && options.wildcardActions.length > 0
55 ? [
56 {
57 Effect: "Allow" as const,
58 Action: [...options.wildcardActions],
59 Resource: ["*"],
60 },
61 ]
62 : []),
63 ],
64 });
65 }
66 return Effect.fn(`${options.tag}(${fileSystem.LogicalId})`)(function* (
67 request?: Omit<I, "FileSystemId">,
68 ) {
69 return yield* op({
70 ...request,
71 FileSystemId: yield* FileSystemId,
72 } as I);
73 });
74 });
75 });
76
77 /**
78 * Build the impl Effect for an account-level EFS operation (no file-system
79 * argument): the deploy-time half grants `actions` on `*`. Used for
80 * operations that authorize against ARNs unknowable at deploy time (e.g.
81 * `DeleteAccessPoint` authorizes on the access point's own ARN, and access
82 * points deleted at runtime are typically created at runtime too).
83 */
84 export const makeEfsAccountHttpBinding = <I, A, E, R>(options: {
85 /** Fully-qualified binding tag, e.g. `AWS.EFS.DeleteAccessPoint`. */
86 tag: string;
87 /** The distilled operation, invoked with the caller's request as-is. */
88 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
89 /** IAM actions granted on `*`. */
90 actions: readonly string[];
91 }) =>
92 Effect.gen(function* () {
93 const bind = yield* AWSBindings.Bind;
94 const op = yield* AWSBindings.withCredentials(options.operation);
95
96 return Effect.fn(function* () {
97 if (!globalThis.__ALCHEMY_RUNTIME__) {
98 yield* bind`${options.tag}()`({
99 policyStatements: [
100 {
101 Effect: "Allow",
102 Action: [...options.actions],
103 Resource: ["*"],
104 },
105 ],
106 });
107 }
108 return Effect.fn(options.tag)(function* (request?: I) {
109 return yield* op((request ?? {}) as I);
110 });
111 });
112 });