{"draft":null,"from":175,"to":318,"files":[{"path":"server/test/git-delta-stream.test.ts","added":64,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/git-delta-stream.test.ts\n@@ -0,0 +1,64 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { applyGitDeltaChunks, type GitByteRanges } from \"../src/git-delta-stream.ts\";\n+\n+const ranges = (bytes: Uint8Array): GitByteRanges => ({\n+\tbyteLength: bytes.length,\n+\tread(start, length) {\n+\t\tassert.ok(length <= 65536);\n+\t\treturn bytes.slice(start, start + length);\n+\t},\n+});\n+const integer = (value: number) => {\n+\tconst bytes = [];\n+\tdo {\n+\t\tconst next = value % 128;\n+\t\tvalue = Math.floor(value / 128);\n+\t\tbytes.push(next | (value ? 128 : 0));\n+\t} while (value);\n+\treturn bytes;\n+};\n+test(\"delta copy and literal instructions use bounded base reads and output\", () => {\n+\tconst base = Uint8Array.from({ length: 200000 }, (_, i) => i % 251);\n+\t// Copy 131072 bytes from offset zero, then append three literal bytes.\n+\tconst delta = Uint8Array.from([...integer(base.length), ...integer(131075), 0xc0, 2, 3, 4, 5, 6]);\n+\tconst output: Uint8Array[] = [];\n+\tassert.equal(\n+\t\tapplyGitDeltaChunks(\n+\t\t\tranges(delta),\n+\t\t\tranges(base),\n+\t\t\t(chunk) => {\n+\t\t\t\tassert.ok(chunk.length <= 65536);\n+\t\t\t\toutput.push(chunk);\n+\t\t\t},\n+\t\t\t200000,\n+\t\t),\n+\t\t131075,\n+\t);\n+\tassert.deepEqual(Buffer.concat(output), Buffer.concat([base.subarray(0, 131072), new Uint8Array([4, 5, 6])]));\n+});\n+test(\"delta refuses malformed instructions, bounds and truncated input\", () => {\n+\tfor (const bytes of [\n+\t\t[1, 0],\n+\t\t[0, 1, 0],\n+\t\t[0, 1, 1],\n+\t\t[0, 1, 0x80],\n+\t\t[0, 2, 1, 7],\n+\t\t[0, 0, 1, 7],\n+\t])\n+\t\tassert.throws(() => applyGitDeltaChunks(ranges(Uint8Array.from(bytes)), ranges(new Uint8Array()), () => {}, 10));\n+\tlet writes = 0;\n+\tassert.throws(\n+\t\t() =>\n+\t\t\tapplyGitDeltaChunks(\n+\t\t\t\tranges(new Uint8Array([0, 11])),\n+\t\t\t\tranges(new Uint8Array()),\n+\t\t\t\t() => {\n+\t\t\t\t\twrites++;\n+\t\t\t\t},\n+\t\t\t\t10,\n+\t\t\t),\n+\t\t/exceeds bound/,\n+\t);\n+\tassert.equal(writes, 0);\n+});"}],"unmatched":[]}