whistle.pastekitlab 1.8.0 → 1.8.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.
Files changed (2) hide show
  1. package/index.js +11 -59
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -152,12 +152,13 @@ async function forwardRequest(req, res, url) {
152
152
  reqBody += chunk;
153
153
  }
154
154
 
155
- // 通过 axios 转发请求
155
+ // 通过 axios 转发请求(使用 arraybuffer)
156
156
  const response = await axios({
157
157
  method: req.method.toLowerCase(),
158
158
  url: url,
159
159
  headers: req.headers,
160
160
  data: reqBody || undefined,
161
+ responseType: 'arraybuffer',
161
162
  validateStatus: () => true
162
163
  });
163
164
 
@@ -167,28 +168,8 @@ async function forwardRequest(req, res, url) {
167
168
  res.setHeader(key, response.headers[key]);
168
169
  });
169
170
 
170
- // 转换响应数据
171
- let responseBody;
172
- if (Buffer.isBuffer(response.data)) {
173
- responseBody = response.data;
174
- } else if (typeof response.data === 'string') {
175
- responseBody = response.data;
176
- } else if (typeof response.data === 'object') {
177
- try {
178
- responseBody = JSON.stringify(response.data);
179
- } catch (e) {
180
- responseBody = String(response.data);
181
- }
182
- } else {
183
- responseBody = String(response.data || '');
184
- }
185
-
186
- // 确保 responseBody 是有效的字符串或 Buffer
187
- if (typeof responseBody !== 'string' && !Buffer.isBuffer(responseBody)) {
188
- responseBody = String(responseBody || '');
189
- }
190
-
191
- res.end(responseBody);
171
+ // 直接返回完整的 Buffer
172
+ res.end(Buffer.from(response.data));
192
173
  log(`直接转发: ${req.method} ${url} -> ${response.status}`);
193
174
 
194
175
  } catch (error) {
@@ -236,13 +217,14 @@ module.exports = (server, options) => {
236
217
  broadcast('REQUEST', requestData);
237
218
  log(`REQ ${req.method} ${url}`);
238
219
 
239
- // 通过 axios 转发请求
220
+ // 通过 axios 转发请求(使用 arraybuffer 接收二进制数据)
240
221
  const startTime = Date.now();
241
222
  const response = await axios({
242
223
  method: req.method.toLowerCase(),
243
224
  url: url,
244
225
  headers: req.headers,
245
226
  data: reqBody || undefined,
227
+ responseType: 'arraybuffer', // 关键:以 ArrayBuffer 接收,保留完整数据
246
228
  validateStatus: () => true // 不抛出 HTTP 错误
247
229
  });
248
230
  const duration = Date.now() - startTime;
@@ -253,38 +235,10 @@ module.exports = (server, options) => {
253
235
  res.setHeader(key, response.headers[key]);
254
236
  });
255
237
 
256
- // 转换响应数据为 Buffer 或字符串
257
- let responseBody;
258
- let responseBodyForNotification = null;
259
-
260
- if (Buffer.isBuffer(response.data)) {
261
- responseBody = response.data;
262
- responseBodyForNotification = bufferToString(response.data);
263
- } else if (typeof response.data === 'string') {
264
- responseBody = response.data;
265
- responseBodyForNotification = response.data;
266
- } else if (typeof response.data === 'object') {
267
- try {
268
- const jsonStr = JSON.stringify(response.data);
269
- responseBody = jsonStr;
270
- responseBodyForNotification = jsonStr;
271
- } catch (e) {
272
- const str = String(response.data);
273
- responseBody = str;
274
- responseBodyForNotification = str;
275
- }
276
- } else {
277
- const str = String(response.data || '');
278
- responseBody = str;
279
- responseBodyForNotification = str;
280
- }
281
-
282
- // 确保 responseBody 是有效的字符串或 Buffer
283
- if (typeof responseBody !== 'string' && !Buffer.isBuffer(responseBody)) {
284
- responseBody = String(responseBody || '');
285
- }
238
+ // 转换响应数据为 Buffer
239
+ const responseBody = Buffer.from(response.data);
286
240
 
287
- // 返回响应体
241
+ // 直接返回完整的 Buffer(不会截断)
288
242
  res.end(responseBody);
289
243
 
290
244
  // 构建响应数据
@@ -292,10 +246,8 @@ module.exports = (server, options) => {
292
246
  ...requestData,
293
247
  statusCode: response.status,
294
248
  responseHeaders: {...response.headers},
295
- responseBody: truncateString(responseBodyForNotification),
296
- responseBodyBase64: Buffer.from(
297
- typeof responseBody === 'string' ? responseBody : responseBody.toString()
298
- ).toString('base64'),
249
+ responseBody: truncateString(bufferToString(responseBody)),
250
+ responseBodyBase64: responseBody.toString('base64'),
299
251
  duration
300
252
  };
301
253
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whistle.pastekitlab",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
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": {