From d701686ffcf31257b2e660749ec266bc24dad3b2 Mon Sep 17 00:00:00 2001 From: syumai Date: Thu, 23 Feb 2023 23:29:22 +0900 Subject: [PATCH] move ToJSRequest and ToResponse to jshttp from dostub --- cloudflare/dostub.go | 39 ++----------------------------------- internal/jshttp/request.go | 15 ++++++++++++++ internal/jshttp/response.go | 26 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/cloudflare/dostub.go b/cloudflare/dostub.go index 01f2b07..2444c2b 100644 --- a/cloudflare/dostub.go +++ b/cloudflare/dostub.go @@ -3,10 +3,7 @@ package cloudflare import ( "context" "fmt" - "io" "net/http" - "strconv" - "strings" "syscall/js" "github.com/syumai/workers/internal/jshttp" @@ -63,7 +60,7 @@ type DurableObjectStub struct { // // https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#sending-http-requests func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) { - jsReq := toJSRequest(req) + jsReq := jshttp.ToJSRequest(req) promise := s.val.Call("fetch", jsReq) jsRes, err := jsutil.AwaitPromise(promise) @@ -71,37 +68,5 @@ func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) { return nil, err } - return toResponse(jsRes) -} - -func toJSRequest(req *http.Request) js.Value { - jsReqOptions := jsutil.NewObject() - jsReqOptions.Set("method", req.Method) - jsReqOptions.Set("headers", jshttp.ToJSHeader(req.Header)) - jsReqBody := js.Undefined() - if req.Body != nil { - jsReqBody = jsutil.ConvertReaderToReadableStream(req.Body) - } - jsReqOptions.Set("body", jsReqBody) - jsReq := jsutil.RequestClass.New(req.URL.String(), jsReqOptions) - return jsReq -} - -func toResponse(res js.Value) (*http.Response, error) { - status := res.Get("status").Int() - promise := res.Call("text") - body, err := jsutil.AwaitPromise(promise) - if err != nil { - return nil, err - } - header := jshttp.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: io.NopCloser(strings.NewReader(body.String())), - ContentLength: contentLength, - }, nil + return jshttp.ToResponse(jsRes) } diff --git a/internal/jshttp/request.go b/internal/jshttp/request.go index a268f69..f6a33e7 100644 --- a/internal/jshttp/request.go +++ b/internal/jshttp/request.go @@ -42,3 +42,18 @@ func ToRequest(req js.Value) (*http.Request, error) { Host: header.Get("Host"), }, nil } + +// ToJSRequest converts *http.Request to JavaScript sides Request. +// - Request: https://developer.mozilla.org/ja/docs/Web/API/Request +func ToJSRequest(req *http.Request) js.Value { + jsReqOptions := jsutil.NewObject() + jsReqOptions.Set("method", req.Method) + jsReqOptions.Set("headers", ToJSHeader(req.Header)) + jsReqBody := js.Undefined() + if req.Body != nil { + jsReqBody = jsutil.ConvertReaderToReadableStream(req.Body) + } + jsReqOptions.Set("body", jsReqBody) + jsReq := jsutil.RequestClass.New(req.URL.String(), jsReqOptions) + return jsReq +} diff --git a/internal/jshttp/response.go b/internal/jshttp/response.go index e9c98cf..9126c9d 100644 --- a/internal/jshttp/response.go +++ b/internal/jshttp/response.go @@ -1,12 +1,38 @@ package jshttp import ( + "io" "net/http" + "strconv" + "strings" "syscall/js" "github.com/syumai/workers/internal/jsutil" ) +// ToResponse converts JavaScript sides Response to *http.Response. +// - Response: https://developer.mozilla.org/ja/docs/Web/API/Response +func ToResponse(res js.Value) (*http.Response, error) { + status := res.Get("status").Int() + promise := res.Call("text") + body, 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: io.NopCloser(strings.NewReader(body.String())), + ContentLength: contentLength, + }, nil +} + +// ToJSResponse converts *http.Response to JavaScript sides Response. +// - Response: https://developer.mozilla.org/ja/docs/Web/API/Response func ToJSResponse(w *ResponseWriterBuffer) (js.Value, error) { <-w.ReadyCh // wait until ready status := w.StatusCode