fix(unified-session): address code review feedback

Fixes 3 issues identified in code review:

1. ENV VAR PRECEDENCE: Changed DEFAULT_CONNECTION_CONFIG to use
   undefined for enableUnifiedSession, allowing env var
   BAILEYS_UNIFIED_SESSION_ENABLED to take precedence.
   Priority is now: explicit config > env var > default (true)

2. SINGLE INITIALIZATION: UnifiedSessionManager is now created
   only once, after sendNode is defined. This avoids:
   - Duplicate circuit breaker instances
   - State reinitialization
   - Unnecessary overhead and logs

3. CONTINUOUS TIME SYNC: serverTimeOffset is now updated on
   every received frame that contains a 't' attribute, not just
   on CB:success. This keeps the offset accurate even during
   long-running connections.
This commit is contained in:
Claude
2026-01-24 19:14:02 +00:00
parent 9f17567951
commit 22eda03eb2
2 changed files with 23 additions and 17 deletions
+3 -1
View File
@@ -108,7 +108,9 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
// + 10 buffer slots for high-load scenarios = 50 total
maxSocketClientListeners: 50,
// Unified session telemetry (reduces detection of unofficial clients)
enableUnifiedSession: true
// NOTE: undefined means "check env var first, then default to true"
// This allows BAILEYS_UNIFIED_SESSION_ENABLED env var to have precedence
enableUnifiedSession: undefined
}
export const MEDIA_PATH_MAP: { [T in MediaType]?: string } = {
+20 -16
View File
@@ -94,9 +94,16 @@ export const makeSocket = (config: SocketConfig) => {
queryCircuitBreaker: queryCircuitBreakerConfig,
connectionCircuitBreaker: connectionCircuitBreakerConfig,
preKeyCircuitBreaker: preKeyCircuitBreakerConfig,
enableUnifiedSession = shouldEnableUnifiedSession()
// If enableUnifiedSession is explicitly set (true/false), use it
// Otherwise (undefined), check env var, then default to true
enableUnifiedSession: enableUnifiedSessionConfig
} = config
// Resolve enableUnifiedSession: explicit config > env var > default (true)
const enableUnifiedSession = enableUnifiedSessionConfig !== undefined
? enableUnifiedSessionConfig
: shouldEnableUnifiedSession()
// Initialize circuit breakers if enabled
let queryCircuitBreaker: CircuitBreaker | undefined
let connectionCircuitBreaker: CircuitBreaker | undefined
@@ -148,18 +155,8 @@ export const makeSocket = (config: SocketConfig) => {
logger.info('Circuit breakers initialized for socket operations')
}
// Initialize Unified Session Manager for telemetry
// Note: sendNode will be set after it's defined below
// Unified Session Manager will be initialized after sendNode is defined
let unifiedSessionManager: UnifiedSessionManager | undefined
if (enableUnifiedSession) {
unifiedSessionManager = createUnifiedSessionManager({
enabled: true,
logger,
enableCircuitBreaker
// sendNode will be configured after sendNode function is defined
})
logger.info('Unified session manager initialized')
}
const publicWAMBuffer = new BinaryInfo()
@@ -240,19 +237,19 @@ export const makeSocket = (config: SocketConfig) => {
return sendRawMessage(buff)
}
// Configure unified session manager with sendNode now that it's defined
if (unifiedSessionManager) {
// Create a wrapper that matches the expected signature
// Initialize Unified Session Manager now that sendNode is defined
// (single initialization to avoid duplicating circuit breakers and state)
if (enableUnifiedSession) {
const sendNodeForSession = async (node: BinaryNode): Promise<void> => {
await sendNode(node)
}
// Recreate with proper sendNode
unifiedSessionManager = createUnifiedSessionManager({
enabled: true,
logger,
enableCircuitBreaker,
sendNode: sendNodeForSession
})
logger.info('Unified session manager initialized')
}
/** Send unified_session telemetry */
@@ -742,6 +739,13 @@ export const makeSocket = (config: SocketConfig) => {
if (!(frame instanceof Uint8Array)) {
const msgId = frame.attrs.id
// Update server time offset from any node with timestamp 't'
// This keeps the offset accurate even after long connections
const serverTime = extractServerTime(frame)
if (serverTime) {
unifiedSessionManager?.updateServerTimeOffset(serverTime)
}
if (logger.level === 'trace') {
logger.trace({ xml: binaryNodeToString(frame), msg: 'recv xml' })
}