-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwasmserve.go
241 lines (224 loc) · 6.47 KB
/
wasmserve.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
//go:build !js
// +build !js
package gwasm
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
)
// Taken from /~https://github.com/hajimehoshi/wasmserve with slight modifications
const indexHTML = `<!DOCTYPE html>
<!-- Polyfill for the old Edge browser -->
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
<script src="wasm_exec.js"></script>
<script>
(async () => {
const resp = await fetch('main.wasm');
if (!resp.ok) {
const pre = document.createElement('pre');
pre.innerText = await resp.text();
document.body.appendChild(pre);
} else {
const src = await resp.arrayBuffer();
const go = new Go();
const result = await WebAssembly.instantiate(src, go.importObject);
go.argv = [];
go.run(result.instance);
}
const reload = await fetch('_wait');
// The server sends a response for '_wait' when a request is sent to '_notify'.
if (reload.ok) {
location.reload();
}
})();
</script>
`
// WASMHandler serves a WASM application. Implements http.Handler interface.
type WASMHandler struct {
// Compiler is the tool used to compile the WASM binary. Can be "go", "tinygo".
Compiler string
// IndexHTML is the html served to the user when loading the application. Should contain
// the WASM bootstrap javascript to correctly load the WASM binary.
IndexHTML string
// WASMReload set to true recompiles the WASM application on every request.
WASMReload bool
// WASMDir points to the directory with the package/module with the Go WASM application.
// If WASMDir is the empty string, it will serve the WASMApplication without compiling.
WASMDir string
// WASMApplication is the compiled WASM binary data as output by the go tool.
WASMApplication []byte
// Bootloader script. Filename can be found in a go installation by running
// out, err := exec.Command(wsm.Compiler, "env", "GOROOT").Output()
// if err != nil {
// return nil, fmt.Errorf("%w: %s", err, string(out))
// }
// filename := filepath.Join(strings.TrimSpace(string(out)), "misc", "wasm", "wasm_exec.js")
WASMExecContent []byte
wasmModTime time.Time
tmpOutputDir string
output io.Writer
startTime time.Time
waitChannel chan struct{}
subHandler http.HandlerFunc
}
// NewWASMHandler returns a handler which does basically the same thing as /~https://github.com/hajimehoshi/wasmserve.
//
// Example of usage which does most of what `wasmserve` does:
// wsm, err := gwasm.NewWASMHandler("app", nil)
// if err != nil {
// log.Fatal(err)
// }
// wsm.WASMReload = true
// wsm.SetOutput(os.Stdout)
// http.Handle("/", wsm)
// log.Fatal(http.ListenAndServe(":8080", nil))
func NewWASMHandler(wasmDir string, subHandler http.HandlerFunc) (*WASMHandler, error) {
if wasmDir == "" {
wasmDir = "."
}
var err error
wsm := &WASMHandler{
Compiler: "go",
WASMDir: wasmDir,
startTime: time.Now(),
waitChannel: make(chan struct{}),
subHandler: subHandler,
}
out, err := exec.Command(wsm.Compiler, "env", "GOROOT").Output()
if err != nil {
return nil, fmt.Errorf("%w: %s", err, string(out))
}
f := filepath.Join(strings.TrimSpace(string(out)), "misc", "wasm", "wasm_exec.js")
wsm.WASMExecContent, err = readFile(f)
if err != nil {
return nil, err
}
err = wsm.setTmpOutputDir()
if err != nil {
return nil, err
}
wsm.IndexHTML = indexHTML
err = wsm.buildWASM()
if err != nil {
return nil, err
}
return wsm, nil
}
// ServeHTTP implements http.Handler interface. For use with http.Handle
func (wsm *WASMHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path[1:]
fpath := path.Base(upath)
if !strings.HasSuffix(r.URL.Path, "/") {
fi, err := os.Stat(fpath)
if err != nil && !os.IsNotExist(err) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if fi != nil && fi.IsDir() {
http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
return
}
}
baseFpath := filepath.Base(fpath)
switch baseFpath {
case ".", "index.html":
http.ServeContent(w, r, "index.html", time.Now(), strings.NewReader(wsm.IndexHTML))
return
case "wasm_exec.js":
http.ServeContent(w, r, "wasm_exec.js", wsm.startTime, bytes.NewReader(wsm.WASMExecContent))
return
case "main.wasm":
if wsm.WASMReload && wsm.WASMDir != "" {
err := wsm.buildWASM()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if len(wsm.WASMApplication) == 0 {
http.Error(w, "no wasm content", http.StatusInternalServerError)
return
}
http.ServeContent(w, r, "main.wasm", wsm.wasmModTime, bytes.NewReader(wsm.WASMApplication))
return
case "_wait":
wsm.waitForUpdate(w, r)
return
case "_notify":
wsm.notifyWaiters(w, r)
return
}
if wsm.subHandler != nil {
wsm.subHandler(w, r)
} else {
msg := "\"" + fpath + "\" path not found\n"
wsm.log([]byte(msg))
http.Error(w, msg, 404)
}
}
func (wsm *WASMHandler) setTmpOutputDir() (err error) {
wsm.tmpOutputDir, err = ioutil.TempDir("", "")
return err
}
func (wsm *WASMHandler) buildWASM() error {
buildName := filepath.Join(wsm.tmpOutputDir, "main.wasm")
args := []string{"build", "-o", buildName}
wsm.log([]byte(wsm.Compiler + " " + strings.Join(args, " ")))
cmdBuild := exec.Command(wsm.Compiler, args...)
cmdBuild.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
cmdBuild.Dir = wsm.WASMDir
out, err := cmdBuild.CombinedOutput()
if err != nil {
return fmt.Errorf("%w\n%s", err, string(out))
}
if len(out) > 0 {
wsm.log(out)
}
wasmContent, err := readFile(buildName)
if err != nil {
return err
}
wsm.WASMApplication = wasmContent
wsm.wasmModTime = time.Now()
return nil
}
// SetOutput sets logging write output to visualize compile-time errors and bad requests.
func (wsm *WASMHandler) SetOutput(w io.Writer) { wsm.output = w }
func (wsm *WASMHandler) log(b []byte) {
if wsm.output != nil {
wsm.output.Write(b)
if b[len(b)-1] != '\n' {
wsm.output.Write([]byte{'\n'})
}
}
}
func (wsm *WASMHandler) waitForUpdate(w http.ResponseWriter, r *http.Request) {
wsm.waitChannel <- struct{}{}
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(nil))
}
func (wsm *WASMHandler) notifyWaiters(w http.ResponseWriter, r *http.Request) {
for {
select {
case <-wsm.waitChannel:
default:
http.ServeContent(w, r, "", time.Now(), bytes.NewReader(nil))
return
}
}
}
func readFile(filename string) ([]byte, error) {
fp, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fp.Close()
return ioutil.ReadAll(fp)
}