hypercore 9.12.0 → 10.0.0-alpha.11

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.
Files changed (86) hide show
  1. package/.github/workflows/test-node.yml +3 -4
  2. package/README.md +131 -404
  3. package/__snapshots__/test/storage.js.snapshot.cjs +15 -0
  4. package/examples/announce.js +19 -0
  5. package/examples/basic.js +10 -0
  6. package/examples/http.js +123 -0
  7. package/examples/lookup.js +20 -0
  8. package/index.js +365 -1600
  9. package/lib/bitfield.js +113 -285
  10. package/lib/block-encryption.js +68 -0
  11. package/lib/block-store.js +58 -0
  12. package/lib/core.js +468 -0
  13. package/lib/extensions.js +76 -0
  14. package/lib/merkle-tree.js +1110 -0
  15. package/lib/messages.js +571 -0
  16. package/lib/mutex.js +39 -0
  17. package/lib/oplog.js +224 -0
  18. package/lib/protocol.js +525 -0
  19. package/lib/random-iterator.js +46 -0
  20. package/lib/remote-bitfield.js +24 -0
  21. package/lib/replicator.js +857 -0
  22. package/lib/streams.js +39 -0
  23. package/package.json +44 -45
  24. package/test/basic.js +59 -471
  25. package/test/bitfield.js +48 -133
  26. package/test/core.js +290 -0
  27. package/test/encodings.js +18 -0
  28. package/test/encryption.js +123 -0
  29. package/test/extension.js +71 -0
  30. package/test/helpers/index.js +23 -0
  31. package/test/merkle-tree.js +518 -0
  32. package/test/mutex.js +137 -0
  33. package/test/oplog.js +399 -0
  34. package/test/preload.js +72 -0
  35. package/test/replicate.js +227 -824
  36. package/test/sessions.js +173 -0
  37. package/test/storage.js +31 -0
  38. package/test/streams.js +39 -146
  39. package/test/user-data.js +47 -0
  40. package/bench/all.sh +0 -65
  41. package/bench/copy-64kb-blocks.js +0 -51
  42. package/bench/helpers/read-throttled.js +0 -27
  43. package/bench/helpers/read.js +0 -47
  44. package/bench/helpers/write.js +0 -29
  45. package/bench/read-16kb-blocks-proof-throttled.js +0 -1
  46. package/bench/read-16kb-blocks-proof.js +0 -1
  47. package/bench/read-16kb-blocks-throttled.js +0 -1
  48. package/bench/read-16kb-blocks.js +0 -1
  49. package/bench/read-512b-blocks.js +0 -1
  50. package/bench/read-64kb-blocks-linear-batch.js +0 -18
  51. package/bench/read-64kb-blocks-linear.js +0 -18
  52. package/bench/read-64kb-blocks-proof.js +0 -1
  53. package/bench/read-64kb-blocks.js +0 -1
  54. package/bench/replicate-16kb-blocks.js +0 -19
  55. package/bench/replicate-64kb-blocks.js +0 -19
  56. package/bench/write-16kb-blocks.js +0 -1
  57. package/bench/write-512b-blocks.js +0 -1
  58. package/bench/write-64kb-blocks-static.js +0 -1
  59. package/bench/write-64kb-blocks.js +0 -1
  60. package/example.js +0 -23
  61. package/lib/cache.js +0 -26
  62. package/lib/crypto.js +0 -5
  63. package/lib/replicate.js +0 -829
  64. package/lib/safe-buffer-equals.js +0 -6
  65. package/lib/storage.js +0 -421
  66. package/lib/tree-index.js +0 -183
  67. package/test/ack.js +0 -306
  68. package/test/audit.js +0 -36
  69. package/test/cache.js +0 -93
  70. package/test/compat.js +0 -209
  71. package/test/copy.js +0 -377
  72. package/test/default-storage.js +0 -51
  73. package/test/extensions.js +0 -137
  74. package/test/get.js +0 -64
  75. package/test/head.js +0 -65
  76. package/test/helpers/create-tracking-ram.js +0 -27
  77. package/test/helpers/create.js +0 -6
  78. package/test/helpers/replicate.js +0 -4
  79. package/test/seek.js +0 -234
  80. package/test/selections.js +0 -95
  81. package/test/set-uploading-downloading.js +0 -91
  82. package/test/stats.js +0 -77
  83. package/test/timeouts.js +0 -22
  84. package/test/tree-index.js +0 -841
  85. package/test/update.js +0 -156
  86. package/test/value-encoding.js +0 -52
