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, 06:00 UTC
| 1 | import { createHash } from "node:crypto"; |
| 2 | import * as Binding from "alchemy/Binding"; |
| 3 | import * as Output from "alchemy/Output"; |
| 4 | import { defaultProviderMode } from "alchemy/ProviderMode"; |
| 5 | import { CurrentRuntimeContext, type RuntimeContext } from "alchemy/RuntimeContext"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { bindBackendEnvironment } from "./BackendConnection.ts"; |
| 13 | import type { Bucket } from "./Bucket.ts"; |
| 14 | import { Credential, validateCredential } from "./Credential.ts"; |
| 15 | import { scopeIdentity, usesInjectedCredentials } from "./CredentialScope.ts"; |
| 16 | import { FunctionEnvironment } from "./FunctionEnvironment.ts"; |
| 17 | import { makeStorageClient, type StorageClient, type StorageConfig } from "./Storage.ts"; |
| 18 | |
| 19 | export interface StorageBindingOptions { |
| 20 | /** Explicit sharing; validated against the requested scope and branch lineage. */ |
| 21 | credential?: Credential; |
| 22 | } |
| 23 | export class StorageBindingError extends Data.TaggedError("StorageBindingError")<{ |
| 24 | message: string; |
| 25 | }> {} |
| 26 | |
| 27 | export type RuntimeStorageMethods<K extends keyof StorageClient> = { |
| 28 | [P in K]: ( |
| 29 | ...args: Parameters<StorageClient[P]> |
| 30 | ) => Effect.Effect< |
| 31 | Effect.Success<ReturnType<StorageClient[P]>>, |
| 32 | Effect.Error<ReturnType<StorageClient[P]>> | StorageBindingError, |
| 33 | RuntimeContext |
| 34 | >; |
| 35 | }; |
| 36 | |
| 37 | /** Internal host wiring shared by read/write and object-bound capabilities. */ |
| 38 | export const makeStorageBinding = (scope: "storage:read" | "storage:write") => |
| 39 | Effect.gen(function* () { |
| 40 | const http = yield* HttpClient.HttpClient; |
| 41 | const runtime = yield* CurrentRuntimeContext; |
| 42 | if (!runtime) |
| 43 | return yield* Effect.die( |
| 44 | new StorageBindingError({ |
| 45 | message: "Storage bindings require a Platform host", |
| 46 | }), |
| 47 | ); |
| 48 | const environment = yield* Effect.serviceOption(FunctionEnvironment); |
| 49 | const createCredential = yield* Credential; |
| 50 | return Effect.fn(function* (bucket: Bucket, options: StorageBindingOptions = {}) { |
| 51 | const host = yield* Binding.Host; |
| 52 | if (!host) |
| 53 | return yield* Effect.die( |
| 54 | new StorageBindingError({ |
| 55 | message: "Storage binding requires a runtime host", |
| 56 | }), |
| 57 | ); |
| 58 | const digest = yield* Effect.sync(() => |
| 59 | createHash("sha256") |
| 60 | .update( |
| 61 | `${host.FQN}:${scopeIdentity(bucket.Props) ?? bucket.FQN}:${scope}:${options.credential?.FQN ?? "managed"}`, |
| 62 | ) |
| 63 | .digest("hex") |
| 64 | .slice(0, 24), |
| 65 | ); |
| 66 | const prefix = `NEON_STORAGE_${digest.toUpperCase()}`; |
| 67 | const bucketKey = `${prefix}_${yield* Effect.sync(() => createHash("sha256").update(bucket.FQN).digest("hex").slice(0, 12).toUpperCase())}_BUCKET`; |
| 68 | const keys = { |
| 69 | injected: `${prefix}_INJECTED`, |
| 70 | endpoint: `${prefix}_ENDPOINT`, |
| 71 | region: `${prefix}_REGION`, |
| 72 | accessKeyId: `${prefix}_ACCESS_KEY_ID`, |
| 73 | secretAccessKey: `${prefix}_SECRET_ACCESS_KEY`, |
| 74 | }; |
| 75 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 76 | const mode = host.Mode ?? (yield* defaultProviderMode); |
| 77 | const injected = !options.credential && usesInjectedCredentials(host, bucket.Props, mode); |
| 78 | const env: Record<string, Output.Output<string | Redacted.Redacted<string>>> = { |
| 79 | [bucketKey]: bucket.bucketName, |
| 80 | [keys.injected]: Output.literal(injected ? "yes" : "no"), |
| 81 | [keys.endpoint]: bucket.endpoint, |
| 82 | [keys.region]: bucket.region, |
| 83 | }; |
| 84 | if (!injected) { |
| 85 | const credential = |
| 86 | options.credential ?? |
| 87 | (yield* createCredential(`Storage${digest}`, { |
| 88 | ...(bucket.Props.branch !== undefined |
| 89 | ? { branch: bucket.Props.branch } |
| 90 | : { project: bucket.Props.project }), |
| 91 | scopes: scope === "storage:write" ? ["storage:read", "storage:write"] : [scope], |
| 92 | })); |
| 93 | const validated = Output.all(Output.of(credential), Output.of(bucket)).pipe( |
| 94 | Output.mapEffect( |
| 95 | Effect.fn(function* ([credential, target]) { |
| 96 | yield* validateCredential(credential, target, scope); |
| 97 | if (scope === "storage:write") |
| 98 | yield* validateCredential(credential, target, "storage:read"); |
| 99 | return credential; |
| 100 | }, Effect.orDie), |
| 101 | ), |
| 102 | ); |
| 103 | env[keys.accessKeyId] = validated.tokenId; |
| 104 | env[keys.secretAccessKey] = validated.s3SecretAccessKey; |
| 105 | } |
| 106 | yield* bindBackendEnvironment(`Storage${digest}:${bucket.FQN}`, env); |
| 107 | } |
| 108 | const required = (key: string) => |
| 109 | runtime.get<string | Redacted.Redacted<string>>(key).pipe( |
| 110 | Effect.flatMap((value) => |
| 111 | value === undefined |
| 112 | ? Effect.fail( |
| 113 | new StorageBindingError({ |
| 114 | message: `Missing storage binding variable ${key}`, |
| 115 | }), |
| 116 | ) |
| 117 | : Effect.succeed(Redacted.isRedacted(value) ? Redacted.value(value) : value), |
| 118 | ), |
| 119 | ); |
| 120 | const config = Effect.gen(function* () { |
| 121 | if ((yield* required(keys.injected)) === "yes") { |
| 122 | const env = Option.getOrUndefined(environment); |
| 123 | if ( |
| 124 | !env?.AWS_ACCESS_KEY_ID || |
| 125 | !env.AWS_SECRET_ACCESS_KEY || |
| 126 | !env.AWS_ENDPOINT_URL_S3 || |
| 127 | !env.AWS_REGION |
| 128 | ) { |
| 129 | return yield* new StorageBindingError({ |
| 130 | message: "Neon Function did not inject complete S3 credentials", |
| 131 | }); |
| 132 | } |
| 133 | return { |
| 134 | endpoint: env.AWS_ENDPOINT_URL_S3, |
| 135 | region: env.AWS_REGION, |
| 136 | accessKeyId: env.AWS_ACCESS_KEY_ID, |
| 137 | secretAccessKey: Redacted.make(env.AWS_SECRET_ACCESS_KEY), |
| 138 | } satisfies StorageConfig; |
| 139 | } |
| 140 | return { |
| 141 | endpoint: yield* required(keys.endpoint), |
| 142 | region: yield* required(keys.region), |
| 143 | accessKeyId: yield* required(keys.accessKeyId), |
| 144 | secretAccessKey: Redacted.make(yield* required(keys.secretAccessKey)), |
| 145 | } satisfies StorageConfig; |
| 146 | }); |
| 147 | const client = Effect.gen(function* () { |
| 148 | return yield* makeStorageClient(yield* config, yield* required(bucketKey)); |
| 149 | }).pipe(Effect.provideService(HttpClient.HttpClient, http)); |
| 150 | return { |
| 151 | get: (key: string) => client.pipe(Effect.flatMap((c) => c.get(key))), |
| 152 | head: (key: string) => client.pipe(Effect.flatMap((c) => c.head(key))), |
| 153 | list: (options?: Parameters<StorageClient["list"]>[0]) => |
| 154 | client.pipe(Effect.flatMap((c) => c.list(options))), |
| 155 | presign: (...args: Parameters<StorageClient["presign"]>) => |
| 156 | client.pipe(Effect.flatMap((c) => c.presign(...args))), |
| 157 | put: (...args: Parameters<StorageClient["put"]>) => |
| 158 | client.pipe(Effect.flatMap((c) => c.put(...args))), |
| 159 | delete: (key: string) => client.pipe(Effect.flatMap((c) => c.delete(key))), |
| 160 | deleteMany: (keys: string[]) => client.pipe(Effect.flatMap((c) => c.deleteMany(keys))), |
| 161 | createMultipartUpload: (...args: Parameters<StorageClient["createMultipartUpload"]>) => |
| 162 | client.pipe(Effect.flatMap((c) => c.createMultipartUpload(...args))), |
| 163 | uploadPart: (...args: Parameters<StorageClient["uploadPart"]>) => |
| 164 | client.pipe(Effect.flatMap((c) => c.uploadPart(...args))), |
| 165 | completeMultipartUpload: (...args: Parameters<StorageClient["completeMultipartUpload"]>) => |
| 166 | client.pipe(Effect.flatMap((c) => c.completeMultipartUpload(...args))), |
| 167 | abortMultipartUpload: (...args: Parameters<StorageClient["abortMultipartUpload"]>) => |
| 168 | client.pipe(Effect.flatMap((c) => c.abortMultipartUpload(...args))), |
| 169 | listMultipartUploads: (...args: Parameters<StorageClient["listMultipartUploads"]>) => |
| 170 | client.pipe(Effect.flatMap((c) => c.listMultipartUploads(...args))), |
| 171 | listParts: (...args: Parameters<StorageClient["listParts"]>) => |
| 172 | client.pipe(Effect.flatMap((c) => c.listParts(...args))), |
| 173 | }; |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | export const storageHttpLayer = FetchHttpClient.layer; |