@torrent-tv/proxy 2.70.0 → 2.71.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.
@@ -1,279 +1,319 @@
1
- /**
2
- * @file Put the piece a reader is blocked on onto the fastest wires that hold
3
- * it.
4
- *
5
- * Measured 2026-08-17: the swarm delivered 5.1-5.9 MB/s against a film consumed
6
- * at about 1 MB/s — a fivefold surplus — and the reader still blocked 47 times
7
- * in two minutes, 1.0-4.5 s each. So the shortage is not bandwidth. The piece
8
- * that blocked had been requested from a median of five peers, and what set the
9
- * tail was WHICH wire held the last outstanding block: a block is reserved for
10
- * exactly one wire, and the read finishes when the slowest holder delivers.
11
- *
12
- * What the library already does, and what it does not: `torrent.critical()`
13
- * (which the reader already sets over its window) enables HOTSWAP — an idle
14
- * wire may take a block away from the SLOWEST holder. It does not duplicate:
15
- * `piece.reserve()` returns -1 once every block is spoken for, and the
16
- * end-game that would ask a second peer for the same block is commented out in
17
- * `webtorrent/lib/torrent.js`. Hotswap therefore fires only when the library's
18
- * own picker happens to visit an idle wire while our piece is critical.
19
- *
20
- * This asks for it on purpose, and chooses who: the wires that are unchoked,
21
- * hold the piece and are measurably fastest are handed the piece through the
22
- * same entry the library's picker uses, with hotswap enabled. Nothing is
23
- * duplicated and no protocol rule is bent — the block moves to a faster holder
24
- * instead of staying with whoever got it first.
25
- */
26
-
27
- /**
28
- * The library's own request entry. Internal, so its absence must be noticed
29
- * rather than swallowed: without it this lever silently does nothing.
30
- *
31
- * @param {object} torrent
32
- * @returns {boolean}
33
- */
34
- export function canPlaceRequests(torrent) {
35
- return typeof torrent?._request === "function";
36
- }
37
-
38
- /**
39
- * Wires that could deliver this piece right now, fastest first.
40
- *
41
- * Excluded, each for its own reason: a wire that is choking us cannot be asked
42
- * at all; one that does not hold the piece has nothing to give; a destroyed one
43
- * is a corpse. Speed is the library's own measurement over its own window, so
44
- * nothing here needs a history of its own.
45
- *
46
- * @param {object} torrent
47
- * @param {number} pieceIndex
48
- * @returns {object[]}
49
- */
50
- export function wiresForPiece(torrent, pieceIndex) {
51
- const wires = Array.isArray(torrent?.wires) ? torrent.wires : [];
52
- const usable = [];
53
- for (const wire of wires) {
54
- if (!wire || wire.destroyed || wire.peerChoking) {
55
- continue;
56
- }
57
- if (wire.peerPieces?.get?.(pieceIndex) !== true) {
58
- continue;
59
- }
60
- usable.push(wire);
61
- }
62
- return usable.sort((left, right) => speedOf(right) - speedOf(left));
63
- }
64
-
65
- /**
66
- * A wire's measured download speed in bytes per second, or 0 when it has not
67
- * been measured. Wrapped because `downloadSpeed` is a method on the wire and a
68
- * throw from a destroyed one must not take the caller with it.
69
- *
70
- * @param {object} wire
71
- * @returns {number}
72
- */
73
- function speedOf(wire) {
74
- try {
75
- const speed = wire?.downloadSpeed?.();
76
- return Number.isFinite(speed) ? speed : 0;
77
- } catch {
78
- // A wire that cannot say how fast it is ranks last, which is the honest
79
- // answer — and the caller's own log line reports how many were asked.
80
- return 0;
81
- }
82
- }
83
-
84
- /**
85
- * Ask the fastest holders of `pieceIndex` for it.
86
- *
87
- * @param {object} torrent
88
- * @param {number} pieceIndex
89
- * @param {number} [limit] - How many wires to push it onto.
90
- * @returns {{ asked: number, attempted: number, considered: number, fastestBytesPerSecond: number }}
91
- * `asked` counts requests the library actually placed: it refuses when a
92
- * wire's pipeline is full or when nothing can be reserved even with hotswap,
93
- * and that refusal is information — a piece nobody can be asked for is
94
- * waiting on the wire, not on the picker.
95
- */
96
- export function askFastestWiresFor(torrent, pieceIndex, limit = 3) {
97
- if (!canPlaceRequests(torrent) || !Number.isInteger(pieceIndex) || pieceIndex < 0) {
98
- return { asked: 0, attempted: 0, considered: 0, fastestBytesPerSecond: 0 };
99
- }
100
- const candidates = wiresForPiece(torrent, pieceIndex);
101
- let asked = 0;
102
- for (const wire of candidates.slice(0, Math.max(1, limit))) {
103
- try {
104
- // `true` is hotswap: if every block is reserved, take one from the
105
- // slowest holder. That is the whole point the reader is blocked
106
- // precisely because a slow holder has one.
107
- if (torrent._request(wire, pieceIndex, true) === true) {
108
- asked += 1;
109
- }
110
- } catch (error) {
111
- // Internal call: report it once per attempt rather than letting the lever
112
- // fail in silence.
113
- throw new Error(`could not place a request for piece ${pieceIndex}: ${error?.message ?? error}`);
114
- }
115
- }
116
- return {
117
- asked,
118
- considered: candidates.length,
119
- // How many of the asks the library placed, against how many it was asked
120
- // for. The caller sums these over the whole wait, and summing `asked`
121
- // against a `considered` taken from the LAST attempt is how the field log
122
- // came to read "steered onto 12 of 6 holders" — a ratio of two different
123
- // things. Both halves are returned per attempt so the caller can add each
124
- // to its own total.
125
- attempted: Math.min(candidates.length, Math.max(1, limit)),
126
- fastestBytesPerSecond: candidates.length > 0 ? speedOf(candidates[0]) : 0
127
- };
128
- }
129
-
130
- /**
131
- * What is actually holding up a piece the reader is blocked on.
132
- *
133
- * The steering above can only move a block that the library will hand over, and
134
- * the field says it often hands over nothing: `steered onto 0 of 9 asks (8
135
- * peers held it)`, recorded 2026-08-18 while eight peers had the piece. When
136
- * every block of a piece is already reserved, `Piece.reserve()` answers -1 and
137
- * there is no request left to place the read then ends when the SLOWEST
138
- * holder delivers its block, however fast the rest of the swarm is.
139
- *
140
- * Duplicating those last blocks onto faster wires is the standard remedy, and
141
- * it is not free: every duplicate is a block's worth of traffic paid twice. So
142
- * this describes the tail before anything is built with it — how many blocks
143
- * are still missing, and on which wires they sit, with each wire's speed. If
144
- * the missing blocks turn out to sit on one slow wire, duplication is aimed at
145
- * exactly the right thing; if they are spread across fast ones, the wait has
146
- * another cause and this work should not be done at all.
147
- *
148
- * Reads only: nothing here changes a reservation or places a request.
149
- *
150
- * @param {import("webtorrent").Torrent} torrent
151
- * @param {number} pieceIndex
152
- * @returns {{ chunks: number, missing: number,
153
- * outstanding: Array<{ blocks: number, bytesPerSecond: number, choking: boolean }> } | null}
154
- * Null only when the piece object is gone it completed and was cleared.
155
- * A piece nobody has reserved a block of yet is NOT null: `torrent-piece`
156
- * creates its buffer lazily on the first reserve, so a piece the picker has
157
- * not reached reads as every block missing and nothing outstanding, which is
158
- * the most informative answer this can give the wait is not on a slow
159
- * holder, it is on nobody having been asked.
160
- */
161
- export function describePieceTail(torrent, pieceIndex) {
162
- const piece = torrent?.pieces?.[pieceIndex];
163
- if (!piece) {
164
- return null;
165
- }
166
- const buffer = Array.isArray(piece._buffer) ? piece._buffer : null;
167
- const chunks = Number.isFinite(piece._chunks)
168
- ? piece._chunks
169
- : (buffer ? buffer.length : 0);
170
- // No buffer means no block of this piece has been reserved yet, so all of it
171
- // is missing. Reading that as "no tail" hid the case worth seeing most.
172
- let missing = chunks;
173
- if (buffer) {
174
- missing = 0;
175
- for (let index = 0; index < chunks; index += 1) {
176
- if (!buffer[index]) {
177
- missing += 1;
178
- }
179
- }
180
- }
181
-
182
- const wires = Array.isArray(torrent?.wires) ? torrent.wires : [];
183
- const outstanding = [];
184
- for (const wire of wires) {
185
- const requests = Array.isArray(wire?.requests) ? wire.requests : [];
186
- const blocks = requests.filter((request) => request?.piece === pieceIndex).length;
187
- if (blocks > 0) {
188
- outstanding.push({
189
- blocks,
190
- bytesPerSecond: speedOf(wire),
191
- choking: wire?.peerChoking === true
192
- });
193
- }
194
- }
195
- // Slowest first: that is the wire the read is waiting on, and the one a
196
- // duplicate would be aimed past.
197
- outstanding.sort((left, right) => left.bytesPerSecond - right.bytesPerSecond);
198
- return { chunks, missing, outstanding };
199
- }
200
-
201
- /**
202
- * Ask a second wire for blocks the reader is still waiting on.
203
- *
204
- * WebTorrent reserves each block for exactly one wire, so once `Piece.reserve()`
205
- * answers -1 there is no request left to place and the read ends when the
206
- * holder of the last block delivers it — however fast the rest of the swarm is.
207
- * The library's own remedy is `_hotswap`, and it does precisely the right
208
- * thing: `piece.cancel(blockIndex)` frees the reservation while leaving the
209
- * first wire's request in flight, so a second wire can be asked for the same
210
- * block and whichever arrives first wins. What it will not do is apply that to
211
- * these tails. It is gated on speed — the held-up wire must be under 48 KB/s
212
- * (`3 * BLOCK_LENGTH`) and at least twice as slow as the asker — and measured
213
- * on a real swarm 2026-08-19, the tails a reader waits on sit at 109, 317 and
214
- * 870 KB/s. Peers the library rightly considers good, because for bulk
215
- * downloading they are; the gate is about throughput across a torrent and knows
216
- * nothing about a reader blocked on one piece now.
217
- *
218
- * Speed is not even what is wrong with them: two blocks — 32 KB — on a wire
219
- * measured at 109 KB/s is 0.3 s of transfer, and that read waited 4.6 s. The
220
- * blocks are not travelling slowly, they are queued behind that wire's other
221
- * work, which no average speed can show.
222
- *
223
- * The cost is bounded by the same measurement: the tails were 2 to 14 blocks of
224
- * 512, so 32 to 224 KB against the 8 MiB piece they hold up. At most one
225
- * duplicate is asked per candidate wire per call, and the caller repeats every
226
- * half second, so a longer tail is covered across attempts rather than in one
227
- * burst.
228
- *
229
- * Safe against the completion path: a block that arrives twice is dropped by
230
- * `Piece.set`, which writes only into an empty slot, and a piece already
231
- * flushed answers `false` from `Piece.init` so the second arrival returns
232
- * before `flush()` is reached.
233
- *
234
- * @param {object} torrent
235
- * @param {number} pieceIndex
236
- * @returns {{ duplicated: number, missing: number, wires: number }}
237
- * `duplicated` counts requests the library actually placed for a block that
238
- * was already outstanding elsewhere.
239
- */
240
- export function duplicateTailFor(torrent, pieceIndex) {
241
- const piece = torrent?.pieces?.[pieceIndex];
242
- const buffer = Array.isArray(piece?._buffer) ? piece._buffer : null;
243
- if (!piece || !buffer || typeof piece.cancel !== "function") {
244
- return { duplicated: 0, missing: 0, wires: 0 };
245
- }
246
- const chunks = Number.isFinite(piece._chunks) ? piece._chunks : buffer.length;
247
- const missing = [];
248
- for (let index = 0; index < chunks; index += 1) {
249
- if (!buffer[index]) {
250
- missing.push(index);
251
- }
252
- }
253
- const candidates = wiresForPiece(torrent, pieceIndex);
254
- if (missing.length === 0 || candidates.length === 0) {
255
- return { duplicated: 0, missing: missing.length, wires: candidates.length };
256
- }
257
-
258
- let duplicated = 0;
259
- // One block per wire: that is what the pipelines can usefully take at once,
260
- // and it needs no number of its own.
261
- for (let index = 0; index < Math.min(missing.length, candidates.length); index += 1) {
262
- // Freeing the reservation is what makes the block askable again; the
263
- // request already in flight is deliberately left alone, because the point
264
- // is to have two of them.
265
- piece.cancel(missing[index]);
266
- // `false` is hotswap: the library's own swap must not run on top of this,
267
- // or it would free a THIRD wire's block as well.
268
- if (torrent._request(candidates[index], pieceIndex, false) === true) {
269
- duplicated += 1;
270
- continue;
271
- }
272
- // The wire's pipeline is full. The block stays in the piece's cancellation
273
- // stack and will be handed to whoever asks next, which is harmless it is
274
- // already in flight elsewhere — but there is no point asking the remaining
275
- // wires, whose pipelines are no emptier.
276
- break;
277
- }
278
- return { duplicated, missing: missing.length, wires: candidates.length };
279
- }
1
+ /**
2
+ * @file Put the piece a reader is blocked on onto the fastest wires that hold
3
+ * it.
4
+ *
5
+ * Measured 2026-08-17: the swarm delivered 5.1-5.9 MB/s against a film consumed
6
+ * at about 1 MB/s — a fivefold surplus — and the reader still blocked 47 times
7
+ * in two minutes, 1.0-4.5 s each. So the shortage is not bandwidth. The piece
8
+ * that blocked had been requested from a median of five peers, and what set the
9
+ * tail was WHICH wire held the last outstanding block: a block is reserved for
10
+ * exactly one wire, and the read finishes when the slowest holder delivers.
11
+ *
12
+ * What the library already does, and what it does not: `torrent.critical()`
13
+ * (which the reader already sets over its window) enables HOTSWAP — an idle
14
+ * wire may take a block away from the SLOWEST holder. It does not duplicate:
15
+ * `piece.reserve()` returns -1 once every block is spoken for, and the
16
+ * end-game that would ask a second peer for the same block is commented out in
17
+ * `webtorrent/lib/torrent.js`. Hotswap therefore fires only when the library's
18
+ * own picker happens to visit an idle wire while our piece is critical.
19
+ *
20
+ * This asks for it on purpose, and chooses who: the wires that are unchoked,
21
+ * hold the piece and are measurably fastest are handed the piece through the
22
+ * same entry the library's picker uses, with hotswap enabled. Nothing is
23
+ * duplicated and no protocol rule is bent — the block moves to a faster holder
24
+ * instead of staying with whoever got it first.
25
+ */
26
+
27
+ /**
28
+ * The library's own request entry. Internal, so its absence must be noticed
29
+ * rather than swallowed: without it this lever silently does nothing.
30
+ *
31
+ * @param {object} torrent
32
+ * @returns {boolean}
33
+ */
34
+ export function canPlaceRequests(torrent) {
35
+ return typeof torrent?._request === "function";
36
+ }
37
+
38
+ /**
39
+ * Wires that could deliver this piece right now, fastest first.
40
+ *
41
+ * Excluded, each for its own reason: a wire that is choking us cannot be asked
42
+ * at all; one that does not hold the piece has nothing to give; a destroyed one
43
+ * is a corpse. Speed is the library's own measurement over its own window, so
44
+ * nothing here needs a history of its own.
45
+ *
46
+ * @param {object} torrent
47
+ * @param {number} pieceIndex
48
+ * @returns {object[]}
49
+ */
50
+ export function wiresForPiece(torrent, pieceIndex) {
51
+ const wires = Array.isArray(torrent?.wires) ? torrent.wires : [];
52
+ const usable = [];
53
+ for (const wire of wires) {
54
+ if (!wire || wire.destroyed || wire.peerChoking) {
55
+ continue;
56
+ }
57
+ if (wire.peerPieces?.get?.(pieceIndex) !== true) {
58
+ continue;
59
+ }
60
+ usable.push(wire);
61
+ }
62
+ return usable.sort((left, right) => speedOf(right) - speedOf(left));
63
+ }
64
+
65
+ /**
66
+ * A wire's measured download speed in bytes per second, or 0 when it has not
67
+ * been measured. Wrapped because `downloadSpeed` is a method on the wire and a
68
+ * throw from a destroyed one must not take the caller with it.
69
+ *
70
+ * @param {object} wire
71
+ * @returns {number}
72
+ */
73
+ function speedOf(wire) {
74
+ try {
75
+ const speed = wire?.downloadSpeed?.();
76
+ return Number.isFinite(speed) ? speed : 0;
77
+ } catch {
78
+ // A wire that cannot say how fast it is ranks last, which is the honest
79
+ // answer — and the caller's own log line reports how many were asked.
80
+ return 0;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Ask the fastest holders of `pieceIndex` for it.
86
+ *
87
+ * @param {object} torrent
88
+ * @param {number} pieceIndex
89
+ * @param {number} [limit] - How many wires to push it onto.
90
+ * @returns {{ asked: number, refusedWhileReserved: number, attempted: number, considered: number, fastestBytesPerSecond: number }}
91
+ * `asked` counts requests the library actually placed: it refuses when a
92
+ * wire's pipeline is full or when nothing can be reserved even with hotswap,
93
+ * and that refusal is information — a piece nobody can be asked for is
94
+ * waiting on the wire, not on the picker.
95
+ */
96
+ export function askFastestWiresFor(torrent, pieceIndex, limit = 3) {
97
+ if (!canPlaceRequests(torrent) || !Number.isInteger(pieceIndex) || pieceIndex < 0) {
98
+ return { asked: 0, refusedWhileReserved: 0, attempted: 0, considered: 0, fastestBytesPerSecond: 0 };
99
+ }
100
+ const candidates = wiresForPiece(torrent, pieceIndex);
101
+ let asked = 0;
102
+ let refusedWhileReserved = 0;
103
+ for (const wire of candidates.slice(0, Math.max(1, limit))) {
104
+ try {
105
+ // `true` is hotswap: if every block is reserved, take one from the
106
+ // slowest holder. That is the whole point — the reader is blocked
107
+ // precisely because a slow holder has one.
108
+ if (torrent._request(wire, pieceIndex, true) === true) {
109
+ asked += 1;
110
+ } else {
111
+ // Refused. Two reasons and they are different: this wire's pipeline is
112
+ // full, or every block of the piece is reserved and displacement did
113
+ // not happen. The second is the interesting one, because the thresholds
114
+ // that decide it are constants inside the library and not settings — a
115
+ // holder at 50 KB/s is just above the 48 KB/s line and is never
116
+ // displaced, however long the piece has been waited for. This counter
117
+ // is what would justify replacing `_hotswap` on the torrent, and
118
+ // without it that would be a change made on a hunch.
119
+ if (pieceIsFullyReserved(torrent, pieceIndex)) {
120
+ refusedWhileReserved += 1;
121
+ }
122
+ }
123
+ } catch (error) {
124
+ // Internal call: report it once per attempt rather than letting the lever
125
+ // fail in silence.
126
+ throw new Error(`could not place a request for piece ${pieceIndex}: ${error?.message ?? error}`);
127
+ }
128
+ }
129
+ return {
130
+ asked,
131
+ refusedWhileReserved,
132
+ considered: candidates.length,
133
+ // How many of the asks the library placed, against how many it was asked
134
+ // for. The caller sums these over the whole wait, and summing `asked`
135
+ // against a `considered` taken from the LAST attempt is how the field log
136
+ // came to read "steered onto 12 of 6 holders" a ratio of two different
137
+ // things. Both halves are returned per attempt so the caller can add each
138
+ // to its own total.
139
+ attempted: Math.min(candidates.length, Math.max(1, limit)),
140
+ fastestBytesPerSecond: candidates.length > 0 ? speedOf(candidates[0]) : 0
141
+ };
142
+ }
143
+
144
+ /**
145
+ * What is actually holding up a piece the reader is blocked on.
146
+ *
147
+ * The steering above can only move a block that the library will hand over, and
148
+ * the field says it often hands over nothing: `steered onto 0 of 9 asks (8
149
+ * peers held it)`, recorded 2026-08-18 while eight peers had the piece. When
150
+ * every block of a piece is already reserved, `Piece.reserve()` answers -1 and
151
+ * there is no request left to place — the read then ends when the SLOWEST
152
+ * holder delivers its block, however fast the rest of the swarm is.
153
+ *
154
+ * Duplicating those last blocks onto faster wires is the standard remedy, and
155
+ * it is not free: every duplicate is a block's worth of traffic paid twice. So
156
+ * this describes the tail before anything is built with it how many blocks
157
+ * are still missing, and on which wires they sit, with each wire's speed. If
158
+ * the missing blocks turn out to sit on one slow wire, duplication is aimed at
159
+ * exactly the right thing; if they are spread across fast ones, the wait has
160
+ * another cause and this work should not be done at all.
161
+ *
162
+ * Reads only: nothing here changes a reservation or places a request.
163
+ *
164
+ * @param {import("webtorrent").Torrent} torrent
165
+ * @param {number} pieceIndex
166
+ * @returns {{ chunks: number, missing: number,
167
+ * outstanding: Array<{ blocks: number, bytesPerSecond: number, choking: boolean }> } | null}
168
+ * Null only when the piece object is gone — it completed and was cleared.
169
+ * A piece nobody has reserved a block of yet is NOT null: `torrent-piece`
170
+ * creates its buffer lazily on the first reserve, so a piece the picker has
171
+ * not reached reads as every block missing and nothing outstanding, which is
172
+ * the most informative answer this can give — the wait is not on a slow
173
+ * holder, it is on nobody having been asked.
174
+ */
175
+ export function describePieceTail(torrent, pieceIndex) {
176
+ const piece = torrent?.pieces?.[pieceIndex];
177
+ if (!piece) {
178
+ return null;
179
+ }
180
+ const buffer = Array.isArray(piece._buffer) ? piece._buffer : null;
181
+ const chunks = Number.isFinite(piece._chunks)
182
+ ? piece._chunks
183
+ : (buffer ? buffer.length : 0);
184
+ // No buffer means no block of this piece has been reserved yet, so all of it
185
+ // is missing. Reading that as "no tail" hid the case worth seeing most.
186
+ let missing = chunks;
187
+ if (buffer) {
188
+ missing = 0;
189
+ for (let index = 0; index < chunks; index += 1) {
190
+ if (!buffer[index]) {
191
+ missing += 1;
192
+ }
193
+ }
194
+ }
195
+
196
+ const wires = Array.isArray(torrent?.wires) ? torrent.wires : [];
197
+ const outstanding = [];
198
+ for (const wire of wires) {
199
+ const requests = Array.isArray(wire?.requests) ? wire.requests : [];
200
+ const blocks = requests.filter((request) => request?.piece === pieceIndex).length;
201
+ if (blocks > 0) {
202
+ outstanding.push({
203
+ blocks,
204
+ bytesPerSecond: speedOf(wire),
205
+ choking: wire?.peerChoking === true
206
+ });
207
+ }
208
+ }
209
+ // Slowest first: that is the wire the read is waiting on, and the one a
210
+ // duplicate would be aimed past.
211
+ outstanding.sort((left, right) => left.bytesPerSecond - right.bytesPerSecond);
212
+ return { chunks, missing, outstanding };
213
+ }
214
+
215
+ /**
216
+ * Ask a second wire for blocks the reader is still waiting on.
217
+ *
218
+ * WebTorrent reserves each block for exactly one wire, so once `Piece.reserve()`
219
+ * answers -1 there is no request left to place and the read ends when the
220
+ * holder of the last block delivers it however fast the rest of the swarm is.
221
+ * The library's own remedy is `_hotswap`, and it does precisely the right
222
+ * thing: `piece.cancel(blockIndex)` frees the reservation while leaving the
223
+ * first wire's request in flight, so a second wire can be asked for the same
224
+ * block and whichever arrives first wins. What it will not do is apply that to
225
+ * these tails. It is gated on speed the held-up wire must be under 48 KB/s
226
+ * (`3 * BLOCK_LENGTH`) and at least twice as slow as the asker and measured
227
+ * on a real swarm 2026-08-19, the tails a reader waits on sit at 109, 317 and
228
+ * 870 KB/s. Peers the library rightly considers good, because for bulk
229
+ * downloading they are; the gate is about throughput across a torrent and knows
230
+ * nothing about a reader blocked on one piece now.
231
+ *
232
+ * Speed is not even what is wrong with them: two blocks — 32 KB — on a wire
233
+ * measured at 109 KB/s is 0.3 s of transfer, and that read waited 4.6 s. The
234
+ * blocks are not travelling slowly, they are queued behind that wire's other
235
+ * work, which no average speed can show.
236
+ *
237
+ * The cost is bounded by the same measurement: the tails were 2 to 14 blocks of
238
+ * 512, so 32 to 224 KB against the 8 MiB piece they hold up. At most one
239
+ * duplicate is asked per candidate wire per call, and the caller repeats every
240
+ * half second, so a longer tail is covered across attempts rather than in one
241
+ * burst.
242
+ *
243
+ * Safe against the completion path: a block that arrives twice is dropped by
244
+ * `Piece.set`, which writes only into an empty slot, and a piece already
245
+ * flushed answers `false` from `Piece.init` so the second arrival returns
246
+ * before `flush()` is reached.
247
+ *
248
+ * @param {object} torrent
249
+ * @param {number} pieceIndex
250
+ * @returns {{ duplicated: number, missing: number, wires: number }}
251
+ * `duplicated` counts requests the library actually placed for a block that
252
+ * was already outstanding elsewhere.
253
+ */
254
+ export function duplicateTailFor(torrent, pieceIndex) {
255
+ const piece = torrent?.pieces?.[pieceIndex];
256
+ const buffer = Array.isArray(piece?._buffer) ? piece._buffer : null;
257
+ if (!piece || !buffer || typeof piece.cancel !== "function") {
258
+ return { duplicated: 0, missing: 0, wires: 0 };
259
+ }
260
+ const chunks = Number.isFinite(piece._chunks) ? piece._chunks : buffer.length;
261
+ const missing = [];
262
+ for (let index = 0; index < chunks; index += 1) {
263
+ if (!buffer[index]) {
264
+ missing.push(index);
265
+ }
266
+ }
267
+ const candidates = wiresForPiece(torrent, pieceIndex);
268
+ if (missing.length === 0 || candidates.length === 0) {
269
+ return { duplicated: 0, missing: missing.length, wires: candidates.length };
270
+ }
271
+
272
+ let duplicated = 0;
273
+ // One block per wire: that is what the pipelines can usefully take at once,
274
+ // and it needs no number of its own.
275
+ for (let index = 0; index < Math.min(missing.length, candidates.length); index += 1) {
276
+ // Freeing the reservation is what makes the block askable again; the
277
+ // request already in flight is deliberately left alone, because the point
278
+ // is to have two of them.
279
+ piece.cancel(missing[index]);
280
+ // `false` is hotswap: the library's own swap must not run on top of this,
281
+ // or it would free a THIRD wire's block as well.
282
+ if (torrent._request(candidates[index], pieceIndex, false) === true) {
283
+ duplicated += 1;
284
+ continue;
285
+ }
286
+ // The wire's pipeline is full. The block stays in the piece's cancellation
287
+ // stack and will be handed to whoever asks next, which is harmless — it is
288
+ // already in flight elsewhere — but there is no point asking the remaining
289
+ // wires, whose pipelines are no emptier.
290
+ break;
291
+ }
292
+ return { duplicated, missing: missing.length, wires: candidates.length };
293
+ }
294
+
295
+ /**
296
+ * Whether every block of a piece is spoken for by some wire.
297
+ *
298
+ * The condition under which the library refuses a request and only displacement
299
+ * could change the answer. Read from the piece's own reservation state, which
300
+ * is what `_request` consults.
301
+ *
302
+ * @param {object} torrent
303
+ * @param {number} pieceIndex
304
+ * @returns {boolean}
305
+ */
306
+ export function pieceIsFullyReserved(torrent, pieceIndex) {
307
+ const piece = torrent?.pieces?.[pieceIndex];
308
+ if (!piece || typeof piece.reserve !== "function") {
309
+ return false;
310
+ }
311
+ const reservation = piece.reserve();
312
+ if (reservation === -1) {
313
+ return true;
314
+ }
315
+ // Taken only to ask the question; give it straight back, or this reader has
316
+ // quietly claimed a block nobody will ever deliver.
317
+ piece.cancel?.(reservation);
318
+ return false;
319
+ }