js

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 4 Imported by: 0

README

tinywasm/js

Typed layer for Service Workers and Web Workers in TinyWASM.

Write Go. The framework generates the JS shim.

Overview

tinywasm/js is the only JS API in the TinyWASM framework — mirroring what tinywasm/css does for stylesheets. SSR modules call typed constructors returning *Script values with final JS content; assetmin writes them to disk without additional coordination.

API

Script (v1 escape hatch)

The core Script type is available for arbitrary JS snippets:

type Script struct {
    Name    string // Empty → bundled into /script.js. Non-empty → standalone /public/<Name>.
    Content string // Raw JavaScript.
}
Runtime configuration (call once at boot)
js.SetRuntime(js.RuntimeGo)    // Standard Go compiler
js.SetRuntime(js.RuntimeTinyGo) // TinyGo compiler (smaller binaries)

tinywasm/app calls this automatically when it detects the compiler mode — user modules never call it directly.

// Bundles wasm_exec.js + WASM bootstrap into /script.js.
js.PageBootstrap() *Script

// Generates /sw.js with wasm_exec.js + SW event listeners inlined.
js.ServiceWorker(handler ServiceWorkerHandler) *Script

// Generates /<name> with wasm_exec.js + message listener inlined.
js.WebWorker(name string, handler WebWorkerHandler) *Script
Handler interfaces

Implement in Go — the shim bridges browser events to your methods:

import (
    "github.com/tinywasm/context" // stdlib context is vetoed in WASM
    "github.com/tinywasm/fetch"
)

type ServiceWorkerHandler interface {
    OnInstall(ctx context.Context) error
    OnActivate(ctx context.Context) error
    OnFetch(ctx context.Context, req *js.Request) (*fetch.Response, error)
}

type WebWorkerHandler interface {
    OnMessage(ctx context.Context, msg *js.Message) (*js.Message, error)
}
Event types
// Request is the inbound FetchEvent intercepted by the SW.
type Request struct {
    URL     string
    Method  string
    Headers []fetch.Header // {Key, Value string} — no maps
    Body    []byte
}

// Message is the postMessage payload for Web Workers.
type Message struct {
    Data []byte
}

Service Worker example (PWA)

package mymodule

import (
    "github.com/tinywasm/context"
    "github.com/tinywasm/fetch"
    "github.com/tinywasm/js"
)

type CachingSW struct{}

func (sw *CachingSW) OnInstall(ctx context.Context) error  { return nil }
func (sw *CachingSW) OnActivate(ctx context.Context) error { return nil }

func (sw *CachingSW) OnFetch(ctx context.Context, req *js.Request) (*fetch.Response, error) {
    // Only intercept API routes; fall through for statics.
    body := []byte(`{"cached": true}`)
    return fetch.NewResponse(200,
        []fetch.Header{{Key: "Content-Type", Value: "application/json"}},
        body,
    ), nil
}

func (m Module) RenderJS() []*js.Script {
    return []*js.Script{
        js.PageBootstrap(),          // wasm_exec + bootstrap → bundled in /script.js
        js.ServiceWorker(&CachingSW{}), // full shim → /sw.js
    }
}

Web Worker example

type ParserWorker struct{}

func (w *ParserWorker) OnMessage(ctx context.Context, msg *js.Message) (*js.Message, error) {
    // Process msg.Data ([]byte) and return result.
    result := process(msg.Data)
    return &js.Message{Data: result}, nil
}

func (m Module) RenderJS() []*js.Script {
    return []*js.Script{
        js.WebWorker("parser.worker.js", &ParserWorker{}),
    }
}

Updating wasm_exec.js

When TinyGo or Go releases a new version:

  1. Update DefaultVersion in tinywasm/tinygo/tinygo.go (for TinyGo). For Go, update the go directive in js/go.mod.
  2. Run: go generate ./...
  3. Run: go test ./... (first run updates assets if generate was skipped; second run must pass)
  4. Publish: gopush (tinywasm/tinygo first, then tinywasm/js)

Escape hatch

For raw JS snippets (analytics, polyfills, init scripts):

// Bundled into /script.js
&js.Script{Content: "console.log('init')"}

// Written as standalone file
&js.Script{Name: "analytics.js", Content: analyticsJS}

Stdlib constraints

tinywasm/js compiles to WASM — the Go stdlib is vetoed to keep binary size minimal.

Stdlib (prohibited) Replacement
context github.com/tinywasm/context
fmt, errors, strings, strconv, path github.com/tinywasm/fmt
encoding/json github.com/tinywasm/json
time github.com/tinywasm/time
map[string]string (headers) []fetch.Header

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetRuntime added in v0.0.2

func SetRuntime(r Runtime)

SetRuntime configures the runtime once at boot.

Types

type Message added in v0.0.2

type Message struct {
	Data []byte
}

Message is the payload of postMessage of a Web Worker.

type Request added in v0.0.2

type Request struct {
	URL     string
	Method  string
	Headers []fetch.Header
	Body    []byte
}

Request is the FetchEvent incoming intercepted by the SW (inbound).

type Runtime added in v0.0.2

type Runtime int

Runtime represents the Go compiler used.

const (
	RuntimeGo Runtime = iota
	RuntimeTinyGo
)

type Script added in v0.0.2

type Script struct {
	Name    string // Simple filename (e.g., "sw.js"); no separators or path traversals.
	Content string
}

Script represents a JS fragment produced by an SSR module. - Empty Name: Content is bundled into the global script.js. - Non-empty Name: Content is written as /public/<Name> (standalone file).

func PageBootstrap added in v0.0.2

func PageBootstrap() *Script

PageBootstrap returns the entrypoint Script for the main page.

func ServiceWorker added in v0.0.2

func ServiceWorker(handler ServiceWorkerHandler) *Script

ServiceWorker returns the standalone Script for the service worker.

func WebWorker added in v0.0.2

func WebWorker(name string, handler WebWorkerHandler) *Script

WebWorker returns a standalone Script for a web worker.

func (*Script) String added in v0.0.2

func (s *Script) String() string

String returns the raw content (for parity with tinywasm/css Stylesheet.String()).

type ServiceWorkerHandler added in v0.0.2

type ServiceWorkerHandler interface {
	OnInstall(ctx context.Context) error
	OnActivate(ctx context.Context) error
	OnFetch(ctx context.Context, req *Request) (*fetch.Response, error)
}

ServiceWorkerHandler is the logic for SW events.

type WebWorkerHandler added in v0.0.2

type WebWorkerHandler interface {
	OnMessage(ctx context.Context, msg *Message) (*Message, error)
}

WebWorkerHandler processes messages received by postMessage.

Jump to

Keyboard shortcuts

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