1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

refactor: rewrite ext/node/http2 to op2 macro (#20629)

This commit is contained in:
Bartek Iwańczuk 2023-09-23 18:25:36 +02:00 committed by GitHub
parent 06297d952d
commit 99dd8097c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,7 @@ use bytes::Bytes;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::future::poll_fn; use deno_core::futures::future::poll_fn;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::ByteString; use deno_core::ByteString;
@ -108,11 +109,12 @@ impl Resource for Http2ServerSendResponse {
} }
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_connect( pub async fn op_http2_connect(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
url: String, #[string] url: String,
) -> Result<(ResourceId, ResourceId), AnyError> { ) -> Result<(ResourceId, ResourceId), AnyError> {
// No permission check necessary because we're using an existing connection // No permission check necessary because we're using an existing connection
let network_stream = { let network_stream = {
@ -135,10 +137,11 @@ pub async fn op_http2_connect(
Ok((client_rid, conn_rid)) Ok((client_rid, conn_rid))
} }
#[op] #[op2(async)]
#[smi]
pub async fn op_http2_listen( pub async fn op_http2_listen(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
let stream = let stream =
take_network_stream_resource(&mut state.borrow_mut().resource_table, rid)?; take_network_stream_resource(&mut state.borrow_mut().resource_table, rid)?;
@ -220,12 +223,13 @@ pub async fn op_http2_accept(
} }
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_send_response( pub async fn op_http2_send_response(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
status: u16, #[smi] status: u16,
headers: Vec<(ByteString, ByteString)>, #[serde] headers: Vec<(ByteString, ByteString)>,
) -> Result<(ResourceId, u32), AnyError> { ) -> Result<(ResourceId, u32), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -251,10 +255,10 @@ pub async fn op_http2_send_response(
Ok((rid, stream_id.into())) Ok((rid, stream_id.into()))
} }
#[op] #[op2(async)]
pub async fn op_http2_poll_client_connection( pub async fn op_http2_poll_client_connection(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state.borrow().resource_table.get::<Http2ClientConn>(rid)?; let resource = state.borrow().resource_table.get::<Http2ClientConn>(rid)?;
@ -273,14 +277,15 @@ pub async fn op_http2_poll_client_connection(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_client_request( pub async fn op_http2_client_request(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
client_rid: ResourceId, #[smi] client_rid: ResourceId,
// TODO(bartlomieju): maybe use a vector with fixed layout to save sending // TODO(bartlomieju): maybe use a vector with fixed layout to save sending
// 4 strings of keys? // 4 strings of keys?
mut pseudo_headers: HashMap<String, String>, #[serde] mut pseudo_headers: HashMap<String, String>,
headers: Vec<(ByteString, ByteString)>, #[serde] headers: Vec<(ByteString, ByteString)>,
) -> Result<(ResourceId, u32), AnyError> { ) -> Result<(ResourceId, u32), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -331,11 +336,11 @@ pub async fn op_http2_client_request(
Ok((stream_rid, stream_id.into())) Ok((stream_rid, stream_id.into()))
} }
#[op] #[op2(async)]
pub async fn op_http2_client_send_data( pub async fn op_http2_client_send_data(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
stream_rid: ResourceId, #[smi] stream_rid: ResourceId,
data: JsBuffer, #[buffer] data: JsBuffer,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -348,10 +353,10 @@ pub async fn op_http2_client_send_data(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
pub async fn op_http2_client_end_stream( pub async fn op_http2_client_end_stream(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
stream_rid: ResourceId, #[smi] stream_rid: ResourceId,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -364,11 +369,11 @@ pub async fn op_http2_client_end_stream(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
pub async fn op_http2_client_reset_stream( pub async fn op_http2_client_reset_stream(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
stream_rid: ResourceId, #[smi] stream_rid: ResourceId,
code: u32, #[smi] code: u32,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -379,11 +384,11 @@ pub async fn op_http2_client_reset_stream(
Ok(()) Ok(())
} }
#[op] #[op2(async)]
pub async fn op_http2_client_send_trailers( pub async fn op_http2_client_send_trailers(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
stream_rid: ResourceId, #[smi] stream_rid: ResourceId,
trailers: Vec<(ByteString, ByteString)>, #[serde] trailers: Vec<(ByteString, ByteString)>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -411,10 +416,11 @@ pub struct Http2ClientResponse {
status_code: u16, status_code: u16,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_client_get_response( pub async fn op_http2_client_get_response(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
stream_rid: ResourceId, #[smi] stream_rid: ResourceId,
) -> Result<Http2ClientResponse, AnyError> { ) -> Result<Http2ClientResponse, AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -479,10 +485,11 @@ fn poll_data_or_trailers(
} }
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_client_get_response_body_chunk( pub async fn op_http2_client_get_response_body_chunk(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
body_rid: ResourceId, #[smi] body_rid: ResourceId,
) -> Result<(Option<Vec<u8>>, bool), AnyError> { ) -> Result<(Option<Vec<u8>>, bool), AnyError> {
let resource = state let resource = state
.borrow() .borrow()
@ -518,10 +525,11 @@ pub async fn op_http2_client_get_response_body_chunk(
} }
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_http2_client_get_response_trailers( pub async fn op_http2_client_get_response_trailers(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
body_rid: ResourceId, #[smi] body_rid: ResourceId,
) -> Result<Option<Vec<(ByteString, ByteString)>>, AnyError> { ) -> Result<Option<Vec<(ByteString, ByteString)>>, AnyError> {
let resource = state let resource = state
.borrow() .borrow()