Skip to content

Commit 0514e24

Browse files
committed
Upgrade holepunch preset
1 parent 5fd5b38 commit 0514e24

File tree

5 files changed

+15
-59
lines changed

5 files changed

+15
-59
lines changed

index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ class Hyperblobs {
154154

155155
async get(id, opts) {
156156
const all =
157-
!opts ||
158-
(!opts.start &&
159-
opts.length === undefined &&
160-
opts.end === undefined &&
161-
!opts.core)
157+
!opts || (!opts.start && opts.length === undefined && opts.end === undefined && !opts.core)
162158
if (all) return this._getAll(id, opts)
163159

164160
const res = []
@@ -176,11 +172,7 @@ class Hyperblobs {
176172
}
177173

178174
async clear(id, opts) {
179-
return this.core.clear(
180-
id.blockOffset,
181-
id.blockOffset + id.blockLength,
182-
opts
183-
)
175+
return this.core.clear(id.blockOffset, id.blockOffset + id.blockLength, opts)
184176
}
185177

186178
createReadStream(id, opts) {

lib/monitor.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ module.exports = class Monitor extends EventEmitter {
2727

2828
this.uploadStats = { ...stats }
2929
this.downloadStats = { ...stats }
30-
this.uploadStats.targetBytes = this.downloadStats.targetBytes =
31-
this.id.byteLength
32-
this.uploadStats.targetBlocks = this.downloadStats.targetBlocks =
33-
this.id.blockLength
34-
this.uploadStats.peers =
35-
this.downloadStats.peers =
36-
this.peers =
37-
this.blobs.core.peers.length
30+
this.uploadStats.targetBytes = this.downloadStats.targetBytes = this.id.byteLength
31+
this.uploadStats.targetBlocks = this.downloadStats.targetBlocks = this.id.blockLength
32+
this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length
3833

3934
this.uploadSpeedometer = speedometer()
4035
this.downloadSpeedometer = speedometer()
@@ -52,30 +47,15 @@ module.exports = class Monitor extends EventEmitter {
5247
}
5348

5449
_onUpload(index, bytes, from) {
55-
this._updateStats(
56-
this.uploadSpeedometer,
57-
this.uploadStats,
58-
index,
59-
bytes,
60-
from
61-
)
50+
this._updateStats(this.uploadSpeedometer, this.uploadStats, index, bytes, from)
6251
}
6352

6453
_onDownload(index, bytes, from) {
65-
this._updateStats(
66-
this.downloadSpeedometer,
67-
this.downloadStats,
68-
index,
69-
bytes,
70-
from
71-
)
54+
this._updateStats(this.downloadSpeedometer, this.downloadStats, index, bytes, from)
7255
}
7356

7457
_updatePeers() {
75-
this.uploadStats.peers =
76-
this.downloadStats.peers =
77-
this.peers =
78-
this.blobs.core.peers.length
58+
this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length
7959
this.emit('update')
8060
}
8161

lib/prefetcher.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33
const MAX_READAHEAD_TARGET = 0.05 // aim to buffer 5% always
44

55
module.exports = class Prefetcher {
6-
constructor(
7-
core,
8-
{ max = 64, start = 0, end = core.length, linear = true } = {}
9-
) {
6+
constructor(core, { max = 64, start = 0, end = core.length, linear = true } = {}) {
107
this.core = core
118
this.max = max
129
this.range = null
1310
this.startBound = start
1411
this.endBound = end
15-
this.maxReadAhead = Math.max(
16-
max * 2,
17-
Math.floor((end - start) * MAX_READAHEAD_TARGET)
18-
)
12+
this.maxReadAhead = Math.max(max * 2, Math.floor((end - start) * MAX_READAHEAD_TARGET))
1913

2014
this.start = start
2115
this.end = start
@@ -57,8 +51,7 @@ module.exports = class Prefetcher {
5751
this.missing++
5852
}
5953

60-
if (end > this.start + this.maxReadAhead)
61-
end = this.start + this.maxReadAhead
54+
if (end > this.start + this.maxReadAhead) end = this.start + this.maxReadAhead
6255
if (end >= this.endBound) end = this.endBound
6356

6457
this.end = end

lib/streams.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@ class BlobReadStream extends Readable {
6969

7070
const start = id.blockOffset
7171
const end = id.blockOffset + id.blockLength
72-
const noPrefetch =
73-
opts.wait === false || opts.prefetch === false || !core.core
72+
const noPrefetch = opts.wait === false || opts.prefetch === false || !core.core
7473

7574
this._prefetch = noPrefetch
7675
? null
7776
: new Prefetcher(this.core, { max: opts.prefetch, start, end })
7877
this._lastPrefetch = null
7978

80-
this._pos =
81-
opts.start !== undefined ? id.byteOffset + opts.start : id.byteOffset
79+
this._pos = opts.start !== undefined ? id.byteOffset + opts.start : id.byteOffset
8280

8381
if (opts.length !== undefined) this._end = this._pos + opts.length
8482
else if (opts.end !== undefined) this._end = id.byteOffset + opts.end + 1
@@ -137,10 +135,7 @@ class BlobReadStream extends Readable {
137135

138136
const remainder = this._end - this._pos
139137
if (this._relativeOffset || remainder < block.length) {
140-
block = block.subarray(
141-
this._relativeOffset,
142-
this._relativeOffset + remainder
143-
)
138+
block = block.subarray(this._relativeOffset, this._relativeOffset + remainder)
144139
}
145140

146141
this._index++

test/all.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ test('upload/download can be monitored', async (t) => {
359359
monitor.on('update', () => {
360360
t.is(monitor.downloadStats.blocks, expectedBlocks.pop())
361361
if (monitor.downloadStats.targetBlocks === monitor.downloadStats.blocks) {
362-
t.is(
363-
monitor.downloadStats.monitoringBytes,
364-
bytes,
365-
'downloaded all bytes'
366-
)
362+
t.is(monitor.downloadStats.monitoringBytes, bytes, 'downloaded all bytes')
367363
}
368364
t.is(monitor.downloadStats.targetBlocks, 2)
369365
t.is(monitor.downloadStats.targetBytes, bytes)

0 commit comments

Comments
 (0)