Files
zeekay 6b47025279 feat(runtime): embed goja + esbuild; add ZAP-over-HTTP transport
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.
2026-06-16 13:07:04 -07:00

78 lines
2.2 KiB
Go

package zaprpc
import (
"fmt"
zap "github.com/zap-proto/go"
)
// The ZAP RPC envelope is the canonical on-the-wire framing for one RPC
// call carried over HTTP (or any byte transport). It is a ZAP Message
// whose root Object has a 24-byte fixed section with three fields:
//
// offset 0: service (text — 8 bytes: u32 rel-offset + u32 len)
// offset 8: method (text — 8 bytes)
// offset 16: payload (bytes — 8 bytes)
//
// Binary is canonical: the X-ZAP-Service / X-ZAP-Method HTTP headers are
// only a fast-path hint and the envelope wins on any disagreement.
const (
envServiceOff = 0
envMethodOff = 8
envPayloadOff = 16
envFixedSize = 24
)
// Envelope is the decoded form of a ZAP RPC frame.
type Envelope struct {
Service string
Method string
Payload []byte
}
// EncodeEnvelope builds the canonical ZAP RPC frame for e.
func EncodeEnvelope(e Envelope) []byte {
b := zap.NewBuilder(256 + len(e.Payload))
ob := b.StartObject(envFixedSize)
ob.SetText(envServiceOff, e.Service)
ob.SetText(envMethodOff, e.Method)
ob.SetBytes(envPayloadOff, e.Payload)
ob.FinishAsRoot()
return b.Finish()
}
// DecodeEnvelope parses a ZAP RPC frame. Returns an error if the bytes
// are not a valid ZAP message.
func DecodeEnvelope(data []byte) (Envelope, error) {
msg, err := zap.Parse(data)
if err != nil {
return Envelope{}, fmt.Errorf("zaprpc: decode envelope: %w", err)
}
root := msg.Root()
if root.IsNull() {
return Envelope{}, fmt.Errorf("zaprpc: decode envelope: null root")
}
return Envelope{
Service: root.Text(envServiceOff),
Method: root.Text(envMethodOff),
Payload: root.Bytes(envPayloadOff),
}, nil
}
// EncodeResponse wraps a method's response payload in a ZAP frame. The
// response uses the same envelope shape with empty service/method so a
// single decoder handles both directions; callers that only need the
// payload can use DecodeResponse.
func EncodeResponse(payload []byte) []byte {
return EncodeEnvelope(Envelope{Payload: payload})
}
// DecodeResponse extracts the payload from a response frame.
func DecodeResponse(data []byte) ([]byte, error) {
e, err := DecodeEnvelope(data)
if err != nil {
return nil, err
}
return e.Payload, nil
}