Bugfix - Non-spec behavour in Request.prototype.text (#2182)

This commit is contained in:
Mees Delzenne 2023-06-26 16:24:25 +02:00 committed by GitHub
parent a10d6df430
commit e0f885e736
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -556,7 +556,12 @@ mod request {
pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result<String> { pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result<String> {
let data = self.take_buffer(ctx).await?; 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())
}
} }
} }
} }

View file

@ -221,7 +221,12 @@ pub mod response {
pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result<String> { pub async fn text<'js>(&self, ctx: Ctx<'js>, args: Rest<()>) -> Result<String> {
let data = self.take_buffer(ctx).await?; 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 // Returns a promise with the response body as text

View file

@ -34,8 +34,11 @@ impl<R: Clone + 'static + Send + Sync> ReadableStream<R> {
pub fn tee(&mut self) -> (ReadableStream<R>, impl Future<Output = ()>) { pub fn tee(&mut self) -> (ReadableStream<R>, impl Future<Output = ()>) {
// replace the stream with a channel driven by as task. // replace the stream with a channel driven by as task.
// TODO: figure out how backpressure works in the stream API. // TODO: figure out how backpressure works in the stream API.
let (send_a, recv_a) = channel::bounded::<R>(16);
let (send_b, recv_b) = channel::bounded::<R>(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::<R>();
let (send_b, recv_b) = channel::unbounded::<R>();
let new_stream = Box::pin(ChannelStream(recv_a)); let new_stream = Box::pin(ChannelStream(recv_a));
let mut old_stream = std::mem::replace(&mut self.0, new_stream); let mut old_stream = std::mem::replace(&mut self.0, new_stream);
let drive = async move { let drive = async move {