From 75c698d8b3c09b02bc487f9b993af4fa66831404 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Wed, 4 Jan 2023 18:57:42 -0500
Subject: [PATCH] Set js header for isonline

---
 connect/headers.go      |  9 +++++++++
 connect/headers_js.go   | 10 ++++++++++
 connect/webConn.go      |  6 +++---
 connect/webConn_test.go |  3 ++-
 4 files changed, 24 insertions(+), 4 deletions(-)
 create mode 100644 connect/headers.go
 create mode 100644 connect/headers_js.go

diff --git a/connect/headers.go b/connect/headers.go
new file mode 100644
index 0000000..b3ab18a
--- /dev/null
+++ b/connect/headers.go
@@ -0,0 +1,9 @@
+//go:build !js || !wasm
+
+package connect
+
+import "net/http"
+
+func (wc *webConn) addHeaders(header http.Header) http.Header {
+	return header
+}
diff --git a/connect/headers_js.go b/connect/headers_js.go
new file mode 100644
index 0000000..17620bc
--- /dev/null
+++ b/connect/headers_js.go
@@ -0,0 +1,10 @@
+//go:build js && wasm
+
+package connect
+
+import "net/http"
+
+func (wc *webConn) addHeaders(header http.Header) http.Header {
+	req.Header.Add("js.fetch:mode", "no-cors")
+	return header
+}
diff --git a/connect/webConn.go b/connect/webConn.go
index 3f89982..4eb69ce 100644
--- a/connect/webConn.go
+++ b/connect/webConn.go
@@ -191,10 +191,10 @@ func (wc *webConn) IsOnline() (time.Duration, bool) {
 	addr := wc.h.GetAddress()
 	pingTimeout := wc.h.params.PingTimeout
 
-	return isOnlineHelper(addr, pingTimeout)
+	return wc.isOnlineHelper(addr, pingTimeout)
 }
 
-func isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool) {
+func (wc *webConn) isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool) {
 	start := time.Now()
 	tr := &http.Transport{
 		TLSClientConfig: &tls.Config{
@@ -225,7 +225,7 @@ func isOnlineHelper(addr string, pingTimeout time.Duration) (time.Duration, bool
 	}
 
 	// IMPORTANT - enables better HTTP(S) discovery, because many browsers block CORS by default.
-	//req.Header.Add("js.fetch:mode", "no-cors")
+	req.Header = wc.addHeaders(req.Header)
 	jww.TRACE.Printf("(GO request): %+v", req)
 
 	req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
diff --git a/connect/webConn_test.go b/connect/webConn_test.go
index 8bca386..a84c92c 100644
--- a/connect/webConn_test.go
+++ b/connect/webConn_test.go
@@ -145,6 +145,7 @@ func Test_checkErrorExceptions(t *testing.T) {
 
 //func Test_isOnline_actual(t *testing.T) {
 //	targetAddr := ".xxnode.io:22840"
-//	_, ok := isOnlineHelper(targetAddr, time.Second*10)
+//	wc := webConn{}
+//	_, ok := wc.isOnlineHelper(targetAddr, time.Second*10)
 //	t.Fatal(ok)
 //}
-- 
GitLab