-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsolution.js
More file actions
30 lines (24 loc) · 795 Bytes
/
solution.js
File metadata and controls
30 lines (24 loc) · 795 Bytes
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
const hypercore = require('hypercore')
const ram = require('random-access-memory')
const pump = require('pump')
const forEachChunk = require('../../lib/for-each-chunk')
module.exports = (key, peer) => {
const feed = hypercore(ram, key, { valueEncoding: 'utf8' })
return new Promise((resolve, reject) => {
// we need to sync our database with the remote one
const onFinishSync = () => {
// when is done we can retrieve the log
const messages = []
const reader = feed.createReadStream()
const ws = forEachChunk((data, enc, next) => {
messages.push(data)
next()
})
pump(reader, ws, err => {
if (err) return reject(err)
resolve(messages)
})
}
pump(peer, feed.replicate(), peer, onFinishSync)
})
}