hypercore 10.0.0-alpha.11 → 10.0.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,123 +0,0 @@
1
- const test = require('brittle')
2
- const RAM = require('random-access-memory')
3
- const Hypercore = require('..')
4
- const { create, replicate } = require('./helpers')
5
-
6
- const encryptionKey = Buffer.alloc(32, 'hello world')
7
-
8
- test('encrypted append and get', async function (t) {
9
- const a = await create({ encryptionKey })
10
-
11
- t.alike(a.encryptionKey, encryptionKey)
12
-
13
- await a.append(['hello'])
14
-
15
- t.is(a.byteLength, 5)
16
- t.is(a.core.tree.byteLength, 5 + a.padding)
17
-
18
- const unencrypted = await a.get(0)
19
- t.alike(unencrypted, Buffer.from('hello'))
20
-
21
- const encrypted = await a.core.blocks.get(0)
22
- t.absent(encrypted.includes('hello'))
23
- })
24
-
25
- test('encrypted seek', async function (t) {
26
- const a = await create({ encryptionKey })
27
-
28
- await a.append(['hello', 'world', '!'])
29
-
30
- t.alike(await a.seek(0), [0, 0])
31
- t.alike(await a.seek(4), [0, 4])
32
- t.alike(await a.seek(5), [1, 0])
33
- t.alike(await a.seek(6), [1, 1])
34
- t.alike(await a.seek(6), [1, 1])
35
- t.alike(await a.seek(9), [1, 4])
36
- t.alike(await a.seek(10), [2, 0])
37
- t.alike(await a.seek(11), [3, 0])
38
- })
39
-
40
- test('encrypted replication', async function (t) {
41
- const a = await create({ encryptionKey })
42
-
43
- await a.append(['a', 'b', 'c', 'd', 'e'])
44
-
45
- t.test('with encryption key', async function (t) {
46
- const b = await create(a.key, { encryptionKey })
47
-
48
- replicate(a, b, t)
49
-
50
- await t.test('through direct download', async function (t) {
51
- const r = b.download({ start: 0, end: a.length })
52
- await r.downloaded()
53
-
54
- for (let i = 0; i < 5; i++) {
55
- t.alike(await b.get(i), await a.get(i))
56
- }
57
- })
58
-
59
- await t.test('through indirect download', async function (t) {
60
- await a.append(['f', 'g', 'h', 'i', 'j'])
61
-
62
- for (let i = 5; i < 10; i++) {
63
- t.alike(await b.get(i), await a.get(i))
64
- }
65
- })
66
- })
67
-
68
- t.test('without encryption key', async function (t) {
69
- const b = await create(a.key)
70
-
71
- replicate(a, b, t)
72
-
73
- await t.test('through direct download', async function (t) {
74
- const r = b.download({ start: 0, end: a.length })
75
- await r.downloaded()
76
-
77
- for (let i = 0; i < 5; i++) {
78
- t.alike(await b.get(i), await a.core.blocks.get(i))
79
- }
80
- })
81
-
82
- await t.test('through indirect download', async function (t) {
83
- await a.append(['f', 'g', 'h', 'i', 'j'])
84
-
85
- for (let i = 5; i < 10; i++) {
86
- t.alike(await b.get(i), await a.core.blocks.get(i))
87
- }
88
- })
89
- })
90
- })
91
-
92
- test('encrypted session', async function (t) {
93
- const a = await create({ encryptionKey })
94
-
95
- await a.append(['hello'])
96
-
97
- const s = a.session()
98
-
99
- t.alike(a.encryptionKey, s.encryptionKey)
100
- t.alike(await s.get(0), Buffer.from('hello'))
101
-
102
- await s.append(['world'])
103
-
104
- const unencrypted = await s.get(1)
105
- t.alike(unencrypted, Buffer.from('world'))
106
- t.alike(await a.get(1), unencrypted)
107
-
108
- const encrypted = await s.core.blocks.get(1)
109
- t.absent(encrypted.includes('world'))
110
- t.alike(await a.core.blocks.get(1), encrypted)
111
- })
112
-
113
- test('encrypted session before ready core', async function (t) {
114
- const a = new Hypercore(RAM, { encryptionKey })
115
- const s = a.session()
116
-
117
- await a.ready()
118
-
119
- t.alike(a.encryptionKey, s.encryptionKey)
120
-
121
- await a.append(['hello'])
122
- t.alike(await s.get(0), Buffer.from('hello'))
123
- })
package/test/extension.js DELETED
@@ -1,71 +0,0 @@
1
- const test = require('brittle')
2
- const { create, replicate, eventFlush } = require('./helpers')
3
-
4
- test('basic extension', async function (t) {
5
- const messages = ['world', 'hello']
6
-
7
- const a = await create()
8
- a.registerExtension('test-extension', {
9
- encoding: 'utf-8',
10
- onmessage: (message, peer) => {
11
- t.ok(peer === a.peers[0])
12
- t.is(message, messages.pop())
13
- }
14
- })
15
-
16
- const b = await create(a.key)
17
- const bExt = b.registerExtension('test-extension', {
18
- encoding: 'utf-8'
19
- })
20
-
21
- replicate(a, b, t)
22
-
23
- await eventFlush()
24
- t.is(b.peers.length, 1)
25
-
26
- bExt.send('hello', b.peers[0])
27
- bExt.send('world', b.peers[0])
28
-
29
- await eventFlush()
30
- t.absent(messages.length)
31
-
32
- t.end()
33
- })
34
-
35
- test('two extensions', async function (t) {
36
- const messages = ['world', 'hello']
37
-
38
- const a = await create()
39
- const b = await create(a.key)
40
-
41
- replicate(a, b, t)
42
-
43
- b.registerExtension('test-extension-1', {
44
- encoding: 'utf-8'
45
- })
46
- const bExt2 = b.registerExtension('test-extension-2', {
47
- encoding: 'utf-8'
48
- })
49
-
50
- await eventFlush()
51
- t.is(b.peers.length, 1)
52
-
53
- bExt2.send('world', b.peers[0])
54
-
55
- await eventFlush()
56
-
57
- a.registerExtension('test-extension-2', {
58
- encoding: 'utf-8',
59
- onmessage: (message, peer) => {
60
- t.ok(peer === a.peers[0])
61
- t.is(message, messages.pop())
62
- }
63
- })
64
-
65
- bExt2.send('hello', b.peers[0])
66
-
67
- await eventFlush()
68
- t.is(messages.length, 1) // First message gets ignored
69
-
70
- t.end()
71
- })
@@ -1,23 +0,0 @@
1
- const Hypercore = require('../../')
2
- const ram = require('random-access-memory')
3
-
4
- module.exports = {
5
- async create (...args) {
6
- const core = new Hypercore(ram, ...args)
7
- await core.ready()
8
- return core
9
- },
10
-
11
- replicate (a, b, t) {
12
- const s1 = a.replicate(true)
13
- const s2 = b.replicate(false)
14
- s1.on('error', err => t.comment(`STREAM ERROR: ${err}`))
15
- s2.on('error', err => t.comment(`STREAM ERROR: ${err}`))
16
- s1.pipe(s2).pipe(s1)
17
- return [s1, s2]
18
- },
19
-
20
- async eventFlush () {
21
- await new Promise(resolve => setImmediate(resolve))
22
- }
23
- }