Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recompiled spidermonkey so we do not need to hex encode stdout #24

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
}

func seedEndpoint(store storage.Store, cache storage.ModCacher) {
b, err := os.ReadFile("examples/go/app.wasm")
b, err := os.ReadFile("examples/js/index.js")
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 6 additions & 0 deletions examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ func handleDashboard(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from the dashboard handler"))
}

func handleIndex(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("login page: <a href=\"/login\" /><br />Dashboard page: <a href=\"/dashboard\" />"))
}

func main() {
router := chi.NewMux()
router.Get("/dashboard", handleDashboard)
router.Get("/login", handleLogin)
router.Get("/", handleIndex)
raptor.Handle(router)
}
29 changes: 8 additions & 21 deletions examples/js/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
// This should come from the official SDK.
// But there is no official SDK yet, so we keep it here.
function hexLog(s) {
for (let i = 0; i < s.length; i++) {
putstr(s.charCodeAt(i).toString(16).padStart(2, "0"))
}
putstr("0a")
}

console.log = hexLog

function respond(res, status) {
var buffer = new ArrayBuffer(8);
var view = new DataView(buffer);
view.setUint32(0, status, true);
view.setUint32(4, res.length, true);

for (let i = 0; i < res.length; i++) {
putstr(res.charCodeAt(i).toString(16).padStart(2, "0"))
}
for (let i = 0; i < view.buffer.byteLength; i++) {
putstr(view.getUint8(i).toString(16).padStart(2, "0"));
}
putstr(res);
writebytes(view)
}

console.log("user log here")
console.log("user log here")
console.log("user log here")
console.log("user log here")
console.log("user log here")
console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");

respond("<h1>From my Raptor application</h1></br>some other stuff here</br>", 200)
respond("Hello world!", 200)
23 changes: 8 additions & 15 deletions internal/_testdata/helloworld.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
// This should come from the official SDK.
// But there is no official SDK yet, so we keep it here.
function hexLog(s) {
for (let i = 0; i < s.length; i++) {
putstr(s.charCodeAt(i).toString(16).padStart(2, "0"))
}
putstr("0a")
}

console.log = hexLog

function respond(res, status) {
var buffer = new ArrayBuffer(8);
var view = new DataView(buffer);
view.setUint32(0, status, true);
view.setUint32(4, res.length, true);

for (let i = 0; i < res.length; i++) {
putstr(res.charCodeAt(i).toString(16).padStart(2, "0"))
}
for (let i = 0; i < view.buffer.byteLength; i++) {
putstr(view.getUint8(i).toString(16).padStart(2, "0"));
}
putstr(res);
writebytes(view)
}

console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");
console.log("USER LOGS");

respond("Hello world!", 200)
2 changes: 1 addition & 1 deletion internal/actrs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *Runtime) handleHTTPRequest(ctx *actor.Context, msg *proto.HTTPRequest)
return
}

logs, res, status, err := shared.ParseStdout(msg.Runtime, r.stdout)
logs, res, status, err := shared.ParseStdout(r.stdout)
if err != nil {
respondError(ctx, http.StatusInternalServerError, "invalid response", msg.ID)
return
Expand Down
4 changes: 2 additions & 2 deletions internal/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestRuntimeInvokeJSCode(t *testing.T) {
scriptArgs := []string{"", "-e", string(b)}
require.Nil(t, r.Invoke(bytes.NewReader(breq), nil, scriptArgs...))

_, res, status, err := shared.ParseStdout("js", out)
_, res, status, err := shared.ParseStdout(out)
require.Nil(t, err)
require.Equal(t, http.StatusOK, status)
require.Equal(t, "Hello world!", string(res))
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestRuntimeInvokeGoCode(t *testing.T) {
r, err := New(context.Background(), args)
require.Nil(t, err)
require.Nil(t, r.Invoke(bytes.NewReader(breq), nil))
_, res, status, err := shared.ParseStdout("go", out)
_, res, status, err := shared.ParseStdout(out)
require.Nil(t, err)
require.Equal(t, http.StatusOK, status)
require.Equal(t, "Hello world!", string(res))
Expand Down
15 changes: 2 additions & 13 deletions internal/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (

var errInvalidHTTPResponse = errors.New("invalid HTTP response")

func ParseStdout(runtime string, stdout io.Reader) (logs []byte, resp []byte, status int, err error) {
stdoutb, err := io.ReadAll(GetDecodedStdout(runtime, stdout))
func ParseStdout(stdout io.Reader) (logs []byte, resp []byte, status int, err error) {
stdoutb, err := io.ReadAll(stdout)
if err != nil {
return
}
Expand Down Expand Up @@ -93,17 +93,6 @@ func makeProtoHeader(header http.Header) map[string]*proto.HeaderFields {
return m
}

func GetDecodedStdout(runtime string, stdout io.Reader) io.Reader {
switch runtime {
case "go":
return stdout
case "js":
return hex.NewDecoder(stdout)
default:
return stdout
}
}

func IsZeroUUID(id uuid.UUID) bool {
return id.String() == UUIDZERO
}
6 changes: 3 additions & 3 deletions internal/shared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ the big brown fox
builder.WriteString(userLogs)
builder.WriteString(userResp)
builder.Write(buf)
if _, _, _, err := ParseStdout("go", builder); err != nil {
if _, _, _, err := ParseStdout(builder); err != nil {
log.Fatal(err)
}
builder.Reset()
Expand All @@ -54,7 +54,7 @@ func TestParseWithoutUserLogs(t *testing.T) {
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(userResp)))
builder.Write(buf)

logs, resp, status, err := ParseStdout("go", builder)
logs, resp, status, err := ParseStdout(builder)
require.Nil(t, err)
require.Equal(t, int(statusCode), status)
require.Equal(t, userResp, string(resp))
Expand All @@ -81,7 +81,7 @@ the big brown fox
binary.LittleEndian.PutUint32(buf[4:8], uint32(len(userResp)))
builder.Write(buf)

logs, resp, status, err := ParseStdout("go", builder)
logs, resp, status, err := ParseStdout(builder)
require.Nil(t, err)
require.Equal(t, int(statusCode), status)
require.Equal(t, userResp, string(resp))
Expand Down
Binary file modified internal/spidermonkey/js.wasm
Binary file not shown.
Loading