mirror of
https://github.com/hanzoai/zip.git
synced 2026-10-30 20:11:40 +00:00
Add Runner — a registry of language → Engine that executes arbitrary-
language source in goroutine-scoped, context.Context-bound tasks. Each
Run spawns a worker (engine.Eval) and a watcher (ctx.Done() → engine
interrupt), joining both before return so a borrowed VM is never handed
back to its pool while still interrupted. This generalizes the settled
JSRuntime.EvalContext cancellation contract (one worker, one watcher,
joined, engine-specific interrupt) across every backend a host registers.
zip ships exactly one in-tree engine: the pure-Go goja engine, surfaced
via (*JSRuntime).Engine() and registered as "js". It self-manages ctx
inside EvalContext (per-call interrupt on the exact borrowed VM), so the
Runner's watcher is a benign no-op for it. base's backends (pyvm, v8vm,
wasmvm, starkvm) plug in through the same Engine seam via thin extruntime
adapters at app startup — the registry is the seam, not an import edge.
zip never imports base; the dependency direction stays base → zip.
Engine is the zip-side projection of base's extruntime backends: any
value implementing Eval(ctx, src, args) satisfies it. Optional
Interruptible lets the watcher abort a runaway eval out-of-band.
Wire the unified runner into examples/express-in-zip: POST /runtime/:lang
reads the body as source, dispatches to the engine for :lang, returns
{result} or 404 {error:"unknown language"}.
Tests (go test -race, all green):
- JS happy path: Run(ctx, "js", "1+2") → int64(3)
- JS function call: define + invoke in one eval
- JS ctx deadline: while(true){} interrupted, DeadlineExceeded, ~50ms
- unknown language: ErrUnknownLanguage sentinel, no panic
- concurrent: 100 goroutines, registry RWMutex + per-call VM isolation
- example: POST /runtime/js "40+2" → {"result":42}; cobol → 404
No cgo. No base import. README documents the Runner + SPI registration.
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestExpressInZip is the proof point: the legacy TS handler in app.ts,
|
|
// transpiled by real esbuild, loaded into real goja, mounted on real
|
|
// Fiber via zip, exercised over a real HTTP roundtrip.
|
|
func TestExpressInZip(t *testing.T) {
|
|
app, err := setup()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Run("GET /legacy/foo", func(t *testing.T) {
|
|
resp, err := app.Fiber().Test(httptest.NewRequest("GET", "/legacy/foo", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
assertContainsAll(t, string(body), `"ok":true`, `"path":"/foo"`, `"body":null`)
|
|
})
|
|
|
|
t.Run("POST /legacy/bar echoes body", func(t *testing.T) {
|
|
req := httptest.NewRequest("POST", "/legacy/bar", strings.NewReader(`{"x":1}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
assertContainsAll(t, string(body), `"ok":true`, `"path":"/bar"`, `"x":1`)
|
|
})
|
|
}
|
|
|
|
// TestRuntimeRoute drives the unified runner over HTTP: the request body
|
|
// is the source, :lang selects the backend. js evaluates in real goja; an
|
|
// unregistered language is a 404 with a structured error.
|
|
func TestRuntimeRoute(t *testing.T) {
|
|
app, err := setup()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Run("POST /runtime/js evaluates source", func(t *testing.T) {
|
|
req := httptest.NewRequest("POST", "/runtime/js", strings.NewReader("40+2"))
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
// goja Export() of an integer arithmetic result is int64; JSON
|
|
// encodes it as the bare number 42.
|
|
assertContainsAll(t, string(body), `"result":42`)
|
|
})
|
|
|
|
t.Run("POST /runtime/cobol is 404 unknown language", func(t *testing.T) {
|
|
req := httptest.NewRequest("POST", "/runtime/cobol", strings.NewReader("DISPLAY 'HI'."))
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 404 {
|
|
t.Fatalf("status = %d, want 404", resp.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
assertContainsAll(t, string(body), `"error":"unknown language"`)
|
|
})
|
|
}
|
|
|
|
func assertContainsAll(t *testing.T, body string, wants ...string) {
|
|
t.Helper()
|
|
for _, w := range wants {
|
|
if !strings.Contains(body, w) {
|
|
t.Fatalf("body %q missing %q", body, w)
|
|
}
|
|
}
|
|
}
|