-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit * provide a better interface for errors, rename `RequestMethod` to `Method` * remove method for array buffer and blob in favor of as_raw * prepare for release * add CI, update readme * hide JsError in the docs * fix CI? * Install wasm-pack in CI * misc * websocket API Fixes: ranile/reqwasm#1 * add tests for websocket * update documentation, prepare for release * fix mistake in documentation * Rewrite WebSockets code (#4) * redo websockets * docs + tests * remove gloo-console * fix CI * Add getters for the underlying WebSocket fields * better API * better API part 2 electric boogaloo * deserialize Blob to Vec<u8> (#9) * Update to Rust 2021 and use JsError from gloo-utils (#10) * Update to Rust 2021 and use JsError from gloo-utils * use new toolchain * Add response.binary method to obtain response as Vec<u8> Fixes: ranile/reqwasm#7 * Remove `Clone` impl from WebSocket. When the WebSocket is used with frameworks, passed down as props, it might be `drop`ed automatically, which closes the WebSocket connection. Initially `Clone` was added so sender and receiver can be in different `spawn_local`s but it turns out that `StreamExt::split` solves that problem very well. See #13 for more information about the issue * Rustfmt + ignore editor config files * Fix onclose handling (#14) * feat: feature gate json, websocket and http; enable them by default (#16) * feat: feature gate json support * feat: feature gate weboscket api * ci: check websocket and json features seperately in CI, check no default features * feat: feature gate the http API * refactor: use futures-core and futures-sink instead of depending on whole of futures * ci: test http feature seperately in CI * fix: only compile error conversion funcs if either APIs are enabled * fix: add futures to dev-deps for tests, fix doc test * 0.3.0 * Fix outdated/missing docs * 0.3.1 * Change edition from 2021 to 2018 (#18) * Change edition from 2021 to 2018 * Fix missing import due to edition 2021 prelude * hopefully this will fix the issue (#19) * There's no message * Replace `async-broadcast` with `futures-channel::mpsc` (#21) We no longer need a multi-producer-multi-consumer channel. There's only one consumer as of ranile/reqwasm@445e9a5 * Release 0.4.0 * Fix message ordering not being preserved (#29) The websocket specification guarantees that messages are received in the same order they are sent. The implementation in this library can violate this guarantee because messages are parsed in a spawn_local block before being sent over the channel. When multiple messages are received before the next executor tick the scheduling order of the futures is unspecified. We fix this by performing all operations synchronously. The only part where async is needed is the conversion of Blob to ArrayBuffer which we obsolete by setting the websocket to always receive binary data as ArrayBuffer. * 0.4.1 * move files for gloo merge * remove licence files * gloo-specific patches * fix CI * re-export net from gloo Co-authored-by: Michael Hueschen <mhuesch@users.noreply.github.com> Co-authored-by: Stepan Henek <stepan+github@henek.name> Co-authored-by: Yusuf Bera Ertan <y.bera003.06@protonmail.com> Co-authored-by: Luke Chu <37006668+lukechu10@users.noreply.github.com> Co-authored-by: Valentin <vakevk+github@gmail.com>
- Loading branch information
1 parent
a46a3e5
commit 65fcda7
Showing
13 changed files
with
1,045 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
[package] | ||
name = "gloo-net" | ||
version = "0.1.0" | ||
authors = ["Rust and WebAssembly Working Group", "Muhammad Hamza <muhammadhamza1311@gmail.com>"] | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/hamza1311/reqwasm" | ||
description = "HTTP requests library for WASM Apps" | ||
readme = "README.md" | ||
keywords = ["requests", "http", "wasm", "websockets"] | ||
categories = ["wasm", "web-programming::http-client", "api-bindings"] | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
|
||
[dependencies] | ||
wasm-bindgen = "0.2" | ||
web-sys = "0.3" | ||
js-sys = "0.3" | ||
gloo-utils = { version = "0.1", path = "../utils" } | ||
|
||
wasm-bindgen-futures = "0.4" | ||
futures-core = { version = "0.3", optional = true } | ||
futures-sink = { version = "0.3", optional = true } | ||
|
||
thiserror = "1.0" | ||
|
||
serde = { version = "1.0", features = ["derive"], optional = true } | ||
serde_json = { version = "1.0", optional = true } | ||
|
||
futures-channel = { version = "0.3", optional = true } | ||
pin-project = { version = "1.0", optional = true } | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.3" | ||
futures = "0.3" | ||
|
||
[features] | ||
default = ["json", "websocket", "http"] | ||
|
||
# Enables `.json()` on `Response` | ||
json = ["wasm-bindgen/serde-serialize", "serde", "serde_json"] | ||
# Enables the WebSocket API | ||
websocket = [ | ||
'web-sys/WebSocket', | ||
'web-sys/ErrorEvent', | ||
'web-sys/FileReader', | ||
'web-sys/MessageEvent', | ||
'web-sys/ProgressEvent', | ||
'web-sys/CloseEvent', | ||
'web-sys/BinaryType', | ||
'web-sys/Blob', | ||
"futures-channel", | ||
"pin-project", | ||
"futures-core", | ||
"futures-sink", | ||
] | ||
# Enables the HTTP API | ||
http = [ | ||
'web-sys/Headers', | ||
'web-sys/Request', | ||
'web-sys/RequestInit', | ||
'web-sys/RequestMode', | ||
'web-sys/Response', | ||
'web-sys/Window', | ||
'web-sys/RequestCache', | ||
'web-sys/RequestCredentials', | ||
'web-sys/ObserverCallback', | ||
'web-sys/RequestRedirect', | ||
'web-sys/ReferrerPolicy', | ||
'web-sys/AbortSignal', | ||
'web-sys/ReadableStream', | ||
'web-sys/Blob', | ||
'web-sys/FormData', | ||
] |
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,57 @@ | ||
<div align="center"> | ||
|
||
<h1><code>gloo-net</code></h1> | ||
|
||
<p> | ||
<a href="https://crates.io/crates/gloo-net"><img src="https://img.shields.io/crates/v/gloo-net.svg?style=flat-square" alt="Crates.io version" /></a> | ||
<a href="https://crates.io/crates/gloo-net"><img src="https://img.shields.io/crates/d/gloo-net.svg?style=flat-square" alt="Download" /></a> | ||
<a href="https://docs.rs/gloo-net"><img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs docs" /></a> | ||
</p> | ||
|
||
<h3> | ||
<a href="https://docs.rs/gloo-net">API Docs</a> | ||
<span> | </span> | ||
<a href="https://github.com/rustwasm/gloo/blob/master/CONTRIBUTING.md">Contributing</a> | ||
<span> | </span> | ||
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a> | ||
</h3> | ||
|
||
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub> | ||
</div> | ||
|
||
HTTP requests library for WASM Apps. It provides idiomatic Rust bindings for the `web_sys` `fetch` and `WebSocket` API | ||
|
||
## Examples | ||
|
||
### HTTP | ||
|
||
```rust | ||
let resp = Request::get("/path") | ||
.send() | ||
.await | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
``` | ||
|
||
### WebSocket | ||
|
||
```rust | ||
use reqwasm::websocket::{Message, futures::WebSocket}; | ||
use wasm_bindgen_futures::spawn_local; | ||
use futures::{SinkExt, StreamExt}; | ||
|
||
let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap(); | ||
let (mut write, mut read) = ws.split(); | ||
|
||
spawn_local(async move { | ||
write.send(Message::Text(String::from("test"))).await.unwrap(); | ||
write.send(Message::Text(String::from("test 2"))).await.unwrap(); | ||
}); | ||
|
||
spawn_local(async move { | ||
while let Some(msg) = read.next().await { | ||
console_log!(format!("1. {:?}", msg)) | ||
} | ||
console_log!("WebSocket Closed") | ||
}) | ||
``` |
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,40 @@ | ||
use gloo_utils::errors::JsError; | ||
use thiserror::Error as ThisError; | ||
|
||
/// All the errors returned by this crate. | ||
#[derive(Debug, ThisError)] | ||
pub enum Error { | ||
/// Error returned by JavaScript. | ||
#[error("{0}")] | ||
JsError(JsError), | ||
/// Error returned by `serde` during deserialization. | ||
#[cfg(feature = "json")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "json")))] | ||
#[error("{0}")] | ||
SerdeError( | ||
#[source] | ||
#[from] | ||
serde_json::Error, | ||
), | ||
} | ||
|
||
#[cfg(any(feature = "http", feature = "websocket"))] | ||
pub(crate) use conversion::*; | ||
#[cfg(any(feature = "http", feature = "websocket"))] | ||
mod conversion { | ||
use gloo_utils::errors::JsError; | ||
use std::convert::TryFrom; | ||
use wasm_bindgen::JsValue; | ||
|
||
#[cfg(feature = "http")] | ||
pub(crate) fn js_to_error(js_value: JsValue) -> super::Error { | ||
super::Error::JsError(js_to_js_error(js_value)) | ||
} | ||
|
||
pub(crate) fn js_to_js_error(js_value: JsValue) -> JsError { | ||
match JsError::try_from(js_value) { | ||
Ok(error) => error, | ||
Err(_) => unreachable!("JsValue passed is not an Error type -- this is a bug"), | ||
} | ||
} | ||
} |
Oops, something went wrong.