Created
September 12, 2023 17:12
-
-
Save ydnar/774e41013b1ebab665e4850494fc775e to your computer and use it in GitHub Desktop.
platform package for GOOS=wasip1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package platform | |
import ( | |
"os" | |
"path/filepath" | |
) | |
// ExecutableDir returns the directory containing the current executable, | |
// or when an error occurs, the current working directory. | |
func ExecutableDir() (string, error) { | |
path, err := os.Executable() | |
if err == nil { | |
return filepath.Dir(path), nil | |
} | |
return os.Getwd() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package platform | |
import ( | |
"context" | |
"net" | |
"net/http" | |
) | |
func init() { | |
// Ensure http.DefaultTransport uses our DialContext. | |
if t, ok := http.DefaultTransport.(*http.Transport); ok { | |
t.DialContext = DialContext | |
} | |
} | |
// A ContextDialer implements the DialContext method, e.g. net.Dialer. | |
type ContextDialer interface { | |
DialContext(ctx context.Context, network, addr string) (net.Conn, error) | |
} | |
type contextDialer struct{} | |
func (*contextDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { | |
return DialContext(ctx, network, addr) | |
} | |
// DialerDefault is the default ContextDialer for other packages in this module. | |
var DialerDefault ContextDialer = &contextDialer{} | |
// DialContext opens a new net.Conn, optionally forcing IPv4. | |
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) { | |
network = filterNetwork(network) | |
return dialContext(ctx, network, addr) | |
} | |
// Listen opens a net.Listener, , optionally forcing IPv4. | |
func Listen(network, addr string) (net.Listener, error) { | |
network = filterNetwork(network) | |
return listen(network, addr) | |
} | |
func filterNetwork(network string) string { | |
if !ForceIPv4 { | |
return network | |
} | |
switch network { | |
case "tcp", "tcp6": | |
return "tcp4" | |
case "udp", "udp6": | |
return "udp4" | |
case "ip", "ip6": | |
return "ip4" | |
} | |
return network | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build !wasip1 | |
package platform | |
import "net" | |
const ( | |
// ForceIPv4 is set to false by default, to allow IPv6 networking. | |
ForceIPv4 = false | |
// TCP is set to "tcp" by default. | |
// Certain platforms may elect to set this to "tcp4" or "tcp6". | |
TCP = "tcp" | |
// UDP is set to "udp" by default. | |
// Certain platforms may elect to set this to "udp4" or "udp6". | |
UDP = "udp" | |
) | |
// dialContext opens a new net.Conn. | |
var dialContext = (&net.Dialer{}).DialContext | |
// Listen opens a net.Listener. | |
var listen = net.Listen |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build wasip1 | |
package platform | |
import ( | |
"github.com/stealthrocket/net/wasip1" | |
) | |
const ( | |
// ForceIPv4 is set to true because Wazero/wasi-go doesn’t support IPv6. | |
ForceIPv4 = true | |
// TCP is set to "tcp4" because Wazero/wasi-go doesn’t support IPv6. | |
TCP = "tcp4" | |
// UDP is set to "udp4" because Wazero/wasi-go doesn’t support IPv6. | |
UDP = "udp4" | |
) | |
// dialContext opens a new net.Conn. | |
var dialContext = wasip1.DialContext | |
// listen opens a net.Listener. | |
var listen = wasip1.Listen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment