Created
August 27, 2019 19:27
-
-
Save RangerMauve/ad937f39953a56e905b5b128a3c60b05 to your computer and use it in GitHub Desktop.
Dream DatArchive API for extensions / peers
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
const archive = await DatArchive.load('someurl', { | |
// Gets passed down to hyperdrive constructor | |
extensions: ['example', 'foo', 'bar'] | |
}) | |
archive.addEventListener('extension', ({detail}) => { | |
const {type, message, peer} = detail | |
if(type === 'foo') { | |
// Similar to the hypercore API: https://github.com/mafintosh/hypercore#feedonextension-name-message-peer | |
await peer.extension('bar', 'Hello World') | |
} | |
const decoder = new TextDecoder() | |
// Message is always an ArrayBuffer | |
const decoded = decoder.decode(message) | |
console.log('got message from peer', type, message, peer.id) | |
}) | |
// Get currently connected peers | |
const peers = await archive.peers() | |
for (let peer of peers) { | |
await peer.extension('foo', 'Hey, world?') | |
} | |
// Broadcast to all peers, same as hyperdrive | |
await archive.extension('foo', 'Hey, world?') | |
// Similar to the hyperdrive API | |
// https://github.com/mafintosh/hyperdrive#archiveonpeer-add-peer | |
archive.addEventListener('peer-add', ({detail}) => { | |
const { peer } = detail | |
await peer.extension('example', 'example') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment