[go] Add JSFetch Options to Transport

3 views
Skip to first unread message

Adriano Sela (Gerrit)

unread,
Feb 1, 2026, 2:27:02 PM (5 days ago) Feb 1
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Adriano Sela has uploaded the change for review

Commit message

Add JSFetch Options to Transport
Signed-off-by: Adriano Sela Aviles <adriano....@gmail.com>
Change-Id: I70b37b4dae1ee1e7166bab0c5fbb2eba19eb98d4

Change diff

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()

Change information

Files:
  • A src/net/http/jsfetch.go
  • M src/net/http/roundtrip_js.go
  • M src/net/http/transport.go
Change size: M
Delta: 3 files changed, 125 insertions(+), 4 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Signed-off-by-Footer
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I70b37b4dae1ee1e7166bab0c5fbb2eba19eb98d4
Gerrit-Change-Number: 740960
Gerrit-PatchSet: 1
Gerrit-Owner: Adriano Sela <adriano....@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Feb 1, 2026, 2:29:55 PM (5 days ago) Feb 1
to Adriano Sela, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

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.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Signed-off-by-Footer
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I70b37b4dae1ee1e7166bab0c5fbb2eba19eb98d4
Gerrit-Change-Number: 740960
Gerrit-PatchSet: 1
Gerrit-Owner: Adriano Sela <adriano....@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Sun, 01 Feb 2026 19:29:48 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
Feb 4, 2026, 6:11:53 PM (2 days ago) Feb 4
to Adriano Sela, goph...@pubsubhelper.golang.org, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Adriano Sela and Damien Neil

Sean Liao voted Hold+1

Hold+1
Open in Gerrit

Related details

Attention is currently required from:
  • Adriano Sela
  • Damien Neil
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement is not satisfiedNo-Signed-off-by-Footer
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I70b37b4dae1ee1e7166bab0c5fbb2eba19eb98d4
    Gerrit-Change-Number: 740960
    Gerrit-PatchSet: 1
    Gerrit-Owner: Adriano Sela <adriano....@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Sean Liao <se...@liao.dev>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Attention: Adriano Sela <adriano....@gmail.com>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Comment-Date: Wed, 04 Feb 2026 23:11:46 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Damien Neil (Gerrit)

    unread,
    Feb 6, 2026, 6:27:51 PM (8 hours ago) Feb 6
    to Adriano Sela, goph...@pubsubhelper.golang.org, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Adriano Sela

    Damien Neil voted and added 1 comment

    Votes added by Damien Neil

    Hold+1

    1 comment

    File src/net/http/transport.go
    Line 342, Patchset 1 (Latest): JSFetchRedirect JSFetchRedirect
    Damien Neil . unresolved

    Thanks.

    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.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Adriano Sela
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Holds
      • requirement is not satisfiedNo-Signed-off-by-Footer
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I70b37b4dae1ee1e7166bab0c5fbb2eba19eb98d4
      Gerrit-Change-Number: 740960
      Gerrit-PatchSet: 1
      Gerrit-Owner: Adriano Sela <adriano....@gmail.com>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Russ Cox <r...@golang.org>
      Gerrit-Attention: Adriano Sela <adriano....@gmail.com>
      Gerrit-Comment-Date: Fri, 06 Feb 2026 23:27:47 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages