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
27 changes: 15 additions & 12 deletions cloudflare/fetch/bind.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package fetch

import (
"errors"
"net/http"
"syscall/js"

"github.com/syumai/workers/internal/jshttp"
"github.com/syumai/workers/internal/jsutil"
)

var globalFetchFunc = js.Global().Get("fetch")

// fetch is a function that reproduces cloudflare fetch.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/fetch/
func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Response, error) {
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(),
)
// The Request object to fetch.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
reqValue := jshttp.ToJSRequest(req)
// The content of the request.
// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
initValue := init.ToJS()

promise := func() js.Value {
if namespace.IsUndefined() {
return globalFetchFunc.Invoke(reqValue, initValue)
}
return namespace.Call("fetch", reqValue, initValue)
}()

jsRes, err := jsutil.AwaitPromise(promise)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cloudflare/fetch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Client is an HTTP client.
type Client struct {
// namespace - Objects that Fetch API belongs to. Default is Global
// namespace - Objects that Fetch API belongs to. Default is undefined
namespace js.Value
}

Expand Down Expand Up @@ -42,7 +42,7 @@ func WithBinding(bind js.Value) ClientOption {
// NewClient returns new Client
func NewClient(opts ...ClientOption) *Client {
c := &Client{
namespace: js.Global(),
namespace: js.Undefined(),
}
c.applyOptions(opts)

Expand Down
2 changes: 1 addition & 1 deletion cloudflare/fetch/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// transport is an implementation of http.RoundTripper
type transport struct {
// namespace - Objects that Fetch API belongs to. Default is Global
// namespace - Objects that Fetch API belongs to. Default is undefined
namespace js.Value
redirect RedirectMode
}
Expand Down
5 changes: 3 additions & 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 All @@ -32,6 +31,8 @@ func ToRequest(req js.Value) (*http.Request, error) {

contentLength, clErr := strconv.ParseInt(header.Get("Content-Length"), 10, 64)
bodyVal := req.Get("body")
// NOTE: `body` is ReadableStream or null. Therefore, `undefined` check may not be necessary.
// https://developer.mozilla.org/docs/Web/API/Request/body#value
if clErr != nil && !bodyVal.IsNull() && !bodyVal.IsUndefined() {
contentLength = -1
}
Expand Down
Loading