Commit Graph

2195 Commits

Author SHA1 Message Date
Claude b719349c57 fix(retry): address PR #12 code review feedback
- Add 'stepped' backoff strategy that uses RETRY_BACKOFF_DELAYS directly
- Update rsocket config to use 'stepped' instead of 'exponential'
- Clarify maxWebSocketListeners calculation (8 core + 10 dynamic + 2 buffer)
- Document maxSocketClientListeners calculation (20 core + 20 dynamic + 10 buffer)
- Change 'exponential backoff' to 'custom progressive backoff' in docs
- Add comprehensive tests for getRetryDelayWithJitter and getAllRetryDelaysWithJitter
- Fix existing test type errors (onRetry mock, always/never predicates)
2026-01-20 13:05:13 +00:00
Claude 2d84da8bd7 fix(retry): resolve circular dependency with Defaults module
Define RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR locally in
retry-utils.ts to avoid circular dependency that caused
"Cannot access 'RETRY_BACKOFF_DELAYS' before initialization" error.
2026-01-20 12:37:18 +00:00
Claude 43ece09fe9 feat(metrics): integrate prom-client for native Prometheus support
- Add prom-client ^15.1.3 as production dependency
- Migrate Counter class to use prom-client internally
- Migrate Gauge class to use prom-client internally
- Migrate Histogram class to use prom-client internally
- Update getMetricsOutput() to use prom-client's native metrics()
- Update contentType() to use prom-client's contentType
- Enable collectDefaultMetrics() for Node.js system metrics
- Maintain backward compatibility with existing API

Benefits:
- Native OpenMetrics format export
- Proper histogram bucket handling
- Default Node.js metrics (memory, CPU, event loop, GC)
- Better integration with Grafana/Prometheus ecosystem
2026-01-20 12:37:17 +00:00
Claude e605a02528 fix(defaults): address PR #10 code review feedback
- Integrate RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR into retry-utils.ts
  - Add retryConfigs.rsocket using the default delays
  - Add getRetryDelayWithJitter() helper function
  - Add getAllRetryDelaysWithJitter() for planning
- Document DEFAULT_CACHE_MAX_KEYS with usage example
- Adjust INITIAL_PREKEY_COUNT from 30 to 200 (moderate value)
- Adjust MIN_UPLOAD_INTERVAL from 60s to 10s (balance rate limiting/responsiveness)
- Revert syncFullHistory to true (avoid breaking change)
2026-01-20 12:36:52 +00:00
Claude 6f75e5ac21 feat(socket): add configurable maxListeners to prevent memory leaks
Based on RSocket's battle-tested configuration:

- Add maxWebSocketListeners config option (default: 20)
  - 8 base WS events + 10 dynamic listeners + 2 buffer slots
- Add maxSocketClientListeners config option (default: 50)
- Replace dangerous setMaxListeners(0) with configurable limits
- Add warning log if user explicitly sets limit to 0

BREAKING: Previous behavior used setMaxListeners(0) which removed
all limits. Now defaults to safe limits but can be overridden via config.
2026-01-20 12:36:51 +00:00
Claude 682b62acbf feat(defaults): apply RSocket's battle-tested configurations
- Add DEFAULT_CACHE_MAX_KEYS with limits per store type (prevents memory leaks)
- Add RETRY_BACKOFF_DELAYS [1s, 2s, 5s, 10s, 20s] for exponential backoff
- Add RETRY_JITTER_FACTOR (0.15) to prevent thundering herd
- Change INITIAL_PREKEY_COUNT from 812 to 30 (safer for rate limiting)
- Change MIN_UPLOAD_INTERVAL from 5s to 60s (avoids rate limiting)
- Change syncFullHistory default to false (conservative approach)

