From f3790224b01026d54929617a73922999c5d32a2d Mon Sep 17 00:00:00 2001 From: crowlkats <13135287+crowlKats@users.noreply.github.com> Date: Wed, 17 Mar 2021 19:55:50 +0100 Subject: [PATCH] move op_crates ops to serde ops --- op_crates/fetch/lib.rs | 93 +++++++++++++++++--------------------- op_crates/url/lib.rs | 49 ++++++++++---------- op_crates/websocket/lib.rs | 31 +++++-------- 3 files changed, 76 insertions(+), 97 deletions(-) diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index 456358b833dfbf..c3767085287e75 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -9,7 +9,6 @@ use deno_core::error::AnyError; use deno_core::futures::Future; use deno_core::futures::Stream; use deno_core::futures::StreamExt; -use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::Url; @@ -103,27 +102,25 @@ pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts") } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchArgs { + method: Option, + url: String, + base_url: Option, + headers: Vec<(String, String)>, + client_rid: Option, + has_body: bool, +} + pub fn op_fetch( state: &mut OpState, - args: Value, + args: FetchArgs, data: &mut [ZeroCopyBuf], ) -> Result where FP: FetchPermissions + 'static, { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct FetchArgs { - method: Option, - url: String, - base_url: Option, - headers: Vec<(String, String)>, - client_rid: Option, - has_body: bool, - } - - let args: FetchArgs = serde_json::from_value(args)?; - let client = if let Some(rid) = args.client_rid { let r = state .resource_table @@ -203,19 +200,17 @@ where })) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchSendArgs { + rid: u32, +} + pub async fn op_fetch_send( state: Rc>, - args: Value, + args: FetchSendArgs, _data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; - let request = state .borrow_mut() .resource_table @@ -273,18 +268,17 @@ pub async fn op_fetch_send( })) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchRequestWriteArgs { + rid: u32, +} + pub async fn op_fetch_request_write( state: Rc>, - args: Value, + args: FetchRequestWriteArgs, data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; let rid = args.rid; let buf = match data.len() { @@ -304,18 +298,17 @@ pub async fn op_fetch_request_write( Ok(json!({})) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchResponseReadArgs { + rid: u32, +} + pub async fn op_fetch_response_read( state: Rc>, - args: Value, + args: FetchResponseReadArgs, data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; let rid = args.rid; if data.len() != 1 { @@ -385,24 +378,22 @@ impl HttpClientResource { } } +#[derive(Deserialize, Default, Debug)] +#[serde(rename_all = "camelCase")] +#[serde(default)] +pub struct CreateHttpClientOptions { + ca_file: Option, + ca_data: Option, +} + pub fn op_create_http_client( state: &mut OpState, - args: Value, + args: CreateHttpClientOptions, _zero_copy: &mut [ZeroCopyBuf], ) -> Result where FP: FetchPermissions + 'static, { - #[derive(Deserialize, Default, Debug)] - #[serde(rename_all = "camelCase")] - #[serde(default)] - struct CreateHttpClientOptions { - ca_file: Option, - ca_data: Option, - } - - let args: CreateHttpClientOptions = serde_json::from_value(args)?; - if let Some(ca_file) = args.ca_file.clone() { let permissions = state.borrow::(); permissions.check_read(&PathBuf::from(ca_file))?; diff --git a/op_crates/url/lib.rs b/op_crates/url/lib.rs index a655d8c34be042..3b55e539c5214e 100644 --- a/op_crates/url/lib.rs +++ b/op_crates/url/lib.rs @@ -4,7 +4,6 @@ use deno_core::error::generic_error; use deno_core::error::type_error; use deno_core::error::uri_error; use deno_core::error::AnyError; -use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::form_urlencoded; @@ -17,31 +16,31 @@ use serde::Serialize; use std::panic::catch_unwind; use std::path::PathBuf; +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct UrlParseArgs { + href: String, + base_href: Option, + // If one of the following are present, this is a setter call. Apply the + // proper `Url::set_*()` method after (re)parsing `href`. + set_hash: Option, + set_host: Option, + set_hostname: Option, + set_password: Option, + set_pathname: Option, + set_port: Option, + set_protocol: Option, + set_search: Option, + set_username: Option, +} + /// Parse `UrlParseArgs::href` with an optional `UrlParseArgs::base_href`, or an /// optional part to "set" after parsing. Return `UrlParts`. pub fn op_url_parse( _state: &mut deno_core::OpState, - args: Value, + args: UrlParseArgs, _zero_copy: &mut [ZeroCopyBuf], ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct UrlParseArgs { - href: String, - base_href: Option, - // If one of the following are present, this is a setter call. Apply the - // proper `Url::set_*()` method after (re)parsing `href`. - set_hash: Option, - set_host: Option, - set_hostname: Option, - set_password: Option, - set_pathname: Option, - set_port: Option, - set_protocol: Option, - set_search: Option, - set_username: Option, - } - let args: UrlParseArgs = serde_json::from_value(args)?; let base_url = args .base_href .as_ref() @@ -120,11 +119,10 @@ pub fn op_url_parse( pub fn op_url_parse_search_params( _state: &mut deno_core::OpState, - args: Value, + args: String, _zero_copy: &mut [ZeroCopyBuf], ) -> Result { - let search: String = serde_json::from_value(args)?; - let search_params: Vec<_> = form_urlencoded::parse(search.as_bytes()) + let search_params: Vec<_> = form_urlencoded::parse(args.as_bytes()) .into_iter() .collect(); Ok(json!(search_params)) @@ -132,12 +130,11 @@ pub fn op_url_parse_search_params( pub fn op_url_stringify_search_params( _state: &mut deno_core::OpState, - args: Value, + args: Vec<(String, String)>, _zero_copy: &mut [ZeroCopyBuf], ) -> Result { - let search_params: Vec<(String, String)> = serde_json::from_value(args)?; let search = form_urlencoded::Serializer::new(String::new()) - .extend_pairs(search_params) + .extend_pairs(args) .finish(); Ok(json!(search)) } diff --git a/op_crates/websocket/lib.rs b/op_crates/websocket/lib.rs index c914f11c52a2e8..665444c9dbe438 100644 --- a/op_crates/websocket/lib.rs +++ b/op_crates/websocket/lib.rs @@ -18,7 +18,7 @@ use deno_core::JsRuntime; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; -use deno_core::{serde_json, ZeroCopyBuf}; +use deno_core::ZeroCopyBuf; use http::{Method, Request, Uri}; use serde::Deserialize; @@ -83,7 +83,7 @@ impl WsStreamResource {} #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct CheckPermissionArgs { +pub struct CheckPermissionArgs { url: String, } @@ -92,14 +92,12 @@ struct CheckPermissionArgs { // but actual op that connects WS is async. pub fn op_ws_check_permission( state: &mut OpState, - args: Value, + args: CheckPermissionArgs, _zero_copy: &mut [ZeroCopyBuf], ) -> Result where WP: WebSocketPermissions + 'static, { - let args: CheckPermissionArgs = serde_json::from_value(args)?; - state .borrow::() .check_net_url(&url::Url::parse(&args.url)?)?; @@ -109,21 +107,19 @@ where #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct CreateArgs { +pub struct CreateArgs { url: String, protocols: String, } pub async fn op_ws_create( state: Rc>, - args: Value, + args: CreateArgs, _bufs: BufVec, ) -> Result where WP: WebSocketPermissions + 'static, { - let args: CreateArgs = serde_json::from_value(args)?; - { let s = state.borrow(); s.borrow::() @@ -217,7 +213,7 @@ where #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct SendArgs { +pub struct SendArgs { rid: u32, kind: String, text: Option, @@ -225,11 +221,9 @@ struct SendArgs { pub async fn op_ws_send( state: Rc>, - args: Value, + args: SendArgs, bufs: BufVec, ) -> Result { - let args: SendArgs = serde_json::from_value(args)?; - let msg = match args.kind.as_str() { "text" => Message::Text(args.text.unwrap()), "binary" => Message::Binary(bufs[0].to_vec()), @@ -250,7 +244,7 @@ pub async fn op_ws_send( #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct CloseArgs { +pub struct CloseArgs { rid: u32, code: Option, reason: Option, @@ -258,10 +252,9 @@ struct CloseArgs { pub async fn op_ws_close( state: Rc>, - args: Value, + args: CloseArgs, _bufs: BufVec, ) -> Result { - let args: CloseArgs = serde_json::from_value(args)?; let rid = args.rid; let msg = Message::Close(args.code.map(|c| CloseFrame { code: CloseCode::from(c), @@ -283,17 +276,15 @@ pub async fn op_ws_close( #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct NextEventArgs { +pub struct NextEventArgs { rid: u32, } pub async fn op_ws_next_event( state: Rc>, - args: Value, + args: NextEventArgs, _bufs: BufVec, ) -> Result { - let args: NextEventArgs = serde_json::from_value(args)?; - let resource = state .borrow_mut() .resource_table