Skip to content

Commit 4b2df21

Browse files
committed
remove unnecessary crate
1 parent aac569a commit 4b2df21

File tree

4 files changed

+47
-124
lines changed

4 files changed

+47
-124
lines changed

Cargo.lock

-79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/llrt_dns_cache/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ name = "llrt_dns_cache"
1212
path = "src/lib.rs"
1313

1414
[dependencies]
15-
dns-lookup = "2"
1615
hyper = { version = "1", features = ["client"] }
1716
hyper-rustls = { version = "0.27", default-features = false, features = [
1817
"webpki-roots",

libs/llrt_dns_cache/src/lib.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
io,
66
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
77
pin::Pin,
8+
result::Result as StdResult,
89
str::FromStr,
910
sync::Arc,
1011
task::{self, Poll},
@@ -15,7 +16,7 @@ use std::{
1516
use hyper_util::client::legacy::connect::{dns::Name, HttpConnector};
1617
use llrt_utils::object::ObjectExt;
1718
use quick_cache::sync::Cache;
18-
use rquickjs::{Ctx, Exception, Result, Value};
19+
use rquickjs::Value;
1920
use tokio::sync::Semaphore;
2021
use tower_service::Service;
2122

@@ -143,56 +144,51 @@ impl CachedDnsResolver {
143144
}
144145
}
145146

146-
pub fn lookup_host<'js>(
147-
ctx: &Ctx<'js>,
147+
pub async fn lookup_host(
148148
hostname: &str,
149-
options: Option<Value<'js>>,
150-
) -> Result<(String, i32)> {
149+
options: Option<Value<'_>>,
150+
) -> StdResult<(String, i32), std::io::Error> {
151151
let mut family = 0;
152152
if let Some(options) = options {
153153
family = if let Some(v) = options.as_int() {
154154
if !matches!(v, 4 | 6) {
155-
Err(Exception::throw_message(
156-
ctx,
157-
"If options is an integer, then it must be 4 or 6",
158-
))?;
155+
return Err(io::Error::new::<String>(
156+
io::ErrorKind::InvalidInput,
157+
"If options is an integer, then it must be 4 or 6".into(),
158+
));
159159
}
160160
v
161161
} else if let Ok(Some(v)) = options.get_optional::<_, i32>("family") {
162162
if !matches!(v, 4 | 6 | 0) {
163-
Err(Exception::throw_message(
164-
ctx,
165-
"If family record is exist, then it must be 4, 6, or 0",
166-
))?;
163+
return Err(io::Error::new::<String>(
164+
io::ErrorKind::InvalidInput,
165+
"If family record is exist, then it must be 4, 6, or 0".into(),
166+
));
167167
}
168168
v
169169
} else {
170170
0
171171
}
172172
}
173173

174-
match dns_lookup::lookup_host(hostname) {
175-
Ok(ips) => {
176-
for ip in ips {
177-
if matches!(family, 4 | 0) {
178-
if let Ok(ipv4) = Ipv4Addr::from_str(&ip.to_string()) {
179-
return Ok((ipv4.to_string(), 4));
180-
}
181-
}
182-
if matches!(family, 6 | 0) {
183-
if let Ok(ipv6) = Ipv6Addr::from_str(&ip.to_string()) {
184-
return Ok((ipv6.to_string(), 6));
185-
}
186-
}
174+
let addrs = tokio::net::lookup_host((hostname, 0)).await?;
175+
let addrs = addrs.collect::<Vec<_>>();
176+
177+
for ip in addrs {
178+
if matches!(family, 4 | 0) {
179+
if let Ok(ipv4) = Ipv4Addr::from_str(&ip.to_string()) {
180+
return Ok((ipv4.to_string(), 4));
187181
}
188-
},
189-
Err(err) => {
190-
Err(Exception::throw_message(ctx, &err.to_string()))?;
191-
},
182+
}
183+
if matches!(family, 6 | 0) {
184+
if let Ok(ipv6) = Ipv6Addr::from_str(&ip.to_string()) {
185+
return Ok((ipv6.to_string(), 6));
186+
}
187+
}
192188
}
193189

194-
Err(Exception::throw_message(
195-
ctx,
196-
"No values ware found matching the criteria",
197-
))?
190+
Err(io::Error::new::<String>(
191+
io::ErrorKind::NotFound,
192+
"No values ware found matching the criteria".into(),
193+
))
198194
}

modules/llrt_dns/src/lib.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
use llrt_context::CtxExtension;
34
use llrt_dns_cache::lookup_host;
45
use llrt_utils::{
56
module::{export_default, ModuleInfo},
@@ -18,16 +19,22 @@ fn lookup<'js>(ctx: Ctx<'js>, hostname: String, args: Rest<Value<'js>>) -> Resul
1819
.and_then(|v| v.into_function())
1920
.or_throw_msg(&ctx, "Callback parameter is not a function")?;
2021

21-
match lookup_host(&ctx, &hostname, args_iter.next()) {
22-
Ok((address, family)) => {
23-
() = cb.call((Null.into_js(&ctx), address, family))?;
24-
Ok::<_, Error>(())
25-
},
26-
Err(err) => {
27-
() = cb.call((Exception::from_message(ctx, &err.to_string()),))?;
28-
Ok(())
29-
},
30-
}
22+
ctx.clone().spawn_exit(async move {
23+
match lookup_host(&hostname, args_iter.next())
24+
.await
25+
.or_throw(&ctx)
26+
{
27+
Ok((address, family)) => {
28+
() = cb.call((Null.into_js(&ctx), address, family))?;
29+
Ok::<_, Error>(())
30+
},
31+
Err(err) => {
32+
() = cb.call((Exception::from_message(ctx, &err.to_string()),))?;
33+
Ok(())
34+
},
35+
}
36+
})?;
37+
Ok(())
3138
}
3239

3340
pub struct DnsModule;

0 commit comments

Comments
 (0)