@torrent-tv/proxy 2.87.0 → 2.87.2

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.
@@ -0,0 +1,325 @@
1
+ import { logger } from "../../utils/logger.js";
2
+ import { bandOf, waits } from "../viewer/WaitLedger.js";
3
+
4
+ const HOLD_HEADER = "x-hold-ms";
5
+ /**
6
+ * Which viewer is asking, or an empty string where the transport names nobody.
7
+ *
8
+ * @param {import("fastify").FastifyRequest} req
9
+ * @returns {string}
10
+ */
11
+ export function consumerOf(req) {
12
+ return typeof req.query?.consumer === "string" ? req.query.consumer : "";
13
+ }
14
+
15
+ /**
16
+ * The generation the request says it was made in, or NaN when it says none.
17
+ *
18
+ * STAMPED BY THE PAGE when the request was SENT (`webrtc-hls-loader.js`), not
19
+ * when it arrived: a request made before a seek and delivered after it still
20
+ * says it belongs to the viewing that was left. Only a stated non-negative
21
+ * integer counts; anything else is "nothing stated".
22
+ *
23
+ * @param {import("fastify").FastifyRequest} req
24
+ * @returns {number}
25
+ */
26
+ export function statedGenerationOf(req) {
27
+ const raw = req.query?.generation;
28
+ if (typeof raw !== "string" || !/^\d+$/.test(raw)) {
29
+ return Number.NaN;
30
+ }
31
+ return Number(raw);
32
+ }
33
+
34
+ /**
35
+ * Answer at once, and do nothing else, when the request belongs to a viewing
36
+ * its viewer has left and whose window for new requests has passed.
37
+ *
38
+ * BEFORE ANY OTHER STEP OF THE ROUTE. Resolving a quality step or a soundtrack
39
+ * can create an output and registers the viewer on it; a stale request that got
40
+ * that far would leave both behind and be refused only afterwards. The answer
41
+ * is the same one a request made pointless by a seek already gets — 503 with
42
+ * `Retry-After: 0` — because it is the same situation seen from the other end.
43
+ *
44
+ * @param {import("fastify").FastifyRequest} req
45
+ * @param {import("fastify").FastifyReply} reply
46
+ * @param {{ acceptsRequest: Function }} viewerRequests
47
+ * @param {string} fileName - For the log line.
48
+ * @returns {boolean} Whether the request was answered here.
49
+ */
50
+ export function refusedAsStale(req, reply, viewerRequests, fileName) {
51
+ const consumerId = consumerOf(req);
52
+ const stated = statedGenerationOf(req);
53
+ if (viewerRequests.acceptsRequest(consumerId, stated)) {
54
+ return false;
55
+ }
56
+ logger.info(
57
+ `[hold] ${fileName} refused: made in generation ${stated} of ${consumerId}, ` +
58
+ `a viewing they have left and whose window for new requests has passed`
59
+ );
60
+ reply.header("Retry-After", "0");
61
+ reply.code(503).send({ error: "Made for a viewing that has been left." });
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * Serve one playlist or segment from a named session.
67
+ *
68
+ * Split from the route above because the variant route
69
+ * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
70
+ * another session of the same family, and must hold, log and answer them
71
+ * identically — a switch of quality must not go through a different code path
72
+ * from the stream it switches away from.
73
+ *
74
+ * @param {import("fastify").FastifyRequest} req
75
+ * @param {import("fastify").FastifyReply} reply
76
+ * @param {{ serving: object, viewerRequests: object, sessionId: string, fileName: string }} params
77
+ * @returns {Promise<void>}
78
+ */
79
+ export async function serveSessionFile(req, reply, { serving, viewerRequests, sessionId, fileName }) {
80
+ // Which viewer is asking. One session serves everyone watching a copied
81
+ // picture, so without it their positions collapse into one and a seek by the
82
+ // viewer in front releases the requests held for the viewer behind. Absent on
83
+ // a plain HTTP transport, where no loader of ours builds the URL, and then
84
+ // the single shared position decides as it always did.
85
+ const consumerId = consumerOf(req);
86
+ // Hold the request only briefly, then answer "retry" instead of waiting for
87
+ // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
88
+ // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
89
+ // file") when it passes — it then cancels in-flight requests, probes
90
+ // neighbouring positions and can restart the stream from the beginning. That
91
+ // is exactly the post-seek "player thrashing" seen in the field, because a
92
+ // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
93
+ // to appear. Holding the connection for 30 s (as this did) guaranteed the
94
+ // timeout on every seek. A short hold keeps the fast path intact (a ready or
95
+ // nearly-ready segment is still served on the first request) while a slow one
96
+ // gets a prompt retryable answer, which resets the player's own deadline.
97
+ // hls.js is unaffected: it consumes the 503 through its retry policy, whose
98
+ // budget the client widens to match (see hls-player.js fragLoadPolicy).
99
+ // Instrumented wait. `clientAborted` flips when the player drops the
100
+ // connection while we are still holding it — the single most informative
101
+ // signal about its real patience, and observable only from this side.
102
+ const holdStartedAt = Date.now();
103
+ let clientAborted = false;
104
+ let onClientAbort = () => {};
105
+ const clientGone = new Promise((resolve) => {
106
+ onClientAbort = () => {
107
+ clientAborted = true;
108
+ resolve();
109
+ };
110
+ });
111
+ req.raw.on("close", onClientAbort);
112
+ const statedHoldMs = Number(req.headers?.[HOLD_HEADER]);
113
+
114
+ const result = await waitForSessionFile(serving, sessionId, fileName, {
115
+ holdMs: Number.isFinite(statedHoldMs) && statedHoldMs > 0 ? statedHoldMs : Number.POSITIVE_INFINITY,
116
+ until: clientGone,
117
+ consumerId
118
+ });
119
+
120
+ req.raw.off("close", onClientAbort);
121
+ const heldMs = Date.now() - holdStartedAt;
122
+ if (result.isPlaylist !== true) {
123
+ const outcome = clientAborted
124
+ ? "client-aborted"
125
+ : result.kind === "ok" ? "served" : result.kind;
126
+ // AGAINST THE RANK THE MAP GAVE IT. A wait is the only thing that says
127
+ // whether the prioritisation is being used well, and this is the one place
128
+ // in the proxy where a viewer is measurably waiting for a named segment.
129
+ // Recorded even when the segment was served at once: a run of short waits
130
+ // at the top rank is what "the urgent zone is being served first" looks
131
+ // like, and without them the table would hold only the failures.
132
+ const ranked = result.ranked ?? null;
133
+ if (ranked) {
134
+ waits.note(ranked.address, heldMs, ranked.rank, ranked.topRank);
135
+ }
136
+ logger.info(
137
+ `[hold] ${fileName} ${outcome} after ${heldMs}ms` +
138
+ (ranked ? ` (the map wants it ${bandOf(ranked.rank, ranked.topRank)}, rank ${ranked.rank} of ${ranked.topRank})` : "")
139
+ );
140
+ }
141
+
142
+ if (result.kind === "not-found") {
143
+ return reply.code(404).send({ error: "Transcode session file was not found." });
144
+ }
145
+ if (result.kind === "superseded") {
146
+ // The viewer moved while this was being held, and this segment is not the
147
+ // one they moved TO — that case is kept and waited out, see
148
+ // `waitForSessionFile`. Answer at once so the player can ask for where it
149
+ // is now; `Retry-After: 0` because there is nothing to wait for.
150
+ //
151
+ // Named in the log, because a refusal that says only "superseded" is what
152
+ // made the 2026-08-18 freeze take a day to explain: the player was refused
153
+ // the segment at its own seek target and nothing recorded which segment or
154
+ // where the viewer was.
155
+ logger.info(
156
+ `[hold] ${fileName} refused: the viewer is at ` +
157
+ `${viewerRequests.viewerPositionOf(sessionId, consumerId).toFixed(1)}s and this is not the segment there`
158
+ );
159
+ reply.header("Retry-After", "0");
160
+ return reply.code(503).send({ error: "Superseded by a seek." });
161
+ }
162
+ if (result.kind === "warming-up") {
163
+ // The segment is still being produced (e.g. just after a seek-restart).
164
+ // Return a retryable 503 — never 202, which hls.js cannot consume as a
165
+ // media segment — so the player retries the fetch shortly.
166
+ reply.header("Retry-After", "1");
167
+ // `Retry-After` tells the player to re-request THIS segment after a short
168
+ // pause. Without it a bare 503 reads as "nothing here", and the player goes
169
+ // looking elsewhere: because our synthetic VOD playlist lists every segment
170
+ // of the file, it believes they all exist and SCANS them (field log: one
171
+ // user seek produced probes at #617, #717, #732…). That scan is what used
172
+ // to steer the encoder off the real target. Whether iOS's native player
173
+ // honours the hint is not guaranteed — its behaviour is closed — but this
174
+ // is the standard, correct way to say "wait, don't look elsewhere", and
175
+ // hls.js already retries the same fragment regardless.
176
+ reply.header("Retry-After", "1");
177
+ // SAY WHY, to the viewer and not only to the log. The same refusal is
178
+ // written into `proxy.log` with the rank the priority map gave this
179
+ // segment; the page had no access to that and told the viewer it did not
180
+ // know why. Field 2026-09-14: `rank 1 of 100` — nothing was making it —
181
+ // printed sixty seconds before the page gave up saying the opposite.
182
+ return reply.code(503).send({
183
+ error: "Transcode segment is still being produced.",
184
+ reason: warmingReason(result.ranked ?? null)
185
+ });
186
+ }
187
+ if (result.kind === "failed") {
188
+ return reply.code(500).send({ error: result.message });
189
+ }
190
+
191
+ if (result.isPlaylist) {
192
+ reply.header("Cache-Control", "no-store");
193
+ } else {
194
+ reply.header("Cache-Control", "public, max-age=60");
195
+ holdWhileSending(reply, result.stream, viewerRequests.holdResponse(sessionId, consumerId));
196
+ }
197
+ reply.header("Content-Type", result.contentType);
198
+ return reply.send(result.stream);
199
+ }
200
+
201
+ /**
202
+ * Keep the output this response comes from held until the response is over.
203
+ *
204
+ * Over means any of `finish`, `close` or `error` of the response, or `error`
205
+ * of the stream being sent — whichever comes first, and usually more than one
206
+ * of them comes: a response that finishes also closes. `release` is safe to
207
+ * call any number of times, which is what makes listening to all of them
208
+ * correct rather than a leak or a double release.
209
+ *
210
+ * A response already over before it began (the requester went during the
211
+ * hold) is released at once: none of its events will fire again.
212
+ *
213
+ * @param {import("fastify").FastifyReply} reply
214
+ * @param {import("node:stream").Readable | undefined} stream
215
+ * @param {() => void} release
216
+ * @returns {void}
217
+ */
218
+ function holdWhileSending(reply, stream, release) {
219
+ const response = reply.raw;
220
+ if (!response || response.destroyed || response.writableEnded) {
221
+ release();
222
+ return;
223
+ }
224
+ response.once("finish", release);
225
+ response.once("close", release);
226
+ response.once("error", release);
227
+ stream?.once?.("error", release);
228
+ }
229
+
230
+ /**
231
+ * Ask `serving.getFileStream()` again each time the file may have become
232
+ * available — a segment's publication, or a waited-on invalidation — until it
233
+ * is, the session fails, the requester's stated wait runs out, or the
234
+ * requester goes.
235
+ *
236
+ * @param {object} serving - `services/server/SegmentServing.js`
237
+ * @param {string} sessionId
238
+ * @param {string} fileName
239
+ * @param {object} hold
240
+ * @param {number} hold.holdMs - How long the requester will wait; may be
241
+ * infinite when it stated nothing.
242
+ * @param {Promise<void>} [hold.until] - Settles when the requester has gone.
243
+ * @param {string} [hold.consumerId] - Which viewer is asking, where the
244
+ * transport carries it. Their own position is what decides whether a held
245
+ * request has been made pointless by a seek — a session can have several
246
+ * viewers, and the seek epoch belongs to all of them.
247
+ * @returns {Promise<Awaited<ReturnType<import("./SegmentServing.js").SegmentServing["getFileStream"]>>>}
248
+ */
249
+ export async function waitForSessionFile(serving, sessionId, fileName, { holdMs, until = null, consumerId = "" }) {
250
+ const startedAt = Date.now();
251
+ let gone = false;
252
+ const requesterGone = until ? until.then(() => { gone = true; }) : new Promise(() => {});
253
+ // The viewer's position when this request was made. A seek makes every held
254
+ // request stale — it asks for a segment nobody is going to watch — and hls.js
255
+ // keeps only ONE fragment load outstanding, so holding on blocks the request
256
+ // the player actually needs now. Measured: 57 s of a 58 s backward seek was
257
+ // this wait, and the segment the viewer wanted took 15 ms once it was asked
258
+ // for.
259
+ let seekEpoch = serving.seekEpoch(sessionId);
260
+ /** @type {{ address: string, rank: number, topRank: number } | null} */
261
+ let lastRanked = null;
262
+ while (!gone && Date.now() - startedAt < holdMs) {
263
+ const result = await serving.getFileStream(sessionId, fileName, { consumerId });
264
+ if (result.kind !== "warming-up") {
265
+ return result;
266
+ }
267
+ if (serving.seekEpoch(sessionId) !== seekEpoch) {
268
+ // A seek moved the epoch. Whether THIS request is stale depends on which
269
+ // segment it asks for: the one the viewer has just landed on races the
270
+ // seek notification and would otherwise be refused at the exact moment it
271
+ // is needed — measured 2026-08-18, two 503s within 80 ms on the segment
272
+ // at the seek target, after which the player never asked for it again.
273
+ if (!serving.requestStillWanted(sessionId, fileName, consumerId)) {
274
+ return { kind: "superseded" };
275
+ }
276
+ logger.info(
277
+ `[hold] ${fileName} kept across a seek: it is the segment the viewer now needs`
278
+ );
279
+ seekEpoch = serving.seekEpoch(sessionId);
280
+ }
281
+ // The rank the map last gave this segment, so a request that runs out of
282
+ // patience is still counted against what it was promised. Kept across the
283
+ // polls because the timeout path has no result of its own.
284
+ lastRanked = result.ranked ?? lastRanked;
285
+ const remaining = Math.max(0, holdMs - (Date.now() - startedAt));
286
+ const waitedForSegment = await serving.waitForSegment(sessionId, fileName, remaining, until);
287
+ if (!waitedForSegment && !gone) {
288
+ // Playlists and init files are not segment publications.
289
+ await Promise.race([delay(Math.min(300, remaining)), requesterGone]);
290
+ }
291
+ }
292
+ return { kind: "warming-up", ranked: lastRanked };
293
+ }
294
+
295
+ /**
296
+ * Resolve after a given number of milliseconds.
297
+ *
298
+ * @param {number} ms
299
+ * @returns {Promise<void>}
300
+ */
301
+ function delay(ms) {
302
+ return new Promise((resolve) => {
303
+ setTimeout(resolve, ms);
304
+ });
305
+ }
306
+
307
+ /**
308
+ * What to tell the VIEWER when a segment is not there yet.
309
+ *
310
+ * In their terms — the film, and what is being done about it — never in ours: a
311
+ * viewer cannot read a log, does not know what a rank is, and is not helped by
312
+ * being told which of our parts is waiting for which other.
313
+ *
314
+ * @param {{ rank: number, topRank: number } | null} ranked - What the priority
315
+ * map thought of this segment while the request was held.
316
+ * @returns {string}
317
+ */
318
+ function warmingReason(ranked) {
319
+ if (ranked && ranked.rank < ranked.topRank) {
320
+ // The map is working somewhere else in the film. That is the answer the
321
+ // failure of 2026-09-14 needed and nobody was given.
322
+ return "This part of the film is not being prepared yet — the proxy is working further along.";
323
+ }
324
+ return "This part of the film is still being prepared.";
325
+ }
@@ -227,6 +227,31 @@ export function wireOutputs({
227
227
  memoryClaimant = null,
228
228
  budgetPolicy = null}) {
229
229
  const parts = {};
230
+ // Encode components receive only the storage operations they use. The
231
+ // concrete SegmentStore remains owned by this assembly and is never handed
232
+ // to the encode layer.
233
+ const segmentCoverage = {
234
+ provenNumbers: (address) => parts.segmentStore.provenNumbers(address),
235
+ announce: (address, index) => parts.segmentStore.announce(address, index),
236
+ filesHeld: (address) => parts.segmentStore.filesHeld(address),
237
+ clearUpAfter: (address, startedAt) => parts.segmentStore.clearUpAfter(address, startedAt)
238
+ };
239
+ const segmentFiles = {
240
+ pathFor: (address) => parts.segmentStore.pathFor(address),
241
+ initOf: (address) => parts.segmentStore.initOf(address),
242
+ directoryFor: (address) => parts.segmentStore.directoryFor(address),
243
+ publish: (address, makingName, format) => parts.segmentStore.publish(address, makingName, format)
244
+ };
245
+ const segmentOutputFiles = {
246
+ addresses: () => parts.segmentStore.addresses(),
247
+ isClosed: (address, index) => parts.segmentStore.isClosed(address, index),
248
+ lastReadAt: (address) => parts.segmentStore.lastReadAt(address),
249
+ directoryFor: (address) => parts.segmentStore.directoryFor(address),
250
+ useFormat: (address, format) => parts.segmentStore.useFormat(address, format)
251
+ };
252
+ const segmentPaths = {
253
+ pathOf: (address, index) => parts.segmentStore.pathOf(address, index)
254
+ };
230
255
  /**
231
256
  * Whether everything this output will ever serve is made: every segment of
232
257
  * its timeline closed in the store. Such an output needs no encoder and
@@ -298,7 +323,7 @@ export function wireOutputs({
298
323
  get outputs() { return parts.outputs; },
299
324
  get priority() { return parts.priority; },
300
325
  get segmentDurationSec() { return parts.segmentDurationSec; },
301
- get segmentStore() { return parts.segmentStore; },
326
+ segmentFiles,
302
327
  get videoEncoder() { return parts.encoders.current; },
303
328
  get encoders() { return parts.encoders; },
304
329
  invalidateWaits: (output) => parts.serving.invalidateWaits(output),
@@ -496,7 +521,7 @@ export function wireOutputs({
496
521
  get returns() { return parts.returns; },
497
522
  get segmentDurationSec() { return parts.segmentDurationSec; },
498
523
  get segmentFormat() { return parts.segmentFormat; },
499
- get segmentStore() { return parts.segmentStore; },
524
+ segmentOutputFiles,
500
525
  get softwarePresetBenchmark() { return parts.softwarePresetBenchmark; },
501
526
  get sourceFiles() { return parts.sourceFiles; },
502
527
  get timelines() { return parts.timelines; },
@@ -848,7 +873,7 @@ export function wireOutputs({
848
873
  segmentSeconds: parts.segmentDurationSec,
849
874
  contentionPenalties: parts.contentionPenalties,
850
875
  startingSpeedFor: (address) => parts.encodeCost.speedForOutput(address),
851
- segmentStore: parts.segmentStore,
876
+ segmentCoverage,
852
877
  // HOW IT ASKS TO DECIDE AGAIN. A plan that refuses to place anything
853
878
  // because an output's input is away needs something to bring it back:
854
879
  // nothing about the state changes while the data is missing, so no event
@@ -946,7 +971,7 @@ export function wireOutputs({
946
971
  get outputs() { return parts.outputs; },
947
972
  get qualityOffer() { return parts.qualityOffer; },
948
973
  get segmentDurationSec() { return parts.segmentDurationSec; },
949
- get segmentStore() { return parts.segmentStore; },
974
+ segmentPaths,
950
975
  get videoEncoder() { return parts.encoders.current; },
951
976
  });
952
977
  parts.budgetTimer = setInterval(() => {
@@ -834,12 +834,12 @@ export class SegmentStore {
834
834
  */
835
835
  drop(key, because) {
836
836
  const dir = path.join(this.#root, directoryNameFor(key));
837
+ this.#unwatch(key);
837
838
  try {
838
839
  rmSync(dir, { recursive: true, force: true });
839
840
  } catch {
840
841
  // Already gone, or in use; the next sweep sees it either way.
841
842
  }
842
- this.#unwatch(key);
843
843
  this.#held.delete(key);
844
844
  this.#formats.delete(key);
845
845
  this.#touched.delete(key);
@@ -70,7 +70,12 @@ function orchestratorWithAStore() {
70
70
  firstByteWaitSec: 0.12,
71
71
  refetchSecPerFilmSecond: () => 0.25,
72
72
  startingSpeedFor: () => 2,
73
- segmentStore: store,
73
+ segmentCoverage: {
74
+ provenNumbers: (address) => store.provenNumbers(address),
75
+ announce: (address, index) => store.announce(address, index),
76
+ filesHeld: (address) => store.filesHeld(address),
77
+ clearUpAfter: (address, startedAt) => store.clearUpAfter(address, startedAt)
78
+ },
74
79
  now: () => 1000,
75
80
  logger,
76
81
  makeRun: ({ address, from, to }) => new EncodeRun({
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Decided with the user 2026-09-16: which pieces are made is the segment
5
5
  * store's, and which encoders run is the encoding orchestrator's. A second
6
- * holder of either is how item 87 stopped playback outright.
6
+ * holder of either is how the stale second owner stopped playback outright.
7
7
  */
8
8
 
9
9
  import test from "node:test";
@@ -0,0 +1,39 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { installationId } from "../services/identity/installation-id.js";
7
+
8
+ test("an installation id is reused from its state directory", (t) => {
9
+ const stateDir = mkdtempSync(path.join(os.tmpdir(), "proxy-identity-"));
10
+ t.after(() => rmSync(stateDir, { recursive: true, force: true }));
11
+
12
+ assert.equal(installationId({ stateDir, makeId: () => "first-id" }), "first-id");
13
+ assert.equal(installationId({ stateDir, makeId: () => "unexpected-id" }), "first-id");
14
+ assert.deepEqual(JSON.parse(readFileSync(path.join(stateDir, "proxy-identity.json"), "utf8")), { id: "first-id" });
15
+ });
16
+
17
+ test("an explicit id takes precedence and becomes the saved installation id", (t) => {
18
+ const stateDir = mkdtempSync(path.join(os.tmpdir(), "proxy-identity-"));
19
+ t.after(() => rmSync(stateDir, { recursive: true, force: true }));
20
+
21
+ assert.equal(installationId({ stateDir, explicitId: " configured-id " }), "configured-id");
22
+ assert.equal(installationId({ stateDir, makeId: () => "unexpected-id" }), "configured-id");
23
+ });
24
+
25
+ test("without a state directory the default id stays ephemeral", () => {
26
+ let calls = 0;
27
+ const makeId = () => `id-${++calls}`;
28
+
29
+ assert.equal(installationId({ makeId }), "id-1");
30
+ assert.equal(installationId({ makeId }), "id-2");
31
+ });
32
+
33
+ test("a damaged saved identity is reported instead of silently replaced", (t) => {
34
+ const stateDir = mkdtempSync(path.join(os.tmpdir(), "proxy-identity-"));
35
+ t.after(() => rmSync(stateDir, { recursive: true, force: true }));
36
+ writeFileSync(path.join(stateDir, "proxy-identity.json"), "{}\n");
37
+
38
+ assert.throws(() => installationId({ stateDir, makeId: () => "replacement" }), /Invalid proxy identity file/);
39
+ });
@@ -319,4 +319,5 @@ test("what the store weighs is reported, and dropping one output frees it", (t)
319
319
  assert.equal(store.stats().bytes, 2000);
320
320
  store.drop(KEY, "nobody is watching it");
321
321
  assert.equal(store.stats().outputs, 0);
322
+ assert.equal(readdirSync(root).length, 0, "the watched output directory was removed");
322
323
  });
@@ -84,6 +84,27 @@ test("a container that declares nothing leaves the banner alone", () => {
84
84
  assert.equal(merged.tracks[0].declaresDefault, false);
85
85
  });
86
86
 
87
+ test("an implicit Matroska English default does not become the subtitle language", () => {
88
+ const banner = [{ index: 0, language: "eng", title: "", isDefault: true }];
89
+ const declared = [{ language: "", languageBcp47: "", declaresLanguage: false, name: "" }];
90
+
91
+ const merged = Container.mergeSubtitleFlags(banner, declared);
92
+
93
+ assert.equal(merged.aligned, true);
94
+ assert.equal(merged.tracks[0].language, "");
95
+ assert.equal(merged.tracks[0].declaresLanguage, false);
96
+ });
97
+
98
+ test("an explicitly declared subtitle language is retained", () => {
99
+ const banner = [{ index: 0, language: "rus", title: "", isDefault: true }];
100
+ const declared = [{ language: "rus", languageBcp47: "", declaresLanguage: true, name: "" }];
101
+
102
+ const merged = Container.mergeSubtitleFlags(banner, declared);
103
+
104
+ assert.equal(merged.tracks[0].language, "rus");
105
+ assert.equal(merged.tracks[0].declaresLanguage, true);
106
+ });
107
+
87
108
  test("a name confirms a pairing when the languages are unstated", () => {
88
109
  assert.equal(Container.pairingHolds({ language: "und", title: "Forced" }, { language: "", name: "Forced" }), true);
89
110
  });
@@ -104,9 +104,11 @@ function trackEntry({ number, type, codecId, language, flags = {}, languageBcp47
104
104
  const parts = [
105
105
  uintElement(ID_TRACK_NUMBER, number),
106
106
  uintElement(ID_TRACK_TYPE, type),
107
- stringElement(ID_CODEC_ID, codecId),
108
- stringElement(ID_LANGUAGE, language)
107
+ stringElement(ID_CODEC_ID, codecId)
109
108
  ];
109
+ if (language !== undefined && language !== null) {
110
+ parts.push(stringElement(ID_LANGUAGE, language));
111
+ }
110
112
  if (languageBcp47 !== null) {
111
113
  parts.push(stringElement(ID_LANGUAGE_BCP47, languageBcp47));
112
114
  }
@@ -281,8 +283,9 @@ test("two walks of one file at the same time read each cluster once", async () =
281
283
 
282
284
  /**
283
285
  * A file whose subtitle tracks carry the flags RFC 9559 defines for them: one
284
- * forced, one for viewers who cannot hear, one the file marks unusable, and one
285
- * writing its language as RFC 5646 alongside the three-letter code.
286
+ * forced, one for viewers who cannot hear, one the file marks unusable, one
287
+ * writing its language as RFC 5646 alongside the three-letter code, and one
288
+ * relying on Matroska's default language.
286
289
  *
287
290
  * @returns {Buffer}
288
291
  */
@@ -293,7 +296,8 @@ function fileWithFlags() {
293
296
  trackEntry({ number: 2, type: 17, codecId: "S_TEXT/UTF8", language: "rus", flags: { forced: true } }),
294
297
  trackEntry({ number: 3, type: 17, codecId: "S_TEXT/UTF8", language: "eng", flags: { hearingImpaired: true } }),
295
298
  trackEntry({ number: 4, type: 17, codecId: "S_TEXT/UTF8", language: "fre", flags: { enabled: false } }),
296
- trackEntry({ number: 5, type: 17, codecId: "S_TEXT/ASS", language: "por", languageBcp47: "pt-BR" })
299
+ trackEntry({ number: 5, type: 17, codecId: "S_TEXT/ASS", language: "por", languageBcp47: "pt-BR" }),
300
+ trackEntry({ number: 6, type: 17, codecId: "S_TEXT/UTF8", language: null })
297
301
  ]));
298
302
  const seekEntry = (targetId, position) => element(ID_SEEK, Buffer.concat([
299
303
  element(ID_SEEK_ID, idBytes(targetId)),
@@ -345,7 +349,7 @@ test("an unusable track keeps its place, so the tracks after it keep theirs", as
345
349
  // s:0 forced, s:1 SDH, s:2 the unusable one, s:3 the Brazilian track.
346
350
  assert.deepEqual(
347
351
  plan.tracks.map((track) => [track.trackNumber, track.declaredIndex]),
348
- [[2, 0], [3, 1], [5, 3]]
352
+ [[2, 0], [3, 1], [5, 3], [6, 4]]
349
353
  );
350
354
  });
351
355
 
@@ -369,3 +373,14 @@ test("where the file writes RFC 5646, that is the language", async () => {
369
373
  assert.equal(track.language, "pt-BR", "not the three-letter por");
370
374
  assert.equal(track.languageBcp47, "pt-BR");
371
375
  });
376
+
377
+ test("an omitted Matroska Language element stays unstated rather than becoming the RFC default", async () => {
378
+ const file = fileWithFlags();
379
+ const plan = await MatroskaContainer.readSubtitlePlan(readerOver(file), file.length);
380
+
381
+ const track = plan.tracks.find((entry) => entry.trackNumber === 6);
382
+ const declared = plan.declared.find((entry) => entry.trackNumber === 6);
383
+ assert.equal(track.language, "");
384
+ assert.equal(track.declaresLanguage, false);
385
+ assert.equal(declared.declaresLanguage, false);
386
+ });