Skip to content

Commit f0c49f9

Browse files
authored
fix: Web encryption crashing on init. (#537)
1 parent 53335c7 commit f0c49f9

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

.changeset/new-doors-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/web': patch
3+
---
4+
5+
Ensuring encryption pragma executes before setting cache size, fixes issue where encryption would throw an error during initialization.

packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ export class WASqliteConnection
235235
await this.openDB();
236236
this.registerBroadcastListeners();
237237
await this.executeSingleStatement(`PRAGMA temp_store = ${this.options.temporaryStorage};`);
238-
await this.executeSingleStatement(`PRAGMA cache_size = -${this.options.cacheSizeKb};`);
239238
await this.executeEncryptionPragma();
239+
await this.executeSingleStatement(`PRAGMA cache_size = -${this.options.cacheSizeKb};`);
240240

241241
this.sqliteAPI.update_hook(this.dbP, (updateType: number, dbName: string | null, tableName: string | null) => {
242242
if (!tableName) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
PowerSyncDatabase,
3+
WASQLiteOpenFactory,
4+
WASQLiteVFS,
5+
WebPowerSyncDatabaseOptionsWithOpenFactory,
6+
WebPowerSyncDatabaseOptionsWithSettings
7+
} from '@powersync/web';
8+
import { v4 as uuid } from 'uuid';
9+
import { describe, expect, it } from 'vitest';
10+
import { testSchema } from './utils/testDb';
11+
12+
describe('Encryption Tests', { sequential: true }, () => {
13+
it('IDBBatchAtomicVFS encryption', async () => {
14+
await testEncryption({
15+
schema: testSchema,
16+
database: { dbFilename: 'iddb-file.db' },
17+
encryptionKey: 'iddb-key'
18+
});
19+
});
20+
21+
it('OPFSCoopSyncVFS encryption', async () => {
22+
await testEncryption({
23+
schema: testSchema,
24+
database: new WASQLiteOpenFactory({
25+
dbFilename: 'opfs-file.db',
26+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
27+
encryptionKey: 'opfs-key'
28+
})
29+
});
30+
});
31+
32+
it('AccessHandlePoolVFS encryption', async () => {
33+
await testEncryption({
34+
schema: testSchema,
35+
database: new WASQLiteOpenFactory({
36+
dbFilename: 'ahp-file.db',
37+
vfs: WASQLiteVFS.AccessHandlePoolVFS,
38+
encryptionKey: 'ahp-key'
39+
})
40+
});
41+
});
42+
});
43+
44+
/**
45+
* The open/close and open again flow is an easy way to verify that encryption is working.
46+
*/
47+
const testEncryption = async (
48+
options: WebPowerSyncDatabaseOptionsWithSettings | WebPowerSyncDatabaseOptionsWithOpenFactory
49+
) => {
50+
let powersync = new PowerSyncDatabase(options as any);
51+
52+
await powersync.init();
53+
await powersync.close();
54+
55+
powersync = new PowerSyncDatabase(options as any);
56+
57+
await powersync.init();
58+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);
59+
const results = await powersync.getAll('SELECT * FROM assets');
60+
expect(results.length).toBe(1);
61+
62+
await powersync.disconnectAndClear();
63+
await powersync.close();
64+
};

0 commit comments

Comments
 (0)