@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/README.md CHANGED
@@ -208,11 +208,9 @@ const reactor = useReactor((s) => s.internal.reactor);
208
208
  const clip = await reactor.requestClip(10); // last 10 s
209
209
  ```
210
210
 
211
- `<ClipPlayer>` uses native HLS on Safari/iOS; install `hls.js` to support Chrome, Firefox, and Edge:
211
+ `<ClipPlayer>` streams clips with `hls.js` wherever the browser has Media Source Extensions — Chrome, Firefox, Edge, Safari, and iOS Safari 17.1 and later. It ships with the SDK, and nothing needs installing. The import is dynamic, so `hls.js` gets a chunk of its own: an app that renders a player fetches it when the first clip plays, and one that never renders a player leaves it out of the build.
212
212
 
213
- ```bash
214
- pnpm add hls.js
215
- ```
213
+ On iOS before 17.1, where those extensions don't exist, the player fetches the clip's chunks itself and plays the assembled MP4 from memory. The viewer waits for the whole clip rather than the first segment, and it plays.
216
214
 
217
215
  Downloads are remuxed into a flat MP4 (`start_time=0`, faststart, `major_brand=isom`) under the hood so the resulting file uploads cleanly to Twitter, Instagram, TikTok, and YouTube without any extra setup on your side — the H.264 / AAC bitstream itself is passed through untouched.
218
216
 
package/dist/index.d.mts CHANGED
@@ -194,6 +194,9 @@ interface ConnectOptions {
194
194
  * to keep recvonly tracks paused on connect and resume them individually via
195
195
  * `resumeTrack()` (e.g. multi-connection apps that only subscribe to a
196
196
  * subset of peers).
197
+ *
198
+ * A track that arrives after that is resumed on its own, so one track
199
+ * appearing never disturbs another the caller has paused.
197
200
  */
198
201
  autoResumeTracks?: boolean;
199
202
  /**
@@ -822,6 +825,8 @@ declare class Reactor {
822
825
  unpublishTrack(name: string): Promise<void>;
823
826
  pauseTrack(name: string): void;
824
827
  resumeTrack(name: string): void;
828
+ /** Whether `name` is a track this client receives rather than sends. */
829
+ private isRecvonly;
825
830
  /**
826
831
  * Reconnects to an existing session with a fresh transport.
827
832
  */
@@ -1061,15 +1066,17 @@ declare function WebcamStream({ track, audio, audioTrack, className, style, vide
1061
1066
  /**
1062
1067
  * Video preview for a captured {@link Clip}.
1063
1068
  *
1064
- * Renders an HLS manifest through the browser's native
1065
- * ``<video controls>`` element. On Safari / iOS the manifest is
1066
- * attached directly (native HLS support). On Chrome / Firefox / Edge
1067
- * ``hls.js`` is dynamically imported — declared as an **optional peer
1068
- * dependency** so consumers who don't use this component aren't billed
1069
- * the ~80 KB. If ``hls.js`` isn't installed and native HLS isn't
1070
- * available the player surfaces an inline error overlay; the
1071
- * underlying chunks remain downloadable via {@link useClipDownload} /
1072
- * {@link ClipDownloadButton} (which don't depend on hls.js at all).
1069
+ * Plays into a ``<video controls>`` element, streaming the clip with
1070
+ * ``hls.js`` wherever Media Source Extensions exist — every current
1071
+ * browser, iOS Safari 17.1 and later included. ``hls.js`` is
1072
+ * dynamically imported, so bundlers give it a chunk of its own that
1073
+ * an app importing this component fetches on first play and one that
1074
+ * never renders a player drops entirely. On iOS before 17.1 the clip
1075
+ * is assembled into a single MP4 and played from memory instead: the
1076
+ * whole clip is fetched before it starts, and it plays. Failures
1077
+ * surface in an inline error overlay, and the same clip stays
1078
+ * downloadable via {@link useClipDownload} /
1079
+ * {@link ClipDownloadButton}.
1073
1080
  *
1074
1081
  * **Preview only.** This component intentionally does *not* render a
1075
1082
  * download UI. Compose it with {@link ClipDownloadButton} or build
package/dist/index.d.ts CHANGED
@@ -194,6 +194,9 @@ interface ConnectOptions {
194
194
  * to keep recvonly tracks paused on connect and resume them individually via
195
195
  * `resumeTrack()` (e.g. multi-connection apps that only subscribe to a
196
196
  * subset of peers).
197
+ *
198
+ * A track that arrives after that is resumed on its own, so one track
199
+ * appearing never disturbs another the caller has paused.
197
200
  */
198
201
  autoResumeTracks?: boolean;
199
202
  /**
@@ -822,6 +825,8 @@ declare class Reactor {
822
825
  unpublishTrack(name: string): Promise<void>;
823
826
  pauseTrack(name: string): void;
824
827
  resumeTrack(name: string): void;
828
+ /** Whether `name` is a track this client receives rather than sends. */
829
+ private isRecvonly;
825
830
  /**
826
831
  * Reconnects to an existing session with a fresh transport.
827
832
  */
@@ -1061,15 +1066,17 @@ declare function WebcamStream({ track, audio, audioTrack, className, style, vide
1061
1066
  /**
1062
1067
  * Video preview for a captured {@link Clip}.
1063
1068
  *
1064
- * Renders an HLS manifest through the browser's native
1065
- * ``<video controls>`` element. On Safari / iOS the manifest is
1066
- * attached directly (native HLS support). On Chrome / Firefox / Edge
1067
- * ``hls.js`` is dynamically imported — declared as an **optional peer
1068
- * dependency** so consumers who don't use this component aren't billed
1069
- * the ~80 KB. If ``hls.js`` isn't installed and native HLS isn't
1070
- * available the player surfaces an inline error overlay; the
1071
- * underlying chunks remain downloadable via {@link useClipDownload} /
1072
- * {@link ClipDownloadButton} (which don't depend on hls.js at all).
1069
+ * Plays into a ``<video controls>`` element, streaming the clip with
1070
+ * ``hls.js`` wherever Media Source Extensions exist — every current
1071
+ * browser, iOS Safari 17.1 and later included. ``hls.js`` is
1072
+ * dynamically imported, so bundlers give it a chunk of its own that
1073
+ * an app importing this component fetches on first play and one that
1074
+ * never renders a player drops entirely. On iOS before 17.1 the clip
1075
+ * is assembled into a single MP4 and played from memory instead: the
1076
+ * whole clip is fetched before it starts, and it plays. Failures
1077
+ * surface in an inline error overlay, and the same clip stays
1078
+ * downloadable via {@link useClipDownload} /
1079
+ * {@link ClipDownloadButton}.
1073
1080
  *
1074
1081
  * **Preview only.** This component intentionally does *not* render a
1075
1082
  * download UI. Compose it with {@link ClipDownloadButton} or build
package/dist/index.js CHANGED
@@ -162,7 +162,7 @@ var import_zod = require("zod");
162
162
  // package.json
163
163
  var package_default = {
164
164
  name: "@reactor-team/js-sdk",
165
- version: "2.12.2",
165
+ version: "2.12.4",
166
166
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
167
167
  license: "Apache-2.0",
168
168
  repository: {
@@ -219,26 +219,20 @@ var package_default = {
219
219
  packageManager: "pnpm@10.12.1",
220
220
  dependencies: {
221
221
  awaitqueue: "^3.3.0",
222
+ "hls.js": "^1.6.0",
222
223
  mp4box: "^2.3.0",
223
224
  "sdp-transform": "^3.0.0",
224
225
  zod: "^4.4.3"
225
226
  },
226
227
  peerDependencies: {
227
- "hls.js": "^1.6.0",
228
228
  react: "^17.0.0 || ^18.0.0 || ^19.0.0",
229
229
  zustand: "^5.0.6"
230
230
  },
231
- peerDependenciesMeta: {
232
- "hls.js": {
233
- optional: true
234
- }
235
- },
236
231
  devDependencies: {
237
232
  "@roamhq/wrtc": "^0.10.0",
238
233
  "@types/node": "^25",
239
234
  "@types/react": "^19.2.15",
240
235
  "@types/sdp-transform": "^2.15.0",
241
- "hls.js": "^1.6.0",
242
236
  prettier: "^3.8.4",
243
237
  tsup: "^8.5.0",
244
238
  typescript: "^6.0.3",
@@ -2564,16 +2558,29 @@ function absolutizeManifestUrls(manifestBody, playlistUrl) {
2564
2558
  }
2565
2559
  function downloadClipAsFile(_0) {
2566
2560
  return __async(this, arguments, function* (clip, filename = "reactor-clip.mp4", options = {}) {
2567
- var _a;
2568
2561
  const manifestBody = yield fetchPlaylist(clip.playlistUrl, {
2569
2562
  predictedReadyAtMs: clip.predictedReadyAtMs,
2570
2563
  signal: options.signal,
2571
2564
  jwt: options.jwt
2572
2565
  });
2573
- const { initUrl, segmentUrls } = parsePlaylist(
2574
- manifestBody,
2575
- clip.playlistUrl
2576
- );
2566
+ const blob = yield assembleClipBlob(manifestBody, clip.playlistUrl, options);
2567
+ if (filename === null) {
2568
+ return blob;
2569
+ }
2570
+ if (typeof document === "undefined" || typeof URL.createObjectURL !== "function") {
2571
+ throw new RecordingError(
2572
+ "DOWNLOAD_UNSUPPORTED",
2573
+ "downloadClipAsFile requires a DOM environment; pass filename=null to skip the download trigger"
2574
+ );
2575
+ }
2576
+ triggerBrowserDownload(blob, filename);
2577
+ return blob;
2578
+ });
2579
+ }
2580
+ function assembleClipBlob(_0, _1) {
2581
+ return __async(this, arguments, function* (manifestBody, playlistUrl, options = {}) {
2582
+ var _a;
2583
+ const { initUrl, segmentUrls } = parsePlaylist(manifestBody, playlistUrl);
2577
2584
  const orderedUrls = [initUrl, ...segmentUrls];
2578
2585
  const parts = [];
2579
2586
  let bytes = 0;
@@ -2604,18 +2611,7 @@ function downloadClipAsFile(_0) {
2604
2611
  });
2605
2612
  }
2606
2613
  const finalBytes = yield maybeRemux(parts);
2607
- const blob = new Blob([finalBytes], { type: "video/mp4" });
2608
- if (filename === null) {
2609
- return blob;
2610
- }
2611
- if (typeof document === "undefined" || typeof URL.createObjectURL !== "function") {
2612
- throw new RecordingError(
2613
- "DOWNLOAD_UNSUPPORTED",
2614
- "downloadClipAsFile requires a DOM environment; pass filename=null to skip the download trigger"
2615
- );
2616
- }
2617
- triggerBrowserDownload(blob, filename);
2618
- return blob;
2614
+ return new Blob([finalBytes], { type: "video/mp4" });
2619
2615
  });
2620
2616
  }
2621
2617
  function maybeRemux(parts) {
@@ -3316,6 +3312,12 @@ var Reactor = class {
3316
3312
  var _a;
3317
3313
  (_a = this.transportClient) == null ? void 0 : _a.resumeTrack(name);
3318
3314
  }
3315
+ /** Whether `name` is a track this client receives rather than sends. */
3316
+ isRecvonly(name) {
3317
+ return this.tracks.some(
3318
+ (track) => track.name === name && track.direction === "recvonly"
3319
+ );
3320
+ }
3319
3321
  /**
3320
3322
  * Reconnects to an existing session with a fresh transport.
3321
3323
  */
@@ -3547,12 +3549,8 @@ var Reactor = class {
3547
3549
  (name, track, stream) => {
3548
3550
  if (this.transportClient !== client) return;
3549
3551
  this.emit("trackReceived", name, track, stream);
3550
- if (this.autoResumeTracks) {
3551
- for (const track2 of this.tracks) {
3552
- if (track2.direction === "recvonly") {
3553
- this.resumeTrack(track2.name);
3554
- }
3555
- }
3552
+ if (this.autoResumeTracks && this.isRecvonly(name)) {
3553
+ this.resumeTrack(name);
3556
3554
  }
3557
3555
  }
3558
3556
  );
@@ -4437,6 +4435,102 @@ function WebcamStream({
4437
4435
 
4438
4436
  // src/react/ClipPlayer.tsx
4439
4437
  var import_react7 = require("react");
4438
+
4439
+ // src/utils/clipPlayback.ts
4440
+ function attachClipPlayback(video, source, {
4441
+ autoPlay,
4442
+ onReady,
4443
+ onError,
4444
+ loadHls = () => import("hls.js").then(
4445
+ (mod) => mod.default
4446
+ )
4447
+ }) {
4448
+ let destroyed = false;
4449
+ let failed = false;
4450
+ let ready = false;
4451
+ let hls = null;
4452
+ let mp4Url = null;
4453
+ const fail = (error) => {
4454
+ if (destroyed || failed) return;
4455
+ failed = true;
4456
+ onError(error);
4457
+ };
4458
+ const handleLoadedMetadata = () => {
4459
+ if (destroyed || failed || ready) return;
4460
+ ready = true;
4461
+ onReady();
4462
+ if (autoPlay) {
4463
+ video.play().catch(() => {
4464
+ });
4465
+ }
4466
+ };
4467
+ const handleElementError = () => {
4468
+ fail(new Error(describeMediaError(video.error)));
4469
+ };
4470
+ video.addEventListener("loadedmetadata", handleLoadedMetadata);
4471
+ video.addEventListener("error", handleElementError);
4472
+ const handle = {
4473
+ destroy: () => {
4474
+ destroyed = true;
4475
+ video.removeEventListener("loadedmetadata", handleLoadedMetadata);
4476
+ video.removeEventListener("error", handleElementError);
4477
+ hls == null ? void 0 : hls.destroy();
4478
+ hls = null;
4479
+ if (mp4Url) {
4480
+ URL.revokeObjectURL(mp4Url);
4481
+ mp4Url = null;
4482
+ }
4483
+ }
4484
+ };
4485
+ const selectPath = () => __async(null, null, function* () {
4486
+ const HlsCtor = hasMediaSource() ? yield loadHls().catch(() => null) : null;
4487
+ if (destroyed) return;
4488
+ if (HlsCtor == null ? void 0 : HlsCtor.isSupported()) {
4489
+ const instance = new HlsCtor();
4490
+ instance.loadSource(source.manifestUrl);
4491
+ instance.attachMedia(video);
4492
+ instance.on(HlsCtor.Events.ERROR, (_evt, data) => {
4493
+ var _a;
4494
+ if (destroyed) return;
4495
+ if (data.fatal) {
4496
+ fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4497
+ return;
4498
+ }
4499
+ console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
4500
+ });
4501
+ hls = instance;
4502
+ return;
4503
+ }
4504
+ const blob = yield source.assembleMp4();
4505
+ if (destroyed) return;
4506
+ mp4Url = URL.createObjectURL(blob);
4507
+ video.src = mp4Url;
4508
+ });
4509
+ selectPath().catch((err) => {
4510
+ fail(err instanceof Error ? err : new Error(String(err)));
4511
+ });
4512
+ return handle;
4513
+ }
4514
+ function hasMediaSource() {
4515
+ var _a, _b;
4516
+ const scope = globalThis;
4517
+ return Boolean(
4518
+ (_b = (_a = scope.ManagedMediaSource) != null ? _a : scope.MediaSource) != null ? _b : scope.WebKitMediaSource
4519
+ );
4520
+ }
4521
+ var MEDIA_ERROR_MESSAGES = {
4522
+ 1: "Playback was aborted.",
4523
+ 2: "A network error interrupted playback.",
4524
+ 3: "This clip could not be decoded.",
4525
+ 4: "This browser cannot play this clip. Use Download instead."
4526
+ };
4527
+ function describeMediaError(error) {
4528
+ var _a;
4529
+ const message = (_a = error && MEDIA_ERROR_MESSAGES[error.code]) != null ? _a : "This clip failed to play in this browser. Use Download instead.";
4530
+ return (error == null ? void 0 : error.message) ? `${message} (${error.message})` : message;
4531
+ }
4532
+
4533
+ // src/react/ClipPlayer.tsx
4440
4534
  var import_jsx_runtime4 = require("react/jsx-runtime");
4441
4535
  function ClipPlayer({
4442
4536
  clip,
@@ -4474,65 +4568,13 @@ function ClipPlayer({
4474
4568
  if (!video) return;
4475
4569
  const abort = new AbortController();
4476
4570
  let cancelled = false;
4477
- let hlsInstance = null;
4571
+ let playback = null;
4478
4572
  let manifestBlobUrl = null;
4479
4573
  const fail = (error) => {
4480
4574
  if (cancelled) return;
4481
4575
  const message = error instanceof RecordingError ? `${error.code}: ${error.reason}` : error.message;
4482
4576
  setPhase({ kind: "error", message, error });
4483
4577
  };
4484
- const attachPlayer = (manifestUrl) => __async(null, null, function* () {
4485
- const canPlayNative = video.canPlayType("application/vnd.apple.mpegurl") !== "";
4486
- if (canPlayNative) {
4487
- video.src = manifestUrl;
4488
- if (autoPlayRef.current) {
4489
- video.play().catch(() => {
4490
- });
4491
- }
4492
- if (!cancelled) setPhase({ kind: "ready" });
4493
- return;
4494
- }
4495
- let HlsCtor;
4496
- try {
4497
- const mod = yield import("hls.js");
4498
- HlsCtor = mod.default;
4499
- } catch (e) {
4500
- fail(
4501
- new Error(
4502
- "HLS playback unavailable in this browser. Install `hls.js` as a peer dependency, or use Download."
4503
- )
4504
- );
4505
- return;
4506
- }
4507
- if (cancelled) return;
4508
- if (!HlsCtor.isSupported()) {
4509
- fail(
4510
- new Error("This browser cannot play HLS clips. Use Download instead.")
4511
- );
4512
- return;
4513
- }
4514
- const hls = new HlsCtor();
4515
- hls.loadSource(manifestUrl);
4516
- hls.attachMedia(video);
4517
- hls.on(HlsCtor.Events.MANIFEST_PARSED, () => {
4518
- if (cancelled) return;
4519
- setPhase({ kind: "ready" });
4520
- if (autoPlayRef.current) {
4521
- video.play().catch(() => {
4522
- });
4523
- }
4524
- });
4525
- hls.on(HlsCtor.Events.ERROR, (_evt, data) => {
4526
- var _a;
4527
- if (cancelled) return;
4528
- if (data.fatal) {
4529
- fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4530
- return;
4531
- }
4532
- console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
4533
- });
4534
- hlsInstance = hls;
4535
- });
4536
4578
  const setup = () => __async(null, null, function* () {
4537
4579
  var _a;
4538
4580
  try {
@@ -4551,7 +4593,22 @@ function ClipPlayer({
4551
4593
  if (cancelled) return;
4552
4594
  setPhase({ kind: "loading" });
4553
4595
  manifestBlobUrl = createPlayableManifestUrl(body, clip.playlistUrl);
4554
- yield attachPlayer(manifestBlobUrl);
4596
+ playback = attachClipPlayback(
4597
+ video,
4598
+ {
4599
+ manifestUrl: manifestBlobUrl,
4600
+ assembleMp4: () => assembleClipBlob(body, clip.playlistUrl, {
4601
+ signal: abort.signal
4602
+ })
4603
+ },
4604
+ {
4605
+ autoPlay: autoPlayRef.current,
4606
+ onReady: () => {
4607
+ if (!cancelled) setPhase({ kind: "ready" });
4608
+ },
4609
+ onError: fail
4610
+ }
4611
+ );
4555
4612
  } catch (err) {
4556
4613
  if (cancelled) return;
4557
4614
  if (err instanceof DOMException && err.name === "AbortError") {
@@ -4564,7 +4621,7 @@ function ClipPlayer({
4564
4621
  return () => {
4565
4622
  cancelled = true;
4566
4623
  abort.abort();
4567
- hlsInstance == null ? void 0 : hlsInstance.destroy();
4624
+ playback == null ? void 0 : playback.destroy();
4568
4625
  video.pause();
4569
4626
  video.removeAttribute("src");
4570
4627
  video.load();