Skip to content

Commit 1297c9a

Browse files
authored
chore(ext/node): use BufView natively in http2 (#21688)
Node HTTP/2 was using the default h2 `Bytes` datatype when we can be making using of `BufView` like we do in `Deno.serve`. `fetch` and `Deno.serverHttp` can't make use of `BufView` because they are using `reqwest` which is stuck on hyper 0.x at this time.
1 parent 36536c7 commit 1297c9a

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

cli/ops/jupyter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub async fn op_jupyter_broadcast(
5555
.new_message(&message_type)
5656
.with_content(content)
5757
.with_metadata(metadata)
58-
.with_buffers(buffers.into_iter().map(|b| b.into()).collect())
58+
.with_buffers(buffers.into_iter().map(|b| b.to_vec().into()).collect())
5959
.send(&mut *iopub_socket.lock().await)
6060
.await?;
6161
}

ext/fetch/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::sync::Arc;
1414
use std::task::Context;
1515
use std::task::Poll;
1616

17+
use bytes::Bytes;
1718
use deno_core::anyhow::Error;
1819
use deno_core::error::type_error;
1920
use deno_core::error::AnyError;
@@ -233,7 +234,7 @@ unsafe impl Send for ResourceToBodyAdapter {}
233234
unsafe impl Sync for ResourceToBodyAdapter {}
234235

235236
impl Stream for ResourceToBodyAdapter {
236-
type Item = Result<BufView, Error>;
237+
type Item = Result<Bytes, Error>;
237238

238239
fn poll_next(
239240
self: Pin<&mut Self>,
@@ -250,9 +251,9 @@ impl Stream for ResourceToBodyAdapter {
250251
Ok(buf) if buf.is_empty() => Poll::Ready(None),
251252
Ok(_) => {
252253
this.1 = Some(this.0.clone().read(64 * 1024));
253-
Poll::Ready(Some(res))
254+
Poll::Ready(Some(res.map(|b| b.to_vec().into())))
254255
}
255-
_ => Poll::Ready(Some(res)),
256+
_ => Poll::Ready(Some(res.map(|b| b.to_vec().into()))),
256257
},
257258
}
258259
} else {

ext/http/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ fn http_response(
739739
Some(data) => {
740740
// If a buffer was passed, but isn't compressible, we use it to
741741
// construct a response body.
742-
Ok((HttpResponseWriter::Closed, Bytes::from(data).into()))
742+
Ok((HttpResponseWriter::Closed, data.to_vec().into()))
743743
}
744744
None if compressing => {
745745
// Create a one way pipe that implements tokio's async io traits. To do
@@ -881,7 +881,7 @@ async fn op_http_write_resource(
881881
}
882882
}
883883
HttpResponseWriter::BodyUncompressed(body) => {
884-
let bytes = Bytes::from(view);
884+
let bytes = view.to_vec().into();
885885
if let Err(err) = body.sender().send_data(bytes).await {
886886
assert!(err.is_closed());
887887
// Pull up the failure associated with the transport connection instead.
@@ -930,7 +930,7 @@ async fn op_http_write(
930930
}
931931
}
932932
HttpResponseWriter::BodyUncompressed(body) => {
933-
let bytes = Bytes::from(buf);
933+
let bytes = Bytes::from(buf.to_vec());
934934
match body.sender().send_data(bytes).await {
935935
Ok(_) => Ok(()),
936936
Err(err) => {

ext/node/ops/http2.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use deno_core::futures::future::poll_fn;
1212
use deno_core::op2;
1313
use deno_core::serde::Serialize;
1414
use deno_core::AsyncRefCell;
15+
use deno_core::BufView;
1516
use deno_core::ByteString;
1617
use deno_core::CancelFuture;
1718
use deno_core::CancelHandle;
@@ -34,7 +35,7 @@ use reqwest::header::HeaderValue;
3435
use url::Url;
3536

3637
pub struct Http2Client {
37-
pub client: AsyncRefCell<h2::client::SendRequest<Bytes>>,
38+
pub client: AsyncRefCell<h2::client::SendRequest<BufView>>,
3839
pub url: Url,
3940
}
4041

@@ -46,7 +47,7 @@ impl Resource for Http2Client {
4647

4748
#[derive(Debug)]
4849
pub struct Http2ClientConn {
49-
pub conn: AsyncRefCell<h2::client::Connection<NetworkStream>>,
50+
pub conn: AsyncRefCell<h2::client::Connection<NetworkStream, BufView>>,
5051
cancel_handle: CancelHandle,
5152
}
5253

@@ -63,7 +64,7 @@ impl Resource for Http2ClientConn {
6364
#[derive(Debug)]
6465
pub struct Http2ClientStream {
6566
pub response: AsyncRefCell<h2::client::ResponseFuture>,
66-
pub stream: AsyncRefCell<h2::SendStream<Bytes>>,
67+
pub stream: AsyncRefCell<h2::SendStream<BufView>>,
6768
}
6869

6970
impl Resource for Http2ClientStream {
@@ -89,7 +90,7 @@ impl Resource for Http2ClientResponseBody {
8990

9091
#[derive(Debug)]
9192
pub struct Http2ServerConnection {
92-
pub conn: AsyncRefCell<h2::server::Connection<NetworkStream, Bytes>>,
93+
pub conn: AsyncRefCell<h2::server::Connection<NetworkStream, BufView>>,
9394
}
9495

9596
impl Resource for Http2ServerConnection {
@@ -99,7 +100,7 @@ impl Resource for Http2ServerConnection {
99100
}
100101

101102
pub struct Http2ServerSendResponse {
102-
pub send_response: AsyncRefCell<h2::server::SendResponse<Bytes>>,
103+
pub send_response: AsyncRefCell<h2::server::SendResponse<BufView>>,
103104
}
104105

105106
impl Resource for Http2ServerSendResponse {
@@ -123,7 +124,8 @@ pub async fn op_http2_connect(
123124

124125
let url = Url::parse(&url)?;
125126

126-
let (client, conn) = h2::client::handshake(network_stream).await?;
127+
let (client, conn) =
128+
h2::client::Builder::new().handshake(network_stream).await?;
127129
let mut state = state.borrow_mut();
128130
let client_rid = state.resource_table.add(Http2Client {
129131
client: AsyncRefCell::new(client),
@@ -145,7 +147,7 @@ pub async fn op_http2_listen(
145147
let stream =
146148
take_network_stream_resource(&mut state.borrow_mut().resource_table, rid)?;
147149

148-
let conn = h2::server::handshake(stream).await?;
150+
let conn = h2::server::Builder::new().handshake(stream).await?;
149151
Ok(
150152
state
151153
.borrow_mut()
@@ -349,7 +351,7 @@ pub async fn op_http2_client_send_data(
349351
let mut stream = RcRef::map(&resource, |r| &r.stream).borrow_mut().await;
350352

351353
// TODO(bartlomieju): handle end of stream
352-
stream.send_data(bytes::Bytes::from(data), false)?;
354+
stream.send_data(data.to_vec().into(), false)?;
353355
Ok(())
354356
}
355357

@@ -365,7 +367,7 @@ pub async fn op_http2_client_end_stream(
365367
let mut stream = RcRef::map(&resource, |r| &r.stream).borrow_mut().await;
366368

367369
// TODO(bartlomieju): handle end of stream
368-
stream.send_data(bytes::Bytes::from(vec![]), true)?;
370+
stream.send_data(BufView::empty(), true)?;
369371
Ok(())
370372
}
371373

0 commit comments

Comments
 (0)