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> {
|
2021-06-02 16:01:13 +05:30
|
|
|
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> {
|
2021-06-02 16:01:13 +05:30
|
|
|
// 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) {
|
2020-10-20 20:32:34 +05:30
|
|
|
try {
|
2021-06-02 16:01:13 +05:30
|
|
|
await glob.document.requestStorageAccess();
|
2020-10-20 20:32:34 +05:30
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
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) {
|
2020-10-16 16:20:37 +05:30
|
|
|
this._serviceWorkerHandler = serviceWorkerHandler;
|
2021-06-02 16:01:13 +05:30
|
|
|
this._idbFactory = idbFactory;
|
2021-08-13 22:43:40 +05:30
|
|
|
// @ts-ignore
|
2021-06-02 16:01:13 +05:30
|
|
|
this._IDBKeyRange = IDBKeyRange;
|
2020-10-16 16:20:37 +05:30
|
|
|
}
|
|
|
|
|
2021-08-13 22:43:40 +05:30
|
|
|
async create(sessionId: string): Promise<Storage> {
|
2020-10-16 16:20:37 +05:30
|
|
|
await this._serviceWorkerHandler?.preventConcurrentSessionAccess(sessionId);
|
2020-10-20 20:32:34 +05:30
|
|
|
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");
|
|
|
|
}
|
|
|
|
});
|
2021-03-05 00:19:13 +05:30
|
|
|
|
2021-06-02 16:01:13 +05:30
|
|
|
const hasWebkitEarlyCloseTxnBug = await detectWebkitEarlyCloseTxnBug(this._idbFactory);
|
|
|
|
const db = await openDatabaseWithSessionId(sessionId, this._idbFactory);
|
2021-08-13 22:43:40 +05:30
|
|
|
// @ts-ignore
|
2021-06-02 16:01:13 +05:30
|
|
|
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);
|
2021-06-02 16:01:13 +05:30
|
|
|
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 }> {
|
2021-06-02 16:01:13 +05:30
|
|
|
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> {
|
2021-06-02 16:01:13 +05:30
|
|
|
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
|
|
|
}
|