moqtail 0.11.0 → 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/client.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 (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 (ObjectDatagramType.isStatus(this.type)) {
@@ -884,10 +947,7 @@ var Datagram = class _Datagram {
884
947
  if (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;
@@ -1054,9 +1114,7 @@ var FetchObject = class _FetchObject {
1054
1114
  if (hasObjectId) buf.putVI(this.objectId);
1055
1115
  if (hasPriority) buf.putU8(this.publisherPriority);
1056
1116
  if (hasExtensions) {
1057
- const extBuf = new ByteBuffer();
1058
- for (const h of this.extensionHeaders) extBuf.putKeyValuePair(h);
1059
- buf.putLengthPrefixedBytes(extBuf.toUint8Array());
1117
+ buf.putLengthPrefixedBytes(serializeKvpList(this.extensionHeaders).toUint8Array());
1060
1118
  }
1061
1119
  const payloadBytes = this.payload ?? new Uint8Array(0);
1062
1120
  buf.putLengthPrefixedBytes(payloadBytes);
@@ -1142,10 +1200,7 @@ var FetchObject = class _FetchObject {
1142
1200
  if (hasExtensions) {
1143
1201
  const extLen = buf.getNumberVI();
1144
1202
  const headerBytes = new FrozenByteBuffer5(buf.getBytes(extLen));
1145
- extensionHeaders = [];
1146
- while (headerBytes.remaining > 0) {
1147
- extensionHeaders.push(headerBytes.getKeyValuePair());
1148
- }
1203
+ extensionHeaders = deserializeKvpListUntilEmpty(headerBytes);
1149
1204
  }
1150
1205
  const payloadLen = buf.getNumberVI();
1151
1206
  const payload = buf.getBytes(payloadLen);
@@ -1606,26 +1661,18 @@ var ImmutableExtensionsExtension = class _ImmutableExtensionsExtension {
1606
1661
  extensions;
1607
1662
  static TYPE = 11 /* ImmutableExtensions */;
1608
1663
  toKeyValuePair() {
1609
- const inner = new ByteBuffer();
1610
- for (const kvp of this.extensions) {
1611
- inner.putBytes(kvp.serialize().toUint8Array());
1612
- }
1613
- return KeyValuePair.tryNewBytes(_ImmutableExtensionsExtension.TYPE, inner.toUint8Array());
1664
+ return KeyValuePair.tryNewBytes(_ImmutableExtensionsExtension.TYPE, serializeKvpList(this.extensions).toUint8Array());
1614
1665
  }
1615
1666
  static fromKeyValuePair(pair) {
1616
1667
  if (Number(pair.typeValue) !== _ImmutableExtensionsExtension.TYPE || !(pair.value instanceof Uint8Array))
1617
1668
  return void 0;
1618
1669
  const buf = new FrozenByteBuffer5(pair.value);
1619
- const extensions = [];
1620
- while (buf.remaining > 0) {
1621
- const inner = KeyValuePair.deserialize(buf);
1622
- if (Number(inner.typeValue) === 11 /* ImmutableExtensions */) {
1623
- throw new ProtocolViolationError(
1624
- "ImmutableExtensionsExtension.fromKeyValuePair",
1625
- "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1626
- );
1627
- }
1628
- extensions.push(inner);
1670
+ const extensions = deserializeKvpListUntilEmpty(buf);
1671
+ if (extensions.some((inner) => Number(inner.typeValue) === 11 /* ImmutableExtensions */)) {
1672
+ throw new ProtocolViolationError(
1673
+ "ImmutableExtensionsExtension.fromKeyValuePair",
1674
+ "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1675
+ );
1629
1676
  }
1630
1677
  return new _ImmutableExtensionsExtension(extensions);
1631
1678
  }
@@ -1712,18 +1759,11 @@ var TrackExtension;
1712
1759
  }
1713
1760
  TrackExtension2.toKeyValuePair = toKeyValuePair;
1714
1761
  function deserializeAll(buf) {
1715
- const result = [];
1716
- while (buf.remaining > 0) {
1717
- const kvp = KeyValuePair.deserialize(buf);
1718
- result.push(fromKeyValuePair(kvp));
1719
- }
1720
- return result;
1762
+ return deserializeKvpListUntilEmpty(buf).map(fromKeyValuePair);
1721
1763
  }
1722
1764
  TrackExtension2.deserializeAll = deserializeAll;
1723
1765
  function serializeInto(exts, payload) {
1724
- for (const ext of exts) {
1725
- payload.putBytes(ext.toKeyValuePair().serialize().toUint8Array());
1726
- }
1766
+ payload.putBytes(serializeKvpList(exts.map((ext) => ext.toKeyValuePair())).toUint8Array());
1727
1767
  }
1728
1768
  TrackExtension2.serializeInto = serializeInto;
1729
1769
  function isDeliveryTimeout(ext) {
@@ -1764,26 +1804,21 @@ var ImmutableExtensionsObjectExtension = class _ImmutableExtensionsObjectExtensi
1764
1804
  extensions;
1765
1805
  static TYPE = 11 /* ImmutableExtensions */;
1766
1806
  toKeyValuePair() {
1767
- const inner = new ByteBuffer();
1768
- for (const kvp of this.extensions) {
1769
- inner.putBytes(kvp.serialize().toUint8Array());
1770
- }
1771
- return KeyValuePair.tryNewBytes(_ImmutableExtensionsObjectExtension.TYPE, inner.toUint8Array());
1807
+ return KeyValuePair.tryNewBytes(
1808
+ _ImmutableExtensionsObjectExtension.TYPE,
1809
+ serializeKvpList(this.extensions).toUint8Array()
1810
+ );
1772
1811
  }
1773
1812
  static fromKeyValuePair(pair) {
1774
1813
  if (Number(pair.typeValue) !== _ImmutableExtensionsObjectExtension.TYPE || !(pair.value instanceof Uint8Array))
1775
1814
  return void 0;
1776
1815
  const buf = new FrozenByteBuffer5(pair.value);
1777
- const extensions = [];
1778
- while (buf.remaining > 0) {
1779
- const inner = KeyValuePair.deserialize(buf);
1780
- if (Number(inner.typeValue) === 11 /* ImmutableExtensions */) {
1781
- throw new ProtocolViolationError(
1782
- "ImmutableExtensionsObjectExtension.fromKeyValuePair",
1783
- "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1784
- );
1785
- }
1786
- extensions.push(inner);
1816
+ const extensions = deserializeKvpListUntilEmpty(buf);
1817
+ if (extensions.some((inner) => Number(inner.typeValue) === 11 /* ImmutableExtensions */)) {
1818
+ throw new ProtocolViolationError(
1819
+ "ImmutableExtensionsObjectExtension.fromKeyValuePair",
1820
+ "ImmutableExtensions must not contain nested ImmutableExtensions (0x0B)"
1821
+ );
1787
1822
  }
1788
1823
  return new _ImmutableExtensionsObjectExtension(extensions);
1789
1824
  }
@@ -1836,18 +1871,11 @@ var ObjectExtension;
1836
1871
  }
1837
1872
  ObjectExtension2.toKeyValuePair = toKeyValuePair;
1838
1873
  function deserializeAll(buf) {
1839
- const result = [];
1840
- while (buf.remaining > 0) {
1841
- const kvp = KeyValuePair.deserialize(buf);
1842
- result.push(fromKeyValuePair(kvp));
1843
- }
1844
- return result;
1874
+ return deserializeKvpListUntilEmpty(buf).map(fromKeyValuePair);
1845
1875
  }
1846
1876
  ObjectExtension2.deserializeAll = deserializeAll;
1847
1877
  function serializeInto(exts, payload) {
1848
- for (const ext of exts) {
1849
- payload.putBytes(ext.toKeyValuePair().serialize().toUint8Array());
1850
- }
1878
+ payload.putBytes(serializeKvpList(exts.map((ext) => ext.toKeyValuePair())).toUint8Array());
1851
1879
  }
1852
1880
  ObjectExtension2.serializeInto = serializeInto;
1853
1881
  function isImmutableExtensions(ext) {
@@ -1869,6 +1897,12 @@ var ObjectExtension;
1869
1897
  })(ObjectExtension || (ObjectExtension = {}));
1870
1898
 
1871
1899
  // src/model/data/subgroup_object.ts
1900
+ function normalizeExtensionHeaders(extensionHeaders) {
1901
+ if (extensionHeaders === null || extensionHeaders.length === 0) {
1902
+ return null;
1903
+ }
1904
+ return extensionHeaders;
1905
+ }
1872
1906
  var SubgroupObject = class _SubgroupObject {
1873
1907
  constructor(objectId, extensionHeaders, objectStatus, payload) {
1874
1908
  this.extensionHeaders = extensionHeaders;
@@ -1881,22 +1915,17 @@ var SubgroupObject = class _SubgroupObject {
1881
1915
  payload;
1882
1916
  objectId;
1883
1917
  static newWithStatus(objectId, extensionHeaders, objectStatus) {
1884
- return new _SubgroupObject(objectId, extensionHeaders, objectStatus, null);
1918
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), objectStatus, null);
1885
1919
  }
1886
1920
  static newWithPayload(objectId, extensionHeaders, payload) {
1887
- return new _SubgroupObject(objectId, extensionHeaders, null, payload);
1921
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), null, payload);
1888
1922
  }
1889
1923
  serialize(previousObjectId) {
1890
1924
  let objectIdDelta = previousObjectId ? this.objectId - previousObjectId - BigInt(1) : this.objectId;
1891
1925
  const buf = new ByteBuffer();
1892
1926
  buf.putVI(objectIdDelta);
1893
- const extensionHeaders = new ByteBuffer();
1894
- if (this.extensionHeaders) {
1895
- for (const header of this.extensionHeaders) {
1896
- extensionHeaders.putKeyValuePair(header);
1897
- }
1898
- const headerBytes = extensionHeaders.toUint8Array();
1899
- buf.putLengthPrefixedBytes(headerBytes);
1927
+ if (this.extensionHeaders !== null) {
1928
+ buf.putLengthPrefixedBytes(serializeKvpList(this.extensionHeaders).toUint8Array());
1900
1929
  }
1901
1930
  if (this.payload) {
1902
1931
  buf.putLengthPrefixedBytes(this.payload);
@@ -1911,12 +1940,9 @@ var SubgroupObject = class _SubgroupObject {
1911
1940
  let objectId = previousObjectId !== void 0 ? previousObjectId + objectDelta + BigInt(1) : objectDelta;
1912
1941
  let extensionHeaders = null;
1913
1942
  if (hasExtensions) {
1914
- extensionHeaders = [];
1915
1943
  const extLen = buf.getNumberVI();
1916
1944
  const headerBytes = new FrozenByteBuffer5(buf.getBytes(extLen));
1917
- while (headerBytes.remaining > 0) {
1918
- extensionHeaders.push(headerBytes.getKeyValuePair());
1919
- }
1945
+ extensionHeaders = deserializeKvpListUntilEmpty(headerBytes);
1920
1946
  }
1921
1947
  const payloadLen = buf.getNumberVI();
1922
1948
  let objectStatus = null;
@@ -1926,7 +1952,7 @@ var SubgroupObject = class _SubgroupObject {
1926
1952
  } else {
1927
1953
  payload = buf.getBytes(payloadLen);
1928
1954
  }
1929
- return new _SubgroupObject(objectId, extensionHeaders, objectStatus, payload);
1955
+ return new _SubgroupObject(objectId, normalizeExtensionHeaders(extensionHeaders), objectStatus, payload);
1930
1956
  }
1931
1957
  };
1932
1958
 
@@ -3082,9 +3108,7 @@ var PublishNamespace = class _PublishNamespace {
3082
3108
  payload.putVI(this.requestId);
3083
3109
  payload.putTuple(this.trackNamespace);
3084
3110
  payload.putVI(this.parameters.length);
3085
- for (const param of this.parameters) {
3086
- payload.putKeyValuePair(param.toKeyValuePair());
3087
- }
3111
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3088
3112
  const payloadBytes = payload.toUint8Array();
3089
3113
  if (payloadBytes.length > 65535) {
3090
3114
  throw new LengthExceedsMaxError("PublishNamespace::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -3105,10 +3129,7 @@ var PublishNamespace = class _PublishNamespace {
3105
3129
  const requestId = buf.getVI();
3106
3130
  const trackNamespace = buf.getTuple();
3107
3131
  const paramCount = buf.getNumberVI();
3108
- const rawParams = new Array(paramCount);
3109
- for (let i = 0; i < paramCount; i++) {
3110
- rawParams[i] = buf.getKeyValuePair();
3111
- }
3132
+ const rawParams = deserializeKvpList(buf, paramCount);
3112
3133
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3113
3134
  return new _PublishNamespace(requestId, trackNamespace, parameters);
3114
3135
  }
@@ -3225,9 +3246,7 @@ var ClientSetup = class _ClientSetup {
3225
3246
  buf.putVI(32 /* ClientSetup */);
3226
3247
  const payload = new ByteBuffer();
3227
3248
  payload.putVI(this.setupParameters.length);
3228
- for (const param of this.setupParameters) {
3229
- payload.putKeyValuePair(param);
3230
- }
3249
+ payload.putBytes(serializeKvpList(this.setupParameters).toUint8Array());
3231
3250
  const payloadBytes = payload.toUint8Array();
3232
3251
  if (payloadBytes.length > 65535) {
3233
3252
  throw new LengthExceedsMaxError("ClientSetup::serialize(payload_length)", 65535, payloadBytes.length);
@@ -3238,10 +3257,7 @@ var ClientSetup = class _ClientSetup {
3238
3257
  }
3239
3258
  static parsePayload(buf) {
3240
3259
  const paramCount = buf.getNumberVI();
3241
- const setupParameters = new Array(paramCount);
3242
- for (let i = 0; i < paramCount; i++) {
3243
- setupParameters[i] = buf.getKeyValuePair();
3244
- }
3260
+ const setupParameters = deserializeKvpList(buf, paramCount);
3245
3261
  return new _ClientSetup(setupParameters);
3246
3262
  }
3247
3263
  };
@@ -3280,9 +3296,7 @@ var Fetch = class _Fetch {
3280
3296
  }
3281
3297
  }
3282
3298
  payload.putVI(this.parameters.length);
3283
- for (const param of this.parameters) {
3284
- payload.putKeyValuePair(param.toKeyValuePair());
3285
- }
3299
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3286
3300
  const payloadBytes = payload.toUint8Array();
3287
3301
  if (payloadBytes.length > 65535) {
3288
3302
  throw new LengthExceedsMaxError("Fetch::serialize(payload_length)", 65535, payloadBytes.length);
@@ -3321,10 +3335,7 @@ var Fetch = class _Fetch {
3321
3335
  throw new Error(`Unknown fetch type: ${fetchType}`);
3322
3336
  }
3323
3337
  const paramCount = buf.getNumberVI();
3324
- const rawParams = new Array(paramCount);
3325
- for (let i = 0; i < paramCount; i++) {
3326
- rawParams[i] = buf.getKeyValuePair();
3327
- }
3338
+ const rawParams = deserializeKvpList(buf, paramCount);
3328
3339
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3329
3340
  return new _Fetch(requestId, props, parameters);
3330
3341
  }
@@ -3380,9 +3391,7 @@ var FetchOk = class _FetchOk {
3380
3391
  payload.putU8(this.endOfTrack ? 1 : 0);
3381
3392
  payload.putLocation(this.endLocation);
3382
3393
  payload.putVI(this.parameters.length);
3383
- for (const param of this.parameters) {
3384
- payload.putKeyValuePair(param.toKeyValuePair());
3385
- }
3394
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3386
3395
  TrackExtension.serializeInto(this.trackExtensions, payload);
3387
3396
  const payloadBytes = payload.toUint8Array();
3388
3397
  if (payloadBytes.length > 65535) {
@@ -3411,10 +3420,7 @@ var FetchOk = class _FetchOk {
3411
3420
  }
3412
3421
  const endLocation = buf.getLocation();
3413
3422
  const paramCount = buf.getNumberVI();
3414
- const rawParams = new Array(paramCount);
3415
- for (let i = 0; i < paramCount; i++) {
3416
- rawParams[i] = buf.getKeyValuePair();
3417
- }
3423
+ const rawParams = deserializeKvpList(buf, paramCount);
3418
3424
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3419
3425
  const trackExtensions = TrackExtension.deserializeAll(buf);
3420
3426
  return new _FetchOk(requestId, endOfTrack, endLocation, parameters, trackExtensions);
@@ -3526,9 +3532,7 @@ var ServerSetup = class _ServerSetup {
3526
3532
  buf.putVI(33 /* ServerSetup */);
3527
3533
  const payload = new ByteBuffer();
3528
3534
  payload.putVI(this.setupParameters.length);
3529
- for (const param of this.setupParameters) {
3530
- payload.putKeyValuePair(param);
3531
- }
3535
+ payload.putBytes(serializeKvpList(this.setupParameters).toUint8Array());
3532
3536
  const payloadBytes = payload.toUint8Array();
3533
3537
  if (payloadBytes.length > 65535) {
3534
3538
  throw new LengthExceedsMaxError("ServerSetup::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -3539,10 +3543,7 @@ var ServerSetup = class _ServerSetup {
3539
3543
  }
3540
3544
  static parsePayload(buf) {
3541
3545
  const paramCount = buf.getNumberVI();
3542
- const setupParameters = new Array(paramCount);
3543
- for (let i = 0; i < paramCount; i++) {
3544
- setupParameters[i] = buf.getKeyValuePair();
3545
- }
3546
+ const setupParameters = deserializeKvpList(buf, paramCount);
3546
3547
  return new _ServerSetup(setupParameters);
3547
3548
  }
3548
3549
  };
@@ -3585,9 +3586,7 @@ var Subscribe = class _Subscribe {
3585
3586
  payload.putVI(this.requestId);
3586
3587
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
3587
3588
  payload.putVI(this.parameters.length);
3588
- for (const param of this.parameters) {
3589
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
3590
- }
3589
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3591
3590
  const payloadBytes = payload.toUint8Array();
3592
3591
  buf.putU16(payloadBytes.length);
3593
3592
  buf.putBytes(payloadBytes);
@@ -3598,8 +3597,7 @@ var Subscribe = class _Subscribe {
3598
3597
  const fullTrackName = buf.getFullTrackName();
3599
3598
  const paramCount = Number(buf.getVI());
3600
3599
  const parameters = [];
3601
- for (let i = 0; i < paramCount; i++) {
3602
- const kvp = KeyValuePair.deserialize(buf);
3600
+ for (const kvp of deserializeKvpList(buf, paramCount)) {
3603
3601
  const param = MessageParameter.fromKeyValuePair(kvp);
3604
3602
  if (param !== void 0) parameters.push(param);
3605
3603
  }
@@ -3676,9 +3674,7 @@ var Publish = class _Publish {
3676
3674
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
3677
3675
  payload.putVI(this.trackAlias);
3678
3676
  payload.putVI(this.parameters.length);
3679
- for (const param of this.parameters) {
3680
- payload.putKeyValuePair(param.toKeyValuePair());
3681
- }
3677
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3682
3678
  TrackExtension.serializeInto(this.trackExtensions, payload);
3683
3679
  const payloadBytes = payload.toUint8Array();
3684
3680
  if (payloadBytes.length > 65535) {
@@ -3693,10 +3689,7 @@ var Publish = class _Publish {
3693
3689
  const fullTrackName = buf.getFullTrackName();
3694
3690
  const trackAlias = buf.getVI();
3695
3691
  const paramCount = buf.getNumberVI();
3696
- const rawParams = new Array(paramCount);
3697
- for (let i = 0; i < paramCount; i++) {
3698
- rawParams[i] = buf.getKeyValuePair();
3699
- }
3692
+ const rawParams = deserializeKvpList(buf, paramCount);
3700
3693
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3701
3694
  const trackExtensions = TrackExtension.deserializeAll(buf);
3702
3695
  return new _Publish(requestId, fullTrackName, trackAlias, parameters, trackExtensions);
@@ -3720,9 +3713,7 @@ var PublishOk = class _PublishOk {
3720
3713
  const payload = new ByteBuffer();
3721
3714
  payload.putVI(this.requestId);
3722
3715
  payload.putVI(this.parameters.length);
3723
- for (const param of this.parameters) {
3724
- payload.putKeyValuePair(param.toKeyValuePair());
3725
- }
3716
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3726
3717
  const payloadBytes = payload.toUint8Array();
3727
3718
  if (payloadBytes.length > 65535) {
3728
3719
  throw new LengthExceedsMaxError("PublishOk::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -3734,10 +3725,7 @@ var PublishOk = class _PublishOk {
3734
3725
  static parsePayload(buf) {
3735
3726
  const requestId = buf.getVI();
3736
3727
  const paramCount = buf.getVI();
3737
- const kvps = [];
3738
- for (let i = 0; i < paramCount; i++) {
3739
- kvps.push(buf.getKeyValuePair());
3740
- }
3728
+ const kvps = deserializeKvpList(buf, paramCount);
3741
3729
  const parameters = MessageParameters.fromKeyValuePairs(kvps);
3742
3730
  return new _PublishOk(requestId, parameters);
3743
3731
  }
@@ -3765,9 +3753,7 @@ var SubscribeOk = class _SubscribeOk {
3765
3753
  payload.putVI(this.requestId);
3766
3754
  payload.putVI(this.trackAlias);
3767
3755
  payload.putVI(this.parameters.length);
3768
- for (const param of this.parameters) {
3769
- payload.putKeyValuePair(param.toKeyValuePair());
3770
- }
3756
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3771
3757
  TrackExtension.serializeInto(this.trackExtensions, payload);
3772
3758
  const payloadBytes = payload.toUint8Array();
3773
3759
  if (payloadBytes.length > 65535) {
@@ -3781,10 +3767,7 @@ var SubscribeOk = class _SubscribeOk {
3781
3767
  const requestId = buf.getVI();
3782
3768
  const trackAlias = buf.getVI();
3783
3769
  const paramCount = buf.getNumberVI();
3784
- const rawParams = new Array(paramCount);
3785
- for (let i = 0; i < paramCount; i++) {
3786
- rawParams[i] = buf.getKeyValuePair();
3787
- }
3770
+ const rawParams = deserializeKvpList(buf, paramCount);
3788
3771
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
3789
3772
  const trackExtensions = TrackExtension.deserializeAll(buf);
3790
3773
  return new _SubscribeOk(requestId, trackAlias, parameters, trackExtensions);
@@ -3811,9 +3794,7 @@ var RequestUpdate = class _RequestUpdate {
3811
3794
  payload.putVI(this.requestId);
3812
3795
  payload.putVI(this.existingRequestId);
3813
3796
  payload.putVI(this.parameters.length);
3814
- for (const param of this.parameters) {
3815
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
3816
- }
3797
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
3817
3798
  const payloadBytes = payload.toUint8Array();
3818
3799
  if (payloadBytes.length > 65535) {
3819
3800
  throw new LengthExceedsMaxError("RequestUpdate::serialize(payloadBytes)", 65535, payloadBytes.length);
@@ -3831,8 +3812,7 @@ var RequestUpdate = class _RequestUpdate {
3831
3812
  throw new CastingError("RequestUpdate.deserialize paramCount", "bigint", "number", `${paramCountBig}`);
3832
3813
  }
3833
3814
  const parameters = [];
3834
- for (let i = 0; i < paramCount; i++) {
3835
- const kvp = KeyValuePair.deserialize(buf);
3815
+ for (const kvp of deserializeKvpList(buf, paramCount)) {
3836
3816
  const param = MessageParameter.fromKeyValuePair(kvp);
3837
3817
  if (param !== void 0) parameters.push(param);
3838
3818
  }
@@ -3989,9 +3969,7 @@ var TrackStatus = class _TrackStatus {
3989
3969
  payload.putVI(this.endGroup);
3990
3970
  }
3991
3971
  payload.putVI(this.subscribeParameters.length);
3992
- for (const param of this.subscribeParameters) {
3993
- payload.putKeyValuePair(param.toKeyValuePair());
3994
- }
3972
+ payload.putBytes(serializeKvpList(this.subscribeParameters.map((p) => p.toKeyValuePair())).toUint8Array());
3995
3973
  const payloadBytes = payload.toUint8Array();
3996
3974
  buf.putU16(payloadBytes.length);
3997
3975
  buf.putBytes(payloadBytes);
@@ -4014,10 +3992,7 @@ var TrackStatus = class _TrackStatus {
4014
3992
  endGroup = buf.getVI();
4015
3993
  }
4016
3994
  const paramCount = Number(buf.getVI());
4017
- const rawParams = new Array(paramCount);
4018
- for (let i = 0; i < paramCount; i++) {
4019
- rawParams[i] = buf.getKeyValuePair();
4020
- }
3995
+ const rawParams = deserializeKvpList(buf, paramCount);
4021
3996
  const subscribeParameters = MessageParameters.fromKeyValuePairs(rawParams);
4022
3997
  return new _TrackStatus(
4023
3998
  requestId,
@@ -4135,9 +4110,7 @@ var SubscribeNamespace = class _SubscribeNamespace {
4135
4110
  payload.putTuple(this.trackNamespacePrefix);
4136
4111
  payload.putVI(this.subscribeOptions);
4137
4112
  payload.putVI(this.parameters.length);
4138
- for (const param of this.parameters) {
4139
- payload.putKeyValuePair(param.toKeyValuePair());
4140
- }
4113
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4141
4114
  const payloadBytes = payload.toUint8Array();
4142
4115
  if (payloadBytes.length > 65535) {
4143
4116
  throw new LengthExceedsMaxError("SubscribeNamespace::serialize(payloadBytes.length)", 65535, payloadBytes.length);
@@ -4151,10 +4124,7 @@ var SubscribeNamespace = class _SubscribeNamespace {
4151
4124
  const trackNamespacePrefix = buf.getTuple();
4152
4125
  const subscribeOptions = Number(buf.getVI());
4153
4126
  const paramCount = buf.getNumberVI();
4154
- const rawParams = new Array(paramCount);
4155
- for (let i = 0; i < paramCount; i++) {
4156
- rawParams[i] = buf.getKeyValuePair();
4157
- }
4127
+ const rawParams = deserializeKvpList(buf, paramCount);
4158
4128
  const parameters = MessageParameters.fromKeyValuePairs(rawParams);
4159
4129
  return new _SubscribeNamespace(requestId, trackNamespacePrefix, subscribeOptions, parameters);
4160
4130
  }
@@ -4205,9 +4175,7 @@ var RequestOk = class _RequestOk {
4205
4175
  const payload = new ByteBuffer();
4206
4176
  payload.putVI(this.requestId);
4207
4177
  payload.putVI(BigInt(this.parameters.length));
4208
- for (const param of this.parameters) {
4209
- payload.putBytes(param.toKeyValuePair().serialize().toUint8Array());
4210
- }
4178
+ payload.putBytes(serializeKvpList(this.parameters.map((p) => p.toKeyValuePair())).toUint8Array());
4211
4179
  const payloadBytes = payload.toUint8Array();
4212
4180
  if (payloadBytes.length > 65535) {
4213
4181
  throw new LengthExceedsMaxError("RequestOk::serialize(payloadBytes)", 65535, payloadBytes.length);
@@ -4224,8 +4192,7 @@ var RequestOk = class _RequestOk {
4224
4192
  throw new CastingError("RequestOk.parsePayload numParams", "bigint", "number", `${numParamsBig}`);
4225
4193
  }
4226
4194
  const parameters = [];
4227
- for (let i = 0; i < numParams; i++) {
4228
- const kvp = KeyValuePair.deserialize(buf);
4195
+ for (const kvp of deserializeKvpList(buf, numParams)) {
4229
4196
  const param = MessageParameter.fromKeyValuePair(kvp);
4230
4197
  if (param !== void 0) parameters.push(param);
4231
4198
  }
@@ -4367,9 +4334,7 @@ var Switch = class _Switch {
4367
4334
  payload.putBytes(this.fullTrackName.serialize().toUint8Array());
4368
4335
  payload.putVI(this.subscriptionRequestId);
4369
4336
  payload.putVI(this.parameters.length);
4370
- for (const param of this.parameters) {
4371
- payload.putBytes(param.serialize().toUint8Array());
4372
- }
4337
+ payload.putBytes(serializeKvpList(this.parameters).toUint8Array());
4373
4338
  const payloadBytes = payload.toUint8Array();
4374
4339
  buf.putU16(payloadBytes.length);
4375
4340
  buf.putBytes(payloadBytes);
@@ -4380,10 +4345,7 @@ var Switch = class _Switch {
4380
4345
  const fullTrackName = buf.getFullTrackName();
4381
4346
  const subscriptionRequestId = buf.getVI();
4382
4347
  const paramCount = Number(buf.getVI());
4383
- const parameters = [];
4384
- for (let i = 0; i < paramCount; i++) {
4385
- parameters.push(KeyValuePair.deserialize(buf));
4386
- }
4348
+ const parameters = deserializeKvpList(buf, paramCount);
4387
4349
  return new _Switch(requestId, fullTrackName, subscriptionRequestId, parameters);
4388
4350
  }
4389
4351
  };
@@ -7003,7 +6965,6 @@ var MOQtailClient = class _MOQtailClient {
7003
6965
  * // Register the track for live subscription
7004
6966
  * client.addOrUpdateTrack({
7005
6967
  * fullTrackName: { namespace: ["camera"], name: "main" },
7006
- * forwardingPreference: ObjectForwardingPreference.Latest,
7007
6968
  * trackSource: { live: liveReadableStream },
7008
6969
  * publisherPriority: 0 // highest priority
7009
6970
  * });
@@ -7013,7 +6974,6 @@ var MOQtailClient = class _MOQtailClient {
7013
6974
  * const cache = new MemoryObjectCache(); // Caches are not yet fully supported
7014
6975
  * client.addOrUpdateTrack({
7015
6976
  * fullTrackName: { namespace: ["camera"], name: "main" },
7016
- * forwardingPreference: ObjectForwardingPreference.Latest,
7017
6977
  * trackSource: { live: liveReadableStream, past: cache },
7018
6978
  * publisherPriority: 8
7019
6979
  * });