# darcas/rollup-sub-resource-integrity architecture

> Architecture diagram of the GitHub repository darcas/rollup-sub-resource-integrity, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/darcas/rollup-sub-resource-integrity
- Repository: https://github.com/darcas/rollup-sub-resource-integrity

## Overview

##### Main Components
- Rollup/Vite Build Process: Represent the overall bundling environment where the plugin is invoked.
- Rollup Plugin (SubResourceIntegrity): This is your core component that hooks into the build process, particularly during the writeBundle phase.
- HTML File Processor: A component within the plugin that is responsible for scanning HTML files, locating <script> and <link> tags, and identifying the resources (via “src” and “href” attributes).
- Hashing Module: This component handles the calculation of SRI hashes using the chosen algorithm (sha256, sha384, or sha512).
- File I/O Module: Manages the reading of resource files and re-writing updated HTML files with the new integrity attributes.
- Configuration: Although minimal in the file tree, note that configuration elements (like the algorithm parameter in the plugin call) guide the plugin behavior.

##### Relationships and Interactions
- The Build Process (Rollup/Vite) calls the plugin as part of its bundling lifecycle.
- During the writeBundle phase, the plugin triggers the HTML File Processor.
- The HTML File Processor reads the output HTML files from the file system (via the File I/O Module).
- For each resource found in the HTML, the Hashing Module calculates the integrity hash.
- The plugin then updates the HTML by embedding the calculated ‘integrity’ attribute and writes the file back.
- Configuration settings (like the chosen hash algorithm) feed into the Hashing Module.

##### Architectural Patterns and Design Principles
- Modular Design: The plugin separates concerns by dividing responsibilities among scanning, hashing, and file I/O.
- Integration via Hooks: It leverages the Rollup build hooks (writeBundle) to integrate smoothly with the build pipeline.
- Configurability: Through a simple parameter (e.g., choosing the hash algorithm), the plugin showcases a plug-and-play design suitable for various build environments.

##### Relevant Technologies and Frameworks
- TypeScript: The code is written in TypeScript (see src/index.ts and tsconfig.json), which enhances type safety and maintainability.
- Rollup (and integration into Vite): The plugin is intended for these ecosystems.
- NPM: The package is managed via npm and is available as @darcas/rollup-sub-resource-integrity.

##### Putting It All Together
- To the left/top, show the Rollup/Vite Build Process triggering the plugin.
- To the right/bottom, illustrate the file system representing the source HTML file input and the output after injection of the SRI attributes.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Rollup/Vite Build"
        RP["Build Process (Rollup/Vite)"]:::external
    end

    subgraph "SubResourceIntegrity Plugin"
        SSI["SubResourceIntegrity Plugin"]:::plugin
        HFP["HTML File Processor"]:::processor
        HM["Hashing Module"]:::hashing
        FIO["File I/O Module"]:::fio
    end

    subgraph "Configuration"
        pkg["package.json"]:::config
        tsconfig["tsconfig.json"]:::config
    end

    FS["File System (HTML Files)"]:::external

    RP -->|"triggers"| SSI
    SSI -->|"initiates"| HFP
    HFP -->|"invokes_read"| FIO
    FIO -->|"reads"| FS
    FS -->|"supplies"| HFP
    HFP -->|"requests_hash"| HM
    pkg -->|"configs"| HM
    tsconfig -->|"configs"| HM
    HM -->|"returns_hash"| HFP
    HFP -->|"updates_html"| FIO
    FIO -->|"writes"| FS

    click SSI "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/src/index.ts"
    click HFP "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/src/index.ts"
    click HM "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/src/index.ts"
    click FIO "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/src/index.ts"
    click pkg "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/package.json"
    click tsconfig "https://github.com/darcas/rollup-sub-resource-integrity/blob/master/tsconfig.json"

    classDef external fill:#FDEBD0,stroke:#D35400,stroke-width:2px;
    classDef plugin fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef processor fill:#D2B4DE,stroke:#884EA0,stroke-width:2px;
    classDef hashing fill:#A9DFBF,stroke:#28B463,stroke-width:2px;
    classDef fio fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
    classDef config fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
```
