moqtail 0.10.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -131,6 +131,7 @@ var TerminationError = class extends MOQtailError {
131
131
 
132
132
  // src/model/common/pair.ts
133
133
  var MAX_VALUE_LENGTH = 2 ** 16 - 1;
134
+ var MAX_U64 = 2n ** 64n - 1n;
134
135
  var KeyValuePair = class _KeyValuePair {
135
136
  /**
136
137
  * The key/type identifier for this pair.
@@ -190,11 +191,29 @@ var KeyValuePair = class _KeyValuePair {
190
191
  }
191
192
  /**
192
193
  * Serializes this key-value pair to a frozen byte buffer.
194
+ * Equivalent to {@link KeyValuePair.serializeDelta} with `prevType = 0n`.
193
195
  * @returns The serialized buffer.
194
196
  */
195
197
  serialize() {
198
+ return this.serializeDelta(0n);
199
+ }
200
+ /**
201
+ * Serializes this pair's wire form, encoding the Type as a delta from
202
+ * `prevType` (the type of the previous KVP in the same list, or 0n if this
203
+ * is the first entry).
204
+ * @param prevType - The type of the previous KVP in the same list.
205
+ * @throws ProtocolViolationError if this pair's type is less than prevType
206
+ * (Delta Type is an unsigned varint, so the list must be sorted ascending).
207
+ */
208
+ serializeDelta(prevType) {
209
+ if (this.typeValue < prevType) {
210
+ throw new ProtocolViolationError(
211
+ "KeyValuePair.serializeDelta",
212
+ `type ${this.typeValue} is less than previous type ${prevType}; a KVP list must be sorted by ascending type to be delta-encoded`
213
+ );
214
+ }
196
215
  const buf = new ByteBuffer();
197
- buf.putVI(this.typeValue);
216
+ buf.putVI(this.typeValue - prevType);
198
217
  if (isVarInt(this)) {
199
218
  buf.putVI(this.value);
200
219
  } else if (isBytes(this)) {
@@ -204,13 +223,32 @@ var KeyValuePair = class _KeyValuePair {
204
223
  }
205
224
  /**
206
225
  * Deserializes a KeyValuePair from a buffer.
226
+ * Equivalent to {@link KeyValuePair.deserializeDelta} with `prevType = 0n`.
207
227
  * @param buf - The buffer to read from.
208
228
  * @returns The deserialized KeyValuePair.
209
229
  * @throws LengthExceedsMaxError if blob length exceeds 65535 bytes.
210
230
  * @throws NotEnoughBytesError if buffer does not contain enough bytes.
211
231
  */
212
232
  static deserialize(buf) {
213
- const typeValue = buf.getVI();
233
+ return _KeyValuePair.deserializeDelta(buf, 0n);
234
+ }
235
+ /**
236
+ * Deserializes a wire-form KVP whose Type field is a delta from `prevType`
237
+ * (the type of the previous KVP in the same list, or 0n if this is the
238
+ * first entry).
239
+ * @param buf - The buffer to read from.
240
+ * @param prevType - The type of the previous KVP in the same list.
241
+ * @throws ProtocolViolationError if prevType + delta exceeds 2^64 - 1.
242
+ */
243
+ static deserializeDelta(buf, prevType) {
244
+ const deltaType = buf.getVI();
245
+ const typeValue = prevType + deltaType;
246
+ if (typeValue > MAX_U64) {
247
+ throw new ProtocolViolationError(
248
+ "KeyValuePair.deserializeDelta",
249
+ `previous type ${prevType} plus delta type ${deltaType} exceeds 2^64 - 1`
250
+ );
251
+ }
214
252
  if (typeValue % 2n === 0n) {
215
253
  const value = buf.getVI();
216
254
  return new _KeyValuePair(typeValue, value);
@@ -245,6 +283,37 @@ var KeyValuePair = class _KeyValuePair {
245
283
  return false;
246
284
  }
247
285
  };
286
+ function serializeKvpList(items) {
287
+ const sorted = [...items].sort((a, b) => a.typeValue < b.typeValue ? -1 : a.typeValue > b.typeValue ? 1 : 0);
288
+ const buf = new ByteBuffer();
289
+ let prevType = 0n;
290
+ for (const kvp of sorted) {
291
+ buf.putBytes(kvp.serializeDelta(prevType).toUint8Array());
292
+ prevType = kvp.typeValue;
293
+ }
294
+ return buf.freeze();
295
+ }
296
+ function deserializeKvpList(buf, count) {
297
+ const n = typeof count === "bigint" ? Number(count) : count;
298
+ const items = new Array(n);
299
+ let prevType = 0n;
300
+ for (let i = 0; i < n; i++) {
301
+ const kvp = KeyValuePair.deserializeDelta(buf, prevType);
302
+ prevType = kvp.typeValue;
303
+ items[i] = kvp;
304
+ }
305
+ return items;
306
+ }
307
+ function deserializeKvpListUntilEmpty(buf) {
308
+ const items = [];
309
+ let prevType = 0n;
310
+ while (buf.remaining > 0) {
311
+ const kvp = KeyValuePair.deserializeDelta(buf, prevType);
312
+ prevType = kvp.typeValue;
313
+ items.push(kvp);
314
+ }
315
+ return items;
316
+ }
248
317
  function isVarInt(pair) {
249
318
  return pair.typeValue % 2n === 0n;
250
319
  }
@@ -847,13 +916,7 @@ var Datagram = class _Datagram {
847
916
  buf.putU8(this.publisherPriority);
848
917
  }
849
918
  if (exports.ObjectDatagramType.hasExtensions(this.type)) {
850
- const extBuf = new ByteBuffer();
851
- if (this.extensionHeaders) {
852
- for (const header of this.extensionHeaders) {
853
- extBuf.putKeyValuePair(header);
854
- }
855
- }
856
- const extBytes = extBuf.toUint8Array();
919
+ const extBytes = this.extensionHeaders ? serializeKvpList(this.extensionHeaders).toUint8Array() : new Uint8Array(0);
857
920
  buf.putLengthPrefixedBytes(extBytes);
858
921
  }
859
922
  if (exports.ObjectDatagramType.isStatus(this.type)) {
@@ -884,10 +947,7 @@ var Datagram = class _Datagram {
884
947
  if (exports.ObjectDatagramType.hasExtensions(msgType)) {
885
948
  const extBytes = buf.getLengthPrefixedBytes();
886
949
  const headerBytes = new FrozenByteBuffer5(extBytes);
887
- extensionHeaders = [];
888
- while (headerBytes.remaining > 0) {
889
- extensionHeaders.push(headerBytes.getKeyValuePair());
890
- }
950
+ extensionHeaders = deserializeKvpListUntilEmpty(headerBytes);
891
951
  }
892
952
  let payload = null;
893
953
  let objectStatus = null;
@@ -1059,9 +1119,7 @@ var FetchObject = class _FetchObject {
1059
1119
  if (hasObjectId) buf.putVI(this.objectId);
1060
1120
  if (hasPriority) buf.putU8(this.publisherPriority);
1061
1121
  if (hasExtensions) {
1062
- const extBuf = new ByteBuffer();
1063
- for (const h of this.extensionHeaders) extBuf.putKeyValuePair(h);
1064
- buf.putLengthPrefixedBytes(extBuf.toUint8Array());
1122
+ buf.putLengthPrefixedBytes(serializeKvpList(this.extensionHeaders).toUint8Array());
1065
1123
  }
1066
1124
  const payloadBytes = this.payload ?? new Uint8Array(0);
1067
1125
  buf.putLengthPrefixedBytes(payloadBytes);
@@ -1147,10 +1205,7 @@ var FetchObject = class _FetchObject {
1147
1205
  if (hasExtensions) {
1148
1206
  const extLen = buf.getNumberVI();
1149
1207
  const headerBytes = new FrozenByteBuffer5(buf.getBytes(extLen));
1150
- extensionHeaders = [];
1151
- while (headerBytes.remaining > 0) {
1152
- extensionHeaders.push(headerBytes.getKeyValuePair());
1153
- }
1208
+ extensionHeaders = deserializeKvpListUntilEmpty(headerBytes);
1154
1209
  }
1155
1210
  const payloadLen = buf.getNumberVI();
1156
1211
  const payload = buf.getBytes(payloadLen);
@@ -1814,26 +1869,18 @@ var ImmutableExtensionsExtension = class _ImmutableExtensionsExtension {
1814
1869
  extensions;
1815
1870
  static TYPE = 11 /* ImmutableExtensions */;
1816
1871
  toKeyValuePair() {
1817
- const inner = new ByteBuffer();
1818
- for (const kvp of this.extensions) {
1819
- inner.putBytes(kvp.serialize().toUint8Array());
1820
- }
1821
- return KeyValuePair.tryNewBytes(_ImmutableExtensionsExtension.TYPE, inner.toUint8Array());
1872
+ return KeyValuePair.tryNewBytes(_ImmutableExtensionsExtension.TYPE, serializeKvpList(this.extensions).toUint8Array());
1822
1873
  }
1823
1874
  static fromKeyValuePair(pair) {
1824
1875
  if (Number(pair.typeValue) !== _ImmutableExtensionsExtension.TYPE || !(pair.value instanceof Uint8Array))
1825
1876
  return void 0;
1826
1877
  const buf = new FrozenByteBuffer5(pair.value);
1827
- const extensions = [];
1828
- while (buf.remaining > 0) {
1829
- const inner = KeyValuePair.deserialize(buf);
1830
- if (Number(inner.typeValue) === 11 /* ImmutableExtensions */) {
1831
- throw new ProtocolViolationError(
1832
- "ImmutableExtensionsExtension.fromKeyValuePair",
1833
- "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1834
- );
1835
- }
1836
- extensions.push(inner);
1878
+ const extensions = deserializeKvpListUntilEmpty(buf);
1879
+ if (extensions.some((inner) => Number(inner.typeValue) === 11 /* ImmutableExtensions */)) {
1880
+ throw new ProtocolViolationError(
1881
+ "ImmutableExtensionsExtension.fromKeyValuePair",
1882
+ "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1883
+ );
1837
1884
  }
1838
1885
  return new _ImmutableExtensionsExtension(extensions);
1839
1886
  }
@@ -1920,18 +1967,11 @@ exports.TrackExtension = void 0;
1920
1967
  }
1921
1968
  TrackExtension2.toKeyValuePair = toKeyValuePair;
1922
1969
  function deserializeAll(buf) {
1923
- const result = [];
1924
- while (buf.remaining > 0) {
1925
- const kvp = KeyValuePair.deserialize(buf);
1926
- result.push(fromKeyValuePair(kvp));
1927
- }
1928
- return result;
1970
+ return deserializeKvpListUntilEmpty(buf).map(fromKeyValuePair);
1929
1971
  }
1930
1972
  TrackExtension2.deserializeAll = deserializeAll;
1931
1973
  function serializeInto(exts, payload) {
1932
- for (const ext of exts) {
1933
- payload.putBytes(ext.toKeyValuePair().serialize().toUint8Array());
1934
- }
1974
+ payload.putBytes(serializeKvpList(exts.map((ext) => ext.toKeyValuePair())).toUint8Array());
1935
1975
  }
1936
1976
  TrackExtension2.serializeInto = serializeInto;
1937
1977
  function isDeliveryTimeout(ext) {
@@ -1972,26 +2012,21 @@ var ImmutableExtensionsObjectExtension = class _ImmutableExtensionsObjectExtensi
1972
2012
  extensions;
1973
2013
  static TYPE = 11 /* ImmutableExtensions */;
1974
2014
  toKeyValuePair() {
1975
- const inner = new ByteBuffer();
1976
- for (const kvp of this.extensions) {
1977
- inner.putBytes(kvp.serialize().toUint8Array());
1978
- }
1979
- return KeyValuePair.tryNewBytes(_ImmutableExtensionsObjectExtension.TYPE, inner.toUint8Array());
2015
+ return KeyValuePair.tryNewBytes(
2016
+ _ImmutableExtensionsObjectExtension.TYPE,
2017
+ serializeKvpList(this.extensions).toUint8Array()
2018
+ );
1980
2019
  }
1981
2020
  static fromKeyValuePair(pair) {
1982
2021
  if (Number(pair.typeValue) !== _ImmutableExtensionsObjectExtension.TYPE || !(pair.value instanceof Uint8Array))
1983
2022
  return void 0;
1984
2023
  const buf = new FrozenByteBuffer5(pair.value);
1985
- const extensions = [];
1986
- while (buf.remaining > 0) {
1987
- const inner = KeyValuePair.deserialize(buf);
1988
- if (Number(inner.typeValue) === 11 /* ImmutableExtensions */) {
1989
- throw new ProtocolViolationError(
1990
- "ImmutableExtensionsObjectExtension.fromKeyValuePair",
1991
- "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1992
- );
1993
- }
1994
- extensions.push(inner);
2024
+ const extensions = deserializeKvpListUntilEmpty(buf);
2025
+ if (extensions.some((inner) => Number(inner.typeValue) === 11 /* ImmutableExtensions */)) {
2026
+ throw new ProtocolViolationError(
2027
+ "ImmutableExtensionsObjectExtension.fromKeyValuePair",
2028
+ "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
2029
+ );
1995
2030
  }
1996
2031
  return new _ImmutableExtensionsObjectExtension(extensions);
1997
2032
  }
@@ -2044,18 +2079,11 @@ exports.ObjectExtension = void 0;
2044
2079
  }
2045
2080
  ObjectExtension2.toKeyValuePair = toKeyValuePair;
2046
2081
  function deserializeAll(buf) {
2047
- const result = [];
2048
- while (buf.remaining > 0) {
2049
- const kvp = KeyValuePair.deserialize(buf);
2050
- result.push(fromKeyValuePair(kvp));
2051
- }
2052
- return result;
2082
+ return deserializeKvpListUntilEmpty(buf).map(fromKeyValuePair);
2053
2083
  }
2054
2084
  ObjectExtension2.deserializeAll = deserializeAll;
2055
2085
  function serializeInto(exts, payload) {
2056
- for (const ext of exts) {
2057
- payload.putBytes(ext.toKeyValuePair().serialize().toUint8Array());
2058
- }
2086
+ payload.putBytes(serializeKvpList(exts.map((ext) => ext.toKeyValuePair())).toUint8Array());
2059
2087
  }
2060
2088
  ObjectExtension2.serializeInto = serializeInto;
2061
2089
  function isImmutableExtensions(ext) {
@@ -2077,6 +2105,12 @@ exports.ObjectExtension = void 0;
2077
2105
  })(exports.ObjectExtension || (exports.ObjectExtension = {}));
2078
2106
 
2079
2107
  // src/model/data/subgroup_object.ts
2108
+ function normalizeExtensionHeaders(extensionHeaders) {
2109
+ if (extensionHeaders === null || extensionHeaders.length === 0) {
2110
+ return null;
2111
+ }
2112
+ return extensionHeaders;
2113
+ }
2080
2114
  var SubgroupObject = class _SubgroupObject {
2081
2115
  constructor(objectId, extensionHeaders, objectStatus, payload) {
2082
2116
  this.extensionHeaders = extensionHeaders;
@@ -2089,22 +2123,17 @@ var SubgroupObject = class _SubgroupObject {
2089
2123
  payload;
2090
2124
  objectId;
2091
2125
  static newWithStatus(objectId, extensionHeaders, objectStatus) {
2092
- return new _SubgroupObject(objectId, extensionHeaders, objectStatus, null);
2126
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), objectStatus, null);
2093
2127
  }
2094
2128
  static newWithPayload(objectId, extensionHeaders, payload) {
2095
- return new _SubgroupObject(objectId, extensionHeaders, null, payload);
2129
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), null, payload);
2096
2130
  }
2097
2131
  serialize(previousObjectId) {
2098
2132
  let objectIdDelta = previousObjectId ? this.objectId - previousObjectId - BigInt(1) : this.objectId;
2099
2133
  const buf = new ByteBuffer();
2100
2134
  buf.putVI(objectIdDelta);
2101
- const extensionHeaders = new ByteBuffer();
2102
- if (this.extensionHeaders) {
2103
- for (const header of this.extensionHeaders) {
2104
- extensionHeaders.putKeyValuePair(header);
2105
- }
2106
- const headerBytes = extensionHeaders.toUint8Array();
2107
- buf.putLengthPrefixedBytes(headerBytes);
2135
+ if (this.extensionHeaders !== null) {
2136
+ buf.putLengthPrefixedBytes(serializeKvpList(this.extensionHeaders).toUint8Array());
2108
2137
  }
2109
2138
  if (this.payload) {
2110
2139
  buf.putLengthPrefixedBytes(this.payload);
@@ -2119,12 +2148,9 @@ var SubgroupObject = class _SubgroupObject {
2119
2148
  let objectId = previousObjectId !== void 0 ? previousObjectId + objectDelta + BigInt(1) : objectDelta;
2120
2149
  let extensionHeaders = null;
2121
2150
  if (hasExtensions) {
2122
- extensionHeaders = [];
2123
2151
  const extLen = buf.getNumberVI();
2124
2152
  const headerBytes = new FrozenByteBuffer5(buf.getBytes(extLen));
2125
- while (headerBytes.remaining > 0) {
2126
- extensionHeaders.push(headerBytes.getKeyValuePair());
2127
- }
2153
+ extensionHeaders = deserializeKvpListUntilEmpty(headerBytes);
2128
2154
  }
2129
2155
  const payloadLen = buf.getNumberVI();
2130
2156
  let objectStatus = null;
@@ -2134,7 +2160,7 @@ var SubgroupObject = class _SubgroupObject {
2134
2160
  } else {
2135
2161
  payload = buf.getBytes(payloadLen);
2136
2162
  }
2137
- return new _SubgroupObject(objectId, extensionHeaders, objectStatus, payload);
2163
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), objectStatus, payload);
2138
2164
  }
2139
2165
  };
2140
2166
 
@@ -3355,9 +3381,7 @@ var PublishNamespace = class _PublishNamespace {
3355
3381
  payload.putVI(this.requestId);
3356
3382
  payload.putTuple(this.trackNamespace);
3357
3383
  payload.putVI(this.parameters.length);
3358
- for (const param of this.parameters) {
3359
- payload.putKeyValuePair(param.toKeyValuePair());
3360
- }
3384
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3361
3385
  const payloadBytes = payload.toUint8Array();
3362
3386
  if (payloadBytes.length > 65535) {
3363
3387
  throw new LengthExceedsMaxError("PublishNamespace::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -3378,10 +3402,7 @@ var PublishNamespace = class _PublishNamespace {
3378
3402
  const requestId = buf.getVI();
3379
3403
  const trackNamespace = buf.getTuple();
3380
3404
  const paramCount = buf.getNumberVI();
3381
- const rawParams = new Array(paramCount);
3382
- for (let i = 0; i < paramCount; i++) {
3383
- rawParams[i] = buf.getKeyValuePair();
3384
- }
3405
+ const rawParams = deserializeKvpList(buf, paramCount);
3385
3406
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3386
3407
  return new _PublishNamespace(requestId, trackNamespace, parameters);
3387
3408
  }
@@ -3498,9 +3519,7 @@ var ClientSetup = class _ClientSetup {
3498
3519
  buf.putVI(32 /* ClientSetup */);
3499
3520
  const payload = new ByteBuffer();
3500
3521
  payload.putVI(this.setupParameters.length);
3501
- for (const param of this.setupParameters) {
3502
- payload.putKeyValuePair(param);
3503
- }
3522
+ payload.putBytes(serializeKvpList(this.setupParameters).toUint8Array());
3504
3523
  const payloadBytes = payload.toUint8Array();
3505
3524
  if (payloadBytes.length > 65535) {
3506
3525
  throw new LengthExceedsMaxError("ClientSetup::serialize(payload_length)", 65535, payloadBytes.length);
@@ -3511,10 +3530,7 @@ var ClientSetup = class _ClientSetup {
3511
3530
  }
3512
3531
  static parsePayload(buf) {
3513
3532
  const paramCount = buf.getNumberVI();
3514
- const setupParameters = new Array(paramCount);
3515
- for (let i = 0; i < paramCount; i++) {
3516
- setupParameters[i] = buf.getKeyValuePair();
3517
- }
3533
+ const setupParameters = deserializeKvpList(buf, paramCount);
3518
3534
  return new _ClientSetup(setupParameters);
3519
3535
  }
3520
3536
  };
@@ -3553,9 +3569,7 @@ var Fetch = class _Fetch {
3553
3569
  }
3554
3570
  }
3555
3571
  payload.putVI(this.parameters.length);
3556
- for (const param of this.parameters) {
3557
- payload.putKeyValuePair(param.toKeyValuePair());
3558
- }
3572
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3559
3573
  const payloadBytes = payload.toUint8Array();
3560
3574
  if (payloadBytes.length > 65535) {
3561
3575
  throw new LengthExceedsMaxError("Fetch::serialize(payload_length)", 65535, payloadBytes.length);
@@ -3594,10 +3608,7 @@ var Fetch = class _Fetch {
3594
3608
  throw new Error(`Unknown fetch type: ${fetchType}`);
3595
3609
  }
3596
3610
  const paramCount = buf.getNumberVI();
3597
- const rawParams = new Array(paramCount);
3598
- for (let i = 0; i < paramCount; i++) {
3599
- rawParams[i] = buf.getKeyValuePair();
3600
- }
3611
+ const rawParams = deserializeKvpList(buf, paramCount);
3601
3612
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3602
3613
  return new _Fetch(requestId, props, parameters);
3603
3614
  }
@@ -3653,9 +3664,7 @@ var FetchOk = class _FetchOk {
3653
3664
  payload.putU8(this.endOfTrack ? 1 : 0);
3654
3665
  payload.putLocation(this.endLocation);
3655
3666
  payload.putVI(this.parameters.length);
3656
- for (const param of this.parameters) {
3657
- payload.putKeyValuePair(param.toKeyValuePair());
3658
- }
3667
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3659
3668
  exports.TrackExtension.serializeInto(this.trackExtensions, payload);
3660
3669
  const payloadBytes = payload.toUint8Array();
3661
3670
  if (payloadBytes.length > 65535) {
@@ -3684,10 +3693,7 @@ var FetchOk = class _FetchOk {
3684
3693
  }
3685
3694
  const endLocation = buf.getLocation();
3686
3695
  const paramCount = buf.getNumberVI();
3687
- const rawParams = new Array(paramCount);
3688
- for (let i = 0; i < paramCount; i++) {
3689
- rawParams[i] = buf.getKeyValuePair();
3690
- }
3696
+ const rawParams = deserializeKvpList(buf, paramCount);
3691
3697
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3692
3698
  const trackExtensions = exports.TrackExtension.deserializeAll(buf);
3693
3699
  return new _FetchOk(requestId, endOfTrack, endLocation, parameters, trackExtensions);
@@ -3799,9 +3805,7 @@ var ServerSetup = class _ServerSetup {
3799
3805
  buf.putVI(33 /* ServerSetup */);
3800
3806
  const payload = new ByteBuffer();
3801
3807
  payload.putVI(this.setupParameters.length);
3802
- for (const param of this.setupParameters) {
3803
- payload.putKeyValuePair(param);
3804
- }
3808
+ payload.putBytes(serializeKvpList(this.setupParameters).toUint8Array());
3805
3809
  const payloadBytes = payload.toUint8Array();
3806
3810
  if (payloadBytes.length > 65535) {
3807
3811
  throw new LengthExceedsMaxError("ServerSetup::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -3812,10 +3816,7 @@ var ServerSetup = class _ServerSetup {
3812
3816
  }
3813
3817
  static parsePayload(buf) {
3814
3818
  const paramCount = buf.getNumberVI();
3815
- const setupParameters = new Array(paramCount);
3816
- for (let i = 0; i < paramCount; i++) {
3817
- setupParameters[i] = buf.getKeyValuePair();
3818
- }
3819
+ const setupParameters = deserializeKvpList(buf, paramCount);
3819
3820
  return new _ServerSetup(setupParameters);
3820
3821
  }
3821
3822
  };
@@ -3858,9 +3859,7 @@ var Subscribe = class _Subscribe {
3858
3859
  payload.putVI(this.requestId);
3859
3860
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
3860
3861
  payload.putVI(this.parameters.length);
3861
- for (const param of this.parameters) {
3862
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
3863
- }
3862
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3864
3863
  const payloadBytes = payload.toUint8Array();
3865
3864
  buf.putU16(payloadBytes.length);
3866
3865
  buf.putBytes(payloadBytes);
@@ -3871,8 +3870,7 @@ var Subscribe = class _Subscribe {
3871
3870
  const fullTrackName = buf.getFullTrackName();
3872
3871
  const paramCount = Number(buf.getVI());
3873
3872
  const parameters = [];
3874
- for (let i = 0; i < paramCount; i++) {
3875
- const kvp = KeyValuePair.deserialize(buf);
3873
+ for (const kvp of deserializeKvpList(buf, paramCount)) {
3876
3874
  const param = exports.MessageParameter.fromKeyValuePair(kvp);
3877
3875
  if (param !== void 0) parameters.push(param);
3878
3876
  }
@@ -3949,9 +3947,7 @@ var Publish = class _Publish {
3949
3947
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
3950
3948
  payload.putVI(this.trackAlias);
3951
3949
  payload.putVI(this.parameters.length);
3952
- for (const param of this.parameters) {
3953
- payload.putKeyValuePair(param.toKeyValuePair());
3954
- }
3950
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3955
3951
  exports.TrackExtension.serializeInto(this.trackExtensions, payload);
3956
3952
  const payloadBytes = payload.toUint8Array();
3957
3953
  if (payloadBytes.length > 65535) {
@@ -3966,10 +3962,7 @@ var Publish = class _Publish {
3966
3962
  const fullTrackName = buf.getFullTrackName();
3967
3963
  const trackAlias = buf.getVI();
3968
3964
  const paramCount = buf.getNumberVI();
3969
- const rawParams = new Array(paramCount);
3970
- for (let i = 0; i < paramCount; i++) {
3971
- rawParams[i] = buf.getKeyValuePair();
3972
- }
3965
+ const rawParams = deserializeKvpList(buf, paramCount);
3973
3966
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3974
3967
  const trackExtensions = exports.TrackExtension.deserializeAll(buf);
3975
3968
  return new _Publish(requestId, fullTrackName, trackAlias, parameters, trackExtensions);
@@ -3993,9 +3986,7 @@ var PublishOk = class _PublishOk {
3993
3986
  const payload = new ByteBuffer();
3994
3987
  payload.putVI(this.requestId);
3995
3988
  payload.putVI(this.parameters.length);
3996
- for (const param of this.parameters) {
3997
- payload.putKeyValuePair(param.toKeyValuePair());
3998
- }
3989
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3999
3990
  const payloadBytes = payload.toUint8Array();
4000
3991
  if (payloadBytes.length > 65535) {
4001
3992
  throw new LengthExceedsMaxError("PublishOk::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -4007,10 +3998,7 @@ var PublishOk = class _PublishOk {
4007
3998
  static parsePayload(buf) {
4008
3999
  const requestId = buf.getVI();
4009
4000
  const paramCount = buf.getVI();
4010
- const kvps = [];
4011
- for (let i = 0; i < paramCount; i++) {
4012
- kvps.push(buf.getKeyValuePair());
4013
- }
4001
+ const kvps = deserializeKvpList(buf, paramCount);
4014
4002
  const parameters = MessageParameters.fromKeyValuePairs(kvps);
4015
4003
  return new _PublishOk(requestId, parameters);
4016
4004
  }
@@ -4038,9 +4026,7 @@ var SubscribeOk = class _SubscribeOk {
4038
4026
  payload.putVI(this.requestId);
4039
4027
  payload.putVI(this.trackAlias);
4040
4028
  payload.putVI(this.parameters.length);
4041
- for (const param of this.parameters) {
4042
- payload.putKeyValuePair(param.toKeyValuePair());
4043
- }
4029
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4044
4030
  exports.TrackExtension.serializeInto(this.trackExtensions, payload);
4045
4031
  const payloadBytes = payload.toUint8Array();
4046
4032
  if (payloadBytes.length > 65535) {
@@ -4054,10 +4040,7 @@ var SubscribeOk = class _SubscribeOk {
4054
4040
  const requestId = buf.getVI();
4055
4041
  const trackAlias = buf.getVI();
4056
4042
  const paramCount = buf.getNumberVI();
4057
- const rawParams = new Array(paramCount);
4058
- for (let i = 0; i < paramCount; i++) {
4059
- rawParams[i] = buf.getKeyValuePair();
4060
- }
4043
+ const rawParams = deserializeKvpList(buf, paramCount);
4061
4044
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
4062
4045
  const trackExtensions = exports.TrackExtension.deserializeAll(buf);
4063
4046
  return new _SubscribeOk(requestId, trackAlias, parameters, trackExtensions);
@@ -4084,9 +4067,7 @@ var RequestUpdate = class _RequestUpdate {
4084
4067
  payload.putVI(this.requestId);
4085
4068
  payload.putVI(this.existingRequestId);
4086
4069
  payload.putVI(this.parameters.length);
4087
- for (const param of this.parameters) {
4088
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
4089
- }
4070
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4090
4071
  const payloadBytes = payload.toUint8Array();
4091
4072
  if (payloadBytes.length > 65535) {
4092
4073
  throw new LengthExceedsMaxError("RequestUpdate::serialize(payloadBytes)", 65535, payloadBytes.length);
@@ -4104,8 +4085,7 @@ var RequestUpdate = class _RequestUpdate {
4104
4085
  throw new CastingError("RequestUpdate.deserialize paramCount", "bigint", "number", `${paramCountBig}`);
4105
4086
  }
4106
4087
  const parameters = [];
4107
- for (let i = 0; i < paramCount; i++) {
4108
- const kvp = KeyValuePair.deserialize(buf);
4088
+ for (const kvp of deserializeKvpList(buf, paramCount)) {
4109
4089
  const param = exports.MessageParameter.fromKeyValuePair(kvp);
4110
4090
  if (param !== void 0) parameters.push(param);
4111
4091
  }
@@ -4262,9 +4242,7 @@ var TrackStatus = class _TrackStatus {
4262
4242
  payload.putVI(this.endGroup);
4263
4243
  }
4264
4244
  payload.putVI(this.subscribeParameters.length);
4265
- for (const param of this.subscribeParameters) {
4266
- payload.putKeyValuePair(param.toKeyValuePair());
4267
- }
4245
+ payload.putBytes(serializeKvpList(this.subscribeParameters.map((p) => p.toKeyValuePair())).toUint8Array());
4268
4246
  const payloadBytes = payload.toUint8Array();
4269
4247
  buf.putU16(payloadBytes.length);
4270
4248
  buf.putBytes(payloadBytes);
@@ -4287,10 +4265,7 @@ var TrackStatus = class _TrackStatus {
4287
4265
  endGroup = buf.getVI();
4288
4266
  }
4289
4267
  const paramCount = Number(buf.getVI());
4290
- const rawParams = new Array(paramCount);
4291
- for (let i = 0; i < paramCount; i++) {
4292
- rawParams[i] = buf.getKeyValuePair();
4293
- }
4268
+ const rawParams = deserializeKvpList(buf, paramCount);
4294
4269
  const subscribeParameters = MessageParameters.fromKeyValuePairs(rawParams);
4295
4270
  return new _TrackStatus(
4296
4271
  requestId,
@@ -4408,9 +4383,7 @@ var SubscribeNamespace = class _SubscribeNamespace {
4408
4383
  payload.putTuple(this.trackNamespacePrefix);
4409
4384
  payload.putVI(this.subscribeOptions);
4410
4385
  payload.putVI(this.parameters.length);
4411
- for (const param of this.parameters) {
4412
- payload.putKeyValuePair(param.toKeyValuePair());
4413
- }
4386
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4414
4387
  const payloadBytes = payload.toUint8Array();
4415
4388
  if (payloadBytes.length > 65535) {
4416
4389
  throw new LengthExceedsMaxError("SubscribeNamespace::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -4424,10 +4397,7 @@ var SubscribeNamespace = class _SubscribeNamespace {
4424
4397
  const trackNamespacePrefix = buf.getTuple();
4425
4398
  const subscribeOptions = Number(buf.getVI());
4426
4399
  const paramCount = buf.getNumberVI();
4427
- const rawParams = new Array(paramCount);
4428
- for (let i = 0; i < paramCount; i++) {
4429
- rawParams[i] = buf.getKeyValuePair();
4430
- }
4400
+ const rawParams = deserializeKvpList(buf, paramCount);
4431
4401
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
4432
4402
  return new _SubscribeNamespace(requestId, trackNamespacePrefix, subscribeOptions, parameters);
4433
4403
  }
@@ -4478,9 +4448,7 @@ var RequestOk = class _RequestOk {
4478
4448
  const payload = new ByteBuffer();
4479
4449
  payload.putVI(this.requestId);
4480
4450
  payload.putVI(BigInt(this.parameters.length));
4481
- for (const param of this.parameters) {
4482
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
4483
- }
4451
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4484
4452
  const payloadBytes = payload.toUint8Array();
4485
4453
  if (payloadBytes.length > 65535) {
4486
4454
  throw new LengthExceedsMaxError("RequestOk::serialize(payloadBytes)", 65535, payloadBytes.length);
@@ -4497,8 +4465,7 @@ var RequestOk = class _RequestOk {
4497
4465
  throw new CastingError("RequestOk.parsePayload numParams", "bigint", "number", `${numParamsBig}`);
4498
4466
  }
4499
4467
  const parameters = [];
4500
- for (let i = 0; i < numParams; i++) {
4501
- const kvp = KeyValuePair.deserialize(buf);
4468
+ for (const kvp of deserializeKvpList(buf, numParams)) {
4502
4469
  const param = exports.MessageParameter.fromKeyValuePair(kvp);
4503
4470
  if (param !== void 0) parameters.push(param);
4504
4471
  }
@@ -4853,9 +4820,7 @@ var Switch = class _Switch {
4853
4820
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
4854
4821
  payload.putVI(this.subscriptionRequestId);
4855
4822
  payload.putVI(this.parameters.length);
4856
- for (const param of this.parameters) {
4857
- payload.putBytes(param.serialize().toUint8Array());
4858
- }
4823
+ payload.putBytes(serializeKvpList(this.parameters).toUint8Array());
4859
4824
  const payloadBytes = payload.toUint8Array();
4860
4825
  buf.putU16(payloadBytes.length);
4861
4826
  buf.putBytes(payloadBytes);
@@ -4866,10 +4831,7 @@ var Switch = class _Switch {
4866
4831
  const fullTrackName = buf.getFullTrackName();
4867
4832
  const subscriptionRequestId = buf.getVI();
4868
4833
  const paramCount = Number(buf.getVI());
4869
- const parameters = [];
4870
- for (let i = 0; i < paramCount; i++) {
4871
- parameters.push(KeyValuePair.deserialize(buf));
4872
- }
4834
+ const parameters = deserializeKvpList(buf, paramCount);
4873
4835
  return new _Switch(requestId, fullTrackName, subscriptionRequestId, parameters);
4874
4836
  }
4875
4837
  };
@@ -5617,6 +5579,7 @@ var SubscribeRequest = class {
5617
5579
  priority;
5618
5580
  forward;
5619
5581
  subscribeParameters;
5582
+ earlyDiscardPolicy;
5620
5583
  largestLocation;
5621
5584
  // Updated on each received object
5622
5585
  streamsAccepted = 0n;
@@ -6452,6 +6415,11 @@ var handlerRequestsBlocked = async (_client, msg) => {
6452
6415
  logger.debug("handler/requests_blocked", "not implemented", msg);
6453
6416
  };
6454
6417
 
6418
+ // src/client/util/validators.ts
6419
+ function isValidTrackAlias(trackAlias) {
6420
+ return trackAlias !== void 0 && trackAlias >= 0n;
6421
+ }
6422
+
6455
6423
  // src/client/handler/subscribe.ts
6456
6424
  var handlerSubscribe = async (client, msg) => {
6457
6425
  logger.debug("handler/subscribe", `received requestId=${msg.requestId} ftn="${msg.fullTrackName}"`);
@@ -6484,7 +6452,7 @@ var handlerSubscribe = async (client, msg) => {
6484
6452
  await client.controlStream.send(response);
6485
6453
  return;
6486
6454
  }
6487
- if (!track.trackAlias) throw new Error("Expected track alias to be set");
6455
+ if (!isValidTrackAlias(track.trackAlias)) throw new Error("Expected track alias to be set");
6488
6456
  const largestLocation = track.trackSource.live.largestLocation;
6489
6457
  const parameters = [...msg.parameters];
6490
6458
  if (largestLocation) {
@@ -7404,14 +7372,17 @@ var MOQtailClient = class _MOQtailClient {
7404
7372
  }
7405
7373
  }
7406
7374
  /**
7407
- * Sets (or replaces) the early discard policy for incoming subgroup streams.
7375
+ * Sets (or replaces) the client-level default early discard policy for incoming subgroup streams.
7408
7376
  *
7409
7377
  * When set, each incoming subgroup QUIC stream is given a deadline of `subgroupReceiveTimeout` ms to
7410
7378
  * complete. If the stream has not finished within that window it is cancelled — objects already
7411
7379
  * delivered to the subscription are kept, but no further objects arrive from that stream.
7412
7380
  *
7381
+ * This is a client-wide default. Individual subscriptions can override it via the `earlyDiscardPolicy`
7382
+ * field in {@link SubscribeOptions}, which takes precedence over this setting.
7383
+ *
7413
7384
  * The policy takes effect on the next stream accepted after this call. Passing a new config
7414
- * replaces the previous one. Pass `undefined` to remove the policy.
7385
+ * replaces the previous one. Pass `undefined` to remove the default.
7415
7386
  *
7416
7387
  * @example
7417
7388
  * ```ts
@@ -7488,7 +7459,6 @@ var MOQtailClient = class _MOQtailClient {
7488
7459
  * // Register the track for live subscription
7489
7460
  * client.addOrUpdateTrack({
7490
7461
  * fullTrackName: { namespace: ["camera"], name: "main" },
7491
- * forwardingPreference: ObjectForwardingPreference.Latest,
7492
7462
  * trackSource: { live: liveReadableStream },
7493
7463
  * publisherPriority: 0 // highest priority
7494
7464
  * });
@@ -7498,7 +7468,6 @@ var MOQtailClient = class _MOQtailClient {
7498
7468
  * const cache = new MemoryObjectCache(); // Caches are not yet fully supported
7499
7469
  * client.addOrUpdateTrack({
7500
7470
  * fullTrackName: { namespace: ["camera"], name: "main" },
7501
- * forwardingPreference: ObjectForwardingPreference.Latest,
7502
7471
  * trackSource: { live: liveReadableStream, past: cache },
7503
7472
  * publisherPriority: 8
7504
7473
  * });
@@ -7506,7 +7475,7 @@ var MOQtailClient = class _MOQtailClient {
7506
7475
  */
7507
7476
  addOrUpdateTrack(track) {
7508
7477
  this.#ensureActive();
7509
- if (!track.trackAlias) {
7478
+ if (!isValidTrackAlias(track.trackAlias)) {
7510
7479
  track.trackAlias = random60bitId();
7511
7480
  }
7512
7481
  this.trackSources.set(track.fullTrackName.toString(), track);
@@ -7638,6 +7607,7 @@ var MOQtailClient = class _MOQtailClient {
7638
7607
  break;
7639
7608
  }
7640
7609
  const request = new SubscribeRequest(msg);
7610
+ request.earlyDiscardPolicy = args.earlyDiscardPolicy;
7641
7611
  this.requests.set(request.requestId, request);
7642
7612
  this.requestIdMap.addMapping(request.requestId, request.fullTrackName);
7643
7613
  logger.debug("MOQtailClient", `subscribe: sending SUBSCRIBE requestId=${msg.requestId} ftn="${fullTrackName}"`);
@@ -7786,7 +7756,7 @@ var MOQtailClient = class _MOQtailClient {
7786
7756
  const request = this.requests.get(subscriptionRequestId);
7787
7757
  if (request instanceof SubscribeRequest) {
7788
7758
  const trackAlias = this.subscriptionAliasMap.get(subscriptionRequestId);
7789
- if (!trackAlias)
7759
+ if (!isValidTrackAlias(trackAlias))
7790
7760
  throw new InternalError("MOQtailClient.subscribeUpdate", "Request exists but track alias mapping does not");
7791
7761
  const subscription = this.subscriptions.get(trackAlias);
7792
7762
  if (!subscription)
@@ -7839,7 +7809,7 @@ var MOQtailClient = class _MOQtailClient {
7839
7809
  if (!(request instanceof SubscribeRequest))
7840
7810
  throw new ProtocolViolationError("MOQtailClient.switch", "Request id is not a subscription");
7841
7811
  const trackAlias = this.subscriptionAliasMap.get(subscriptionRequestId);
7842
- if (!trackAlias)
7812
+ if (!isValidTrackAlias(trackAlias))
7843
7813
  throw new InternalError("MOQtailClient.switch", "Request exists but track alias mapping does not");
7844
7814
  const subscription = this.subscriptions.get(trackAlias);
7845
7815
  if (!subscription) throw new InternalError("MOQtailClient.switch", "Request exists but subscription does not");
@@ -8504,11 +8474,12 @@ var MOQtailClient = class _MOQtailClient {
8504
8474
  subscription.streamsAccepted++;
8505
8475
  let firstObjectId = null;
8506
8476
  let subgroupTimeoutId;
8507
- if (this.#earlyDiscardPolicy?.subgroupReceiveTimeout !== void 0) {
8477
+ const effectiveDiscardPolicy = subscription.earlyDiscardPolicy ?? this.#earlyDiscardPolicy;
8478
+ if (effectiveDiscardPolicy?.subgroupReceiveTimeout !== void 0) {
8508
8479
  subgroupTimeoutId = setTimeout(() => {
8509
8480
  reader.cancel("early discard: subgroupReceiveTimeout exceeded").catch(() => {
8510
8481
  });
8511
- }, this.#earlyDiscardPolicy.subgroupReceiveTimeout);
8482
+ }, effectiveDiscardPolicy.subgroupReceiveTimeout);
8512
8483
  }
8513
8484
  try {
8514
8485
  while (true) {
@@ -8984,6 +8955,8 @@ exports.VideoConfig = VideoConfig;
8984
8955
  exports.VideoFrameMarking = VideoFrameMarking;
8985
8956
  exports.applyMessageParameterUpdate = applyMessageParameterUpdate;
8986
8957
  exports.controlMessageTypeFromBigInt = controlMessageTypeFromBigInt;
8958
+ exports.deserializeKvpList = deserializeKvpList;
8959
+ exports.deserializeKvpListUntilEmpty = deserializeKvpListUntilEmpty;
8987
8960
  exports.fetchTypeFromBigInt = fetchTypeFromBigInt;
8988
8961
  exports.filterTypeFromBigInt = filterTypeFromBigInt;
8989
8962
  exports.groupOrderFromNumber = groupOrderFromNumber;
@@ -8994,6 +8967,7 @@ exports.logger = logger;
8994
8967
  exports.messageParameterTypeFromNumber = messageParameterTypeFromNumber;
8995
8968
  exports.publishDoneStatusCodeFromBigInt = publishDoneStatusCodeFromBigInt;
8996
8969
  exports.requestErrorCodeFromBigInt = requestErrorCodeFromBigInt;
8970
+ exports.serializeKvpList = serializeKvpList;
8997
8971
  exports.setLogEnabledModules = setLogEnabledModules;
8998
8972
  exports.setLogLevel = setLogLevel;
8999
8973
  exports.setupParameterTypeFromNumber = setupParameterTypeFromNumber;