diff --git a/src/pcap/PcapOutput.ts b/src/pcap/PcapOutput.ts index fc578ffd0e5..33a0c6176cb 100644 --- a/src/pcap/PcapOutput.ts +++ b/src/pcap/PcapOutput.ts @@ -485,13 +485,7 @@ const recordPacketEndpoints = ( scratch.add(addr1); scratch.add(address); } - const prev = state.endpoints.get(address); - if (prev === undefined) { - chargeRetainedString(state, address); - state.endpoints.set(address, 1); - } else { - state.endpoints.set(address, prev + 1); - } + countValue(state.endpoints, address, state); } }; visit(field("ip.src")); @@ -626,15 +620,7 @@ const processPacketNonDns = ( for (let i = 0; i < snis.length; i += 1) { if (snis[i] !== "") addChargedString(state, state.sni, snis[i]!); } - if (protocol !== "") { - const prev = state.protocols.get(protocol); - if (prev === undefined) { - chargeRetainedString(state, protocol); - state.protocols.set(protocol, 1); - } else { - state.protocols.set(protocol, prev + 1); - } - } + if (protocol !== "") countValue(state.protocols, protocol, state); recordPacketHttpUris(state, field); return ( layers.dns !== undefined || @@ -646,8 +632,15 @@ const processPacketNonDns = ( ); }; -const countValue = (counts: Map, value: string): void => { - counts.set(value, (counts.get(value) ?? 0) + 1); +const countValue = ( + counts: Map, + value: string, + retained?: NonDnsAccumulator, +): void => { + const previous = counts.get(value); + if (previous === undefined && retained !== undefined) + chargeRetainedString(retained, value); + counts.set(value, (previous ?? 0) + 1); }; const consumeCount = (counts: Map, value: string): boolean => {