From 21edd603c60ba5e2f434ff4f8771fb6d7a95b52f Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 7 Jan 2021 14:17:41 +0100 Subject: [PATCH 1/5] [ci]: feature `http` and `ws` removed. --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 84c0a12735..20d8c8c7ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,6 @@ script: - cargo build --verbose --all-features - cargo test --verbose --all-features - cargo build --verbose --no-default-features - - cargo build --verbose --features "http" - - cargo build --verbose --features "ws" - cargo check --benches --all-features # TODO: add this back when the documentation has been updated. - RUSTDOCFLAGS="--warn broken_intra_doc_links" cargo doc --verbose --workspace --no-deps --document-private-items From 767704fe9282094306e3a53380e089eef2f0fa60 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 7 Jan 2021 14:18:39 +0100 Subject: [PATCH 2/5] refactor: re-org crate with smaller crates. --- Cargo.toml | 67 ++++--------------- benches/Cargo.toml | 23 +++++++ benches/{benches.rs => benches/bench.rs} | 13 ++-- benches/src/lib.rs | 0 http-client/Cargo.toml | 25 +++++++ .../client/http => http-client/src}/client.rs | 10 +-- .../http/mod.rs => http-client/src/lib.rs | 2 +- {src/client/http => http-client/src}/tests.rs | 9 ++- .../http => http-client/src}/transport.rs | 10 +-- http-server/Cargo.toml | 28 ++++++++ src/http/mod.rs => http-server/src/lib.rs | 2 +- {src/http => http-server/src}/raw/core.rs | 8 ++- {src/http => http-server/src}/raw/mod.rs | 0 {src/http => http-server/src}/raw/tests.rs | 7 +- {src/ws => http-server/src}/raw/typed_rp.rs | 4 +- {src/http => http-server/src}/server.rs | 12 ++-- {src/http => http-server/src}/tests.rs | 5 +- .../src}/transport/background.rs | 6 +- .../http => http-server/src}/transport/mod.rs | 5 +- .../src}/transport/response.rs | 2 +- src/client/mod.rs | 15 ----- src/lib.rs | 61 ----------------- src/utils/mod.rs | 2 - test-utils/Cargo.toml | 18 ++--- tests/Cargo.toml | 16 +++++ tests/{ => src}/helpers.rs | 6 +- tests/{integration_tests.rs => src/lib.rs} | 5 +- types/Cargo.toml | 16 +++++ {src/types => types/src}/error.rs | 2 +- {src/types => types/src}/http.rs | 1 - {src/types => types/src}/jsonrpc/error.rs | 0 {src/types => types/src}/jsonrpc/id.rs | 0 {src/types => types/src}/jsonrpc/mod.rs | 0 {src/types => types/src}/jsonrpc/params.rs | 2 +- {src/types => types/src}/jsonrpc/request.rs | 0 {src/types => types/src}/jsonrpc/response.rs | 0 {src/types => types/src}/jsonrpc/version.rs | 0 .../src}/jsonrpc/wrapped/batch.rs | 4 +- .../src}/jsonrpc/wrapped/batches.rs | 4 +- .../src}/jsonrpc/wrapped/mod.rs | 0 .../src}/jsonrpc/wrapped/notification.rs | 2 +- .../src}/jsonrpc/wrapped/params.rs | 2 +- src/types/mod.rs => types/src/lib.rs | 8 ++- utils/Cargo.toml | 20 ++++++ .../src}/http/access_control.rs | 6 +- {src/utils => utils/src}/http/cors.rs | 6 +- {src/utils => utils/src}/http/hosts.rs | 2 +- .../utils => utils/src}/http/hyper_helpers.rs | 5 +- {src/utils => utils/src}/http/matcher.rs | 0 {src/utils => utils/src}/http/mod.rs | 0 utils/src/lib.rs | 1 + ws-client/Cargo.toml | 26 +++++++ {src/client/ws => ws-client/src}/client.rs | 38 +++++------ .../ws => ws-client/src}/jsonrpc_transport.rs | 12 ++-- src/client/ws/mod.rs => ws-client/src/lib.rs | 2 +- {src/client/ws => ws-client/src}/manager.rs | 8 ++- {src/client/ws => ws-client/src}/stream.rs | 0 {src/client/ws => ws-client/src}/tests.rs | 4 +- {src/client/ws => ws-client/src}/transport.rs | 5 +- ws-server/Cargo.toml | 27 ++++++++ src/ws/mod.rs => ws-server/src/lib.rs | 2 + {src/ws => ws-server/src}/raw/core.rs | 10 +-- {src/ws => ws-server/src}/raw/mod.rs | 0 {src/http => ws-server/src}/raw/typed_rp.rs | 4 +- {src/ws => ws-server/src}/server.rs | 10 +-- {src/ws => ws-server/src}/tests.rs | 9 +-- {src/ws => ws-server/src}/transport.rs | 2 +- 67 files changed, 339 insertions(+), 262 deletions(-) create mode 100644 benches/Cargo.toml rename benches/{benches.rs => benches/bench.rs} (90%) create mode 100644 benches/src/lib.rs create mode 100644 http-client/Cargo.toml rename {src/client/http => http-client/src}/client.rs (94%) rename src/client/http/mod.rs => http-client/src/lib.rs (73%) rename {src/client/http => http-client/src}/tests.rs (95%) rename {src/client/http => http-client/src}/transport.rs (95%) create mode 100644 http-server/Cargo.toml rename src/http/mod.rs => http-server/src/lib.rs (97%) rename {src/http => http-server/src}/raw/core.rs (97%) rename {src/http => http-server/src}/raw/mod.rs (100%) rename {src/http => http-server/src}/raw/tests.rs (92%) rename {src/ws => http-server/src}/raw/typed_rp.rs (97%) rename {src/http => http-server/src}/server.rs (97%) rename {src/http => http-server/src}/tests.rs (97%) rename {src/http => http-server/src}/transport/background.rs (97%) rename {src/http => http-server/src}/transport/mod.rs (98%) rename {src/http => http-server/src}/transport/response.rs (99%) delete mode 100644 src/client/mod.rs delete mode 100644 src/lib.rs delete mode 100644 src/utils/mod.rs create mode 100644 tests/Cargo.toml rename tests/{ => src}/helpers.rs (96%) rename tests/{integration_tests.rs => src/lib.rs} (97%) create mode 100644 types/Cargo.toml rename {src/types => types/src}/error.rs (98%) rename {src/types => types/src}/http.rs (99%) rename {src/types => types/src}/jsonrpc/error.rs (100%) rename {src/types => types/src}/jsonrpc/id.rs (100%) rename {src/types => types/src}/jsonrpc/mod.rs (100%) rename {src/types => types/src}/jsonrpc/params.rs (98%) rename {src/types => types/src}/jsonrpc/request.rs (100%) rename {src/types => types/src}/jsonrpc/response.rs (100%) rename {src/types => types/src}/jsonrpc/version.rs (100%) rename {src/types => types/src}/jsonrpc/wrapped/batch.rs (99%) rename {src/types => types/src}/jsonrpc/wrapped/batches.rs (99%) rename {src/types => types/src}/jsonrpc/wrapped/mod.rs (100%) rename {src/types => types/src}/jsonrpc/wrapped/notification.rs (97%) rename {src/types => types/src}/jsonrpc/wrapped/params.rs (99%) rename src/types/mod.rs => types/src/lib.rs (51%) create mode 100644 utils/Cargo.toml rename {src/utils => utils/src}/http/access_control.rs (96%) rename {src/utils => utils/src}/http/cors.rs (99%) rename {src/utils => utils/src}/http/hosts.rs (99%) rename {src/utils => utils/src}/http/hyper_helpers.rs (97%) rename {src/utils => utils/src}/http/matcher.rs (100%) rename {src/utils => utils/src}/http/mod.rs (100%) create mode 100644 utils/src/lib.rs create mode 100644 ws-client/Cargo.toml rename {src/client/ws => ws-client/src}/client.rs (95%) rename {src/client/ws => ws-client/src}/jsonrpc_transport.rs (90%) rename src/client/ws/mod.rs => ws-client/src/lib.rs (80%) rename {src/client/ws => ws-client/src}/manager.rs (98%) rename {src/client/ws => ws-client/src}/stream.rs (100%) rename {src/client/ws => ws-client/src}/tests.rs (98%) rename {src/client/ws => ws-client/src}/transport.rs (98%) create mode 100644 ws-server/Cargo.toml rename src/ws/mod.rs => ws-server/src/lib.rs (98%) rename {src/ws => ws-server/src}/raw/core.rs (98%) rename {src/ws => ws-server/src}/raw/mod.rs (100%) rename {src/http => ws-server/src}/raw/typed_rp.rs (97%) rename {src/ws => ws-server/src}/server.rs (98%) rename {src/ws => ws-server/src}/tests.rs (98%) rename {src/ws => ws-server/src}/transport.rs (99%) diff --git a/Cargo.toml b/Cargo.toml index 36d9766e88..4cf93a8085 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,55 +1,12 @@ -[package] -name = "jsonrpsee" -description = "JSON-RPC crate" -version = "2.0.0" -authors = ["Pierre Krieger "] -license = "MIT" -edition = "2018" - -[dependencies] -async-std = { version = "1.8.0", features = ["attributes"] } -bs58 = "0.4.0" -fnv = "1.0.7" -futures = "0.3.8" -globset = "0.4.6" -hashbrown = "0.9.1" -lazy_static = "1.4.0" -log = "0.4.11" -parking_lot = "0.11.1" -pin-project = "1.0.2" -jsonrpsee-proc-macros = { path = "proc-macros" } -rand = "0.7.3" -serde = { version = "1.0.118", default-features = false, features = ["derive"] } -serde_json = "1.0.60" -smallvec = { version = "1.5.1", default-features = false } -thiserror = "1.0.22" - -# used for tests and HTTP/hyper -# the features are used only for tests but enabled because `dev-dependencies` are leaked into dependencies. -tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros"], optional = true } - -# HTTP-related dependencies -hyper = { version = "0.14", features = ["stream", "client", "server", "http1", "http2", "tcp"], optional = true } -unicase = { version = "2.6.0", optional = true } - -# WS-related dependencies -async-tls = { version = "0.11.0", optional = true } -bytes = { version = "0.6.0", optional = true } -soketto = { version = "0.4.2", optional = true } -url = { version = "2.2.0", optional = true } -webpki = { version = "0.21.4", optional = true } - -[features] -default = ["http", "ws"] -http = ["hyper", "tokio", "unicase"] -ws = ["async-tls", "bytes", "soketto", "url", "webpki"] - -[dev-dependencies] -criterion = "0.3.3" -env_logger = "0.8.2" -jsonrpsee-test-utils = { path = "test-utils" } -num_cpus = "1.13.0" - -[[bench]] -name = "benches" -harness = false +[workspace] +members = [ + "benches", + "http-client", + "http-server", + "test-utils", + "tests", + "types", + "utils", + "ws-client", + "ws-server", +] diff --git a/benches/Cargo.toml b/benches/Cargo.toml new file mode 100644 index 0000000000..5a1ed35782 --- /dev/null +++ b/benches/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "jsonrpsee-benchmarks" +version = "0.1.0" +authors = ["Parity Technologies "] +description = "Benchmarks for jsonrpsee" +edition = "2018" +license = "MIT" + +[dev-dependencies] +async-std = "1.8" +criterion = "0.3" +futures = "0.3" +jsonrpsee-types = { path = "../types" } +jsonrpsee-http-client = { path = "../http-client" } +jsonrpsee-ws-client = { path = "../ws-client" } +jsonrpsee-ws-server = { path = "../ws-server" } +jsonrpsee-http-server = { path = "../http-server" } +num_cpus = "1" +tokio = { version = "1", features = ["full"] } + +[[bench]] +name = "bench" +harness = false diff --git a/benches/benches.rs b/benches/benches/bench.rs similarity index 90% rename from benches/benches.rs rename to benches/benches/bench.rs index ba145ef077..8b363058e0 100644 --- a/benches/benches.rs +++ b/benches/benches/bench.rs @@ -1,10 +1,11 @@ use async_std::task::block_on; use criterion::*; use futures::channel::oneshot::{self, Sender}; -use jsonrpsee::client::{HttpClient, HttpConfig, WsClient, WsConfig}; -use jsonrpsee::http::HttpServer; -use jsonrpsee::types::jsonrpc::{JsonValue, Params}; -use jsonrpsee::ws::WsServer; +use jsonrpsee_http_client::{HttpClient, HttpConfig}; +use jsonrpsee_http_server::HttpServer; +use jsonrpsee_types::jsonrpc::{JsonValue, Params}; +use jsonrpsee_ws_client::{WsClient, WsConfig}; +use jsonrpsee_ws_server::WsServer; use std::net::SocketAddr; use std::sync::Arc; @@ -37,7 +38,7 @@ async fn ws_server(tx: Sender) { } pub fn http_requests(c: &mut criterion::Criterion) { - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); let (tx_addr, rx_addr) = oneshot::channel::(); async_std::task::spawn(http_server(tx_addr)); let server_addr = block_on(rx_addr).unwrap(); @@ -73,7 +74,7 @@ pub fn http_requests(c: &mut criterion::Criterion) { } pub fn websocket_requests(c: &mut criterion::Criterion) { - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); let (tx_addr, rx_addr) = oneshot::channel::(); async_std::task::spawn(ws_server(tx_addr)); let server_addr = block_on(rx_addr).unwrap(); diff --git a/benches/src/lib.rs b/benches/src/lib.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/http-client/Cargo.toml b/http-client/Cargo.toml new file mode 100644 index 0000000000..3d5f44af3e --- /dev/null +++ b/http-client/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "jsonrpsee-http-client" +version = "0.1.0" +authors = ["Parity Technologies ", "Pierre Krieger "] +description = "HTTP client for JSON-RPC" +edition = "2018" +license = "MIT" + +[dependencies] +futures = "0.3" +hyper = { version = "0.14", features = ["stream", "client", "server", "http1", "http2", "tcp"] } +jsonrpsee-types = { path = "../types", version = "0.1" } +jsonrpsee-utils = { path = "../utils", version = "0.1" } +log = "0.4" +serde = { version = "1.0", default-features = false, features = ["derive"] } +serde_json = "1.0" +thiserror = "1.0" +# `macros feature` is only used for tests but enabled globally because `dev-dependencies` +# are leaked. +tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros"] } +unicase = "2.6" +url = "2.2" + +[dev-dependencies] +jsonrpsee-test-utils = { path = "../test-utils" } diff --git a/src/client/http/client.rs b/http-client/src/client.rs similarity index 94% rename from src/client/http/client.rs rename to http-client/src/client.rs index fa0c27ed93..dd006db8bb 100644 --- a/src/client/http/client.rs +++ b/http-client/src/client.rs @@ -1,7 +1,9 @@ -use crate::client::http::transport::HttpTransportClient; -use crate::types::error::Error; -use crate::types::http::HttpConfig; -use crate::types::jsonrpc::{self, JsonValue}; +use crate::transport::HttpTransportClient; +use jsonrpsee_types::{ + error::Error, + http::HttpConfig, + jsonrpc::{self, JsonValue}, +}; use std::convert::TryInto; use std::sync::atomic::{AtomicU64, Ordering}; diff --git a/src/client/http/mod.rs b/http-client/src/lib.rs similarity index 73% rename from src/client/http/mod.rs rename to http-client/src/lib.rs index f98e1c4209..cd27e16576 100644 --- a/src/client/http/mod.rs +++ b/http-client/src/lib.rs @@ -4,6 +4,6 @@ mod transport; #[cfg(test)] mod tests; -pub use crate::types::http::HttpConfig; pub use client::HttpClient; +pub use jsonrpsee_types::http::HttpConfig; pub use transport::HttpTransportClient; diff --git a/src/client/http/tests.rs b/http-client/src/tests.rs similarity index 95% rename from src/client/http/tests.rs rename to http-client/src/tests.rs index 4285285101..ebf4f9e2da 100644 --- a/src/client/http/tests.rs +++ b/http-client/src/tests.rs @@ -1,6 +1,9 @@ -use crate::client::{HttpClient, HttpConfig}; -use crate::types::error::Error; -use crate::types::jsonrpc::{self, ErrorCode, JsonValue, Params}; +use crate::client::HttpClient; +use jsonrpsee_types::{ + error::Error, + http::HttpConfig, + jsonrpc::{self, ErrorCode, JsonValue, Params}, +}; use jsonrpsee_test_utils::helpers::*; use jsonrpsee_test_utils::types::Id; diff --git a/src/client/http/transport.rs b/http-client/src/transport.rs similarity index 95% rename from src/client/http/transport.rs rename to http-client/src/transport.rs index bc5f10ee8e..ede7049606 100644 --- a/src/client/http/transport.rs +++ b/http-client/src/transport.rs @@ -6,8 +6,8 @@ // that we need to be guaranteed that hyper doesn't re-use an existing connection if we ever reset // the JSON-RPC request id to a value that might have already been used. -use crate::types::{error::GenericTransportError, http::HttpConfig, jsonrpc}; -use crate::utils::http::hyper_helpers; +use jsonrpsee_types::{error::GenericTransportError, http::HttpConfig, jsonrpc}; +use jsonrpsee_utils::http::hyper_helpers; use thiserror::Error; const CONTENT_TYPE_JSON: &str = "application/json"; @@ -132,8 +132,10 @@ where #[cfg(test)] mod tests { use super::{Error, HttpTransportClient}; - use crate::types::http::HttpConfig; - use crate::types::jsonrpc::{Call, Id, MethodCall, Params, Request, Version}; + use jsonrpsee_types::{ + http::HttpConfig, + jsonrpc::{Call, Id, MethodCall, Params, Request, Version}, + }; #[test] fn invalid_http_url_rejected() { diff --git a/http-server/Cargo.toml b/http-server/Cargo.toml new file mode 100644 index 0000000000..cf6735064d --- /dev/null +++ b/http-server/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "jsonrpsee-http-server" +version = "0.1.0" +authors = ["Parity Technologies ", "Pierre Krieger "] +description = "HTTP server for JSON-RPC" +edition = "2018" +license = "MIT" + +[dependencies] +async-std = "1.8" +futures = "0.3" +fnv = "1" +hyper = { version = "0.14", features = ["stream", "client", "server", "http1", "http2", "tcp"] } +jsonrpsee-types = { path = "../types", version = "0.1" } +jsonrpsee-utils = { path = "../utils", version = "0.1" } +log = "0.4" +serde = { version = "1", default-features = false, features = ["derive"] } +serde_json = "1" +parking_lot = "0.11" +thiserror = "1" +# `macros feature` is only used for tests but enabled globally because `dev-dependencies` +# are leaked. +tokio = { version = "1", features = ["net", "rt-multi-thread", "macros"] } +unicase = "2" + +[dev-dependencies] +jsonrpsee-test-utils = { path = "../test-utils" } +jsonrpsee-http-client = { path = "../http-client" } diff --git a/src/http/mod.rs b/http-server/src/lib.rs similarity index 97% rename from src/http/mod.rs rename to http-server/src/lib.rs index c7859e2423..876593bbab 100644 --- a/src/http/mod.rs +++ b/http-server/src/lib.rs @@ -31,7 +31,7 @@ mod transport; #[cfg(test)] mod tests; -pub use crate::types::http::HttpConfig; +pub use jsonrpsee_types::http::HttpConfig; pub use raw::RawServer as HttpRawServer; pub use raw::RawServerEvent as HttpRawServerEvent; pub use raw::TypedResponder as HttpTypedResponder; diff --git a/src/http/raw/core.rs b/http-server/src/raw/core.rs similarity index 97% rename from src/http/raw/core.rs rename to http-server/src/raw/core.rs index 547d9be852..8c44a2c3fe 100644 --- a/src/http/raw/core.rs +++ b/http-server/src/raw/core.rs @@ -24,9 +24,11 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::http::transport::{HttpTransportServer, TransportServerEvent}; -use crate::types::jsonrpc; -use crate::types::jsonrpc::wrapped::{batches, Notification, Params}; +use crate::transport::{HttpTransportServer, TransportServerEvent}; +use jsonrpsee_types::jsonrpc::{ + self, + wrapped::{batches, Notification, Params}, +}; use core::{fmt, hash::Hash}; diff --git a/src/http/raw/mod.rs b/http-server/src/raw/mod.rs similarity index 100% rename from src/http/raw/mod.rs rename to http-server/src/raw/mod.rs diff --git a/src/http/raw/tests.rs b/http-server/src/raw/tests.rs similarity index 92% rename from src/http/raw/tests.rs rename to http-server/src/raw/tests.rs index 313e65274a..30a5ab9bfa 100644 --- a/src/http/raw/tests.rs +++ b/http-server/src/raw/tests.rs @@ -26,10 +26,9 @@ #![cfg(test)] -use crate::client::HttpTransportClient; -use crate::http::{HttpRawServer, HttpRawServerEvent, HttpTransportServer}; -use crate::types::http::HttpConfig; -use crate::types::jsonrpc::{self, Call, MethodCall, Notification, Params, Request, Version}; +use crate::{HttpConfig, HttpRawServer, HttpRawServerEvent, HttpTransportServer}; +use jsonrpsee_http_client::HttpTransportClient; +use jsonrpsee_types::jsonrpc::{self, Call, MethodCall, Notification, Params, Request, Version}; use serde_json::Value; async fn connection_context() -> (HttpTransportClient, HttpRawServer) { diff --git a/src/ws/raw/typed_rp.rs b/http-server/src/raw/typed_rp.rs similarity index 97% rename from src/ws/raw/typed_rp.rs rename to http-server/src/raw/typed_rp.rs index a05d1fc4cb..2fd54aa389 100644 --- a/src/ws/raw/typed_rp.rs +++ b/http-server/src/raw/typed_rp.rs @@ -24,9 +24,9 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc; -use crate::ws::raw::RawServerRequest; +use crate::raw::RawServerRequest; use core::marker::PhantomData; +use jsonrpsee_types::jsonrpc; /// Allows responding to a server request in a more elegant and strongly-typed fashion. pub struct TypedResponder<'a, T> { diff --git a/src/http/server.rs b/http-server/src/server.rs similarity index 97% rename from src/http/server.rs rename to http-server/src/server.rs index 6e88257483..3e1fa2abd7 100644 --- a/src/http/server.rs +++ b/http-server/src/server.rs @@ -24,11 +24,13 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::http::raw::{RawServer, RawServerEvent, RawServerRequestId}; -use crate::http::transport::HttpTransportServer; -use crate::types::error::Error; -use crate::types::http::HttpConfig; -use crate::types::jsonrpc::{self, JsonValue}; +use crate::raw::{RawServer, RawServerEvent, RawServerRequestId}; +use crate::transport::HttpTransportServer; +use jsonrpsee_types::{ + error::Error, + http::HttpConfig, + jsonrpc::{self, JsonValue}, +}; use futures::{channel::mpsc, future::Either, pin_mut, prelude::*}; use parking_lot::Mutex; diff --git a/src/http/tests.rs b/http-server/src/tests.rs similarity index 97% rename from src/http/tests.rs rename to http-server/src/tests.rs index ead8e813db..2cef8e45d8 100644 --- a/src/http/tests.rs +++ b/http-server/src/tests.rs @@ -1,13 +1,12 @@ #![cfg(test)] -use crate::http::HttpServer; -use crate::types::http::HttpConfig; -use crate::types::jsonrpc::JsonValue; +use crate::{HttpConfig, HttpServer}; use futures::channel::oneshot::{self, Sender}; use futures::future::FutureExt; use futures::{pin_mut, select}; use jsonrpsee_test_utils::helpers::*; use jsonrpsee_test_utils::types::{Id, StatusCode}; +use jsonrpsee_types::jsonrpc::JsonValue; use std::net::SocketAddr; async fn server(server_started_tx: Sender) { diff --git a/src/http/transport/background.rs b/http-server/src/transport/background.rs similarity index 97% rename from src/http/transport/background.rs rename to http-server/src/transport/background.rs index ab909b5f99..9d645a36c0 100644 --- a/src/http/transport/background.rs +++ b/http-server/src/transport/background.rs @@ -24,12 +24,12 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::http::transport::response; -use crate::types::{error::GenericTransportError, http::HttpConfig, jsonrpc}; -use crate::utils::http::{access_control::AccessControl, hyper_helpers}; +use crate::transport::response; use futures::{channel::mpsc, channel::oneshot, prelude::*}; use hyper::service::{make_service_fn, service_fn}; use hyper::Error; +use jsonrpsee_types::{error::GenericTransportError, http::HttpConfig, jsonrpc}; +use jsonrpsee_utils::http::{access_control::AccessControl, hyper_helpers}; use std::{error, net::SocketAddr, thread}; /// Background thread that serves HTTP requests. diff --git a/src/http/transport/mod.rs b/http-server/src/transport/mod.rs similarity index 98% rename from src/http/transport/mod.rs rename to http-server/src/transport/mod.rs index c37cb7851d..5db960eebc 100644 --- a/src/http/transport/mod.rs +++ b/http-server/src/transport/mod.rs @@ -28,9 +28,8 @@ mod background; #[allow(unused)] mod response; -use crate::types::http::HttpConfig; -use crate::types::jsonrpc; -use crate::utils::http::access_control::AccessControl; +use jsonrpsee_types::{http::HttpConfig, jsonrpc}; +use jsonrpsee_utils::http::access_control::AccessControl; use fnv::FnvHashMap; use futures::{channel::oneshot, prelude::*}; diff --git a/src/http/transport/response.rs b/http-server/src/transport/response.rs similarity index 99% rename from src/http/transport/response.rs rename to http-server/src/transport/response.rs index 77d5c94c5d..7996dcebbe 100644 --- a/src/http/transport/response.rs +++ b/http-server/src/transport/response.rs @@ -26,7 +26,7 @@ //! Contains common builders for hyper responses. -use crate::types::jsonrpc; +use jsonrpsee_types::jsonrpc; /// Create a response for plaintext internal error. pub fn internal_error>(msg: T) -> hyper::Response { diff --git a/src/client/mod.rs b/src/client/mod.rs deleted file mode 100644 index d498136a38..0000000000 --- a/src/client/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -#[cfg(feature = "http")] -mod http; -#[cfg(feature = "ws")] -mod ws; - -// TODO: just export `Client` because the underlying layers is not likely to be used. -// Unless we want the user to have to possibility to not spawn a background thread to -// handle responses. -#[cfg(feature = "http")] -pub use http::{HttpClient, HttpConfig, HttpTransportClient}; -#[cfg(feature = "ws")] -pub use ws::{ - jsonrpc_transport as ws_jsonrpc_transport, transport as ws_transport, Client as WsClient, Config as WsConfig, - Subscription as WsSubscription, -}; diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 75062e0f07..0000000000 --- a/src/lib.rs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2019 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any -// person obtaining a copy of this software and associated -// documentation files (the "Software"), to deal in the -// Software without restriction, including without -// limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software -// is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice -// shall be included in all copies or substantial portions -// of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -//! JSON-RPC clients, servers, and utilities. -//! -//! This crate allows you to perform outgoing JSON-RPC requests and creating servers accepting -//! JSON-RPC requests. Only [JSON-RPC version 2](https://www.jsonrpc.org/specification) is -//! supported. -//! -//! In addition to the core JSON-RPC specifications this crate also supports the non-standard -//! "JSON-RPC pub sub" extension, which allows the server to push notifications the client -//! subscribes to. This extension is most notably used in the Ethereum ecosystem, but it is very -//! generic and can be used for any purpose related or not to Ethereum. -//! -//! # Writing an API definition (optional) -//! -//! Before starting to perform or answer queries, one optional step is to define your JSON-RPC API -//! using the `rpc_api!` macro. - -#![deny(unsafe_code)] -#![warn(missing_docs)] - -extern crate alloc; - -pub use jsonrpsee_proc_macros::rpc_api; - -/// Client implementations. -pub mod client; -/// JSONRPC 2.0 HTTP server. -#[cfg(feature = "http")] -pub mod http; -/// Shared types. -pub mod types; -/// Utils. -pub mod utils; -/// JSONRPC 2.0 WebSocket server. -#[cfg(feature = "ws")] -pub mod ws; diff --git a/src/utils/mod.rs b/src/utils/mod.rs deleted file mode 100644 index d56a2d3919..0000000000 --- a/src/utils/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(feature = "http")] -pub mod http; diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 4b509909f6..5e98a5ccf9 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -1,19 +1,19 @@ [package] name = "jsonrpsee-test-utils" version = "0.1.0" -authors = ["Niklas "] +authors = ["Parity Technologies "] license = "MIT" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -async-std = "1.8.0" -futures = "0.3.8" -hyper = "0.14" -log = "0.4.11" -serde = { version = "1.0.118", default-features = false, features = ["derive"] } -serde_json = "1.0.60" -soketto = "0.4.2" -tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros", "time"] } +async-std = "1.8" +futures = "0.3" +hyper = { version = "0.14", features = ["full"] } +log = "0.4" +serde = { version = "1", default-features = false, features = ["derive"] } +serde_json = "1" +soketto = "0.4" +tokio = { version = "1", features = ["net", "rt-multi-thread", "macros", "time"] } tokio-util = { version = "0.6", features = ["compat"] } diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 0000000000..1e985bbbf9 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "jsonrpsee-integration-tests" +version = "0.1.0" +authors = ["Parity Technologies "] +description = "Integration tests for jsonrpsee" +edition = "2018" +license = "MIT" + +[dev-dependencies] +futures = "0.3" +jsonrpsee-types = { path = "../types" } +jsonrpsee-http-client = { path = "../http-client" } +jsonrpsee-ws-client = { path = "../ws-client" } +jsonrpsee-ws-server = { path = "../ws-server" } +jsonrpsee-http-server = { path = "../http-server" } +tokio = { version = "1", features = ["full"] } diff --git a/tests/helpers.rs b/tests/src/helpers.rs similarity index 96% rename from tests/helpers.rs rename to tests/src/helpers.rs index dade37a968..aecb788bb4 100644 --- a/tests/helpers.rs +++ b/tests/src/helpers.rs @@ -24,9 +24,9 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use jsonrpsee::http::{HttpConfig, HttpServer}; -use jsonrpsee::types::jsonrpc::JsonValue; -use jsonrpsee::ws::WsServer; +use jsonrpsee_http_server::{HttpConfig, HttpServer}; +use jsonrpsee_types::jsonrpc::JsonValue; +use jsonrpsee_ws_server::WsServer; use std::net::SocketAddr; use std::time::Duration; diff --git a/tests/integration_tests.rs b/tests/src/lib.rs similarity index 97% rename from tests/integration_tests.rs rename to tests/src/lib.rs index dec44e24aa..109b76f10d 100644 --- a/tests/integration_tests.rs +++ b/tests/src/lib.rs @@ -33,8 +33,9 @@ use std::time::Duration; use futures::channel::oneshot; use helpers::{http_server, websocket_server, websocket_server_with_wait_period}; -use jsonrpsee::client::{HttpClient, HttpConfig, WsClient, WsConfig, WsSubscription}; -use jsonrpsee::types::jsonrpc::{JsonValue, Params}; +use jsonrpsee_http_client::{HttpClient, HttpConfig}; +use jsonrpsee_types::jsonrpc::{JsonValue, Params}; +use jsonrpsee_ws_client::{WsClient, WsConfig, WsSubscription}; #[tokio::test] async fn ws_subscription_works() { diff --git a/types/Cargo.toml b/types/Cargo.toml new file mode 100644 index 0000000000..dfdcf2390e --- /dev/null +++ b/types/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "jsonrpsee-types" +version = "0.1.0" +authors = ["Parity Technologies "] +description = "Shared types for jsonrpsee" +edition = "2018" +license = "MIT" + +[dependencies] +hashbrown = "0.9" +fnv = "1.0" +futures = "0.3" +thiserror = "1.0" +serde = { version = "1.0", default-features = false, features = ["derive"] } +serde_json = "1.0" +smallvec = "1.0" diff --git a/src/types/error.rs b/types/src/error.rs similarity index 98% rename from src/types/error.rs rename to types/src/error.rs index d4b38852aa..e36bd6958e 100644 --- a/src/types/error.rs +++ b/types/src/error.rs @@ -1,4 +1,4 @@ -use crate::types::jsonrpc; +use crate::jsonrpc; use std::fmt; /// Convenience type for displaying errors. #[derive(Clone, Debug, PartialEq)] diff --git a/src/types/http.rs b/types/src/http.rs similarity index 99% rename from src/types/http.rs rename to types/src/http.rs index a6558559b4..44e7d54fdb 100644 --- a/src/types/http.rs +++ b/types/src/http.rs @@ -2,7 +2,6 @@ /// Default maximum request body size (10 MB). const DEFAULT_MAX_BODY_SIZE_TEN_MB: u32 = 10 * 1024 * 1024; - /// HTTP configuration. #[derive(Copy, Clone, Debug, PartialEq)] pub struct HttpConfig { diff --git a/src/types/jsonrpc/error.rs b/types/src/jsonrpc/error.rs similarity index 100% rename from src/types/jsonrpc/error.rs rename to types/src/jsonrpc/error.rs diff --git a/src/types/jsonrpc/id.rs b/types/src/jsonrpc/id.rs similarity index 100% rename from src/types/jsonrpc/id.rs rename to types/src/jsonrpc/id.rs diff --git a/src/types/jsonrpc/mod.rs b/types/src/jsonrpc/mod.rs similarity index 100% rename from src/types/jsonrpc/mod.rs rename to types/src/jsonrpc/mod.rs diff --git a/src/types/jsonrpc/params.rs b/types/src/jsonrpc/params.rs similarity index 98% rename from src/types/jsonrpc/params.rs rename to types/src/jsonrpc/params.rs index 86403188e9..f031fd2352 100644 --- a/src/types/jsonrpc/params.rs +++ b/types/src/jsonrpc/params.rs @@ -78,7 +78,7 @@ impl From for JsonValue { #[cfg(test)] mod tests { use super::Params; - use crate::types::jsonrpc::{Error, ErrorCode, JsonValue}; + use crate::jsonrpc::{Error, ErrorCode, JsonValue}; #[test] fn params_deserialization() { diff --git a/src/types/jsonrpc/request.rs b/types/src/jsonrpc/request.rs similarity index 100% rename from src/types/jsonrpc/request.rs rename to types/src/jsonrpc/request.rs diff --git a/src/types/jsonrpc/response.rs b/types/src/jsonrpc/response.rs similarity index 100% rename from src/types/jsonrpc/response.rs rename to types/src/jsonrpc/response.rs diff --git a/src/types/jsonrpc/version.rs b/types/src/jsonrpc/version.rs similarity index 100% rename from src/types/jsonrpc/version.rs rename to types/src/jsonrpc/version.rs diff --git a/src/types/jsonrpc/wrapped/batch.rs b/types/src/jsonrpc/wrapped/batch.rs similarity index 99% rename from src/types/jsonrpc/wrapped/batch.rs rename to types/src/jsonrpc/wrapped/batch.rs index aa8fe11bda..34c85c580f 100644 --- a/src/types/jsonrpc/wrapped/batch.rs +++ b/types/src/jsonrpc/wrapped/batch.rs @@ -24,7 +24,7 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc::{ +use crate::jsonrpc::{ self, wrapped::{Notification, Params}, }; @@ -262,7 +262,7 @@ impl<'a> fmt::Debug for BatchElem<'a> { #[cfg(test)] mod tests { use super::{BatchInc, BatchState}; - use crate::types::jsonrpc::{self, wrapped::Notification}; + use crate::jsonrpc::{self, wrapped::Notification}; #[test] fn basic_notification() { diff --git a/src/types/jsonrpc/wrapped/batches.rs b/types/src/jsonrpc/wrapped/batches.rs similarity index 99% rename from src/types/jsonrpc/wrapped/batches.rs rename to types/src/jsonrpc/wrapped/batches.rs index 3681682f4e..ab73a58c8f 100644 --- a/src/types/jsonrpc/wrapped/batches.rs +++ b/types/src/jsonrpc/wrapped/batches.rs @@ -24,7 +24,7 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc::{ +use crate::jsonrpc::{ self, wrapped::{batch, Notification, Params}, }; @@ -295,7 +295,7 @@ where #[cfg(test)] mod tests { use super::{BatchesEvent, BatchesState}; - use crate::types::jsonrpc::{self, wrapped::Notification}; + use crate::jsonrpc::{self, wrapped::Notification}; #[test] fn basic_notification() { diff --git a/src/types/jsonrpc/wrapped/mod.rs b/types/src/jsonrpc/wrapped/mod.rs similarity index 100% rename from src/types/jsonrpc/wrapped/mod.rs rename to types/src/jsonrpc/wrapped/mod.rs diff --git a/src/types/jsonrpc/wrapped/notification.rs b/types/src/jsonrpc/wrapped/notification.rs similarity index 97% rename from src/types/jsonrpc/wrapped/notification.rs rename to types/src/jsonrpc/wrapped/notification.rs index 43718439f9..750c9e9290 100644 --- a/src/types/jsonrpc/wrapped/notification.rs +++ b/types/src/jsonrpc/wrapped/notification.rs @@ -24,7 +24,7 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc::{self, wrapped::Params}; +use crate::jsonrpc::{self, wrapped::Params}; use core::fmt; /// Notification received on a server. diff --git a/src/types/jsonrpc/wrapped/params.rs b/types/src/jsonrpc/wrapped/params.rs similarity index 99% rename from src/types/jsonrpc/wrapped/params.rs rename to types/src/jsonrpc/wrapped/params.rs index 47e9cd04cc..405742a907 100644 --- a/src/types/jsonrpc/wrapped/params.rs +++ b/types/src/jsonrpc/wrapped/params.rs @@ -24,7 +24,7 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc; +use crate::jsonrpc; use alloc::string::String; use core::fmt; diff --git a/src/types/mod.rs b/types/src/lib.rs similarity index 51% rename from src/types/mod.rs rename to types/src/lib.rs index 5f41eaf82b..e81726e9fd 100644 --- a/src/types/mod.rs +++ b/types/src/lib.rs @@ -1,3 +1,10 @@ +//! Shared types in `jsonrpsee` for clients, servers and utilities. + +#![deny(unsafe_code)] +#![warn(missing_docs)] + +extern crate alloc; + /// JSON-RPC 2.0 specification related types. pub mod jsonrpc; @@ -5,5 +12,4 @@ pub mod jsonrpc; pub mod error; /// Shared types for HTTP -#[cfg(feature = "http")] pub mod http; diff --git a/utils/Cargo.toml b/utils/Cargo.toml new file mode 100644 index 0000000000..e7171ec121 --- /dev/null +++ b/utils/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "jsonrpsee-utils" +version = "0.1.0" +authors = ["Parity Technologies "] +description = "Utilities for jsonrpsee" +edition = "2018" +license = "MIT" + +[dependencies] +futures = "0.3" +globset = "0.4" +hyper = { version = "0.14", features = ["stream"] } +jsonrpsee-types = { path = "../types", version = "0.1" } +lazy_static = "1.4" +log = "0.4" +unicase = "2.6" + +[dev-dependencies] +serde_json = "1.0" +tokio = { version = "1.0", features = ["macros"] } diff --git a/src/utils/http/access_control.rs b/utils/src/http/access_control.rs similarity index 96% rename from src/utils/http/access_control.rs rename to utils/src/http/access_control.rs index f4eefef144..242f332ccb 100644 --- a/src/utils/http/access_control.rs +++ b/utils/src/http/access_control.rs @@ -26,9 +26,9 @@ //! Access control based on http headers -use crate::utils::http::cors::{AccessControlAllowHeaders, AccessControlAllowOrigin}; -use crate::utils::http::hosts::{AllowHosts, Host}; -use crate::utils::http::{cors, hosts, hyper_helpers}; +use crate::http::cors::{AccessControlAllowHeaders, AccessControlAllowOrigin}; +use crate::http::hosts::{AllowHosts, Host}; +use crate::http::{cors, hosts, hyper_helpers}; use hyper::{self, header}; /// Define access on control on http layer diff --git a/src/utils/http/cors.rs b/utils/src/http/cors.rs similarity index 99% rename from src/utils/http/cors.rs rename to utils/src/http/cors.rs index 92df7ca7de..3fb548d5ee 100644 --- a/src/utils/http/cors.rs +++ b/utils/src/http/cors.rs @@ -26,8 +26,8 @@ //! CORS handling utility functions -use crate::utils::http::hosts::{Host, Port}; -use crate::utils::http::matcher::{Matcher, Pattern}; +use crate::http::hosts::{Host, Port}; +use crate::http::matcher::{Matcher, Pattern}; use lazy_static::lazy_static; use std::collections::HashSet; use std::{fmt, ops}; @@ -323,7 +323,7 @@ lazy_static! { #[cfg(test)] mod tests { use super::*; - use crate::utils::http::hosts::Host; + use crate::http::hosts::Host; use std::iter; #[test] diff --git a/src/utils/http/hosts.rs b/utils/src/http/hosts.rs similarity index 99% rename from src/utils/http/hosts.rs rename to utils/src/http/hosts.rs index 1a3cf4b1f0..82c62a119f 100644 --- a/src/utils/http/hosts.rs +++ b/utils/src/http/hosts.rs @@ -26,7 +26,7 @@ //! Host header validation. -use crate::utils::http::matcher::{Matcher, Pattern}; +use crate::http::matcher::{Matcher, Pattern}; use std::collections::HashSet; use std::net::SocketAddr; diff --git a/src/utils/http/hyper_helpers.rs b/utils/src/http/hyper_helpers.rs similarity index 97% rename from src/utils/http/hyper_helpers.rs rename to utils/src/http/hyper_helpers.rs index cb4b2cf066..546ec620b5 100644 --- a/src/utils/http/hyper_helpers.rs +++ b/utils/src/http/hyper_helpers.rs @@ -26,9 +26,8 @@ //! Utility methods relying on hyper -use crate::types::error::GenericTransportError; -use crate::types::http::HttpConfig; use futures::StreamExt; +use jsonrpsee_types::{error::GenericTransportError, http::HttpConfig}; /// Read a hyper response with configured `HTTP` settings. /// @@ -92,7 +91,7 @@ pub fn read_header_values<'a>( #[cfg(test)] mod tests { use super::{read_header_content_length, read_response_to_body, HttpConfig}; - use crate::types::jsonrpc; + use jsonrpsee_types::jsonrpc; #[tokio::test] async fn body_to_request_works() { diff --git a/src/utils/http/matcher.rs b/utils/src/http/matcher.rs similarity index 100% rename from src/utils/http/matcher.rs rename to utils/src/http/matcher.rs diff --git a/src/utils/http/mod.rs b/utils/src/http/mod.rs similarity index 100% rename from src/utils/http/mod.rs rename to utils/src/http/mod.rs diff --git a/utils/src/lib.rs b/utils/src/lib.rs new file mode 100644 index 0000000000..3883215fcb --- /dev/null +++ b/utils/src/lib.rs @@ -0,0 +1 @@ +pub mod http; diff --git a/ws-client/Cargo.toml b/ws-client/Cargo.toml new file mode 100644 index 0000000000..5472b70df1 --- /dev/null +++ b/ws-client/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "jsonrpsee-ws-client" +version = "0.1.0" +authors = ["Parity Technologies ", "Pierre Krieger "] +description = "WebSocket client for JSON-RPC" +edition = "2018" +license = "MIT" + +[dependencies] +async-std = { version = "1.8", features = ["attributes"] } +async-tls = "0.11" +fnv = "1" +futures = "0.3" +jsonrpsee-types = { path = "../types", version = "0.1" } +log = "0.4" +serde = { version = "1", default-features = false, features = ["derive"] } +serde_json = "1" +soketto = "0.4" +pin-project = "1" +thiserror = "1" +url = "2" +webpki = "0.21" + +[dev-dependencies] +jsonrpsee-test-utils = { path = "../test-utils" } +tokio = { version = "1", features = ["full"] } diff --git a/src/client/ws/client.rs b/ws-client/src/client.rs similarity index 95% rename from src/client/ws/client.rs rename to ws-client/src/client.rs index f5defdc177..795a1d7a53 100644 --- a/src/client/ws/client.rs +++ b/ws-client/src/client.rs @@ -24,17 +24,17 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::client::ws::jsonrpc_transport; -use crate::client::ws::manager::{RequestManager, RequestStatus}; -use crate::types::error::Error; -use crate::types::jsonrpc::{self, JsonValue}; - +use crate::jsonrpc_transport; +use crate::manager::{RequestManager, RequestStatus}; use futures::{ channel::{mpsc, oneshot}, prelude::*, sink::SinkExt, }; -use jsonrpc::SubscriptionId; +use jsonrpsee_types::{ + error::Error, + jsonrpc::{self, JsonValue, SubscriptionId}, +}; use std::convert::TryInto; use std::{io, marker::PhantomData}; @@ -44,16 +44,16 @@ use std::{io, marker::PhantomData}; /// > task running in parallel. If this is not desirable, you are encouraged to use the /// > [`RawClient`] struct instead. #[derive(Clone)] -pub struct Client { +pub struct WsClient { /// Channel to send requests to the background task. to_back: mpsc::Sender, /// Config. - config: Config, + config: WsConfig, } #[derive(Copy, Clone, Debug)] /// Configuration. -pub struct Config { +pub struct WsConfig { /// Backend channel for serving requests and notifications. pub request_channel_capacity: usize, /// Backend channel for each unique subscription. @@ -62,7 +62,7 @@ pub struct Config { pub max_request_body_size: usize, } -impl Default for Config { +impl Default for WsConfig { fn default() -> Self { Self { request_channel_capacity: 100, @@ -73,7 +73,7 @@ impl Default for Config { } /// Active subscription on a [`Client`]. -pub struct Subscription { +pub struct WsSubscription { /// Channel to send requests to the background task. to_back: mpsc::Sender, /// Channel from which we receive notifications from the server, as undecoded `JsonValue`s. @@ -126,11 +126,11 @@ enum FrontToBack { SubscriptionClosed(SubscriptionId), } -impl Client { +impl WsClient { /// Initializes a new WebSocket client /// /// Fails when the URL is invalid. - pub async fn new(remote_addr: impl AsRef, config: Config) -> Result { + pub async fn new(remote_addr: impl AsRef, config: WsConfig) -> Result { let (sender, receiver) = jsonrpc_transport::websocket_connection(remote_addr.as_ref()) .await .map_err(|e| Error::TransportError(Box::new(e)))?; @@ -140,7 +140,7 @@ impl Client { async_std::task::spawn(async move { background_task(sender, receiver, from_front, config).await; }); - Ok(Client { to_back, config }) + Ok(Self { to_back, config }) } /// Send a notification to the server. @@ -194,7 +194,7 @@ impl Client { subscribe_method: impl Into, params: impl Into, unsubscribe_method: impl Into, - ) -> Result, Error> { + ) -> Result, Error> { let subscribe_method = subscribe_method.into(); let unsubscribe_method = unsubscribe_method.into(); @@ -224,11 +224,11 @@ impl Client { } }; - Ok(Subscription { to_back: self.to_back.clone(), notifs_rx, marker: PhantomData, id }) + Ok(WsSubscription { to_back: self.to_back.clone(), notifs_rx, marker: PhantomData, id }) } } -impl Subscription +impl WsSubscription where Notif: jsonrpc::DeserializeOwned, { @@ -249,7 +249,7 @@ where } } -impl Drop for Subscription { +impl Drop for WsSubscription { fn drop(&mut self) { // We can't actually guarantee that this goes through. If the background task is busy, then // the channel's buffer will be full, and our unsubscription request will never make it. @@ -265,7 +265,7 @@ async fn background_task( mut sender: jsonrpc_transport::Sender, receiver: jsonrpc_transport::Receiver, mut frontend: mpsc::Receiver, - config: Config, + config: WsConfig, ) { let mut manager = RequestManager::new(); diff --git a/src/client/ws/jsonrpc_transport.rs b/ws-client/src/jsonrpc_transport.rs similarity index 90% rename from src/client/ws/jsonrpc_transport.rs rename to ws-client/src/jsonrpc_transport.rs index 900a5fd9cf..d7412edf66 100644 --- a/src/client/ws/jsonrpc_transport.rs +++ b/ws-client/src/jsonrpc_transport.rs @@ -2,8 +2,8 @@ //! //! Wraps the underlying WebSocket transport with specific JSONRPC details. -use crate::client::ws::transport::{self, WsConnectError, WsNewDnsError}; -use crate::types::jsonrpc; +use crate::transport::{self, WsConnectError, WsNewDnsError}; +use jsonrpsee_types::jsonrpc; /// Creates a new JSONRPC WebSocket connection, represented as a Sender and Receiver pair. pub async fn websocket_connection(target: impl AsRef) -> Result<(Sender, Receiver), WsNewDnsError> { @@ -15,12 +15,12 @@ pub async fn websocket_connection(target: impl AsRef) -> Result<(Sender, Re /// It's a wrapper over `WebSocket sender` with additional `JSONRPC request_id`. pub struct Sender { request_id: u64, - transport: crate::client::ws::transport::Sender, + transport: transport::Sender, } impl Sender { /// Creates a new JSONRPC sender. - pub fn new(transport: crate::client::ws::transport::Sender) -> Self { + pub fn new(transport: transport::Sender) -> Self { Self { transport, request_id: 0 } } @@ -91,12 +91,12 @@ impl Sender { /// JSONRPC WebSocket receiver. pub struct Receiver { - transport: crate::client::ws::transport::Receiver, + transport: transport::Receiver, } impl Receiver { /// Create a new JSONRPC WebSocket receiver. - pub fn new(transport: crate::client::ws::transport::Receiver) -> Self { + pub fn new(transport: transport::Receiver) -> Self { Self { transport } } diff --git a/src/client/ws/mod.rs b/ws-client/src/lib.rs similarity index 80% rename from src/client/ws/mod.rs rename to ws-client/src/lib.rs index 3de12e82ed..f3737896ca 100644 --- a/src/client/ws/mod.rs +++ b/ws-client/src/lib.rs @@ -12,4 +12,4 @@ pub mod transport; #[cfg(test)] mod tests; -pub use client::{Client, Config, Subscription}; +pub use client::{WsClient, WsConfig, WsSubscription}; diff --git a/src/client/ws/manager.rs b/ws-client/src/manager.rs similarity index 98% rename from src/client/ws/manager.rs rename to ws-client/src/manager.rs index b804c58094..6f3bb4bb52 100644 --- a/src/client/ws/manager.rs +++ b/ws-client/src/manager.rs @@ -5,10 +5,12 @@ //! (the specs allow number, string or null but this crate only supports numbers) //! * Subscription ID - unique ID generated by server -use crate::types::error::Error; -use crate::types::jsonrpc::{JsonValue, SubscriptionId}; use fnv::FnvHashMap; use futures::channel::{mpsc, oneshot}; +use jsonrpsee_types::{ + error::Error, + jsonrpc::{JsonValue, SubscriptionId}, +}; use std::collections::hash_map::{Entry, HashMap}; enum Kind { @@ -191,8 +193,8 @@ impl RequestManager { #[cfg(test)] mod tests { use super::{Error, RequestManager}; - use crate::types::jsonrpc::{JsonValue, SubscriptionId}; use futures::channel::{mpsc, oneshot}; + use jsonrpsee_types::jsonrpc::{JsonValue, SubscriptionId}; #[test] fn insert_remove_pending_request_works() { diff --git a/src/client/ws/stream.rs b/ws-client/src/stream.rs similarity index 100% rename from src/client/ws/stream.rs rename to ws-client/src/stream.rs diff --git a/src/client/ws/tests.rs b/ws-client/src/tests.rs similarity index 98% rename from src/client/ws/tests.rs rename to ws-client/src/tests.rs index 9b99cbdf77..c52757acf3 100644 --- a/src/client/ws/tests.rs +++ b/ws-client/src/tests.rs @@ -1,11 +1,9 @@ #![cfg(test)] use crate::client::{WsClient, WsConfig, WsSubscription}; -use crate::types::error::Error; -use crate::types::jsonrpc; - use jsonrpsee_test_utils::helpers::*; use jsonrpsee_test_utils::types::{Id, WebSocketTestServer}; +use jsonrpsee_types::{error::Error, jsonrpc}; fn assert_error_response(response: Result, code: jsonrpc::ErrorCode, message: String) { let expected = jsonrpc::Error { code, message, data: None }; diff --git a/src/client/ws/transport.rs b/ws-client/src/transport.rs similarity index 98% rename from src/client/ws/transport.rs rename to ws-client/src/transport.rs index 44b50f9e26..639e2eee2c 100644 --- a/src/client/ws/transport.rs +++ b/ws-client/src/transport.rs @@ -24,18 +24,17 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc; - use async_std::net::{TcpStream, ToSocketAddrs}; use async_tls::client::TlsStream; use futures::io::{BufReader, BufWriter}; use futures::prelude::*; +use jsonrpsee_types::jsonrpc; use soketto::connection; use soketto::handshake::client::{Client as WsRawClient, ServerResponse}; use std::{borrow::Cow, io, net::SocketAddr, time::Duration}; use thiserror::Error; -type TlsOrPlain = crate::client::ws::stream::EitherStream>; +type TlsOrPlain = crate::stream::EitherStream>; /// Sending end of WebSocket transport. pub struct Sender { diff --git a/ws-server/Cargo.toml b/ws-server/Cargo.toml new file mode 100644 index 0000000000..86c6b5f485 --- /dev/null +++ b/ws-server/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "jsonrpsee-ws-server" +version = "0.1.0" +authors = ["Parity Technologies ", "Pierre Krieger "] +description = "WebSocket server for JSON-RPC" +edition = "2018" +license = "MIT" + +[dependencies] +async-std = { version = "1.8.0", features = ["attributes"] } +bs58 = "0.4" +fnv = "1" +futures = "0.3" +hashbrown = "0.9" +jsonrpsee-types = { path = "../types", version = "0.1" } +log = "0.4" +parking_lot = "0.11" +rand = "0.8" +serde = { version = "1", default-features = false, features = ["derive"] } +serde_json = "1" +soketto = "0.4" +thiserror = "1" + +[dev-dependencies] +jsonrpsee-test-utils = { path = "../test-utils" } +jsonrpsee-ws-client = { path = "../ws-client" } +tokio = { version = "1", features = ["full"] } diff --git a/src/ws/mod.rs b/ws-server/src/lib.rs similarity index 98% rename from src/ws/mod.rs rename to ws-server/src/lib.rs index 73a8133e40..f5e4414f93 100644 --- a/src/ws/mod.rs +++ b/ws-server/src/lib.rs @@ -24,6 +24,8 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +extern crate alloc; + mod raw; mod server; mod transport; diff --git a/src/ws/raw/core.rs b/ws-server/src/raw/core.rs similarity index 98% rename from src/ws/raw/core.rs rename to ws-server/src/raw/core.rs index f4de5045d6..ea09d348cd 100644 --- a/src/ws/raw/core.rs +++ b/ws-server/src/raw/core.rs @@ -24,14 +24,16 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc::wrapped::{batches, Notification, Params}; -use crate::types::jsonrpc::{self, JsonValue}; -use crate::ws::transport::{TransportServerEvent, WsRequestId as RequestId, WsTransportServer}; +use crate::transport::{TransportServerEvent, WsRequestId as RequestId, WsTransportServer}; +use jsonrpsee_types::{ + jsonrpc::wrapped::{batches, Notification, Params}, + jsonrpc::{self, JsonValue}, +}; use alloc::{borrow::ToOwned as _, string::String, vec, vec::Vec}; +use core::convert::TryFrom; use core::{fmt, hash::Hash, num::NonZeroUsize}; use hashbrown::{hash_map::Entry, HashMap}; -use std::convert::TryFrom; /// Wraps around a "raw server" and adds capabilities. /// diff --git a/src/ws/raw/mod.rs b/ws-server/src/raw/mod.rs similarity index 100% rename from src/ws/raw/mod.rs rename to ws-server/src/raw/mod.rs diff --git a/src/http/raw/typed_rp.rs b/ws-server/src/raw/typed_rp.rs similarity index 97% rename from src/http/raw/typed_rp.rs rename to ws-server/src/raw/typed_rp.rs index 6729ee34a1..2fd54aa389 100644 --- a/src/http/raw/typed_rp.rs +++ b/ws-server/src/raw/typed_rp.rs @@ -24,9 +24,9 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::http::raw::RawServerRequest; -use crate::types::jsonrpc; +use crate::raw::RawServerRequest; use core::marker::PhantomData; +use jsonrpsee_types::jsonrpc; /// Allows responding to a server request in a more elegant and strongly-typed fashion. pub struct TypedResponder<'a, T> { diff --git a/src/ws/server.rs b/ws-server/src/server.rs similarity index 98% rename from src/ws/server.rs rename to ws-server/src/server.rs index 020b48316f..422bf53bee 100644 --- a/src/ws/server.rs +++ b/ws-server/src/server.rs @@ -24,10 +24,12 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::error::Error; -use crate::types::jsonrpc::{self, JsonValue}; -use crate::ws::raw::{RawServer, RawServerEvent, RawServerRequestId, RawServerSubscriptionId}; -use crate::ws::transport::WsTransportServer; +use crate::raw::{RawServer, RawServerEvent, RawServerRequestId, RawServerSubscriptionId}; +use crate::transport::WsTransportServer; +use jsonrpsee_types::{ + error::Error, + jsonrpc::{self, JsonValue}, +}; use futures::{channel::mpsc, future::Either, pin_mut, prelude::*}; use parking_lot::Mutex; diff --git a/src/ws/tests.rs b/ws-server/src/tests.rs similarity index 98% rename from src/ws/tests.rs rename to ws-server/src/tests.rs index 04347a2eaf..a9d0a29e17 100644 --- a/src/ws/tests.rs +++ b/ws-server/src/tests.rs @@ -1,16 +1,13 @@ #![cfg(test)] -use crate::types::error::Error; -use crate::types::jsonrpc::JsonValue; -use crate::ws::WsServer; - -use std::net::SocketAddr; - +use crate::WsServer; use futures::channel::oneshot::{self, Sender}; use futures::future::FutureExt; use futures::{pin_mut, select}; use jsonrpsee_test_utils::helpers::*; use jsonrpsee_test_utils::types::{Id, WebSocketTestClient}; +use jsonrpsee_types::{error::Error, jsonrpc::JsonValue}; +use std::net::SocketAddr; /// Spawns a dummy `JSONRPC v2 WebSocket` /// It has two hardcoded methods "say_hello" and "add", one hardcoded notification "notif" diff --git a/src/ws/transport.rs b/ws-server/src/transport.rs similarity index 99% rename from src/ws/transport.rs rename to ws-server/src/transport.rs index 58fb8534eb..04b843ceec 100644 --- a/src/ws/transport.rs +++ b/ws-server/src/transport.rs @@ -24,7 +24,7 @@ // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::types::jsonrpc; +use jsonrpsee_types::jsonrpc; use async_std::net::{TcpListener, TcpStream}; use futures::{channel::mpsc, prelude::*}; From a812a7ecaf1006564db70da0788a8ef8d12d7ea2 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 7 Jan 2021 14:36:12 +0100 Subject: [PATCH 3/5] fmt --- benches/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/benches/src/lib.rs b/benches/src/lib.rs index e69de29bb2..8b13789179 100644 --- a/benches/src/lib.rs +++ b/benches/src/lib.rs @@ -0,0 +1 @@ + From b0ca4bbb33b5b2a66777f47dfe7946f67fcbe004 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 7 Jan 2021 16:07:54 +0100 Subject: [PATCH 4/5] [ci]: remove default features Currently there are no features in the crates, so that check is not needed. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 20d8c8c7ba..0466910d34 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ script: - cargo fmt --all -- --check - cargo build --verbose --all-features - cargo test --verbose --all-features - - cargo build --verbose --no-default-features - cargo check --benches --all-features # TODO: add this back when the documentation has been updated. - RUSTDOCFLAGS="--warn broken_intra_doc_links" cargo doc --verbose --workspace --no-deps --document-private-items From ae069c6ef17f8a2a1898eba59d9271e6a29951a4 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 7 Jan 2021 17:50:40 +0100 Subject: [PATCH 5/5] [http client]: remove unused dependency tokio --- http-client/Cargo.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/http-client/Cargo.toml b/http-client/Cargo.toml index 3d5f44af3e..dd1a6b114a 100644 --- a/http-client/Cargo.toml +++ b/http-client/Cargo.toml @@ -15,11 +15,9 @@ log = "0.4" serde = { version = "1.0", default-features = false, features = ["derive"] } serde_json = "1.0" thiserror = "1.0" -# `macros feature` is only used for tests but enabled globally because `dev-dependencies` -# are leaked. -tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros"] } unicase = "2.6" url = "2.2" [dev-dependencies] jsonrpsee-test-utils = { path = "../test-utils" } +tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros"] }