From dd59a7fe655f2eabefe0cf489eeec08f36353b96 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Jan 2026 02:05:14 +0000 Subject: [PATCH] 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 --- src/Utils/baileys-event-stream.ts | 3 ++- src/Utils/baileys-logger.ts | 15 +++++++++++---- src/Utils/cache-utils.ts | 5 +++-- src/Utils/logger-adapter.ts | 10 +++++++--- src/Utils/prometheus-metrics.ts | 12 ++++++------ src/Utils/trace-context.ts | 9 ++++++--- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/Utils/baileys-event-stream.ts b/src/Utils/baileys-event-stream.ts index 2a62d18b..d5d2c3cf 100644 --- a/src/Utils/baileys-event-stream.ts +++ b/src/Utils/baileys-event-stream.ts @@ -321,7 +321,8 @@ export class BaileysEventStream extends EventEmitter { // Find correct position let insertIndex = this.buffer.length for (let i = 0; i < this.buffer.length; i++) { - if (PRIORITY_VALUES[this.buffer[i].priority] > eventPriorityValue) { + const bufferItem = this.buffer[i] + if (bufferItem && PRIORITY_VALUES[bufferItem.priority] > eventPriorityValue) { insertIndex = i break } diff --git a/src/Utils/baileys-logger.ts b/src/Utils/baileys-logger.ts index 54400338..486c4fbb 100644 --- a/src/Utils/baileys-logger.ts +++ b/src/Utils/baileys-logger.ts @@ -331,8 +331,13 @@ export class BaileysLogger implements ILogger { ...(typeof sanitizedObj === 'object' && sanitizedObj !== null ? sanitizedObj : { value: sanitizedObj }), } - // Structured log - this.structuredLogger[level](enrichedObj, msg) + // Structured log (skip if level is 'silent') + if (level !== 'silent') { + const logMethod = this.structuredLogger[level] as ((obj: unknown, msg?: string) => void) | undefined + if (logMethod) { + logMethod.call(this.structuredLogger, enrichedObj, msg) + } + } // Event handler if (this.config.eventHandler) { @@ -429,8 +434,10 @@ export class BaileysLogger implements ILogger { if (process.env.NODE_ENV === 'production') { // In production, mask part of the number const parts = jid.split('@') - if (parts.length === 2 && parts[0].length > 4) { - return `${parts[0].substring(0, 4)}****@${parts[1]}` + const localPart = parts[0] + const domain = parts[1] + if (parts.length === 2 && localPart && domain && localPart.length > 4) { + return `${localPart.substring(0, 4)}****@${domain}` } } return jid diff --git a/src/Utils/cache-utils.ts b/src/Utils/cache-utils.ts index 22772d22..205cd28f 100644 --- a/src/Utils/cache-utils.ts +++ b/src/Utils/cache-utils.ts @@ -500,11 +500,12 @@ export function withCache unknown>( /** * Global singleton cache by namespace */ -const globalCaches: Map> = new Map() +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const globalCaches: Map> = new Map() export function getGlobalCache(namespace: string, options?: CacheOptions): Cache { if (!globalCaches.has(namespace)) { - globalCaches.set(namespace, new Cache({ ...options, namespace })) + globalCaches.set(namespace, new Cache({ ...options, namespace }) as Cache) } return globalCaches.get(namespace) as Cache } diff --git a/src/Utils/logger-adapter.ts b/src/Utils/logger-adapter.ts index 141d7188..8d7d3b4b 100644 --- a/src/Utils/logger-adapter.ts +++ b/src/Utils/logger-adapter.ts @@ -120,7 +120,7 @@ export class LoggerAdapter implements ILogger { } if (this.config.levelMapping && level in this.config.levelMapping) { - return this.config.levelMapping[level] + return this.config.levelMapping[level] ?? 'info' } return (level as LogLevel) || 'info' @@ -327,8 +327,12 @@ function createLogMethod( return (obj: unknown, msg?: string) => { if (typeof logger[level] === 'function') { ;(logger[level] as (obj: unknown, msg?: string) => void)(obj, msg) - } else if (typeof (console as Record)[level] === 'function') { - ;(console as Record void>)[level](obj, msg) + } else { + // Fallback to console methods + const consoleMethod = (console as unknown as Record void) | undefined>)[level] + if (typeof consoleMethod === 'function') { + consoleMethod(obj, msg) + } } } } diff --git a/src/Utils/prometheus-metrics.ts b/src/Utils/prometheus-metrics.ts index 70e1863b..d35db233 100644 --- a/src/Utils/prometheus-metrics.ts +++ b/src/Utils/prometheus-metrics.ts @@ -566,7 +566,7 @@ export class Summary implements BaseMetric { * Metrics registry */ export class MetricsRegistry { - private metrics: Map = new Map() + private metricsMap: Map = new Map() private prefix: string private defaultLabels: Labels @@ -580,7 +580,7 @@ export class MetricsRegistry { */ register(metric: T): T { const fullName = this.prefix ? `${this.prefix}_${metric.name}` : metric.name - this.metrics.set(fullName, metric) + this.metricsMap.set(fullName, metric) return metric } @@ -589,7 +589,7 @@ export class MetricsRegistry { */ get(name: string): Counter | Gauge | Histogram | Summary | undefined { const fullName = this.prefix ? `${this.prefix}_${name}` : name - return this.metrics.get(fullName) + return this.metricsMap.get(fullName) } /** @@ -597,14 +597,14 @@ export class MetricsRegistry { */ remove(name: string): boolean { const fullName = this.prefix ? `${this.prefix}_${name}` : name - return this.metrics.delete(fullName) + return this.metricsMap.delete(fullName) } /** * Reset all metrics */ resetAll(): void { - for (const metric of this.metrics.values()) { + for (const metric of this.metricsMap.values()) { metric.reset() } } @@ -615,7 +615,7 @@ export class MetricsRegistry { async metrics(): Promise { const lines: string[] = [] - for (const [name, metric] of this.metrics) { + for (const [name, metric] of this.metricsMap) { lines.push(`# HELP ${name} ${metric.help}`) lines.push(`# TYPE ${name} ${metric.type}`) diff --git a/src/Utils/trace-context.ts b/src/Utils/trace-context.ts index 398baa49..9d87d501 100644 --- a/src/Utils/trace-context.ts +++ b/src/Utils/trace-context.ts @@ -554,11 +554,14 @@ export function extractTraceHeaders(headers: Record) } // Parse baggage - if (headers[TRACE_HEADERS.BAGGAGE]) { + const baggageHeader = headers[TRACE_HEADERS.BAGGAGE] + if (baggageHeader) { options.baggage = {} - const pairs = headers[TRACE_HEADERS.BAGGAGE].split(',') + const pairs = baggageHeader.split(',') for (const pair of pairs) { - const [key, value] = pair.split('=') + const parts = pair.split('=') + const key = parts[0] + const value = parts[1] if (key && value) { options.baggage[key.trim()] = decodeURIComponent(value.trim()) }