2026-09-13 14:14:38 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
|
value: true
|
|
|
|
|
});
|
|
|
|
|
exports.RemoteIndexedDBStoreBackend = void 0;
|
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
|
var _logger = require("../logger");
|
|
|
|
|
var _utils = require("../utils");
|
2026-09-12 23:57:45 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 - 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-09-13 14:14:38 -04:00
|
|
|
class RemoteIndexedDBStoreBackend {
|
2026-09-12 23:57:45 -04:00
|
|
|
// Callback for when the IndexedDB gets closed unexpectedly
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An IndexedDB store backend where the actual backend sits in a web
|
|
|
|
|
* worker.
|
|
|
|
|
*
|
|
|
|
|
* Construct a new Indexed Database store backend. This requires a call to
|
|
|
|
|
* `connect()` before this store can be used.
|
|
|
|
|
* @param workerFactory - Factory which produces a Worker
|
|
|
|
|
* @param dbName - Optional database name. The same name must be used
|
|
|
|
|
* to open the same database.
|
|
|
|
|
*/
|
|
|
|
|
constructor(workerFactory, dbName) {
|
2026-09-13 14:14:38 -04:00
|
|
|
this.workerFactory = workerFactory;
|
|
|
|
|
this.dbName = dbName;
|
|
|
|
|
(0, _defineProperty2.default)(this, "worker", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "nextSeq", 0);
|
2026-09-12 23:57:45 -04:00
|
|
|
// The currently in-flight requests to the actual backend
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "inFlight", {});
|
2026-09-12 23:57:45 -04:00
|
|
|
// seq: promise
|
|
|
|
|
// Once we start connecting, we keep the promise and re-use it
|
|
|
|
|
// if we try to connect again
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "startPromise", void 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "onWorkerMessage", ev => {
|
2026-09-12 23:57:45 -04:00
|
|
|
const msg = ev.data;
|
|
|
|
|
if (msg.command == "closed") {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$onClose;
|
|
|
|
|
(_this$onClose = this.onClose) === null || _this$onClose === void 0 ? void 0 : _this$onClose.call(this);
|
2026-09-12 23:57:45 -04:00
|
|
|
} else if (msg.command == "cmd_success" || msg.command == "cmd_fail") {
|
|
|
|
|
if (msg.seq === undefined) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error("Got reply from worker with no seq");
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const def = this.inFlight[msg.seq];
|
|
|
|
|
if (def === undefined) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error("Got reply for unknown seq " + msg.seq);
|
2026-09-12 23:57:45 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
delete this.inFlight[msg.seq];
|
|
|
|
|
if (msg.command == "cmd_success") {
|
|
|
|
|
def.resolve(msg.result);
|
|
|
|
|
} else {
|
|
|
|
|
const error = new Error(msg.error.message);
|
|
|
|
|
error.name = msg.error.name;
|
|
|
|
|
def.reject(error);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.warn("Unrecognised message from worker: ", msg);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to connect to the database. This can fail if the user does not
|
|
|
|
|
* grant permission.
|
|
|
|
|
* @returns Promise which resolves if successfully connected.
|
|
|
|
|
*/
|
|
|
|
|
connect(onClose) {
|
|
|
|
|
this.onClose = onClose;
|
|
|
|
|
return this.ensureStarted().then(() => this.doCmd("connect"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the entire database. This should be used when logging out of a client
|
|
|
|
|
* to prevent mixing data between accounts.
|
|
|
|
|
* @returns Resolved when the database is cleared.
|
|
|
|
|
*/
|
|
|
|
|
clearDatabase() {
|
|
|
|
|
return this.ensureStarted().then(() => this.doCmd("clearDatabase"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @returns whether or not the database was newly created in this session. */
|
|
|
|
|
isNewlyCreated() {
|
|
|
|
|
return this.doCmd("isNewlyCreated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns Promise which resolves with a sync response to restore the
|
|
|
|
|
* client state to where it was at the last save, or null if there
|
|
|
|
|
* is no saved sync data.
|
|
|
|
|
*/
|
|
|
|
|
getSavedSync() {
|
|
|
|
|
return this.doCmd("getSavedSync");
|
|
|
|
|
}
|
|
|
|
|
getNextBatchToken() {
|
|
|
|
|
return this.doCmd("getNextBatchToken");
|
|
|
|
|
}
|
|
|
|
|
setSyncData(syncData) {
|
|
|
|
|
return this.doCmd("setSyncData", [syncData]);
|
|
|
|
|
}
|
|
|
|
|
syncToDatabase(userTuples) {
|
|
|
|
|
return this.doCmd("syncToDatabase", [userTuples]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the out-of-band membership events for this room that
|
|
|
|
|
* were previously loaded.
|
|
|
|
|
* @returns the events, potentially an empty array if OOB loading didn't yield any new members
|
|
|
|
|
* @returns in case the members for this room haven't been stored yet
|
|
|
|
|
*/
|
|
|
|
|
getOutOfBandMembers(roomId) {
|
|
|
|
|
return this.doCmd("getOutOfBandMembers", [roomId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores the out-of-band membership events for this room. Note that
|
|
|
|
|
* it still makes sense to store an empty array as the OOB status for the room is
|
|
|
|
|
* marked as fetched, and getOutOfBandMembers will return an empty array instead of null
|
|
|
|
|
* @param membershipEvents - the membership events to store
|
|
|
|
|
* @returns when all members have been stored
|
|
|
|
|
*/
|
|
|
|
|
setOutOfBandMembers(roomId, membershipEvents) {
|
|
|
|
|
return this.doCmd("setOutOfBandMembers", [roomId, membershipEvents]);
|
|
|
|
|
}
|
|
|
|
|
clearOutOfBandMembers(roomId) {
|
|
|
|
|
return this.doCmd("clearOutOfBandMembers", [roomId]);
|
|
|
|
|
}
|
|
|
|
|
getClientOptions() {
|
|
|
|
|
return this.doCmd("getClientOptions");
|
|
|
|
|
}
|
|
|
|
|
storeClientOptions(options) {
|
|
|
|
|
return this.doCmd("storeClientOptions", [options]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load all user presence events from the database. This is not cached.
|
|
|
|
|
* @returns A list of presence events in their raw form.
|
|
|
|
|
*/
|
|
|
|
|
getUserPresenceEvents() {
|
|
|
|
|
return this.doCmd("getUserPresenceEvents");
|
|
|
|
|
}
|
|
|
|
|
async saveToDeviceBatches(batches) {
|
|
|
|
|
return this.doCmd("saveToDeviceBatches", [batches]);
|
|
|
|
|
}
|
|
|
|
|
async getOldestToDeviceBatch() {
|
|
|
|
|
return this.doCmd("getOldestToDeviceBatch");
|
|
|
|
|
}
|
|
|
|
|
async removeToDeviceBatch(id) {
|
|
|
|
|
return this.doCmd("removeToDeviceBatch", [id]);
|
|
|
|
|
}
|
|
|
|
|
ensureStarted() {
|
2026-09-13 14:14:38 -04:00
|
|
|
if (!this.startPromise) {
|
|
|
|
|
this.worker = this.workerFactory();
|
2026-09-12 23:57:45 -04:00
|
|
|
this.worker.onmessage = this.onWorkerMessage;
|
|
|
|
|
|
|
|
|
|
// tell the worker the db name.
|
2026-09-13 14:14:38 -04:00
|
|
|
this.startPromise = this.doCmd("setupWorker", [this.dbName]).then(() => {
|
|
|
|
|
_logger.logger.log("IndexedDB worker is ready");
|
2026-09-12 23:57:45 -04:00
|
|
|
});
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
return this.startPromise;
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
doCmd(command, args) {
|
|
|
|
|
// wrap in a q so if the postMessage throws,
|
|
|
|
|
// the promise automatically gets rejected
|
|
|
|
|
return Promise.resolve().then(() => {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$worker;
|
2026-09-12 23:57:45 -04:00
|
|
|
const seq = this.nextSeq++;
|
2026-09-13 14:14:38 -04:00
|
|
|
const def = (0, _utils.defer)();
|
2026-09-12 23:57:45 -04:00
|
|
|
this.inFlight[seq] = def;
|
2026-09-13 14:14:38 -04:00
|
|
|
(_this$worker = this.worker) === null || _this$worker === void 0 ? void 0 : _this$worker.postMessage({
|
2026-09-12 23:57:45 -04:00
|
|
|
command,
|
|
|
|
|
seq,
|
|
|
|
|
args
|
|
|
|
|
});
|
|
|
|
|
return def.promise;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* Destroy the web worker
|
|
|
|
|
*/
|
|
|
|
|
async destroy() {
|
2026-09-13 14:14:38 -04:00
|
|
|
var _this$worker2;
|
|
|
|
|
(_this$worker2 = this.worker) === null || _this$worker2 === void 0 ? void 0 : _this$worker2.terminate();
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.RemoteIndexedDBStoreBackend = RemoteIndexedDBStoreBackend;
|
2026-09-12 23:57:45 -04:00
|
|
|
//# sourceMappingURL=indexeddb-remote-backend.js.map
|