@mt0926/node-network-devtools 0.1.0

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.
Files changed (73) hide show
  1. package/BUILD.md +204 -0
  2. package/LICENSE +21 -0
  3. package/README.md +310 -0
  4. package/README.zh-CN.md +310 -0
  5. package/dist/esm/adapters/nextjs.js +123 -0
  6. package/dist/esm/adapters/nextjs.js.map +1 -0
  7. package/dist/esm/cdp/cdp-bridge.js +312 -0
  8. package/dist/esm/cdp/cdp-bridge.js.map +1 -0
  9. package/dist/esm/cli.js +203 -0
  10. package/dist/esm/cli.js.map +1 -0
  11. package/dist/esm/config.js +136 -0
  12. package/dist/esm/config.js.map +1 -0
  13. package/dist/esm/context/context-manager.js +126 -0
  14. package/dist/esm/context/context-manager.js.map +1 -0
  15. package/dist/esm/gui/browser-launcher.js +165 -0
  16. package/dist/esm/gui/browser-launcher.js.map +1 -0
  17. package/dist/esm/gui/event-bridge.js +192 -0
  18. package/dist/esm/gui/event-bridge.js.map +1 -0
  19. package/dist/esm/gui/port-utils.js +80 -0
  20. package/dist/esm/gui/port-utils.js.map +1 -0
  21. package/dist/esm/gui/server.js +227 -0
  22. package/dist/esm/gui/server.js.map +1 -0
  23. package/dist/esm/gui/websocket-hub.js +326 -0
  24. package/dist/esm/gui/websocket-hub.js.map +1 -0
  25. package/dist/esm/index.js +90 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/esm/interceptors/http-patcher.js +203 -0
  28. package/dist/esm/interceptors/http-patcher.js.map +1 -0
  29. package/dist/esm/interceptors/undici-patcher.js +324 -0
  30. package/dist/esm/interceptors/undici-patcher.js.map +1 -0
  31. package/dist/esm/register.js +132 -0
  32. package/dist/esm/register.js.map +1 -0
  33. package/dist/esm/store/ring-buffer.js +236 -0
  34. package/dist/esm/store/ring-buffer.js.map +1 -0
  35. package/dist/esm/test-setup.js +7 -0
  36. package/dist/esm/test-setup.js.map +1 -0
  37. package/dist/gui/assets/index.css +1 -0
  38. package/dist/gui/assets/index.js +40 -0
  39. package/dist/gui/index.html +14 -0
  40. package/dist/types/adapters/nextjs.d.ts +80 -0
  41. package/dist/types/adapters/nextjs.d.ts.map +1 -0
  42. package/dist/types/cdp/cdp-bridge.d.ts +86 -0
  43. package/dist/types/cdp/cdp-bridge.d.ts.map +1 -0
  44. package/dist/types/cli.d.ts +8 -0
  45. package/dist/types/cli.d.ts.map +1 -0
  46. package/dist/types/config.d.ts +57 -0
  47. package/dist/types/config.d.ts.map +1 -0
  48. package/dist/types/context/context-manager.d.ts +96 -0
  49. package/dist/types/context/context-manager.d.ts.map +1 -0
  50. package/dist/types/gui/browser-launcher.d.ts +52 -0
  51. package/dist/types/gui/browser-launcher.d.ts.map +1 -0
  52. package/dist/types/gui/event-bridge.d.ts +36 -0
  53. package/dist/types/gui/event-bridge.d.ts.map +1 -0
  54. package/dist/types/gui/port-utils.d.ts +25 -0
  55. package/dist/types/gui/port-utils.d.ts.map +1 -0
  56. package/dist/types/gui/server.d.ts +50 -0
  57. package/dist/types/gui/server.d.ts.map +1 -0
  58. package/dist/types/gui/websocket-hub.d.ts +67 -0
  59. package/dist/types/gui/websocket-hub.d.ts.map +1 -0
  60. package/dist/types/index.d.ts +44 -0
  61. package/dist/types/index.d.ts.map +1 -0
  62. package/dist/types/interceptors/http-patcher.d.ts +32 -0
  63. package/dist/types/interceptors/http-patcher.d.ts.map +1 -0
  64. package/dist/types/interceptors/undici-patcher.d.ts +37 -0
  65. package/dist/types/interceptors/undici-patcher.d.ts.map +1 -0
  66. package/dist/types/register.d.ts +18 -0
  67. package/dist/types/register.d.ts.map +1 -0
  68. package/dist/types/store/ring-buffer.d.ts +148 -0
  69. package/dist/types/store/ring-buffer.d.ts.map +1 -0
  70. package/dist/types/test-setup.d.ts +7 -0
  71. package/dist/types/test-setup.d.ts.map +1 -0
  72. package/package.json +103 -0
  73. package/templates/instrumentation.ts +32 -0
