@moqtap/codec 0.1.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/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/chunk-23YG7F46.js +764 -0
- package/dist/chunk-2NARXGVA.cjs +194 -0
- package/dist/chunk-3BSZ55L3.cjs +307 -0
- package/dist/chunk-5WFXFLL4.cjs +1185 -0
- package/dist/chunk-DC4L6ZIT.js +307 -0
- package/dist/chunk-GDRGWFEK.cjs +498 -0
- package/dist/chunk-IQPDRQVC.js +1185 -0
- package/dist/chunk-QYG6KGOV.cjs +101 -0
- package/dist/chunk-UOBWHJA5.js +101 -0
- package/dist/chunk-WNTXF3DE.cjs +764 -0
- package/dist/chunk-YBSEOSSP.js +194 -0
- package/dist/chunk-YPXLV5YK.js +498 -0
- package/dist/codec-CTvFtQQI.d.cts +86 -0
- package/dist/codec-qPzfmLNu.d.ts +86 -0
- package/dist/draft14-session.cjs +6 -0
- package/dist/draft14-session.d.cts +8 -0
- package/dist/draft14-session.d.ts +8 -0
- package/dist/draft14-session.js +6 -0
- package/dist/draft14.cjs +121 -0
- package/dist/draft14.d.cts +96 -0
- package/dist/draft14.d.ts +96 -0
- package/dist/draft14.js +121 -0
- package/dist/draft7-session.cjs +7 -0
- package/dist/draft7-session.d.cts +7 -0
- package/dist/draft7-session.d.ts +7 -0
- package/dist/draft7-session.js +7 -0
- package/dist/draft7.cjs +60 -0
- package/dist/draft7.d.cts +72 -0
- package/dist/draft7.d.ts +72 -0
- package/dist/draft7.js +60 -0
- package/dist/index.cjs +40 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +40 -0
- package/dist/session-types-B9NIf7_F.d.ts +101 -0
- package/dist/session-types-CCo-oA-d.d.cts +101 -0
- package/dist/session.cjs +27 -0
- package/dist/session.d.cts +24 -0
- package/dist/session.d.ts +24 -0
- package/dist/session.js +27 -0
- package/dist/types-CIk5W10V.d.cts +249 -0
- package/dist/types-CIk5W10V.d.ts +249 -0
- package/dist/types-ClXELFGN.d.cts +241 -0
- package/dist/types-ClXELFGN.d.ts +241 -0
- package/package.json +84 -0
- package/src/core/buffer-reader.ts +107 -0
- package/src/core/buffer-writer.ts +91 -0
- package/src/core/errors.ts +1 -0
- package/src/core/session-types.ts +103 -0
- package/src/core/types.ts +363 -0
- package/src/drafts/draft07/announce-fsm.ts +2 -0
- package/src/drafts/draft07/codec.ts +874 -0
- package/src/drafts/draft07/index.ts +70 -0
- package/src/drafts/draft07/messages.ts +44 -0
- package/src/drafts/draft07/parameters.ts +12 -0
- package/src/drafts/draft07/rules.ts +75 -0
- package/src/drafts/draft07/session-fsm.ts +353 -0
- package/src/drafts/draft07/session.ts +21 -0
- package/src/drafts/draft07/subscription-fsm.ts +3 -0
- package/src/drafts/draft07/varint.ts +23 -0
- package/src/drafts/draft14/codec.ts +1330 -0
- package/src/drafts/draft14/index.ts +132 -0
- package/src/drafts/draft14/messages.ts +76 -0
- package/src/drafts/draft14/rules.ts +70 -0
- package/src/drafts/draft14/session-fsm.ts +480 -0
- package/src/drafts/draft14/session.ts +26 -0
- package/src/drafts/draft14/types.ts +365 -0
- package/src/index.ts +85 -0
- package/src/session.ts +58 -0
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BufferReader,
|
|
3
|
+
BufferWriter,
|
|
4
|
+
DecodeError
|
|
5
|
+
} from "./chunk-YBSEOSSP.js";
|
|
6
|
+
|
|
7
|
+
// src/drafts/draft07/messages.ts
|
|
8
|
+
var MESSAGE_TYPE_IDS = {
|
|
9
|
+
// Control messages (on control stream)
|
|
10
|
+
subscribe_update: 0x02n,
|
|
11
|
+
subscribe: 0x03n,
|
|
12
|
+
subscribe_ok: 0x04n,
|
|
13
|
+
subscribe_error: 0x05n,
|
|
14
|
+
announce: 0x06n,
|
|
15
|
+
announce_ok: 0x07n,
|
|
16
|
+
announce_error: 0x08n,
|
|
17
|
+
unannounce: 0x09n,
|
|
18
|
+
unsubscribe: 0x0an,
|
|
19
|
+
subscribe_done: 0x0bn,
|
|
20
|
+
announce_cancel: 0x0cn,
|
|
21
|
+
track_status_request: 0x0dn,
|
|
22
|
+
track_status: 0x0en,
|
|
23
|
+
goaway: 0x10n,
|
|
24
|
+
subscribe_announces: 0x11n,
|
|
25
|
+
subscribe_announces_ok: 0x12n,
|
|
26
|
+
subscribe_announces_error: 0x13n,
|
|
27
|
+
unsubscribe_announces: 0x14n,
|
|
28
|
+
max_subscribe_id: 0x15n,
|
|
29
|
+
fetch: 0x16n,
|
|
30
|
+
fetch_cancel: 0x17n,
|
|
31
|
+
fetch_ok: 0x18n,
|
|
32
|
+
fetch_error: 0x19n,
|
|
33
|
+
client_setup: 0x40n,
|
|
34
|
+
server_setup: 0x41n,
|
|
35
|
+
// Data stream messages
|
|
36
|
+
object_stream: 0x00n,
|
|
37
|
+
object_datagram: 0x01n,
|
|
38
|
+
stream_header_track: 0x50n,
|
|
39
|
+
stream_header_group: 0x51n,
|
|
40
|
+
stream_header_subgroup: 0x04n
|
|
41
|
+
// Note: same ID as subscribe_ok but used on data streams, context disambiguates
|
|
42
|
+
};
|
|
43
|
+
var MESSAGE_ID_TO_TYPE = /* @__PURE__ */ new Map();
|
|
44
|
+
for (const [name, id] of Object.entries(MESSAGE_TYPE_IDS)) {
|
|
45
|
+
if (!MESSAGE_ID_TO_TYPE.has(id) || name !== "stream_header_subgroup") {
|
|
46
|
+
MESSAGE_ID_TO_TYPE.set(id, name);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/drafts/draft07/varint.ts
|
|
51
|
+
function encodeVarInt(value) {
|
|
52
|
+
const writer = new BufferWriter(8);
|
|
53
|
+
writer.writeVarInt(value);
|
|
54
|
+
return writer.finish();
|
|
55
|
+
}
|
|
56
|
+
function decodeVarInt(bytes, offset = 0) {
|
|
57
|
+
try {
|
|
58
|
+
const reader = new BufferReader(bytes, offset);
|
|
59
|
+
const value = reader.readVarInt();
|
|
60
|
+
return { ok: true, value, bytesRead: reader.offset - offset };
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (e instanceof DecodeError) {
|
|
63
|
+
return { ok: false, error: e };
|
|
64
|
+
}
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/drafts/draft07/codec.ts
|
|
70
|
+
var FILTER_TYPE_TO_WIRE = {
|
|
71
|
+
latest_group: 1n,
|
|
72
|
+
latest_object: 2n,
|
|
73
|
+
absolute_start: 3n,
|
|
74
|
+
absolute_range: 4n
|
|
75
|
+
};
|
|
76
|
+
var WIRE_TO_FILTER_TYPE = /* @__PURE__ */ new Map([
|
|
77
|
+
[1n, "latest_group"],
|
|
78
|
+
[2n, "latest_object"],
|
|
79
|
+
[3n, "absolute_start"],
|
|
80
|
+
[4n, "absolute_range"]
|
|
81
|
+
]);
|
|
82
|
+
var GROUP_ORDER_TO_WIRE = {
|
|
83
|
+
original: 0,
|
|
84
|
+
ascending: 1,
|
|
85
|
+
descending: 2
|
|
86
|
+
};
|
|
87
|
+
var WIRE_TO_GROUP_ORDER = /* @__PURE__ */ new Map([
|
|
88
|
+
[0, "original"],
|
|
89
|
+
[1, "ascending"],
|
|
90
|
+
[2, "descending"]
|
|
91
|
+
]);
|
|
92
|
+
function writeGroupOrder(writer, value) {
|
|
93
|
+
const wire = GROUP_ORDER_TO_WIRE[value];
|
|
94
|
+
writer.writeUint8(wire);
|
|
95
|
+
}
|
|
96
|
+
function readGroupOrder(reader) {
|
|
97
|
+
const wire = reader.readUint8();
|
|
98
|
+
const value = WIRE_TO_GROUP_ORDER.get(wire);
|
|
99
|
+
if (value === void 0) {
|
|
100
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid group order value: ${wire}`, reader.offset - 1);
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
var DATA_STREAM_TYPE_IDS = /* @__PURE__ */ new Set([
|
|
105
|
+
MESSAGE_TYPE_IDS.object_stream,
|
|
106
|
+
MESSAGE_TYPE_IDS.object_datagram,
|
|
107
|
+
MESSAGE_TYPE_IDS.stream_header_track,
|
|
108
|
+
MESSAGE_TYPE_IDS.stream_header_group
|
|
109
|
+
]);
|
|
110
|
+
function encodeClientSetup(msg, writer) {
|
|
111
|
+
writer.writeVarInt(msg.supportedVersions.length);
|
|
112
|
+
for (const version of msg.supportedVersions) {
|
|
113
|
+
writer.writeVarInt(version);
|
|
114
|
+
}
|
|
115
|
+
writer.writeParameters(msg.parameters);
|
|
116
|
+
}
|
|
117
|
+
function encodeServerSetup(msg, writer) {
|
|
118
|
+
writer.writeVarInt(msg.selectedVersion);
|
|
119
|
+
writer.writeParameters(msg.parameters);
|
|
120
|
+
}
|
|
121
|
+
function encodeSubscribe(msg, writer) {
|
|
122
|
+
writer.writeVarInt(msg.subscribeId);
|
|
123
|
+
writer.writeVarInt(msg.trackAlias);
|
|
124
|
+
writer.writeTuple(msg.trackNamespace);
|
|
125
|
+
writer.writeString(msg.trackName);
|
|
126
|
+
writer.writeUint8(msg.subscriberPriority);
|
|
127
|
+
writeGroupOrder(writer, msg.groupOrder);
|
|
128
|
+
writer.writeVarInt(FILTER_TYPE_TO_WIRE[msg.filterType]);
|
|
129
|
+
if (msg.filterType === "absolute_start" || msg.filterType === "absolute_range") {
|
|
130
|
+
writer.writeVarInt(msg.startGroup);
|
|
131
|
+
writer.writeVarInt(msg.startObject);
|
|
132
|
+
}
|
|
133
|
+
if (msg.filterType === "absolute_range") {
|
|
134
|
+
writer.writeVarInt(msg.endGroup);
|
|
135
|
+
writer.writeVarInt(msg.endObject);
|
|
136
|
+
}
|
|
137
|
+
writer.writeParameters(msg.parameters);
|
|
138
|
+
}
|
|
139
|
+
function encodeSubscribeOk(msg, writer) {
|
|
140
|
+
writer.writeVarInt(msg.subscribeId);
|
|
141
|
+
writer.writeVarInt(msg.expires);
|
|
142
|
+
writeGroupOrder(writer, msg.groupOrder);
|
|
143
|
+
writer.writeUint8(msg.contentExists ? 1 : 0);
|
|
144
|
+
if (msg.contentExists) {
|
|
145
|
+
writer.writeVarInt(msg.largestGroupId);
|
|
146
|
+
writer.writeVarInt(msg.largestObjectId);
|
|
147
|
+
}
|
|
148
|
+
writer.writeParameters(msg.parameters);
|
|
149
|
+
}
|
|
150
|
+
function encodeSubscribeError(msg, writer) {
|
|
151
|
+
writer.writeVarInt(msg.subscribeId);
|
|
152
|
+
writer.writeVarInt(msg.errorCode);
|
|
153
|
+
writer.writeString(msg.reasonPhrase);
|
|
154
|
+
writer.writeVarInt(msg.trackAlias);
|
|
155
|
+
}
|
|
156
|
+
function encodeSubscribeDone(msg, writer) {
|
|
157
|
+
writer.writeVarInt(msg.subscribeId);
|
|
158
|
+
writer.writeVarInt(msg.statusCode);
|
|
159
|
+
writer.writeString(msg.reasonPhrase);
|
|
160
|
+
writer.writeUint8(msg.contentExists ? 1 : 0);
|
|
161
|
+
if (msg.contentExists) {
|
|
162
|
+
writer.writeVarInt(msg.finalGroupId);
|
|
163
|
+
writer.writeVarInt(msg.finalObjectId);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function encodeSubscribeUpdate(msg, writer) {
|
|
167
|
+
writer.writeVarInt(msg.subscribeId);
|
|
168
|
+
writer.writeVarInt(msg.startGroup);
|
|
169
|
+
writer.writeVarInt(msg.startObject);
|
|
170
|
+
writer.writeVarInt(msg.endGroup);
|
|
171
|
+
writer.writeVarInt(msg.endObject);
|
|
172
|
+
writer.writeUint8(msg.subscriberPriority);
|
|
173
|
+
writer.writeParameters(msg.parameters);
|
|
174
|
+
}
|
|
175
|
+
function encodeUnsubscribe(msg, writer) {
|
|
176
|
+
writer.writeVarInt(msg.subscribeId);
|
|
177
|
+
}
|
|
178
|
+
function encodeAnnounce(msg, writer) {
|
|
179
|
+
writer.writeTuple(msg.trackNamespace);
|
|
180
|
+
writer.writeParameters(msg.parameters);
|
|
181
|
+
}
|
|
182
|
+
function encodeAnnounceOk(msg, writer) {
|
|
183
|
+
writer.writeTuple(msg.trackNamespace);
|
|
184
|
+
}
|
|
185
|
+
function encodeAnnounceError(msg, writer) {
|
|
186
|
+
writer.writeTuple(msg.trackNamespace);
|
|
187
|
+
writer.writeVarInt(msg.errorCode);
|
|
188
|
+
writer.writeString(msg.reasonPhrase);
|
|
189
|
+
}
|
|
190
|
+
function encodeAnnounceCancel(msg, writer) {
|
|
191
|
+
writer.writeTuple(msg.trackNamespace);
|
|
192
|
+
writer.writeVarInt(msg.errorCode);
|
|
193
|
+
writer.writeString(msg.reasonPhrase);
|
|
194
|
+
}
|
|
195
|
+
function encodeUnannounce(msg, writer) {
|
|
196
|
+
writer.writeTuple(msg.trackNamespace);
|
|
197
|
+
}
|
|
198
|
+
function encodeTrackStatusRequest(msg, writer) {
|
|
199
|
+
writer.writeTuple(msg.trackNamespace);
|
|
200
|
+
writer.writeString(msg.trackName);
|
|
201
|
+
}
|
|
202
|
+
function encodeTrackStatus(msg, writer) {
|
|
203
|
+
writer.writeTuple(msg.trackNamespace);
|
|
204
|
+
writer.writeString(msg.trackName);
|
|
205
|
+
writer.writeVarInt(msg.statusCode);
|
|
206
|
+
writer.writeVarInt(msg.lastGroupId);
|
|
207
|
+
writer.writeVarInt(msg.lastObjectId);
|
|
208
|
+
}
|
|
209
|
+
function encodeGoAway(msg, writer) {
|
|
210
|
+
writer.writeString(msg.newSessionUri);
|
|
211
|
+
}
|
|
212
|
+
function encodeSubscribeAnnounces(msg, writer) {
|
|
213
|
+
writer.writeTuple(msg.trackNamespace);
|
|
214
|
+
writer.writeParameters(msg.parameters);
|
|
215
|
+
}
|
|
216
|
+
function encodeSubscribeAnnouncesOk(msg, writer) {
|
|
217
|
+
writer.writeTuple(msg.trackNamespace);
|
|
218
|
+
}
|
|
219
|
+
function encodeSubscribeAnnouncesError(msg, writer) {
|
|
220
|
+
writer.writeTuple(msg.trackNamespace);
|
|
221
|
+
writer.writeVarInt(msg.errorCode);
|
|
222
|
+
writer.writeString(msg.reasonPhrase);
|
|
223
|
+
}
|
|
224
|
+
function encodeUnsubscribeAnnounces(msg, writer) {
|
|
225
|
+
writer.writeTuple(msg.trackNamespace);
|
|
226
|
+
}
|
|
227
|
+
function encodeMaxSubscribeId(msg, writer) {
|
|
228
|
+
writer.writeVarInt(msg.subscribeId);
|
|
229
|
+
}
|
|
230
|
+
function encodeFetch(msg, writer) {
|
|
231
|
+
writer.writeVarInt(msg.subscribeId);
|
|
232
|
+
writer.writeTuple(msg.trackNamespace);
|
|
233
|
+
writer.writeString(msg.trackName);
|
|
234
|
+
writer.writeUint8(msg.subscriberPriority);
|
|
235
|
+
writeGroupOrder(writer, msg.groupOrder);
|
|
236
|
+
writer.writeVarInt(msg.startGroup);
|
|
237
|
+
writer.writeVarInt(msg.startObject);
|
|
238
|
+
writer.writeVarInt(msg.endGroup);
|
|
239
|
+
writer.writeVarInt(msg.endObject);
|
|
240
|
+
writer.writeParameters(msg.parameters);
|
|
241
|
+
}
|
|
242
|
+
function encodeFetchOk(msg, writer) {
|
|
243
|
+
writer.writeVarInt(msg.subscribeId);
|
|
244
|
+
writeGroupOrder(writer, msg.groupOrder);
|
|
245
|
+
writer.writeUint8(msg.endOfTrack ? 1 : 0);
|
|
246
|
+
writer.writeVarInt(msg.largestGroupId);
|
|
247
|
+
writer.writeVarInt(msg.largestObjectId);
|
|
248
|
+
writer.writeParameters(msg.parameters);
|
|
249
|
+
}
|
|
250
|
+
function encodeFetchError(msg, writer) {
|
|
251
|
+
writer.writeVarInt(msg.subscribeId);
|
|
252
|
+
writer.writeVarInt(msg.errorCode);
|
|
253
|
+
writer.writeString(msg.reasonPhrase);
|
|
254
|
+
}
|
|
255
|
+
function encodeFetchCancel(msg, writer) {
|
|
256
|
+
writer.writeVarInt(msg.subscribeId);
|
|
257
|
+
}
|
|
258
|
+
function encodeObjectStream(msg, writer) {
|
|
259
|
+
writer.writeVarInt(MESSAGE_TYPE_IDS.object_stream);
|
|
260
|
+
writer.writeVarInt(msg.subscribeId);
|
|
261
|
+
writer.writeVarInt(msg.trackAlias);
|
|
262
|
+
writer.writeVarInt(msg.groupId);
|
|
263
|
+
writer.writeVarInt(msg.objectId);
|
|
264
|
+
writer.writeUint8(msg.publisherPriority);
|
|
265
|
+
if (msg.objectStatus !== void 0) {
|
|
266
|
+
writer.writeVarInt(msg.objectStatus);
|
|
267
|
+
} else {
|
|
268
|
+
writer.writeVarInt(0);
|
|
269
|
+
}
|
|
270
|
+
writer.writeBytes(msg.payload);
|
|
271
|
+
}
|
|
272
|
+
function encodeObjectDatagram(msg, writer) {
|
|
273
|
+
writer.writeVarInt(MESSAGE_TYPE_IDS.object_datagram);
|
|
274
|
+
writer.writeVarInt(msg.subscribeId);
|
|
275
|
+
writer.writeVarInt(msg.trackAlias);
|
|
276
|
+
writer.writeVarInt(msg.groupId);
|
|
277
|
+
writer.writeVarInt(msg.objectId);
|
|
278
|
+
writer.writeUint8(msg.publisherPriority);
|
|
279
|
+
if (msg.objectStatus !== void 0) {
|
|
280
|
+
writer.writeVarInt(msg.objectStatus);
|
|
281
|
+
} else {
|
|
282
|
+
writer.writeVarInt(0);
|
|
283
|
+
}
|
|
284
|
+
writer.writeBytes(msg.payload);
|
|
285
|
+
}
|
|
286
|
+
function encodeStreamHeaderTrack(msg, writer) {
|
|
287
|
+
writer.writeVarInt(MESSAGE_TYPE_IDS.stream_header_track);
|
|
288
|
+
writer.writeVarInt(msg.subscribeId);
|
|
289
|
+
writer.writeVarInt(msg.trackAlias);
|
|
290
|
+
writer.writeUint8(msg.publisherPriority);
|
|
291
|
+
}
|
|
292
|
+
function encodeStreamHeaderGroup(msg, writer) {
|
|
293
|
+
writer.writeVarInt(MESSAGE_TYPE_IDS.stream_header_group);
|
|
294
|
+
writer.writeVarInt(msg.subscribeId);
|
|
295
|
+
writer.writeVarInt(msg.trackAlias);
|
|
296
|
+
writer.writeVarInt(msg.groupId);
|
|
297
|
+
writer.writeUint8(msg.publisherPriority);
|
|
298
|
+
}
|
|
299
|
+
function encodeStreamHeaderSubgroup(msg, writer) {
|
|
300
|
+
writer.writeVarInt(MESSAGE_TYPE_IDS.stream_header_subgroup);
|
|
301
|
+
writer.writeVarInt(msg.subscribeId);
|
|
302
|
+
writer.writeVarInt(msg.trackAlias);
|
|
303
|
+
writer.writeVarInt(msg.groupId);
|
|
304
|
+
writer.writeVarInt(msg.subgroupId);
|
|
305
|
+
writer.writeUint8(msg.publisherPriority);
|
|
306
|
+
}
|
|
307
|
+
var controlEncoders = {
|
|
308
|
+
client_setup: encodeClientSetup,
|
|
309
|
+
server_setup: encodeServerSetup,
|
|
310
|
+
subscribe: encodeSubscribe,
|
|
311
|
+
subscribe_ok: encodeSubscribeOk,
|
|
312
|
+
subscribe_error: encodeSubscribeError,
|
|
313
|
+
subscribe_done: encodeSubscribeDone,
|
|
314
|
+
subscribe_update: encodeSubscribeUpdate,
|
|
315
|
+
unsubscribe: encodeUnsubscribe,
|
|
316
|
+
announce: encodeAnnounce,
|
|
317
|
+
announce_ok: encodeAnnounceOk,
|
|
318
|
+
announce_error: encodeAnnounceError,
|
|
319
|
+
announce_cancel: encodeAnnounceCancel,
|
|
320
|
+
unannounce: encodeUnannounce,
|
|
321
|
+
track_status_request: encodeTrackStatusRequest,
|
|
322
|
+
track_status: encodeTrackStatus,
|
|
323
|
+
goaway: encodeGoAway,
|
|
324
|
+
subscribe_announces: encodeSubscribeAnnounces,
|
|
325
|
+
subscribe_announces_ok: encodeSubscribeAnnouncesOk,
|
|
326
|
+
subscribe_announces_error: encodeSubscribeAnnouncesError,
|
|
327
|
+
unsubscribe_announces: encodeUnsubscribeAnnounces,
|
|
328
|
+
max_subscribe_id: encodeMaxSubscribeId,
|
|
329
|
+
fetch: encodeFetch,
|
|
330
|
+
fetch_ok: encodeFetchOk,
|
|
331
|
+
fetch_error: encodeFetchError,
|
|
332
|
+
fetch_cancel: encodeFetchCancel
|
|
333
|
+
};
|
|
334
|
+
var dataStreamEncoders = {
|
|
335
|
+
object_stream: encodeObjectStream,
|
|
336
|
+
object_datagram: encodeObjectDatagram,
|
|
337
|
+
stream_header_track: encodeStreamHeaderTrack,
|
|
338
|
+
stream_header_group: encodeStreamHeaderGroup,
|
|
339
|
+
stream_header_subgroup: encodeStreamHeaderSubgroup
|
|
340
|
+
};
|
|
341
|
+
function decodeClientSetup(reader) {
|
|
342
|
+
const numVersions = reader.readVarInt();
|
|
343
|
+
if (numVersions === 0n) {
|
|
344
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", "supported_versions must not be empty", reader.offset);
|
|
345
|
+
}
|
|
346
|
+
const supportedVersions = [];
|
|
347
|
+
for (let i = 0n; i < numVersions; i++) {
|
|
348
|
+
supportedVersions.push(reader.readVarInt());
|
|
349
|
+
}
|
|
350
|
+
const parameters = reader.readParameters();
|
|
351
|
+
return { type: "client_setup", supportedVersions, parameters };
|
|
352
|
+
}
|
|
353
|
+
function decodeServerSetup(reader) {
|
|
354
|
+
const selectedVersion = reader.readVarInt();
|
|
355
|
+
const parameters = reader.readParameters();
|
|
356
|
+
return { type: "server_setup", selectedVersion, parameters };
|
|
357
|
+
}
|
|
358
|
+
function decodeSubscribe(reader) {
|
|
359
|
+
const subscribeId = reader.readVarInt();
|
|
360
|
+
const trackAlias = reader.readVarInt();
|
|
361
|
+
const trackNamespace = reader.readTuple();
|
|
362
|
+
const trackName = reader.readString();
|
|
363
|
+
const subscriberPriority = reader.readUint8();
|
|
364
|
+
const groupOrder = readGroupOrder(reader);
|
|
365
|
+
const filterTypeWire = reader.readVarInt();
|
|
366
|
+
const filterType = WIRE_TO_FILTER_TYPE.get(filterTypeWire);
|
|
367
|
+
if (filterType === void 0) {
|
|
368
|
+
throw new DecodeError("CONSTRAINT_VIOLATION", `Invalid filter type: ${filterTypeWire}`, reader.offset);
|
|
369
|
+
}
|
|
370
|
+
const base = {
|
|
371
|
+
type: "subscribe",
|
|
372
|
+
subscribeId,
|
|
373
|
+
trackAlias,
|
|
374
|
+
trackNamespace,
|
|
375
|
+
trackName,
|
|
376
|
+
subscriberPriority,
|
|
377
|
+
groupOrder,
|
|
378
|
+
filterType,
|
|
379
|
+
parameters: void 0
|
|
380
|
+
};
|
|
381
|
+
if (filterType === "absolute_start") {
|
|
382
|
+
const startGroup = reader.readVarInt();
|
|
383
|
+
const startObject = reader.readVarInt();
|
|
384
|
+
base.parameters = reader.readParameters();
|
|
385
|
+
return { ...base, startGroup, startObject };
|
|
386
|
+
}
|
|
387
|
+
if (filterType === "absolute_range") {
|
|
388
|
+
const startGroup = reader.readVarInt();
|
|
389
|
+
const startObject = reader.readVarInt();
|
|
390
|
+
const endGroup = reader.readVarInt();
|
|
391
|
+
const endObject = reader.readVarInt();
|
|
392
|
+
base.parameters = reader.readParameters();
|
|
393
|
+
return { ...base, startGroup, startObject, endGroup, endObject };
|
|
394
|
+
}
|
|
395
|
+
base.parameters = reader.readParameters();
|
|
396
|
+
return base;
|
|
397
|
+
}
|
|
398
|
+
function decodeSubscribeOk(reader) {
|
|
399
|
+
const subscribeId = reader.readVarInt();
|
|
400
|
+
const expires = reader.readVarInt();
|
|
401
|
+
const groupOrder = readGroupOrder(reader);
|
|
402
|
+
const contentExistsWire = reader.readUint8();
|
|
403
|
+
const contentExists = contentExistsWire !== 0;
|
|
404
|
+
if (contentExists) {
|
|
405
|
+
const largestGroupId = reader.readVarInt();
|
|
406
|
+
const largestObjectId = reader.readVarInt();
|
|
407
|
+
const parameters2 = reader.readParameters();
|
|
408
|
+
return { type: "subscribe_ok", subscribeId, expires, groupOrder, contentExists, largestGroupId, largestObjectId, parameters: parameters2 };
|
|
409
|
+
}
|
|
410
|
+
const parameters = reader.readParameters();
|
|
411
|
+
return { type: "subscribe_ok", subscribeId, expires, groupOrder, contentExists, parameters };
|
|
412
|
+
}
|
|
413
|
+
function decodeSubscribeError(reader) {
|
|
414
|
+
const subscribeId = reader.readVarInt();
|
|
415
|
+
const errorCode = reader.readVarInt();
|
|
416
|
+
const reasonPhrase = reader.readString();
|
|
417
|
+
const trackAlias = reader.readVarInt();
|
|
418
|
+
return { type: "subscribe_error", subscribeId, errorCode, reasonPhrase, trackAlias };
|
|
419
|
+
}
|
|
420
|
+
function decodeSubscribeDone(reader) {
|
|
421
|
+
const subscribeId = reader.readVarInt();
|
|
422
|
+
const statusCode = reader.readVarInt();
|
|
423
|
+
const reasonPhrase = reader.readString();
|
|
424
|
+
const contentExistsWire = reader.readUint8();
|
|
425
|
+
const contentExists = contentExistsWire !== 0;
|
|
426
|
+
if (contentExists) {
|
|
427
|
+
const finalGroupId = reader.readVarInt();
|
|
428
|
+
const finalObjectId = reader.readVarInt();
|
|
429
|
+
return { type: "subscribe_done", subscribeId, statusCode, reasonPhrase, contentExists, finalGroupId, finalObjectId };
|
|
430
|
+
}
|
|
431
|
+
return { type: "subscribe_done", subscribeId, statusCode, reasonPhrase, contentExists };
|
|
432
|
+
}
|
|
433
|
+
function decodeSubscribeUpdate(reader) {
|
|
434
|
+
const subscribeId = reader.readVarInt();
|
|
435
|
+
const startGroup = reader.readVarInt();
|
|
436
|
+
const startObject = reader.readVarInt();
|
|
437
|
+
const endGroup = reader.readVarInt();
|
|
438
|
+
const endObject = reader.readVarInt();
|
|
439
|
+
const subscriberPriority = reader.readUint8();
|
|
440
|
+
const parameters = reader.readParameters();
|
|
441
|
+
return { type: "subscribe_update", subscribeId, startGroup, startObject, endGroup, endObject, subscriberPriority, parameters };
|
|
442
|
+
}
|
|
443
|
+
function decodeUnsubscribe(reader) {
|
|
444
|
+
const subscribeId = reader.readVarInt();
|
|
445
|
+
return { type: "unsubscribe", subscribeId };
|
|
446
|
+
}
|
|
447
|
+
function decodeAnnounce(reader) {
|
|
448
|
+
const trackNamespace = reader.readTuple();
|
|
449
|
+
const parameters = reader.readParameters();
|
|
450
|
+
return { type: "announce", trackNamespace, parameters };
|
|
451
|
+
}
|
|
452
|
+
function decodeAnnounceOk(reader) {
|
|
453
|
+
const trackNamespace = reader.readTuple();
|
|
454
|
+
return { type: "announce_ok", trackNamespace };
|
|
455
|
+
}
|
|
456
|
+
function decodeAnnounceError(reader) {
|
|
457
|
+
const trackNamespace = reader.readTuple();
|
|
458
|
+
const errorCode = reader.readVarInt();
|
|
459
|
+
const reasonPhrase = reader.readString();
|
|
460
|
+
return { type: "announce_error", trackNamespace, errorCode, reasonPhrase };
|
|
461
|
+
}
|
|
462
|
+
function decodeAnnounceCancel(reader) {
|
|
463
|
+
const trackNamespace = reader.readTuple();
|
|
464
|
+
const errorCode = reader.readVarInt();
|
|
465
|
+
const reasonPhrase = reader.readString();
|
|
466
|
+
return { type: "announce_cancel", trackNamespace, errorCode, reasonPhrase };
|
|
467
|
+
}
|
|
468
|
+
function decodeUnannounce(reader) {
|
|
469
|
+
const trackNamespace = reader.readTuple();
|
|
470
|
+
return { type: "unannounce", trackNamespace };
|
|
471
|
+
}
|
|
472
|
+
function decodeTrackStatusRequest(reader) {
|
|
473
|
+
const trackNamespace = reader.readTuple();
|
|
474
|
+
const trackName = reader.readString();
|
|
475
|
+
return { type: "track_status_request", trackNamespace, trackName };
|
|
476
|
+
}
|
|
477
|
+
function decodeTrackStatus(reader) {
|
|
478
|
+
const trackNamespace = reader.readTuple();
|
|
479
|
+
const trackName = reader.readString();
|
|
480
|
+
const statusCode = reader.readVarInt();
|
|
481
|
+
const lastGroupId = reader.readVarInt();
|
|
482
|
+
const lastObjectId = reader.readVarInt();
|
|
483
|
+
return { type: "track_status", trackNamespace, trackName, statusCode, lastGroupId, lastObjectId };
|
|
484
|
+
}
|
|
485
|
+
function decodeGoAway(reader) {
|
|
486
|
+
const newSessionUri = reader.readString();
|
|
487
|
+
return { type: "goaway", newSessionUri };
|
|
488
|
+
}
|
|
489
|
+
function decodeSubscribeAnnounces(reader) {
|
|
490
|
+
const trackNamespace = reader.readTuple();
|
|
491
|
+
const parameters = reader.readParameters();
|
|
492
|
+
return { type: "subscribe_announces", trackNamespace, parameters };
|
|
493
|
+
}
|
|
494
|
+
function decodeSubscribeAnnouncesOk(reader) {
|
|
495
|
+
const trackNamespace = reader.readTuple();
|
|
496
|
+
return { type: "subscribe_announces_ok", trackNamespace };
|
|
497
|
+
}
|
|
498
|
+
function decodeSubscribeAnnouncesError(reader) {
|
|
499
|
+
const trackNamespace = reader.readTuple();
|
|
500
|
+
const errorCode = reader.readVarInt();
|
|
501
|
+
const reasonPhrase = reader.readString();
|
|
502
|
+
return { type: "subscribe_announces_error", trackNamespace, errorCode, reasonPhrase };
|
|
503
|
+
}
|
|
504
|
+
function decodeUnsubscribeAnnounces(reader) {
|
|
505
|
+
const trackNamespace = reader.readTuple();
|
|
506
|
+
return { type: "unsubscribe_announces", trackNamespace };
|
|
507
|
+
}
|
|
508
|
+
function decodeMaxSubscribeId(reader) {
|
|
509
|
+
const subscribeId = reader.readVarInt();
|
|
510
|
+
return { type: "max_subscribe_id", subscribeId };
|
|
511
|
+
}
|
|
512
|
+
function decodeFetch(reader) {
|
|
513
|
+
const subscribeId = reader.readVarInt();
|
|
514
|
+
const trackNamespace = reader.readTuple();
|
|
515
|
+
const trackName = reader.readString();
|
|
516
|
+
const subscriberPriority = reader.readUint8();
|
|
517
|
+
const groupOrder = readGroupOrder(reader);
|
|
518
|
+
const startGroup = reader.readVarInt();
|
|
519
|
+
const startObject = reader.readVarInt();
|
|
520
|
+
const endGroup = reader.readVarInt();
|
|
521
|
+
const endObject = reader.readVarInt();
|
|
522
|
+
const parameters = reader.readParameters();
|
|
523
|
+
return { type: "fetch", subscribeId, trackNamespace, trackName, subscriberPriority, groupOrder, startGroup, startObject, endGroup, endObject, parameters };
|
|
524
|
+
}
|
|
525
|
+
function decodeFetchOk(reader) {
|
|
526
|
+
const subscribeId = reader.readVarInt();
|
|
527
|
+
const groupOrder = readGroupOrder(reader);
|
|
528
|
+
const endOfTrackWire = reader.readUint8();
|
|
529
|
+
const endOfTrack = endOfTrackWire !== 0;
|
|
530
|
+
const largestGroupId = reader.readVarInt();
|
|
531
|
+
const largestObjectId = reader.readVarInt();
|
|
532
|
+
const parameters = reader.readParameters();
|
|
533
|
+
return { type: "fetch_ok", subscribeId, groupOrder, endOfTrack, largestGroupId, largestObjectId, parameters };
|
|
534
|
+
}
|
|
535
|
+
function decodeFetchError(reader) {
|
|
536
|
+
const subscribeId = reader.readVarInt();
|
|
537
|
+
const errorCode = reader.readVarInt();
|
|
538
|
+
const reasonPhrase = reader.readString();
|
|
539
|
+
return { type: "fetch_error", subscribeId, errorCode, reasonPhrase };
|
|
540
|
+
}
|
|
541
|
+
function decodeFetchCancel(reader) {
|
|
542
|
+
const subscribeId = reader.readVarInt();
|
|
543
|
+
return { type: "fetch_cancel", subscribeId };
|
|
544
|
+
}
|
|
545
|
+
function decodeObjectStream(reader) {
|
|
546
|
+
const subscribeId = reader.readVarInt();
|
|
547
|
+
const trackAlias = reader.readVarInt();
|
|
548
|
+
const groupId = reader.readVarInt();
|
|
549
|
+
const objectId = reader.readVarInt();
|
|
550
|
+
const publisherPriority = reader.readUint8();
|
|
551
|
+
const objectStatusRaw = Number(reader.readVarInt());
|
|
552
|
+
const payload = reader.readBytes(reader.remaining);
|
|
553
|
+
const base = { type: "object_stream", subscribeId, trackAlias, groupId, objectId, publisherPriority, payload };
|
|
554
|
+
if (objectStatusRaw !== 0) {
|
|
555
|
+
return { ...base, objectStatus: objectStatusRaw };
|
|
556
|
+
}
|
|
557
|
+
return base;
|
|
558
|
+
}
|
|
559
|
+
function decodeObjectDatagram(reader) {
|
|
560
|
+
const subscribeId = reader.readVarInt();
|
|
561
|
+
const trackAlias = reader.readVarInt();
|
|
562
|
+
const groupId = reader.readVarInt();
|
|
563
|
+
const objectId = reader.readVarInt();
|
|
564
|
+
const publisherPriority = reader.readUint8();
|
|
565
|
+
const objectStatusRaw = Number(reader.readVarInt());
|
|
566
|
+
const payload = reader.readBytes(reader.remaining);
|
|
567
|
+
const base = { type: "object_datagram", subscribeId, trackAlias, groupId, objectId, publisherPriority, payload };
|
|
568
|
+
if (objectStatusRaw !== 0) {
|
|
569
|
+
return { ...base, objectStatus: objectStatusRaw };
|
|
570
|
+
}
|
|
571
|
+
return base;
|
|
572
|
+
}
|
|
573
|
+
function decodeStreamHeaderTrack(reader) {
|
|
574
|
+
const subscribeId = reader.readVarInt();
|
|
575
|
+
const trackAlias = reader.readVarInt();
|
|
576
|
+
const publisherPriority = reader.readUint8();
|
|
577
|
+
return { type: "stream_header_track", subscribeId, trackAlias, publisherPriority };
|
|
578
|
+
}
|
|
579
|
+
function decodeStreamHeaderGroup(reader) {
|
|
580
|
+
const subscribeId = reader.readVarInt();
|
|
581
|
+
const trackAlias = reader.readVarInt();
|
|
582
|
+
const groupId = reader.readVarInt();
|
|
583
|
+
const publisherPriority = reader.readUint8();
|
|
584
|
+
return { type: "stream_header_group", subscribeId, trackAlias, groupId, publisherPriority };
|
|
585
|
+
}
|
|
586
|
+
function decodeStreamHeaderSubgroup(reader) {
|
|
587
|
+
const subscribeId = reader.readVarInt();
|
|
588
|
+
const trackAlias = reader.readVarInt();
|
|
589
|
+
const groupId = reader.readVarInt();
|
|
590
|
+
const subgroupId = reader.readVarInt();
|
|
591
|
+
const publisherPriority = reader.readUint8();
|
|
592
|
+
return { type: "stream_header_subgroup", subscribeId, trackAlias, groupId, subgroupId, publisherPriority };
|
|
593
|
+
}
|
|
594
|
+
var controlDecoders = /* @__PURE__ */ new Map([
|
|
595
|
+
[MESSAGE_TYPE_IDS.client_setup, decodeClientSetup],
|
|
596
|
+
[MESSAGE_TYPE_IDS.server_setup, decodeServerSetup],
|
|
597
|
+
[MESSAGE_TYPE_IDS.subscribe, decodeSubscribe],
|
|
598
|
+
[MESSAGE_TYPE_IDS.subscribe_ok, decodeSubscribeOk],
|
|
599
|
+
[MESSAGE_TYPE_IDS.subscribe_error, decodeSubscribeError],
|
|
600
|
+
[MESSAGE_TYPE_IDS.subscribe_done, decodeSubscribeDone],
|
|
601
|
+
[MESSAGE_TYPE_IDS.subscribe_update, decodeSubscribeUpdate],
|
|
602
|
+
[MESSAGE_TYPE_IDS.unsubscribe, decodeUnsubscribe],
|
|
603
|
+
[MESSAGE_TYPE_IDS.announce, decodeAnnounce],
|
|
604
|
+
[MESSAGE_TYPE_IDS.announce_ok, decodeAnnounceOk],
|
|
605
|
+
[MESSAGE_TYPE_IDS.announce_error, decodeAnnounceError],
|
|
606
|
+
[MESSAGE_TYPE_IDS.announce_cancel, decodeAnnounceCancel],
|
|
607
|
+
[MESSAGE_TYPE_IDS.unannounce, decodeUnannounce],
|
|
608
|
+
[MESSAGE_TYPE_IDS.track_status_request, decodeTrackStatusRequest],
|
|
609
|
+
[MESSAGE_TYPE_IDS.track_status, decodeTrackStatus],
|
|
610
|
+
[MESSAGE_TYPE_IDS.goaway, decodeGoAway],
|
|
611
|
+
[MESSAGE_TYPE_IDS.subscribe_announces, decodeSubscribeAnnounces],
|
|
612
|
+
[MESSAGE_TYPE_IDS.subscribe_announces_ok, decodeSubscribeAnnouncesOk],
|
|
613
|
+
[MESSAGE_TYPE_IDS.subscribe_announces_error, decodeSubscribeAnnouncesError],
|
|
614
|
+
[MESSAGE_TYPE_IDS.unsubscribe_announces, decodeUnsubscribeAnnounces],
|
|
615
|
+
[MESSAGE_TYPE_IDS.max_subscribe_id, decodeMaxSubscribeId],
|
|
616
|
+
[MESSAGE_TYPE_IDS.fetch, decodeFetch],
|
|
617
|
+
[MESSAGE_TYPE_IDS.fetch_ok, decodeFetchOk],
|
|
618
|
+
[MESSAGE_TYPE_IDS.fetch_error, decodeFetchError],
|
|
619
|
+
[MESSAGE_TYPE_IDS.fetch_cancel, decodeFetchCancel]
|
|
620
|
+
]);
|
|
621
|
+
var dataStreamDecoders = /* @__PURE__ */ new Map([
|
|
622
|
+
[MESSAGE_TYPE_IDS.object_stream, decodeObjectStream],
|
|
623
|
+
[MESSAGE_TYPE_IDS.object_datagram, decodeObjectDatagram],
|
|
624
|
+
[MESSAGE_TYPE_IDS.stream_header_track, decodeStreamHeaderTrack],
|
|
625
|
+
[MESSAGE_TYPE_IDS.stream_header_group, decodeStreamHeaderGroup],
|
|
626
|
+
[MESSAGE_TYPE_IDS.stream_header_subgroup, decodeStreamHeaderSubgroup]
|
|
627
|
+
]);
|
|
628
|
+
var MESSAGE_TYPE_TO_WIRE = {
|
|
629
|
+
client_setup: MESSAGE_TYPE_IDS.client_setup,
|
|
630
|
+
server_setup: MESSAGE_TYPE_IDS.server_setup,
|
|
631
|
+
subscribe: MESSAGE_TYPE_IDS.subscribe,
|
|
632
|
+
subscribe_ok: MESSAGE_TYPE_IDS.subscribe_ok,
|
|
633
|
+
subscribe_error: MESSAGE_TYPE_IDS.subscribe_error,
|
|
634
|
+
subscribe_done: MESSAGE_TYPE_IDS.subscribe_done,
|
|
635
|
+
subscribe_update: MESSAGE_TYPE_IDS.subscribe_update,
|
|
636
|
+
unsubscribe: MESSAGE_TYPE_IDS.unsubscribe,
|
|
637
|
+
announce: MESSAGE_TYPE_IDS.announce,
|
|
638
|
+
announce_ok: MESSAGE_TYPE_IDS.announce_ok,
|
|
639
|
+
announce_error: MESSAGE_TYPE_IDS.announce_error,
|
|
640
|
+
announce_cancel: MESSAGE_TYPE_IDS.announce_cancel,
|
|
641
|
+
unannounce: MESSAGE_TYPE_IDS.unannounce,
|
|
642
|
+
track_status_request: MESSAGE_TYPE_IDS.track_status_request,
|
|
643
|
+
track_status: MESSAGE_TYPE_IDS.track_status,
|
|
644
|
+
goaway: MESSAGE_TYPE_IDS.goaway,
|
|
645
|
+
subscribe_announces: MESSAGE_TYPE_IDS.subscribe_announces,
|
|
646
|
+
subscribe_announces_ok: MESSAGE_TYPE_IDS.subscribe_announces_ok,
|
|
647
|
+
subscribe_announces_error: MESSAGE_TYPE_IDS.subscribe_announces_error,
|
|
648
|
+
unsubscribe_announces: MESSAGE_TYPE_IDS.unsubscribe_announces,
|
|
649
|
+
max_subscribe_id: MESSAGE_TYPE_IDS.max_subscribe_id,
|
|
650
|
+
fetch: MESSAGE_TYPE_IDS.fetch,
|
|
651
|
+
fetch_ok: MESSAGE_TYPE_IDS.fetch_ok,
|
|
652
|
+
fetch_error: MESSAGE_TYPE_IDS.fetch_error,
|
|
653
|
+
fetch_cancel: MESSAGE_TYPE_IDS.fetch_cancel
|
|
654
|
+
};
|
|
655
|
+
function encodeMessageImpl(message) {
|
|
656
|
+
const dataEncoder = dataStreamEncoders[message.type];
|
|
657
|
+
if (dataEncoder) {
|
|
658
|
+
const writer = new BufferWriter();
|
|
659
|
+
dataEncoder(message, writer);
|
|
660
|
+
return writer.finish();
|
|
661
|
+
}
|
|
662
|
+
const controlEncoder = controlEncoders[message.type];
|
|
663
|
+
if (!controlEncoder) {
|
|
664
|
+
throw new Error(`Unknown message type: ${message.type}`);
|
|
665
|
+
}
|
|
666
|
+
const payloadWriter = new BufferWriter();
|
|
667
|
+
controlEncoder(message, payloadWriter);
|
|
668
|
+
const payload = payloadWriter.finish();
|
|
669
|
+
const frameWriter = new BufferWriter();
|
|
670
|
+
frameWriter.writeVarInt(MESSAGE_TYPE_TO_WIRE[message.type]);
|
|
671
|
+
frameWriter.writeVarInt(payload.byteLength);
|
|
672
|
+
frameWriter.writeBytes(payload);
|
|
673
|
+
return frameWriter.finish();
|
|
674
|
+
}
|
|
675
|
+
function decodeMessageImpl(bytes) {
|
|
676
|
+
try {
|
|
677
|
+
const reader = new BufferReader(bytes, 0);
|
|
678
|
+
const typeId = reader.readVarInt();
|
|
679
|
+
if (DATA_STREAM_TYPE_IDS.has(typeId)) {
|
|
680
|
+
const decoder2 = dataStreamDecoders.get(typeId);
|
|
681
|
+
if (!decoder2) {
|
|
682
|
+
return {
|
|
683
|
+
ok: false,
|
|
684
|
+
error: new DecodeError("UNKNOWN_MESSAGE_TYPE", `Unknown data stream type ID: 0x${typeId.toString(16)}`, 0)
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
const message2 = decoder2(reader);
|
|
688
|
+
return { ok: true, value: message2, bytesRead: reader.offset };
|
|
689
|
+
}
|
|
690
|
+
const payloadLength = Number(reader.readVarInt());
|
|
691
|
+
const headerBytes = reader.offset;
|
|
692
|
+
if (reader.remaining < payloadLength) {
|
|
693
|
+
return {
|
|
694
|
+
ok: false,
|
|
695
|
+
error: new DecodeError("UNEXPECTED_END", `Not enough bytes for payload: need ${payloadLength}, have ${reader.remaining}`, reader.offset)
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
const payloadBytes = reader.readBytes(payloadLength);
|
|
699
|
+
const totalBytesRead = reader.offset;
|
|
700
|
+
const decoder = controlDecoders.get(typeId);
|
|
701
|
+
if (!decoder) {
|
|
702
|
+
return {
|
|
703
|
+
ok: false,
|
|
704
|
+
error: new DecodeError("UNKNOWN_MESSAGE_TYPE", `Unknown message type ID: 0x${typeId.toString(16)}`, 0)
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
const payloadReader = new BufferReader(payloadBytes, 0);
|
|
708
|
+
const message = decoder(payloadReader);
|
|
709
|
+
return { ok: true, value: message, bytesRead: totalBytesRead };
|
|
710
|
+
} catch (e) {
|
|
711
|
+
if (e instanceof DecodeError) {
|
|
712
|
+
return { ok: false, error: e };
|
|
713
|
+
}
|
|
714
|
+
throw e;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
function createStreamDecoderImpl() {
|
|
718
|
+
let buffer = new Uint8Array(0);
|
|
719
|
+
return new TransformStream({
|
|
720
|
+
transform(chunk, controller) {
|
|
721
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
722
|
+
newBuffer.set(buffer, 0);
|
|
723
|
+
newBuffer.set(chunk, buffer.length);
|
|
724
|
+
buffer = newBuffer;
|
|
725
|
+
while (buffer.length > 0) {
|
|
726
|
+
const result = decodeMessageImpl(buffer);
|
|
727
|
+
if (!result.ok) {
|
|
728
|
+
if (result.error.code === "UNEXPECTED_END") {
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
controller.error(result.error);
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
controller.enqueue(result.value);
|
|
735
|
+
buffer = buffer.slice(result.bytesRead);
|
|
736
|
+
}
|
|
737
|
+
},
|
|
738
|
+
flush(controller) {
|
|
739
|
+
if (buffer.length > 0) {
|
|
740
|
+
controller.error(
|
|
741
|
+
new DecodeError("UNEXPECTED_END", "Stream ended with incomplete message data", 0)
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
function createDraft07Codec() {
|
|
748
|
+
return {
|
|
749
|
+
draft: "draft-ietf-moq-transport-07",
|
|
750
|
+
encodeMessage: encodeMessageImpl,
|
|
751
|
+
decodeMessage: decodeMessageImpl,
|
|
752
|
+
encodeVarInt,
|
|
753
|
+
decodeVarInt,
|
|
754
|
+
createStreamDecoder: createStreamDecoderImpl
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
export {
|
|
759
|
+
MESSAGE_TYPE_IDS,
|
|
760
|
+
MESSAGE_ID_TO_TYPE,
|
|
761
|
+
encodeVarInt,
|
|
762
|
+
decodeVarInt,
|
|
763
|
+
createDraft07Codec
|
|
764
|
+
};
|