file.ts
This file
Path
packages/project/src/code/file.ts
173 lines · 4.7 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 1–4why, one step back1import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";2import type { RepoNotFound } from "@repo/artifacts/repo-host";3import type { FileView, Rev } from "@repo/protocol/domain/code";4import { BlobSha } from "@repo/protocol/ids";Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 5why, one step back5import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 6–11why, one step back6import { UnknownPath } from "@repo/protocol/refusal";7import type { UnknownRevision } from "@repo/protocol/refusal";8import { Effect, Option, Schema } from "effect";910import type { CodeProject } from "../code.ts";11import { CodeCache } from "./cache.ts";Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 12why, one step back12import { versionAt } from "./file-walk.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 13why, one step back13import { commitAt, resolveAt } from "./revision.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 14–36why, one step back14import { entryAt } from "./tree.ts";1516export const TEXT_CAP = 1024 * 1024;1718// git's own test: a NUL among the first bytes means the blob is not text.19const SNIFF = 8192;2021const KEPT_TEXT = 256 * 1024;2223export const isBinary = (bytes: Uint8Array) =>24 bytes.subarray(0, SNIFF).includes(0);2526export interface Contents {27 readonly size: number;28 readonly binary: boolean;29 readonly truncated: boolean;30 readonly text: Option.Option<string>;31}3233const MemoryLimit = Schema.Struct({ code: Schema.Literal("MEMORY_LIMIT") });3435const isMemoryLimit = Schema.is(MemoryLimit);36Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 37why, one step back37/** Artifacts' binding throws a too-large blob as a defect, which here is an answer. */Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 38–96why, one step back38export const unlessTooLarge = <A, E, R>(39 read: Effect.Effect<A, E, R>,40 tooLarge: () => A41): Effect.Effect<A, E, R> =>42 Effect.catchDefect(read, (defect) =>43 isMemoryLimit(defect) ? Effect.succeed(tooLarge()) : Effect.die(defect)44 );4546export const contentsOfBytes = (bytes: Uint8Array): Contents => {47 const binary = isBinary(bytes);48 const truncated = !binary && bytes.length > TEXT_CAP;4950 return {51 binary,52 size: bytes.length,53 text:54 binary || truncated55 ? Option.none()56 : Option.some(new TextDecoder().decode(bytes)),57 truncated,58 };59};6061export const contentsOf = (62 repo: RepoName,63 blob: BlobSha64): Effect.Effect<Option.Option<Contents>, RepoNotFound, RepoHost> =>65 Effect.gen(function* () {66 const cache = yield* CodeCache;6768 return yield* cache.through(69 `${repo}:text:${blob}`,70 unlessTooLarge(71 RepoHost.use((host) => host.readBlob(repo, ObjectId.make(blob))).pipe(72 Effect.map(Option.map(contentsOfBytes))73 ),74 () =>75 Option.some({76 binary: false,77 size: 0,78 text: Option.none(),79 truncated: true,80 })81 ),82 {83 heavy: true,84 keep: (contents) =>85 Option.match(contents, {86 onNone: () => false,87 onSome: ({ text }) =>88 Option.match(text, {89 onNone: () => true,90 onSome: (read) => read.length <= KEPT_TEXT,91 }),92 }),93 }94 );95 });96Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 97–114why, one step back97const viewOf = (98 commit: CommitSha,99 path: RepoPath,100 sha: BlobSha,101 contents: Contents102): FileView => ({103 at: commit,104 binary: contents.binary,105 path,106 sha,107 size: contents.size,108 truncated: contents.truncated,109 ...Option.match(contents.text, {110 onNone: () => ({}),111 onSome: (text) => ({ text }),112 }),113});114Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 115why, one step back115/** ASSUMPTION: Artifacts' `readFile` gives a symlink's own blob, as the tree does, rather than following it. */Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
lines 116–133why, one step back116const wholeAt = (repo: RepoName, commit: CommitSha, path: RepoPath) =>117 Effect.gen(function* () {118 const cache = yield* CodeCache;119120 return yield* cache.through(121 `${repo}:file:${commit}:${path}`,122 unlessTooLarge(versionAt(repo, commit, path), Option.none),123 {124 heavy: true,125 keep: (read) =>126 Option.match(read, {127 onNone: () => false,128 onSome: ({ bytes }) => bytes.byteLength <= KEPT_TEXT,129 }),130 }131 );132 });133Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 134–139why, one step back134export const fileView = (135 project: CodeProject,136 at: Option.Option<Rev>,137 path: RepoPath138): Effect.Effect<FileView, UnknownRevision | UnknownPath, RepoHost> =>139 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 140why, one step back140 const { commit, repo } = yield* resolveAt(project, at);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 141why, one step back141Merge redesign into mainoscabrielbefore adoptioncaedd21
line 142why, one step back142 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 143–152why, one step back143144 if (Option.isSome(whole)) {145 return viewOf(146 commit,147 path,148 whole.value.blob,149 contentsOfBytes(whole.value.bytes)150 );151 }152Merge redesign into mainoscabrielbefore adoptioncaedd21
line 153why, one step back153 const { tree } = yield* commitAt(repo, commit).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 154–157why, one step back154 Effect.flatMap(Effect.fromOption),155 Effect.orDie156 );157Merge redesign into mainoscabrielbefore adoptioncaedd21
line 158why, one step back158 const entry = yield* entryAt(repo, tree, path).pipe(Effect.orDie);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 159–165why, one step back159160 if (Option.isNone(entry) || entry.value.type !== "blob") {161 return yield* new UnknownPath({ commit, path });162 }163164 const sha = BlobSha.make(entry.value.sha);165Merge redesign into mainoscabrielbefore adoptioncaedd21
line 166why, one step back166 const contents = yield* contentsOf(repo, sha).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 167–171why, one step back167 Effect.orDie,168 Effect.flatMap(Effect.fromOption),169 Effect.orDie170 );171Read a file's view in one call when a file is thereoscabrielbefore adoptione90a730
line 172why, one step back172 return viewOf(commit, path, sha, contents);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 173why, one step back173 });