Based on RSocket fork's production-tested configuration.
2026-01-20 12:36:51 +00:00
Claude 4e68f9885c fix(lid-mapping): address PR #9 code review feedback
- Add bounds validation for config values (prevent DoS via env vars)
- Replace ?? false with || false in isValidMapping (correct operator)
- Rename deleteMapping to deleteMappingFromCache (clarify behavior)
- Keep deleteMapping as deprecated alias for backward compatibility
- Document destroyed state handling in read-only methods
- Document async metrics module loading race condition
- Document storeLIDPNMappings return type change
- Add failure tracking and warning in getLIDsForPNs
- Document exponential backoff retry pattern in config and method
2026-01-20 05:04:25 +00:00
Claude ab96bb69db feat(structured-logger): enhance with enterprise-grade features
- Add environment variable configuration (BAILEYS_LOG_*)
- Implement log buffering for batch writes with configurable flush interval
- Add rate limiting using token bucket algorithm to prevent log flooding
- Implement async logging queue for non-blocking operations
- Add circuit breaker pattern for external hook failures
- Add destroy() method for proper resource cleanup
- Add comprehensive statistics tracking (metrics)
- Add Prometheus metrics integration support
- Improve type safety with ResolvedLoggerConfig type
2026-01-20 04:57:41 +00:00
Claude 0688a5ec4a feat(lid-mapping): enhance with enterprise-grade features
Major improvements to LIDMappingStore:

Configuration (BAILEYS_LID_* env vars):
- BAILEYS_LID_CACHE_TTL_MS: Cache TTL (default: 3 days)
- BAILEYS_LID_MAX_CACHE_SIZE: Max cache entries (default: 50000)
- BAILEYS_LID_BATCH_SIZE: Batch size for bulk ops (default: 100)
- BAILEYS_LID_RETRY_ATTEMPTS: Retry attempts (default: 3)
- BAILEYS_LID_RETRY_DELAY_MS: Retry delay (default: 1000)
- BAILEYS_LID_METRICS: Enable Prometheus metrics
- BAILEYS_LID_DEBUG: Enable debug logging

New Features:
- Comprehensive statistics tracking (getStatistics())
- Cache warming capability (warmCache())
- Cache management (clearCache(), getCacheInfo())
- Batch operations for bulk mappings
- Retry logic with exponential backoff
- Custom error types (LIDMappingError, LIDMappingErrorCode)
- Proper resource cleanup (destroy())
- hasMappingForPN() for existence checks
- deleteMapping() for cache invalidation

Enterprise Patterns:
- Environment variable configuration
- Statistics and metrics integration
- Retry with exponential backoff
- Destroyed state protection
- Batch processing for performance
- Detailed error codes and messages
2026-01-20 04:42:08 +00:00
Claude 33b44ff19d fix(metrics): clear startPromise on stop() to allow restart
FIX: startPromise was never cleared after stop(), causing start()
to return the old resolved promise instead of creating a new server.

This broke any flow that stops and restarts metrics (config reloads, tests).

Now stop() clears startPromise, allowing proper server restart.
2026-01-20 04:26:27 +00:00
Claude 21b632592a fix(metrics): address PR #6 code review feedback
Fixes based on Copilot AI and Codex code review comments:

1. Race Condition Fix (lines 1065-1069):
   - BEFORE: setTimeout(100ms) was incomplete - callers could resolve
     before server was actually ready
   - FIX: Cache the Promise from first start() call using startPromise
   - All concurrent calls now return the same Promise
   - Properly handles both success and error cases

2. CPU Percentage Calculation (lines 993-996):
   - BEFORE: Division by cpuCount gave artificially low values
     (e.g., 25% instead of 100% on 4-core for single-threaded process)
   - FIX: Removed cpuCount division
   - Now follows Unix/Prometheus standard: 100% = 1 core fully used
   - Values can exceed 100% on multi-core (200% = 2 cores at 100%)
   - Updated metric description to clarify this behavior
2026-01-20 04:22:00 +00:00
Claude 834cb75ba4 fix(metrics): address PR #4 code review feedback
Fixes based on Copilot AI and Codex code review comments:

