@pollen-robotics/reachy-mini-sdk 1.10.0-dev.0.main.b50d97e → 1.10.0-dev.0.main.e7f2273

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.
@@ -1,7 +1,7 @@
1
- import { decodeCredsFromHash as O, PROTOCOL_VERSION as g, PROTOCOL_SOURCE as y, isProtocolMessage as N } from "../lib/protocol.js";
2
- const _ = 8e3, M = 2e3, R = 900 * 1e3;
3
- let k = null, S = "*";
4
- function P() {
1
+ import { decodeCredsFromHash as M, PROTOCOL_VERSION as g, PROTOCOL_SOURCE as w, isProtocolMessage as O } from "../lib/protocol.js";
2
+ const L = 8e3, P = 2e3, U = 900 * 1e3;
3
+ let k = null, T = null, S = "*";
4
+ function R() {
5
5
  try {
6
6
  if (typeof document < "u" && document.referrer)
7
7
  return new URL(document.referrer).origin;
@@ -9,47 +9,66 @@ function P() {
9
9
  }
10
10
  return "*";
11
11
  }
12
- async function q(t = {}) {
13
- return k || (k = C(t)), await k;
12
+ async function B(t = {}) {
13
+ return k || (k = A(t)), await k;
14
14
  }
15
- async function C(t) {
16
- S = P();
17
- const r = t.expectedOrigin ?? S, n = O(window.location.hash);
18
- if (A(), !n)
15
+ async function z(t = {}) {
16
+ return T || (T = D(t)), await T;
17
+ }
18
+ async function A(t) {
19
+ S = R();
20
+ const r = t.expectedOrigin ?? S, n = M(window.location.hash);
21
+ if (H(), !n)
19
22
  throw new Error(
20
23
  "[reachy-mini-sdk/host/embed] no creds bundle found in URL hash. Was the embed mounted directly without ?embedded=1#creds=...?"
21
24
  );
22
- if (!await U(_))
25
+ if (!await F(L))
23
26
  throw new Error(
24
- `[reachy-mini-sdk/host/embed] window.ReachyMini did not become available within ${_}ms - check the SDK CDN tag.`
27
+ `[reachy-mini-sdk/host/embed] window.ReachyMini did not become available within ${L}ms - check the SDK CDN tag.`
25
28
  );
26
29
  const e = n.userName ?? n.username ?? null;
27
- n.hfToken && e && D(n.hfToken, e);
30
+ n.hfToken && e && j(n.hfToken, e);
28
31
  const a = new window.ReachyMini({
29
32
  appName: n.appName,
30
33
  signalingUrl: n.signalingUrl,
31
34
  ...t.sdkOptions
32
- }), c = H(r);
35
+ }), d = C(r);
33
36
  b({
34
- source: y,
37
+ source: w,
35
38
  type: "embed:ready",
36
39
  version: g
37
- }), c.start();
38
- const u = await c.awaitHostInit(M, n);
39
- w("connecting", "link"), f("boot:link:start", { robotPeerId: u.robotPeerId }), await a.authenticate(), f("boot:authenticate:ok", { state: a.state }), await a.connect(), f("boot:connect:ok", {
40
+ }), d.start();
41
+ const u = await d.awaitHostInit(P, n);
42
+ y("connecting", "link"), p("boot:link:start", { robotPeerId: u.robotPeerId }), await a.authenticate(), p("boot:authenticate:ok", { state: a.state }), await a.connect(), p("boot:connect:ok", {
40
43
  state: a.state,
41
44
  robots: (a.robots ?? []).length
42
- }), w("connecting", "session"), f("boot:session:start", { robotPeerId: u.robotPeerId }), $(a);
45
+ }), y("connecting", "session"), p("boot:session:start", { robotPeerId: u.robotPeerId }), $(a);
43
46
  try {
44
- await a.startSession(u.robotPeerId), f("boot:session:ok");
45
- } catch (d) {
46
- throw f("boot:session:error", {
47
- message: d?.message ?? String(d)
48
- }), d;
47
+ await a.startSession(u.robotPeerId), p("boot:session:ok");
48
+ } catch (l) {
49
+ throw p("boot:session:error", {
50
+ message: l?.message ?? String(l)
51
+ }), l;
49
52
  }
50
- return w("connecting", "wake"), f("boot:wake:start"), await a.ensureAwake(), f("boot:wake:ok"), c.attachPageHide(a), w("live", null), J(a), c.buildHandle(a, u);
53
+ return y("connecting", "wake"), p("boot:wake:start"), await a.ensureAwake(), p("boot:wake:ok"), d.attachPageHide(a), y("live", null), K(a), d.buildHandle(a, u);
51
54
  }
