-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (132 loc) · 3.56 KB
/
main.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
package main
import (
"context"
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"runtime"
"strconv"
"strings"
_ "unsafe"
cdruntime "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
)
//go:embed index.html
var Index string
func init() {
log.SetFlags(0)
log.SetPrefix("wasmexec: ")
}
//go:linkname runtime_addExitHook runtime.addExitHook
func runtime_addExitHook(f func(), runOnNonZeroExit bool)
func main() {
if len(os.Args) < 2 {
log.Fatal("path to the test file is required")
}
p := os.Args[1]
// read the contents of the wasm file.
wf, err := os.ReadFile(p)
if err != nil {
log.Fatal(err)
}
// read the wasm exec file.
exf, err := os.ReadFile(path.Join(runtime.GOROOT(), "misc/wasm/wasm_exec.js"))
if err != nil {
log.Fatal(err)
}
// parse the template.
tmpl, err := template.New("index").Parse(Index)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
// serve index.html.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/html")
err := tmpl.Execute(w, struct {
Args []string
}{
Args: os.Args[2:],
})
if err != nil {
log.Fatal(err)
}
})
// serve the wasm_exec.js file.
mux.HandleFunc("/wasm_exec.js", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/javascript")
w.Write(exf)
})
// serve the wasm file.
mux.HandleFunc("/main.wasm", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/wasm")
w.Write(wf)
})
// create a testing server with a using random port.
srv := httptest.NewServer(mux)
defer srv.Close()
args := chromedp.DefaultExecAllocatorOptions[:]
env := os.Getenv("WASMEXEC_HEADLESS")
if strings.ToLower(env) == "false" || env == "0" {
args = append(args, chromedp.Flag("headless", false))
}
exec, cancel := chromedp.NewExecAllocator(context.Background(), args...)
defer cancel()
// create a new chrome instance.
ctx, cancel := chromedp.NewContext(exec)
defer cancel()
// defer only runs when we finish the program or we panic, Go doesn't provide a public way to handle an exit event.
// we use the private runtime exit hook here to ensure Chrome has been quit.
runtime_addExitHook(func() {
chromedp.Cancel(ctx)
}, true)
done := make(chan bool, 1)
// listen for events from chrome.
chromedp.ListenTarget(ctx, func(e any) {
switch e := e.(type) {
// either log the message or mark as done.
case *cdruntime.EventConsoleAPICalled:
for _, arg := range e.Args {
av := string(arg.Value)
// try to unquote the argument value.
v, err := strconv.Unquote(av)
if err != nil {
// fallback to quoted value.
v = av
}
// wait for the exit message.
if v == "wasmexec:exit" {
done <- true
break
}
// log the message.
fmt.Printf("%v ", v)
}
fmt.Print("\n")
// handle any exception.
case *cdruntime.EventExceptionThrown:
log.Fatal(e.ExceptionDetails.Error())
}
})
// parse the server URL.
u, err := url.Parse(srv.URL)
if err != nil {
log.Fatal(err)
}
// change the host to localhost from 127.0.0.1, as it's treated as a secure context in chrome.
// https://github.com/chromium/chromium/blob/bc3997b37babf6be84386bcaf780d81af5f4dcfb/services/network/public/cpp/is_potentially_trustworthy.cc#L304-L310.
u.Host = "localhost:" + u.Port()
// launch chrome and navigate to the homepage.
err = chromedp.Run(ctx, chromedp.Navigate(u.String()))
if err != nil {
log.Fatal(err)
}
// wait for program to exit.
<-done
}