verae-archive-worm/src/archive.js

40 lines
1 KiB
JavaScript

/**
* In-process WORM archive: append-only records + bloom.
* @module archive
*/
import { Bloom } from './bloom.js';
export class WormArchive {
/**
* @param {string} archiveId
*/
constructor(archiveId) {
this.archiveId = archiveId;
this.bloom = new Bloom();
/** @type {Map<string, object[]>} */
this.byHash = new Map();
}
/**
* @param {{ sha256: string, kind: string, record: object }} put
*/
put(put) {
const key = String(put.sha256).toLowerCase();
const list = this.byHash.get(key) ?? [];
list.push({ kind: put.kind, record: put.record, archiveId: this.archiveId, storedAt: new Date().toISOString() });
this.byHash.set(key, list);
this.bloom.add(key);
}
/**
* Bloom miss → null (caller must stay silent). Hit → records (may be empty if false positive).
* @param {string} sha256
* @returns {object[]|null}
*/
query(sha256) {
const key = String(sha256).toLowerCase();
if (!this.bloom.mightHave(key)) return null;
return this.byHash.get(key) ?? [];
}
}