Skip to content
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

Enable setting headers and query parameters. Also add 'fake extensions' mechanism. #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ repository = "https://github.com/johanhelsing/bevy_web_asset"
version = "0.9.0"

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
"bevy_asset",
] }
bevy = { version = "0.14", default-features = false, features = ["bevy_asset"] }
pin-project = "1.1.5"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -26,7 +24,13 @@ surf = { version = "2.3", default-features = false, features = [
] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.67", default-features = false }
web-sys = { version = "0.3.67", default-features = false, features = [
"Window",
"Request",
"Headers",
"RequestInit",
"RequestMode",
] }
js-sys = { version = "0.3", default-features = false }
wasm-bindgen = { version = "0.2", default-features = false }
wasm-bindgen-futures = "0.4"
Expand All @@ -37,6 +41,7 @@ bevy = { version = "0.14", default-features = false, features = [
"bevy_core_pipeline",
"bevy_sprite",
"png",
"jpeg",
"webgl2",
"x11", # GitHub Actions runners don't have libxkbcommon installed, so can't use Wayland
] }
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,30 @@ commands.spawn(SpriteBundle {
});
```

Supports adding headers and query parameters, specified initially when adding the plugin:

```rust ignore
WebAssetPlugin::default()
.enable_fake_extensions() // for URLs which don't have a file extension, add "..png" which won't be sent
.push_header("x-api-key", "somekey") // set a api key for all requests
.push_header("Accept", "application/octet-stream") // we want a binary file
.push_query("quality", "high"), // this appends ?quality=high to the actual requests
```

## Bevy version support

I intend to support the latest bevy release in the `main` branch.

|bevy|bevy_web_asset|
|----|--------------|
|0.14|0.9, main |
|0.13|0.8 |
|0.12|0.7 |
|0.9 |0.5 |
|0.8 |0.4 |
|0.7 |0.3 |
|0.6 |0.2 |
|0.5 |0.1 |
| bevy | bevy_web_asset |
| ---- | -------------- |
| 0.14 | 0.9, main |
| 0.13 | 0.8 |
| 0.12 | 0.7 |
| 0.9 | 0.5 |
| 0.8 | 0.4 |
| 0.7 | 0.3 |
| 0.6 | 0.2 |
| 0.5 | 0.1 |

## License

Expand Down
2 changes: 1 addition & 1 deletion examples/web_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
.add_plugins((
// The web asset plugin must be inserted before the `AssetPlugin` so
// that the AssetPlugin recognizes the new sources.
WebAssetPlugin,
WebAssetPlugin::default(),
DefaultPlugins,
))
.add_systems(Startup, setup)
Expand Down
64 changes: 60 additions & 4 deletions src/web_asset_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, utils::HashMap};

use crate::web_asset_source::*;
use bevy::asset::io::AssetSource;
Expand All @@ -21,17 +21,73 @@ use bevy::asset::io::AssetSource;
/// ));
/// ```
#[derive(Default)]
pub struct WebAssetPlugin;
pub struct WebAssetPlugin {
headers: HashMap<String, Vec<String>>,
query: HashMap<String, String>,
fake_extension: bool,
}

impl WebAssetPlugin {
/// Headers will be passed along with each request
pub fn new(headers: HashMap<String, Vec<String>>, query: HashMap<String, String>) -> Self {
Self {
headers,
query,
fake_extension: false,
}
}

/// Enable "fake extension". This turns "test/example..png" into "test/example", but leaves single dots alone.
pub fn enable_fake_extensions(mut self) -> Self {
self.fake_extension = true;
self
}

/// Push a new header to be sent along every asset load. The same key can be pushed multiple times.
pub fn push_header(mut self, key: impl ToString, value: impl ToString) -> Self {
self.headers
.entry(key.to_string())
.or_insert_with(Vec::new)
.push(value.to_string());
self
}

/// Push a query parameter, which will be appended to the reqeust before its sent
pub fn push_query(mut self, key: impl ToString, value: impl ToString) -> Self {
self.query.insert(key.to_string(), value.to_string());
self
}
}

impl Plugin for WebAssetPlugin {
fn build(&self, app: &mut App) {
let headers = self.headers.clone();
let query = self.query.clone();
let fake_extension = self.fake_extension;
app.register_asset_source(
"http",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Http)),
AssetSource::build().with_reader(move || {
Box::new(WebAssetReader {
protocol: Protocol::Http,
headers: headers.clone(),
query: query.clone(),
fake_extensions: fake_extension,
})
}),
);

let query = self.query.clone();
let headers = self.headers.clone();
app.register_asset_source(
"https",
AssetSource::build().with_reader(|| Box::new(WebAssetReader::Https)),
AssetSource::build().with_reader(move || {
Box::new(WebAssetReader {
protocol: Protocol::Https,
headers: headers.clone(),
query: query.clone(),
fake_extensions: fake_extension,
})
}),
);
}
}
Loading