-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.go
More file actions
147 lines (121 loc) · 3.14 KB
/
js.go
File metadata and controls
147 lines (121 loc) · 3.14 KB
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
package wazemmes
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"go.uber.org/zap"
)
type request struct {
Headers http.Header `json:"headers"`
URL *url.URL `json:"url"`
Body string `json:"body"`
Method string `json:"method"`
}
type response struct {
Headers http.Header `json:"headers"`
Body string `json:"body"`
Status int `json:"status"`
}
type baseHandler struct {
Request request `json:"request"`
Response response `json:"response"`
Error string `json:"error"`
}
type Input struct {
BaseHandler baseHandler `json:"-"`
Context string `json:"context"`
}
type Output = baseHandler
func NewWasmHandlerJS(modulepath string, _ any, poolConfiguration map[string]interface{},
logger *zap.Logger) (*WasmHandler, error) {
ctx := context.Background()
runtime := wazero.NewRuntime(ctx)
if _, err := wasi_snapshot_preview1.Instantiate(ctx, runtime); err != nil {
return nil, fmt.Errorf("failed to instantiate WASI: %w", err)
}
wasmFile, err := os.ReadFile(modulepath)
if err != nil {
return nil, fmt.Errorf("failed to read WASM module: %w", err)
}
compiled, err := runtime.CompileModule(ctx, wasmFile)
if err != nil {
return nil, fmt.Errorf("failed to compile WASM module: %w", err)
}
wasmHandlerJS := &JSWASMHandler{
runtime: runtime,
compiledModule: compiled,
}
return NewWasmHandlerInstance(
func(ctx context.Context, next Handler) Handler {
return wasmHandlerJS
},
poolConfiguration,
logger,
)
}
type JSWASMHandler struct {
runtime wazero.Runtime
compiledModule wazero.CompiledModule
}
func (h *JSWASMHandler) ServeHTTP(rw http.ResponseWriter, httpReq *http.Request) error {
ctx := httpReq.Context()
var buf bytes.Buffer
if httpReq.Body != nil {
_, _ = io.Copy(&buf, httpReq.Body)
_ = httpReq.Body.Close()
httpReq.Body = io.NopCloser(bytes.NewBuffer(buf.Bytes()))
}
req := request{
Headers: httpReq.Header,
URL: httpReq.URL,
Body: buf.String(),
Method: httpReq.Method,
}
res := response{
Headers: httpReq.Header,
Body: buf.String(),
Status: 0,
}
reqBytes, _ := json.Marshal(Input{
BaseHandler: baseHandler{
Request: req,
Response: res,
Error: "",
},
Context: "request",
})
stdin := bytes.NewBuffer(reqBytes)
stdout := new(bytes.Buffer)
config := wazero.NewModuleConfig().
WithSysWalltime().
WithStartFunctions("_start", "_initialize").
WithStdin(stdin).
WithStdout(stdout).
WithStderr(os.Stderr)
module, _ := h.runtime.InstantiateModule(ctx, h.compiledModule, config)
defer func() {
_ = module.Close(ctx)
}()
var response baseHandler
_ = json.NewDecoder(bytes.NewReader(stdout.Bytes())).Decode(&response)
if response.Error != "" {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(response.Error))
return errors.New(response.Error)
}
for key, values := range response.Response.Headers {
for _, value := range values {
rw.Header().Set(key, value)
}
}
_, _ = rw.Write([]byte(response.Response.Body))
return nil
}