Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
b80cca5731 | |||
981b39a00b | |||
1a6a8a36b0 | |||
c55ae178b4 | |||
f5b3297d9d | |||
a060c22222 | |||
d02b310372 | |||
2086754a95 |
5 changed files with 56 additions and 5 deletions
|
@ -5,4 +5,7 @@ Made using the [jsStreams](https://git.ailur.dev/Ailur/jsStreams) library.
|
|||
|
||||
[](https://goreportcard.com/report/git.ailur.dev/ailur/jsFetch) [](https://pkg.go.dev/git.ailur.dev/ailur/jsFetch)
|
||||
|
||||
The API is exactly the same as net/http.
|
||||
The API is exactly the same as net/http.
|
||||
|
||||
## Important note
|
||||
It is common for it to return the error "Failed to fetch" with the error ERR_H2_OR_QUIC_REQUIRED in Chromium-based browsers. To fix this, you are able to modify the request to set `request.DisableStreamedClient` to `true`. This is because Chromium has a method of upload ReadableStream data that requires HTTP/2 or QUIC to be enabled for security reasons.
|
2
go.mod
2
go.mod
|
@ -3,6 +3,6 @@ module git.ailur.dev/ailur/jsFetch
|
|||
go 1.22
|
||||
|
||||
require (
|
||||
git.ailur.dev/ailur/jsStreams v1.2.0
|
||||
git.ailur.dev/ailur/jsStreams v1.2.1
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,4 +1,6 @@
|
|||
git.ailur.dev/ailur/jsStreams v1.2.0 h1:BRtLEyjkUoPKPu0Y6odUbSMlKCYNyR792TYRtujKfPw=
|
||||
git.ailur.dev/ailur/jsStreams v1.2.0/go.mod h1:/ZCvbUcWkZRuKIkO7jH6b5vIjzdxIOP8ET8X0src5Go=
|
||||
git.ailur.dev/ailur/jsStreams v1.2.1 h1:nXZYZrxHJCVwR0Kx/X+TenMBmS6Gh8Uc2DMinbyiGoo=
|
||||
git.ailur.dev/ailur/jsStreams v1.2.1/go.mod h1:/ZCvbUcWkZRuKIkO7jH6b5vIjzdxIOP8ET8X0src5Go=
|
||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
|
|
50
main.go
50
main.go
|
@ -1,6 +1,7 @@
|
|||
package jsFetch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
@ -187,7 +188,25 @@ func (t *Transport) RoundTrip(req *Request) (resp *Response, err error) {
|
|||
resp.ContentLength = -1
|
||||
}
|
||||
}
|
||||
resp.Body = jsStreams.NewReadableStream(args[0].Get("body"))
|
||||
|
||||
// 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
|
||||
}))
|
||||
}
|
||||
|
||||
// Standard-library compatibility fields
|
||||
resp.Proto = "HTTP/1.1"
|
||||
|
@ -314,6 +333,9 @@ var Fetch = &Client{
|
|||
Timeout: 20 * time.Second,
|
||||
}
|
||||
|
||||
// DefaultClient is an alias for Fetch.
|
||||
var DefaultClient = Fetch
|
||||
|
||||
func CanonicalHeaderKey(key string) string {
|
||||
const allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~"
|
||||
for _, char := range []rune(key) {
|
||||
|
@ -397,7 +419,7 @@ type Request struct {
|
|||
// HTTP/2 or QUIC to be enabled when using V8, which is not always the case, particularly on
|
||||
// older servers or test servers.
|
||||
//
|
||||
// If DisableStreamedClientChecks is set to true, the client will first attempt to detect if
|
||||
// If DisableStreamedClientChecks is set to false, the client will first attempt to detect if
|
||||
// the server supports HTTP/2 or QUIC and if you are running a supported JavaScript engine.
|
||||
// Supported browser engines include:
|
||||
// - V8 (Chrome, Edge, Opera)
|
||||
|
@ -652,7 +674,15 @@ 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
|
||||
}
|
||||
|
||||
|
@ -661,8 +691,16 @@ 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
|
||||
}
|
||||
|
||||
|
@ -673,7 +711,15 @@ 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
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ func tryHead(url string) error {
|
|||
}
|
||||
|
||||
func tryPost(url string, message string) error {
|
||||
response, err := jsFetch.Post(url, strings.NewReader(message))
|
||||
response, err := jsFetch.Post(url, "text/plain", strings.NewReader(message))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue