Compare commits

..

No commits in common. "master" and "v1.1.2" have entirely different histories.

2 changed files with 2 additions and 45 deletions

45
main.go
View file

@ -1,7 +1,6 @@
package jsFetch
import (
"bytes"
"context"
"errors"
"io"
@ -188,25 +187,7 @@ func (t *Transport) RoundTrip(req *Request) (resp *Response, err error) {
resp.ContentLength = -1
}
}
// Safari doesn't support readable streams
println("Trying duck test")
if js.Global().Get("ApplePaySession").IsUndefined() {
println("Not an apple")
resp.Body = jsStreams.NewReadableStream(args[0].Get("body"))
} else {
// Read in the body with .arrayBuffer()
promise := args[0].Call("arrayBuffer")
waitGroup.Add(1)
promise.Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
uint8Array := js.Global().Get("Uint8Array").New(args[0])
b := make([]byte, uint8Array.Get("length").Int())
js.CopyBytesToGo(b, uint8Array)
resp.Body = io.NopCloser(bytes.NewReader(b))
waitGroup.Done()
return nil
}))
}
resp.Body = jsStreams.NewReadableStream(args[0].Get("body"))
// Standard-library compatibility fields
resp.Proto = "HTTP/1.1"
@ -674,15 +655,7 @@ func Get(url string) (response *Response, err error) {
if err != nil {
return
}
// Disable if not https, we don't detect chromium, or we are on safari
if !strings.HasPrefix(url, "https://") || !js.Global().Get("chrome").IsUndefined() || js.Global().Get("ApplePaySession").IsUndefined() {
request.DisableStreamedClient = true
}
response, err = Fetch.Do(request)
if !request.DisableStreamedClient && err != nil && err.Error() == "Failed to fetch" && !js.Global().Get("chrome").IsUndefined() {
request.DisableStreamedClient = true
response, err = Fetch.Do(request)
}
return
}
@ -691,16 +664,8 @@ func Post(url string, contentType string, body io.Reader) (response *Response, e
if err != nil {
return
}
// Disable if not https, we don't detect chromium, or we are on safari
if !strings.HasPrefix(url, "https://") || !js.Global().Get("chrome").IsUndefined() || js.Global().Get("ApplePaySession").IsUndefined() {
request.DisableStreamedClient = true
}
request.Header.Add("Content-Type", contentType)
response, err = Fetch.Do(request)
if !request.DisableStreamedClient && err != nil && err.Error() == "Failed to fetch" && !js.Global().Get("chrome").IsUndefined() {
request.DisableStreamedClient = true
response, err = Fetch.Do(request)
}
return
}
@ -711,15 +676,7 @@ func PostForm(url string, data url.Values) (response *Response, err error) {
return
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Disable if not https, we don't detect chromium, or we are on safari
if !strings.HasPrefix(url, "https://") || !js.Global().Get("chrome").IsUndefined() || js.Global().Get("ApplePaySession").IsUndefined() {
request.DisableStreamedClient = true
}
response, err = Fetch.Do(request)
if !request.DisableStreamedClient && err != nil && err.Error() == "Failed to fetch" && !js.Global().Get("chrome").IsUndefined() {
request.DisableStreamedClient = true
response, err = Fetch.Do(request)
}
return
}

View file

@ -38,7 +38,7 @@ func tryHead(url string) error {
}
func tryPost(url string, message string) error {
response, err := jsFetch.Post(url, "text/plain", strings.NewReader(message))
response, err := jsFetch.Post(url, strings.NewReader(message))
if err != nil {
return err
}