-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
36 lines (33 loc) · 1.21 KB
/
test.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
const replicate = require('hypercore-replicate')
const hypercore = require('hypercore')
const crypto = require('hypercore-crypto')
const ready = require('hypercore-ready')
const test = require('tape')
const ram = require('random-access-memory')
const hook = require('./')
test('createHook(key)', (t) => {
const key = crypto.randomBytes(32)
const message = Buffer.from('hello')
const onwrite = hook(key)
const feed = hypercore(ram, { onwrite })
feed.ready(() => {
// use `Buffer.from()` to make copy as `feed.append()`
// will modify `buffer` in place
feed.append(Buffer.from(message), (err) => {
const onwrite = hook(key) // shadow
const authenticated = hypercore(ram, feed.key, { onwrite })
const unauthenticated = hypercore(ram, feed.key)
ready(authenticated, unauthenticated, () => {
replicate(feed, authenticated, unauthenticated, (err) => {
authenticated.head((err, buf) => {
t.ok(0 === Buffer.compare(buf, message), 'authenticated message')
unauthenticated.head((err, buf) => {
t.ok(0 !== Buffer.compare(buf, message), 'unauthenticated message')
t.end()
})
})
})
})
})
})
})