Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions cloudflare/fetch/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp
if namespace.IsUndefined() {
return nil, errors.New("fetch function not found")
}
promise := namespace.Call("fetch",
// The Request object to fetch.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
jshttp.ToJSRequest(req),
// The content of the request.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
init.ToJS(),
)
var promise js.Value
// The Request object to fetch.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
var reqValue = jshttp.ToJSRequest(req)
// The content of the request.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
var initValue = init.ToJS()
if namespace.Equal(js.Global()) {
// global fetch
fetchFunc := namespace.Get("fetch")
promise = fetchFunc.Invoke(reqValue, initValue)
} else {
// service-binding
promise = namespace.Call("fetch", reqValue, initValue)
}

jsRes, err := jsutil.AwaitPromise(promise)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/jshttp/request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jshttp

import (
"bytes"
"io"
"net/http"
"net/url"
Expand All @@ -16,7 +15,7 @@ import (
// - ReadableStream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
func ToBody(streamOrNull js.Value) io.ReadCloser {
if streamOrNull.IsNull() {
return io.NopCloser(bytes.NewReader([]byte{}))
return nil
}
return jsutil.ConvertReadableStreamToReadCloser(streamOrNull)
}
Expand Down
Loading