diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-06 10:33:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 10:33:43 +0200 |
commit | ff5d072702aee52882787ea85dd73573a8f8f316 (patch) | |
tree | 346d9ac446bbb307edaac0ce965b36dfe511305e /runtime/ops/net_unix.rs | |
parent | 2c52c0a145337050fee03313a7c5698b761969dc (diff) |
refactor: rewrite "net" ops to use serde_v8 (#10028)
Diffstat (limited to 'runtime/ops/net_unix.rs')
-rw-r--r-- | runtime/ops/net_unix.rs | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/runtime/ops/net_unix.rs b/runtime/ops/net_unix.rs index 86c5ab8a0..4a2fbf1de 100644 --- a/runtime/ops/net_unix.rs +++ b/runtime/ops/net_unix.rs @@ -1,14 +1,16 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use super::utils::into_string; use crate::ops::io::UnixStreamResource; 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 deno_core::error::bad_resource; use deno_core::error::custom_error; use deno_core::error::null_opbuf; use deno_core::error::AnyError; -use deno_core::serde_json::json; -use deno_core::serde_json::Value; use deno_core::AsyncRefCell; use deno_core::CancelHandle; use deno_core::CancelTryFuture; @@ -59,8 +61,7 @@ impl Resource for UnixDatagramResource { #[derive(Serialize)] pub struct UnixAddr { - pub path: String, - pub transport: String, + pub path: Option<String>, } #[derive(Deserialize)] @@ -72,7 +73,7 @@ pub(crate) async fn accept_unix( state: Rc<RefCell<OpState>>, args: AcceptArgs, _bufs: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<OpConn, AnyError> { let rid = args.rid; let resource = state @@ -92,24 +93,22 @@ pub(crate) async fn accept_unix( let resource = UnixStreamResource::new(unix_stream.into_split()); let mut state = state.borrow_mut(); let rid = state.resource_table.add(resource); - Ok(json!({ - "rid": rid, - "localAddr": { - "path": local_addr.as_pathname(), - "transport": "unix", - }, - "remoteAddr": { - "path": remote_addr.as_pathname(), - "transport": "unix", - } - })) + Ok(OpConn { + rid, + local_addr: Some(OpAddr::Unix(UnixAddr { + path: local_addr.as_pathname().and_then(pathstring), + })), + remote_addr: Some(OpAddr::Unix(UnixAddr { + path: remote_addr.as_pathname().and_then(pathstring), + })), + }) } pub(crate) async fn receive_unix_packet( state: Rc<RefCell<OpState>>, args: ReceiveArgs, buf: Option<ZeroCopyBuf>, -) -> Result<Value, AnyError> { +) -> Result<OpPacket, AnyError> { let mut buf = buf.ok_or_else(null_opbuf)?; let rid = args.rid; @@ -125,13 +124,12 @@ pub(crate) async fn receive_unix_packet( let cancel = RcRef::map(resource, |r| &r.cancel); let (size, remote_addr) = socket.recv_from(&mut buf).try_or_cancel(cancel).await?; - Ok(json!({ - "size": size, - "remoteAddr": { - "path": remote_addr.as_pathname(), - "transport": "unixpacket", - } - })) + Ok(OpPacket { + size, + remote_addr: OpAddr::UnixPacket(UnixAddr { + path: remote_addr.as_pathname().and_then(pathstring), + }), + }) } pub fn listen_unix( @@ -169,3 +167,7 @@ pub fn listen_unix_packet( Ok((rid, local_addr)) } + +pub fn pathstring(pathname: &Path) -> Option<String> { + into_string(pathname.into()).ok() +} |