@rocket.chat/forked-matrix-bot-sdk
Version:
TypeScript/JavaScript SDK for Matrix bots and appservices
83 lines (82 loc) • 3.48 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 default rust-sdk store (sled, file-based).
* @category Storage providers
*/
class RustSdkCryptoStorageProvider {
/**
* Creates a new rust-sdk storage provider.
* @param {string} storagePath The *directory* to persist database details to.
*/
constructor(storagePath) {
this.storagePath = storagePath;
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: {},
});
}
getDeviceId() {
return __awaiter(this, void 0, void 0, function* () {
return this.db.get('deviceId').value();
});
}
setDeviceId(deviceId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.set('deviceId', deviceId).write();
});
}
getRoom(roomId) {
return __awaiter(this, void 0, void 0, function* () {
const key = sha512().update(roomId).digest('hex');
return this.db.get(`rooms.${key}`).value();
});
}
storeRoom(roomId, config) {
return __awaiter(this, void 0, void 0, function* () {
const key = sha512().update(roomId).digest('hex');
this.db.set(`rooms.${key}`, config).write();
});
}
}
exports.RustSdkCryptoStorageProvider = RustSdkCryptoStorageProvider;
/**
* An appservice crypto storage provider for the default rust-sdk store (sled, file-based).
* @category Storage providers
*/
class RustSdkAppserviceCryptoStorageProvider extends RustSdkCryptoStorageProvider {
/**
* Creates a new rust-sdk storage provider.
* @param {string} baseStoragePath The *directory* to persist database details to.
*/
constructor(baseStoragePath) {
super(path.join(baseStoragePath, "_default"));
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));
}
}
exports.RustSdkAppserviceCryptoStorageProvider = RustSdkAppserviceCryptoStorageProvider;