@torrent-tv/proxy 2.80.11 → 2.80.13

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,282 +1,267 @@
1
- /**
2
- * @file Which outputs of one file exist right now, and what each of them is.
3
- *
4
- * Every question here is answered by walking the live sessions and looking at
5
- * what they ARE — the same file, a step, a soundtrack, this height — and not by
6
- * following a list of ids anybody keeps. A list of ids is a link between
7
- * sessions: it ties their lifetimes together, it goes stale when one of them is
8
- * disposed, and it has to be cleaned from the other side. What an output is is
9
- * enough to find it, which is the rule `OutputSpec` exists for.
10
- *
11
- * Nothing here writes anything except the two answers a session memoizes about
12
- * itself, and nothing here knows about encoders, viewers, the torrent or the
13
- * disk. It is the layer the quality budget and the serving path both stand on,
14
- * and it is separated first for that reason.
15
- */
16
-
17
- import { variantHeightsFor } from "./ladder.js";
18
-
19
- export class LiveOutputs {
20
- /**
21
- * @param {object} params
22
- * @param {Map<string, object>} params.sessionsById - The live sessions. Read,
23
- * never written.
24
- */
25
- constructor({ sessionsById }) {
26
- this.sessionsById = sessionsById;
27
- }
28
-
29
- /**
30
- * Every live session producing ONE output.
31
- *
32
- * The output is the address the encoding layer works in: two sessions whose
33
- * output parameters agree ARE the same output, so what one of them measured
34
- * about the machine is true of the other.
35
- *
36
- * @param {string} address
37
- * @returns {object[]}
38
- */
39
- sessionsOn(address) {
40
- const found = [];
41
- for (const session of this.sessionsById.values()) {
42
- if (session.outputKey === address && session.state !== "disposed") {
43
- found.push(session);
44
- }
45
- }
46
- return found;
47
- }
48
-
49
- /**
50
- * Every live session of one file: the picture, its quality steps, and the
51
- * soundtracks published separately.
52
- *
53
- * The file is what they share, so the file is what this asks about. Where the
54
- * sums this feeds are concerned that is also the right question: two pictures
55
- * of one file are two encoders on one machine whether or not anybody thinks
56
- * of them as one film.
57
- *
58
- * @param {object} session
59
- * @returns {object[]}
60
- */
61
- familyOf(session) {
62
- const family = [session];
63
- const key = session?.file?.key;
64
- for (const other of this.sessionsById.values()) {
65
- if (other === session || other.state === "disposed" || other.file?.key !== key) {
66
- continue;
67
- }
68
- family.push(other);
69
- }
70
- return family;
71
- }
72
-
73
- /**
74
- * The soundtracks published separately for this picture, live ones only.
75
- *
76
- * @param {object} base
77
- * @returns {object[]}
78
- */
79
- renditionsOf(base) {
80
- return this.familyOf(base).filter((session) => session !== base && session.audioOnly === true);
81
- }
82
-
83
- /**
84
- * The quality steps of this picture, live ones only.
85
- *
86
- * A step is a session made as one — `isStep`, written where it is created —
87
- * and not merely "another session of this file that carries a picture". The
88
- * difference is the second picture: one file can hold two — a browser that
89
- * understands rendition groups and one that needs the sound muxed in produce
90
- * two — and calling one of them a step of the other would let a switch away
91
- * from a step stop the encoder of somebody else's picture.
92
- *
93
- * @param {object} base
94
- * @returns {object[]}
95
- */
96
- stepsOf(base) {
97
- return this.familyOf(base).filter((session) => session !== base && session.isStep === true);
98
- }
99
-
100
- /**
101
- * The picture a step belongs to, or the session itself when it is not a step.
102
- *
103
- * How a session came to be is a fact about it, not a link to another session
104
- * — so a step whose picture has gone answers for itself rather than following
105
- * a dead reference, and nothing has to be cleaned from the other side when
106
- * one of them ends.
107
- *
108
- * One file can carry two pictures at once, and then this returns whichever
109
- * was made first. Everything asked of the answer is a fact of the FILE and of
110
- * this host: what heights can be offered, what a step costs, when the budget
111
- * last acted. Two pictures of one file answer all of those alike.
112
- *
113
- * @param {object} session
114
- * @returns {object}
115
- */
116
- pictureOf(session) {
117
- if (!session || session.isStep !== true) {
118
- return session;
119
- }
120
- for (const other of this.familyOf(session)) {
121
- if (other.isStep !== true && other.audioOnly !== true) {
122
- return other;
123
- }
124
- }
125
- return session;
126
- }
127
-
128
- /**
129
- * Whether this output is the one that person is CONSUMING.
130
- *
131
- * A person holds a record on more outputs than they are watching, and the two
132
- * are different facts. The picture is where their record lives the browser
133
- * addresses it, their chosen soundtrack is written on it, their position is
134
- * read from it — so they never stop being known to it; but the moment they
135
- * step down to 480p, the 1080p output is producing for nobody.
136
- *
137
- * Which is why this exists and why it is here. It is asked when the priority
138
- * map of one output is built, and it decides whether an encoder on that output
139
- * is wanted at all. Without it every output of a film was handed the film's
140
- * whole map: the plan wanted an encoder on each, the session manager killed
141
- * the ones it judged unwatched, and the viewer's own move announced itself and
142
- * started them again the two authorities of 2026-09-08.
143
- *
144
- * The viewer arrives as PLAIN FIELDS. This layer knows the shape of a film a
145
- * picture, its steps, its soundtracks and the viewer layer knows where a
146
- * person stands; neither holds the other.
147
- *
148
- * STATED AS A REFUSAL, and deliberately only where the refusal is certain.
149
- * Being known to an output is watching it everywhere except one case, because
150
- * everywhere else a person who stops watching is let go of: a soundtrack
151
- * nobody chose, a step nobody is on. The exception is the picture itself,
152
- * which they are never let go of — and which therefore had no way at all of
153
- * knowing it was producing for nobody.
154
- *
155
- * So a step, a soundtrack, a step being warmed, a step whose init has just
156
- * been asked for: all watched, as before. The picture: watched unless this
157
- * person is on a step of it.
158
- *
159
- * @param {object} session
160
- * @param {{ activeVariantId?: string | null, warmingVariantId?: string | null }} viewer
161
- * @returns {boolean}
162
- */
163
- watchedBy(session, viewer) {
164
- if (!session || !viewer) {
165
- return false;
166
- }
167
- if (session.isStep === true || session.audioOnly === true) {
168
- return true;
169
- }
170
- // The picture. A step of it on their screen is a statement that they are
171
- // not looking at this; a step merely being made ready for them is not, and
172
- // during a warm-up both are genuinely being produced.
173
- const step = viewer.activeVariantId ?? null;
174
- return step === null || step === session.id;
175
- }
176
-
177
- /**
178
- * Which variant a session IS, as a height. Zero encode height means "keep the
179
- * source", so the source's own height is the answer.
180
- *
181
- * Settled once and then kept, because it is a NAME — the player addresses the
182
- * variant by it for the whole session, having fetched the master exactly
183
- * once. The height a session encodes at is not stable: the realtime budget
184
- * steps it down when the host cannot keep up. Deriving the name afresh each
185
- * time would mean a downshift silently renames the variant the viewer is
186
- * watching, and the next segment request under the old name would build a
187
- * SECOND session at the height the host had just proved it could not manage.
188
- * A downshift changes the picture inside the variant instead, which is what
189
- * it has always done.
190
- *
191
- * @param {object} session
192
- * @returns {number}
193
- */
194
- variantHeightOf(session) {
195
- if (Number.isInteger(session.variantHeight) && session.variantHeight > 0) {
196
- return session.variantHeight;
197
- }
198
- const encodeHeight = Number(session.output.encodeHeight) || 0;
199
- session.variantHeight = encodeHeight > 0
200
- ? encodeHeight
201
- : Math.round(Number(session.file.height) || 0);
202
- return session.variantHeight;
203
- }
204
-
205
- /**
206
- * The height a session's encoder is actually producing, or 0 when it produces
207
- * no encoded picture of its own (a copy, or a soundtrack).
208
- *
209
- * A COPY must never be adopted: it costs no encoder at all, so handing it to
210
- * a request for a re-encoded rung would give away the one thing this host can
211
- * always serve.
212
- *
213
- * @param {object} session
214
- * @returns {number}
215
- */
216
- producedHeightOf(session) {
217
- if (!session || session.transcodeVideo !== true || session.audioOnly === true) {
218
- return 0;
219
- }
220
- return Math.round(Number(session.output.encodeHeight) || 0);
221
- }
222
-
223
- /**
224
- * The heights this file's variants CAN be spliced at — a fact about the
225
- * source and the cut grid, settled once and never moved.
226
- *
227
- * Separate from which of them are worth OFFERING to the viewer right now, on
228
- * a machine whose load moves every five seconds. Both were the same list
229
- * until 2026-08-18, and that is what broke playback outright: the browser is
230
- * told at session creation that a master playlist exists, and 192 ms later —
231
- * after the session's own encoder had started and the first supply reading
232
- * had arrived — the live list had fallen from five rungs to one,
233
- * `buildMasterPlaylist` returned null for having fewer than two, and the
234
- * master answered 404 to the very session that had just published it. hls.js
235
- * treats that as fatal and unrecoverable, so nothing played at all (session
236
- * `4ef731d8`, "Moana (2016).mkv", 17:43:01).
237
- *
238
- * A live figure may decide what to offer. It may not decide whether a
239
- * published document exists.
240
- *
241
- * @param {object} session
242
- * @returns {number[]} Largest first.
243
- */
244
- splicableHeights(session) {
245
- const owner = this.pictureOf(session);
246
- if (Array.isArray(owner.splicableHeights)) {
247
- return owner.splicableHeights;
248
- }
249
- const heights = new Set(variantHeightsFor(Number(owner.file.height) || 0));
250
- const own = this.variantHeightOf(owner);
251
- if (own > 0) {
252
- heights.add(own);
253
- }
254
- owner.splicableHeights = [...heights].sort((left, right) => right - left);
255
- return owner.splicableHeights;
256
- }
257
-
258
- /**
259
- * Whether this stream publishes a master playlist at all that is, whether
260
- * there is anything for a player to move BETWEEN.
261
- *
262
- * Asked in one place because two callers depend on the same answer and used
263
- * to compute it differently: the builder refused a copied stream whose cut
264
- * grid is a fiction, while the budget looked only at how many heights could
265
- * in principle be spliced. A copy with no readable keyframe index therefore
266
- * had requests recorded against it — asking a player with no variants to
267
- * change variant, once every window, for the whole film.
268
- *
269
- * @param {object} session
270
- * @returns {boolean}
271
- */
272
- publishesVariants(session) {
273
- const owner = this.pictureOf(session);
274
- // A copy can only be cut where the source already has a keyframe, so a rung
275
- // meant to splice into it has to be cut at exactly those times. A copy that
276
- // fell back to an even grid ffmpeg does not cut on has nothing to align to.
277
- if (!owner.transcodeVideo && owner.timeline.cutGrid !== "keyframe") {
278
- return false;
279
- }
280
- return this.splicableHeights(owner).length >= 2;
281
- }
282
- }
1
+ /**
2
+ * @file Which outputs of one file exist right now, and what each of them is.
3
+ *
4
+ * Every question here is answered by walking the live sessions and looking at
5
+ * what they ARE — the same file, a step, a soundtrack, this height — and not by
6
+ * following a list of ids anybody keeps. A list of ids is a link between
7
+ * sessions: it ties their lifetimes together, it goes stale when one of them is
8
+ * disposed, and it has to be cleaned from the other side. What an output is is
9
+ * enough to find it, which is the rule `OutputSpec` exists for.
10
+ *
11
+ * Nothing here writes anything except the two answers a session memoizes about
12
+ * itself, and nothing here knows about encoders, viewers, the torrent or the
13
+ * disk. It is the layer the quality budget and the serving path both stand on,
14
+ * and it is separated first for that reason.
15
+ */
16
+
17
+ import { variantHeightsFor } from "./ladder.js";
18
+
19
+ export class LiveOutputs {
20
+ /**
21
+ * @param {object} params
22
+ * @param {Map<string, object>} params.sessionsById - The live sessions. Read,
23
+ * never written.
24
+ */
25
+ constructor({ sessionsById }) {
26
+ this.sessionsById = sessionsById;
27
+ }
28
+
29
+ /**
30
+ * Every live session producing ONE output.
31
+ *
32
+ * The output is the address the encoding layer works in: two sessions whose
33
+ * output parameters agree ARE the same output, so what one of them measured
34
+ * about the machine is true of the other.
35
+ *
36
+ * @param {string} address
37
+ * @returns {object[]}
38
+ */
39
+ sessionsOn(address) {
40
+ const found = [];
41
+ for (const session of this.sessionsById.values()) {
42
+ if (session.outputKey === address && session.state !== "disposed") {
43
+ found.push(session);
44
+ }
45
+ }
46
+ return found;
47
+ }
48
+
49
+ /**
50
+ * Every live session of one file: the picture, its quality steps, and the
51
+ * soundtracks published separately.
52
+ *
53
+ * The file is what they share, so the file is what this asks about. Where the
54
+ * sums this feeds are concerned that is also the right question: two pictures
55
+ * of one file are two encoders on one machine whether or not anybody thinks
56
+ * of them as one film.
57
+ *
58
+ * @param {object} session
59
+ * @returns {object[]}
60
+ */
61
+ familyOf(session) {
62
+ const family = [session];
63
+ const key = session?.file?.key;
64
+ for (const other of this.sessionsById.values()) {
65
+ if (other === session || other.state === "disposed" || other.file?.key !== key) {
66
+ continue;
67
+ }
68
+ family.push(other);
69
+ }
70
+ return family;
71
+ }
72
+
73
+ /**
74
+ * The soundtracks published separately for this picture, live ones only.
75
+ *
76
+ * @param {object} base
77
+ * @returns {object[]}
78
+ */
79
+ renditionsOf(base) {
80
+ return this.familyOf(base).filter((session) => session !== base && session.audioOnly === true);
81
+ }
82
+
83
+ /**
84
+ * The quality steps of this picture, live ones only.
85
+ *
86
+ * A step is a session made as one — `isStep`, written where it is created —
87
+ * and not merely "another session of this file that carries a picture". The
88
+ * difference is the second picture: one file can hold two — a browser that
89
+ * understands rendition groups and one that needs the sound muxed in produce
90
+ * two — and calling one of them a step of the other would let a switch away
91
+ * from a step stop the encoder of somebody else's picture.
92
+ *
93
+ * @param {object} base
94
+ * @returns {object[]}
95
+ */
96
+ stepsOf(base) {
97
+ return this.familyOf(base).filter((session) => session !== base && session.isStep === true);
98
+ }
99
+
100
+ /**
101
+ * The picture a step belongs to, or the session itself when it is not a step.
102
+ *
103
+ * How a session came to be is a fact about it, not a link to another session
104
+ * — so a step whose picture has gone answers for itself rather than following
105
+ * a dead reference, and nothing has to be cleaned from the other side when
106
+ * one of them ends.
107
+ *
108
+ * One file can carry two pictures at once, and then this returns whichever
109
+ * was made first. Everything asked of the answer is a fact of the FILE and of
110
+ * this host: what heights can be offered, what a step costs, when the budget
111
+ * last acted. Two pictures of one file answer all of those alike.
112
+ *
113
+ * @param {object} session
114
+ * @returns {object}
115
+ */
116
+ pictureOf(session) {
117
+ if (!session || session.isStep !== true) {
118
+ return session;
119
+ }
120
+ for (const other of this.familyOf(session)) {
121
+ if (other.isStep !== true && other.audioOnly !== true) {
122
+ return other;
123
+ }
124
+ }
125
+ return session;
126
+ }
127
+
128
+ /**
129
+ * Whether this output has been SUPERSEDED by the step named here.
130
+ *
131
+ * A question about the shape of a film and nothing else: one picture, its
132
+ * steps, its soundtracks. A step supersedes only the picture it is a step of
133
+ * never another step, never a soundtrack, which are produced or not
134
+ * according to whether anybody asked for them.
135
+ *
136
+ * The step arrives as a plain id. Which step is on somebody's screen is a fact
137
+ * about a PERSON, and this layer neither holds a person nor knows what one
138
+ * looks like; whoever asks holds both and states the id.
139
+ *
140
+ * It is asked while the priority map of one output is built, and it is what
141
+ * makes the difference between a picture somebody is watching and a picture
142
+ * they stepped off. Without it every output of a film was handed the film's
143
+ * whole map, the plan wanted an encoder on each, the session manager killed
144
+ * the ones it judged unwatched, and the viewer's own move started them again
145
+ * — the two authorities of 2026-09-08.
146
+ *
147
+ * @param {object} session
148
+ * @param {string | null} [stepOnScreen] - The step being watched, or null for
149
+ * "the height the film was opened at", which supersedes nothing.
150
+ * @returns {boolean}
151
+ */
152
+ supersededBy(session, stepOnScreen = null) {
153
+ if (!session || session.isStep === true || session.audioOnly === true) {
154
+ return false;
155
+ }
156
+ // The picture. A step of it on screen says outright that this is not what
157
+ // is being looked at; a step merely being made READY supersedes nothing,
158
+ // because through a warm-up both are genuinely being produced.
159
+ return typeof stepOnScreen === "string" && stepOnScreen !== session.id;
160
+ }
161
+
162
+ /**
163
+ * Which variant a session IS, as a height. Zero encode height means "keep the
164
+ * source", so the source's own height is the answer.
165
+ *
166
+ * Settled once and then kept, because it is a NAME — the player addresses the
167
+ * variant by it for the whole session, having fetched the master exactly
168
+ * once. The height a session encodes at is not stable: the realtime budget
169
+ * steps it down when the host cannot keep up. Deriving the name afresh each
170
+ * time would mean a downshift silently renames the variant the viewer is
171
+ * watching, and the next segment request under the old name would build a
172
+ * SECOND session at the height the host had just proved it could not manage.
173
+ * A downshift changes the picture inside the variant instead, which is what
174
+ * it has always done.
175
+ *
176
+ * @param {object} session
177
+ * @returns {number}
178
+ */
179
+ variantHeightOf(session) {
180
+ if (Number.isInteger(session.variantHeight) && session.variantHeight > 0) {
181
+ return session.variantHeight;
182
+ }
183
+ const encodeHeight = Number(session.output.encodeHeight) || 0;
184
+ session.variantHeight = encodeHeight > 0
185
+ ? encodeHeight
186
+ : Math.round(Number(session.file.height) || 0);
187
+ return session.variantHeight;
188
+ }
189
+
190
+ /**
191
+ * The height a session's encoder is actually producing, or 0 when it produces
192
+ * no encoded picture of its own (a copy, or a soundtrack).
193
+ *
194
+ * A COPY must never be adopted: it costs no encoder at all, so handing it to
195
+ * a request for a re-encoded rung would give away the one thing this host can
196
+ * always serve.
197
+ *
198
+ * @param {object} session
199
+ * @returns {number}
200
+ */
201
+ producedHeightOf(session) {
202
+ if (!session || session.transcodeVideo !== true || session.audioOnly === true) {
203
+ return 0;
204
+ }
205
+ return Math.round(Number(session.output.encodeHeight) || 0);
206
+ }
207
+
208
+ /**
209
+ * The heights this file's variants CAN be spliced at a fact about the
210
+ * source and the cut grid, settled once and never moved.
211
+ *
212
+ * Separate from which of them are worth OFFERING to the viewer right now, on
213
+ * a machine whose load moves every five seconds. Both were the same list
214
+ * until 2026-08-18, and that is what broke playback outright: the browser is
215
+ * told at session creation that a master playlist exists, and 192 ms later —
216
+ * after the session's own encoder had started and the first supply reading
217
+ * had arrived the live list had fallen from five rungs to one,
218
+ * `buildMasterPlaylist` returned null for having fewer than two, and the
219
+ * master answered 404 to the very session that had just published it. hls.js
220
+ * treats that as fatal and unrecoverable, so nothing played at all (session
221
+ * `4ef731d8`, "Moana (2016).mkv", 17:43:01).
222
+ *
223
+ * A live figure may decide what to offer. It may not decide whether a
224
+ * published document exists.
225
+ *
226
+ * @param {object} session
227
+ * @returns {number[]} Largest first.
228
+ */
229
+ splicableHeights(session) {
230
+ const owner = this.pictureOf(session);
231
+ if (Array.isArray(owner.splicableHeights)) {
232
+ return owner.splicableHeights;
233
+ }
234
+ const heights = new Set(variantHeightsFor(Number(owner.file.height) || 0));
235
+ const own = this.variantHeightOf(owner);
236
+ if (own > 0) {
237
+ heights.add(own);
238
+ }
239
+ owner.splicableHeights = [...heights].sort((left, right) => right - left);
240
+ return owner.splicableHeights;
241
+ }
242
+
243
+ /**
244
+ * Whether this stream publishes a master playlist at all — that is, whether
245
+ * there is anything for a player to move BETWEEN.
246
+ *
247
+ * Asked in one place because two callers depend on the same answer and used
248
+ * to compute it differently: the builder refused a copied stream whose cut
249
+ * grid is a fiction, while the budget looked only at how many heights could
250
+ * in principle be spliced. A copy with no readable keyframe index therefore
251
+ * had requests recorded against it — asking a player with no variants to
252
+ * change variant, once every window, for the whole film.
253
+ *
254
+ * @param {object} session
255
+ * @returns {boolean}
256
+ */
257
+ publishesVariants(session) {
258
+ const owner = this.pictureOf(session);
259
+ // A copy can only be cut where the source already has a keyframe, so a rung
260
+ // meant to splice into it has to be cut at exactly those times. A copy that
261
+ // fell back to an even grid ffmpeg does not cut on has nothing to align to.
262
+ if (!owner.transcodeVideo && owner.timeline.cutGrid !== "keyframe") {
263
+ return false;
264
+ }
265
+ return this.splicableHeights(owner).length >= 2;
266
+ }
267
+ }