package/BUILD.md ADDED
@@ -0,0 +1,204 @@
1
+ # 构建说明
2
+
3
+ ## 完整构建
4
+
5
+ 构建整个项目(包括 GUI):
6
+
7
+ ```bash
8
+ pnpm build
9
+ ```
10
+
11
+ 这会执行以下步骤:
12
+ 1. `pnpm build:esm` - 构建 ESM 模块
13
+ 2. `pnpm build:types` - 生成 TypeScript 类型定义
14
+ 3. `pnpm build:gui` - 构建 Web GUI 前端
15
+
16
+ ## 分步构建
17
+
18
+ ### 1. 构建核心模块
19
+
20
+ ```bash
21
+ pnpm build:esm
22
+ ```
23
+
24
+ 生成 ESM 格式的 JavaScript 文件到 `dist/esm/`
25
+
26
+ ### 2. 生成类型定义
27
+
28
+ ```bash
29
+ pnpm build:types
30
+ ```
31
+
32
+ 生成 TypeScript 类型定义文件到 `dist/types/`
33
+
34
+ ### 3. 构建 GUI
35
+
36
+ ```bash
37
+ pnpm build:gui
38
+ ```
39
+
40
+ 或者直接在 GUI 目录构建:
41
+
42
+ ```bash
43
+ cd packages/gui
44
+ pnpm build
45
+ ```
46
+
47
+ 生成 GUI 静态资源到 `dist/gui/`
48
+
49
+ ## 构建产物
50
+
51
+ 构建完成后,`dist` 目录结构如下:
52
+
53
+ ```
54
+ dist/
55
+ ├── esm/ # ESM 模块
56
+ │ ├── index.js
57
+ │ ├── cli.js
58
+ │ ├── register.js
59
+ │ └── ...
60
+ ├── types/ # TypeScript 类型定义
61
+ │ ├── index.d.ts
62
+ │ └── ...
63
+ └── gui/ # Web GUI 静态资源
64
+ ├── index.html
65
+ └── assets/
66
+ ├── index.css
67
+ └── index.js
68
+ ```
69
+
70
+ ## 开发模式
71
+
72
+ ### 开发核心模块
73
+
74
+ ```bash
75
+ # 监听模式(需要手动添加)
76
+ pnpm build:esm --watch
77
+ ```
78
+
79
+ ### 开发 GUI
80
+
81
+ ```bash
82
+ cd packages/gui
83
+ pnpm dev
84
+ ```
85
+
86
+ 这会启动 Vite 开发服务器,支持热更新。
87
+
88
+ ## 清理构建产物
89
+
90
+ ```bash
91
+ pnpm clean
92
+ ```
93
+
94
+ 这会删除整个 `dist` 目录。
95
+
96
+ ## 常见问题
97
+
98
+ ### Q: GUI 页面显示 404 错误?
99
+
100
+ A: 确保已经构建了 GUI:
101
+
102
+ ```bash
103
+ pnpm build:gui
104
+ ```
105
+
106
+ ### Q: 修改了 GUI 代码但没有生效?
107
+
108
+ A: 需要重新构建 GUI:
109
+
110
+ ```bash
111
+ cd packages/gui
112
+ pnpm build
113
+ ```
114
+
115
+ ### Q: 如何只构建核心模块而不构建 GUI?
116
+
117
+ A: 使用分步构建:
118
+
119
+ ```bash
120
+ pnpm build:esm
121
+ pnpm build:types
122
+ ```
123
+
124
+ ### Q: 构建失败怎么办?
125
+
126
+ A: 检查以下几点:
127
+ 1. 确保安装了所有依赖:`pnpm install`
128
+ 2. 确保 Node.js 版本 >= 18.0.0
129
+ 3. 清理后重新构建:`pnpm clean && pnpm build`
130
+
131
+ ## CI/CD 集成
132
+
133
+ 在 CI/CD 流程中,建议使用完整构建:
134
+
135
+ ```bash
136
+ # 安装依赖
137
+ pnpm install
138
+
139
+ # 完整构建
140
+ pnpm build
141
+
142
+ # 运行测试
143
+ pnpm test:all
144
+ ```
145
+
146
+ ## 发布前检查
147
+
148
+ 发布前确保:
149
+
150
+ 1. ✅ 所有代码已提交
151
+ 2. ✅ 运行完整构建:`pnpm build`
152
+ 3. ✅ 运行所有测试:`pnpm test:all`
153
+ 4. ✅ 检查 `dist` 目录包含所有必要文件
154
+ 5. ✅ 更新版本号
155
+ 6. ✅ 更新 CHANGELOG
156
+
157
+ ## 性能优化
158
+
159
+ ### 并行构建
160
+
161
+ 如果需要加速构建,可以并行执行:
162
+
163
+ ```bash
164
+ # 使用 npm-run-all(需要安装)
165
+ npm-run-all --parallel build:esm build:types build:gui
166
+ ```
167
+
168
+ ### 增量构建
169
+
170
+ TypeScript 支持增量构建,可以加快重复构建速度:
171
+
172
+ ```bash
173
+ # 已在 tsconfig 中启用 incremental: true
174
+ pnpm build:esm
175
+ ```
176
+
177
+ ## 故障排查
178
+
179
+ ### GUI 构建失败
180
+
181
+ ```bash
182
+ # 清理 GUI 缓存
183
+ cd packages/gui
184
+ rm -rf node_modules .vite dist
185
+ pnpm install
186
+ pnpm build
187
+ ```
188
+
189
+ ### TypeScript 编译错误
190
+
191
+ ```bash
192
+ # 清理 TypeScript 缓存
193
+ rm -rf dist
194
+ rm -f tsconfig.tsbuildinfo
195
+ pnpm build
196
+ ```
197
+
198
+ ### 依赖问题
199
+
200
+ ```bash
201
+ # 清理所有依赖并重新安装
202
+ rm -rf node_modules packages/gui/node_modules
203
+ pnpm install
204
+ ```
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,310 @@
1
+ <div align="center">
2
+
3
+ # 🔍 Node Network DevTools
4
+
5
+ **Monitor Node.js network requests with Chrome DevTools integration and built-in Web GUI**
6
+
7
+ [![npm version](https://img.shields.io/npm/v/node-network-devtools.svg)](https://www.npmjs.com/package/node-network-devtools)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
+ [![Node.js Version](https://img.shields.io/node/v/node-network-devtools.svg)](https://nodejs.org)
10
+
11
+ [English](#) | [中文文档](./README.zh-CN.md)
12
+
13
+ </div>
14
+
15
+ ---
16
+
17
+ ## ✨ Features
18
+
19
+ - 🔍 **Dual Interception** - Supports both `http/https` modules and `undici/fetch` API
20
+ - 🎯 **Zero Intrusion** - Auto-inject via `-r` or `--import`, no code changes needed
21
+ - 📊 **DevTools Integration** - View all requests in Chrome DevTools Network panel
22
+ - 🖥️ **Built-in Web GUI** - Chrome DevTools-like interface with real-time updates
23
+ - 🔗 **Request Tracing** - AsyncLocalStorage-based request correlation
24
+ - 🛡️ **Security** - Auto-redact sensitive headers (Authorization, Cookie, etc.)
25
+ - ⚡ **Next.js Compatible** - Preserves `next.revalidate`, `next.tags` options
26
+ - 📦 **TypeScript** - Full TypeScript support with type definitions
27
+
28
+ ## 📸 Screenshots
29
+
30
+ ### Web GUI Interface
31
+ ![Web GUI](https://via.placeholder.com/800x450?text=Web+GUI+Screenshot)
32
+
33
+ ### Chrome DevTools Integration
34
+ ![Chrome DevTools](https://via.placeholder.com/800x450?text=Chrome+DevTools+Screenshot)
35
+
36
+ ## 🚀 Quick Start
37
+
38
+ ### Installation
39
+
40
+ ```bash
41
+ npm install node-network-devtools
42
+ # or
43
+ pnpm add node-network-devtools
44
+ # or
45
+ yarn add node-network-devtools
46
+ ```
47
+
48
+ ### Usage
49
+
50
+ #### Method 1: CLI (Recommended)
51
+
52
+ ```bash
53
+ npx node-network-devtools your-script.js
54
+ # or use the short alias
55
+ npx nnd your-script.js
56
+ ```
57
+
58
+ The CLI automatically adds the `--inspect` flag and injects the interceptor.
59
+
60
+ #### Method 2: Using `-r` flag
61
+
62
+ ```bash
63
+ node --inspect -r node-network-devtools/register your-script.js
64
+ ```
65
+
66
+ #### Method 3: Programmatic
67
+
68
+ ```typescript
69
+ import { install } from 'node-network-devtools';
70
+
71
+ await install();
72
+
73
+ // Your application code
74
+ import express from 'express';
75
+ const app = express();
76
+ // ...
77
+ ```
78
+
79
+ ### Viewing Requests
80
+
81
+ After starting your application:
82
+
83
+ 1. **Web GUI** (Default): A browser window will automatically open showing the GUI
84
+ 2. **Chrome DevTools**: Open Chrome and navigate to `chrome://inspect`, then click "Open dedicated DevTools for Node"
85
+
86
+ ## 🖥️ Web GUI
87
+
88
+ The built-in Web GUI provides a Chrome DevTools-like experience for monitoring network requests.
89
+
90
+ ### Features
91
+
92
+ - 📋 **Request List** - Real-time display of all network requests
93
+ - 🔍 **Search & Filter** - Filter by URL, method, status code, and type
94
+ - 📝 **Details Panel** - View headers, payload, response, and timing
95
+ - 🎨 **Theme Toggle** - Dark/Light theme support
96
+ - ⏸️ **Pause/Resume** - Pause request capture for analysis
97
+ - 🔄 **Real-time Updates** - WebSocket-based live updates
98
+
99
+ ### GUI Configuration
100
+
101
+ ```bash
102
+ # Specify GUI port
103
+ NND_GUI_PORT=9230 npx nnd your-script.js
104
+
105
+ # Specify WebSocket port
106
+ NND_WS_PORT=9231 npx nnd your-script.js
107
+
108
+ # Disable GUI
109
+ NND_GUI_ENABLED=false npx nnd your-script.js
110
+
111
+ # Disable auto-open browser
112
+ NND_AUTO_OPEN=false npx nnd your-script.js
113
+ ```
114
+
115
+ ## 🔧 Configuration
116
+
117
+ ### Environment Variables
118
+
119
+ #### Core Settings
120
+
121
+ | Variable | Description | Default |
122
+ |----------|-------------|---------|
123
+ | `NND_MAX_REQUESTS` | Maximum stored requests | 1000 |
124
+ | `NND_MAX_BODY_SIZE` | Maximum body size (bytes) | 1048576 (1MB) |
125
+ | `NND_INTERCEPT_HTTP` | Intercept http/https | true |
126
+ | `NND_INTERCEPT_UNDICI` | Intercept undici/fetch | true |
127
+ | `NND_REDACT_HEADERS` | Headers to redact (comma-separated) | authorization,cookie |
128
+ | `NND_AUTO_CONNECT` | Auto-connect to CDP | true |
129
+
130
+ #### GUI Settings
131
+
132
+ | Variable | Description | Default |
133
+ |----------|-------------|---------|
134
+ | `NND_GUI_ENABLED` | Enable GUI server | true |
135
+ | `NND_GUI_PORT` | GUI server port | auto |
136
+ | `NND_WS_PORT` | WebSocket port | auto |
137
+ | `NND_AUTO_OPEN` | Auto-open browser | true |
138
+
139
+ ### Programmatic Configuration
140
+
141
+ ```typescript
142
+ import { setConfig } from 'node-network-devtools';
143
+
144
+ setConfig({
145
+ maxRequests: 500,
146
+ maxBodySize: 512 * 1024,
147
+ redactHeaders: ['authorization', 'cookie', 'x-api-key'],
148
+ guiEnabled: true,
149
+ autoOpen: false,
150
+ });
151
+ ```
152
+
153
+ ## 🎯 Framework Integration
154
+
155
+ ### Next.js
156
+
157
+ 1. Copy `templates/instrumentation.ts` to your project root
158
+ 2. Enable instrumentation in `next.config.js`:
159
+
160
+ ```javascript
161
+ module.exports = {
162
+ experimental: {
163
+ instrumentationHook: true,
164
+ },
165
+ };
166
+ ```
167
+
168
+ 3. Start with `--inspect`:
169
+
170
+ ```bash
171
+ NODE_OPTIONS='--inspect' npm run dev
172
+ ```
173
+
174
+ Or configure in `package.json`:
175
+
176
+ ```json
177
+ {
178
+ "scripts": {
179
+ "dev:debug": "NODE_OPTIONS='--inspect' next dev"
180
+ }
181
+ }
182
+ ```
183
+
184
+ ### Express
185
+
186
+ ```typescript
187
+ import express from 'express';
188
+ import { install } from 'node-network-devtools';
189
+
190
+ await install();
191
+
192
+ const app = express();
193
+ // Your routes...
194
+ ```
195
+
196
+ ### Other Frameworks
197
+
198
+ Works with any Node.js framework! Just install the interceptor before your application code.
199
+
200
+ ## 📚 API Reference
201
+
202
+ ### Main Exports
203
+
204
+ ```typescript
205
+ // Quick install
206
+ import { install, startGUI, stopGUI } from 'node-network-devtools';
207
+
208
+ // Configuration
209
+ import { getConfig, setConfig, resetConfig } from 'node-network-devtools';
210
+
211
+ // Request store
212
+ import { getRequestStore } from 'node-network-devtools';
213
+
214
+ // Context tracing
215
+ import {
216
+ runWithTrace,
217
+ getCurrentTraceId,
218
+ generateTraceId
219
+ } from 'node-network-devtools';
220
+
221
+ // Interceptors
222
+ import { HttpPatcher, UndiciPatcher } from 'node-network-devtools';
223
+
224
+ // CDP Bridge
225
+ import { getCDPBridge, isInspectorEnabled } from 'node-network-devtools';
226
+ ```
227
+
228
+ ### Request Tracing
229
+
230
+ Correlate multiple requests in the same business flow:
231
+
232
+ ```typescript
233
+ import { runWithTrace, getRequestStore } from 'node-network-devtools';
234
+
235
+ await runWithTrace('user-login', async () => {
236
+ // These requests will be correlated with the same traceId
237
+ await fetch('https://api.example.com/auth');
238
+ await fetch('https://api.example.com/user');
239
+ });
240
+
241
+ // Query correlated requests
242
+ const store = getRequestStore();
243
+ const requests = store.getByTraceId('user-login');
244
+ ```
245
+
246
+ ## 📖 Examples
247
+
248
+ Check the [examples](./examples) directory for more usage examples:
249
+
250
+ - [basic-http](./examples/basic-http) - Basic HTTP request monitoring
251
+ - [fetch-api](./examples/fetch-api) - Fetch API monitoring
252
+ - [request-tracing](./examples/request-tracing) - Request tracing
253
+ - [express-server](./examples/express-server) - Express server example
254
+ - [programmatic-api](./examples/programmatic-api) - Programmatic API usage
255
+ - [nextjs-app](./examples/nextjs-app) - Next.js App Router integration
256
+
257
+ ## 🔬 How It Works
258
+
259
+ 1. **HTTP Interception**: Uses `@mswjs/interceptors` to intercept http/https module requests
260
+ 2. **Undici Interception**: Uses `Agent.compose()` to register interceptors for fetch requests
261
+ 3. **CDP Bridge**: Connects to V8 Inspector via `node:inspector` module and sends Network domain events
262
+ 4. **Context Propagation**: Uses `AsyncLocalStorage` to pass TraceID through async call chains
263
+ 5. **Event Bridge**: Forwards intercepted requests to WebSocket clients for real-time GUI updates
264
+
265
+ ## 🤝 Contributing
266
+
267
+ Contributions are welcome! Please read our [Contributing Guide](./CONTRIBUTING.md) for details.
268
+
269
+ ### Development Setup
270
+
271
+ ```bash
272
+ # Clone the repository
273
+ git clone https://github.com/dong0926/node-network-devtools.git
274
+ cd node-network-devtools
275
+
276
+ # Install dependencies
277
+ pnpm install
278
+
279
+ # Build the project
280
+ pnpm build
281
+
282
+ # Run tests
283
+ pnpm test:all
284
+ ```
285
+
286
+ ## 📝 License
287
+
288
+ MIT © [ddddd](https://github.com/dong0926)
289
+
290
+ ## 🙏 Acknowledgments
291
+
292
+ - [@mswjs/interceptors](https://github.com/mswjs/interceptors) - HTTP request interception
293
+ - [undici](https://github.com/nodejs/undici) - HTTP/1.1 client
294
+ - [ws](https://github.com/websockets/ws) - WebSocket implementation
295
+
296
+ ## 📮 Support
297
+
298
+ - 🐛 [Report Issues](https://github.com/dong0926/node-network-devtools/issues)
299
+ - 💬 [Discussions](https://github.com/dong0926/node-network-devtools/discussions)
300
+ - 📧 Email: your.email@example.com
301
+
302
+ ---
303
+
304
+ <div align="center">
305
+
306
+ **If you find this project helpful, please give it a ⭐️!**
307
+
308
+ Made with ❤️ by [ddddd](https://github.com/dong0926)
309
+
310
+ </div>