whistle.pastekitlab 1.6.2 → 1.6.4

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 (2) hide show
  1. package/index.js +42 -43
  2. package/package.json +2 -4
package/index.js CHANGED
@@ -137,77 +137,76 @@ function broadcast(type, data) {
137
137
 
138
138
  // ==================== Whistle 插件主逻辑 ====================
139
139
  module.exports = (server, options) => {
140
- log('插件已启动(使用 request/response 钩子)');
140
+ log('插件已启动(request/response 钩子)');
141
141
  startWebSocketServer();
142
- // server.on('request', (req, res) => {
143
- // console.log('>>> [GLOBAL]', req.fullUrl);
144
- // });
145
142
 
146
143
  return {
147
- // ✅ 监听请求
148
144
  request: (req, res, next) => {
149
145
  try {
150
- console.info("req:" + JSON.stringify(req))
151
146
  const url = req.fullUrl || req.url;
152
- // if (!shouldIntercept(url)) {
153
- // return next(); // 不处理,直接放行
154
- // }
155
-
156
- const data = {
157
- eventId: generateId(),
158
- url,
159
- method: req.method,
160
- headers: {...req.headers},
161
- body: bufferToString(req._reqBody),
162
- bodyBase64: bufferToBase64(req._reqBody)
163
- };
164
-
165
- log(`REQ ${req.method} ${url}`);
166
- broadcast('REQUEST', data);
147
+ log(`🟡 REQ ${req.method} ${url}`);
148
+
149
+ // ⚠️ 必须优先调用 next,避免阻塞代理链
150
+ next();
151
+
152
+ // 异步获取 Body,不阻塞请求转发
153
+ req.getBody().then(body => {
154
+ const data = {
155
+ eventId: generateId(),
156
+ url,
157
+ method: req.method,
158
+ headers: { ...req.headers },
159
+ body: bufferToString(body),
160
+ bodyBase64: bufferToBase64(body)
161
+ };
162
+ broadcast('REQUEST', data);
163
+ }).catch(e => log('❌ request body error: ' + e.message));
167
164
 
168
165
  } catch (e) {
169
- log('request error: ' + e.message);
166
+ log('request hook error: ' + e.message);
167
+ next(); // 异常时也必须放行
170
168
  }
171
- next(); // ⚠️ 必须调用,让 whistle 继续代理
172
169
  },
173
170
 
174
- // ✅ 监听响应
175
171
  response: (req, res, next) => {
176
172
  try {
177
- console.info("rsp:" + JSON.stringify(req))
178
-
179
173
  const url = req.fullUrl || req.url;
174
+
175
+ // 按需拦截(确保该函数对测试 URL 返回 true)
180
176
  if (!shouldIntercept(url)) {
181
177
  return next();
182
178
  }
183
179
 
184
- const data = {
185
- eventId: generateId(),
186
- url,
187
- method: req.method,
188
- statusCode: res.statusCode,
189
- headers: {...res.headers},
190
- body: bufferToString(res._resBody),
191
- bodyBase64: bufferToBase64(res._resBody)
192
- };
193
-
194
- log(`RES ${res.statusCode} ${url}`);
195
- broadcast('RESPONSE', data);
180
+ log(`🟢 RES ${res.statusCode} ${url}`);
181
+ next(); // 先放行响应给客户端
182
+
183
+ res.getBody().then(body => {
184
+ const data = {
185
+ eventId: generateId(),
186
+ url,
187
+ method: req.method,
188
+ statusCode: res.statusCode,
189
+ headers: { ...res.headers },
190
+ body: bufferToString(body),
191
+ bodyBase64: bufferToBase64(body)
192
+ };
193
+ broadcast('RESPONSE', data);
194
+ }).catch(e => log('❌ response body error: ' + e.message));
196
195
 
197
196
  } catch (e) {
198
- log('response error: ' + e.message);
197
+ log('response hook error: ' + e.message);
198
+ next();
199
199
  }
200
- next(); // ⚠️ 必须调用,继续后续处理
201
200
  }
202
201
  };
203
202
  };
204
203
 
205
- // ==================== 可选:暴露 Web UI ====================
204
+ // UI 接口保持不变
206
205
  module.exports.ui = (uiServer, options) => {
207
- // 如果你需要提供 /plugin/pastekitlab/... 接口,可以在这里用 uiServer
208
206
  uiServer.on('request', (req, res) => {
209
207
  if (req.url === '/plugin/pastekitlab/config') {
210
- res.end(JSON.stringify({status: 'ok', wsPort: CONFIG.wsPort}));
208
+ res.setHeader('Content-Type', 'application/json');
209
+ res.end(JSON.stringify({ status: 'ok', wsPort: CONFIG.wsPort }));
211
210
  }
212
211
  });
213
212
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whistle.pastekitlab",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "Whistle plugin for PasteKit Lab - Intercepts requests and sends them to requestlistviewer via WebSocket",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,12 +18,10 @@
18
18
  "dependencies": {
19
19
  "ws": "^8.19.0"
20
20
  },
21
- "whistleMetadata": {
22
- "type": "reqScript"
23
- },
24
21
  "whistlePlugin": true,
25
22
  "whistleConfig": {
26
23
  "name": "pastekitlab",
24
+ "type": "plugin",
27
25
  "description": "PasteKit Lab 请求拦截插件,通过 WebSocket 将请求发送到 requestlistviewer 展示"
28
26
  }
29
27
  }