(&self, ctx: Ctx<'js>)
| 491 | } |
| 492 | |
| 493 | fn clone(&self, ctx: Ctx<'js>) -> Result<Self> { |
| 494 | let headers = if let Some(headers) = &self.headers { |
| 495 | Some(Class::<Headers>::instance( |
| 496 | ctx.clone(), |
| 497 | headers.borrow().clone(), |
| 498 | )?) |
| 499 | } else { |
| 500 | None |
| 501 | }; |
| 502 | |
| 503 | let body_guard = self.body.read().unwrap(); |
| 504 | let cloned_body = match &*body_guard { |
| 505 | BodyVariant::Provided(provided) => { |
| 506 | let Some(provided) = provided else { |
| 507 | return Err(Exception::throw_type( |
| 508 | &ctx, |
| 509 | "Cannot clone request after body has been used", |
| 510 | )); |
| 511 | }; |
| 512 | // For ReadableStream bodies, use tee() to create two branches |
| 513 | if let Some(stream) = provided |
| 514 | .as_object() |
| 515 | .and_then(Class::<ReadableStream>::from_object) |
| 516 | { |
| 517 | let (branch1, branch2) = |
| 518 | llrt_stream_web::tee_readable_stream(ctx.clone(), stream.clone())?; |
| 519 | |
| 520 | // Update original body to use branch1 |
| 521 | drop(body_guard); |
| 522 | let mut body_write = self.body.write().unwrap(); |
| 523 | *body_write = BodyVariant::Provided(Some(branch1.into_value())); |
| 524 | *self.body_stream.write().unwrap() = None; |
| 525 | |
| 526 | return Ok(Self { |
| 527 | url: self.url.clone(), |
| 528 | method: self.method.clone(), |
| 529 | headers, |
| 530 | body: RwLock::new(BodyVariant::Provided(Some(branch2.into_value()))), |
| 531 | body_stream: RwLock::new(None), |
| 532 | signal: self.signal.clone(), |
| 533 | mode: self.mode.clone(), |
| 534 | keepalive: self.keepalive, |
| 535 | agent: self.agent.clone(), |
| 536 | }); |
| 537 | } |
| 538 | BodyVariant::Provided(Some(provided.clone())) |
| 539 | }, |
| 540 | BodyVariant::Empty => BodyVariant::Empty, |
| 541 | }; |
| 542 | drop(body_guard); |
| 543 | |
| 544 | Ok(Self { |
| 545 | url: self.url.clone(), |
| 546 | method: self.method.clone(), |
| 547 | headers, |
| 548 | body: RwLock::new(cloned_body), |
| 549 | body_stream: RwLock::new(None), |
| 550 | signal: self.signal.clone(), |
no test coverage detected