Initial import of verae-archive-worm from zapier monorepo
This commit is contained in:
commit
4c7fe00ff4
6 changed files with 145 additions and 0 deletions
40
src/archive.js
Normal file
40
src/archive.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* 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) ?? [];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue