2022-05-18 00:04:37 +09:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"syscall/js"
|
|
|
|
)
|
|
|
|
|
|
|
|
func toJSHeader(header http.Header) js.Value {
|
|
|
|
h := headersClass.New()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
respInit := newObject()
|
|
|
|
respInit.Set("status", status)
|
|
|
|
respInit.Set("statusText", http.StatusText(status))
|
2022-05-20 00:11:47 +09:00
|
|
|
respInit.Set("headers", toJSHeader(w.Header()))
|
|
|
|
readableStream := convertReaderToReadableStream(w.reader)
|
2022-05-18 00:04:37 +09:00
|
|
|
return responseClass.New(readableStream, respInit), nil
|
|
|
|
}
|