Skip to content

Commit

Permalink
move fetch's stream logic to jshttp.ToStreamResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Feb 1, 2024
1 parent d31baa3 commit 94f29eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
16 changes: 1 addition & 15 deletions cloudflare/fetch/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fetch
import (
"errors"
"net/http"
"strconv"
"syscall/js"

"github.com/syumai/workers/internal/jshttp"
Expand Down Expand Up @@ -31,18 +30,5 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp
return nil, err
}

// Create TransformStream
ts := js.Global().Get("IdentityTransformStream").New()
readable := ts.Get("readable")
writable := ts.Get("writable")
jsRes.Get("body").Call("pipeTo", writable)

// Create response
res := new(http.Response)
res.StatusCode = jsRes.Get("status").Int()
res.Status = strconv.Itoa(res.StatusCode) + " " + jsRes.Get("statusText").String()
res.Header = jshttp.ToHeader(jsRes.Get("headers"))
res.Body = jsutil.ConvertReadableStreamToReadCloser(readable)

return res, nil
return jshttp.ToStreamResponse(jsRes)
}
34 changes: 25 additions & 9 deletions internal/jshttp/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,43 @@ import (
"github.com/syumai/workers/internal/jsutil"
)

// ToResponse converts JavaScript sides Response to *http.Response.
// - Response: https://developer.mozilla.org/docs/Web/API/Response
func ToResponse(res js.Value) (*http.Response, error) {
func toResponse(res js.Value, body io.ReadCloser) (*http.Response, error) {
status := res.Get("status").Int()
promise := res.Call("blob")
blob, err := jsutil.AwaitPromise(promise)
if err != nil {
return nil, err
}
header := ToHeader(res.Get("headers"))
contentLength, _ := strconv.ParseInt(header.Get("Content-Length"), 10, 64)

return &http.Response{
Status: strconv.Itoa(status) + " " + res.Get("statusText").String(),
StatusCode: status,
Header: header,
Body: jsutil.ConvertReadableStreamToReadCloser(blob.Call("stream")),
Body: body,
ContentLength: contentLength,
}, nil
}

// ToResponse converts JavaScript sides Response to *http.Response.
// - Response: https://developer.mozilla.org/docs/Web/API/Response
func ToResponse(res js.Value) (*http.Response, error) {
promise := res.Call("blob")
blob, err := jsutil.AwaitPromise(promise)
if err != nil {
return nil, err
}
body := jsutil.ConvertReadableStreamToReadCloser(blob.Call("stream"))
return toResponse(res, body)
}

// ToStreamResponse pipes JavaScript sides Response to TransformStream and converts to *http.Response.
// - see: https://developers.cloudflare.com/workers/runtime-apis/streams/
func ToStreamResponse(res js.Value) (*http.Response, error) {
ts := js.Global().Get("IdentityTransformStream").New()
readable := ts.Get("readable")
writable := ts.Get("writable")
res.Get("body").Call("pipeTo", writable)
body := jsutil.ConvertReadableStreamToReadCloser(readable)
return toResponse(res, body)
}

// ToJSResponse converts *http.Response to JavaScript sides Response class object.
func ToJSResponse(res *http.Response) js.Value {
return newJSResponse(res.StatusCode, res.Header, res.Body, nil)
Expand Down

0 comments on commit 94f29eb

Please sign in to comment.