@viceme-ai/sdk 0.4.0 → 0.6.2

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