Skip to content

Commit f72389c

Browse files
committed
Also test web
1 parent 0fcc1f0 commit f72389c

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed

packages/powersync_sqlcipher/example/pubspec.lock

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ packages:
5757
url: "https://pub.dev"
5858
source: hosted
5959
version: "1.19.1"
60+
convert:
61+
dependency: transitive
62+
description:
63+
name: convert
64+
sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68
65+
url: "https://pub.dev"
66+
source: hosted
67+
version: "3.1.2"
6068
crypto:
6169
dependency: transitive
6270
description:
@@ -218,6 +226,14 @@ packages:
218226
url: "https://pub.dev"
219227
source: hosted
220228
version: "1.16.0"
229+
mime:
230+
dependency: transitive
231+
description:
232+
name: mime
233+
sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6"
234+
url: "https://pub.dev"
235+
source: hosted
236+
version: "2.0.0"
221237
mutex:
222238
dependency: transitive
223239
description:
@@ -343,6 +359,22 @@ packages:
343359
url: "https://pub.dev"
344360
source: hosted
345361
version: "1.5.0"
362+
shelf:
363+
dependency: "direct dev"
364+
description:
365+
name: shelf
366+
sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12
367+
url: "https://pub.dev"
368+
source: hosted
369+
version: "1.4.2"
370+
shelf_static:
371+
dependency: "direct dev"
372+
description:
373+
name: shelf_static
374+
sha256: c87c3875f91262785dade62d135760c2c69cb217ac759485334c5857ad89f6e3
375+
url: "https://pub.dev"
376+
source: hosted
377+
version: "1.1.3"
346378
sky_engine:
347379
dependency: transitive
348380
description: flutter
@@ -404,7 +436,7 @@ packages:
404436
source: hosted
405437
version: "1.12.1"
406438
stream_channel:
407-
dependency: transitive
439+
dependency: "direct dev"
408440
description:
409441
name: stream_channel
410442
sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
@@ -484,7 +516,7 @@ packages:
484516
source: hosted
485517
version: "15.0.0"
486518
web:
487-
dependency: transitive
519+
dependency: "direct dev"
488520
description:
489521
name: web
490522
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"

packages/powersync_sqlcipher/example/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ dev_dependencies:
2222
sdk: flutter
2323

2424
flutter_lints: ^5.0.0
25+
shelf: ^1.4.2
26+
shelf_static: ^1.1.3
27+
stream_channel: ^2.1.4
28+
web: ^1.1.1
2529

2630
flutter:
2731
uses-material-design: true
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
@TestOn('js')
2+
library;
3+
4+
import 'dart:js_interop';
5+
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:powersync_sqlcipher/powersync.dart';
8+
import 'package:powersync_sqlcipher/sqlite_async.dart';
9+
import 'package:web/web.dart' as web;
10+
11+
void main() {
12+
// We can't run integration tests on the web, so this is a small smoke test
13+
// using the worker
14+
test('can use encrypted database', () async {
15+
final channel = spawnHybridUri('/test/worker_server.dart');
16+
final port = (await channel.stream.first as num).toInt();
17+
final sqliteWasmUri = 'http://localhost:$port/sqlite3mc.wasm';
18+
// Cross origin workers are not supported, but we can supply a Blob
19+
var sqliteUri = 'http://localhost:$port/db_worker.js';
20+
21+
final blob = web.Blob(
22+
<web.BlobPart>['importScripts("$sqliteUri");'.toJS].toJS,
23+
web.BlobPropertyBag(type: 'application/javascript'),
24+
);
25+
sqliteUri = _createObjectURL(blob);
26+
27+
final webOptions = SqliteOptions(
28+
webSqliteOptions: WebSqliteOptions(
29+
wasmUri: sqliteWasmUri.toString(),
30+
workerUri: sqliteUri,
31+
),
32+
);
33+
34+
final path = 'powersync-demo.db';
35+
36+
var db = PowerSyncDatabase.withFactory(
37+
PowerSyncSQLCipherOpenFactory(
38+
path: path,
39+
key: 'demo-key',
40+
sqliteOptions: webOptions,
41+
),
42+
schema: schema,
43+
);
44+
45+
await db.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', [
46+
'My username',
47+
]);
48+
await db.close();
49+
50+
expect(() async {
51+
db = PowerSyncDatabase.withFactory(
52+
PowerSyncSQLCipherOpenFactory(
53+
path: path,
54+
key: 'changed-key',
55+
sqliteOptions: webOptions,
56+
),
57+
schema: schema,
58+
);
59+
60+
await db.initialize();
61+
}, throwsA(anything));
62+
});
63+
}
64+
65+
@JS('URL.createObjectURL')
66+
external String _createObjectURL(web.Blob blob);
67+
68+
final schema = Schema([
69+
Table('users', [Column.text('name')]),
70+
]);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'dart:io';
2+
3+
import 'package:path/path.dart' as p;
4+
import 'package:shelf/shelf.dart';
5+
import 'package:shelf/shelf_io.dart' as io;
6+
import 'package:shelf_static/shelf_static.dart';
7+
import 'package:stream_channel/stream_channel.dart';
8+
9+
const _corsHeaders = {'Access-Control-Allow-Origin': '*'};
10+
11+
Middleware cors() {
12+
Response? handleOptionsRequest(Request request) {
13+
if (request.method == 'OPTIONS') {
14+
return Response.ok(null, headers: _corsHeaders);
15+
} else {
16+
// Returning null will run the regular request handler
17+
return null;
18+
}
19+
}
20+
21+
Response addCorsHeaders(Response response) {
22+
return response.change(headers: _corsHeaders);
23+
}
24+
25+
return createMiddleware(
26+
requestHandler: handleOptionsRequest,
27+
responseHandler: addCorsHeaders,
28+
);
29+
}
30+
31+
Future<void> hybridMain(StreamChannel<Object?> channel) async {
32+
final sqliteOutputPath = p.join('web', 'sqlite3mc.wasm');
33+
34+
if (!(await File(sqliteOutputPath).exists())) {
35+
throw AssertionError(
36+
'sqlite3mc.wasm file should be present in the ./web folder',
37+
);
38+
}
39+
40+
final server = await HttpServer.bind('localhost', 0);
41+
42+
final handler = const Pipeline()
43+
.addMiddleware(cors())
44+
.addHandler(createStaticHandler('web'));
45+
io.serveRequests(server, handler);
46+
47+
channel.sink.add(server.port);
48+
await channel.stream.listen(null).asFuture<void>().then<void>((_) async {
49+
print('closing server');
50+
await server.close();
51+
});
52+
}

0 commit comments

Comments
 (0)