veilid/veilid-flutter/lib/veilid_js.dart

429 lines
12 KiB
Dart
Raw Normal View History

2022-02-07 02:18:42 +00:00
import 'veilid.dart';
2022-03-17 14:31:10 +00:00
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:js_util' as js_util;
2022-02-07 02:18:42 +00:00
import 'dart:async';
2022-03-16 03:02:24 +00:00
import 'dart:convert';
2022-10-01 02:37:55 +00:00
import 'dart:typed_data';
2022-02-07 02:18:42 +00:00
2023-05-15 00:50:28 +00:00
import 'veilid_encoding.dart';
2023-01-04 22:02:45 +00:00
2023-05-14 00:36:52 +00:00
//////////////////////////////////////////////////////////
// WASM Platform-specific config
class VeilidWASMConfigLoggingPerformance {
bool enabled;
VeilidConfigLogLevel level;
bool logsInTimings;
bool logsInConsole;
VeilidWASMConfigLoggingPerformance({
required this.enabled,
required this.level,
required this.logsInTimings,
required this.logsInConsole,
});
Map<String, dynamic> get json {
return {
'enabled': enabled,
'level': level.json,
'logs_in_timings': logsInTimings,
'logs_in_console': logsInConsole,
};
}
VeilidWASMConfigLoggingPerformance.fromJson(dynamic json)
: enabled = json['enabled'],
level = veilidConfigLogLevelFromJson(json['level']),
logsInTimings = json['logs_in_timings'],
logsInConsole = json['logs_in_console'];
}
class VeilidWASMConfigLoggingApi {
bool enabled;
VeilidConfigLogLevel level;
VeilidWASMConfigLoggingApi({
required this.enabled,
required this.level,
});
Map<String, dynamic> get json {
return {
'enabled': enabled,
'level': level.json,
};
}
VeilidWASMConfigLoggingApi.fromJson(dynamic json)
: enabled = json['enabled'],
level = veilidConfigLogLevelFromJson(json['level']);
}
class VeilidWASMConfigLogging {
VeilidWASMConfigLoggingPerformance performance;
VeilidWASMConfigLoggingApi api;
VeilidWASMConfigLogging({required this.performance, required this.api});
Map<String, dynamic> get json {
return {
'performance': performance.json,
'api': api.json,
};
}
VeilidWASMConfigLogging.fromJson(dynamic json)
: performance =
VeilidWASMConfigLoggingPerformance.fromJson(json['performance']),
api = VeilidWASMConfigLoggingApi.fromJson(json['api']);
}
class VeilidWASMConfig {
VeilidWASMConfigLogging logging;
VeilidWASMConfig({
required this.logging,
});
Map<String, dynamic> get json {
return {
'logging': logging.json,
};
}
VeilidWASMConfig.fromJson(dynamic json)
: logging = VeilidWASMConfigLogging.fromJson(json['logging']);
}
2022-02-07 02:18:42 +00:00
//////////////////////////////////////////////////////////
Veilid getVeilid() => VeilidJS();
2022-03-17 14:31:10 +00:00
Object wasm = js_util.getProperty(html.window, "veilid_wasm");
2022-03-16 03:02:24 +00:00
2022-03-17 14:31:10 +00:00
Future<T> _wrapApiPromise<T>(Object p) {
return js_util.promiseToFuture(p).then((value) => value as T).catchError(
(error) => Future<T>.error(
VeilidAPIException.fromJson(jsonDecode(error as String))));
2022-03-16 03:02:24 +00:00
}
2022-12-03 03:52:03 +00:00
class _Ctx {
final int id;
final VeilidJS js;
_Ctx(this.id, this.js);
}
2022-11-26 19:16:02 +00:00
// JS implementation of VeilidRoutingContext
class VeilidRoutingContextJS implements VeilidRoutingContext {
2022-12-03 03:52:03 +00:00
final _Ctx _ctx;
static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => {
js_util.callMethod(wasm, "release_routing_context", [ctx.id])
});
VeilidRoutingContextJS._(this._ctx) {
_finalizer.attach(this, _ctx, detach: this);
}
2022-11-26 19:16:02 +00:00
@override
2022-12-03 03:52:03 +00:00
VeilidRoutingContextJS withPrivacy() {
int newId =
js_util.callMethod(wasm, "routing_context_with_privacy", [_ctx.id]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
2022-11-26 19:16:02 +00:00
}
@override
2022-12-03 03:52:03 +00:00
VeilidRoutingContextJS withCustomPrivacy(Stability stability) {
final newId = js_util.callMethod(
wasm, "routing_context_with_custom_privacy", [_ctx.id, stability.json]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
2022-11-26 19:16:02 +00:00
}
@override
2022-12-03 03:52:03 +00:00
VeilidRoutingContextJS withSequencing(Sequencing sequencing) {
final newId = js_util.callMethod(
wasm, "routing_context_with_sequencing", [_ctx.id, sequencing.json]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
2022-11-26 19:16:02 +00:00
}
@override
Future<Uint8List> appCall(String target, Uint8List request) async {
2023-01-04 22:02:45 +00:00
var encodedRequest = base64UrlNoPadEncode(request);
2022-11-26 19:16:02 +00:00
2023-01-04 22:02:45 +00:00
return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod(
2023-03-01 20:50:30 +00:00
wasm, "routing_context_app_call", [_ctx.id, target, encodedRequest])));
2022-11-26 19:16:02 +00:00
}
@override
2022-12-29 03:53:58 +00:00
Future<void> appMessage(String target, Uint8List message) {
2023-01-04 22:02:45 +00:00
var encodedMessage = base64UrlNoPadEncode(message);
2022-11-26 19:16:02 +00:00
2023-03-01 20:50:30 +00:00
return _wrapApiPromise(js_util.callMethod(wasm,
"routing_context_app_message", [_ctx.id, target, encodedMessage]));
2022-11-26 19:16:02 +00:00
}
}
2022-12-29 03:53:58 +00:00
class _TDBT {
final int id;
VeilidTableDBJS tdbjs;
VeilidJS js;
_TDBT(this.id, this.tdbjs, this.js);
}
// JS implementation of VeilidTableDBTransaction
class VeilidTableDBTransactionJS extends VeilidTableDBTransaction {
final _TDBT _tdbt;
static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => {
js_util.callMethod(wasm, "release_table_db_transaction", [tdbt.id])
});
VeilidTableDBTransactionJS._(this._tdbt) {
_finalizer.attach(this, _tdbt, detach: this);
}
@override
Future<void> commit() {
2023-01-04 19:51:13 +00:00
return _wrapApiPromise(
js_util.callMethod(wasm, "table_db_transaction_commit", [_tdbt.id]));
2022-12-29 03:53:58 +00:00
}
@override
Future<void> rollback() {
2023-01-04 19:51:13 +00:00
return _wrapApiPromise(
js_util.callMethod(wasm, "table_db_transaction_rollback", [_tdbt.id]));
2022-12-29 03:53:58 +00:00
}
@override
Future<void> store(int col, Uint8List key, Uint8List value) {
2023-01-04 22:02:45 +00:00
final encodedKey = base64UrlNoPadEncode(key);
final encodedValue = base64UrlNoPadEncode(value);
2022-12-29 03:53:58 +00:00
2023-01-04 22:02:45 +00:00
return _wrapApiPromise(js_util.callMethod(
wasm,
"table_db_transaction_store",
[_tdbt.id, col, encodedKey, encodedValue]));
2022-12-29 03:53:58 +00:00
}
@override
Future<bool> delete(int col, Uint8List key) {
2023-01-04 22:02:45 +00:00
final encodedKey = base64UrlNoPadEncode(key);
2022-12-29 03:53:58 +00:00
return _wrapApiPromise(js_util.callMethod(
2023-01-04 22:02:45 +00:00
wasm, "table_db_transaction_delete", [_tdbt.id, col, encodedKey]));
2022-12-29 03:53:58 +00:00
}
}
class _TDB {
final int id;
VeilidJS js;
_TDB(this.id, this.js);
}
// JS implementation of VeilidTableDB
class VeilidTableDBJS extends VeilidTableDB {
final _TDB _tdb;
static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => {
js_util.callMethod(wasm, "release_table_db", [tdb.id])
});
VeilidTableDBJS._(this._tdb) {
_finalizer.attach(this, _tdb, detach: this);
}
@override
int getColumnCount() {
return js_util.callMethod(wasm, "table_db_get_column_count", [_tdb.id]);
}
@override
List<Uint8List> getKeys(int col) {
String? s = js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id, col]);
if (s == null) {
throw VeilidAPIExceptionInternal("No db for id");
}
List<dynamic> jarr = jsonDecode(s);
2023-01-04 22:02:45 +00:00
return jarr.map((e) => base64UrlNoPadDecode(e)).toList();
2022-12-29 03:53:58 +00:00
}
@override
VeilidTableDBTransaction transact() {
final id = js_util.callMethod(wasm, "table_db_transact", [_tdb.id]);
return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js));
}
@override
Future<void> store(int col, Uint8List key, Uint8List value) {
2023-01-04 22:02:45 +00:00
final encodedKey = base64UrlNoPadEncode(key);
final encodedValue = base64UrlNoPadEncode(value);
2022-12-29 03:53:58 +00:00
return _wrapApiPromise(js_util.callMethod(
2023-01-04 22:02:45 +00:00
wasm, "table_db_store", [_tdb.id, col, encodedKey, encodedValue]));
2022-12-29 03:53:58 +00:00
}
@override
Future<Uint8List?> load(int col, Uint8List key) async {
2023-01-04 22:02:45 +00:00
final encodedKey = base64UrlNoPadEncode(key);
2022-12-29 03:53:58 +00:00
2023-01-04 19:51:13 +00:00
String? out = await _wrapApiPromise(
2023-01-04 22:02:45 +00:00
js_util.callMethod(wasm, "table_db_load", [_tdb.id, col, encodedKey]));
2022-12-29 03:53:58 +00:00
if (out == null) {
return null;
}
2023-01-04 22:02:45 +00:00
return base64UrlNoPadDecode(out);
2022-12-29 03:53:58 +00:00
}
@override
Future<bool> delete(int col, Uint8List key) {
2023-01-04 22:02:45 +00:00
final encodedKey = base64UrlNoPadEncode(key);
2022-12-29 03:53:58 +00:00
2023-01-04 22:02:45 +00:00
return _wrapApiPromise(js_util
.callMethod(wasm, "table_db_delete", [_tdb.id, col, encodedKey]));
2022-12-29 03:53:58 +00:00
}
}
2022-11-26 19:16:02 +00:00
// JS implementation of high level Veilid API
2022-02-14 02:09:43 +00:00
class VeilidJS implements Veilid {
2022-06-16 01:51:38 +00:00
@override
2022-07-01 20:20:43 +00:00
void initializeVeilidCore(Map<String, dynamic> platformConfigJson) {
2022-06-16 01:51:38 +00:00
var platformConfigJsonString =
jsonEncode(platformConfigJson, toEncodable: veilidApiToEncodable);
2022-07-01 20:20:43 +00:00
js_util
.callMethod(wasm, "initialize_veilid_core", [platformConfigJsonString]);
2022-06-16 01:51:38 +00:00
}
2022-07-01 16:13:52 +00:00
@override
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) {
var logLevelJsonString =
jsonEncode(logLevel.json, toEncodable: veilidApiToEncodable);
js_util.callMethod(wasm, "change_log_level", [layer, logLevelJsonString]);
}
2022-03-04 01:45:39 +00:00
@override
2022-09-09 20:27:13 +00:00
Future<Stream<VeilidUpdate>> startupVeilidCore(VeilidConfig config) async {
2022-03-16 03:02:24 +00:00
var streamController = StreamController<VeilidUpdate>();
2022-03-17 14:31:10 +00:00
updateCallback(String update) {
var updateJson = jsonDecode(update);
if (updateJson["kind"] == "Shutdown") {
streamController.close();
} else {
var update = VeilidUpdate.fromJson(updateJson);
streamController.add(update);
}
}
await _wrapApiPromise(js_util.callMethod(wasm, "startup_veilid_core", [
js.allowInterop(updateCallback),
jsonEncode(config.json, toEncodable: veilidApiToEncodable)
]));
2022-09-09 20:27:13 +00:00
return streamController.stream;
2022-02-07 02:18:42 +00:00
}
2022-03-04 01:45:39 +00:00
@override
2022-03-16 03:02:24 +00:00
Future<VeilidState> getVeilidState() async {
2022-03-17 14:31:10 +00:00
return VeilidState.fromJson(jsonDecode(await _wrapApiPromise(
js_util.callMethod(wasm, "get_veilid_state", []))));
2022-02-07 02:18:42 +00:00
}
2022-09-06 22:59:41 +00:00
@override
2022-12-29 03:53:58 +00:00
Future<void> attach() {
2022-09-06 22:59:41 +00:00
return _wrapApiPromise(js_util.callMethod(wasm, "attach", []));
}
@override
2022-12-29 03:53:58 +00:00
Future<void> detach() {
2022-09-06 22:59:41 +00:00
return _wrapApiPromise(js_util.callMethod(wasm, "detach", []));
}
2022-03-04 01:45:39 +00:00
@override
2022-03-17 14:31:10 +00:00
Future<void> shutdownVeilidCore() {
return _wrapApiPromise(
js_util.callMethod(wasm, "shutdown_veilid_core", []));
2022-02-07 02:18:42 +00:00
}
2022-03-04 01:45:39 +00:00
@override
2022-11-26 19:16:02 +00:00
Future<VeilidRoutingContext> routingContext() async {
2023-01-04 19:20:41 +00:00
int id =
await _wrapApiPromise(js_util.callMethod(wasm, "routing_context", []));
2022-12-03 03:52:03 +00:00
return VeilidRoutingContextJS._(_Ctx(id, this));
2022-11-26 19:16:02 +00:00
}
@override
2023-03-01 20:50:30 +00:00
Future<RouteBlob> newPrivateRoute() async {
2022-12-03 03:52:03 +00:00
Map<String, dynamic> blobJson = jsonDecode(await _wrapApiPromise(
js_util.callMethod(wasm, "new_private_route", [])));
2023-03-01 20:50:30 +00:00
return RouteBlob.fromJson(blobJson);
2022-11-26 19:16:02 +00:00
}
@override
2023-03-01 20:50:30 +00:00
Future<RouteBlob> newCustomPrivateRoute(
2022-11-26 19:16:02 +00:00
Stability stability, Sequencing sequencing) async {
2022-12-03 03:52:03 +00:00
var stabilityString =
jsonEncode(stability, toEncodable: veilidApiToEncodable);
var sequencingString =
jsonEncode(sequencing, toEncodable: veilidApiToEncodable);
2022-11-26 19:16:02 +00:00
2022-12-03 03:52:03 +00:00
Map<String, dynamic> blobJson = jsonDecode(await _wrapApiPromise(js_util
.callMethod(
wasm, "new_private_route", [stabilityString, sequencingString])));
2023-03-01 20:50:30 +00:00
return RouteBlob.fromJson(blobJson);
2022-11-26 19:16:02 +00:00
}
@override
2022-12-29 03:53:58 +00:00
Future<String> importRemotePrivateRoute(Uint8List blob) {
2023-01-04 22:02:45 +00:00
var encodedBlob = base64UrlNoPadEncode(blob);
2022-11-26 19:16:02 +00:00
return _wrapApiPromise(
js_util.callMethod(wasm, "import_remote_private_route", [encodedBlob]));
}
@override
2022-12-29 03:53:58 +00:00
Future<void> releasePrivateRoute(String key) {
2022-11-26 19:16:02 +00:00
return _wrapApiPromise(
js_util.callMethod(wasm, "release_private_route", [key]));
2022-03-04 01:45:39 +00:00
}
2022-10-01 02:37:55 +00:00
@override
Future<void> appCallReply(String id, Uint8List message) {
2023-01-04 22:02:45 +00:00
var encodedMessage = base64UrlNoPadEncode(message);
2022-10-01 02:37:55 +00:00
return _wrapApiPromise(
js_util.callMethod(wasm, "app_call_reply", [id, encodedMessage]));
}
2022-12-03 03:52:03 +00:00
2022-12-29 03:53:58 +00:00
@override
Future<VeilidTableDB> openTableDB(String name, int columnCount) async {
int id = await _wrapApiPromise(
js_util.callMethod(wasm, "open_table_db", [name, columnCount]));
return VeilidTableDBJS._(_TDB(id, this));
}
@override
Future<bool> deleteTableDB(String name) {
return _wrapApiPromise(js_util.callMethod(wasm, "delete_table_db", [name]));
}
2022-11-26 19:16:02 +00:00
@override
2022-12-11 00:11:58 +00:00
Future<String> debug(String command) async {
2023-01-04 19:20:41 +00:00
return await _wrapApiPromise(js_util.callMethod(wasm, "debug", [command]));
2022-11-26 19:16:02 +00:00
}
2022-10-01 02:37:55 +00:00
2022-03-04 01:45:39 +00:00
@override
2022-03-17 14:31:10 +00:00
String veilidVersionString() {
return js_util.callMethod(wasm, "veilid_version_string", []);
2022-02-07 02:18:42 +00:00
}
2022-03-04 01:45:39 +00:00
@override
2022-03-17 14:31:10 +00:00
VeilidVersion veilidVersion() {
2022-12-03 03:52:03 +00:00
Map<String, dynamic> jsonVersion =
2022-03-17 14:31:10 +00:00
jsonDecode(js_util.callMethod(wasm, "veilid_version", []));
2022-03-16 03:02:24 +00:00
return VeilidVersion(
jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]);
2022-02-07 02:18:42 +00:00
}
}