forked from olebedev/go-duktape-fetch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch.go
157 lines (132 loc) · 3.31 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//go:generate go-bindata -debug -pkg fetch -o bindata.go ./dist
package fetch
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"gopkg.in/olebedev/go-duktape.v3"
)
var bundle string
func init() {
b, err := Asset("dist/bundle.js")
must(err)
bundle = string(b)
}
func Define(c *duktape.Context) {
DefineWithRoundTripper(c, http.DefaultTransport)
}
func DefineWithBaseURL(c *duktape.Context, u *url.URL) {
DefineWithRoundTripper(c, roundTripperFunc(func(r *http.Request) (*http.Response, error) {
r.URL.Scheme = u.Scheme
r.URL.Host = u.Host
return http.DefaultTransport.RoundTrip(r)
}))
}
func DefineWithRoundTripper(c *duktape.Context, rt http.RoundTripper) {
c.PushTimers()
must(c.PevalString(bundle))
c.Pop()
c.PushGlobalObject()
c.GetPropString(-1, "fetch")
c.PushGoFunction(goFetchSync(rt))
c.PutPropString(-2, "goFetchSync")
c.Pop2()
}
func DefineWithHandler(c *duktape.Context, h http.Handler) {
DefineWithRoundTripper(c, roundTripperFunc(func(r *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()
h.ServeHTTP(recorder, r)
return &http.Response{
Request: r,
StatusCode: recorder.Code,
Status: http.StatusText(recorder.Code),
Header: recorder.HeaderMap,
Body: ioutil.NopCloser(recorder.Body),
}, nil
}))
}
func goFetchSync(rt http.RoundTripper) func(*duktape.Context) int {
return func(c *duktape.Context) int {
var opts = struct {
URL string `json:"url"`
Method string `json:"method"`
Headers http.Header `json:"headers"`
Body string `json:"body"`
}{
URL: c.SafeToString(0),
Method: http.MethodGet,
Headers: http.Header{},
}
err := json.Unmarshal([]byte(c.JsonEncode(1)), &opts)
if err != nil {
c.Pop2()
c.PushErrorObject(1, "%v", err)
return 1
}
req, err := http.NewRequest(opts.Method, opts.URL, strings.NewReader(opts.Body))
if err != nil {
c.Pop2()
c.PushErrorObject(2, "%v", err)
return 1
}
resp, err := doRequest(req, rt)
if err != nil {
c.Pop2()
c.PushErrorObject(3, "%v", err)
return 1
}
j, err := json.MarshalIndent(resp, "", " ")
if err != nil {
c.Pop2()
c.PushErrorObject(4, "%v", err)
return 1
}
c.Pop3()
c.PushString(string(j))
c.JsonDecode(-1)
return 1
}
}
type response struct {
URL string `json:"url"`
Method string `json:"method"`
Headers http.Header `json:"headers"`
Body string `json:"body"`
Status int `json:"status"`
StatusText string `json:"statusText,omitempty"`
}
func doRequest(req *http.Request, rt http.RoundTripper) (response, error) {
client := http.Client{
Transport: rt,
}
httpResp, err := client.Do(req)
if err != nil {
return response{}, err
}
resp := response{
URL: req.URL.String(),
Method: req.Method,
Headers: httpResp.Header,
Status: httpResp.StatusCode,
StatusText: httpResp.Status,
}
defer httpResp.Body.Close()
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return resp, err
}
resp.Body = string(body)
return resp, nil
}
type roundTripperFunc func(r *http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
func must(err error) {
if err != nil {
panic(err)
}
}