mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
move ToJSRequest and ToResponse to jshttp from dostub
This commit is contained in:
parent
51cc301c82
commit
d701686ffc
@ -3,10 +3,7 @@ package cloudflare
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/syumai/workers/internal/jshttp"
|
"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
|
// https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#sending-http-requests
|
||||||
func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) {
|
func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) {
|
||||||
jsReq := toJSRequest(req)
|
jsReq := jshttp.ToJSRequest(req)
|
||||||
|
|
||||||
promise := s.val.Call("fetch", jsReq)
|
promise := s.val.Call("fetch", jsReq)
|
||||||
jsRes, err := jsutil.AwaitPromise(promise)
|
jsRes, err := jsutil.AwaitPromise(promise)
|
||||||
@ -71,37 +68,5 @@ func (s *DurableObjectStub) Fetch(req *http.Request) (*http.Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return toResponse(jsRes)
|
return jshttp.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
|
|
||||||
}
|
}
|
||||||
|
@ -42,3 +42,18 @@ func ToRequest(req js.Value) (*http.Request, error) {
|
|||||||
Host: header.Get("Host"),
|
Host: header.Get("Host"),
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
@ -1,12 +1,38 @@
|
|||||||
package jshttp
|
package jshttp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
|
||||||
"github.com/syumai/workers/internal/jsutil"
|
"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) {
|
func ToJSResponse(w *ResponseWriterBuffer) (js.Value, error) {
|
||||||
<-w.ReadyCh // wait until ready
|
<-w.ReadyCh // wait until ready
|
||||||
status := w.StatusCode
|
status := w.StatusCode
|
||||||
|
Loading…
x
Reference in New Issue
Block a user