Documentation
¶
Index ¶
- Constants
- func DefaultAPIWorkers() int
- func EvaluateExpr(code string) (string, error)
- func ToMap(v any) (map[string]any, bool)
- func ToNative(v any) any
- type APIRequest
- type APIResult
- type APIRouteRuntime
- type MiddlewareRequest
- type MiddlewareResult
- type MiddlewareRuntime
- type Runtime
- func (r *Runtime) Call(function string, args ...any) (any, error)
- func (r *Runtime) CallValue(function string, args ...any) (quickjs.Value, error)
- func (r *Runtime) Close() error
- func (r *Runtime) Compile(code string, flags int) ([]byte, error)
- func (r *Runtime) DrainJobs()
- func (r *Runtime) Error(args []any)
- func (r *Runtime) EvalBytecode(bytecode []byte) (any, error)
- func (r *Runtime) Execute(code string) (any, error)
- func (r *Runtime) ExecuteModule(code string) (any, error)
- func (r *Runtime) GetGlobal(name string) (any, error)
- func (r *Runtime) Log(args []any)
- func (r *Runtime) LogPrefix() string
- func (r *Runtime) RegisterFunc(name string, f func(args []any) (any, error)) error
- func (r *Runtime) RegisterFuncWithThis(name string, f func(this quickjs.Value, args []any) (any, error)) error
- func (r *Runtime) SetEnv(env map[string]string) error
- func (r *Runtime) SetGlobal(name string, value any) error
- func (r *Runtime) SetLogPrefix(prefix string)
- func (r *Runtime) VM() *quickjs.VM
- func (r *Runtime) Warn(args []any)
Constants ¶
const ( // EvalGlobal evaluates in the global scope EvalGlobal = quickjs.EvalGlobal // EvalModule evaluates as an ES module EvalModule = quickjs.EvalModule )
Variables ¶
This section is empty.
Functions ¶
func DefaultAPIWorkers ¶ added in v0.4.0
func DefaultAPIWorkers() int
DefaultAPIWorkers returns the default embedded API worker-pool size.
func EvaluateExpr ¶
EvaluateExpr evaluates a self-contained JS expression in a fresh QuickJS VM and returns its String() representation. Because QuickJS is a full ECMAScript engine, every built-in (Date, Math, String, Number, Array, Object, ...) is available - so SSR evaluation of globals like Date.now() produces a genuine JS-engine value rather than a Go approximation. Returns an error when the expression throws (e.g. it references an identifier that isn't defined).
Types ¶
type APIRequest ¶
type APIRequest struct {
URL string `json:"url"`
Method string `json:"method"`
Path string `json:"path"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
APIRequest is the input to an API route handler
type APIResult ¶
type APIResult struct {
Status int `json:"status"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Error string `json:"error"`
}
APIResult is the output of an API route handler
type APIRouteRuntime ¶
type APIRouteRuntime struct {
// contains filtered or unexported fields
}
APIRouteRuntime executes compiled API routes via embedded quickjs.
In the default (shared) mode it keeps a pool of long-lived VMs, each of which loads a route module once and reuses it, so a request costs one handler call rather than a fresh VM + full module re-evaluation. Module state persists per worker across requests, matching Node/Next semantics.
In isolation mode it creates a fresh VM per request (slower, but each request gets clean module state); this is also used in dev so file edits are picked up.
func NewAPIRouteRuntime ¶
func NewAPIRouteRuntime(apiDir string) *APIRouteRuntime
NewAPIRouteRuntime creates a new API route runtime
func (*APIRouteRuntime) Close ¶ added in v0.4.0
func (a *APIRouteRuntime) Close()
Close releases all idle workers.
func (*APIRouteRuntime) Execute ¶
func (a *APIRouteRuntime) Execute(req APIRequest) APIResult
Execute runs the API route handler for the given path.
func (*APIRouteRuntime) ResolveFile ¶ added in v0.4.0
func (a *APIRouteRuntime) ResolveFile(reqPath string) string
ResolveFile returns the out-dir-relative file name (slash-separated, e.g. "users/index.js") that would serve the given request path, or "" when none exists. The server uses it to decide whether a Node-only route must be forwarded to the sidecar.
func (*APIRouteRuntime) SetEnv ¶ added in v0.4.0
func (a *APIRouteRuntime) SetEnv(env map[string]string)
SetEnv injects environment variables into every worker created by the pool. Call before serving.
func (*APIRouteRuntime) SetPool ¶ added in v0.4.0
func (a *APIRouteRuntime) SetPool(workers int, isolation bool)
SetPool configures the pool size and whether each request runs in a fresh VM (isolation) instead of a shared, stateful worker. Call before serving.
type MiddlewareRequest ¶
type MiddlewareRequest struct {
URL string `json:"url"`
Method string `json:"method"`
Path string `json:"path"`
Headers map[string]string `json:"headers"`
}
MiddlewareRequest is the input to the middleware function
type MiddlewareResult ¶
type MiddlewareResult struct {
Action string `json:"action"`
Status int `json:"status"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Error string `json:"error"`
}
MiddlewareResult is the output of the middleware function
type MiddlewareRuntime ¶
type MiddlewareRuntime struct {
// contains filtered or unexported fields
}
MiddlewareRuntime executes middleware.ts via embedded quickjs
func LoadMiddlewareRuntime ¶
func LoadMiddlewareRuntime(root string) *MiddlewareRuntime
LoadMiddlewareRuntime loads the middleware.js and returns a runtime, or nil if no middleware
func NewMiddlewareRuntime ¶
func NewMiddlewareRuntime(middlewarePath string) (*MiddlewareRuntime, error)
NewMiddlewareRuntime creates a new middleware runtime and loads the middleware.js
func (*MiddlewareRuntime) Close ¶
func (m *MiddlewareRuntime) Close()
Close releases the runtime resources
func (*MiddlewareRuntime) Execute ¶
func (m *MiddlewareRuntime) Execute(req MiddlewareRequest) MiddlewareResult
Execute runs the middleware with the given request and returns the result
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime wraps a quickjs VM with krate-specific Web API polyfills
func (*Runtime) Compile ¶ added in v0.4.0
Compile compiles JavaScript to quickjs bytecode for later evaluation with EvalBytecode. Caching bytecode lets a long-lived VM skip re-parsing a script on every call.
func (*Runtime) DrainJobs ¶
func (r *Runtime) DrainJobs()
DrainJobs executes pending promise jobs (microtasks). Call after Execute to resolve promises.
func (*Runtime) EvalBytecode ¶ added in v0.4.0
EvalBytecode evaluates previously compiled quickjs bytecode.
func (*Runtime) ExecuteModule ¶
ExecuteModule evaluates JavaScript code as an ES module
func (*Runtime) RegisterFunc ¶
RegisterFunc registers a Go function as a global JS function. A panic in the host function is converted into a JS-catchable error rather than unwinding across the QuickJS FFI boundary.
func (*Runtime) RegisterFuncWithThis ¶
func (r *Runtime) RegisterFuncWithThis(name string, f func(this quickjs.Value, args []any) (any, error)) error
RegisterFuncWithThis registers a Go function that receives the this object (with the same panic containment as RegisterFunc).
func (*Runtime) SetEnv ¶
SetEnv sets environment variables for the runtime. Values are JSON-encoded so keys/values containing quotes or backslashes are safe.
func (*Runtime) SetLogPrefix ¶ added in v0.4.0
SetLogPrefix sets a prefix prepended to every console line. Plugin hosts set it so console.log/error/warn output is attributable to the plugin that ran.