-
Notifications
You must be signed in to change notification settings - Fork 1
/
gonreli.go
47 lines (42 loc) · 919 Bytes
/
gonreli.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
//+build js,wasm
package gonreli
import (
"errors"
"net/http"
"syscall/js"
)
var (
jsRequire = js.Global().Get("require")
jsSocketType = jsRequire.Invoke("net").Get("Socket")
jsBufferType = js.Global().Get("Buffer")
jsProcess = js.Global().Get("process")
undefined = js.Undefined()
)
func Wrap(h http.Handler) js.Callback {
return js.NewCallback(func(args []js.Value) {
req, resp := newRequest(args[0]), newResponse(args[1])
go func() {
var err error
defer func() {
r := recover()
if r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = errors.New("Unknown error")
}
http.Error(resp, err.Error(), http.StatusInternalServerError)
}
}()
h.ServeHTTP(resp, req)
if resp.hijacked {
//resp.Get("connection").Call("end")
} else {
resp.Call("end")
}
}()
})
}