mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
HIP-0106's plugin contract, in the framework rather than in 116 repositories. Declaration projects the LIVE ROUTER — every method+pattern, complete by construction, so a route a plugin serves and does not publish is still declared. That is the analytics-ingestion outage class: the published paths were listed, the four ingestion doors were not, and every product beacon in the fleet answered 405. Declaration/Route/(*App).Declaration what a host discovers /.well-known/zip/plugin.json the same value off a running child (*App).Described `<bin> openapi <f>` / `<bin> declare <f>` Tenant the tenancy read, with the real rule Delegate a continuation for another org Ask dial a peer by name, invoke one op (*App).Peer peer ops on their own app, own socket (*App).Ops /healthz + /readyz + /metrics Config.Eager the one fact a router cannot show App.claim a second claim of a prefix is an ERROR zip/runtime -> zip/js a language runtime is an adaptor Four things the reference plugin falsified while being built: - Describe was already taken. zip.Describe(methodPath, Doc) is the doc registry generated code calls. The projection writer is (*App).Described, one call, because argv is read in one place and a main cannot forget to forward it. - zip's control plane is NOT all under /.well-known/. /docs and the MCP path are not, so a prefix filter let a child declare them — paths outside /v1/ that every plugin serves and whose first claimant takes the HOST's document and agent door. App.control now registers and records in one call, so leaking one is impossible rather than unlikely. - OPS_PORT appeared in no Go file in the fleet. HIP-0119 named the paths and nobody implemented them, so conformance meant 116 repos hand-rolling three routes and a listener. It is one sibling app now, brought up by Listen. - There is deliberately NO default ops port. A default made every second app in a process die on "address already in use" — a plugin has two apps in it and a test process has many. The deployment states OPS_PORT; a host states none on a child, so a child correctly owns none. Load's duplicate claim used to return nil while the first registration kept answering: the losing plugin's whole surface served another plugin's 404 and nothing said so. It is now an error naming both claimants, all-or-nothing across a plugin's prefixes, taken before anything is spawned. a.ops was the op registry and is now a.registry — the ONE registry the OpenAPI document, the MCP tool, the CLI command and the plane op are all projected from. a.ops is the ops sibling. Co-authored-by: Hanzo Dev <[email protected]>
121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package js
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/dop251/goja"
|
|
)
|
|
|
|
// CommonJS-style module loading without an external dependency. Each VM
|
|
// gets a require(name) global backed by a per-VM registry of module
|
|
// sources and a memoized cache of evaluated module.exports. A module is
|
|
// evaluated the first time it is required, wrapped in the canonical
|
|
// CommonJS function scope:
|
|
//
|
|
// (function (module, exports, require) { <src> })
|
|
//
|
|
// so module source can do `module.exports = ...` or `exports.foo = ...`.
|
|
|
|
const requireRegistryKey = "__zip_modules__"
|
|
const requireCacheKey = "__zip_module_cache__"
|
|
|
|
// vmModules holds a VM's module sources and evaluated-exports cache. It
|
|
// lives in a Go-side side-table keyed by the *goja.Runtime pointer so it
|
|
// is never visible to (or mutable from) JS.
|
|
type vmModules struct {
|
|
mu sync.Mutex
|
|
sources map[string]string
|
|
cache map[string]goja.Value
|
|
}
|
|
|
|
var (
|
|
moduleTablesMu sync.Mutex
|
|
moduleTables = map[*goja.Runtime]*vmModules{}
|
|
)
|
|
|
|
func tableFor(vm *goja.Runtime) *vmModules {
|
|
moduleTablesMu.Lock()
|
|
defer moduleTablesMu.Unlock()
|
|
t, ok := moduleTables[vm]
|
|
if !ok {
|
|
t = &vmModules{sources: map[string]string{}, cache: map[string]goja.Value{}}
|
|
moduleTables[vm] = t
|
|
}
|
|
return t
|
|
}
|
|
|
|
// installRequire registers the require() global on vm and seeds it with
|
|
// the given module sources.
|
|
func installRequire(vm *goja.Runtime, modules map[string]string) error {
|
|
t := tableFor(vm)
|
|
t.mu.Lock()
|
|
for name, src := range modules {
|
|
t.sources[name] = src
|
|
}
|
|
t.mu.Unlock()
|
|
|
|
return vm.Set("require", func(name string) (goja.Value, error) {
|
|
return requireModule(vm, name)
|
|
})
|
|
}
|
|
|
|
// registerModule adds (or replaces) one module source on vm and
|
|
// invalidates any cached evaluation of it.
|
|
func registerModule(vm *goja.Runtime, name, src string) error {
|
|
t := tableFor(vm)
|
|
t.mu.Lock()
|
|
t.sources[name] = src
|
|
delete(t.cache, name)
|
|
t.mu.Unlock()
|
|
// Ensure require() exists even if the VM was built with no modules.
|
|
if v := vm.Get("require"); v == nil || goja.IsUndefined(v) {
|
|
return vm.Set("require", func(name string) (goja.Value, error) {
|
|
return requireModule(vm, name)
|
|
})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// requireModule resolves name to its module.exports, evaluating the
|
|
// source on first use and memoizing the result.
|
|
func requireModule(vm *goja.Runtime, name string) (goja.Value, error) {
|
|
t := tableFor(vm)
|
|
t.mu.Lock()
|
|
if cached, ok := t.cache[name]; ok {
|
|
t.mu.Unlock()
|
|
return cached, nil
|
|
}
|
|
src, ok := t.sources[name]
|
|
t.mu.Unlock()
|
|
if !ok {
|
|
return nil, fmt.Errorf("zip/js: module not found: %q", name)
|
|
}
|
|
|
|
// Wrap in the CommonJS scope and evaluate.
|
|
wrapped := "(function(module, exports, require){" + src + "\n})"
|
|
fnVal, err := vm.RunString(wrapped)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("zip/js: load module %q: %w", name, err)
|
|
}
|
|
fn, ok := goja.AssertFunction(fnVal)
|
|
if !ok {
|
|
return nil, fmt.Errorf("zip/js: module %q did not compile to a function", name)
|
|
}
|
|
|
|
module := vm.NewObject()
|
|
exports := vm.NewObject()
|
|
_ = module.Set("exports", exports)
|
|
requireFn := vm.Get("require")
|
|
|
|
if _, err := fn(goja.Undefined(), module, exports, requireFn); err != nil {
|
|
return nil, fmt.Errorf("zip/js: eval module %q: %w", name, err)
|
|
}
|
|
|
|
result := module.Get("exports")
|
|
t.mu.Lock()
|
|
t.cache[name] = result
|
|
t.mu.Unlock()
|
|
return result, nil
|
|
}
|