file.ts
This file
Path
packages/project/src/code/file.ts
151 lines · 3.9 KiB · TypeScript
Last changed by
Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
History of this fileTake it
Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 1why, one step back1import type { RepoHost, RepoNotFound } from "@repo/artifacts/repo-host";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 2–3why, one step back2import type { FileView, Rev } from "@repo/protocol/domain/code";3import { BlobSha } from "@repo/protocol/ids";Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 4why, one step back4import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 5–6why, one step back5import { UnknownPath } from "@repo/protocol/refusal";6import type { UnknownRevision } from "@repo/protocol/refusal";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 7why, one step back7import { Effect, Option } from "effect";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 8–9why, one step back89import type { CodeProject } from "../code.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 10–16why, one step back10import {11 contentsOf,12 contentsOfBytes,13 keepContents,14 unlessTooLarge,15} from "./blob.ts";16import type { Contents } from "./blob.ts";Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 17why, one step back17import { versionAt } from "./file-walk.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 18–26why, one step back18import { keptOnlyIfCanonical } from "./history.ts";19import {20 blobKept,21 fileKept,22 fileKey,23 keepObject,24 objectKey,25 throughStore,26} from "./objects.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 27why, one step back27import { commitAt, resolveAt } from "./revision.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 28why, one step back28import type { Resolved } from "./revision.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 29–30why, one step back29import { entryAt } from "./tree.ts";30Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 31–37why, one step back31export {32 contentsOf,33 contentsOfBytes,34 isBinary,35 TEXT_CAP,36 unlessTooLarge,37} from "./blob.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 38why, one step back38Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 39why, one step back39export type { Contents } from "./blob.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 40why, one step back40Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 41–58why, one step back41const viewOf = (42 commit: CommitSha,43 path: RepoPath,44 sha: BlobSha,45 contents: Contents46): FileView => ({47 at: commit,48 binary: contents.binary,49 path,50 sha,51 size: contents.size,52 truncated: contents.truncated,53 ...Option.match(contents.text, {54 onNone: () => ({}),55 onSome: (text) => ({ text }),56 }),57});58Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 59–74why, one step back59interface Whole {60 readonly blob: BlobSha;61 readonly contents: Contents;62}6364/**65 * The file in one host call, kept as the blob its path names at the commit66 * and that blob's contents, each by its id: a kept file is read from the67 * store alone. ASSUMPTION: Artifacts' `readFile` gives a symlink's own blob,68 * as the tree does, rather than following it.69 */70const wholeAt = (71 repo: RepoName,72 commit: CommitSha,73 path: RepoPath74): Effect.Effect<Option.Option<Whole>, RepoNotFound, RepoHost> =>Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 75why, one step back75 Effect.gen(function* () {Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 76why, one step back76 const key = yield* fileKey(repo, commit, path);Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 77why, one step back77Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 78–84why, one step back78 const named = yield* throughStore(fileKept, key, Effect.succeedNone);7980 if (Option.isSome(named)) {81 const kept = yield* contentsOf(repo, named.value.blob);8283 if (Option.isSome(kept)) {84 return Option.some({ blob: named.value.blob, contents: kept.value });Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 85why, one step back85 }Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 86–90why, one step back86 }8788 const read = yield* unlessTooLarge(89 versionAt(repo, commit, path),90 Option.noneRead a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 91why, one step back91 );Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 92–106why, one step back9293 if (Option.isNone(read)) {94 return Option.none();95 }9697 const { blob, bytes } = read.value;98 const contents = contentsOfBytes(bytes);99100 yield* keepObject(fileKept, key, { blob });101 yield* keepObject(blobKept, objectKey(blobKept, repo, blob), contents, {102 heavy: true,103 keep: keepContents,104 });105106 return Option.some({ blob, contents });Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 107–108why, one step back107 });108Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 109–110why, one step back109const fileAtCommit = (110 { commit, repo }: Resolved,Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 111why, one step back111 path: RepoPathMerge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 112why, one step back112): Effect.Effect<FileView, UnknownPath, RepoHost> =>Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 113why, one step back113 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 114why, one step back114 const whole = yield* wholeAt(repo, commit, path).pipe(Effect.orDie);Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 115–116why, one step back115116 if (Option.isSome(whole)) {Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 117why, one step back117 return viewOf(commit, path, whole.value.blob, whole.value.contents);Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 118–119why, one step back118 }119Merge redesign into mainoscabrielbefore adoptioncaedd21
line 120why, one step back120 const { tree } = yield* commitAt(repo, commit).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 121–124why, one step back121 Effect.flatMap(Effect.fromOption),122 Effect.orDie123 );124Merge redesign into mainoscabrielbefore adoptioncaedd21
line 125why, one step back125 const entry = yield* entryAt(repo, tree, path).pipe(Effect.orDie);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 126–132why, one step back126127 if (Option.isNone(entry) || entry.value.type !== "blob") {128 return yield* new UnknownPath({ commit, path });129 }130131 const sha = BlobSha.make(entry.value.sha);132Merge redesign into mainoscabrielbefore adoptioncaedd21
line 133why, one step back133 const contents = yield* contentsOf(repo, sha).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 134–138why, one step back134 Effect.orDie,135 Effect.flatMap(Effect.fromOption),136 Effect.orDie137 );138Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 139why, one step back139 return viewOf(commit, path, sha, contents);Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 140–150why, one step back140 });141142export const fileView = (143 project: CodeProject,144 at: Option.Option<Rev>,145 path: RepoPath146): Effect.Effect<FileView, UnknownRevision | UnknownPath, RepoHost> =>147 Effect.gen(function* () {148 const resolved = yield* resolveAt(project, at);149150 return yield* keptOnlyIfCanonical(resolved, fileAtCommit(resolved, path));Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 151why, one step back151 });