Skip to content

Commit 4c5d979

Browse files
net/http: disable fetch on NodeJS
NodeJS 18 introduced support for the fetch API for making HTTP requests. This broke all wasm tests that were relying on NodeJS falling back to the fake network implementation in net_fake.go. Disable the fetch API on NodeJS to get tests passing. Fixes #57613 Change-Id: Icb2cce6d5289d812da798e07366f8ac26b5f82cb Reviewed-on: https://go-review.googlesource.com/c/go/+/463976 Reviewed-by: Evan Phoenix <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 3875258 commit 4c5d979

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/net/http/roundtrip_js.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ const jsFetchRedirect = "js.fetch:redirect"
4444
// the browser globals.
4545
var jsFetchMissing = js.Global().Get("fetch").IsUndefined()
4646

47+
// jsFetchDisabled will be true if the "process" global is present.
48+
// We use this as an indicator that we're running in Node.js. We
49+
// want to disable the Fetch API in Node.js because it breaks
50+
// our wasm tests. See https://go.dev/issue/57613 for more information.
51+
var jsFetchDisabled = !js.Global().Get("process").IsUndefined()
52+
4753
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
4854
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
4955
// The Transport has a documented contract that states that if the DialContext or
@@ -52,7 +58,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
5258
// though they are deprecated. Therefore, if any of these are set, we should obey
5359
// the contract and dial using the regular round-trip instead. Otherwise, we'll try
5460
// 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+
if t.Dial != nil || t.DialContext != nil || t.DialTLS != nil || t.DialTLSContext != nil || jsFetchMissing || jsFetchDisabled {
5662
return t.roundTrip(req)
5763
}
5864

0 commit comments

Comments
 (0)