2024-05-03 02:47:30 +09:00
|
|
|
//go:build js && wasm
|
|
|
|
|
2022-05-18 00:04:37 +09:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
2023-02-11 10:30:51 +09:00
|
|
|
"context"
|
2022-05-18 00:04:37 +09:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"syscall/js"
|
2022-09-13 23:18:17 +09:00
|
|
|
|
2023-02-23 23:20:21 +09:00
|
|
|
"github.com/syumai/workers/internal/jshttp"
|
2022-09-13 23:18:17 +09:00
|
|
|
"github.com/syumai/workers/internal/jsutil"
|
2024-01-22 21:29:39 +09:00
|
|
|
"github.com/syumai/workers/internal/runtimecontext"
|
2022-05-18 00:04:37 +09:00
|
|
|
)
|
|
|
|
|
2024-01-24 22:05:49 +09:00
|
|
|
var (
|
|
|
|
httpHandler http.Handler
|
|
|
|
closeCh = make(chan struct{})
|
|
|
|
)
|
2022-05-18 00:04:37 +09:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var handleRequestCallback js.Func
|
|
|
|
handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any {
|
2023-02-11 10:30:51 +09:00
|
|
|
reqObj := args[0]
|
2022-05-18 00:04:37 +09:00
|
|
|
var cb js.Func
|
|
|
|
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
|
|
|
|
defer cb.Release()
|
|
|
|
resolve := pArgs[0]
|
2024-04-21 03:40:44 +09:00
|
|
|
reject := pArgs[1]
|
2022-05-18 00:04:37 +09:00
|
|
|
go func() {
|
2024-04-21 03:40:44 +09:00
|
|
|
if len(args) > 1 {
|
|
|
|
reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args)))
|
|
|
|
return
|
|
|
|
}
|
2024-04-17 00:56:35 +09:00
|
|
|
res, err := handleRequest(reqObj)
|
2022-05-18 00:04:37 +09:00
|
|
|
if err != nil {
|
2024-04-21 03:40:44 +09:00
|
|
|
reject.Invoke(jsutil.Error(err.Error()))
|
|
|
|
return
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|
|
|
|
resolve.Invoke(res)
|
|
|
|
}()
|
|
|
|
return js.Undefined()
|
|
|
|
})
|
2022-09-13 23:18:17 +09:00
|
|
|
return jsutil.NewPromise(cb)
|
2022-05-18 00:04:37 +09:00
|
|
|
})
|
2024-01-04 23:43:24 +09:00
|
|
|
jsutil.Binding.Set("handleRequest", handleRequestCallback)
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|
|
|
|
|
2024-01-24 22:05:49 +09:00
|
|
|
type appCloser struct {
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *appCloser) Close() error {
|
|
|
|
defer close(closeCh)
|
|
|
|
return c.ReadCloser.Close()
|
|
|
|
}
|
|
|
|
|
2022-05-18 00:04:37 +09:00
|
|
|
// handleRequest accepts a Request object and returns Response object.
|
2024-04-17 00:56:35 +09:00
|
|
|
func handleRequest(reqObj js.Value) (js.Value, error) {
|
2022-05-18 00:04:37 +09:00
|
|
|
if httpHandler == nil {
|
|
|
|
return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.")
|
|
|
|
}
|
2023-02-23 23:20:21 +09:00
|
|
|
req, err := jshttp.ToRequest(reqObj)
|
2022-05-18 00:04:37 +09:00
|
|
|
if err != nil {
|
2024-04-21 03:40:44 +09:00
|
|
|
return js.Value{}, err
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|
2024-04-17 00:56:35 +09:00
|
|
|
ctx := runtimecontext.New(context.Background(), reqObj)
|
2023-02-11 10:30:51 +09:00
|
|
|
req = req.WithContext(ctx)
|
2022-05-18 00:04:37 +09:00
|
|
|
reader, writer := io.Pipe()
|
2023-04-29 12:18:34 +09:00
|
|
|
w := &jshttp.ResponseWriter{
|
2023-02-22 19:23:56 +09:00
|
|
|
HeaderValue: http.Header{},
|
|
|
|
StatusCode: http.StatusOK,
|
2024-01-24 22:05:49 +09:00
|
|
|
Reader: &appCloser{reader},
|
2023-02-22 19:23:56 +09:00
|
|
|
Writer: writer,
|
|
|
|
ReadyCh: make(chan struct{}),
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|
|
|
|
go func() {
|
2023-02-22 19:23:56 +09:00
|
|
|
defer w.Ready()
|
2022-05-18 00:04:37 +09:00
|
|
|
defer writer.Close()
|
|
|
|
httpHandler.ServeHTTP(w, req)
|
|
|
|
}()
|
2023-04-29 11:56:21 +09:00
|
|
|
<-w.ReadyCh
|
|
|
|
return w.ToJSResponse(), nil
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|
|
|
|
|
2024-01-04 22:59:05 +09:00
|
|
|
//go:wasmimport workers ready
|
|
|
|
func ready()
|
|
|
|
|
2024-05-03 02:47:30 +09:00
|
|
|
// Server serves http.Handler on a JS runtime.
|
2022-05-18 00:04:37 +09:00
|
|
|
// if the given handler is nil, http.DefaultServeMux will be used.
|
|
|
|
func Serve(handler http.Handler) {
|
|
|
|
if handler == nil {
|
|
|
|
handler = http.DefaultServeMux
|
|
|
|
}
|
|
|
|
httpHandler = handler
|
2024-01-04 22:59:05 +09:00
|
|
|
ready()
|
2024-04-21 03:32:55 +09:00
|
|
|
<-closeCh
|
2022-05-18 00:04:37 +09:00
|
|
|
}
|