Skip to content

Commit 35a36ca

Browse files
committed
Don't override the global value
1 parent 76ce48b commit 35a36ca

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/net/http/roundtrip_js.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build js && wasm
65
// +build js,wasm
76

87
package http
@@ -41,23 +40,19 @@ const jsFetchCreds = "js.fetch:credentials"
4140
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
4241
const jsFetchRedirect = "js.fetch:redirect"
4342

44-
var useFakeNetwork = js.Global().Get("fetch").IsUndefined()
43+
// jsFetchMissing will be true if the Fetch API is not present in
44+
// the browser globals.
45+
var jsFetchMissing = !js.Global().Get("fetch").IsUndefined()
4546

4647
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
4748
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
4849
// The Transport has a documented contract that states that if the DialContext or
4950
// DialTLSContext functions are set, they will be used to set up the connections.
5051
// If they aren't set then the documented contract is to use Dial or DialTLS, even
5152
// 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.
60-
if useFakeNetwork {
53+
// the contract and dial using the regular round-trip instead. Otherwise, we'll try
54+
// to fall back on the Fetch API, unless it's not available.
55+
if t.Dial != nil || t.DialContext != nil || t.DialTLS != nil || t.DialTLSContext != nil || jsFetchMissing {
6156
return t.roundTrip(req)
6257
}
6358

0 commit comments

Comments
 (0)