diff --git a/.changeset/twenty-eagles-pretend.md b/.changeset/twenty-eagles-pretend.md new file mode 100644 index 00000000..f2d13b87 --- /dev/null +++ b/.changeset/twenty-eagles-pretend.md @@ -0,0 +1,5 @@ +--- +'@electric-sql/pglite': patch +--- + +Refactor the protocol message parse code to be simpler and easer to follow diff --git a/packages/pglite/src/parse.ts b/packages/pglite/src/parse.ts index 4d44f966..d04fa790 100644 --- a/packages/pglite/src/parse.ts +++ b/packages/pglite/src/parse.ts @@ -23,52 +23,69 @@ export function parseResults( let affectedRows = 0 const parsers = { ...defaultParsers, ...options?.parsers } - const filteredMessages = messages.filter( - (msg) => - msg.name === 'rowDescription' || - msg.name === 'dataRow' || - msg.name === 'commandComplete', + const processMessageTypes = new Set([ + 'rowDescription', + 'dataRow', + 'commandComplete', + ]) + + const filteredMessages = messages.filter((msg) => + processMessageTypes.has(msg.name), ) filteredMessages.forEach((message, index) => { - if (message.name === 'rowDescription') { - const msg = message as RowDescriptionMessage - currentResultSet.fields = msg.fields.map((field) => ({ - name: field.name, - dataTypeID: field.dataTypeID, - })) - } else if (message.name === 'dataRow' && currentResultSet) { - const msg = message as DataRowMessage - if (options?.rowMode === 'array') { - currentResultSet.rows.push( - msg.fields.map((field, i) => - parseType(field, currentResultSet!.fields[i].dataTypeID, parsers), - ), - ) - } else { - // rowMode === "object" - currentResultSet.rows.push( - Object.fromEntries( - msg.fields.map((field, i) => [ - currentResultSet!.fields[i].name, + switch (message.name) { + case 'rowDescription': { + const msg = message as RowDescriptionMessage + currentResultSet.fields = msg.fields.map((field) => ({ + name: field.name, + dataTypeID: field.dataTypeID, + })) + break + } + case 'dataRow': { + if (!currentResultSet) break + const msg = message as DataRowMessage + if (options?.rowMode === 'array') { + currentResultSet.rows.push( + msg.fields.map((field, i) => parseType(field, currentResultSet!.fields[i].dataTypeID, parsers), - ]), - ), - ) + ), + ) + } else { + // rowMode === "object" + currentResultSet.rows.push( + Object.fromEntries( + msg.fields.map((field, i) => [ + currentResultSet!.fields[i].name, + parseType( + field, + currentResultSet!.fields[i].dataTypeID, + parsers, + ), + ]), + ), + ) + } + break } - } else if (message.name === 'commandComplete') { - const msg = message as CommandCompleteMessage - affectedRows += retrieveRowCount(msg) + case 'commandComplete': { + const msg = message as CommandCompleteMessage + affectedRows += retrieveRowCount(msg) - if (index === filteredMessages.length - 1) - resultSets.push({ - ...currentResultSet, - affectedRows, - ...(blob ? { blob } : {}), - }) - else resultSets.push(currentResultSet) + if (index === filteredMessages.length - 1) { + resultSets.push({ + ...currentResultSet, + affectedRows, + ...(blob ? { blob } : {}), + }) + } else { + resultSets.push(currentResultSet) + } - currentResultSet = { rows: [], fields: [] } + currentResultSet = { rows: [], fields: [] } + break + } } })