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.
72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
package zaprpc
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// HTTP header hints. The envelope in the body is canonical; these are a
|
|
// fast-path so a router/proxy can route without decoding the body. On
|
|
// disagreement the binary envelope wins (see decodeRequest).
|
|
const (
|
|
HeaderService = "X-ZAP-Service"
|
|
HeaderMethod = "X-ZAP-Method"
|
|
)
|
|
|
|
// HTTPHandler returns a fiber.Handler that serves the ZAP RPC plane over
|
|
// HTTP POST. The request body is a canonical ZAP envelope (service,
|
|
// method, payload); the handler decodes it, dispatches through reg, and
|
|
// writes a ZAP response envelope. Returns fiber.Handler (not zip.Handler)
|
|
// because zaprpc is imported by zip — depending back would cycle.
|
|
//
|
|
// Mount it on any zip/Fiber router:
|
|
//
|
|
// app.Fiber().Post("/zap", zaprpc.HTTPHandler(app.ZAPRegistry()))
|
|
func HTTPHandler(reg *Registry) fiber.Handler {
|
|
return func(c fiber.Ctx) error {
|
|
body := c.Body()
|
|
if len(body) == 0 {
|
|
return writeErr(c, fiber.StatusBadRequest, "empty ZAP body")
|
|
}
|
|
|
|
env, err := DecodeEnvelope(body)
|
|
if err != nil {
|
|
return writeErr(c, fiber.StatusBadRequest, err.Error())
|
|
}
|
|
|
|
// Headers are only a hint; the envelope is canonical. We accept
|
|
// the header values when the envelope omits them, but never let a
|
|
// header override a present envelope field.
|
|
if env.Service == "" {
|
|
env.Service = c.Get(HeaderService)
|
|
}
|
|
if env.Method == "" {
|
|
env.Method = c.Get(HeaderMethod)
|
|
}
|
|
if env.Service == "" || env.Method == "" {
|
|
return writeErr(c, fiber.StatusBadRequest, "missing service or method")
|
|
}
|
|
|
|
out, err := reg.Dispatch(c.Context(), env.Service, env.Method, env.Payload)
|
|
if err != nil {
|
|
if err == ErrNoService {
|
|
return writeErr(c, fiber.StatusNotFound, err.Error())
|
|
}
|
|
return writeErr(c, fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
c.Status(fiber.StatusOK)
|
|
c.Set(fiber.HeaderContentType, "application/zap")
|
|
c.Set(HeaderService, env.Service)
|
|
c.Set(HeaderMethod, env.Method)
|
|
return c.Send(EncodeResponse(out))
|
|
}
|
|
}
|
|
|
|
func writeErr(c fiber.Ctx, status int, msg string) error {
|
|
c.Status(status)
|
|
c.Set(fiber.HeaderContentType, "application/json")
|
|
return c.SendString(fmt.Sprintf(`{"error":%q,"status":%d}`, msg, status))
|
|
}
|