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
|
package js
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
"flyscrape/js/jsbundle"
"github.com/evanw/esbuild/pkg/api"
v8 "rogchap.com/v8go"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type RunOptions struct {
HTML string
}
type RunFunc func(RunOptions) any
type Options struct {
URL string `json:"url"`
}
func Compile(file string) (*Options, RunFunc, error) {
src, err := build(file)
if err != nil {
return nil, nil, err
}
os.WriteFile("out.js", []byte(src), 0o644)
return vm(src)
}
func build(file string) (string, error) {
dir, err := os.MkdirTemp("", "flyscrape")
if err != nil {
return "", err
}
defer os.RemoveAll(dir)
tmpfile := filepath.Join(dir, "flyscrape.js")
if err := os.WriteFile(tmpfile, jsbundle.Flyscrape, 0o644); err != nil {
return "", err
}
resolve := api.Plugin{
Name: "flyscrape",
Setup: func(build api.PluginBuild) {
build.OnResolve(api.OnResolveOptions{
Filter: "^flyscrape$",
}, func(ora api.OnResolveArgs) (api.OnResolveResult, error) {
return api.OnResolveResult{Path: tmpfile}, nil
})
},
}
res := api.Build(api.BuildOptions{
EntryPoints: []string{file},
Bundle: true,
Platform: api.PlatformNode,
Plugins: []api.Plugin{resolve},
})
var errs []error
for _, msg := range res.Errors {
errs = append(errs, fmt.Errorf("%s", msg.Text))
}
if len(res.Errors) > 0 {
return "", errors.Join(errs...)
}
out := string(res.OutputFiles[0].Contents)
return out, nil
}
func vm(src string) (*Options, RunFunc, error) {
os.WriteFile("out.js", []byte(src), 0o644)
ctx := v8.NewContext()
ctx.RunScript("var module = {}", "main.js")
if _, err := ctx.RunScript(src, "main.js"); err != nil {
return nil, nil, fmt.Errorf("run bundled js: %w", err)
}
val, err := ctx.RunScript("module.exports.options", "main.js")
if err != nil {
return nil, nil, fmt.Errorf("export options: %w", err)
}
options, err := val.AsObject()
if err != nil {
return nil, nil, fmt.Errorf("cast options as object: %w", err)
}
var opts Options
url, err := options.Get("url")
if err != nil {
return nil, nil, fmt.Errorf("getting url from options: %w", err)
}
opts.URL = url.String()
run := func(ro RunOptions) any {
suffix := randSeq(10)
ctx.Global().Set("html_"+suffix, ro.HTML)
data, err := ctx.RunScript(fmt.Sprintf(`JSON.stringify(module.exports.default({html: html_%s}))`, suffix), "main.js")
if err != nil {
return err.Error()
}
var obj any
if err := json.Unmarshal([]byte(data.String()), &obj); err != nil {
return err.Error()
}
return obj
}
return &opts, run, nil
}
func randSeq(n int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
|