fix: Handle undefined results in USync query parser (#1891)

This commit is contained in:
João Lucas de Oliveira Lopes
2025-10-08 13:23:16 -03:00
committed by GitHub
parent a3ec28e051
commit 612c97f505
+28 -23
View File
@@ -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