-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_bldr.go
More file actions
297 lines (267 loc) · 7.32 KB
/
Copy pathfetch_bldr.go
File metadata and controls
297 lines (267 loc) · 7.32 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//go:build js && tinygo && bldr_tinygo_js_imports
package fetch
import (
"bytes"
"context"
"io"
"runtime"
"strconv"
"sync"
"unsafe"
"github.com/aperturerobotics/fastjson"
"github.com/pkg/errors"
)
type bldrFetchResult struct {
metaID uint32
metaLen uint32
bodyID uint32
bodyLen uint32
err error
}
var (
bldrFetchMu sync.Mutex
bldrFetchNextOpID uint32
bldrFetchOps = map[uint32]chan bldrFetchResult{}
)
//go:wasmimport gojs bldr.tinygo.fetch
func bldrTinyGoFetch(opID uint32, urlPtr unsafe.Pointer, urlLen uint32, reqPtr unsafe.Pointer, reqLen uint32, bodyPtr unsafe.Pointer, bodyLen uint32)
//go:wasmimport gojs bldr.tinygo.fetchAbort
func bldrTinyGoFetchAbort(opID uint32) uint32
//go:wasmimport gojs bldr.tinygo.takeStoredBytes
func bldrTinyGoTakeStoredBytes(bytesID uint32, ptr unsafe.Pointer, len uint32) uint32
//go:wasmimport gojs bldr.tinygo.dropStoredBytes
func bldrTinyGoDropStoredBytes(bytesID uint32) uint32
// Fetch uses Bldr's TinyGo fetch import to make requests without raw
// syscall/js callbacks or response-object decoding in Go.
func Fetch(url string, opts *Opts) (*Response, error) {
reqBytes, bodyBytes, err := buildBldrFetchRequest(opts)
if err != nil {
return nil, err
}
opID, ch := registerBldrFetchOp()
defer deleteBldrFetchOp(opID)
ctx := context.Background()
if opts != nil && opts.Signal != nil {
ctx = opts.Signal
}
urlBytes := []byte(url)
bldrTinyGoFetch(
opID,
bytesPtr(urlBytes),
uint32(len(urlBytes)),
bytesPtr(reqBytes),
uint32(len(reqBytes)),
bytesPtr(bodyBytes),
uint32(len(bodyBytes)),
)
runtime.KeepAlive(urlBytes)
runtime.KeepAlive(reqBytes)
runtime.KeepAlive(bodyBytes)
select {
case result := <-ch:
return finishBldrFetchResult(result)
case <-ctx.Done():
deleteBldrFetchOp(opID)
bldrTinyGoFetchAbort(opID)
select {
case result := <-ch:
return finishBldrFetchResult(result)
default:
}
return nil, ctx.Err()
}
}
func buildBldrFetchRequest(opts *Opts) ([]byte, []byte, error) {
req := []byte{'{'}
first := true
var body []byte
if opts != nil {
req = appendJSONStringField(req, &first, "method", opts.Method)
req = appendJSONHeaderField(req, &first, "header", opts.Header)
req = appendJSONStringField(req, &first, "mode", opts.Mode)
req = appendJSONStringField(req, &first, "credentials", opts.Credentials)
req = appendJSONStringField(req, &first, "cache", opts.Cache)
req = appendJSONStringField(req, &first, "redirect", opts.Redirect)
req = appendJSONStringField(req, &first, "referrer", opts.Referrer)
req = appendJSONStringField(req, &first, "referrerPolicy", opts.ReferrerPolicy)
req = appendJSONStringField(req, &first, "integrity", opts.Integrity)
if opts.KeepAlive != nil {
req = appendJSONBoolField(req, &first, "keepAlive", *opts.KeepAlive)
}
if opts.Signal != nil {
req = appendJSONBoolField(req, &first, "signal", true)
}
if opts.Body != nil {
var err error
body, err = io.ReadAll(opts.Body)
if err != nil {
return nil, nil, err
}
}
}
req = append(req, '}')
return req, body, nil
}
func registerBldrFetchOp() (uint32, chan bldrFetchResult) {
ch := make(chan bldrFetchResult, 1)
bldrFetchMu.Lock()
bldrFetchNextOpID++
if bldrFetchNextOpID == 0 {
bldrFetchNextOpID++
}
opID := bldrFetchNextOpID
bldrFetchOps[opID] = ch
bldrFetchMu.Unlock()
return opID, ch
}
func deleteBldrFetchOp(opID uint32) {
bldrFetchMu.Lock()
delete(bldrFetchOps, opID)
bldrFetchMu.Unlock()
}
func sendBldrFetchResult(opID uint32, result bldrFetchResult) bool {
bldrFetchMu.Lock()
ch := bldrFetchOps[opID]
sent := false
if ch != nil {
select {
case ch <- result:
sent = true
default:
}
}
bldrFetchMu.Unlock()
return sent
}
//go:wasmexport BLDR_TINYGO_FETCH_RESOLVE
func bldrTinyGoFetchResolve(opID uint32, metaID uint32, metaLen uint32, bodyID uint32, bodyLen uint32) {
if sendBldrFetchResult(opID, bldrFetchResult{
metaID: metaID,
metaLen: metaLen,
bodyID: bodyID,
bodyLen: bodyLen,
}) {
return
}
dropBldrStoredBytes(metaID)
dropBldrStoredBytes(bodyID)
}
func finishBldrFetchResult(result bldrFetchResult) (*Response, error) {
if result.err != nil {
return nil, result.err
}
metaBytes, ok := takeBldrStoredBytes(result.metaID, result.metaLen)
if !ok {
dropBldrStoredBytes(result.bodyID)
return nil, errors.New("fetch metadata unavailable")
}
bodyBytes, ok := takeBldrStoredBytes(result.bodyID, result.bodyLen)
if !ok {
return nil, errors.New("fetch body unavailable")
}
return buildBldrFetchResponse(metaBytes, bodyBytes)
}
func buildBldrFetchResponse(metaBytes []byte, bodyBytes []byte) (*Response, error) {
var parser fastjson.Parser
meta, err := parser.ParseBytes(metaBytes)
if err != nil {
return nil, errors.Wrap(err, "decode fetch metadata")
}
headers := Header{}
for _, item := range meta.GetArray("header") {
key := item.GetStringBytes("key")
if key == nil {
continue
}
headers.Add(string(key), string(item.GetStringBytes("value")))
}
statusText := string(meta.GetStringBytes("statusText"))
statusCode := meta.GetInt("statusCode")
status := strconv.Itoa(statusCode)
if statusText != "" {
status += " " + statusText
}
return &Response{
OK: meta.GetBool("ok"),
Header: headers,
Redirected: meta.GetBool("redirected"),
StatusCode: statusCode,
Status: status,
Type: string(meta.GetStringBytes("type")),
URL: string(meta.GetStringBytes("url")),
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
}, nil
}
//go:wasmexport BLDR_TINYGO_FETCH_REJECT
func bldrTinyGoFetchReject(opID uint32, code uint32) {
sendBldrFetchResult(opID, bldrFetchResult{err: errors.Errorf("fetch failed with code %d", code)})
}
func takeBldrStoredBytes(bytesID uint32, length uint32) ([]byte, bool) {
if length == 0 {
return nil, bldrTinyGoTakeStoredBytes(bytesID, nil, 0) != 0
}
bytes := make([]byte, int(length))
if bldrTinyGoTakeStoredBytes(bytesID, unsafe.Pointer(&bytes[0]), length) == 0 {
return nil, false
}
return bytes, true
}
func dropBldrStoredBytes(bytesID uint32) {
bldrTinyGoDropStoredBytes(bytesID)
}
func appendJSONFieldName(dst []byte, first *bool, name string) []byte {
if *first {
*first = false
} else {
dst = append(dst, ',')
}
dst = strconv.AppendQuote(dst, name)
dst = append(dst, ':')
return dst
}
func appendJSONStringField(dst []byte, first *bool, name string, value string) []byte {
if value == "" {
return dst
}
dst = appendJSONFieldName(dst, first, name)
return strconv.AppendQuote(dst, value)
}
func appendJSONBoolField(dst []byte, first *bool, name string, value bool) []byte {
dst = appendJSONFieldName(dst, first, name)
if value {
return append(dst, "true"...)
}
return append(dst, "false"...)
}
func appendJSONHeaderField(dst []byte, first *bool, name string, header Header) []byte {
if len(header) == 0 {
return dst
}
dst = appendJSONFieldName(dst, first, name)
dst = append(dst, '{')
headerFirst := true
for key, values := range header {
if headerFirst {
headerFirst = false
} else {
dst = append(dst, ',')
}
dst = strconv.AppendQuote(dst, key)
dst = append(dst, ':', '[')
for idx, value := range values {
if idx != 0 {
dst = append(dst, ',')
}
dst = strconv.AppendQuote(dst, value)
}
dst = append(dst, ']')
}
dst = append(dst, '}')
return dst
}
func bytesPtr(bytes []byte) unsafe.Pointer {
if len(bytes) == 0 {
return nil
}
return unsafe.Pointer(&bytes[0])
}