jsruntime

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
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

func EvaluateExpr(code string) (string, error)

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).

func ToMap

func ToMap(v any) (map[string]any, bool)

ToMap converts a quickjs object result to a Go map.

func ToNative

func ToNative(v any) any

ToNative converts a quickjs result to a native Go type. JS objects are JSON-deserialized; primitives pass through.

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

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 New

func New() (*Runtime, error)

New creates a new JS runtime with Web API polyfills

func (*Runtime) Call

func (r *Runtime) Call(function string, args ...any) (any, error)

Call calls a global function with the given arguments

func (*Runtime) CallValue

func (r *Runtime) CallValue(function string, args ...any) (quickjs.Value, error)

CallValue calls a global function and returns a Value

func (*Runtime) Close

func (r *Runtime) Close() error

Close releases all resources

func (*Runtime) Compile added in v0.4.0

func (r *Runtime) Compile(code string, flags int) ([]byte, error)

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) Error added in v0.4.0

func (r *Runtime) Error(args []any)

Error writes an error line to stderr.

func (*Runtime) EvalBytecode added in v0.4.0

func (r *Runtime) EvalBytecode(bytecode []byte) (any, error)

EvalBytecode evaluates previously compiled quickjs bytecode.

func (*Runtime) Execute

func (r *Runtime) Execute(code string) (any, error)

Execute evaluates JavaScript code and returns the result

func (*Runtime) ExecuteModule

func (r *Runtime) ExecuteModule(code string) (any, error)

ExecuteModule evaluates JavaScript code as an ES module

func (*Runtime) GetGlobal

func (r *Runtime) GetGlobal(name string) (any, error)

GetGlobal gets a global variable

func (*Runtime) Log added in v0.4.0

func (r *Runtime) Log(args []any)

Log writes a console-style line to stdout.

func (*Runtime) LogPrefix added in v0.4.0

func (r *Runtime) LogPrefix() string

LogPrefix returns the current console prefix.

func (*Runtime) RegisterFunc

func (r *Runtime) RegisterFunc(name string, f func(args []any) (any, error)) error

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

func (r *Runtime) SetEnv(env map[string]string) error

SetEnv sets environment variables for the runtime. Values are JSON-encoded so keys/values containing quotes or backslashes are safe.

func (*Runtime) SetGlobal

func (r *Runtime) SetGlobal(name string, value any) error

SetGlobal sets a global variable

func (*Runtime) SetLogPrefix added in v0.4.0

func (r *Runtime) SetLogPrefix(prefix string)

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.

func (*Runtime) VM

func (r *Runtime) VM() *quickjs.VM

VM returns the underlying quickjs VM

func (*Runtime) Warn added in v0.4.0

func (r *Runtime) Warn(args []any)

Warn writes a warning line to stderr.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL