Skip to content

Commit

Permalink
Merge pull request #20 from folkertdev/run-with-wasi
Browse files Browse the repository at this point in the history
run tests with target `wasm32-unknown-unknown`
  • Loading branch information
tweedegolf-marc authored Jun 24, 2024
2 parents d881d7f + 815d410 commit 8821f06
Show file tree
Hide file tree
Showing 18 changed files with 582 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.wasm32-unknown-unknown]
runner = "wasmtime run --invoke _start"

[target.wasm32-wasi]
runner = "wasmtime run --invoke _start"
29 changes: 29 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ jobs:
run: cargo test -- --nocapture && cargo test --doc
shell: bash

check-wasm32-unknown-unknown:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- run: rustup toolchain install stable --profile minimal

- uses: actions/cache/restore@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: linux-x86-64-gnu-${{ hashFiles('Cargo.toml') }}
restore-keys: linux-x86-64-gnu-

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install wasm-pack
run: cargo install wasm-pack

- name: Run tests with wasm-pack
working-directory: tsp
run: wasm-pack test --node -- -p tsp --no-default-features --features "resolve"

fuzz:
name: run cargo-fuzz
runs-on: ubuntu-22.04
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = [ "tsp", "examples", "fuzz" , "tsp-python"]
members = [ "tsp", "examples", "fuzz" , "tsp-python", "tsp-javascript"]

[workspace.package]
version = "0.1.0"
Expand Down
21 changes: 21 additions & 0 deletions tsp-javascript/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "tsp-javascript"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
readme.workspace = true
description.workspace = true
publish.workspace = true
rust-version.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["console"] }
tsp = { path = "../tsp", default-features = false, features = ["resolve"] }
serde.workspace = true
serde-wasm-bindgen = "0.4"
287 changes: 287 additions & 0 deletions tsp-javascript/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

pub struct Error(tsp::Error);

impl From<Error> for JsValue {
fn from(e: Error) -> Self {
JsValue::from_str(&format!("{:?}", e.0))
}
}

#[derive(Default, Clone)]
#[wasm_bindgen]
pub struct Store(tsp::Store);

#[wasm_bindgen]
pub struct SealedMessage {
#[wasm_bindgen(getter_with_clone)]
pub url: String,
#[wasm_bindgen(getter_with_clone)]
pub bytes: Vec<u8>,
}

#[wasm_bindgen]
impl Store {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen]
pub fn add_private_vid(&self, vid: OwnedVid) -> Result<(), Error> {
self.0.add_private_vid(vid.0).map_err(Error)
}

#[wasm_bindgen]
pub fn seal_message(
&self,
sender: String,
receiver: String,
nonconfidential_data: Option<Vec<u8>>,
message: Vec<u8>,
) -> Result<SealedMessage, Error> {
let (url, bytes) = self
.0
.seal_message(
&sender,
&receiver,
nonconfidential_data.as_deref(),
&message,
)
.map_err(Error)?;

Ok(SealedMessage {
url: url.to_string(),
bytes,
})
}

#[wasm_bindgen]
pub fn open_message(&self, mut message: Vec<u8>) -> Result<FlatReceivedTspMessage, Error> {
self.0
.open_message(&mut message)
.map(FlatReceivedTspMessage::from)
.map_err(Error)
}
}

#[wasm_bindgen]
pub struct OwnedVid(tsp::OwnedVid);

#[wasm_bindgen]
impl OwnedVid {
#[wasm_bindgen]
pub fn new_did_peer(url: String) -> Self {
OwnedVid(tsp::OwnedVid::new_did_peer(url.parse().unwrap()))
}

#[wasm_bindgen]
pub fn identifier(&self) -> String {
use tsp::VerifiedVid;
self.0.identifier().to_string()
}
}

#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ReceivedTspMessageVariant {
GenericMessage = 0,
RequestRelationship = 1,
AcceptRelationship = 2,
CancelRelationship = 3,
ForwardRequest = 4,
}

impl From<&tsp::ReceivedTspMessage> for ReceivedTspMessageVariant {
fn from(value: &tsp::ReceivedTspMessage) -> Self {
match value {
tsp::ReceivedTspMessage::GenericMessage { .. } => Self::GenericMessage,
tsp::ReceivedTspMessage::RequestRelationship { .. } => Self::RequestRelationship,
tsp::ReceivedTspMessage::AcceptRelationship { .. } => Self::AcceptRelationship,
tsp::ReceivedTspMessage::CancelRelationship { .. } => Self::CancelRelationship,
tsp::ReceivedTspMessage::ForwardRequest { .. } => Self::ForwardRequest,
#[cfg(not(target_arch = "wasm32"))]
tsp::ReceivedTspMessage::PendingMessage { .. } => unreachable!(),
}
}
}

