@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 52 lines 1.76 KB
Raw
1 import * as machines from "@distilled.cloud/fly-io/machines";
2 import * as Effect from "effect/Effect";
3 import * as FetchHttpClient from "effect/http/FetchHttpClient";
4 import * as Layer from "effect/Layer";
5 import * as Redacted from "effect/Redacted";
6 import { CredentialsFromAmbientOrEnv } from "./Credentials.ts";
7 import { Decrypt, type DecryptRequest } from "./Decrypt.ts";
8 import { unwrapSecretValue } from "./SecretHttp.ts";
9 import { base64ToBytes, bytesToBase64, makeHttpSecretKeyBinding } from "./SecretKeyHttp.ts";
10
11 /**
12 * HTTP implementation of {@link Decrypt}. Provide it on the
13 * {@link Service} or Action Effect.
14 *
15 *
16 * ### Provide the layer
17 * **Example:** On a Service
18 * ```typescript
19 * Effect.gen(function* () {
20 * const decrypt = yield* Fly.Decrypt(Box);
21 * // ...
22 * }).pipe(Effect.provide(Fly.DecryptHttp))
23 * ```
24 *
25 * @layer
26 * @product Secret Key
27 * @provides Fly.Decrypt
28 */
29 export const DecryptHttp = Layer.effect(
30 Decrypt,
31 Effect.suspend(() =>
32 makeHttpSecretKeyBinding({
33 makeClient: (auth, appName, secretName) =>
34 Effect.fn("Fly.Decrypt")(function* (request: DecryptRequest) {
35 const res = yield* auth.authorize(
36 machines.decryptSecretKey({
37 app_name: yield* appName,
38 secret_name: yield* secretName,
39 ciphertext: bytesToBase64(request.ciphertext),
40 associated_data:
41 request.associatedData === undefined
42 ? undefined
43 : bytesToBase64(request.associatedData),
44 }),
45 );
46 return {
47 plaintext: Redacted.make(base64ToBytes(unwrapSecretValue(res.plaintext ?? ""))),
48 };
49 }),
50 }),
51 ),
52 ).pipe(Layer.provide(FetchHttpClient.layer), Layer.provide(CredentialsFromAmbientOrEnv));