package warpc

import "github.com/gohugoio/hugo/internal/warpc"

Index

Constants

const (
	MessageKindJSON string = "json"
	MessageKindBlob string = "blob"
)

Variables

var ErrShutdown = fmt.Errorf("dispatcher is shutting down")

Types

type Binary

type Binary struct {
	// The name of the binary.
	// For 	quickjs, this must match the instance import name, "javy_quickjs_provider_v2".
	// For the main module, we only use this for caching.
	Name string

	// THe wasm binary.
	Data []byte
}

Binary represents a WebAssembly binary.

type CommonImageProcessingParams

type CommonImageProcessingParams struct {
	Width  int `json:"width,omitempty"`
	Height int `json:"height,omitempty"`
	Stride int `json:"stride,omitempty"`

	// For animated images.
	FrameDurations []int `json:"frameDurations,omitempty"`
	LoopCount      int   `json:"loopCount,omitempty"`
}

type CompileModuleContext

type CompileModuleContext struct {
	Opts    Options
	Runtime wazero.Runtime
}

type CompiledModule

type CompiledModule struct {
	// Runtime (e.g. QuickJS) may be nil if not needed (e.g. embedded in Module).
	Runtime wazero.CompiledModule

	// If Runtime is not nil, this should be the name of the instance.
	RuntimeName string

	// The main module to instantiate.
	// This will be insantiated multiple times in a pool,
	// so it does not need a name.
	Module wazero.CompiledModule
}

type DestinationProvider

type DestinationProvider interface {
	GetDestination() io.Writer
}

type Dispatcher

type Dispatcher[Q, R any] interface {
	Execute(ctx context.Context, q Message[Q]) (Message[R], error)
	Close() error
}

func Start

func Start[Q, R any](opts Options) (Dispatcher[Q, R], error)

Start creates a new dispatcher pool.

type Dispatchers

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

Dispatchers holds all the dispatchers for the warpc package.

func AllDispatchers

func AllDispatchers(katexOpts, webpOpts Options) *Dispatchers

AllDispatchers creates all the dispatchers for the warpc package. Note that the individual dispatchers are started lazily. Remember to call Close on the returned Dispatchers when done.

func (*Dispatchers) Close

func (d *Dispatchers) Close() error

func (*Dispatchers) Katex

func (*Dispatchers) NewWepCodec

func (d *Dispatchers) NewWepCodec(quality int, hint string) (*WebpCodec, error)

func (*Dispatchers) Webp

type Header struct {
	// Major version of the protocol.
	Version uint16 `json:"version"`

	// Unique ID for the request.
	// Note that this only needs to be unique within the current request set time window.
	ID uint32 `json:"id"`

	// Command is the command to execute.
	Command string `json:"command"`

	// RequestKinds is a list of kinds in this RPC request,
	// e.g. {"json", "blob"}, or {"json"}.
	RequestKinds []string `json:"requestKinds"`
	// ResponseKinds is a list of kinds expected in the response,
	// e.g. {"json", "blob"}, or {"json"}.
	ResponseKinds []string `json:"responseKinds"`

	// Set in the response if there was an error.
	Err string `json:"err"`

	// Warnings is a list of warnings that may be returned in the response.
	Warnings []string `json:"warnings,omitempty"`
}

Header is in both the request and response.

type KatexInput

type KatexInput struct {
	Expression string       `json:"expression"`
	Options    KatexOptions `json:"options"`
}

See https://katex.org/docs/options.html

type KatexOptions

type KatexOptions struct {
	// html, mathml (default), htmlAndMathml
	Output string `json:"output"`

	// If true, display math in display mode, false in inline mode.
	DisplayMode bool `json:"displayMode"`

	// Render \tags on the left side instead of the right.
	Leqno bool `json:"leqno"`

	// If true,  render flush left with a 2em left margin.
	Fleqn bool `json:"fleqn"`

	// The color used for typesetting errors.
	// A color string given in the format "#XXX" or "#XXXXXX"
	ErrorColor string `json:"errorColor"`

	// A collection of custom macros.
	Macros map[string]string `json:"macros,omitempty"`

	// Specifies a minimum thickness, in ems, for fraction lines.
	MinRuleThickness float64 `json:"minRuleThickness"`

	// If true, KaTeX will throw a ParseError when it encounters an unsupported command.
	ThrowOnError bool `json:"throwOnError"`

	// Controls how KaTeX handles LaTeX features that offer convenience but
	// aren't officially supported, one of error (default), ignore, or warn.
	//
	//  - error: Throws an error when convenient, unsupported LaTeX features
	//    are encountered.
	//  - ignore: Allows convenient, unsupported LaTeX features without any
	//    feedback.
	//  - warn: Emits a warning when convenient, unsupported LaTeX features are
	//    encountered.
	//
	// The "newLineInDisplayMode" error code, which flags the use of \\
	// or \newline in display mode outside an array or tabular environment, is
	// intentionally designed not to throw an error, despite this behavior
	// being questionable.
	Strict string `json:"strict"`
}

KatexOptions defines the options for the KaTeX rendering. See https://katex.org/docs/options.html

type KatexOutput

type KatexOutput struct {
	Output string `json:"output"`
}

type Message

type Message[T any] struct {
	Header Header `json:"header"`
	Data   T      `json:"data"`
}

func (Message[T]) GetID

func (m Message[T]) GetID() uint32

type Options

type Options struct {
	Ctx context.Context

	Infof func(format string, v ...any)

	Warnf func(format string, v ...any)

	// E.g. quickjs wasm. May be omitted if not needed.
	Runtime Binary

	// The main module to instantiate.
	Main Binary

	CompilationCacheDir string
	PoolSize            int

	// Memory limit in MiB.
	Memory int
}

type ProtocolType

type ProtocolType int
const (
	ProtocolJSON          ProtocolType = iota + 1
	ProtocolJSONAndBinary              = iota
)

type SourceProvider

type SourceProvider interface {
	GetSource() hugio.SizeReader
}

type WEBP

type WEBP struct {
	image.Image // The first frame.
	// contains filtered or unexported fields
}

WEBP represents an animated WebP image. The naming deliberately matches the fields in the standard library image/gif package.

func (*WEBP) GetFrameDurations

func (w *WEBP) GetFrameDurations() []int

func (*WEBP) GetFrames

func (w *WEBP) GetFrames() []image.Image

func (*WEBP) GetLoopCount

func (w *WEBP) GetLoopCount() int

func (*WEBP) GetRaw

func (w *WEBP) GetRaw() any

func (*WEBP) SetFrames

func (w *WEBP) SetFrames(frames []image.Image)

func (*WEBP) SetWidthHeight

func (w *WEBP) SetWidthHeight(width, height int)

type WebpCodec

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

func (*WebpCodec) Decode

func (d *WebpCodec) Decode(r io.Reader) (image.Image, error)

Decode reads a WEBP image from r and returns it as an image.Image. Note that animated WebP images are returnes as an himage.AnimatedImage.

func (*WebpCodec) DecodeConfig

func (d *WebpCodec) DecodeConfig(r io.Reader) (image.Config, error)

func (*WebpCodec) Encode

func (d *WebpCodec) Encode(w io.Writer, img image.Image) error

type WebpInput

type WebpInput struct {
	Source      hugio.SizeReader `json:"-"`       // Will be sent in a separate stream.
	Destination io.Writer        `json:"-"`       // Will be used to write the result to.
	Options     map[string]any   `json:"options"` // Config options.
	Params      map[string]any   `json:"params"`  // Command params (width, height, etc.).
}

If you're reading this and questioning the protocol on top of WASM using JSON and streams instead of WAS Call's with pointers written to linear memory:

- The goal of this is to eventually make it into one or more RPC plugin APIs. - Passing pointers around has a number of challenges in that context. - One would be that it's not possible to pass pointers to child processes (e.g. non-WASM plugins).

Also, you would think that this JSON/streams approach would be significantly slower than using pointers directly, but in practice, at least for WebP, the difference is negligible, see below output from a test run:

[pointers] DecodeWebp took 18.168375ms [pointers] EncodeWebp took 13.959458ms [pointers] DecodeWebpConfig took 93.083µs

[streams] DecodeWebp took 17.192917ms [streams] EncodeWebp took 14.084792ms [streams] DecodeWebpConfig took 54.334µs

Also note that the placement of this code in this internal package is also temporary. We 1. Need to get the WASM RPC plugin infrastructure in place, and 2. Need to decide on the final API shape for image processing plugins.

func (WebpInput) GetDestination

func (w WebpInput) GetDestination() io.Writer

func (WebpInput) GetSource

func (w WebpInput) GetSource() hugio.SizeReader

type WebpOutput

type WebpOutput struct {
	Params CommonImageProcessingParams `json:"params"`
}

Source Files

katex.go warpc.go webp.go

Directories

PathSynopsis
internal/warpc/genjs
Version
v0.153.4 (latest)
Published
Dec 28, 2025
Platform
linux/amd64
Imports
26 packages
Last checked
4 months ago

Tools for package owners.