folder.ts
This file
Path
apps/web/src/server/folder.ts
93 lines · 3.0 KiB · TypeScript
Last changed by
Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
History of this fileTake it
Merge build/1005oscabrielbefore adoptionb124cbb
lines 1–9why, one step back1/**2 * A folder and its README, read on the server: the read both the project's3 * page (its root at main) and a folder's page (`/$project/tree/$rev/$`)4 * render from, so either arrives whole, README included, before the5 * browser runs anything. It lives here, not in `features/code/`, because6 * it calls the contracts and the views there import no transport.7 */8import { Client } from "@repo/protocol/contracts";9import type { FileView, Rev, TreeListing } from "@repo/protocol/domain/code";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 10–11why, one step back10import type { ProjectName } from "@repo/protocol/ids";11import { RepoPath } from "@repo/protocol/ids";Merge build/1005oscabrielbefore adoptionb124cbb
lines 12–20why, one step back12import type { Unauthenticated } from "@repo/protocol/principal";13import type {14 UnknownPath,15 UnknownProject,16 UnknownRevision,17} from "@repo/protocol/refusal";18import { Effect, Option } from "effect";19import type { RpcClientError } from "effect/rpc";20Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 21–23why, one step back21/** The name most folders give their README, read beside the folder rather than after it. */22const GUESS = "README.md";23Merge build/1005oscabrielbefore adoptionb124cbb
lines 24–29why, one step back24/**25 * The folder `path` (the root when none) of `project` at `rev`, and its26 * README; a README that cannot be read leaves the folder without it, while27 * a folder that cannot be read is the platform's refusal (or the call's28 * own failure: the platform did not answer, or turned the credential29 * away).Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 30–34why, one step back30 *31 * The folder and a `README.md` in it are read at once, saving a round trip32 * on most pages. The guess stands only when it is the README the folder33 * names, read at the folder's commit (`main` may move between the two34 * reads); otherwise the folder's own README is read at its commit.Merge build/1005oscabrielbefore adoptionb124cbb
lines 35–52why, one step back35 */36export const readFolderWithReadme = (input: {37 readonly path: Option.Option<RepoPath>;38 readonly project: ProjectName;39 readonly rev: Rev;40}): Effect.Effect<41 { readonly listing: TreeListing; readonly readme: Option.Option<FileView> },42 | UnknownProject43 | UnknownRevision44 | UnknownPath45 | RpcClientError.RpcClientError46 | Unauthenticated,47 Client48> =>49 Effect.gen(function* () {50 const { path, project, rev } = input;51 const client = yield* Client;52Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 53–58why, one step back53 const guessed = RepoPath.make(54 Option.match(path, {55 onNone: () => GUESS,56 onSome: (folder) => `${folder}/${GUESS}`,57 })58 );Merge build/1005oscabrielbefore adoptionb124cbb
line 59why, one step back59Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 60–82why, one step back60 const [listing, guess] = yield* Effect.all(61 [62 client.readTree({63 at: rev,64 project,65 ...Option.match(path, {66 onNone: () => ({}),67 onSome: (at) => ({ path: at }),68 }),69 }),70 client71 .readFile({ at: rev, path: guessed, project })72 .pipe(Effect.option),73 ],74 { concurrency: 2 }75 );7677 const named = listing.readme;7879 if (named === undefined) {80 return { listing, readme: Option.none() };81 }82Merge build/1005oscabrielbefore adoptionb124cbb
line 83why, one step back83 const readme =Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 84–87why, one step back84 Option.isSome(guess) &&85 guess.value.path === named.path &&86 guess.value.at === listing.at87 ? guessMerge build/1005oscabrielbefore adoptionb124cbb
line 88why, one step back88 : yield* clientFold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 89why, one step back89 .readFile({ at: listing.at, path: named.path, project })Merge build/1005oscabrielbefore adoptionb124cbb
lines 90–93why, one step back90 .pipe(Effect.option);9192 return { listing, readme };93 });