-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime_instance.go
146 lines (120 loc) · 3.75 KB
/
runtime_instance.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
package heresy
import (
"context"
"fmt"
"sync/atomic"
"go.miragespace.co/heresy/event"
"go.miragespace.co/heresy/express"
"go.miragespace.co/heresy/extensions/common"
"go.miragespace.co/heresy/extensions/common/shared"
"go.miragespace.co/heresy/extensions/console"
"go.miragespace.co/heresy/extensions/fetch"
"go.miragespace.co/heresy/extensions/kv"
"go.miragespace.co/heresy/extensions/promise"
"go.miragespace.co/heresy/extensions/stream"
"go.miragespace.co/heresy/polyfill"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/require"
"github.com/dop251/goja_nodejs/url"
"go.uber.org/zap"
)
var nilInstance *runtimeInstance = nil
type handlerType int
const (
handlerTypeUnset handlerType = iota
handlerTypeExpress
handlerTypeEvent
)
type runtimeInstance struct {
logger *zap.Logger
middlewareHandler atomic.Value // goja.Value
middlewareType atomic.Value // handlerType
handlerOption atomic.Pointer[nativeHandlerOptions] // nativeHandlerOptions
ioContextPool *common.IOContextPool
contextPool *express.RequestContextPool
eventPool *event.FetchEventPool
eventLoop *eventloop.EventLoop
resolver *promise.PromiseResolver
stream *stream.StreamController
fetcher *fetch.Fetch
kv *kv.KVManager
vm *goja.Runtime
}
func (inst *runtimeInstance) stop(interrupt bool) {
if interrupt {
inst.vm.Interrupt(context.Canceled)
}
inst.eventLoop.StopNoWait()
}
func (inst *runtimeInstance) optionHelper(vm *goja.Runtime, opt goja.Value) {
if goja.IsUndefined(opt) {
return
}
var options nativeHandlerOptions
if err := vm.ExportTo(opt, &options); err == nil {
inst.handlerOption.Store(&options)
}
}
func (inst *runtimeInstance) prepareInstance(logger *zap.Logger, symbols *polyfill.RuntimeSymbols) (setup chan error) {
setup = make(chan error, 1)
inst.eventLoop.RunOnLoop(func(vm *goja.Runtime) {
url.Enable(vm)
vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
vm.Set("console", require.Require(vm, console.ModuleName))
vm.Set("registerExpressHandler", func(fc goja.FunctionCall) (ret goja.Value) {
ret = goja.Undefined()
opt := fc.Argument(1)
inst.optionHelper(vm, opt)
fn := fc.Argument(0)
if _, ok := goja.AssertFunction(fn); ok {
inst.middlewareHandler.Store(fn)
inst.middlewareType.Store(handlerTypeExpress)
}
return
})
vm.Set("registerEventHandler", func(fc goja.FunctionCall) (ret goja.Value) {
ret = goja.Undefined()
opt := fc.Argument(1)
inst.optionHelper(vm, opt)
fn := fc.Argument(0)
if _, ok := goja.AssertFunction(fn); ok {
inst.middlewareHandler.Store(fn)
inst.middlewareType.Store(handlerTypeEvent)
}
return
})
headersPool := shared.NewHeadersProxyPool(vm, symbols)
inst.ioContextPool = common.NewIOContextPool(logger, headersPool, 10)
inst.contextPool = express.NewRequestContextPool(express.RequestContextDeps{
Logger: logger,
Eventloop: inst.eventLoop,
Fetch: inst.fetcher,
KV: inst.kv,
})
inst.eventPool = event.NewFetchEventPool(event.FetchEventDeps{
Logger: logger,
Symbols: symbols,
Eventloop: inst.eventLoop,
Stream: inst.stream,
Resolver: inst.resolver,
Fetch: inst.fetcher,
KV: inst.kv,
})
inst.vm = vm // reference is kept for .Interrupt
setup <- nil
})
return
}
func (inst *runtimeInstance) loadProgram(prog *goja.Program) (setup chan error) {
setup = make(chan error, 1)
inst.eventLoop.RunOnLoop(func(vm *goja.Runtime) {
_, err := vm.RunProgram(prog)
if err != nil {
setup <- fmt.Errorf("error setting up handler script: %w", err)
return
}
setup <- nil
})
return
}