forked from iopipe/turtle-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
355 lines (309 loc) · 6.94 KB
/
filter.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"github.com/Sirupsen/logrus"
"github.com/robertkrimen/otto"
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"path"
"strings"
"crypto/sha256"
"io/ioutil"
"net/http"
)
const FILTER_BASE string = "http://192.241.174.50/filters/"
const REQUIREJS_URL string = "http://requirejs.org/docs/release/2.1.22/r.js"
type filterTuple struct {
fromObjType string
toObjType string
}
func listScripts() ([]string, error) {
var results []string
path, err := getCachePath("")
if err != nil {
return nil, err
}
fi, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range fi {
results = append(results, file.Name())
}
return results, nil
}
func exportScript(name string, args ...string) error {
var basedir = path.Join("export", name)
var err error
if err = os.MkdirAll(basedir, 700); err != nil {
return err
}
var scriptf = path.Join(basedir, "index.js")
var packagef = path.Join(basedir, "package.json")
var filters []string
for _, filter := range args {
filters = append(filters, fmt.Sprintf("\"%s\"", filter))
}
var pipeline = strings.Join(filters, ",")
var jscript = fmt.Sprintf(
`/* Generated by 'iopipe export'
var iopipe = require("iopipe")
module.exports = function() {
if (arguments.length > 2) {
throw "Too many arguments"
}
if (arguments.length == 2) {
callback = arguments[1]
return iopipe.define(%s, callback)(arguments[0])
}
if (arguments.length == 1) {
callback = arguments[0]
return iopipe.define(%s)(arguments[0])
}
return iopipe.exec(%s)
}
`, pipeline, pipeline, pipeline)
var jpackage = fmt.Sprintf(
`{
"name": "%s",
"version": "0.0.1",
"description": "%s exported via iopipe",
"author": "iopipe exporter",
"dependencies": {
"iopipe": "",
"read-stream": "",
"request": ""
},
"main": "./index.js"
}
`, name, name)
if err = ioutil.WriteFile(scriptf, []byte(jscript), 400); err != nil {
return err
}
if err = ioutil.WriteFile(packagef, []byte(jpackage), 400); err != nil {
return err
}
return nil
}
func makeFilter(script string) (func(input string) (string, error), error) {
vm := otto.New()
return makeFilterWithVM(vm, script)
}
func makeFilterWithVM(vm *otto.Otto, script string) (func(input string) (string, error), error) {
var (
res *http.Response
body []byte
err error
)
res, err = http.Get(REQUIREJS_URL)
if err != nil {
log.Fatal(err)
}
body, err = ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
rjs := string(body[:])
return func(input string) (string, error) {
logrus.Debug("Adding RequireJS")
vm.Run(rjs)
logrus.Debug("Executing script: " + script)
_, err := vm.Run(`
var module = { "exports": function() { } }
`)
if err != nil {
return "", err
}
_, err = vm.Run(script)
if err != nil {
return "", err
}
event := make(chan otto.Value)
oval, err := vm.ToValue(input)
if err != nil {
return "", err
}
jscontext := createContext(vm, event)
go vm.Call("module.exports", nil, oval, jscontext)
value := <-event
return value.ToString()
}, nil
}
func createContext(vm *otto.Otto, event chan otto.Value) otto.Value {
var cbfunc = func(invar ...interface{}) {
var lovar otto.Value
if len(invar) > 0 {
lovar, _ = vm.ToValue(invar[0])
} else {
lovar = otto.NullValue()
}
event <- lovar
close(event)
}
vm.Set("_iopipe_cb", cbfunc)
vm.Run(`_iopipe_context = { "done": _iopipe_cb,
"success": _iopipe_cb,
"fail": _iopipe_cb }`)
jscontext, _ := vm.Get("_iopipe_context")
return jscontext
}
func fetchFilter(filterPath string) ([]byte, error) {
var (
res *http.Response
body []byte
err error
)
path := path.Join(FILTER_BASE, filterPath)
res, err = http.Get(path)
if err != nil {
return nil, err
}
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
res.Body.Close()
/* Verify digest */
chksum := sha256.Sum256(body[:])
if filterPath != string(chksum[:]) {
return nil, errors.New("Checksum failure")
}
return body, nil
}
func importScript(file string) (string, error) {
var err error
var fH io.Reader
if file == "-" {
fH = os.Stdin
} else {
fH, err = os.Open(file)
if err != nil {
return "", err
}
}
reader := bufio.NewReader(fH)
body, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
id, err := writeFilterCache(body[:])
if err != nil {
return id, err
}
return id, nil
}
func getFilter(filterPath string) (func(input string) (string, error), error) {
var script []byte
var err error
/* Do we have this cached? */
if script, err := readFilterCache(filterPath); err == nil {
return makeFilter(string(script[:]))
} else {
return nil, err
}
/* If not, fetch */
if script, err = fetchFilter(filterPath); err != nil {
return nil, err
}
if _, err = writeFilterCache(script); err != nil {
return nil, err
}
return makeFilter(string(script[:]))
}
func getPipeline(filterPath string) (func(input string) (string, error), error) {
var script []byte
var err error
diskPath, err := getCachePath("")
if err != nil {
return nil, err
}
/* Do we have this cached? */
if _, err := os.Stat(diskPath); err == nil {
script, err = ioutil.ReadFile(diskPath)
return makeFilter(string(script[:]))
}
/* If not, fetch */
if script, err = fetchFilter(filterPath); err != nil {
return nil, err
}
/* Write cache */
if _, err = writeFilterCache(script); err != nil {
return nil, err
}
return makeFilter(string(script[:]))
}
func findFilters(fromObjType string, toObjType string) (string, error) {
var (
res *http.Response
body []byte
err error
)
path := path.Join(FILTER_BASE, fromObjType, toObjType)
res, err = http.Get(path)
if err != nil {
return "", err
}
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
res.Body.Close()
response := string(body[:])
return response, nil
}
func findPipelines(fromObjType string, toObjType string) (string, error) {
var (
res *http.Response
body []byte
err error
)
path := path.Join(FILTER_BASE, fromObjType, toObjType)
res, err = http.Get(path)
if err != nil {
return "", err
}
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
res.Body.Close()
response := string(body[:])
return response, nil
}
func publishPipeline(pipelineName string) {
}
func subscribePipeline(pipelineName string) {
}
func createPipeline(pipeparts []string) (string, error) {
return "", nil
}
func removeFilter(filterid string) error {
path, err := getCachePath(filterid)
if err != nil {
return err
}
err = os.Remove(path)
if err != nil {
return err
}
return nil
}
func tagPipeline(pipeline string) error {
return nil
}
func tagFilter(filterid string, name string) error {
path, err := getCachePath(filterid)
if err != nil {
return err
}
destpath, err := getCachePath(name)
if err != nil {
return err
}
err = os.Symlink(path, destpath)
return err
}