@torrent-tv/proxy 2.22.0 → 2.24.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/CHANGELOG.md +20 -0
- package/docs/encode-run-state.md +96 -0
- package/package.json +2 -1
- package/scripts/render-run-graph.js +173 -0
- package/services/encode-exit.js +80 -0
- package/services/encode-run-state.js +488 -0
- package/services/hls-session-manager.js +627 -85
- package/test/concurrent-cost.test.js +138 -82
- package/test/encode-exit.test.js +93 -0
- package/test/encode-run-state.test.js +314 -0
- package/test/quality-variants.test.js +67 -1
- package/test/run-graph-drift.test.js +37 -0
- package/test/segment-serve-wiring.test.js +46 -0
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The encoder run as a transition table: states, the relation between
|
|
3
|
+
* them, and the answers derived from a state. Pure — no ffmpeg, no torrent, no
|
|
4
|
+
* filesystem, no clock — so every decision can be exercised by `node --test`.
|
|
5
|
+
*
|
|
6
|
+
* This table is the SPECIFICATION, not a description of code written elsewhere.
|
|
7
|
+
* The session manager executes it, the picture in `docs/encode-run-state.md` is
|
|
8
|
+
* rendered from it, and every transition a real run makes is logged as state,
|
|
9
|
+
* event and target — so a run that takes an edge this table does not declare is
|
|
10
|
+
* a violation the log names, and a state never entered is a gap in coverage.
|
|
11
|
+
*
|
|
12
|
+
* Why a table at all, for this and not for everything: five field failures in a
|
|
13
|
+
* row were not wrong lines but EMPTY CELLS — a pair of state and event nobody
|
|
14
|
+
* had considered. A run at #26 meeting a request for #0; a request behind the
|
|
15
|
+
* head arriving while a seek settles; a suspended encoder's cumulative speed
|
|
16
|
+
* read as a measurement; a session in "failed" answering 500 for ever because
|
|
17
|
+
* no edge leads out of it; an audio track left with its encoder when the viewer
|
|
18
|
+
* moved to another. A table makes an empty cell visible before a release; a
|
|
19
|
+
* conditional buried in 6900 lines does not.
|
|
20
|
+
*
|
|
21
|
+
* The four principles are the project's, and they decide everything below —
|
|
22
|
+
* they are argued at length in `server/public/domain/app-state.js`, whose shape
|
|
23
|
+
* this follows deliberately:
|
|
24
|
+
*
|
|
25
|
+
* 1. **Moore.** An output depends on the state alone, never on the edge taken.
|
|
26
|
+
* So "is the input being read", "can the process be signalled" and "what
|
|
27
|
+
* does a missing segment get answered" are functions of the state, computed
|
|
28
|
+
* by the caller, never commanded alongside a transition.
|
|
29
|
+
* 2. **Extended state.** Something becomes a state only when it changes what is
|
|
30
|
+
* legal or what is shown. `encoderPauseUnsupported`, the seek-failure
|
|
31
|
+
* counter, the wait epoch and the rest stay variables with guards.
|
|
32
|
+
* 3. **Hierarchy.** An edge shared by several states is declared once on their
|
|
33
|
+
* superstate; {@link nextState} walks the containment chain.
|
|
34
|
+
* 4. **Graph discipline.** Deterministic, total (an unlisted pair is IGNORED,
|
|
35
|
+
* never an exception), every state reachable, no dead ends. The ABSENT edges
|
|
36
|
+
* carry the meaning — see {@link ABSENT_EDGE_INVARIANTS}.
|
|
37
|
+
*
|
|
38
|
+
* Derivation from the code as it stood at proxy 2.22.0, with line numbers:
|
|
39
|
+
* `research/encode-run-machine-2026-08-16.md`.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Control states of ONE encoder run inside one session.
|
|
44
|
+
*
|
|
45
|
+
* The session's own lifetime (live vs disposed) is deliberately not here: it is
|
|
46
|
+
* two states and one edge, and the value is in taking it out of the field this
|
|
47
|
+
* machine replaces, not in tabulating it.
|
|
48
|
+
*
|
|
49
|
+
* @readonly
|
|
50
|
+
*/
|
|
51
|
+
export const ENCODE_RUN_STATE = Object.freeze({
|
|
52
|
+
/**
|
|
53
|
+
* No process exists and one is expected: before the first run, and between a
|
|
54
|
+
* retry timer firing and the spawn it leads to.
|
|
55
|
+
*
|
|
56
|
+
* Distinct from {@link ENCODE_RUN_STATE.STOPPED}, which looks identical in
|
|
57
|
+
* the old field set (`session.ffmpeg === null`) and calls for the opposite
|
|
58
|
+
* answer — that is the whole reason both exist.
|
|
59
|
+
*/
|
|
60
|
+
IDLE: "IDLE",
|
|
61
|
+
/** Spawned; nothing servable produced by THIS run yet. */
|
|
62
|
+
STARTING: "STARTING",
|
|
63
|
+
/** This run has produced at least one servable segment. */
|
|
64
|
+
PRODUCING: "PRODUCING",
|
|
65
|
+
/**
|
|
66
|
+
* `SIGSTOP` delivered, the process alive. The one state where a live run
|
|
67
|
+
* reads nothing: suspending the encoder stops the only thing pulling on the
|
|
68
|
+
* torrent, which is how an eleven-minute download stall came about (2.9.105).
|
|
69
|
+
*/
|
|
70
|
+
SUSPENDED: "SUSPENDED",
|
|
71
|
+
/**
|
|
72
|
+
* The input went away and a restart is timed. Not a failure: the data can
|
|
73
|
+
* come back, so requests are held rather than refused (2.9.112).
|
|
74
|
+
*/
|
|
75
|
+
RETRY_WAIT: "RETRY_WAIT",
|
|
76
|
+
/**
|
|
77
|
+
* Stopped on purpose with no replacement — a rung the viewer switched away
|
|
78
|
+
* from. Its segments stay servable and a switch back restarts it, but nothing
|
|
79
|
+
* a REQUEST does may revive it, or the host ends up running the encoder the
|
|
80
|
+
* viewer left as well as the one they chose.
|
|
81
|
+
*/
|
|
82
|
+
STOPPED: "STOPPED",
|
|
83
|
+
/** Ran through the last segment of the file. Nothing is owed. */
|
|
84
|
+
ENDED_COMPLETE: "ENDED_COMPLETE",
|
|
85
|
+
/** Terminal for this target: requests are answered as failures. */
|
|
86
|
+
ENDED_FAILED: "ENDED_FAILED"
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/** Where a run begins, so no caller hardcodes it. */
|
|
90
|
+
export const INITIAL_RUN_STATE = ENCODE_RUN_STATE.IDLE;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One line per state, for the rendered picture.
|
|
94
|
+
*
|
|
95
|
+
* Data rather than a comment because the drawing is GENERATED from this file:
|
|
96
|
+
* a legend kept beside the diagram drifts from the table, and a drawing that
|
|
97
|
+
* disagrees with the code is worse than none.
|
|
98
|
+
*
|
|
99
|
+
* @type {Readonly<Record<string, string>>}
|
|
100
|
+
*/
|
|
101
|
+
export const STATE_MEANING = Object.freeze({
|
|
102
|
+
[ENCODE_RUN_STATE.IDLE]: "No process, and one is expected — before the first run, or after a retry timer fired.",
|
|
103
|
+
[ENCODE_RUN_STATE.STARTING]: "Spawned; this run has produced nothing servable yet.",
|
|
104
|
+
[ENCODE_RUN_STATE.PRODUCING]: "This run has produced at least one servable segment.",
|
|
105
|
+
[ENCODE_RUN_STATE.SUSPENDED]: "SIGSTOP delivered, process alive — and nothing is reading the input.",
|
|
106
|
+
[ENCODE_RUN_STATE.RETRY_WAIT]: "The input went away; a restart is timed. Requests are held, not refused.",
|
|
107
|
+
[ENCODE_RUN_STATE.STOPPED]: "Stopped on purpose with no replacement — a rung the viewer switched away from.",
|
|
108
|
+
[ENCODE_RUN_STATE.ENDED_COMPLETE]: "Ran through the last segment of the file. Nothing is owed.",
|
|
109
|
+
[ENCODE_RUN_STATE.ENDED_FAILED]: "Terminal for this target: requests are answered as failures."
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Superstates. Containers, not states: they declare a shared edge once and
|
|
114
|
+
* express an output over a group.
|
|
115
|
+
*
|
|
116
|
+
* @readonly
|
|
117
|
+
*/
|
|
118
|
+
export const ENCODE_RUN_SUPERSTATE = Object.freeze({
|
|
119
|
+
/** A pid exists and can be signalled: STARTING, PRODUCING, SUSPENDED. */
|
|
120
|
+
ALIVE: "ALIVE",
|
|
121
|
+
/** A missing segment is held rather than refused: ALIVE plus RETRY_WAIT. */
|
|
122
|
+
WORKING: "WORKING",
|
|
123
|
+
/** Every state. Exists so a universal edge is written once. */
|
|
124
|
+
RUN: "RUN"
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Freeze an object and everything under it. `Object.freeze` is shallow, and a
|
|
129
|
+
* transition relation that can be edited at runtime is not a specification.
|
|
130
|
+
*
|
|
131
|
+
* @template T
|
|
132
|
+
* @param {T} value
|
|
133
|
+
* @returns {T}
|
|
134
|
+
*/
|
|
135
|
+
function deepFreeze(value) {
|
|
136
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
137
|
+
Object.freeze(value);
|
|
138
|
+
for (const inner of Object.values(value)) {
|
|
139
|
+
deepFreeze(inner);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Containment, innermost first — the chain {@link nextState} walks when a state
|
|
147
|
+
* declares no edge of its own for an event.
|
|
148
|
+
*
|
|
149
|
+
* @type {Readonly<Record<string, string | null>>}
|
|
150
|
+
*/
|
|
151
|
+
const PARENT = Object.freeze({
|
|
152
|
+
[ENCODE_RUN_STATE.STARTING]: ENCODE_RUN_SUPERSTATE.ALIVE,
|
|
153
|
+
[ENCODE_RUN_STATE.PRODUCING]: ENCODE_RUN_SUPERSTATE.ALIVE,
|
|
154
|
+
[ENCODE_RUN_STATE.SUSPENDED]: ENCODE_RUN_SUPERSTATE.ALIVE,
|
|
155
|
+
[ENCODE_RUN_SUPERSTATE.ALIVE]: ENCODE_RUN_SUPERSTATE.WORKING,
|
|
156
|
+
[ENCODE_RUN_STATE.RETRY_WAIT]: ENCODE_RUN_SUPERSTATE.WORKING,
|
|
157
|
+
[ENCODE_RUN_SUPERSTATE.WORKING]: ENCODE_RUN_SUPERSTATE.RUN,
|
|
158
|
+
[ENCODE_RUN_STATE.IDLE]: ENCODE_RUN_SUPERSTATE.RUN,
|
|
159
|
+
[ENCODE_RUN_STATE.STOPPED]: ENCODE_RUN_SUPERSTATE.RUN,
|
|
160
|
+
[ENCODE_RUN_STATE.ENDED_COMPLETE]: ENCODE_RUN_SUPERSTATE.RUN,
|
|
161
|
+
[ENCODE_RUN_STATE.ENDED_FAILED]: ENCODE_RUN_SUPERSTATE.RUN,
|
|
162
|
+
[ENCODE_RUN_SUPERSTATE.RUN]: null
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Events, named for what HAPPENED. An event that names its target cannot be
|
|
167
|
+
* refused without contradicting itself.
|
|
168
|
+
*
|
|
169
|
+
* `EXITED_COMPLETE` and `EXITED_SHORT` are two events for one exit code
|
|
170
|
+
* because ffmpeg exits 0 both at the end of the file and when its input simply
|
|
171
|
+
* stops delivering — a distinction that cost a frozen player and had to be
|
|
172
|
+
* recovered by comparing what was produced against the published playlist
|
|
173
|
+
* (2.9.104). Written down, it can never collapse back into one.
|
|
174
|
+
*
|
|
175
|
+
* @readonly
|
|
176
|
+
*/
|
|
177
|
+
export const ENCODE_RUN_EVENT = Object.freeze({
|
|
178
|
+
/** A process was spawned for this session. */
|
|
179
|
+
SPAWNED: "SPAWNED",
|
|
180
|
+
/**
|
|
181
|
+
* The first servable segment of THIS run was served.
|
|
182
|
+
*
|
|
183
|
+
* The only event whose raising is guarded by the current state, and
|
|
184
|
+
* deliberately so: "something has been produced" is a level, not an edge, and
|
|
185
|
+
* the guard is a READ of the state rather than a second copy of it. Declared
|
|
186
|
+
* on STARTING alone, so a segment served by a suspended run cannot resume it
|
|
187
|
+
* — which is exactly the sawtooth of 2.9.93.
|
|
188
|
+
*/
|
|
189
|
+
FIRST_SEGMENT: "FIRST_SEGMENT",
|
|
190
|
+
/** `SIGSTOP` was delivered. */
|
|
191
|
+
SUSPEND_ORDERED: "SUSPEND_ORDERED",
|
|
192
|
+
/** `SIGCONT` was sent. */
|
|
193
|
+
RESUME_ORDERED: "RESUME_ORDERED",
|
|
194
|
+
/** The run was stopped with no replacement. */
|
|
195
|
+
STOP_ORDERED: "STOP_ORDERED",
|
|
196
|
+
/** Exit 0, having produced through the last segment. */
|
|
197
|
+
EXITED_COMPLETE: "EXITED_COMPLETE",
|
|
198
|
+
/** Exit 0, short of the last segment — the input dried up. */
|
|
199
|
+
EXITED_SHORT: "EXITED_SHORT",
|
|
200
|
+
/** Died because the input was not there. Recoverable. */
|
|
201
|
+
EXITED_INPUT_LOST: "EXITED_INPUT_LOST",
|
|
202
|
+
/** Died for any other reason, or could not be spawned at all. */
|
|
203
|
+
EXITED_FAILED: "EXITED_FAILED",
|
|
204
|
+
/** The input-retry timer fired. */
|
|
205
|
+
RETRY_DUE: "RETRY_DUE"
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* One line per event, for the rendered picture. Same reason as
|
|
210
|
+
* {@link STATE_MEANING}.
|
|
211
|
+
*
|
|
212
|
+
* @type {Readonly<Record<string, string>>}
|
|
213
|
+
*/
|
|
214
|
+
export const EVENT_MEANING = Object.freeze({
|
|
215
|
+
[ENCODE_RUN_EVENT.SPAWNED]: "A process was spawned for this session.",
|
|
216
|
+
[ENCODE_RUN_EVENT.FIRST_SEGMENT]: "The first servable segment of this run was served.",
|
|
217
|
+
[ENCODE_RUN_EVENT.SUSPEND_ORDERED]: "SIGSTOP was delivered.",
|
|
218
|
+
[ENCODE_RUN_EVENT.RESUME_ORDERED]: "SIGCONT was sent.",
|
|
219
|
+
[ENCODE_RUN_EVENT.STOP_ORDERED]: "The run was stopped with no replacement.",
|
|
220
|
+
[ENCODE_RUN_EVENT.EXITED_COMPLETE]: "Exit 0, having produced through the last segment.",
|
|
221
|
+
[ENCODE_RUN_EVENT.EXITED_SHORT]: "Exit 0, short of the last segment — the input dried up.",
|
|
222
|
+
[ENCODE_RUN_EVENT.EXITED_INPUT_LOST]: "Died because the input was not there. Recoverable.",
|
|
223
|
+
[ENCODE_RUN_EVENT.EXITED_FAILED]: "Died for any other reason, or could not be spawned.",
|
|
224
|
+
[ENCODE_RUN_EVENT.RETRY_DUE]: "The input-retry timer fired."
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The transition relation. One target per state and event.
|
|
229
|
+
*
|
|
230
|
+
* `SPAWNED` sits on RUN because a run may be spawned from any state at all: at
|
|
231
|
+
* session creation (IDLE), replacing a live run on a seek (ALIVE), after a
|
|
232
|
+
* retry (IDLE), on a switch back to a rung (STOPPED), and after a failure
|
|
233
|
+
* (ENDED_*). Declared once, it also makes the restart edge impossible to
|
|
234
|
+
* forget on a state added later.
|
|
235
|
+
*
|
|
236
|
+
* @type {Readonly<Record<string, Readonly<Record<string, string>>>>}
|
|
237
|
+
*/
|
|
238
|
+
const TRANSITIONS = deepFreeze({
|
|
239
|
+
[ENCODE_RUN_SUPERSTATE.RUN]: {
|
|
240
|
+
// A spawn always lands in STARTING, including from STARTING itself: the new
|
|
241
|
+
// run has produced nothing, whatever its predecessor had done.
|
|
242
|
+
[ENCODE_RUN_EVENT.SPAWNED]: ENCODE_RUN_STATE.STARTING
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
[ENCODE_RUN_SUPERSTATE.WORKING]: {
|
|
246
|
+
// Stopping reaches a dead run too: the exit handler leaves the handle in
|
|
247
|
+
// place, so `#stopEncodeRun` runs its course in RETRY_WAIT as well.
|
|
248
|
+
[ENCODE_RUN_EVENT.STOP_ORDERED]: ENCODE_RUN_STATE.STOPPED
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
[ENCODE_RUN_SUPERSTATE.ALIVE]: {
|
|
252
|
+
[ENCODE_RUN_EVENT.SUSPEND_ORDERED]: ENCODE_RUN_STATE.SUSPENDED,
|
|
253
|
+
[ENCODE_RUN_EVENT.EXITED_COMPLETE]: ENCODE_RUN_STATE.ENDED_COMPLETE,
|
|
254
|
+
// Stopping short is a FAILURE that may be restarted, not a finished file.
|
|
255
|
+
[ENCODE_RUN_EVENT.EXITED_SHORT]: ENCODE_RUN_STATE.ENDED_FAILED,
|
|
256
|
+
[ENCODE_RUN_EVENT.EXITED_INPUT_LOST]: ENCODE_RUN_STATE.RETRY_WAIT,
|
|
257
|
+
[ENCODE_RUN_EVENT.EXITED_FAILED]: ENCODE_RUN_STATE.ENDED_FAILED
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
[ENCODE_RUN_STATE.STARTING]: {
|
|
261
|
+
[ENCODE_RUN_EVENT.FIRST_SEGMENT]: ENCODE_RUN_STATE.PRODUCING
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
[ENCODE_RUN_STATE.SUSPENDED]: {
|
|
265
|
+
// Resuming lands in PRODUCING even for a run suspended before its first
|
|
266
|
+
// segment (2.9.117 suspended one 136 ms in). No output distinguishes the
|
|
267
|
+
// two: both read the input, both can be signalled, both hold a missing
|
|
268
|
+
// segment and both say "running" on the wire. The distinction that does
|
|
269
|
+
// exist — the warm-up figure — is answered before the first spawn.
|
|
270
|
+
[ENCODE_RUN_EVENT.RESUME_ORDERED]: ENCODE_RUN_STATE.PRODUCING
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
[ENCODE_RUN_STATE.RETRY_WAIT]: {
|
|
274
|
+
// The timer fired; nothing is running and nothing is armed. The spawn it
|
|
275
|
+
// leads to may still be abandoned (a newer run won the race), and IDLE is
|
|
276
|
+
// the honest state for that moment.
|
|
277
|
+
[ENCODE_RUN_EVENT.RETRY_DUE]: ENCODE_RUN_STATE.IDLE
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* What the MISSING edges assert, as data so the tests execute them rather than
|
|
283
|
+
* a reader trusting prose. Each entry is a shipped defect stated as a rule.
|
|
284
|
+
*
|
|
285
|
+
* @type {ReadonlyArray<{ from: string, event: string, mustNotReach: string, because: string }>}
|
|
286
|
+
*/
|
|
287
|
+
export const ABSENT_EDGE_INVARIANTS = Object.freeze([
|
|
288
|
+
{
|
|
289
|
+
from: ENCODE_RUN_STATE.SUSPENDED,
|
|
290
|
+
event: ENCODE_RUN_EVENT.FIRST_SEGMENT,
|
|
291
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
292
|
+
because:
|
|
293
|
+
"a segment request must not release a suspended encoder — 2.9.93, where any request did, " +
|
|
294
|
+
"and the run sawtoothed from 155 s to 922 s ahead of the viewer in three minutes"
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
from: ENCODE_RUN_STATE.ENDED_FAILED,
|
|
298
|
+
event: ENCODE_RUN_EVENT.FIRST_SEGMENT,
|
|
299
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
300
|
+
because:
|
|
301
|
+
"a dead run is not a producing one — 2.9.93, where the handle pointed at a corpse and every " +
|
|
302
|
+
"later seek was waved through as already covered, so the session answered 500 for ever"
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
from: ENCODE_RUN_STATE.ENDED_FAILED,
|
|
306
|
+
event: ENCODE_RUN_EVENT.RESUME_ORDERED,
|
|
307
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
308
|
+
because: "there is no process to continue; only a spawn leads out of a failure"
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
from: ENCODE_RUN_STATE.IDLE,
|
|
312
|
+
event: ENCODE_RUN_EVENT.FIRST_SEGMENT,
|
|
313
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
314
|
+
because: "nothing produces before a process exists"
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
from: ENCODE_RUN_STATE.RETRY_WAIT,
|
|
318
|
+
event: ENCODE_RUN_EVENT.FIRST_SEGMENT,
|
|
319
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
320
|
+
because: "a run waiting for its input back has no process; only a spawn resumes production"
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
from: ENCODE_RUN_STATE.STOPPED,
|
|
324
|
+
event: ENCODE_RUN_EVENT.RESUME_ORDERED,
|
|
325
|
+
mustNotReach: ENCODE_RUN_STATE.PRODUCING,
|
|
326
|
+
because:
|
|
327
|
+
"a rung the viewer switched away from must not be revived by its own held requests — " +
|
|
328
|
+
"the host has one encoder's worth of capacity and the rung on screen needs it"
|
|
329
|
+
}
|
|
330
|
+
]);
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The state `event` leads to, or `null` when the event means nothing here.
|
|
334
|
+
*
|
|
335
|
+
* Two answers with two meanings, kept apart on purpose:
|
|
336
|
+
*
|
|
337
|
+
* - `null` — no edge exists for this pair; the event is IGNORED. A caller may
|
|
338
|
+
* reasonably log it, and in this codebase that log line IS the measurement
|
|
339
|
+
* of whether this table matches reality.
|
|
340
|
+
* - the state passed in — an edge exists and leads back here; nothing
|
|
341
|
+
* changed, and no entry work may re-run.
|
|
342
|
+
*
|
|
343
|
+
* Never throws. The machine this pattern replaces did, from inside an event
|
|
344
|
+
* listener, so a refused transition abandoned the rest of the handler and left
|
|
345
|
+
* the app describing a state it was no longer in.
|
|
346
|
+
*
|
|
347
|
+
* @param {string} state - Current state.
|
|
348
|
+
* @param {string} event - One of {@link ENCODE_RUN_EVENT}.
|
|
349
|
+
* @returns {string | null}
|
|
350
|
+
*/
|
|
351
|
+
export function nextState(state, event) {
|
|
352
|
+
let scope = state;
|
|
353
|
+
while (scope) {
|
|
354
|
+
const target = TRANSITIONS[scope]?.[event];
|
|
355
|
+
if (target !== undefined) {
|
|
356
|
+
return target;
|
|
357
|
+
}
|
|
358
|
+
scope = PARENT[scope] ?? null;
|
|
359
|
+
}
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Whether `state` sits inside `superstate` (or is it).
|
|
365
|
+
*
|
|
366
|
+
* @param {string} state
|
|
367
|
+
* @param {string} superstate - One of {@link ENCODE_RUN_SUPERSTATE}.
|
|
368
|
+
* @returns {boolean}
|
|
369
|
+
*/
|
|
370
|
+
export function isWithin(state, superstate) {
|
|
371
|
+
let scope = state;
|
|
372
|
+
while (scope) {
|
|
373
|
+
if (scope === superstate) {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
scope = PARENT[scope] ?? null;
|
|
377
|
+
}
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Is anything reading the input right now?
|
|
383
|
+
*
|
|
384
|
+
* The output the torrent turns on, and the one that had no name anywhere in the
|
|
385
|
+
* codebase until the pool learned it the hard way: suspending the encoder stops
|
|
386
|
+
* the only reader, the reader's window is then satisfied, WebTorrent drops the
|
|
387
|
+
* selection, and the download dies with the swarm wide open — eleven minutes of
|
|
388
|
+
* it, measured 2026-08-05 with 150 peers connected.
|
|
389
|
+
*
|
|
390
|
+
* @param {string} state
|
|
391
|
+
* @returns {boolean}
|
|
392
|
+
*/
|
|
393
|
+
export function isInputBeingRead(state) {
|
|
394
|
+
return state === ENCODE_RUN_STATE.STARTING || state === ENCODE_RUN_STATE.PRODUCING;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* May a signal be sent to this run's process?
|
|
399
|
+
*
|
|
400
|
+
* Replaces re-deriving the answer from a child-process handle at five sites.
|
|
401
|
+
* The OS remains the authority on whether a pid still exists: a caller must
|
|
402
|
+
* keep its `try`/`catch` and feed a throw back as an exit event, never
|
|
403
|
+
* contradict it. A machine that believes it owns the process's life is wrong
|
|
404
|
+
* the first time ffmpeg is killed from outside.
|
|
405
|
+
*
|
|
406
|
+
* @param {string} state
|
|
407
|
+
* @returns {boolean}
|
|
408
|
+
*/
|
|
409
|
+
export function processCanBeSignalled(state) {
|
|
410
|
+
return isWithin(state, ENCODE_RUN_SUPERSTATE.ALIVE);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* What a request for a segment that is not on disk gets.
|
|
415
|
+
*
|
|
416
|
+
* `"hold"` and `"fail"` were three answers to one condition across the serving
|
|
417
|
+
* path — hold, 404 and 500 — chosen at each site. They are one answer here.
|
|
418
|
+
*
|
|
419
|
+
* @param {string} state
|
|
420
|
+
* @returns {"hold" | "fail"}
|
|
421
|
+
*/
|
|
422
|
+
export function answerForMissingSegment(state) {
|
|
423
|
+
return state === ENCODE_RUN_STATE.ENDED_FAILED ? "fail" : "hold";
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* The status string the browser is given.
|
|
428
|
+
*
|
|
429
|
+
* A Moore output rather than a second field maintained by hand at seven sites,
|
|
430
|
+
* which is how the two came to be read together with `||`.
|
|
431
|
+
*
|
|
432
|
+
* @param {string} state
|
|
433
|
+
* @returns {"starting" | "running" | "ready" | "failed"}
|
|
434
|
+
*/
|
|
435
|
+
export function wireState(state) {
|
|
436
|
+
if (state === ENCODE_RUN_STATE.IDLE || state === ENCODE_RUN_STATE.RETRY_WAIT) {
|
|
437
|
+
return "starting";
|
|
438
|
+
}
|
|
439
|
+
if (state === ENCODE_RUN_STATE.ENDED_COMPLETE) {
|
|
440
|
+
return "ready";
|
|
441
|
+
}
|
|
442
|
+
if (state === ENCODE_RUN_STATE.ENDED_FAILED) {
|
|
443
|
+
return "failed";
|
|
444
|
+
}
|
|
445
|
+
// STARTING, PRODUCING, SUSPENDED and STOPPED. The last of these keeps the
|
|
446
|
+
// value its run left behind, which is what a stopped rung reports today; a
|
|
447
|
+
// family's progress is answered from the variant on screen in any case.
|
|
448
|
+
return "running";
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* May the encoder be repositioned from this state?
|
|
453
|
+
*
|
|
454
|
+
* False only for a run that reached the end of the file: everything it could
|
|
455
|
+
* produce exists, so a restart would re-make what is already on disk.
|
|
456
|
+
*
|
|
457
|
+
* @param {string} state
|
|
458
|
+
* @returns {boolean}
|
|
459
|
+
*/
|
|
460
|
+
export function mayRestart(state) {
|
|
461
|
+
return state !== ENCODE_RUN_STATE.ENDED_COMPLETE;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Every declared edge, flattened, for the tests and for drawing the graph.
|
|
466
|
+
* Superstate edges are reported against the superstate, not expanded.
|
|
467
|
+
*
|
|
468
|
+
* @returns {Array<{ from: string, event: string, to: string }>}
|
|
469
|
+
*/
|
|
470
|
+
export function declaredEdges() {
|
|
471
|
+
const edges = [];
|
|
472
|
+
for (const [from, byEvent] of Object.entries(TRANSITIONS)) {
|
|
473
|
+
for (const [event, to] of Object.entries(byEvent)) {
|
|
474
|
+
edges.push({ from, event, to });
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return edges;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* The containment chain, for the graph renderer. A copy, so no caller can edit
|
|
482
|
+
* the table by editing what it was handed.
|
|
483
|
+
*
|
|
484
|
+
* @returns {Array<{ state: string, parent: string | null }>}
|
|
485
|
+
*/
|
|
486
|
+
export function declaredContainment() {
|
|
487
|
+
return Object.entries(PARENT).map(([state, parent]) => ({ state, parent }));
|
|
488
|
+
}
|