Add JSFetch Options to Transport
diff --git a/src/net/http/jsfetch.go b/src/net/http/jsfetch.go
new file mode 100644
index 0000000..62bdb21
--- /dev/null
+++ b/src/net/http/jsfetch.go
@@ -0,0 +1,70 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package http
+
+// JSFetchMode is an option to the Fetch API mode setting.
+//
+// This type is only used when GOOS=js and GOARCH=wasm.
+// On other platforms, these values are defined but ignored.
+//
+// See https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
+type JSFetchMode string
+
+const (
+ // JSFetchModeSameOrigin restricts the request to same-origin requests.
+ // This is the default mode.
+ JSFetchModeSameOrigin JSFetchMode = "same-origin"
+
+ // JSFetchModeCORS enables Cross-Origin Resource Sharing.
+ JSFetchModeCORS JSFetchMode = "cors"
+
+ // JSFetchModeNoCORS allows requests for resources from other origins
+ // that do not support CORS, but with limited access to the response.
+ JSFetchModeNoCORS JSFetchMode = "no-cors"
+
+ // JSFetchModeNavigate is used only by HTML navigation.
+ JSFetchModeNavigate JSFetchMode = "navigate"
+)
+
+// JSFetchCredentials is an option to the Fetch API credentials setting.
+//
+// This type is only used when GOOS=js and GOARCH=wasm.
+// On other platforms, these values are defined but ignored.
+//
+// See https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
+type JSFetchCredentials string
+
+const (
+ // JSFetchCredentialsSameOrigin sends credentials for same-origin requests.
+ // This is the default credentials mode.
+ JSFetchCredentialsSameOrigin JSFetchCredentials = "same-origin"
+
+ // JSFetchCredentialsOmit never sends or receives credentials.
+ JSFetchCredentialsOmit JSFetchCredentials = "omit"
+
+ // JSFetchCredentialsInclude always sends and receives credentials,
+ // even for cross-origin requests.
+ JSFetchCredentialsInclude JSFetchCredentials = "include"
+)
+
+// JSFetchRedirect is an option to the Fetch API redirect setting.
+//
+// This type is only used when GOOS=js and GOARCH=wasm.
+// On other platforms, these values are defined but ignored.
+//
+// See https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
+type JSFetchRedirect string
+
+const (
+ // JSFetchRedirectFollow automatically follows redirects.
+ // This is the default redirect mode.
+ JSFetchRedirectFollow JSFetchRedirect = "follow"
+
+ // JSFetchRedirectError returns an error if a redirect occurs.
+ JSFetchRedirectError JSFetchRedirect = "error"
+
+ // JSFetchRedirectManual allows manual handling of redirects.
+ JSFetchRedirectManual JSFetchRedirect = "manual"
+)
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go
index d375751..4f86bc7 100644
--- a/src/net/http/roundtrip_js.go
+++ b/src/net/http/roundtrip_js.go
@@ -81,19 +81,37 @@
// See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// for options available.
opt.Set("method", req.Method)
- opt.Set("credentials", "same-origin")
+
+ // Set fetch 'credentials', default to "same-origin"
+ credentials := string(t.JSFetchCredentials)
+ if credentials == "" {
+ credentials = string(JSFetchCredentialsSameOrigin)
+ }
if h := req.Header.Get(jsFetchCreds); h != "" {
- opt.Set("credentials", h)
+ credentials = h
req.Header.Del(jsFetchCreds)
}
+ opt.Set("credentials", credentials)
+
+ // Set fetch 'mode'
+ mode := string(t.JSFetchMode)
if h := req.Header.Get(jsFetchMode); h != "" {
- opt.Set("mode", h)
+ mode = h
req.Header.Del(jsFetchMode)
}
+ if mode != "" {
+ opt.Set("mode", mode)
+ }
+
+ // Set fetch 'redirect'
+ redirect := string(t.JSFetchRedirect)
if h := req.Header.Get(jsFetchRedirect); h != "" {
- opt.Set("redirect", h)
+ redirect = h
req.Header.Del(jsFetchRedirect)
}
+ if redirect != "" {
+ opt.Set("redirect", redirect)
+ }
if !ac.IsUndefined() {
opt.Set("signal", ac.Get("signal"))
}
diff --git a/src/net/http/transport.go b/src/net/http/transport.go
index 26a25d2..5831dbe 100644
--- a/src/net/http/transport.go
+++ b/src/net/http/transport.go
@@ -310,6 +310,36 @@
// If ForceAttemptHTTP2 is true, or if TLSNextProto contains an "h2" entry,
// the default is HTTP/1 and HTTP/2.
Protocols *Protocols
+
+ // JSFetchMode specifies the mode option for the Fetch API when using
+ // the WASM/JS platform. Valid values are defined by the JSFetchMode type.
+ // If empty, JSFetchModeSameOrigin is used.
+ //
+ // This field is only used when GOOS=js and GOARCH=wasm.
+ // On other platforms, it is ignored.
+ //
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
+ JSFetchMode JSFetchMode
+
+ // JSFetchCredentials specifies the credentials option for the Fetch API
+ // when using the WASM/JS platform. Valid values are defined by the
+ // JSFetchCredentials type. If empty, JSFetchCredentialsSameOrigin is used.
+ //
+ // This field is only used when GOOS=js and GOARCH=wasm.
+ // On other platforms, it is ignored.
+ //
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
+ JSFetchCredentials JSFetchCredentials
+
+ // JSFetchRedirect specifies the redirect option for the Fetch API
+ // when using the WASM/JS platform. Valid values are defined by the
+ // JSFetchRedirect type. If empty, JSFetchRedirectFollow is used.
+ //
+ // This field is only used when GOOS=js and GOARCH=wasm.
+ // On other platforms, it is ignored.
+ //
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
+ JSFetchRedirect JSFetchRedirect
}
func (t *Transport) writeBufferSize() int {
@@ -358,6 +388,9 @@
ForceAttemptHTTP2: t.ForceAttemptHTTP2,
WriteBufferSize: t.WriteBufferSize,
ReadBufferSize: t.ReadBufferSize,
+ JSFetchMode: t.JSFetchMode,
+ JSFetchCredentials: t.JSFetchCredentials,
+ JSFetchRedirect: t.JSFetchRedirect,
}
if t.TLSClientConfig != nil {
t2.TLSClientConfig = t.TLSClientConfig.Clone()
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Congratulations on opening your first change. Thank you for your contribution!
Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.
Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.
During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Hold | +1 |
JSFetchRedirect JSFetchRedirectThanks.
Adding new APIs requires an accepted proposal:
https://github.com/golang/proposal
I haven't looked at this change in detail, but I'm dubious that we would want to add a collection of JSFetch-specific fields to Transport. The general desire to configure JSFetch behavior seems reasonable enough, though, and perhaps there's some reasonable API change we could make here. This would still need to start with a proposal, though.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |