jsruntime

package
v0.3.0-beta.5 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 14 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 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

func NewAPIRouteRuntime

func NewAPIRouteRuntime(apiDir string) *APIRouteRuntime

NewAPIRouteRuntime creates a new API route runtime

func (*APIRouteRuntime) Execute

func (a *APIRouteRuntime) Execute(req APIRequest) APIResult

Execute runs the API route handler for the given path

type ISRCacheEntry

type ISRCacheEntry struct {
	HTML       string
	HeadHTML   string
	ScriptHTML string
	Timestamp  time.Time
}

ISRCacheEntry holds a cached ISR page render.

type ISRPageInfo

type ISRPageInfo struct {
	Route      string
	Revalidate int // seconds
}

ISRPageInfo describes an ISR page with its revalidation interval.

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

func (r *Runtime) DrainJobs()

DrainJobs executes pending promise jobs (microtasks). Call after Execute to resolve promises.

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

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

RegisterFunc registers a Go function as a global JS function

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

func (*Runtime) SetEnv

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

SetEnv sets environment variables for the runtime

func (*Runtime) SetGlobal

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

SetGlobal sets a global variable

func (*Runtime) VM

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

VM returns the underlying quickjs VM

type RuntimeComponentResult

type RuntimeComponentResult struct {
	HTML  string `json:"html"`
	Error string `json:"error,omitempty"`
}

RuntimeComponentResult is the output of a runtime component render.

type RuntimeComponentRuntime

type RuntimeComponentRuntime struct {
	// contains filtered or unexported fields
}

RuntimeComponentRuntime executes compiled runtime component bundles via embedded quickjs. Each component is a standalone IIFE that defines globalThis.__krate_render(propsJSON).

func NewRuntimeComponentRuntime

func NewRuntimeComponentRuntime(componentDir string) *RuntimeComponentRuntime

NewRuntimeComponentRuntime creates a runtime for executing compiled runtime components.

func (*RuntimeComponentRuntime) ClearCache

func (r *RuntimeComponentRuntime) ClearCache()

ClearCache removes all cached bundle code. Call during dev mode hot reload when a *.runtime.tsx file changes to force re-read from disk.

func (*RuntimeComponentRuntime) RenderComponent

func (r *RuntimeComponentRuntime) RenderComponent(componentName, propsJSON string) RuntimeComponentResult

RenderComponent executes a compiled runtime component bundle and returns the HTML. componentName is used to locate the <name>.runtime.js file in the component directory. propsJSON is a JSON string of props to pass to the component function.

func (*RuntimeComponentRuntime) RenderComponentAsync

func (r *RuntimeComponentRuntime) RenderComponentAsync(componentName, propsJSON string) RuntimeComponentResult

RenderComponentAsync is like RenderComponent but supports async components. It drains the microtask queue after execution to resolve any promises.

type SSRManifest

type SSRManifest struct {
	Pages             []SSRPageManifest `json:"pages"`
	Stylesheet        string            `json:"stylesheet,omitempty"`
	RuntimeJS         string            `json:"runtimeJS,omitempty"`
	RuntimeComponents []SSRPageManifest `json:"runtimeComponents,omitempty"`
}

SSRManifest is the on-disk manifest for embedded SSR.

type SSRPageManifest

type SSRPageManifest struct {
	Route      string `json:"route"`
	BundlePath string `json:"bundlePath"` // IIFE bundle relative to outDir
	Mode       string `json:"mode"`       // ssr, isr, streaming
	Revalidate int    `json:"revalidate,omitempty"`
}

SSRPageManifest describes a page that can be rendered at serve time.

type SSRPageRequest

type SSRPageRequest struct {
	Route   string            `json:"route"`
	URL     string            `json:"url"`
	Method  string            `json:"method"`
	Headers map[string]string `json:"headers"`
	Params  map[string]string `json:"params"`
	Query   map[string]string `json:"query"`
}

SSRPageRequest is the input for rendering a page via QuickJS.

type SSRPageResponse

type SSRPageResponse struct {
	HTML       string `json:"html"`
	HeadHTML   string `json:"headHTML,omitempty"`
	ScriptHTML string `json:"scriptHTML,omitempty"`
	Status     int    `json:"status"`
	Redirect   string `json:"redirect,omitempty"`
	NotFound   bool   `json:"notFound,omitempty"`
}

SSRPageResponse is the output from rendering a page via QuickJS.

type SSRRuntime

type SSRRuntime struct {
	// contains filtered or unexported fields
}

SSRRuntime renders pages via embedded QuickJS, replacing the Node.js sidecar.

func NewSSRRuntime

func NewSSRRuntime(root, outDir string) *SSRRuntime

NewSSRRuntime creates an embedded SSR runtime from the manifest.

func (*SSRRuntime) ClearBundleCache

func (s *SSRRuntime) ClearBundleCache()

ClearBundleCache removes all cached bundles. Call during dev mode hot reload.

func (*SSRRuntime) FindPageForRoute

func (s *SSRRuntime) FindPageForRoute(urlPath string) (*SSRPageManifest, map[string]string)

FindPageForRoute matches a URL path against route patterns and returns the page + params.

func (*SSRRuntime) GetISRPages

func (s *SSRRuntime) GetISRPages() []ISRPageInfo

GetISRPages returns all ISR pages and their revalidation intervals.

func (*SSRRuntime) GetRuntimeJS

func (s *SSRRuntime) GetRuntimeJS() string

GetRuntimeJS returns the shared runtime chunk path.

func (*SSRRuntime) GetStylesheet

func (s *SSRRuntime) GetStylesheet() string

GetStylesheet returns the global CSS filename.

func (*SSRRuntime) Invalidate

func (s *SSRRuntime) Invalidate(route string)

Invalidate removes the ISR cache for a route.

func (*SSRRuntime) IsISRPage

func (s *SSRRuntime) IsISRPage(route string) bool

IsISRPage checks if a route uses ISR.

func (*SSRRuntime) IsRunning

func (s *SSRRuntime) IsRunning() bool

IsRunning returns true if the manifest is loaded.

func (*SSRRuntime) IsSSRPage

func (s *SSRRuntime) IsSSRPage(route string) bool

IsSSRPage checks if a route requires server-side rendering.

func (*SSRRuntime) IsStreamingPage

func (s *SSRRuntime) IsStreamingPage(route string) bool

IsStreamingPage checks if a route uses streaming SSR.

func (*SSRRuntime) LoadManifest

func (s *SSRRuntime) LoadManifest() error

LoadManifest reads the SSR manifest from the output directory.

func (*SSRRuntime) RenderPage

func (s *SSRRuntime) RenderPage(req SSRPageRequest) (*SSRPageResponse, error)

RenderPage renders a page via QuickJS and returns the response.

Jump to

Keyboard shortcuts

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