@blockcast/mmt-container 0.1.0-main.c71423b8a4e4 → 0.1.0-main.ce3c8aeb7f68

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.
Files changed (148) hide show
  1. package/dist/align.d.ts +6 -2
  2. package/dist/align.d.ts.map +1 -1
  3. package/dist/align.js.map +2 -2
  4. package/dist/audio-ring-buffer.js +23 -36
  5. package/dist/audio-ring-buffer.js.map +1 -1
  6. package/dist/browser-runtime.js +6865 -6485
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.d.ts +3 -1
  9. package/dist/carp-catalog.d.ts.map +1 -1
  10. package/dist/carp-catalog.js +21 -2
  11. package/dist/carp-catalog.js.map +4 -4
  12. package/dist/catalog-fec-params.js +10 -0
  13. package/dist/catalog-fec-params.js.map +4 -4
  14. package/dist/cmaf-block-assembler.d.ts +42 -0
  15. package/dist/cmaf-block-assembler.d.ts.map +1 -1
  16. package/dist/cmaf-block-assembler.js +542 -535
  17. package/dist/cmaf-block-assembler.js.map +2 -2
  18. package/dist/cmaf-group-batcher.js +27 -46
  19. package/dist/cmaf-group-batcher.js.map +2 -2
  20. package/dist/cmcd-producer.js +45 -66
  21. package/dist/cmcd-producer.js.map +1 -1
  22. package/dist/fec-manager.d.ts +13 -1
  23. package/dist/fec-manager.d.ts.map +1 -1
  24. package/dist/fec-manager.js +1151 -871
  25. package/dist/fec-manager.js.map +2 -2
  26. package/dist/fec-starvation-detector.js +25 -40
  27. package/dist/fec-starvation-detector.js.map +1 -1
  28. package/dist/fec-worker-setup.js +242 -225
  29. package/dist/fec-worker-setup.js.map +3 -3
  30. package/dist/index.d.ts +3 -3
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js.map +2 -2
  33. package/dist/iso-bmff-timing.js +63 -88
  34. package/dist/iso-bmff-timing.js.map +1 -1
  35. package/dist/loc.js +7 -4
  36. package/dist/loc.js.map +1 -1
  37. package/dist/manifest-auth-plan.js +292 -311
  38. package/dist/manifest-auth-plan.js.map +1 -1
  39. package/dist/media-router.d.ts +0 -99
  40. package/dist/media-router.d.ts.map +1 -1
  41. package/dist/media-router.js +270 -504
  42. package/dist/media-router.js.map +3 -3
  43. package/dist/mfu-reassembler.js +597 -607
  44. package/dist/mfu-reassembler.js.map +3 -3
  45. package/dist/mmtp-container-parser.d.ts +37 -0
  46. package/dist/mmtp-container-parser.d.ts.map +1 -1
  47. package/dist/mmtp-container-parser.js +1531 -1522
  48. package/dist/mmtp-container-parser.js.map +3 -3
  49. package/dist/mmtp-container-parser.no-modules.js +179 -102
  50. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  51. package/dist/mmtp-decode-pipeline.js +1641 -1651
  52. package/dist/mmtp-decode-pipeline.js.map +3 -3
  53. package/dist/mmtp-router.d.ts +34 -0
  54. package/dist/mmtp-router.d.ts.map +1 -1
  55. package/dist/mmtp-router.js +3676 -3390
  56. package/dist/mmtp-router.js.map +3 -3
  57. package/dist/mmtp-to-cmaf.d.ts +7 -1
  58. package/dist/mmtp-to-cmaf.d.ts.map +1 -1
  59. package/dist/mmtp-to-cmaf.js +109 -134
  60. package/dist/mmtp-to-cmaf.js.map +2 -2
  61. package/dist/multicast-input.d.ts +7 -35
  62. package/dist/multicast-input.d.ts.map +1 -1
  63. package/dist/multicast-input.js +21 -2
  64. package/dist/multicast-input.js.map +4 -4
  65. package/dist/multicast-manager.d.ts +10 -1
  66. package/dist/multicast-manager.d.ts.map +1 -1
  67. package/dist/multicast-manager.js +5584 -4961
  68. package/dist/multicast-manager.js.map +4 -4
  69. package/dist/multicast-transport.d.ts +7 -0
  70. package/dist/multicast-transport.d.ts.map +1 -1
  71. package/dist/multicast-transport.js +369 -421
  72. package/dist/multicast-transport.js.map +4 -4
  73. package/dist/multicast-unwrap.js +53 -30
  74. package/dist/multicast-unwrap.js.map +2 -2
  75. package/dist/no-modules.js +2096 -2097
  76. package/dist/no-modules.js.map +3 -3
  77. package/dist/sample-combiner.js +118 -139
  78. package/dist/sample-combiner.js.map +1 -1
  79. package/dist/signaling.js.map +2 -2
  80. package/dist/source-auth.js +353 -382
  81. package/dist/source-auth.js.map +1 -1
  82. package/dist/switching-set-alignment.js +14 -29
  83. package/dist/switching-set-alignment.js.map +1 -1
  84. package/dist/timestamp-codec.js +17 -11
  85. package/dist/timestamp-codec.js.map +2 -2
  86. package/dist/track-pipeline-setup.js +105 -125
  87. package/dist/track-pipeline-setup.js.map +1 -1
  88. package/dist/track-pipeline.js +107 -127
  89. package/dist/track-pipeline.js.map +1 -1
  90. package/dist/track-session.d.ts +16 -0
  91. package/dist/track-session.d.ts.map +1 -1
  92. package/dist/track-session.js +230 -243
  93. package/dist/track-session.js.map +2 -2
  94. package/dist/transport-pool.js +24 -38
  95. package/dist/transport-pool.js.map +1 -1
  96. package/dist/wasm-mfu-reassembler.d.ts +6 -1
  97. package/dist/wasm-mfu-reassembler.d.ts.map +1 -1
  98. package/dist/wasm-mfu-reassembler.js +127 -144
  99. package/dist/wasm-mfu-reassembler.js.map +2 -2
  100. package/dist/wasm-mmtp-container-parser.js +1659 -1650
  101. package/dist/wasm-mmtp-container-parser.js.map +3 -3
  102. package/node_modules/@blockcast/fec-worker/README.md +5 -1
  103. package/node_modules/@blockcast/fec-worker/dist/fec-worker-client.d.ts +8 -2
  104. package/node_modules/@blockcast/fec-worker/dist/fec-worker-client.d.ts.map +1 -1
  105. package/node_modules/@blockcast/fec-worker/dist/fec-worker-client.js +89 -16
  106. package/node_modules/@blockcast/fec-worker/dist/fec-worker-client.js.map +1 -1
  107. package/node_modules/@blockcast/fec-worker/dist/fec-worker-types.d.ts +48 -3
  108. package/node_modules/@blockcast/fec-worker/dist/fec-worker-types.d.ts.map +1 -1
  109. package/node_modules/@blockcast/fec-worker/dist/fec-worker.js +55 -8
  110. package/node_modules/@blockcast/fec-worker/dist/fec-worker.js.map +2 -2
  111. package/node_modules/@blockcast/fec-worker/dist/index.d.ts +1 -1
  112. package/node_modules/@blockcast/fec-worker/dist/index.d.ts.map +1 -1
  113. package/node_modules/@blockcast/fec-worker/dist/index.js.map +1 -1
  114. package/node_modules/@blockcast/fec-worker/src/fec-worker-client.ts +97 -17
  115. package/node_modules/@blockcast/fec-worker/src/fec-worker-types.ts +46 -2
  116. package/node_modules/@blockcast/fec-worker/src/fec-worker.ts +78 -10
  117. package/node_modules/@blockcast/fec-worker/src/index.ts +2 -0
  118. package/node_modules/@blockcast/mmt-base/README.md +1 -0
  119. package/node_modules/@blockcast/mmt-base/dist/mmtp.d.ts +52 -0
  120. package/node_modules/@blockcast/mmt-base/dist/mmtp.d.ts.map +1 -1
  121. package/node_modules/@blockcast/mmt-base/dist/mmtp.js +142 -37
  122. package/node_modules/@blockcast/mmt-base/dist/mmtp.js.map +1 -1
  123. package/node_modules/@blockcast/mmt-base/src/mmtp.ts +158 -37
  124. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/README.md +1 -0
  125. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/dist/mmtp.d.ts +52 -0
  126. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/dist/mmtp.d.ts.map +1 -1
  127. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/dist/mmtp.js +142 -37
  128. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/dist/mmtp.js.map +1 -1
  129. package/node_modules/@blockcast/mmt-fec/node_modules/@blockcast/mmt-base/src/mmtp.ts +158 -37
  130. package/package.json +7 -6
  131. package/schemas/catalog.schema.json +1 -1
  132. package/src/align.ts +6 -2
  133. package/src/carp-catalog.ts +13 -3
  134. package/src/cmaf-block-assembler.ts +90 -81
  135. package/src/fec-manager.ts +530 -146
  136. package/src/index.ts +2 -2
  137. package/src/media-router.ts +0 -397
  138. package/src/mmtp-container-parser.ts +97 -21
  139. package/src/mmtp-router.ts +302 -125
  140. package/src/mmtp-to-cmaf.ts +21 -7
  141. package/src/multicast-input.ts +23 -3
  142. package/src/multicast-manager.ts +83 -11
  143. package/src/multicast-transport.ts +56 -17
  144. package/src/track-session.ts +16 -0
  145. package/src/wasm-mfu-reassembler.ts +40 -24
  146. package/test-vectors/catalog/README.md +2 -0
  147. package/test-vectors/catalog/negative/multicast-packet-id-signaling.json +26 -0
  148. package/test-vectors/catalog-validation.json +11 -0
@@ -1,20 +1,3 @@
1
- var __typeError = (msg) => {
2
- throw TypeError(msg);
3
- };
4
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
- var __privateWrapper = (obj, member, setter, getter) => ({
10
- set _(value) {
11
- __privateSet(obj, member, value, setter);
12
- },
13
- get _() {
14
- return __privateGet(obj, member, getter);
15
- }
16
- });
17
-
18
1
  // ../mmt-alta-parse/dist/index.js
19
2
  var MPU_HEADER_SIZE = 8;
20
3
  var MPU_LENGTH_FIELD_SIZE = 2;
@@ -189,7 +172,7 @@ function resolveDeclaredTrailers(payload, declaredEnd, fecType, options) {
189
172
  }
190
173
  return null;
191
174
  }
192
- function planMfuExtraction(mmtpPacket, options) {
175
+ function planMfuExtraction(mmtpPacket, options, parsedHeader) {
193
176
  if (mmtpPacket.length < MMTP_HEADER_SIZE + MPU_HEADER_SIZE2) {
194
177
  return {
195
178
  rejection: {
@@ -199,15 +182,19 @@ function planMfuExtraction(mmtpPacket, options) {
199
182
  };
200
183
  }
201
184
  let header;
202
- try {
203
- header = parseMMTPHeader(mmtpPacket);
204
- } catch (error) {
205
- return {
206
- rejection: {
207
- gate: "mmtp-header-unparseable",
208
- detail: error instanceof Error ? error.message : String(error)
209
- }
210
- };
185
+ if (parsedHeader) {
186
+ header = parsedHeader;
187
+ } else {
188
+ try {
189
+ header = parseMMTPHeader(mmtpPacket);
190
+ } catch (error) {
191
+ return {
192
+ rejection: {
193
+ gate: "mmtp-header-unparseable",
194
+ detail: error instanceof Error ? error.message : String(error)
195
+ }
196
+ };
197
+ }
211
198
  }
212
199
  if (header.packetType !== PACKET_TYPE_MPU) {
213
200
  return {
@@ -260,7 +247,9 @@ function planMfuExtraction(mmtpPacket, options) {
260
247
  return { plan: { header, payload, mpu, fragmentType, layout, trailerLayout } };
261
248
  }
262
249
  function extractMFUPayload(mmtpPacket, options) {
263
- const result = planMfuExtraction(mmtpPacket, options);
250
+ return extractPlannedMfu(mmtpPacket, planMfuExtraction(mmtpPacket, options));
251
+ }
252
+ function extractPlannedMfu(mmtpPacket, result) {
264
253
  if ("rejection" in result)
265
254
  return null;
266
255
  const { header, payload, mpu, fragmentType, layout, trailerLayout } = result.plan;
@@ -419,21 +408,19 @@ var EVICTION_GUARD_DATA_UNITS = 12;
419
408
  function parkedRunHoldsASlot(hasOpenRun, fragmentCount) {
420
409
  return !hasOpenRun && fragmentCount > 0;
421
410
  }
422
- var _buffers, _timeouts, _activeDataUnits, _pendingContinuations, _emitted, _emittedOrder, _completed, _completedOrder, _timeoutMs, _maxBuffered, _nextAdmissionSeq, _MFUReassembler_instances, addAssociatedFragment_fn, parkContinuation_fn, drainPendingContinuations_fn, unopenedParkedRunCount_fn, incompleteRunCount_fn, evictLeastRecentlyAdmitted_fn, releaseSlot_fn, reject_fn, associateDataUnit_fn, tryReassemble_fn, assemble_fn, setTimeout_fn, clearPendingTimeout_fn, armHeadlessPendingTimeout_fn, deletePendingRun_fn, onPendingTimeout_fn, clearTimeout_fn, markEmitted_fn, markCompleted_fn, orderValue_fn, removeActiveDataUnitForBuffer_fn, openRunAt_fn, onTimeout_fn, pendingDebugSamples_fn;
423
411
  var MFUReassembler = class {
424
412
  constructor(options) {
425
- __privateAdd(this, _MFUReassembler_instances);
426
413
  // Fragment buffers and active runs use the authored MFU DU identity.
427
414
  // Continuations may wait in one counter-indexed queue per data-unit address
428
415
  // until FI=1 supplies that identity. The address is transport association
429
416
  // only; the object key stays the intrinsic DU identity.
430
- __privateAdd(this, _buffers, /* @__PURE__ */ new Map());
431
- __privateAdd(this, _timeouts, /* @__PURE__ */ new Map());
432
- __privateAdd(this, _activeDataUnits, /* @__PURE__ */ new Map());
433
- __privateAdd(this, _pendingContinuations, /* @__PURE__ */ new Map());
434
- __privateAdd(this, _emitted, /* @__PURE__ */ new Set());
417
+ this.#buffers = /* @__PURE__ */ new Map();
418
+ this.#timeouts = /* @__PURE__ */ new Map();
419
+ this.#activeDataUnits = /* @__PURE__ */ new Map();
420
+ this.#pendingContinuations = /* @__PURE__ */ new Map();
421
+ this.#emitted = /* @__PURE__ */ new Set();
435
422
  // same sample keys, so FEC-recovered duplicate heads are dropped
436
- __privateAdd(this, _emittedOrder, []);
423
+ this.#emittedOrder = [];
437
424
  // Addresses of emitted data units. A continuation addressed to one of them
438
425
  // is a late copy of a fragment the run already consumed (FEC recovery
439
426
  // supplied it first), so it is rejected as stale rather than parked as a
@@ -443,15 +430,13 @@ var MFUReassembler = class {
443
430
  // completions. (#emitted is a separate, per-engine window: this engine keeps
444
431
  // maxBuffered sample keys where Rust keeps 2x; the completed set does not
445
432
  // derive from it.)
446
- __privateAdd(this, _completed, /* @__PURE__ */ new Set());
447
- __privateAdd(this, _completedOrder, []);
448
- __privateAdd(this, _timeoutMs);
449
- __privateAdd(this, _maxBuffered);
433
+ this.#completed = /* @__PURE__ */ new Set();
434
+ this.#completedOrder = [];
450
435
  /**
451
436
  * Monotonic admission ordinal. Counts slot admissions, never reads a clock,
452
437
  * so eviction order survives on hosts that run `timeoutMs: null`.
453
438
  */
454
- __privateAdd(this, _nextAdmissionSeq, 0);
439
+ this.#nextAdmissionSeq = 0;
455
440
  /** Statistics for debugging */
456
441
  this.stats = {
457
442
  totalFragments: 0,
@@ -468,30 +453,41 @@ var MFUReassembler = class {
468
453
  if (!Number.isInteger(maxBuffered) || maxBuffered <= 0) {
469
454
  throw new Error("[MFUReassembler] maxBuffered must be a positive integer");
470
455
  }
471
- __privateSet(this, _timeoutMs, options.timeoutMs);
472
- __privateSet(this, _maxBuffered, maxBuffered);
473
- }
456
+ this.#timeoutMs = options.timeoutMs;
457
+ this.#maxBuffered = maxBuffered;
458
+ }
459
+ #buffers;
460
+ #timeouts;
461
+ #activeDataUnits;
462
+ #pendingContinuations;
463
+ #emitted;
464
+ #emittedOrder;
465
+ #completed;
466
+ #completedOrder;
467
+ #timeoutMs;
468
+ #maxBuffered;
469
+ #nextAdmissionSeq;
474
470
  /** Update the expiry policy for all retained and future incomplete MFUs. */
475
471
  set timeoutMs(ms) {
476
472
  if (ms !== null && (!Number.isFinite(ms) || ms <= 0)) {
477
473
  throw new Error("[MFUReassembler] timeoutMs must be > 0 (derive from frame duration or FEC config)");
478
474
  }
479
- __privateSet(this, _timeoutMs, ms);
475
+ this.#timeoutMs = ms;
480
476
  const now = Date.now();
481
- for (const [bufferKey, state] of __privateGet(this, _timeouts)) {
477
+ for (const [bufferKey, state] of this.#timeouts) {
482
478
  if (state.timeoutId !== void 0) clearTimeout(state.timeoutId);
483
479
  delete state.timeoutId;
484
480
  if (ms !== null) {
485
- state.timeoutId = setTimeout(() => __privateMethod(this, _MFUReassembler_instances, onTimeout_fn).call(this, bufferKey), Math.max(0, state.startedAt + ms - now));
481
+ state.timeoutId = setTimeout(() => this.#onTimeout(bufferKey), Math.max(0, state.startedAt + ms - now));
486
482
  }
487
483
  }
488
- for (const address of __privateGet(this, _pendingContinuations).keys()) {
489
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
490
- __privateMethod(this, _MFUReassembler_instances, armHeadlessPendingTimeout_fn).call(this, address);
484
+ for (const address of this.#pendingContinuations.keys()) {
485
+ this.#clearPendingTimeout(address);
486
+ this.#armHeadlessPendingTimeout(address);
491
487
  }
492
488
  }
493
489
  get timeoutMs() {
494
- return __privateGet(this, _timeoutMs);
490
+ return this.#timeoutMs;
495
491
  }
496
492
  /**
497
493
  * Add a fragment and return reassembled MFU if complete
@@ -499,10 +495,10 @@ var MFUReassembler = class {
499
495
  addFragment(fragment) {
500
496
  this.stats.totalFragments++;
501
497
  if (!hasValidFragmentHeader(fragment)) {
502
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "invalid-fragment-state", "invalid MFU fragmentation indicator or counter");
498
+ this.#reject("invalid-fragment-state", "invalid MFU fragmentation indicator or counter");
503
499
  }
504
500
  if (fragment.itemId !== void 0 && hasAnySampleIdentityField(fragment)) {
505
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "conflicting-data-unit-identity", "MFU carries timed and non-timed DU identities");
501
+ this.#reject("conflicting-data-unit-identity", "MFU carries timed and non-timed DU identities");
506
502
  }
507
503
  const { mpuSequenceNumber, fragmentationIndicator } = fragment;
508
504
  if (fragmentationIndicator === 0) {
@@ -510,13 +506,13 @@ var MFUReassembler = class {
510
506
  const hasSampleFields = hasAnySampleIdentityField(fragment);
511
507
  const hasItemIdentity = fragment.itemId !== void 0;
512
508
  if (!sampleIdentity && !hasItemIdentity) {
513
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "complete MFU has no intrinsic DU identity");
509
+ this.#reject("missing-data-unit-identity", "complete MFU has no intrinsic DU identity");
514
510
  }
515
511
  if (hasSampleFields && !sampleIdentity) {
516
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "complete MFU has an incomplete sample identity");
512
+ this.#reject("missing-data-unit-identity", "complete MFU has an incomplete sample identity");
517
513
  }
518
514
  if (hasItemIdentity && !isU32(fragment.itemId)) {
519
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "complete MFU has an invalid Item_ID");
515
+ this.#reject("missing-data-unit-identity", "complete MFU has an invalid Item_ID");
520
516
  }
521
517
  this.stats.reassembledMFUs++;
522
518
  return {
@@ -538,67 +534,516 @@ var MFUReassembler = class {
538
534
  }
539
535
  const sequence = fragment.packetSequenceNumber;
540
536
  if (!isU32(sequence)) {
541
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-packet-sequence", "fragmented MFU has no MMTP packet sequence number");
537
+ this.#reject("missing-packet-sequence", "fragmented MFU has no MMTP packet sequence number");
542
538
  }
543
539
  const address = dataUnitAddress(fragment, sequence);
544
540
  if (fragmentationIndicator !== 1) {
545
- const open = __privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address);
541
+ const open = this.#openRunAt(address);
546
542
  if (open === void 0) {
547
- if (__privateGet(this, _completed).has(address)) {
548
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "fragment-counter-mismatch", "MFU continuation names a data unit that already completed");
543
+ if (this.#completed.has(address)) {
544
+ this.#reject("fragment-counter-mismatch", "MFU continuation names a data unit that already completed");
549
545
  }
550
- __privateMethod(this, _MFUReassembler_instances, parkContinuation_fn).call(this, address, fragment);
546
+ this.#parkContinuation(address, fragment);
551
547
  return null;
552
548
  }
553
549
  const [runKey2, active] = open;
554
550
  if (active.nextFragmentCounter > fragment.fragmentCounter) {
555
- __privateMethod(this, _MFUReassembler_instances, parkContinuation_fn).call(this, address, fragment);
551
+ this.#parkContinuation(address, fragment);
556
552
  return null;
557
553
  }
558
554
  if (active.nextFragmentCounter < fragment.fragmentCounter) {
559
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "fragment-counter-mismatch", "MFU continuation is behind the authored fragment-counter countdown");
555
+ this.#reject("fragment-counter-mismatch", "MFU continuation is behind the authored fragment-counter countdown");
560
556
  }
561
- const result2 = __privateMethod(this, _MFUReassembler_instances, addAssociatedFragment_fn).call(this, fragment, runKey2, address);
557
+ const result2 = this.#addAssociatedFragment(fragment, runKey2, address);
562
558
  if (result2 !== null) return result2;
563
- return __privateMethod(this, _MFUReassembler_instances, drainPendingContinuations_fn).call(this, address, runKey2);
559
+ return this.#drainPendingContinuations(address, runKey2);
564
560
  }
565
561
  const runKey = objectKey(fragment);
566
- if ((hasSampleIdentity(fragment) || isU32(fragment.itemId)) && __privateGet(this, _emitted).has(runKey)) {
562
+ if ((hasSampleIdentity(fragment) || isU32(fragment.itemId)) && this.#emitted.has(runKey)) {
567
563
  return null;
568
564
  }
569
- if (__privateGet(this, _activeDataUnits).has(runKey)) {
570
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "overlapping-data-unit", "fragmented MFU overlaps an active data unit");
565
+ if (this.#activeDataUnits.has(runKey)) {
566
+ this.#reject("overlapping-data-unit", "fragmented MFU overlaps an active data unit");
571
567
  }
572
- if (__privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0) {
573
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "overlapping-data-unit", "fragmented MFU opens on an address held by an open data unit");
568
+ if (this.#openRunAt(address) !== void 0) {
569
+ this.#reject("overlapping-data-unit", "fragmented MFU opens on an address held by an open data unit");
574
570
  }
575
- const pending = __privateGet(this, _pendingContinuations).get(address);
571
+ const pending = this.#pendingContinuations.get(address);
576
572
  if (pending !== void 0 && [...pending.fragments.keys()].some((counter) => counter >= fragment.fragmentCounter)) {
577
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "fragment-counter-mismatch", "queued continuation counter is not below the authored first-fragment counter");
573
+ this.#reject(
574
+ "fragment-counter-mismatch",
575
+ "queued continuation counter is not below the authored first-fragment counter"
576
+ );
578
577
  }
579
- const result = __privateMethod(this, _MFUReassembler_instances, addAssociatedFragment_fn).call(this, fragment, runKey, address, pending?.startedAt);
580
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
578
+ const result = this.#addAssociatedFragment(fragment, runKey, address, pending?.startedAt);
579
+ this.#clearPendingTimeout(address);
581
580
  if (result !== null) return result;
582
- return __privateMethod(this, _MFUReassembler_instances, drainPendingContinuations_fn).call(this, address, runKey);
581
+ return this.#drainPendingContinuations(address, runKey);
582
+ }
583
+ #addAssociatedFragment(fragment, runKey, address, startedAt) {
584
+ const association = this.#associateDataUnit(fragment, runKey, address);
585
+ const { bufferKey, first, terminal } = association;
586
+ let fragments = this.#buffers.get(bufferKey);
587
+ if (first) {
588
+ if (fragments !== void 0) {
589
+ this.#reject("overlapping-data-unit", "intrinsic data-unit buffer already exists");
590
+ }
591
+ fragments = [];
592
+ this.#buffers.set(bufferKey, fragments);
593
+ } else if (fragments === void 0) {
594
+ throw new Error("[MFUReassembler] active data unit has no fragment buffer");
595
+ }
596
+ if (first) this.#setTimeout(bufferKey, startedAt);
597
+ fragments.push(fragment);
598
+ if (!terminal) return null;
599
+ const result = this.#tryReassemble(bufferKey);
600
+ this.#activeDataUnits.delete(runKey);
601
+ if (result === null) {
602
+ this.#clearTimeout(bufferKey);
603
+ this.#buffers.delete(bufferKey);
604
+ this.#reject("incomplete-terminal-fragment", "validated terminal fragment did not complete its data unit");
605
+ }
606
+ this.#markCompleted(address);
607
+ return result;
608
+ }
609
+ #parkContinuation(address, fragment) {
610
+ let pending = this.#pendingContinuations.get(address);
611
+ const open = this.#openRunAt(address);
612
+ const alreadyCharged = open !== void 0 || parkedRunHoldsASlot(open !== void 0, pending?.fragments.size ?? 0);
613
+ if (!alreadyCharged && this.#incompleteRunCount() >= this.#maxBuffered) {
614
+ if (!this.#evictLeastRecentlyAdmitted()) {
615
+ this.#reject("active-data-unit-limit", "active MFU data-unit limit reached");
616
+ }
617
+ }
618
+ let armAfterInsert = false;
619
+ if (pending === void 0) {
620
+ pending = {
621
+ fragments: /* @__PURE__ */ new Map(),
622
+ startedAt: Date.now(),
623
+ admissionSeq: open !== void 0 ? open[1].admissionSeq : this.#nextAdmissionSeq++
624
+ };
625
+ this.#pendingContinuations.set(address, pending);
626
+ armAfterInsert = true;
627
+ } else if (!alreadyCharged) {
628
+ pending.admissionSeq = this.#nextAdmissionSeq++;
629
+ pending.startedAt = Date.now();
630
+ this.#clearPendingTimeout(address);
631
+ armAfterInsert = true;
632
+ }
633
+ if (pending.fragments.has(fragment.fragmentCounter)) {
634
+ this.#reject("fragment-counter-mismatch", "duplicate queued continuation counter would replace retained media");
635
+ }
636
+ pending.fragments.set(fragment.fragmentCounter, fragment);
637
+ if (armAfterInsert) this.#armHeadlessPendingTimeout(address);
638
+ }
639
+ #drainPendingContinuations(address, runKey) {
640
+ while (true) {
641
+ const active = this.#activeDataUnits.get(runKey);
642
+ const pending = this.#pendingContinuations.get(address);
643
+ if (active === void 0) {
644
+ if (pending?.fragments.size === 0) this.#deletePendingRun(address);
645
+ return null;
646
+ }
647
+ const fragment = pending?.fragments.get(active.nextFragmentCounter);
648
+ if (fragment === void 0) {
649
+ if (pending?.fragments.size === 0) this.#deletePendingRun(address);
650
+ return null;
651
+ }
652
+ pending.fragments.delete(active.nextFragmentCounter);
653
+ if (pending.fragments.size === 0) this.#deletePendingRun(address);
654
+ const result = this.#addAssociatedFragment(fragment, runKey, address);
655
+ if (result !== null) {
656
+ return result;
657
+ }
658
+ }
659
+ }
660
+ /** Parked data units that hold a `maxBuffered` slot of their own. Membership
661
+ * is `parkedRunHoldsASlot`, the same rule #evictLeastRecentlyAdmitted offers
662
+ * as candidates, so nothing can be counted here that eviction cannot reclaim.
663
+ *
664
+ * One site, two callers — `#incompleteRunCount` and `pendingCount` — mirroring
665
+ * Rust's `unopened_parked_run_count`, which `incomplete_run_count` and
666
+ * `buffered_count` both delegate to. Both callers used to inline this loop
667
+ * verbatim, so converging the *rule* moved one copy and left the other as the
668
+ * divergence (Ally's review of #400). The rule can no longer drift now that
669
+ * every site routes through `parkedRunHoldsASlot`; keeping the loop itself at
670
+ * one site is what stops the scaffolding drifting the next time this question
671
+ * grows an argument. */
672
+ #unopenedParkedRunCount() {
673
+ let count = 0;
674
+ for (const [address, pending] of this.#pendingContinuations) {
675
+ if (parkedRunHoldsASlot(this.#openRunAt(address) !== void 0, pending.fragments.size)) {
676
+ count++;
677
+ }
678
+ }
679
+ return count;
680
+ }
681
+ /** Every open run plus every headless parked data unit that holds a slot,
682
+ * one slot each. This is the quantity maxBuffered bounds. The parked term is
683
+ * `#unopenedParkedRunCount`, whose membership is `parkedRunHoldsASlot` — the
684
+ * same rule #evictLeastRecentlyAdmitted offers as candidates, so nothing can
685
+ * be counted here that eviction cannot reclaim. */
686
+ #incompleteRunCount() {
687
+ return this.#activeDataUnits.size + this.#unopenedParkedRunCount();
688
+ }
689
+ /**
690
+ * Drop the incomplete data unit admitted longest ago so a newly arriving one
691
+ * can take its slot. Selection is by monotonic admission ordinal, never by
692
+ * wall clock, so this is a bounded-capacity replacement policy and not a
693
+ * timeout: it behaves identically on hosts that never drive `cleanup_timeouts`
694
+ * (`timeoutMs: null`). Only incomplete data units are candidates — a data
695
+ * unit that completed has already left both maps.
696
+ *
697
+ * Returns false only if no slot is charged, which cannot happen at the bound
698
+ * (`maxBuffered` is a positive integer); admission then falls back to the
699
+ * fail-closed `active-data-unit-limit` reject, which stays a stable host code.
700
+ * That holds because the candidate set here is built from the same
701
+ * `parkedRunHoldsASlot` rule `#incompleteRunCount` counts by, so reaching the
702
+ * bound implies at least one candidate. Filtering one and not the other would
703
+ * make the claim false: a queue counted but not offered charges a slot no
704
+ * eviction can reclaim, and the receiver refuses every later data unit
705
+ * permanently.
706
+ */
707
+ #evictLeastRecentlyAdmitted() {
708
+ let victimAddress;
709
+ let victimSeq = Number.POSITIVE_INFINITY;
710
+ for (const [, active] of this.#activeDataUnits) {
711
+ if (active.admissionSeq < victimSeq) {
712
+ victimSeq = active.admissionSeq;
713
+ victimAddress = active.address;
714
+ }
715
+ }
716
+ for (const [address, pending] of this.#pendingContinuations) {
717
+ if (!parkedRunHoldsASlot(this.#openRunAt(address) !== void 0, pending.fragments.size)) {
718
+ continue;
719
+ }
720
+ if (pending.admissionSeq < victimSeq) {
721
+ victimSeq = pending.admissionSeq;
722
+ victimAddress = address;
723
+ }
724
+ }
725
+ if (victimAddress === void 0) return false;
726
+ this.#releaseSlot(victimAddress);
727
+ this.stats.evictedMemoryLimit++;
728
+ return true;
729
+ }
730
+ /**
731
+ * Release every form of state one address holds: its open run, that run's
732
+ * buffered fragments and deadline, and its continuation queue. The address is
733
+ * deliberately not marked completed — the data unit never emitted, so a later
734
+ * fragment of it may open a fresh slot rather than being rejected as stale.
735
+ */
736
+ #releaseSlot(address) {
737
+ const open = this.#openRunAt(address);
738
+ if (open !== void 0) {
739
+ const [runKey, active] = open;
740
+ this.#activeDataUnits.delete(runKey);
741
+ this.#clearTimeout(active.bufferKey);
742
+ this.#buffers.delete(active.bufferKey);
743
+ }
744
+ this.#deletePendingRun(address);
745
+ }
746
+ #reject(code, message) {
747
+ this.stats.rejectedFragments++;
748
+ throw new MfuReassemblyError(code, message);
749
+ }
750
+ #associateDataUnit(fragment, runKey, address) {
751
+ const fi = fragment.fragmentationIndicator;
752
+ if (fi === 1) {
753
+ const parked = this.#pendingContinuations.get(address);
754
+ const inheritsParkedSlot = parkedRunHoldsASlot(
755
+ this.#openRunAt(address) !== void 0,
756
+ parked?.fragments.size ?? 0
757
+ );
758
+ const sampleIdentity = hasSampleIdentity(fragment);
759
+ const hasSampleFields = hasAnySampleIdentityField(fragment);
760
+ const hasItemIdentity = fragment.itemId !== void 0;
761
+ if (sampleIdentity && hasItemIdentity) {
762
+ this.#reject("conflicting-data-unit-identity", "fragmented MFU carries timed and non-timed DU identities");
763
+ }
764
+ if (!sampleIdentity && !hasItemIdentity) {
765
+ this.#reject("missing-data-unit-identity", "first fragment has no intrinsic DU identity");
766
+ }
767
+ if (hasSampleFields && !sampleIdentity) {
768
+ this.#reject("missing-data-unit-identity", "first fragment has an incomplete sample identity");
769
+ }
770
+ if (hasItemIdentity && !isU32(fragment.itemId)) {
771
+ this.#reject("missing-data-unit-identity", "first fragment has an invalid Item_ID");
772
+ }
773
+ const bufferKey = objectKey(fragment);
774
+ if (this.#buffers.has(bufferKey)) {
775
+ this.#reject("overlapping-data-unit", "intrinsic data-unit buffer already exists");
776
+ }
777
+ let sampleEnd = null;
778
+ if (sampleIdentity) {
779
+ sampleEnd = checkedSampleEnd(fragment.sampleOffset, fragment.data.byteLength);
780
+ if (sampleEnd === null) this.#reject("offset-overflow", "sample byte offset overflow");
781
+ }
782
+ if (!inheritsParkedSlot && this.#incompleteRunCount() >= this.#maxBuffered) {
783
+ if (!this.#evictLeastRecentlyAdmitted()) {
784
+ this.#reject("active-data-unit-limit", "active MFU data-unit limit reached");
785
+ }
786
+ }
787
+ const admissionSeq = inheritsParkedSlot ? parked.admissionSeq : this.#nextAdmissionSeq++;
788
+ let active2;
789
+ if (sampleIdentity) {
790
+ active2 = {
791
+ address,
792
+ kind: "sample",
793
+ bufferKey,
794
+ nextFragmentCounter: fragment.fragmentCounter - 1,
795
+ admissionSeq,
796
+ movieFragmentSequenceNumber: fragment.movieFragmentSequenceNumber,
797
+ sampleNumber: fragment.sampleNumber,
798
+ nextOffset: sampleEnd,
799
+ ...fragment.catalogTrackName === void 0 ? {} : { catalogTrackName: fragment.catalogTrackName }
800
+ };
801
+ } else {
802
+ active2 = {
803
+ address,
804
+ kind: "item",
805
+ bufferKey,
806
+ nextFragmentCounter: fragment.fragmentCounter - 1,
807
+ admissionSeq,
808
+ itemId: fragment.itemId,
809
+ ...fragment.catalogTrackName === void 0 ? {} : { catalogTrackName: fragment.catalogTrackName }
810
+ };
811
+ }
812
+ this.#activeDataUnits.set(objectKey(fragment), active2);
813
+ return { bufferKey, first: true, terminal: false };
814
+ }
815
+ const active = this.#activeDataUnits.get(runKey);
816
+ if (active === void 0) {
817
+ throw new Error("[MFUReassembler] continuation was not parked before association");
818
+ }
819
+ if (fragment.fragmentCounter !== active.nextFragmentCounter) {
820
+ this.#reject(
821
+ "fragment-counter-mismatch",
822
+ "MFU continuation does not follow the authored fragment-counter countdown"
823
+ );
824
+ }
825
+ if (active.kind === "sample") {
826
+ if (fragment.itemId !== void 0) {
827
+ this.#reject("data-unit-identity-mismatch", "sample continuation carries an Item_ID");
828
+ }
829
+ if (hasAnySampleIdentityField(fragment) && (!hasSampleIdentity(fragment) || fragment.movieFragmentSequenceNumber !== active.movieFragmentSequenceNumber || fragment.sampleNumber !== active.sampleNumber || fragment.sampleOffset !== active.nextOffset)) {
830
+ this.#reject("data-unit-identity-mismatch", "sample continuation identity does not match");
831
+ }
832
+ const nextOffset = checkedSampleEnd(active.nextOffset, fragment.data.byteLength);
833
+ if (nextOffset === null) this.#reject("offset-overflow", "sample byte offset overflow");
834
+ fragment.movieFragmentSequenceNumber = active.movieFragmentSequenceNumber;
835
+ fragment.sampleNumber = active.sampleNumber;
836
+ fragment.sampleOffset = active.nextOffset;
837
+ if (fi === 2) {
838
+ active.nextOffset = nextOffset;
839
+ active.nextFragmentCounter = fragment.fragmentCounter - 1;
840
+ }
841
+ } else {
842
+ if (hasAnySampleIdentityField(fragment) || fragment.itemId !== void 0 && fragment.itemId !== active.itemId) {
843
+ this.#reject("data-unit-identity-mismatch", "Item_ID continuation identity does not match");
844
+ }
845
+ fragment.itemId = active.itemId;
846
+ if (fi === 2) active.nextFragmentCounter = fragment.fragmentCounter - 1;
847
+ }
848
+ return {
849
+ bufferKey: active.bufferKey,
850
+ first: false,
851
+ terminal: fi === 3
852
+ };
853
+ }
854
+ #tryReassemble(bufferKey) {
855
+ const fragments = this.#buffers.get(bufferKey);
856
+ if (!fragments || fragments.length === 0) {
857
+ return null;
858
+ }
859
+ const sorted = [...fragments].sort((a, b) => this.#orderValue(a) - this.#orderValue(b));
860
+ if (sorted[0].fragmentationIndicator !== 1 || sorted[sorted.length - 1].fragmentationIndicator !== 3) {
861
+ return null;
862
+ }
863
+ if (sorted.slice(1, -1).some((fragment) => fragment.fragmentationIndicator !== 2)) {
864
+ return null;
865
+ }
866
+ for (let i = 1; i < sorted.length; i++) {
867
+ if (this.#orderValue(sorted[i]) !== this.#orderValue(sorted[i - 1]) + 1) {
868
+ return null;
869
+ }
870
+ }
871
+ if (ordersByOffset(sorted)) {
872
+ for (let i = 1; i < sorted.length; i++) {
873
+ const previous = sorted[i - 1];
874
+ if (sorted[i].sampleOffset !== previous.sampleOffset + previous.data.byteLength) {
875
+ return null;
876
+ }
877
+ }
878
+ }
879
+ fragments.splice(0, fragments.length, ...sorted);
880
+ return this.#assemble(bufferKey, fragments);
881
+ }
882
+ #assemble(bufferKey, fragments) {
883
+ const totalSize = fragments.reduce((sum, f) => sum + f.data.length, 0);
884
+ const data = new Uint8Array(totalSize);
885
+ let offset = 0;
886
+ for (const fragment of fragments) {
887
+ data.set(fragment.data, offset);
888
+ offset += fragment.data.length;
889
+ }
890
+ this.#clearTimeout(bufferKey);
891
+ this.#buffers.delete(bufferKey);
892
+ this.#markEmitted(bufferKey);
893
+ this.stats.reassembledMFUs++;
894
+ return {
895
+ mpuSequenceNumber: fragments[0].mpuSequenceNumber,
896
+ packetSequenceNumber: fragments[0].packetSequenceNumber,
897
+ ...assembledPacketSequences(fragments),
898
+ data,
899
+ timestamp: fragments[0].timestamp,
900
+ fragmentCount: fragments.length,
901
+ rapFlag: fragments[0].rapFlag,
902
+ priority: fragments.find((f) => f.priority !== void 0)?.priority,
903
+ sampleNumber: fragments[0].sampleNumber,
904
+ movieFragmentSequenceNumber: fragments[0].movieFragmentSequenceNumber,
905
+ sampleOffset: fragments[0].sampleOffset,
906
+ source: fragments[0].source,
907
+ deliveryProvenance: deliveryProvenanceOf(fragments),
908
+ catalogTrackName: fragments[0].catalogTrackName
909
+ };
910
+ }
911
+ #setTimeout(bufferKey, startedAt = Date.now()) {
912
+ const state = { startedAt };
913
+ if (this.#timeoutMs !== null) {
914
+ state.timeoutId = setTimeout(
915
+ () => this.#onTimeout(bufferKey),
916
+ Math.max(0, startedAt + this.#timeoutMs - Date.now())
917
+ );
918
+ }
919
+ this.#timeouts.set(bufferKey, state);
920
+ }
921
+ #clearPendingTimeout(address) {
922
+ const pending = this.#pendingContinuations.get(address);
923
+ if (pending?.timeoutId !== void 0) clearTimeout(pending.timeoutId);
924
+ if (pending !== void 0) {
925
+ delete pending.timeoutId;
926
+ }
927
+ }
928
+ /**
929
+ * A queue whose run closed without completing is headless again and expires
930
+ * from its own first arrival, exactly as it would had the head never come.
931
+ *
932
+ * An empty queue is not armed: it holds no data unit that could fail to
933
+ * complete, so there is nothing for a deadline to expire. Every arming site
934
+ * reaches this function, which takes that test through `parkedRunHoldsASlot`
935
+ * rather than filtering on the open run alone, which is how this engine
936
+ * reaches Rust's answer: its
937
+ * `next_timeout_at` derives candidate deadlines from the queue's live
938
+ * entries, so an empty queue yields none (BLO-32420).
939
+ */
940
+ #armHeadlessPendingTimeout(address) {
941
+ const pending = this.#pendingContinuations.get(address);
942
+ if (pending === void 0 || pending.timeoutId !== void 0 || this.#timeoutMs === null) return;
943
+ if (!parkedRunHoldsASlot(this.#openRunAt(address) !== void 0, pending.fragments.size)) return;
944
+ pending.timeoutId = setTimeout(
945
+ () => this.#onPendingTimeout(address),
946
+ Math.max(0, pending.startedAt + this.#timeoutMs - Date.now())
947
+ );
948
+ }
949
+ #deletePendingRun(address) {
950
+ this.#clearPendingTimeout(address);
951
+ this.#pendingContinuations.delete(address);
952
+ }
953
+ #onPendingTimeout(address) {
954
+ const pending = this.#pendingContinuations.get(address);
955
+ if (pending === void 0) return;
956
+ if (!parkedRunHoldsASlot(this.#openRunAt(address) !== void 0, pending.fragments.size)) {
957
+ this.#clearPendingTimeout(address);
958
+ return;
959
+ }
960
+ this.stats.timedOutMFUs++;
961
+ console.warn(
962
+ `[MfuReassembler] timeout: run=${address} fragments=${pending.fragments.size} (continuations awaiting first fragment, discarded)`
963
+ );
964
+ this.#pendingContinuations.delete(address);
965
+ }
966
+ #clearTimeout(bufferKey) {
967
+ const state = this.#timeouts.get(bufferKey);
968
+ if (state !== void 0) {
969
+ if (state.timeoutId !== void 0) clearTimeout(state.timeoutId);
970
+ this.#timeouts.delete(bufferKey);
971
+ }
972
+ }
973
+ #markEmitted(bufferKey) {
974
+ if (this.#emitted.has(bufferKey)) return;
975
+ this.#emitted.add(bufferKey);
976
+ this.#emittedOrder.push(bufferKey);
977
+ while (this.#emittedOrder.length > this.#maxBuffered) {
978
+ const oldest = this.#emittedOrder.shift();
979
+ if (oldest !== void 0) this.#emitted.delete(oldest);
980
+ }
981
+ }
982
+ #markCompleted(address) {
983
+ if (this.#completed.has(address)) return;
984
+ this.#completed.add(address);
985
+ this.#completedOrder.push(address);
986
+ const cap = this.#maxBuffered * COMPLETED_ADDRESS_WINDOW_MULTIPLE;
987
+ while (this.#completedOrder.length > cap) {
988
+ const oldest = this.#completedOrder.shift();
989
+ if (oldest !== void 0) this.#completed.delete(oldest);
990
+ }
991
+ }
992
+ #orderValue(fragment) {
993
+ return fragmentCounterOrder(fragment);
994
+ }
995
+ /** Close the run behind an expired buffer. Early arrivals parked under its
996
+ * address stay, now headless, and expire from their own arrival. */
997
+ #removeActiveDataUnitForBuffer(bufferKey) {
998
+ for (const [objectKey2, active] of this.#activeDataUnits) {
999
+ if (active.bufferKey === bufferKey) {
1000
+ this.#activeDataUnits.delete(objectKey2);
1001
+ this.#armHeadlessPendingTimeout(active.address);
1002
+ }
1003
+ }
1004
+ }
1005
+ /** The one open run holding this address, with its object key. Admission
1006
+ * rejects a second head on an open address, so at most one run can match. */
1007
+ #openRunAt(address) {
1008
+ for (const entry of this.#activeDataUnits) {
1009
+ if (entry[1].address === address) return entry;
1010
+ }
1011
+ return void 0;
1012
+ }
1013
+ #onTimeout(bufferKey) {
1014
+ const fragments = this.#buffers.get(bufferKey);
1015
+ if (!fragments || fragments.length === 0) {
1016
+ this.#buffers.delete(bufferKey);
1017
+ this.#timeouts.delete(bufferKey);
1018
+ this.#removeActiveDataUnitForBuffer(bufferKey);
1019
+ this.stats.timedOutMFUs++;
1020
+ console.warn(`[MfuReassembler] timeout: key=${bufferKey} fragments=0 (no data buffered)`);
1021
+ return;
1022
+ }
1023
+ this.stats.timedOutMFUs++;
1024
+ console.warn(`[MfuReassembler] timeout: key=${bufferKey} fragments=${fragments.length} (partial, discarded)`);
1025
+ this.#buffers.delete(bufferKey);
1026
+ this.#timeouts.delete(bufferKey);
1027
+ this.#removeActiveDataUnitForBuffer(bufferKey);
583
1028
  }
584
1029
  /**
585
1030
  * Flush all buffered fragments
586
1031
  */
587
1032
  flush() {
588
- for (const key of __privateGet(this, _buffers).keys()) {
589
- __privateMethod(this, _MFUReassembler_instances, clearTimeout_fn).call(this, key);
1033
+ for (const key of this.#buffers.keys()) {
1034
+ this.#clearTimeout(key);
590
1035
  }
591
- __privateGet(this, _buffers).clear();
592
- __privateGet(this, _timeouts).clear();
593
- __privateGet(this, _activeDataUnits).clear();
594
- for (const address of __privateGet(this, _pendingContinuations).keys()) {
595
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
1036
+ this.#buffers.clear();
1037
+ this.#timeouts.clear();
1038
+ this.#activeDataUnits.clear();
1039
+ for (const address of this.#pendingContinuations.keys()) {
1040
+ this.#clearPendingTimeout(address);
596
1041
  }
597
- __privateGet(this, _pendingContinuations).clear();
598
- __privateGet(this, _emitted).clear();
599
- __privateGet(this, _emittedOrder).length = 0;
600
- __privateGet(this, _completed).clear();
601
- __privateGet(this, _completedOrder).length = 0;
1042
+ this.#pendingContinuations.clear();
1043
+ this.#emitted.clear();
1044
+ this.#emittedOrder.length = 0;
1045
+ this.#completed.clear();
1046
+ this.#completedOrder.length = 0;
602
1047
  }
603
1048
  /** Drop one catalog stream's incomplete and dedup state. */
604
1049
  resetStream(catalogTrackName) {
@@ -611,34 +1056,34 @@ var MFUReassembler = class {
611
1056
  }
612
1057
  };
613
1058
  const discarded = /* @__PURE__ */ new Set();
614
- for (const [key, fragments] of __privateGet(this, _buffers)) {
1059
+ for (const [key, fragments] of this.#buffers) {
615
1060
  if (fragments.some((fragment) => fragment.catalogTrackName === catalogTrackName)) {
616
1061
  discarded.add(key);
617
1062
  }
618
1063
  }
619
- for (const [objectKey2, active] of __privateGet(this, _activeDataUnits)) {
1064
+ for (const [objectKey2, active] of this.#activeDataUnits) {
620
1065
  if (active.catalogTrackName === catalogTrackName) {
621
1066
  discarded.add(active.bufferKey);
622
- __privateGet(this, _activeDataUnits).delete(objectKey2);
1067
+ this.#activeDataUnits.delete(objectKey2);
623
1068
  }
624
1069
  }
625
- for (const [address, pending] of __privateGet(this, _pendingContinuations)) {
1070
+ for (const [address, pending] of this.#pendingContinuations) {
626
1071
  if ([...pending.fragments.values()].some((fragment) => fragment.catalogTrackName === catalogTrackName) || belongsToStream(address))
627
- __privateMethod(this, _MFUReassembler_instances, deletePendingRun_fn).call(this, address);
1072
+ this.#deletePendingRun(address);
628
1073
  }
629
- for (const key of __privateGet(this, _emitted)) {
1074
+ for (const key of this.#emitted) {
630
1075
  if (belongsToStream(key)) discarded.add(key);
631
1076
  }
632
1077
  for (const key of discarded) {
633
- __privateMethod(this, _MFUReassembler_instances, clearTimeout_fn).call(this, key);
634
- __privateGet(this, _buffers).delete(key);
635
- __privateGet(this, _emitted).delete(key);
1078
+ this.#clearTimeout(key);
1079
+ this.#buffers.delete(key);
1080
+ this.#emitted.delete(key);
636
1081
  }
637
- __privateSet(this, _emittedOrder, __privateGet(this, _emittedOrder).filter((key) => !discarded.has(key)));
638
- for (const address of __privateGet(this, _completed)) {
639
- if (belongsToStream(address)) __privateGet(this, _completed).delete(address);
1082
+ this.#emittedOrder = this.#emittedOrder.filter((key) => !discarded.has(key));
1083
+ for (const address of this.#completed) {
1084
+ if (belongsToStream(address)) this.#completed.delete(address);
640
1085
  }
641
- __privateSet(this, _completedOrder, __privateGet(this, _completedOrder).filter((address) => __privateGet(this, _completed).has(address)));
1086
+ this.#completedOrder = this.#completedOrder.filter((address) => this.#completed.has(address));
642
1087
  }
643
1088
  /** Reset all state — flush buffers and zero stats */
644
1089
  reset() {
@@ -670,533 +1115,78 @@ var MFUReassembler = class {
670
1115
  * routing both through one method makes structural rather than conventional.
671
1116
  */
672
1117
  get pendingCount() {
673
- return __privateGet(this, _buffers).size + __privateMethod(this, _MFUReassembler_instances, unopenedParkedRunCount_fn).call(this);
1118
+ return this.#buffers.size + this.#unopenedParkedRunCount();
674
1119
  }
675
1120
  /**
676
1121
  * Get current buffer status
677
1122
  */
678
1123
  getStatus() {
679
1124
  let pendingFragments = 0;
680
- for (const fragments of __privateGet(this, _buffers).values()) {
1125
+ for (const fragments of this.#buffers.values()) {
681
1126
  pendingFragments += fragments.length;
682
1127
  }
683
- for (const pending of __privateGet(this, _pendingContinuations).values()) {
1128
+ for (const pending of this.#pendingContinuations.values()) {
684
1129
  pendingFragments += pending.fragments.size;
685
1130
  }
686
1131
  return {
687
1132
  bufferedMFUs: this.pendingCount,
688
1133
  pendingFragments,
689
- pendingSamples: __privateMethod(this, _MFUReassembler_instances, pendingDebugSamples_fn).call(this),
1134
+ pendingSamples: this.#pendingDebugSamples(),
690
1135
  stats: { ...this.stats }
691
1136
  };
692
1137
  }
693
- };
694
- _buffers = new WeakMap();
695
- _timeouts = new WeakMap();
696
- _activeDataUnits = new WeakMap();
697
- _pendingContinuations = new WeakMap();
698
- _emitted = new WeakMap();
699
- _emittedOrder = new WeakMap();
700
- _completed = new WeakMap();
701
- _completedOrder = new WeakMap();
702
- _timeoutMs = new WeakMap();
703
- _maxBuffered = new WeakMap();
704
- _nextAdmissionSeq = new WeakMap();
705
- _MFUReassembler_instances = new WeakSet();
706
- addAssociatedFragment_fn = function(fragment, runKey, address, startedAt) {
707
- const association = __privateMethod(this, _MFUReassembler_instances, associateDataUnit_fn).call(this, fragment, runKey, address);
708
- const { bufferKey, first, terminal } = association;
709
- let fragments = __privateGet(this, _buffers).get(bufferKey);
710
- if (first) {
711
- if (fragments !== void 0) {
712
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "overlapping-data-unit", "intrinsic data-unit buffer already exists");
713
- }
714
- fragments = [];
715
- __privateGet(this, _buffers).set(bufferKey, fragments);
716
- } else if (fragments === void 0) {
717
- throw new Error("[MFUReassembler] active data unit has no fragment buffer");
718
- }
719
- if (first) __privateMethod(this, _MFUReassembler_instances, setTimeout_fn).call(this, bufferKey, startedAt);
720
- fragments.push(fragment);
721
- if (!terminal) return null;
722
- const result = __privateMethod(this, _MFUReassembler_instances, tryReassemble_fn).call(this, bufferKey);
723
- __privateGet(this, _activeDataUnits).delete(runKey);
724
- if (result === null) {
725
- __privateMethod(this, _MFUReassembler_instances, clearTimeout_fn).call(this, bufferKey);
726
- __privateGet(this, _buffers).delete(bufferKey);
727
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "incomplete-terminal-fragment", "validated terminal fragment did not complete its data unit");
728
- }
729
- __privateMethod(this, _MFUReassembler_instances, markCompleted_fn).call(this, address);
730
- return result;
731
- };
732
- parkContinuation_fn = function(address, fragment) {
733
- let pending = __privateGet(this, _pendingContinuations).get(address);
734
- const open = __privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address);
735
- const alreadyCharged = open !== void 0 || parkedRunHoldsASlot(open !== void 0, pending?.fragments.size ?? 0);
736
- if (!alreadyCharged && __privateMethod(this, _MFUReassembler_instances, incompleteRunCount_fn).call(this) >= __privateGet(this, _maxBuffered)) {
737
- if (!__privateMethod(this, _MFUReassembler_instances, evictLeastRecentlyAdmitted_fn).call(this)) {
738
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "active-data-unit-limit", "active MFU data-unit limit reached");
739
- }
740
- }
741
- let armAfterInsert = false;
742
- if (pending === void 0) {
743
- pending = {
744
- fragments: /* @__PURE__ */ new Map(),
745
- startedAt: Date.now(),
746
- admissionSeq: open !== void 0 ? open[1].admissionSeq : __privateWrapper(this, _nextAdmissionSeq)._++
747
- };
748
- __privateGet(this, _pendingContinuations).set(address, pending);
749
- armAfterInsert = true;
750
- } else if (!alreadyCharged) {
751
- pending.admissionSeq = __privateWrapper(this, _nextAdmissionSeq)._++;
752
- pending.startedAt = Date.now();
753
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
754
- armAfterInsert = true;
755
- }
756
- if (pending.fragments.has(fragment.fragmentCounter)) {
757
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "fragment-counter-mismatch", "duplicate queued continuation counter would replace retained media");
758
- }
759
- pending.fragments.set(fragment.fragmentCounter, fragment);
760
- if (armAfterInsert) __privateMethod(this, _MFUReassembler_instances, armHeadlessPendingTimeout_fn).call(this, address);
761
- };
762
- drainPendingContinuations_fn = function(address, runKey) {
763
- while (true) {
764
- const active = __privateGet(this, _activeDataUnits).get(runKey);
765
- const pending = __privateGet(this, _pendingContinuations).get(address);
766
- if (active === void 0) {
767
- if (pending?.fragments.size === 0) __privateMethod(this, _MFUReassembler_instances, deletePendingRun_fn).call(this, address);
768
- return null;
769
- }
770
- const fragment = pending?.fragments.get(active.nextFragmentCounter);
771
- if (fragment === void 0) {
772
- if (pending?.fragments.size === 0) __privateMethod(this, _MFUReassembler_instances, deletePendingRun_fn).call(this, address);
773
- return null;
774
- }
775
- pending.fragments.delete(active.nextFragmentCounter);
776
- if (pending.fragments.size === 0) __privateMethod(this, _MFUReassembler_instances, deletePendingRun_fn).call(this, address);
777
- const result = __privateMethod(this, _MFUReassembler_instances, addAssociatedFragment_fn).call(this, fragment, runKey, address);
778
- if (result !== null) {
779
- return result;
780
- }
781
- }
782
- };
783
- /** Parked data units that hold a `maxBuffered` slot of their own. Membership
784
- * is `parkedRunHoldsASlot`, the same rule #evictLeastRecentlyAdmitted offers
785
- * as candidates, so nothing can be counted here that eviction cannot reclaim.
786
- *
787
- * One site, two callers — `#incompleteRunCount` and `pendingCount` — mirroring
788
- * Rust's `unopened_parked_run_count`, which `incomplete_run_count` and
789
- * `buffered_count` both delegate to. Both callers used to inline this loop
790
- * verbatim, so converging the *rule* moved one copy and left the other as the
791
- * divergence (Ally's review of #400). The rule can no longer drift now that
792
- * every site routes through `parkedRunHoldsASlot`; keeping the loop itself at
793
- * one site is what stops the scaffolding drifting the next time this question
794
- * grows an argument. */
795
- unopenedParkedRunCount_fn = function() {
796
- let count = 0;
797
- for (const [address, pending] of __privateGet(this, _pendingContinuations)) {
798
- if (parkedRunHoldsASlot(__privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0, pending.fragments.size)) {
799
- count++;
800
- }
801
- }
802
- return count;
803
- };
804
- /** Every open run plus every headless parked data unit that holds a slot,
805
- * one slot each. This is the quantity maxBuffered bounds. The parked term is
806
- * `#unopenedParkedRunCount`, whose membership is `parkedRunHoldsASlot` — the
807
- * same rule #evictLeastRecentlyAdmitted offers as candidates, so nothing can
808
- * be counted here that eviction cannot reclaim. */
809
- incompleteRunCount_fn = function() {
810
- return __privateGet(this, _activeDataUnits).size + __privateMethod(this, _MFUReassembler_instances, unopenedParkedRunCount_fn).call(this);
811
- };
812
- /**
813
- * Drop the incomplete data unit admitted longest ago so a newly arriving one
814
- * can take its slot. Selection is by monotonic admission ordinal, never by
815
- * wall clock, so this is a bounded-capacity replacement policy and not a
816
- * timeout: it behaves identically on hosts that never drive `cleanup_timeouts`
817
- * (`timeoutMs: null`). Only incomplete data units are candidates — a data
818
- * unit that completed has already left both maps.
819
- *
820
- * Returns false only if no slot is charged, which cannot happen at the bound
821
- * (`maxBuffered` is a positive integer); admission then falls back to the
822
- * fail-closed `active-data-unit-limit` reject, which stays a stable host code.
823
- * That holds because the candidate set here is built from the same
824
- * `parkedRunHoldsASlot` rule `#incompleteRunCount` counts by, so reaching the
825
- * bound implies at least one candidate. Filtering one and not the other would
826
- * make the claim false: a queue counted but not offered charges a slot no
827
- * eviction can reclaim, and the receiver refuses every later data unit
828
- * permanently.
829
- */
830
- evictLeastRecentlyAdmitted_fn = function() {
831
- let victimAddress;
832
- let victimSeq = Number.POSITIVE_INFINITY;
833
- for (const [, active] of __privateGet(this, _activeDataUnits)) {
834
- if (active.admissionSeq < victimSeq) {
835
- victimSeq = active.admissionSeq;
836
- victimAddress = active.address;
837
- }
838
- }
839
- for (const [address, pending] of __privateGet(this, _pendingContinuations)) {
840
- if (!parkedRunHoldsASlot(__privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0, pending.fragments.size)) {
841
- continue;
842
- }
843
- if (pending.admissionSeq < victimSeq) {
844
- victimSeq = pending.admissionSeq;
845
- victimAddress = address;
846
- }
847
- }
848
- if (victimAddress === void 0) return false;
849
- __privateMethod(this, _MFUReassembler_instances, releaseSlot_fn).call(this, victimAddress);
850
- this.stats.evictedMemoryLimit++;
851
- return true;
852
- };
853
- /**
854
- * Release every form of state one address holds: its open run, that run's
855
- * buffered fragments and deadline, and its continuation queue. The address is
856
- * deliberately not marked completed — the data unit never emitted, so a later
857
- * fragment of it may open a fresh slot rather than being rejected as stale.
858
- */
859
- releaseSlot_fn = function(address) {
860
- const open = __privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address);
861
- if (open !== void 0) {
862
- const [runKey, active] = open;
863
- __privateGet(this, _activeDataUnits).delete(runKey);
864
- __privateMethod(this, _MFUReassembler_instances, clearTimeout_fn).call(this, active.bufferKey);
865
- __privateGet(this, _buffers).delete(active.bufferKey);
866
- }
867
- __privateMethod(this, _MFUReassembler_instances, deletePendingRun_fn).call(this, address);
868
- };
869
- reject_fn = function(code, message) {
870
- this.stats.rejectedFragments++;
871
- throw new MfuReassemblyError(code, message);
872
- };
873
- associateDataUnit_fn = function(fragment, runKey, address) {
874
- const fi = fragment.fragmentationIndicator;
875
- if (fi === 1) {
876
- const parked = __privateGet(this, _pendingContinuations).get(address);
877
- const inheritsParkedSlot = parkedRunHoldsASlot(
878
- __privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0,
879
- parked?.fragments.size ?? 0
880
- );
881
- const sampleIdentity = hasSampleIdentity(fragment);
882
- const hasSampleFields = hasAnySampleIdentityField(fragment);
883
- const hasItemIdentity = fragment.itemId !== void 0;
884
- if (sampleIdentity && hasItemIdentity) {
885
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "conflicting-data-unit-identity", "fragmented MFU carries timed and non-timed DU identities");
886
- }
887
- if (!sampleIdentity && !hasItemIdentity) {
888
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "first fragment has no intrinsic DU identity");
889
- }
890
- if (hasSampleFields && !sampleIdentity) {
891
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "first fragment has an incomplete sample identity");
892
- }
893
- if (hasItemIdentity && !isU32(fragment.itemId)) {
894
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "missing-data-unit-identity", "first fragment has an invalid Item_ID");
895
- }
896
- const bufferKey = objectKey(fragment);
897
- if (__privateGet(this, _buffers).has(bufferKey)) {
898
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "overlapping-data-unit", "intrinsic data-unit buffer already exists");
899
- }
900
- let sampleEnd = null;
901
- if (sampleIdentity) {
902
- sampleEnd = checkedSampleEnd(fragment.sampleOffset, fragment.data.byteLength);
903
- if (sampleEnd === null) __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "offset-overflow", "sample byte offset overflow");
904
- }
905
- if (!inheritsParkedSlot && __privateMethod(this, _MFUReassembler_instances, incompleteRunCount_fn).call(this) >= __privateGet(this, _maxBuffered)) {
906
- if (!__privateMethod(this, _MFUReassembler_instances, evictLeastRecentlyAdmitted_fn).call(this)) {
907
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "active-data-unit-limit", "active MFU data-unit limit reached");
1138
+ #pendingDebugSamples() {
1139
+ const out = [];
1140
+ for (const [key, fragments] of this.#buffers) {
1141
+ if (fragments.length === 0) continue;
1142
+ const sorted = [...fragments].sort((a, b) => this.#orderValue(a) - this.#orderValue(b));
1143
+ const fiCounts = [0, 0, 0, 0];
1144
+ let minCounter = Infinity;
1145
+ let maxCounter = -Infinity;
1146
+ let minOffset = Infinity;
1147
+ let maxOffset = -Infinity;
1148
+ for (const fragment of sorted) {
1149
+ fiCounts[fragment.fragmentationIndicator] = (fiCounts[fragment.fragmentationIndicator] ?? 0) + 1;
1150
+ minCounter = Math.min(minCounter, fragment.fragmentCounter);
1151
+ maxCounter = Math.max(maxCounter, fragment.fragmentCounter);
1152
+ if (typeof fragment.sampleOffset === "number") {
1153
+ minOffset = Math.min(minOffset, fragment.sampleOffset);
1154
+ maxOffset = Math.max(maxOffset, fragment.sampleOffset);
1155
+ }
908
1156
  }
909
- }
910
- const admissionSeq = inheritsParkedSlot ? parked.admissionSeq : __privateWrapper(this, _nextAdmissionSeq)._++;
911
- let active2;
912
- if (sampleIdentity) {
913
- active2 = {
914
- address,
915
- kind: "sample",
916
- bufferKey,
917
- nextFragmentCounter: fragment.fragmentCounter - 1,
918
- admissionSeq,
919
- movieFragmentSequenceNumber: fragment.movieFragmentSequenceNumber,
920
- sampleNumber: fragment.sampleNumber,
921
- nextOffset: sampleEnd,
922
- ...fragment.catalogTrackName === void 0 ? {} : { catalogTrackName: fragment.catalogTrackName }
923
- };
924
- } else {
925
- active2 = {
926
- address,
927
- kind: "item",
928
- bufferKey,
929
- nextFragmentCounter: fragment.fragmentCounter - 1,
930
- admissionSeq,
931
- itemId: fragment.itemId,
932
- ...fragment.catalogTrackName === void 0 ? {} : { catalogTrackName: fragment.catalogTrackName }
1157
+ const point = (fragment) => ({
1158
+ fi: fragment.fragmentationIndicator,
1159
+ counter: fragment.fragmentCounter,
1160
+ order: this.#orderValue(fragment),
1161
+ ...typeof fragment.packetSequenceNumber === "number" ? { seq: fragment.packetSequenceNumber } : {},
1162
+ ...typeof fragment.sampleOffset === "number" ? { offset: fragment.sampleOffset } : {}
1163
+ });
1164
+ const summary = {
1165
+ key,
1166
+ fragments: sorted.length,
1167
+ fi0: fiCounts[0] ?? 0,
1168
+ fi1: fiCounts[1] ?? 0,
1169
+ fi2: fiCounts[2] ?? 0,
1170
+ fi3: fiCounts[3] ?? 0,
1171
+ firstFi: sorted[0].fragmentationIndicator,
1172
+ lastFi: sorted[sorted.length - 1].fragmentationIndicator,
1173
+ firstOrder: this.#orderValue(sorted[0]),
1174
+ lastOrder: this.#orderValue(sorted[sorted.length - 1]),
1175
+ minCounter,
1176
+ maxCounter,
1177
+ movieFragmentSequenceNumber: sorted.find((f) => f.movieFragmentSequenceNumber !== void 0)?.movieFragmentSequenceNumber,
1178
+ sampleNumber: sorted.find((f) => f.sampleNumber !== void 0)?.sampleNumber,
1179
+ mpuSequenceNumber: sorted[0].mpuSequenceNumber,
1180
+ sortedHead: sorted.slice(0, 6).map(point),
1181
+ sortedTail: sorted.slice(-6).map(point)
933
1182
  };
1183
+ if (minOffset !== Infinity) summary.minOffset = minOffset;
1184
+ if (maxOffset !== -Infinity) summary.maxOffset = maxOffset;
1185
+ out.push(summary);
1186
+ if (out.length >= 12) break;
934
1187
  }
935
- __privateGet(this, _activeDataUnits).set(objectKey(fragment), active2);
936
- return { bufferKey, first: true, terminal: false };
937
- }
938
- const active = __privateGet(this, _activeDataUnits).get(runKey);
939
- if (active === void 0) {
940
- throw new Error("[MFUReassembler] continuation was not parked before association");
941
- }
942
- if (fragment.fragmentCounter !== active.nextFragmentCounter) {
943
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "fragment-counter-mismatch", "MFU continuation does not follow the authored fragment-counter countdown");
944
- }
945
- if (active.kind === "sample") {
946
- if (fragment.itemId !== void 0) {
947
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "data-unit-identity-mismatch", "sample continuation carries an Item_ID");
948
- }
949
- if (hasAnySampleIdentityField(fragment) && (!hasSampleIdentity(fragment) || fragment.movieFragmentSequenceNumber !== active.movieFragmentSequenceNumber || fragment.sampleNumber !== active.sampleNumber || fragment.sampleOffset !== active.nextOffset)) {
950
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "data-unit-identity-mismatch", "sample continuation identity does not match");
951
- }
952
- const nextOffset = checkedSampleEnd(active.nextOffset, fragment.data.byteLength);
953
- if (nextOffset === null) __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "offset-overflow", "sample byte offset overflow");
954
- fragment.movieFragmentSequenceNumber = active.movieFragmentSequenceNumber;
955
- fragment.sampleNumber = active.sampleNumber;
956
- fragment.sampleOffset = active.nextOffset;
957
- if (fi === 2) {
958
- active.nextOffset = nextOffset;
959
- active.nextFragmentCounter = fragment.fragmentCounter - 1;
960
- }
961
- } else {
962
- if (hasAnySampleIdentityField(fragment) || fragment.itemId !== void 0 && fragment.itemId !== active.itemId) {
963
- __privateMethod(this, _MFUReassembler_instances, reject_fn).call(this, "data-unit-identity-mismatch", "Item_ID continuation identity does not match");
964
- }
965
- fragment.itemId = active.itemId;
966
- if (fi === 2) active.nextFragmentCounter = fragment.fragmentCounter - 1;
967
- }
968
- return {
969
- bufferKey: active.bufferKey,
970
- first: false,
971
- terminal: fi === 3
972
- };
973
- };
974
- tryReassemble_fn = function(bufferKey) {
975
- const fragments = __privateGet(this, _buffers).get(bufferKey);
976
- if (!fragments || fragments.length === 0) {
977
- return null;
978
- }
979
- const sorted = [...fragments].sort((a, b) => __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, a) - __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, b));
980
- if (sorted[0].fragmentationIndicator !== 1 || sorted[sorted.length - 1].fragmentationIndicator !== 3) {
981
- return null;
982
- }
983
- if (sorted.slice(1, -1).some((fragment) => fragment.fragmentationIndicator !== 2)) {
984
- return null;
985
- }
986
- for (let i = 1; i < sorted.length; i++) {
987
- if (__privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, sorted[i]) !== __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, sorted[i - 1]) + 1) {
988
- return null;
989
- }
990
- }
991
- if (ordersByOffset(sorted)) {
992
- for (let i = 1; i < sorted.length; i++) {
993
- const previous = sorted[i - 1];
994
- if (sorted[i].sampleOffset !== previous.sampleOffset + previous.data.byteLength) {
995
- return null;
996
- }
997
- }
998
- }
999
- fragments.splice(0, fragments.length, ...sorted);
1000
- return __privateMethod(this, _MFUReassembler_instances, assemble_fn).call(this, bufferKey, fragments);
1001
- };
1002
- assemble_fn = function(bufferKey, fragments) {
1003
- const totalSize = fragments.reduce((sum, f) => sum + f.data.length, 0);
1004
- const data = new Uint8Array(totalSize);
1005
- let offset = 0;
1006
- for (const fragment of fragments) {
1007
- data.set(fragment.data, offset);
1008
- offset += fragment.data.length;
1009
- }
1010
- __privateMethod(this, _MFUReassembler_instances, clearTimeout_fn).call(this, bufferKey);
1011
- __privateGet(this, _buffers).delete(bufferKey);
1012
- __privateMethod(this, _MFUReassembler_instances, markEmitted_fn).call(this, bufferKey);
1013
- this.stats.reassembledMFUs++;
1014
- return {
1015
- mpuSequenceNumber: fragments[0].mpuSequenceNumber,
1016
- packetSequenceNumber: fragments[0].packetSequenceNumber,
1017
- ...assembledPacketSequences(fragments),
1018
- data,
1019
- timestamp: fragments[0].timestamp,
1020
- fragmentCount: fragments.length,
1021
- rapFlag: fragments[0].rapFlag,
1022
- priority: fragments.find((f) => f.priority !== void 0)?.priority,
1023
- sampleNumber: fragments[0].sampleNumber,
1024
- movieFragmentSequenceNumber: fragments[0].movieFragmentSequenceNumber,
1025
- sampleOffset: fragments[0].sampleOffset,
1026
- source: fragments[0].source,
1027
- deliveryProvenance: deliveryProvenanceOf(fragments),
1028
- catalogTrackName: fragments[0].catalogTrackName
1029
- };
1030
- };
1031
- setTimeout_fn = function(bufferKey, startedAt = Date.now()) {
1032
- const state = { startedAt };
1033
- if (__privateGet(this, _timeoutMs) !== null) {
1034
- state.timeoutId = setTimeout(
1035
- () => __privateMethod(this, _MFUReassembler_instances, onTimeout_fn).call(this, bufferKey),
1036
- Math.max(0, startedAt + __privateGet(this, _timeoutMs) - Date.now())
1037
- );
1038
- }
1039
- __privateGet(this, _timeouts).set(bufferKey, state);
1040
- };
1041
- clearPendingTimeout_fn = function(address) {
1042
- const pending = __privateGet(this, _pendingContinuations).get(address);
1043
- if (pending?.timeoutId !== void 0) clearTimeout(pending.timeoutId);
1044
- if (pending !== void 0) {
1045
- delete pending.timeoutId;
1046
- }
1047
- };
1048
- /**
1049
- * A queue whose run closed without completing is headless again and expires
1050
- * from its own first arrival, exactly as it would had the head never come.
1051
- *
1052
- * An empty queue is not armed: it holds no data unit that could fail to
1053
- * complete, so there is nothing for a deadline to expire. Every arming site
1054
- * reaches this function, which takes that test through `parkedRunHoldsASlot`
1055
- * rather than filtering on the open run alone, which is how this engine
1056
- * reaches Rust's answer: its
1057
- * `next_timeout_at` derives candidate deadlines from the queue's live
1058
- * entries, so an empty queue yields none (BLO-32420).
1059
- */
1060
- armHeadlessPendingTimeout_fn = function(address) {
1061
- const pending = __privateGet(this, _pendingContinuations).get(address);
1062
- if (pending === void 0 || pending.timeoutId !== void 0 || __privateGet(this, _timeoutMs) === null) return;
1063
- if (!parkedRunHoldsASlot(__privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0, pending.fragments.size)) return;
1064
- pending.timeoutId = setTimeout(
1065
- () => __privateMethod(this, _MFUReassembler_instances, onPendingTimeout_fn).call(this, address),
1066
- Math.max(0, pending.startedAt + __privateGet(this, _timeoutMs) - Date.now())
1067
- );
1068
- };
1069
- deletePendingRun_fn = function(address) {
1070
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
1071
- __privateGet(this, _pendingContinuations).delete(address);
1072
- };
1073
- onPendingTimeout_fn = function(address) {
1074
- const pending = __privateGet(this, _pendingContinuations).get(address);
1075
- if (pending === void 0) return;
1076
- if (!parkedRunHoldsASlot(__privateMethod(this, _MFUReassembler_instances, openRunAt_fn).call(this, address) !== void 0, pending.fragments.size)) {
1077
- __privateMethod(this, _MFUReassembler_instances, clearPendingTimeout_fn).call(this, address);
1078
- return;
1079
- }
1080
- this.stats.timedOutMFUs++;
1081
- console.warn(
1082
- `[MfuReassembler] timeout: run=${address} fragments=${pending.fragments.size} (continuations awaiting first fragment, discarded)`
1083
- );
1084
- __privateGet(this, _pendingContinuations).delete(address);
1085
- };
1086
- clearTimeout_fn = function(bufferKey) {
1087
- const state = __privateGet(this, _timeouts).get(bufferKey);
1088
- if (state !== void 0) {
1089
- if (state.timeoutId !== void 0) clearTimeout(state.timeoutId);
1090
- __privateGet(this, _timeouts).delete(bufferKey);
1091
- }
1092
- };
1093
- markEmitted_fn = function(bufferKey) {
1094
- if (__privateGet(this, _emitted).has(bufferKey)) return;
1095
- __privateGet(this, _emitted).add(bufferKey);
1096
- __privateGet(this, _emittedOrder).push(bufferKey);
1097
- while (__privateGet(this, _emittedOrder).length > __privateGet(this, _maxBuffered)) {
1098
- const oldest = __privateGet(this, _emittedOrder).shift();
1099
- if (oldest !== void 0) __privateGet(this, _emitted).delete(oldest);
1100
- }
1101
- };
1102
- markCompleted_fn = function(address) {
1103
- if (__privateGet(this, _completed).has(address)) return;
1104
- __privateGet(this, _completed).add(address);
1105
- __privateGet(this, _completedOrder).push(address);
1106
- const cap = __privateGet(this, _maxBuffered) * COMPLETED_ADDRESS_WINDOW_MULTIPLE;
1107
- while (__privateGet(this, _completedOrder).length > cap) {
1108
- const oldest = __privateGet(this, _completedOrder).shift();
1109
- if (oldest !== void 0) __privateGet(this, _completed).delete(oldest);
1110
- }
1111
- };
1112
- orderValue_fn = function(fragment) {
1113
- return fragmentCounterOrder(fragment);
1114
- };
1115
- /** Close the run behind an expired buffer. Early arrivals parked under its
1116
- * address stay, now headless, and expire from their own arrival. */
1117
- removeActiveDataUnitForBuffer_fn = function(bufferKey) {
1118
- for (const [objectKey2, active] of __privateGet(this, _activeDataUnits)) {
1119
- if (active.bufferKey === bufferKey) {
1120
- __privateGet(this, _activeDataUnits).delete(objectKey2);
1121
- __privateMethod(this, _MFUReassembler_instances, armHeadlessPendingTimeout_fn).call(this, active.address);
1122
- }
1123
- }
1124
- };
1125
- /** The one open run holding this address, with its object key. Admission
1126
- * rejects a second head on an open address, so at most one run can match. */
1127
- openRunAt_fn = function(address) {
1128
- for (const entry of __privateGet(this, _activeDataUnits)) {
1129
- if (entry[1].address === address) return entry;
1130
- }
1131
- return void 0;
1132
- };
1133
- onTimeout_fn = function(bufferKey) {
1134
- const fragments = __privateGet(this, _buffers).get(bufferKey);
1135
- if (!fragments || fragments.length === 0) {
1136
- __privateGet(this, _buffers).delete(bufferKey);
1137
- __privateGet(this, _timeouts).delete(bufferKey);
1138
- __privateMethod(this, _MFUReassembler_instances, removeActiveDataUnitForBuffer_fn).call(this, bufferKey);
1139
- this.stats.timedOutMFUs++;
1140
- console.warn(`[MfuReassembler] timeout: key=${bufferKey} fragments=0 (no data buffered)`);
1141
- return;
1142
- }
1143
- this.stats.timedOutMFUs++;
1144
- console.warn(`[MfuReassembler] timeout: key=${bufferKey} fragments=${fragments.length} (partial, discarded)`);
1145
- __privateGet(this, _buffers).delete(bufferKey);
1146
- __privateGet(this, _timeouts).delete(bufferKey);
1147
- __privateMethod(this, _MFUReassembler_instances, removeActiveDataUnitForBuffer_fn).call(this, bufferKey);
1148
- };
1149
- pendingDebugSamples_fn = function() {
1150
- const out = [];
1151
- for (const [key, fragments] of __privateGet(this, _buffers)) {
1152
- if (fragments.length === 0) continue;
1153
- const sorted = [...fragments].sort((a, b) => __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, a) - __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, b));
1154
- const fiCounts = [0, 0, 0, 0];
1155
- let minCounter = Infinity;
1156
- let maxCounter = -Infinity;
1157
- let minOffset = Infinity;
1158
- let maxOffset = -Infinity;
1159
- for (const fragment of sorted) {
1160
- fiCounts[fragment.fragmentationIndicator] = (fiCounts[fragment.fragmentationIndicator] ?? 0) + 1;
1161
- minCounter = Math.min(minCounter, fragment.fragmentCounter);
1162
- maxCounter = Math.max(maxCounter, fragment.fragmentCounter);
1163
- if (typeof fragment.sampleOffset === "number") {
1164
- minOffset = Math.min(minOffset, fragment.sampleOffset);
1165
- maxOffset = Math.max(maxOffset, fragment.sampleOffset);
1166
- }
1167
- }
1168
- const point = (fragment) => ({
1169
- fi: fragment.fragmentationIndicator,
1170
- counter: fragment.fragmentCounter,
1171
- order: __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, fragment),
1172
- ...typeof fragment.packetSequenceNumber === "number" ? { seq: fragment.packetSequenceNumber } : {},
1173
- ...typeof fragment.sampleOffset === "number" ? { offset: fragment.sampleOffset } : {}
1174
- });
1175
- const summary = {
1176
- key,
1177
- fragments: sorted.length,
1178
- fi0: fiCounts[0] ?? 0,
1179
- fi1: fiCounts[1] ?? 0,
1180
- fi2: fiCounts[2] ?? 0,
1181
- fi3: fiCounts[3] ?? 0,
1182
- firstFi: sorted[0].fragmentationIndicator,
1183
- lastFi: sorted[sorted.length - 1].fragmentationIndicator,
1184
- firstOrder: __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, sorted[0]),
1185
- lastOrder: __privateMethod(this, _MFUReassembler_instances, orderValue_fn).call(this, sorted[sorted.length - 1]),
1186
- minCounter,
1187
- maxCounter,
1188
- movieFragmentSequenceNumber: sorted.find((f) => f.movieFragmentSequenceNumber !== void 0)?.movieFragmentSequenceNumber,
1189
- sampleNumber: sorted.find((f) => f.sampleNumber !== void 0)?.sampleNumber,
1190
- mpuSequenceNumber: sorted[0].mpuSequenceNumber,
1191
- sortedHead: sorted.slice(0, 6).map(point),
1192
- sortedTail: sorted.slice(-6).map(point)
1193
- };
1194
- if (minOffset !== Infinity) summary.minOffset = minOffset;
1195
- if (maxOffset !== -Infinity) summary.maxOffset = maxOffset;
1196
- out.push(summary);
1197
- if (out.length >= 12) break;
1188
+ return out;
1198
1189
  }
1199
- return out;
1200
1190
  };
1201
1191
  function createFragment(mmtpPacket) {
1202
1192
  const extraction = extractMFUPayload(mmtpPacket);