@anzaui/engine - v0.6.0
    Preparing search index...

    Class Engine

    A loaded template set plus a validated signing key. Get one from Setup.run; it renders pages (Engine.renderPage) and envelopes (Engine.renderFragment).

    The engine has three steps: load (read and SHA-256 each .html file), parse (split into chunks and build a render function), both done once by Setup.run, and render (call the function, then optionally sign). Rendering does a name check and one Map.get, then a loop of string concatenations; it never reads the filesystem.

    The cache store and file watcher types are internal: build engines with Setup rather than this constructor.

    const engine = await new Setup({ root: './templates' }).run();
    const doc = await engine.renderPage('/', { title: 'Home' });
    const envelope = await engine.renderFragment('feed/card.html', 'feed', { title: 'Live' });
    Index
    • Parameters

      • root: string

        The template root directory.

      • cache: CacheStore

        The loaded templates.

      • signing: Signer | SignOptions = ...

        Signing options (validated here) or an existing Signer.

      • Optionalwatcher: FileWatcher

        The file watcher when templates are reloaded on change.

      • options: EngineOptions = {}

        Fragment allowlist.

      Returns Engine

      AnzaError CRYPTO when signing holds a missing or malformed key.

    cache: CacheStore

    The in-memory template cache (replaced wholesale on reload).

    fragments: readonly string[]

    Fragment allowlist (see EngineOptions.fragments).

    root: string

    The template root directory.

    signer: Signer

    The validated signing key. Never printed; signer.publicKey is the raw Ed25519 public key to publish to verifiers.

    watcher?: FileWatcher

    The file watcher when watch: true; call watcher.close() to stop reloading.

    • Look a template up by name. Never touches the filesystem.

      Parameters

      • name: string

        Template name relative to the root, e.g. feed/card.html.

      Returns Template

      The parsed template.

      AnzaError VALIDATION for a malformed name (see validName); NOT_FOUND when no such template is loaded.

      const html = engine.get('feed/card.html').bind({ title: 'Live Update' });
      
    • Render templateName into a (signed) envelope for slot. Templates outside the fragments allowlist are reported as not found.

      Parameters

      • templateName: string

        Template name relative to the root.

      • slot: string

        Target name written into the envelope.

      • Optionalparams: Params

        Values for the placeholders.

      Returns Promise<Envelope>

      An envelope stamped with the current Unix time, signed when signing is on.

      AnzaError VALIDATION for a malformed name; NOT_FOUND for an unknown template or one outside the allowlist.

    • Render the page for route (the pipeline is described on Page).

      Parameters

      • route: string

        The request path, e.g. / or /docs/intro.

      • Optionalparams: Params

        Values for the page and shell placeholders.

      Returns Promise<Document>

      The full HTML document.

      AnzaError NOT_FOUND for an unknown or malformed route without a pages/index.html catch-all.

    • Sign a v2 message for ts, slot and html (no CSS) with the engine's key.

      Parameters

      • ts: number

        Unix time in seconds.

      • slot: string

        Target name.

      • html: string

        Fragment HTML.

      Returns Promise<string | undefined>

      Lowercase hex signature, or undefined when signing is off.

    • Verify an envelope against this engine's own key (HMAC secret or the Ed25519 public key). Same checks, in the same order, as the standalone verifyEnvelope.

      Parameters

      Returns void

      AnzaError CRYPTO naming the failed check, or signing is off when the engine has no key.

      engine.verifyEnvelope(JSON.parse(body), { maxAgeSecs: 60, slot: 'feed' });