#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum MessageType {
Signed,
SignedAndEncrypted,
}

#[wasm_bindgen(inspectable)]
#[derive(Debug, Serialize, Deserialize)]
pub struct FlatReceivedTspMessage {
pub variant: ReceivedTspMessageVariant,
sender: Option<String>,
nonconfidential_data: Option<Option<Vec<u8>>>,
message: Option<Vec<u8>>,
pub message_type: Option<MessageType>,
route: Option<Option<Vec<Vec<u8>>>>,
nested_vid: Option<Option<String>>,
thread_id: Option<Vec<u8>>,
next_hop: Option<String>,
payload: Option<Vec<u8>>,
opaque_payload: Option<Vec<u8>>,
unknown_vid: Option<String>,
}

#[wasm_bindgen]
impl FlatReceivedTspMessage {
#[wasm_bindgen(getter)]
pub fn sender(&self) -> Option<String> {
self.sender.clone()
}

#[wasm_bindgen(getter)]
pub fn nonconfidential_data(&self) -> JsValue {
match &self.nonconfidential_data {
Some(Some(data)) => serde_wasm_bindgen::to_value(data).unwrap(),
_ => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn message(&self) -> JsValue {
match &self.message {
Some(data) => serde_wasm_bindgen::to_value(data).unwrap(),
None => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn route(&self) -> JsValue {
match &self.route {
Some(Some(routes)) => serde_wasm_bindgen::to_value(routes).unwrap(),
_ => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn nested_vid(&self) -> JsValue {
match &self.nested_vid {
Some(Some(vid)) => JsValue::from_str(vid),
_ => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn thread_id(&self) -> JsValue {
match &self.thread_id {
Some(data) => serde_wasm_bindgen::to_value(data).unwrap(),
None => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn next_hop(&self) -> JsValue {
match &self.next_hop {
Some(next_hop) => JsValue::from_str(next_hop),
None => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn payload(&self) -> JsValue {
match &self.payload {
Some(data) => serde_wasm_bindgen::to_value(data).unwrap(),
None => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn opaque_payload(&self) -> JsValue {
match &self.opaque_payload {
Some(data) => serde_wasm_bindgen::to_value(data).unwrap(),
None => JsValue::NULL,
}
}

#[wasm_bindgen(getter)]
pub fn unknown_vid(&self) -> JsValue {
match &self.unknown_vid {
Some(unknown_vid) => JsValue::from_str(unknown_vid),
None => JsValue::NULL,
}
}
}

impl From<tsp::ReceivedTspMessage> for FlatReceivedTspMessage {
fn from(value: tsp::ReceivedTspMessage) -> Self {
let variant = ReceivedTspMessageVariant::from(&value);

let mut this = FlatReceivedTspMessage {
variant,
sender: None,
nonconfidential_data: None,
message: None,
message_type: None,
route: None,
nested_vid: None,
thread_id: None,
next_hop: None,
payload: None,
opaque_payload: None,
unknown_vid: None,
};

match value {
tsp::ReceivedTspMessage::GenericMessage {
sender,
nonconfidential_data,
message,
message_type,
} => {
this.sender = Some(sender);
this.nonconfidential_data = Some(nonconfidential_data);
this.message = Some(message);
this.message_type = match message_type {
tsp::definitions::MessageType::Signed => Some(MessageType::Signed),
tsp::definitions::MessageType::SignedAndEncrypted => {
Some(MessageType::SignedAndEncrypted)
}
};
}
tsp::ReceivedTspMessage::RequestRelationship {
sender,
route,
nested_vid,
thread_id,
} => {
this.sender = Some(sender);
this.route = Some(route);
this.nested_vid = Some(nested_vid);
this.thread_id = Some(thread_id.to_vec());
}
tsp::ReceivedTspMessage::AcceptRelationship { sender, nested_vid } => {
this.sender = Some(sender);
this.nested_vid = Some(nested_vid);
}
tsp::ReceivedTspMessage::CancelRelationship { sender } => {
this.sender = Some(sender);
}
tsp::ReceivedTspMessage::ForwardRequest {
sender,
next_hop,
route,
opaque_payload,
} => {
this.sender = Some(sender);
this.next_hop = Some(next_hop);
this.route = Some(Some(route));
this.opaque_payload = Some(opaque_payload);
}
#[cfg(not(target_arch = "wasm32"))]
tsp::ReceivedTspMessage::PendingMessage { .. } => {
unreachable!()
}
};

this
}
}
1 change: 1 addition & 0 deletions tsp-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Loading

0 comments on commit 8821f06

Please sign in to comment.