@reactor-team/js-sdk 3.0.1 → 3.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/dist/index.cjs +416 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +77 -16
- package/dist/index.d.ts +77 -16
- package/dist/index.js +416 -98
- package/dist/index.js.map +1 -1
- package/dist/wasm/reactor_wasm.d.ts +42 -0
- package/dist/wasm/reactor_wasm.js +40 -5
- package/dist/wasm/reactor_wasm_bg.wasm +0 -0
- package/dist/wasm/reactor_wasm_bg.wasm.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -244,16 +244,53 @@ function extractFileRefs(data) {
|
|
|
244
244
|
let uploads;
|
|
245
245
|
let scalars;
|
|
246
246
|
for (const [key, value] of Object.entries(data)) {
|
|
247
|
-
if (
|
|
247
|
+
if (isFileRef(value)) {
|
|
248
|
+
uploads ??= {};
|
|
249
|
+
scalars ??= { ...data };
|
|
250
|
+
uploads[key] = toWireFileRef(value);
|
|
251
|
+
delete scalars[key];
|
|
248
252
|
continue;
|
|
249
253
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
+
const rewritten = rewriteNestedFileRefs(value);
|
|
255
|
+
if (rewritten !== value) {
|
|
256
|
+
scalars ??= { ...data };
|
|
257
|
+
scalars[key] = rewritten;
|
|
258
|
+
}
|
|
254
259
|
}
|
|
255
260
|
return { data: scalars ?? data, uploads };
|
|
256
261
|
}
|
|
262
|
+
function rewriteNestedFileRefs(value) {
|
|
263
|
+
if (isFileRef(value)) {
|
|
264
|
+
return toWireFileRef(value);
|
|
265
|
+
}
|
|
266
|
+
if (Array.isArray(value)) {
|
|
267
|
+
const elements = value;
|
|
268
|
+
let copy;
|
|
269
|
+
elements.forEach((element, index) => {
|
|
270
|
+
const rewritten = rewriteNestedFileRefs(element);
|
|
271
|
+
if (rewritten !== element) {
|
|
272
|
+
copy ??= [...elements];
|
|
273
|
+
copy[index] = rewritten;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
return copy ?? elements;
|
|
277
|
+
}
|
|
278
|
+
if (isSerializedAsObject(value)) {
|
|
279
|
+
let copy;
|
|
280
|
+
for (const [key, element] of Object.entries(value)) {
|
|
281
|
+
const rewritten = rewriteNestedFileRefs(element);
|
|
282
|
+
if (rewritten !== element) {
|
|
283
|
+
copy ??= { ...value };
|
|
284
|
+
copy[key] = rewritten;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return copy ?? value;
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
}
|
|
291
|
+
function isSerializedAsObject(value) {
|
|
292
|
+
return typeof value === "object" && value !== null && !ArrayBuffer.isView(value);
|
|
293
|
+
}
|
|
257
294
|
|
|
258
295
|
// src/internal/recording.ts
|
|
259
296
|
function toPublicClip(clip) {
|
|
@@ -269,14 +306,185 @@ function toPublicClip(clip) {
|
|
|
269
306
|
}
|
|
270
307
|
|
|
271
308
|
// src/internal/stats.ts
|
|
272
|
-
var
|
|
273
|
-
|
|
309
|
+
var LOCAL_STATS_INTERVAL_MS = 2e3;
|
|
310
|
+
var RUNTIME_REPORT_INTERVAL_MS = 5e3;
|
|
311
|
+
function pollPeerStats(client, intervalMs, onReport) {
|
|
312
|
+
let stopped = false;
|
|
313
|
+
let reading = false;
|
|
314
|
+
const handle = setInterval(() => {
|
|
315
|
+
if (reading) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const peerConnection = client.getPeerConnection();
|
|
319
|
+
if (!peerConnection) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
reading = true;
|
|
323
|
+
void peerConnection.getStats().then(
|
|
324
|
+
(report) => {
|
|
325
|
+
if (stopped) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
onReport(report);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
console.error("[Reactor] stats report handler threw:", error);
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
() => {
|
|
335
|
+
}
|
|
336
|
+
).finally(() => {
|
|
337
|
+
reading = false;
|
|
338
|
+
});
|
|
339
|
+
}, intervalMs);
|
|
340
|
+
return () => {
|
|
341
|
+
stopped = true;
|
|
342
|
+
clearInterval(handle);
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function roundMs(ms) {
|
|
346
|
+
return Math.round(ms * 1e3) / 1e3;
|
|
347
|
+
}
|
|
348
|
+
function secondsToMs(seconds) {
|
|
349
|
+
return roundMs(seconds * 1e3);
|
|
350
|
+
}
|
|
351
|
+
var WIRE_CODECS = {
|
|
352
|
+
video: /* @__PURE__ */ new Set(["vp8", "vp9", "av1", "h264", "h265"]),
|
|
353
|
+
audio: /* @__PURE__ */ new Set(["opus"])
|
|
354
|
+
};
|
|
355
|
+
function createTrackStatsExtractor() {
|
|
356
|
+
const lastBytes = /* @__PURE__ */ new Map();
|
|
357
|
+
return (report, tracks, pausedTracks) => {
|
|
358
|
+
const reportWithLookup = report;
|
|
359
|
+
const stats = [];
|
|
360
|
+
const seenStreams = /* @__PURE__ */ new Set();
|
|
361
|
+
const remoteInbound = /* @__PURE__ */ new Map();
|
|
362
|
+
report.forEach((stat) => {
|
|
363
|
+
if (stat.type === "remote-inbound-rtp" && stat.localId !== void 0) {
|
|
364
|
+
remoteInbound.set(stat.localId, stat);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
report.forEach((stat) => {
|
|
368
|
+
const inbound = stat.type === "inbound-rtp";
|
|
369
|
+
if (!inbound && stat.type !== "outbound-rtp" || stat.mid === void 0) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const direction = inbound ? "recvonly" : "sendonly";
|
|
373
|
+
const streamKey = `${direction}:${stat.mid}`;
|
|
374
|
+
if (seenStreams.has(streamKey)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const track = tracks.find((entry) => entry.mid === stat.mid && entry.direction === direction);
|
|
378
|
+
const codecStat = stat.codecId !== void 0 ? reportWithLookup.get(stat.codecId) : void 0;
|
|
379
|
+
const codec = codecStat?.mimeType?.split("/")[1]?.toLowerCase();
|
|
380
|
+
if (track === void 0 || codec === void 0 || !WIRE_CODECS[track.kind].has(codec)) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
seenStreams.add(streamKey);
|
|
384
|
+
const metrics = {};
|
|
385
|
+
const put = (key, value) => {
|
|
386
|
+
if (value !== void 0) {
|
|
387
|
+
metrics[key] = value;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
const bytes = inbound ? stat.bytesReceived : stat.bytesSent;
|
|
391
|
+
if (bytes !== void 0) {
|
|
392
|
+
const previous = lastBytes.get(stat.id);
|
|
393
|
+
if (previous !== void 0 && stat.timestamp > previous.timestamp && bytes >= previous.bytes) {
|
|
394
|
+
put("bitrate_bps", Math.round((bytes - previous.bytes) * 8 * 1e3 / (stat.timestamp - previous.timestamp)));
|
|
395
|
+
}
|
|
396
|
+
lastBytes.set(stat.id, { bytes, timestamp: stat.timestamp });
|
|
397
|
+
}
|
|
398
|
+
const keyframeRequests = stat.firCount !== void 0 || stat.pliCount !== void 0 ? (stat.firCount ?? 0) + (stat.pliCount ?? 0) : void 0;
|
|
399
|
+
if (inbound) {
|
|
400
|
+
put("packets_received", stat.packetsReceived);
|
|
401
|
+
put("packets_lost", stat.packetsLost);
|
|
402
|
+
put("jitter_ms", stat.jitter !== void 0 ? secondsToMs(stat.jitter) : void 0);
|
|
403
|
+
put("nack_count", stat.nackCount);
|
|
404
|
+
if (track.kind === "video") {
|
|
405
|
+
put("frames_per_second", stat.framesPerSecond);
|
|
406
|
+
put("frames_decoded", stat.framesDecoded);
|
|
407
|
+
put("frames_dropped", stat.framesDropped);
|
|
408
|
+
put("frame_width", stat.frameWidth);
|
|
409
|
+
put("frame_height", stat.frameHeight);
|
|
410
|
+
put("keyframe_requests", keyframeRequests);
|
|
411
|
+
} else {
|
|
412
|
+
put("concealed_samples", stat.concealedSamples);
|
|
413
|
+
put("total_samples_received", stat.totalSamplesReceived);
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
const remote = remoteInbound.get(stat.id);
|
|
417
|
+
put("packets_sent", stat.packetsSent);
|
|
418
|
+
put("retransmitted_packets_sent", stat.retransmittedPacketsSent);
|
|
419
|
+
put("nack_count", stat.nackCount);
|
|
420
|
+
put("packets_lost", remote?.packetsLost);
|
|
421
|
+
put("jitter_ms", remote?.jitter !== void 0 ? secondsToMs(remote.jitter) : void 0);
|
|
422
|
+
put("round_trip_time_ms", remote?.roundTripTime !== void 0 ? secondsToMs(remote.roundTripTime) : void 0);
|
|
423
|
+
if (track.kind === "video") {
|
|
424
|
+
put("frames_per_second", stat.framesPerSecond);
|
|
425
|
+
put("frames_encoded", stat.framesEncoded);
|
|
426
|
+
put("frame_width", stat.frameWidth);
|
|
427
|
+
put("frame_height", stat.frameHeight);
|
|
428
|
+
put("keyframe_requests", keyframeRequests);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
stats.push({
|
|
432
|
+
timestamp: Date.now(),
|
|
433
|
+
trackName: track.name,
|
|
434
|
+
kind: track.kind,
|
|
435
|
+
direction,
|
|
436
|
+
codec,
|
|
437
|
+
paused: pausedTracks.includes(track.name),
|
|
438
|
+
metrics
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
return stats;
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
function toClientConnectionStat(report, timeToConnectMs) {
|
|
445
|
+
const pair = activeCandidatePair(report);
|
|
446
|
+
const metrics = {};
|
|
447
|
+
if (pair?.availableOutgoingBitrate !== void 0) {
|
|
448
|
+
metrics.available_outgoing_bitrate_bps = pair.availableOutgoingBitrate;
|
|
449
|
+
}
|
|
450
|
+
if (pair?.availableIncomingBitrate !== void 0) {
|
|
451
|
+
metrics.available_incoming_bitrate_bps = pair.availableIncomingBitrate;
|
|
452
|
+
}
|
|
453
|
+
if (pair?.currentRoundTripTime !== void 0) {
|
|
454
|
+
metrics.connection_rtt_ms = secondsToMs(pair.currentRoundTripTime);
|
|
455
|
+
}
|
|
456
|
+
if (timeToConnectMs !== void 0) {
|
|
457
|
+
metrics.time_to_connect_ms = roundMs(timeToConnectMs);
|
|
458
|
+
}
|
|
459
|
+
return { timestamp: Date.now(), metrics };
|
|
460
|
+
}
|
|
461
|
+
function activeCandidatePair(report) {
|
|
462
|
+
let namedByTransport;
|
|
463
|
+
let flaggedSelected;
|
|
464
|
+
let firstNominated;
|
|
465
|
+
report.forEach((stat) => {
|
|
466
|
+
if (stat.type === "transport" && stat.selectedCandidatePairId) {
|
|
467
|
+
namedByTransport ??= stat.selectedCandidatePairId;
|
|
468
|
+
}
|
|
469
|
+
if (stat.type === "candidate-pair" && stat.state === "succeeded") {
|
|
470
|
+
if (stat.selected) {
|
|
471
|
+
flaggedSelected ??= stat;
|
|
472
|
+
}
|
|
473
|
+
if (stat.nominated) {
|
|
474
|
+
firstNominated ??= stat;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
const named = namedByTransport !== void 0 ? report.get(namedByTransport) : void 0;
|
|
479
|
+
const namedSucceeded = named?.state === "succeeded" ? named : void 0;
|
|
480
|
+
return namedSucceeded ?? flaggedSelected ?? firstNominated;
|
|
481
|
+
}
|
|
482
|
+
function createConnectionStatsExtractor() {
|
|
274
483
|
let lastBytesReceived;
|
|
275
484
|
let lastBytesSent;
|
|
276
485
|
let lastCandPairTimestamp;
|
|
277
486
|
let lastCandPairId;
|
|
278
487
|
return (report) => {
|
|
279
|
-
let candPairId;
|
|
280
488
|
let rtt;
|
|
281
489
|
let availableOutgoingBitrate;
|
|
282
490
|
let availableIncomingBitrate;
|
|
@@ -287,40 +495,35 @@ function createRTCStatsExtractor() {
|
|
|
287
495
|
let jitter;
|
|
288
496
|
let packetLossRatio;
|
|
289
497
|
let candidateType;
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
const samePair = lastCandPairId === candPairId;
|
|
308
|
-
const timeDiff = samePair && lastCandPairTimestamp !== void 0 ? stat.timestamp - lastCandPairTimestamp : 0;
|
|
309
|
-
if (stat.bytesReceived !== void 0) {
|
|
310
|
-
if (samePair && lastBytesReceived !== void 0 && timeDiff > 0) {
|
|
311
|
-
incomingBitrate = (stat.bytesReceived - lastBytesReceived) * 8 / timeDiff * 1e3;
|
|
312
|
-
}
|
|
313
|
-
lastBytesReceived = stat.bytesReceived;
|
|
498
|
+
const pair = activeCandidatePair(report);
|
|
499
|
+
if (pair !== void 0) {
|
|
500
|
+
if (pair.currentRoundTripTime !== void 0) {
|
|
501
|
+
rtt = pair.currentRoundTripTime * 1e3;
|
|
502
|
+
}
|
|
503
|
+
availableOutgoingBitrate = pair.availableOutgoingBitrate;
|
|
504
|
+
availableIncomingBitrate = pair.availableIncomingBitrate;
|
|
505
|
+
const localCandidate = pair.localCandidateId !== void 0 ? report.get(pair.localCandidateId) : void 0;
|
|
506
|
+
if (localCandidate?.candidateType) {
|
|
507
|
+
candidateType = localCandidate.candidateType;
|
|
508
|
+
}
|
|
509
|
+
const samePair = lastCandPairId === pair.id;
|
|
510
|
+
const timeDiff = samePair && lastCandPairTimestamp !== void 0 ? pair.timestamp - lastCandPairTimestamp : 0;
|
|
511
|
+
if (pair.bytesReceived !== void 0) {
|
|
512
|
+
if (samePair && lastBytesReceived !== void 0 && timeDiff > 0) {
|
|
513
|
+
incomingBitrate = (pair.bytesReceived - lastBytesReceived) * 8 / timeDiff * 1e3;
|
|
314
514
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
515
|
+
lastBytesReceived = pair.bytesReceived;
|
|
516
|
+
}
|
|
517
|
+
if (pair.bytesSent !== void 0) {
|
|
518
|
+
if (samePair && lastBytesSent !== void 0 && timeDiff > 0) {
|
|
519
|
+
outgoingBitrate = (pair.bytesSent - lastBytesSent) * 8 / timeDiff * 1e3;
|
|
320
520
|
}
|
|
321
|
-
|
|
322
|
-
lastCandPairId = candPairId;
|
|
521
|
+
lastBytesSent = pair.bytesSent;
|
|
323
522
|
}
|
|
523
|
+
lastCandPairTimestamp = pair.timestamp;
|
|
524
|
+
lastCandPairId = pair.id;
|
|
525
|
+
}
|
|
526
|
+
report.forEach((stat) => {
|
|
324
527
|
if (videoInboundRtpId === void 0 && stat.type === "inbound-rtp" && stat.kind === "video") {
|
|
325
528
|
videoInboundRtpId = stat.id;
|
|
326
529
|
if (stat.framesPerSecond !== void 0) {
|
|
@@ -375,7 +578,7 @@ async function importWasmModule() {
|
|
|
375
578
|
// package.json
|
|
376
579
|
var package_default = {
|
|
377
580
|
name: "@reactor-team/js-sdk",
|
|
378
|
-
version: "3.0
|
|
581
|
+
version: "3.1.0",
|
|
379
582
|
description: "Connect your JavaScript/TypeScript app to a live Reactor model in the browser.",
|
|
380
583
|
license: "Apache-2.0",
|
|
381
584
|
repository: {
|
|
@@ -851,11 +1054,14 @@ var Reactor = class {
|
|
|
851
1054
|
capabilities;
|
|
852
1055
|
stats;
|
|
853
1056
|
connectionTimings;
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
*
|
|
858
|
-
|
|
1057
|
+
/** The stop functions of the running stats pollers — see `pollPeerStats()`. */
|
|
1058
|
+
stopStatsPollers = [];
|
|
1059
|
+
/** Whether this connection has already reported its time to connect —
|
|
1060
|
+
* a one-time fact, sent on the first client-stats report that carries it
|
|
1061
|
+
* and omitted on every one after. Set only once a batch actually went out
|
|
1062
|
+
* with it, so a batch sent before the timings exist doesn't use it up.
|
|
1063
|
+
* Reset per `startStatsPolling()` call, i.e. per connect()/reconnect(). */
|
|
1064
|
+
hasSentConnectTime = false;
|
|
859
1065
|
/** Set on the "connecting" status transition, cleared once `connectionTimings`
|
|
860
1066
|
* is finalized on "ready" — see `handleStatusChanged()`. */
|
|
861
1067
|
connectStartTime;
|
|
@@ -868,6 +1074,70 @@ var Reactor = class {
|
|
|
868
1074
|
* take ownership of Rust value while it was borrowed") or corrupt it
|
|
869
1075
|
* outright. */
|
|
870
1076
|
queue = new AwaitQueue();
|
|
1077
|
+
/** In-flight calls to `withReaderAccess()` — a "reply" `.catch(() => {})`
|
|
1078
|
+
* copy of each, never the caller's own promise, so one rejecting can't
|
|
1079
|
+
* make `Promise.all()` in `withWriterAccess()` reject and skip waiting
|
|
1080
|
+
* for the rest. See `withReaderAccess()`/`withWriterAccess()`. */
|
|
1081
|
+
activeReads = /* @__PURE__ */ new Set();
|
|
1082
|
+
/** True for the span of a `withWriterAccess()` call — after it has waited
|
|
1083
|
+
* out every read active when it started, before it releases the ones
|
|
1084
|
+
* that arrived while it ran. */
|
|
1085
|
+
writerActive = false;
|
|
1086
|
+
/** Readers parked in `withReaderAccess()`'s wait loop, woken once
|
|
1087
|
+
* `withWriterAccess()` finishes; each re-checks `writerActive` rather
|
|
1088
|
+
* than assuming its turn, since another writer already queued behind the
|
|
1089
|
+
* first one may have claimed it first. */
|
|
1090
|
+
writerIdleWaiters = [];
|
|
1091
|
+
/**
|
|
1092
|
+
* Runs a read — sendCommand()/requestSchema()/requestClip()/
|
|
1093
|
+
* requestRecording() — excluded from a lifecycle op (`withWriterAccess()`)
|
|
1094
|
+
* but not from other reads: unlike `queue`, concurrent reads run
|
|
1095
|
+
* concurrently. Queuing them the same way `queue` serializes writes would
|
|
1096
|
+
* head-of-line block an unrelated command, or even disconnect() itself,
|
|
1097
|
+
* behind one slow reply (the binding correlates replies independently, so
|
|
1098
|
+
* nothing requires this to be serial).
|
|
1099
|
+
*
|
|
1100
|
+
* Race-free the same way the wasm binding's own `readyState` check is: the
|
|
1101
|
+
* `writerActive` check and this call's registration in `activeReads` both
|
|
1102
|
+
* happen before the first `await` inside *fn*, so nothing can run between
|
|
1103
|
+
* them on a single-threaded JS event loop.
|
|
1104
|
+
*/
|
|
1105
|
+
async withReaderAccess(fn) {
|
|
1106
|
+
while (this.writerActive) {
|
|
1107
|
+
await new Promise((resolve) => this.writerIdleWaiters.push(resolve));
|
|
1108
|
+
}
|
|
1109
|
+
const result = fn();
|
|
1110
|
+
const tracked = result.catch(() => void 0);
|
|
1111
|
+
this.activeReads.add(tracked);
|
|
1112
|
+
try {
|
|
1113
|
+
return await result;
|
|
1114
|
+
} finally {
|
|
1115
|
+
this.activeReads.delete(tracked);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Runs a lifecycle op's client-touching body exclusively: waits out every
|
|
1120
|
+
* read already in flight, then blocks new ones (`writerActive`) until it
|
|
1121
|
+
* finishes. Only ever called from inside a `queue`-serialized task, so at
|
|
1122
|
+
* most one writer runs this at a time — it doesn't serialize against
|
|
1123
|
+
* itself, only against `activeReads`.
|
|
1124
|
+
*/
|
|
1125
|
+
async withWriterAccess(fn) {
|
|
1126
|
+
this.writerActive = true;
|
|
1127
|
+
if (this.activeReads.size > 0) {
|
|
1128
|
+
await Promise.all(this.activeReads);
|
|
1129
|
+
}
|
|
1130
|
+
try {
|
|
1131
|
+
return await fn();
|
|
1132
|
+
} finally {
|
|
1133
|
+
this.writerActive = false;
|
|
1134
|
+
const waiters = this.writerIdleWaiters;
|
|
1135
|
+
this.writerIdleWaiters = [];
|
|
1136
|
+
for (const wake of waiters) {
|
|
1137
|
+
wake();
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
871
1141
|
constructor(options) {
|
|
872
1142
|
const { jwt, ...clientOptions } = options;
|
|
873
1143
|
this.clientOptions = { sdkVersion: package_default.version, ...clientOptions };
|
|
@@ -891,10 +1161,13 @@ var Reactor = class {
|
|
|
891
1161
|
this.jwt = jwt;
|
|
892
1162
|
this.client?.setJwt(jwt);
|
|
893
1163
|
}
|
|
894
|
-
await this.queue.push(
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
1164
|
+
await this.queue.push(
|
|
1165
|
+
() => this.withWriterAccess(async () => {
|
|
1166
|
+
const client = await this.getOrCreateClient();
|
|
1167
|
+
await client.connect(options);
|
|
1168
|
+
}),
|
|
1169
|
+
"connect"
|
|
1170
|
+
);
|
|
898
1171
|
} catch (cause) {
|
|
899
1172
|
throw this.captureError(cause);
|
|
900
1173
|
}
|
|
@@ -917,15 +1190,18 @@ var Reactor = class {
|
|
|
917
1190
|
async disconnect(recoverable = false) {
|
|
918
1191
|
this.assertNotDisposed();
|
|
919
1192
|
try {
|
|
920
|
-
await this.queue.push(
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
1193
|
+
await this.queue.push(
|
|
1194
|
+
() => this.withWriterAccess(async () => {
|
|
1195
|
+
if (this.client) {
|
|
1196
|
+
await this.client.disconnect();
|
|
1197
|
+
}
|
|
1198
|
+
this.resetConnectionState();
|
|
1199
|
+
if (!recoverable) {
|
|
1200
|
+
this.freeClient();
|
|
1201
|
+
}
|
|
1202
|
+
}),
|
|
1203
|
+
"disconnect"
|
|
1204
|
+
);
|
|
929
1205
|
} catch (cause) {
|
|
930
1206
|
throw this.captureError(cause);
|
|
931
1207
|
}
|
|
@@ -939,10 +1215,13 @@ var Reactor = class {
|
|
|
939
1215
|
async reconnect(options) {
|
|
940
1216
|
this.assertNotDisposed();
|
|
941
1217
|
try {
|
|
942
|
-
await this.queue.push(
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1218
|
+
await this.queue.push(
|
|
1219
|
+
() => this.withWriterAccess(async () => {
|
|
1220
|
+
const client = await this.getOrCreateClient();
|
|
1221
|
+
await client.reconnect(options);
|
|
1222
|
+
}),
|
|
1223
|
+
"reconnect"
|
|
1224
|
+
);
|
|
946
1225
|
} catch (cause) {
|
|
947
1226
|
throw this.captureError(cause);
|
|
948
1227
|
}
|
|
@@ -957,9 +1236,11 @@ var Reactor = class {
|
|
|
957
1236
|
* mode) actually hands back `null` — normalized here so callers can rely
|
|
958
1237
|
* on the documented type instead of the binding's serialization quirk.
|
|
959
1238
|
*
|
|
960
|
-
* A `FileRef` (from `uploadFile`) may be passed
|
|
961
|
-
*
|
|
962
|
-
* separate upload reference rather than embedded in the JSON
|
|
1239
|
+
* A `FileRef` (from `uploadFile`) may be passed in `data` wherever the
|
|
1240
|
+
* model's schema declares a file. As a top-level value it is extracted and
|
|
1241
|
+
* sent as a separate upload reference rather than embedded in the JSON
|
|
1242
|
+
* payload; inside an array or object (a parameter that takes several files,
|
|
1243
|
+
* say) it is serialized as an upload reference in place.
|
|
963
1244
|
*
|
|
964
1245
|
* Unlike every other method here, this never rejects — a failure (the
|
|
965
1246
|
* session isn't `"ready"`, the send itself fails, …) is reported through
|
|
@@ -981,9 +1262,11 @@ var Reactor = class {
|
|
|
981
1262
|
}
|
|
982
1263
|
try {
|
|
983
1264
|
this.assertNotDisposed();
|
|
984
|
-
const client = await this.getOrCreateClient();
|
|
985
1265
|
const extracted = extractFileRefs(data);
|
|
986
|
-
const reply = await
|
|
1266
|
+
const reply = await this.withReaderAccess(async () => {
|
|
1267
|
+
const client = await this.getOrCreateClient();
|
|
1268
|
+
return client.sendCommand(command, extracted.data, extracted.uploads);
|
|
1269
|
+
});
|
|
987
1270
|
return reply ?? void 0;
|
|
988
1271
|
} catch (cause) {
|
|
989
1272
|
this.emitError(cause);
|
|
@@ -1010,8 +1293,10 @@ var Reactor = class {
|
|
|
1010
1293
|
switch (command) {
|
|
1011
1294
|
case "requestSchema":
|
|
1012
1295
|
try {
|
|
1013
|
-
|
|
1014
|
-
|
|
1296
|
+
await this.withReaderAccess(async () => {
|
|
1297
|
+
const client = await this.getOrCreateClient();
|
|
1298
|
+
await this.refreshSchema(client);
|
|
1299
|
+
});
|
|
1015
1300
|
} catch (cause) {
|
|
1016
1301
|
this.emitError(cause);
|
|
1017
1302
|
}
|
|
@@ -1033,8 +1318,10 @@ var Reactor = class {
|
|
|
1033
1318
|
async requestSchema() {
|
|
1034
1319
|
this.assertNotDisposed();
|
|
1035
1320
|
try {
|
|
1036
|
-
|
|
1037
|
-
|
|
1321
|
+
return await this.withReaderAccess(async () => {
|
|
1322
|
+
const client = await this.getOrCreateClient();
|
|
1323
|
+
return this.normalizeSchema(await client.requestSchema());
|
|
1324
|
+
});
|
|
1038
1325
|
} catch (cause) {
|
|
1039
1326
|
throw this.captureError(cause);
|
|
1040
1327
|
}
|
|
@@ -1174,8 +1461,10 @@ var Reactor = class {
|
|
|
1174
1461
|
async requestClip(durationSeconds) {
|
|
1175
1462
|
this.assertNotDisposed();
|
|
1176
1463
|
try {
|
|
1177
|
-
|
|
1178
|
-
|
|
1464
|
+
return await this.withReaderAccess(async () => {
|
|
1465
|
+
const client = await this.getOrCreateClient();
|
|
1466
|
+
return toPublicClip(await client.requestClip(durationSeconds));
|
|
1467
|
+
});
|
|
1179
1468
|
} catch (cause) {
|
|
1180
1469
|
throw this.captureError(cause);
|
|
1181
1470
|
}
|
|
@@ -1184,8 +1473,10 @@ var Reactor = class {
|
|
|
1184
1473
|
async requestRecording() {
|
|
1185
1474
|
this.assertNotDisposed();
|
|
1186
1475
|
try {
|
|
1187
|
-
|
|
1188
|
-
|
|
1476
|
+
return await this.withReaderAccess(async () => {
|
|
1477
|
+
const client = await this.getOrCreateClient();
|
|
1478
|
+
return toPublicClip(await client.requestRecording());
|
|
1479
|
+
});
|
|
1189
1480
|
} catch (cause) {
|
|
1190
1481
|
throw this.captureError(cause);
|
|
1191
1482
|
}
|
|
@@ -1197,8 +1488,9 @@ var Reactor = class {
|
|
|
1197
1488
|
// ── Uploads ─────────────────────────────────────────────────────────────
|
|
1198
1489
|
/**
|
|
1199
1490
|
* Uploads a file to the session's object store, resolving with a
|
|
1200
|
-
* `FileRef` to pass
|
|
1201
|
-
* (see `extractFileRefs`). The binding itself
|
|
1491
|
+
* `FileRef` to pass in a `sendCommand()`'s `data`, at the top level or
|
|
1492
|
+
* inside an array or object (see `extractFileRefs`). The binding itself
|
|
1493
|
+
* takes `(file, name?)`
|
|
1202
1494
|
* positionally and already applies the right defaulting — a bare
|
|
1203
1495
|
* `Blob`'s name falls back to `"upload"`, and an empty/missing mime type
|
|
1204
1496
|
* to `"application/octet-stream"` — so there's nothing to wrap here.
|
|
@@ -1272,7 +1564,7 @@ var Reactor = class {
|
|
|
1272
1564
|
this.resetConnectionState();
|
|
1273
1565
|
this.emitter.clear();
|
|
1274
1566
|
if (client) {
|
|
1275
|
-
void this.queue.push(() => client.free(), "dispose").catch(() => {
|
|
1567
|
+
void this.queue.push(() => this.withWriterAccess(() => Promise.resolve(client.free())), "dispose").catch(() => {
|
|
1276
1568
|
});
|
|
1277
1569
|
}
|
|
1278
1570
|
}
|
|
@@ -1298,7 +1590,7 @@ var Reactor = class {
|
|
|
1298
1590
|
getLastError() {
|
|
1299
1591
|
return this._lastError;
|
|
1300
1592
|
}
|
|
1301
|
-
/** The most recent WebRTC connection stats, polled every `
|
|
1593
|
+
/** The most recent WebRTC connection stats, polled every `LOCAL_STATS_INTERVAL_MS`
|
|
1302
1594
|
* while "ready" — see `statsUpdate`. `undefined` before the first sample. */
|
|
1303
1595
|
getStats() {
|
|
1304
1596
|
return this.stats;
|
|
@@ -1448,31 +1740,57 @@ var Reactor = class {
|
|
|
1448
1740
|
this.stopStatsPolling();
|
|
1449
1741
|
}
|
|
1450
1742
|
}
|
|
1743
|
+
/** Starts both stats pollers: the local one updates `getStats()` and emits
|
|
1744
|
+
* `statsUpdate`, the runtime one sends a client-stats batch. Each owns the
|
|
1745
|
+
* extractor it computes bitrates with, so a bitrate covers its own
|
|
1746
|
+
* poller's interval. */
|
|
1451
1747
|
startStatsPolling(client) {
|
|
1452
1748
|
this.stopStatsPolling();
|
|
1453
|
-
const
|
|
1454
|
-
const
|
|
1455
|
-
this.
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
}
|
|
1460
|
-
peerConnection.getStats().then((report) => {
|
|
1461
|
-
if (generation !== this.statsPollGeneration) {
|
|
1462
|
-
return;
|
|
1463
|
-
}
|
|
1464
|
-
this.stats = { ...extractStats(report), connectionTimings: this.connectionTimings };
|
|
1749
|
+
const extractConnectionStats = createConnectionStatsExtractor();
|
|
1750
|
+
const extractTrackStats = createTrackStatsExtractor();
|
|
1751
|
+
this.hasSentConnectTime = false;
|
|
1752
|
+
this.stopStatsPollers = [
|
|
1753
|
+
pollPeerStats(client, LOCAL_STATS_INTERVAL_MS, (report) => {
|
|
1754
|
+
this.stats = { ...extractConnectionStats(report), connectionTimings: this.connectionTimings };
|
|
1465
1755
|
this.emitter.emit("statsUpdate", this.stats);
|
|
1466
|
-
})
|
|
1467
|
-
|
|
1468
|
-
|
|
1756
|
+
}),
|
|
1757
|
+
pollPeerStats(client, RUNTIME_REPORT_INTERVAL_MS, (report) => {
|
|
1758
|
+
this.reportClientStats(client, report, extractTrackStats);
|
|
1759
|
+
})
|
|
1760
|
+
];
|
|
1761
|
+
}
|
|
1762
|
+
/** Report one quality batch to the runtime: a reading per negotiated
|
|
1763
|
+
* track — audio and video, sent and received — see
|
|
1764
|
+
* `createTrackStatsExtractor`, and the connection-wide reading — see
|
|
1765
|
+
* `toClientConnectionStat`. The connection reading goes even while no
|
|
1766
|
+
* track has one yet (none negotiated onto a named track, or no codec
|
|
1767
|
+
* reported yet), so the runtime gets the bandwidth estimate, the
|
|
1768
|
+
* round-trip time and the time to connect without waiting on the tracks.
|
|
1769
|
+
* Skipped only when there is nothing at all to say; one is due again in
|
|
1770
|
+
* `RUNTIME_REPORT_INTERVAL_MS`. The time to connect goes on the first batch
|
|
1771
|
+
* actually sent for this connection — see `hasSentConnectTime`.
|
|
1772
|
+
* `sendClientStats` throws synchronously on a closing connection; caught
|
|
1773
|
+
* here for the same reason `getStats()`'s rejection is above. */
|
|
1774
|
+
reportClientStats(client, report, extractTrackStats) {
|
|
1775
|
+
const trackStats = extractTrackStats(report, this.trackMapping(), this.pausedTracks());
|
|
1776
|
+
const timeToConnectMs = this.hasSentConnectTime ? void 0 : this.connectionTimings?.totalMs;
|
|
1777
|
+
const connectionStat = toClientConnectionStat(report, timeToConnectMs);
|
|
1778
|
+
if (trackStats.length === 0 && Object.keys(connectionStat.metrics).length === 0) {
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
try {
|
|
1782
|
+
client.sendClientStats(trackStats, connectionStat);
|
|
1783
|
+
if (timeToConnectMs !== void 0) {
|
|
1784
|
+
this.hasSentConnectTime = true;
|
|
1785
|
+
}
|
|
1786
|
+
} catch {
|
|
1787
|
+
}
|
|
1469
1788
|
}
|
|
1470
1789
|
stopStatsPolling() {
|
|
1471
|
-
this.
|
|
1472
|
-
|
|
1473
|
-
clearInterval(this.statsPollHandle);
|
|
1474
|
-
this.statsPollHandle = void 0;
|
|
1790
|
+
for (const stop of this.stopStatsPollers) {
|
|
1791
|
+
stop();
|
|
1475
1792
|
}
|
|
1793
|
+
this.stopStatsPollers = [];
|
|
1476
1794
|
this.stats = void 0;
|
|
1477
1795
|
}
|
|
1478
1796
|
/** Called on every `disconnect()` and on `[Symbol.dispose]`. Leaves
|