forked from olebedev/gojax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
119 lines (105 loc) · 2.86 KB
/
fetch.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
116
117
118
119
package fetch
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/textproto"
"net/url"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/fy0/gojax/fetch/internal/data"
"github.com/pkg/errors"
)
// Enable enables fetch for the instance. Loop instance is required instead of
// flat goja's. B/c fetch polyfill uses timeouts for promises.
//
// The second parameter could be any http handler. Even you local instance,
// to handle http requests locally programmatically.
func Enable(loop *eventloop.EventLoop, proxy http.Handler) error {
if proxy == nil {
return errors.New("proxy handler cannot be nil")
}
script := string(data.MustAsset("dist/bundle.js"))
prg, err := goja.Compile("fetch.js", script, false)
if err != nil {
return errors.Wrap(err, "compile script")
}
loop.RunOnLoop(func(vm *goja.Runtime) {
vm.Set("__fetch__", request(loop, proxy))
_, err := vm.RunProgram(prg)
if err != nil {
panic(err)
}
})
return nil
}
func urlEncode(u string) string {
u2, err := url.Parse(u)
if err != nil {
panic(err)
}
u2.RawQuery = u2.Query().Encode()
return u2.String()
}
func request(loop *eventloop.EventLoop, proxy http.Handler) func(call goja.FunctionCall) goja.Value {
return func(call goja.FunctionCall) goja.Value {
if fn, ok := goja.AssertFunction(call.Argument(2)); ok {
u := call.Argument(0).String()
o := call.Argument(1).Export().(map[string]interface{})
u = urlEncode(u)
go func() {
var body io.Reader
method := http.MethodGet
header := make(http.Header)
if headers, ex := o["headers"]; ex {
if hmap, okh := headers.(map[string]interface{}); okh {
for key, value := range hmap {
v := value.([]interface{})
for _, item := range v {
var i []string
i = append(i, item.(string))
header[textproto.CanonicalMIMEHeaderKey(key)] = i
}
}
}
}
if b, ex := o["body"]; ex {
if bo, ok := b.(string); ok {
body = bytes.NewBufferString(bo)
}
}
if m, ex := o["method"]; ex {
if me, ok := m.(string); ok {
method = me
}
}
var toRet map[string]interface{}
res := httptest.NewRecorder()
req, err := http.NewRequest(method, u, body)
if err != nil {
toRet = map[string]interface{}{
"body": fmt.Sprintf("Internal Server Error: %s", err.Error()),
"headers": make(map[string][]string),
"status": http.StatusInternalServerError,
"method": method,
"url": u,
}
} else {
req.Header = header
proxy.ServeHTTP(res, req)
toRet = map[string]interface{}{
"body": res.Body.String(),
"headers": map[string][]string(res.Header()),
"status": res.Code,
"method": method,
"url": u,
}
}
loop.RunOnLoop(func(vm *goja.Runtime) { fn(nil, vm.ToValue(toRet)) })
}()
}
return nil
}
}