@reactor-team/js-sdk 3.0.0 → 3.0.2
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 +100 -133
- package/dist/index.cjs +264 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -9
- package/dist/index.d.ts +82 -9
- package/dist/index.js +264 -39
- package/dist/index.js.map +1 -1
- package/dist/wasm/reactor_wasm.d.ts +20 -3
- package/dist/wasm/reactor_wasm.js +58 -14
- package/dist/wasm/reactor_wasm_bg.wasm +0 -0
- package/dist/wasm/reactor_wasm_bg.wasm.d.ts +4 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -317,16 +317,53 @@ function extractFileRefs(data) {
|
|
|
317
317
|
let uploads;
|
|
318
318
|
let scalars;
|
|
319
319
|
for (const [key, value] of Object.entries(data)) {
|
|
320
|
-
if (
|
|
320
|
+
if (isFileRef(value)) {
|
|
321
|
+
uploads ??= {};
|
|
322
|
+
scalars ??= { ...data };
|
|
323
|
+
uploads[key] = toWireFileRef(value);
|
|
324
|
+
delete scalars[key];
|
|
321
325
|
continue;
|
|
322
326
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
+
const rewritten = rewriteNestedFileRefs(value);
|
|
328
|
+
if (rewritten !== value) {
|
|
329
|
+
scalars ??= { ...data };
|
|
330
|
+
scalars[key] = rewritten;
|
|
331
|
+
}
|
|
327
332
|
}
|
|
328
333
|
return { data: scalars ?? data, uploads };
|
|
329
334
|
}
|
|
335
|
+
function rewriteNestedFileRefs(value) {
|
|
336
|
+
if (isFileRef(value)) {
|
|
337
|
+
return toWireFileRef(value);
|
|
338
|
+
}
|
|
339
|
+
if (Array.isArray(value)) {
|
|
340
|
+
const elements = value;
|
|
341
|
+
let copy;
|
|
342
|
+
elements.forEach((element, index) => {
|
|
343
|
+
const rewritten = rewriteNestedFileRefs(element);
|
|
344
|
+
if (rewritten !== element) {
|
|
345
|
+
copy ??= [...elements];
|
|
346
|
+
copy[index] = rewritten;
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
return copy ?? elements;
|
|
350
|
+
}
|
|
351
|
+
if (isSerializedAsObject(value)) {
|
|
352
|
+
let copy;
|
|
353
|
+
for (const [key, element] of Object.entries(value)) {
|
|
354
|
+
const rewritten = rewriteNestedFileRefs(element);
|
|
355
|
+
if (rewritten !== element) {
|
|
356
|
+
copy ??= { ...value };
|
|
357
|
+
copy[key] = rewritten;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return copy ?? value;
|
|
361
|
+
}
|
|
362
|
+
return value;
|
|
363
|
+
}
|
|
364
|
+
function isSerializedAsObject(value) {
|
|
365
|
+
return typeof value === "object" && value !== null && !ArrayBuffer.isView(value);
|
|
366
|
+
}
|
|
330
367
|
|
|
331
368
|
// src/internal/recording.ts
|
|
332
369
|
function toPublicClip(clip) {
|
|
@@ -445,6 +482,69 @@ async function importWasmModule() {
|
|
|
445
482
|
return module2;
|
|
446
483
|
}
|
|
447
484
|
|
|
485
|
+
// package.json
|
|
486
|
+
var package_default = {
|
|
487
|
+
name: "@reactor-team/js-sdk",
|
|
488
|
+
version: "3.0.2",
|
|
489
|
+
description: "Connect your JavaScript/TypeScript app to a live Reactor model in the browser.",
|
|
490
|
+
license: "Apache-2.0",
|
|
491
|
+
repository: {
|
|
492
|
+
type: "git",
|
|
493
|
+
url: "git+https://github.com/reactor-team/reactor-client-sdks.git",
|
|
494
|
+
directory: "sdks/js"
|
|
495
|
+
},
|
|
496
|
+
type: "module",
|
|
497
|
+
sideEffects: false,
|
|
498
|
+
main: "./dist/index.cjs",
|
|
499
|
+
module: "./dist/index.js",
|
|
500
|
+
types: "./dist/index.d.ts",
|
|
501
|
+
exports: {
|
|
502
|
+
".": {
|
|
503
|
+
types: "./dist/index.d.ts",
|
|
504
|
+
import: "./dist/index.js",
|
|
505
|
+
require: "./dist/index.cjs"
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
files: [
|
|
509
|
+
"dist"
|
|
510
|
+
],
|
|
511
|
+
engines: {
|
|
512
|
+
node: ">=18"
|
|
513
|
+
},
|
|
514
|
+
scripts: {
|
|
515
|
+
build: "tsup && node scripts/copy-wasm.mjs",
|
|
516
|
+
prepack: "npm run build",
|
|
517
|
+
dev: "tsup --watch",
|
|
518
|
+
"type-check": "tsc --noEmit",
|
|
519
|
+
lint: "eslint .",
|
|
520
|
+
test: "vitest run",
|
|
521
|
+
clean: "rm -rf dist"
|
|
522
|
+
},
|
|
523
|
+
dependencies: {
|
|
524
|
+
awaitqueue: "^3.3.1",
|
|
525
|
+
"hls.js": "^1.6.0",
|
|
526
|
+
mp4box: "^2.4.1"
|
|
527
|
+
},
|
|
528
|
+
peerDependencies: {
|
|
529
|
+
react: "^18.0.0 || ^19.0.0"
|
|
530
|
+
},
|
|
531
|
+
devDependencies: {
|
|
532
|
+
"@eslint/js": "^9.39.5",
|
|
533
|
+
"@stylistic/eslint-plugin": "^5.10.0",
|
|
534
|
+
"@testing-library/react": "^16.0.0",
|
|
535
|
+
"@types/react": "^19.0.0",
|
|
536
|
+
eslint: "^9.39.5",
|
|
537
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
538
|
+
jsdom: "^25.0.0",
|
|
539
|
+
react: "^19.0.0",
|
|
540
|
+
"react-dom": "^19.0.0",
|
|
541
|
+
tsup: "^8.3.0",
|
|
542
|
+
typescript: "^5.6.0",
|
|
543
|
+
"typescript-eslint": "^8.67.0",
|
|
544
|
+
vitest: "^4.1.11"
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
|
|
448
548
|
// src/recording.ts
|
|
449
549
|
var RecordingError = class extends Error {
|
|
450
550
|
constructor(code, reason) {
|
|
@@ -878,9 +978,73 @@ var Reactor = class {
|
|
|
878
978
|
* take ownership of Rust value while it was borrowed") or corrupt it
|
|
879
979
|
* outright. */
|
|
880
980
|
queue = new import_awaitqueue.AwaitQueue();
|
|
981
|
+
/** In-flight calls to `withReaderAccess()` — a "reply" `.catch(() => {})`
|
|
982
|
+
* copy of each, never the caller's own promise, so one rejecting can't
|
|
983
|
+
* make `Promise.all()` in `withWriterAccess()` reject and skip waiting
|
|
984
|
+
* for the rest. See `withReaderAccess()`/`withWriterAccess()`. */
|
|
985
|
+
activeReads = /* @__PURE__ */ new Set();
|
|
986
|
+
/** True for the span of a `withWriterAccess()` call — after it has waited
|
|
987
|
+
* out every read active when it started, before it releases the ones
|
|
988
|
+
* that arrived while it ran. */
|
|
989
|
+
writerActive = false;
|
|
990
|
+
/** Readers parked in `withReaderAccess()`'s wait loop, woken once
|
|
991
|
+
* `withWriterAccess()` finishes; each re-checks `writerActive` rather
|
|
992
|
+
* than assuming its turn, since another writer already queued behind the
|
|
993
|
+
* first one may have claimed it first. */
|
|
994
|
+
writerIdleWaiters = [];
|
|
995
|
+
/**
|
|
996
|
+
* Runs a read — sendCommand()/requestSchema()/requestClip()/
|
|
997
|
+
* requestRecording() — excluded from a lifecycle op (`withWriterAccess()`)
|
|
998
|
+
* but not from other reads: unlike `queue`, concurrent reads run
|
|
999
|
+
* concurrently. Queuing them the same way `queue` serializes writes would
|
|
1000
|
+
* head-of-line block an unrelated command, or even disconnect() itself,
|
|
1001
|
+
* behind one slow reply (the binding correlates replies independently, so
|
|
1002
|
+
* nothing requires this to be serial).
|
|
1003
|
+
*
|
|
1004
|
+
* Race-free the same way the wasm binding's own `readyState` check is: the
|
|
1005
|
+
* `writerActive` check and this call's registration in `activeReads` both
|
|
1006
|
+
* happen before the first `await` inside *fn*, so nothing can run between
|
|
1007
|
+
* them on a single-threaded JS event loop.
|
|
1008
|
+
*/
|
|
1009
|
+
async withReaderAccess(fn) {
|
|
1010
|
+
while (this.writerActive) {
|
|
1011
|
+
await new Promise((resolve) => this.writerIdleWaiters.push(resolve));
|
|
1012
|
+
}
|
|
1013
|
+
const result = fn();
|
|
1014
|
+
const tracked = result.catch(() => void 0);
|
|
1015
|
+
this.activeReads.add(tracked);
|
|
1016
|
+
try {
|
|
1017
|
+
return await result;
|
|
1018
|
+
} finally {
|
|
1019
|
+
this.activeReads.delete(tracked);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* Runs a lifecycle op's client-touching body exclusively: waits out every
|
|
1024
|
+
* read already in flight, then blocks new ones (`writerActive`) until it
|
|
1025
|
+
* finishes. Only ever called from inside a `queue`-serialized task, so at
|
|
1026
|
+
* most one writer runs this at a time — it doesn't serialize against
|
|
1027
|
+
* itself, only against `activeReads`.
|
|
1028
|
+
*/
|
|
1029
|
+
async withWriterAccess(fn) {
|
|
1030
|
+
this.writerActive = true;
|
|
1031
|
+
if (this.activeReads.size > 0) {
|
|
1032
|
+
await Promise.all(this.activeReads);
|
|
1033
|
+
}
|
|
1034
|
+
try {
|
|
1035
|
+
return await fn();
|
|
1036
|
+
} finally {
|
|
1037
|
+
this.writerActive = false;
|
|
1038
|
+
const waiters = this.writerIdleWaiters;
|
|
1039
|
+
this.writerIdleWaiters = [];
|
|
1040
|
+
for (const wake of waiters) {
|
|
1041
|
+
wake();
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
881
1045
|
constructor(options) {
|
|
882
1046
|
const { jwt, ...clientOptions } = options;
|
|
883
|
-
this.clientOptions = clientOptions;
|
|
1047
|
+
this.clientOptions = { sdkVersion: package_default.version, ...clientOptions };
|
|
884
1048
|
this.jwt = jwt ?? null;
|
|
885
1049
|
}
|
|
886
1050
|
// ── Lifecycle ───────────────────────────────────────────────────────────
|
|
@@ -901,10 +1065,13 @@ var Reactor = class {
|
|
|
901
1065
|
this.jwt = jwt;
|
|
902
1066
|
this.client?.setJwt(jwt);
|
|
903
1067
|
}
|
|
904
|
-
await this.queue.push(
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1068
|
+
await this.queue.push(
|
|
1069
|
+
() => this.withWriterAccess(async () => {
|
|
1070
|
+
const client = await this.getOrCreateClient();
|
|
1071
|
+
await client.connect(options);
|
|
1072
|
+
}),
|
|
1073
|
+
"connect"
|
|
1074
|
+
);
|
|
908
1075
|
} catch (cause) {
|
|
909
1076
|
throw this.captureError(cause);
|
|
910
1077
|
}
|
|
@@ -927,15 +1094,18 @@ var Reactor = class {
|
|
|
927
1094
|
async disconnect(recoverable = false) {
|
|
928
1095
|
this.assertNotDisposed();
|
|
929
1096
|
try {
|
|
930
|
-
await this.queue.push(
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
1097
|
+
await this.queue.push(
|
|
1098
|
+
() => this.withWriterAccess(async () => {
|
|
1099
|
+
if (this.client) {
|
|
1100
|
+
await this.client.disconnect();
|
|
1101
|
+
}
|
|
1102
|
+
this.resetConnectionState();
|
|
1103
|
+
if (!recoverable) {
|
|
1104
|
+
this.freeClient();
|
|
1105
|
+
}
|
|
1106
|
+
}),
|
|
1107
|
+
"disconnect"
|
|
1108
|
+
);
|
|
939
1109
|
} catch (cause) {
|
|
940
1110
|
throw this.captureError(cause);
|
|
941
1111
|
}
|
|
@@ -949,10 +1119,13 @@ var Reactor = class {
|
|
|
949
1119
|
async reconnect(options) {
|
|
950
1120
|
this.assertNotDisposed();
|
|
951
1121
|
try {
|
|
952
|
-
await this.queue.push(
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1122
|
+
await this.queue.push(
|
|
1123
|
+
() => this.withWriterAccess(async () => {
|
|
1124
|
+
const client = await this.getOrCreateClient();
|
|
1125
|
+
await client.reconnect(options);
|
|
1126
|
+
}),
|
|
1127
|
+
"reconnect"
|
|
1128
|
+
);
|
|
956
1129
|
} catch (cause) {
|
|
957
1130
|
throw this.captureError(cause);
|
|
958
1131
|
}
|
|
@@ -967,9 +1140,11 @@ var Reactor = class {
|
|
|
967
1140
|
* mode) actually hands back `null` — normalized here so callers can rely
|
|
968
1141
|
* on the documented type instead of the binding's serialization quirk.
|
|
969
1142
|
*
|
|
970
|
-
* A `FileRef` (from `uploadFile`) may be passed
|
|
971
|
-
*
|
|
972
|
-
* separate upload reference rather than embedded in the JSON
|
|
1143
|
+
* A `FileRef` (from `uploadFile`) may be passed in `data` wherever the
|
|
1144
|
+
* model's schema declares a file. As a top-level value it is extracted and
|
|
1145
|
+
* sent as a separate upload reference rather than embedded in the JSON
|
|
1146
|
+
* payload; inside an array or object (a parameter that takes several files,
|
|
1147
|
+
* say) it is serialized as an upload reference in place.
|
|
973
1148
|
*
|
|
974
1149
|
* Unlike every other method here, this never rejects — a failure (the
|
|
975
1150
|
* session isn't `"ready"`, the send itself fails, …) is reported through
|
|
@@ -991,9 +1166,11 @@ var Reactor = class {
|
|
|
991
1166
|
}
|
|
992
1167
|
try {
|
|
993
1168
|
this.assertNotDisposed();
|
|
994
|
-
const client = await this.getOrCreateClient();
|
|
995
1169
|
const extracted = extractFileRefs(data);
|
|
996
|
-
const reply = await
|
|
1170
|
+
const reply = await this.withReaderAccess(async () => {
|
|
1171
|
+
const client = await this.getOrCreateClient();
|
|
1172
|
+
return client.sendCommand(command, extracted.data, extracted.uploads);
|
|
1173
|
+
});
|
|
997
1174
|
return reply ?? void 0;
|
|
998
1175
|
} catch (cause) {
|
|
999
1176
|
this.emitError(cause);
|
|
@@ -1020,8 +1197,10 @@ var Reactor = class {
|
|
|
1020
1197
|
switch (command) {
|
|
1021
1198
|
case "requestSchema":
|
|
1022
1199
|
try {
|
|
1023
|
-
|
|
1024
|
-
|
|
1200
|
+
await this.withReaderAccess(async () => {
|
|
1201
|
+
const client = await this.getOrCreateClient();
|
|
1202
|
+
await this.refreshSchema(client);
|
|
1203
|
+
});
|
|
1025
1204
|
} catch (cause) {
|
|
1026
1205
|
this.emitError(cause);
|
|
1027
1206
|
}
|
|
@@ -1043,8 +1222,10 @@ var Reactor = class {
|
|
|
1043
1222
|
async requestSchema() {
|
|
1044
1223
|
this.assertNotDisposed();
|
|
1045
1224
|
try {
|
|
1046
|
-
|
|
1047
|
-
|
|
1225
|
+
return await this.withReaderAccess(async () => {
|
|
1226
|
+
const client = await this.getOrCreateClient();
|
|
1227
|
+
return this.normalizeSchema(await client.requestSchema());
|
|
1228
|
+
});
|
|
1048
1229
|
} catch (cause) {
|
|
1049
1230
|
throw this.captureError(cause);
|
|
1050
1231
|
}
|
|
@@ -1123,6 +1304,45 @@ var Reactor = class {
|
|
|
1123
1304
|
throw this.captureError(cause);
|
|
1124
1305
|
}
|
|
1125
1306
|
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Bounds what one sender may spend, in bits per second.
|
|
1309
|
+
*
|
|
1310
|
+
* This is the ceiling that actually caps a video encoder, and it is easy to
|
|
1311
|
+
* hit without knowing it exists: with nothing set, a Chromium-based browser
|
|
1312
|
+
* derives a sender's maximum from the frame size alone, and that maximum is
|
|
1313
|
+
* 2500 kbps for anything above 960x540. A 720p or 1080p publish caps at
|
|
1314
|
+
* 2.5 Mbps until this raises it, however good the connection is.
|
|
1315
|
+
*
|
|
1316
|
+
* ```ts
|
|
1317
|
+
* await reactor.publishTrack('camera', cameraTrack);
|
|
1318
|
+
* await reactor.setTrackBitrate('camera', { maxBps: 8_000_000 });
|
|
1319
|
+
* ```
|
|
1320
|
+
*
|
|
1321
|
+
* A ceiling is permission, not a target: the encoder still spends only what
|
|
1322
|
+
* the congestion controller allocated and what the picture needs. What
|
|
1323
|
+
* raising it buys is headroom for the moments that would otherwise clip.
|
|
1324
|
+
*
|
|
1325
|
+
* Needs a connected client, and a published track — there is no sender to
|
|
1326
|
+
* bound before `publishTrack()`.
|
|
1327
|
+
*
|
|
1328
|
+
* Omit a bound to leave it at the browser default. `minBps` is non-standard —
|
|
1329
|
+
* Chromium honours it, other engines ignore it.
|
|
1330
|
+
*
|
|
1331
|
+
* There is no connection-wide counterpart: browsers expose no equivalent of
|
|
1332
|
+
* the native SDKs' `setBitrate`, and a method that quietly did nothing would
|
|
1333
|
+
* be worse than its absence.
|
|
1334
|
+
*/
|
|
1335
|
+
async setTrackBitrate(name, bounds = {}) {
|
|
1336
|
+
this.assertNotDisposed();
|
|
1337
|
+
try {
|
|
1338
|
+
await this.queue.push(async () => {
|
|
1339
|
+
const client = await this.getOrCreateClient();
|
|
1340
|
+
await client.setTrackBitrate(name, bounds.minBps, bounds.maxBps);
|
|
1341
|
+
}, "setTrackBitrate");
|
|
1342
|
+
} catch (cause) {
|
|
1343
|
+
throw this.captureError(cause);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1126
1346
|
/** Resumes a track previously stopped with `pauseTrack()`. */
|
|
1127
1347
|
async resumeTrack(name) {
|
|
1128
1348
|
this.assertNotDisposed();
|
|
@@ -1145,8 +1365,10 @@ var Reactor = class {
|
|
|
1145
1365
|
async requestClip(durationSeconds) {
|
|
1146
1366
|
this.assertNotDisposed();
|
|
1147
1367
|
try {
|
|
1148
|
-
|
|
1149
|
-
|
|
1368
|
+
return await this.withReaderAccess(async () => {
|
|
1369
|
+
const client = await this.getOrCreateClient();
|
|
1370
|
+
return toPublicClip(await client.requestClip(durationSeconds));
|
|
1371
|
+
});
|
|
1150
1372
|
} catch (cause) {
|
|
1151
1373
|
throw this.captureError(cause);
|
|
1152
1374
|
}
|
|
@@ -1155,8 +1377,10 @@ var Reactor = class {
|
|
|
1155
1377
|
async requestRecording() {
|
|
1156
1378
|
this.assertNotDisposed();
|
|
1157
1379
|
try {
|
|
1158
|
-
|
|
1159
|
-
|
|
1380
|
+
return await this.withReaderAccess(async () => {
|
|
1381
|
+
const client = await this.getOrCreateClient();
|
|
1382
|
+
return toPublicClip(await client.requestRecording());
|
|
1383
|
+
});
|
|
1160
1384
|
} catch (cause) {
|
|
1161
1385
|
throw this.captureError(cause);
|
|
1162
1386
|
}
|
|
@@ -1168,8 +1392,9 @@ var Reactor = class {
|
|
|
1168
1392
|
// ── Uploads ─────────────────────────────────────────────────────────────
|
|
1169
1393
|
/**
|
|
1170
1394
|
* Uploads a file to the session's object store, resolving with a
|
|
1171
|
-
* `FileRef` to pass
|
|
1172
|
-
* (see `extractFileRefs`). The binding itself
|
|
1395
|
+
* `FileRef` to pass in a `sendCommand()`'s `data`, at the top level or
|
|
1396
|
+
* inside an array or object (see `extractFileRefs`). The binding itself
|
|
1397
|
+
* takes `(file, name?)`
|
|
1173
1398
|
* positionally and already applies the right defaulting — a bare
|
|
1174
1399
|
* `Blob`'s name falls back to `"upload"`, and an empty/missing mime type
|
|
1175
1400
|
* to `"application/octet-stream"` — so there's nothing to wrap here.
|
|
@@ -1243,7 +1468,7 @@ var Reactor = class {
|
|
|
1243
1468
|
this.resetConnectionState();
|
|
1244
1469
|
this.emitter.clear();
|
|
1245
1470
|
if (client) {
|
|
1246
|
-
void this.queue.push(() => client.free(), "dispose").catch(() => {
|
|
1471
|
+
void this.queue.push(() => this.withWriterAccess(() => Promise.resolve(client.free())), "dispose").catch(() => {
|
|
1247
1472
|
});
|
|
1248
1473
|
}
|
|
1249
1474
|
}
|