0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

chore(ext/fetch): custom arity (#14198)

This commit is contained in:
Divy Srivastava 2022-04-23 22:19:06 +05:30 committed by GitHub
parent d2c80aa26f
commit 2eb8c3b82f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 30 deletions

View file

@ -20,7 +20,12 @@
*/ */
function createHttpClient(options) { function createHttpClient(options) {
options.caCerts ??= []; options.caCerts ??= [];
return new HttpClient(core.opSync("op_fetch_custom_client", options)); return new HttpClient(
core.opSync(
"op_fetch_custom_client",
options,
),
);
} }
class HttpClient { class HttpClient {

View file

@ -67,8 +67,17 @@
* @param {Uint8Array | null} body * @param {Uint8Array | null} body
* @returns {{ requestRid: number, requestBodyRid: number | null }} * @returns {{ requestRid: number, requestBodyRid: number | null }}
*/ */
function opFetch(args, body) { function opFetch(method, url, headers, clientRid, hasBody, bodyLength, body) {
return core.opSync("op_fetch", args, body); return core.opSync(
"op_fetch",
method,
url,
headers,
clientRid,
hasBody,
bodyLength,
body,
);
} }
/** /**
@ -210,14 +219,12 @@
} }
const { requestRid, requestBodyRid, cancelHandleRid } = opFetch( const { requestRid, requestBodyRid, cancelHandleRid } = opFetch(
{ req.method,
method: req.method, req.currentUrl(),
url: req.currentUrl(), req.headerList,
headers: req.headerList, req.clientRid,
clientRid: req.clientRid, reqBody !== null,
hasBody: reqBody !== null, req.body?.length,
bodyLength: req.body?.length,
},
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, reqBody) ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, reqBody)
? reqBody ? reqBody
: null, : null,

View file

@ -174,18 +174,6 @@ pub trait FetchPermissions {
pub fn get_declaration() -> PathBuf { pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts") 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)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct FetchReturn { pub struct FetchReturn {
@ -197,13 +185,18 @@ pub struct FetchReturn {
#[op] #[op]
pub fn op_fetch<FP>( pub fn op_fetch<FP>(
state: &mut OpState, 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>, data: Option<ZeroCopyBuf>,
) -> Result<FetchReturn, AnyError> ) -> Result<FetchReturn, AnyError>
where where
FP: FetchPermissions + 'static, 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)?; let r = state.resource_table.get::<HttpClientResource>(rid)?;
r.client.clone() r.client.clone()
} else { } else {
@ -211,8 +204,8 @@ where
client.clone() client.clone()
}; };
let method = Method::from_bytes(&args.method)?; let method = Method::from_bytes(&method)?;
let url = Url::parse(&args.url)?; let url = Url::parse(&url)?;
// Check scheme before asking for net permission // Check scheme before asking for net permission
let scheme = url.scheme(); let scheme = url.scheme();
@ -251,7 +244,7 @@ where
let mut request = client.request(method.clone(), url); 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 { match data {
None => { None => {
// If no body is passed, we return a writer for streaming the body. // If no body is passed, we return a writer for streaming the body.
@ -259,7 +252,7 @@ where
// If the size of the body is known, we include a content-length // If the size of the body is known, we include a content-length
// header explicitly. // header explicitly.
if let Some(body_size) = args.body_length { if let Some(body_size) = body_length {
request = request =
request.header(CONTENT_LENGTH, HeaderValue::from(body_size)) request.header(CONTENT_LENGTH, HeaderValue::from(body_size))
} }
@ -289,7 +282,7 @@ where
None None
}; };
for (key, value) in args.headers { for (key, value) in headers {
let name = HeaderName::from_bytes(&key) let name = HeaderName::from_bytes(&key)
.map_err(|err| type_error(err.to_string()))?; .map_err(|err| type_error(err.to_string()))?;
let v = HeaderValue::from_bytes(&value) let v = HeaderValue::from_bytes(&value)

View file

@ -115,6 +115,7 @@ pub fn op(attr: TokenStream, item: TokenStream) -> TokenStream {
} }
#[inline] #[inline]
#[allow(clippy::too_many_arguments)]
#original_func #original_func
pub fn v8_func #generics ( pub fn v8_func #generics (