veilid/veilid-flutter/lib/veilid_js.dart

200 lines
6.1 KiB
Dart
Raw Normal View History

2022-02-06 20:18:42 -06:00
import 'veilid.dart';
2022-03-17 09:31:10 -05:00
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:js_util' as js_util;
2022-02-06 20:18:42 -06:00
import 'dart:async';
2022-03-15 22:02:24 -05:00
import 'dart:convert';
2022-09-30 21:37:55 -05:00
import 'dart:typed_data';
2022-02-06 20:18:42 -06:00
//////////////////////////////////////////////////////////
Veilid getVeilid() => VeilidJS();
2022-03-17 09:31:10 -05:00
Object wasm = js_util.getProperty(html.window, "veilid_wasm");
2022-03-15 22:02:24 -05:00
2022-03-17 09:31:10 -05: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-15 22:02:24 -05:00
}
2022-11-26 13:16:02 -06:00
// JS implementation of VeilidRoutingContext
class VeilidRoutingContextJS implements VeilidRoutingContext {
final int _id;
final VeilidFFI _ffi;
VeilidRoutingContextFFI._(this._id, this._ffi);
@override
VeilidRoutingContextFFI withPrivacy() {
final newId = _ffi._routingContextWithPrivacy(_id);
return VeilidRoutingContextFFI._(newId, _ffi);
}
@override
VeilidRoutingContextFFI withCustomPrivacy(Stability stability) {
final newId = _ffi._routingContextWithCustomPrivacy(
_id, stability.json.toNativeUtf8());
return VeilidRoutingContextFFI._(newId, _ffi);
}
@override
VeilidRoutingContextFFI withSequencing(Sequencing sequencing) {
final newId =
_ffi._routingContextWithSequencing(_id, sequencing.json.toNativeUtf8());
return VeilidRoutingContextFFI._(newId, _ffi);
}
@override
Future<Uint8List> appCall(String target, Uint8List request) async {
var nativeEncodedTarget = target.toNativeUtf8();
var nativeEncodedRequest = base64UrlEncode(request).toNativeUtf8();
final recvPort = ReceivePort("routing_context_app_call");
final sendPort = recvPort.sendPort;
_ffi._routingContextAppCall(
sendPort.nativePort, _id, nativeEncodedTarget, nativeEncodedRequest);
final out = await processFuturePlain(recvPort.first);
return base64Decode(out);
}
@override
Future<void> appMessage(String target, Uint8List message) async {
var nativeEncodedTarget = target.toNativeUtf8();
var nativeEncodedMessage = base64UrlEncode(message).toNativeUtf8();
final recvPort = ReceivePort("routing_context_app_call");
final sendPort = recvPort.sendPort;
_ffi._routingContextAppCall(
sendPort.nativePort, _id, nativeEncodedTarget, nativeEncodedMessage);
return processFutureVoid(recvPort.first);
}
}
// JS implementation of high level Veilid API
2022-02-13 20:09:43 -06:00
class VeilidJS implements Veilid {
2022-06-15 20:51:38 -05:00
@override
2022-07-01 15:20:43 -05:00
void initializeVeilidCore(Map<String, dynamic> platformConfigJson) {
2022-06-15 20:51:38 -05:00
var platformConfigJsonString =
jsonEncode(platformConfigJson, toEncodable: veilidApiToEncodable);
2022-07-01 15:20:43 -05:00
js_util
.callMethod(wasm, "initialize_veilid_core", [platformConfigJsonString]);
2022-06-15 20:51:38 -05:00
}
2022-07-01 11:13:52 -05: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-03 19:45:39 -06:00
@override
2022-09-09 15:27:13 -05:00
Future<Stream<VeilidUpdate>> startupVeilidCore(VeilidConfig config) async {
2022-03-15 22:02:24 -05:00
var streamController = StreamController<VeilidUpdate>();
2022-03-17 09:31:10 -05: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 15:27:13 -05:00
return streamController.stream;
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-03-15 22:02:24 -05:00
Future<VeilidState> getVeilidState() async {
2022-03-17 09:31:10 -05:00
return VeilidState.fromJson(jsonDecode(await _wrapApiPromise(
js_util.callMethod(wasm, "get_veilid_state", []))));
2022-02-06 20:18:42 -06:00
}
2022-09-06 17:59:41 -05:00
@override
Future<void> attach() async {
return _wrapApiPromise(js_util.callMethod(wasm, "attach", []));
}
@override
Future<void> detach() async {
return _wrapApiPromise(js_util.callMethod(wasm, "detach", []));
}
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
Future<void> shutdownVeilidCore() {
return _wrapApiPromise(
js_util.callMethod(wasm, "shutdown_veilid_core", []));
2022-02-06 20:18:42 -06:00
}
2022-11-26 13:16:02 -06:00
2022-03-03 19:45:39 -06:00
@override
2022-11-26 13:16:02 -06:00
Future<VeilidRoutingContext> routingContext() async {
final recvPort = ReceivePort("routing_context");
final sendPort = recvPort.sendPort;
_routingContext(sendPort.nativePort);
final id = await processFuturePlain(recvPort.first);
return VeilidRoutingContextFFI._(id, this);
}
@override
Future<KeyBlob> newPrivateRoute() async {
final recvPort = ReceivePort("new_private_route");
final sendPort = recvPort.sendPort;
_newPrivateRoute(sendPort.nativePort);
return processFutureJson(KeyBlob.fromJson, recvPort.first);
}
@override
Future<KeyBlob> newCustomPrivateRoute(
Stability stability, Sequencing sequencing) async {
return _wrapApiPromise(
js_util.callMethod(wasm, "new_custom_private_route", [stability, sequencing]));
}
@override
Future<String> importRemotePrivateRoute(Uint8List blob) async {
var encodedBlob = base64UrlEncode(blob);
return _wrapApiPromise(
js_util.callMethod(wasm, "import_remote_private_route", [encodedBlob]));
}
@override
Future<void> releasePrivateRoute(String key) async {
return _wrapApiPromise(
js_util.callMethod(wasm, "release_private_route", [key]));
2022-03-03 19:45:39 -06:00
}
2022-09-30 21:37:55 -05:00
@override
Future<void> appCallReply(String id, Uint8List message) {
var encodedMessage = base64UrlEncode(message);
return _wrapApiPromise(
js_util.callMethod(wasm, "app_call_reply", [id, encodedMessage]));
}
2022-11-26 13:16:02 -06:00
@override
Future<String> debug(String command) {
return _wrapApiPromise(js_util.callMethod(wasm, "debug", [command]));
}
2022-09-30 21:37:55 -05:00
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
String veilidVersionString() {
return js_util.callMethod(wasm, "veilid_version_string", []);
2022-02-06 20:18:42 -06:00
}
2022-03-03 19:45:39 -06:00
@override
2022-03-17 09:31:10 -05:00
VeilidVersion veilidVersion() {
var jsonVersion =
jsonDecode(js_util.callMethod(wasm, "veilid_version", []));
2022-03-15 22:02:24 -05:00
return VeilidVersion(
jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]);
2022-02-06 20:18:42 -06:00
}
}