Skip to content

Commit 76ce48b

Browse files
committed
Update roundtrip_js.go to guarantee that the DialContext, DialTLSContext, Dial and DialTLS functions in http.Transport are respected, even under JS/WASM
There aren't any particular assumptions that state that the net.Conn has to be an actual socket, but instead might be a kind of virtual network or connection handler. The current API shape tries to use the Fetch API for everything, even when we've tried to tell the Transport not to.
1 parent 37f9a8f commit 76ce48b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/net/http/roundtrip_js.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ var useFakeNetwork = js.Global().Get("fetch").IsUndefined()
4545

4646
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
4747
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
48+
// The Transport has a documented contract that states that if the DialContext or
49+
// DialTLSContext functions are set, they will be used to set up the connections.
50+
// If they aren't set then the documented contract is to use Dial or DialTLS, even
51+
// though they are deprecated. Therefore, if any of these are set, we should obey
52+
// the contract and dial using the regular round-trip instead. Otherwise we will
53+
// end up calling the browser Fetch API unexpectedly.
54+
if t.Dial != nil || t.DialContext != nil || t.DialTLS != nil || t.DialTLSContext != nil {
55+
useFakeNetwork = true
56+
}
57+
58+
// If the browser Fetch API is unavailable, or the above conditions are met, then
59+
// we will lean on fake networking to set up the connection.
4860
if useFakeNetwork {
4961
return t.roundTrip(req)
5062
}

0 commit comments

Comments
 (0)