fix: address code review feedback (batch 2)
1. Add logger to writeCacheFile (version-cache.ts) - Now logs warnings when file write fails - Helps debugging in production environments 2. Sanitize logging in parseGroupResult (communities.ts) - Changed from info to debug level - Removed full node/groupNode dumps (sensitive data) - Now only logs nodeTag and groupId 3. Add input validation in parseNewsletterCreateResponse (newsletter.ts) - Validates response structure before destructuring - Adds fallback values for parseInt (prevents NaN) - Adds null checks for optional fields 4. Add health-status.ts module - getHealthStatus(): Full health check with circuit breakers, cache, memory - isHealthy(): Simple boolean for liveness probes - getSimpleHealthStatus(): Returns 'ok', 'degraded', or 'error' - Useful for k8s probes and monitoring dashboards
This commit is contained in:
@@ -78,11 +78,11 @@ export const makeCommunitiesSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
async function parseGroupResult(node: BinaryNode) {
|
||||
logger.info({ node }, 'parseGroupResult')
|
||||
logger.debug({ nodeTag: node.tag }, 'parseGroupResult')
|
||||
const groupNode = getBinaryNodeChild(node, 'group')
|
||||
if (groupNode) {
|
||||
try {
|
||||
logger.info({ groupNode }, 'groupNode')
|
||||
logger.debug({ groupId: groupNode.attrs?.id }, 'parsing group metadata')
|
||||
const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`)
|
||||
return metadata ? metadata : Optional.empty()
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,21 +7,32 @@ import { makeGroupsSocket } from './groups'
|
||||
import { executeWMexQuery as genericExecuteWMexQuery } from './mex'
|
||||
|
||||
const parseNewsletterCreateResponse = (response: NewsletterCreateResponse): NewsletterMetadata => {
|
||||
// Validate response structure before destructuring
|
||||
if (!response?.id || !response?.thread_metadata) {
|
||||
throw new Error('Invalid newsletter response: missing id or thread_metadata')
|
||||
}
|
||||
|
||||
const { id, thread_metadata: thread, viewer_metadata: viewer } = response
|
||||
|
||||
// Validate required thread metadata fields
|
||||
if (!thread.name?.text) {
|
||||
throw new Error('Invalid newsletter response: missing thread name')
|
||||
}
|
||||
|
||||
return {
|
||||
id: id,
|
||||
owner: undefined,
|
||||
name: thread.name.text,
|
||||
creation_time: parseInt(thread.creation_time, 10),
|
||||
description: thread.description.text,
|
||||
invite: thread.invite,
|
||||
subscribers: parseInt(thread.subscribers_count, 10),
|
||||
creation_time: parseInt(thread.creation_time, 10) || 0,
|
||||
description: thread.description?.text || '',
|
||||
invite: thread.invite || '',
|
||||
subscribers: parseInt(thread.subscribers_count, 10) || 0,
|
||||
verification: thread.verification,
|
||||
picture: {
|
||||
id: thread.picture.id,
|
||||
directPath: thread.picture.direct_path
|
||||
},
|
||||
mute_state: viewer.mute
|
||||
picture: thread.picture ? {
|
||||
id: thread.picture.id || '',
|
||||
directPath: thread.picture.direct_path || ''
|
||||
} : { id: '', directPath: '' },
|
||||
mute_state: viewer?.mute
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user