mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
refactor: rewrite "net" ops to use serde_v8 (#10028)
This commit is contained in:
parent
2c52c0a145
commit
ff5d072702
7 changed files with 206 additions and 175 deletions
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
// Some deserializer fields are only used on Unix and Windows build fails without it
|
// Some deserializer fields are only used on Unix and Windows build fails without it
|
||||||
use super::io::StdFileResource;
|
use super::io::StdFileResource;
|
||||||
|
use super::utils::into_string;
|
||||||
use crate::fs_util::canonicalize_path;
|
use crate::fs_util::canonicalize_path;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
use deno_core::error::bad_resource_id;
|
use deno_core::error::bad_resource_id;
|
||||||
|
@ -108,13 +109,6 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||||
super::reg_json_async(rt, "op_utime_async", op_utime_async);
|
super::reg_json_async(rt, "op_utime_async", op_utime_async);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
|
||||||
s.into_string().map_err(|s| {
|
|
||||||
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
|
||||||
custom_error("InvalidData", message)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct OpenArgs {
|
pub struct OpenArgs {
|
||||||
|
|
|
@ -18,6 +18,7 @@ pub mod timers;
|
||||||
pub mod tls;
|
pub mod tls;
|
||||||
pub mod tty;
|
pub mod tty;
|
||||||
pub mod url;
|
pub mod url;
|
||||||
|
mod utils;
|
||||||
pub mod web_worker;
|
pub mod web_worker;
|
||||||
pub mod webgpu;
|
pub mod webgpu;
|
||||||
pub mod websocket;
|
pub mod websocket;
|
||||||
|
|
|
@ -9,8 +9,6 @@ use deno_core::error::generic_error;
|
||||||
use deno_core::error::null_opbuf;
|
use deno_core::error::null_opbuf;
|
||||||
use deno_core::error::type_error;
|
use deno_core::error::type_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json::json;
|
|
||||||
use deno_core::serde_json::Value;
|
|
||||||
use deno_core::AsyncRefCell;
|
use deno_core::AsyncRefCell;
|
||||||
use deno_core::CancelHandle;
|
use deno_core::CancelHandle;
|
||||||
use deno_core::CancelTryFuture;
|
use deno_core::CancelTryFuture;
|
||||||
|
@ -53,6 +51,39 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||||
super::reg_json_async(rt, "op_dns_resolve", op_dns_resolve);
|
super::reg_json_async(rt, "op_dns_resolve", op_dns_resolve);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct OpConn {
|
||||||
|
pub rid: ResourceId,
|
||||||
|
pub remote_addr: Option<OpAddr>,
|
||||||
|
pub local_addr: Option<OpAddr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(tag = "transport", rename_all = "lowercase")]
|
||||||
|
pub enum OpAddr {
|
||||||
|
Tcp(IpAddr),
|
||||||
|
Udp(IpAddr),
|
||||||
|
#[cfg(unix)]
|
||||||
|
Unix(net_unix::UnixAddr),
|
||||||
|
#[cfg(unix)]
|
||||||
|
UnixPacket(net_unix::UnixAddr),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
/// A received datagram packet (from udp or unixpacket)
|
||||||
|
pub struct OpPacket {
|
||||||
|
pub size: usize,
|
||||||
|
pub remote_addr: OpAddr,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct IpAddr {
|
||||||
|
pub hostname: String,
|
||||||
|
pub port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub(crate) struct AcceptArgs {
|
pub(crate) struct AcceptArgs {
|
||||||
pub rid: ResourceId,
|
pub rid: ResourceId,
|
||||||
|
@ -63,7 +94,7 @@ async fn accept_tcp(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: AcceptArgs,
|
args: AcceptArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
let rid = args.rid;
|
let rid = args.rid;
|
||||||
|
|
||||||
let resource = state
|
let resource = state
|
||||||
|
@ -91,37 +122,36 @@ async fn accept_tcp(
|
||||||
let rid = state
|
let rid = state
|
||||||
.resource_table
|
.resource_table
|
||||||
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"hostname": local_addr.ip().to_string(),
|
hostname: local_addr.ip().to_string(),
|
||||||
"port": local_addr.port(),
|
port: local_addr.port(),
|
||||||
"transport": "tcp",
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"remoteAddr": {
|
hostname: remote_addr.ip().to_string(),
|
||||||
"hostname": remote_addr.ip().to_string(),
|
port: remote_addr.port(),
|
||||||
"port": remote_addr.port(),
|
})),
|
||||||
"transport": "tcp",
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn op_accept(
|
async fn op_accept(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: AcceptArgs,
|
args: AcceptArgs,
|
||||||
_buf: Option<ZeroCopyBuf>,
|
_buf: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
match args.transport.as_str() {
|
match args.transport.as_str() {
|
||||||
"tcp" => accept_tcp(state, args, _buf).await,
|
"tcp" => accept_tcp(state, args, _buf).await,
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
"unix" => net_unix::accept_unix(state, args, _buf).await,
|
"unix" => net_unix::accept_unix(state, args, _buf).await,
|
||||||
_ => Err(generic_error(format!(
|
other => Err(bad_transport(other)),
|
||||||
"Unsupported transport protocol {}",
|
|
||||||
args.transport
|
|
||||||
))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bad_transport(transport: &str) -> AnyError {
|
||||||
|
generic_error(format!("Unsupported transport protocol {}", transport))
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub(crate) struct ReceiveArgs {
|
pub(crate) struct ReceiveArgs {
|
||||||
pub rid: ResourceId,
|
pub rid: ResourceId,
|
||||||
|
@ -132,7 +162,7 @@ async fn receive_udp(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ReceiveArgs,
|
args: ReceiveArgs,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpPacket, AnyError> {
|
||||||
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
|
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
|
||||||
let mut zero_copy = zero_copy.clone();
|
let mut zero_copy = zero_copy.clone();
|
||||||
|
|
||||||
|
@ -149,29 +179,25 @@ async fn receive_udp(
|
||||||
.recv_from(&mut zero_copy)
|
.recv_from(&mut zero_copy)
|
||||||
.try_or_cancel(cancel_handle)
|
.try_or_cancel(cancel_handle)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(json!({
|
Ok(OpPacket {
|
||||||
"size": size,
|
size,
|
||||||
"remoteAddr": {
|
remote_addr: OpAddr::Udp(IpAddr {
|
||||||
"hostname": remote_addr.ip().to_string(),
|
hostname: remote_addr.ip().to_string(),
|
||||||
"port": remote_addr.port(),
|
port: remote_addr.port(),
|
||||||
"transport": "udp",
|
}),
|
||||||
}
|
})
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn op_datagram_receive(
|
async fn op_datagram_receive(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ReceiveArgs,
|
args: ReceiveArgs,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpPacket, AnyError> {
|
||||||
match args.transport.as_str() {
|
match args.transport.as_str() {
|
||||||
"udp" => receive_udp(state, args, zero_copy).await,
|
"udp" => receive_udp(state, args, zero_copy).await,
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
"unixpacket" => net_unix::receive_unix_packet(state, args, zero_copy).await,
|
"unixpacket" => net_unix::receive_unix_packet(state, args, zero_copy).await,
|
||||||
_ => Err(generic_error(format!(
|
other => Err(bad_transport(other)),
|
||||||
"Unsupported transport protocol {}",
|
|
||||||
args.transport
|
|
||||||
))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +213,7 @@ async fn op_datagram_send(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: SendArgs,
|
args: SendArgs,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<usize, AnyError> {
|
||||||
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
|
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
|
||||||
let zero_copy = zero_copy.clone();
|
let zero_copy = zero_copy.clone();
|
||||||
|
|
||||||
|
@ -215,7 +241,7 @@ async fn op_datagram_send(
|
||||||
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
||||||
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
||||||
let byte_length = socket.send_to(&zero_copy, &addr).await?;
|
let byte_length = socket.send_to(&zero_copy, &addr).await?;
|
||||||
Ok(json!(byte_length))
|
Ok(byte_length)
|
||||||
}
|
}
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
SendArgs {
|
SendArgs {
|
||||||
|
@ -239,7 +265,7 @@ async fn op_datagram_send(
|
||||||
.try_borrow_mut()
|
.try_borrow_mut()
|
||||||
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
|
.ok_or_else(|| custom_error("Busy", "Socket already in use"))?;
|
||||||
let byte_length = socket.send_to(&zero_copy, address_path).await?;
|
let byte_length = socket.send_to(&zero_copy, address_path).await?;
|
||||||
Ok(json!(byte_length))
|
Ok(byte_length)
|
||||||
}
|
}
|
||||||
_ => Err(type_error("Wrong argument format!")),
|
_ => Err(type_error("Wrong argument format!")),
|
||||||
}
|
}
|
||||||
|
@ -256,7 +282,7 @@ async fn op_connect(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ConnectArgs,
|
args: ConnectArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
match args {
|
match args {
|
||||||
ConnectArgs {
|
ConnectArgs {
|
||||||
transport,
|
transport,
|
||||||
|
@ -281,19 +307,17 @@ async fn op_connect(
|
||||||
let rid = state_
|
let rid = state_
|
||||||
.resource_table
|
.resource_table
|
||||||
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"hostname": local_addr.ip().to_string(),
|
hostname: local_addr.ip().to_string(),
|
||||||
"port": local_addr.port(),
|
port: local_addr.port(),
|
||||||
"transport": transport,
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"remoteAddr": {
|
hostname: remote_addr.ip().to_string(),
|
||||||
"hostname": remote_addr.ip().to_string(),
|
port: remote_addr.port(),
|
||||||
"port": remote_addr.port(),
|
})),
|
||||||
"transport": transport,
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
ConnectArgs {
|
ConnectArgs {
|
||||||
|
@ -315,17 +339,15 @@ async fn op_connect(
|
||||||
let mut state_ = state.borrow_mut();
|
let mut state_ = state.borrow_mut();
|
||||||
let resource = UnixStreamResource::new(unix_stream.into_split());
|
let resource = UnixStreamResource::new(unix_stream.into_split());
|
||||||
let rid = state_.resource_table.add(resource);
|
let rid = state_.resource_table.add(resource);
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Unix(net_unix::UnixAddr {
|
||||||
"path": local_addr.as_pathname(),
|
path: local_addr.as_pathname().and_then(net_unix::pathstring),
|
||||||
"transport": transport,
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Unix(net_unix::UnixAddr {
|
||||||
"remoteAddr": {
|
path: remote_addr.as_pathname().and_then(net_unix::pathstring),
|
||||||
"path": remote_addr.as_pathname(),
|
})),
|
||||||
"transport": transport,
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
_ => Err(type_error("Wrong argument format!")),
|
_ => Err(type_error("Wrong argument format!")),
|
||||||
}
|
}
|
||||||
|
@ -420,7 +442,7 @@ fn op_listen(
|
||||||
state: &mut OpState,
|
state: &mut OpState,
|
||||||
args: ListenArgs,
|
args: ListenArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
let permissions = state.borrow::<Permissions>();
|
let permissions = state.borrow::<Permissions>();
|
||||||
match args {
|
match args {
|
||||||
ListenArgs {
|
ListenArgs {
|
||||||
|
@ -447,14 +469,20 @@ fn op_listen(
|
||||||
local_addr.ip().to_string(),
|
local_addr.ip().to_string(),
|
||||||
local_addr.port()
|
local_addr.port()
|
||||||
);
|
);
|
||||||
Ok(json!({
|
let ip_addr = IpAddr {
|
||||||
"rid": rid,
|
hostname: local_addr.ip().to_string(),
|
||||||
"localAddr": {
|
port: local_addr.port(),
|
||||||
"hostname": local_addr.ip().to_string(),
|
};
|
||||||
"port": local_addr.port(),
|
Ok(OpConn {
|
||||||
"transport": transport,
|
rid,
|
||||||
},
|
local_addr: Some(match transport.as_str() {
|
||||||
}))
|
"udp" => OpAddr::Udp(ip_addr),
|
||||||
|
"tcp" => OpAddr::Tcp(ip_addr),
|
||||||
|
// NOTE: This could be unreachable!()
|
||||||
|
other => return Err(bad_transport(other)),
|
||||||
|
}),
|
||||||
|
remote_addr: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
ListenArgs {
|
ListenArgs {
|
||||||
|
@ -482,13 +510,19 @@ fn op_listen(
|
||||||
rid,
|
rid,
|
||||||
local_addr.as_pathname().unwrap().display(),
|
local_addr.as_pathname().unwrap().display(),
|
||||||
);
|
);
|
||||||
Ok(json!({
|
let unix_addr = net_unix::UnixAddr {
|
||||||
"rid": rid,
|
path: local_addr.as_pathname().and_then(net_unix::pathstring),
|
||||||
"localAddr": {
|
};
|
||||||
"path": local_addr.as_pathname(),
|
|
||||||
"transport": transport,
|
Ok(OpConn {
|
||||||
},
|
rid,
|
||||||
}))
|
local_addr: Some(match transport.as_str() {
|
||||||
|
"unix" => OpAddr::Unix(unix_addr),
|
||||||
|
"unixpacket" => OpAddr::UnixPacket(unix_addr),
|
||||||
|
other => return Err(bad_transport(other)),
|
||||||
|
}),
|
||||||
|
remote_addr: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
_ => Err(type_error("Wrong argument format!")),
|
_ => Err(type_error("Wrong argument format!")),
|
||||||
|
@ -546,7 +580,7 @@ async fn op_dns_resolve(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ResolveAddrArgs,
|
args: ResolveAddrArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<Vec<DnsReturnRecord>, AnyError> {
|
||||||
let ResolveAddrArgs {
|
let ResolveAddrArgs {
|
||||||
query,
|
query,
|
||||||
record_type,
|
record_type,
|
||||||
|
@ -584,7 +618,7 @@ async fn op_dns_resolve(
|
||||||
|
|
||||||
let resolver = AsyncResolver::tokio(config, opts)?;
|
let resolver = AsyncResolver::tokio(config, opts)?;
|
||||||
|
|
||||||
let results: Vec<DnsReturnRecord> = resolver
|
let results = resolver
|
||||||
.lookup(query, record_type, Default::default())
|
.lookup(query, record_type, Default::default())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| generic_error(format!("{}", e)))?
|
.map_err(|e| generic_error(format!("{}", e)))?
|
||||||
|
@ -592,7 +626,7 @@ async fn op_dns_resolve(
|
||||||
.filter_map(rdata_to_return_record(record_type))
|
.filter_map(rdata_to_return_record(record_type))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(json!(results))
|
Ok(results)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rdata_to_return_record(
|
fn rdata_to_return_record(
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
use super::utils::into_string;
|
||||||
use crate::ops::io::UnixStreamResource;
|
use crate::ops::io::UnixStreamResource;
|
||||||
use crate::ops::net::AcceptArgs;
|
use crate::ops::net::AcceptArgs;
|
||||||
|
use crate::ops::net::OpAddr;
|
||||||
|
use crate::ops::net::OpConn;
|
||||||
|
use crate::ops::net::OpPacket;
|
||||||
use crate::ops::net::ReceiveArgs;
|
use crate::ops::net::ReceiveArgs;
|
||||||
use deno_core::error::bad_resource;
|
use deno_core::error::bad_resource;
|
||||||
use deno_core::error::custom_error;
|
use deno_core::error::custom_error;
|
||||||
use deno_core::error::null_opbuf;
|
use deno_core::error::null_opbuf;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json::json;
|
|
||||||
use deno_core::serde_json::Value;
|
|
||||||
use deno_core::AsyncRefCell;
|
use deno_core::AsyncRefCell;
|
||||||
use deno_core::CancelHandle;
|
use deno_core::CancelHandle;
|
||||||
use deno_core::CancelTryFuture;
|
use deno_core::CancelTryFuture;
|
||||||
|
@ -59,8 +61,7 @@ impl Resource for UnixDatagramResource {
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct UnixAddr {
|
pub struct UnixAddr {
|
||||||
pub path: String,
|
pub path: Option<String>,
|
||||||
pub transport: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -72,7 +73,7 @@ pub(crate) async fn accept_unix(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: AcceptArgs,
|
args: AcceptArgs,
|
||||||
_bufs: Option<ZeroCopyBuf>,
|
_bufs: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
let rid = args.rid;
|
let rid = args.rid;
|
||||||
|
|
||||||
let resource = state
|
let resource = state
|
||||||
|
@ -92,24 +93,22 @@ pub(crate) async fn accept_unix(
|
||||||
let resource = UnixStreamResource::new(unix_stream.into_split());
|
let resource = UnixStreamResource::new(unix_stream.into_split());
|
||||||
let mut state = state.borrow_mut();
|
let mut state = state.borrow_mut();
|
||||||
let rid = state.resource_table.add(resource);
|
let rid = state.resource_table.add(resource);
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Unix(UnixAddr {
|
||||||
"path": local_addr.as_pathname(),
|
path: local_addr.as_pathname().and_then(pathstring),
|
||||||
"transport": "unix",
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Unix(UnixAddr {
|
||||||
"remoteAddr": {
|
path: remote_addr.as_pathname().and_then(pathstring),
|
||||||
"path": remote_addr.as_pathname(),
|
})),
|
||||||
"transport": "unix",
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn receive_unix_packet(
|
pub(crate) async fn receive_unix_packet(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ReceiveArgs,
|
args: ReceiveArgs,
|
||||||
buf: Option<ZeroCopyBuf>,
|
buf: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpPacket, AnyError> {
|
||||||
let mut buf = buf.ok_or_else(null_opbuf)?;
|
let mut buf = buf.ok_or_else(null_opbuf)?;
|
||||||
|
|
||||||
let rid = args.rid;
|
let rid = args.rid;
|
||||||
|
@ -125,13 +124,12 @@ pub(crate) async fn receive_unix_packet(
|
||||||
let cancel = RcRef::map(resource, |r| &r.cancel);
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
||||||
let (size, remote_addr) =
|
let (size, remote_addr) =
|
||||||
socket.recv_from(&mut buf).try_or_cancel(cancel).await?;
|
socket.recv_from(&mut buf).try_or_cancel(cancel).await?;
|
||||||
Ok(json!({
|
Ok(OpPacket {
|
||||||
"size": size,
|
size,
|
||||||
"remoteAddr": {
|
remote_addr: OpAddr::UnixPacket(UnixAddr {
|
||||||
"path": remote_addr.as_pathname(),
|
path: remote_addr.as_pathname().and_then(pathstring),
|
||||||
"transport": "unixpacket",
|
}),
|
||||||
}
|
})
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen_unix(
|
pub fn listen_unix(
|
||||||
|
@ -169,3 +167,7 @@ pub fn listen_unix_packet(
|
||||||
|
|
||||||
Ok((rid, local_addr))
|
Ok((rid, local_addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pathstring(pathname: &Path) -> Option<String> {
|
||||||
|
into_string(pathname.into()).ok()
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
use super::utils::into_string;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
use deno_core::error::{custom_error, type_error, AnyError};
|
use deno_core::error::{type_error, AnyError};
|
||||||
use deno_core::url::Url;
|
use deno_core::url::Url;
|
||||||
use deno_core::OpState;
|
use deno_core::OpState;
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
|
@ -42,14 +43,6 @@ fn op_exec_path(
|
||||||
into_string(path.into_os_string())
|
into_string(path.into_os_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(@AaronO): share this code with fs' into_string()
|
|
||||||
fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
|
||||||
s.into_string().map_err(|s| {
|
|
||||||
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
|
||||||
custom_error("InvalidData", message)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct SetEnv {
|
pub struct SetEnv {
|
||||||
key: String,
|
key: String,
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
use super::io::TcpStreamResource;
|
use super::io::TcpStreamResource;
|
||||||
use super::io::TlsClientStreamResource;
|
use super::io::TlsClientStreamResource;
|
||||||
use super::io::TlsServerStreamResource;
|
use super::io::TlsServerStreamResource;
|
||||||
|
use super::net::IpAddr;
|
||||||
|
use super::net::OpAddr;
|
||||||
|
use super::net::OpConn;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
use crate::resolve_addr::resolve_addr;
|
use crate::resolve_addr::resolve_addr;
|
||||||
use crate::resolve_addr::resolve_addr_sync;
|
use crate::resolve_addr::resolve_addr_sync;
|
||||||
|
@ -11,8 +14,6 @@ use deno_core::error::bad_resource_id;
|
||||||
use deno_core::error::custom_error;
|
use deno_core::error::custom_error;
|
||||||
use deno_core::error::generic_error;
|
use deno_core::error::generic_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json::json;
|
|
||||||
use deno_core::serde_json::Value;
|
|
||||||
use deno_core::AsyncRefCell;
|
use deno_core::AsyncRefCell;
|
||||||
use deno_core::CancelHandle;
|
use deno_core::CancelHandle;
|
||||||
use deno_core::CancelTryFuture;
|
use deno_core::CancelTryFuture;
|
||||||
|
@ -97,7 +98,7 @@ async fn op_start_tls(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: StartTlsArgs,
|
args: StartTlsArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
let rid = args.rid;
|
let rid = args.rid;
|
||||||
|
|
||||||
let mut domain = args.hostname.as_str();
|
let mut domain = args.hostname.as_str();
|
||||||
|
@ -148,26 +149,26 @@ async fn op_start_tls(
|
||||||
.resource_table
|
.resource_table
|
||||||
.add(TlsClientStreamResource::from(tls_stream))
|
.add(TlsClientStreamResource::from(tls_stream))
|
||||||
};
|
};
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"hostname": local_addr.ip().to_string(),
|
hostname: local_addr.ip().to_string(),
|
||||||
"port": local_addr.port(),
|
port: local_addr.port(),
|
||||||
"transport": "tcp",
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"remoteAddr": {
|
hostname: remote_addr.ip().to_string(),
|
||||||
"hostname": remote_addr.ip().to_string(),
|
port: remote_addr.port(),
|
||||||
"port": remote_addr.port(),
|
})),
|
||||||
"transport": "tcp",
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn op_connect_tls(
|
async fn op_connect_tls(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
args: ConnectTlsArgs,
|
args: ConnectTlsArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
|
assert_eq!(args.transport, "tcp");
|
||||||
|
|
||||||
{
|
{
|
||||||
let s = state.borrow();
|
let s = state.borrow();
|
||||||
let permissions = s.borrow::<Permissions>();
|
let permissions = s.borrow::<Permissions>();
|
||||||
|
@ -208,19 +209,17 @@ async fn op_connect_tls(
|
||||||
.resource_table
|
.resource_table
|
||||||
.add(TlsClientStreamResource::from(tls_stream))
|
.add(TlsClientStreamResource::from(tls_stream))
|
||||||
};
|
};
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"hostname": local_addr.ip().to_string(),
|
hostname: local_addr.ip().to_string(),
|
||||||
"port": local_addr.port(),
|
port: local_addr.port(),
|
||||||
"transport": args.transport,
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"remoteAddr": {
|
hostname: remote_addr.ip().to_string(),
|
||||||
"hostname": remote_addr.ip().to_string(),
|
port: remote_addr.port(),
|
||||||
"port": remote_addr.port(),
|
})),
|
||||||
"transport": args.transport,
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_certs(path: &str) -> Result<Vec<Certificate>, AnyError> {
|
fn load_certs(path: &str) -> Result<Vec<Certificate>, AnyError> {
|
||||||
|
@ -307,7 +306,7 @@ fn op_listen_tls(
|
||||||
state: &mut OpState,
|
state: &mut OpState,
|
||||||
args: ListenTlsArgs,
|
args: ListenTlsArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
assert_eq!(args.transport, "tcp");
|
assert_eq!(args.transport, "tcp");
|
||||||
|
|
||||||
let cert_file = args.cert_file;
|
let cert_file = args.cert_file;
|
||||||
|
@ -338,21 +337,21 @@ fn op_listen_tls(
|
||||||
|
|
||||||
let rid = state.resource_table.add(tls_listener_resource);
|
let rid = state.resource_table.add(tls_listener_resource);
|
||||||
|
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"hostname": local_addr.ip().to_string(),
|
hostname: local_addr.ip().to_string(),
|
||||||
"port": local_addr.port(),
|
port: local_addr.port(),
|
||||||
"transport": args.transport,
|
})),
|
||||||
},
|
remote_addr: None,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn op_accept_tls(
|
async fn op_accept_tls(
|
||||||
state: Rc<RefCell<OpState>>,
|
state: Rc<RefCell<OpState>>,
|
||||||
rid: ResourceId,
|
rid: ResourceId,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<Value, AnyError> {
|
) -> Result<OpConn, AnyError> {
|
||||||
let resource = state
|
let resource = state
|
||||||
.borrow()
|
.borrow()
|
||||||
.resource_table
|
.resource_table
|
||||||
|
@ -392,17 +391,15 @@ async fn op_accept_tls(
|
||||||
.add(TlsServerStreamResource::from(tls_stream))
|
.add(TlsServerStreamResource::from(tls_stream))
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(json!({
|
Ok(OpConn {
|
||||||
"rid": rid,
|
rid,
|
||||||
"localAddr": {
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"transport": "tcp",
|
hostname: local_addr.ip().to_string(),
|
||||||
"hostname": local_addr.ip().to_string(),
|
port: local_addr.port(),
|
||||||
"port": local_addr.port()
|
})),
|
||||||
},
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
||||||
"remoteAddr": {
|
hostname: remote_addr.ip().to_string(),
|
||||||
"transport": "tcp",
|
port: remote_addr.port(),
|
||||||
"hostname": remote_addr.ip().to_string(),
|
})),
|
||||||
"port": remote_addr.port()
|
})
|
||||||
}
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
10
runtime/ops/utils.rs
Normal file
10
runtime/ops/utils.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
use deno_core::error::custom_error;
|
||||||
|
use deno_core::error::AnyError;
|
||||||
|
|
||||||
|
/// A utility function to map OsStrings to Strings
|
||||||
|
pub fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
||||||
|
s.into_string().map_err(|s| {
|
||||||
|
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
||||||
|
custom_error("InvalidData", message)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue