@machinemetrics/mm-react-embeddable 2.0.0-5 → 2.0.0-7

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 x, { createContext as le, useContext as G, useState as C, useRef as b, useCallback as m, useEffect as T, useMemo as pe } from "react";
2
- import w from "prop-types";
3
- import se from "styled-components";
4
- const re = le(null), J = le({
1
+ import $, { createContext as fe, useContext as V, useState as O, useRef as m, useCallback as h, useEffect as b, useMemo as ge } from "react";
2
+ import E from "prop-types";
3
+ import ie from "styled-components";
4
+ const ne = fe(null), J = fe({
5
5
  pending: [],
6
6
  authorize: () => {
7
7
  },
@@ -14,37 +14,37 @@ const re = le(null), J = le({
14
14
  originEpochs: {},
15
15
  credentials: {},
16
16
  unsupported: {}
17
- }), he = "access_token", oe = (t) => t ? t.kind ? t : t.accessToken ? {
18
- kind: he,
17
+ }), be = "access_token", ce = (t) => t ? t.kind ? t : t.accessToken ? {
18
+ kind: be,
19
19
  accessToken: t.accessToken,
20
20
  expiresAt: t.expiresAt || null
21
- } : null : null, me = 60, ge = 120, be = /* @__PURE__ */ new Set([
21
+ } : null : null, Ee = 60, Re = 120, we = /* @__PURE__ */ new Set([
22
22
  "host_not_allowed",
23
23
  "unbound_host_bound_guest",
24
24
  "invalid_token",
25
25
  "provider_unavailable"
26
- ]), te = typeof window != "undefined" && window.location.protocol === "https:", Ee = 8e3, N = (t) => {
26
+ ]), te = typeof window != "undefined" && window.location.protocol === "https:", Te = 8e3, N = (t) => {
27
27
  try {
28
28
  return new URL(t).origin;
29
29
  } catch {
30
30
  return null;
31
31
  }
32
- }, ie = (t, s) => {
32
+ }, ue = (t, s) => {
33
33
  const c = s == null ? void 0 : s.origin;
34
34
  return !c || N(t == null ? void 0 : t.authUrl) !== c ? null : c;
35
- }, ce = (t) => {
35
+ }, le = (t) => {
36
36
  if (!t || /^\d{1,3}(?:\.\d{1,3}){3}$/.test(t) || t.includes(":"))
37
37
  return t;
38
38
  const s = t.split(".");
39
39
  return s.length <= 2 ? t : s.slice(-2).join(".");
40
- }, ue = (t, s) => {
40
+ }, ae = (t, s) => {
41
41
  try {
42
42
  const c = new URL(t), d = new URL(s);
43
- return c.protocol !== d.protocol ? !0 : ce(c.hostname) !== ce(d.hostname);
43
+ return c.protocol !== d.protocol ? !0 : le(c.hostname) !== le(d.hostname);
44
44
  } catch {
45
45
  return !0;
46
46
  }
47
- }, Re = (t, { onBlocked: s, onClosed: c }) => {
47
+ }, ke = (t, { onBlocked: s, onClosed: c }) => {
48
48
  const d = window.open(
49
49
  t.authUrl,
50
50
  "mm-embed-auth",
@@ -53,35 +53,39 @@ const re = le(null), J = le({
53
53
  if (!d)
54
54
  return s(), () => {
55
55
  };
56
- const h = setInterval(() => {
57
- d.closed && (clearInterval(h), c());
56
+ const f = setInterval(() => {
57
+ d.closed && (clearInterval(f), c());
58
58
  }, 1e3);
59
- return () => clearInterval(h);
60
- }, ae = ({ children: t, launcher: s, broker: c }) => {
61
- const d = G(re), [h, E] = C({}), [H, I] = C({}), [$, S] = C({}), [q, y] = C({}), Y = b($);
62
- Y.current = $;
63
- const R = b(h);
64
- R.current = h;
65
- const F = b(null), V = b({}), A = b({}), Z = b({}), P = b(c);
66
- P.current = c;
67
- const U = m((e) => {
68
- const r = A.current[e];
59
+ return () => clearInterval(f);
60
+ }, pe = ({ children: t, launcher: s, broker: c }) => {
61
+ const d = V(ne), [f, g] = O({}), [P, B] = O({}), [W, H] = O({}), [k, z] = O({}), y = m(W);
62
+ y.current = W;
63
+ const U = m(f);
64
+ U.current = f;
65
+ const x = m(null), Y = m({}), R = m({}), G = m({}), v = m(c);
66
+ v.current = c;
67
+ const M = h((e) => {
68
+ const r = R.current[e];
69
69
  if (!r)
70
70
  return;
71
- const n = (u) => !u[e] || u[e].clientId ? u : { ...u, [e]: { ...u[e], clientId: r } };
72
- E(n), y(n);
73
- }, []), i = m(
71
+ const n = (i) => !i[e] || i[e].clientId ? i : { ...i, [e]: { ...i[e], clientId: r } };
72
+ g(n), z(n);
73
+ }, []), L = h(
74
74
  (e, r) => {
75
- e && (y((n) => n[e] ? n : {
75
+ e && (z((n) => n[e] ? n : {
76
76
  ...n,
77
- [e]: { origin: e, reason: r, clientId: A.current[e] || null }
78
- }), U(e));
77
+ [e]: {
78
+ origin: e,
79
+ reason: r,
80
+ clientId: R.current[e] || null
81
+ }
82
+ }), M(e));
79
83
  },
80
- [U]
81
- ), l = m(
84
+ [M]
85
+ ), S = h(
82
86
  (e, { v2: r = !1, origin: n = null } = {}) => {
83
- n && (r && Y.current[n] || (E((u) => u[n] ? u : {
84
- ...u,
87
+ n && (r && y.current[n] || (g((i) => i[n] ? i : {
88
+ ...i,
85
89
  [n]: {
86
90
  origin: n,
87
91
  authUrl: e.authUrl,
@@ -89,62 +93,65 @@ const re = le(null), J = le({
89
93
  // v2 entries try the invisible silent resolver first — but only on a host that can
90
94
  // frame login (https). Otherwise (and for legacy signals) go straight to interactive,
91
95
  // so an http host doesn't wait out a silent frame that can never load login.
92
- status: r && P.current ? "broker" : r && e.silentUrl && te ? "silent" : "interactive",
96
+ status: r && v.current ? "broker" : r && e.silentUrl && te ? "silent" : "interactive",
93
97
  v2: r,
94
98
  // v1.9: the scope the guest asked for; forwarded verbatim to the broker.
95
99
  scope: e.scope || null,
96
- acceptsCredentials: Z.current[n] || null,
100
+ acceptsCredentials: G.current[n] || null,
97
101
  consent: null,
98
102
  error: null,
99
103
  // clientId from the request payload (v2) or the cached init (legacy);
100
104
  // the host resolves the display name from it.
101
- clientId: e.clientId || A.current[n] || null,
105
+ clientId: e.clientId || R.current[n] || null,
102
106
  popupBlocked: !1
103
107
  }
104
- }), U(n)));
108
+ }), M(n)));
105
109
  },
106
- [U]
107
- ), o = b({}), a = m((e) => {
108
- o.current[e] && (clearTimeout(o.current[e]), delete o.current[e]);
109
- }, []), f = m((e) => {
110
- a(e), E((r) => {
111
- if (!r[e])
112
- return r;
113
- const n = { ...r };
114
- return delete n[e], n;
115
- });
116
- }, [a]), g = m(
110
+ [M]
111
+ ), C = m({}), l = h((e) => {
112
+ C.current[e] && (clearTimeout(C.current[e]), delete C.current[e]);
113
+ }, []), u = h(
117
114
  (e) => {
118
- e && (f(e), I((r) => ({
115
+ l(e), g((r) => {
116
+ if (!r[e])
117
+ return r;
118
+ const n = { ...r };
119
+ return delete n[e], n;
120
+ });
121
+ },
122
+ [l]
123
+ ), o = h(
124
+ (e) => {
125
+ e && (u(e), B((r) => ({
119
126
  ...r,
120
127
  [e]: (r[e] || 0) + 1
121
128
  })));
122
129
  },
123
- [f]
124
- ), O = m((e) => {
125
- E((r) => !r[e] || r[e].status === "interactive" ? r : {
130
+ [u]
131
+ ), a = h((e) => {
132
+ g((r) => !r[e] || r[e].status === "interactive" ? r : {
126
133
  ...r,
127
134
  [e]: { ...r[e], status: "interactive" }
128
135
  });
129
- }, []), j = m(
136
+ }, []), T = h(
130
137
  (e) => {
131
- f(e.origin);
138
+ u(e.origin);
132
139
  },
133
- [f]
134
- ), k = m(
140
+ [u]
141
+ ), p = h(
135
142
  (e, r) => {
136
- const n = ie(e, r);
143
+ const n = ue(e, r);
137
144
  if (n) {
138
- if (ue(n, window.location.origin)) {
139
- i(n, "cross-site-legacy");
145
+ if (ae(n, window.location.origin)) {
146
+ L(n, "cross-site-legacy");
140
147
  return;
141
148
  }
142
- l(e, { origin: n });
149
+ S(e, { origin: n });
143
150
  }
144
151
  },
145
- [l, i]
152
+ [S, L]
146
153
  );
147
- T(() => {
154
+ b(() => {
148
155
  if (!d)
149
156
  return;
150
157
  const e = [
@@ -155,179 +162,185 @@ const re = le(null), J = le({
155
162
  // clientId is only a lookup key: the host resolves the display name from
156
163
  // it, and a broker binds it to the attested origin server-side.
157
164
  d.addListener("*", "init", (r, n) => {
158
- n != null && n.origin && (r != null && r.clientId) && (A.current[n.origin] = r.clientId, U(n.origin)), n != null && n.origin && Array.isArray(r == null ? void 0 : r.acceptsCredentials) ? Z.current[n.origin] = r.acceptsCredentials : n != null && n.origin && ue(n.origin, window.location.origin) && i(n.origin, "cross-site-legacy");
165
+ n != null && n.origin && (r != null && r.clientId) && (R.current[n.origin] = r.clientId, M(n.origin)), n != null && n.origin && Array.isArray(r == null ? void 0 : r.acceptsCredentials) ? G.current[n.origin] = r.acceptsCredentials : n != null && n.origin && ae(n.origin, window.location.origin) && L(n.origin, "cross-site-legacy");
159
166
  }),
160
- d.addListener("*", "authorize", k),
161
- d.addListener("*", "login", k),
167
+ d.addListener("*", "authorize", p),
168
+ d.addListener("*", "login", p),
162
169
  d.addListener(
163
170
  "*",
164
171
  "credential-request",
165
- (r, n) => l(r, { v2: !0, origin: ie(r, n) })
172
+ (r, n) => S(r, { v2: !0, origin: ue(r, n) })
166
173
  )
167
174
  ];
168
175
  return () => e.forEach((r) => r());
169
- }, [d, k, l, U, i]), T(() => {
176
+ }, [d, p, S, M, L]), b(() => {
170
177
  const e = (r) => {
171
178
  const n = r == null ? void 0 : r.data;
172
179
  if (!n || n.type !== "authorized" && n.type !== "authorize-failed")
173
180
  return;
174
- const u = N(n.authUrl);
175
- if (!(!u || r.origin !== u)) {
181
+ const i = N(n.authUrl);
182
+ if (!(!i || r.origin !== i)) {
176
183
  if (n.type === "authorized") {
177
184
  if (n.accessToken) {
178
- if (!R.current[u])
185
+ if (!U.current[i])
179
186
  return;
180
- S((p) => ({
181
- ...p,
182
- [u]: oe({ accessToken: n.accessToken })
183
- })), f(u);
187
+ H((_) => ({
188
+ ..._,
189
+ [i]: ce({ accessToken: n.accessToken })
190
+ })), u(i);
184
191
  } else
185
- g(u);
192
+ o(i);
186
193
  return;
187
194
  }
188
- O(u);
195
+ a(i);
189
196
  }
190
197
  };
191
198
  return window.addEventListener("message", e), () => window.removeEventListener("message", e);
192
- }, [f, g, O]);
193
- const B = m(
199
+ }, [u, o, a]);
200
+ const F = h(
194
201
  (e, r) => {
195
- const n = e == null ? void 0 : e.origin, u = oe(r);
196
- !n || !u || (S((p) => ({ ...p, [n]: u })), f(n));
202
+ const n = e == null ? void 0 : e.origin, i = ce(r);
203
+ !n || !i || (H((_) => ({ ..._, [n]: i })), u(n));
197
204
  },
198
- [f]
199
- ), z = m((e, r) => {
200
- E((n) => n[e] ? { ...n, [e]: { ...n[e], ...r } } : n);
201
- }, []), L = b({}), M = m(
205
+ [u]
206
+ ), w = h((e, r) => {
207
+ g((n) => n[e] ? { ...n, [e]: { ...n[e], ...r } } : n);
208
+ }, []), I = m({}), A = h(
202
209
  async (e, { approve: r } = {}) => {
203
- const n = P.current, { origin: u } = e;
204
- let p;
210
+ const n = v.current, { origin: i } = e;
211
+ let _;
205
212
  try {
206
- p = await n(
207
- { origin: u, clientId: e.clientId, scope: e.scope, authUrl: e.authUrl },
213
+ _ = await n(
214
+ {
215
+ origin: i,
216
+ clientId: e.clientId,
217
+ scope: e.scope,
218
+ authUrl: e.authUrl
219
+ },
208
220
  { approve: r }
209
221
  );
210
222
  } catch {
211
- r ? z(u, { error: { reason: "broker_unavailable" } }) : z(u, {
223
+ r ? w(i, { error: { reason: "broker_unavailable" } }) : w(i, {
212
224
  status: e.silentUrl && te ? "silent" : "interactive"
213
225
  });
214
226
  return;
215
227
  }
216
- if ((p == null ? void 0 : p.status) === 200 && p.credential) {
217
- B(e, p.credential);
228
+ const oe = _ == null ? void 0 : _.status, Z = (_ == null ? void 0 : _.body) || {};
229
+ if (oe === 200 && Z.credential) {
230
+ F(e, Z.credential);
218
231
  return;
219
232
  }
220
- if ((p == null ? void 0 : p.status) === 409 && p.consentRequired) {
221
- z(u, {
233
+ if (oe === 409 && Z.consentRequired) {
234
+ w(i, {
222
235
  status: "interactive",
223
236
  consent: {
224
- client: p.client || null,
225
- stepUp: !!p.stepUp,
226
- scope: p.scope || e.scope
237
+ client: Z.client || null,
238
+ stepUp: !!Z.stepUp,
239
+ scope: Z.scope || e.scope
227
240
  },
228
241
  error: null
229
242
  });
230
243
  return;
231
244
  }
232
- const Q = (p == null ? void 0 : p.error) || "broker_error";
233
- if (!r && be.has(Q)) {
234
- z(u, {
245
+ const ee = Z.error || "broker_error";
246
+ if (!r && we.has(ee)) {
247
+ w(i, {
235
248
  status: e.silentUrl && te ? "silent" : "interactive"
236
249
  });
237
250
  return;
238
251
  }
239
- if (r && Q === "consent_denied") {
240
- z(u, { error: null });
252
+ if (r && ee === "consent_denied") {
253
+ w(i, { error: null });
241
254
  return;
242
255
  }
243
- if (Q === "rate_limited" && !e.rateLimitRetried) {
244
- const ee = Math.min(
245
- Number(p == null ? void 0 : p.retryAfter) > 0 ? Number(p.retryAfter) : me,
246
- ge
247
- ), fe = { ...e, rateLimitRetried: !0 };
248
- a(u), o.current[u] = setTimeout(() => {
249
- delete o.current[u], L.current[u] = !0, v.current(fe, { approve: r }).finally(() => {
250
- delete L.current[u];
256
+ if (ee === "rate_limited" && !e.rateLimitRetried) {
257
+ const re = Math.min(
258
+ Number(Z.retryAfter) > 0 ? Number(Z.retryAfter) : Ee,
259
+ Re
260
+ ), me = { ...e, rateLimitRetried: !0 };
261
+ l(i), C.current[i] = setTimeout(() => {
262
+ delete C.current[i], I.current[i] = !0, D.current(me, { approve: r }).finally(() => {
263
+ delete I.current[i];
251
264
  });
252
- }, ee * 1e3), z(u, { rateLimitRetried: !0 });
265
+ }, re * 1e3), w(i, { rateLimitRetried: !0 });
253
266
  return;
254
267
  }
255
- f(u), y((ee) => ({
256
- ...ee,
257
- [u]: {
258
- origin: u,
259
- reason: `broker:${Q}`,
260
- detail: p || null,
261
- clientId: e.clientId || A.current[u] || null
268
+ u(i), z((re) => ({
269
+ ...re,
270
+ [i]: {
271
+ origin: i,
272
+ reason: `broker:${ee}`,
273
+ detail: _ || null,
274
+ clientId: e.clientId || R.current[i] || null
262
275
  }
263
276
  }));
264
277
  },
265
- [B, z, f, a]
266
- ), v = b(M);
267
- v.current = M, T(() => {
268
- Object.values(h).forEach((e) => {
269
- e.status !== "broker" || L.current[e.origin] || o.current[e.origin] || (L.current[e.origin] = !0, M(e).finally(() => {
270
- delete L.current[e.origin];
278
+ [F, w, u, l]
279
+ ), D = m(A);
280
+ D.current = A, b(() => {
281
+ Object.values(f).forEach((e) => {
282
+ e.status !== "broker" || I.current[e.origin] || C.current[e.origin] || (I.current[e.origin] = !0, A(e).finally(() => {
283
+ delete I.current[e.origin];
271
284
  }));
272
285
  });
273
- }, [h, M]);
274
- const K = m(
275
- (e) => !P.current || !(e != null && e.consent) ? Promise.resolve() : M(e, { approve: !0 }),
276
- [M]
286
+ }, [f, A]);
287
+ const Q = h(
288
+ (e) => !v.current || !(e != null && e.consent) ? Promise.resolve() : A(e, { approve: !0 }),
289
+ [A]
277
290
  );
278
- T(() => {
279
- const e = V.current;
280
- Object.values(h).forEach((r) => {
291
+ b(() => {
292
+ const e = Y.current;
293
+ Object.values(f).forEach((r) => {
281
294
  r.status === "silent" && !e[r.origin] && (e[r.origin] = setTimeout(() => {
282
- delete e[r.origin], O(r.origin);
283
- }, Ee));
295
+ delete e[r.origin], a(r.origin);
296
+ }, Te));
284
297
  }), Object.keys(e).forEach((r) => {
285
- (!h[r] || h[r].status !== "silent") && (clearTimeout(e[r]), delete e[r]);
298
+ (!f[r] || f[r].status !== "silent") && (clearTimeout(e[r]), delete e[r]);
286
299
  });
287
- }, [h, O]), T(() => () => {
288
- F.current && F.current(), Object.values(V.current).forEach(clearTimeout), Object.values(o.current).forEach(clearTimeout);
300
+ }, [f, a]), b(() => () => {
301
+ x.current && x.current(), Object.values(Y.current).forEach(clearTimeout), Object.values(C.current).forEach(clearTimeout);
289
302
  }, []);
290
- const W = m(
303
+ const K = h(
291
304
  (e) => {
292
- F.current && (F.current(), F.current = null);
293
- const r = s || Re;
294
- F.current = r(e, {
305
+ x.current && (x.current(), x.current = null);
306
+ const r = s || ke;
307
+ x.current = r(e, {
295
308
  onBlocked: () => {
296
- E((n) => n[e.origin] ? {
309
+ g((n) => n[e.origin] ? {
297
310
  ...n,
298
311
  [e.origin]: { ...n[e.origin], popupBlocked: !0 }
299
312
  } : n);
300
313
  },
301
314
  onClosed: () => {
302
315
  if (e.v2) {
303
- te && E((n) => !n[e.origin] || !n[e.origin].silentUrl ? n : {
316
+ te && g((n) => !n[e.origin] || !n[e.origin].silentUrl ? n : {
304
317
  ...n,
305
318
  [e.origin]: { ...n[e.origin], status: "silent" }
306
319
  });
307
320
  return;
308
321
  }
309
- g(e.origin);
322
+ o(e.origin);
310
323
  }
311
324
  });
312
325
  },
313
- [s, g]
314
- ), _ = {
326
+ [s, o]
327
+ ), j = {
315
328
  // Hosts render UI for interactive entries only; silent resolution is
316
329
  // invisible by design.
317
- pending: Object.values(h).filter(
330
+ pending: Object.values(f).filter(
318
331
  (e) => e.status === "interactive"
319
332
  ),
320
- authorize: W,
321
- approve: K,
322
- complete: B,
323
- dismiss: j,
324
- originEpochs: H,
325
- credentials: $,
326
- unsupported: q
327
- }, D = Object.values(h).filter(
333
+ authorize: K,
334
+ approve: Q,
335
+ complete: F,
336
+ dismiss: T,
337
+ originEpochs: P,
338
+ credentials: W,
339
+ unsupported: k
340
+ }, q = Object.values(f).filter(
328
341
  (e) => e.status === "silent" && e.silentUrl
329
342
  );
330
- return /* @__PURE__ */ x.createElement(J.Provider, { value: _ }, t, D.map((e) => /* @__PURE__ */ x.createElement(
343
+ return /* @__PURE__ */ $.createElement(J.Provider, { value: j }, t, q.map((e) => /* @__PURE__ */ $.createElement(
331
344
  "iframe",
332
345
  {
333
346
  key: `mm-silent-auth-${e.origin}`,
@@ -339,179 +352,204 @@ const re = le(null), J = le({
339
352
  }
340
353
  )));
341
354
  };
342
- ae.propTypes = {
343
- children: w.node,
344
- launcher: w.func,
355
+ pe.propTypes = {
356
+ children: E.node,
357
+ launcher: E.func,
345
358
  /**
346
359
  * `async (request, { approve }) => result` where request =
347
360
  * `{ origin, clientId, scope, authUrl }` (origin is the zone-attested frame origin) and result
348
- * mirrors the authorization server's broker response: `{ status: 200, credential, scope,
349
- * client }`, `{ status: 409, consentRequired: true, client: { name, hostname }, stepUp }`, or
350
- * `{ status, error }`. Provided only by hosts that are themselves first-party clients.
361
+ * is `{ status, body }`: the HTTP status and the authorization server's parsed JSON, kept
362
+ * apart so a body field can never shadow the status. Bodies: `{ credential, scope, client }`
363
+ * on 200, `{ consentRequired: true, client: { name, hostname }, stepUp }` on 409, `{ error,
364
+ * … }` otherwise. Provided only by hosts that are themselves first-party clients.
351
365
  */
352
- broker: w.func
366
+ broker: E.func
353
367
  };
354
- const ye = () => G(J), Te = (t) => {
355
- const { originEpochs: s } = G(J), c = N(t);
368
+ const qe = () => V(J), Ie = (t) => {
369
+ const { originEpochs: s } = V(J), c = N(t);
356
370
  return c && s[c] || 0;
357
- }, ke = (t) => {
358
- const { credentials: s } = G(J), c = N(t);
371
+ }, _e = (t) => {
372
+ const { credentials: s } = V(J), c = N(t);
359
373
  return c && s[c] || null;
360
- }, we = (t) => {
361
- const { unsupported: s } = G(J), c = N(t);
374
+ }, Ue = (t) => {
375
+ const { unsupported: s } = V(J), c = N(t);
362
376
  return c && s[c] || null;
363
- }, Ie = ({ children: t, authorizationLauncher: s, credentialBroker: c }) => {
364
- const [d, h] = C({}), [E, H] = C({}), I = b({}), $ = b({}), S = b({}), q = m((i, l) => {
377
+ }, de = "MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.", Me = ({
378
+ children: t,
379
+ authorizationLauncher: s,
380
+ credentialBroker: c,
381
+ ready: d
382
+ }) => {
383
+ const f = d === !0, g = m(!1);
384
+ b(() => {
385
+ d !== void 0 || g.current || (g.current = !0, console.error(de), typeof window != "undefined" && typeof window.alert == "function" && window.alert(de));
386
+ }, [d]);
387
+ const [P, B] = O({}), [W, H] = O({}), k = m({}), z = m({}), y = m({}), U = h((l, u) => {
365
388
  var o;
366
- return !!((o = S.current[i]) != null && o.has(l));
367
- }, []), y = m((i, l, o) => {
368
- h((a) => ({ ...a, [i]: { origin: l, frameRef: o } }));
369
- }, []), Y = m((i) => {
370
- h((l) => {
371
- const o = { ...l };
372
- return delete o[i], o;
373
- }), H((l) => {
374
- const o = { ...l };
375
- return delete o[i], o;
376
- }), delete S.current[i];
389
+ return !!((o = y.current[l]) != null && o.has(u));
390
+ }, []), x = h((l, u, o) => {
391
+ B((a) => ({ ...a, [l]: { origin: u, frameRef: o } }));
392
+ }, []), Y = h((l) => {
393
+ B((u) => {
394
+ const o = { ...u };
395
+ return delete o[l], o;
396
+ }), H((u) => {
397
+ const o = { ...u };
398
+ return delete o[l], o;
399
+ }), delete y.current[l];
377
400
  }, []);
378
- T(() => {
379
- const i = (o = [], a, f) => {
380
- o.forEach((g) => g(a, f));
381
- }, l = (o) => {
382
- var B, z, L, M, v, K, W;
383
- if (!((B = o == null ? void 0 : o.data) != null && B.instanceId))
401
+ b(() => {
402
+ const l = (o = [], a, T) => {
403
+ o.forEach((p) => p(a, T));
404
+ }, u = (o) => {
405
+ var A, D, Q, K, j, q, e;
406
+ if (!((A = o == null ? void 0 : o.data) != null && A.instanceId))
384
407
  return;
385
- const { instanceId: a, type: f } = o.data, g = d[a], O = (L = (z = g == null ? void 0 : g.frameRef) == null ? void 0 : z.current) == null ? void 0 : L.contentWindow;
386
- if (!O || o.source !== O || o.origin !== g.origin)
408
+ const { instanceId: a, type: T } = o.data, p = P[a], F = (Q = (D = p == null ? void 0 : p.frameRef) == null ? void 0 : D.current) == null ? void 0 : Q.contentWindow;
409
+ if (!F || o.source !== F || o.origin !== p.origin)
387
410
  return;
388
- const j = { origin: o.origin }, k = I.current;
389
- if (f === "response") {
390
- const { callbackId: _, response: D, error: e } = o.data, r = $.current[_];
391
- r && (r(D, e), delete $.current[_]);
411
+ const w = { origin: o.origin }, I = k.current;
412
+ if (T === "response") {
413
+ const { callbackId: r, response: n, error: i } = o.data, _ = z.current[r];
414
+ _ && (_(n, i), delete z.current[r]);
392
415
  return;
393
416
  }
394
- if (f === "mark-registered") {
395
- const { requestType: _ } = o.data;
396
- S.current[a] || (S.current[a] = /* @__PURE__ */ new Set()), S.current[a].add(_);
417
+ if (T === "mark-registered") {
418
+ const { requestType: r } = o.data;
419
+ y.current[a] || (y.current[a] = /* @__PURE__ */ new Set()), y.current[a].add(r);
397
420
  return;
398
421
  }
399
- f === "ready" && H((_) => ({ ..._, [a]: !0 })), i((M = k[a]) == null ? void 0 : M[f], o.data, j), i((v = k["*"]) == null ? void 0 : v[f], o.data, j), i((K = k[a]) == null ? void 0 : K["*"], o.data, j), i((W = k["*"]) == null ? void 0 : W["*"], o.data, j);
422
+ T === "ready" && H((r) => ({ ...r, [a]: !0 })), l((K = I[a]) == null ? void 0 : K[T], o.data, w), l((j = I["*"]) == null ? void 0 : j[T], o.data, w), l((q = I[a]) == null ? void 0 : q["*"], o.data, w), l((e = I["*"]) == null ? void 0 : e["*"], o.data, w);
400
423
  };
401
- return window.addEventListener("message", l), () => {
402
- window.removeEventListener("message", l);
424
+ return window.addEventListener("message", u), () => {
425
+ window.removeEventListener("message", u);
403
426
  };
404
- }, [d]);
405
- const R = m(
406
- (i, l, o) => {
407
- var f, g;
408
- const a = d[i];
427
+ }, [P]);
428
+ const R = h(
429
+ (l, u, o) => {
430
+ var T, p;
431
+ const a = P[l];
409
432
  if (!a) {
410
- console.warn(`Origin not set for id: ${i}. Unable to send message.`);
433
+ console.warn(`Origin not set for id: ${l}. Unable to send message.`);
411
434
  return;
412
435
  }
413
- (g = (f = a == null ? void 0 : a.frameRef) == null ? void 0 : f.current) != null && g.contentWindow && a.frameRef.current.contentWindow.postMessage(
414
- { type: l, ...o, instanceId: i },
436
+ (p = (T = a == null ? void 0 : a.frameRef) == null ? void 0 : T.current) != null && p.contentWindow && a.frameRef.current.contentWindow.postMessage(
437
+ { type: u, ...o, instanceId: l },
415
438
  a.origin
416
439
  );
417
440
  },
418
- [d]
419
- ), F = b(0), V = m(() => `${Date.now()}-${++F.current}`, []), A = m(
420
- async (i, l, o = {}) => new Promise((a, f) => {
421
- const g = V(), O = (j, k) => {
422
- delete $.current[g], k ? f(k) : a(j);
441
+ [P]
442
+ ), G = m(0), v = h(() => `${Date.now()}-${++G.current}`, []), M = h(
443
+ async (l, u, o = {}) => new Promise((a, T) => {
444
+ const p = v(), F = (w, I) => {
445
+ delete z.current[p], I ? T(I) : a(w);
423
446
  };
424
- $.current[g] = O, R(i, "request", { callbackId: g, requestType: l, data: o });
447
+ z.current[p] = F, R(l, "request", { callbackId: p, requestType: u, data: o });
425
448
  }),
426
449
  [R]
427
- ), Z = m(
428
- (i, l) => {
429
- R(i, "load-options", l);
450
+ ), L = h(
451
+ (l, u) => {
452
+ R(l, "load-options", u);
430
453
  },
431
454
  [R]
432
- ), P = m(
433
- (i, l) => {
434
- R(i, "validate", l);
455
+ ), S = h(
456
+ (l, u) => {
457
+ R(l, "validate", u);
435
458
  },
436
459
  [R]
437
- ), U = pe(
460
+ ), C = ge(
438
461
  () => ({
439
- registerFrame: y,
462
+ registerFrame: x,
440
463
  unregisterFrame: Y,
441
464
  sendMessage: R,
442
- sendRequest: A,
443
- loadOptions: Z,
444
- validate: P,
445
- hasHandler: q,
446
- readyFrames: E,
447
- addListener: (i, l, o) => (I.current[i] || (I.current[i] = {}), I.current[i][l] || (I.current[i][l] = []), I.current[i][l].push(o), () => {
448
- I.current[i][l] = I.current[i][l].filter((a) => a !== o);
465
+ sendRequest: M,
466
+ loadOptions: L,
467
+ validate: S,
468
+ hasHandler: U,
469
+ readyFrames: W,
470
+ ready: f,
471
+ addListener: (l, u, o) => (k.current[l] || (k.current[l] = {}), k.current[l][u] || (k.current[l][u] = []), k.current[l][u].push(o), () => {
472
+ k.current[l][u] = k.current[l][u].filter((a) => a !== o);
449
473
  })
450
474
  }),
451
475
  [
452
- y,
476
+ x,
453
477
  Y,
454
478
  R,
455
- A,
456
- Z,
457
- P,
458
- q,
459
- E
479
+ M,
480
+ L,
481
+ S,
482
+ U,
483
+ W,
484
+ f
460
485
  ]
461
486
  );
462
- return /* @__PURE__ */ x.createElement(re.Provider, { value: U }, /* @__PURE__ */ x.createElement(ae, { launcher: s, broker: c }, t));
487
+ return /* @__PURE__ */ $.createElement(ne.Provider, { value: C }, /* @__PURE__ */ $.createElement(
488
+ pe,
489
+ {
490
+ launcher: s,
491
+ broker: c
492
+ },
493
+ t
494
+ ));
463
495
  }, X = (t, s, c) => {
464
- const d = G(re);
496
+ const d = V(ne);
465
497
  if (d === null)
466
498
  throw new Error(
467
499
  "useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
468
500
  );
469
- const h = b(c);
470
- T(() => {
471
- h.current = c;
472
- }, [c]), T(() => {
501
+ const f = m(c);
502
+ b(() => {
503
+ f.current = c;
504
+ }, [c]), b(() => {
473
505
  if (!(!t || !s)) {
474
506
  if (s.startsWith("key:")) {
475
- const E = s.substring(4);
476
- return d.readyFrames[t] && d.sendMessage(t, "subscribe-key", { keyChord: E }), d.addListener(
507
+ const g = s.substring(4);
508
+ return d.readyFrames[t] && d.sendMessage(t, "subscribe-key", { keyChord: g }), d.addListener(
477
509
  t,
478
- `key-pressed:${E}`,
479
- (...I) => h.current(...I)
510
+ `key-pressed:${g}`,
511
+ (...B) => f.current(...B)
480
512
  );
481
513
  }
482
514
  return d.addListener(
483
515
  t,
484
516
  s,
485
- (...E) => h.current(...E)
517
+ (...g) => f.current(...g)
486
518
  );
487
519
  }
488
520
  }, [d, t, s]);
489
521
  };
490
- Ie.propTypes = {
491
- children: w.node.isRequired,
522
+ Me.propTypes = {
523
+ children: E.node.isRequired,
524
+ /**
525
+ * Required. True once the host holds the bearer its `credentialBroker` sends; every
526
+ * MMEmbeddableZone below stays unmounted (no iframe src) until then, so the first broker call
527
+ * always carries a token. Missing prop = not ready, plus a one-time alert.
528
+ */
529
+ ready: E.bool.isRequired,
492
530
  /**
493
531
  * Optional interactive-step launcher for embedded authorizations,
494
532
  * `(entry, { onBlocked, onClosed }) => cleanup`. Defaults to a top-level
495
533
  * popup. Hosts that cannot open windows (Android WebView shells) drive
496
534
  * their own UI from useEmbeddedAuthorizations() instead.
497
535
  */
498
- authorizationLauncher: w.func,
536
+ authorizationLauncher: E.func,
499
537
  /**
500
538
  * Optional credential broker for hosts that are first-party MachineMetrics clients
501
539
  * (`async (request, { approve }) => result`, see EmbeddedAuthorizationManager). When present,
502
540
  * v2 credential requests are brokered server-side (no popup, no hidden frame) and consent is
503
541
  * rendered natively by the host from the broker's `client` identity.
504
542
  */
505
- credentialBroker: w.func
543
+ credentialBroker: E.func
506
544
  };
507
- const ne = se.default || se, _e = 1e4, Ue = "credentials", Le = (t, s) => `${t}/mm-embedded-interop?id=${encodeURIComponent(s)}&origin=${encodeURIComponent(
545
+ const se = ie.default || ie, Le = 1e4, Ce = "credentials", Ae = (t, s) => `${t}/mm-embedded-interop?id=${encodeURIComponent(s)}&origin=${encodeURIComponent(
508
546
  window.location.origin
509
- )}&caps=${Ue}`, Me = ne.iframe`
547
+ )}&caps=${Ce}`, Oe = se.iframe`
510
548
  width: 100%;
511
549
  height: 100%;
512
550
  border: none;
513
551
  visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
514
- `, Ce = ne.div`
552
+ `, ze = se.div`
515
553
  width: 100%;
516
554
  height: 100%;
517
555
  display: flex;
@@ -524,7 +562,7 @@ const ne = se.default || se, _e = 1e4, Ue = "credentials", Le = (t, s) => `${t}/
524
562
  text-align: center;
525
563
  color: #555;
526
564
  font-size: 14px;
527
- `, Ae = (t) => {
565
+ `, xe = (t) => {
528
566
  const s = t && t.origin || "This app", c = t && t.reason || "";
529
567
  switch (c) {
530
568
  case "broker:scope_above_ceiling":
@@ -552,154 +590,158 @@ const ne = se.default || se, _e = 1e4, Ue = "credentials", Le = (t, s) => `${t}/
552
590
  detail: "It needs an update to support embedding from a different domain."
553
591
  };
554
592
  }
555
- }, de = ({ entry: t }) => {
556
- const s = Ae(t);
557
- return /* @__PURE__ */ x.createElement(Ce, { "data-reason": t && t.reason || void 0 }, /* @__PURE__ */ x.createElement("div", { style: { fontWeight: 600 } }, s.title), /* @__PURE__ */ x.createElement("div", { style: { fontSize: 12, color: "#888" } }, s.detail));
593
+ }, he = ({ entry: t }) => {
594
+ const s = xe(t);
595
+ return /* @__PURE__ */ $.createElement(ze, { "data-reason": t && t.reason || void 0 }, /* @__PURE__ */ $.createElement("div", { style: { fontWeight: 600 } }, s.title), /* @__PURE__ */ $.createElement("div", { style: { fontSize: 12, color: "#888" } }, s.detail));
558
596
  };
559
- de.propTypes = {
560
- entry: w.shape({
561
- origin: w.string,
562
- name: w.string,
563
- reason: w.string,
564
- detail: w.object
597
+ he.propTypes = {
598
+ entry: E.shape({
599
+ origin: E.string,
600
+ name: E.string,
601
+ reason: E.string,
602
+ detail: E.object
565
603
  })
566
604
  };
567
- const Oe = ne.div`
605
+ const $e = se.div`
568
606
  height: 100%;
569
607
  display: flex;
570
608
  flex: 1;
571
609
  align-items: center;
572
610
  justify-content: center;
573
- `, ze = () => /* @__PURE__ */ x.createElement(Oe, null, /* @__PURE__ */ x.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), xe = ({
611
+ `, ye = () => /* @__PURE__ */ $.createElement($e, null, /* @__PURE__ */ $.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), Se = ({
574
612
  id: t,
575
613
  src: s,
576
614
  path: c,
577
615
  onInit: d = () => {
578
616
  },
579
- onTimeout: h,
580
- context: E = {},
581
- params: H = {},
582
- renderUnsupported: I,
583
- renderTimeout: $,
584
- ...S
617
+ onTimeout: f,
618
+ context: g = {},
619
+ params: P = {},
620
+ renderUnsupported: B,
621
+ renderTimeout: W,
622
+ ...H
585
623
  }) => {
586
- if ("isConfiguring" in S)
624
+ if ("isConfiguring" in H)
587
625
  return console.error(
588
626
  "isConfiguring must be passed via the params prop to MMEmbeddableZone"
589
627
  ), null;
590
- if ("colorMode" in E)
628
+ if ("colorMode" in g)
591
629
  return console.error(
592
630
  "colorMode must be passed via the params prop to MMEmbeddableZone"
593
631
  ), null;
594
- if ("isFullScreen" in E)
632
+ if ("isFullScreen" in g)
595
633
  return console.error(
596
634
  "isFullScreen must be passed via the params prop to MMEmbeddableZone"
597
635
  ), null;
598
- const q = b(), [y, Y] = C(), [R, F] = C(0), [V, A] = C(!1), [Z, P] = C(!1), U = Te(s), i = ke(s), l = we(s), [o, a] = C(0), [f, g] = C(null), { registerFrame: O, unregisterFrame: j, sendMessage: k } = G(
599
- re
600
- ), [B, z] = C(), L = b(), M = b(h), v = b(d), K = b(!1), W = m(() => {
601
- K.current = !1, clearTimeout(L.current), F(0), A(!1), P(!1), g(null);
636
+ const k = m(), [z, y] = O(), [U, x] = O(0), [Y, R] = O(!1), [G, v] = O(!1), M = Ie(s), L = _e(s), S = Ue(s), [C, l] = O(0), [u, o] = O(null), { registerFrame: a, unregisterFrame: T, sendMessage: p, ready: F } = V(
637
+ ne
638
+ ), [w, I] = O(), A = m(), D = m(f), Q = m(d), K = m(!1), j = h(() => {
639
+ K.current = !1, clearTimeout(A.current), x(0), R(!1), v(!1), o(null);
602
640
  }, []);
603
- T(() => {
604
- (U > 0 || o > 0) && W();
605
- }, [U, o, W]), T(() => {
606
- M.current = h;
607
- }, [h]), T(() => {
608
- v.current = d;
609
- }, [d]), T(() => {
610
- R && k(t, "set-context", {
641
+ b(() => {
642
+ (M > 0 || C > 0) && j();
643
+ }, [M, C, j]), b(() => {
644
+ D.current = f;
645
+ }, [f]), b(() => {
646
+ Q.current = d;
647
+ }, [d]), b(() => {
648
+ U && p(t, "set-context", {
611
649
  context: {
612
- ...E
650
+ ...g
613
651
  }
614
652
  });
615
- }, [E, t, R]), T(() => {
616
- R && k(t, "set-params", { params: H });
617
- }, [H, t, R]), T(() => {
618
- c !== void 0 && z((c == null ? void 0 : c.indexOf("/")) === 0 ? c : `/${c}`);
619
- }, [c]), T(() => {
620
- B === void 0 || !R || k(t, "navigate", { path: B });
621
- }, [B, R, t, k]), T(() => {
622
- if (!s || !t || !q)
653
+ }, [g, t, U]), b(() => {
654
+ U && p(t, "set-params", { params: P });
655
+ }, [P, t, U]), b(() => {
656
+ c !== void 0 && I((c == null ? void 0 : c.indexOf("/")) === 0 ? c : `/${c}`);
657
+ }, [c]), b(() => {
658
+ w === void 0 || !U || p(t, "navigate", { path: w });
659
+ }, [w, U, t, p]), b(() => {
660
+ if (!s || !t || !k)
623
661
  return;
624
- W();
625
- const e = new URL(s);
626
- return O(t, e.origin, q), v.current(), Y(Le(e.origin, t)), () => {
627
- j(t);
662
+ if (!F) {
663
+ j(), y(void 0);
664
+ return;
665
+ }
666
+ j();
667
+ const r = new URL(s);
668
+ return a(t, r.origin, k), Q.current(), y(Ae(r.origin, t)), () => {
669
+ T(t);
628
670
  };
629
- }, [s, t, O, q, W]), T(() => {
630
- if (!(!y || K.current))
631
- return L.current = setTimeout(() => {
632
- A(!0), M.current && M.current();
633
- }, _e), () => clearTimeout(L.current);
634
- }, [y, s, U, o]);
635
- const _ = m(() => {
636
- K.current = !0, clearTimeout(L.current), A(!1);
671
+ }, [s, t, F, a, k, j]), b(() => {
672
+ if (!(!z || K.current))
673
+ return A.current = setTimeout(() => {
674
+ R(!0), D.current && D.current();
675
+ }, Le), () => clearTimeout(A.current);
676
+ }, [z, s, M, C]);
677
+ const q = h(() => {
678
+ K.current = !0, clearTimeout(A.current), R(!1);
637
679
  }, []);
638
- X(t, "init", (e) => {
639
- _(), Array.isArray(e == null ? void 0 : e.acceptsCredentials) && g(e.acceptsCredentials);
640
- }), X(t, "authorize", _), X(t, "login", _), X(t, "credential-request", () => {
641
- _(), P(!0);
642
- }), T(() => {
643
- if (!Z || !i)
680
+ X(t, "init", (r) => {
681
+ q(), Array.isArray(r == null ? void 0 : r.acceptsCredentials) && o(r.acceptsCredentials);
682
+ }), X(t, "authorize", q), X(t, "login", q), X(t, "credential-request", () => {
683
+ q(), v(!0);
684
+ }), b(() => {
685
+ if (!G || !L)
644
686
  return;
645
- const e = f;
646
- if (Array.isArray(e) && !e.includes(i.kind))
687
+ const r = u;
688
+ if (Array.isArray(r) && !r.includes(L.kind))
647
689
  return;
648
- const r = i.kind === "access_token" ? { accessToken: i.accessToken } : {};
649
- k(t, "credential", { ...r, credential: i });
650
- }, [Z, i, f, t, k]), X(t, "ready", () => {
651
- _(), F((e) => e + 1);
690
+ const n = L.kind === "access_token" ? { accessToken: L.accessToken } : {};
691
+ p(t, "credential", { ...n, credential: L });
692
+ }, [G, L, u, t, p]), X(t, "ready", () => {
693
+ q(), x((r) => r + 1);
652
694
  });
653
- const D = b();
695
+ const e = m();
654
696
  return X(t, "heartbeat", () => {
655
- D.current && clearTimeout(D.current), D.current = setTimeout(() => {
656
- a((e) => e + 1);
697
+ e.current && clearTimeout(e.current), e.current = setTimeout(() => {
698
+ l((r) => r + 1);
657
699
  }, 6e3);
658
- }), T(() => () => {
659
- D.current && clearTimeout(D.current);
660
- }, []), l ? I ? I(l) : /* @__PURE__ */ x.createElement(de, { entry: l }) : V && !R ? $ ? $() : /* @__PURE__ */ x.createElement(ze, null) : /* @__PURE__ */ x.createElement(
661
- Me,
700
+ }), b(() => () => {
701
+ e.current && clearTimeout(e.current);
702
+ }, []), S ? B ? B(S) : /* @__PURE__ */ $.createElement(he, { entry: S }) : Y && !U ? W ? W() : /* @__PURE__ */ $.createElement(ye, null) : /* @__PURE__ */ $.createElement(
703
+ Oe,
662
704
  {
663
- key: `auth-epoch-${U}-frame-${o}`,
664
- $isReady: R,
665
- ref: q,
666
- src: y,
667
- ...S
705
+ key: `auth-epoch-${M}-frame-${C}`,
706
+ $isReady: U,
707
+ ref: k,
708
+ src: z,
709
+ ...H
668
710
  }
669
711
  );
670
712
  };
671
- xe.propTypes = {
672
- id: w.string,
673
- src: w.string.isRequired,
674
- path: w.string,
675
- onInit: w.func,
676
- onTimeout: w.func,
713
+ Se.propTypes = {
714
+ id: E.string,
715
+ src: E.string.isRequired,
716
+ path: E.string,
717
+ onInit: E.func,
718
+ onTimeout: E.func,
677
719
  /**
678
720
  * Optional host-styled renderer for the structurally-unsupported embed
679
721
  * state (legacy app on a cross-site origin), `(entry) => ReactNode`. Falls
680
722
  * back to a minimal built-in placeholder.
681
723
  */
682
- renderUnsupported: w.func,
724
+ renderUnsupported: E.func,
683
725
  /**
684
726
  * Optional host-styled renderer for the handshake-timeout failure state
685
727
  * (the app is not embeddable / could not load), `() => ReactNode`. Falls
686
728
  * back to a minimal centered message.
687
729
  */
688
- renderTimeout: w.func
730
+ renderTimeout: E.func
689
731
  };
690
732
  export {
691
- he as CREDENTIAL_KIND_ACCESS_TOKEN,
733
+ be as CREDENTIAL_KIND_ACCESS_TOKEN,
692
734
  J as EmbeddedAuthorizationContext,
693
- xe as MMEmbeddableZone,
694
- re as MMEmbeddableZoneContext,
695
- Ie as MMEmbeddableZoneProvider,
696
- ue as isCrossSite,
697
- oe as normalizeCredential,
698
- Re as popupAuthorizationLauncher,
699
- Ae as unsupportedCopy,
700
- ye as useEmbeddedAuthorizations,
701
- ke as useEmbeddedCredential,
735
+ Se as MMEmbeddableZone,
736
+ ne as MMEmbeddableZoneContext,
737
+ Me as MMEmbeddableZoneProvider,
738
+ ae as isCrossSite,
739
+ ce as normalizeCredential,
740
+ ke as popupAuthorizationLauncher,
741
+ xe as unsupportedCopy,
742
+ qe as useEmbeddedAuthorizations,
743
+ _e as useEmbeddedCredential,
702
744
  X as useEmbeddedEffect,
703
- Te as useEmbeddedOriginEpoch,
704
- we as useEmbeddedUnsupported
745
+ Ie as useEmbeddedOriginEpoch,
746
+ Ue as useEmbeddedUnsupported
705
747
  };