mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
fix build error
This commit is contained in:
parent
d7d667f106
commit
88f0dc1798
12
handler.go
12
handler.go
@ -55,14 +55,14 @@ func handleRequest(reqObj js.Value, runtimeCtxObj js.Value) (js.Value, error) {
|
||||
req = req.WithContext(ctx)
|
||||
reader, writer := io.Pipe()
|
||||
w := &jsutil.ResponseWriterBuffer{
|
||||
header: http.Header{},
|
||||
statusCode: http.StatusOK,
|
||||
reader: reader,
|
||||
writer: writer,
|
||||
readyCh: make(chan struct{}),
|
||||
HeaderValue: http.Header{},
|
||||
StatusCode: http.StatusOK,
|
||||
Reader: reader,
|
||||
Writer: writer,
|
||||
ReadyCh: make(chan struct{}),
|
||||
}
|
||||
go func() {
|
||||
defer w.ready()
|
||||
defer w.Ready()
|
||||
defer writer.Close()
|
||||
httpHandler.ServeHTTP(w, req)
|
||||
}()
|
||||
|
@ -16,8 +16,8 @@ func ToJSHeader(header http.Header) js.Value {
|
||||
}
|
||||
|
||||
func ToJSResponse(w *ResponseWriterBuffer) (js.Value, error) {
|
||||
<-w.readyCh // wait until ready
|
||||
status := w.statusCode
|
||||
<-w.ReadyCh // wait until ready
|
||||
status := w.StatusCode
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
@ -25,6 +25,6 @@ func ToJSResponse(w *ResponseWriterBuffer) (js.Value, error) {
|
||||
respInit.Set("status", status)
|
||||
respInit.Set("statusText", http.StatusText(status))
|
||||
respInit.Set("headers", ToJSHeader(w.Header()))
|
||||
readableStream := ConvertReaderToReadableStream(w.reader)
|
||||
readableStream := ConvertReaderToReadableStream(w.Reader)
|
||||
return ResponseClass.New(readableStream, respInit), nil
|
||||
}
|
||||
|
@ -7,32 +7,32 @@ import (
|
||||
)
|
||||
|
||||
type ResponseWriterBuffer struct {
|
||||
header http.Header
|
||||
statusCode int
|
||||
reader *io.PipeReader
|
||||
writer *io.PipeWriter
|
||||
readyCh chan struct{}
|
||||
once sync.Once
|
||||
HeaderValue http.Header
|
||||
StatusCode int
|
||||
Reader *io.PipeReader
|
||||
Writer *io.PipeWriter
|
||||
ReadyCh chan struct{}
|
||||
Once sync.Once
|
||||
}
|
||||
|
||||
var _ http.ResponseWriter = &ResponseWriterBuffer{}
|
||||
|
||||
// ready indicates that ResponseWriterBuffer is ready to be converted to Response.
|
||||
func (w *ResponseWriterBuffer) ready() {
|
||||
w.once.Do(func() {
|
||||
close(w.readyCh)
|
||||
// Ready indicates that ResponseWriterBuffer is ready to be converted to Response.
|
||||
func (w *ResponseWriterBuffer) Ready() {
|
||||
w.Once.Do(func() {
|
||||
close(w.ReadyCh)
|
||||
})
|
||||
}
|
||||
|
||||
func (w *ResponseWriterBuffer) Write(data []byte) (n int, err error) {
|
||||
w.ready()
|
||||
return w.writer.Write(data)
|
||||
w.Ready()
|
||||
return w.Writer.Write(data)
|
||||
}
|
||||
|
||||
func (w *ResponseWriterBuffer) Header() http.Header {
|
||||
return w.header
|
||||
return w.HeaderValue
|
||||
}
|
||||
|
||||
func (w *ResponseWriterBuffer) WriteHeader(statusCode int) {
|
||||
w.statusCode = statusCode
|
||||
w.StatusCode = statusCode
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user