hypercore 10.8.0 → 10.9.0
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 +8 -0
- package/index.js +6 -2
- package/lib/bitfield.js +51 -0
- package/package.json +1 -1
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') {
|
|
@@ -472,6 +472,10 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
472
472
|
return this.core.tree.length
|
|
473
473
|
}
|
|
474
474
|
|
|
475
|
+
get indexedLength () {
|
|
476
|
+
return this.length
|
|
477
|
+
}
|
|
478
|
+
|
|
475
479
|
/**
|
|
476
480
|
* Deprecated. Use `const { byteLength } = await core.info()`.
|
|
477
481
|
*/
|
package/lib/bitfield.js
CHANGED
|
@@ -53,6 +53,27 @@ 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 {
|
|
@@ -308,6 +329,36 @@ module.exports = class Bitfield {
|
|
|
308
329
|
return this.findLast(false, position)
|
|
309
330
|
}
|
|
310
331
|
|
|
332
|
+
count (start, length, val) {
|
|
333
|
+
let j = start & (BITS_PER_PAGE - 1)
|
|
334
|
+
let i = (start - j) / BITS_PER_PAGE
|
|
335
|
+
let c = 0
|
|
336
|
+
|
|
337
|
+
while (length > 0) {
|
|
338
|
+
const p = this._pages.get(i)
|
|
339
|
+
|
|
340
|
+
const end = Math.min(j + length, BITS_PER_PAGE)
|
|
341
|
+
const range = end - j
|
|
342
|
+
|
|
343
|
+
if (p) c += p.count(j, range, val)
|
|
344
|
+
else if (!val) c += range
|
|
345
|
+
|
|
346
|
+
j = 0
|
|
347
|
+
i++
|
|
348
|
+
length -= range
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return c
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
countSet (start, length) {
|
|
355
|
+
return this.count(start, length, true)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
countUnset (start, length) {
|
|
359
|
+
return this.count(start, length, false)
|
|
360
|
+
}
|
|
361
|
+
|
|
311
362
|
* want (start, length) {
|
|
312
363
|
const j = start & (BITS_PER_SEGMENT - 1)
|
|
313
364
|
let i = (start - j) / BITS_PER_SEGMENT
|