1. CPU Usage Metric (line ~930-933):
   - FIX: process.cpuUsage() now calculates delta between measurements
   - Returns actual percentage (0-100) instead of cumulative seconds
   - Normalized across CPU cores

2. Duplicate SystemMetricsCollector (line ~1353-1355):
   - FIX: Removed duplicate instantiation from PrometheusMetricsManager
   - Now uses MetricsServer's collector via getSystemCollector()

3. Collection Interval (line ~1006-1008):
   - FIX: Added collectIntervalMs to MetricsConfig
   - Configurable via BAILEYS_PROMETHEUS_COLLECT_INTERVAL_MS

4. Error Handling (line ~1021-1022):
   - FIX: Enhanced error messages with structured JSON response
   - Includes error message, timestamp, and logs to console

5. Server Binding Security (line ~1034-1038):
   - FIX: Default host changed from 0.0.0.0 to 127.0.0.1
   - Configurable via BAILEYS_PROMETHEUS_HOST
   - Warning shown if exposed on all interfaces

6. Race Condition (line ~998-1001):
   - FIX: Added isStarting flag to prevent concurrent initialization
   - Multiple concurrent start() calls now handled safely
2026-01-20 04:12:58 +00:00
Claude 11e7c71fd1 feat(event-buffer): enhance with enterprise-grade features
- Add environment variable configuration (BAILEYS_BUFFER_*)
- Implement destroy() method for proper resource cleanup
- Add force flush capability with flush(force) parameter
- Implement buffer overflow detection and prevention
- Add adaptive timeout algorithm based on event rate
- Replace historyCache.clear() with LRU cache cleanup
- Integrate Prometheus metrics for buffer monitoring
- Add isDestroyed state protection
- Fix createBufferedFunction to prevent orphaned timers
- Add comprehensive statistics tracking (getStatistics())
- Add configuration accessor (getConfig())

