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
This commit is contained in:
Claude
2026-01-20 02:05:14 +00:00
parent a934be9bc9
commit dd59a7fe65
6 changed files with 35 additions and 19 deletions
+2 -1
View File
@@ -321,7 +321,8 @@ export class BaileysEventStream extends EventEmitter {
// Find correct position // Find correct position
let insertIndex = this.buffer.length let insertIndex = this.buffer.length
for (let i = 0; i < this.buffer.length; i++) { 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 insertIndex = i
break break
} }
+11 -4
View File
@@ -331,8 +331,13 @@ export class BaileysLogger implements ILogger {
...(typeof sanitizedObj === 'object' && sanitizedObj !== null ? sanitizedObj : { value: sanitizedObj }), ...(typeof sanitizedObj === 'object' && sanitizedObj !== null ? sanitizedObj : { value: sanitizedObj }),
} }
// Structured log // Structured log (skip if level is 'silent')
this.structuredLogger[level](enrichedObj, msg) 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 // Event handler
if (this.config.eventHandler) { if (this.config.eventHandler) {
@@ -429,8 +434,10 @@ export class BaileysLogger implements ILogger {
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
// In production, mask part of the number // In production, mask part of the number
const parts = jid.split('@') const parts = jid.split('@')
if (parts.length === 2 && parts[0].length > 4) { const localPart = parts[0]
return `${parts[0].substring(0, 4)}****@${parts[1]}` const domain = parts[1]
if (parts.length === 2 && localPart && domain && localPart.length > 4) {
return `${localPart.substring(0, 4)}****@${domain}`
} }
} }
return jid return jid
+3 -2
View File
@@ -500,11 +500,12 @@ export function withCache<T extends (...args: unknown[]) => unknown>(
/** /**
* Global singleton cache by namespace * Global singleton cache by namespace
*/ */
const globalCaches: Map<string, Cache<unknown>> = new Map() // eslint-disable-next-line @typescript-eslint/no-explicit-any
const globalCaches: Map<string, Cache<any>> = new Map()
export function getGlobalCache<V>(namespace: string, options?: CacheOptions<V>): Cache<V> { export function getGlobalCache<V>(namespace: string, options?: CacheOptions<V>): Cache<V> {
if (!globalCaches.has(namespace)) { if (!globalCaches.has(namespace)) {
globalCaches.set(namespace, new Cache<V>({ ...options, namespace })) globalCaches.set(namespace, new Cache<V>({ ...options, namespace }) as Cache<V>)
} }
return globalCaches.get(namespace) as Cache<V> return globalCaches.get(namespace) as Cache<V>
} }
+7 -3
View File
@@ -120,7 +120,7 @@ export class LoggerAdapter implements ILogger {
} }
if (this.config.levelMapping && level in this.config.levelMapping) { 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' return (level as LogLevel) || 'info'
@@ -327,8 +327,12 @@ function createLogMethod(
return (obj: unknown, msg?: string) => { return (obj: unknown, msg?: string) => {
if (typeof logger[level] === 'function') { if (typeof logger[level] === 'function') {
;(logger[level] as (obj: unknown, msg?: string) => void)(obj, msg) ;(logger[level] as (obj: unknown, msg?: string) => void)(obj, msg)
} else if (typeof (console as Record<string, unknown>)[level] === 'function') { } else {
;(console as Record<string, (...args: unknown[]) => void>)[level](obj, msg) // Fallback to console methods
const consoleMethod = (console as unknown as Record<string, ((...args: unknown[]) => void) | undefined>)[level]
if (typeof consoleMethod === 'function') {
consoleMethod(obj, msg)
}
} }
} }
} }
+6 -6
View File
@@ -566,7 +566,7 @@ export class Summary implements BaseMetric {
* Metrics registry * Metrics registry
*/ */
export class MetricsRegistry { export class MetricsRegistry {
private metrics: Map<string, Counter | Gauge | Histogram | Summary> = new Map() private metricsMap: Map<string, Counter | Gauge | Histogram | Summary> = new Map()
private prefix: string private prefix: string
private defaultLabels: Labels private defaultLabels: Labels
@@ -580,7 +580,7 @@ export class MetricsRegistry {
*/ */
register<T extends Counter | Gauge | Histogram | Summary>(metric: T): T { register<T extends Counter | Gauge | Histogram | Summary>(metric: T): T {
const fullName = this.prefix ? `${this.prefix}_${metric.name}` : metric.name const fullName = this.prefix ? `${this.prefix}_${metric.name}` : metric.name
this.metrics.set(fullName, metric) this.metricsMap.set(fullName, metric)
return metric return metric
} }
@@ -589,7 +589,7 @@ export class MetricsRegistry {
*/ */
get(name: string): Counter | Gauge | Histogram | Summary | undefined { get(name: string): Counter | Gauge | Histogram | Summary | undefined {
const fullName = this.prefix ? `${this.prefix}_${name}` : name 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 { remove(name: string): boolean {
const fullName = this.prefix ? `${this.prefix}_${name}` : name const fullName = this.prefix ? `${this.prefix}_${name}` : name
return this.metrics.delete(fullName) return this.metricsMap.delete(fullName)
} }
/** /**
* Reset all metrics * Reset all metrics
*/ */
resetAll(): void { resetAll(): void {
for (const metric of this.metrics.values()) { for (const metric of this.metricsMap.values()) {
metric.reset() metric.reset()
} }
} }
@@ -615,7 +615,7 @@ export class MetricsRegistry {
async metrics(): Promise<string> { async metrics(): Promise<string> {
const lines: string[] = [] 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(`# HELP ${name} ${metric.help}`)
lines.push(`# TYPE ${name} ${metric.type}`) lines.push(`# TYPE ${name} ${metric.type}`)
+6 -3
View File
@@ -554,11 +554,14 @@ export function extractTraceHeaders(headers: Record<string, string | undefined>)
} }
// Parse baggage // Parse baggage
if (headers[TRACE_HEADERS.BAGGAGE]) { const baggageHeader = headers[TRACE_HEADERS.BAGGAGE]
if (baggageHeader) {
options.baggage = {} options.baggage = {}
const pairs = headers[TRACE_HEADERS.BAGGAGE].split(',') const pairs = baggageHeader.split(',')
for (const pair of pairs) { 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) { if (key && value) {
options.baggage[key.trim()] = decodeURIComponent(value.trim()) options.baggage[key.trim()] = decodeURIComponent(value.trim())
} }