# ariya/phantomjs architecture

> Architecture diagram of the GitHub repository ariya/phantomjs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ariya/phantomjs
- Repository: https://github.com/ariya/phantomjs

## Overview

PhantomJS is a scriptable headless WebKit runtime. A user runs a script through the executable; the Phantom controller provides page and system-facing objects, while WebPage supports page automation, network activity, and rendering. The README also identifies headless testing, screen capture, and network monitoring as product uses. The graph emphasizes these runtime stages and documented core capabilities; several connections are supported only by source-index references, so unsampled behavior is not expanded into a detailed pipeline.

Main entry points: [`src/phantom.cpp`](https://github.com/ariya/phantomjs/blob/master/src/phantom.cpp) (Phantom controller), [`src/webpage.cpp`](https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp) (WebPage engine), [`src/cookiejar.cpp`](https://github.com/ariya/phantomjs/blob/master/src/cookiejar.cpp) (Cookie jar), [`src/main.cpp`](https://github.com/ariya/phantomjs/blob/master/src/main.cpp) (Executable entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime control"]
  node_main["Executable entry<br/>[main.cpp]"]
  node_phantom["Phantom controller<br/>[phantom.cpp]"]
  node_bootstrap["JS bootstrap<br/>[bootstrap.js]"]
  node_config["Runtime configuration<br/>[config.cpp]"]
  node_cli["Command-line parser<br/>[qcommandline.cpp]"]
end

subgraph group_web["Web automation"]
  node_webpage["WebPage engine<br/>[webpage.cpp]"]
  node_page_api["Page JS API<br/>[webpage.js]"]
  node_network["Network access"]
  node_cookies[("Cookie jar<br/>[cookiejar.cpp]")]
  node_webserver["Web server API<br/>[webserver.cpp]"]
  node_mongoose["HTTP server engine<br/>[mongoose.c]"]
end

subgraph group_apis["Script APIs"]
  node_child_native["Child processes<br/>[childprocess.cpp]"]
  node_child_api["Child process API<br/>[child_process.js]"]
  node_filesystem["Filesystem API<br/>[filesystem.cpp]"]
  node_fs_api["Filesystem JS API<br/>[fs.js]"]
  node_system["System API<br/>[system.cpp]"]
  node_system_api["System JS API<br/>[system.js]"]
  node_cookie_api["Cookie JS API<br/>[cookiejar.js]"]
  node_repl["Interactive REPL<br/>[repl.cpp]"]
end

subgraph group_services["Runtime services"]
  node_terminal["Terminal support<br/>[terminal.cpp]"]
  node_encoding["Text encoding<br/>[encoding.cpp]"]
  node_callbacks["Callback support<br/>[callback.cpp]"]
end

node_user(("Script user"))

node_user -->|"runs script"| node_main
node_main -->|"starts"| node_phantom
node_phantom -->|"creates pages"| node_webpage
node_phantom -->|"uses settings"| node_config
node_phantom -->|"creates"| node_child_native
node_phantom -->|"uses"| node_system
node_phantom -->|"creates"| node_webserver
node_phantom -->|"creates"| node_cookies
node_webpage -->|"uses"| node_network
node_network -->|"uses"| node_cookies
node_webserver -->|"starts server"| node_mongoose
node_phantom -->|"uses"| node_repl
node_repl -.->|"uses"| node_terminal
node_child_api -.->|"wraps"| node_child_native
node_page_api -.->|"wraps"| node_webpage
node_fs_api -.->|"wraps"| node_filesystem
node_system_api -.->|"wraps"| node_system
node_cookie_api -.->|"wraps"| node_cookies

click node_main "https://github.com/ariya/phantomjs/blob/master/src/main.cpp"
click node_phantom "https://github.com/ariya/phantomjs/blob/master/src/phantom.cpp"
click node_bootstrap "https://github.com/ariya/phantomjs/blob/master/src/bootstrap.js"
click node_config "https://github.com/ariya/phantomjs/blob/master/src/config.cpp"
click node_cli "https://github.com/ariya/phantomjs/blob/master/src/qcommandline/qcommandline.cpp"
click node_webpage "https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp"
click node_page_api "https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js"
click node_network "https://github.com/ariya/phantomjs/blob/master/src/networkaccessmanager.cpp"
click node_cookies "https://github.com/ariya/phantomjs/blob/master/src/cookiejar.cpp"
click node_webserver "https://github.com/ariya/phantomjs/blob/master/src/webserver.cpp"
click node_mongoose "https://github.com/ariya/phantomjs/blob/master/src/mongoose/mongoose.c"
click node_child_native "https://github.com/ariya/phantomjs/blob/master/src/childprocess.cpp"
click node_child_api "https://github.com/ariya/phantomjs/blob/master/src/modules/child_process.js"
click node_filesystem "https://github.com/ariya/phantomjs/blob/master/src/filesystem.cpp"
click node_fs_api "https://github.com/ariya/phantomjs/blob/master/src/modules/fs.js"
click node_system "https://github.com/ariya/phantomjs/blob/master/src/system.cpp"
click node_system_api "https://github.com/ariya/phantomjs/blob/master/src/modules/system.js"
click node_cookie_api "https://github.com/ariya/phantomjs/blob/master/src/modules/cookiejar.js"
click node_repl "https://github.com/ariya/phantomjs/blob/master/src/repl.cpp"
click node_terminal "https://github.com/ariya/phantomjs/blob/master/src/terminal.cpp"
click node_encoding "https://github.com/ariya/phantomjs/blob/master/src/encoding.cpp"
click node_callbacks "https://github.com/ariya/phantomjs/blob/master/src/callback.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_phantom,node_bootstrap,node_config,node_cli,node_user toneBlue
class node_webpage,node_page_api,node_network,node_cookies,node_webserver,node_mongoose toneAmber
class node_child_native,node_child_api,node_filesystem,node_fs_api,node_system,node_system_api,node_cookie_api,node_repl toneMint
class node_terminal,node_encoding,node_callbacks toneRose
```

## Components

### Runtime control

- [Executable entry](https://github.com/ariya/phantomjs/blob/master/src/main.cpp): `src/main.cpp`
- [Phantom controller](https://github.com/ariya/phantomjs/blob/master/src/phantom.cpp): `src/phantom.cpp`
- [JS bootstrap](https://github.com/ariya/phantomjs/blob/master/src/bootstrap.js): `src/bootstrap.js`
- [Runtime configuration](https://github.com/ariya/phantomjs/blob/master/src/config.cpp): `src/config.cpp`
- [Command-line parser](https://github.com/ariya/phantomjs/blob/master/src/qcommandline/qcommandline.cpp): `src/qcommandline/qcommandline.cpp`

### Web automation

- [WebPage engine](https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp): `src/webpage.cpp`
- [Page JS API](https://github.com/ariya/phantomjs/blob/master/src/modules/webpage.js): `src/modules/webpage.js`
- [Network access](https://github.com/ariya/phantomjs/blob/master/src/networkaccessmanager.cpp): `src/networkaccessmanager.cpp`
- [Cookie jar](https://github.com/ariya/phantomjs/blob/master/src/cookiejar.cpp): `src/cookiejar.cpp`
- [Web server API](https://github.com/ariya/phantomjs/blob/master/src/webserver.cpp): `src/webserver.cpp`
- [HTTP server engine](https://github.com/ariya/phantomjs/blob/master/src/mongoose/mongoose.c): `src/mongoose/mongoose.c`

### Script APIs

- [Child processes](https://github.com/ariya/phantomjs/blob/master/src/childprocess.cpp): `src/childprocess.cpp`
- [Child process API](https://github.com/ariya/phantomjs/blob/master/src/modules/child_process.js): `src/modules/child_process.js`
- [Filesystem API](https://github.com/ariya/phantomjs/blob/master/src/filesystem.cpp): `src/filesystem.cpp`
- [Filesystem JS API](https://github.com/ariya/phantomjs/blob/master/src/modules/fs.js): `src/modules/fs.js`
- [System API](https://github.com/ariya/phantomjs/blob/master/src/system.cpp): `src/system.cpp`
- [System JS API](https://github.com/ariya/phantomjs/blob/master/src/modules/system.js): `src/modules/system.js`
- [Cookie JS API](https://github.com/ariya/phantomjs/blob/master/src/modules/cookiejar.js): `src/modules/cookiejar.js`
- [Interactive REPL](https://github.com/ariya/phantomjs/blob/master/src/repl.cpp): `src/repl.cpp`

### Runtime services

- [Terminal support](https://github.com/ariya/phantomjs/blob/master/src/terminal.cpp): `src/terminal.cpp`
- [Text encoding](https://github.com/ariya/phantomjs/blob/master/src/encoding.cpp): `src/encoding.cpp`
- [Callback support](https://github.com/ariya/phantomjs/blob/master/src/callback.cpp): `src/callback.cpp`

### Other components

- **Script user**

## Connections

- Script user → Executable entry: runs script
- Executable entry → Phantom controller: starts (evidence: `src/main.cpp`)
- Phantom controller → WebPage engine: creates pages (evidence: `src/phantom.cpp`)
- Phantom controller → Runtime configuration: uses settings (evidence: `src/phantom.cpp`)
- Phantom controller → Child processes: creates (evidence: `src/phantom.cpp`)
- Phantom controller → System API: uses (evidence: `src/phantom.cpp`)
- Phantom controller → Web server API: creates (evidence: `src/phantom.cpp`)
- Phantom controller → Cookie jar: creates (evidence: `src/phantom.cpp`)
- WebPage engine → Network access: uses (evidence: `src/webpage.cpp`)
- Network access → Cookie jar: uses (evidence: `src/networkaccessmanager.cpp`)
- Web server API → HTTP server engine: starts server (evidence: `src/webserver.cpp`)
- Phantom controller → Interactive REPL: uses (evidence: `src/phantom.cpp`)
- Interactive REPL → Terminal support: uses
- Child process API → Child processes: wraps
- Page JS API → WebPage engine: wraps
- Filesystem JS API → Filesystem API: wraps
- System JS API → System API: wraps
- Cookie JS API → Cookie jar: wraps
