Skip to content

Migrate dart:html and package:js to package:web and dart:js_interop #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2024
Merged

Conversation

svenopdehipt
Copy link

Migrate packages to support wasm in the next stable release.
Removed unused code in subtle.dart and update dependencies for the example project.
I have also used the new index.html structure and tested it wasm on the current beta channel, but I have reverted the index.html because it doesn't work with the current stable release.

@juliansteenbakker
Copy link
Owner

Hi all, i am working on integrating #680, so this should not be needed anymore. Thank you anyway!

@svenopdehipt
Copy link
Author

#680 does still use package:js and not dart:js_interop

@svenopdehipt svenopdehipt changed the title Migrate dart:html and package:js to package:web and darts_interop Migrate dart:html and package:js to package:web and dart:js_interop May 6, 2024
Copy link
Contributor

@koji-1009 koji-1009 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good PR! I hope this is helpful.

encryptionKey = await js_util.promiseToFuture<html.CryptoKey>(
crypto.importKey("raw", jwk, algorithm, false, ["encrypt", "decrypt"]),
);
encryptionKey = await crypto.importKey("raw", jwk.toJS, algorithm, false, ["encrypt".toJS, "decrypt".toJS].toJS).toDart;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a private extension for List<String> seemed to increase readability. Please consider it if you like.

extension on List<String> {
  JSArray<JSString> get toJS => [
        ...map((e) => e.toJS),
      ].toJS;
}

@@ -130,26 +127,24 @@ class FlutterSecureStorageWeb extends FlutterSecureStoragePlatform {
required Map<String, String> options,
}) async {
final iv =
html.window.crypto!.getRandomValues(Uint8List(12)).buffer.asUint8List();
(web.window.crypto.getRandomValues(Uint8List(12).toJS) as js_interop.JSUint8Array).toDart.buffer.asUint8List();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since getRandomValues updates its arguments, the following implementation would reduce the toDart and toJS conversions.

final iv = Uint8List(12);
web.window.crypto.getRandomValues(iv.toJS);

https://github.com/google/webcrypto.dart/blob/232f5d6faeeefe232726f1a2938df695ad16406b/test/crypto_subtle_test.dart#L64

}

return map;
}

crypto.Algorithm _getAlgorithm(Uint8List iv) =>
crypto.Algorithm(name: 'AES-GCM', length: 256, iv: iv);
js_interop.JSObject _getAlgorithm(Uint8List iv) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying JSAny instead of JSObject may reduce type conversion.

https://github.com/dart-lang/web/blob/v0.5.1/lib/src/dom/webcryptoapi.dart

@svenopdehipt
Copy link
Author

I have updated the code with the changes provided above

@svenopdehipt
Copy link
Author

I have just tested all the changed code and noticed that the get random value does work for javascript but does not work for wasm. I will revert this change.

@koji-1009
Copy link
Contributor

koji-1009 commented May 6, 2024

Thanks! I understand that package:web completely replaces package:js, so I think this change will be necessary.

Since package:web ^0.5.0 requires Dart 3.3, it may be a good idea to update the package required dart version. However, in applications that do not use web, this change may seem disruptive.
Nevertheless, plus_package has already been changed to require Dart 3.3 or higher. I would be happy for maintainers to consider this.

fluttercommunity/plus_plugins#2653

@svenopdehipt
Copy link
Author

I just noticed that the jsonwebkey file isn't used anymore and could also be deleted. Should I delete this?

@paulking86
Copy link

paulking86 commented May 9, 2024

@svenopdehipt - Thanks for your work on this! Is there any chance you can pull develop into this branch (or rebase) to resolve merge conflicts? 🙏

@svenopdehipt
Copy link
Author

I have rebased the branch with the develop branch of your repo.

@paulking86
Copy link

Rapid work @svenopdehipt - Thank you!

My only thoughts now are relating to @koji-1009's comment above. Should our pubspec.yaml now look like this instead?

environment:
  sdk: ">=3.3.0 <4.0.0"
  flutter: ">=3.19.0"

@svenopdehipt
Copy link
Author

svenopdehipt commented May 9, 2024

I do think that it would make sense to update the pubspec.yaml, because older flutter and dart versions aren't supported anyway due though the package:web dependency.

@paulking86
Copy link

Keen to hear your thoughts @juliansteenbakker, as I know you were discussing this in #680!

@paulking86
Copy link

Bump. Any updates on this?

@@ -13,6 +13,7 @@ dependencies:
flutter_secure_storage_platform_interface: ^1.1.1
flutter_web_plugins:
sdk: flutter
web: '>=0.5.0 <1.0.0'
js: ^0.6.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is js still a dependency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw, that the file you reference is outdated. Please look at the changes now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you changed it in the last commit: c9f2b62#diff-dd1ec4717f1b8774ed50d5be49319e0a3e9976c07f16f133ac101a5f07066a3e (where you added it), where you removed it: dd4d5d0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am resolving some merge conflicts due to the recent messy revert of some breaking changes, it won't be added when i merge this.

@juliansteenbakker
Copy link
Owner

Seems all good! So what i'm gonna do is merge this, change the version constraints of flutter, and release it under a new major version. I created a separate branch where we can continue support for the current version 9, because the sudden change in flutter versions will require lots of people to update flutter, which can be a hassle in some projects.

@juliansteenbakker juliansteenbakker merged commit d484bdf into juliansteenbakker:develop May 15, 2024
0 of 3 checks passed
@juliansteenbakker
Copy link
Owner

So, because these changes require a recent version of flutter, i have decided to make it an opt-in.
I have publish v2.0.0-beta.1 of the flutter_secure_storage_web package, which you can use by setting the following line in your pubspec.yaml:

dependency_overrides:
  flutter_secure_storage_web: ^2.0.0-beta.1

I have also added this in the readme, so users can decide of themself to use this version, or stick with the old implementation which doesn't require the latest flutter version.

@JgomesAT
Copy link

JgomesAT commented Aug 1, 2024

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants