Last active
March 3, 2023 00:19
-
-
Save Nuhvi/ce4475b1a7c668db064beb326f915eed to your computer and use it in GitHub Desktop.
Hyperswarm record max size should be less than MTU
This file contains 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
import DHT from '@hyperswarm/dht' | |
const MTU = 1500 | |
const payloadSize = MTU / 2 | |
const payload = random(payloadSize) | |
const dht = new DHT() | |
const mutablePutResponse = await dht.mutablePut(dht.defaultKeyPair, payload) | |
console.log("MutablePut successfull", { closestNodes: mutablePutResponse.closestNodes.length }) | |
const mutableGetResponse = await dht.mutableGet(dht.defaultKeyPair.publicKey) | |
console.log("MutableGet response:", { success: !!mutableGetResponse, retrieved_size: mutableGetResponse.value.byteLength }) | |
dht.destroy() | |
function random (size = 32) { | |
return Buffer.concat( | |
Array(Math.ceil(size / 4)).fill(fourbytes()) | |
).slice(0, size) | |
function fourbytes() { | |
return Buffer.from(Math.random().toString('16').slice(2, 10), "hex") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment