mirror of
https://github.com/denoland/deno.git
synced 2025-10-02 23:24:37 +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
|
@ -1,10 +1,9 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::state::State;
|
||||
use core::task::Poll;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpRegistry;
|
||||
use deno_core::OpState;
|
||||
use futures::future::poll_fn;
|
||||
use futures::StreamExt;
|
||||
use futures::{ready, SinkExt};
|
||||
|
@ -12,6 +11,7 @@ use http::{Method, Request, Uri};
|
|||
use serde_derive::Deserialize;
|
||||
use serde_json::Value;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::rc::Rc;
|
||||
|
@ -26,11 +26,11 @@ use tokio_tungstenite::tungstenite::{
|
|||
use tokio_tungstenite::{client_async, WebSocketStream};
|
||||
use webpki::DNSNameRef;
|
||||
|
||||
pub fn init(s: &Rc<State>) {
|
||||
s.register_op_json_async("op_ws_create", op_ws_create);
|
||||
s.register_op_json_async("op_ws_send", op_ws_send);
|
||||
s.register_op_json_async("op_ws_close", op_ws_close);
|
||||
s.register_op_json_async("op_ws_next_event", op_ws_next_event);
|
||||
pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||
super::reg_json_async(rt, "op_ws_create", op_ws_create);
|
||||
super::reg_json_async(rt, "op_ws_send", op_ws_send);
|
||||
super::reg_json_async(rt, "op_ws_close", op_ws_close);
|
||||
super::reg_json_async(rt, "op_ws_next_event", op_ws_next_event);
|
||||
}
|
||||
|
||||
type MaybeTlsStream =
|
||||
|
@ -46,13 +46,16 @@ struct CreateArgs {
|
|||
}
|
||||
|
||||
pub async fn op_ws_create(
|
||||
state: Rc<State>,
|
||||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
let args: CreateArgs = serde_json::from_value(args)?;
|
||||
state.check_net_url(&url::Url::parse(&args.url)?)?;
|
||||
let ca_file = state.global_state.flags.ca_file.clone();
|
||||
let ca_file = {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
cli_state.check_net_url(&url::Url::parse(&args.url)?)?;
|
||||
cli_state.global_state.flags.ca_file.clone()
|
||||
};
|
||||
let uri: Uri = args.url.parse().unwrap();
|
||||
let request = Request::builder()
|
||||
.method(Method::GET)
|
||||
|
@ -99,9 +102,9 @@ pub async fn op_ws_create(
|
|||
let (stream, response): (WsStream, Response) =
|
||||
client_async(request, socket).await.unwrap();
|
||||
|
||||
let mut state = state.borrow_mut();
|
||||
let rid = state
|
||||
.resource_table
|
||||
.borrow_mut()
|
||||
.add("webSocketStream", Box::new(stream));
|
||||
|
||||
let protocol = match response.headers().get("Sec-WebSocket-Protocol") {
|
||||
|
@ -130,7 +133,7 @@ struct SendArgs {
|
|||
}
|
||||
|
||||
pub async fn op_ws_send(
|
||||
state: Rc<State>,
|
||||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
|
@ -143,8 +146,9 @@ pub async fn op_ws_send(
|
|||
let rid = args.rid;
|
||||
|
||||
poll_fn(move |cx| {
|
||||
let mut resource_table = state.resource_table.borrow_mut();
|
||||
let stream = resource_table
|
||||
let mut state = state.borrow_mut();
|
||||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
|
||||
|
@ -171,7 +175,7 @@ struct CloseArgs {
|
|||
}
|
||||
|
||||
pub async fn op_ws_close(
|
||||
state: Rc<State>,
|
||||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
|
@ -186,8 +190,9 @@ pub async fn op_ws_close(
|
|||
})));
|
||||
|
||||
poll_fn(move |cx| {
|
||||
let mut resource_table = state.resource_table.borrow_mut();
|
||||
let stream = resource_table
|
||||
let mut state = state.borrow_mut();
|
||||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
|
||||
|
@ -213,14 +218,15 @@ struct NextEventArgs {
|
|||
}
|
||||
|
||||
pub async fn op_ws_next_event(
|
||||
state: Rc<State>,
|
||||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
let args: NextEventArgs = serde_json::from_value(args)?;
|
||||
poll_fn(move |cx| {
|
||||
let mut resource_table = state.resource_table.borrow_mut();
|
||||
let stream = resource_table
|
||||
let mut state = state.borrow_mut();
|
||||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(args.rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
stream
|
||||
|
@ -248,7 +254,7 @@ pub async fn op_ws_next_event(
|
|||
Some(Ok(Message::Pong(_))) => json!({"type": "pong"}),
|
||||
Some(Err(_)) => json!({"type": "error"}),
|
||||
None => {
|
||||
resource_table.close(args.rid).unwrap();
|
||||
state.resource_table.close(args.rid).unwrap();
|
||||
json!({"type": "closed"})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue