| … | … | @@ -0,0 +1,110 @@ |
| 1 | /** |
| 2 | * A fetched bundle is a transfer artifact — once unbundled, drop it. |
| 3 | * |
| 4 | * Regression: the fetcher applied bundles and left every one of them in the |
| 5 | * overlay's destDir. On stortron that reached 17 GB, 736 files, back to June. |
| 6 | * |
| 7 | * The dangerous half is what must NOT be deleted: hyper-overlay short-circuits |
| 8 | * a hash it already holds and returns where those bytes really live, so an |
| 9 | * unconditional unlink would destroy this node's own producer spool. |
| 10 | */ |
| 11 | import test from 'brittle' |
| 12 | import fs from '#node:fs' |
| 13 | import fsp from '#node:fs/promises' |
| 14 | import path from '#node:path' |
| 15 | import os from '#node:os' |
| 16 | |
| 17 | import { discardFetchedBundle, materializeObject } from '../lib/git-overlay-fetch.js' |
| 18 | |
| 19 | async function tmpdir (name) { |
| 20 | return fsp.mkdtemp(path.join(os.tmpdir(), 'pgv3-gipclean-' + name + '-')) |
| 21 | } |
| 22 | |
| 23 | test('a throwaway fetch is deleted', async (t) => { |
| 24 | const dir = await tmpdir('throwaway') |
| 25 | const destPath = path.join(dir, 'blob-abc123-1700000000000-xyz789') |
| 26 | fs.writeFileSync(destPath, 'bundle bytes') |
| 27 | |
| 28 | t.is(discardFetchedBundle({ destPath }, null), true) |
| 29 | t.absent(fs.existsSync(destPath), 'file removed') |
| 30 | }) |
| 31 | |
| 32 | test('a local short-circuit is NEVER deleted', async (t) => { |
| 33 | const dir = await tmpdir('local') |
| 34 | // what the producer spool looks like: canonical bytes, natural name |
| 35 | const destPath = path.join(dir, 'deadbeef.bundle') |
| 36 | fs.writeFileSync(destPath, 'canonical bytes') |
| 37 | |
| 38 | t.is(discardFetchedBundle({ destPath, local: true }, null), false) |
| 39 | t.ok(fs.existsSync(destPath), 'canonical file kept') |
| 40 | }) |
| 41 | |
| 42 | test('a mirror-spool (content-addressed) path is NEVER deleted', async (t) => { |
| 43 | const dir = await tmpdir('spool') |
| 44 | const destPath = path.join(dir, 'a'.repeat(64)) |
| 45 | fs.writeFileSync(destPath, 'durably re-served bytes') |
| 46 | |
| 47 | t.is(discardFetchedBundle({ destPath }, null), false) |
| 48 | t.ok(fs.existsSync(destPath), 'spool file kept') |
| 49 | }) |
| 50 | |
| 51 | test('cleanup failure never breaks a successful fetch', async (t) => { |
| 52 | const warnings = [] |
| 53 | const res = { destPath: '/nonexistent/blob-abc-1-2' } |
| 54 | t.is(discardFetchedBundle(res, { warn: (m) => warnings.push(m) }), false) |
| 55 | t.is(warnings.length, 1, 'warned rather than threw') |
| 56 | }) |
| 57 | |
| 58 | test('materializeObject drops each bundle after applying it', async (t) => { |
| 59 | const dir = await tmpdir('materialize') |
| 60 | const repoPath = path.join(dir, 'repo.git') |
| 61 | fs.mkdirSync(repoPath) |
| 62 | |
| 63 | // two-link chain: base then head |
| 64 | const fetched = [] |
| 65 | function bundleFile (name) { |
| 66 | const p = path.join(dir, 'blob-' + name + '-1700000000000-aaaaaa') |
| 67 | fs.writeFileSync(p, '# v2 git bundle\n') |
| 68 | fetched.push(p) |
| 69 | return p |
| 70 | } |
| 71 | |
| 72 | const applied = [] |
| 73 | const present = new Set() |
| 74 | |
| 75 | const tracker = { |
| 76 | async getBundle (oid) { |
| 77 | if (oid === 'head') return { contentHash: 'hash-head', baseOid: 'base' } |
| 78 | if (oid === 'base') return { contentHash: 'hash-base', baseOid: null } |
| 79 | return null |
| 80 | } |
| 81 | } |
| 82 | const overlay = { |
| 83 | async fetchFile (hash) { |
| 84 | return { destPath: bundleFile(hash) } |
| 85 | } |
| 86 | } |
| 87 | // stand in for git: cat-file -e answers from `present`, unbundle records |
| 88 | const git = (cmd, args) => { |
| 89 | if (args.includes('cat-file')) { |
| 90 | const oid = args[args.indexOf('-e') + 1].replace('^{object}', '') |
| 91 | if (!present.has(oid)) throw new Error('missing') |
| 92 | return Buffer.from('') |
| 93 | } |
| 94 | if (args.includes('unbundle')) { |
| 95 | applied.push(args[args.length - 1]) |
| 96 | // applying the head bundle satisfies the target |
| 97 | if (applied.length === 2) present.add('head') |
| 98 | return Buffer.from('abc refs/heads/main\n') |
| 99 | } |
| 100 | return Buffer.from('') |
| 101 | } |
| 102 | |
| 103 | await materializeObject({ oid: 'head', tracker, overlay, repoPath, git }) |
| 104 | |
| 105 | t.is(applied.length, 2, 'applied base then head') |
| 106 | t.is(fetched.length, 2, 'fetched two bundles') |
| 107 | for (const f of fetched) t.absent(fs.existsSync(f), 'fetched bundle removed: ' + path.basename(f)) |
| 108 | |
| 109 | await fsp.rm(dir, { recursive: true, force: true }) |
| 110 | }) |