@reactor-team/js-sdk 2.12.2 → 2.12.4

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.mjs CHANGED
@@ -99,7 +99,7 @@ import { z } from "zod";
99
99
  // package.json
100
100
  var package_default = {
101
101
  name: "@reactor-team/js-sdk",
102
- version: "2.12.2",
102
+ version: "2.12.4",
103
103
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
104
104
  license: "Apache-2.0",
105
105
  repository: {
@@ -156,26 +156,20 @@ var package_default = {
156
156
  packageManager: "pnpm@10.12.1",
157
157
  dependencies: {
158
158
  awaitqueue: "^3.3.0",
159
+ "hls.js": "^1.6.0",
159
160
  mp4box: "^2.3.0",
160
161
  "sdp-transform": "^3.0.0",
161
162
  zod: "^4.4.3"
162
163
  },
163
164
  peerDependencies: {
164
- "hls.js": "^1.6.0",
165
165
  react: "^17.0.0 || ^18.0.0 || ^19.0.0",
166
166
  zustand: "^5.0.6"
167
167
  },
168
- peerDependenciesMeta: {
169
- "hls.js": {
170
- optional: true
171
- }
172
- },
173
168
  devDependencies: {
174
169
  "@roamhq/wrtc": "^0.10.0",
175
170
  "@types/node": "^25",
176
171
  "@types/react": "^19.2.15",
177
172
  "@types/sdp-transform": "^2.15.0",
178
- "hls.js": "^1.6.0",
179
173
  prettier: "^3.8.4",
180
174
  tsup: "^8.5.0",
181
175
  typescript: "^6.0.3",
@@ -2501,16 +2495,29 @@ function absolutizeManifestUrls(manifestBody, playlistUrl) {
2501
2495
  }
2502
2496
  function downloadClipAsFile(_0) {
2503
2497
  return __async(this, arguments, function* (clip, filename = "reactor-clip.mp4", options = {}) {
2504
- var _a;
2505
2498
  const manifestBody = yield fetchPlaylist(clip.playlistUrl, {
2506
2499
  predictedReadyAtMs: clip.predictedReadyAtMs,
2507
2500
  signal: options.signal,
2508
2501
  jwt: options.jwt
2509
2502
  });
2510
- const { initUrl, segmentUrls } = parsePlaylist(
2511
- manifestBody,
2512
- clip.playlistUrl
2513
- );
2503
+ const blob = yield assembleClipBlob(manifestBody, clip.playlistUrl, options);
2504
+ if (filename === null) {
2505
+ return blob;
2506
+ }
2507
+ if (typeof document === "undefined" || typeof URL.createObjectURL !== "function") {
2508
+ throw new RecordingError(
2509
+ "DOWNLOAD_UNSUPPORTED",
2510
+ "downloadClipAsFile requires a DOM environment; pass filename=null to skip the download trigger"
2511
+ );
2512
+ }
2513
+ triggerBrowserDownload(blob, filename);
2514
+ return blob;
2515
+ });
2516
+ }
2517
+ function assembleClipBlob(_0, _1) {
2518
+ return __async(this, arguments, function* (manifestBody, playlistUrl, options = {}) {
2519
+ var _a;
2520
+ const { initUrl, segmentUrls } = parsePlaylist(manifestBody, playlistUrl);
2514
2521
  const orderedUrls = [initUrl, ...segmentUrls];
2515
2522
  const parts = [];
2516
2523
  let bytes = 0;
@@ -2541,18 +2548,7 @@ function downloadClipAsFile(_0) {
2541
2548
  });
2542
2549
  }
2543
2550
  const finalBytes = yield maybeRemux(parts);
2544
- const blob = new Blob([finalBytes], { type: "video/mp4" });
2545
- if (filename === null) {
2546
- return blob;
2547
- }
2548
- if (typeof document === "undefined" || typeof URL.createObjectURL !== "function") {
2549
- throw new RecordingError(
2550
- "DOWNLOAD_UNSUPPORTED",
2551
- "downloadClipAsFile requires a DOM environment; pass filename=null to skip the download trigger"
2552
- );
2553
- }
2554
- triggerBrowserDownload(blob, filename);
2555
- return blob;
2551
+ return new Blob([finalBytes], { type: "video/mp4" });
2556
2552
  });
2557
2553
  }
2558
2554
  function maybeRemux(parts) {
@@ -3253,6 +3249,12 @@ var Reactor = class {
3253
3249
  var _a;
3254
3250
  (_a = this.transportClient) == null ? void 0 : _a.resumeTrack(name);
3255
3251
  }
3252
+ /** Whether `name` is a track this client receives rather than sends. */
3253
+ isRecvonly(name) {
3254
+ return this.tracks.some(
3255
+ (track) => track.name === name && track.direction === "recvonly"
3256
+ );
3257
+ }
3256
3258
  /**
3257
3259
  * Reconnects to an existing session with a fresh transport.
3258
3260
  */
@@ -3484,12 +3486,8 @@ var Reactor = class {
3484
3486
  (name, track, stream) => {
3485
3487
  if (this.transportClient !== client) return;
3486
3488
  this.emit("trackReceived", name, track, stream);
3487
- if (this.autoResumeTracks) {
3488
- for (const track2 of this.tracks) {
3489
- if (track2.direction === "recvonly") {
3490
- this.resumeTrack(track2.name);
3491
- }
3492
- }
3489
+ if (this.autoResumeTracks && this.isRecvonly(name)) {
3490
+ this.resumeTrack(name);
3493
3491
  }
3494
3492
  }
3495
3493
  );
@@ -4380,6 +4378,102 @@ function WebcamStream({
4380
4378
 
4381
4379
  // src/react/ClipPlayer.tsx
4382
4380
  import { useContext as useContext2, useEffect as useEffect5, useRef as useRef5, useState as useState4 } from "react";
4381
+
4382
+ // src/utils/clipPlayback.ts
4383
+ function attachClipPlayback(video, source, {
4384
+ autoPlay,
4385
+ onReady,
4386
+ onError,
4387
+ loadHls = () => import("hls.js").then(
4388
+ (mod) => mod.default
4389
+ )
4390
+ }) {
4391
+ let destroyed = false;
4392
+ let failed = false;
4393
+ let ready = false;
4394
+ let hls = null;
4395
+ let mp4Url = null;
4396
+ const fail = (error) => {
4397
+ if (destroyed || failed) return;
4398
+ failed = true;
4399
+ onError(error);
4400
+ };
4401
+ const handleLoadedMetadata = () => {
4402
+ if (destroyed || failed || ready) return;
4403
+ ready = true;
4404
+ onReady();
4405
+ if (autoPlay) {
4406
+ video.play().catch(() => {
4407
+ });
4408
+ }
4409
+ };
4410
+ const handleElementError = () => {
4411
+ fail(new Error(describeMediaError(video.error)));
4412
+ };
4413
+ video.addEventListener("loadedmetadata", handleLoadedMetadata);
4414
+ video.addEventListener("error", handleElementError);
4415
+ const handle = {
4416
+ destroy: () => {
4417
+ destroyed = true;
4418
+ video.removeEventListener("loadedmetadata", handleLoadedMetadata);
4419
+ video.removeEventListener("error", handleElementError);
4420
+ hls == null ? void 0 : hls.destroy();
4421
+ hls = null;
4422
+ if (mp4Url) {
4423
+ URL.revokeObjectURL(mp4Url);
4424
+ mp4Url = null;
4425
+ }
4426
+ }
4427
+ };
4428
+ const selectPath = () => __async(null, null, function* () {
4429
+ const HlsCtor = hasMediaSource() ? yield loadHls().catch(() => null) : null;
4430
+ if (destroyed) return;
4431
+ if (HlsCtor == null ? void 0 : HlsCtor.isSupported()) {
4432
+ const instance = new HlsCtor();
4433
+ instance.loadSource(source.manifestUrl);
4434
+ instance.attachMedia(video);
4435
+ instance.on(HlsCtor.Events.ERROR, (_evt, data) => {
4436
+ var _a;
4437
+ if (destroyed) return;
4438
+ if (data.fatal) {
4439
+ fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4440
+ return;
4441
+ }
4442
+ console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
4443
+ });
4444
+ hls = instance;
4445
+ return;
4446
+ }
4447
+ const blob = yield source.assembleMp4();
4448
+ if (destroyed) return;
4449
+ mp4Url = URL.createObjectURL(blob);
4450
+ video.src = mp4Url;
4451
+ });
4452
+ selectPath().catch((err) => {
4453
+ fail(err instanceof Error ? err : new Error(String(err)));
4454
+ });
4455
+ return handle;
4456
+ }
4457
+ function hasMediaSource() {
4458
+ var _a, _b;
4459
+ const scope = globalThis;
4460
+ return Boolean(
4461
+ (_b = (_a = scope.ManagedMediaSource) != null ? _a : scope.MediaSource) != null ? _b : scope.WebKitMediaSource
4462
+ );
4463
+ }
4464
+ var MEDIA_ERROR_MESSAGES = {
4465
+ 1: "Playback was aborted.",
4466
+ 2: "A network error interrupted playback.",
4467
+ 3: "This clip could not be decoded.",
4468
+ 4: "This browser cannot play this clip. Use Download instead."
4469
+ };
4470
+ function describeMediaError(error) {
4471
+ var _a;
4472
+ const message = (_a = error && MEDIA_ERROR_MESSAGES[error.code]) != null ? _a : "This clip failed to play in this browser. Use Download instead.";
4473
+ return (error == null ? void 0 : error.message) ? `${message} (${error.message})` : message;
4474
+ }
4475
+
4476
+ // src/react/ClipPlayer.tsx
4383
4477
  import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
4384
4478
  function ClipPlayer({
4385
4479
  clip,
@@ -4417,65 +4511,13 @@ function ClipPlayer({
4417
4511
  if (!video) return;
4418
4512
  const abort = new AbortController();
4419
4513
  let cancelled = false;
4420
- let hlsInstance = null;
4514
+ let playback = null;
4421
4515
  let manifestBlobUrl = null;
4422
4516
  const fail = (error) => {
4423
4517
  if (cancelled) return;
4424
4518
  const message = error instanceof RecordingError ? `${error.code}: ${error.reason}` : error.message;
4425
4519
  setPhase({ kind: "error", message, error });
4426
4520
  };
4427
- const attachPlayer = (manifestUrl) => __async(null, null, function* () {
4428
- const canPlayNative = video.canPlayType("application/vnd.apple.mpegurl") !== "";
4429
- if (canPlayNative) {
4430
- video.src = manifestUrl;
4431
- if (autoPlayRef.current) {
4432
- video.play().catch(() => {
4433
- });
4434
- }
4435
- if (!cancelled) setPhase({ kind: "ready" });
4436
- return;
4437
- }
4438
- let HlsCtor;
4439
- try {
4440
- const mod = yield import("hls.js");
4441
- HlsCtor = mod.default;
4442
- } catch (e) {
4443
- fail(
4444
- new Error(
4445
- "HLS playback unavailable in this browser. Install `hls.js` as a peer dependency, or use Download."
4446
- )
4447
- );
4448
- return;
4449
- }
4450
- if (cancelled) return;
4451
- if (!HlsCtor.isSupported()) {
4452
- fail(
4453
- new Error("This browser cannot play HLS clips. Use Download instead.")
4454
- );
4455
- return;
4456
- }
4457
- const hls = new HlsCtor();
4458
- hls.loadSource(manifestUrl);
4459
- hls.attachMedia(video);
4460
- hls.on(HlsCtor.Events.MANIFEST_PARSED, () => {
4461
- if (cancelled) return;
4462
- setPhase({ kind: "ready" });
4463
- if (autoPlayRef.current) {
4464
- video.play().catch(() => {
4465
- });
4466
- }
4467
- });
4468
- hls.on(HlsCtor.Events.ERROR, (_evt, data) => {
4469
- var _a;
4470
- if (cancelled) return;
4471
- if (data.fatal) {
4472
- fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4473
- return;
4474
- }
4475
- console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
4476
- });
4477
- hlsInstance = hls;
4478
- });
4479
4521
  const setup = () => __async(null, null, function* () {
4480
4522
  var _a;
4481
4523
  try {
@@ -4494,7 +4536,22 @@ function ClipPlayer({
4494
4536
  if (cancelled) return;
4495
4537
  setPhase({ kind: "loading" });
4496
4538
  manifestBlobUrl = createPlayableManifestUrl(body, clip.playlistUrl);
4497
- yield attachPlayer(manifestBlobUrl);
4539
+ playback = attachClipPlayback(
4540
+ video,
4541
+ {
4542
+ manifestUrl: manifestBlobUrl,
4543
+ assembleMp4: () => assembleClipBlob(body, clip.playlistUrl, {
4544
+ signal: abort.signal
4545
+ })
4546
+ },
4547
+ {
4548
+ autoPlay: autoPlayRef.current,
4549
+ onReady: () => {
4550
+ if (!cancelled) setPhase({ kind: "ready" });
4551
+ },
4552
+ onError: fail
4553
+ }
4554
+ );
4498
4555
  } catch (err) {
4499
4556
  if (cancelled) return;
4500
4557
  if (err instanceof DOMException && err.name === "AbortError") {
@@ -4507,7 +4564,7 @@ function ClipPlayer({
4507
4564
  return () => {
4508
4565
  cancelled = true;
4509
4566
  abort.abort();
4510
- hlsInstance == null ? void 0 : hlsInstance.destroy();
4567
+ playback == null ? void 0 : playback.destroy();
4511
4568
  video.pause();
4512
4569
  video.removeAttribute("src");
4513
4570
  video.load();