@reactor-team/js-sdk 2.12.3 → 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
@@ -1066,15 +1066,17 @@ declare function WebcamStream({ track, audio, audioTrack, className, style, vide
1066
1066
  /**
1067
1067
  * Video preview for a captured {@link Clip}.
1068
1068
  *
1069
- * Renders an HLS manifest through the browser's native
1070
- * ``<video controls>`` element. On Safari / iOS the manifest is
1071
- * attached directly (native HLS support). On Chrome / Firefox / Edge
1072
- * ``hls.js`` is dynamically imported — declared as an **optional peer
1073
- * dependency** so consumers who don't use this component aren't billed
1074
- * the ~80 KB. If ``hls.js`` isn't installed and native HLS isn't
1075
- * available the player surfaces an inline error overlay; the
1076
- * underlying chunks remain downloadable via {@link useClipDownload} /
1077
- * {@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}.
1078
1080
  *
1079
1081
  * **Preview only.** This component intentionally does *not* render a
1080
1082
  * download UI. Compose it with {@link ClipDownloadButton} or build
package/dist/index.d.ts CHANGED
@@ -1066,15 +1066,17 @@ declare function WebcamStream({ track, audio, audioTrack, className, style, vide
1066
1066
  /**
1067
1067
  * Video preview for a captured {@link Clip}.
1068
1068
  *
1069
- * Renders an HLS manifest through the browser's native
1070
- * ``<video controls>`` element. On Safari / iOS the manifest is
1071
- * attached directly (native HLS support). On Chrome / Firefox / Edge
1072
- * ``hls.js`` is dynamically imported — declared as an **optional peer
1073
- * dependency** so consumers who don't use this component aren't billed
1074
- * the ~80 KB. If ``hls.js`` isn't installed and native HLS isn't
1075
- * available the player surfaces an inline error overlay; the
1076
- * underlying chunks remain downloadable via {@link useClipDownload} /
1077
- * {@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}.
1078
1080
  *
1079
1081
  * **Preview only.** This component intentionally does *not* render a
1080
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.3",
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) {
@@ -4439,6 +4435,102 @@ function WebcamStream({
4439
4435
 
4440
4436
  // src/react/ClipPlayer.tsx
4441
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
4442
4534
  var import_jsx_runtime4 = require("react/jsx-runtime");
4443
4535
  function ClipPlayer({
4444
4536
  clip,
@@ -4476,65 +4568,13 @@ function ClipPlayer({
4476
4568
  if (!video) return;
4477
4569
  const abort = new AbortController();
4478
4570
  let cancelled = false;
4479
- let hlsInstance = null;
4571
+ let playback = null;
4480
4572
  let manifestBlobUrl = null;
4481
4573
  const fail = (error) => {
4482
4574
  if (cancelled) return;
4483
4575
  const message = error instanceof RecordingError ? `${error.code}: ${error.reason}` : error.message;
4484
4576
  setPhase({ kind: "error", message, error });
4485
4577
  };
4486
- const attachPlayer = (manifestUrl) => __async(null, null, function* () {
4487
- const canPlayNative = video.canPlayType("application/vnd.apple.mpegurl") !== "";
4488
- if (canPlayNative) {
4489
- video.src = manifestUrl;
4490
- if (autoPlayRef.current) {
4491
- video.play().catch(() => {
4492
- });
4493
- }
4494
- if (!cancelled) setPhase({ kind: "ready" });
4495
- return;
4496
- }
4497
- let HlsCtor;
4498
- try {
4499
- const mod = yield import("hls.js");
4500
- HlsCtor = mod.default;
4501
- } catch (e) {
4502
- fail(
4503
- new Error(
4504
- "HLS playback unavailable in this browser. Install `hls.js` as a peer dependency, or use Download."
4505
- )
4506
- );
4507
- return;
4508
- }
4509
- if (cancelled) return;
4510
- if (!HlsCtor.isSupported()) {
4511
- fail(
4512
- new Error("This browser cannot play HLS clips. Use Download instead.")
4513
- );
4514
- return;
4515
- }
4516
- const hls = new HlsCtor();
4517
- hls.loadSource(manifestUrl);
4518
- hls.attachMedia(video);
4519
- hls.on(HlsCtor.Events.MANIFEST_PARSED, () => {
4520
- if (cancelled) return;
4521
- setPhase({ kind: "ready" });
4522
- if (autoPlayRef.current) {
4523
- video.play().catch(() => {
4524
- });
4525
- }
4526
- });
4527
- hls.on(HlsCtor.Events.ERROR, (_evt, data) => {
4528
- var _a;
4529
- if (cancelled) return;
4530
- if (data.fatal) {
4531
- fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4532
- return;
4533
- }
4534
- console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
4535
- });
4536
- hlsInstance = hls;
4537
- });
4538
4578
  const setup = () => __async(null, null, function* () {
4539
4579
  var _a;
4540
4580
  try {
@@ -4553,7 +4593,22 @@ function ClipPlayer({
4553
4593
  if (cancelled) return;
4554
4594
  setPhase({ kind: "loading" });
4555
4595
  manifestBlobUrl = createPlayableManifestUrl(body, clip.playlistUrl);
4556
- 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
+ );
4557
4612
  } catch (err) {
4558
4613
  if (cancelled) return;
4559
4614
  if (err instanceof DOMException && err.name === "AbortError") {
@@ -4566,7 +4621,7 @@ function ClipPlayer({
4566
4621
  return () => {
4567
4622
  cancelled = true;
4568
4623
  abort.abort();
4569
- hlsInstance == null ? void 0 : hlsInstance.destroy();
4624
+ playback == null ? void 0 : playback.destroy();
4570
4625
  video.pause();
4571
4626
  video.removeAttribute("src");
4572
4627
  video.load();