# kiwix/kiwix-js architecture

> Architecture diagram of the GitHub repository kiwix/kiwix-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kiwix/kiwix-js
- Repository: https://github.com/kiwix/kiwix-js

## Overview

##### Project Type and Purpose
- Kiwix JS is primarily a browser extension and PWA that allows users to read vast offline resources like Wikipedia from highly compressed ZIM archives.
- The README emphasizes that it supports multiple platforms (Firefox, Chrome/Edge, Safari, Electron, etc.) and highlights different modes for processing and serving content (via Service Workers, fallback “Restricted” modes, etc.).
- The system also integrates decoders (such as xz and zstd decoders compiled via Emscripten) to handle archived content and uses a modular library-based architecture.

##### Main Components
a. User Interface (UI):
- “www” directory assets (HTML, CSS, JavaScript). These include the main application pages (index.html, article.html, main.html, etc.), and support libraries in the www/js/lib directory.
- “index.html” at the project root and other HTML files (e.g., background scripts like backgroundscript.js, replayWorker.js, service-worker.js) which handle browser extension lifecycle events.

b. Service Worker / Content Injection Module:
- Service Worker component that intercepts network requests to inject content from ZIM files. Also indicate the alternative “ServiceWorkerLocal” and “Restricted” modes managed via configuration.

c. Offline Content and Decoders:
- Emscripten directories that contain decoders (e.g., xzdec and zstandard modules) which provide decompression and data extraction for offline content.
- Decoders to the main content processing library (found in www/js/lib such as zimArchive, zimArchiveLoader, etc.) that manages file parsing and dependency injection.

d. Browser Integration and Extension Manifests:
- Integration point with browser extension frameworks by including nodes for the various manifest files (Manifest V2, V3, manifest.webapp, manifest.webmanifest), background scripts, and configuration files.
- Workflows that handle installation from browser stores and backwards compatibility layers (e.g., packaged zip/crx files prepared via scripts).

e. File Access and Configuration:
- File system interactions, such as use of the File System Access API, webkitdirectory fallback, and drag-and-drop file selection for opening ZIM archives.
- A node for the localization support (the _locales and i18n directories) to show that the app supports multiple languages.

f. Build, Testing, and Deployment Pipeline:
- Build system and tooling using nodes for package.json, rollup.config.js, babel.config.js, and various scripts in the “scripts” directory which package, compile, and deploy the extension.
- Continuous integration/deployment pipeline in the .github/workflows directory, Docker configurations in the docker folder, and tests in the tests folder (both end-to-end and unit tests).

- “Library / Content Injection” component with the “File Access API” block indicating how archives are loaded and parsed.
- Link “Build Tools” to both the “UI” and “Extension Packaging” components, indicating how source code is transformed and bundled.

