Last active
November 20, 2024 20:08
-
-
Save yeus/364514ce60596ff9eb13cbe9ac5d1309 to your computer and use it in GitHub Desktop.
Helia minimal html test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Helia Node Status</title> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@helia/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@helia/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@chainsafe/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@chainsafe/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@libp2p/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@libp2p/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/datastore-level@^11.0.1/dist/index.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/@libp2p/[email protected]/dist/index.min.js"></script> | |
</head> | |
<body> | |
<h1>Helia Node Status</h1> | |
<button id="startHelia">Start Helia</button> | |
<button id="fetchStatus">Fetch Node Status</button> | |
<button id="addTestString">Add Test String</button> | |
<div> | |
FileID: | |
<pre id="fileId"></pre> | |
</div> | |
<pre id="status"></pre> | |
<script> | |
// check at these two addresses: | |
// - https://check.ipfs.network/?cid=bafkreifhfkgqicrwdtsfdtpebwxy3fxc2gtp5dfj2unqxgvyrzldwcpfxy | |
// - https://pl-diagnose.on.fleek.co/#/diagnose/access-content?backend=https%3A%2F%2Fpl-diagnose.onrender.com | |
let node; // Helia instance | |
let info; | |
let statusInterval; // Interval ID for periodic fetching | |
// Create and start Helia | |
async function startHelia() { | |
const { createHelia } = Helia; | |
const { MemoryBlockstore } = BlockstoreCore; | |
const { LevelDatastore } = DatastoreLevel; | |
const { unixfs } = HeliaUnixfs; | |
// Initialize datastore and blockstore | |
const blockstore = new MemoryBlockstore(); | |
const datastore = new LevelDatastore('helia-datastore'); | |
node = await createHelia({ datastore, blockstore }); | |
// TODO: not sure if this is needed | |
// await node.libp2p.services.dht.setMode('server'); | |
document.getElementById('status').textContent = | |
'Helia started successfully!'; | |
// Start automatic status fetching | |
if (!statusInterval) { | |
statusInterval = setInterval(fetchNodeStatus, 2000); | |
} | |
} | |
// Fetch Helia node status | |
async function fetchNodeStatus() { | |
if (!node) { | |
alert('Please start Helia first!'); | |
return; | |
} | |
try { | |
const dhtMode = await node.libp2p.services.dht?.getMode(); | |
const statusText = `${node.libp2p.status} - ${ | |
dhtMode === 'client' ? 'DHT Client' : 'DHT Server' | |
}`; | |
info = { | |
peerId: node.libp2p.peerId.toString(), | |
'node started': node ? true : false, | |
connectedPeers: node.libp2p.getPeers().length, | |
metrics: node.metrics, | |
dhtMode: statusText, | |
addresses: node.libp2p | |
.getMultiaddrs() | |
.map((addr) => addr.toString()), | |
}; | |
} catch (error) { | |
console.error('Error fetching node status:', error); | |
info = { error: error.message }; | |
} | |
document.getElementById('status').textContent = JSON.stringify( | |
info, | |
null, | |
2, | |
); | |
} | |
// Add a test string | |
async function addTestString() { | |
if (!HeliaStrings) { | |
alert('Please start Helia first!'); | |
return; | |
} | |
const content = 'Hello from Helia!'; | |
const cid = await HeliaStrings.strings(node).add(content); | |
// do we need this? | |
node.routing.provide(cid); | |
document.getElementById('fileId').textContent = | |
`Added string with CID: ${cid.toString()}`; | |
} | |
// Attach event listeners | |
document | |
.getElementById('startHelia') | |
.addEventListener('click', startHelia); | |
document | |
.getElementById('fetchStatus') | |
.addEventListener('click', fetchNodeStatus); | |
document | |
.getElementById('addTestString') | |
.addEventListener('click', addTestString); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment