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.
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package zaprpc
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// echoService is a tiny ZAP service: method "ping" returns its payload
|
|
// prefixed with "pong:".
|
|
type echoService struct{}
|
|
|
|
func (echoService) Name() string { return "echo.v1" }
|
|
func (echoService) Handle(_ context.Context, method string, payload []byte) ([]byte, error) {
|
|
if method != "ping" {
|
|
return nil, ErrNoService
|
|
}
|
|
return append([]byte("pong:"), payload...), nil
|
|
}
|
|
|
|
func TestEnvelopeRoundTrip(t *testing.T) {
|
|
in := Envelope{Service: "echo.v1", Method: "ping", Payload: []byte("hello")}
|
|
frame := EncodeEnvelope(in)
|
|
out, err := DecodeEnvelope(frame)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out.Service != in.Service || out.Method != in.Method || !bytes.Equal(out.Payload, in.Payload) {
|
|
t.Fatalf("roundtrip mismatch: %+v != %+v", out, in)
|
|
}
|
|
}
|
|
|
|
func TestHTTPHandler(t *testing.T) {
|
|
reg := NewRegistry()
|
|
reg.Register(echoService{})
|
|
|
|
app := fiber.New()
|
|
app.Post("/zap", HTTPHandler(reg))
|
|
|
|
frame := EncodeEnvelope(Envelope{Service: "echo.v1", Method: "ping", Payload: []byte("xyz")})
|
|
req := httptest.NewRequest("POST", "/zap", bytes.NewReader(frame))
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
if ct := resp.Header.Get("Content-Type"); ct != "application/zap" {
|
|
t.Fatalf("content-type = %q, want application/zap", ct)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
payload, err := DecodeResponse(body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(payload) != "pong:xyz" {
|
|
t.Fatalf("payload = %q, want pong:xyz", payload)
|
|
}
|
|
}
|
|
|
|
func TestHTTPHandler_UnknownService(t *testing.T) {
|
|
reg := NewRegistry()
|
|
app := fiber.New()
|
|
app.Post("/zap", HTTPHandler(reg))
|
|
|
|
frame := EncodeEnvelope(Envelope{Service: "nope.v1", Method: "ping", Payload: []byte("x")})
|
|
resp, err := app.Test(httptest.NewRequest("POST", "/zap", bytes.NewReader(frame)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 404 {
|
|
t.Fatalf("status = %d, want 404", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestHTTPHandler_EmptyBody(t *testing.T) {
|
|
reg := NewRegistry()
|
|
app := fiber.New()
|
|
app.Post("/zap", HTTPHandler(reg))
|
|
resp, err := app.Test(httptest.NewRequest("POST", "/zap", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.StatusCode != 400 {
|
|
t.Fatalf("status = %d, want 400", resp.StatusCode)
|
|
}
|
|
}
|