whistle.pastekitlab 1.7.1 → 1.7.2
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/index.js +46 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -152,7 +152,23 @@ module.exports = (server, options) => {
|
|
|
152
152
|
reqBody += chunk;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
// 构建请求数据
|
|
156
|
+
const requestData = {
|
|
157
|
+
eventId: generateId(),
|
|
158
|
+
url,
|
|
159
|
+
method: req.method,
|
|
160
|
+
headers: {...req.headers},
|
|
161
|
+
body: bufferToString(Buffer.from(reqBody)),
|
|
162
|
+
bodyBase64: Buffer.from(reqBody).toString('base64'),
|
|
163
|
+
timestamp: Date.now()
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// 通知 Chrome 插件:请求开始
|
|
167
|
+
broadcast('REQUEST', requestData);
|
|
168
|
+
log(`REQ ${req.method} ${url}`);
|
|
169
|
+
|
|
155
170
|
// 通过 axios 转发请求
|
|
171
|
+
const startTime = Date.now();
|
|
156
172
|
const response = await axios({
|
|
157
173
|
method: req.method.toLowerCase(),
|
|
158
174
|
url: url,
|
|
@@ -160,6 +176,7 @@ module.exports = (server, options) => {
|
|
|
160
176
|
data: reqBody || undefined,
|
|
161
177
|
validateStatus: () => true // 不抛出 HTTP 错误
|
|
162
178
|
});
|
|
179
|
+
const duration = Date.now() - startTime;
|
|
163
180
|
|
|
164
181
|
// 设置响应状态码和头
|
|
165
182
|
res.statusCode = response.status;
|
|
@@ -170,12 +187,40 @@ module.exports = (server, options) => {
|
|
|
170
187
|
// 返回响应体
|
|
171
188
|
res.end(response.data);
|
|
172
189
|
|
|
173
|
-
|
|
190
|
+
// 构建响应数据
|
|
191
|
+
const responseData = {
|
|
192
|
+
...requestData,
|
|
193
|
+
statusCode: response.status,
|
|
194
|
+
responseHeaders: {...response.headers},
|
|
195
|
+
responseBody: bufferToString(
|
|
196
|
+
typeof response.data === 'string'
|
|
197
|
+
? Buffer.from(response.data)
|
|
198
|
+
: Buffer.from(JSON.stringify(response.data))
|
|
199
|
+
),
|
|
200
|
+
responseBodyBase64: (
|
|
201
|
+
typeof response.data === 'string'
|
|
202
|
+
? Buffer.from(response.data)
|
|
203
|
+
: Buffer.from(JSON.stringify(response.data))
|
|
204
|
+
).toString('base64'),
|
|
205
|
+
duration
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// 通知 Chrome 插件:响应完成
|
|
209
|
+
broadcast('RESPONSE', responseData);
|
|
210
|
+
log(`转发成功: ${req.method} ${url} -> ${response.status} (${duration}ms)`);
|
|
174
211
|
|
|
175
212
|
} catch (error) {
|
|
176
213
|
log(`转发失败: ${error.message}`);
|
|
177
214
|
res.statusCode = 500;
|
|
178
215
|
res.end(JSON.stringify({ error: error.message }));
|
|
216
|
+
|
|
217
|
+
// 通知 Chrome 插件:请求失败
|
|
218
|
+
broadcast('ERROR', {
|
|
219
|
+
url: req.fullUrl || req.url,
|
|
220
|
+
method: req.method,
|
|
221
|
+
error: error.message,
|
|
222
|
+
timestamp: Date.now()
|
|
223
|
+
});
|
|
179
224
|
}
|
|
180
225
|
});
|
|
181
226
|
};
|
package/package.json
CHANGED