-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.go
140 lines (115 loc) · 3.07 KB
/
extension.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
package fetch
import (
"expvar"
"fmt"
"net/http"
"go.miragespace.co/heresy/extensions/common"
"go.miragespace.co/heresy/extensions/common/x"
"go.miragespace.co/heresy/extensions/stream"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
)
var (
fetcherNew = expvar.NewInt("fetcher.New")
fetcherPut = expvar.NewInt("fetcher.Put")
)
const UserAgent = "heresy-runtime/fetcher"
var ErrUnsupportedReadableStream = fmt.Errorf("using custom ReadableStream as body is currently unsupported")
type Fetch struct {
FetchConfig
runtimeFetchWrapper goja.Callable
runtimeReponseHelper goja.Value
fetcherPool *x.Pool[*NativeFetcher]
}
type FetchConfig struct {
Stream *stream.StreamController
Eventloop *eventloop.EventLoop
Client *http.Client
}
type NativeFetcher struct {
nativeWrapper *NativeFetchWrapper
nativeFunc goja.Value
}
func (n *NativeFetcher) NativeFunc() goja.Value {
return n.nativeFunc
}
func (c *FetchConfig) Validate() error {
if c.Eventloop == nil {
return fmt.Errorf("nil Eventloop is invalid")
}
if c.Stream == nil {
return fmt.Errorf("nil StreamController is invalid")
}
if c.Client == nil {
return fmt.Errorf("nil http.Client is invalid")
}
return nil
}
func NewFetch(cfg FetchConfig) (*Fetch, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
f := &Fetch{
FetchConfig: cfg,
}
setup := make(chan error, 1)
f.Eventloop.RunOnLoop(func(vm *goja.Runtime) {
_, err := vm.RunProgram(fetchWrapperProg)
if err != nil {
setup <- err
return
}
promiseResolver := vm.Get(fetchWrapperSymbol)
wrapper, ok := goja.AssertFunction(promiseResolver)
if !ok {
setup <- fmt.Errorf("internal error: %s is not a function", fetchWrapperSymbol)
return
}
f.runtimeFetchWrapper = wrapper
promiseResolver = vm.Get(responseHelperSymbol)
f.runtimeReponseHelper = promiseResolver
f.fetcherPool = x.NewPool[*NativeFetcher](x.DefaultPoolCapacity).
WithFactory(func() *NativeFetcher {
fetcherNew.Add(1)
wrapper := &NativeFetchWrapper{
cfg: f.FetchConfig,
}
obj := vm.CreateObject(nil)
obj.Set("doFetch", vm.ToValue(wrapper.doFetch))
fn, err := f.runtimeFetchWrapper(goja.Undefined(), obj)
if err != nil {
panic(fmt.Errorf("runtime panic: Failed to get native fetch: %w", err))
}
return &NativeFetcher{
nativeWrapper: wrapper,
nativeFunc: fn,
}
})
setup <- nil
})
err := <-setup
if err != nil {
return nil, err
}
return f, nil
}
func (f *Fetch) GetResponseHelper() goja.Value {
return f.runtimeReponseHelper
}
// func (f *Fetch) NewNativeFetch(t *common.IOContext) *NativeFetcher {
// valCh := make(chan *NativeFetcher, 1)
// f.Eventloop.RunOnLoop(func(vm *goja.Runtime) {
// valCh <- f.NewNativeFetchVM(t, vm)
// })
// return <-valCh
// }
func (f *Fetch) NewNativeFetchVM(t *common.IOContext, vm *goja.Runtime) *NativeFetcher {
fetcher := f.fetcherPool.Get()
fetcher.nativeWrapper.ioContext = t
t.RegisterCleanup(func() {
fetcher.nativeWrapper.ioContext = nil
f.fetcherPool.Put(fetcher)
fetcherPut.Add(1)
})
return fetcher
}