-
Notifications
You must be signed in to change notification settings - Fork 41
/
handler_js.go
115 lines (102 loc) · 2.63 KB
/
handler_js.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//go:build js && wasm
package workers
import (
"context"
"fmt"
"io"
"net/http"
"syscall/js"
"github.com/syumai/workers/internal/jshttp"
"github.com/syumai/workers/internal/jsutil"
"github.com/syumai/workers/internal/runtimecontext"
)
var (
httpHandler http.Handler
doneCh = make(chan struct{})
)
func init() {
var handleRequestCallback js.Func
handleRequestCallback = js.FuncOf(func(this js.Value, args []js.Value) any {
reqObj := args[0]
var cb js.Func
cb = js.FuncOf(func(_ js.Value, pArgs []js.Value) any {
defer cb.Release()
resolve := pArgs[0]
reject := pArgs[1]
go func() {
if len(args) > 1 {
reject.Invoke(jsutil.Errorf("too many args given to handleRequest: %d", len(args)))
return
}
res, err := handleRequest(reqObj)
if err != nil {
reject.Invoke(jsutil.Error(err.Error()))
return
}
resolve.Invoke(res)
}()
return js.Undefined()
})
return jsutil.NewPromise(cb)
})
jsutil.Binding.Set("handleRequest", handleRequestCallback)
}
type appCloser struct {
io.ReadCloser
}
func (c *appCloser) Close() error {
defer close(doneCh)
return c.ReadCloser.Close()
}
// handleRequest accepts a Request object and returns Response object.
func handleRequest(reqObj js.Value) (js.Value, error) {
if httpHandler == nil {
return js.Value{}, fmt.Errorf("Serve must be called before handleRequest.")
}
req, err := jshttp.ToRequest(reqObj)
if err != nil {
return js.Value{}, err
}
ctx := runtimecontext.New(context.Background(), reqObj)
req = req.WithContext(ctx)
reader, writer := io.Pipe()
w := &jshttp.ResponseWriter{
HeaderValue: http.Header{},
StatusCode: http.StatusOK,
Reader: &appCloser{reader},
Writer: writer,
ReadyCh: make(chan struct{}),
}
go func() {
defer w.Ready()
defer writer.Close()
httpHandler.ServeHTTP(w, req)
}()
<-w.ReadyCh
return w.ToJSResponse(), nil
}
// Serve serves http.Handler on a JS runtime.
// if the given handler is nil, http.DefaultServeMux will be used.
func Serve(handler http.Handler) {
ServeNonBlock(handler)
Ready()
<-Done()
}
// ServeNonBlock sets the http.Handler to be served but does not signal readiness or block
// indefinitely. The non-blocking form is meant to be used in conjunction with Ready and WaitForCompletion.
func ServeNonBlock(handler http.Handler) {
if handler == nil {
handler = http.DefaultServeMux
}
httpHandler = handler
}
//go:wasmimport workers ready
func ready()
// Ready must be called after all setups of the Go side's handlers are done.
func Ready() {
ready()
}
// Done returns a channel which is closed when the handler is done.
func Done() <-chan struct{} {
return doneCh
}