Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2BE support #380

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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