mirror of
https://github.com/hanzoai/zip.git
synced 2026-10-30 20:11:40 +00:00
Establish zip as the canonical Hanzo Go web framework with embedded
in-process JS execution and a native ZAP RPC HTTP transport. One Go
binary mounts native handlers, legacy TS/JS handlers, WebSocket, and
the ZAP RPC plane — no separate runtime service, no inter-service RPC,
no container-per-service.
zip/runtime/ (new):
- jsvm.go NewJSRuntime / Eval / RegisterHostFn / LoadModule, backed
by a pool of *goja.Runtime (pure Go, no CGO). Pool pattern
lifted from hanzoai/base/plugins/gojavm, slimmed to run +
forEach so late host-fn / module registration reaches warm
VMs.
- pool.go fixed-size goja VM pool; saturated calls fall back to a
fully-provisioned one-off VM.
- require.go CommonJS-style require(): per-VM module sources + memoized
exports in a Go-side side-table (never visible to JS).
- esbuild.go TranspileToES5: TS/modern-JS -> goja-ready JS via esbuild's
pure-Go API (ES2015 target, CommonJS format).
- handler.go JSHandler / JSModule: mount an Express-shaped (req, res) JS
function as a fiber.Handler; req mirrors method/path/query/
headers/body, res mirrors status/set/json/send.
- json.go encode/decode through internal/jsonenc — one wire format.
zip/zaprpc/ (new on existing Registry seam):
- envelope.go canonical ZAP RPC frame (service/method/payload) over the
zap-proto/go wire codec.
- http.go HTTPHandler(reg): serves the ZAP RPC plane over HTTP POST;
binary envelope canonical, X-ZAP-* headers are a hint only.
examples/express-in-zip/ (new): legacy app.ts -> real esbuild -> real
goja -> real Fiber -> real HTTP, with an integration test proving
GET /legacy/foo and POST /legacy/bar body echo.
README: embedded-JS migration path + single-binary (HIP-0106) sections.
go build ./... and go test ./... (incl. -race) pass.
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"io"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// TestJSHandler_EndToEnd: an Express-shaped handler defined as a global
|
|
// JS function, mounted via JSHandler, exercised over a real HTTP
|
|
// roundtrip through Fiber.
|
|
func TestJSHandler_EndToEnd(t *testing.T) {
|
|
rt, _ := NewJSRuntime(JSOptions{PoolSize: 2})
|
|
_, err := rt.Eval(`
|
|
function handler(req, res) {
|
|
res.status(201).set("X-From", "goja").json({
|
|
ok: true,
|
|
method: req.method,
|
|
path: req.path,
|
|
body: req.body,
|
|
});
|
|
}
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
app := fiber.New()
|
|
app.Post("/echo", JSHandler(rt, "handler"))
|
|
|
|
req := httptest.NewRequest("POST", "/echo", strings.NewReader(`{"x":1}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 201 {
|
|
t.Fatalf("status = %d, want 201", resp.StatusCode)
|
|
}
|
|
if got := resp.Header.Get("X-From"); got != "goja" {
|
|
t.Fatalf("X-From = %q, want goja", got)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
bs := string(body)
|
|
for _, want := range []string{`"ok":true`, `"method":"POST"`, `"path":"/echo"`, `"x":1`} {
|
|
if !strings.Contains(bs, want) {
|
|
t.Fatalf("body %q missing %q", bs, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestJSModule_EndToEnd: a CommonJS module whose exports IS the handler,
|
|
// loaded from esbuild-transpiled TS, mounted via JSModule.
|
|
func TestJSModule_EndToEnd(t *testing.T) {
|
|
rt, _ := NewJSRuntime(JSOptions{PoolSize: 1})
|
|
|
|
tsSrc := []byte(`
|
|
module.exports = function (req: any, res: any) {
|
|
res.json({ ok: true, path: req.path });
|
|
};
|
|
`)
|
|
es5, err := TranspileToES5(tsSrc, ESOptions{Loader: "ts"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := rt.LoadModule("app", string(es5)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
h, err := JSModule(rt, "app")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
app := fiber.New()
|
|
app.Get("/m", h)
|
|
|
|
resp, err := app.Test(httptest.NewRequest("GET", "/m", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
if !strings.Contains(string(body), `"path":"/m"`) {
|
|
t.Fatalf("body %q missing path", body)
|
|
}
|
|
}
|