mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
Use gotham-like state for ops (#7385)
Provides a concrete state type that can be dynamically added. This is necessary for op crates. * renames BasicState to OpState * async ops take `Rc<RefCell<OpState>>` * sync ops take `&mut OpState` * removes `OpRegistry`, `OpRouter` traits * `get_error_class_fn` moved to OpState * ResourceTable moved to OpState
This commit is contained in:
parent
6f70e6e72b
commit
7c2e7c6608
44 changed files with 1576 additions and 1348 deletions
|
@ -2,10 +2,9 @@
|
|||
|
||||
use super::io::{StreamResource, StreamResourceHolder};
|
||||
use crate::http_util::{create_http_client, HttpBody};
|
||||
use crate::state::State;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpRegistry;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use http::header::HeaderName;
|
||||
use http::header::HeaderValue;
|
||||
|
@ -13,13 +12,14 @@ use http::Method;
|
|||
use reqwest::Client;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::Value;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::From;
|
||||
use std::path::PathBuf;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn init(s: &Rc<State>) {
|
||||
s.register_op_json_async("op_fetch", op_fetch);
|
||||
s.register_op_json_sync("op_create_http_client", op_create_http_client);
|
||||
pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||
super::reg_json_async(rt, "op_fetch", op_fetch);
|
||||
super::reg_json_sync(rt, "op_create_http_client", op_create_http_client);
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -32,7 +32,7 @@ struct FetchArgs {
|
|||
}
|
||||
|
||||
async fn op_fetch(
|
||||
state: Rc<State>,
|
||||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
data: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
|
@ -40,13 +40,15 @@ async fn op_fetch(
|
|||
let url = args.url;
|
||||
|
||||
let client = if let Some(rid) = args.client_rid {
|
||||
let resource_table_ = state.resource_table.borrow();
|
||||
let r = resource_table_
|
||||
let state = state.borrow();
|
||||
let r = state
|
||||
.resource_table
|
||||
.get::<HttpClientResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
r.client.clone()
|
||||
} else {
|
||||
let client_ref = state.http_client.borrow_mut();
|
||||
let cli_state = super::cli_state2(&state);
|
||||
let client_ref = cli_state.http_client.borrow();
|
||||
client_ref.clone()
|
||||
};
|
||||
|
||||
|
@ -66,7 +68,7 @@ async fn op_fetch(
|
|||
)));
|
||||
}
|
||||
|
||||
state.check_net_url(&url_)?;
|
||||
super::cli_state2(&state).check_net_url(&url_)?;
|
||||
|
||||
let mut request = client.request(method, url_);
|
||||
|
||||
|
@ -93,7 +95,7 @@ async fn op_fetch(
|
|||
}
|
||||
|
||||
let body = HttpBody::from(res);
|
||||
let rid = state.resource_table.borrow_mut().add(
|
||||
let rid = state.borrow_mut().resource_table.add(
|
||||
"httpBody",
|
||||
Box::new(StreamResourceHolder::new(StreamResource::HttpBody(
|
||||
Box::new(body),
|
||||
|
@ -128,21 +130,20 @@ struct CreateHttpClientOptions {
|
|||
}
|
||||
|
||||
fn op_create_http_client(
|
||||
state: &State,
|
||||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
let args: CreateHttpClientOptions = serde_json::from_value(args)?;
|
||||
|
||||
if let Some(ca_file) = args.ca_file.clone() {
|
||||
state.check_read(&PathBuf::from(ca_file))?;
|
||||
super::cli_state(state).check_read(&PathBuf::from(ca_file))?;
|
||||
}
|
||||
|
||||
let client = create_http_client(args.ca_file.as_deref()).unwrap();
|
||||
|
||||
let rid = state
|
||||
.resource_table
|
||||
.borrow_mut()
|
||||
.add("httpClient", Box::new(HttpClientResource::new(client)));
|
||||
Ok(json!(rid))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue