From 218a16685b79cf3f1af8af5f39b1e37b4849c6b9 Mon Sep 17 00:00:00 2001 From: hunghd Date: Thu, 1 Aug 2024 15:20:05 +0700 Subject: [PATCH 1/2] migrate to package:web and dart:js_interop --- example/pubspec.lock | 10 +++++- example/web/index.html | 12 +------ lib/web/rsa_web.dart | 71 ++++++++++++++++++++++++++++++++---------- pubspec.yaml | 3 +- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index e054384..da5e34a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -491,6 +491,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + web: + dependency: transitive + description: + name: web + sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 + url: "https://pub.dev" + source: hosted + version: "1.0.0" web_socket_channel: dependency: transitive description: @@ -524,5 +532,5 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=3.3.0 <4.0.0" + dart: ">=3.4.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/example/web/index.html b/example/web/index.html index d650fa8..74dcd35 100644 --- a/example/web/index.html +++ b/example/web/index.html @@ -30,16 +30,6 @@ - - - + diff --git a/lib/web/rsa_web.dart b/lib/web/rsa_web.dart index 6f73db9..55d88a9 100644 --- a/lib/web/rsa_web.dart +++ b/lib/web/rsa_web.dart @@ -1,21 +1,24 @@ import 'dart:async'; -import 'dart:collection'; -import 'dart:html'; +import 'dart:js_interop'; import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; +import 'package:web/web.dart'; class FastRsaPlugin { var _counter = 0; - Worker worker = new Worker('assets/packages/fast_rsa/web/assets/worker.js'); + Worker worker = Worker( + 'assets/packages/fast_rsa/web/assets/worker.js'.toJS, + ); Map> completers = {}; static void registerWith(Registrar registrar) { final MethodChannel channel = MethodChannel( - 'fast_rsa', - const StandardMethodCodec(), - // ignore: deprecated_member_use - registrar.messenger); + 'fast_rsa', + const StandardMethodCodec(), + // ignore: deprecated_member_use + registrar.messenger, + ); final FastRsaPlugin instance = FastRsaPlugin(); instance.listen(); channel.setMethodCallHandler(instance.handleMethodCall); @@ -26,19 +29,23 @@ class FastRsaPlugin { } void listen() async { - worker.onMessage.listen((event) { - LinkedHashMap data = event.data; - var completer = completers[data['id']]; + void _onMessage(Event event) { + final msgEvent = event as MessageEvent; + final data = msgEvent.data as RsaResponse; + var completer = completers[data.id]; if (completer == null) { return; } - if (data['error'] != null && data['error'] != "") { - completer.completeError(data['error']); + if (data.error != null && data.error! != '') { + completer.completeError(data.error!); } else { - completer.complete(data['response']); + completer.complete(data.response?.toDart); } - completers.remove(data['id']); - }); + completers.remove(data.id); + } + + worker.onmessage = _onMessage.toJS; + // worker.addEventListener('message', _onMessage.toJS); } Future bridgeCall(String name, Uint8List? /*!*/ request) async { @@ -46,7 +53,39 @@ class FastRsaPlugin { var id = _counter.toString(); var completer = new Completer(); completers[id] = completer; - worker.postMessage({'id': id, 'name': name, 'request': request}); + worker.postMessage(RsaRequest( + id: id, + name: name, + request: request?.toJS, + )); return completer.future; } } + +@JS() +@anonymous +extension type RsaRequest._(JSObject _) implements JSObject { + external String id; + external String name; + external JSUint8Array? request; + + external RsaRequest({ + String id, + String name, + JSUint8Array? request, + }); +} + +@JS() +@anonymous +extension type RsaResponse._(JSObject _) implements JSObject { + external String id; + external String? error; + external JSUint8Array? response; + + external RsaResponse({ + String id, + String? error, + JSUint8Array? response, + }); +} diff --git a/pubspec.yaml b/pubspec.yaml index 2e93409..38634d9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 3.6.3 homepage: https://github.com/jerson/flutter-rsa environment: - sdk: '>=2.17.0 <3.0.0' + sdk: '>=3.3.0 <4.0.0' flutter: ">=1.10.0" false_secrets: @@ -18,6 +18,7 @@ dependencies: ffi: ^2.0.1 flat_buffers: ^2.0.5 path: ^1.8.2 + web: ">=0.5.0 <2.0.0" dev_dependencies: flutter_test: From 75e4306ce9a56e1ebe462a477f038e0d345b0426 Mon Sep 17 00:00:00 2001 From: Gerson Alexander Pardo Gamez Date: Mon, 5 Aug 2024 15:12:37 -0500 Subject: [PATCH 2/2] chore: add final to ffi.dart --- lib/bridge/ffi.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bridge/ffi.dart b/lib/bridge/ffi.dart index 101165c..8c71685 100644 --- a/lib/bridge/ffi.dart +++ b/lib/bridge/ffi.dart @@ -12,7 +12,7 @@ typedef Call = ffi.Pointer Function( int, ); -class FFIBytesReturn extends ffi.Struct { +final class FFIBytesReturn extends ffi.Struct { external ffi.Pointer message; @ffi.Int32()