@@ -27,16 +27,12 @@ class DArgon2FlutterWeb extends DArgon2Platform {
27
27
28
28
/// The static hashwasm URL to use when getting the script.
29
29
static const hashwasm =
30
- "https://cdn.jsdelivr.net/npm/hash-wasm@4.8 .0/dist/argon2.umd.min " ;
30
+ "https://cdn.jsdelivr.net/npm/hash-wasm@4.9 .0/+esm " ;
31
31
32
32
/// The [DArgon2FlutterWeb] constructor. Needed to set the global hashwasm
33
33
/// variable to the right script.
34
34
DArgon2FlutterWeb () {
35
- if (context['require' ] != null ) {
36
- _registerRequire ();
37
- } else {
38
- _registerNormal ();
39
- }
35
+ _registerDependency ();
40
36
}
41
37
42
38
@override
@@ -110,52 +106,46 @@ class DArgon2FlutterWeb extends DArgon2Platform {
110
106
return current;
111
107
}
112
108
113
- /// Registers the hashwasm argon2 implementation in release or script-loaded
114
- /// environments by adding a script tag with the dependency.
115
- void _registerNormal () async {
116
- print ("NORMAL" );
117
- // Create the script element
118
- ScriptElement script = ScriptElement ();
119
- script.type = "text/javascript" ;
120
- script.charset = "utf-8" ;
121
- script.async = true ;
122
- script.src = "$hashwasm .js" ;
123
- // Add it to the document head
124
- assert (document.head != null );
125
- document.head! .append (script);
126
- // await its load
127
- await script.onLoad.first;
128
- }
129
-
130
109
/// Registers the hashwasm argon2 implementation in debug or requirejs loaded
131
110
/// environments as adding the script tag would not suffice then.
132
111
///
133
112
/// Adds it as a require.js dependency and sets the global hashwasm variable
134
113
/// to the require app.
135
- void _registerRequire () async {
114
+ void _registerDependency () async {
136
115
// Make sure it's not already there
137
116
if (context['hashwasm' ] != null ) return ;
138
- // Get the require object
139
- JsObject require = JsObject .fromBrowserObject (context['require' ]);
140
- // Add the script to the config
141
- require.callMethod ('config' , [
142
- JsObject .jsify ({
143
- 'paths' : {"hashwasm" : hashwasm}
144
- })
145
- ]);
117
+
118
+ String windowVar = "hashwasm" ;
119
+
120
+ ScriptElement script = ScriptElement ();
121
+ script.type = 'text/javascript' ;
122
+ script.crossOrigin = 'anonymous' ;
123
+ script.text = '''
124
+ window.trigger_$windowVar = async (callback) => {
125
+ let {argon2i, argon2d, argon2id} = await import($hashwasm );
126
+ callback([argon2i, argon2d, argon2id]);
127
+ };
128
+ ''' ;
129
+
130
+ assert (document.head != null );
131
+ document.head! .append (script);
146
132
Completer completer = Completer ();
147
- List <String > services = ['hashwasm' ];
148
- // Load the script
149
- context.callMethod ('require' , [
150
- JsObject .jsify (services),
151
- (app) {
152
- // Set it to the global variable
153
- context['hashwasm' ] = app;
133
+
134
+ context.callMethod ('trigger_$windowVar ' , [
135
+ (module) {
136
+ context[windowVar] = JsObject .jsify ({
137
+ "argon2i" : module[0 ],
138
+ "argon2d" : module[1 ],
139
+ "argon2id" : module[2 ],
140
+ });
141
+ context.deleteProperty ('trigger_$windowVar ' );
154
142
completer.complete ();
155
143
}
156
144
]);
145
+
157
146
await completer.future;
158
147
}
148
+
159
149
}
160
150
161
151
/// JS interop binding to call the hashwasm argon2i hash function.
0 commit comments