@viceme-ai/sdk 0.4.0 → 0.6.1

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.
Files changed (55) hide show
  1. package/README.md +105 -16
  2. package/dist/browser-events.d.ts +5 -2
  3. package/dist/browser-events.d.ts.map +1 -1
  4. package/dist/chunks/build-endpoints-NrDmg6jf.js +12 -0
  5. package/dist/chunks/build-endpoints-NrDmg6jf.js.map +1 -0
  6. package/dist/chunks/{client-Cbldd7F2.js → client-w9vih8Ng.js} +267 -238
  7. package/dist/chunks/client-w9vih8Ng.js.map +1 -0
  8. package/dist/chunks/{errors-B-Qv_azy.js → errors-C7DfLKcF.js} +14 -22
  9. package/dist/chunks/errors-C7DfLKcF.js.map +1 -0
  10. package/dist/chunks/validation-patAYhXC.js +43 -0
  11. package/dist/chunks/validation-patAYhXC.js.map +1 -0
  12. package/dist/chunks/version-uPTHb89K.js +6 -0
  13. package/dist/chunks/{version-C8DeBm39.js.map → version-uPTHb89K.js.map} +1 -1
  14. package/dist/core/capabilities.d.ts +2 -0
  15. package/dist/core/capabilities.d.ts.map +1 -1
  16. package/dist/core/client.d.ts.map +1 -1
  17. package/dist/core/config.d.ts +1 -1
  18. package/dist/core/config.d.ts.map +1 -1
  19. package/dist/core/errors.d.ts +1 -1
  20. package/dist/core/errors.d.ts.map +1 -1
  21. package/dist/core/presentation.d.ts.map +1 -1
  22. package/dist/danmaku.js +5 -4
  23. package/dist/danmaku.js.map +1 -1
  24. package/dist/generated/public-contract.d.ts +108 -1
  25. package/dist/generated/public-contract.d.ts.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.js +4 -4
  28. package/dist/index.js.map +1 -1
  29. package/dist/manifest.json +49 -37
  30. package/dist/testing.js +14 -14
  31. package/dist/testing.js.map +1 -1
  32. package/dist/tip/headless.d.ts +4 -0
  33. package/dist/tip/headless.d.ts.map +1 -0
  34. package/dist/tip/index.d.ts +47 -2
  35. package/dist/tip/index.d.ts.map +1 -1
  36. package/dist/tip/mount.d.ts.map +1 -1
  37. package/dist/tip/testing.d.ts +9 -0
  38. package/dist/tip/testing.d.ts.map +1 -0
  39. package/dist/tip/testing.js +43 -0
  40. package/dist/tip/testing.js.map +1 -0
  41. package/dist/tip/validation.d.ts +7 -0
  42. package/dist/tip/validation.d.ts.map +1 -0
  43. package/dist/tip.js +419 -84
  44. package/dist/tip.js.map +1 -1
  45. package/dist/version.d.ts +1 -1
  46. package/dist/viceme.min.js +5 -2
  47. package/dist/viceme.min.js.map +1 -1
  48. package/package.json +7 -6
  49. package/dist/bootstrap.min.js +0 -2
  50. package/dist/bootstrap.min.js.map +0 -1
  51. package/dist/chunks/client-Cbldd7F2.js.map +0 -1
  52. package/dist/chunks/errors-B-Qv_azy.js.map +0 -1
  53. package/dist/chunks/version-C8DeBm39.js +0 -6
  54. package/dist/loader/bootstrap.d.ts +0 -1
  55. package/dist/loader/bootstrap.d.ts.map +0 -1
