devlens-mcp 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -21
- package/docs/setup-guide.md +11 -31
- package/package.json +1 -1
- package/.claude/settings.json +0 -12
- package/.claude/settings.local.json +0 -17
- package/bin/cli.ts +0 -22
- package/bin/register.ts +0 -96
- package/dist/src/config/devlens-config.d.ts +0 -92
- package/dist/src/config/devlens-config.d.ts.map +0 -1
- package/dist/src/config/devlens-config.js +0 -70
- package/dist/src/config/devlens-config.js.map +0 -1
- package/dist/src/index.d.ts +0 -35
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -8
- package/dist/src/index.js.map +0 -1
- package/dist/src/metro/cdp-client.d.ts +0 -48
- package/dist/src/metro/cdp-client.d.ts.map +0 -1
- package/dist/src/metro/cdp-client.js +0 -127
- package/dist/src/metro/cdp-client.js.map +0 -1
- package/dist/src/metro/log-collector.d.ts +0 -30
- package/dist/src/metro/log-collector.d.ts.map +0 -1
- package/dist/src/metro/log-collector.js +0 -114
- package/dist/src/metro/log-collector.js.map +0 -1
- package/dist/src/metro/metro-bridge.d.ts +0 -56
- package/dist/src/metro/metro-bridge.d.ts.map +0 -1
- package/dist/src/metro/metro-bridge.js +0 -255
- package/dist/src/metro/metro-bridge.js.map +0 -1
- package/dist/src/metro/network-inspector.d.ts +0 -34
- package/dist/src/metro/network-inspector.d.ts.map +0 -1
- package/dist/src/metro/network-inspector.js +0 -100
- package/dist/src/metro/network-inspector.js.map +0 -1
- package/dist/src/platform/android/adb.d.ts +0 -50
- package/dist/src/platform/android/adb.d.ts.map +0 -1
- package/dist/src/platform/android/adb.js +0 -137
- package/dist/src/platform/android/adb.js.map +0 -1
- package/dist/src/platform/android/android-device.d.ts +0 -21
- package/dist/src/platform/android/android-device.d.ts.map +0 -1
- package/dist/src/platform/android/android-device.js +0 -94
- package/dist/src/platform/android/android-device.js.map +0 -1
- package/dist/src/platform/android/ui-automator.d.ts +0 -17
- package/dist/src/platform/android/ui-automator.d.ts.map +0 -1
- package/dist/src/platform/android/ui-automator.js +0 -126
- package/dist/src/platform/android/ui-automator.js.map +0 -1
- package/dist/src/platform/device-manager.d.ts +0 -28
- package/dist/src/platform/device-manager.d.ts.map +0 -1
- package/dist/src/platform/device-manager.js +0 -185
- package/dist/src/platform/device-manager.js.map +0 -1
- package/dist/src/platform/device.d.ts +0 -86
- package/dist/src/platform/device.d.ts.map +0 -1
- package/dist/src/platform/device.js +0 -7
- package/dist/src/platform/device.js.map +0 -1
- package/dist/src/platform/ios/accessibility.d.ts +0 -17
- package/dist/src/platform/ios/accessibility.d.ts.map +0 -1
- package/dist/src/platform/ios/accessibility.js +0 -159
- package/dist/src/platform/ios/accessibility.js.map +0 -1
- package/dist/src/platform/ios/ios-device.d.ts +0 -22
- package/dist/src/platform/ios/ios-device.d.ts.map +0 -1
- package/dist/src/platform/ios/ios-device.js +0 -97
- package/dist/src/platform/ios/ios-device.js.map +0 -1
- package/dist/src/platform/ios/simctl.d.ts +0 -54
- package/dist/src/platform/ios/simctl.d.ts.map +0 -1
- package/dist/src/platform/ios/simctl.js +0 -192
- package/dist/src/platform/ios/simctl.js.map +0 -1
- package/dist/src/server.d.ts +0 -3
- package/dist/src/server.d.ts.map +0 -1
- package/dist/src/server.js +0 -176
- package/dist/src/server.js.map +0 -1
- package/dist/src/snapshot/formatter.d.ts +0 -18
- package/dist/src/snapshot/formatter.d.ts.map +0 -1
- package/dist/src/snapshot/formatter.js +0 -86
- package/dist/src/snapshot/formatter.js.map +0 -1
- package/dist/src/snapshot/ref-registry.d.ts +0 -67
- package/dist/src/snapshot/ref-registry.d.ts.map +0 -1
- package/dist/src/snapshot/ref-registry.js +0 -169
- package/dist/src/snapshot/ref-registry.js.map +0 -1
- package/dist/src/snapshot/snapshot-differ.d.ts +0 -57
- package/dist/src/snapshot/snapshot-differ.d.ts.map +0 -1
- package/dist/src/snapshot/snapshot-differ.js +0 -153
- package/dist/src/snapshot/snapshot-differ.js.map +0 -1
- package/dist/src/tools/app-tools.d.ts +0 -71
- package/dist/src/tools/app-tools.d.ts.map +0 -1
- package/dist/src/tools/app-tools.js +0 -97
- package/dist/src/tools/app-tools.js.map +0 -1
- package/dist/src/tools/device-tools.d.ts +0 -53
- package/dist/src/tools/device-tools.d.ts.map +0 -1
- package/dist/src/tools/device-tools.js +0 -86
- package/dist/src/tools/device-tools.js.map +0 -1
- package/dist/src/tools/ds-tools.d.ts +0 -65
- package/dist/src/tools/ds-tools.d.ts.map +0 -1
- package/dist/src/tools/ds-tools.js +0 -314
- package/dist/src/tools/ds-tools.js.map +0 -1
- package/dist/src/tools/interaction-tools.d.ts +0 -248
- package/dist/src/tools/interaction-tools.d.ts.map +0 -1
- package/dist/src/tools/interaction-tools.js +0 -391
- package/dist/src/tools/interaction-tools.js.map +0 -1
- package/dist/src/tools/metro-tools.d.ts +0 -115
- package/dist/src/tools/metro-tools.d.ts.map +0 -1
- package/dist/src/tools/metro-tools.js +0 -270
- package/dist/src/tools/metro-tools.js.map +0 -1
- package/dist/src/tools/navigation-tools.d.ts +0 -36
- package/dist/src/tools/navigation-tools.d.ts.map +0 -1
- package/dist/src/tools/navigation-tools.js +0 -60
- package/dist/src/tools/navigation-tools.js.map +0 -1
- package/dist/src/tools/screenshot-tools.d.ts +0 -298
- package/dist/src/tools/screenshot-tools.d.ts.map +0 -1
- package/dist/src/tools/screenshot-tools.js +0 -565
- package/dist/src/tools/screenshot-tools.js.map +0 -1
- package/dist/src/tools/snapshot-tools.d.ts +0 -161
- package/dist/src/tools/snapshot-tools.d.ts.map +0 -1
- package/dist/src/tools/snapshot-tools.js +0 -479
- package/dist/src/tools/snapshot-tools.js.map +0 -1
- package/dist/src/utils/image-preprocess.d.ts +0 -49
- package/dist/src/utils/image-preprocess.d.ts.map +0 -1
- package/dist/src/utils/image-preprocess.js +0 -322
- package/dist/src/utils/image-preprocess.js.map +0 -1
- package/dist/src/utils/retry.d.ts +0 -21
- package/dist/src/utils/retry.d.ts.map +0 -1
- package/dist/src/utils/retry.js +0 -33
- package/dist/src/utils/retry.js.map +0 -1
- package/dist/src/visual/comparator.d.ts +0 -51
- package/dist/src/visual/comparator.d.ts.map +0 -1
- package/dist/src/visual/comparator.js +0 -119
- package/dist/src/visual/comparator.js.map +0 -1
- package/dist/src/visual/layout-analyzer.d.ts +0 -64
- package/dist/src/visual/layout-analyzer.d.ts.map +0 -1
- package/dist/src/visual/layout-analyzer.js +0 -198
- package/dist/src/visual/layout-analyzer.js.map +0 -1
- package/dist/src/visual/screenshot.d.ts +0 -17
- package/dist/src/visual/screenshot.d.ts.map +0 -1
- package/dist/src/visual/screenshot.js +0 -39
- package/dist/src/visual/screenshot.js.map +0 -1
- package/src/config/devlens-config.ts +0 -76
- package/src/index.ts +0 -5
- package/src/metro/cdp-client.ts +0 -160
- package/src/metro/log-collector.ts +0 -137
- package/src/metro/metro-bridge.ts +0 -307
- package/src/metro/network-inspector.ts +0 -134
- package/src/platform/android/adb.ts +0 -200
- package/src/platform/android/android-device.ts +0 -116
- package/src/platform/android/ui-automator.ts +0 -141
- package/src/platform/device-manager.ts +0 -229
- package/src/platform/device.ts +0 -110
- package/src/platform/ios/accessibility.ts +0 -189
- package/src/platform/ios/ios-device.ts +0 -116
- package/src/platform/ios/simctl.ts +0 -244
- package/src/server.ts +0 -228
- package/src/snapshot/formatter.ts +0 -102
- package/src/snapshot/ref-registry.ts +0 -230
- package/src/snapshot/snapshot-differ.ts +0 -220
- package/src/tools/app-tools.ts +0 -111
- package/src/tools/device-tools.ts +0 -96
- package/src/tools/ds-tools.ts +0 -395
- package/src/tools/interaction-tools.ts +0 -467
- package/src/tools/metro-tools.ts +0 -320
- package/src/tools/navigation-tools.ts +0 -71
- package/src/tools/screenshot-tools.ts +0 -698
- package/src/tools/snapshot-tools.ts +0 -585
- package/src/utils/image-preprocess.ts +0 -430
- package/src/utils/retry.ts +0 -51
- package/src/visual/comparator.ts +0 -191
- package/src/visual/layout-analyzer.ts +0 -283
- package/src/visual/screenshot.ts +0 -49
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import type { CdpClient, CdpMessage } from "./cdp-client.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Collects console.log/warn/error messages from the React Native app
|
|
5
|
-
* via the CDP Console and Runtime domains.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export interface LogEntry {
|
|
9
|
-
level: "log" | "info" | "warn" | "error" | "debug";
|
|
10
|
-
message: string;
|
|
11
|
-
timestamp: number;
|
|
12
|
-
source?: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class LogCollector {
|
|
16
|
-
private logs: LogEntry[] = [];
|
|
17
|
-
private maxLogs: number = 500;
|
|
18
|
-
|
|
19
|
-
constructor(private cdp: CdpClient) {}
|
|
20
|
-
|
|
21
|
-
/** Start listening for console messages */
|
|
22
|
-
start(): void {
|
|
23
|
-
// Listen for Console.messageAdded (older CDP)
|
|
24
|
-
this.cdp.on("Console.messageAdded", (msg: CdpMessage) => {
|
|
25
|
-
const entry = msg.params?.message;
|
|
26
|
-
if (entry) {
|
|
27
|
-
this.addLog({
|
|
28
|
-
level: this.normalizeLevel(entry.level),
|
|
29
|
-
message: entry.text || "",
|
|
30
|
-
timestamp: Date.now(),
|
|
31
|
-
source: entry.source,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Listen for Runtime.consoleAPICalled (newer CDP)
|
|
37
|
-
this.cdp.on("Runtime.consoleAPICalled", (msg: CdpMessage) => {
|
|
38
|
-
const params = msg.params;
|
|
39
|
-
if (params) {
|
|
40
|
-
const args = params.args || [];
|
|
41
|
-
const message = args
|
|
42
|
-
.map((arg: any) => {
|
|
43
|
-
if (arg.type === "string") return arg.value;
|
|
44
|
-
if (arg.type === "number") return String(arg.value);
|
|
45
|
-
if (arg.type === "boolean") return String(arg.value);
|
|
46
|
-
if (arg.type === "undefined") return "undefined";
|
|
47
|
-
if (arg.type === "object" && arg.value)
|
|
48
|
-
return JSON.stringify(arg.value);
|
|
49
|
-
if (arg.description) return arg.description;
|
|
50
|
-
return String(arg.value ?? "");
|
|
51
|
-
})
|
|
52
|
-
.join(" ");
|
|
53
|
-
|
|
54
|
-
this.addLog({
|
|
55
|
-
level: this.normalizeLevel(params.type),
|
|
56
|
-
message,
|
|
57
|
-
timestamp: params.timestamp || Date.now(),
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Listen for Runtime.exceptionThrown
|
|
63
|
-
this.cdp.on("Runtime.exceptionThrown", (msg: CdpMessage) => {
|
|
64
|
-
const details = msg.params?.exceptionDetails;
|
|
65
|
-
if (details) {
|
|
66
|
-
this.addLog({
|
|
67
|
-
level: "error",
|
|
68
|
-
message:
|
|
69
|
-
details.text ||
|
|
70
|
-
details.exception?.description ||
|
|
71
|
-
"Unknown exception",
|
|
72
|
-
timestamp: Date.now(),
|
|
73
|
-
source: "exception",
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Get collected logs, optionally filtered by level and time */
|
|
80
|
-
getLogs(options?: {
|
|
81
|
-
level?: "log" | "info" | "warn" | "error" | "debug";
|
|
82
|
-
since?: number;
|
|
83
|
-
limit?: number;
|
|
84
|
-
}): LogEntry[] {
|
|
85
|
-
let filtered = this.logs;
|
|
86
|
-
|
|
87
|
-
if (options?.level) {
|
|
88
|
-
const levelOrder = { debug: 0, log: 1, info: 2, warn: 3, error: 4 };
|
|
89
|
-
const minLevel = levelOrder[options.level] ?? 0;
|
|
90
|
-
filtered = filtered.filter(
|
|
91
|
-
(log) => (levelOrder[log.level] ?? 0) >= minLevel
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (options?.since) {
|
|
96
|
-
filtered = filtered.filter((log) => log.timestamp >= options.since!);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (options?.limit) {
|
|
100
|
-
filtered = filtered.slice(-options.limit);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return filtered;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Clear all collected logs */
|
|
107
|
-
clear(): void {
|
|
108
|
-
this.logs = [];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private addLog(entry: LogEntry): void {
|
|
112
|
-
this.logs.push(entry);
|
|
113
|
-
// Keep buffer bounded
|
|
114
|
-
if (this.logs.length > this.maxLogs) {
|
|
115
|
-
this.logs = this.logs.slice(-Math.floor(this.maxLogs * 0.8));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private normalizeLevel(
|
|
120
|
-
level: string
|
|
121
|
-
): "log" | "info" | "warn" | "error" | "debug" {
|
|
122
|
-
switch (level?.toLowerCase()) {
|
|
123
|
-
case "warning":
|
|
124
|
-
case "warn":
|
|
125
|
-
return "warn";
|
|
126
|
-
case "error":
|
|
127
|
-
return "error";
|
|
128
|
-
case "debug":
|
|
129
|
-
case "verbose":
|
|
130
|
-
return "debug";
|
|
131
|
-
case "info":
|
|
132
|
-
return "info";
|
|
133
|
-
default:
|
|
134
|
-
return "log";
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import { CdpClient } from "./cdp-client.js";
|
|
2
|
-
import { LogCollector } from "./log-collector.js";
|
|
3
|
-
import { NetworkInspector } from "./network-inspector.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Metro Bridge — manages the connection to Metro bundler's debugger
|
|
7
|
-
* and provides access to console logs, network requests, and JS execution.
|
|
8
|
-
*
|
|
9
|
-
* Metro/Hermes exposes CDP endpoints. To discover available pages:
|
|
10
|
-
* GET http://localhost:{port}/json
|
|
11
|
-
*
|
|
12
|
-
* The main debugger endpoint is typically:
|
|
13
|
-
* ws://localhost:{port}/inspector/device?device=0&page=-1
|
|
14
|
-
*
|
|
15
|
-
* For newer React Native (0.76+) with the new debugger:
|
|
16
|
-
* ws://localhost:{port}/inspector/device?device=0&page=1
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
export interface MetroStatus {
|
|
20
|
-
/** Whether the Metro packager process is running and responding */
|
|
21
|
-
running: boolean;
|
|
22
|
-
/** The port being checked */
|
|
23
|
-
port: number;
|
|
24
|
-
/** Raw response from /status endpoint (e.g., "packager-status:running") */
|
|
25
|
-
packagerStatus: string | null;
|
|
26
|
-
/** Whether a CDP WebSocket connection is currently active */
|
|
27
|
-
cdpConnected: boolean;
|
|
28
|
-
/** Error message if Metro is unreachable */
|
|
29
|
-
error?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class MetroBridge {
|
|
33
|
-
private cdp: CdpClient | null = null;
|
|
34
|
-
private _logCollector: LogCollector | null = null;
|
|
35
|
-
private _networkInspector: NetworkInspector | null = null;
|
|
36
|
-
private _connected: boolean = false;
|
|
37
|
-
|
|
38
|
-
constructor(private port: number = 8081) {}
|
|
39
|
-
|
|
40
|
-
/** Connect to Metro bundler's debugger */
|
|
41
|
-
async connect(): Promise<void> {
|
|
42
|
-
if (this._connected) return;
|
|
43
|
-
|
|
44
|
-
// Try to discover the debugger endpoint
|
|
45
|
-
const endpoint = await this.discoverEndpoint();
|
|
46
|
-
this.cdp = new CdpClient(endpoint);
|
|
47
|
-
await this.cdp.connect();
|
|
48
|
-
|
|
49
|
-
// Initialize collectors
|
|
50
|
-
this._logCollector = new LogCollector(this.cdp);
|
|
51
|
-
this._networkInspector = new NetworkInspector(this.cdp);
|
|
52
|
-
|
|
53
|
-
// Enable CDP domains
|
|
54
|
-
await this.cdp.send("Runtime.enable");
|
|
55
|
-
await this.cdp.send("Console.enable").catch(() => {
|
|
56
|
-
// Console domain might not be available in all environments
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Start collecting
|
|
60
|
-
this._logCollector.start();
|
|
61
|
-
this._networkInspector.start();
|
|
62
|
-
|
|
63
|
-
this._connected = true;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Disconnect from Metro */
|
|
67
|
-
disconnect(): void {
|
|
68
|
-
if (this.cdp) {
|
|
69
|
-
this.cdp.close();
|
|
70
|
-
this.cdp = null;
|
|
71
|
-
}
|
|
72
|
-
this._logCollector = null;
|
|
73
|
-
this._networkInspector = null;
|
|
74
|
-
this._connected = false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Check if connected to Metro */
|
|
78
|
-
get connected(): boolean {
|
|
79
|
-
return this._connected && (this.cdp?.isConnected() ?? false);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Get the log collector */
|
|
83
|
-
get logCollector(): LogCollector {
|
|
84
|
-
if (!this._logCollector) {
|
|
85
|
-
throw new Error("Not connected to Metro. Call connect() first.");
|
|
86
|
-
}
|
|
87
|
-
return this._logCollector;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Get the network inspector */
|
|
91
|
-
get networkInspector(): NetworkInspector {
|
|
92
|
-
if (!this._networkInspector) {
|
|
93
|
-
throw new Error("Not connected to Metro. Call connect() first.");
|
|
94
|
-
}
|
|
95
|
-
return this._networkInspector;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Execute JavaScript in the React Native context */
|
|
99
|
-
async evaluate(expression: string): Promise<any> {
|
|
100
|
-
if (!this.cdp || !this._connected) {
|
|
101
|
-
throw new Error("Not connected to Metro.");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const result = await this.cdp.send("Runtime.evaluate", {
|
|
105
|
-
expression,
|
|
106
|
-
returnByValue: true,
|
|
107
|
-
awaitPromise: true,
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
if (result?.exceptionDetails) {
|
|
111
|
-
throw new Error(
|
|
112
|
-
`JS Error: ${result.exceptionDetails.text || result.exceptionDetails.exception?.description}`
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return result?.result?.value;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Trigger a hot reload / fast refresh */
|
|
120
|
-
async hotReload(): Promise<void> {
|
|
121
|
-
try {
|
|
122
|
-
// Method 1: Use the Metro reload endpoint
|
|
123
|
-
const http = await import("http");
|
|
124
|
-
await new Promise<void>((resolve, reject) => {
|
|
125
|
-
const req = http.request(
|
|
126
|
-
{
|
|
127
|
-
hostname: "localhost",
|
|
128
|
-
port: this.port,
|
|
129
|
-
path: "/reload",
|
|
130
|
-
method: "POST",
|
|
131
|
-
},
|
|
132
|
-
(res) => {
|
|
133
|
-
res.resume();
|
|
134
|
-
resolve();
|
|
135
|
-
}
|
|
136
|
-
);
|
|
137
|
-
req.on("error", reject);
|
|
138
|
-
req.setTimeout(5000, () => {
|
|
139
|
-
req.destroy();
|
|
140
|
-
reject(new Error("Reload request timeout"));
|
|
141
|
-
});
|
|
142
|
-
req.end();
|
|
143
|
-
});
|
|
144
|
-
} catch {
|
|
145
|
-
// Method 2: Fallback - evaluate reload command via CDP
|
|
146
|
-
if (this.cdp && this._connected) {
|
|
147
|
-
await this.cdp.send("Runtime.evaluate", {
|
|
148
|
-
expression:
|
|
149
|
-
'if (typeof __DEV__ !== "undefined" && __DEV__) { const DevSettings = require("react-native/Libraries/Utilities/DevSettings"); DevSettings.reload(); }',
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Get the React component tree by evaluating JS in the RN context */
|
|
156
|
-
async getComponentTree(maxDepth: number = 10): Promise<string> {
|
|
157
|
-
if (!this.cdp || !this._connected) {
|
|
158
|
-
throw new Error("Not connected to Metro.");
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Inject a helper that traverses the React fiber tree
|
|
162
|
-
const script = `
|
|
163
|
-
(function() {
|
|
164
|
-
try {
|
|
165
|
-
// Find the root fiber
|
|
166
|
-
const roots = [];
|
|
167
|
-
const fiberRoots = document?.querySelectorAll?.('[data-reactroot]') || [];
|
|
168
|
-
|
|
169
|
-
// For React Native, we need to find the fiber root differently
|
|
170
|
-
// The __REACT_DEVTOOLS_GLOBAL_HOOK__ is available when DevTools is enabled
|
|
171
|
-
const hook = globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
172
|
-
if (!hook || !hook.renderers) {
|
|
173
|
-
return "React DevTools hook not available. Ensure the app is running in __DEV__ mode.";
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const renderers = Array.from(hook.renderers.values());
|
|
177
|
-
if (renderers.length === 0) {
|
|
178
|
-
return "No React renderers found.";
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Get fiber roots from the hook
|
|
182
|
-
const fiberRootSet = hook.getFiberRoots?.(renderers[0].rendererID || 1);
|
|
183
|
-
if (!fiberRootSet || fiberRootSet.size === 0) {
|
|
184
|
-
return "No fiber roots found. Is a React Native app running?";
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function traverseFiber(fiber, depth, maxDepth) {
|
|
188
|
-
if (!fiber || depth > maxDepth) return null;
|
|
189
|
-
|
|
190
|
-
const name = fiber.type?.displayName || fiber.type?.name || fiber.type || 'Unknown';
|
|
191
|
-
const props = {};
|
|
192
|
-
|
|
193
|
-
if (fiber.memoizedProps) {
|
|
194
|
-
for (const [key, value] of Object.entries(fiber.memoizedProps)) {
|
|
195
|
-
if (key === 'children') continue;
|
|
196
|
-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
197
|
-
props[key] = value;
|
|
198
|
-
} else if (typeof value === 'function') {
|
|
199
|
-
props[key] = '[function]';
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const children = [];
|
|
205
|
-
let child = fiber.child;
|
|
206
|
-
while (child) {
|
|
207
|
-
const result = traverseFiber(child, depth + 1, maxDepth);
|
|
208
|
-
if (result) children.push(result);
|
|
209
|
-
child = child.sibling;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Skip internal React types
|
|
213
|
-
if (typeof name === 'number' || name === 'Unknown') {
|
|
214
|
-
return children.length === 1 ? children[0] : children.length > 0 ? { type: 'Fragment', children } : null;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return {
|
|
218
|
-
type: String(name),
|
|
219
|
-
props: Object.keys(props).length > 0 ? props : undefined,
|
|
220
|
-
children: children.length > 0 ? children : undefined,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const root = Array.from(fiberRootSet)[0];
|
|
225
|
-
const tree = traverseFiber(root.current, 0, ${maxDepth});
|
|
226
|
-
return JSON.stringify(tree, null, 2);
|
|
227
|
-
} catch (e) {
|
|
228
|
-
return "Error getting component tree: " + e.message;
|
|
229
|
-
}
|
|
230
|
-
})()
|
|
231
|
-
`;
|
|
232
|
-
|
|
233
|
-
const result = await this.cdp.send("Runtime.evaluate", {
|
|
234
|
-
expression: script,
|
|
235
|
-
returnByValue: true,
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
return result?.result?.value || "Could not retrieve component tree";
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/** Proactively check if Metro bundler is running and healthy */
|
|
242
|
-
async getStatus(): Promise<MetroStatus> {
|
|
243
|
-
const status: MetroStatus = {
|
|
244
|
-
running: false,
|
|
245
|
-
port: this.port,
|
|
246
|
-
packagerStatus: null,
|
|
247
|
-
cdpConnected: this.connected,
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
try {
|
|
251
|
-
const controller = new AbortController();
|
|
252
|
-
const timeout = setTimeout(() => controller.abort(), 3000);
|
|
253
|
-
const response = await fetch(`http://localhost:${this.port}/status`, {
|
|
254
|
-
signal: controller.signal,
|
|
255
|
-
});
|
|
256
|
-
clearTimeout(timeout);
|
|
257
|
-
const body = await response.text();
|
|
258
|
-
status.packagerStatus = body.trim();
|
|
259
|
-
status.running = body.includes("packager-status:running");
|
|
260
|
-
} catch (err: any) {
|
|
261
|
-
status.error = `Cannot reach Metro on port ${this.port}: ${err.message}`;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return status;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/** Discover the debugger WebSocket endpoint */
|
|
268
|
-
private async discoverEndpoint(): Promise<string> {
|
|
269
|
-
const http = await import("http");
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
// Query Metro's page list
|
|
273
|
-
const pages = await new Promise<any[]>((resolve, reject) => {
|
|
274
|
-
http
|
|
275
|
-
.get(`http://localhost:${this.port}/json`, (res) => {
|
|
276
|
-
let data = "";
|
|
277
|
-
res.on("data", (chunk) => (data += chunk));
|
|
278
|
-
res.on("end", () => {
|
|
279
|
-
try {
|
|
280
|
-
resolve(JSON.parse(data));
|
|
281
|
-
} catch {
|
|
282
|
-
resolve([]);
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
})
|
|
286
|
-
.on("error", reject);
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
// Find the best debugger page
|
|
290
|
-
const debuggerPage = pages.find(
|
|
291
|
-
(p) =>
|
|
292
|
-
p.title?.includes("Hermes") ||
|
|
293
|
-
p.title?.includes("React Native") ||
|
|
294
|
-
p.webSocketDebuggerUrl
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
if (debuggerPage?.webSocketDebuggerUrl) {
|
|
298
|
-
return debuggerPage.webSocketDebuggerUrl;
|
|
299
|
-
}
|
|
300
|
-
} catch {
|
|
301
|
-
// Metro might not support /json endpoint
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// Fallback to default endpoint
|
|
305
|
-
return `ws://localhost:${this.port}/inspector/device?device=0&page=-1`;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import type { CdpClient, CdpMessage } from "./cdp-client.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Intercepts network requests from the React Native app via CDP's Network domain.
|
|
5
|
-
* Captures fetch/XHR requests, their responses, and timing information.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export interface NetworkRequest {
|
|
9
|
-
id: string;
|
|
10
|
-
url: string;
|
|
11
|
-
method: string;
|
|
12
|
-
status?: number;
|
|
13
|
-
statusText?: string;
|
|
14
|
-
type?: string;
|
|
15
|
-
startTime: number;
|
|
16
|
-
endTime?: number;
|
|
17
|
-
duration?: number;
|
|
18
|
-
requestHeaders?: Record<string, string>;
|
|
19
|
-
responseHeaders?: Record<string, string>;
|
|
20
|
-
size?: number;
|
|
21
|
-
error?: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class NetworkInspector {
|
|
25
|
-
private requests: Map<string, NetworkRequest> = new Map();
|
|
26
|
-
private maxRequests: number = 200;
|
|
27
|
-
|
|
28
|
-
constructor(private cdp: CdpClient) {}
|
|
29
|
-
|
|
30
|
-
/** Start intercepting network requests */
|
|
31
|
-
start(): void {
|
|
32
|
-
// Enable Network domain
|
|
33
|
-
this.cdp.send("Network.enable").catch(() => {
|
|
34
|
-
// Network domain might not be available
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Request started
|
|
38
|
-
this.cdp.on("Network.requestWillBeSent", (msg: CdpMessage) => {
|
|
39
|
-
const params = msg.params;
|
|
40
|
-
if (!params) return;
|
|
41
|
-
|
|
42
|
-
const request: NetworkRequest = {
|
|
43
|
-
id: params.requestId,
|
|
44
|
-
url: params.request?.url || "",
|
|
45
|
-
method: params.request?.method || "GET",
|
|
46
|
-
type: params.type,
|
|
47
|
-
startTime: Date.now(),
|
|
48
|
-
requestHeaders: params.request?.headers,
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
this.requests.set(params.requestId, request);
|
|
52
|
-
this.trimRequests();
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
// Response received
|
|
56
|
-
this.cdp.on("Network.responseReceived", (msg: CdpMessage) => {
|
|
57
|
-
const params = msg.params;
|
|
58
|
-
if (!params) return;
|
|
59
|
-
|
|
60
|
-
const request = this.requests.get(params.requestId);
|
|
61
|
-
if (request) {
|
|
62
|
-
request.status = params.response?.status;
|
|
63
|
-
request.statusText = params.response?.statusText;
|
|
64
|
-
request.responseHeaders = params.response?.headers;
|
|
65
|
-
request.endTime = Date.now();
|
|
66
|
-
request.duration = request.endTime - request.startTime;
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Loading finished
|
|
71
|
-
this.cdp.on("Network.loadingFinished", (msg: CdpMessage) => {
|
|
72
|
-
const params = msg.params;
|
|
73
|
-
if (!params) return;
|
|
74
|
-
|
|
75
|
-
const request = this.requests.get(params.requestId);
|
|
76
|
-
if (request) {
|
|
77
|
-
request.size = params.encodedDataLength;
|
|
78
|
-
if (!request.endTime) {
|
|
79
|
-
request.endTime = Date.now();
|
|
80
|
-
request.duration = request.endTime - request.startTime;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// Loading failed
|
|
86
|
-
this.cdp.on("Network.loadingFailed", (msg: CdpMessage) => {
|
|
87
|
-
const params = msg.params;
|
|
88
|
-
if (!params) return;
|
|
89
|
-
|
|
90
|
-
const request = this.requests.get(params.requestId);
|
|
91
|
-
if (request) {
|
|
92
|
-
request.error = params.errorText || "Request failed";
|
|
93
|
-
request.endTime = Date.now();
|
|
94
|
-
request.duration = request.endTime - request.startTime;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Get all captured network requests, optionally filtered by URL pattern */
|
|
100
|
-
getRequests(urlPattern?: string): NetworkRequest[] {
|
|
101
|
-
let requests = Array.from(this.requests.values());
|
|
102
|
-
|
|
103
|
-
if (urlPattern) {
|
|
104
|
-
const pattern = urlPattern.toLowerCase();
|
|
105
|
-
requests = requests.filter((r) =>
|
|
106
|
-
r.url.toLowerCase().includes(pattern)
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Sort by start time (newest first)
|
|
111
|
-
return requests.sort((a, b) => b.startTime - a.startTime);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Clear all captured requests */
|
|
115
|
-
clear(): void {
|
|
116
|
-
this.requests.clear();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private trimRequests(): void {
|
|
120
|
-
if (this.requests.size > this.maxRequests) {
|
|
121
|
-
// Remove oldest entries
|
|
122
|
-
const entries = Array.from(this.requests.entries()).sort(
|
|
123
|
-
(a, b) => a[1].startTime - b[1].startTime
|
|
124
|
-
);
|
|
125
|
-
const toRemove = entries.slice(
|
|
126
|
-
0,
|
|
127
|
-
entries.length - Math.floor(this.maxRequests * 0.8)
|
|
128
|
-
);
|
|
129
|
-
for (const [id] of toRemove) {
|
|
130
|
-
this.requests.delete(id);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|