# cookiegmvn/mbbank architecture

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

- Interactive diagram: https://gitdiagram.com/cookiegmvn/mbbank
- Repository: https://github.com/cookiegmvn/mbbank

## Overview

##### Project Type and Purpose
- This is a Node.js/TypeScript library (no UI) that wraps the Vietnam Military Bank’s HTTP API in promise‐based methods and provides optional OCR captcha‐solving via either Tesseract CLI or a WebAssembly model.

##### File Structure
- Top‐level “src” organizes core code:
- src/index.ts exports the MB client class
- src/MB.ts implements the main library interface (login, getBalance, getTransactions, etc.)
- src/utils contains modules for:
– Global.ts (HTTP client, session/cookies, default headers)
– LoadWasm.ts (download or load local WASM OCR model)
– OCRModel.ts (runs OCR in WASM)
– Tesseract.ts (invokes external Tesseract binary)
– Wasm.ts (low-level WASM bindings)
- src/typings define TypeScript interfaces for API payloads
- Build & config: tsup.config.ts bundles, typedoc.json generates docs, tsconfig.json for compilation, ESLint/GitHub workflows for CI.
- “dist” holds the compiled JS + type declarations, “docs” holds the generated website.

##### README Insights
- Describes client configuration (credentials, preferred OCR method, saveWasm), features (login, balance, transaction history), and external dependencies (tesseract-ocr CLI, optional WASM model caching).

a. Main Components
1. User Application – calls library methods
2. MB Library Core (MB class)
3. HTTP Layer (utils/Global)
4. OCR Layer (choose one based on preferredOCRMethod):
– Tesseract CLI Module (utils/Tesseract)
– WASM OCR Module (utils/OCRModel + utils/LoadWasm + utils/Wasm + local filesystem cache)
5. Remote MBBank API (login, balance, transactions endpoints)
6. Local Filesystem (optional WASM file caching)
7. External Process: Tesseract OCR executable (if selected)

b. Relationships & Data Flows
- MB Library → HTTP Layer: build requests, attach cookies/session
- HTTP Layer ↔ Remote MBBank API: request/response HTTP traffic
- When encountering a CAPTCHA challenge:
- MB Library delegates to OCR Layer
- OCR Layer uses either:
­– Tesseract CLI: spawn external process → returns text
­– WASM OCR: Load model (from GitHub or local FS) → run inference → returns text
- OCR result → MB Library → continues HTTP workflow
- If saveWasm=true: LoadWasm writes/reads a .wasm file on Local Filesystem

c. Architectural Patterns & Principles
- Single‐class API façade (MB.ts) that encapsulates all flows
- Strategy pattern for OCR: interchangeable implementations (default WASM, tesseract, custom)
- Separation of concerns: HTTP, OCR, configuration, and types are modular
- Promise‐based async flows