##### Important Architectural Patterns and Design Principles
- Separation of concerns: front-end UI, content injection, native decoding, and platform-specific integration are clearly decoupled.
- Fallback strategies such as switching between ServiceWorker modes and the “Restricted” mode, highlighting the conditional nature of content serving based on browser capabilities.
- That a modular, plug-and-play architecture allows for multiple target platforms (from browser extensions to full PWAs).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client-side Components
    subgraph "Client-side Components"
        %% UI Layer
        subgraph "UI Layer"
            UI_www["UI (www)"]:::ui
            Main_Index["index.html"]:::ui
        end
        %% Service Worker Layer
        subgraph "Service Worker Layer"
            ServiceWorker["Service Worker"]:::sw
            BackgroundScript["Background Script"]:::sw
            ReplayWorker["Replay Worker"]:::sw
        end
        %% Decoder Modules
        subgraph "Decoder Modules"
            DecoderWrappers["Decoder Wrappers (www/js/lib)"]:::decoder
            XzDecoder["Xz Decoder (emscripten/xzdec)"]:::decoder
            ZstdDecoder["Zstandard Decoder (emscripten/zstandard)"]:::decoder
        end
    end

    %% Browser Integration & Localization
    subgraph "Browser Integration & Localization"
        %% Extension Manifests
        subgraph "Extension Manifests"
            Manifest_FX["Manifest FX V3"]:::integration
            Manifest_V3["Manifest V3"]:::integration
            Manifest_V2["Manifest V2"]:::integration
            Manifest_Webapp["Manifest Webapp"]:::integration
            Manifest_Webmanifest["Manifest Webmanifest"]:::integration
        end
        %% File Access & Localization
        subgraph "File Access & Localization"
            Locales["_locales"]:::access
            I18n["i18n"]:::access
        end
    end

    %% Build, Testing, and Deployment Pipeline
    subgraph "Build Pipeline"
        Package["package.json"]:::build
        Babel["babel.config.js"]:::build
        Rollup["rollup.config.js"]:::build
        Scripts["scripts"]:::build
        Workflows[".github/workflows"]:::build
        Docker["docker"]:::build
        Tests["tests"]:::build
    end

    %% Relationships and Flows
    UI_www -->|"triggers"| ServiceWorker
    ServiceWorker -->|"injects"| DecoderWrappers
    DecoderWrappers -->|"decodes"| XzDecoder
    DecoderWrappers -->|"decodes"| ZstdDecoder

    UI_www -->|"loads"| Locales
    UI_www -->|"loads"| I18n

    BackgroundScript -->|"declaredIn"| Manifest_V2
    ServiceWorker -->|"declaredIn"| Manifest_V3
    ReplayWorker -->|"associatedWith"| Manifest_Webapp
    UI_www -->|"compatibleWith"| Manifest_Webmanifest

    Scripts -->|"builds"| UI_www
    Scripts -->|"builds"| ServiceWorker
    Scripts -->|"packages"| Manifest_FX

    %% Click Events for UI
    click UI_www "https://github.com/kiwix/kiwix-js/tree/main/www"
    click Main_Index "https://github.com/kiwix/kiwix-js/blob/main/index.html"

    %% Click Events for Service Worker / Injection Module
    click ServiceWorker "https://github.com/kiwix/kiwix-js/blob/main/service-worker.js"
    click BackgroundScript "https://github.com/kiwix/kiwix-js/blob/main/backgroundscript.js"
    click ReplayWorker "https://github.com/kiwix/kiwix-js/blob/main/replayWorker.js"

    %% Click Events for Offline Content and Decoders
    click XzDecoder "https://github.com/kiwix/kiwix-js/tree/main/emscripten/xzdec"
    click ZstdDecoder "https://github.com/kiwix/kiwix-js/tree/main/emscripten/zstandard"
    click DecoderWrappers "https://github.com/kiwix/kiwix-js/tree/main/www/js/lib"

    %% Click Events for Browser Integration and Extension Manifests
    click Manifest_FX "https://github.com/kiwix/kiwix-js/blob/main/manifest.fx.v3.json"
    click Manifest_V3 "https://github.com/kiwix/kiwix-js/blob/main/manifest.json"
    click Manifest_V2 "https://github.com/kiwix/kiwix-js/blob/main/manifest.v2.json"
    click Manifest_Webapp "https://github.com/kiwix/kiwix-js/blob/main/manifest.webapp"
    click Manifest_Webmanifest "https://github.com/kiwix/kiwix-js/blob/main/manifest.webmanifest"

    %% Click Events for File Access and Localization
    click Locales "https://github.com/kiwix/kiwix-js/tree/main/_locales"
    click I18n "https://github.com/kiwix/kiwix-js/tree/main/i18n"

    %% Click Events for Build, Testing, and Deployment Pipeline
    click Package "https://github.com/kiwix/kiwix-js/blob/main/package.json"
    click Babel "https://github.com/kiwix/kiwix-js/blob/main/babel.config.js"
    click Rollup "https://github.com/kiwix/kiwix-js/blob/main/rollup.config.js"
    click Scripts "https://github.com/kiwix/kiwix-js/tree/main/scripts"
    click Workflows "https://github.com/kiwix/kiwix-js/tree/main/.github/workflows"
    click Docker "https://github.com/kiwix/kiwix-js/tree/main/docker"
    click Tests "https://github.com/kiwix/kiwix-js/tree/main/tests"

    %% Styles
    classDef ui fill:#e0f7fa,stroke:#006064,stroke-width:2px;
    classDef sw fill:#e1bee7,stroke:#4a148c,stroke-width:2px;
    classDef decoder fill:#fff9c4,stroke:#f9a825,stroke-width:2px;
    classDef integration fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef access fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef build fill:#d1c4e9,stroke:#512da8,stroke-width:2px;
```
