-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: WASM client via web-sys transport #648
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
261adb9
feat: untested web-sys transport
niklasad1 6e8324d
rewrite me
niklasad1 6831194
Merge remote-tracking branch 'origin/master' into wasm-client
niklasad1 d671947
make it work
niklasad1 908a156
add hacks and works :)
niklasad1 b6e1e04
add subscription test too
niklasad1 0497648
revert StdError change; still works
niklasad1 267d260
cleanup
niklasad1 e03566b
remove hacks
niklasad1 8307117
more wasm tests outside workspace
niklasad1 d2769e0
kill mutually exclusive features
niklasad1 6346a8b
Merge remote-tracking branch 'origin/master' into wasm-client
niklasad1 f7351f4
merge nits
niklasad1 7d8a688
remove unsafe hack
niklasad1 1414280
fix nit
niklasad1 8e6028e
core: fix features and deps
niklasad1 e56c099
ci: add WASM test
niklasad1 12b383b
test again
niklasad1 337d76d
work work
niklasad1 520edfc
comeon
niklasad1 f006173
work work
niklasad1 9baac7f
revert unintentional change
niklasad1 d98659b
Update core/Cargo.toml
niklasad1 56fccd2
Update core/src/client/async_client/mod.rs
niklasad1 ff3e0b6
revert needless change: std hashmap + fxhashmap works
niklasad1 40a3bdd
cleanup
niklasad1 f2389ab
extract try_connect_until fn
niklasad1 3ac6dbb
Merge remote-tracking branch 'origin/master' into wasm-client
niklasad1 af3884e
remove todo
niklasad1 4ec7477
fix bad merge
niklasad1 34e1dbb
add wasm client wrapper crate
niklasad1 0b8ff78
fix nits
niklasad1 6b32049
use gloo-net dependency
niklasad1 b558166
Merge remote-tracking branch 'origin/master' into wasm-client
niklasad1 1fc3705
fix build
niklasad1 0972961
Merge remote-tracking branch 'origin/master' into wasm-client
niklasad1 c683541
grumbles CI: rename to `wasm_tests`
niklasad1 ee4bb2f
fix bad merge
niklasad1 cad8c75
fix grumbles
niklasad1 7d6e9e5
fix nit
niklasad1 b2de6d0
comeon CI
niklasad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use core::fmt; | ||
|
||
use futures_channel::mpsc; | ||
use futures_util::sink::SinkExt; | ||
use futures_util::stream::{SplitSink, SplitStream, StreamExt}; | ||
use gloo_net::websocket::{futures::WebSocket, Message, WebSocketError}; | ||
use jsonrpsee_core::async_trait; | ||
use jsonrpsee_core::client::{TransportReceiverT, TransportSenderT}; | ||
|
||
/// Web-sys transport error that can occur. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
/// Internal send error | ||
#[error("Could not send message: {0}")] | ||
SendError(#[from] mpsc::SendError), | ||
/// Sender went away | ||
#[error("Sender went away couldn't receive the message")] | ||
SenderDisconnected, | ||
/// Error that occurred in `JS context`. | ||
#[error("JS Error: {0:?}")] | ||
Js(String), | ||
/// WebSocket error | ||
#[error("WebSocket Error: {0:?}")] | ||
WebSocket(WebSocketError), | ||
} | ||
|
||
/// Sender. | ||
pub struct Sender(SplitSink<WebSocket, Message>); | ||
|
||
impl fmt::Debug for Sender { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Sender").finish() | ||
} | ||
} | ||
|
||
/// Receiver. | ||
pub struct Receiver(SplitStream<WebSocket>); | ||
|
||
impl fmt::Debug for Receiver { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Receiver").finish() | ||
} | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl TransportSenderT for Sender { | ||
type Error = Error; | ||
|
||
async fn send(&mut self, msg: String) -> Result<(), Self::Error> { | ||
tracing::trace!("tx: {:?}", msg); | ||
self.0.send(Message::Text(msg)).await.map_err(|e| Error::WebSocket(e))?; | ||
Ok(()) | ||
} | ||
|
||
async fn close(&mut self) -> Result<(), Error> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl TransportReceiverT for Receiver { | ||
type Error = Error; | ||
|
||
async fn receive(&mut self) -> Result<String, Self::Error> { | ||
match self.0.next().await { | ||
Some(Ok(msg)) => { | ||
tracing::trace!("rx: {:?}", msg); | ||
|
||
let txt = match msg { | ||
Message::Bytes(bytes) => String::from_utf8(bytes).expect("WebSocket message is valid utf8; qed"), | ||
Message::Text(txt) => txt, | ||
}; | ||
|
||
Ok(txt) | ||
} | ||
Some(Err(err)) => Err(Error::WebSocket(err)), | ||
None => Err(Error::SenderDisconnected), | ||
} | ||
} | ||
} | ||
|
||
/// Create a transport sender & receiver pair. | ||
pub async fn connect(url: impl AsRef<str>) -> Result<(Sender, Receiver), Error> { | ||
let websocket = WebSocket::open(url.as_ref()).map_err(|e| Error::Js(e.to_string()))?; | ||
let (write, read) = websocket.split(); | ||
|
||
Ok((Sender(write), Receiver(read))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "jsonrpsee-wasm-client" | ||
version = "0.10.1" | ||
authors = ["Parity Technologies <admin@parity.io>", "Pierre Krieger <pierre.krieger1708@gmail.com>"] | ||
description = "WASM client for JSON-RPC" | ||
edition = "2021" | ||
license = "MIT" | ||
repository = "https://github.com/paritytech/jsonrpsee" | ||
homepage = "https://github.com/paritytech/jsonrpsee" | ||
documentation = "https://docs.rs/jsonrpsee-ws-client" | ||
|
||
[dependencies] | ||
jsonrpsee-types = { path = "../../types", version = "0.10.1" } | ||
jsonrpsee-client-transport = { path = "../transport", version = "0.10.1", features = ["web"] } | ||
jsonrpsee-core = { path = "../../core", version = "0.10.1", features = ["async-wasm-client"] } | ||
futures-channel = "0.3" | ||
|
||
[dev-dependencies] | ||
env_logger = "0.9" | ||
jsonrpsee-test-utils = { path = "../../test-utils" } | ||
tokio = { version = "1", features = ["macros"] } | ||
serde_json = "1" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done
cargo install wasm-pack
in the past; is there an advantage to doing it this way?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aah; one advantage of the script by the looks of it is that it'll download pre-compiled binaries if they exist; that's good to know!