-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
52 lines (38 loc) · 1.17 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const hypercore = require('hypercore')
const RAM = require('random-access-memory')
const tmp = require('tmp')
const fs = require('fs')
const path = require('path')
tmp.setGracefulCleanup()
const intercept = require('./')
const test = require('tape')
test('can intercept hypercore storage', (t) => {
t.plan(3)
const storageDir = tmp.dirSync().name
const writer = hypercore(RAM)
writer.append('hello', () => {
const reader = hypercore(RAM, writer.key)
reader.ready(() => {
const unintercept = intercept(reader, { putData, getData })
const stream1 = writer.replicate(true)
const stream2 = reader.replicate(false)
stream1.pipe(stream2).pipe(stream1)
reader.get(0, (err) => {
t.notOk(err, 'read chunk from peer')
unintercept()
t.end()
})
})
})
function putData (index, data, cb) {
t.equal(index, 0, 'stored data from intercept')
fs.writeFile(makeLocation(index), data, cb)
}
function getData (index, cb) {
t.equal(index, 0, 'read data from intercept')
fs.readFile(makeLocation(index), cb)
}
function makeLocation (index) {
return path.join(storageDir, `${index}.bin`)
}
})