@moqtap/codec 0.7.0 → 0.8.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.
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Draft-agnostic readers for decoded control messages.
3
+ *
4
+ * The codec keeps one message model per draft, because the drafts genuinely
5
+ * disagree: a track alias travels in SUBSCRIBE through draft-11 and in
6
+ * SUBSCRIBE_OK from draft-12; a request was a `subscribe_id` through draft-10
7
+ * and a `request_id` after it; a joining FETCH points at a
8
+ * `joining_subscribe_id` through draft-13 and a `joining_request_id` after it.
9
+ * Faithfulness to each draft is the point of the codec.
10
+ *
11
+ * What consumers usually want is the answer, not the archaeology — and every
12
+ * one of them that has asked has written the same field-spelling ladder by
13
+ * hand. These accessors are that ladder, written once.
14
+ *
15
+ * Each returns `undefined` when the message does not carry the field, which
16
+ * includes the cases where the draft has not assigned it yet: `trackAliasOf`
17
+ * on a draft-14 SUBSCRIBE is `undefined` because the publisher has not chosen
18
+ * the alias until SUBSCRIBE_OK.
19
+ *
20
+ * Both snake_case (what this codec emits) and camelCase spellings are read, so
21
+ * these also work on messages recovered from qlog-style traces written by
22
+ * other tools.
23
+ */
24
+
25
+ /** Any decoded control message, from any draft. */
26
+ export type AnyMessage = Readonly<Record<string, unknown>>
27
+
28
+ /** A track's full identity: the namespace tuple plus the name within it. */
29
+ export interface TrackIdentity {
30
+ readonly namespace: string[]
31
+ readonly name: string
32
+ }
33
+
34
+ /** First defined value among several spellings of one field. */
35
+ function pick(msg: AnyMessage, names: readonly string[]): unknown {
36
+ for (const name of names) {
37
+ const value = msg[name]
38
+ if (value != null) return value
39
+ }
40
+ return undefined
41
+ }
42
+
43
+ function bigintOf(value: unknown): bigint | undefined {
44
+ if (value == null) return undefined
45
+ if (typeof value === 'bigint') return value
46
+ if (typeof value === 'number' || typeof value === 'string') {
47
+ try {
48
+ return BigInt(value)
49
+ } catch {
50
+ return undefined
51
+ }
52
+ }
53
+ return undefined
54
+ }
55
+
56
+ /**
57
+ * The request this message belongs to.
58
+ *
59
+ * Named `subscribe_id` through draft-10 and `request_id` from draft-11, when
60
+ * the id space widened to cover every request type rather than subscriptions
61
+ * alone.
62
+ *
63
+ * Returns `undefined` for draft-17+ responses, which carry no id at all: each
64
+ * request there has its own bidirectional stream, and that stream — not a
65
+ * field — is what ties a response to its request.
66
+ */
67
+ export function requestIdOf(msg: AnyMessage): bigint | undefined {
68
+ return bigintOf(pick(msg, ['request_id', 'requestId', 'subscribe_id', 'subscribeId']))
69
+ }
70
+
71
+ /**
72
+ * The track alias this message assigns or refers to, if any.
73
+ *
74
+ * Which message carries it moved: SUBSCRIBE through draft-11, then SUBSCRIBE_OK
75
+ * from draft-12, when assigning the alias became the publisher's job. PUBLISH
76
+ * carries it alongside the track name in every draft that has PUBLISH.
77
+ */
78
+ export function trackAliasOf(msg: AnyMessage): bigint | undefined {
79
+ return bigintOf(pick(msg, ['track_alias', 'trackAlias']))
80
+ }
81
+
82
+ /**
83
+ * The track this message names, or `undefined` if it names none.
84
+ *
85
+ * Covers SUBSCRIBE, PUBLISH, TRACK_STATUS and a standalone FETCH — whose track
86
+ * sits under `standalone` in most drafts and flat in draft-07, which predates
87
+ * joining fetch. A joining FETCH names no track of its own; use
88
+ * {@link joiningRequestIdOf} to find the request whose track it continues.
89
+ *
90
+ * PUBLISH_NAMESPACE and its relatives are namespace-only and yield
91
+ * `undefined`: a namespace is not a track.
92
+ */
93
+ export function trackOf(msg: AnyMessage): TrackIdentity | undefined {
94
+ const source = (pick(msg, ['standalone']) ?? msg) as AnyMessage
95
+ const name = pick(source, ['track_name', 'trackName'])
96
+ if (typeof name !== 'string') return undefined
97
+ const namespace = pick(source, ['track_namespace', 'trackNamespace'])
98
+ return {
99
+ namespace: Array.isArray(namespace) ? namespace.map(String) : [],
100
+ name,
101
+ }
102
+ }
103
+
104
+ /**
105
+ * For a joining FETCH, the request whose track it continues.
106
+ *
107
+ * Spelled `joining_subscribe_id` through draft-13 and `joining_request_id`
108
+ * after it, nested under `joining` in every draft that has one.
109
+ */
110
+ export function joiningRequestIdOf(msg: AnyMessage): bigint | undefined {
111
+ const source = (pick(msg, ['joining']) ?? msg) as AnyMessage
112
+ return bigintOf(
113
+ pick(source, [
114
+ 'joining_request_id',
115
+ 'joiningRequestId',
116
+ 'joining_subscribe_id',
117
+ 'joiningSubscribeId',
118
+ ]),
119
+ )
120
+ }
@@ -333,8 +333,8 @@ function encodeSubscribePayload(
333
333
  w.writeVarInt(msg.request_id)
334
334
  w.writeTuple(msg.track_namespace)
335
335
  w.writeString(msg.track_name)
336
- w.writeUint8(Number(msg.subscriber_priority))
337
- w.writeUint8(Number(msg.group_order))
336
+ w.writeUint8(msg.subscriber_priority)
337
+ w.writeUint8(msg.group_order)
338
338
  w.writeVarInt(msg.forward)
339
339
  w.writeVarInt(msg.filter_type)
340
340
  const ft = msg.filter_type
@@ -355,7 +355,7 @@ function encodeSubscribeOkPayload(
355
355
  w.writeVarInt(msg.request_id)
356
356
  w.writeVarInt(msg.track_alias)
357
357
  w.writeVarInt(msg.expires)
358
- w.writeUint8(Number(msg.group_order))
358
+ w.writeUint8(msg.group_order)
359
359
  w.writeVarInt(msg.content_exists)
360
360
  if (msg.content_exists === 1n) {
361
361
  w.writeVarInt(msg.largest_location!.group)
@@ -373,7 +373,7 @@ function encodeSubscribeUpdatePayload(
373
373
  w.writeVarInt(msg.start_group)
374
374
  w.writeVarInt(msg.start_object)
375
375
  w.writeVarInt(msg.end_group)
376
- w.writeUint8(Number(msg.subscriber_priority))
376
+ w.writeUint8(msg.subscriber_priority)
377
377
  w.writeVarInt(msg.forward)
378
378
  encodeMessageParams(msg.parameters, w)
379
379
  }
@@ -399,7 +399,7 @@ function encodePublishPayload(msg: Draft14Message & { type: 'publish' }, w: Buff
399
399
  w.writeTuple(msg.track_namespace)
400
400
  w.writeString(msg.track_name)
401
401
  w.writeVarInt(msg.track_alias)
402
- w.writeUint8(Number(msg.group_order))
402
+ w.writeUint8(msg.group_order)
403
403
  w.writeVarInt(msg.content_exists)
404
404
  if (msg.content_exists === 1n) {
405
405
  w.writeVarInt(msg.largest_location!.group)
@@ -415,8 +415,8 @@ function encodePublishOkPayload(
415
415
  ): void {
416
416
  w.writeVarInt(msg.request_id)
417
417
  w.writeVarInt(msg.forward)
418
- w.writeUint8(Number(msg.subscriber_priority))
419
- w.writeUint8(Number(msg.group_order))
418
+ w.writeUint8(msg.subscriber_priority)
419
+ w.writeUint8(msg.group_order)
420
420
  w.writeVarInt(msg.filter_type)
421
421
  const ft = msg.filter_type
422
422
  if (ft >= FilterType.AbsoluteStart) {
@@ -525,27 +525,27 @@ function encodeUnsubscribeNamespacePayload(
525
525
 
526
526
  function encodeFetchPayload(msg: Draft14Message & { type: 'fetch' }, w: BufferWriter): void {
527
527
  w.writeVarInt(msg.request_id)
528
- w.writeUint8(Number(msg.subscriber_priority))
529
- w.writeUint8(Number(msg.group_order))
528
+ w.writeUint8(msg.subscriber_priority)
529
+ w.writeUint8(msg.group_order)
530
530
  w.writeVarInt(msg.fetch_type)
531
531
  const ft = msg.fetch_type
532
- if (ft === FetchType.Standalone) {
533
- w.writeTuple(msg.track_namespace!)
534
- w.writeString(msg.track_name!)
535
- w.writeVarInt(msg.start_group!)
536
- w.writeVarInt(msg.start_object!)
537
- w.writeVarInt(msg.end_group!)
538
- w.writeVarInt(msg.end_object!)
539
- } else {
540
- w.writeVarInt(msg.joining_request_id!)
541
- w.writeVarInt(msg.joining_start!)
532
+ if (ft === FetchType.Standalone && msg.standalone) {
533
+ w.writeTuple(msg.standalone.track_namespace)
534
+ w.writeString(msg.standalone.track_name)
535
+ w.writeVarInt(msg.standalone.start_group)
536
+ w.writeVarInt(msg.standalone.start_object)
537
+ w.writeVarInt(msg.standalone.end_group)
538
+ w.writeVarInt(msg.standalone.end_object)
539
+ } else if (msg.joining) {
540
+ w.writeVarInt(msg.joining.joining_request_id)
541
+ w.writeVarInt(msg.joining.joining_start)
542
542
  }
543
543
  encodeMessageParams(msg.parameters, w)
544
544
  }
545
545
 
546
546
  function encodeFetchOkPayload(msg: Draft14Message & { type: 'fetch_ok' }, w: BufferWriter): void {
547
547
  w.writeVarInt(msg.request_id)
548
- w.writeUint8(Number(msg.group_order))
548
+ w.writeUint8(msg.group_order)
549
549
  w.writeVarInt(msg.end_of_track)
550
550
  w.writeVarInt(msg.end_location.group)
551
551
  w.writeVarInt(msg.end_location.object)
@@ -575,8 +575,8 @@ function encodeTrackStatusPayload(
575
575
  w.writeVarInt(msg.request_id)
576
576
  w.writeTuple(msg.track_namespace)
577
577
  w.writeString(msg.track_name)
578
- w.writeUint8(Number(msg.subscriber_priority))
579
- w.writeUint8(Number(msg.group_order))
578
+ w.writeUint8(msg.subscriber_priority)
579
+ w.writeUint8(msg.group_order)
580
580
  w.writeVarInt(msg.forward)
581
581
  w.writeVarInt(msg.filter_type)
582
582
  const ft = msg.filter_type
@@ -594,7 +594,7 @@ function encodeTrackStatusOkPayload(
594
594
  w.writeVarInt(msg.request_id)
595
595
  w.writeVarInt(msg.track_alias)
596
596
  w.writeVarInt(msg.expires)
597
- w.writeUint8(Number(msg.group_order))
597
+ w.writeUint8(msg.group_order)
598
598
  w.writeVarInt(msg.content_exists)
599
599
  if (msg.content_exists === 1n) {
600
600
  w.writeVarInt(msg.largest_location!.group)
@@ -659,8 +659,8 @@ function decodeSubscribePayload(r: BufferReader): Draft14Message {
659
659
  const request_id = r.readVarInt()
660
660
  const track_namespace = r.readTuple()
661
661
  const track_name = r.readString()
662
- const subscriber_priority = BigInt(r.readUint8())
663
- const group_order = BigInt(r.readUint8())
662
+ const subscriber_priority = r.readUint8()
663
+ const group_order = r.readUint8()
664
664
  const forward = r.readVarInt()
665
665
  const filter_type = r.readVarInt()
666
666
 
@@ -705,7 +705,7 @@ function decodeSubscribeOkPayload(r: BufferReader): Draft14Message {
705
705
  const request_id = r.readVarInt()
706
706
  const track_alias = r.readVarInt()
707
707
  const expires = r.readVarInt()
708
- const group_order = BigInt(r.readUint8())
708
+ const group_order = r.readUint8()
709
709
  const content_exists = r.readVarInt()
710
710
 
711
711
  let largest_location: Location | undefined
@@ -736,7 +736,7 @@ function decodeSubscribeUpdatePayload(r: BufferReader): Draft14Message {
736
736
  const start_group = r.readVarInt()
737
737
  const start_object = r.readVarInt()
738
738
  const end_group = r.readVarInt()
739
- const subscriber_priority = BigInt(r.readUint8())
739
+ const subscriber_priority = r.readUint8()
740
740
  const forward = r.readVarInt()
741
741
  const parameters = decodeMessageParams(r)
742
742
  return {
@@ -769,7 +769,7 @@ function decodePublishPayload(r: BufferReader): Draft14Message {
769
769
  const track_namespace = r.readTuple()
770
770
  const track_name = r.readString()
771
771
  const track_alias = r.readVarInt()
772
- const group_order = BigInt(r.readUint8())
772
+ const group_order = r.readUint8()
773
773
  const content_exists = r.readVarInt()
774
774
  let largest_location: Location | undefined
775
775
  if (content_exists === 1n) {
@@ -797,8 +797,8 @@ function decodePublishPayload(r: BufferReader): Draft14Message {
797
797
  function decodePublishOkPayload(r: BufferReader): Draft14Message {
798
798
  const request_id = r.readVarInt()
799
799
  const forward = r.readVarInt()
800
- const subscriber_priority = BigInt(r.readUint8())
801
- const group_order = BigInt(r.readUint8())
800
+ const subscriber_priority = r.readUint8()
801
+ const group_order = r.readUint8()
802
802
  const filter_type = r.readVarInt()
803
803
 
804
804
  let start_group: bigint | undefined
@@ -931,8 +931,8 @@ function decodeUnsubscribeNamespacePayload(r: BufferReader): Draft14Message {
931
931
 
932
932
  function decodeFetchPayload(r: BufferReader): Draft14Message {
933
933
  const request_id = r.readVarInt()
934
- const subscriber_priority = BigInt(r.readUint8())
935
- const group_order = BigInt(r.readUint8())
934
+ const subscriber_priority = r.readUint8()
935
+ const group_order = r.readUint8()
936
936
  const fetch_type = r.readVarInt()
937
937
 
938
938
  if (fetch_type < FetchType.Standalone || fetch_type > FetchType.AbsoluteJoining) {
@@ -940,12 +940,14 @@ function decodeFetchPayload(r: BufferReader): Draft14Message {
940
940
  }
941
941
 
942
942
  if (fetch_type === FetchType.Standalone) {
943
- const track_namespace = r.readTuple()
944
- const track_name = r.readString()
945
- const start_group = r.readVarInt()
946
- const start_object = r.readVarInt()
947
- const end_group = r.readVarInt()
948
- const end_object = r.readVarInt()
943
+ const standalone = {
944
+ track_namespace: r.readTuple(),
945
+ track_name: r.readString(),
946
+ start_group: r.readVarInt(),
947
+ start_object: r.readVarInt(),
948
+ end_group: r.readVarInt(),
949
+ end_object: r.readVarInt(),
950
+ }
949
951
  const parameters = decodeMessageParams(r)
950
952
  return {
951
953
  type: 'fetch',
@@ -953,17 +955,14 @@ function decodeFetchPayload(r: BufferReader): Draft14Message {
953
955
  subscriber_priority,
954
956
  group_order,
955
957
  fetch_type,
956
- track_namespace,
957
- track_name,
958
- start_group,
959
- start_object,
960
- end_group,
961
- end_object,
958
+ standalone,
962
959
  parameters,
963
960
  }
964
961
  } else {
965
- const joining_request_id = r.readVarInt()
966
- const joining_start = r.readVarInt()
962
+ const joining = {
963
+ joining_request_id: r.readVarInt(),
964
+ joining_start: r.readVarInt(),
965
+ }
967
966
  const parameters = decodeMessageParams(r)
968
967
  return {
969
968
  type: 'fetch',
@@ -971,8 +970,7 @@ function decodeFetchPayload(r: BufferReader): Draft14Message {
971
970
  subscriber_priority,
972
971
  group_order,
973
972
  fetch_type,
974
- joining_request_id,
975
- joining_start,
973
+ joining,
976
974
  parameters,
977
975
  }
978
976
  }
@@ -980,7 +978,7 @@ function decodeFetchPayload(r: BufferReader): Draft14Message {
980
978
 
981
979
  function decodeFetchOkPayload(r: BufferReader): Draft14Message {
982
980
  const request_id = r.readVarInt()
983
- const group_order = BigInt(r.readUint8())
981
+ const group_order = r.readUint8()
984
982
  const end_of_track = r.readVarInt()
985
983
  const end_location: Location = {
986
984
  group: r.readVarInt(),
@@ -1013,8 +1011,8 @@ function decodeTrackStatusPayload(r: BufferReader): Draft14Message {
1013
1011
  const request_id = r.readVarInt()
1014
1012
  const track_namespace = r.readTuple()
1015
1013
  const track_name = r.readString()
1016
- const subscriber_priority = BigInt(r.readUint8())
1017
- const group_order = BigInt(r.readUint8())
1014
+ const subscriber_priority = r.readUint8()
1015
+ const group_order = r.readUint8()
1018
1016
  const forward = r.readVarInt()
1019
1017
  const filter_type = r.readVarInt()
1020
1018
 
@@ -1048,7 +1046,7 @@ function decodeTrackStatusOkPayload(r: BufferReader): Draft14Message {
1048
1046
  const request_id = r.readVarInt()
1049
1047
  const track_alias = r.readVarInt()
1050
1048
  const expires = r.readVarInt()
1051
- const group_order = BigInt(r.readUint8())
1049
+ const group_order = r.readUint8()
1052
1050
  const content_exists = r.readVarInt()
1053
1051
 
1054
1052
  let largest_location: Location | undefined
@@ -115,8 +115,8 @@ export interface Draft14Subscribe extends Draft14BaseMessage {
115
115
  readonly request_id: bigint
116
116
  readonly track_namespace: string[]
117
117
  readonly track_name: string
118
- readonly subscriber_priority: bigint
119
- readonly group_order: bigint
118
+ readonly subscriber_priority: number // uint8
119
+ readonly group_order: number // uint8
120
120
  readonly forward: bigint
121
121
  readonly filter_type: bigint
122
122
  readonly start_group?: bigint
@@ -130,7 +130,7 @@ export interface Draft14SubscribeOk extends Draft14BaseMessage {
130
130
  readonly request_id: bigint
131
131
  readonly track_alias: bigint
132
132
  readonly expires: bigint
133
- readonly group_order: bigint
133
+ readonly group_order: number // uint8
134
134
  readonly content_exists: bigint
135
135
  readonly largest_location?: Location
136
136
  readonly parameters: Draft14Params
@@ -143,7 +143,7 @@ export interface Draft14SubscribeUpdate extends Draft14BaseMessage {
143
143
  readonly start_group: bigint
144
144
  readonly start_object: bigint
145
145
  readonly end_group: bigint
146
- readonly subscriber_priority: bigint
146
+ readonly subscriber_priority: number // uint8
147
147
  readonly forward: bigint
148
148
  readonly parameters: Draft14Params
149
149
  }
@@ -167,7 +167,7 @@ export interface Draft14Publish extends Draft14BaseMessage {
167
167
  readonly track_namespace: string[]
168
168
  readonly track_name: string
169
169
  readonly track_alias: bigint
170
- readonly group_order: bigint
170
+ readonly group_order: number // uint8
171
171
  readonly content_exists: bigint
172
172
  readonly largest_location?: Location
173
173
  readonly forward: bigint
@@ -178,8 +178,8 @@ export interface Draft14PublishOk extends Draft14BaseMessage {
178
178
  readonly type: 'publish_ok'
179
179
  readonly request_id: bigint
180
180
  readonly forward: bigint
181
- readonly subscriber_priority: bigint
182
- readonly group_order: bigint
181
+ readonly subscriber_priority: number // uint8
182
+ readonly group_order: number // uint8
183
183
  readonly filter_type: bigint
184
184
  readonly start_group?: bigint
185
185
  readonly start_object?: bigint
@@ -261,29 +261,37 @@ export interface Draft14UnsubscribeNamespace extends Draft14BaseMessage {
261
261
  }
262
262
 
263
263
  // Fetch
264
+ export interface StandaloneFetch {
265
+ readonly track_namespace: string[]
266
+ readonly track_name: string
267
+ readonly start_group: bigint
268
+ readonly start_object: bigint
269
+ readonly end_group: bigint
270
+ readonly end_object: bigint
271
+ }
272
+
273
+ export interface JoiningFetch {
274
+ readonly joining_request_id: bigint
275
+ readonly joining_start: bigint
276
+ }
277
+
264
278
  export interface Draft14Fetch extends Draft14BaseMessage {
265
279
  readonly type: 'fetch'
266
280
  readonly request_id: bigint
267
- readonly subscriber_priority: bigint
268
- readonly group_order: bigint
281
+ readonly subscriber_priority: number // uint8
282
+ readonly group_order: number // uint8
269
283
  readonly fetch_type: bigint
270
- // Standalone fields (fetch_type=1)
271
- readonly track_namespace?: string[]
272
- readonly track_name?: string
273
- readonly start_group?: bigint
274
- readonly start_object?: bigint
275
- readonly end_group?: bigint
276
- readonly end_object?: bigint
277
- // Joining fields (fetch_type=2 or 3)
278
- readonly joining_request_id?: bigint
279
- readonly joining_start?: bigint
284
+ /** Present when fetch_type = 1 */
285
+ readonly standalone?: StandaloneFetch
286
+ /** Present when fetch_type = 2 or 3 */
287
+ readonly joining?: JoiningFetch
280
288
  readonly parameters: Draft14Params
281
289
  }
282
290
 
283
291
  export interface Draft14FetchOk extends Draft14BaseMessage {
284
292
  readonly type: 'fetch_ok'
285
293
  readonly request_id: bigint
286
- readonly group_order: bigint
294
+ readonly group_order: number // uint8
287
295
  readonly end_of_track: bigint
288
296
  readonly end_location: Location
289
297
  readonly parameters: Draft14Params
@@ -307,8 +315,8 @@ export interface Draft14TrackStatus extends Draft14BaseMessage {
307
315
  readonly request_id: bigint
308
316
  readonly track_namespace: string[]
309
317
  readonly track_name: string
310
- readonly subscriber_priority: bigint
311
- readonly group_order: bigint
318
+ readonly subscriber_priority: number // uint8
319
+ readonly group_order: number // uint8
312
320
  readonly forward: bigint
313
321
  readonly filter_type: bigint
314
322
  readonly start_group?: bigint
@@ -321,7 +329,7 @@ export interface Draft14TrackStatusOk extends Draft14BaseMessage {
321
329
  readonly request_id: bigint
322
330
  readonly track_alias: bigint
323
331
  readonly expires: bigint
324
- readonly group_order: bigint
332
+ readonly group_order: number // uint8
325
333
  readonly content_exists: bigint
326
334
  readonly largest_location?: Location
327
335
  readonly parameters: Draft14Params
package/src/index.ts CHANGED
@@ -151,6 +151,14 @@ export function createCodec(
151
151
  }
152
152
  }
153
153
 
154
+ // Draft-agnostic readers for decoded messages
155
+ export type { AnyMessage, TrackIdentity } from './core/accessors.js'
156
+ export {
157
+ joiningRequestIdOf,
158
+ requestIdOf,
159
+ trackAliasOf,
160
+ trackOf,
161
+ } from './core/accessors.js'
154
162
  export type {
155
163
  BaseCodec,
156
164
  CodecOptions,