From 612c97f505498eb40edd6c077007beb700a6a3e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lucas=20de=20Oliveira=20Lopes?= <55464917+jlucaso1@users.noreply.github.com> Date: Wed, 8 Oct 2025 13:23:16 -0300 Subject: [PATCH] fix: Handle undefined results in USync query parser (#1891) --- src/WAUSync/USyncQuery.ts | 51 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/WAUSync/USyncQuery.ts b/src/WAUSync/USyncQuery.ts index 0c66e24c..fdf9d78e 100644 --- a/src/WAUSync/USyncQuery.ts +++ b/src/WAUSync/USyncQuery.ts @@ -45,8 +45,8 @@ export class USyncQuery { return this } - parseUSyncQueryResult(result: BinaryNode): USyncQueryResult | undefined { - if (result.attrs.type !== 'result') { + parseUSyncQueryResult(result: BinaryNode | undefined): USyncQueryResult | undefined { + if (!result || result.attrs.type !== 'result') { return } @@ -68,27 +68,32 @@ export class USyncQuery { //TODO: see if there are any errors in the result node //const resultNode = getBinaryNodeChild(usyncNode, 'result') - const listNode = getBinaryNodeChild(usyncNode, 'list') - if (Array.isArray(listNode?.content) && typeof listNode !== 'undefined') { - queryResult.list = listNode.content.map(node => { - const id = node?.attrs.jid! - const data = Array.isArray(node?.content) - ? Object.fromEntries( - node.content - .map(content => { - const protocol = content.tag - const parser = protocolMap[protocol] - if (parser) { - return [protocol, parser(content)] - } else { - return [protocol, null] - } - }) - .filter(([, b]) => b !== null) as [string, unknown][] - ) - : {} - return { ...data, id } - }) + const listNode = usyncNode ? getBinaryNodeChild(usyncNode, 'list') : undefined + + if (listNode?.content && Array.isArray(listNode.content)) { + queryResult.list = listNode.content.reduce((acc: USyncQueryResultList[], node) => { + const id = node?.attrs.jid + if (id) { + const data = Array.isArray(node?.content) + ? Object.fromEntries( + node.content + .map(content => { + const protocol = content.tag + const parser = protocolMap[protocol] + if (parser) { + return [protocol, parser(content)] + } else { + return [protocol, null] + } + }) + .filter(([, b]) => b !== null) as [string, unknown][] + ) + : {} + acc.push({ ...data, id }) + } + + return acc + }, []) } //TODO: implement side list