# duheng/sniperjs architecture

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

- Interactive diagram: https://gitdiagram.com/duheng/sniperjs
- Repository: https://github.com/duheng/sniperjs

## Overview

##### Project Type and Purpose
- This is an open-source, mono-repo JavaScript error-reporting library (managed by Lerna) that provides non-intrusive error collection and configurable report delivery.  It targets both browser environments and multiple mini-app platforms (WeChat, Baidu, Alipay, etc.).

##### File-Structure Analysis
- At the root: a Lerna configuration (lerna.json) plus a Rollup config (rollup.config.js) for building all packages.
- packages/
- ErrorReporter – the core error-capture engine (src/ → dist/).
- MiniWxErrorReporter – platform-specific “adaptor” for WeChat-style mini-apps, built on top of core helpers.
- utils – shared utilities (throttling, merging, type checking).
- Build artifacts live under each package’s dist/ folder; package.json files declare inter-package dependencies.

##### Key README Insights
- Describes features: automatic JS error, HTTP error, Promise rejection capture, optional breadcrumbs.
- Configuration options for report URL, filters, sampling, and hook callback (beforeReport).
- Usage pattern: import the mini package before your App() call, instantiate with a config object, then the library silently intercepts, aggregates, and dispatches error logs to your server.

##### Detailed Points to Capture
- Lerna’s role in linking inter-package dependencies.
- Rollup config’s use across packages.
- The hierarchy: utils at the base, core engine on top, platform SDK at the top.
- Config object fields driving behavior (url, filters, sampling, callbacks).
- HTTP transport as a pluggable endpoint.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Monorepo Root
    subgraph "Monorepo Root"
        Lerna["lerna.json"]:::build
        RootPkg["package.json\npackage-lock.json"]:::build
    end

    %% Build Pipeline
    subgraph "Build Pipeline"
        Rollup["rollup.config.js"]:::build
    end

    %% Packages
    subgraph "packages/utils"
        UtilsSrc["src/index.js"]:::runtime
        UtilsDist["dist/index.js"]:::build
        UtilsPkg["package.json"]:::build
    end

    subgraph "packages/ErrorReporter"
        CoreSrc["src/index.js"]:::runtime
        CoreValidate["src/validateConfig.js"]:::runtime
        CoreDist["dist/index.js"]:::build
        CorePkg["package.json"]:::build
    end

    subgraph "packages/MiniWxErrorReporter"
        SDKSrc["src/index.js"]:::runtime
        Helper1["helpers/centralTry.js"]:::plugin
        Helper2["helpers/errorTypeReg.js"]:::plugin
        Helper3["helpers/parseError.js"]:::plugin
        Helper4["helpers/pluginEventBreadcrumbs.js"]:::plugin
        Helper5["helpers/pluginHookApp.js"]:::plugin
        Helper6["helpers/pluginHookRq.js"]:::plugin
        Helper7["helpers/pluginOnMemoryWarning.js"]:::plugin
        Helper8["helpers/pluginPatchPromise.js"]:::plugin
        Helper9["helpers/request.js"]:::plugin
        SDKDist["dist/index.js"]:::build
        SDKPkg["package.json"]:::build
    end

    %% External Systems
    ConsumerApp["Consumer Application"]:::external
    ExternalService["External Reporting Service"]:::external

    %% Runtime Flow Helpers
    beforeReport["beforeReport Hook"]:::runtime
    HTTPTransport["HTTP Transport (POST)"]:::runtime

    %% Dependencies & Build Relations
    Lerna -->|links| UtilsPkg
    Lerna -->|links| CorePkg
    Lerna -->|links| SDKPkg

    UtilsSrc -->|built by| Rollup
    Rollup --> UtilsDist

    CoreSrc -->|built by| Rollup
    CoreValidate -->|built by| Rollup
    Rollup --> CoreDist

    SDKSrc -->|built by| Rollup
    Helper1 -->|built by| Rollup
    Helper2 -->|built by| Rollup
    Helper3 -->|built by| Rollup
    Helper4 -->|built by| Rollup
    Helper5 -->|built by| Rollup
    Helper6 -->|built by| Rollup
    Helper7 -->|built by| Rollup
    Helper8 -->|built by| Rollup
    Helper9 -->|built by| Rollup
    Rollup --> SDKDist

    %% Package Dependencies
    UtilsPkg -->|provides utils| UtilsSrc
    CorePkg -->|consumes utils| UtilsSrc
    SDKPkg -->|consumes core| CoreSrc

    %% Runtime Data Flow
    ConsumerApp -->|imports| SDKSrc
    ConsumerApp -->|triggers errors| Helper5
    Helper1 --> SDKSrc
    Helper2 --> SDKSrc
    Helper3 --> SDKSrc
    Helper4 --> SDKSrc
    Helper6 --> SDKSrc
    Helper7 --> SDKSrc
    Helper8 --> SDKSrc
    Helper9 --> SDKSrc

    SDKSrc -->|forwards error| CoreSrc
    CoreSrc -->|validates config| CoreValidate
    CoreSrc -->|invokes| beforeReport
    beforeReport -->|passes to| HTTPTransport
    HTTPTransport -->|POST error| ExternalService

    %% Click Events
    click Lerna "https://github.com/duheng/sniperjs/blob/master/lerna.json"
    click RootPkg "https://github.com/duheng/sniperjs/blob/master/package.json"
    click Rollup "https://github.com/duheng/sniperjs/blob/master/rollup.config.js"
    click UtilsSrc "https://github.com/duheng/sniperjs/blob/master/packages/utils/src/index.js"
    click UtilsDist "https://github.com/duheng/sniperjs/blob/master/packages/utils/dist/index.js"
    click UtilsPkg "https://github.com/duheng/sniperjs/blob/master/packages/utils/package.json"
    click CoreSrc "https://github.com/duheng/sniperjs/blob/master/packages/ErrorReporter/src/index.js"
    click CoreValidate "https://github.com/duheng/sniperjs/blob/master/packages/ErrorReporter/src/validateConfig.js"
    click CoreDist "https://github.com/duheng/sniperjs/blob/master/packages/ErrorReporter/dist/index.js"
    click CorePkg "https://github.com/duheng/sniperjs/blob/master/packages/ErrorReporter/package.json"
    click SDKSrc "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/index.js"
    click Helper1 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/centralTry.js"
    click Helper2 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/errorTypeReg.js"
    click Helper3 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/parseError.js"
    click Helper4 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/pluginEventBreadcrumbs.js"
    click Helper5 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/pluginHookApp.js"
    click Helper6 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/pluginHookRq.js"
    click Helper7 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/pluginOnMemoryWarning.js"
    click Helper8 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/pluginPatchPromise.js"
    click Helper9 "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/src/helpers/request.js"
    click SDKDist "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/dist/index.js"
    click SDKPkg "https://github.com/duheng/sniperjs/blob/master/packages/MiniWxErrorReporter/package.json"

    %% Styles
    classDef build fill:#f2d7d5,stroke:#c0392b;
    classDef runtime fill:#d5f5e3,stroke:#27ae60;
    classDef external fill:#fcf3cf,stroke:#f1c40f;
    classDef plugin fill:#d6eaf8,stroke:#2980b9;
```
