This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathgoja.go
155 lines (134 loc) · 3.46 KB
/
goja.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
package gojajs
import (
"errors"
"io/ioutil"
"time"
"github.com/compose/mejson"
"github.com/compose/transporter/function"
"github.com/compose/transporter/log"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/data"
"github.com/compose/transporter/message/ops"
gojaVM "github.com/dop251/goja"
)
var (
_ function.Function = &goja{}
// ErrInvalidMessageType is a generic error returned when the `data` property returned in the document from
// the JS function was not of type map[string]interface{}
ErrInvalidMessageType = errors.New("returned document was not a map")
// ErrEmptyFilename will be returned when the profided filename is empty.
ErrEmptyFilename = errors.New("no filename specified")
)
func init() {
function.Add(
"goja",
func() function.Function {
return &goja{}
},
)
function.Add(
"js",
func() function.Function {
return &goja{}
},
)
}
type goja struct {
Filename string `json:"filename"`
vm *gojaVM.Runtime
}
// JSFunc defines the structure a transformer function.
type JSFunc func(map[string]interface{}) *gojaVM.Object
// Apply fulfills the function.Function interface by transforming the incoming message with the configured
// JavaScript function.
func (g *goja) Apply(msg message.Msg) (message.Msg, error) {
if g.vm == nil {
if err := g.initVM(); err != nil {
return nil, err
}
}
return g.transformOne(msg)
}
func (g *goja) initVM() error {
g.vm = gojaVM.New()
fn, err := extractFunction(g.Filename)
if err != nil {
return err
}
_, err = g.vm.RunString(fn)
return err
}
func extractFunction(filename string) (string, error) {
if filename == "" {
return "", ErrEmptyFilename
}
ba, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
return string(ba), nil
}
func (g *goja) transformOne(msg message.Msg) (message.Msg, error) {
var (
outDoc gojaVM.Value
doc interface{}
err error
)
now := time.Now().Nanosecond()
currMsg := data.Data{"ts": msg.Timestamp(), "op": msg.OP().String(), "ns": msg.Namespace()}
curData := msg.Data()
doc, err = mejson.Marshal(curData.AsMap())
if err != nil {
return msg, err
}
currMsg["data"] = doc
// lets run our transformer on the document
beforeVM := time.Now().Nanosecond()
var jsf JSFunc
g.vm.ExportTo(g.vm.Get("transform"), &jsf)
outDoc = jsf(currMsg)
var res map[string]interface{}
if g.vm.ExportTo(outDoc, &res); err != nil {
return msg, err
}
afterVM := time.Now().Nanosecond()
newMsg, err := toMsg(g.vm, msg, res)
if err != nil {
return nil, err
}
then := time.Now().Nanosecond()
log.With("transformed_in_micro", (then-now)/1000).
With("marshaled_in_micro", (beforeVM-now)/1000).
With("vm_time_in_micro", (afterVM-beforeVM)/1000).
With("unmarshaled_in_micro", (then-afterVM)/1000).
Debugln("document transformed")
return newMsg, nil
}
func toMsg(vm *gojaVM.Runtime, origMsg message.Msg, incoming map[string]interface{}) (message.Msg, error) {
var (
op ops.Op
ts int64
ns string
mapData data.Data
)
m := data.Data(incoming)
op = ops.OpTypeFromString(m.Get("op").(string))
if op == ops.Skip {
return nil, nil
}
ts = m.Get("ts").(int64)
ns = m.Get("ns").(string)
switch newData := m.Get("data").(type) {
case map[string]interface{}:
d, err := mejson.Unmarshal(newData)
if err != nil {
return nil, err
}
mapData = data.Data(d)
default:
return nil, ErrInvalidMessageType
}
msg := message.From(op, ns, mapData).(*message.Base)
msg.TS = ts
return msg, nil
}