Skip to content

Commit

Permalink
Merge pull request #1 from linera-io/04-17-Messaging
Browse files Browse the repository at this point in the history
Add a messaging pathway between the Web page and the Wasm backend
  • Loading branch information
Twey authored Apr 19, 2024
2 parents c5fed4d + 3d67204 commit 2b9ec1d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 84 deletions.
48 changes: 2 additions & 46 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
#!/bin/sh

command -v cargo >/dev/null 2>&1 || { echo >&2 "Installing cargo..."; curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh; }
command -v wasm-pack >/dev/null 2>&1 || { echo >&2 "Installing wasm-pack..."; cargo install wasm-pack; }

rm -rf pkg

# Find value of argument --manifest-version
for arg in "$@"; do
case "$arg" in
--manifest-version=*)
manifest_version="${arg#*=}"
;;
esac
done

# Find whether release was passed
for arg in "$@"; do
case "$arg" in
--release)
release=true
;;
esac
done

# Build for release or debug
if [ "$release" = true ]; then
wasm-pack build --target=no-modules --release || exit 1
else
wasm-pack build --target=no-modules --dev || exit 1
fi

# Copy manifest.json to pkg
if [ "$manifest_version" = "v3" ] || [ "$manifest_version" = "3" ]; then
cp manifest_v3.json pkg/manifest.json
else if [ "$manifest_version" = "v2" ] || [ "$manifest_version" = "2" ]; then
cp manifest_v2.json pkg/manifest.json
else
echo "Packaging with manifest version v2. Pass --manifest-version=v3 to package with manifest version 3."
cp manifest_v2.json pkg/manifest.json
fi
fi

printf "
const runtime = chrome.runtime || browser.runtime;
async function run() {
await wasm_bindgen(runtime.getURL('linera_web_bg.wasm'));
}
wasm-pack build --target=web "$@"

run();
" >> pkg/run_wasm.js
ln js/* manifest.json pkg/
10 changes: 10 additions & 0 deletions js/content_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function respond(id, message) {
window.dispatchEvent(new CustomEvent("linera-wallet-response", {
detail: { id, message },
}))
}

window.addEventListener("linera-wallet-request", async e =>
respond(e.detail.id, await chrome.runtime.sendMessage(e.detail.message)));

window.dispatchEvent(new Event("linera-wallet-loaded"));
12 changes: 12 additions & 0 deletions js/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import init from './linera_web.js';
import * as wasm from './linera_web.js';

const runtime = chrome.runtime || browser.runtime;

(async () => {
await init(runtime.getURL('linera_web_bg.wasm'));
runtime.onMessage.addListener((message, _sender, respond) => {
wasm[message.function].call(null, message.arguments).then(respond);
return true;
});
})();
20 changes: 20 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name" : "linera-web",
"version" : "1.0",
"description" : "linera-web",
"permissions": [],
"content_scripts": [
{
"matches": ["file:///*"],
"js": ["content_script.js"]
}
],
"background": {
"service_worker": "service_worker.js",
"type": "module"
},
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval';"
},
"manifest_version": 3
}
18 changes: 0 additions & 18 deletions manifest_v2.json

This file was deleted.

19 changes: 0 additions & 19 deletions manifest_v3.json

This file was deleted.

19 changes: 19 additions & 0 deletions sample-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<script src="linera.js"></script>
<script>
async function foo() {
const response = await Linera.sendRequest({
'function': 'query',
'arguments': [3],
});
console.log('response: ', response);
}

foo();
</script>
</head>
<body>
</body>
</html>
36 changes: 36 additions & 0 deletions sample-app/linera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Linera = (() => {
let loaded = new Promise(resolve => {
const listener = window.addEventListener("linera-wallet-loaded", () => {
resolve();
window.removeEventListener("linera-wallet-loaded", listener);
});
});

window.addEventListener("linera-wallet-response", e => {
responses.get(e.detail.id)?.(e.detail.message);
return false;
});

let nextMessageId = 0;
let responses = new Map();

return {
load: async () => await loaded,
sendRequest: async request => {
await loaded;

return new Promise(resolve => {
responses.set(nextMessageId, resolve);
window.dispatchEvent(new CustomEvent(
"linera-wallet-request",
{
detail: {
id: nextMessageId,
message: request,
},
},
));
});
},
};
})();
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use web_sys::*;
#[macro_use]
mod util;

#[wasm_bindgen]
pub async fn query(n: u32) -> u32 {
n + 1
}

#[wasm_bindgen(start)]
pub async fn main() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));

log!("Hello World!");
}
}

0 comments on commit 2b9ec1d

Please sign in to comment.