@@ -1,31 +1,34 @@
1
- import { b as M, a as T, V as w, c as C, i as G, B as W } from "./errors-B-Qv_azy.js";
2
- import { S as z, A as H } from "./version-C8DeBm39.js";
3
- const V = /* @__PURE__ */ new Set(["cn", "global"]), j = {
4
- cn: M.cn,
5
- global: M.global
1
+ import { a as C, V as b, c as x, i as G } from "./errors-C7DfLKcF.js";
2
+ import { a as q, B as z } from "./build-endpoints-NrDmg6jf.js";
3
+ import { S as H, A as V } from "./version-uPTHb89K.js";
4
+ const W = /* @__PURE__ */ new Set(["cn", "global"]), j = {
5
+ cn: q.cn,
6
+ global: q.global
6
7
  };
7
- function fe(t) {
8
+ function me(t) {
8
9
  return j[t];
9
10
  }
10
11
  function B(t) {
11
- return typeof t == "string" && /^wrk_[A-Za-z0-9_-]{4,124}$/.test(t);
12
+ return typeof t == "string" && /^wrk_(?:test|live)_[A-Za-z0-9_-]{4,119}$/.test(t);
12
13
  }
13
14
  function Y(t) {
14
- return typeof t == "string" && V.has(t);
15
+ return typeof t == "string" && W.has(t);
15
16
  }
16
- function me(t) {
17
+ function he(t) {
17
18
  if (typeof t != "object" || t === null)
18
- throw T("createViceMe expects a configuration object.");
19
+ throw C("createViceMe expects a configuration object.");
19
20
  const e = t, i = /* @__PURE__ */ new Set(["workKey", "region", "signal"]);
20
- for (const a of Object.keys(e))
21
- if (!i.has(a))
22
- throw T(`Unknown configuration field "${a}".`);
21
+ for (const r of Object.keys(e))
22
+ if (!i.has(r))
23
+ throw C(`Unknown configuration field "${r}".`);
23
24
  if (!B(e.workKey))
24
- throw T('Configuration field "workKey" must be a public work key ("wrk_…").');
25
+ throw C(
26
+ 'Configuration field "workKey" must start with "wrk_test_" or "wrk_live_".'
27
+ );
25
28
  if (!Y(e.region))
26
- throw T('Configuration field "region" must be "cn" or "global".');
29
+ throw C('Configuration field "region" must be "cn" or "global".');
27
30
  if (e.signal !== void 0 && !(e.signal instanceof AbortSignal))
28
- throw T('Configuration field "signal" must be an AbortSignal.');
31
+ throw C('Configuration field "signal" must be an AbortSignal.');
29
32
  return {
30
33
  workKey: e.workKey,
31
34
  region: e.region,
@@ -50,7 +53,7 @@ class Q {
50
53
  transition(e) {
51
54
  if (e !== this.#e) {
52
55
  if (!$[this.#e].includes(e))
53
- throw new w({
56
+ throw new b({
54
57
  code: "INTERNAL_ERROR",
55
58
  message: `Illegal lifecycle transition ${this.#e} -> ${e}.`,
56
59
  retryable: !1
@@ -61,7 +64,7 @@ class Q {
61
64
  }
62
65
  /** Any operation on a destroyed client must fail closed with this guard. */
63
66
  assertAlive() {
64
- if (this.destroyed) throw C();
67
+ if (this.destroyed) throw x();
65
68
  }
66
69
  subscribe(e) {
67
70
  return this.#t.add(e), () => {
@@ -79,25 +82,25 @@ function X(t, e) {
79
82
  if (typeof i.workKey != "string" || !Array.isArray(i.capabilities) || !i.capabilities.every((c) => typeof c == "string"))
80
83
  throw R();
81
84
  if (i.workKey !== e)
82
- throw new w({
85
+ throw new b({
83
86
  code: "WORK_NOT_FOUND",
84
87
  message: "Work-session response did not match the requested work key.",
85
88
  retryable: !1
86
89
  });
87
- const a = {
90
+ const r = {
88
91
  work: {
89
92
  key: i.workKey,
90
93
  capabilities: i.capabilities
91
94
  }
92
95
  };
93
- if (typeof i.token == "string" && (a.token = i.token), typeof i.expiresAt == "string") {
96
+ if (typeof i.token == "string" && (r.token = i.token), typeof i.expiresAt == "string") {
94
97
  const c = new Date(i.expiresAt).getTime();
95
- Number.isNaN(c) || (a.expiresAt = c);
98
+ Number.isNaN(c) || (r.expiresAt = c);
96
99
  }
97
- return a;
100
+ return r;
98
101
  }
99
102
  function R() {
100
- return new w({
103
+ return new b({
101
104
  code: "INTERNAL_ERROR",
102
105
  message: "Malformed work-session response.",
103
106
  retryable: !0
@@ -107,7 +110,7 @@ class J {
107
110
  #e;
108
111
  #t;
109
112
  #i;
110
- #a;
113
+ #r;
111
114
  constructor(e) {
112
115
  this.#e = e, this.#t = e.now ?? (() => Date.now());
113
116
  }
@@ -115,7 +118,7 @@ class J {
115
118
  return this.#i;
116
119
  }
117
120
  /** True when the cached snapshot's server-provided expiry has passed. */
118
- #r() {
121
+ #a() {
119
122
  const e = this.#i?.expiresAt;
120
123
  return e !== void 0 && e <= this.#t();
121
124
  }
@@ -128,10 +131,10 @@ class J {
128
131
  */
129
132
  establish() {
130
133
  if (this.#i) {
131
- if (!this.#r()) return Promise.resolve(this.#i);
134
+ if (!this.#a()) return Promise.resolve(this.#i);
132
135
  this.#i = void 0;
133
136
  }
134
- return this.#a ??= this.#e.transport.request({
137
+ return this.#r ??= this.#e.transport.request({
135
138
  method: "POST",
136
139
  path: "/v1/public/work-sdk/sessions",
137
140
  body: { workKey: this.#e.workKey },
@@ -141,8 +144,8 @@ class J {
141
144
  const i = X(e.body, this.#e.workKey);
142
145
  return this.#i = i, i;
143
146
  }).finally(() => {
144
- this.#a = void 0;
145
- }), this.#a;
147
+ this.#r = void 0;
148
+ }), this.#r;
146
149
  }
147
150
  async request(e) {
148
151
  const i = await this.establish();
@@ -154,8 +157,8 @@ class J {
154
157
  userAuthorization: i.userToken,
155
158
  signal: this.#e.signal
156
159
  });
157
- } catch (a) {
158
- if (!(a instanceof w) || a.code !== "SESSION_EXPIRED") throw a;
160
+ } catch (r) {
161
+ if (!(r instanceof b) || r.code !== "SESSION_EXPIRED") throw r;
159
162
  this.#i === i && this.invalidate();
160
163
  const c = await this.establish();
161
164
  if (!c.token) throw R();
@@ -184,10 +187,10 @@ class J {
184
187
  }
185
188
  /** Hard cleanup: forget the token and pending work. */
186
189
  destroy() {
187
- this.invalidate(), this.#a = void 0;
190
+ this.invalidate(), this.#r = void 0;
188
191
  }
189
192
  }
190
- const _ = "viceme-access-layer", D = 50;
193
+ const M = "viceme-access-layer", U = 50;
191
194
  function Z(t) {
192
195
  switch (t) {
193
196
  case "SIGN_IN":
@@ -223,12 +226,12 @@ function ee(t) {
223
226
  }
224
227
  }
225
228
  function te() {
226
- if (customElements.get(_)) return;
229
+ if (customElements.get(M)) return;
227
230
  class t extends HTMLElement {
228
231
  interaction;
229
232
  connectedCallback() {
230
- const i = Z(this.interaction.action), a = this.attachShadow({ mode: "open" });
231
- a.innerHTML = `
233
+ const i = Z(this.interaction.action), r = this.attachShadow({ mode: "open" });
234
+ r.innerHTML = `
232
235
  <style>
233
236
  :host {
234
237
  position: fixed;
@@ -409,6 +412,9 @@ function te() {
409
412
  }
410
413
  [data-viceme='panel'][data-frame='true'] { width: min(30rem, 100%); }
411
414
  [data-viceme='panel'][data-action='SIGN_IN'] { width: min(30rem, 100%); }
415
+ [data-viceme='panel'][data-action='CHECKOUT'][data-frame='true'] {
416
+ width: min(42rem, 100%);
417
+ }
412
418
  }
413
419
  @media (prefers-reduced-motion: no-preference) {
414
420
  [data-viceme='panel'] { animation: viceme-enter 160ms ease-out; }
@@ -440,105 +446,105 @@ function te() {
440
446
  <iframe data-viceme="frame" title="" referrerpolicy="no-referrer" allow="payment"></iframe>
441
447
  </section>
442
448
  `;
443
- const c = a.querySelector("[data-viceme='panel']");
449
+ const c = r.querySelector("[data-viceme='panel']");
444
450
  c.dataset.action = this.interaction.action, c.dataset.frame = "false";
445
- const S = a.querySelector("[data-viceme='description']");
446
- S.textContent = i.description;
447
- const u = a.querySelector("[data-viceme='action']"), A = u.parentElement, E = a.querySelector("[data-viceme='close']"), L = a.querySelector("[data-viceme='backdrop']"), N = a.querySelector("[data-viceme='error']"), I = a.querySelector("[data-viceme='frame']"), U = a.querySelector("[data-viceme='profile']"), r = a.querySelector("[data-viceme='avatar']"), o = a.querySelector("[data-viceme='avatar-fallback']"), l = a.querySelector("[data-viceme='profile-name']"), n = a.querySelector(
451
+ const N = r.querySelector("[data-viceme='description']");
452
+ N.textContent = i.description;
453
+ const f = r.querySelector("[data-viceme='action']"), O = f.parentElement, A = r.querySelector("[data-viceme='close']"), L = r.querySelector("[data-viceme='backdrop']"), T = r.querySelector("[data-viceme='error']"), I = r.querySelector("[data-viceme='frame']"), P = r.querySelector("[data-viceme='profile']"), n = r.querySelector("[data-viceme='avatar']"), o = r.querySelector("[data-viceme='avatar-fallback']"), u = r.querySelector("[data-viceme='profile-name']"), a = r.querySelector(
448
454
  "[data-viceme='profile-description']"
449
- ), f = a.querySelector("[data-viceme='profile-stats']");
450
- u.textContent = i.label, u.hidden = !1, A.dataset.single = "true";
451
- const b = i.label;
455
+ ), l = r.querySelector("[data-viceme='profile-stats']");
456
+ f.textContent = i.label, f.hidden = !1, O.dataset.single = "true";
457
+ const v = i.label;
452
458
  I.title = this.interaction.action === "SIGN_IN" ? "登录授权" : "支付";
453
459
  const d = this.interaction.followTarget;
454
460
  if (d) {
455
461
  c.setAttribute(
456
462
  "aria-label",
457
463
  this.interaction.action === "SIGN_IN" ? `授权 ${d.displayName}` : `关注 ${d.displayName}`
458
- ), U.dataset.visible = "true", l.textContent = d.displayName;
464
+ ), P.dataset.visible = "true", u.textContent = d.displayName;
459
465
  const s = [...d.description ?? ""];
460
- n.textContent = s.length > D ? `${s.slice(0, D).join("")}…` : s.join(""), s.length > D && (n.title = d.description), f.textContent = `${d.workCount} 个作品`, o.textContent = d.displayName.trim().slice(0, 1) || "V", d.avatarUrl && (r.src = d.avatarUrl, r.alt = `${d.displayName}的头像`, r.hidden = !1, o.hidden = !0);
466
+ a.textContent = s.length > U ? `${s.slice(0, U).join("")}…` : s.join(""), s.length > U && (a.title = d.description), l.textContent = `${d.workCount} 个作品`, o.textContent = d.displayName.trim().slice(0, 1) || "V", d.avatarUrl && (n.src = d.avatarUrl, n.alt = `${d.displayName}的头像`, n.hidden = !1, o.hidden = !0);
461
467
  }
462
- let y = null;
463
- const k = (s) => {
468
+ let p = null;
469
+ const E = (s) => {
464
470
  this.dispatchEvent(new CustomEvent("viceme:access-layer-close", { detail: s }));
465
- }, h = () => {
466
- y?.cancel(), k("dismissed");
471
+ }, y = () => {
472
+ p?.cancel(), E("dismissed");
467
473
  };
468
- L.addEventListener("click", h), E.addEventListener("click", h), this.addEventListener("keydown", (s) => {
469
- if (s.key === "Escape" && h(), s.key === "Tab") {
470
- if (s.preventDefault(), y?.type === "frame") {
474
+ L.addEventListener("click", y), A.addEventListener("click", y), this.addEventListener("keydown", (s) => {
475
+ if (s.key === "Escape" && y(), s.key === "Tab") {
476
+ if (s.preventDefault(), p?.type === "frame") {
471
477
  I.focus();
472
478
  return;
473
479
  }
474
- const p = [E, ...u.hidden ? [] : [u]].filter((K) => !K.disabled && !K.hidden);
475
- if (p.length === 0) return;
476
- const q = p.indexOf(a.activeElement), F = s.shiftKey ? -1 : 1;
477
- p[(q + F + p.length) % p.length]?.focus();
480
+ const m = [A, ...f.hidden ? [] : [f]].filter((_) => !_.disabled && !_.hidden);
481
+ if (m.length === 0) return;
482
+ const D = m.indexOf(r.activeElement), g = s.shiftKey ? -1 : 1;
483
+ m[(D + g + m.length) % m.length]?.focus();
478
484
  }
479
485
  });
480
- const O = async () => {
481
- u.disabled = !0, u.setAttribute("aria-busy", "true"), u.textContent = "正在打开…", N.textContent = "";
486
+ const S = async () => {
487
+ f.disabled = !0, f.setAttribute("aria-busy", "true"), f.textContent = "正在打开…", T.textContent = "";
482
488
  try {
483
489
  const s = await this.interaction.perform();
484
- s.type === "frame" ? (y = s, c.dataset.frame = "true", I.src = s.url, I.focus(), await s.completion) : s.type === "external" && (y = s, await s.completion), k("acted");
490
+ s.type === "frame" ? (p = s, c.dataset.frame = "true", I.src = s.url, I.focus(), await s.completion) : s.type === "external" && (p = s, await s.completion), E("acted");
485
491
  } catch (s) {
486
- y?.cancel(), y = null, c.dataset.frame = "false", I.removeAttribute("src"), N.textContent = ee(s), u.hidden = !1, u.disabled = !1, u.removeAttribute("aria-busy"), u.textContent = b, u.focus();
492
+ p?.cancel(), p = null, c.dataset.frame = "false", I.removeAttribute("src"), T.textContent = ee(s), f.hidden = !1, f.disabled = !1, f.removeAttribute("aria-busy"), f.textContent = v, f.focus();
487
493
  }
488
494
  };
489
- u.addEventListener("click", O), queueMicrotask(() => u.focus());
495
+ f.addEventListener("click", S), queueMicrotask(() => f.focus());
490
496
  }
491
497
  }
492
- customElements.define(_, t);
498
+ customElements.define(M, t);
493
499
  }
494
- const x = async (t) => {
500
+ const K = async (t) => {
495
501
  if (typeof document > "u" || typeof customElements > "u")
496
502
  return "dismissed";
497
503
  te();
498
- const e = document.createElement(_);
504
+ const e = document.createElement(M);
499
505
  e.interaction = t;
500
- const i = document.documentElement.style.overflow, a = document.activeElement instanceof HTMLElement ? document.activeElement : null;
506
+ const i = document.documentElement.style.overflow, r = document.activeElement instanceof HTMLElement ? document.activeElement : null;
501
507
  document.documentElement.style.overflow = "hidden", document.body.append(e);
502
508
  try {
503
509
  return await new Promise((c) => {
504
510
  e.addEventListener(
505
511
  "viceme:access-layer-close",
506
- (S) => c(S.detail),
512
+ (N) => c(N.detail),
507
513
  { once: !0 }
508
514
  );
509
515
  });
510
516
  } finally {
511
- e.remove(), document.documentElement.style.overflow = i, a?.focus();
517
+ e.remove(), document.documentElement.style.overflow = i, r?.focus();
512
518
  }
513
519
  };
514
- function v(t) {
515
- if (typeof t != "object" || t === null) throw m();
520
+ function k(t) {
521
+ if (typeof t != "object" || t === null) throw h();
516
522
  return t;
517
523
  }
518
- function m() {
519
- return new w({
524
+ function h() {
525
+ return new b({
520
526
  code: "INTERNAL_ERROR",
521
527
  message: "Malformed public API response.",
522
528
  retryable: !0
523
529
  });
524
530
  }
525
531
  function ie(t) {
526
- const e = v(t);
532
+ const e = k(t);
527
533
  if (typeof e.id != "string" || !(typeof e.displayName == "string" || e.displayName === null) || !(typeof e.avatarUrl == "string" || e.avatarUrl === null))
528
- throw m();
534
+ throw h();
529
535
  return {
530
536
  subject: e.id,
531
537
  nickname: e.displayName,
532
538
  avatarUrl: e.avatarUrl
533
539
  };
534
540
  }
535
- function P(t) {
536
- const e = v(t);
541
+ function F(t) {
542
+ const e = k(t);
537
543
  if (typeof e.following != "boolean" || !(typeof e.followedAt == "string" || e.followedAt === null))
538
- throw m();
539
- const i = v(e.creator);
544
+ throw h();
545
+ const i = k(e.creator);
540
546
  if (typeof i.displayName != "string" || !(typeof i.avatarUrl == "string" || i.avatarUrl === null) || !(typeof i.description == "string" || i.description === null) || !Number.isInteger(i.publishedWorkCount) || i.publishedWorkCount < 0)
541
- throw m();
547
+ throw h();
542
548
  return {
543
549
  following: e.following,
544
550
  followedAt: e.followedAt,
@@ -551,8 +557,8 @@ function P(t) {
551
557
  }
552
558
  };
553
559
  }
554
- function ae(t) {
555
- const e = v(t), i = /* @__PURE__ */ new Set([
560
+ function re(t) {
561
+ const e = k(t), i = /* @__PURE__ */ new Set([
556
562
  "PUBLIC",
557
563
  "OWNER",
558
564
  "FOLLOWING",
@@ -562,22 +568,22 @@ function ae(t) {
562
568
  "PURCHASE_REQUIRED",
563
569
  "FEATURE_NOT_FOUND",
564
570
  "FEATURE_DISABLED"
565
- ]), a = /* @__PURE__ */ new Set([null, "SIGN_IN", "FOLLOW", "CHECKOUT"]);
566
- if (typeof e.allowed != "boolean" || typeof e.reason != "string" || !i.has(e.reason) || !a.has(e.nextAction))
567
- throw m();
571
+ ]), r = /* @__PURE__ */ new Set([null, "SIGN_IN", "FOLLOW", "CHECKOUT"]);
572
+ if (typeof e.allowed != "boolean" || typeof e.reason != "string" || !i.has(e.reason) || !r.has(e.nextAction))
573
+ throw h();
568
574
  return {
569
575
  allowed: e.allowed,
570
576
  reason: e.reason,
571
577
  nextAction: e.nextAction
572
578
  };
573
579
  }
574
- function re(t) {
575
- const e = v(t);
580
+ function ae(t) {
581
+ const e = k(t);
576
582
  if (typeof e.featureKey != "string" || !/^[a-z][a-z0-9_-]{1,63}$/.test(e.featureKey) || typeof e.title != "string" || e.title.trim().length === 0 || e.title.length > 120 || !(e.policyType === "PUBLIC" || e.policyType === "FOLLOW_OWNER" || e.policyType === "WORK_ENTITLEMENT"))
577
- throw m();
578
- const i = e.price === null ? null : v(e.price);
583
+ throw h();
584
+ const i = e.price === null ? null : k(e.price);
579
585
  if (i !== null && (!Number.isInteger(i.amountCents) || i.amountCents <= 0 || i.currency !== "CNY") || e.policyType === "WORK_ENTITLEMENT" != (i !== null))
580
- throw m();
586
+ throw h();
581
587
  return {
582
588
  featureKey: e.featureKey,
583
589
  title: e.title,
@@ -585,11 +591,11 @@ function re(t) {
585
591
  price: i === null ? null : { amountCents: i.amountCents, currency: "CNY" }
586
592
  };
587
593
  }
588
- function oe() {
594
+ function ne() {
589
595
  const t = crypto.getRandomValues(new Uint8Array(32));
590
- return ne(t);
596
+ return oe(t);
591
597
  }
592
- function ne(t) {
598
+ function oe(t) {
593
599
  let e = "";
594
600
  for (const i of t) e += String.fromCharCode(i);
595
601
  return btoa(e).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
@@ -598,232 +604,254 @@ function se() {
598
604
  return typeof document < "u" && document.documentElement.lang.toLowerCase() === "en-us" ? "en-US" : "zh-CN";
599
605
  }
600
606
  function ce(t) {
601
- const e = () => new w({
607
+ const e = () => new b({
602
608
  code: "AUTH_CANCELLED",
603
609
  message: "The interactive action was cancelled.",
604
610
  retryable: !1
605
- }), i = (r, o, l, n, f, b) => {
611
+ }), i = (n, o, u, a, l, v) => {
606
612
  if (typeof window > "u")
607
- throw new w({
613
+ throw new b({
608
614
  code: "CONFIG_INVALID",
609
615
  message: "Interactive access requires a browser window.",
610
616
  retryable: !1
611
617
  });
612
- let d = !0, y, k;
613
- const h = () => {
618
+ let d = !0, p, E;
619
+ const y = () => {
614
620
  d && (d = !1, window.removeEventListener("message", s));
615
- }, O = new Promise((g, p) => {
616
- y = g, k = p;
617
- }), s = (g) => {
618
- if (!d || g.origin !== o || typeof g.data != "object" || g.data === null) return;
619
- const p = g.data;
620
- if (!(p.workKey !== t.workKey || p.channel !== l)) {
621
- if (p.type === `viceme:${n}:ready`) {
622
- g.source && "postMessage" in g.source && g.source.postMessage(
621
+ }, S = new Promise((w, m) => {
622
+ p = w, E = m;
623
+ }), s = (w) => {
624
+ if (!d || w.origin !== o || typeof w.data != "object" || w.data === null) return;
625
+ const m = w.data;
626
+ if (!(m.workKey !== t.workKey || m.channel !== u)) {
627
+ if (m.type === `viceme:${a}:ready`) {
628
+ w.source && "postMessage" in w.source && w.source.postMessage(
623
629
  {
624
- type: `viceme:${n}:init`,
630
+ type: `viceme:${a}:init`,
625
631
  workKey: t.workKey,
626
- channel: l,
627
- ...f
632
+ channel: u,
633
+ ...l
628
634
  },
629
635
  o
630
636
  );
631
637
  return;
632
638
  }
633
- if (p.type === `viceme:${n}:cancelled`) {
634
- h(), k(e());
639
+ if (m.type === `viceme:${a}:cancelled`) {
640
+ y(), E(e());
635
641
  return;
636
642
  }
637
- p.type === `viceme:${n}:complete` && (h(), b(p).then(y, k));
643
+ m.type === `viceme:${a}:complete` && (y(), v(m).then(p, E));
638
644
  }
639
645
  };
640
646
  return window.addEventListener("message", s), {
641
647
  type: "frame",
642
- url: r,
643
- completion: O,
648
+ url: n,
649
+ completion: S,
644
650
  cancel() {
645
- h(), y();
651
+ y(), p();
646
652
  }
647
653
  };
648
- }, a = async () => {
654
+ }, r = async () => {
649
655
  if (await t.ready(), typeof window > "u")
650
- throw new w({
656
+ throw new b({
651
657
  code: "CONFIG_INVALID",
652
658
  message: "Interactive access requires a browser window.",
653
659
  retryable: !1
654
660
  });
655
- const r = oe(), o = new URL("/sdk/login", t.widgetOrigin);
661
+ const n = ne(), o = new URL("/sdk/login", t.widgetOrigin);
656
662
  o.search = new URLSearchParams({
657
663
  workKey: t.workKey,
658
- channel: r,
664
+ channel: n,
659
665
  parentOrigin: window.location.origin,
660
666
  locale: se()
661
667
  }).toString();
662
- const l = t.session.snapshot?.token;
663
- if (!l) throw m();
668
+ const u = t.session.snapshot?.token;
669
+ if (!u) throw h();
664
670
  return i(
665
671
  o.toString(),
666
672
  t.widgetOrigin,
667
- r,
673
+ n,
668
674
  "auth",
669
- { workSessionToken: l },
670
- async (n) => {
671
- if (typeof n.userToken != "string")
672
- throw m();
673
- t.session.authenticate({ userToken: n.userToken, user: ie(n.user) });
675
+ { workSessionToken: u },
676
+ async (a) => {
677
+ if (typeof a.userToken != "string")
678
+ throw h();
679
+ t.session.authenticate({ userToken: a.userToken, user: ie(a.user) });
674
680
  }
675
681
  );
676
- }, c = async () => (await t.ready(), P(
682
+ }, c = async () => (await t.ready(), F(
677
683
  (await t.session.request({ method: "GET", path: "/v1/public/work-sdk/follow" })).body
678
- )), S = async () => (await t.ready(), P(
684
+ )), N = async () => (await t.ready(), F(
679
685
  (await t.session.request({ method: "PUT", path: "/v1/public/work-sdk/follow" })).body
680
- )), u = {
686
+ )), f = {
681
687
  async getState() {
682
688
  await t.ready();
683
- const r = t.session.snapshot?.user ?? null;
684
- return { authenticated: r !== null, user: r };
689
+ const n = t.session.snapshot?.user ?? null;
690
+ return { authenticated: n !== null, user: n };
685
691
  },
686
692
  async signIn() {
687
- if (await (t.presenter ?? x)({
693
+ if (await (t.presenter ?? K)({
688
694
  featureKey: "auth",
689
695
  reason: "AUTH_REQUIRED",
690
696
  action: "SIGN_IN",
691
- perform: a
697
+ perform: r
692
698
  }) === "dismissed") throw e();
693
699
  return this.getState();
694
700
  },
695
701
  async signOut() {
696
702
  await t.ready(), await t.session.signOut();
697
703
  }
698
- }, A = {
704
+ }, O = {
699
705
  getState: c,
700
- follow: S,
706
+ follow: N,
701
707
  unfollow: async () => {
702
- throw new w({
708
+ throw new b({
703
709
  code: "CAPABILITY_DISABLED",
704
710
  message: "Unfollow is not exposed by website access.",
705
711
  retryable: !1
706
712
  });
707
713
  }
708
- }, E = async (r) => {
714
+ }, A = async (n) => {
709
715
  await t.ready();
710
- const o = v(
716
+ const o = k(
711
717
  (await t.session.request({
712
718
  method: "POST",
713
719
  path: "/v1/public/work-sdk/access/check",
714
- body: { featureKeys: r }
720
+ body: { featureKeys: n }
715
721
  })).body
716
- ), l = v(o.decisions);
722
+ ), u = k(o.decisions);
717
723
  return Object.fromEntries(
718
- Object.entries(l).map(([n, f]) => [n, ae(f)])
724
+ Object.entries(u).map(([a, l]) => [a, re(l)])
719
725
  );
720
- }, L = async (r, o = {}) => {
726
+ }, L = async (n, o = {}) => {
721
727
  await t.ready();
722
- const l = {
723
- featureKey: r,
728
+ const u = {
729
+ featureKey: n,
724
730
  locale: o.locale ?? "zh-CN"
725
- }, n = v(
731
+ }, a = k(
726
732
  (await t.session.request({
727
733
  method: "POST",
728
734
  path: "/v1/public/work-sdk/checkout",
729
- body: l
735
+ body: u
730
736
  })).body
731
737
  );
732
- if (typeof n.checkoutUrl != "string" || typeof n.alreadyOwned != "boolean")
733
- throw m();
738
+ if (typeof a.checkoutUrl != "string" || typeof a.alreadyOwned != "boolean" || !(a.expiresAt === null || typeof a.expiresAt == "string" && !Number.isNaN(Date.parse(a.expiresAt))) || a.alreadyOwned !== (a.expiresAt === null))
739
+ throw h();
740
+ let l;
734
741
  try {
735
- new URL(n.checkoutUrl);
742
+ l = new URL(a.checkoutUrl);
736
743
  } catch {
737
- throw m();
744
+ throw h();
738
745
  }
746
+ if (!a.alreadyOwned && (l.protocol !== "https:" || l.origin !== t.widgetOrigin))
747
+ throw h();
739
748
  return {
740
- checkoutUrl: n.checkoutUrl,
741
- alreadyOwned: n.alreadyOwned
749
+ checkoutUrl: a.checkoutUrl,
750
+ alreadyOwned: a.alreadyOwned,
751
+ expiresAt: a.expiresAt
742
752
  };
743
- }, N = (r, o) => {
744
- if (r.alreadyOwned) return { type: "completed" };
753
+ }, T = (n, o) => {
754
+ if (n.alreadyOwned) return { type: "completed" };
755
+ const u = Date.parse(n.expiresAt);
745
756
  if (typeof window > "u")
746
- throw new w({
757
+ throw new b({
747
758
  code: "CONFIG_INVALID",
748
759
  message: "Hosted checkout requires a browser window.",
749
760
  retryable: !1
750
761
  });
751
- const l = window.open(r.checkoutUrl, "_blank", "popup,width=520,height=760");
752
- if (!l)
753
- throw new w({
754
- code: "AUTH_CANCELLED",
755
- message: "The hosted checkout window was blocked.",
756
- retryable: !1
757
- });
758
- let n = !0, f, b, d;
759
- const y = new Promise((h, O) => {
760
- b = h, d = O;
761
- }), k = async () => {
762
- if (n)
762
+ let a = !0, l, v, d, p;
763
+ const E = new Promise((g, _) => {
764
+ d = g, p = _;
765
+ }), y = () => {
766
+ l && clearTimeout(l), v && clearTimeout(v), document.removeEventListener("visibilitychange", m);
767
+ }, S = () => {
768
+ a && (a = !1, y(), p(
769
+ new b({
770
+ code: "SESSION_EXPIRED",
771
+ message: "Hosted checkout session expired.",
772
+ retryable: !0
773
+ })
774
+ ));
775
+ }, s = () => {
776
+ if (!a || document.visibilityState === "hidden") return;
777
+ const g = u - t.now();
778
+ if (g <= 0) {
779
+ S();
780
+ return;
781
+ }
782
+ l = setTimeout(() => {
783
+ w();
784
+ }, Math.min(1500, g));
785
+ }, w = async () => {
786
+ if (a)
763
787
  try {
764
- const h = (await E([o]))[o];
765
- if (!h) throw m();
766
- if (h.allowed) {
767
- n = !1, l.close(), b();
768
- return;
769
- }
770
- if (l.closed) {
771
- n = !1, d(e());
788
+ const g = (await A([o]))[o];
789
+ if (!g) throw h();
790
+ if (g.allowed) {
791
+ a = !1, y(), d();
772
792
  return;
773
793
  }
774
- f = setTimeout(() => {
775
- k();
776
- }, 1500);
777
- } catch (h) {
778
- n = !1, d(h);
794
+ s();
795
+ } catch (g) {
796
+ a = !1, y(), p(g);
779
797
  }
780
798
  };
781
- return f = setTimeout(() => {
782
- k();
783
- }, 1500), {
784
- type: "external",
785
- completion: y,
799
+ function m() {
800
+ if (a) {
801
+ if (document.visibilityState === "hidden") {
802
+ l && clearTimeout(l), l = void 0;
803
+ return;
804
+ }
805
+ s();
806
+ }
807
+ }
808
+ document.addEventListener("visibilitychange", m);
809
+ const D = u - t.now();
810
+ return D <= 0 ? S() : (v = setTimeout(S, D), s()), {
811
+ type: "frame",
812
+ url: n.checkoutUrl,
813
+ completion: E,
786
814
  cancel() {
787
- n && (n = !1, f && clearTimeout(f), l.close(), b());
815
+ a && (a = !1, y(), d());
788
816
  }
789
817
  };
790
818
  };
791
- return { auth: u, follow: A, access: {
819
+ return { auth: f, follow: O, access: {
792
820
  async getFeatures() {
793
821
  await t.ready();
794
- const r = v(
822
+ const n = k(
795
823
  (await t.session.request({
796
824
  method: "GET",
797
825
  path: "/v1/public/work-sdk/access/features"
798
826
  })).body
799
827
  );
800
- if (!Array.isArray(r.features) || r.features.length > 100)
801
- throw m();
802
- return r.features.map(re);
828
+ if (!Array.isArray(n.features) || n.features.length > 100)
829
+ throw h();
830
+ return n.features.map(ae);
803
831
  },
804
- async check(r) {
805
- const l = (await E([r]))[r];
806
- if (!l) throw m();
807
- return l;
832
+ async check(n) {
833
+ const u = (await A([n]))[n];
834
+ if (!u) throw h();
835
+ return u;
808
836
  },
809
- checkMany: E,
810
- async require(r) {
811
- let o = await this.check(r);
812
- const l = t.presenter ?? x;
813
- for (let n = 0; !o.allowed && o.nextAction && n < 3; n += 1) {
814
- const f = o.nextAction, b = f === "FOLLOW" ? (await A.getState()).target : void 0, d = f === "CHECKOUT" ? await L(r) : void 0;
837
+ checkMany: A,
838
+ async require(n) {
839
+ let o = await this.check(n);
840
+ const u = t.presenter ?? K;
841
+ for (let a = 0; !o.allowed && o.nextAction && a < 3; a += 1) {
842
+ const l = o.nextAction, v = l === "FOLLOW" ? (await O.getState()).target : void 0, d = l === "CHECKOUT" ? await L(n) : void 0;
815
843
  if (d?.alreadyOwned) {
816
- o = await this.check(r);
844
+ o = await this.check(n);
817
845
  continue;
818
846
  }
819
- if (await l({
820
- featureKey: r,
847
+ if (await u({
848
+ featureKey: n,
821
849
  reason: o.reason,
822
- action: f,
823
- ...b ? { followTarget: b } : {},
824
- perform: async () => f === "SIGN_IN" ? a() : f === "FOLLOW" ? (await A.follow(), { type: "completed" }) : N(d, r)
850
+ action: l,
851
+ ...v ? { followTarget: v } : {},
852
+ perform: async () => l === "SIGN_IN" ? r() : l === "FOLLOW" ? (await O.follow(), { type: "completed" }) : T(d, n)
825
853
  }) === "dismissed") return o;
826
- o = await this.check(r);
854
+ o = await this.check(n);
827
855
  }
828
856
  return o;
829
857
  },
@@ -831,14 +859,14 @@ function ce(t) {
831
859
  await t.ready();
832
860
  }
833
861
  }, checkout: {
834
- async open(r) {
835
- const o = await L(r.featureKey, r);
862
+ async open(n) {
863
+ const o = await L(n.featureKey, n);
836
864
  if (o.alreadyOwned) return o;
837
- if (await (t.presenter ?? x)({
838
- featureKey: r.featureKey,
865
+ if (await (t.presenter ?? K)({
866
+ featureKey: n.featureKey,
839
867
  reason: "PURCHASE_REQUIRED",
840
868
  action: "CHECKOUT",
841
- perform: async () => N(o, r.featureKey)
869
+ perform: async () => T(o, n.featureKey)
842
870
  }) === "dismissed") throw e();
843
871
  return o;
844
872
  }
@@ -849,46 +877,47 @@ class le extends DOMException {
849
877
  super("ViceMe client destroyed.", "AbortError");
850
878
  }
851
879
  }
852
- class he {
880
+ class pe {
853
881
  #e = new Q();
854
882
  #t;
855
883
  #i;
856
- #a = new AbortController();
857
- #r;
884
+ #r = new AbortController();
885
+ #a;
858
886
  auth;
859
887
  access;
860
888
  checkout;
861
889
  /** Detaches the caller-signal listener; undefined when none was attached. */
862
- #o;
890
+ #n;
863
891
  constructor(e) {
864
892
  this.#i = e.config, this.#t = new J({
865
893
  workKey: e.config.workKey,
866
894
  transport: e.transport,
867
- signal: this.#a.signal,
895
+ signal: this.#r.signal,
868
896
  ...e.now !== void 0 ? { now: e.now } : {}
869
897
  });
870
898
  const i = ce({
871
899
  session: this.#t,
872
900
  workKey: e.config.workKey,
873
- widgetOrigin: W[e.config.region],
901
+ widgetOrigin: z[e.config.region],
902
+ now: e.now ?? (() => Date.now()),
874
903
  presenter: e.presenter,
875
- ready: () => this.#n()
904
+ ready: () => this.#o()
876
905
  });
877
906
  this.auth = i.auth, this.access = i.access, this.checkout = i.checkout;
878
- const a = e.config.signal;
879
- if (a) {
907
+ const r = e.config.signal;
908
+ if (r) {
880
909
  const c = () => {
881
- this.#a.abort(
882
- a.reason instanceof Error ? a.reason : new DOMException("Caller signal aborted.", "AbortError")
910
+ this.#r.abort(
911
+ r.reason instanceof Error ? r.reason : new DOMException("Caller signal aborted.", "AbortError")
883
912
  );
884
913
  };
885
- a.aborted ? c() : (a.addEventListener("abort", c, { once: !0 }), this.#o = () => {
886
- a.removeEventListener("abort", c);
914
+ r.aborted ? c() : (r.addEventListener("abort", c, { once: !0 }), this.#n = () => {
915
+ r.removeEventListener("abort", c);
887
916
  });
888
917
  }
889
918
  }
890
919
  get version() {
891
- return z;
920
+ return H;
892
921
  }
893
922
  get workKey() {
894
923
  return this.#i.workKey;
@@ -901,18 +930,18 @@ class he {
901
930
  }
902
931
  /** Internal: public API major, used by the loader registry namespace. */
903
932
  get apiMajor() {
904
- return H;
933
+ return V;
905
934
  }
906
935
  ready() {
907
- return this.#e.destroyed ? Promise.reject(C()) : (this.#r || (this.#e.transition("READY"), this.#r = Promise.resolve()), this.#r);
936
+ return this.#e.destroyed ? Promise.reject(x()) : (this.#a || (this.#e.transition("READY"), this.#a = Promise.resolve()), this.#a);
908
937
  }
909
- async #n() {
910
- if (this.#e.destroyed) throw C();
938
+ async #o() {
939
+ if (this.#e.destroyed) throw x();
911
940
  await this.ready();
912
941
  try {
913
- if (await this.#t.establish(), this.#e.destroyed) throw C();
942
+ if (await this.#t.establish(), this.#e.destroyed) throw x();
914
943
  } catch (e) {
915
- throw this.#e.destroyed ? C() : (this.#t.invalidate(), e);
944
+ throw this.#e.destroyed ? x() : (this.#t.invalidate(), e);
916
945
  }
917
946
  }
918
947
  hasCapability(e) {
@@ -936,14 +965,14 @@ class he {
936
965
  return this.#t.snapshot;
937
966
  }
938
967
  destroy() {
939
- this.#e.destroyed || (this.#e.transition("DESTROYED"), this.#a.abort(new le()), this.#t.destroy(), this.#e.clearListeners(), this.#r = void 0, this.#o?.(), this.#o = void 0);
968
+ this.#e.destroyed || (this.#e.transition("DESTROYED"), this.#r.abort(new le()), this.#t.destroy(), this.#e.clearListeners(), this.#a = void 0, this.#n?.(), this.#n = void 0);
940
969
  }
941
970
  }
942
971
  export {
943
- he as V,
972
+ pe as V,
944
973
  Y as a,
945
974
  B as i,
946
- fe as r,
947
- me as v
975
+ me as r,
976
+ he as v
948
977
  };
949
- //# sourceMappingURL=client-Cbldd7F2.js.map
978
+ //# sourceMappingURL=client-w9vih8Ng.js.map