sailfork / t3code-github / .repos/ alchemy-effect/ examples/ cloudflare-worker/ src/ Sandbox.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import { Stack } from "alchemy/Stack";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import type { PlatformError } from "effect/PlatformError";
import * as ChildProcess from "effect/process/ChildProcess";
import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
import * as Stream from "effect/Stream";

export class Sandbox extends Cloudflare.Container<
  Sandbox,
  {
    /**
     * Execute a command in a sandbox.
     */
    shell: (command: string) => Effect.Effect<
      {
        exitCode: number;
        stdout: string;
        stderr: string;
      },
      PlatformError
    >;
  }
>()("Sandbox") {}

export const SandboxLive = /* @__PURE__ */ Sandbox.make(
  Stack.useSync((stack) => ({
    main: import.meta.url,
    instanceType: stack.stage === "prod" ? "standard-1" : "dev",
    observability: {
      logs: {
        enabled: true,
      },
    },
  })),
  Effect.gen(function* () {
    //
    const cp = yield* ChildProcessSpawner;

    let counter = 0;

    return Sandbox.of({
      shell: (command) =>
        cp
          .spawn(
            ChildProcess.make(command, {
              shell: true,
            }),
          )
          .pipe(
            Effect.flatMap((handle) =>
              Effect.all(
                [
                  handle.exitCode,
                  handle.stdout.pipe(Stream.decodeText, Stream.mkString),
                  handle.stderr.pipe(Stream.decodeText, Stream.mkString),
                ],
                { concurrency: "unbounded" },
              ),
            ),
            Effect.map(([exitCode, stdout, stderr]) => ({
              exitCode,
              stdout,
              stderr,
            })),
            Effect.scoped,
          ),
      fetch: Effect.gen(function* () {
        const request = yield* HttpServerRequest;
        const url = new URL(request.url, "http://localhost");

        if (url.pathname === "/increment") {
          counter++;
          return yield* HttpServerResponse.json({ counter });
        }

        return HttpServerResponse.text("Hello from Sandbox container!");
      }),
    });
  }),
);

export default SandboxLive;