Skip to content

Commit a37e2ea

Browse files
move initializers to wasip1/net_wasip1.go
Signed-off-by: Achille Roussel <[email protected]>
1 parent 499c4b4 commit a37e2ea

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

wasip1/dial_wasip1.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ package wasip1
33
import (
44
"context"
55
"net"
6-
"net/http"
76
"os"
87

98
"github.com/stealthrocket/net/internal/syscall"
109
)
1110

12-
func init() {
13-
if t, ok := http.DefaultTransport.(*http.Transport); ok {
14-
t.DialContext = DialContext
15-
}
16-
}
17-
1811
// Dial connects to the address on the named network.
1912
func Dial(network, address string) (net.Conn, error) {
2013
addr, err := lookupAddr("dial", network, address)

wasip1/net_wasip1.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package wasip1
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net"
7+
"net/http"
8+
)
9+
10+
func dialResolverNotSupported(ctx context.Context, network, address string) (net.Conn, error) {
11+
// The net.Resolver type makes a call to net.DialUDP to determine which
12+
// resolved addresses are reachable, which does not go through its Dial
13+
// hook. As a result, it is unusable on GOOS=wasip1 because it fails
14+
// even when the Dial function is set because WASI preview 1 does not
15+
// have a mechanism for opening UDP sockets.
16+
//
17+
// Instead of having (often indirect) use of the net.Resolver crash, we
18+
// override the Dial function to error earlier in the resolver lifecycle
19+
// with an error which is more explicit to the end user.
20+
return nil, errors.New("net.Resolver not supported on GOOS=wasip1")
21+
}
22+
23+
func init() {
24+
net.DefaultResolver.Dial = dialResolverNotSupported
25+
26+
if t, ok := http.DefaultTransport.(*http.Transport); ok {
27+
t.DialContext = DialContext
28+
}
29+
}
30+
31+
func newOpError(op string, addr net.Addr, err error) error {
32+
return &net.OpError{
33+
Op: op,
34+
Net: addr.Network(),
35+
Addr: addr,
36+
Err: err,
37+
}
38+
}
39+
40+
type netAddr struct {
41+
network string
42+
address string
43+
}
44+
45+
func (na *netAddr) Network() string { return na.address }
46+
func (na *netAddr) String() string { return na.address }

0 commit comments

Comments
 (0)