mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
commonize ToJSResponse func
This commit is contained in:
parent
a0ec52cd49
commit
78444c3c67
18
cloudflare/cache/method.go
vendored
18
cloudflare/cache/method.go
vendored
@ -9,22 +9,6 @@ import (
|
|||||||
"github.com/syumai/workers/internal/jsutil"
|
"github.com/syumai/workers/internal/jsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// toJSResponse converts *http.Response to JS Response
|
|
||||||
func toJSResponse(res *http.Response) js.Value {
|
|
||||||
status := res.StatusCode
|
|
||||||
if status == 0 {
|
|
||||||
status = http.StatusOK
|
|
||||||
}
|
|
||||||
respInit := jsutil.NewObject()
|
|
||||||
respInit.Set("status", status)
|
|
||||||
respInit.Set("statusText", http.StatusText(status))
|
|
||||||
respInit.Set("headers", jshttp.ToJSHeader(res.Header))
|
|
||||||
|
|
||||||
readableStream := jsutil.ConvertReaderToReadableStream(res.Body)
|
|
||||||
|
|
||||||
return jsutil.ResponseClass.New(readableStream, respInit)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put attempts to add a response to the cache, using the given request as the key.
|
// Put attempts to add a response to the cache, using the given request as the key.
|
||||||
// Returns an error for the following conditions
|
// Returns an error for the following conditions
|
||||||
// - the request passed is a method other than GET.
|
// - the request passed is a method other than GET.
|
||||||
@ -32,7 +16,7 @@ func toJSResponse(res *http.Response) js.Value {
|
|||||||
// - Cache-Control instructs not to cache or if the response is too large.
|
// - Cache-Control instructs not to cache or if the response is too large.
|
||||||
// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#put
|
// docs: https://developers.cloudflare.com/workers/runtime-apis/cache/#put
|
||||||
func (c *Cache) Put(req *http.Request, res *http.Response) error {
|
func (c *Cache) Put(req *http.Request, res *http.Response) error {
|
||||||
_, err := jsutil.AwaitPromise(c.instance.Call("put", jshttp.ToJSRequest(req), toJSResponse(res)))
|
_, err := jsutil.AwaitPromise(c.instance.Call("put", jshttp.ToJSRequest(req), jshttp.ToJSResponse(res)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,8 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
|||||||
defer writer.Close()
|
defer writer.Close()
|
||||||
httpHandler.ServeHTTP(w, req)
|
httpHandler.ServeHTTP(w, req)
|
||||||
}()
|
}()
|
||||||
return jshttp.ToJSResponse(w)
|
<-w.ReadyCh
|
||||||
|
return w.ToJSResponse(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server serves http.Handler on Cloudflare Workers.
|
// Server serves http.Handler on Cloudflare Workers.
|
||||||
|
@ -31,24 +31,28 @@ func ToResponse(res js.Value) (*http.Response, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToJSResponse converts *http.Response to JavaScript sides Response.
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newJSResponse creates JavaScript sides Response class object.
|
||||||
// - Response: https://developer.mozilla.org/docs/Web/API/Response
|
// - Response: https://developer.mozilla.org/docs/Web/API/Response
|
||||||
func ToJSResponse(w *ResponseWriterBuffer) (js.Value, error) {
|
func newJSResponse(statusCode int, headers http.Header, body io.ReadCloser) js.Value {
|
||||||
<-w.ReadyCh // wait until ready
|
status := statusCode
|
||||||
status := w.StatusCode
|
|
||||||
if status == 0 {
|
if status == 0 {
|
||||||
status = http.StatusOK
|
status = http.StatusOK
|
||||||
}
|
}
|
||||||
respInit := jsutil.NewObject()
|
respInit := jsutil.NewObject()
|
||||||
respInit.Set("status", status)
|
respInit.Set("status", status)
|
||||||
respInit.Set("statusText", http.StatusText(status))
|
respInit.Set("statusText", http.StatusText(status))
|
||||||
respInit.Set("headers", ToJSHeader(w.Header()))
|
respInit.Set("headers", ToJSHeader(headers))
|
||||||
if status == http.StatusSwitchingProtocols ||
|
if status == http.StatusSwitchingProtocols ||
|
||||||
status == http.StatusNoContent ||
|
status == http.StatusNoContent ||
|
||||||
status == http.StatusResetContent ||
|
status == http.StatusResetContent ||
|
||||||
status == http.StatusNotModified {
|
status == http.StatusNotModified {
|
||||||
return jsutil.ResponseClass.New(jsutil.Null, respInit), nil
|
return jsutil.ResponseClass.New(jsutil.Null, respInit)
|
||||||
}
|
}
|
||||||
readableStream := jsutil.ConvertReaderToReadableStream(w.Reader)
|
readableStream := jsutil.ConvertReaderToReadableStream(body)
|
||||||
return jsutil.ResponseClass.New(readableStream, respInit), nil
|
return jsutil.ResponseClass.New(readableStream, respInit)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResponseWriterBuffer struct {
|
type ResponseWriterBuffer struct {
|
||||||
@ -36,3 +37,9 @@ func (w *ResponseWriterBuffer) Header() http.Header {
|
|||||||
func (w *ResponseWriterBuffer) WriteHeader(statusCode int) {
|
func (w *ResponseWriterBuffer) WriteHeader(statusCode int) {
|
||||||
w.StatusCode = statusCode
|
w.StatusCode = statusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToJSResponse converts *ResponseWriterBuffer to JavaScript sides Response.
|
||||||
|
// - Response: https://developer.mozilla.org/docs/Web/API/Response
|
||||||
|
func (w *ResponseWriterBuffer) ToJSResponse() js.Value {
|
||||||
|
return newJSResponse(w.StatusCode, w.HeaderValue, w.Reader)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user