Enterprise features:
- Configurable buffer size limits (maxBufferSize)
- Configurable timeout ranges (min/max)
- LRU cleanup ratio configuration
- Buffer overflow warning threshold
- Adaptive timeout based on event rate
2026-01-20 03:45:52 +00:00
Claude 8a705e5a1e fix(metrics): update env vars to match BAILEYS_PROMETHEUS_* convention
- Support BAILEYS_PROMETHEUS_ENABLED, BAILEYS_PROMETHEUS_PORT, etc.
- Add support for BAILEYS_PROMETHEUS_LABELS JSON configuration
- Maintain backward compatibility with METRICS_* prefix
- Update default port to 9092 (matching user's .env)
- Enable by opt-in (enabled=false by default)
- Improved startup log with labels info
2026-01-20 03:19:32 +00:00
Claude 0c173212fd feat(metrics): enhance Prometheus metrics with enterprise-grade features
- Add HTTP server for /metrics endpoint with configurable port
- Add environment variable configuration (METRICS_ENABLED, METRICS_PORT, etc.)
- Add SystemMetricsCollector for process/system metrics (CPU, memory, load avg)
- Add Event Buffer metrics (size, capacity, utilization, flushes, overflows)
- Add Adaptive Flush metrics (interval, adjustments, throughput, backpressure)
- Add Circuit Breaker metrics (state, trips, recoveries, rejections)
- Add Query metrics (latency, count, timeouts)
- Add Presence and Group metrics
- Add History Sync metrics
- Add helper functions (trackDuration, trackDurationAsync, trackOperation)
- Add Express middleware support
- Add global metrics manager with initialize/shutdown lifecycle
- Total metrics increased from 18 to 50+
2026-01-20 03:14:44 +00:00
Claude 5714e8e940 merge: resolve conflicts with master 2026-01-20 02:38:37 +00:00
Claude f1bee96e68 chore: update lock files 2026-01-20 02:32:15 +00:00
Claude 61f5f76ef0 feat(socket): integrate circuit breaker with Socket operations
- Add circuit breaker configuration options to SocketConfig type
- Initialize circuit breakers for query, connection, and pre-key operations
- Wrap query() with circuit breaker protection for all WhatsApp queries
- Wrap sendRawMessage() with connection circuit breaker
- Integrate pre-key circuit breaker with uploadPreKeys()
- Add circuit breaker utilities to socket return object (stats, reset)
- Clean up circuit breakers on connection close
- Fix TypeScript errors in utility files (prometheus-metrics, logger-adapter, etc.)

Circuit breakers provide:
- Sliding window failure tracking
- Automatic state transitions (closed -> open -> half-open)
- Configurable thresholds and timeouts
- Prometheus metrics integration
- Event callbacks for monitoring
2026-01-20 02:31:23 +00:00
Renato Alcara 5edb7d6fa2 Merge pull request #2
## Summary
- Fixed TypeScript compilation errors that were preventing npm installation from git

## Changes
- **baileys-event-stream.ts**: Add null check for buffer item in priority insertion
- **baileys-logger.ts**: Handle 'silent' log level properly and fix JID sanitization with undefined checks
- **cache-utils.ts**: Fix type incompatibility in global cache by using `any` type for Map
- **logger-adapter.ts**: Add nullish coalescing for level mapping and fix console type casting
- **prometheus-metrics.ts**: Rename private `metrics` property to `metricsMap` to avoid conflict with `metrics()` method
- **trace-context.ts**: Store baggage header in variable before accessing to fix undefined check

## Test plan
- [x] `npm run build` completes successfully without TypeScript errors
- [x] Package can be installed via `npm i @whiskeysockets/baileys@git+https://github.com/rsalcara/InfiniteAPI.git#claude/fix-npm-dependency-kTdUe`
2026-01-19 23:09:21 -03:00
Claude f72b9fc4e7 chore: update lock files after dependency installation 2026-01-20 02:05:59 +00:00
Claude dd59a7fe65 fix(utils): resolve TypeScript compilation errors
- baileys-event-stream.ts: Add null check for buffer item in priority insertion
- baileys-logger.ts: Handle 'silent' log level and fix JID sanitization
- cache-utils.ts: Fix type incompatibility in global cache with any type
- logger-adapter.ts: Add nullish coalescing for level mapping and fix console type cast
- prometheus-metrics.ts: Rename private 'metrics' property to 'metricsMap' to avoid conflict with method
- trace-context.ts: Store baggage header in variable before accessing to fix undefined check
2026-01-20 02:05:14 +00:00
Renato Alcara a934be9bc9 feat(utils): add observability and resilience utilities
## Summary

This PR adds a comprehensive set of observability and resilience utilities ported from RSocket, with all documentation translated to English.

### Changes

- **Structured Logger** (`structured-logger.ts`) - ILogger-compatible structured logging with context and metadata
- **Baileys Logger** (`baileys-logger.ts`) - Pino-compatible logger adapter for Baileys integration
- **Trace Context** (`trace-context.ts`) - Distributed tracing with AsyncLocalStorage, span tracking, and header propagation
- **Prometheus Metrics** (`prometheus-metrics.ts`) - Counter, Gauge, Histogram, and Summary metrics for monitoring
- **Cache Utils** (`cache-utils.ts`) - LRU cache with TTL, hit/miss metrics, and multi-level cache support
- **Circuit Breaker** (`circuit-breaker.ts`) - Enhanced with:
  - Sliding window failure tracking
  - Volume threshold configuration
  - Factory functions for WhatsApp-specific use cases (PreKey, Connection, Message)
- **Retry Utils** (`retry-utils.ts`) - Exponential backoff, jitter, Fibonacci strategies with cancellation support
- **Event Stream** (`baileys-event-stream.ts`) - Event streaming with backpressure and dead letter queue

### Technical Details

- All comments translated from Portuguese to English
- TypeScript strict mode compatible
- ESM module exports
- Factory functions for common circuit breaker patterns

### Stats

- 9 files changed
- 852 insertions(+)
- 601 deletions(-)
2026-01-19 22:55:48 -03:00
Claude 64c7712da7 refactor(utils): translate all comments to English and enhance circuit breaker
- Translate all Portuguese comments to English in utility modules
- Enhance circuit-breaker.ts with sliding window failure tracking
- Add factory functions for WhatsApp-specific circuit breakers
  (createPreKeyCircuitBreaker, createConnectionCircuitBreaker, createMessageCircuitBreaker)
- Improve volume threshold support for circuit breaker decisions
- Standardize documentation across all utility files

Files updated:
- structured-logger.ts: English comments
- baileys-logger.ts: English comments
- trace-context.ts: English comments
- prometheus-metrics.ts: English comments
- cache-utils.ts: English comments
- circuit-breaker.ts: Enhanced with sliding window + English comments
- retry-utils.ts: English comments
- baileys-event-stream.ts: English comments
- index.ts: English comments
2026-01-20 01:48:23 +00:00
Claude 882a1f57cf feat(utils): add observability and resilience utilities
Implement comprehensive utility modules for InfiniteAPI:

Logging:
- structured-logger: JSON logging with levels, sanitization, metrics
- logger-adapter: Pino/Console/Structured adapter pattern
- baileys-logger: WhatsApp-specific logger with categories

Observability:
- trace-context: Distributed tracing with spans, correlation IDs
- prometheus-metrics: Counter, Gauge, Histogram, Summary metrics

Resilience:
- cache-utils: LRU cache with TTL, multi-level support
- circuit-breaker: 3-state circuit breaker with fallbacks
- retry-utils: Exponential backoff with jitter, predicates

Event Streaming:
- baileys-event-stream: Priority queues, backpressure, DLQ

Includes unit tests for all new modules.
2026-01-20 01:26:06 +00:00
Rajeh Taher 5b5ac85443 example: revamp example and add options for unit tests 2026-01-18 17:53:09 +02:00
Rajeh Taher c4c22c9628 example: customizable socket URL 2026-01-18 17:26:17 +02:00
Rajeh Taher de6432cf5a example: improve ping-pong 2026-01-18 17:17:50 +02:00
Rajeh Taher b62cb40a70 chore: lint 2026-01-18 17:16:28 +02:00
Rajeh Taher 282f065f53 connection-deadlock, socket: improve socket end conditions 2026-01-18 17:16:28 +02:00
Vrypt db86cdc545 fix(proto-extract): regenerate corrupted yarn.lock to restore install process (#1981)
* fix(proto-extract): regenerate corrupted yarn.lock to restore install process

* chore(proto-extract): update acorn parser to latest version for compatibility with new WhatsApp JS syntax

* Update baileys version to 2.3000.1029027441

* Update version number in Defaults index

* Revert WAProto.proto to resolve merge conflict and restore expected structure

---------

Co-authored-by: Vrypt <vryptt@gmail.com>
Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
2026-01-18 01:45:23 +02:00
YonkoSam a2677c89dd Fix critical memory leak in event buffer (#2160) 2026-01-18 01:44:28 +02:00
Rajeh Taher 56ee829757 process-message: remove timeout before event emit 2026-01-18 01:35:50 +02:00
Rajeh Taher e740fc5b4a messages-send: revamp message type function 2026-01-18 01:35:50 +02:00
Rajeh Taher 2ec9d9ff1d gitignore: fix ignoring logs coming from example file 2026-01-18 01:34:55 +02:00
Rajeh Taher 83e0f22af8 messages-recv: decrease PDO response timeout 2026-01-18 01:34:39 +02:00
Rajeh Taher b1c76ebe2d chore: lint+bugfix 2026-01-18 01:31:56 +02:00
João Lucas de Oliveira Lopes e53fa7b8e5 Feat improve testing coverage e2e (#1799)
* fix: ensure proper socket closure and await connection termination in tests

* feat(tests): enhance E2E tests for image and video message handling, including downloads and group interactions
2026-01-18 01:16:24 +02:00
vini 0b3b2a88cd fix(messages): enhance nullish value checks in message content generation (#2180) 2026-01-18 01:11:33 +02:00
YonkoSam 1408499d7c moved retryCount before validating the session (#2167) 2026-01-18 01:11:10 +02:00
Matheus Filype 349e7bd771 feat: send tctoken to profile update and presence subscribe (#2257)
* fix: improve message resend logic by adding checks for message IDs

* Revert "fix: improve message resend logic by adding checks for message IDs"

This reverts commit c03f9d8e6fc6cbfbb9d1f8f67c169700e704213d.

* feat(tc-token): implement buildTcTokenFromJid utility and integrate into chats socket

* fix(tc-token): ensure consistent return value when tcTokenBuffer is absent

* fix(chats): update import path for buildTcTokenFromJid utility
2026-01-18 01:07:53 +02:00
João Lucas de Oliveira Lopes d4ef73aca5 feat: implement automated WhatsApp version update workflow and related scripts (#2130)
* feat: implement automated WhatsApp version update workflow and related scripts

* change cron to weekly

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
2026-01-18 01:07:05 +02:00
João Lucas de Oliveira Lopes 9611a1a982 fix(WAProto): Handle string values in long fields during JSON serialization (#1991)
* feat: add patch-tojson functionality for improved proto serialization

* Remove patch-tojson functionality and its import from the main index file to streamline the codebase.

* refactor: simplify longToString and longToNumber functions for better readability and performance
2026-01-14 08:33:45 +02:00
vini 432c26a07c fix(messages): handle encryption failures per recipient and fail when all fail (#2226) 2026-01-08 22:07:11 +02:00
vini c392d4ce6c feat: add support for FB and Interop JID encoding/decoding and empty strings (#2189) 2026-01-08 21:56:50 +02:00
Ibrahim Pelumi Lasisi 720cc688d6 fix: avoid variable shadowing and preserve empty business profile fields (#2183)
* Update business.ts

* chore: fix lint issues
2026-01-08 21:56:13 +02:00
Gustavo Quadri 81d9c12e86 fix: getmessagetype to ensure consistency with whatsapp behavior (#2245) 2026-01-08 21:51:34 +02:00
João Lucas de Oliveira Lopes c9c3481817 implement message reporting tokens (#1906)
* feat: implement message reporting tokens and privacy token handling

* feat: add support for privacy tokens in profile picture requests and history sync

* chore: pr feedback purpshell

* fix: improve privacy token handling and error messaging in socket configuration

* feat: enhance privacy token handling with improved sender mapping

* chore: removing tc token in favor of #2080

* chore: revert some unecessary changes

* feat(reporting): enhance reporting token extraction and compilation logic

* feat(reporting): add unit tests for reporting token utilities

* fix(reporting): streamline reporting token attachment logic in message sending

* fix: adjust reporting token inclusion logic to prevent retries

* chore: add return type to shouldIncludeReportingToken and improve getToken function type safety
2026-01-08 21:50:49 +02:00
Skid 4e681d3219 feat: Verify leaf signature (#2208)
* Update WA_CERT_DETAILS with issuer and public key

* certificate validation

* fix:lint

* padding

* lint: fix tab

---------

Co-authored-by: skidy89 <tutorialesyg310@gmail.com>
2026-01-08 21:49:37 +02:00
Matheus Filype 250477497d Add Feature LabelMember (Based on #2164) (#2198)
* fix: improve message resend logic by adding checks for message IDs

* Revert "fix: improve message resend logic by adding checks for message IDs"

This reverts commit c03f9d8e6fc6cbfbb9d1f8f67c169700e704213d.

* feat: add group member label update functionality and event emission

* feat: refactor updateMemberLabel function for improved readability

* feat: use optional chaining for label association message in processMessage

* feat: add updateMemberLabel to makeMessagesSocket for enhanced functionality

* fix: correct log message for group member tag update event

Co-authored-by: FgsiDev
2025-12-19 22:00:48 +02:00
vini 925ed6a7b3 feat(appstate): emit setting events (#2057) 2025-12-15 01:21:44 +02:00