-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
62 lines (48 loc) · 1.61 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import axios from 'axios'
import Corestore from 'corestore'
import Rehoster from 'hypercore-rehoster'
import Hyperswarm from 'hyperswarm'
import SwarmManager from 'swarm-manager'
import setupRehostServer from './lib/server.js'
const corestoreLoc = './my-store'
const corestore = new Corestore(corestoreLoc)
const rehoster = getRehoster(corestore, new Hyperswarm())
console.log('Setting up rehost server')
const host = '127.0.0.1'
const app = await setupRehostServer(rehoster, { logger: false, host })
const server = app.server
const url = `http://${host}:${server.address().port}/`
console.log('initial keys:')
await printKeys(url)
console.log('\nAdd a key')
const publicKey = 'b'.repeat(64)
await axios.put(`${url}${publicKey}`, { info: 'Rehosts my documents' })
console.log('now keys:')
await printKeys(url)
const { details } = (await axios.get(`${url}info`)).data
console.log('\n', details) // Includes your own core
console.log('\nRemove a key')
await axios.delete(`${url}${publicKey}`)
console.log('Final keys:')
await printKeys(url)
server.close(
async () => await Promise.all([rehoster.close(), rehoster.swarmManager.close()])
)
async function printKeys (url) {
const entries = (await axios.get(url)).data
if (entries.length === 0) console.log('<No entries>')
for (const entry of entries) {
console.log(` -${entry.key} (${entry.info || ''})`)
}
}
function getRehoster (store, swarm) {
swarm.on('connection', (socket) => {
store.replicate(socket)
socket.on('error', () => {})
})
const swarmManager = new SwarmManager(swarm)
return new Rehoster(
store.namespace('rehoster'),
swarmManager
)
}