Zhiva Architecture
Zhiva's architecture is designed to be modular and leverage familiar web development patterns while remaining lightweight.
Core Components
-
The Native Engine: A lightweight, cross-platform executable responsible for creating and managing a webview window. It acts as the bridge between the operating system and the web application.
-
The Bun Runtime: Zhiva uses Bun as its fast JavaScript/TypeScript runtime. The runtime is responsible for executing your application's backend server code (
src/index.ts) and launching the native engine. -
The Base Library (
base-lib): A shared TypeScript library (@wxn0brp/zhiva-base-lib) that provides the core logic and APIs for Zhiva applications. Internally,base-libutilizes thefalcon-framelibrary to provide its server-side functionalities.- Server/API Framework: It exposes an Express-like API for building your backend:
app: An application instance used to configure the web server (e.g., serving static directories withapp.static()).oneWindow: A function to create the main application window.apiRouter: A router for defining secure API endpoints that are automatically authenticated.
- Convenient Response Handling: API handlers can simply return a JavaScript
objectorstring, and the framework automatically formats it as a JSON or text HTTP response, enhancing Developer Experience (DX). - Client-side API: For the frontend, it provides helpers to communicate with the native layer for features like window management and notifications.
- Server/API Framework: It exposes an Express-like API for building your backend:
-
Your Application: A standard web project in a Git repository. It typically consists of:
- Backend Code (e.g.,
src/index.ts): Runs on the server using Bun and sets up the server and API routes using thebase-lib. - Frontend Code (e.g.,
src/browser.ts): Runs in the webview. It's usually compiled into adistfolder, which is then served by theapp. - Static Assets (e.g.,
public/): Containsindex.htmland other assets.
- Backend Code (e.g.,
Communication and Startup Flow
- The
zhivaCLI starts your application's main script (e.g.,bun run src/index.ts). - Your backend script imports
appandoneWindowfrom the base library. - You configure the server by calling
app.static("public")andapp.static("dist")to serve your HTML, CSS, and compiled JavaScript. - You call
await oneWindow()to create and display the desktop application window. - Your script also imports
apiRouterto define secure API endpoints (e.g.,apiRouter.get(...)). - The native engine loads the
index.htmlfrom yourpublicdirectory. - Your frontend JavaScript (from
dist/) runs in the webview and makesfetchcalls to yourapiRouterendpoints (e.g.,/api/installed). - The
apiRouterhandles the request, executes your logic, and returns a JSON or text response based on the returnedobjectorstring.
This entire process is secure and self-contained, with the apiRouter ensuring that only your application's UI can access the backend APIs.