brakit 0.7.1 → 0.7.3

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/dist/api.js CHANGED
@@ -1618,7 +1618,7 @@ var AnalysisEngine = class {
1618
1618
  };
1619
1619
 
1620
1620
  // src/index.ts
1621
- var VERSION = "0.7.1";
1621
+ var VERSION = "0.7.3";
1622
1622
  export {
1623
1623
  AdapterRegistry,
1624
1624
  AnalysisEngine,
@@ -693,7 +693,7 @@ import { resolve as resolve2 } from "path";
693
693
  import { randomUUID as randomUUID3 } from "crypto";
694
694
 
695
695
  // src/index.ts
696
- var VERSION = "0.7.1";
696
+ var VERSION = "0.7.3";
697
697
 
698
698
  // src/cli/commands/install.ts
699
699
  var IMPORT_LINE = `import "brakit";`;
@@ -3669,7 +3669,7 @@ var init_src = __esm({
3669
3669
  init_rules();
3670
3670
  init_engine();
3671
3671
  init_insights2();
3672
- VERSION = "0.7.1";
3672
+ VERSION = "0.7.3";
3673
3673
  }
3674
3674
  });
3675
3675
 
@@ -6281,6 +6281,7 @@ var init_guard = __esm({
6281
6281
  });
6282
6282
 
6283
6283
  // src/runtime/capture.ts
6284
+ import { gunzipSync, brotliDecompressSync, inflateSync } from "zlib";
6284
6285
  function outgoingToIncoming(headers) {
6285
6286
  const result = {};
6286
6287
  for (const [key, value] of Object.entries(headers)) {
@@ -6293,52 +6294,65 @@ function outgoingToIncoming(headers) {
6293
6294
  }
6294
6295
  return result;
6295
6296
  }
6297
+ function decompress(body, encoding) {
6298
+ try {
6299
+ if (encoding === "gzip") return gunzipSync(body);
6300
+ if (encoding === "br") return brotliDecompressSync(body);
6301
+ if (encoding === "deflate") return inflateSync(body);
6302
+ } catch {
6303
+ }
6304
+ return body;
6305
+ }
6296
6306
  function captureInProcess(req, res, requestId) {
6297
6307
  const startTime = performance.now();
6298
6308
  const method = req.method ?? "GET";
6299
- const shouldCaptureBody = method !== "GET" && method !== "HEAD";
6300
- const reqChunks = [];
6301
- let reqSize = 0;
6302
- if (shouldCaptureBody) {
6303
- req.on("data", (chunk) => {
6304
- if (reqSize < DEFAULT_MAX_BODY_CAPTURE) {
6305
- reqChunks.push(chunk);
6306
- reqSize += chunk.length;
6307
- }
6308
- });
6309
- }
6310
6309
  const resChunks = [];
6311
6310
  let resSize = 0;
6312
6311
  const originalWrite = res.write;
6313
6312
  const originalEnd = res.end;
6314
- res.write = function(chunk, ...args) {
6315
- if (chunk && resSize < DEFAULT_MAX_BODY_CAPTURE) {
6316
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
6317
- resChunks.push(buf);
6318
- resSize += buf.length;
6313
+ res.write = function(...args) {
6314
+ try {
6315
+ const chunk = args[0];
6316
+ if (chunk != null && typeof chunk !== "function" && resSize < DEFAULT_MAX_BODY_CAPTURE) {
6317
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
6318
+ resChunks.push(buf);
6319
+ resSize += buf.length;
6320
+ }
6321
+ } catch {
6319
6322
  }
6320
- return originalWrite.apply(this, [chunk, ...args]);
6323
+ return originalWrite.apply(this, args);
6321
6324
  };
6322
6325
  res.end = function(...args) {
6323
- const chunk = typeof args[0] !== "function" ? args[0] : void 0;
6324
- if (chunk && resSize < DEFAULT_MAX_BODY_CAPTURE) {
6325
- const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
6326
- resChunks.push(buf);
6326
+ try {
6327
+ const chunk = typeof args[0] !== "function" ? args[0] : void 0;
6328
+ if (chunk != null && resSize < DEFAULT_MAX_BODY_CAPTURE) {
6329
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
6330
+ resChunks.push(buf);
6331
+ }
6332
+ } catch {
6327
6333
  }
6328
6334
  const result = originalEnd.apply(this, args);
6329
- defaultStore.capture({
6330
- requestId,
6331
- method,
6332
- url: req.url ?? "/",
6333
- requestHeaders: req.headers,
6334
- requestBody: reqChunks.length > 0 ? Buffer.concat(reqChunks) : null,
6335
- statusCode: res.statusCode,
6336
- responseHeaders: outgoingToIncoming(res.getHeaders()),
6337
- responseBody: resChunks.length > 0 ? Buffer.concat(resChunks) : null,
6338
- responseContentType: String(res.getHeader("content-type") ?? ""),
6339
- startTime,
6340
- config: { maxBodyCapture: DEFAULT_MAX_BODY_CAPTURE }
6341
- });
6335
+ try {
6336
+ const encoding = String(res.getHeader("content-encoding") ?? "").toLowerCase();
6337
+ let body = resChunks.length > 0 ? Buffer.concat(resChunks) : null;
6338
+ if (body && encoding) {
6339
+ body = decompress(body, encoding);
6340
+ }
6341
+ defaultStore.capture({
6342
+ requestId,
6343
+ method,
6344
+ url: req.url ?? "/",
6345
+ requestHeaders: req.headers,
6346
+ requestBody: null,
6347
+ statusCode: res.statusCode,
6348
+ responseHeaders: outgoingToIncoming(res.getHeaders()),
6349
+ responseBody: body,
6350
+ responseContentType: String(res.getHeader("content-type") ?? ""),
6351
+ startTime,
6352
+ config: { maxBodyCapture: DEFAULT_MAX_BODY_CAPTURE }
6353
+ });
6354
+ } catch {
6355
+ }
6342
6356
  return result;
6343
6357
  };
6344
6358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brakit",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "See what your API is really doing. Security scanning, N+1 detection, duplicate calls, DB queries — one command, zero config.",
5
5
  "type": "module",
6
6
  "bin": {