Skip to content
Myr

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 file

Take it

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 1why, one step back
1import type { RepoHost, RepoNotFound } from "@repo/artifacts/repo-host";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 2–3why, one step back
2import 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 back
4import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 5–6why, one step back
5import { UnknownPath } from "@repo/protocol/refusal";
6import type { UnknownRevision } from "@repo/protocol/refusal";

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 7why, one step back
7import { Effect, Option } from "effect";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 8–9why, one step back
8
9import type { CodeProject } from "../code.ts";

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 10–16why, one step back
10import {
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 back
17import { versionAt } from "./file-walk.ts";

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 18–26why, one step back
18import { 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 back
27import { commitAt, resolveAt } from "./revision.ts";

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 28why, one step back
28import type { Resolved } from "./revision.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 29–30why, one step back
29import { entryAt } from "./tree.ts";
30

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 31–37why, one step back
31export {
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 back
38

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 39why, one step back
39export type { Contents } from "./blob.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 40why, one step back
40

Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730

lines 41–58why, one step back
41const 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});58

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 59–74why, one step back
59interface 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 back
75 Effect.gen(function* () {

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 76why, one step back
76 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 back
77

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 78–84why, one step back
78 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 back
85 }

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 86–90why, one step back
86 }
8788 const read = yield* unlessTooLarge(89 versionAt(repo, commit, path),90 Option.none

Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730

line 91why, one step back
91 );

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 92–106why, one step back
92
93 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 back
107 });
108

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 109–110why, one step back
109const fileAtCommit = (
110 { commit, repo }: Resolved,

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 111why, one step back
111 path: RepoPath

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 112why, one step back
112): Effect.Effect<FileView, UnknownPath, RepoHost> =>

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 113why, one step back
113 Effect.gen(function* () {

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 114why, one step back
114 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 back
115
116 if (Option.isSome(whole)) {

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 117why, one step back
117 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 back
118 }
119

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 120why, one step back
120 const { tree } = yield* commitAt(repo, commit).pipe(

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 121–124why, one step back
121 Effect.flatMap(Effect.fromOption),
122 Effect.orDie123 );124

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 125why, one step back
125 const entry = yield* entryAt(repo, tree, path).pipe(Effect.orDie);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 126–132why, one step back
126
127 if (Option.isNone(entry) || entry.value.type !== "blob") {128 return yield* new UnknownPath({ commit, path });129 }130131 const sha = BlobSha.make(entry.value.sha);132

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 133why, one step back
133 const contents = yield* contentsOf(repo, sha).pipe(

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 134–138why, one step back
134 Effect.orDie,
135 Effect.flatMap(Effect.fromOption),136 Effect.orDie137 );138

Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730

line 139why, one step back
139 return viewOf(commit, path, sha, contents);

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 140–150why, one step back
140 });
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 back
151 });