Skip to content
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
54 changes: 2 additions & 52 deletions core/runtime/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/fetch

use crate::fetch::headers::JsHeaders;
use crate::fetch::headers_iterator::{HeadersIterator, IterationKind};
use crate::fetch::request::{JsRequest, RequestInit};
use crate::fetch::response::JsResponse;
use boa_engine::class::Class;
use boa_engine::object::FunctionObjectBuilder;
use boa_engine::property::PropertyDescriptor;
use boa_engine::realm::Realm;
use boa_engine::{
Context, Finalize, JsData, JsError, JsObject, JsResult, JsString, JsSymbol, JsValue,
NativeObject, Trace, boa_module, js_error, js_string, native_function::NativeFunction,
Context, Finalize, JsData, JsError, JsObject, JsResult, JsString, JsValue, NativeObject, Trace,
boa_module, js_error,
};
use either::Either;
use http::{HeaderName, HeaderValue, Request as HttpRequest, Request};
Expand Down Expand Up @@ -209,24 +206,6 @@ pub mod js_module {
}
}

fn headers_symbol_iterator(
this: &JsValue,
_: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let this_object = this.as_object().ok_or_else(
|| js_error!(TypeError: "`Headers.prototype[Symbol.iterator]` requires a `Headers` object"),
)?;

let Ok(headers) = this_object.clone().downcast::<JsHeaders>() else {
return Err(
js_error!(TypeError: "`Headers.prototype[Symbol.iterator]` requires a `Headers` object"),
);
};

HeadersIterator::create_headers_iterator(headers, IterationKind::KeyAndValue, context)
}

/// Register the `fetch` function in the realm, as well as ALL supporting classes.
/// Pass `None` as the realm to register globally.
///
Expand All @@ -243,34 +222,5 @@ pub fn register<F: Fetcher>(
context.insert_data(FetcherRc(Rc::new(fetcher)));
}
js_module::boa_register::<F>(realm.clone(), context)?;

// TODO(#4688): Replace this manual `[Symbol.iterator]` wiring once `#[boa(class)]`
// supports symbol-named methods.
let headers_proto = match realm {
Some(realm) => realm.get_class::<JsHeaders>(),
None => context.get_global_class::<JsHeaders>(),
}
.ok_or_else(|| js_error!(Error: "Headers class should be registered"))?
.prototype();

let iterator = FunctionObjectBuilder::new(
context.realm(),
NativeFunction::from_fn_ptr(headers_symbol_iterator),
)
.name(js_string!("[Symbol.iterator]"))
.length(0)
.constructor(false)
.build();

headers_proto.define_property_or_throw(
JsSymbol::iterator(),
PropertyDescriptor::builder()
.value(iterator)
.writable(true)
.enumerable(false)
.configurable(true),
context,
)?;

Ok(())
}
2 changes: 0 additions & 2 deletions core/runtime/src/fetch/tests/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ fn headers_iterator_throws_on_invalid_this() {
throw Error("expected the call above to throw");
} catch (e) {
assert(e instanceof TypeError);
assertEq(e.message, "`Headers.prototype[Symbol.iterator]` requires a `Headers` object");
}

try {
const iterator = Headers.prototype[Symbol.iterator].call(1);
throw Error("expected the call above to throw");
} catch (e) {
assert(e instanceof TypeError);
assertEq(e.message, "`Headers.prototype[Symbol.iterator]` requires a `Headers` object");
}
"#,
),
Expand Down