@moqtap/codec 0.6.0 → 0.7.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/README.md +4 -2
- package/dist/{chunk-CXFAQKL3.js → chunk-23KRITJ6.js} +1 -1
- package/dist/{chunk-JL3NEW3O.cjs → chunk-4XLYCT5Q.cjs} +90 -1
- package/dist/{chunk-DLSGOZ6P.js → chunk-5XZKO2U5.js} +34 -34
- package/dist/{chunk-62LYLROG.js → chunk-APTV6F72.js} +35 -35
- package/dist/{chunk-JB5VDJ2V.cjs → chunk-B56AG5HU.cjs} +37 -37
- package/dist/{chunk-4HBKHNU2.cjs → chunk-CZNW3R5E.cjs} +37 -37
- package/dist/{chunk-Q5D6OW6Q.js → chunk-DORX55MO.js} +1 -1
- package/dist/{chunk-OGZTQA24.cjs → chunk-EQAYMCXO.cjs} +64 -64
- package/dist/{chunk-HYI6LFMD.js → chunk-HEO2JG6C.js} +1 -1
- package/dist/{chunk-UP4XPH4J.js → chunk-I55J3KXD.js} +31 -31
- package/dist/{chunk-HZHSO7EW.cjs → chunk-IDV2JYJ6.cjs} +39 -39
- package/dist/{chunk-BAG6YWCP.cjs → chunk-IEJULBKB.cjs} +63 -63
- package/dist/{chunk-Y6KCSM3V.js → chunk-IJAF6PAY.js} +1 -1
- package/dist/{chunk-WMJVSBBE.cjs → chunk-J3ASFPCX.cjs} +57 -57
- package/dist/{chunk-COWGGUL4.js → chunk-JKMYAUGY.js} +1 -1
- package/dist/{chunk-SIYYMFCC.cjs → chunk-KCH4BQXK.cjs} +37 -37
- package/dist/{chunk-VOU7PNHI.js → chunk-KLPWJU6W.js} +89 -0
- package/dist/{chunk-NZ7GYEMT.js → chunk-KNLPX3YA.js} +1 -1
- package/dist/{chunk-A33U4WVS.cjs → chunk-KTVN4P6J.cjs} +67 -67
- package/dist/{chunk-7KBAWOBA.js → chunk-KYVTM5VI.js} +1 -1
- package/dist/{chunk-RZ3UML47.js → chunk-MD2OA7SE.js} +1 -1
- package/dist/{chunk-MSG2FXJ2.cjs → chunk-MH7537FB.cjs} +68 -68
- package/dist/{chunk-SHMA4PBY.js → chunk-N2V7Z37J.js} +1 -1
- package/dist/{chunk-HO44OANK.cjs → chunk-QGIHC6BA.cjs} +36 -36
- package/dist/{chunk-PKEOQSSZ.cjs → chunk-RIIA4ALQ.cjs} +37 -37
- package/dist/{chunk-XZE5R57R.cjs → chunk-VZHL2U2N.cjs} +37 -37
- package/dist/{chunk-RW4UG7CN.js → chunk-XHAP2YST.js} +1 -1
- package/dist/{chunk-JTRU76JN.cjs → chunk-Y4FRXYX6.cjs} +59 -59
- package/dist/draft07.cjs +4 -4
- package/dist/draft07.js +2 -2
- package/dist/draft08.cjs +3 -3
- package/dist/draft08.js +2 -2
- package/dist/draft09.cjs +3 -3
- package/dist/draft09.js +2 -2
- package/dist/draft10.cjs +3 -3
- package/dist/draft10.js +2 -2
- package/dist/draft11.cjs +3 -3
- package/dist/draft11.js +2 -2
- package/dist/draft12.cjs +3 -3
- package/dist/draft12.js +2 -2
- package/dist/draft13.cjs +3 -3
- package/dist/draft13.js +2 -2
- package/dist/draft14.cjs +3 -3
- package/dist/draft14.js +2 -2
- package/dist/draft15.cjs +3 -3
- package/dist/draft15.js +2 -2
- package/dist/draft16.cjs +3 -3
- package/dist/draft16.js +2 -2
- package/dist/draft17.cjs +3 -3
- package/dist/draft17.js +2 -2
- package/dist/draft18.cjs +3 -3
- package/dist/draft18.js +2 -2
- package/dist/draft19.cjs +3 -3
- package/dist/draft19.js +2 -2
- package/dist/index.cjs +28 -28
- package/dist/index.js +14 -14
- package/package.json +2 -2
- package/src/core/buffer-reader.ts +74 -2
- package/src/core/buffer-writer.ts +56 -4
- package/src/drafts/draft17/codec.ts +2 -2
- package/src/drafts/draft17/data-streams.ts +2 -2
- package/src/drafts/draft18/codec.ts +2 -2
- package/src/drafts/draft18/data-streams.ts +2 -2
- package/src/drafts/draft19/codec.ts +2 -2
- package/src/drafts/draft19/data-streams.ts +2 -2
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _chunk4XLYCT5Qcjs = require('./chunk-4XLYCT5Q.cjs');
|
|
8
8
|
|
|
9
9
|
// src/drafts/draft13/data-streams.ts
|
|
10
10
|
var FETCH_STREAM_TYPE = 0x05n;
|
|
11
11
|
function encodeSubgroupStream(stream) {
|
|
12
|
-
const w = new (0,
|
|
12
|
+
const w = new (0, _chunk4XLYCT5Qcjs.BufferWriter)();
|
|
13
13
|
const typeId = stream.streamTypeId;
|
|
14
14
|
w.writeVarInt(BigInt(typeId));
|
|
15
15
|
const extensionsPresent = (typeId & 1) !== 0;
|
|
@@ -34,7 +34,7 @@ function encodeSubgroupStream(stream) {
|
|
|
34
34
|
return w.finish();
|
|
35
35
|
}
|
|
36
36
|
function encodeDatagram(dg) {
|
|
37
|
-
const w = new (0,
|
|
37
|
+
const w = new (0, _chunk4XLYCT5Qcjs.BufferWriter)();
|
|
38
38
|
w.writeVarInt(BigInt(dg.streamTypeId));
|
|
39
39
|
w.writeVarInt(dg.trackAlias);
|
|
40
40
|
w.writeVarInt(dg.groupId);
|
|
@@ -55,7 +55,7 @@ function encodeDatagram(dg) {
|
|
|
55
55
|
return w.finish();
|
|
56
56
|
}
|
|
57
57
|
function encodeFetchStream(stream) {
|
|
58
|
-
const w = new (0,
|
|
58
|
+
const w = new (0, _chunk4XLYCT5Qcjs.BufferWriter)();
|
|
59
59
|
w.writeVarInt(FETCH_STREAM_TYPE);
|
|
60
60
|
w.writeVarInt(stream.requestId);
|
|
61
61
|
for (const obj of stream.objects) {
|
|
@@ -77,14 +77,14 @@ function encodeFetchStream(stream) {
|
|
|
77
77
|
}
|
|
78
78
|
function decodeSubgroupStream(bytes) {
|
|
79
79
|
try {
|
|
80
|
-
const r = new (0,
|
|
80
|
+
const r = new (0, _chunk4XLYCT5Qcjs.BufferReader)(bytes);
|
|
81
81
|
const streamTypeId = Number(r.readVarInt());
|
|
82
82
|
const validRange1 = streamTypeId >= 16 && streamTypeId <= 21;
|
|
83
83
|
const validRange2 = streamTypeId >= 24 && streamTypeId <= 29;
|
|
84
84
|
if (!validRange1 && !validRange2) {
|
|
85
85
|
return {
|
|
86
86
|
ok: false,
|
|
87
|
-
error: new (0,
|
|
87
|
+
error: new (0, _chunk4XLYCT5Qcjs.DecodeError)(
|
|
88
88
|
"CONSTRAINT_VIOLATION",
|
|
89
89
|
`Expected subgroup type 0x10-0x15 or 0x18-0x1D, got 0x${streamTypeId.toString(16)}`,
|
|
90
90
|
0
|
|
@@ -154,18 +154,18 @@ function decodeSubgroupStream(bytes) {
|
|
|
154
154
|
bytesRead: r.offset
|
|
155
155
|
};
|
|
156
156
|
} catch (e) {
|
|
157
|
-
if (e instanceof
|
|
157
|
+
if (e instanceof _chunk4XLYCT5Qcjs.DecodeError) return { ok: false, error: e };
|
|
158
158
|
throw e;
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
function decodeDatagram(bytes) {
|
|
162
162
|
try {
|
|
163
|
-
const r = new (0,
|
|
163
|
+
const r = new (0, _chunk4XLYCT5Qcjs.BufferReader)(bytes);
|
|
164
164
|
const streamTypeId = Number(r.readVarInt());
|
|
165
165
|
if (streamTypeId < 0 || streamTypeId > 5) {
|
|
166
166
|
return {
|
|
167
167
|
ok: false,
|
|
168
|
-
error: new (0,
|
|
168
|
+
error: new (0, _chunk4XLYCT5Qcjs.DecodeError)(
|
|
169
169
|
"CONSTRAINT_VIOLATION",
|
|
170
170
|
`Expected datagram type 0x00-0x05, got 0x${streamTypeId.toString(16)}`,
|
|
171
171
|
0
|
|
@@ -212,18 +212,18 @@ function decodeDatagram(bytes) {
|
|
|
212
212
|
result.objectStatus = objectStatus;
|
|
213
213
|
return { ok: true, value: result, bytesRead: r.offset };
|
|
214
214
|
} catch (e) {
|
|
215
|
-
if (e instanceof
|
|
215
|
+
if (e instanceof _chunk4XLYCT5Qcjs.DecodeError) return { ok: false, error: e };
|
|
216
216
|
throw e;
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
function decodeFetchStream(bytes) {
|
|
220
220
|
try {
|
|
221
|
-
const r = new (0,
|
|
221
|
+
const r = new (0, _chunk4XLYCT5Qcjs.BufferReader)(bytes);
|
|
222
222
|
const streamType = r.readVarInt();
|
|
223
223
|
if (streamType !== FETCH_STREAM_TYPE) {
|
|
224
224
|
return {
|
|
225
225
|
ok: false,
|
|
226
|
-
error: new (0,
|
|
226
|
+
error: new (0, _chunk4XLYCT5Qcjs.DecodeError)(
|
|
227
227
|
"CONSTRAINT_VIOLATION",
|
|
228
228
|
`Expected fetch type 0x05, got 0x${streamType.toString(16)}`,
|
|
229
229
|
0
|
|
@@ -275,7 +275,7 @@ function decodeFetchStream(bytes) {
|
|
|
275
275
|
bytesRead: r.offset
|
|
276
276
|
};
|
|
277
277
|
} catch (e) {
|
|
278
|
-
if (e instanceof
|
|
278
|
+
if (e instanceof _chunk4XLYCT5Qcjs.DecodeError) return { ok: false, error: e };
|
|
279
279
|
throw e;
|
|
280
280
|
}
|
|
281
281
|
}
|
|
@@ -377,7 +377,7 @@ var MESSAGE_ID_MAP = new Map(
|
|
|
377
377
|
var textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
378
378
|
var textDecoder = /* @__PURE__ */ new TextDecoder();
|
|
379
379
|
function encodeAuthorizationToken(at, w) {
|
|
380
|
-
const tmpW = new (0,
|
|
380
|
+
const tmpW = new (0, _chunk4XLYCT5Qcjs.BufferWriter)(64);
|
|
381
381
|
tmpW.writeVarInt(at.alias_type);
|
|
382
382
|
const aliasType = Number(at.alias_type);
|
|
383
383
|
if (aliasType === 0 || aliasType === 1 || aliasType === 2) {
|
|
@@ -438,11 +438,11 @@ function encodeSetupParams(params, w) {
|
|
|
438
438
|
const id = BigInt(u.id);
|
|
439
439
|
w.writeVarInt(id);
|
|
440
440
|
if (id % 2n === 0n) {
|
|
441
|
-
const raw =
|
|
442
|
-
const tmpReader = new (0,
|
|
441
|
+
const raw = _chunk4XLYCT5Qcjs.hexToBytes.call(void 0, u.raw_hex);
|
|
442
|
+
const tmpReader = new (0, _chunk4XLYCT5Qcjs.BufferReader)(raw);
|
|
443
443
|
w.writeVarInt(tmpReader.readVarInt());
|
|
444
444
|
} else {
|
|
445
|
-
const raw =
|
|
445
|
+
const raw = _chunk4XLYCT5Qcjs.hexToBytes.call(void 0, u.raw_hex);
|
|
446
446
|
w.writeVarInt(raw.byteLength);
|
|
447
447
|
w.writeBytes(raw);
|
|
448
448
|
}
|
|
@@ -462,13 +462,13 @@ function decodeSetupParams(r) {
|
|
|
462
462
|
} else if (paramType === SETUP_PARAM_MAX_AUTH_TOKEN_CACHE_SIZE) {
|
|
463
463
|
result.max_auth_token_cache_size = value;
|
|
464
464
|
} else {
|
|
465
|
-
const tmpW = new (0,
|
|
465
|
+
const tmpW = new (0, _chunk4XLYCT5Qcjs.BufferWriter)(16);
|
|
466
466
|
tmpW.writeVarInt(value);
|
|
467
467
|
const raw = tmpW.finish();
|
|
468
468
|
unknown.push({
|
|
469
469
|
id: `0x${paramType.toString(16)}`,
|
|
470
470
|
length: raw.byteLength,
|
|
471
|
-
raw_hex:
|
|
471
|
+
raw_hex: _chunk4XLYCT5Qcjs.bytesToHex.call(void 0, raw)
|
|
472
472
|
});
|
|
473
473
|
}
|
|
474
474
|
} else {
|
|
@@ -483,7 +483,7 @@ function decodeSetupParams(r) {
|
|
|
483
483
|
unknown.push({
|
|
484
484
|
id: `0x${paramType.toString(16)}`,
|
|
485
485
|
length,
|
|
486
|
-
raw_hex:
|
|
486
|
+
raw_hex: _chunk4XLYCT5Qcjs.bytesToHex.call(void 0, bytes)
|
|
487
487
|
});
|
|
488
488
|
}
|
|
489
489
|
}
|
|
@@ -514,11 +514,11 @@ function encodeParams(params, w) {
|
|
|
514
514
|
const id = BigInt(u.id);
|
|
515
515
|
w.writeVarInt(id);
|
|
516
516
|
if (id % 2n === 0n) {
|
|
517
|
-
const raw =
|
|
518
|
-
const tmpReader = new (0,
|
|
517
|
+
const raw = _chunk4XLYCT5Qcjs.hexToBytes.call(void 0, u.raw_hex);
|
|
518
|
+
const tmpReader = new (0, _chunk4XLYCT5Qcjs.BufferReader)(raw);
|
|
519
519
|
w.writeVarInt(tmpReader.readVarInt());
|
|
520
520
|
} else {
|
|
521
|
-
const raw =
|
|
521
|
+
const raw = _chunk4XLYCT5Qcjs.hexToBytes.call(void 0, u.raw_hex);
|
|
522
522
|
w.writeVarInt(raw.byteLength);
|
|
523
523
|
w.writeBytes(raw);
|
|
524
524
|
}
|
|
@@ -538,13 +538,13 @@ function decodeParams(r) {
|
|
|
538
538
|
} else if (paramType === PARAM_MAX_CACHE_DURATION) {
|
|
539
539
|
result.max_cache_duration = value;
|
|
540
540
|
} else {
|
|
541
|
-
const tmpW = new (0,
|
|
541
|
+
const tmpW = new (0, _chunk4XLYCT5Qcjs.BufferWriter)(16);
|
|
542
542
|
tmpW.writeVarInt(value);
|
|
543
543
|
const raw = tmpW.finish();
|
|
544
544
|
unknown.push({
|
|
545
545
|
id: `0x${paramType.toString(16)}`,
|
|
546
546
|
length: raw.byteLength,
|
|
547
|
-
raw_hex:
|
|
547
|
+
raw_hex: _chunk4XLYCT5Qcjs.bytesToHex.call(void 0, raw)
|
|
548
548
|
});
|
|
549
549
|
}
|
|
550
550
|
} else {
|
|
@@ -556,7 +556,7 @@ function decodeParams(r) {
|
|
|
556
556
|
unknown.push({
|
|
557
557
|
id: `0x${paramType.toString(16)}`,
|
|
558
558
|
length,
|
|
559
|
-
raw_hex:
|
|
559
|
+
raw_hex: _chunk4XLYCT5Qcjs.bytesToHex.call(void 0, bytes)
|
|
560
560
|
});
|
|
561
561
|
}
|
|
562
562
|
}
|
|
@@ -780,7 +780,7 @@ function encodeRequestsBlockedPayload(msg, w) {
|
|
|
780
780
|
function decodeClientSetupPayload(r) {
|
|
781
781
|
const numVersions = Number(r.readVarInt());
|
|
782
782
|
if (numVersions === 0) {
|
|
783
|
-
throw new (0,
|
|
783
|
+
throw new (0, _chunk4XLYCT5Qcjs.DecodeError)("CONSTRAINT_VIOLATION", "supported_versions must not be empty", r.offset);
|
|
784
784
|
}
|
|
785
785
|
const supported_versions = [];
|
|
786
786
|
for (let i = 0; i < numVersions; i++) supported_versions.push(r.readVarInt());
|
|
@@ -802,7 +802,7 @@ function decodeSubscribePayload(r) {
|
|
|
802
802
|
const filter_type = r.readVarInt();
|
|
803
803
|
const ft = Number(filter_type);
|
|
804
804
|
if (ft < 1 || ft > 4) {
|
|
805
|
-
throw new (0,
|
|
805
|
+
throw new (0, _chunk4XLYCT5Qcjs.DecodeError)("CONSTRAINT_VIOLATION", `Invalid filter_type: ${ft}`, r.offset);
|
|
806
806
|
}
|
|
807
807
|
let start_group;
|
|
808
808
|
let start_object;
|
|
@@ -1028,7 +1028,7 @@ function decodeFetchPayload(r) {
|
|
|
1028
1028
|
const fetch_type = r.readVarInt();
|
|
1029
1029
|
const ft = Number(fetch_type);
|
|
1030
1030
|
if (ft < 1 || ft > 3) {
|
|
1031
|
-
throw new (0,
|
|
1031
|
+
throw new (0, _chunk4XLYCT5Qcjs.DecodeError)("CONSTRAINT_VIOLATION", `Invalid fetch_type: ${ft}`, r.offset);
|
|
1032
1032
|
}
|
|
1033
1033
|
let standalone;
|
|
1034
1034
|
let joining;
|
|
@@ -1100,7 +1100,7 @@ function decodeTrackStatusPayload(r) {
|
|
|
1100
1100
|
const filter_type = r.readVarInt();
|
|
1101
1101
|
const ft = Number(filter_type);
|
|
1102
1102
|
if (ft < 1 || ft > 4) {
|
|
1103
|
-
throw new (0,
|
|
1103
|
+
throw new (0, _chunk4XLYCT5Qcjs.DecodeError)("CONSTRAINT_VIOLATION", `Invalid filter_type: ${ft}`, r.offset);
|
|
1104
1104
|
}
|
|
1105
1105
|
let start_group;
|
|
1106
1106
|
let start_object;
|
|
@@ -1207,10 +1207,10 @@ var payloadDecoders = /* @__PURE__ */ new Map([
|
|
|
1207
1207
|
function encodeMessage(message) {
|
|
1208
1208
|
const typeId = MESSAGE_ID_MAP.get(message.type);
|
|
1209
1209
|
if (typeId === void 0) throw new Error(`Unknown message type: ${message.type}`);
|
|
1210
|
-
const payloadWriter = new (0,
|
|
1210
|
+
const payloadWriter = new (0, _chunk4XLYCT5Qcjs.BufferWriter)();
|
|
1211
1211
|
encodePayload(message, payloadWriter);
|
|
1212
1212
|
const payload = payloadWriter.finishView();
|
|
1213
|
-
const writer = new (0,
|
|
1213
|
+
const writer = new (0, _chunk4XLYCT5Qcjs.BufferWriter)(payload.byteLength + 16);
|
|
1214
1214
|
writer.writeVarInt(typeId);
|
|
1215
1215
|
if (payload.byteLength > 65535) {
|
|
1216
1216
|
throw new Error(`Payload too large for 16-bit length: ${payload.byteLength}`);
|
|
@@ -1290,18 +1290,18 @@ function encodePayload(msg, w) {
|
|
|
1290
1290
|
}
|
|
1291
1291
|
function decodeMessage(bytes) {
|
|
1292
1292
|
try {
|
|
1293
|
-
const reader = new (0,
|
|
1293
|
+
const reader = new (0, _chunk4XLYCT5Qcjs.BufferReader)(bytes);
|
|
1294
1294
|
const typeId = reader.readVarInt();
|
|
1295
1295
|
const lenHi = reader.readUint8();
|
|
1296
1296
|
const lenLo = reader.readUint8();
|
|
1297
1297
|
const payloadLength = lenHi << 8 | lenLo;
|
|
1298
1298
|
const payloadBytes = reader.readBytes(payloadLength);
|
|
1299
|
-
const payloadReader = new (0,
|
|
1299
|
+
const payloadReader = new (0, _chunk4XLYCT5Qcjs.BufferReader)(payloadBytes);
|
|
1300
1300
|
const decoder = payloadDecoders.get(typeId);
|
|
1301
1301
|
if (!decoder) {
|
|
1302
1302
|
return {
|
|
1303
1303
|
ok: false,
|
|
1304
|
-
error: new (0,
|
|
1304
|
+
error: new (0, _chunk4XLYCT5Qcjs.DecodeError)(
|
|
1305
1305
|
"UNKNOWN_MESSAGE_TYPE",
|
|
1306
1306
|
`Unknown message type ID: 0x${typeId.toString(16)}`,
|
|
1307
1307
|
0
|
|
@@ -1311,7 +1311,7 @@ function decodeMessage(bytes) {
|
|
|
1311
1311
|
const message = decoder(payloadReader);
|
|
1312
1312
|
return { ok: true, value: message, bytesRead: reader.offset };
|
|
1313
1313
|
} catch (e) {
|
|
1314
|
-
if (e instanceof
|
|
1314
|
+
if (e instanceof _chunk4XLYCT5Qcjs.DecodeError) return { ok: false, error: e };
|
|
1315
1315
|
throw e;
|
|
1316
1316
|
}
|
|
1317
1317
|
}
|
|
@@ -1341,7 +1341,7 @@ function createStreamDecoder() {
|
|
|
1341
1341
|
},
|
|
1342
1342
|
flush(controller) {
|
|
1343
1343
|
if (offset < buffer.length)
|
|
1344
|
-
controller.error(new (0,
|
|
1344
|
+
controller.error(new (0, _chunk4XLYCT5Qcjs.DecodeError)("UNEXPECTED_END", "Stream ended with incomplete data", 0));
|
|
1345
1345
|
}
|
|
1346
1346
|
});
|
|
1347
1347
|
}
|
|
@@ -124,6 +124,54 @@ var BufferReader = class {
|
|
|
124
124
|
return params;
|
|
125
125
|
}
|
|
126
126
|
};
|
|
127
|
+
var MoqtBufferReader = class extends BufferReader {
|
|
128
|
+
/** Draft-17 omits the 7-byte length; draft-18 restored it. */
|
|
129
|
+
get allowSevenByte() {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
readVarInt() {
|
|
133
|
+
if (this.remaining < 1) {
|
|
134
|
+
throw new DecodeError("UNEXPECTED_END", "Not enough bytes for varint", this.pos);
|
|
135
|
+
}
|
|
136
|
+
const first = this.view.getUint8(this.pos);
|
|
137
|
+
if (first === 255) {
|
|
138
|
+
if (this.remaining < 9) {
|
|
139
|
+
throw new DecodeError("UNEXPECTED_END", "Not enough bytes for 9-byte varint", this.pos);
|
|
140
|
+
}
|
|
141
|
+
const value2 = this.view.getBigUint64(this.pos + 1);
|
|
142
|
+
this.pos += 9;
|
|
143
|
+
return value2;
|
|
144
|
+
}
|
|
145
|
+
let leadingOnes = 0;
|
|
146
|
+
while (leadingOnes < 8 && (first & 128 >> leadingOnes) !== 0) leadingOnes++;
|
|
147
|
+
const length = leadingOnes + 1;
|
|
148
|
+
if (length === 7 && !this.allowSevenByte) {
|
|
149
|
+
throw new DecodeError(
|
|
150
|
+
"INVALID_VARINT",
|
|
151
|
+
"The 7-byte varint is not defined in draft-17",
|
|
152
|
+
this.pos
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (this.remaining < length) {
|
|
156
|
+
throw new DecodeError(
|
|
157
|
+
"UNEXPECTED_END",
|
|
158
|
+
`Not enough bytes for ${length}-byte varint`,
|
|
159
|
+
this.pos
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
let value = BigInt(first & (1 << 8 - length) - 1);
|
|
163
|
+
for (let i = 1; i < length; i++) {
|
|
164
|
+
value = value << 8n | BigInt(this.view.getUint8(this.pos + i));
|
|
165
|
+
}
|
|
166
|
+
this.pos += length;
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var Draft17BufferReader = class extends MoqtBufferReader {
|
|
171
|
+
get allowSevenByte() {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
127
175
|
|
|
128
176
|
// src/core/buffer-writer.ts
|
|
129
177
|
var textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
@@ -211,6 +259,43 @@ var BufferWriter = class {
|
|
|
211
259
|
return this.buffer.subarray(0, this.pos);
|
|
212
260
|
}
|
|
213
261
|
};
|
|
262
|
+
var MoqtBufferWriter = class extends BufferWriter {
|
|
263
|
+
/** Draft-17 omits the 7-byte length; draft-18 restored it. */
|
|
264
|
+
get allowSevenByte() {
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
writeVarInt(value) {
|
|
268
|
+
const v = BigInt(value);
|
|
269
|
+
if (v < 0n) throw new Error("VarInt value must be non-negative");
|
|
270
|
+
if (v > 0xffffffffffffffffn) throw new Error("VarInt value exceeds 64-bit range");
|
|
271
|
+
let length = 9;
|
|
272
|
+
for (let n = 1; n <= 8; n++) {
|
|
273
|
+
if (n === 7 && !this.allowSevenByte) continue;
|
|
274
|
+
if (v < 1n << BigInt(7 * n)) {
|
|
275
|
+
length = n;
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
this.ensureCapacity(length);
|
|
280
|
+
if (length === 9) {
|
|
281
|
+
this.view.setUint8(this.pos, 255);
|
|
282
|
+
this.view.setBigUint64(this.pos + 1, v);
|
|
283
|
+
this.pos += 9;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const prefix = BigInt((1 << length - 1) - 1 << 9 - length) & 0xffn;
|
|
287
|
+
const combined = prefix << BigInt(8 * (length - 1)) | v;
|
|
288
|
+
for (let i = 0; i < length; i++) {
|
|
289
|
+
this.view.setUint8(this.pos + i, Number(combined >> BigInt(8 * (length - 1 - i)) & 0xffn));
|
|
290
|
+
}
|
|
291
|
+
this.pos += length;
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var Draft17BufferWriter = class extends MoqtBufferWriter {
|
|
295
|
+
get allowSevenByte() {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
214
299
|
|
|
215
300
|
// src/core/hex.ts
|
|
216
301
|
var HEX_TABLE = /* @__PURE__ */ Array.from(
|
|
@@ -235,7 +320,11 @@ function hexToBytes(hex) {
|
|
|
235
320
|
export {
|
|
236
321
|
DecodeError,
|
|
237
322
|
BufferReader,
|
|
323
|
+
MoqtBufferReader,
|
|
324
|
+
Draft17BufferReader,
|
|
238
325
|
BufferWriter,
|
|
326
|
+
MoqtBufferWriter,
|
|
327
|
+
Draft17BufferWriter,
|
|
239
328
|
bytesToHex,
|
|
240
329
|
hexToBytes
|
|
241
330
|
};
|