d. Key Technologies & Libraries
- Node.js HTTP (axios or fetch inside utils/Global)
- TypeScript for types and safety
- Tesseract‐OCR CLI
- WebAssembly (WASM) model loaded at runtime
- tsup for bundling, typedoc for docs, ESLint and GitHub Actions for CI

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Main Flow
    UserApp["User Application"]:::external
    Entry["src/index.ts\n(Entry Point Exporter)"]:::internal
    MBLib["src/MB.ts\n(MBBank Library)"]:::internal
    HTTP["src/utils/Global.ts\n(HTTP Layer)"]:::internal
    API["Remote MBBank API"]:::external
    TesseractProc["Tesseract OCR Executable"]:::external
    LocalFS["Local Filesystem\n(WASM Cache)"]:::local

    subgraph OCR_Strategy["OCR Strategy"]
        direction TB
        TesseractMod["src/utils/Tesseract.ts\n(Tesseract CLI Module)"]:::internal
        subgraph WASM_OCR["WASM OCR"]
            direction TB
            OCRModel["src/utils/OCRModel.ts\n(OCRModel)"]:::internal
            LoadWasm["src/utils/LoadWasm.ts\n(LoadWasm)"]:::internal
            WasmBind["src/utils/Wasm.ts\n(WASM Bindings)"]:::internal
        end
    end

    %% Connections
    UserApp -->|"calls async"| Entry
    Entry -->|"exports MB"| MBLib
    MBLib -->|"uses async HTTP"| HTTP
    HTTP -->|"HTTP req/res"| API
    MBLib -->|"on CAPTCHA"| OCR_Strategy
    OCR_Strategy -->|"spawn process"| TesseractMod
    TesseractMod -->|"executes"| TesseractProc
    OCR_Strategy -->|"WASM inference"| OCRModel
    OCRModel -->|loads via| LoadWasm
    LoadWasm -->|reads/writes| LocalFS
    LoadWasm -->|binds via| WasmBind
    OCRModel -->|returns text| MBLib

    %% Project Files & Config
    subgraph Project_Metadata["Project Metadata & Docs"]
        direction TB
        PM1["package.json"]:::internal
        PM2["README.md"]:::internal
        subgraph Typings["TypeScript Interfaces"]
            MBApi["src/typings/MBApi.ts"]:::internal
            MBLogin["src/typings/MBLogin.ts"]:::internal
        end
        subgraph Build_Config["Build & Config"]
            TSUP["tsup.config.ts"]:::internal
            TSCONF["tsconfig.json"]:::internal
            DOCS_CONF["typedoc.json"]:::internal
        end
        subgraph Lint_CI["Linting & CI"]
            ESLINT[".eslintrc.js"]:::internal
            WORK1[".github/workflows/eslint.yaml"]:::internal
            WORK2[".github/workflows/nodejs.yaml"]:::internal
        end
        subgraph Output["Compiled & Docs"]
            DIST_JS["dist/index.js"]:::internal
            DIST_DTS["dist/index.d.ts"]:::internal
            DOCS["docs/index.html"]:::internal
        end
    end

    %% Click Events
    click MBLib "https://github.com/cookiegmvn/mbbank/blob/main/src/MB.ts"
    click Entry "https://github.com/cookiegmvn/mbbank/blob/main/src/index.ts"
    click HTTP "https://github.com/cookiegmvn/mbbank/blob/main/src/utils/Global.ts"
    click TesseractMod "https://github.com/cookiegmvn/mbbank/blob/main/src/utils/Tesseract.ts"
    click OCRModel "https://github.com/cookiegmvn/mbbank/blob/main/src/utils/OCRModel.ts"
    click LoadWasm "https://github.com/cookiegmvn/mbbank/blob/main/src/utils/LoadWasm.ts"
    click WasmBind "https://github.com/cookiegmvn/mbbank/blob/main/src/utils/Wasm.ts"
    click MBApi "https://github.com/cookiegmvn/mbbank/blob/main/src/typings/MBApi.ts"
    click MBLogin "https://github.com/cookiegmvn/mbbank/blob/main/src/typings/MBLogin.ts"
    click TSUP "https://github.com/cookiegmvn/mbbank/blob/main/tsup.config.ts"
    click TSCONF "https://github.com/cookiegmvn/mbbank/blob/main/tsconfig.json"
    click DOCS_CONF "https://github.com/cookiegmvn/mbbank/blob/main/typedoc.json"
    click ESLINT "https://github.com/cookiegmvn/mbbank/blob/main/.eslintrc.js"
    click WORK1 "https://github.com/cookiegmvn/mbbank/blob/main/.github/workflows/eslint.yaml"
    click WORK2 "https://github.com/cookiegmvn/mbbank/blob/main/.github/workflows/nodejs.yaml"
    click DIST_JS "https://github.com/cookiegmvn/mbbank/blob/main/dist/index.js"
    click DIST_DTS "https://github.com/cookiegmvn/mbbank/blob/main/dist/index.d.ts"
    click DOCS "https://github.com/cookiegmvn/mbbank/blob/main/docs/index.html"
    click PM1 "https://github.com/cookiegmvn/mbbank/blob/main/package.json"
    click PM2 "https://github.com/cookiegmvn/mbbank/blob/main/README.md"

    %% Styles
    classDef internal fill:#add8e6,stroke:#333,stroke-width:1px
    classDef external fill:#90ee90,stroke:#333,stroke-width:1px
    classDef local fill:#d3d3d3,stroke:#333,stroke-width:1px
```
