hypercore 10.8.1 → 10.9.1

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.
package/README.md CHANGED
@@ -416,3 +416,11 @@ Emitted when the core has been appended to (i.e. has a new length / byteLength),
416
416
  #### `core.on('truncate', ancestors, forkId)`
417
417
 
418
418
  Emitted when the core has been truncated, either locally or remotely.
419
+
420
+ #### `core.on('peer-add')`
421
+
422
+ Emitted when a new connection has been established with a peer.
423
+
424
+ #### `core.on('peer-remove')`
425
+
426
+ Emitted when a peer's connection has been closed.
package/index.js CHANGED
@@ -27,10 +27,10 @@ module.exports = class Hypercore extends EventEmitter {
27
27
  if (isOptions(storage)) {
28
28
  opts = storage
29
29
  storage = null
30
- key = null
30
+ key = opts.key || null
31
31
  } else if (isOptions(key)) {
32
32
  opts = key
33
- key = null
33
+ key = opts.key || null
34
34
  }
35
35
 
36
36
  if (key && typeof key === 'string') {
package/lib/bitfield.js CHANGED
@@ -53,13 +53,34 @@ class BitfieldPage {
53
53
  findLast (val, position) {
54
54
  return quickbit.findLast(this.bitfield, val, position)
55
55
  }
56
+
57
+ count (start, length, val) {
58
+ const end = start + length
59
+
60
+ let i = start
61
+ let c = 0
62
+
63
+ while (length > 0) {
64
+ const l = this.findFirst(val, i)
65
+ if (l === -1 || l >= end) return c
66
+
67
+ const h = this.findFirst(!val, l + 1)
68
+ if (h === -1 || h >= end) return c + end - l
69
+
70
+ c += h - l
71
+ length -= h - i
72
+ i = h
73
+ }
74
+
75
+ return c
76
+ }
56
77
  }
57
78
 
58
79
  class BitfieldSegment {
59
80
  constructor (index, bitfield) {
60
81
  this.index = index
61
82
  this.offset = index * BYTES_PER_SEGMENT
62
- this.tree = quickbit.Index.from(bitfield)
83
+ this.tree = quickbit.Index.from(bitfield, BYTES_PER_SEGMENT)
63
84
  this.pages = new Array(PAGES_PER_SEGMENT)
64
85
  }
65
86
 
@@ -86,7 +107,7 @@ class BitfieldSegment {
86
107
  const bitfield = new Uint32Array(target)
87
108
  bitfield.set(this.bitfield)
88
109
 
89
- this.tree = quickbit.Index.from(bitfield)
110
+ this.tree = quickbit.Index.from(bitfield, BYTES_PER_SEGMENT)
90
111
 
91
112
  for (let i = 0; i < this.pages.length; i++) {
92
113
  const page = this.pages[i]
@@ -105,17 +126,16 @@ class BitfieldSegment {
105
126
  const j = position & (BITS_PER_PAGE - 1)
106
127
  const i = (position - j) / BITS_PER_PAGE
107
128
 
108
- if (i >= PAGES_PER_SEGMENT) return -1
129
+ if (i >= PAGES_PER_SEGMENT) return val ? -1 : position
109
130
 
110
131
  const p = this.pages[i]
111
132
 
112
- if (p) {
113
- const index = p.findFirst(val, j)
133
+ let index = -1
114
134
 
115
- if (index !== -1) {
116
- return i * BITS_PER_PAGE + index
117
- }
118
- }
135
+ if (p) index = p.findFirst(val, j)
136
+ else if (!val) index = j
137
+
138
+ if (index !== -1) return i * BITS_PER_PAGE + index
119
139
 
120
140
  return -1
121
141
  }
@@ -130,13 +150,12 @@ class BitfieldSegment {
130
150
 
131
151
  const p = this.pages[i]
132
152
 
133
- if (p) {
134
- const index = p.findLast(val, j)
153
+ let index = -1
135
154
 
136
- if (index !== -1) {
137
- return i * BITS_PER_PAGE + index
138
- }
139
- }
155
+ if (p) index = p.findLast(val, j)
156
+ else if (!val) index = j
157
+
158
+ if (index !== -1) return i * BITS_PER_PAGE + index
140
159
 
141
160
  return -1
142
161
  }
@@ -255,19 +274,18 @@ module.exports = class Bitfield {
255
274
  while (i < this._segments.maxLength) {
256
275
  const s = this._segments.get(i)
257
276
 
258
- if (s) {
259
- const index = s.findFirst(val, j)
277
+ let index = -1
260
278
 
261
- if (index !== -1) {
262
- return i * BITS_PER_SEGMENT + index
263
- }
264
- }
279
+ if (s) index = s.findFirst(val, j)
280
+ else if (!val) index = j
281
+
282
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
265
283
 
266
284
  j = 0
267
285
  i++
268
286
  }
269
287
 
270
- return -1
288
+ return val ? -1 : position
271
289
  }
272
290
 
273
291
  firstSet (position) {
@@ -285,13 +303,12 @@ module.exports = class Bitfield {
285
303
  while (i >= 0) {
286
304
  const s = this._segments.get(i)
287
305
 
288
- if (s) {
289
- const index = s.findLast(val, j)
306
+ let index = -1
290
307
 
291
- if (index !== -1) {
292
- return i * BITS_PER_SEGMENT + index
293
- }
294
- }
308
+ if (s) index = s.findLast(val, j)
309
+ else if (!val) index = j
310
+
311
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
295
312
 
296
313
  j = BITS_PER_SEGMENT - 1
297
314
  i--
@@ -308,6 +325,36 @@ module.exports = class Bitfield {
308
325
  return this.findLast(false, position)
309
326
  }
310
327
 
328
+ count (start, length, val) {
329
+ let j = start & (BITS_PER_PAGE - 1)
330
+ let i = (start - j) / BITS_PER_PAGE
331
+ let c = 0
332
+
333
+ while (length > 0) {
334
+ const p = this._pages.get(i)
335
+
336
+ const end = Math.min(j + length, BITS_PER_PAGE)
337
+ const range = end - j
338
+
339
+ if (p) c += p.count(j, range, val)
340
+ else if (!val) c += range
341
+
342
+ j = 0
343
+ i++
344
+ length -= range
345
+ }
346
+
347
+ return c
348
+ }
349
+
350
+ countSet (start, length) {
351
+ return this.count(start, length, true)
352
+ }
353
+
354
+ countUnset (start, length) {
355
+ return this.count(start, length, false)
356
+ }
357
+
311
358
  * want (start, length) {
312
359
  const j = start & (BITS_PER_SEGMENT - 1)
313
360
  let i = (start - j) / BITS_PER_SEGMENT
@@ -58,7 +58,7 @@ class RemoteBitfieldSegment {
58
58
  constructor (index) {
59
59
  this.index = index
60
60
  this.offset = index * BYTES_PER_SEGMENT
61
- this.tree = quickbit.Index.from([])
61
+ this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
62
62
  this.pages = new Array(PAGES_PER_SEGMENT)
63
63
  }
64
64
 
@@ -87,17 +87,16 @@ class RemoteBitfieldSegment {
87
87
  const j = position & (BITS_PER_PAGE - 1)
88
88
  const i = (position - j) / BITS_PER_PAGE
89
89
 
90
- if (i >= PAGES_PER_SEGMENT) return -1
90
+ if (i >= PAGES_PER_SEGMENT) return val ? -1 : position
91
91
 
92
92
  const p = this.pages[i]
93
93
 
94
- if (p) {
95
- const index = p.findFirst(val, j)
94
+ let index = -1
96
95
 
97
- if (index !== -1) {
98
- return i * BITS_PER_PAGE + index
99
- }
100
- }
96
+ if (p) index = p.findFirst(val, j)
97
+ else if (!val) index = j
98
+
99
+ if (index !== -1) return i * BITS_PER_PAGE + index
101
100
 
102
101
  return -1
103
102
  }
@@ -108,17 +107,16 @@ class RemoteBitfieldSegment {
108
107
  const j = position & (BITS_PER_PAGE - 1)
109
108
  const i = (position - j) / BITS_PER_PAGE
110
109
 
111
- if (i >= PAGES_PER_SEGMENT) return -1
110
+ if (i >= PAGES_PER_SEGMENT) return val ? -1 : position
112
111
 
113
112
  const p = this.pages[i]
114
113
 
115
- if (p) {
116
- const index = p.findLast(val, j)
114
+ let index = -1
117
115
 
118
- if (index !== -1) {
119
- return i * BITS_PER_PAGE + index
120
- }
121
- }
116
+ if (p) index = p.findLast(val, j)
117
+ else if (!val) index = j
118
+
119
+ if (index !== -1) return i * BITS_PER_PAGE + index
122
120
 
123
121
  return -1
124
122
  }
@@ -187,19 +185,18 @@ module.exports = class RemoteBitfield {
187
185
  while (i < this._segments.maxLength) {
188
186
  const s = this._segments.get(i)
189
187
 
190
- if (s) {
191
- const index = s.findFirst(val, j)
188
+ let index = -1
192
189
 
193
- if (index !== -1) {
194
- return i * BITS_PER_SEGMENT + index
195
- }
196
- }
190
+ if (s) index = s.findFirst(val, j)
191
+ else if (!val) index = j
192
+
193
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
197
194
 
198
195
  j = 0
199
196
  i++
200
197
  }
201
198
 
202
- return -1
199
+ return val ? -1 : position
203
200
  }
204
201
 
205
202
  firstSet (position) {
@@ -217,13 +214,12 @@ module.exports = class RemoteBitfield {
217
214
  while (i >= 0) {
218
215
  const s = this._segments.get(i)
219
216
 
220
- if (s) {
221
- const index = s.findLast(val, j)
217
+ let index = -1
222
218
 
223
- if (index !== -1) {
224
- return i * BITS_PER_SEGMENT + index
225
- }
226
- }
219
+ if (s) index = s.findLast(val, j)
220
+ else if (!val) index = j
221
+
222
+ if (index !== -1) return i * BITS_PER_SEGMENT + index
227
223
 
228
224
  j = BITS_PER_SEGMENT - 1
229
225
  i--
package/lib/replicator.js CHANGED
@@ -1673,17 +1673,13 @@ function clampRange (core, r) {
1673
1673
  if (r.blocks === null) {
1674
1674
  const start = core.bitfield.firstUnset(r.start)
1675
1675
 
1676
- if (r.end === -1) {
1677
- r.start = start === -1 ? core.tree.length : start
1678
- } else {
1679
- const end = core.bitfield.lastUnset(r.end - 1) + 1
1680
-
1681
- if (start === -1) {
1682
- r.start = r.end
1683
- } else {
1684
- r.start = start
1685
- r.end = end
1686
- }
1676
+ if (r.end === -1) r.start = start === -1 ? core.tree.length : start
1677
+ else if (start === -1) r.start = r.end
1678
+ else {
1679
+ const end = core.bitfield.lastUnset(r.end)
1680
+
1681
+ r.start = start
1682
+ r.end = end
1687
1683
  }
1688
1684
  } else {
1689
1685
  while (r.start < r.end && core.bitfield.get(r.blocks[r.start])) r.start++
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.8.1",
3
+ "version": "10.9.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,7 +43,7 @@
43
43
  "hypercore-crypto": "^3.2.1",
44
44
  "is-options": "^1.0.1",
45
45
  "protomux": "^3.4.0",
46
- "quickbit-universal": "^2.0.3",
46
+ "quickbit-universal": "^2.1.0",
47
47
  "random-access-file": "^4.0.0",
48
48
  "random-array-iterator": "^1.0.0",
49
49
  "safety-catch": "^1.0.1",