Skip to content

Commit 68cc8cc

Browse files
committed
Make encryption example a separate button
1 parent 739d071 commit 68cc8cc

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

sqlite3/example/web/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ <h1>sqlite3 web demo</h1>
2222
<li>With an Origin-Private FileSystem (OPFS)-based storage implementation. Note that this requires two workers and a special header
2323
that is not available with <code>build_runner serve</code>, but it will work when launching this website with <code>dart run tool/example_server.dart</code> <button id="start-opfs">Start demo!</button>
2424
</li>
25+
<li>With an in-memory test (using <a href="https://utelle.github.io/SQLite3MultipleCiphers/">SQLite3 Multiple Ciphers</a> for encryption): <button id="start-encryption">Start encryption!</button></li>
2526
</ul>
2627

2728
After launching the example, you can check the console and the code in <code>main.dart</code> (or <code>worker.dart</code> for the OPFS example)

sqlite3/example/web/main.dart

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ import 'package:sqlite3/wasm.dart';
55
Future<void> main() async {
66
final startIndexedDb = document.getElementById('start-idb')!;
77
final startOpfs = document.getElementById('start-opfs')!;
8+
final startEncryption = document.getElementById('start-encryption')!;
89

910
startIndexedDb.onClick.listen((_) async {
1011
startIndexedDb.remove();
11-
1212
final sqlite3 =
1313
await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.debug.wasm'));
1414

1515
print(sqlite3.version);
1616

1717
sqlite3.registerVirtualFileSystem(
18-
InMemoryFileSystem(),
18+
await IndexedDbFileSystem.open(dbName: 'sqlite3-example'),
1919
makeDefault: true,
2020
);
2121

22-
sqlite3.open('/database', vfs: 'multipleciphers-dart-memory')
23-
..execute("pragma key = 'test';")
22+
sqlite3.open('/database')
2423
..execute('pragma user_version = 1')
2524
..execute('CREATE TABLE foo (bar INTEGER NOT NULL);')
2625
..execute('INSERT INTO foo (bar) VALUES (?)', [3])
@@ -36,4 +35,27 @@ Future<void> main() async {
3635
final worker = Worker('worker.dart.js');
3736
worker.postMessage('start');
3837
});
38+
39+
startEncryption.onClick.listen((_) async {
40+
startEncryption.remove();
41+
final sqlite3 = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3mc.wasm'));
42+
43+
sqlite3.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true);
44+
45+
sqlite3.open('/database')
46+
..execute("pragma key = 'test';")
47+
..execute('pragma user_version = 1')
48+
..execute('CREATE TABLE foo (bar INTEGER NOT NULL);')
49+
..execute('INSERT INTO foo (bar) VALUES (?)', [3])
50+
..dispose();
51+
52+
final db = sqlite3.open('/database');
53+
try {
54+
db.select('SELECT * FROM foo');
55+
} on SqliteException {
56+
print('database call failed (expected due to missing key)');
57+
}
58+
db.execute("pragma key = 'test';");
59+
print(db.select('SELECT * FROM foo'));
60+
});
3961
}

0 commit comments

Comments
 (0)