# Hyperfrontend Architecture Hyperfrontend is a layered architecture designed for runtime micro-frontend integration. At its core, it enables independently deployed frontend applications ("features") to communicate through secure, contract-validated messaging—regardless of what framework they use. ```mermaid --- config: theme: base themeVariables: fontSize: 12px --- flowchart TB subgraph Ecosystem["HYPERFRONTEND ECOSYSTEM"] direction TB subgraph Apps["Applications"] direction LR Host["HOST APPLICATION
(React, Vue, etc.)"] Feature1["FEATURE #1
(Any Framework)"] Feature2["FEATURE #2
(Any Framework)"] end Nexus["@hyperfrontend/nexus
Communication Protocol
(Broker-Channel Architecture)"] Protocol["@hyperfrontend/network-protocol
Security Layer
(Encryption + Obfuscation)"] Crypto["@hyperfrontend/cryptography
AES-GCM + PBKDF2"] Host --> Nexus Feature1 --> Nexus Feature2 --> Nexus Nexus --> Protocol Protocol --> Crypto end Crypto -.- CryptoLabel["Crypto Primitives"] ``` --- ## Library Stack The architecture is composed of specialized libraries that layer on top of each other: | Layer | Package | Responsibility | | ----------------- | --------------------------------- | -------------------------------------------------- | | **Tooling** | `@hyperfrontend/features` | Nx plugin for shell generation and automation | | **Communication** | `@hyperfrontend/nexus` | Broker-channel messaging with contracts | | **Security** | `@hyperfrontend/network-protocol` | Encryption pipelines and obfuscation | | **Crypto** | `@hyperfrontend/cryptography` | AES-GCM encryption, PBKDF2 key derivation, hashing | | **Foundation** | `@hyperfrontend/state-machine` | State management patterns | | | `@hyperfrontend/logging` | Structured logging | | | `@hyperfrontend/web-worker` | Web Worker utilities | | | `@hyperfrontend/utils/*` | Data, string, list, time, function utilities | --- ## Communication Layer: Nexus `@hyperfrontend/nexus` implements a TCP-like protocol over the browser's `postMessage` API. It provides secure, contract-validated messaging between browser contexts (iframes, windows, tabs, web workers). ### Broker-Channel Model ```mermaid --- config: theme: base themeVariables: fontSize: 12px --- flowchart TB subgraph Host["HOST WINDOW"] subgraph Broker["BROKER"] BrokerDesc["• Routes incoming postMessage events
• Manages channel registry
• Validates contracts and origins
• Applies security policies"]:::cleanWide subgraph Channels["Channels"] direction LR ChannelA["Channel A"] ChannelB["Channel B"] ChannelC["Channel C"] end end subgraph Features["Features"] direction LR IframeA["iframe
Feature A"] WindowB["window
Feature B"] WorkerC["worker
Feature C"] end ChannelA --> IframeA ChannelB --> WindowB ChannelC --> WorkerC end classDef cleanWide fill:none,stroke:none,text-align:left,padding:0px 0px 0px ``` **Broker**: A singleton within each window context that routes messages to the appropriate channel. The broker holds the channel registry, validates incoming messages against contracts, and applies security policies. **Channel**: A bidirectional communication pipe between two browser contexts. Each channel manages its own: - Connection lifecycle (pending → active → closed) - Message queue for buffering before connection - Event subscriptions (lifecycle and domain messages) - Security transport adapter (for encryption) ### Connection Protocol Channels establish connections using a three-way handshake inspired by TCP: ```mermaid --- config: theme: base themeVariables: fontSize: 12px --- sequenceDiagram participant Host as HOST participant Feature as FEATURE Host->>Feature: SYN (pid, contract) Feature->>Host: SYN-ACK (pid, ack) Host->>Feature: ACK (pid) Note over Host,Feature: CONNECTION ACTIVE ``` Each connection attempt is tracked by a Process ID (UUID), enabling multiple concurrent connection attempts and clean lifecycle management. ### Contract System Contracts define the communication interface between a host and feature: ```typescript const contract = { emitted: [ { type: 'CONFIG', schema: configSchema }, // Host sends configuration { type: 'NAVIGATION', schema: navSchema }, // Host sends navigation events ], accepted: [ { type: 'READY', schema: readySchema }, // Feature signals readiness { type: 'DATA', schema: dataSchema }, // Feature sends data updates ], } ``` - **Emitted actions**: Message types this context sends - **Accepted actions**: Message types this context receives - **JSON Schema validation**: Optional runtime validation of message payloads --- ## Security Layer: Network Protocol `@hyperfrontend/network-protocol` provides defense-in-depth security for message transport. It sits between the application layer and the raw `postMessage` transport. ### Protocol Versions | Version | Security Level | Use Case | | ------- | --------------- | ------------------------------------------ | | **v1** | Obfuscation | Trusted environments, same-origin features | | **v2** | Full Encryption | Cross-origin features, sensitive data | ### Message Pipeline Messages pass through staged queues for transformation: **Outbound Pipeline** ```mermaid flowchart LR A1["Plaintext Message"] --> B1["Encryption Queue"] B1 --> C1["Serialization Queue"] C1 --> D1["Obfuscation Queue"] D1 --> E1["Wire Format"] ``` **Inbound Pipeline** ```mermaid flowchart LR A2["Wire Format"] --> B2["Deobfuscation Queue"] B2 --> C2["Deserialization Queue"] C2 --> D2["Decryption Queue"] D2 --> E2["Plaintext Message"] ``` ### Security Features | Feature | Description | | -------------------------------- | --------------------------------------------------------------------------- | | **Dynamic Key Encryption** | Keys are exchanged per-message via the packet's `key` field | | **Time-Based Password Rotation** | Passwords rotate based on UTC time intervals, synchronized across endpoints | | **Clock Skew Handling** | Automatically attempts ±1 time windows for deobfuscation | | **Packet Obfuscation** | Makes ciphertext unrecognizable as encrypted data | --- ## Cryptography Layer `@hyperfrontend/cryptography` provides isomorphic cryptographic primitives—identical APIs for browser (Web Crypto API) and Node.js (crypto module). ### Capabilities | Capability | Implementation | Details | | ------------------ | ------------------- | ---------------------------------------------------------- | | **Encryption** | AES-256-GCM | Authenticated encryption with password-derived keys | | **Key Derivation** | PBKDF2 | 100,000 iterations, unique salt per operation | | **Hashing** | SHA-256 | Hexadecimal output | | **Time Passwords** | UTC-synchronized | Generates passwords for current/previous/next time windows | | **Vault Storage** | In-memory encrypted | Password-protected storage with optional single-use mode | ### Platform Parity ```typescript // Browser import { encrypt, decrypt } from '@hyperfrontend/cryptography/browser' // Node.js import { encrypt, decrypt } from '@hyperfrontend/cryptography/node' // Same API, platform-optimized implementation const encrypted = await encrypt('sensitive data', 'password') const decrypted = await decrypt(encrypted, 'password') ``` --- ## The Shell Pattern A **shell** is a self-contained package that knows how to load and communicate with a specific feature. Shells are the distribution mechanism for hyperfrontend features. ### What the Shell Contains ```mermaid flowchart LR subgraph shell["FEATURE SHELL"] direction LR subgraph connection["Connection Setup"] conn1["Broker config"] conn2["Channel init"] conn3["Contracts"] end subgraph visual["Visual Coordination"] vis1["Styling"] vis2["Sizing"] vis3["Loading"] end subgraph api["Fluent API"] api1[".mount()"] api2[".send()"] api3[".on()"] end end ``` > ⚠️ The shell does **not** contain the feature app code. Features load at runtime from their deployment URL. ### Distribution Options | Method | Use Case | Consumption | | --------------- | -------------------- | ----------------------------------------------------------- | | **npm package** | Modern build systems | `import { FeatureA } from '@org/shell-feature-a/react'` | | **CDN script** | Legacy applications | `