TriFrost Documentation
website·Indexed 29 days ago
https://www.trifrost.devA runtime-agnostic server framework featuring built-in rate limiting, caching, and a server-first styling engine. TriFrost supports Node.js, Bun, and Cloudflare Workers (workerd), providing a composable App class, integrated routing, and a structured logging system with JSON, Console, and OpenTelemetry exporters.
What's inside TriFrost
- TriFrost is a runtime-agnostic, performance-focused, TypeScript-first server framework. It is designed to be deployed across multiple environments using an adaptive core and runtime adapters, allowing developers to write application code once and deploy it to various targets without changing the core logic. Key architectural pillars include an explicit, no-magic design for middleware and routing, zero-cost abstractions for performance, and first-class TypeScript support with type-safe middleware chains and context.
Overview of TriFrost Atomic Data Reactor ($tfdr)
The TriFrost Atomic Data Reactor is a lightweight reactive data proxy used within<Script>blocks to enable local data binding and change tracking. It allows developers to declaratively wire DOM elements to logic while maintaining type safety and SSR compatibility. The reactor is exposed as thedataargument in<Script data={...}>blocks. Initial state passed to thedataprop is automatically cloned and made reactive.Overview of TriFrost backend framework principles
TriFrost is a runtime-agnostic backend framework designed to provide a balance of type safety, performance, and portability. It is built to avoid the rigidity and runtime-locking common in other frameworks, allowing developers to write code once and run it across various environments without changes.Overview of TriFrost Atomic runtime
Atomic is a zero-cost client runtime for styles and scripts in TriFrost. It acts as a part renderer, compiler target, and runtime engine that allows developers to build interactive, styled UIs without shipping redundant payloads, layout shift, or dead bundles. It supports inline<style>and<script>tags that are automatically deduplicated, minified, and flushed. It does not require a client-side framework, virtual DOM, or bundle configuration.Overview of TriFrost supported runtimes
TriFrost uses a runtime-agnostic architecture that allows application code to remain consistent and portable across different JavaScript execution environments. It officially supports Node.js, Bun, and Workerd (Cloudflare Workers), enabling developers to choose between traditional server environments, high-performance runtimes, or edge deployments.Overview of TriFrost Authentication Middleware
TriFrost provides first-class authentication middleware that follows a consistent five-step lifecycle:
- Extract credentials (header, query, cookie, etc.).
- Run interval validation (e.g., verify cookie structure).
- Execute a user-defined
validate()function to check against secrets or databases. - Attach the result to
ctx.state.$authif valid. - Fail fast by returning a
401 Unauthorizedif invalid, short-circuiting the request chain.
Authentication can be applied globally via
app.use(), per group viarouter.use(), or per route viaroute.use().Use the `$` utility namespace in Atomic Script blocks
In TriFrost v0.40.0+, the$utility namespace is available as the third argument in Atomic<Script>blocks. This centralized namespace replaces previous node-bound methods to reduce VM boot footprints and improve performance.<Script data={{...}}> {(el, data, $) => { // Use $ utilities here }} </Script>Understand the TriFrost request lifecycle flow
TriFrost uses a linear, non-recursive request lifecycle. The flow is as follows:
- Incoming Request: The request is wrapped in a runtime-specific
Contextobject and a logger instance is spawned. - Route Matching: The path is matched against defined routes. If no match is found, it triggers the nearest
onNotFoundhandler or returns a 404. - Initialize: The logger is patched with route metadata, the body is automatically parsed for
POST,PUT, andPATCHrequests (available viactx.body), and an internal timeout timer (default 30s) starts. - Middleware Execution: App-level, then router-level, then route-level middleware are executed. Each is span-wrapped for tracing. TriFrost checks for short-circuiting after each middleware (e.g., if a response was sent,
setStatus()≥ 400 was called, orctx.abort()was run). - Handler Execution: The route handler runs the main business logic.
- Cleanup & Flush: Metrics/spans are finalized and the logger is flushed. If no response was sent, TriFrost defaults to a 500 error or triggers
onError()if the status is ≥ 400. - After Hooks: Callbacks registered via
ctx.after()execute post-response.
- Incoming Request: The request is wrapped in a runtime-specific
Implement fragment-based rendering with TriFrost Atomic
TriFrost Atomic uses a fragment-rendering architecture where interactions are powered by
ctx.html(...). Instead of sending full pages or large bundles, the server returns only the necessary JSX, scripts, and styles for a specific interaction. This allows for scoped hydration and fast load times. Key architectural patterns include:- Menus: Swapped in and out as fragments.
- Games: Mounted on demand with DOM and Canvas logic encapsulated inside
<Script>components. - Global State: Coordinated using
AtomicRelay(pubsub events) andAtomicStore(typed local storage).
TriFrost Framework Overview
TriFrost is a fast, typed, and runtime-agnostic backend framework designed to run on Bun, Workers, Node, and other environments without requiring bundlers or heavy ceremony. It is built on three core pillars: Type Safety, Runtime Portability, and Performance. The framework provides a streamed, typed, and reactive SSR experience where routes are fragments loaded on demand and hydrated with a VM, eliminating the need for React or a client-side router.Manage script lifecycles with ARC in TriFrost
TriFrost implements an Automatic Reference Counting (ARC) lifecycle for its scripting engine. This ensures that functions and data payloads bound to the DOM are automatically released when the associated injected<Script>blocks are removed from the DOM. This deterministic memory management reduces per-fragment script size and removes reliance on a garbage collector for fragment-based hydration.Automatic script deduplication via the Script Engine
TriFrost 0.34.0 introduces an internal Script Engine that automatically deduplicates repeated scripts and shared data nodes across the render tree. This ensures that only unique scripts are emitted in the final HTML, regardless of how many times a component is rendered.