tsx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 46 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tsx runs TSX apps: bundles of what tsx/bundler emits, on the goja JavaScript engine, or the page's own on the web (internal/js), rendered by core as Go apps are. It is a port of NSH's nsh-js: reactive.go of reactive.rs, dom.go of dom.rs, protocol.go of protocol.rs and modules.go of modules.rs, so a change there ports here (docs/architecture/tsx.md).

A bundle is a script: imports of NSGo's modules (nsgo, nsgo/base, nsgo:rt) are calls of require.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{Load: 10 * time.Second, Call: 2 * time.Second, Stack: 1000, Harden: true,
	Nodes: 200_000, Timers: 10_000, Requests: 32, Body: 64 << 20, Storage: 1 << 20}

DefaultLimits are NSH's: 10 seconds to load, 2 per call, 1000 frames, hardened, 200 000 nodes; and 10 000 timers, 32 requests, 64 MiB bodies and 1 MiB of storage.

View Source
var ErrBudget = errors.New("tsx: the script ran past its time budget")

ErrBudget is what a call cut at its time budget fails with.

View Source
var ErrNoExport = errors.New("tsx: the plugin exports no such function")

ErrNoExport is the failure of a call of a name the plugin does not export as a function.

View Source
var ErrUnisolated = errors.New("tsx: the web cannot isolate a sandboxed package from the page, so its capabilities would not hold; set Sandbox.WebUnisolated to run it with the page's authority")

ErrUnisolated is what a sandbox with Capabilities or Grant fails with on the web unless Sandbox.WebUnisolated is set: the page's engine cannot isolate a package's realm from the page, so what it declares would not hold.

View Source
var SandboxModules = []string{"nsgo", "nsgo:rt", "nsgo/base", "nsgo/base/plot", "nsgo/ui", "nsgo/ui/chat", "nsgo/mobile"}

SandboxModules are the modules a sandboxed bundle imports without Sandbox.Modules naming them: NSGo's own, with what is registered into them SandboxSafe, none of the Go functions registered with RegisterFunctions, and none of their exports that act on the whole app (setColorScheme, BackHandler.exitApp).

Functions

func App

func App(name, src string) func(cx *core.Scope) core.Node

App returns the app of a bundle, for nsgo.Run and nsgotest.Start: it runs the bundle, and what the bundle mounts is the app. name names the bundle in stacks.

func Convert

func Convert[T any](name, decl string, read func(any) T, write func(T) any)

Convert gives the values of T a JS form that need not be an object, as a date is a string: read makes one from what JS gives (exported, its numbers float64 or int64), and write, when set, the value JS gets. decl, a TypeScript declaration of name, declares it.

func Declarations

func Declarations() map[string]string