52
- function L(t) {
55
+ async function D(t) {
56
+ S = R();
57
+ const r = t.expectedOrigin ?? S, n = M(window.location.hash);
58
+ if (H(), !n)
59
+ throw new Error(
60
+ "[reachy-mini-sdk/host/embed] no creds bundle found in URL hash. Was the embed mounted directly without ?embedded=1#creds=...?"
61
+ );
62
+ const c = C(r);
63
+ b({
64
+ source: w,
65
+ type: "embed:ready",
66
+ version: g
67
+ }), c.start();
68
+ const e = await c.awaitHostInit(P, n);
69
+ return c.attachPageHideCredsOnly(), y("live", null), c.buildCredsOnlyHandle(e, n);
70
+ }
71
+ function E(t) {
53
72
  return {
54
73
  theme: t.theme,
55
74
  config: t.config,
@@ -59,7 +78,7 @@ function L(t) {
59
78
  robotPeerId: t.robotPeerId
60
79
  };
61
80
  }
62
- function E(t) {
81
+ function _(t) {
63
82
  return {
64
83
  theme: t.theme,
65
84
  config: t.config,
@@ -69,13 +88,13 @@ function E(t) {
69
88
  robotPeerId: t.robotPeerId
70
89
  };
71
90
  }
72
- function H(t) {
73
- const r = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Set();
74
- let e = null, a = !1, c = !1, u = null;
75
- function d(o) {
91
+ function C(t) {
92
+ const r = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set();
93
+ let e = null, a = !1, d = !1, u = null;
94
+ function l(o) {
76
95
  switch (o.type) {
77
96
  case "host:init": {
78
- e = E(o), n.forEach((i) => i(e.theme)), l.forEach((i) => i(e.config));
97
+ e = _(o), n.forEach((i) => i(e.theme)), c.forEach((i) => i(e.config));
79
98
  break;
80
99
  }
81
100
  case "host:theme-changed": {
@@ -83,7 +102,7 @@ function H(t) {
83
102
  break;
84
103
  }
85
104
  case "host:config-changed": {
86
- e && (e.config = o.config), l.forEach((i) => i(o.config));
105
+ e && (e.config = o.config), c.forEach((i) => i(o.config));
87
106
  break;
88
107
  }
89
108
  case "host:leaving": {
@@ -103,21 +122,21 @@ function H(t) {
103
122
  }
104
123
  return {
105
124
  start() {
106
- c || (c = !0, u = (o) => {
107
- t !== "*" && o.origin !== t || N(o.data) && d(o.data);
125
+ d || (d = !0, u = (o) => {
126
+ t !== "*" && o.origin !== t || O(o.data) && l(o.data);
108
127
  }, window.addEventListener("message", u));
109
128
  },
110
129
  async awaitHostInit(o, i) {
111
130
  return window.parent !== window ? e || new Promise((h) => {
112
- const p = (v) => {
113
- if (t !== "*" && v.origin !== t || !N(v.data)) return;
114
- const T = v.data;
115
- T.type === "host:init" && (window.removeEventListener("message", p), window.clearTimeout(I), e = E(T), h(e));
116
- }, I = window.setTimeout(() => {
117
- window.removeEventListener("message", p), e || (e = L(i)), h(e);
131
+ const m = (I) => {
132
+ if (t !== "*" && I.origin !== t || !O(I.data)) return;
133
+ const N = I.data;
134
+ N.type === "host:init" && (window.removeEventListener("message", m), window.clearTimeout(v), e = _(N), h(e));
135
+ }, v = window.setTimeout(() => {
136
+ window.removeEventListener("message", m), e || (e = E(i)), h(e);
118
137
  }, o);
119
- window.addEventListener("message", p);
120
- }) : (e = L(i), e);
138
+ window.addEventListener("message", m);
139
+ }) : (e = E(i), e);
121
140
  },
122
141
  attachPageHide(o) {
123
142
  const i = () => {
@@ -129,12 +148,83 @@ function H(t) {
129
148
  };
130
149
  window.addEventListener("pagehide", i, { once: !0 });
131
150
  },
151
+ /** Credentials-only variant — no SDK to stop; just fire onLeave. */
152
+ attachPageHideCredsOnly() {
153
+ window.addEventListener("pagehide", s, { once: !0 });
154
+ },
155
+ /**
156
+ * Build the credentials-only handle. Same lifecycle accessors as
157
+ * `buildHandle` but exposes `hfToken` / `robotPeerId` /
158
+ * `signalingUrl` (from the creds bundle) instead of a `reachy`
159
+ * SDK instance, so the embed app can forward those to its
160
+ * backend.
161
+ */
162
+ buildCredsOnlyHandle(o, i) {
163
+ if (e = o, !i.hfToken)
164
+ throw new Error(
165
+ "[reachy-mini-sdk/host/embed] creds-only handle requires hfToken; the host shell did not provide one (visitor logged out?)."
166
+ );
167
+ return {
168
+ hfToken: i.hfToken,
169
+ robotPeerId: o.robotPeerId,
170
+ signalingUrl: i.signalingUrl,
171
+ get theme() {
172
+ return e.theme;
173
+ },
174
+ get config() {
175
+ return e.config;
176
+ },
177
+ get appName() {
178
+ return e.appName;
179
+ },
180
+ get hostName() {
181
+ return e.hostName;
182
+ },
183
+ get userName() {
184
+ return e.userName;
185
+ },
186
+ onLeave(f) {
187
+ return r.add(f), () => r.delete(f);
188
+ },
189
+ onThemeChange(f) {
190
+ return n.add(f), () => n.delete(f);
191
+ },
192
+ onConfigChange(f) {
193
+ const h = (m) => f(m);
194
+ return c.add(h), () => c.delete(h);
195
+ },
196
+ setAppState(f) {
197
+ y(
198
+ f.phase,
199
+ f.connectingStep ?? null,
200
+ f.message ?? null
201
+ );
202
+ },
203
+ requestLeave() {
204
+ b({
205
+ source: w,
206
+ type: "embed:request-leave",
207
+ version: g
208
+ });
209
+ },
210
+ reportError(f, h) {
211
+ b({
212
+ source: w,
213
+ type: "embed:error",
214
+ version: g,
215
+ message: f,
216
+ fatal: h?.fatal === !0,
217
+ detail: h?.detail
218
+ });
219
+ }
220
+ };
221
+ },
132
222
  buildHandle(o, i) {
133
223
  e = i;
134
- const m = K(o);
224
+ const f = W(o);
135
225
  return {
136
226
  reachy: o,
137
- media: m,
227
+ media: f,
138
228
  get theme() {
139
229
  return e.theme;
140
230
  },
@@ -157,11 +247,11 @@ function H(t) {
157
247
  return n.add(h), () => n.delete(h);
158
248
  },
159
249
  onConfigChange(h) {
160
- const p = (I) => h(I);
161
- return l.add(p), () => l.delete(p);
250
+ const m = (v) => h(v);
251
+ return c.add(m), () => c.delete(m);
162
252
  },
163
253
  setAppState(h) {
164
- w(
254
+ y(
165
255
  h.phase,
166
256
  h.connectingStep ?? null,
167
257
  h.message ?? null
@@ -169,42 +259,42 @@ function H(t) {
169
259
  },
170
260
  requestLeave() {
171
261
  b({
172
- source: y,
262
+ source: w,
173
263
  type: "embed:request-leave",
174
264
  version: g
175
265
  });
176
266
  },
177
- reportError(h, p) {
267
+ reportError(h, m) {
178
268
  b({
179
- source: y,
269
+ source: w,
180
270
  type: "embed:error",
181
271
  version: g,
182
272
  message: h,
183
- fatal: p?.fatal === !0,
184
- detail: p?.detail
273
+ fatal: m?.fatal === !0,
274
+ detail: m?.detail
185
275
  });
186
276
  }
187
277
  };
188
278
  }
189
279
  };
190
280
  }
191
- function A() {
281
+ function H() {
192
282
  try {
193
283
  const t = window.location.pathname + window.location.search;
194
284
  history.replaceState(history.state, document.title, t);
195
285
  } catch {
196
286
  }
197
287
  }
198
- function D(t, r) {
288
+ function j(t, r) {
199
289
  try {
200
290
  sessionStorage.setItem("hf_token", t), sessionStorage.setItem("hf_username", r), sessionStorage.setItem(
201
291
  "hf_token_expires",
202
- new Date(Date.now() + R).toISOString()
292
+ new Date(Date.now() + U).toISOString()
203
293
  );
204
294
  } catch {
205
295
  }
206
296
  }
207
- function U(t) {
297
+ function F(t) {
208
298
  return new Promise((r) => {
209
299
  if (typeof window > "u") {
210
300
  r(!1);
@@ -215,12 +305,12 @@ function U(t) {
215
305
  return;
216
306
  }
217
307
  let n = !1;
218
- const l = () => {
219
- n || (n = !0, window.removeEventListener("reachymini:ready", l), window.clearTimeout(e), r(!!window.ReachyMini));
308
+ const c = () => {
309
+ n || (n = !0, window.removeEventListener("reachymini:ready", c), window.clearTimeout(e), r(!!window.ReachyMini));
220
310
  }, e = window.setTimeout(() => {
221
- n || (n = !0, window.removeEventListener("reachymini:ready", l), r(!1));
311
+ n || (n = !0, window.removeEventListener("reachymini:ready", c), r(!1));
222
312
  }, t);
223
- window.addEventListener("reachymini:ready", l);
313
+ window.addEventListener("reachymini:ready", c);
224
314
  });
225
315
  }
226
316
  function b(t) {
@@ -231,61 +321,61 @@ function b(t) {
231
321
  console.warn("[reachy-mini-sdk/host/embed] postMessage to host failed", r);
232
322
  }
233
323
  }
234
- function w(t, r, n = null, l = null) {
324
+ function y(t, r, n = null, c = null) {
235
325
  b({
236
- source: y,
326
+ source: w,
237
327
  type: "embed:app-state",
238
328
  version: g,
239
329
  phase: t,
240
330
  connectingStep: r,
241
331
  message: n,
242
- rttMs: l
332
+ rttMs: c
243
333
  });
244
334
  }
245
- const j = 1500, F = 6;
246
- async function V(t) {
335
+ const V = 1500, q = 6;
336
+ async function J(t) {
247
337
  try {
248
338
  const r = await t.getStats();
249
339
  let n = null;
250
- return r.forEach((l) => {
251
- const e = l;
340
+ return r.forEach((c) => {
341
+ const e = c;
252
342
  e.type !== "candidate-pair" || !(e.selected === !0 || e.nominated === !0 && e.state === "succeeded") || typeof e.currentRoundTripTime == "number" && (n = e.currentRoundTripTime * 1e3);
253
343
  }), n;
254
344
  } catch {
255
345
  return null;
256
346
  }
257
347
  }
258
- function J(t) {
348
+ function K(t) {
259
349
  if (typeof window > "u") return;
260
350
  const r = t._pc;
261
351
  if (!r) return;
262
352
  const n = [];
263
- let l = !1;
353
+ let c = !1;
264
354
  const e = async () => {
265
- if (l) return;
266
- const u = await V(r);
267
- u !== null && (n.push(u), n.length > F && n.shift(), w("live", null, null, Math.min(...n)));
355
+ if (c) return;
356
+ const u = await J(r);
357
+ u !== null && (n.push(u), n.length > q && n.shift(), y("live", null, null, Math.min(...n)));
268
358
  }, a = window.setInterval(() => {
269
359
  e();
270
- }, j);
360
+ }, V);
271
361
  window.setTimeout(() => {
272
362
  e();
273
363
  }, 600);
274
- const c = () => {
275
- l || (l = !0, window.clearInterval(a));
364
+ const d = () => {
365
+ c || (c = !0, window.clearInterval(a));
276
366
  };
277
367
  try {
278
- t.addEventListener("sessionStopped", c);
368
+ t.addEventListener("sessionStopped", d);
279
369
  } catch {
280
370
  }
281
- window.addEventListener("pagehide", c, { once: !0 });
371
+ window.addEventListener("pagehide", d, { once: !0 });
282
372
  }
283
- function f(t, r = {}) {
373
+ function p(t, r = {}) {
284
374
  if (!(typeof window > "u")) {
285
375
  try {
286
376
  window.parent.postMessage(
287
377
  {
288
- source: y,
378
+ source: w,
289
379
  type: "embed:debug",
290
380
  version: g,
291
381
  tag: t,
@@ -307,33 +397,33 @@ function f(t, r = {}) {
307
397
  }
308
398
  }
309
399
  }
310
- function K(t) {
400
+ function W(t) {
311
401
  let r = null;
312
- const n = t, l = () => {
313
- const c = n._pc;
314
- if (!c) return null;
315
- const u = c.getReceivers().map((d) => d.track).filter(
316
- (d) => d !== null && d.kind !== "" && d.readyState === "live"
402
+ const n = t, c = () => {
403
+ const d = n._pc;
404
+ if (!d) return null;
405
+ const u = d.getReceivers().map((l) => l.track).filter(
406
+ (l) => l !== null && l.kind !== "" && l.readyState === "live"
317
407
  );
318
408
  return u.length === 0 ? null : new MediaStream(u);
319
- }, e = () => r || (r = l(), r && f("media:cache:init", {
409
+ }, e = () => r || (r = c(), r && p("media:cache:init", {
320
410
  videoTracks: r.getVideoTracks().length,
321
411
  audioTracks: r.getAudioTracks().length
322
412
  }), r), a = () => {
323
- r && (r = null, f("media:cache:clear"));
413
+ r && (r = null, p("media:cache:clear"));
324
414
  };
325
415
  return t.addEventListener("sessionStopped", a), {
326
- attachVideo(c) {
327
- const u = t.attachVideo(c), d = e();
328
- if (d && c.srcObject !== d)
416
+ attachVideo(d) {
417
+ const u = t.attachVideo(d), l = e();
418
+ if (l && d.srcObject !== l)
329
419
  try {
330
- c.srcObject = d, c.play().catch(() => {
331
- }), f("media:attach:replay", {
332
- videoTracks: d.getVideoTracks().length,
333
- audioTracks: d.getAudioTracks().length
420
+ d.srcObject = l, d.play().catch(() => {
421
+ }), p("media:attach:replay", {
422
+ videoTracks: l.getVideoTracks().length,
423
+ audioTracks: l.getAudioTracks().length
334
424
  });
335
425
  } catch (s) {
336
- f("media:attach:replay:error", {
426
+ p("media:attach:replay:error", {
337
427
  message: s?.message ?? String(s)
338
428
  });
339
429
  }
@@ -363,24 +453,24 @@ function $(t) {
363
453
  for (const e of r)
364
454
  try {
365
455
  t.addEventListener(e, (a) => {
366
- let c = {};
456
+ let d = {};
367
457
  const u = a;
368
458
  if (u && typeof u == "object" && "detail" in u)
369
459
  try {
370
- c = JSON.parse(JSON.stringify(u.detail ?? null));
460
+ d = JSON.parse(JSON.stringify(u.detail ?? null));
371
461
  } catch {
372
- c = { _unserializable: !0 };
462
+ d = { _unserializable: !0 };
373
463
  }
374
- f(`sdk:${e}`, c);
464
+ p(`sdk:${e}`, d);
375
465
  });
376
466
  } catch {
377
467
  }
378
468
  try {
379
469
  const e = t, a = e._handleSignalingMessage;
380
470
  if (typeof a == "function") {
381
- const c = e._sendToServer;
382
- typeof c == "function" && (e._sendToServer = async function(d) {
383
- const s = d, o = { type: s?.type ?? "?" };
471
+ const d = e._sendToServer;
472
+ typeof d == "function" && (e._sendToServer = async function(l) {
473
+ const s = l, o = { type: s?.type ?? "?" };
384
474
  if (s && "peerId" in s && (o.peerId = String(s.peerId)), s && "sessionId" in s && (o.sessionId = String(s.sessionId)), s && "sdp" in s) {
385
475
  const i = s.sdp;
386
476
  o.sdpType = i?.type ?? "?", o.sdpLen = i?.sdp?.length ?? 0;
@@ -389,22 +479,22 @@ function $(t) {
389
479
  const i = s.ice;
390
480
  o.iceCand = (i?.candidate ?? "").slice(0, 60) || "<end-of-candidates>";
391
481
  }
392
- f("sdk:send", o);
482
+ p("sdk:send", o);
393
483
  try {
394
- const i = await c.call(this, d), m = i;
395
- return f("sdk:send:res", {
484
+ const i = await d.call(this, l), f = i;
485
+ return p("sdk:send:res", {
396
486
  inFor: o.type,
397
- resType: m?.type ?? null,
398
- keys: m ? Object.keys(m) : []
487
+ resType: f?.type ?? null,
488
+ keys: f ? Object.keys(f) : []
399
489
  }), i;
400
490
  } catch (i) {
401
- throw f("sdk:send:err", {
491
+ throw p("sdk:send:err", {
402
492
  inFor: o.type,
403
493
  msg: i?.message ?? String(i)
404
494
  }), i;
405
495
  }
406
- }), e._handleSignalingMessage = function(d) {
407
- const s = d, o = { type: s?.type ?? "?" };
496
+ }), e._handleSignalingMessage = function(l) {
497
+ const s = l, o = { type: s?.type ?? "?" };
408
498
  if ("sessionId" in s && (o.sessionId = String(s.sessionId)), "peerId" in s && (o.peerId = String(s.peerId)), "sdp" in s) {
409
499
  const i = s.sdp;
410
500
  o.sdpType = i?.type ?? "?", o.sdpLen = i?.sdp?.length ?? 0;
@@ -413,14 +503,14 @@ function $(t) {
413
503
  const i = s.ice;
414
504
  o.iceCand = (i?.candidate ?? "").slice(0, 60) || "<end-of-candidates>";
415
505
  }
416
- return "reason" in s && (o.reason = String(s.reason)), f("sdk:sse", o), a.call(this, d);
506
+ return "reason" in s && (o.reason = String(s.reason)), p("sdk:sse", o), a.call(this, l);
417
507
  };
418
508
  }
419
509
  } catch {
420
510
  }
421
- const n = Date.now(), l = window.setInterval(() => {
422
- const e = t, a = e._pc, c = e._dc;
423
- f("sdk:probe", {
511
+ const n = Date.now(), c = window.setInterval(() => {
512
+ const e = t, a = e._pc, d = e._dc;
513
+ p("sdk:probe", {
424
514
  elapsedMs: Date.now() - n,
425
515
  myPeerId: e._peerId ?? null,
426
516
  state: e._state ?? null,
@@ -429,11 +519,12 @@ function $(t) {
429
519
  iceState: a?.iceConnectionState ?? null,
430
520
  iceGather: a?.iceGatheringState ?? null,
431
521
  signalingState: a?.signalingState ?? null,
432
- dcState: c?.readyState ?? null,
522
+ dcState: d?.readyState ?? null,
433
523
  sessionId: e._sessionId ?? null
434
- }), Date.now() - n > 3e4 && window.clearInterval(l);
524
+ }), Date.now() - n > 3e4 && window.clearInterval(c);
435
525
  }, 1500);
436
526
  }
437
527
  export {
438
- q as c
528
+ z as a,
529
+ B as c
439
530
  };