apitally 0.15.3 → 0.16.1

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 (65) hide show
  1. package/dist/adonisjs/middleware.cjs.map +1 -1
  2. package/dist/adonisjs/middleware.js.map +1 -1
  3. package/dist/adonisjs/provider.cjs +118 -46
  4. package/dist/adonisjs/provider.cjs.map +1 -1
  5. package/dist/adonisjs/provider.js +118 -46
  6. package/dist/adonisjs/provider.js.map +1 -1
  7. package/dist/common/client.cjs +118 -46
  8. package/dist/common/client.cjs.map +1 -1
  9. package/dist/common/client.js +118 -46
  10. package/dist/common/client.js.map +1 -1
  11. package/dist/common/requestLogger.cjs +118 -46
  12. package/dist/common/requestLogger.cjs.map +1 -1
  13. package/dist/common/requestLogger.d.cts +5 -0
  14. package/dist/common/requestLogger.d.ts +5 -0
  15. package/dist/common/requestLogger.js +118 -46
  16. package/dist/common/requestLogger.js.map +1 -1
  17. package/dist/express/index.cjs +126 -50
  18. package/dist/express/index.cjs.map +1 -1
  19. package/dist/express/index.js +126 -50
  20. package/dist/express/index.js.map +1 -1
  21. package/dist/express/middleware.cjs +126 -50
  22. package/dist/express/middleware.cjs.map +1 -1
  23. package/dist/express/middleware.js +126 -50
  24. package/dist/express/middleware.js.map +1 -1
  25. package/dist/express/utils.cjs +8 -4
  26. package/dist/express/utils.cjs.map +1 -1
  27. package/dist/express/utils.js +8 -4
  28. package/dist/express/utils.js.map +1 -1
  29. package/dist/fastify/index.cjs +118 -46
  30. package/dist/fastify/index.cjs.map +1 -1
  31. package/dist/fastify/index.js +118 -46
  32. package/dist/fastify/index.js.map +1 -1
  33. package/dist/fastify/plugin.cjs +118 -46
  34. package/dist/fastify/plugin.cjs.map +1 -1
  35. package/dist/fastify/plugin.js +118 -46
  36. package/dist/fastify/plugin.js.map +1 -1
  37. package/dist/h3/index.cjs +118 -46
  38. package/dist/h3/index.cjs.map +1 -1
  39. package/dist/h3/index.js +118 -46
  40. package/dist/h3/index.js.map +1 -1
  41. package/dist/h3/plugin.cjs +118 -46
  42. package/dist/h3/plugin.cjs.map +1 -1
  43. package/dist/h3/plugin.js +118 -46
  44. package/dist/h3/plugin.js.map +1 -1
  45. package/dist/hono/index.cjs +118 -46
  46. package/dist/hono/index.cjs.map +1 -1
  47. package/dist/hono/index.js +118 -46
  48. package/dist/hono/index.js.map +1 -1
  49. package/dist/hono/middleware.cjs +118 -46
  50. package/dist/hono/middleware.cjs.map +1 -1
  51. package/dist/hono/middleware.js +118 -46
  52. package/dist/hono/middleware.js.map +1 -1
  53. package/dist/koa/index.cjs +118 -46
  54. package/dist/koa/index.cjs.map +1 -1
  55. package/dist/koa/index.js +118 -46
  56. package/dist/koa/index.js.map +1 -1
  57. package/dist/koa/middleware.cjs +118 -46
  58. package/dist/koa/middleware.cjs.map +1 -1
  59. package/dist/koa/middleware.js +118 -46
  60. package/dist/koa/middleware.js.map +1 -1
  61. package/dist/nestjs/index.cjs +126 -50
  62. package/dist/nestjs/index.cjs.map +1 -1
  63. package/dist/nestjs/index.js +126 -50
  64. package/dist/nestjs/index.js.map +1 -1
  65. package/package.json +1 -1
@@ -438,6 +438,16 @@ var MASK_HEADER_PATTERNS = [
438
438
  /token/i,
439
439
  /cookie/i
440
440
  ];
441
+ var MASK_BODY_FIELD_PATTERNS = [
442
+ /password/i,
443
+ /pwd/i,
444
+ /token/i,
445
+ /secret/i,
446
+ /auth/i,
447
+ /card[-_ ]?number/i,
448
+ /ccv/i,
449
+ /ssn/i
450
+ ];
441
451
  var DEFAULT_CONFIG = {
442
452
  enabled: false,
443
453
  logQueryParams: true,
@@ -448,6 +458,7 @@ var DEFAULT_CONFIG = {
448
458
  logException: true,
449
459
  maskQueryParams: [],
450
460
  maskHeaders: [],
461
+ maskBodyFields: [],
451
462
  excludePaths: []
452
463
  };
453
464
  var _RequestLogger = class _RequestLogger {
@@ -498,11 +509,23 @@ var _RequestLogger = class _RequestLogger {
498
509
  ];
499
510
  return matchPatterns(name, patterns);
500
511
  }
512
+ shouldMaskBodyField(name) {
513
+ const patterns = [
514
+ ...this.config.maskBodyFields,
515
+ ...MASK_BODY_FIELD_PATTERNS
516
+ ];
517
+ return matchPatterns(name, patterns);
518
+ }
501
519
  hasSupportedContentType(headers) {
502
520
  var _a2;
503
521
  const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
504
522
  return this.isSupportedContentType(contentType);
505
523
  }
524
+ hasJsonContentType(headers) {
525
+ var _a2;
526
+ const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
527
+ return contentType ? /\bjson\b/i.test(contentType) : null;
528
+ }
506
529
  isSupportedContentType(contentType) {
507
530
  return typeof contentType === "string" && ALLOWED_CONTENT_TYPES.some((t) => contentType.startsWith(t));
508
531
  }
@@ -521,6 +544,72 @@ var _RequestLogger = class _RequestLogger {
521
544
  this.shouldMaskHeader(k) ? MASKED : v
522
545
  ]);
523
546
  }
547
+ maskBody(data) {
548
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
549
+ const result = {};
550
+ for (const [key, value] of Object.entries(data)) {
551
+ if (typeof value === "string" && this.shouldMaskBodyField(key)) {
552
+ result[key] = MASKED;
553
+ } else {
554
+ result[key] = this.maskBody(value);
555
+ }
556
+ }
557
+ return result;
558
+ }
559
+ if (Array.isArray(data)) {
560
+ return data.map((item) => this.maskBody(item));
561
+ }
562
+ return data;
563
+ }
564
+ applyMasking(item) {
565
+ if (this.config.maskRequestBodyCallback && item.request.body && item.request.body !== BODY_TOO_LARGE) {
566
+ try {
567
+ const maskedBody = this.config.maskRequestBodyCallback(item.request);
568
+ item.request.body = maskedBody ?? BODY_MASKED;
569
+ } catch {
570
+ item.request.body = void 0;
571
+ }
572
+ }
573
+ if (this.config.maskResponseBodyCallback && item.response.body && item.response.body !== BODY_TOO_LARGE) {
574
+ try {
575
+ const maskedBody = this.config.maskResponseBodyCallback(item.request, item.response);
576
+ item.response.body = maskedBody ?? BODY_MASKED;
577
+ } catch {
578
+ item.response.body = void 0;
579
+ }
580
+ }
581
+ if (item.request.body && item.request.body.length > MAX_BODY_SIZE) {
582
+ item.request.body = BODY_TOO_LARGE;
583
+ }
584
+ if (item.response.body && item.response.body.length > MAX_BODY_SIZE) {
585
+ item.response.body = BODY_TOO_LARGE;
586
+ }
587
+ for (const key of [
588
+ "request",
589
+ "response"
590
+ ]) {
591
+ const bodyData = item[key].body;
592
+ if (!bodyData || bodyData === BODY_TOO_LARGE || bodyData === BODY_MASKED) {
593
+ continue;
594
+ }
595
+ const headers = item[key].headers;
596
+ const hasJsonContent = this.hasJsonContentType(headers);
597
+ if (hasJsonContent === null || hasJsonContent) {
598
+ try {
599
+ const parsedBody = JSON.parse(bodyData.toString());
600
+ const maskedBody = this.maskBody(parsedBody);
601
+ item[key].body = import_buffer2.Buffer.from(JSON.stringify(maskedBody));
602
+ } catch {
603
+ }
604
+ }
605
+ }
606
+ item.request.headers = this.config.logRequestHeaders ? this.maskHeaders(item.request.headers) : [];
607
+ item.response.headers = this.config.logResponseHeaders ? this.maskHeaders(item.response.headers) : [];
608
+ const url = new URL(item.request.url);
609
+ url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
610
+ item.request.url = url.toString();
611
+ return item;
612
+ }
524
613
  logRequest(request, response, error) {
525
614
  var _a2, _b, _c;
526
615
  if (!this.enabled || this.suspendUntil !== null) return;
@@ -530,64 +619,30 @@ var _RequestLogger = class _RequestLogger {
530
619
  if (this.shouldExcludePath(path) || this.shouldExcludeUserAgent(userAgent) || (((_c = (_b = this.config).excludeCallback) == null ? void 0 : _c.call(_b, request, response)) ?? false)) {
531
620
  return;
532
621
  }
533
- url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
534
- request.url = url.toString();
535
622
  if (!this.config.logRequestBody || !this.hasSupportedContentType(request.headers)) {
536
623
  request.body = void 0;
537
- } else if (request.body) {
538
- if (request.body.length > MAX_BODY_SIZE) {
539
- request.body = BODY_TOO_LARGE;
540
- } else if (this.config.maskRequestBodyCallback) {
541
- try {
542
- request.body = this.config.maskRequestBodyCallback(request) ?? BODY_MASKED;
543
- if (request.body.length > MAX_BODY_SIZE) {
544
- request.body = BODY_TOO_LARGE;
545
- }
546
- } catch {
547
- request.body = void 0;
548
- }
549
- }
550
624
  }
551
625
  if (!this.config.logResponseBody || !this.hasSupportedContentType(response.headers)) {
552
626
  response.body = void 0;
553
- } else if (response.body) {
554
- if (response.body.length > MAX_BODY_SIZE) {
555
- response.body = BODY_TOO_LARGE;
556
- } else if (this.config.maskResponseBodyCallback) {
557
- try {
558
- response.body = this.config.maskResponseBodyCallback(request, response) ?? BODY_MASKED;
559
- if (response.body.length > MAX_BODY_SIZE) {
560
- response.body = BODY_TOO_LARGE;
561
- }
562
- } catch {
563
- response.body = void 0;
564
- }
565
- }
566
627
  }
567
- request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
568
- response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
628
+ if (request.size !== void 0 && request.size < 0) {
629
+ request.size = void 0;
630
+ }
631
+ if (response.size !== void 0 && response.size < 0) {
632
+ response.size = void 0;
633
+ }
569
634
  const item = {
570
635
  uuid: (0, import_crypto3.randomUUID)(),
571
- request: skipEmptyValues(request),
572
- response: skipEmptyValues(response),
636
+ request,
637
+ response,
573
638
  exception: error && this.config.logException ? {
574
639
  type: error.name,
575
640
  message: truncateExceptionMessage(error.message),
576
641
  stacktrace: truncateExceptionStackTrace(error.stack || ""),
577
642
  sentryEventId: getSentryEventId()
578
- } : null
643
+ } : void 0
579
644
  };
580
- [
581
- item.request.body,
582
- item.response.body
583
- ].forEach((body) => {
584
- if (body) {
585
- body.toJSON = function() {
586
- return this.toString("base64");
587
- };
588
- }
589
- });
590
- this.pendingWrites.push(JSON.stringify(item));
645
+ this.pendingWrites.push(item);
591
646
  if (this.pendingWrites.length > MAX_PENDING_WRITES) {
592
647
  this.pendingWrites.shift();
593
648
  }
@@ -601,9 +656,26 @@ var _RequestLogger = class _RequestLogger {
601
656
  this.currentFile = new TempGzipFile();
602
657
  }
603
658
  while (this.pendingWrites.length > 0) {
604
- const item = this.pendingWrites.shift();
659
+ let item = this.pendingWrites.shift();
605
660
  if (item) {
606
- await this.currentFile.writeLine(import_buffer2.Buffer.from(item));
661
+ item = this.applyMasking(item);
662
+ const finalItem = {
663
+ uuid: item.uuid,
664
+ request: skipEmptyValues(item.request),
665
+ response: skipEmptyValues(item.response),
666
+ exception: item.exception
667
+ };
668
+ [
669
+ finalItem.request.body,
670
+ finalItem.response.body
671
+ ].forEach((body) => {
672
+ if (body) {
673
+ body.toJSON = function() {
674
+ return this.toString("base64");
675
+ };
676
+ }
677
+ });
678
+ await this.currentFile.writeLine(import_buffer2.Buffer.from(JSON.stringify(finalItem)));
607
679
  }
608
680
  }
609
681
  });
@@ -1253,10 +1325,14 @@ var parseStack = /* @__PURE__ */ __name(function(stack, basePath, endpoints, ver
1253
1325
  }, "parseStack");
1254
1326
  var getEndpoints = /* @__PURE__ */ __name(function(app, basePath) {
1255
1327
  const endpoints = parseEndpoints(app);
1256
- return endpoints.flatMap((route) => route.methods.filter((method) => ![
1257
- "HEAD",
1258
- "OPTIONS"
1259
- ].includes(method.toUpperCase())).map((method) => ({
1328
+ const standardHttpMethods = [
1329
+ "GET",
1330
+ "POST",
1331
+ "PUT",
1332
+ "DELETE",
1333
+ "PATCH"
1334
+ ];
1335
+ return endpoints.flatMap((route) => route.methods.filter((method) => standardHttpMethods.includes(method.toUpperCase())).map((method) => ({
1260
1336
  method,
1261
1337
  path: (basePath + route.path).replace(/\/\//g, "/")
1262
1338
  })));