| 1 | import * as sprites from "@distilled.cloud/fly-io/sprites"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import { CredentialsFromAmbientOrEnv } from "./Credentials.ts"; |
| 6 | import { Exec, type ExecRequest } from "./Exec.ts"; |
| 7 | import { makeHttpSpriteBinding } from "./SpriteHttp.ts"; |
| 8 | |
| 9 | /** |
| 10 | * HTTP implementation of {@link Exec}. Provide it on the |
| 11 | * {@link Sprite}, {@link Service}, or Action Effect. |
| 12 | * |
| 13 | * |
| 14 | * ### Provide the layer |
| 15 | * **Example:** On a Sprite |
| 16 | * ```typescript |
| 17 | * Effect.gen(function* () { |
| 18 | * const exec = yield* Fly.Exec(Box); |
| 19 | * // ... |
| 20 | * }).pipe(Effect.provide(Fly.ExecHttp)) |
| 21 | * ``` |
| 22 | * |
| 23 | * @layer |
| 24 | * @product Sprite |
| 25 | * @provides Fly.Exec |
| 26 | */ |
| 27 | export const ExecHttp = Layer.effect( |
| 28 | Exec, |
| 29 | Effect.suspend(() => |
| 30 | makeHttpSpriteBinding({ |
| 31 | makeClient: (auth, spriteName) => |
| 32 | Effect.fn("Fly.Exec")(function* (request: ExecRequest) { |
| 33 | return yield* auth.authorize( |
| 34 | sprites.execCommand({ |
| 35 | name: yield* spriteName, |
| 36 | cmd: request.cmd, |
| 37 | path: request.path, |
| 38 | stdin: request.stdin, |
| 39 | env: request.env, |
| 40 | dir: request.dir, |
| 41 | body: request.body, |
| 42 | }), |
| 43 | ); |
| 44 | }), |
| 45 | }), |
| 46 | ), |
| 47 | ).pipe( |
| 48 | Layer.provide(FetchHttpClient.layer), |
| 49 | Layer.provide(CredentialsFromAmbientOrEnv), |
| 50 | ); |