cloudflare-workers/responsewriter.go

39 lines
736 B
Go
Raw Normal View History

2022-05-18 00:04:37 +09:00
package workers
import (
"io"
"net/http"
2022-05-20 00:11:47 +09:00
"sync"
2022-05-18 00:04:37 +09:00
)
type responseWriterBuffer struct {
header http.Header
statusCode int
2022-05-20 00:11:47 +09:00
reader *io.PipeReader
writer *io.PipeWriter
readyCh chan struct{}
once sync.Once
2022-05-18 00:04:37 +09:00
}
var _ http.ResponseWriter = &responseWriterBuffer{}
2022-05-20 00:11:47 +09:00
// ready indicates that responseWriterBuffer is ready to be converted to Response.
func (w *responseWriterBuffer) ready() {
w.once.Do(func() {
close(w.readyCh)
})
2022-05-18 00:04:37 +09:00
}
2022-05-20 00:11:47 +09:00
func (w *responseWriterBuffer) Write(data []byte) (n int, err error) {
w.ready()
return w.writer.Write(data)
}
func (w *responseWriterBuffer) Header() http.Header {
return w.header
2022-05-18 00:04:37 +09:00
}
2022-05-20 00:11:47 +09:00
func (w *responseWriterBuffer) WriteHeader(statusCode int) {
w.statusCode = statusCode
2022-05-18 00:04:37 +09:00
}