2022-05-18 00:04:37 +09:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"syscall/js"
|
2022-09-13 23:18:17 +09:00
|
|
|
|
|
|
|
"github.com/syumai/workers/internal/jsutil"
|
2022-05-18 00:04:37 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
func toJSHeader(header http.Header) js.Value {
|
2022-09-13 23:18:17 +09:00
|
|
|
h := jsutil.HeadersClass.New()
|
2022-05-18 00:04:37 +09:00
|
|
|
for key, values := range header {
|
|
|
|
for _, value := range values {
|
|
|
|
h.Call("append", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-05-20 00:11:47 +09:00
|
|
|
func toJSResponse(w *responseWriterBuffer) (js.Value, error) {
|
|
|
|
<-w.readyCh // wait until ready
|
|
|
|
status := w.statusCode
|
2022-05-18 00:04:37 +09:00
|
|
|
if status == 0 {
|
|
|
|
status = http.StatusOK
|
|
|
|
}
|
2022-09-13 23:18:17 +09:00
|
|
|
respInit := jsutil.NewObject()
|
2022-05-18 00:04:37 +09:00
|
|
|
respInit.Set("status", status)
|
|
|
|
respInit.Set("statusText", http.StatusText(status))
|
2022-05-20 00:11:47 +09:00
|
|
|
respInit.Set("headers", toJSHeader(w.Header()))
|
2022-09-13 23:18:17 +09:00
|
|
|
readableStream := jsutil.ConvertReaderToReadableStream(w.reader)
|
|
|
|
return jsutil.ResponseClass.New(readableStream, respInit), nil
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|