Skip to content

Commit

Permalink
Added fallback option when instantiateStreaming is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
jerson committed Apr 16, 2022
1 parent 90b6ee9 commit 3f639d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.8

- Added fallback option when instantiateStreaming is not available

## 3.2.7

- Updated binaries to 1.3.4
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.2.7"
version: "3.2.8"
package_config:
dependency: transitive
description:
Expand Down
34 changes: 33 additions & 1 deletion lib/web/assets/worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
self.importScripts("wasm_exec.js");

self.loaded = false;
self.library = "openpgp.wasm";

load = () => {

if (!WebAssembly.hasOwnProperty('instantiateStreaming')){
return loadFallback();
}

const go = new Go();
let mod, inst;
return WebAssembly.instantiateStreaming(
fetch("openpgp.wasm"),
fetch(self.library),
go.importObject
).then(async (result) => {
mod = result.module;
Expand All @@ -25,6 +32,31 @@ load = () => {
});
};

loadFallback = () => {
const go = new Go();
let mod, inst;
return fetch(self.library).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, go.importObject)
).then(async (result) => {
mod = result.module;
inst = result.instance;
const run = async () => {
try {
self.loaded = true;
await go.run(inst);
self.loaded = false;
} catch (e) {
console.warn(e);
self.loaded = false;
loadFallback();
}
};
run();
});
};

onmessage = async ({ data }) => {
if (!self.loaded) {
await load();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: openpgp
description: library for use OpenPGP with support for android and ios, macOS, linux, windows, web and hover
version: 3.2.7
version: 3.2.8
homepage: https://github.com/jerson/flutter-openpgp

environment:
Expand Down

0 comments on commit 3f639d7

Please sign in to comment.