binding-live.test.ts
This file
Path
packages/artifacts/test/binding-live.test.ts
172 lines · 5.4 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
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
line 1why, one step back1/**Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
lines 2–4why, one step back2 * Opt-in, never in `verify`: runs only with `LIVE_ARTIFACTS=1` and3 * `LIVE_ARTIFACTS_HARNESS` naming, by absolute path, a harness module outside4 * this repo. The harness exports `live()`, which resolves toMerge 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 5–56why, one step back5 *6 * ```ts7 * {8 * namespace: string; // a namespace this build made9 * binding: Artifacts; // `getPlatformProxy({ remoteBindings: true })`'s `env.ARTIFACTS`10 * fetch: (request: Request) => Promise<Response>;11 * dispose: () => Promise<void>; // the proxy's12 * }13 * ```14 */15import { describe } from "@effect/vitest";16import { Data, Effect, FiberSet, Layer, Predicate, Schema } from "effect";1718import { Fetch, FetchFailed } from "../src/fetch.ts";19import { RepoHostBinding } from "../src/repo-host/binding.ts";20import { describeRepoHost } from "./repo-host-contract.ts";2122const FORBIDDEN = "witness";2324const harnessPath = process.env.LIVE_ARTIFACTS_HARNESS;2526const live = process.env.LIVE_ARTIFACTS === "1" && harnessPath !== undefined;2728// The harness's functions, taken on trust as its doc comment types them:29// a function's signature cannot be checked at run time.3031const Start = Schema.declare((input): input is () => Promise<object> =>32 Predicate.isFunction(input)33);3435const Dispose = Schema.declare((input): input is () => Promise<void> =>36 Predicate.isFunction(input)37);3839const Send = Schema.declare(40 (input): input is (request: Request) => Promise<Response> =>41 Predicate.isFunction(input)42);4344const HarnessModule = Schema.Struct({ live: Start });4546const Harness = Schema.Struct({47 binding: Schema.declare(48 (input): input is Artifacts =>49 Predicate.hasProperty(input, "get") && Predicate.isFunction(input.get)50 ),51 dispose: Dispose,52 fetch: Send,53 namespace: Schema.NonEmptyString.pipe(54 Schema.check(55 Schema.makeFilter((namespace) =>56 namespace === FORBIDDENDrop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 57why, one step back57 ? `${FORBIDDEN} belongs to another project`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 58–90why, one step back58 : undefined59 )60 )61 ),62});6364type Harness = typeof Harness.Type;6566class HarnessFailed extends Data.TaggedError("HarnessFailed")<{67 readonly reason: string;68}> {}6970const harness = (path: string) =>71 Effect.acquireRelease(72 Effect.gen(function* () {73 const module = yield* Effect.tryPromise({74 catch: () => new HarnessFailed({ reason: `cannot import ${path}` }),75 try: () => import(path),76 }).pipe(Effect.flatMap(Schema.decodeUnknownEffect(HarnessModule)));7778 return yield* Effect.tryPromise({79 catch: () => new HarnessFailed({ reason: "live() rejected" }),80 try: () => module.live(),81 }).pipe(Effect.flatMap(Schema.decodeUnknownEffect(Harness)));82 }).pipe(Effect.orDie),83 ({ dispose }) => Effect.tryPromise(() => dispose()).pipe(Effect.ignore)84 );8586const freshPrefix = () =>87 `live-${crypto88 .getRandomValues(new Uint32Array(2))89 .reduce((text, word) => text + word.toString(36), "")}-`;90Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 91why, one step back91/** Each method is forwarded by name: RPC stubs' methods are not copied by a spread. */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 92–158why, one step back92const prefixed = (binding: Artifacts) =>93 Effect.gen(function* () {94 const prefix = freshPrefix();95 const run = yield* FiberSet.makeRuntimePromise();96 const made = new Set<string>();9798 const named = (name: string) => {99 made.add(`${prefix}${name}`);100101 return `${prefix}${name}`;102 };103104 yield* Effect.addFinalizer(() =>105 Effect.forEach(made, (name) =>106 Effect.tryPromise(() => binding.delete(name)).pipe(Effect.ignore)107 )108 );109110 const handle = (repo: ArtifactsRepo): ArtifactsRepo => ({111 createToken: (scope, ttl) => repo.createToken(scope, ttl),112 fork: (target, options) => repo.fork(named(target), options),113 info: () => repo.info(),114 listTokens: () => repo.listTokens(),115 log: (options) => repo.log(options),116 readBlob: (hash) => repo.readBlob(hash),117 readCommit: (hash) => repo.readCommit(hash),118 readFile: (args) => repo.readFile(args),119 readTree: (hash) => repo.readTree(hash),120 revokeToken: (tokenOrId) => repo.revokeToken(tokenOrId),121 [Symbol.dispose]: () => {122 repo[Symbol.dispose]();123 },124 });125126 return {127 create: (name, options) => binding.create(named(name), options),128 delete: (name) => binding.delete(`${prefix}${name}`),129 get: (name) =>130 run(131 Effect.promise(() => binding.get(`${prefix}${name}`)).pipe(132 Effect.map(handle)133 )134 ),135 import: ({ source, target }) =>136 binding.import({137 source,138 target: { ...target, name: named(target.name) },139 }),140 list: (options) => binding.list(options),141 } satisfies Artifacts;142 });143144const fetchOver = (send: Harness["fetch"]) =>145 Fetch.of({146 fetch: (request) =>147 Effect.tryPromise({148 catch: (cause) =>149 new FetchFailed({150 reason: cause instanceof Error ? cause.message : "no response",151 url: request.url,152 }),153 try: () => send(request),154 }),155 });156157describe.runIf(live)("against live Artifacts", () => {158 // Artifacts imports only public HTTPS remotes, and its own refuse anonymousDrop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 159why, one step back159 // reads, so the import cases do not run here.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 160–172why, one step back160 describeRepoHost(161 "binding (live)",162 Layer.unwrap(163 Effect.gen(function* () {164 const { binding, fetch } = yield* harness(harnessPath ?? "");165166 return RepoHostBinding.layer(yield* prefixed(binding)).pipe(167 Layer.provide(Layer.succeed(Fetch, fetchOver(fetch)))168 );169 })170 )171 );172});