Declarations returns the TypeScript declarations of the modules TSX imports, by path under .nsgo/: types/nsgo/*.d.ts for NSGo's, a types/<module>.d.ts for each module registered (tsx.Register), and tsconfig.json, which an app's tsconfig.json extends.

func Fields

func Fields[T any](names map[string]string)

Fields names fields of the data struct T in JS by names, NSH's where its fields' names differ: a bool NoX field named x is negated, true unless set ({"NoResize": "resizable"}).

func Hot

func Hot(name, src string) (app func(cx *core.Scope) core.Node, update func(src string))

Hot returns the app of a hot bundle (bundler.CompileHot), as App does, and what runs a newer one in the same VM: the modules whose code changed run again and their components' instances render again, while the rest of the app keeps its state. update runs on the engine goroutine, as from Scope.Post.

func Optional

func Optional[T any](fields ...string)

Optional declares the named fields of T optional, an object may leave them out, its zero value read in their place.

func Register

func Register(module string, components map[string]core.Node, opts ...Option)

Register makes Go components importable from module in TSX: each value of components is a component's zero value, a struct implementing core.Node, exported under its key. Sandboxed bundles import them only with SandboxSafe or as Sandbox.Modules names them. Register them in an init function: it panics once an app has started, and into NSGo's own modules (nsgo/...) from packages outside NSGo.

func RegisterClass

func RegisterClass[T any](module, name string, c Class[T], opts ...Option)

RegisterClass makes *T the class name, importable from module in TSX: its exported methods are the class's methods, under their names in lower camel case, but for its getters and their setters, and Close, which dispose() calls. JS holds a *T for each instance, the same object while it lives, and dispose() lets it go, after which the object throws. A member's or function's parameter of type *T takes an instance and a result of type *T gives one. Sandboxed bundles import it, with its statics, only with SandboxSafe, the one option it takes, or as Sandbox.Modules names it. Register classes as Register does components.

func RegisterFunctions

func RegisterFunctions(module string, funcs map[string]any)

RegisterFunctions makes Go functions importable from module in TSX, and nativeModule(module) gives them too, as an embedding app's UI set exports its functions: each value of funcs is a func, exported under its key. Sandboxed bundles get them only as Sandbox.Modules says. JS's arguments convert to its parameters and its result back, structs as objects with lower-camel keys; a non-nil error it returns last throws. A func taking a context.Context first is async: it runs on a goroutine and JS gets a promise (classes.go). Register them as Register does components.

func RegisterMessages

func RegisterMessages[M any](module string, in func(locale string) M, params map[string][]string)

RegisterMessages makes a package of catalogs' messages importable from module in TSX, as typed functions: in is the package's In, and params its Parameters, which name each message's parameters in the declarations. The module exports messages, whose methods say each message in the language the app shows and follow it as it changes, messagesIn(locale) for a language of the caller's choice, and language(), the language the app shows. Sandboxed bundles get them only as Sandbox.Modules says. Register them as Register does components:

tsx.RegisterMessages("app/locales", locales.In, locales.Parameters)

func RegisterStatics

func RegisterStatics(module, component string, funcs map[string]any, opts ...Option)

RegisterStatics gives component, which Register made importable from module, static functions, as NSH's TextView.parse: each value of funcs is a func, called with JS's arguments read as its parameters' types and its results given back as the component's props and events cross. Sandboxed bundles get them only with SandboxSafe, the one option they take, or as Sandbox.Modules names them ("nsgo/base.TextView.parse"), and only on a component they import. Register them as Register does components.

func Required

func Required[T any](fields ...string)

Required declares the named fields of T required, which TypeScript asks an object to give: by default numbers and structs are, and every field of an event or a request Go hands JS (a type named …Event or …Request) but pointers.

func Sandboxed

func Sandboxed(name, src string, s Sandbox) func(cx *core.Scope) core.Node

Sandboxed returns the app of a bundle, as App does, run within s, in a view of its own: its frame, which fills the room its parent gives it, no more, so the bundle cannot grow over the host's views, and clips what the bundle shows, overlays included (core.Scope.Confine). In a scroll view that room is the scroll view's height: a host that wants the bundle as tall as its content puts it in a view of its own. On the web it panics with ErrUnisolated when s has Capabilities or Grant, unless s.WebUnisolated.

func Shape

func Shape[T any](name, decl string, encode func(T) map[string]any, decode func(map[string]any) T)

Shape gives the values of T a JS shape of their own, NSH's where T's fields differ from it: encode makes the object JS gets, its values crossing as any Go value does, decode, when set, reads one JS gives (exported, its numbers float64), and decl, a TypeScript declaration of name, declares it.

func SplitApp

func SplitApp(name, src string, chunk func(key string) (string, error)) func(cx *core.Scope) core.Node

SplitApp returns the app of a split bundle (bundler.CompileSplit), as App does: an import() of one of its chunks calls chunk, on a goroutine of its own, for the chunk's script by its key, and runs it.

func TokenDeclarations

func TokenDeclarations(t *core.Theme) string

TokenDeclarations declares the names token() reads in theme t, for .nsgo/types/nsgo/tokens.d.ts: its colors, as strings, and its tokens, lengths as numbers. The nsgo tool writes it from the app's theme.

func WithFetcher

func WithFetcher(app func(cx *core.Scope) core.Node, f Fetcher) func(cx *core.Scope) core.Node

WithFetcher returns app, its fetches answered by f.

func WriteDeclarations

func WriteDeclarations(dir string) error

WriteDeclarations writes Declarations under dir, an app's .nsgo.

Types

type Class

type Class[T any] struct {
	// New, a func returning a *T and optionally an error, is the
	// constructor. Without it JS gets instances only from members and
	// functions that return them.
	New any
	// Getters are methods of *T without parameters read as properties,
	// in Go's names; a method SetX beside the getter X sets X.
	Getters []string
	// Statics are functions on the class, by their JS names.
	Statics map[string]any
}

Class says how RegisterClass shows a Go type T to TSX.

type Fetcher

type Fetcher func(req *http.Request) (*http.Response, error)

A Fetcher answers an app's http and https requests in place of the network: a test's stand-in network.

type JSError

type JSError struct{ Name, Message string }

JSError is an error JS throws as the built-in error Name names, a TypeError or a RangeError, rather than an Error.

func (*JSError) Error

func (e *JSError) Error() string

type Limits

type Limits struct {
	// Load is how long the bundle's script may run as it loads, and Call
	// how long each call into JS may run (a handler, an effect, a
	// render): past it, the call is cut with ErrBudget.
	Load, Call time.Duration
	// Stack is how deep JS calls may nest.
	Stack int
	// Harden deletes eval, has the Function constructors throw and
	// freezes the built-in prototypes, so code cannot be made from
	// strings or the shared objects changed.
	Harden bool
	// Nodes is how many elements the bundle may hold at once, each of
	// which may hold a native view; past it the bundle stops.
	Nodes int
	// Timers is how many timers and frame callbacks may wait at once, and
	// Requests how many fetches may be under way: one more fails.
	Timers, Requests int
	// Body is the most bytes a request's or a response's body may hold.
	Body int
	// Storage is the most bytes its partition of the app's storage may
	// hold (Sandbox.Storage).
	Storage int
}

Limits are what a bundle may use. Zero fields leave that unlimited.

func ParseLimits

func ParseLimits(spec string) (Limits, error)

ParseLimits reads limits as nsgo dev -limits writes them: DefaultLimits with the fields spec names set, as "nodes=1000,call=500ms". Its keys are load, call, stack, nodes, timers, requests, body and storage; "default" or "" is DefaultLimits.

type Option

type Option func(*faceSpec)

Option adjusts how a Go component's props are named, or, SandboxSafe, who may import it.

func Default added in v0.2.0

func Default(field string, value any) Option

Default gives field the value it has while its TSX prop is absent or undefined, for a field whose default is not its type's zero. For a getter, or an XFunc twin named X or XFunc, value is what the getter returns while the prop reads undefined, so a reactive prop goes back to the default in place.

func DockOptions

func DockOptions() []Option

typed declares the types field's custom declarations name. DockOptions are DockArea's props, for a package that registers a form of it.

func Enum

func Enum[T ~int | ~uint8 | ~uint16 | ~int32 | ~uint32](names map[string]T) Option

Enum names the values of an integer type for the props of its type, which read and report them by name.

func Event

func Event(field string, names ...string) Option

Event names the fields of the one object event field passes its handler, a name for each of its arguments, as NSH's #[args] does: Event("OnOpenChange", "open", "reason"). An event of no name given passes the event struct it has as it is, and a bare value as { value }; Bounds, what every OnLayout has, as { bounds }.

func Flag

func Flag(field, prop, on, choices string) Option

Flag sets the bool field from prop, a choice of names: true when it is on. It is declared as choices, a TypeScript union of the names: Flag("Unified", "layout", "unified", `"split" | "unified"`).

func Omit

func Omit(fields ...string) Option

Omit leaves fields out of a Go component's TSX face: what JS has no use for, as a Go function of Go values.

func Preset

func Preset(field string, value any) Option

Preset sets field to value in every element of the component and leaves it out of its TSX face, as a Go function a face gives a TSX component: Preset("StyleOf", typography).

func Rename

func Rename(field, prop string) Option

Rename names field's prop prop.

func SandboxSafe

func SandboxSafe() Option

SandboxSafe gives what Register, RegisterClass, RegisterStatics or a part registers to the bundles a Sandbox runs, such as plugins, in the modules they import: only what reaches nothing past the sandbox's capabilities and acts on nothing outside the view that shows it. What is registered without it they import only as Sandbox.Modules names it, as a terminal, which starts programs, or a scroll bar theme, which restyles the whole window.

func Union

func Union[T any](members map[string]any) Option

Union names the types implementing interface type T for the props that hold one: a JS object's type tag picks its member, and a member given to JS carries its tag.

type Plugin

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

Plugin is a package that gives the Go app functions rather than a view: what its entry module exports, which Call calls. It runs sandboxed as View does, under the scope that loaded it, until Close or that scope's end.

// search.tsx
export async function search(query: string) { ... }

plugin, err := tsx.Load(cx, p, tsx.Sandbox{})
plugin.Call("search", []any{"query"}, func(result any, err error) { ... })

func Load

func Load(cx *core.Scope, p *pkg.Package, s Sandbox) (plugin *Plugin, err error)

Load runs package p as a plugin under cx, within s as View runs it, and returns it once its entry module ran, or a nil Plugin and what that threw, which goes to no Catch above cx and not to the app's report, or ErrUnisolated on the web as View panics with it. Call it on the engine's goroutine.

func (*Plugin) Call

func (p *Plugin) Call(name string, args []any, done func(result any, err error))

Call calls the plugin's export name with args, values JSON encodes, and has done hear what it returns, or what its promise resolves with, as JSON decodes it, on the engine's goroutine; err is what it threw or rejected with, ErrNoExport, or why the plugin stopped. Call it on the engine's goroutine.

func (*Plugin) Close

func (p *Plugin) Close()

Close stops the plugin and frees what it holds.

func (*Plugin) Exports

func (p *Plugin) Exports() []string

Exports are the names of the functions the plugin exports.

type Sandbox

type Sandbox struct {
	// Limits are what it may use, DefaultLimits when nil.
	Limits *Limits
	// Capabilities are what its code may reach (a package's own); nil
	// reaches what the app's code does. Grant, when set, narrows them to
	// what it also allows: what the host gives this view.
	Capabilities, Grant *pkg.Capabilities
	// Modules are what it may import beyond SandboxModules and what is registered SandboxSafe into them: a module ("myapp"), one export ("nsgo/base.Terminal", "nsgo.setColorScheme") or static or member ("nsgo/base.Scrollbar.setTheme", "nsgo/mobile.BackHandler.exitApp"), a native component ("native:MapView"), or "*" for everything.
	Modules []string
	// Host is the channel its host module reaches, for the Go app to
	// answer its calls and send it events; nil gives it none.
	Host *core.Host
	// Storage names its partition of the app's storage and of its secrets (core.Scope.ProvideSecrets), which no one else's code sees; "" leaves it the app's.
	Storage string
	// WebUnisolated runs it on the web with the page's authority. The page's engine runs it in a realm of the page's origin, which reaches the page's own APIs (XMLHttpRequest, WebSocket, navigator, localStorage, the page's window) past Go, so there Capabilities and Grant hold only the requests the runtime and core make, and Storage's partition and a pkg.Trust kept in the page's storage are open to its code. Without it, a sandbox with Capabilities or Grant refuses to start on the web with ErrUnisolated. Other targets isolate it and ignore this.
	WebUnisolated bool
}

Sandbox is what a bundle runs within: its limits, what it may reach and import, the host it talks to and the storage it keeps.

type View

type View struct {
	Package *pkg.Package
	// Sandbox's Capabilities are the package's and its Storage its app
	// ID when unset.
	Sandbox
}

View runs the app of a package a pkg.Trust verified where it is placed in a Go app, sandboxed: the app's own screens delivered anew, or a plugin. The package's capabilities hold it, narrowed by Grant; it keeps a partition of the app's storage of its app ID's, and its host module reaches Host alone. A package that throws as it loads, or stops at a quota, panics, as any component does; place it under a core.Catch to show something else. The web cannot isolate a package from the page, so there it panics with ErrUnisolated unless Sandbox.WebUnisolated.

p, err := trust.Verify(data) // off the engine's goroutine
...
core.Catch{Try: tsx.View{Package: p}.Render, Fallback: failed}

func (View) Render

func (v View) Render(cx *core.Scope) core.Node

Render implements core.Node.

Directories

Path Synopsis
Package bundler compiles TSX apps into the scripts the tsx package runs, as NSH's bundler does (nsh-bundler): esbuild strips TypeScript, lowers syntax goja lacks and bundles the app's modules into one script, and jsx.go compiles the JSX into calls of the renderer, as NSH's compiler does.
Package bundler compiles TSX apps into the scripts the tsx package runs, as NSH's bundler does (nsh-bundler): esbuild strips TypeScript, lowers syntax goja lacks and bundles the app's modules into one script, and jsx.go compiles the JSX into calls of the renderer, as NSH's compiler does.
Package compiler runs TSX apps: App runs the bundle the nsgo tool compiled ahead (package bundler), or under go run and go test compiles the app's sources as it starts, and under nsgo dev follows the dev server's new bundles.
Package compiler runs TSX apps: App runs the bundle the nsgo tool compiled ahead (package bundler), or under go run and go test compiles the app's sources as it starts, and under nsgo dev follows the dev server's new bundles.
Package diff gives TSX nsgo/base's DiffView, the changes between texts, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package diff gives TSX nsgo/base's DiffView, the changes between texts, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package editor gives TSX nsgo/base's Editor, a code editor, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package editor gives TSX nsgo/base's Editor, a code editor, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
internal
js
Package js is the JavaScript engine package tsx runs apps on: goja on every target but the web, where the page's own engine runs them (browser.go).
Package js is the JavaScript engine package tsx runs apps on: goja on every target but the web, where the page's own engine runs them (browser.go).
Package pkg signs TSX apps into packages and verifies them, for code an app downloads: its own screens updated, or plugins.
Package pkg signs TSX apps into packages and verifies them, for code an app downloads: its own screens updated, or plugins.
Package richeditor gives TSX nsgo/base's RichEditor and its sessions, which link packages other apps should not: an app whose TSX uses them imports this package for its effect.
Package richeditor gives TSX nsgo/base's RichEditor and its sessions, which link packages other apps should not: an app whose TSX uses them imports this package for its effect.
Package terminal gives TSX nsgo/base's Terminal, base/terminal's View over a session of its own, which links libghostty-vt's binding: an app whose TSX uses it imports this package for its effect.
Package terminal gives TSX nsgo/base's Terminal, base/terminal's View over a session of its own, which links libghostty-vt's binding: an app whose TSX uses it imports this package for its effect.
Package textview gives TSX nsgo/base's TextView, Markdown and HTML documents, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package textview gives TSX nsgo/base's TextView, Markdown and HTML documents, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package tsxtest runs a TSX app's tests, its *.test.tsx files, as Go tests (NSH's nsh/test): each file imports nsgo/test, whose open starts the app headless, or on a platform under nsgo test -target, through nsgotest, and whose App acts as a user does.
Package tsxtest runs a TSX app's tests, its *.test.tsx files, as Go tests (NSH's nsh/test): each file imports nsgo/test, whose open starts the app headless, or on a platform under nsgo test -target, through nsgotest, and whose App acts as a user does.
ui
Package ui gives TSX nsgo/ui, package ui's components as Go components, for an app that imports this package for its effect; a theme of its own needs ui's roles, as core.DefaultTheme and ui.WarmDuskTheme have.
Package ui gives TSX nsgo/ui, package ui's components as Go components, for an app that imports this package for its effect; a theme of its own needs ui's roles, as core.DefaultTheme and ui.WarmDuskTheme have.
chat
Package chat gives TSX nsgo/ui/chat, ui/chat's conversation components, for an app that imports this package for its effect beside tsx/ui.
Package chat gives TSX nsgo/ui/chat, ui/chat's conversation components, for an app that imports this package for its effect beside tsx/ui.
richeditor
Package richeditor gives TSX nsgo/ui's RichEditor, ui/richeditor's View over a RichEditorSession, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package richeditor gives TSX nsgo/ui's RichEditor, ui/richeditor's View over a RichEditorSession, which links packages other apps should not: an app whose TSX uses it imports this package for its effect.
Package websrc holds the scripts a page's realm runs: the realm's helpers and the sources of the runtime's programs.
Package websrc holds the scripts a page's realm runs: the realm's helpers and the sources of the runtime's programs.

Jump to

Keyboard shortcuts

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