From 1b2e2dc0594bae8a3e22129f0d9e3dec58d60925 Mon Sep 17 00:00:00 2001 From: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:48:32 +0400 Subject: [PATCH] fix UI output formatting --- pages/index.js | 92 +++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/pages/index.js b/pages/index.js index 67ab3f5..76caf0c 100755 --- a/pages/index.js +++ b/pages/index.js @@ -964,10 +964,10 @@ export default function Home(props) {
Peer Id:
-
{dataResult.Provider.ID}
+
{dataResult.PeerId}
MultiAddress:
-
{dataResult.Provider.Addrs}
+
{dataResult.MultiAddress}
{dataResult.Protocol && ( <> @@ -1422,58 +1422,47 @@ function onSearch( const res = data.MultihashResults[0]; const provResults = res.ProviderResults; - let providers = {}; - for (let i = 0; i < res.ProviderResults.length; i++) { - let provider = res.ProviderResults[i]; - if (providers[provider.Provider.ID] == undefined) { - providers[provider.Provider.ID] = {}; - } - providers[provider.Provider.ID][provider.ContextID] = provider; - } - const pids = Object.keys(providers); - let toDisplay = []; - for (let i = 0; i < pids.length; i++) { - let pd = providers[pids[i]]; - let addrs = ""; - let keys = {}; - let contexts = Object.keys(pd); - for (let j = 0; j < contexts.length; j++) { - let mdBytes = base64ToBytesArr(pd[contexts[j]].Metadata); - while (mdBytes.length > 0) { - let next = popProtocol(mdBytes); - let name = next[0]; - mdBytes = next[1]; + // Process each ProviderResult individually + for (let i = 0; i < provResults.length; i++) { + let providerResult = provResults[i]; + let mdBytes = base64ToBytesArr(providerResult.Metadata); + let protocols = []; + let deals = []; + + // Extract protocols and deal information + while (mdBytes.length > 0) { + let next = popProtocol(mdBytes); + let name = next[0]; + mdBytes = next[1]; + + if (name !== -1) { let ctx = toContext(name, mdBytes); - if (keys[name] == undefined) { - keys[name] = []; - } if (ctx[0] != "") { - keys[name].push(ctx[0]); + deals.push(ctx[0]); } mdBytes = ctx[1]; - addrs = pd[contexts[j]].Provider.Addrs; - if (name == -1) { - break; - } + protocols.push(name); + } else { + break; } } - for (const [index, value] of Object.keys(keys).entries()) { - let displayEntry = { ...provResults[i] }; - displayEntry["Protocol"] = value; + // Build the display entry with correct Provider.Addrs + let displayEntry = { + PeerId: providerResult.Provider.ID, + MultiAddress: providerResult.Provider.Addrs.join(", "), + Protocol: protocols.join(", "), + }; - const deals = []; - for (const [dealIndex, dealValue] of keys[value].entries()) { - deals.push(dealValue); - } - if (deals.length) { - displayEntry["DealInfo"] = deals; - } - toDisplay.push(displayEntry); + if (deals.length > 0) { + displayEntry["DealInfo"] = deals; } + + toDisplay.push(displayEntry); } + setDisplayData(toDisplay); }) .catch((error) => { @@ -1485,10 +1474,20 @@ function popProtocol(buf) { try { let [code, Vlen] = readVarint(buf, 0); buf = buf.slice(Vlen); - if (code == 0x900) { - return ["Bitswap", buf]; - } else if (code == 0x910 || code == 4128768) { - return ["Graphsync", buf]; + + // Map of protocol codes to protocol names + const protocolMap = { + 0x0900: "Bitswap", // 2304 + 0x0910: "Graphsync", // 2320 + 0x0920: "HTTP", // 2336 + }; + + // Convert code to hexadecimal for mapping + const hexCode = code.toString(16).toLowerCase(); + const protocolName = protocolMap[`0x${hexCode}`]; + + if (protocolName) { + return [protocolName, buf]; } else { return [code, buf]; } @@ -1496,6 +1495,7 @@ function popProtocol(buf) { return [-1, buf]; } } + function toContext(code, buf) { if (code == "Graphsync") { try {