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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 01:11:10 +05:30
|
|
|
import {Storage} from "./Storage.js";
|
2019-10-13 00:46:48 +05:30
|
|
|
import { openDatabase, reqAsPromise } from "./utils.js";
|
2019-12-14 22:59:35 +05:30
|
|
|
import { exportSession, importSession } from "./export.js";
|
2020-06-27 02:56:24 +05:30
|
|
|
import { schema } from "./schema.js";
|
2021-03-05 00:19:13 +05:30
|
|
|
import { detectWebkitEarlyCloseTxnBug } from "./quirks.js";
|
2019-12-14 22:59:35 +05:30
|
|
|
|
2020-09-17 16:04:10 +05:30
|
|
|
const sessionName = sessionId => `hydrogen_session_${sessionId}`;
|
2020-06-27 02:56:24 +05:30
|
|
|
const openDatabaseWithSessionId = sessionId => openDatabase(sessionName(sessionId), createStores, schema.length);
|
2019-02-07 03:34:39 +05:30
|
|
|
|
2020-10-20 20:32:34 +05:30
|
|
|
async function requestPersistedStorage() {
|
|
|
|
if (navigator?.storage?.persist) {
|
|
|
|
return await navigator.storage.persist();
|
|
|
|
} else if (document.requestStorageAccess) {
|
|
|
|
try {
|
|
|
|
await document.requestStorageAccess();
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class StorageFactory {
|
2020-10-16 16:20:37 +05:30
|
|
|
constructor(serviceWorkerHandler) {
|
|
|
|
this._serviceWorkerHandler = serviceWorkerHandler;
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:46:48 +05:30
|
|
|
async create(sessionId) {
|
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
|
|
|
|
|
|
|
const hasWebkitEarlyCloseTxnBug = await detectWebkitEarlyCloseTxnBug();
|
2019-12-14 22:59:35 +05:30
|
|
|
const db = await openDatabaseWithSessionId(sessionId);
|
2021-03-05 00:19:13 +05:30
|
|
|
return new Storage(db, hasWebkitEarlyCloseTxnBug);
|
2019-10-13 00:46:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
delete(sessionId) {
|
2019-12-14 22:59:35 +05:30
|
|
|
const databaseName = sessionName(sessionId);
|
2020-09-28 18:22:12 +05:30
|
|
|
const req = indexedDB.deleteDatabase(databaseName);
|
2019-10-13 00:46:48 +05:30
|
|
|
return reqAsPromise(req);
|
|
|
|
}
|
2019-12-14 22:59:35 +05:30
|
|
|
|
|
|
|
async export(sessionId) {
|
|
|
|
const db = await openDatabaseWithSessionId(sessionId);
|
|
|
|
return await exportSession(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
async import(sessionId, data) {
|
|
|
|
const db = await openDatabaseWithSessionId(sessionId);
|
|
|
|
return await importSession(db, data);
|
|
|
|
}
|
2019-02-05 04:51:50 +05:30
|
|
|
}
|
|
|
|
|
2020-06-27 02:56:24 +05:30
|
|
|
async function createStores(db, txn, oldVersion, version) {
|
|
|
|
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
|
|
|
}
|