@torrent-tv/proxy 2.80.8 → 2.80.9
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/CHANGELOG.md +1666 -1658
- package/bin/cli.js +606 -596
- package/package.json +1 -1
- package/services/encode/CoverageMap.js +428 -401
- package/services/encode/SegmentStore.js +718 -669
- package/services/hls-session-manager.js +7 -19
- package/services/memory-report.js +596 -592
- package/services/orchestrators/EncodeOrchestrator.js +726 -594
- package/test/coverage-follows-the-disk.test.js +187 -0
- package/test/coverage-map.test.js +195 -178
- package/test/encode-plan.test.js +539 -540
- package/test/run-intervals.test.js +100 -100
- package/test/segment-store.test.js +216 -187
|
@@ -1,401 +1,428 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file What has been made of one output, what is being made right now, and by
|
|
3
|
-
* whom.
|
|
4
|
-
*
|
|
5
|
-
* One map per set of output parameters — never per session, never per viewer.
|
|
6
|
-
* Every segment number is in exactly one of three states:
|
|
7
|
-
*
|
|
8
|
-
* 1. **ready** — a file that is closed and can be served to anybody;
|
|
9
|
-
* 2. **being made** — claimed by a named live run, which has been given that
|
|
10
|
-
* stretch and is working forward through it;
|
|
11
|
-
* 3. **free** — nobody has made it and nobody is making it.
|
|
12
|
-
*
|
|
13
|
-
* Two questions are asked of it constantly and both have to be cheap, because
|
|
14
|
-
* one of them is on the path that answers a viewer:
|
|
15
|
-
*
|
|
16
|
-
* 1. is segment N ready — a set lookup;
|
|
17
|
-
* 2. where is the first gap at or after N — a walk over numbers, never over the
|
|
18
|
-
* disk. The walk it replaces listed every run directory of a session on the
|
|
19
|
-
* thread carrying the data channel, 1350 files for a 90-minute film, on
|
|
20
|
-
* every segment request.
|
|
21
|
-
*
|
|
22
|
-
* **
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* @param {number}
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
if (Number.isInteger(
|
|
81
|
-
this.#
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* @param {number} index
|
|
98
|
-
*/
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @param {number}
|
|
175
|
-
* @
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @param {number} index
|
|
202
|
-
* @
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
* @
|
|
303
|
-
*
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file What has been made of one output, what is being made right now, and by
|
|
3
|
+
* whom.
|
|
4
|
+
*
|
|
5
|
+
* One map per set of output parameters — never per session, never per viewer.
|
|
6
|
+
* Every segment number is in exactly one of three states:
|
|
7
|
+
*
|
|
8
|
+
* 1. **ready** — a file that is closed and can be served to anybody;
|
|
9
|
+
* 2. **being made** — claimed by a named live run, which has been given that
|
|
10
|
+
* stretch and is working forward through it;
|
|
11
|
+
* 3. **free** — nobody has made it and nobody is making it.
|
|
12
|
+
*
|
|
13
|
+
* Two questions are asked of it constantly and both have to be cheap, because
|
|
14
|
+
* one of them is on the path that answers a viewer:
|
|
15
|
+
*
|
|
16
|
+
* 1. is segment N ready — a set lookup;
|
|
17
|
+
* 2. where is the first gap at or after N — a walk over numbers, never over the
|
|
18
|
+
* disk. The walk it replaces listed every run directory of a session on the
|
|
19
|
+
* thread carrying the data channel, 1350 files for a 90-minute film, on
|
|
20
|
+
* every segment request.
|
|
21
|
+
*
|
|
22
|
+
* **READINESS IS A PROJECTION, NEVER A MEMORY.** What is ready is a fact of the
|
|
23
|
+
* disk, and the disk has one owner — the segment store. This map does not
|
|
24
|
+
* remember what it was once told: {@link CoverageMap#setReady} REPLACES the
|
|
25
|
+
* whole picture, so every number it calls ready was placed there by that one
|
|
26
|
+
* authority, whole, immediately before the answer was used.
|
|
27
|
+
*
|
|
28
|
+
* It used to accumulate. `markReadyAll` added and nothing ever took away — the
|
|
29
|
+
* word for taking away existed and was called from no line of the product — so
|
|
30
|
+
* a number stayed ready for the life of the process after its file had been
|
|
31
|
+
* discarded, dropped for room, or overwritten by a run restarting on it. Field
|
|
32
|
+
* 2026-09-07: this map said 482 of 482 segments were made while the directory
|
|
33
|
+
* held nothing a header could be lifted out of; the plan therefore scored every
|
|
34
|
+
* arrangement as equally perfect, took the one encoder away as unnecessary — its
|
|
35
|
+
* own words, "the film is no worse off without it" — and placed none for the
|
|
36
|
+
* rest of the session. Two sessions in a row ended with the viewer looking at an
|
|
37
|
+
* error card, and the second one never received a single byte.
|
|
38
|
+
*
|
|
39
|
+
* The remedy is not a way to un-mark. A second owner that is kept in step can
|
|
40
|
+
* fall out of step again; a projection cannot.
|
|
41
|
+
*
|
|
42
|
+
* **Claims are intervals, not heads.** A run says which stretch it was given,
|
|
43
|
+
* so two runs on one output cannot be sent to the same numbers: the gap finder
|
|
44
|
+
* skips what another run will reach. A run that only announced its current
|
|
45
|
+
* position would leave the question "will anybody make #400" unanswerable
|
|
46
|
+
* without guessing at its speed.
|
|
47
|
+
*
|
|
48
|
+
* **Nothing here touches a disk, a process or a clock**, so every decision it
|
|
49
|
+
* makes can be exercised with numbers alone.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/** @typedef {"ready" | "making" | "free"} SegmentState */
|
|
53
|
+
|
|
54
|
+
export class CoverageMap {
|
|
55
|
+
/** Numbers whose file is closed and servable. @type {Set<number>} */
|
|
56
|
+
#ready = new Set();
|
|
57
|
+
|
|
58
|
+
/** Run id → the stretch it was given, both ends inclusive. @type {Map<string, {from: number, to: number}>} */
|
|
59
|
+
#claims = new Map();
|
|
60
|
+
|
|
61
|
+
/** How many segments this output has in total. @type {number} */
|
|
62
|
+
#segmentCount;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {object} [params]
|
|
66
|
+
* @param {number} [params.segmentCount=0] - The length of the output, in
|
|
67
|
+
* segments. Zero means it is not known yet, and then a gap search has to be
|
|
68
|
+
* given its own bound by the caller.
|
|
69
|
+
*/
|
|
70
|
+
constructor({ segmentCount = 0 } = {}) {
|
|
71
|
+
this.#segmentCount = Number.isInteger(segmentCount) && segmentCount > 0 ? segmentCount : 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The length of the output, once the playlist is known.
|
|
76
|
+
*
|
|
77
|
+
* @param {number} count
|
|
78
|
+
*/
|
|
79
|
+
setSegmentCount(count) {
|
|
80
|
+
if (Number.isInteger(count) && count > 0) {
|
|
81
|
+
this.#segmentCount = count;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @returns {number} */
|
|
86
|
+
get segmentCount() {
|
|
87
|
+
return this.#segmentCount;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Record that a segment is closed and can be served.
|
|
92
|
+
*
|
|
93
|
+
* Idempotent, and deliberately independent of who made it: a segment made by
|
|
94
|
+
* a run that has since died is as good as one made by a run still going, and
|
|
95
|
+
* a segment left by a previous life of this process is as good as either.
|
|
96
|
+
*
|
|
97
|
+
* @param {number} index
|
|
98
|
+
*/
|
|
99
|
+
markReady(index) {
|
|
100
|
+
if (Number.isInteger(index) && index >= 0) {
|
|
101
|
+
this.#ready.add(index);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* State the WHOLE picture of what is ready, replacing whatever was here.
|
|
107
|
+
*
|
|
108
|
+
* The only way readiness enters this map in the product, and the reason it
|
|
109
|
+
* cannot drift: a number absent from `indexes` is not ready, whatever this map
|
|
110
|
+
* was told a moment ago. Its file may have been discarded with the run that
|
|
111
|
+
* had it open, dropped to make room, or reopened by a run restarting on it —
|
|
112
|
+
* none of which this map can see, and none of which it now has to.
|
|
113
|
+
*
|
|
114
|
+
* There is deliberately no way to take one number back. A retraction is a
|
|
115
|
+
* second owner keeping a copy in step, and a copy kept in step is what this
|
|
116
|
+
* replaced.
|
|
117
|
+
*
|
|
118
|
+
* @param {Iterable<number>} indexes
|
|
119
|
+
*/
|
|
120
|
+
setReady(indexes) {
|
|
121
|
+
const stated = new Set();
|
|
122
|
+
for (const index of indexes) {
|
|
123
|
+
if (Number.isInteger(index) && index >= 0) {
|
|
124
|
+
stated.add(index);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.#ready = stated;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A run has been given a stretch to fill.
|
|
132
|
+
*
|
|
133
|
+
* Replaces whatever that run claimed before, because a run has one stretch at
|
|
134
|
+
* a time: moved forward past ready material, it states the new one.
|
|
135
|
+
*
|
|
136
|
+
* @param {object} run - The run itself. A run has no name and needs none:
|
|
137
|
+
* what identifies it here is that it IS itself, and what identifies it in a
|
|
138
|
+
* log line is the stretch it was given, which no other live run of this
|
|
139
|
+
* output can hold.
|
|
140
|
+
* @param {number} from - First segment number, inclusive.
|
|
141
|
+
* @param {number} to - Last segment number, inclusive. May be
|
|
142
|
+
* `Number.POSITIVE_INFINITY` for a run with no end yet, which is what every
|
|
143
|
+
* run was before ends existed.
|
|
144
|
+
*/
|
|
145
|
+
claim(run, from, to) {
|
|
146
|
+
if (!run || !Number.isInteger(from) || from < 0) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const end = Number.isFinite(to) ? Math.max(from, Math.trunc(to)) : Number.POSITIVE_INFINITY;
|
|
150
|
+
this.#claims.set(run, { from, to: end });
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A run has ended. Whatever it did not finish goes back to free.
|
|
155
|
+
*
|
|
156
|
+
* Nothing is un-marked: what it DID finish stays ready, because a closed file
|
|
157
|
+
* is closed whoever made it and whatever became of them afterwards.
|
|
158
|
+
*
|
|
159
|
+
* @param {object} run
|
|
160
|
+
*/
|
|
161
|
+
release(run) {
|
|
162
|
+
this.#claims.delete(run);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* How many numbers between two are already made.
|
|
167
|
+
*
|
|
168
|
+
* What an encoder driving from one to the other would produce a SECOND time,
|
|
169
|
+
* and therefore what the swarm would be asked to fetch a second time. It is a
|
|
170
|
+
* term of when that encoder arrives, not a separate question about whether to
|
|
171
|
+
* move it: an encoder that has to re-make three hundred pieces on its way is
|
|
172
|
+
* simply slower to get there, and the model compares arrivals.
|
|
173
|
+
*
|
|
174
|
+
* @param {number} from - Inclusive.
|
|
175
|
+
* @param {number} to - Inclusive.
|
|
176
|
+
* @returns {number}
|
|
177
|
+
*/
|
|
178
|
+
madeBetween(from, to) {
|
|
179
|
+
const first = Number.isInteger(from) && from > 0 ? from : 0;
|
|
180
|
+
const last = Number.isInteger(to) ? to : -1;
|
|
181
|
+
let count = 0;
|
|
182
|
+
for (let index = first; index <= last; index += 1) {
|
|
183
|
+
if (this.isReady(index)) {
|
|
184
|
+
count += 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return count;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @param {number} index
|
|
192
|
+
* @returns {boolean}
|
|
193
|
+
*/
|
|
194
|
+
isReady(index) {
|
|
195
|
+
return this.#ready.has(index);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The run that was given this number, if any.
|
|
200
|
+
*
|
|
201
|
+
* @param {number} index
|
|
202
|
+
* @returns {object | null}
|
|
203
|
+
*/
|
|
204
|
+
makerOf(index) {
|
|
205
|
+
for (const [run, span] of this.#claims) {
|
|
206
|
+
if (index >= span.from && index <= span.to) {
|
|
207
|
+
return run;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {number} index
|
|
215
|
+
* @returns {SegmentState}
|
|
216
|
+
*/
|
|
217
|
+
stateOf(index) {
|
|
218
|
+
if (this.#ready.has(index)) {
|
|
219
|
+
return "ready";
|
|
220
|
+
}
|
|
221
|
+
return this.makerOf(index) === null ? "free" : "making";
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The first number at or after `index` that nobody has made and nobody is
|
|
226
|
+
* making — where a new run belongs.
|
|
227
|
+
*
|
|
228
|
+
* @param {number} index
|
|
229
|
+
* @param {number} [bound] - Search no further than this number, inclusive.
|
|
230
|
+
* Defaults to the last segment of the output; required while the length is
|
|
231
|
+
* unknown.
|
|
232
|
+
* @param {object} [exceptRun] - The run asking. Its own claim does not make
|
|
233
|
+
* a number taken as far as it is concerned: a run looking for where to move
|
|
234
|
+
* would otherwise be blocked by the very stretch it is trying to leave, and
|
|
235
|
+
* a run that had claimed the rest of the film could never move at all.
|
|
236
|
+
* @returns {number | null} Null when there is no gap in range, which is what
|
|
237
|
+
* "everything ahead is already covered" looks like.
|
|
238
|
+
*/
|
|
239
|
+
firstGapFrom(index, bound = undefined, exceptRun = null) {
|
|
240
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
241
|
+
const last = Number.isInteger(bound) ? bound : this.#segmentCount - 1;
|
|
242
|
+
if (!Number.isInteger(last) || last < start) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
for (let at = start; at <= last; at += 1) {
|
|
246
|
+
if (this.#ready.has(at)) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const maker = this.makerOf(at);
|
|
250
|
+
if (maker === null || CoverageMap.#isExcepted(maker, exceptRun)) {
|
|
251
|
+
return at;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* How many numbers from `index` onwards are already covered — ready, or
|
|
259
|
+
* claimed by a run other than `exceptRun`.
|
|
260
|
+
*
|
|
261
|
+
* This is what prices a decision: a run that has arrived at covered material
|
|
262
|
+
* either drives through it, paying its own encode time for every one of these
|
|
263
|
+
* numbers, or is moved to the gap beyond them, paying one restart. Both terms
|
|
264
|
+
* are measured elsewhere; this is the length.
|
|
265
|
+
*
|
|
266
|
+
* @param {number} index
|
|
267
|
+
* @param {object} [exceptRun] - The run asking. Its own claim does not
|
|
268
|
+
* count as somebody else's coverage.
|
|
269
|
+
* @returns {number}
|
|
270
|
+
*/
|
|
271
|
+
coveredRunFrom(index, exceptRun = null) {
|
|
272
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
273
|
+
const last = this.#segmentCount > 0 ? this.#segmentCount - 1 : Number.MAX_SAFE_INTEGER;
|
|
274
|
+
let at = start;
|
|
275
|
+
while (at <= last) {
|
|
276
|
+
if (this.#ready.has(at)) {
|
|
277
|
+
at += 1;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
const maker = this.makerOf(at);
|
|
281
|
+
if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
|
|
282
|
+
at += 1;
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
return at - start;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* How many numbers from `index` onward have NOT been made, claims ignored.
|
|
292
|
+
*
|
|
293
|
+
* The stretch an encoder placed here could work through before it would be
|
|
294
|
+
* re-making something that exists. Claims are deliberately left out: a claim
|
|
295
|
+
* says another encoder MEANS to make it, and where two encoders are being
|
|
296
|
+
* placed in one pass the claims of the moment are about to be re-cut — so
|
|
297
|
+
* asking about them here gives an answer that was true a step ago. Where one
|
|
298
|
+
* encoder's road ends because another begins is decided once, over all the
|
|
299
|
+
* placements together, after they are known.
|
|
300
|
+
*
|
|
301
|
+
* @param {number} index
|
|
302
|
+
* @returns {number} Zero when `index` is already made; the rest of the track
|
|
303
|
+
* when nothing ahead is; `Infinity` when the length is not yet known.
|
|
304
|
+
*/
|
|
305
|
+
unmadeRunFrom(index) {
|
|
306
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
307
|
+
if (this.isReady(start)) {
|
|
308
|
+
return 0;
|
|
309
|
+
}
|
|
310
|
+
if (this.#segmentCount <= 0) {
|
|
311
|
+
return Number.POSITIVE_INFINITY;
|
|
312
|
+
}
|
|
313
|
+
let end = start;
|
|
314
|
+
while (end < this.#segmentCount && !this.isReady(end)) {
|
|
315
|
+
end += 1;
|
|
316
|
+
}
|
|
317
|
+
return end - start;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* How many numbers from `index` onwards are free — nobody has made them and
|
|
322
|
+
* nobody is making them.
|
|
323
|
+
*
|
|
324
|
+
* This is what gives a run its END. A run handed the whole rest of the film
|
|
325
|
+
* would drive straight through the next stretch somebody else is making; a
|
|
326
|
+
* run handed exactly the free stretch stops where the covered material
|
|
327
|
+
* begins, which is also where it would have been moved to anyway.
|
|
328
|
+
*
|
|
329
|
+
* @param {number} index
|
|
330
|
+
* @param {object} [exceptRun] - The run asking, whose own claim does not
|
|
331
|
+
* make a number unfree for it.
|
|
332
|
+
* @returns {number} Zero when `index` itself is not free.
|
|
333
|
+
*/
|
|
334
|
+
freeRunFrom(index, exceptRun = null) {
|
|
335
|
+
const start = Number.isInteger(index) && index > 0 ? index : 0;
|
|
336
|
+
if (this.#segmentCount <= 0) {
|
|
337
|
+
// The length is not known, so how far the free stretch reaches is not
|
|
338
|
+
// known either, and the honest answer is "as far as there is film" — the
|
|
339
|
+
// caller turns that into a run with no end.
|
|
340
|
+
//
|
|
341
|
+
// Never walked one number at a time to find that out. It used to be, up to
|
|
342
|
+
// MAX_SAFE_INTEGER, with a scan of every claim at each step: on the addon
|
|
343
|
+
// host, 2026-09-05, the main thread spun at 100% from the look-ahead timer
|
|
344
|
+
// and the proxy stopped answering anything at all, its own log included.
|
|
345
|
+
// The length was missing because the field naming it had moved and three
|
|
346
|
+
// readers were left on the old name — but a walk whose end depends on a
|
|
347
|
+
// field being present must not be able to do this even then.
|
|
348
|
+
const covered = this.#firstCoveredFrom(start, exceptRun);
|
|
349
|
+
return covered === null ? Number.POSITIVE_INFINITY : covered - start;
|
|
350
|
+
}
|
|
351
|
+
const last = this.#segmentCount - 1;
|
|
352
|
+
let at = start;
|
|
353
|
+
while (at <= last) {
|
|
354
|
+
if (this.#ready.has(at)) {
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
const maker = this.makerOf(at);
|
|
358
|
+
if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
at += 1;
|
|
362
|
+
}
|
|
363
|
+
return at - start;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The first number at or after `index` that somebody has made or is making,
|
|
368
|
+
* or null when nobody has touched anything from there on.
|
|
369
|
+
*
|
|
370
|
+
* Asked of what the map HOLDS rather than by walking the numbers, so it can be
|
|
371
|
+
* answered without a length: the map knows every ready number and every claim,
|
|
372
|
+
* and both are finite however long the film is.
|
|
373
|
+
*
|
|
374
|
+
* @param {number} index
|
|
375
|
+
* @param {object | null} exceptRun
|
|
376
|
+
* @returns {number | null}
|
|
377
|
+
*/
|
|
378
|
+
/**
|
|
379
|
+
* Is this claim one of the runs the caller is setting aside?
|
|
380
|
+
*
|
|
381
|
+
* A single run or a set of them: a pass that re-cuts several roads at once has
|
|
382
|
+
* to ask about all of them together, and asking once per run gave an answer
|
|
383
|
+
* true of no moment.
|
|
384
|
+
*
|
|
385
|
+
* @param {object} maker
|
|
386
|
+
* @param {object | Set<object> | null} except
|
|
387
|
+
* @returns {boolean}
|
|
388
|
+
*/
|
|
389
|
+
static #isExcepted(maker, except) {
|
|
390
|
+
if (except === null || except === undefined) {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
return except instanceof Set ? except.has(maker) : maker === except;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
#firstCoveredFrom(index, exceptRun) {
|
|
397
|
+
let lowest = null;
|
|
398
|
+
for (const ready of this.#ready) {
|
|
399
|
+
if (ready >= index && (lowest === null || ready < lowest)) {
|
|
400
|
+
lowest = ready;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
for (const [run, span] of this.#claims) {
|
|
404
|
+
if (CoverageMap.#isExcepted(run, exceptRun)) {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
// A claim that has already begun covers `index` itself.
|
|
408
|
+
const covers = span.from <= index && index <= span.to ? index : span.from;
|
|
409
|
+
if (covers >= index && (lowest === null || covers < lowest)) {
|
|
410
|
+
lowest = covers;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return lowest;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* What this map holds, for a log line.
|
|
418
|
+
*
|
|
419
|
+
* @returns {{ ready: number, claims: number, segmentCount: number }}
|
|
420
|
+
*/
|
|
421
|
+
stats() {
|
|
422
|
+
return {
|
|
423
|
+
ready: this.#ready.size,
|
|
424
|
+
claims: this.#claims.size,
|
|
425
|
+
segmentCount: this.#segmentCount
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
}
|