|
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -//go:build js && wasm |
6 | 5 | // +build js,wasm
|
7 | 6 |
|
8 | 7 | package http
|
@@ -41,23 +40,19 @@ const jsFetchCreds = "js.fetch:credentials"
|
41 | 40 | // Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
|
42 | 41 | const jsFetchRedirect = "js.fetch:redirect"
|
43 | 42 |
|
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() |
45 | 46 |
|
46 | 47 | // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
|
47 | 48 | func (t *Transport) RoundTrip(req *Request) (*Response, error) {
|
48 | 49 | // The Transport has a documented contract that states that if the DialContext or
|
49 | 50 | // DialTLSContext functions are set, they will be used to set up the connections.
|
50 | 51 | // If they aren't set then the documented contract is to use Dial or DialTLS, even
|
51 | 52 | // 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 { |
61 | 56 | return t.roundTrip(req)
|
62 | 57 | }
|
63 | 58 |
|
|
0 commit comments