binding.ts
This file
Path
packages/project/src/code-store/binding.ts
57 lines · 1.7 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 1–5why, one step back1import { Data, Effect, Layer, Option } from "effect";23import { CodeStore, fittingKey } from "../code-store.ts";4import type { CodeStoreApi } from "../code-store.ts";5Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 6why, one step back6/** Structural, so the package needs no Workers types. */Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 7–57why, one step back7export interface BucketLike {8 readonly get: (9 key: string10 ) => Promise<{ readonly text: () => Promise<string> } | null>;11 readonly put: (12 key: string,13 value: string14 ) => Promise<{ readonly key: string } | null>;15}1617class CodeStoreUnreachable extends Data.TaggedError("CodeStoreUnreachable")<{18 readonly cause: unknown;19}> {}2021const reach = <A>(run: () => Promise<A>) =>22 Effect.tryPromise({23 catch: (cause) => new CodeStoreUnreachable({ cause }),24 try: run,25 });2627export const CodeStoreR2 = (bucket: BucketLike) => {28 const store: CodeStoreApi = {29 get: (key) =>30 Effect.flatMap(fittingKey(key), (fit) =>31 reach(() => bucket.get(fit)).pipe(32 Effect.flatMap((found) =>33 found === null34 ? Effect.succeedNone35 : Effect.asSome(reach(() => found.text()))36 ),37 Effect.catchTag("CodeStoreUnreachable", ({ cause }) =>38 Effect.as(39 Effect.logWarning(`the code store could not read ${fit}`, cause),40 Option.none<string>()41 )42 )43 )44 ),45 put: (key, value) =>46 Effect.flatMap(fittingKey(key), (fit) =>47 reach(() => bucket.put(fit, value)).pipe(48 Effect.asVoid,49 Effect.catchTag("CodeStoreUnreachable", ({ cause }) =>50 Effect.logWarning(`the code store could not keep ${fit}`, cause)51 )52 )53 ),54 };5556 return Layer.succeed(CodeStore, store);57};