Skip to content

Commit

Permalink
chore(ext/fetch): custom arity (denoland#14198)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Apr 23, 2022
1 parent d2c80aa commit 2eb8c3b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
7 changes: 6 additions & 1 deletion ext/fetch/22_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
*/
function createHttpClient(options) {
options.caCerts ??= [];
return new HttpClient(core.opSync("op_fetch_custom_client", options));
return new HttpClient(
core.opSync(
"op_fetch_custom_client",
options,
),
);
}

class HttpClient {
Expand Down
27 changes: 17 additions & 10 deletions ext/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@
* @param {Uint8Array | null} body
* @returns {{ requestRid: number, requestBodyRid: number | null }}
*/
function opFetch(args, body) {
return core.opSync("op_fetch", args, body);
function opFetch(method, url, headers, clientRid, hasBody, bodyLength, body) {
return core.opSync(
"op_fetch",
method,
url,
headers,
clientRid,
hasBody,
bodyLength,
body,
);
}

/**
Expand Down Expand Up @@ -210,14 +219,12 @@
}

const { requestRid, requestBodyRid, cancelHandleRid } = opFetch(
{
method: req.method,
url: req.currentUrl(),
headers: req.headerList,
clientRid: req.clientRid,
hasBody: reqBody !== null,
bodyLength: req.body?.length,
},
req.method,
req.currentUrl(),
req.headerList,
req.clientRid,
reqBody !== null,
req.body?.length,
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, reqBody)
? reqBody
: null,
Expand Down
31 changes: 12 additions & 19 deletions ext/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,6 @@ pub trait FetchPermissions {
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: ByteString,
url: String,
headers: Vec<(ByteString, ByteString)>,
client_rid: Option<u32>,
has_body: bool,
body_length: Option<u64>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchReturn {
Expand All @@ -197,22 +185,27 @@ pub struct FetchReturn {
#[op]
pub fn op_fetch<FP>(
state: &mut OpState,
args: FetchArgs,
method: ByteString,
url: String,
headers: Vec<(ByteString, ByteString)>,
client_rid: Option<u32>,
has_body: bool,
body_length: Option<u64>,
data: Option<ZeroCopyBuf>,
) -> Result<FetchReturn, AnyError>
where
FP: FetchPermissions + 'static,
{
let client = if let Some(rid) = args.client_rid {
let client = if let Some(rid) = client_rid {
let r = state.resource_table.get::<HttpClientResource>(rid)?;
r.client.clone()
} else {
let client = state.borrow::<reqwest::Client>();
client.clone()
};

let method = Method::from_bytes(&args.method)?;
let url = Url::parse(&args.url)?;
let method = Method::from_bytes(&method)?;
let url = Url::parse(&url)?;

// Check scheme before asking for net permission
let scheme = url.scheme();
Expand Down Expand Up @@ -251,15 +244,15 @@ where

let mut request = client.request(method.clone(), url);

let request_body_rid = if args.has_body {
let request_body_rid = if has_body {
match data {
None => {
// If no body is passed, we return a writer for streaming the body.
let (tx, rx) = mpsc::channel::<std::io::Result<Vec<u8>>>(1);

// If the size of the body is known, we include a content-length
// header explicitly.
if let Some(body_size) = args.body_length {
if let Some(body_size) = body_length {
request =
request.header(CONTENT_LENGTH, HeaderValue::from(body_size))
}
Expand Down Expand Up @@ -289,7 +282,7 @@ where
None
};

for (key, value) in args.headers {
for (key, value) in headers {
let name = HeaderName::from_bytes(&key)
.map_err(|err| type_error(err.to_string()))?;
let v = HeaderValue::from_bytes(&value)
Expand Down
1 change: 1 addition & 0 deletions ops/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
}

#[inline]
#[allow(clippy::too_many_arguments)]
#original_func

pub fn v8_func #generics (
Expand Down

0 comments on commit 2eb8c3b

Please sign in to comment.