69 lines
2.8 KiB
JavaScript
69 lines
2.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.RustSdkAppserviceCryptoStorageProvider = exports.RustSdkCryptoStorageProvider = void 0;
|
|
const lowdb = require("lowdb");
|
|
const FileSync = require("lowdb/adapters/FileSync");
|
|
const mkdirp = require("mkdirp");
|
|
const path = require("path");
|
|
const sha512 = require("hash.js/lib/hash/sha/512");
|
|
const sha256 = require("hash.js/lib/hash/sha/256");
|
|
/**
|
|
* A crypto storage provider for the file-based rust-sdk store.
|
|
* @category Storage providers
|
|
*/
|
|
class RustSdkCryptoStorageProvider {
|
|
/**
|
|
* Creates a new rust-sdk storage provider.
|
|
* @param storagePath The *directory* to persist database details to.
|
|
* @param storageType The storage type to use. Must be supported by the rust-sdk.
|
|
*/
|
|
constructor(storagePath, storageType = 0 /* RustSdkCryptoStoreType.Sled */) {
|
|
this.storagePath = storagePath;
|
|
this.storageType = storageType;
|
|
this.storagePath = path.resolve(this.storagePath);
|
|
mkdirp.sync(storagePath);
|
|
const adapter = new FileSync(path.join(storagePath, "bot-sdk.json"));
|
|
this.db = lowdb(adapter);
|
|
this.db.defaults({
|
|
deviceId: null,
|
|
rooms: {},
|
|
});
|
|
}
|
|
async getDeviceId() {
|
|
return this.db.get('deviceId').value();
|
|
}
|
|
async setDeviceId(deviceId) {
|
|
this.db.set('deviceId', deviceId).write();
|
|
}
|
|
async getRoom(roomId) {
|
|
const key = sha512().update(roomId).digest('hex');
|
|
return this.db.get(`rooms.${key}`).value();
|
|
}
|
|
async storeRoom(roomId, config) {
|
|
const key = sha512().update(roomId).digest('hex');
|
|
this.db.set(`rooms.${key}`, config).write();
|
|
}
|
|
}
|
|
exports.RustSdkCryptoStorageProvider = RustSdkCryptoStorageProvider;
|
|
/**
|
|
* An appservice crypto storage provider for the file-based rust-sdk store.
|
|
* @category Storage providers
|
|
*/
|
|
class RustSdkAppserviceCryptoStorageProvider extends RustSdkCryptoStorageProvider {
|
|
/**
|
|
* Creates a new rust-sdk storage provider.
|
|
* @param baseStoragePath The *directory* to persist database details to.
|
|
* @param storageType The storage type to use. Must be supported by the rust-sdk.
|
|
*/
|
|
constructor(baseStoragePath, storageType = 0 /* RustSdkCryptoStoreType.Sled */) {
|
|
super(path.join(baseStoragePath, "_default"), storageType);
|
|
this.baseStoragePath = baseStoragePath;
|
|
}
|
|
storageForUser(userId) {
|
|
// sha256 because sha512 is a bit big for some operating systems
|
|
const key = sha256().update(userId).digest('hex');
|
|
return new RustSdkCryptoStorageProvider(path.join(this.baseStoragePath, key), this.storageType);
|
|
}
|
|
}
|
|
exports.RustSdkAppserviceCryptoStorageProvider = RustSdkAppserviceCryptoStorageProvider;
|
|
//# sourceMappingURL=RustSdkCryptoStorageProvider.js.map
|