Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(op_crates): move op_crates ops to serde ops #9817

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 42 additions & 51 deletions op_crates/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>,
url: String,
base_url: Option<String>,
headers: Vec<(String, String)>,
client_rid: Option<u32>,
has_body: bool,
}

pub fn op_fetch<FP>(
state: &mut OpState,
args: Value,
args: FetchArgs,
data: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError>
where
FP: FetchPermissions + 'static,
{
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct FetchArgs {
method: Option<String>,
url: String,
base_url: Option<String>,
headers: Vec<(String, String)>,
client_rid: Option<u32>,
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
Expand Down Expand Up @@ -203,19 +200,17 @@ where
}))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchSendArgs {
rid: u32,
}

pub async fn op_fetch_send(
state: Rc<RefCell<OpState>>,
args: Value,
args: FetchSendArgs,
_data: BufVec,
) -> Result<Value, AnyError> {
#[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
Expand Down Expand Up @@ -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<RefCell<OpState>>,
args: Value,
args: FetchRequestWriteArgs,
data: BufVec,
) -> Result<Value, AnyError> {
#[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() {
Expand All @@ -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<RefCell<OpState>>,
args: Value,
args: FetchResponseReadArgs,
data: BufVec,
) -> Result<Value, AnyError> {
#[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 {
Expand Down Expand Up @@ -385,24 +378,22 @@ impl HttpClientResource {
}
}

#[derive(Deserialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct CreateHttpClientOptions {
ca_file: Option<String>,
ca_data: Option<String>,
}

pub fn op_create_http_client<FP>(
state: &mut OpState,
args: Value,
args: CreateHttpClientOptions,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError>
where
FP: FetchPermissions + 'static,
{
#[derive(Deserialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
struct CreateHttpClientOptions {
ca_file: Option<String>,
ca_data: Option<String>,
}

let args: CreateHttpClientOptions = serde_json::from_value(args)?;

if let Some(ca_file) = args.ca_file.clone() {
let permissions = state.borrow::<FP>();
permissions.check_read(&PathBuf::from(ca_file))?;
Expand Down
49 changes: 23 additions & 26 deletions op_crates/url/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String>,
// 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<String>,
set_host: Option<String>,
set_hostname: Option<String>,
set_password: Option<String>,
set_pathname: Option<String>,
set_port: Option<String>,
set_protocol: Option<String>,
set_search: Option<String>,
set_username: Option<String>,
}

/// 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<Value, AnyError> {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct UrlParseArgs {
href: String,
base_href: Option<String>,
// 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<String>,
set_host: Option<String>,
set_hostname: Option<String>,
set_password: Option<String>,
set_pathname: Option<String>,
set_port: Option<String>,
set_protocol: Option<String>,
set_search: Option<String>,
set_username: Option<String>,
}
let args: UrlParseArgs = serde_json::from_value(args)?;
let base_url = args
.base_href
.as_ref()
Expand Down Expand Up @@ -120,24 +119,22 @@ 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<Value, AnyError> {
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))
}

pub fn op_url_stringify_search_params(
_state: &mut deno_core::OpState,
args: Value,
args: Vec<(String, String)>,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
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))
}
Expand Down
31 changes: 11 additions & 20 deletions op_crates/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +83,7 @@ impl WsStreamResource {}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CheckPermissionArgs {
pub struct CheckPermissionArgs {
url: String,
}

Expand All @@ -92,14 +92,12 @@ struct CheckPermissionArgs {
// but actual op that connects WS is async.
pub fn op_ws_check_permission<WP>(
state: &mut OpState,
args: Value,
args: CheckPermissionArgs,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError>
where
WP: WebSocketPermissions + 'static,
{
let args: CheckPermissionArgs = serde_json::from_value(args)?;

state
.borrow::<WP>()
.check_net_url(&url::Url::parse(&args.url)?)?;
Expand All @@ -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<WP>(
state: Rc<RefCell<OpState>>,
args: Value,
args: CreateArgs,
_bufs: BufVec,
) -> Result<Value, AnyError>
where
WP: WebSocketPermissions + 'static,
{
let args: CreateArgs = serde_json::from_value(args)?;

{
let s = state.borrow();
s.borrow::<WP>()
Expand Down Expand Up @@ -217,19 +213,17 @@ where

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendArgs {
pub struct SendArgs {
rid: u32,
kind: String,
text: Option<String>,
}

pub async fn op_ws_send(
state: Rc<RefCell<OpState>>,
args: Value,
args: SendArgs,
bufs: BufVec,
) -> Result<Value, AnyError> {
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()),
Expand All @@ -250,18 +244,17 @@ pub async fn op_ws_send(

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CloseArgs {
pub struct CloseArgs {
rid: u32,
code: Option<u16>,
reason: Option<String>,
}

pub async fn op_ws_close(
state: Rc<RefCell<OpState>>,
args: Value,
args: CloseArgs,
_bufs: BufVec,
) -> Result<Value, AnyError> {
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),
Expand All @@ -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<RefCell<OpState>>,
args: Value,
args: NextEventArgs,
_bufs: BufVec,
) -> Result<Value, AnyError> {
let args: NextEventArgs = serde_json::from_value(args)?;

let resource = state
.borrow_mut()
.resource_table
Expand Down