web/repo: a diff side the page didn't ship costs one read, not two, since the diff already knows its size
Signed-off-by: dawn <90008@klbr.net>
2 unmodified lines3456789101177 unmodified lines89909192939495969798992 unmodified linesimport { branches as knotMirrorBranches, blob as knotMirrorBlob, blobRawUrl as knotMirrorBlobRawUrl, blobText as knotMirrorBlobText, languages as knotMirrorLanguages, getBranch, getTag, tags as knotMirrorTags,77 unmodified lines
export const blob = (target: GitTarget, params: { ref: string; path: string }, maxBytes: number) => knotMirrorBlob(target.ctx, { repo: target.repo, ...params }, maxBytes);
export const blobText = (target: GitTarget, params: { ref: string; path: string }) => knotMirrorBlobText(target.ctx, { repo: target.repo, ...params });
export const blobRawUrl = ( target: GitTarget, params: { ref: string; path: string; download?: boolean }): string => knotMirrorBlobRawUrl(target.ctx.serviceUrl, { repo: target.repo, ...params });170 unmodified linesExpand all171172173174175176177178179180181182183184185186187188189338 unmodified linesExpand all170 unmodified linesExpand all lastCommit };};
// a diff already knows each side's size and kind, so its text needs only the bytes:// null for a side that turns out binaryexport const blobText = async ( ctx: BobbinContext, params: { repo: string; ref: string; path: string }, init?: XrpcRequestInit): Promise<string | null> => { const bytes = new Uint8Array(await (await rawGet(ctx, BLOB_NSID, params, init)).arrayBuffer()); return looksBinary(bytes) ? null : new TextDecoder().decode(bytes);};
const isReadmeFile = (name: string, mode: string): boolean => /^readme(?:\.[^.]+)?$/i.test(name) && treeEntryKind(mode) === "file";
const pathInTree = (path: string | undefined, name: string): string =>338 unmodified linesExpand all217 unmodified linesExpand all218219220221222223224225226227229 unmodified linesExpand all217 unmodified linesExpand all }); // two for the changed file, one for the added file's right side: the absent // side is never fetched, it would 404 expect(blobCalls(calls)).toHaveLength(3); // the diff carries each side's size, so no side asks for its entry first expect(calls.filter((path) => path.endsWith("getEntry"))).toEqual([]); });
it("diffs a root commit against the empty tree and reads only the new side", async () => { const head = "a".repeat(40);229 unmodified linesExpand all1223456185 unmodified linesExpand all19219319419519619719819920020120220320419619719819920020120220320420552 unmodified linesimport { MAX_INLINE_SIZE } from "#lib/api/blob.ts";import { blob, knotMirrorTarget, type GitServiceConfig } from "#lib/api/gitclient.ts";import { blobText, knotMirrorTarget, type GitServiceConfig } from "#lib/api/gitclient.ts";import { diffFileKind, diffFileStat, diffSideExists,185 unmodified linesExpand all // a side that doesn't exist at this ref is an empty side, not a failed fetch if (!diffSideExists(src)) return ""; if (src.content !== undefined) return src.content; // a rate-limited or briefly failing mirror is worth retrying; a blob that is // really not there is not, so only the transient shape escapes const output = await blob(git, { ref, path: src.path }, MAX_INLINE_SIZE).catch( (cause: unknown) => { if (isTransient(cause)) throw cause; return null; } ); if (!output || output.encoding !== "utf-8") return null; return output.content ?? ""; // really not there is not, so only the transient shape escapes. the diff already // checked the size, so no getEntry goes first return blobText(git, { ref, path: src.path }).catch((cause: unknown) => { if (isTransient(cause)) throw cause; return null; });};
// a server render ships the text of the first files with the page, so the browser neither// fetches their sides nor waits for them to paint; past the cap it fetches the rest as it52 unmodified lines