This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/storage/idb/StorageFactory.ts

99 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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.
*/
2021-08-13 01:36:09 +05:30
import {Storage} from "./Storage";
2021-08-10 02:26:20 +05:30
import { openDatabase, reqAsPromise } from "./utils";
2021-08-13 01:50:37 +05:30
import { exportSession, importSession } from "./export";
2021-08-13 01:58:36 +05:30
import { schema } from "./schema";
2021-08-13 22:34:03 +05:30
import { detectWebkitEarlyCloseTxnBug } from "./quirks";
2019-12-14 22:59:35 +05:30
2021-08-13 22:43:40 +05:30
const sessionName = (sessionId: string) => `hydrogen_session_${sessionId}`;
const openDatabaseWithSessionId = function(sessionId: string, idbFactory: IDBFactory): Promise<IDBDatabase> {
return openDatabase(sessionName(sessionId), createStores, schema.length, idbFactory);
}
2019-02-07 03:34:39 +05:30
2021-08-13 22:43:40 +05:30
interface ServiceWorkerHandler {
preventConcurrentSessionAccess: (sessionId: string) => Promise<void>;
}
async function requestPersistedStorage(): Promise<boolean> {
// don't assume browser so we can run in node with fake-idb
const glob = this;
if (glob?.navigator?.storage?.persist) {
return await glob.navigator.storage.persist();
} else if (glob?.document.requestStorageAccess) {
try {
await glob.document.requestStorageAccess();
return true;
} catch (err) {
return false;
}
} else {
return false;
}
}
export class StorageFactory {
2021-08-13 22:43:40 +05:30
private _serviceWorkerHandler: ServiceWorkerHandler;
private _idbFactory: IDBFactory;
constructor(serviceWorkerHandler: ServiceWorkerHandler, idbFactory: IDBFactory = window.indexedDB, IDBKeyRange = window.IDBKeyRange) {
this._serviceWorkerHandler = serviceWorkerHandler;
this._idbFactory = idbFactory;
2021-08-13 22:43:40 +05:30
// @ts-ignore
this._IDBKeyRange = IDBKeyRange;
}
2021-08-13 22:43:40 +05:30
async create(sessionId: string): Promise<Storage> {
await this._serviceWorkerHandler?.preventConcurrentSessionAccess(sessionId);
requestPersistedStorage().then(persisted => {
// Firefox lies here though, and returns true even if the user denied the request
if (!persisted) {
console.warn("no persisted storage, database can be evicted by browser");
}
});
const hasWebkitEarlyCloseTxnBug = await detectWebkitEarlyCloseTxnBug(this._idbFactory);
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
2021-08-13 22:43:40 +05:30
// @ts-ignore
return new Storage(db, this._IDBKeyRange, hasWebkitEarlyCloseTxnBug);
2019-10-13 00:46:48 +05:30
}
2021-08-13 22:43:40 +05:30
delete(sessionId: string): Promise<IDBDatabase> {
2019-12-14 22:59:35 +05:30
const databaseName = sessionName(sessionId);
const req = this._idbFactory.deleteDatabase(databaseName);
2019-10-13 00:46:48 +05:30
return reqAsPromise(req);
}
2019-12-14 22:59:35 +05:30
2021-08-13 22:43:40 +05:30
async export(sessionId: string): Promise<{ [storeName: string]: any }> {
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
2019-12-14 22:59:35 +05:30
return await exportSession(db);
}
2021-08-13 22:43:40 +05:30
async import(sessionId: string, data: { [storeName: string]: any }): Promise<void> {
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
2019-12-14 22:59:35 +05:30
return await importSession(db, data);
}
2019-02-05 04:51:50 +05:30
}
2021-08-13 22:43:40 +05:30
async function createStores(db: IDBDatabase, txn: IDBTransaction, oldVersion: number | null, version: number): Promise<void> {
2020-06-27 02:56:24 +05:30
const startIdx = oldVersion || 0;
2019-04-18 23:49:43 +05:30
2020-06-27 02:56:24 +05:30
for(let i = startIdx; i < version; ++i) {
await schema[i](db, txn);
}
2019-04-18 23:49:43 +05:30
}