Skip to content

Commit

Permalink
feat(node-wasm)!: Webpack compatibility (#377)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikołaj Florkiewicz <[email protected]>
Co-authored-by: Maciej Zwoliński <[email protected]>
  • Loading branch information
fl0rek and zvolin authored Sep 19, 2024
1 parent 36f1ceb commit dae322c
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 566 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ web-sys = { version = "0.3.70", features = [
"BroadcastChannel",
"DedicatedWorkerGlobalScope",
"Headers",
"MessageChannel",
"MessageEvent",
"MessagePort",
"Navigator",
Expand All @@ -70,6 +71,9 @@ web-sys = { version = "0.3.70", features = [
"WorkerType",
] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.42"

[package.metadata.docs.rs]
targets = ["wasm32-unknown-unknown"]

Expand Down
34 changes: 30 additions & 4 deletions node-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,40 @@
A compatibility layer for the [`Lumina`](https://github.com/eigerco/lumina) node to
work within a browser environment and be operable with javascript.

# Example
Starting lumina inside a dedicated worker

```javascript
import init, { Node, NodeConfig, Network } from "/wasm/lumina_node_wasm.js";
import { spawnNode, NodeConfig, Network } from "lumina-node";

await init();
const node = await spawnNode();
const mainnetConfig = NodeConfig.default(Network.Mainnet);

const config = NodeConfig.default(Network.Mainnet);
const node = await new Node(config);
await node.start(mainnetConfig);

await node.wait_connected();
await node.request_head_header();
```

## Manual setup

Note that `spawnNode` implicitly calls wasm initialisation code. If you want to set things up manually, make sure to call the default export before using any of the wasm functionality.

```javascript
import init, { NodeConfig, Network } from "lumina-node";

await init();
const config = NodeConfig.default(Network.Mainnet);

// client and worker accept any object with MessagePort like interface e.g. Worker
const channel = new MessageChannel();
const worker = new NodeWorker(channel.port1);

// note that this runs lumina in the current context (and doesn't create a new web-worker). Promise created with `.run()` never completes.
const worker_promise = worker.run();

// client port can be used locally or transferred like any plain MessagePort
const client = await new NodeClient(channel.port2);
await client.wait_connected();
await client.request_head_header();
```
1 change: 1 addition & 0 deletions node-wasm/js/README.md
6 changes: 6 additions & 0 deletions node-wasm/js/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Spawn a worker running lumina node and get the `NodeClient` connected to it.
*/
export function spawnNode(): Promise<NodeClient>;
export * from "lumina-node-wasm";
export default function init(): Promise<void>;
14 changes: 14 additions & 0 deletions node-wasm/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import init, { NodeClient } from "lumina-node-wasm"

/**
* Spawn a worker running lumina node and get the `NodeClient` connected to it.
*/
export async function spawnNode() {
await init();
let worker = new Worker(new URL("worker.js", import.meta.url));
let client = await new NodeClient(worker);
return client;
}

export * from "lumina-node-wasm";
export default init;
31 changes: 31 additions & 0 deletions node-wasm/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "lumina-node",
"type": "module",
"collaborators": [
"Eiger <[email protected]>"
],
"description": "Lumina node for Celestia, running in browser",
"version": "0.2.0",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/eigerco/lumina.git"
},
"files": [
"index.js",
"index.d.ts",
"worker.js"
],
"main": "index.js",
"homepage": "https://www.eiger.co",
"dependencies": {
"lumina-node-wasm": "0.2.0"
},
"keywords": [
"blockchain",
"celestia",
"lumina",
"node",
"browser"
]
}
10 changes: 10 additions & 0 deletions node-wasm/js/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import init, { NodeWorker, NodeClient } from "lumina-node-wasm"

Error.stackTraceLimit = 99;

init().then(async () => {
let worker = new NodeWorker(self);
console.log("starting worker: ", worker);

await worker.run();
});
Loading

0 comments on commit dae322c

Please sign in to comment.