From e0f885e736ee2b28e773f722f92c5da00c5d51d1 Mon Sep 17 00:00:00 2001 From: Mees Delzenne Date: Mon, 26 Jun 2023 16:24:25 +0200 Subject: [PATCH] Bugfix - Non-spec behavour in `Request.prototype.text` (#2182) --- lib/src/fnc/script/fetch/classes/request.rs | 7 ++++++- lib/src/fnc/script/fetch/classes/response/mod.rs | 7 ++++++- lib/src/fnc/script/fetch/stream.rs | 7 +++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/src/fnc/script/fetch/classes/request.rs b/lib/src/fnc/script/fetch/classes/request.rs index 96c70f2f..f57c982e 100644 --- a/lib/src/fnc/script/fetch/classes/request.rs +++ b/lib/src/fnc/script/fetch/classes/request.rs @@ -556,7 +556,12 @@ mod request { pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result { let data = self.take_buffer(ctx).await?; - Ok(String::from_utf8(data.to_vec())?) + // Skip UTF-BOM + if data.starts_with(&[0xEF, 0xBB, 0xBF]) { + Ok(String::from_utf8_lossy(&data[3..]).into_owned()) + } else { + Ok(String::from_utf8_lossy(&data).into_owned()) + } } } } diff --git a/lib/src/fnc/script/fetch/classes/response/mod.rs b/lib/src/fnc/script/fetch/classes/response/mod.rs index e92d198e..87a0223b 100644 --- a/lib/src/fnc/script/fetch/classes/response/mod.rs +++ b/lib/src/fnc/script/fetch/classes/response/mod.rs @@ -221,7 +221,12 @@ pub mod response { pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result { let data = self.take_buffer(ctx).await?; - Ok(String::from_utf8(data.to_vec())?) + // Skip UTF-BOM + if data.starts_with(&[0xEF, 0xBB, 0xBF]) { + Ok(String::from_utf8_lossy(&data[3..]).into_owned()) + } else { + Ok(String::from_utf8_lossy(&data).into_owned()) + } } // Returns a promise with the response body as text diff --git a/lib/src/fnc/script/fetch/stream.rs b/lib/src/fnc/script/fetch/stream.rs index 6f088587..542d7e9d 100644 --- a/lib/src/fnc/script/fetch/stream.rs +++ b/lib/src/fnc/script/fetch/stream.rs @@ -34,8 +34,11 @@ impl ReadableStream { pub fn tee(&mut self) -> (ReadableStream, impl Future) { // replace the stream with a channel driven by as task. // TODO: figure out how backpressure works in the stream API. - let (send_a, recv_a) = channel::bounded::(16); - let (send_b, recv_b) = channel::bounded::(16); + + // Unbounded, otherwise when one channel gets awaited it might block forever because the + // other channel fills up. + let (send_a, recv_a) = channel::unbounded::(); + let (send_b, recv_b) = channel::unbounded::(); let new_stream = Box::pin(ChannelStream(recv_a)); let mut old_stream = std::mem::replace(&mut self.0, new_stream); let drive = async move {