package/test/copy.js DELETED
@@ -1,377 +0,0 @@
1
- var create = require('./helpers/create')
2
- var tape = require('tape')
3
-
4
- tape('hash copy', function (t) {
5
- var a = create()
6
-
7
- a.ready(function () {
8
- var b = create(a.key)
9
-
10
- a.append(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], function () {
11
- a.proof(0, { hash: true }, function (err, proof) {
12
- t.error(err, 'no error')
13
- b.put(0, null, proof, function (err) {
14
- t.error(err, 'no error')
15
- a.proof(4, { hash: true, digest: b.digest(4) }, function (err, proof) {
16
- t.error(err, 'no error')
17
- b.put(4, null, proof, function (err) {
18
- t.error(err, 'no error')
19
- t.end()
20
- })
21
- })
22
- })
23
- })
24
- })
25
- })
26
- })
27
-
28
- // tests below was generated by a randomizer triggering different replication edge cases
29
-
30
- tape('chaos monkey generated #1', function (t) {
31
- var ops = []
32
- ops.push({ type: 'append', value: '998' })
33
- ops.push({ type: 'copy', block: 0, from: 'feed', to: 'clone1' })
34
- runOps(t, ops)
35
- })
36
-
37
- tape('chaos monkey generated #2', function (t) {
38
- var ops = []
39
- ops.push({ type: 'append', value: '993' })
40
- ops.push({ type: 'append', value: '992' })
41
- ops.push({ type: 'append', value: '991' })
42
- ops.push({ type: 'copy', block: 0, from: 'feed', to: 'clone1' })
43
- ops.push({ type: 'append', value: '984' })
44
- ops.push({ type: 'copy', block: 1, from: 'feed', to: 'clone1' })
45
- ops.push({ type: 'append', value: '982' })
46
- ops.push({ type: 'copy', block: 0, from: 'clone1', to: 'clone2' })
47
- ops.push({ type: 'copy', block: 3, from: 'feed', to: 'clone1' })
48
- runOps(t, ops)
49
- })
50
-
51
- tape('chaos monkey generated #3', function (t) {
52
- var ops = []
53
- ops.push({ type: 'append', value: '996' })
54
- ops.push({ type: 'append', value: '994' })
55
- ops.push({ type: 'copy', block: 1, from: 'feed', to: 'clone1' })
56
- ops.push({ type: 'copy', block: 1, from: 'clone1', to: 'clone2' })
57
- ops.push({ type: 'append', value: '991' })
58
- ops.push({ type: 'append', value: '990' })
59
- ops.push({ type: 'append', value: '988' })
60
- ops.push({ type: 'append', value: '985' })
61
- ops.push({ type: 'copy', block: 5, from: 'feed', to: 'clone1' })
62
- ops.push({ type: 'copy', block: 5, from: 'clone1', to: 'clone2' })
63
- ops.push({ type: 'append', value: '981' })
64
- ops.push({ type: 'append', value: '980' })
65
- ops.push({ type: 'append', value: '976' })
66
- ops.push({ type: 'append', value: '974' })
67
- ops.push({ type: 'append', value: '972' })
68
- ops.push({ type: 'append', value: '971' })
69
- ops.push({ type: 'append', value: '969' })
70
- ops.push({ type: 'copy', block: 8, from: 'feed', to: 'clone1' })
71
- ops.push({ type: 'copy', block: 8, from: 'clone1', to: 'clone2' })
72
- ops.push({ type: 'append', value: '965' })
73
- ops.push({ type: 'copy', block: 6, from: 'feed', to: 'clone1' })
74
- ops.push({ type: 'copy', block: 3, from: 'feed', to: 'clone1' })
75
- ops.push({ type: 'append', value: '961' })
76
- ops.push({ type: 'copy', block: 6, from: 'clone1', to: 'clone2' })
77
- ops.push({ type: 'append', value: '957' })
78
- ops.push({ type: 'append', value: '956' })
79
- ops.push({ type: 'copy', block: 3, from: 'clone1', to: 'clone2' })
80
- ops.push({ type: 'append', value: '953' })
81
- ops.push({ type: 'append', value: '952' })
82
- ops.push({ type: 'append', value: '951' })
83
- ops.push({ type: 'append', value: '950' })
84
- ops.push({ type: 'copy', block: 17, from: 'feed', to: 'clone1' })
85
- ops.push({ type: 'append', value: '947' })
86
- ops.push({ type: 'append', value: '946' })
87
- ops.push({ type: 'copy', block: 18, from: 'feed', to: 'clone1' })
88
- ops.push({ type: 'append', value: '944' })
89
- ops.push({ type: 'copy', block: 22, from: 'feed', to: 'clone1' })
90
- runOps(t, ops)
91
- })
92
-
93
- tape('chaos monkey randomized #4', function (t) {
94
- var ops = []
95
- ops.push({ type: 'append', value: '490' })
96
- ops.push({ type: 'append', value: '483' })
97
- ops.push({ type: 'append', value: '479' })
98
- ops.push({ type: 'append', value: '476' })
99
- ops.push({ type: 'append', value: '472' })
100
- ops.push({ type: 'append', value: '470' })
101
- ops.push({ type: 'copy', block: 1, from: 'feed', to: 'clone1' })
102
- ops.push({ type: 'append', value: '461' })
103
- ops.push({ type: 'copy', block: 2, from: 'feed', to: 'clone1' })
104
- ops.push({ type: 'append', value: '459' })
105
- ops.push({ type: 'copy', block: 1, from: 'clone1', to: 'clone2' })
106
- ops.push({ type: 'append', value: '455' })
107
- ops.push({ type: 'append', value: '450' })
108
- ops.push({ type: 'copy', block: 2, from: 'clone1', to: 'clone2' })
109
- ops.push({ type: 'append', value: '447' })
110
- ops.push({ type: 'append', value: '446' })
111
- ops.push({ type: 'append', value: '444' })
112
- ops.push({ type: 'append', value: '443' })
113
- ops.push({ type: 'copy', block: 5, from: 'feed', to: 'clone1' })
114
- ops.push({ type: 'copy', block: 5, from: 'clone1', to: 'clone2' })
115
- ops.push({ type: 'append', value: '432' })
116
- ops.push({ type: 'append', value: '431' })
117
- ops.push({ type: 'append', value: '430' })
118
- ops.push({ type: 'copy', block: 16, from: 'feed', to: 'clone1' })
119
- ops.push({ type: 'append', value: '427' })
120
- ops.push({ type: 'append', value: '426' })
121
- ops.push({ type: 'copy', block: 16, from: 'clone1', to: 'clone2' })
122
- ops.push({ type: 'append', value: '421' })
123
- ops.push({ type: 'append', value: '420' })
124
- ops.push({ type: 'copy', block: 4, from: 'feed', to: 'clone1' })
125
- ops.push({ type: 'append', value: '415' })
126
- ops.push({ type: 'append', value: '411' })
127
- ops.push({ type: 'copy', block: 4, from: 'clone1', to: 'clone2' })
128
- ops.push({ type: 'append', value: '407' })
129
- ops.push({ type: 'append', value: '405' })
130
- ops.push({ type: 'append', value: '401' })
131
- ops.push({ type: 'append', value: '399' })
132
- ops.push({ type: 'append', value: '398' })
133
- ops.push({ type: 'copy', block: 24, from: 'feed', to: 'clone1' })
134
- ops.push({ type: 'copy', block: 9, from: 'feed', to: 'clone1' })
135
- ops.push({ type: 'copy', block: 24, from: 'clone1', to: 'clone2' })
136
- ops.push({ type: 'append', value: '390' })
137
- ops.push({ type: 'append', value: '389' })
138
- ops.push({ type: 'copy', block: 9, from: 'clone1', to: 'clone2' })
139
- ops.push({ type: 'copy', block: 11, from: 'feed', to: 'clone1' })
140
- ops.push({ type: 'copy', block: 23, from: 'feed', to: 'clone1' })
141
- ops.push({ type: 'append', value: '384' })
142
- ops.push({ type: 'copy', block: 0, from: 'feed', to: 'clone1' })
143
- ops.push({ type: 'append', value: '380' })
144
- ops.push({ type: 'copy', block: 26, from: 'feed', to: 'clone1' })
145
- ops.push({ type: 'copy', block: 29, from: 'feed', to: 'clone1' })
146
- ops.push({ type: 'append', value: '371' })
147
- ops.push({ type: 'copy', block: 15, from: 'feed', to: 'clone1' })
148
- ops.push({ type: 'copy', block: 27, from: 'feed', to: 'clone1' })
149
- ops.push({ type: 'append', value: '366' })
150
- ops.push({ type: 'copy', block: 11, from: 'clone1', to: 'clone2' })
151
- ops.push({ type: 'copy', block: 29, from: 'clone1', to: 'clone2' })
152
- ops.push({ type: 'copy', block: 15, from: 'clone1', to: 'clone2' })
153
- ops.push({ type: 'append', value: '357' })
154
- ops.push({ type: 'copy', block: 0, from: 'clone1', to: 'clone2' })
155
- ops.push({ type: 'append', value: '351' })
156
- ops.push({ type: 'append', value: '349' })
157
- ops.push({ type: 'copy', block: 23, from: 'clone1', to: 'clone2' })
158
- ops.push({ type: 'copy', block: 8, from: 'feed', to: 'clone1' })
159
- ops.push({ type: 'copy', block: 27, from: 'clone1', to: 'clone2' })
160
- ops.push({ type: 'copy', block: 14, from: 'feed', to: 'clone1' })
161
- ops.push({ type: 'copy', block: 14, from: 'clone1', to: 'clone2' })
162
- ops.push({ type: 'append', value: '330' })
163
- ops.push({ type: 'copy', block: 8, from: 'clone1', to: 'clone2' })
164
- ops.push({ type: 'append', value: '328' })
165
- ops.push({ type: 'append', value: '327' })
166
- ops.push({ type: 'append', value: '326' })
167
- ops.push({ type: 'append', value: '324' })
168
- ops.push({ type: 'copy', block: 26, from: 'clone1', to: 'clone2' })
169
- ops.push({ type: 'append', value: '314' })
170
- ops.push({ type: 'append', value: '313' })
171
- ops.push({ type: 'append', value: '312' })
172
- ops.push({ type: 'append', value: '308' })
173
- ops.push({ type: 'append', value: '307' })
174
- ops.push({ type: 'append', value: '306' })
175
- ops.push({ type: 'append', value: '303' })
176
- ops.push({ type: 'append', value: '300' })
177
- ops.push({ type: 'copy', block: 46, from: 'feed', to: 'clone1' })
178
- ops.push({ type: 'append', value: '295' })
179
- ops.push({ type: 'append', value: '292' })
180
- ops.push({ type: 'append', value: '291' })
181
- ops.push({ type: 'copy', block: 46, from: 'clone1', to: 'clone2' })
182
- ops.push({ type: 'append', value: '288' })
183
- ops.push({ type: 'append', value: '277' })
184
- ops.push({ type: 'copy', block: 51, from: 'feed', to: 'clone1' })
185
- ops.push({ type: 'copy', block: 39, from: 'feed', to: 'clone1' })
186
- ops.push({ type: 'append', value: '271' })
187
- ops.push({ type: 'append', value: '269' })
188
- ops.push({ type: 'copy', block: 45, from: 'feed', to: 'clone1' })
189
- ops.push({ type: 'append', value: '264' })
190
- ops.push({ type: 'copy', block: 51, from: 'clone1', to: 'clone2' })
191
- ops.push({ type: 'append', value: '262' })
192
- ops.push({ type: 'copy', block: 20, from: 'feed', to: 'clone1' })
193
- ops.push({ type: 'append', value: '259' })
194
- ops.push({ type: 'copy', block: 45, from: 'clone1', to: 'clone2' })
195
- ops.push({ type: 'copy', block: 39, from: 'clone1', to: 'clone2' })
196
- ops.push({ type: 'append', value: '256' })
197
- ops.push({ type: 'append', value: '253' })
198
- ops.push({ type: 'append', value: '252' })
199
- ops.push({ type: 'copy', block: 56, from: 'feed', to: 'clone1' })
200
- ops.push({ type: 'append', value: '249' })
201
- ops.push({ type: 'append', value: '248' })
202
- ops.push({ type: 'copy', block: 52, from: 'feed', to: 'clone1' })
203
- ops.push({ type: 'copy', block: 53, from: 'feed', to: 'clone1' })
204
- ops.push({ type: 'append', value: '239' })
205
- ops.push({ type: 'append', value: '237' })
206
- ops.push({ type: 'append', value: '236' })
207
- ops.push({ type: 'append', value: '233' })
208
- ops.push({ type: 'append', value: '231' })
209
- ops.push({ type: 'copy', block: 20, from: 'clone1', to: 'clone2' })
210
- ops.push({ type: 'append', value: '227' })
211
- ops.push({ type: 'copy', block: 53, from: 'clone1', to: 'clone2' })
212
- ops.push({ type: 'append', value: '225' })
213
- ops.push({ type: 'copy', block: 56, from: 'clone1', to: 'clone2' })
214
- ops.push({ type: 'copy', block: 52, from: 'clone1', to: 'clone2' })
215
- ops.push({ type: 'append', value: '220' })
216
- ops.push({ type: 'copy', block: 49, from: 'feed', to: 'clone1' })
217
- ops.push({ type: 'append', value: '216' })
218
- ops.push({ type: 'copy', block: 49, from: 'clone1', to: 'clone2' })
219
- ops.push({ type: 'append', value: '214' })
220
- ops.push({ type: 'append', value: '213' })
221
- ops.push({ type: 'append', value: '212' })
222
- ops.push({ type: 'append', value: '210' })
223
- ops.push({ type: 'append', value: '208' })
224
- ops.push({ type: 'append', value: '205' })
225
- ops.push({ type: 'append', value: '200' })
226
- ops.push({ type: 'copy', block: 30, from: 'feed', to: 'clone1' })
227
- ops.push({ type: 'copy', block: 80, from: 'feed', to: 'clone1' })
228
- ops.push({ type: 'append', value: '195' })
229
- ops.push({ type: 'append', value: '194' })
230
- ops.push({ type: 'append', value: '192' })
231
- ops.push({ type: 'copy', block: 30, from: 'clone1', to: 'clone2' })
232
- ops.push({ type: 'append', value: '188' })
233
- ops.push({ type: 'append', value: '187' })
234
- ops.push({ type: 'copy', block: 80, from: 'clone1', to: 'clone2' })
235
- ops.push({ type: 'append', value: '185' })
236
- ops.push({ type: 'copy', block: 61, from: 'feed', to: 'clone1' })
237
- ops.push({ type: 'append', value: '182' })
238
- ops.push({ type: 'append', value: '181' })
239
- ops.push({ type: 'append', value: '180' })
240
- ops.push({ type: 'copy', block: 44, from: 'feed', to: 'clone1' })
241
- ops.push({ type: 'append', value: '172' })
242
- ops.push({ type: 'append', value: '171' })
243
- ops.push({ type: 'copy', block: 72, from: 'feed', to: 'clone1' })
244
- ops.push({ type: 'append', value: '166' })
245
- ops.push({ type: 'append', value: '164' })
246
- ops.push({ type: 'append', value: '162' })
247
- ops.push({ type: 'append', value: '161' })
248
- ops.push({ type: 'copy', block: 19, from: 'feed', to: 'clone1' })
249
- ops.push({ type: 'append', value: '157' })
250
- ops.push({ type: 'copy', block: 72, from: 'clone1', to: 'clone2' })
251
- ops.push({ type: 'copy', block: 25, from: 'feed', to: 'clone1' })
252
- ops.push({ type: 'append', value: '149' })
253
- ops.push({ type: 'append', value: '146' })
254
- ops.push({ type: 'append', value: '142' })
255
- ops.push({ type: 'append', value: '141' })
256
- ops.push({ type: 'append', value: '133' })
257
- ops.push({ type: 'copy', block: 61, from: 'clone1', to: 'clone2' })
258
- ops.push({ type: 'copy', block: 19, from: 'clone1', to: 'clone2' })
259
- ops.push({ type: 'append', value: '126' })
260
- ops.push({ type: 'copy', block: 25, from: 'clone1', to: 'clone2' })
261
- ops.push({ type: 'append', value: '123' })
262
- ops.push({ type: 'copy', block: 81, from: 'feed', to: 'clone1' })
263
- runOps(t, ops)
264
- })
265
-
266
- tape('chaos monkey randomized', function (t) {
267
- var feed = create()
268
- var clone1
269
- var clone2
270
- var names = ['feed', 'clone1', 'clone2']
271
- var runs = 500
272
- var repro = ' var ops = []\n'
273
-
274
- feed.ready(function () {
275
- clone1 = create(feed.key)
276
- clone2 = create(feed.key)
277
- clone1.ready(function () {
278
- clone2.ready(loop)
279
- })
280
- })
281
-
282
- function loop () {
283
- if (--runs === 0) return t.end()
284
- if (Math.random() < 0.3) {
285
- repro += ' ops.push({type: \'append\', value: \'' + runs + '\'})\n'
286
- return feed.append('' + runs, loop)
287
- }
288
- if (Math.random() < 0.5) return process.nextTick(loop)
289
-
290
- var from = [feed, clone1, clone2][(Math.random() * 3) | 0]
291
- var to = null
292
- if (from === clone1) to = clone2
293
- else if (from === clone2) to = clone1
294
- else to = [clone1, clone1][(Math.random() * 2) | 0]
295
-
296
- var blk = -1
297
- var seen = 0
298
-
299
- for (var i = 0; i < from.length; i++) {
300
- if (!from.has(i) || to.has(i)) continue
301
- if (Math.random() < 1 / ++seen) blk = i
302
- }
303
-
304
- if (blk === -1) return process.nextTick(loop)
305
-
306
- repro += ' ops.push({type: \'copy\', block: ' + blk + ', from: \'' + name(from) + '\', to: \'' + name(to) + '\'})\n'
307
- copy(blk, from, to, function (err) {
308
- if (err) {
309
- t.fail('failed to copy ' + blk + ' from ' + name(from) + ' to ' + name(to))
310
- t.end()
311
- repro += ' runOps(t, ops)\n'
312
- console.error('tape(\'chaos monkey randomized\', function (t) {\n' + repro + '})')
313
- return
314
- }
315
-
316
- t.pass('copied ' + blk + ' from ' + name(from) + ' to ' + name(to))
317
- loop()
318
- })
319
- }
320
-
321
- function name (f) {
322
- return names[[feed, clone1, clone2].indexOf(f)]
323
- }
324
- })
325
-
326
- function runOps (t, ops) {
327
- var feed = create()
328
- var clone1
329
- var clone2
330
-
331
- feed.ready(function () {
332
- clone1 = create(feed.key)
333
- clone2 = create(feed.key)
334
- clone1.ready(function () {
335
- clone2.ready(loop)
336
- })
337
- })
338
-
339
- function get (name) {
340
- if (name === 'feed') return feed
341
- if (name === 'clone1') return clone1
342
- return clone2
343
- }
344
-
345
- function loop () {
346
- var next = ops.shift()
347
- if (!next) {
348
- t.end()
349
- return
350
- }
351
- if (next.type === 'append') {
352
- feed.append(next.value, loop)
353
- return
354
- }
355
- if (next.type === 'copy') {
356
- copy(next.block, get(next.from), get(next.to), function (err) {
357
- if (err) {
358
- t.fail('failed to copy ' + next.block + ' from ' + next.from + ' to ' + next.to)
359
- t.end()
360
- return
361
- }
362
- t.pass('copied ' + next.block + ' from ' + next.from + ' to ' + next.to)
363
- loop()
364
- })
365
- }
366
- }
367
- }
368
-
369
- function copy (index, from, to, cb) {
370
- from.proof(index, { digest: to.tree.digest(2 * index) }, function (err, proof) {
371
- if (err) return cb(err)
372
- from.get(index, function (err, data) {
373
- if (err) return cb(err)
374
- to.put(index, data, proof, cb)
375
- })
376
- })
377
- }
@@ -1,51 +0,0 @@
1
- const tape = require('tape')
2
- const path = require('path')
3
- const hypercore = require('../')
4
-
5
- tape('default storage works', function (t) {
6
- const dir = path.join(__dirname, 'sandbox')
7
- const feed = hypercore(dir, { overwrite: true })
8
-
9
- feed.append('a', function (err) {
10
- t.error(err, 'no error')
11
-
12
- feed.get(0, function (err, data) {
13
- t.error(err, 'no error')
14
- t.same(data, Buffer.from('a'))
15
- t.same(feed.length, 1)
16
-
17
- feed.close(function () {
18
- const feed2 = hypercore(dir)
19
-
20
- feed2.ready(function (err) {
21
- t.error(err, 'no error')
22
- t.same(feed2.length, 1)
23
- feed2.close(function () {
24
- t.end()
25
- })
26
- })
27
- })
28
- })
29
- })
30
- })
31
-
32
- tape('destroying storage works', function (t) {
33
- const dir = path.join(__dirname, 'sandbox')
34
- const feed = hypercore(dir, { overwrite: true })
35
-
36
- feed.ready(function () {
37
- const key = feed.key
38
- t.ok(key, 'generated key')
39
- feed.destroyStorage(function () {
40
- t.pass('destroyed feed')
41
- const feed2 = hypercore(dir)
42
-
43
- feed2.ready(function () {
44
- t.notSame(feed2.key, key, 'data got destroyed')
45
- feed2.close(function () {
46
- t.end()
47
- })
48
- })
49
- })
50
- })
51
- })
@@ -1,137 +0,0 @@
1
- var create = require('./helpers/create')
2
- var replicate = require('./helpers/replicate')
3
- var tape = require('tape')
4
-
5
- var EXAMPLE_TYPE = 'example'
6
- var EXAMPLE_MESSAGE = Buffer.from([4, 20])
7
-
8
- tape('send and receive extension messages', function (t) {
9
- t.plan(2)
10
-
11
- var feed1 = create(null)
12
-
13
- const e1 = feed1.registerExtension(EXAMPLE_TYPE, {
14
- onmessage (message, peer) {
15
- t.equal(message.toString('hex'), EXAMPLE_MESSAGE.toString('hex'))
16
- }
17
- })
18
-
19
- feed1.ready(function () {
20
- var feed2 = create(feed1.key)
21
-
22
- const e2 = feed2.registerExtension(EXAMPLE_TYPE, {
23
- onmessage (message, peer) {
24
- t.equal(message.toString('hex'), EXAMPLE_MESSAGE.toString('hex'))
25
- e2.send(EXAMPLE_MESSAGE, peer)
26
- }
27
- })
28
-
29
- feed1.once('peer-open', function () {
30
- e1.broadcast(EXAMPLE_MESSAGE)
31
- })
32
-
33
- replicate(feed1, feed2, { live: true })
34
- })
35
- })
36
-
37
- tape('send and receive extension messages with encoding', function (t) {
38
- t.plan(2)
39
-
40
- const feed1 = create(null)
41
-
42
- const e1 = feed1.registerExtension(EXAMPLE_TYPE, {
43
- encoding: 'json',
44
- onmessage (message, peer) {
45
- t.same(message, { hi: 'e1' })
46
- }
47
- })
48
-
49
- feed1.ready(function () {
50
- const feed2 = create(feed1.key)
51
-
52
- const e2 = feed2.registerExtension(EXAMPLE_TYPE, {
53
- encoding: 'json',
54
- onmessage (message, peer) {
55
- t.same(message, { hi: 'e2' })
56
- e2.send({ hi: 'e1' }, peer)
57
- }
58
- })
59
-
60
- feed1.once('peer-open', function () {
61
- e1.broadcast({ hi: 'e2' })
62
- })
63
-
64
- replicate(feed1, feed2, { live: true })
65
- })
66
- })
67
-
68
- tape('send and receive extension messages with multiple extensions', function (t) {
69
- t.plan(2)
70
-
71
- var feed1 = create(null)
72
-
73
- const e1 = feed1.registerExtension(EXAMPLE_TYPE, {
74
- onmessage (message, peer) {
75
- t.equal(message.toString('hex'), EXAMPLE_MESSAGE.toString('hex'))
76
- }
77
- })
78
-
79
- feed1.registerExtension('aa')
80
-
81
- feed1.ready(function () {
82
- var feed2 = create(feed1.key)
83
-
84
- feed2.registerExtension('bb')
85
-
86
- const e2 = feed2.registerExtension(EXAMPLE_TYPE, {
87
- onmessage (message, peer) {
88
- t.equal(message.toString('hex'), EXAMPLE_MESSAGE.toString('hex'))
89
- e2.send(EXAMPLE_MESSAGE, peer)
90
- }
91
- })
92
-
93
- feed1.once('peer-open', function () {
94
- e1.broadcast(EXAMPLE_MESSAGE)
95
- })
96
-
97
- replicate(feed1, feed2, { live: true })
98
- })
99
- })
100
-
101
- tape('extension encoding', t => {
102
- t.plan(3)
103
-
104
- const f1 = create(null)
105
-
106
- const dummyExt = {
107
- encoding: {
108
- encode () {
109
- t.pass('encoder was invoked')
110
- return Buffer.from('test')
111
- },
112
- decode () {
113
- t.pass('decode was invoked')
114
- return 'test'
115
- }
116
- },
117
- onerror (err) {
118
- t.error(err, 'no error')
119
- },
120
- onmessage () {
121
- t.pass('got message')
122
- }
123
- }
124
-
125
- const inst1 = f1.registerExtension('dummy', dummyExt)
126
-
127
- f1.ready(function () {
128
- const f2 = create(f1.key)
129
- f2.ready(function () {
130
- f2.registerExtension('dummy', dummyExt)
131
- replicate(f1, f2, { live: true })
132
- f1.once('peer-open', function () {
133
- inst1.broadcast({ hello: 'world' })
134
- })
135
- })
136
- })
137
- })
package/test/get.js DELETED
@@ -1,64 +0,0 @@
1
- var create = require('./helpers/create')
2
- var replicate = require('./helpers/replicate')
3
- var tape = require('tape')
4
-
5
- tape('get with ifAvailable waits correctly', t => {
6
- var feed1 = create()
7
- var feed2 = null
8
-
9
- feed1.append('hello', () => {
10
- feed2 = create(feed1.key)
11
- feed2.get(0, { ifAvailable: true }, (err, content) => {
12
- t.error(err, 'no error')
13
- t.same(content, Buffer.from('hello'))
14
- t.end()
15
- })
16
- replicate(feed1, feed2)
17
- })
18
- })
19
-
20
- tape('get with ifAvailable returns null for unavilable index', t => {
21
- var feed1 = create()
22
- var feed2 = null
23
-
24
- feed1.append('hello', () => {
25
- feed2 = create(feed1.key)
26
- feed2.get(1, { ifAvailable: true }, (err, content) => {
27
- t.true(err)
28
- t.end()
29
- })
30
- replicate(feed1, feed2)
31
- })
32
- })
33
-
34
- tape('get with ifAvailable does not wait forever', t => {
35
- var feed1 = create()
36
- var feed2 = null
37
-
38
- feed1.append('hello', () => {
39
- feed2 = create(feed1.key)
40
- feed2.get(0, { ifAvailable: true }, (err, content) => {
41
- t.true(err)
42
- t.end()
43
- })
44
- setTimeout(() => {
45
- replicate(feed1, feed2)
46
- }, 50)
47
- })
48
- })
49
-
50
- tape('get with top-level ifAvailable option does not wait forever', t => {
51
- var feed1 = create()
52
- var feed2 = null
53
-
54
- feed1.append('hello', () => {
55
- feed2 = create(feed1.key, { ifAvailable: true })
56
- feed2.get(0, (err, content) => {
57
- t.true(err)
58
- t.end()
59
- })
60
- setTimeout(() => {
61
- replicate(feed1, feed2)
62
- }, 50)
63
- })
64
- })