@@ -5,6 +5,7 @@ use std::{
5
5
io,
6
6
net:: { Ipv4Addr , Ipv6Addr , SocketAddr } ,
7
7
pin:: Pin ,
8
+ result:: Result as StdResult ,
8
9
str:: FromStr ,
9
10
sync:: Arc ,
10
11
task:: { self , Poll } ,
@@ -15,7 +16,7 @@ use std::{
15
16
use hyper_util:: client:: legacy:: connect:: { dns:: Name , HttpConnector } ;
16
17
use llrt_utils:: object:: ObjectExt ;
17
18
use quick_cache:: sync:: Cache ;
18
- use rquickjs:: { Ctx , Exception , Result , Value } ;
19
+ use rquickjs:: Value ;
19
20
use tokio:: sync:: Semaphore ;
20
21
use tower_service:: Service ;
21
22
@@ -143,56 +144,51 @@ impl CachedDnsResolver {
143
144
}
144
145
}
145
146
146
- pub fn lookup_host < ' js > (
147
- ctx : & Ctx < ' js > ,
147
+ pub async fn lookup_host (
148
148
hostname : & str ,
149
- options : Option < Value < ' js > > ,
150
- ) -> Result < ( String , i32 ) > {
149
+ options : Option < Value < ' _ > > ,
150
+ ) -> StdResult < ( String , i32 ) , std :: io :: Error > {
151
151
let mut family = 0 ;
152
152
if let Some ( options) = options {
153
153
family = if let Some ( v) = options. as_int ( ) {
154
154
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
+ ) ) ;
159
159
}
160
160
v
161
161
} else if let Ok ( Some ( v) ) = options. get_optional :: < _ , i32 > ( "family" ) {
162
162
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
+ ) ) ;
167
167
}
168
168
v
169
169
} else {
170
170
0
171
171
}
172
172
}
173
173
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 ) ) ;
187
181
}
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
+ }
192
188
}
193
189
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
+ ) )
198
194
}
0 commit comments