feat(vault): v2 — compressed entries, chunked objects, index pushes, lazy reads, prune (4.14.0)
Entries are deflate-raw compressed when it pays, split into 4 MiB sealed parts, and manifests are compressed (SVM2). A push with a local index reuses unchanged files without downloading anything, and a push with no changes commits nothing. Readers fetch one manifest and only the entries they need. The relay records what each version references and can prune old versions signed by the owner. Cross-language vectors in test-vectors/vault-v2.json. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -118,3 +118,134 @@ describe('semantics', () => {
|
||||
store.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('prune', () => {
|
||||
const h = (c: string) => c.repeat(64);
|
||||
|
||||
test('removes unkept versions and only the objects nothing else uses', async () => {
|
||||
const store = new SqliteVaultStore(scratch('prune'));
|
||||
await store.putObject(VAULT, h('1'), new Uint8Array(10)); // manifest 1
|
||||
await store.putObject(VAULT, h('2'), new Uint8Array(20)); // manifest 2
|
||||
await store.putObject(VAULT, h('a'), new Uint8Array(30)); // shared file
|
||||
await store.putObject(VAULT, h('b'), new Uint8Array(40)); // only in v1
|
||||
await store.appendLog(VAULT, { seq: 1, manifest: h('1'), at: 1, bytes: 0 }, [h('1'), h('a'), h('b')]);
|
||||
await store.appendLog(VAULT, { seq: 2, manifest: h('2'), at: 2, bytes: 0 }, [h('2'), h('a')]);
|
||||
|
||||
const res = await store.prune(VAULT, { keep: [2], upTo: 2, protectSince: Date.now() });
|
||||
expect(res).toEqual({ removedVersions: 1, removedObjects: 2, freedBytes: 50 });
|
||||
expect(await store.hasObject(VAULT, h('a'))).toBe(true);
|
||||
expect(await store.hasObject(VAULT, h('b'))).toBe(false);
|
||||
expect((await store.log(VAULT)).map((e) => e.seq)).toEqual([2]);
|
||||
store.close();
|
||||
});
|
||||
|
||||
test('a fresh, never-committed upload survives; a stale one does not', async () => {
|
||||
const store = new SqliteVaultStore(scratch('grace'));
|
||||
await store.putObject(VAULT, h('1'), new Uint8Array(1));
|
||||
await store.appendLog(VAULT, { seq: 1, manifest: h('1'), at: 1, bytes: 0 }, [h('1')]);
|
||||
await store.putObject(VAULT, h('c'), new Uint8Array(5));
|
||||
|
||||
const kept = await store.prune(VAULT, { keep: [1], upTo: 1, protectSince: Date.now() - 60_000 });
|
||||
expect(kept.removedObjects).toBe(0);
|
||||
const gone = await store.prune(VAULT, { keep: [1], upTo: 1, protectSince: Date.now() + 1 });
|
||||
expect(gone.removedObjects).toBe(1);
|
||||
expect(await store.hasObject(VAULT, h('c'))).toBe(false);
|
||||
store.close();
|
||||
});
|
||||
|
||||
test('never removes an entry above upTo, even when it is not in keep', async () => {
|
||||
const store = new SqliteVaultStore(scratch('upto'));
|
||||
for (let seq = 1; seq <= 3; seq++) {
|
||||
await store.appendLog(VAULT, { seq, manifest: `${seq}`.repeat(64), at: seq, bytes: 0 }, []);
|
||||
}
|
||||
await store.prune(VAULT, { keep: [2], upTo: 2, protectSince: 0 });
|
||||
expect((await store.log(VAULT)).map((e) => e.seq)).toEqual([2, 3]);
|
||||
store.close();
|
||||
});
|
||||
|
||||
test('untracked entries pin what existed when they were appended, until they are gone', async () => {
|
||||
const store = new SqliteVaultStore(scratch('pin'));
|
||||
await store.putObject(VAULT, h('1'), new Uint8Array(1));
|
||||
await store.putObject(VAULT, h('b'), new Uint8Array(1));
|
||||
await store.appendLog(VAULT, { seq: 1, manifest: h('1'), at: 1, bytes: 0 }); // no refs
|
||||
await store.putObject(VAULT, h('2'), new Uint8Array(1));
|
||||
await store.appendLog(VAULT, { seq: 2, manifest: h('2'), at: 2, bytes: 0 }, [h('2')]);
|
||||
await store.putObject(VAULT, h('3'), new Uint8Array(1));
|
||||
await store.appendLog(VAULT, { seq: 3, manifest: h('3'), at: 3, bytes: 0 }, [h('3')]);
|
||||
|
||||
const first = await store.prune(VAULT, { keep: [1, 3], upTo: 3, protectSince: Date.now() });
|
||||
expect(first.removedObjects).toBe(1); // only manifest 2
|
||||
expect(await store.hasObject(VAULT, h('b'))).toBe(true);
|
||||
|
||||
const second = await store.prune(VAULT, { keep: [3], upTo: 3, protectSince: Date.now() });
|
||||
expect(second.removedObjects).toBe(2); // manifest 1 and b, unpinned
|
||||
expect(await store.hasObject(VAULT, h('3'))).toBe(true);
|
||||
store.close();
|
||||
});
|
||||
|
||||
test('a database from before ref tracking migrates with everything pinned', async () => {
|
||||
// The exact pre-v2 schema, with a version and its objects in it.
|
||||
const path = scratch('migrate');
|
||||
const { Database } = await import('bun:sqlite');
|
||||
const old = new Database(path, { create: true });
|
||||
old.exec(`
|
||||
CREATE TABLE shade_vault_objects (vault_id TEXT NOT NULL, hash TEXT NOT NULL, bytes BLOB NOT NULL,
|
||||
size INTEGER NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY (vault_id, hash));
|
||||
CREATE TABLE shade_vault_log (vault_id TEXT NOT NULL, seq INTEGER NOT NULL, manifest TEXT NOT NULL,
|
||||
at INTEGER NOT NULL, bytes INTEGER NOT NULL, PRIMARY KEY (vault_id, seq));
|
||||
`);
|
||||
old.prepare('INSERT INTO shade_vault_objects VALUES (?, ?, ?, ?, ?)').run(VAULT, h('1'), new Uint8Array(1), 1, 0);
|
||||
old.prepare('INSERT INTO shade_vault_objects VALUES (?, ?, ?, ?, ?)').run(VAULT, h('a'), new Uint8Array(1), 1, 0);
|
||||
old.prepare('INSERT INTO shade_vault_log VALUES (?, ?, ?, ?, ?)').run(VAULT, 1, h('1'), 1, 2);
|
||||
old.close();
|
||||
|
||||
const store = new SqliteVaultStore(path);
|
||||
// A tracked version on top, then drop nothing but nothing referenced by it:
|
||||
// the old file object must survive because version 1 may use it.
|
||||
await store.putObject(VAULT, h('2'), new Uint8Array(1));
|
||||
await store.appendLog(VAULT, { seq: 2, manifest: h('2'), at: 2, bytes: 0 }, [h('2')]);
|
||||
const res = await store.prune(VAULT, { keep: [1, 2], upTo: 2, protectSince: Date.now() });
|
||||
expect(res.removedObjects).toBe(0);
|
||||
expect(await store.hasObject(VAULT, h('a'))).toBe(true);
|
||||
store.close();
|
||||
|
||||
// Reopening runs the migration check again and must be a no-op.
|
||||
const again = new SqliteVaultStore(path);
|
||||
expect((await again.log(VAULT)).map((e) => e.seq)).toEqual([1, 2]);
|
||||
const final = await again.prune(VAULT, { keep: [2], upTo: 2, protectSince: Date.now() });
|
||||
expect(final).toMatchObject({ removedVersions: 1, removedObjects: 2 });
|
||||
again.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('through the real routes and client', () => {
|
||||
test('v2 push, index reuse, multi-part and prune all work on SQLite', async () => {
|
||||
const { SubtleCryptoProvider } = await import('@shade/crypto-web');
|
||||
const { createVaultRoutes } = await import('@shade/vault/server');
|
||||
const { HttpVaultTransport, VaultClient, deriveVaultKeys } = await import('@shade/vault');
|
||||
const crypto = new SubtleCryptoProvider();
|
||||
const store = new SqliteVaultStore(scratch('e2e'));
|
||||
const routes = createVaultRoutes(store, crypto, { pruneGraceMs: 0 });
|
||||
const transport = new HttpVaultTransport('http://v', (i, init) => routes.fetch(new Request(i, init)));
|
||||
const keys = await deriveVaultKeys(new Uint8Array(32).fill(5), 'scaffold');
|
||||
const client = new VaultClient(crypto, keys, transport, { chunkSize: 512 });
|
||||
|
||||
const big = new Uint8Array(2000);
|
||||
globalThis.crypto.getRandomValues(big);
|
||||
const text = new TextEncoder().encode('linje\n'.repeat(500));
|
||||
const first = await client.push([{ path: 'big.bin', bytes: big }, { path: 't.md', bytes: text }], 1);
|
||||
const same = await client.push([{ path: 'big.bin', bytes: big }, { path: 't.md', bytes: text }], 2, undefined, {
|
||||
index: first.index,
|
||||
});
|
||||
expect(same.unchanged).toBe(true);
|
||||
await client.push([{ path: 't.md', bytes: text }], 3, undefined, { index: same.index });
|
||||
|
||||
const pruned = await client.prune([2]);
|
||||
expect(pruned.removedVersions).toBe(1);
|
||||
expect(pruned.removedObjects).toBe(5); // big.bin's 4 parts + manifest 1
|
||||
const { files } = await client.pull();
|
||||
expect(files.get('t.md')).toEqual(text);
|
||||
expect(files.has('big.bin')).toBe(false);
|
||||
store.close();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user