Skip to content

Commit

Permalink
Remove HeaderExt and AbortSignalExt (#621)
Browse files Browse the repository at this point in the history
* Remove HeaderExt

Closes #617

* Fix deprecation warnings

* feature
  • Loading branch information
kflansburg authored Aug 19, 2024
1 parent 6a5bfc1 commit c7e966c
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 251 deletions.
273 changes: 161 additions & 112 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ async fn main(_req: Request, _env: Env, _ctx: Context) -> Result<Response> {
}

fn str_to_readable_stream(value: &str) -> web_sys::ReadableStream {
let mut req_init = web_sys::RequestInit::new();
req_init.method("POST");
req_init.body(Some(&JsValue::from_str(value)));
let req_init = web_sys::RequestInit::new();
req_init.set_method("POST");
req_init.set_body(&JsValue::from_str(value));
let req = web_sys::Request::new_with_str_and_init("http://internal", &req_init).unwrap();
req.body().unwrap()
}
2 changes: 1 addition & 1 deletion worker-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ wasm-bindgen.workspace = true
cfg-if = "1.0.0"

[dependencies.web-sys]
version = "0.3.63"
version = ">=0.3.70"
features = [
"ReadableStream",
"WritableStream",
Expand Down
4 changes: 0 additions & 4 deletions worker-sys/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
mod abort_controller;
mod abort_signal;
mod cache_storage;
mod headers;
mod request;
mod response;
mod response_init;
mod websocket;

pub use abort_controller::*;
pub use abort_signal::*;
pub use cache_storage::*;
pub use headers::*;
pub use request::*;
pub use response::*;
pub use response_init::*;
Expand Down
42 changes: 0 additions & 42 deletions worker-sys/src/ext/abort_signal.rs

This file was deleted.

42 changes: 0 additions & 42 deletions worker-sys/src/ext/headers.rs

This file was deleted.

2 changes: 1 addition & 1 deletion worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ version = "0.8.4"
default-features = false

[dependencies.web-sys]
version = "0.3.63"
version = ">=0.3.70"
features = [
"File",
"WorkerGlobalScope",
Expand Down
1 change: 0 additions & 1 deletion worker/src/abort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;

use wasm_bindgen::JsValue;
use worker_sys::ext::{AbortControllerExt, AbortSignalExt};

/// An interface that allows you to abort in-flight [Fetch](crate::Fetch) requests.
#[derive(Debug)]
Expand Down
8 changes: 4 additions & 4 deletions worker/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ impl Cache {
key: K,
ignore_method: bool,
) -> Result<Option<Response>> {
let mut options = web_sys::CacheQueryOptions::new();
options.ignore_method(ignore_method);
let options = web_sys::CacheQueryOptions::new();
options.set_ignore_method(ignore_method);

let promise = match key.into() {
CacheKey::Url(url) => self
Expand Down Expand Up @@ -136,8 +136,8 @@ impl Cache {
key: K,
ignore_method: bool,
) -> Result<CacheDeletionOutcome> {
let mut options = web_sys::CacheQueryOptions::new();
options.ignore_method(ignore_method);
let options = web_sys::CacheQueryOptions::new();
options.set_ignore_method(ignore_method);

let promise = match key.into() {
CacheKey::Url(url) => self
Expand Down
8 changes: 4 additions & 4 deletions worker/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl Fetch {
}

async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result<Response> {
let mut init = web_sys::RequestInit::new();
init.signal(signal.map(|x| x.deref()));
let init = web_sys::RequestInit::new();
init.set_signal(signal.map(|x| x.deref()));

let worker: web_sys::WorkerGlobalScope = js_sys::global().unchecked_into();
let promise = worker.fetch_with_str_and_init(url, &init);
Expand All @@ -42,8 +42,8 @@ async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result<Respo
}

async fn fetch_with_request(request: &Request, signal: Option<&AbortSignal>) -> Result<Response> {
let mut init = web_sys::RequestInit::new();
init.signal(signal.map(|x| x.deref()));
let init = web_sys::RequestInit::new();
init.set_signal(signal.map(|x| x.deref()));

let worker: web_sys::WorkerGlobalScope = js_sys::global().unchecked_into();
let req = request.inner();
Expand Down
7 changes: 0 additions & 7 deletions worker/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{
use http::{header::HeaderName, HeaderMap, HeaderValue};
use js_sys::Array;
use wasm_bindgen::JsValue;
use worker_sys::ext::HeadersExt;

/// A [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) representation used in
/// Request and Response objects.
Expand Down Expand Up @@ -65,8 +64,6 @@ impl Headers {
pub fn entries(&self) -> HeaderIterator {
self.0
.entries()
// Header.entries() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
.unwrap()
.into_iter()
// The entries iterator.next() will always return a proper value: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
.map((|a| a.unwrap().into()) as F1)
Expand All @@ -79,8 +76,6 @@ impl Headers {
pub fn keys(&self) -> impl Iterator<Item = String> {
self.0
.keys()
// Header.keys() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys
.unwrap()
.into_iter()
// The keys iterator.next() will always return a proper value containing a string
.map(|a| a.unwrap().as_string().unwrap())
Expand All @@ -91,8 +86,6 @@ impl Headers {
pub fn values(&self) -> impl Iterator<Item = String> {
self.0
.values()
// Header.values() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers/values
.unwrap()
.into_iter()
// The values iterator.next() will always return a proper value containing a string
.map(|a| a.unwrap().as_string().unwrap())
Expand Down
3 changes: 1 addition & 2 deletions worker/src/http/header.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::Result;
use http::{HeaderMap, HeaderName, HeaderValue};
use js_sys::Array;
use worker_sys::ext::HeadersExt;

pub(crate) fn header_map_from_web_sys_headers(
from_headers: web_sys::Headers,
to_headers: &mut HeaderMap,
) -> Result<()> {
// Header.entries() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
for res in from_headers.entries()?.into_iter() {
for res in from_headers.entries().into_iter() {
// The entries iterator.next() will always return a proper value: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
let a: Array = res?.into();
// The entries iterator always returns an array[2] of strings
Expand Down
12 changes: 6 additions & 6 deletions worker/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ pub fn from_wasm(req: web_sys::Request) -> Result<http::Request<Body>> {
pub fn to_wasm<B: http_body::Body<Data = Bytes> + 'static>(
mut req: http::Request<B>,
) -> Result<web_sys::Request> {
let mut init = web_sys::RequestInit::new();
init.method(req.method().as_str());
let init = web_sys::RequestInit::new();
init.set_method(req.method().as_str());
let headers = web_sys_headers_from_header_map(req.headers())?;
init.headers(headers.as_ref());
init.set_headers(headers.as_ref());
let uri = req.uri().to_string();

let signal = req.extensions_mut().remove::<AbortSignal>();
init.signal(signal.as_ref().map(|s| s.inner()));
init.set_signal(signal.as_ref().map(|s| s.inner()));

if let Some(redirect) = req.extensions_mut().remove::<RequestRedirect>() {
init.redirect(redirect.into());
init.set_redirect(redirect.into());
}

if let Some(cf) = req.extensions_mut().remove::<Cf>() {
Expand All @@ -81,7 +81,7 @@ pub fn to_wasm<B: http_body::Body<Data = Bytes> + 'static>(
if !body.is_end_stream() {
let readable_stream =
wasm_streams::ReadableStream::from_stream(BodyStream::new(body)).into_raw();
init.body(Some(readable_stream.as_ref()));
init.set_body(readable_stream.as_ref());
}

Ok(web_sys::Request::new_with_str_and_init(&uri, &init)?)
Expand Down
29 changes: 14 additions & 15 deletions worker/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,20 @@ impl TryFrom<&Request> for web_sys::Request {
impl Request {
/// Construct a new `Request` with an HTTP Method.
pub fn new(uri: &str, method: Method) -> Result<Self> {
web_sys::Request::new_with_str_and_init(
uri,
web_sys::RequestInit::new().method(method.as_ref()),
)
.map(|req| {
let mut req: Request = req.into();
req.immutable = false;
req
})
.map_err(|e| {
Error::JsError(
e.as_string()
.unwrap_or_else(|| "invalid URL or method for Request".to_string()),
)
})
let init = web_sys::RequestInit::new();
init.set_method(method.as_ref());
web_sys::Request::new_with_str_and_init(uri, &init)
.map(|req| {
let mut req: Request = req.into();
req.immutable = false;
req
})
.map_err(|e| {
Error::JsError(
e.as_string()
.unwrap_or_else(|| "invalid URL or method for Request".to_string()),
)
})
}

/// Construct a new `Request` with a `RequestInit` configuration.
Expand Down
12 changes: 7 additions & 5 deletions worker/src/request_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ impl RequestInit {

impl From<&RequestInit> for web_sys::RequestInit {
fn from(req: &RequestInit) -> Self {
let mut inner = web_sys::RequestInit::new();
inner.headers(req.headers.as_ref());
inner.method(req.method.as_ref());
inner.redirect(req.redirect.into());
inner.body(req.body.as_ref());
let inner = web_sys::RequestInit::new();
inner.set_headers(req.headers.as_ref());
inner.set_method(req.method.as_ref());
inner.set_redirect(req.redirect.into());
if let Some(body) = req.body.as_ref() {
inner.set_body(body);
}

// set the Cloudflare-specific `cf` property on FFI RequestInit
let r = ::js_sys::Reflect::set(
Expand Down
4 changes: 2 additions & 2 deletions worker/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ impl ResponseBuilder {
impl From<ResponseBuilder> for web_sys::ResponseInit {
fn from(init: ResponseBuilder) -> Self {
let mut edge_init = web_sys::ResponseInit::new();
edge_init.status(init.status_code);
edge_init.headers(&init.headers.0);
edge_init.set_status(init.status_code);
edge_init.set_headers(&init.headers.0);
if let Some(websocket) = &init.websocket {
edge_init
.websocket(websocket.as_ref())
Expand Down

0 comments on commit c7e966c

Please sign in to comment.