Versions 148 to 318
new file, 249 lines
1+import assert from "node:assert/strict"; 2+import { randomBytes } from "node:crypto"; 3+import { test } from "node:test"; 4+import git, { type HttpClient } from "isomorphic-git"; 5+import { GitDiskIntake } from "../src/git-disk-intake.ts"; 6+import { GitFileContents } from "../src/git-file-contents.ts"; 7+import { GitIntakeTrees } from "../src/git-intake-trees.ts"; 8+import { pushedSince } from "../src/ingest.ts"; 9+import { MemoryFS } from "../src/memory-fs.ts"; 10+import { commitShared } from "../src/push.ts"; 11+import { memoryStorage } from "./helpers.ts"; 13+const packet = (text: string | Uint8Array) => { 14+ const body = Buffer.from(text); 15+ return Buffer.concat([Buffer.from((body.length + 4).toString(16).padStart(4, "0")), body]); 18+for (const mode of ["success", "limit", "fatal", "server-intake", "text-intake", "binary-limit"]) 19+ test(`actual disk clone handles ${mode}`, async (t) => { 20+ const rejectPack = mode === "limit"; 21+ const source = new MemoryFS(), 23+ await git.init({ fs: source, dir, defaultBranch: "main" }); 24+ const blob = mode === "text-intake" ? Buffer.from("x".repeat(16383) + "🚀é中".repeat(20000)) : randomBytes(400000); 25+ if (mode === "binary-limit") blob[0] = 255; 26+ const blobOid = await git.writeBlob({ fs: source, dir, blob }); 27+ const tree = await git.writeTree({ 30+ tree: [{ mode: "100644", path: "large.bin", oid: blobOid, type: "blob" }], 33+ name: "Capacity fixture", 34+ email: "fixture@example.invalid", 38+ const base = await git.writeCommit({ 46+ message: "Base\n\nabso-version: 0\n", 49+ const head = await git.writeCommit({ 57+ message: "Fixture\n", 61+ await git.packObjects({ 64+ oids: [head, base, tree, blobOid], 68+ const advertisement = Buffer.concat([ 69+ packet("# service=git-upload-pack\n"), 71+ packet(`${head} HEAD\0symref=HEAD:refs/heads/main side-band-64k shallow\n`), 72+ packet(`${head} refs/heads/main\n`), 76+ let uploadChunksRead = 0; 77+ let uploadClosed = false; 78+ const uploadParts = [packet("NAK\n")]; 79+ for (let at = 0; at < packed.length; at += 16000) { 80+ uploadParts.push(packet(Buffer.concat([Buffer.from([1]), packed.subarray(at, at + 16000)]))); 82+ packet(Buffer.concat([Buffer.from([2]), Buffer.from("Counting objects: 50% (1/2)\n".repeat(200))])), 84+ if (mode === "fatal" && at === 16000) { 85+ uploadParts.push(packet(Buffer.concat([Buffer.from([3]), Buffer.from("fixture remote failure")]))); 89+ uploadParts.push(Buffer.from("0000")); 90+ const http: HttpClient = { 91+ request: async ({ url, method }) => { 92+ const body = method === "POST" ? Buffer.concat(uploadParts) : advertisement; 93+ if (method === "POST") uploads++; 96+ method: method ?? "GET", 100+ body: (async function* () { 102+ for (let at = 0; at < body.length; at += 997) { 103+ if (method === "POST") uploadChunksRead++; 104+ yield body.subarray(at, at + 997); 107+ if (method === "POST") uploadClosed = true; 113+ const storage = memoryStorage(); 114+ const intake = new GitDiskIntake(storage, rejectPack ? 70000 : 2000000, 1000000); 115+ const ingest = intake.ingest.bind(intake); 116+ intake.ingest = async (...args) => { 117+ await new Promise((resolve) => setTimeout(resolve, 25)); 118+ assert.ok(uploadChunksRead < 64, `pack source read ahead ${uploadChunksRead} chunks before disk consumption`); 119+ return ingest(...args); 122+ intake.attach(cache); 123+ const contents = new GitFileContents(storage, 1000000, 2000000); 124+ const fs = new MemoryFS(contents); 125+ const write = fs.promises.writeFile; 126+ fs.promises.writeFile = async (path, ...args) => { 127+ assert.ok(!/\.(pack|idx)$/.test(path), "clone must not persist a collected pack or index"); 128+ return write(path, ...args); 131+ if (mode === "server-intake" || mode === "text-intake" || mode === "binary-limit") { 132+ const trees = new GitIntakeTrees(storage); 133+ if (mode === "text-intake" || mode === "binary-limit") { 134+ const original = GitDiskIntake.prototype.read; 135+ t.mock.method(GitDiskIntake.prototype, "read", function (this: GitDiskIntake, oid: string) { 136+ if (oid === blobOid) throw Error("whole blob read forbidden"); 137+ return original.call(this, oid); 142+ info: async () => ({ remote: "https://fixture.invalid/repo" }), 143+ createToken: async () => ({ plaintext: "invented-test-token" }), 144+ [Symbol.dispose]() {}, 147+ const found = await pushedSince( 152+ assert.notEqual(mode, "binary-limit", "oversized binary must not reach the store"); 153+ assert.deepEqual(Buffer.from(bytes), blob); 154+ return "fixture-blob"; 156+ mode === "binary-limit" ? () => "previous-blob" : undefined, 160+ maxPackBytes: 2000000, 161+ maxObjectBytes: 1000000, 162+ maxBinaryBytes: mode === "binary-limit" ? 100000 : undefined, 164+ retainFileChunks: (files, path, chunks, oid) => trees.retainChunks(files, path, chunks, oid), 167+ assert.equal(found!.base, 0); 168+ assert.equal(found!.commits.length, 1); 169+ if (mode === "text-intake") assert.equal(found!.commits[0]!.files["large.bin"], blob.toString("utf8")); 170+ else if (mode === "binary-limit") { 171+ assert.equal(found!.commits[0]!.blobs["large.bin"], "previous-blob"); 172+ assert.deepEqual(found!.tooBig, ["large.bin"]); 173+ } else assert.equal(found!.commits[0]!.blobs["large.bin"], "fixture-blob"); 175+ for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"]) 176+ assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0); 179+ const clone = git.clone({ 184+ url: "https://fixture.invalid/repo", 189+ if (mode !== "success") { 190+ await assert.rejects(clone, rejectPack ? /pack exceeds byte limit/ : /fixture remote failure/); 191+ assert.equal(uploadClosed, true); 192+ for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"]) 193+ assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0); 197+ intake.assertReady(); 198+ assert.equal((await git.log({ fs, dir, cache, ref: "main" })).length, 2); 204+ trees: [git.TREE({ ref: head })], 205+ map: async (path, [entry]) => { 206+ if (path === "large.bin") { 207+ assert.deepEqual(Buffer.from((await entry!.content())!), blob); 213+ assert.equal(walked, 1); 214+ assert.equal(uploads, 1); 215+ assert.equal(await git.resolveRef({ fs, dir, ref: "main" }), head); 216+ assert.equal((await git.readCommit({ fs, dir, cache, oid: head })).commit.tree, tree); 217+ assert.deepEqual(Buffer.from((await git.readBlob({ fs, dir, cache, oid: blobOid })).blob), blob); 218+ const exported = await commitShared( 224+ files: { "new.txt": "Disk export fixture" }, 225+ blobs: { "large.bin": "fixture-blob" }, 231+ const next = await git.readCommit({ fs, dir, cache, oid: exported.commit }); 232+ assert.deepEqual(next.commit.parent, [head]); 234+ Buffer.from((await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "large.bin" })).blob), 239+ (await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "new.txt" })).blob, 241+ "Disk export fixture", 243+ for (const entry of fs.entries.values()) if (entry.kind === "file") assert.equal(entry.data.byteLength, 0);