sdk.ts
This file
Path
apps/sandbox/src/box/sdk.ts
117 lines · 3.3 KiB · TypeScript
Last changed by
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–6why, one step back1import { Files } from "@cloudflare/sandbox";2import { Duration, Effect, Layer } from "effect";34import { Box, BoxFailed } from "../box.ts";5import type { ExecOptions } from "../box.ts";6Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 7why, one step back7/** `instance` is set at start, not from `wrangler.jsonc`; absent means `lite`. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 8–64why, one step back8export interface ContainerStart {9 readonly image: string;10 readonly enableInternet: boolean;11 readonly instance?: ContainerStartupOptions["instance"];12}1314const text = new TextDecoder();1516const started = (container: Container, start: ContainerStart) =>17 Effect.sync(() => {18 if (!container.running) {19 container.start(20 start.instance === undefined21 ? { enableInternet: start.enableInternet, image: start.image }22 : {23 enableInternet: start.enableInternet,24 image: start.image,25 instance: start.instance,26 }27 );28 }2930 return container;31 });3233const execOptions = (options: ExecOptions, signal: AbortSignal) => {34 const exec: ContainerExecOptions = { signal };3536 if (options.cwd !== undefined) {37 exec.cwd = options.cwd;38 }3940 if (options.env !== undefined) {41 exec.env = { ...options.env };42 }4344 return exec;45};4647const failed =48 (operation: BoxFailed["operation"]) =>49 (cause: unknown): BoxFailed =>50 new BoxFailed({ operation, reason: String(cause) });5152const run = (53 container: Container,54 argv: readonly string[],55 options: ExecOptions56) =>57 Effect.gen(function* () {58 const process = yield* Effect.tryPromise({59 catch: failed("exec"),60 try: (signal) => container.exec([...argv], execOptions(options, signal)),61 });6263 // Effect aborts `exec`'s signal only while `exec` itself is pending;64 // once the process runs, an interrupt (a lane's time limit) kills it,Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 65why, one step back65 // or it would run on beside the lanes after it.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 66–117why, one step back66 const output = yield* Effect.tryPromise({67 catch: failed("exec"),68 try: () => process.output(),69 }).pipe(70 Effect.onInterrupt(() =>71 Effect.sync(() => {72 process.kill();73 })74 )75 );7677 return {78 exitCode: output.exitCode,79 stderr: text.decode(output.stderr),80 stdout: text.decode(output.stdout),81 };82 });8384export const SdkBox = {85 layer: (container: Container, start: ContainerStart): Layer.Layer<Box> =>86 Layer.succeed(87 Box,88 Box.of({89 destroy: () =>90 Effect.ignore(Effect.tryPromise(() => container.destroy())),91 exec: (argv, options = {}) =>92 Effect.flatMap(started(container, start), (running) => {93 const command = run(running, argv, options);9495 return options.timeoutMs === undefined96 ? command97 : Effect.timeoutOrElse(command, {98 duration: Duration.millis(options.timeoutMs),99 orElse: () =>100 Effect.fail(101 new BoxFailed({102 operation: "exec",103 reason: `timed out after ${options.timeoutMs} ms`,104 })105 ),106 });107 }),108 writeFile: (path, bytes) =>109 Effect.flatMap(started(container, start), (running) =>110 Effect.tryPromise({111 catch: failed("writeFile"),112 try: () => new Files(running).writeFile(path, bytes),113 })114 ),115 })116 ),117};