Changeset View
Changeset View
Standalone View
Standalone View
web/database/worker/db-worker.js
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | import { | ||||
localforageConfig, | localforageConfig, | ||||
SQLITE_CONTENT, | SQLITE_CONTENT, | ||||
SQLITE_ENCRYPTION_KEY, | SQLITE_ENCRYPTION_KEY, | ||||
} from '../utils/constants.js'; | } from '../utils/constants.js'; | ||||
import { | import { | ||||
decryptDatabaseFile, | decryptDatabaseFile, | ||||
encryptDatabaseFile, | encryptDatabaseFile, | ||||
generateDatabaseCryptoKey, | generateDatabaseCryptoKey, | ||||
importJWKKey, | |||||
} from '../utils/worker-crypto-utils.js'; | } from '../utils/worker-crypto-utils.js'; | ||||
localforage.config(localforageConfig); | localforage.config(localforageConfig); | ||||
let sqliteDb: ?SqliteDatabase = null; | let sqliteDb: ?SqliteDatabase = null; | ||||
let encryptionKey: ?CryptoKey = null; | let encryptionKey: ?CryptoKey = null; | ||||
let persistNeeded: boolean = false; | let persistNeeded: boolean = false; | ||||
let persistInProgress: boolean = false; | let persistInProgress: boolean = false; | ||||
async function initDatabase(sqljsFilePath: string, sqljsFilename: ?string) { | async function initDatabase( | ||||
sqljsFilePath: string, | |||||
sqljsFilename: ?string, | |||||
encryptionKeyJWK?: ?SubtleCrypto$JsonWebKey, | |||||
) { | |||||
if (encryptionKeyJWK) { | |||||
encryptionKey = await importJWKKey(encryptionKeyJWK); | |||||
} else { | |||||
encryptionKey = await localforage.getItem(SQLITE_ENCRYPTION_KEY); | encryptionKey = await localforage.getItem(SQLITE_ENCRYPTION_KEY); | ||||
if (!encryptionKey) { | if (!encryptionKey) { | ||||
const cryptoKey = await generateDatabaseCryptoKey({ extractable: false }); | const cryptoKey = await generateDatabaseCryptoKey({ extractable: false }); | ||||
await localforage.setItem(SQLITE_ENCRYPTION_KEY, cryptoKey); | await localforage.setItem(SQLITE_ENCRYPTION_KEY, cryptoKey); | ||||
} | } | ||||
} | |||||
const encryptedContent = await localforage.getItem(SQLITE_CONTENT); | const encryptedContent = await localforage.getItem(SQLITE_CONTENT); | ||||
let dbContent = null; | let dbContent = null; | ||||
try { | try { | ||||
if (encryptionKey && encryptedContent) { | if (encryptionKey && encryptedContent) { | ||||
dbContent = await decryptDatabaseFile(encryptedContent, encryptionKey); | dbContent = await decryptDatabaseFile(encryptedContent, encryptionKey); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | ): Promise<?WorkerResponseMessage> { | ||||
) { | ) { | ||||
const cryptoKey = await generateDatabaseCryptoKey({ extractable: false }); | const cryptoKey = await generateDatabaseCryptoKey({ extractable: false }); | ||||
await localforage.setItem(SQLITE_ENCRYPTION_KEY, cryptoKey); | await localforage.setItem(SQLITE_ENCRYPTION_KEY, cryptoKey); | ||||
return undefined; | return undefined; | ||||
} | } | ||||
// database operations | // database operations | ||||
if (message.type === workerRequestMessageTypes.INIT) { | if (message.type === workerRequestMessageTypes.INIT) { | ||||
await initDatabase(message.sqljsFilePath, message.sqljsFilename); | await initDatabase( | ||||
message.sqljsFilePath, | |||||
message.sqljsFilename, | |||||
message.encryptionKey, | |||||
); | |||||
return undefined; | return undefined; | ||||
} else if (message.type === workerRequestMessageTypes.CLEAR_SENSITIVE_DATA) { | } else if (message.type === workerRequestMessageTypes.CLEAR_SENSITIVE_DATA) { | ||||
encryptionKey = null; | encryptionKey = null; | ||||
if (sqliteDb) { | if (sqliteDb) { | ||||
sqliteDb.close(); | sqliteDb.close(); | ||||
} | } | ||||
await localforage.clear(); | await localforage.clear(); | ||||
return undefined; | return undefined; | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |