2023-02-23 23:20:21 +09:00
|
|
|
package jshttp
|
2023-02-22 19:10:47 +09:00
|
|
|
|
|
|
|
import (
|
2023-02-23 23:29:22 +09:00
|
|
|
"io"
|
2023-02-22 19:10:47 +09:00
|
|
|
"net/http"
|
2023-02-23 23:29:22 +09:00
|
|
|
"strconv"
|
2023-02-22 19:10:47 +09:00
|
|
|
"syscall/js"
|
2023-02-23 23:20:21 +09:00
|
|
|
|
|
|
|
"github.com/syumai/workers/internal/jsutil"
|
2023-02-22 19:10:47 +09:00
|
|
|
)
|
|
|
|
|
2024-02-01 09:33:52 +09:00
|
|
|
func toResponse(res js.Value, body io.ReadCloser) (*http.Response, error) {
|
2023-02-23 23:29:22 +09:00
|
|
|
status := res.Get("status").Int()
|
|
|
|
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,
|
2024-02-01 09:33:52 +09:00
|
|
|
Body: body,
|
2023-02-23 23:29:22 +09:00
|
|
|
ContentLength: contentLength,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:33:52 +09:00
|
|
|
// 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) {
|
2024-02-01 09:44:39 +09:00
|
|
|
body := jsutil.ConvertReadableStreamToReadCloser(res.Get("body"))
|
2024-02-01 09:33:52 +09:00
|
|
|
return toResponse(res, body)
|
|
|
|
}
|
|
|
|
|
2023-04-29 11:56:21 +09:00
|
|
|
// ToJSResponse converts *http.Response to JavaScript sides Response class object.
|
|
|
|
func ToJSResponse(res *http.Response) js.Value {
|
2024-10-30 20:24:31 -04:00
|
|
|
return newJSResponse(res.StatusCode, res.Header, res.ContentLength, res.Body, nil)
|
2023-04-29 11:56:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// newJSResponse creates JavaScript sides Response class object.
|
2023-02-23 23:30:01 +09:00
|
|
|
// - Response: https://developer.mozilla.org/docs/Web/API/Response
|
2024-10-30 20:24:31 -04:00
|
|
|
func newJSResponse(statusCode int, headers http.Header, contentLength int64, body io.ReadCloser, rawBody *js.Value) js.Value {
|
2023-04-29 11:56:21 +09:00
|
|
|
status := statusCode
|
2023-02-22 19:10:47 +09:00
|
|
|
if status == 0 {
|
|
|
|
status = http.StatusOK
|
|
|
|
}
|
2023-02-23 23:20:21 +09:00
|
|
|
respInit := jsutil.NewObject()
|
2023-02-22 19:10:47 +09:00
|
|
|
respInit.Set("status", status)
|
|
|
|
respInit.Set("statusText", http.StatusText(status))
|
2023-04-29 11:56:21 +09:00
|
|
|
respInit.Set("headers", ToJSHeader(headers))
|
2023-04-26 12:56:33 +09:00
|
|
|
if status == http.StatusSwitchingProtocols ||
|
|
|
|
status == http.StatusNoContent ||
|
|
|
|
status == http.StatusResetContent ||
|
|
|
|
status == http.StatusNotModified {
|
2023-04-29 11:56:21 +09:00
|
|
|
return jsutil.ResponseClass.New(jsutil.Null, respInit)
|
2023-04-26 12:56:33 +09:00
|
|
|
}
|
2024-01-24 22:26:21 +09:00
|
|
|
var readableStream js.Value
|
|
|
|
if rawBody != nil {
|
|
|
|
readableStream = *rawBody
|
2024-10-30 20:24:31 -04:00
|
|
|
} else if contentLength > 0 {
|
|
|
|
readableStream = jsutil.ConvertReaderToFixedLengthStream(body, contentLength)
|
2024-01-24 22:26:21 +09:00
|
|
|
} else {
|
|
|
|
readableStream = jsutil.ConvertReaderToReadableStream(body)
|
|
|
|
}
|
2023-04-29 11:56:21 +09:00
|
|
|
return jsutil.ResponseClass.New(readableStream, respInit)
|
2023-02-22 19:10:47 +09:00
|
|
|
}
|