Skip to content

Instantly share code, notes, and snippets.

@Nuhvi
Last active March 3, 2023 00:19
Show Gist options
  • Save Nuhvi/ce4475b1a7c668db064beb326f915eed to your computer and use it in GitHub Desktop.
Save Nuhvi/ce4475b1a7c668db064beb326f915eed to your computer and use it in GitHub Desktop.
Hyperswarm record max size should be less than MTU
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