Skip to content

Commit

Permalink
Merge pull request #36 from LexLuthr/patch-1
Browse files Browse the repository at this point in the history
fix: UI output formatting
  • Loading branch information
willscott authored Oct 7, 2024
2 parents 1b3fa4e + 1b2e2dc commit c0502aa
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,10 +964,10 @@ export default function Home(props) {
<div className="resultItem" key={resultIndex}>
<dl>
<dt>Peer Id:</dt>
<dd>{dataResult.Provider.ID}</dd>
<dd>{dataResult.PeerId}</dd>

<dt>MultiAddress:</dt>
<dd>{dataResult.Provider.Addrs}</dd>
<dd>{dataResult.MultiAddress}</dd>

{dataResult.Protocol && (
<>
Expand Down Expand Up @@ -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) => {
Expand All @@ -1485,17 +1474,28 @@ 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];
}
} catch (e) {
return [-1, buf];
}
}

function toContext(code, buf) {
if (code == "Graphsync") {
try {
Expand Down

0 comments on commit c0502aa

Please sign in to comment.