Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"is-my-json-valid": "^2.20.5",
"js-yaml": "^4.0.0",
"matrix-appservice": "^0.10.0",
"matrix-bot-sdk": "^0.6.0-beta.2",
"matrix-bot-sdk": "^0.6.0-beta.4",
"matrix-js-sdk": "^12.4.1",
"nedb": "^1.8.0",
"nopt": "^5.0.0",
Expand Down
40 changes: 40 additions & 0 deletions src/models/encryption/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RustSdkAppserviceCryptoStorageProvider } from "matrix-bot-sdk";
import { watchFile, promises as fs } from "fs";

/**
* The RustSdk Crypto store uses a "sled" DB, which is a flat file KV store
* operated on by the rust internals. This class provides a way to synchronise
* that dataset in another place (e.g. a PostgreSQL table).
*/
export class SynchronisedCryptoStore extends RustSdkAppserviceCryptoStorageProvider {

private timeout: NodeJS.Timeout|null = null;

constructor(
baseStoragePath: string,
onSync: (error: Error|null, data?: Buffer) => Promise<void>,
debounceMs = 2500
) {
super(baseStoragePath);
const sync = () => {
fs.readFile(baseStoragePath, null).then((s) => {
onSync(null, s);
}).catch(onSync);
this.timeout = null;
};

watchFile(baseStoragePath, () => {
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
sync();
}, debounceMs);
});

// Save the file before the process closes.
process.on("beforeExit", () => {
sync();
});
}
}
Loading