1
1
// Copyright 2018-2025 the Deno authors. MIT license.
2
2
3
+ use std:: cell:: RefCell ;
4
+ use std:: rc:: Rc ;
3
5
use std:: str:: FromStr ;
4
6
5
7
use deno_core:: op2;
8
+ use deno_core:: OpState ;
9
+ use deno_error:: JsError ;
10
+ use deno_permissions:: PermissionCheckError ;
6
11
use hyper_util:: client:: legacy:: connect:: dns:: GaiResolver ;
7
12
use hyper_util:: client:: legacy:: connect:: dns:: Name ;
8
13
use serde:: Serialize ;
@@ -15,26 +20,37 @@ struct GetAddrInfoResult {
15
20
address : String ,
16
21
}
17
22
18
- #[ derive( Debug , thiserror:: Error , deno_error:: JsError ) ]
19
- #[ class( generic) ]
20
- #[ error( "Could not resolve the hostname '{hostname}'" ) ]
21
- pub struct GetAddrInfoError {
22
- hostname : String ,
23
+ #[ derive( Debug , thiserror:: Error , JsError ) ]
24
+ pub enum GetAddrInfoError {
25
+ #[ class( inherit) ]
26
+ #[ error( transparent) ]
27
+ Permission ( #[ from] PermissionCheckError ) ,
28
+ #[ class( type ) ]
29
+ #[ error( "Could not resolve the hostname \" {0}\" " ) ]
30
+ Resolution ( String ) ,
23
31
}
24
32
25
33
#[ op2( async , stack_trace) ]
26
34
#[ serde]
27
- pub async fn op_getaddrinfo (
35
+ pub async fn op_getaddrinfo < P > (
36
+ state : Rc < RefCell < OpState > > ,
28
37
#[ string] hostname : String ,
29
- ) -> Result < Vec < GetAddrInfoResult > , GetAddrInfoError > {
38
+ ) -> Result < Vec < GetAddrInfoResult > , GetAddrInfoError >
39
+ where
40
+ P : crate :: NodePermissions + ' static ,
41
+ {
42
+ {
43
+ let mut state_ = state. borrow_mut ( ) ;
44
+ let permissions = state_. borrow_mut :: < P > ( ) ;
45
+ permissions. check_net ( ( hostname. as_str ( ) , None ) , "lookup" ) ?;
46
+ }
30
47
let mut resolver = GaiResolver :: new ( ) ;
31
- let name = Name :: from_str ( & hostname) . map_err ( |_| GetAddrInfoError {
32
- hostname : hostname. clone ( ) ,
33
- } ) ?;
48
+ let name = Name :: from_str ( & hostname)
49
+ . map_err ( |_| GetAddrInfoError :: Resolution ( hostname. clone ( ) ) ) ?;
34
50
resolver
35
51
. call ( name)
36
52
. await
37
- . map_err ( |_| GetAddrInfoError { hostname } )
53
+ . map_err ( |_| GetAddrInfoError :: Resolution ( hostname) )
38
54
. map ( |addrs| {
39
55
addrs
40
56
. into_iter ( )
0 commit comments