forked from TheThingsArchive/ttn
/
functions.go
147 lines (122 loc) · 3.86 KB
/
functions.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
// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package fields
import (
"errors"
"fmt"
"time"
"github.com/robertkrimen/otto"
)
// Functions decodes, converts and validates payload using JavaScript functions
type Functions struct {
// Decoder is a JavaScript function that accepts the payload as byte array and
// returns an object containing the decoded values
Decoder string
// Converter is a JavaScript function that accepts the data as decoded by
// Decoder and returns an object containing the converted values
Converter string
// Validator is a JavaScript function that validates the data is converted by
// Converter and returns a boolean value indicating the validity of the data
Validator string
}
// timeOut is the maximum allowed time a payload function is allowed to run
var timeOut = time.Second
// Decode decodes the payload using the Decoder function into a map
func (f *Functions) Decode(payload []byte) (map[string]interface{}, error) {
if f.Decoder == "" {
return nil, errors.New("Decoder function not set")
}
vm := otto.New()
vm.Set("payload", payload)
value, err := RunUnsafeCode(vm, fmt.Sprintf("(%s)(payload)", f.Decoder), timeOut)
if err != nil {
return nil, err
}
if !value.IsObject() {
return nil, errors.New("Decoder does not return an object")
}
v, _ := value.Export()
m, ok := v.(map[string]interface{})
if !ok {
return nil, errors.New("Decoder does not return an object")
}
return m, nil
}
// Convert converts the values in the specified map to a another map using the
// Converter function. If the Converter function is not set, this function
// returns the data as-is
func (f *Functions) Convert(data map[string]interface{}) (map[string]interface{}, error) {
if f.Converter == "" {
return data, nil
}
vm := otto.New()
vm.Set("data", data)
value, err := RunUnsafeCode(vm, fmt.Sprintf("(%s)(data)", f.Converter), timeOut)
if err != nil {
return nil, err
}
if !value.IsObject() {
return nil, errors.New("Converter does not return an object")
}
v, _ := value.Export()
m, ok := v.(map[string]interface{})
if !ok {
return nil, errors.New("Decoder does not return an object")
}
return m, nil
}
// Validate validates the values in the specified map using the Validator
// function. If the Validator function is not set, this function returns true
func (f *Functions) Validate(data map[string]interface{}) (bool, error) {
if f.Validator == "" {
return true, nil
}
vm := otto.New()
vm.Set("data", data)
value, err := RunUnsafeCode(vm, fmt.Sprintf("(%s)(data)", f.Validator), timeOut)
if err != nil {
return false, err
}
if !value.IsBoolean() {
return false, errors.New("Validator does not return a boolean")
}
return value.ToBoolean()
}
// Process decodes the specified payload, converts it and test the validity
func (f *Functions) Process(payload []byte) (map[string]interface{}, bool, error) {
decoded, err := f.Decode(payload)
if err != nil {
return nil, false, err
}
converted, err := f.Convert(decoded)
if err != nil {
return nil, false, err
}
valid, err := f.Validate(converted)
return converted, valid, err
}
var timeOutExceeded = errors.New("Code has been running to long")
func RunUnsafeCode(vm *otto.Otto, code string, timeOut time.Duration) (value otto.Value, err error) {
start := time.Now()
defer func() {
duration := time.Since(start)
if caught := recover(); caught != nil {
if caught == timeOutExceeded {
value = otto.Value{}
err = fmt.Errorf("Interrupted javascript execution after %v", duration)
return
}
// if this is not the our timeout interrupt, raise the panic again
// so someone else can handle it
panic(caught)
}
}()
vm.Interrupt = make(chan func(), 1)
go func() {
time.Sleep(timeOut)
vm.Interrupt <- func() {
panic(timeOutExceeded)
}
}()
return vm.Run(code)
}