@commentkit/comment-kit 1.5.1 → 2.0.0

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 (141) hide show
  1. package/CHANGELOG.md +281 -582
  2. package/README.md +334 -154
  3. package/SECURITY.md +47 -46
  4. package/dist/node/THIRD_PARTY_NOTICES.txt +56 -0
  5. package/dist/node/browser.js +17 -0
  6. package/dist/node/cli/api.js +226 -0
  7. package/dist/node/cli/bin.js +18 -0
  8. package/dist/node/cli/credentials.js +268 -0
  9. package/dist/node/cli/doctor.js +725 -0
  10. package/dist/node/cli/format.js +54 -0
  11. package/dist/node/cli/init.js +372 -0
  12. package/dist/node/cli/main.js +519 -0
  13. package/dist/node/cli/mcp.js +114 -0
  14. package/dist/node/cli/open.js +20 -0
  15. package/dist/node/plugins/astro-B5y1Gtbf.js +31 -0
  16. package/dist/node/plugins/astro.js +26 -0
  17. package/dist/node/plugins/git-BIXLbL3f.js +92 -0
  18. package/dist/node/plugins/jsx-Bg4BdSph.js +7958 -0
  19. package/dist/node/plugins/next.js +48 -0
  20. package/dist/node/plugins/options-THop1K92.js +71 -0
  21. package/dist/node/plugins/transform-CKiAuSbk.js +1155 -0
  22. package/dist/node/plugins/vite-BdAYUW10.js +74 -0
  23. package/dist/node/plugins/vite.js +1 -0
  24. package/dist/node/plugins/webpack-R2U2DLlY.js +35 -0
  25. package/dist/node/plugins/webpack-loader.cjs +21 -0
  26. package/dist/node/plugins/webpack-loader.js +18 -0
  27. package/dist/node/plugins/webpack.js +1 -0
  28. package/dist/node/shared/endpoint.js +26 -0
  29. package/dist/node/shared/format.js +63 -0
  30. package/dist/node/types/astro.d.ts +11 -0
  31. package/dist/node/types/next.d.ts +34 -0
  32. package/dist/node/types/options.d.ts +59 -0
  33. package/dist/node/types/vite.d.ts +11 -0
  34. package/dist/node/types/webpack-loader.d.ts +22 -0
  35. package/dist/node/types/webpack.d.ts +27 -0
  36. package/dist/npm/THIRD_PARTY_NOTICES.txt +6 -6
  37. package/dist/npm/chunks/AgentSend-DxRJIYiz.js +68 -0
  38. package/dist/npm/chunks/Checklist-CnlfeSbc.js +282 -0
  39. package/dist/npm/chunks/Composer-B1bBVszY.js +725 -0
  40. package/dist/npm/chunks/Guest-vvNPz8h-.js +267 -0
  41. package/dist/npm/chunks/MarkupEditor-VCOXh9d2.js +338 -0
  42. package/dist/npm/chunks/Panel-F37vIL1K.js +1206 -0
  43. package/dist/npm/chunks/Settings-BGoZS2iV.js +231 -0
  44. package/dist/npm/chunks/Shortcuts-B2OMvczr.js +62 -0
  45. package/dist/npm/chunks/StyleEditor-CJ_d8Huf.js +46 -0
  46. package/dist/npm/chunks/StylePanel-yQzHHlY7.js +199 -0
  47. package/dist/npm/chunks/TaskFields-fmy6_HtK.js +289 -0
  48. package/dist/npm/chunks/ThreadCard-D51slKST.js +1113 -0
  49. package/dist/npm/chunks/Viewer-BOBadqmC.js +111 -0
  50. package/dist/npm/chunks/{agent-CqDO3JMA.js → agent-C1OQzBAm.js} +20 -21
  51. package/dist/npm/chunks/{anchor-OCQnjp_6.js → anchor-BsQNVkHj.js} +109 -87
  52. package/dist/npm/chunks/{ar-BQ9K9Oqf.js → ar-CAJ3XT8e.js} +73 -20
  53. package/dist/npm/chunks/card-bits-CAnzrqx3.js +53 -0
  54. package/dist/npm/chunks/{client-dZeNdizE.js → client-kNAo2TTG.js} +292 -285
  55. package/dist/npm/chunks/{colors-CAjY4jxf.js → colors-CWAn_swA.js} +1 -2
  56. package/dist/npm/chunks/confetti-CkBdSu19.js +49 -0
  57. package/dist/npm/chunks/context-B8UAzve0.js +20 -0
  58. package/dist/npm/chunks/context-BuMl7A07.js +48 -0
  59. package/dist/npm/chunks/{de-CcPi8xdC.js → de-Dl18G48t.js} +64 -19
  60. package/dist/npm/chunks/dialog-GO04oC3D.js +3 -0
  61. package/dist/npm/chunks/dpop-U48QIQd7.js +96 -0
  62. package/dist/npm/chunks/drafts-B0ctr1g9.js +21 -0
  63. package/dist/npm/chunks/{es-DXgkjDCG.js → es-Dxk79lS9.js} +64 -19
  64. package/dist/npm/chunks/extension-CKFpPWmi.js +62 -0
  65. package/dist/npm/chunks/{extension-BFkXOmX5.js → extension-E81hWyiT.js} +21 -14
  66. package/dist/npm/chunks/files-DyD5BZgp.js +114 -0
  67. package/dist/npm/chunks/{fr-IjJaUbWh.js → fr-8BebGKYD.js} +121 -76
  68. package/dist/npm/chunks/{hi-BhKQK2MU.js → hi-BIN6Yy_p.js} +65 -20
  69. package/dist/npm/chunks/{html2canvas-pro.esm-lgs6tb4C.js → html2canvas-pro.esm-B7b8p5Wr.js} +608 -528
  70. package/dist/npm/chunks/i18n-DOBt8e9U.js +936 -0
  71. package/dist/npm/chunks/identify-CN-3xzqL.js +106 -0
  72. package/dist/npm/chunks/{it-DKiSCkiD.js → it-Cr6EwpPQ.js} +83 -38
  73. package/dist/npm/chunks/{ja-BI1WagDg.js → ja-Iy3s5aur.js} +62 -17
  74. package/dist/npm/chunks/late-words-B2bmZceJ.js +12 -0
  75. package/dist/npm/chunks/live-CelVeZfP.js +85 -0
  76. package/dist/npm/chunks/locale-D8Q4Q2Nt.js +26 -0
  77. package/dist/npm/chunks/{lock-C82sC17d.js → lock-B1JLlOhz.js} +0 -1
  78. package/dist/npm/chunks/{nl-CJw3VUQ5.js → nl-B84WHqqe.js} +65 -20
  79. package/dist/npm/chunks/page-key-DepmJzC1.js +62 -0
  80. package/dist/npm/chunks/{pick-dDDLxCDI.js → pick-yTyqtxN0.js} +46 -46
  81. package/dist/npm/chunks/pkce-ChKGSW_4.js +8 -0
  82. package/dist/npm/chunks/placement-BF_HOUDD.js +74 -0
  83. package/dist/npm/chunks/privacy-BWUGlHw5.js +57 -0
  84. package/dist/npm/chunks/{pt-BR-CjBolC5g.js → pt-BR-DOkdlfoe.js} +64 -19
  85. package/dist/npm/chunks/{realtime-D22tI_AS.js → realtime-C79zF50C.js} +16 -15
  86. package/dist/npm/chunks/report-CrIqQZDI.js +44 -0
  87. package/dist/npm/chunks/{rolldown-runtime-Y_bBpyJl.js → rolldown-runtime-QGRDbDUS.js} +1 -2
  88. package/dist/npm/chunks/show-BgVC8gWY.js +97 -0
  89. package/dist/npm/chunks/signin-Dd1rb2BP.js +75 -0
  90. package/dist/npm/chunks/{source-BQS2_xfB.js → source-pXVve2Cx.js} +2 -3
  91. package/dist/npm/chunks/src-BTMy4Gyc.js +208 -0
  92. package/dist/npm/chunks/start-DtVlpt7p.js +1878 -0
  93. package/dist/npm/chunks/states-sfTbjZz8.js +3 -0
  94. package/dist/npm/chunks/upload-MgPYqPyE.js +42 -0
  95. package/dist/npm/chunks/uploads-D49mywwz.js +1146 -0
  96. package/dist/npm/chunks/wrap-Bl0gbnFa.js +69 -0
  97. package/dist/npm/chunks/{zh-CN-BrZGx2S0.js → zh-CN-Z5fQCb7G.js} +63 -18
  98. package/dist/npm/element.js +59 -23
  99. package/dist/npm/headless.js +7 -3
  100. package/dist/npm/index.js +3 -3
  101. package/dist/npm/react.js +19 -13
  102. package/dist/npm/svelte.js +29 -14
  103. package/dist/npm/types/adapters/react.d.ts +4 -1
  104. package/dist/npm/types/adapters/svelte.d.ts +4 -1
  105. package/dist/npm/types/adapters/vue.d.ts +35 -2
  106. package/dist/npm/types/anchor/index.d.ts +19 -0
  107. package/dist/npm/types/capture/privacy.d.ts +14 -0
  108. package/dist/npm/types/core/api.d.ts +20 -1
  109. package/dist/npm/types/core/auth.d.ts +47 -14
  110. package/dist/npm/types/core/client.d.ts +60 -4
  111. package/dist/npm/types/core/page-key.d.ts +42 -0
  112. package/dist/npm/types/core/types.d.ts +87 -4
  113. package/dist/npm/types/element.d.ts +17 -7
  114. package/dist/npm/types/headless.d.ts +2 -1
  115. package/dist/npm/types/index.d.ts +16 -6
  116. package/dist/npm/vue.js +45 -11
  117. package/package.json +95 -21
  118. package/dist/npm/chunks/AgentSend-DjFZSC0S.js +0 -64
  119. package/dist/npm/chunks/Checklist-BEz4IZdT.js +0 -259
  120. package/dist/npm/chunks/Composer-D1wbmdjF.js +0 -681
  121. package/dist/npm/chunks/Guest-CtyRy8Kz.js +0 -170
  122. package/dist/npm/chunks/MarkupEditor-CPH4yKx4.js +0 -334
  123. package/dist/npm/chunks/Panel-DSoUAlbQ.js +0 -863
  124. package/dist/npm/chunks/Settings-C_qV4n7J.js +0 -206
  125. package/dist/npm/chunks/Shortcuts-ChnaribX.js +0 -60
  126. package/dist/npm/chunks/StyleEditor-DqZd6C5o.js +0 -44
  127. package/dist/npm/chunks/StylePanel-DMC3riYK.js +0 -199
  128. package/dist/npm/chunks/TaskFields-BfjjllMx.js +0 -272
  129. package/dist/npm/chunks/ThreadCard-DmKAelSL.js +0 -1026
  130. package/dist/npm/chunks/Viewer-mw69qSI5.js +0 -110
  131. package/dist/npm/chunks/card-bits-C7bIk5KX.js +0 -126
  132. package/dist/npm/chunks/confetti-DKeKR7-6.js +0 -41
  133. package/dist/npm/chunks/drafts-CpQdHhcK.js +0 -51
  134. package/dist/npm/chunks/extension-Gvei3opb.js +0 -54
  135. package/dist/npm/chunks/i18n-DAJGndZ2.js +0 -904
  136. package/dist/npm/chunks/live-MvvBdoGd.js +0 -34
  137. package/dist/npm/chunks/report-DoDVi1qT.js +0 -28
  138. package/dist/npm/chunks/src-C_oUnL0i.js +0 -150
  139. package/dist/npm/chunks/start-C5-rebEU.js +0 -1783
  140. package/dist/npm/chunks/upload-CzPAjktW.js +0 -42
  141. package/dist/npm/chunks/uploads-tpLjiJdH.js +0 -541
@@ -1,9 +1,19 @@
1
- //#region src/core/types.ts
2
- var e = class extends Error {
1
+ // src/core/id.ts
2
+ function e(e) {
3
+ let t = "";
4
+ for (let n of e) t += String.fromCharCode(n);
5
+ return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
6
+ }
7
+ function t(t = 32) {
8
+ let n = new Uint8Array(t);
9
+ return crypto.getRandomValues(n), e(n);
10
+ }
11
+ // src/core/types.ts
12
+ var n = class extends Error {
3
13
  constructor(e, t, n, r) {
4
14
  super(n), this.status = e, this.code = t, this.errors = r, this.name = "CommentKitError";
5
15
  }
6
- }, t = /* @__PURE__ */ new Set([
16
+ }, r = /* @__PURE__ */ new Set([
7
17
  408,
8
18
  425,
9
19
  429,
@@ -11,58 +21,56 @@ var e = class extends Error {
11
21
  502,
12
22
  503,
13
23
  504
14
- ]), n = 4, r = class {
24
+ ]), i = 3e4, a = 4, o = class {
15
25
  constructor(e, t, n) {
16
26
  this.baseUrl = e, this.tokens = t, this.sdkVersion = n;
17
27
  }
18
- async request(r, c = {}) {
19
- let l = c.method ?? "GET", u = c.idempotencyKey ?? (c.idempotent ? crypto.randomUUID() : null), d = !1, f = 0;
20
- for (let p = 0;; p++) {
21
- let m = {
28
+ async request(e, o = {}) {
29
+ let d = o.method ?? "GET", f = o.idempotencyKey ?? (o.idempotent ? t(16) : null), p = !1, m = !1, h = 0, g = this.url(e, o.query);
30
+ for (let e = 0;; e++) {
31
+ let t = {
22
32
  Accept: "application/json",
23
33
  "X-CommentKit-SDK": this.sdkVersion,
24
- ...c.headers
34
+ ...o.headers
25
35
  };
26
- if (c.body !== void 0 && (m["Content-Type"] = "application/json"), u && (m["Idempotency-Key"] = u), !c.anonymous) {
27
- let e = await this.tokens.accessToken();
28
- e && (m.Authorization = `Bearer ${e}`);
29
- }
30
- let h;
36
+ o.body !== void 0 && (t["Content-Type"] = "application/json"), f && (t["Idempotency-Key"] = f);
37
+ let _ = o.anonymous ? null : await this.tokens.accessToken(), v = !!this.tokens.proof;
38
+ _ && (t.Authorization = `${v ? "DPoP" : "Bearer"} ${_}`), v && (_ || o.proof) && (t.DPoP = await this.tokens.proof(d, g, _));
39
+ let y = s(i, o.signal), b, x;
31
40
  try {
32
- h = await fetch(this.url(r, c.query), {
33
- method: l,
34
- headers: m,
35
- body: c.body === void 0 ? void 0 : JSON.stringify(c.body),
41
+ b = await fetch(g, {
42
+ method: d,
43
+ headers: t,
44
+ body: o.body === void 0 ? void 0 : JSON.stringify(o.body),
36
45
  credentials: "omit",
37
- signal: c.signal
38
- });
46
+ signal: y.signal
47
+ }), x = await b.text();
39
48
  } catch (t) {
40
- if (c.signal?.aborted) throw t;
41
- if (p < 3 && (l === "GET" || u)) {
42
- await o(a(p));
49
+ if (o.signal?.aborted) throw t;
50
+ if (e < 3 && (d === "GET" || f)) {
51
+ await l(c(e));
43
52
  continue;
44
53
  }
45
- throw new e(0, "network_error", navigator.onLine === !1 ? "You appear to be offline. Your draft is saved; try again in a moment." : "Couldn't reach CommentKit. Check your connection and try again.");
46
- }
47
- if (h.status === 401 && !c.anonymous && !d && (d = !0, await this.tokens.recover())) continue;
48
- if (t.has(h.status) && p < 3 && (l === "GET" || u) && !await i(h)) {
49
- let e = Number(h.headers.get("Retry-After"));
50
- await o(Number.isFinite(e) && e > 0 ? Math.min(e, 30) * 1e3 : a(p));
51
- continue;
52
- }
53
- if (h.status === 409 && u && f < n && h.headers.has("Retry-After") && (await h.clone().json().catch(() => null))?.code === "idempotency_in_progress") {
54
- f++;
55
- let e = Number(h.headers.get("Retry-After"));
56
- await o((Number.isFinite(e) && e > 0 ? Math.min(e, 5) * 1e3 : 1e3) + Math.random() * 250);
57
- continue;
54
+ throw new n(0, "network_error", navigator.onLine === !1 ? "You appear to be offline. Your draft is saved; try again in a moment." : "Couldn't reach CommentKit. Check your connection and try again.");
55
+ } finally {
56
+ y.done();
58
57
  }
59
- if ((h.ok || h.status === 304) && c.onETag?.(h.headers.get("ETag")), h.status === 204 || h.status === 304) return;
60
- let g = await h.text(), _ = g ? s(g) : null;
61
- if (!h.ok) {
62
- let t = _ ?? {};
63
- throw new e(h.status, t.code ?? `http_${h.status}`, t.detail ?? t.title ?? "Something went wrong. Please try again.", t.errors);
58
+ let S = x ? u(x) : null, C = S ?? {};
59
+ if (this.tokens.seen?.(b, C.code) && !m) m = !0;
60
+ else if (!(b.status === 401 && !o.anonymous && !p && (p = !0, await this.tokens.recover()))) {
61
+ if (r.has(b.status) && e < 3 && (d === "GET" || f) && C.code !== "widget_disabled") {
62
+ let t = Number(b.headers.get("Retry-After"));
63
+ await l(Number.isFinite(t) && t > 0 ? Math.min(t, 30) * 1e3 : c(e));
64
+ } else if (b.status === 409 && f && h < a && b.headers.has("Retry-After") && C.code === "idempotency_in_progress") {
65
+ h++;
66
+ let e = Number(b.headers.get("Retry-After"));
67
+ await l((Number.isFinite(e) && e > 0 ? Math.min(e, 5) * 1e3 : 1e3) + Math.random() * 250);
68
+ } else {
69
+ if ((b.ok || b.status === 304) && o.onETag?.(b.headers.get("ETag")), b.status === 204 || b.status === 304) return;
70
+ if (!b.ok) throw new n(b.status, C.code ?? `http_${b.status}`, C.detail ?? C.title ?? "Something went wrong. Please try again.", C.errors);
71
+ return S;
72
+ }
64
73
  }
65
- return _;
66
74
  }
67
75
  }
68
76
  url(e, t) {
@@ -71,68 +79,69 @@ var e = class extends Error {
71
79
  return n.toString();
72
80
  }
73
81
  };
74
- async function i(e) {
75
- return e.status === 503 && (await e.clone().json().catch(() => null))?.code === "widget_disabled";
82
+ function s(e, t) {
83
+ let n = new AbortController(), r = () => n.abort(), i = setTimeout(r, e);
84
+ return t?.aborted && r(), t?.addEventListener("abort", r), {
85
+ signal: n.signal,
86
+ done() {
87
+ clearTimeout(i), t?.removeEventListener("abort", r);
88
+ }
89
+ };
76
90
  }
77
- function a(e) {
91
+ function c(e) {
78
92
  return Math.min(8e3, 400 * 2 ** e) + Math.random() * 250;
79
93
  }
80
- function o(e) {
94
+ function l(e) {
81
95
  return new Promise((t) => setTimeout(t, e));
82
96
  }
83
- function s(e) {
97
+ function u(e) {
84
98
  try {
85
99
  return JSON.parse(e);
86
100
  } catch {
87
101
  return null;
88
102
  }
89
103
  }
90
- //#endregion
91
- //#region src/core/pkce.ts
92
- function c(e) {
93
- let t = "";
94
- for (let n of e) t += String.fromCharCode(n);
95
- return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
96
- }
97
- function l(e = 32) {
98
- let t = new Uint8Array(e);
99
- return crypto.getRandomValues(t), c(t);
100
- }
101
- async function u(e) {
102
- let t = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(e));
103
- return c(new Uint8Array(t));
104
- }
105
- //#endregion
106
- //#region src/core/auth.ts
107
- function d(e = navigator.userAgent) {
108
- return /FBAN|FBAV|Instagram|Line\/|WhatsApp|Snapchat|LinkedInApp|Slack|Twitter|MicroMessenger|; wv\)|GSA\//i.test(e);
109
- }
110
- var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h = 1e4, g = class {
104
+ // src/core/auth.ts
105
+ var d = 1e4, f = /^[A-Za-z0-9]{16,128}$/, p = class {
111
106
  constructor(e, t, n = location.origin) {
112
- this.endpoint = e, this.projectKey = t, this.origin = n, this.access = null, this.refreshing = null, this.listeners = /* @__PURE__ */ new Set(), this.remember = !0, this.authorize = null;
107
+ this.endpoint = e, this.projectKey = t, this.origin = n, this.access = null, this.held = null, this.dpop = null, this.refreshing = null, this.handoff = !1, this.renewing = null, this.listeners = /* @__PURE__ */ new Set(), this.renew = async () => !1, this.remember = !0, this.authorize = null, this.signInLink = null, this.finishLink = null, this.onStorage = (e) => {
108
+ e.key && e.key !== this.storageKey || this.readRefresh() || this.clear();
109
+ }, addEventListener("storage", this.onStorage);
110
+ }
111
+ destroy() {
112
+ removeEventListener("storage", this.onStorage);
113
113
  }
114
114
  get storageKey() {
115
115
  return `commentkit:v1:${this.projectKey}`;
116
116
  }
117
117
  hasSession() {
118
- return this.access !== null || this.readRefresh() !== null;
118
+ return (this.access?.expiresAt ?? 0) > Date.now() || this.readRefresh() !== null;
119
119
  }
120
120
  onChange(e) {
121
121
  return this.listeners.add(e), () => this.listeners.delete(e);
122
122
  }
123
123
  async accessToken() {
124
- return this.access && this.access.expiresAt - 3e4 > Date.now() ? this.access.token : this.readRefresh() ? await this.refresh() ? this.access?.token ?? null : null : this.pending()?.token ?? null;
124
+ return this.access && this.access.expiresAt - 3e4 > Date.now() ? this.access.token : !this.handoff && !this.readRefresh() ? this.pending()?.token ?? null : await this.recover() ? this.access?.token ?? null : null;
125
125
  }
126
126
  async recover() {
127
- return this.access = null, this.refresh();
127
+ if (this.access = null, !this.handoff) return this.refresh();
128
+ let e = await (this.renewing ?? (this.renewing = this.renew().finally(() => this.renewing = null)));
129
+ return e || this.clear(!0), e;
130
+ }
131
+ takeSignInLink(e) {
132
+ return e !== null && (f.test(e) && (this.signInLink = e), this.signInLink !== null);
133
+ }
134
+ async proof(e, t, n) {
135
+ return (this.dpop ?? (this.dpop = await import("./dpop-U48QIQd7.js"))).proof(this.projectKey, e, t, n);
128
136
  }
129
- async signIn(e = {}) {
130
- let t = l(48), n = l(24), r = await u(t), i = e.mode ?? "auto", a = i === "redirect" || i === "auto" && d(), o = this.authorizeUrl(r, n, e.share ?? null, a ? location.href : null);
131
- if (a) return this.redirect(o, t, n);
132
- let s = window.open(o.toString(), "commentkit-signin", f);
133
- if (!s) return i === "auto" ? this.redirect(this.authorizeUrl(r, n, e.share ?? null, location.href), t, n) : "popup_blocked";
134
- let c = await this.awaitCode(s, n);
135
- return c ? await this.exchange(c, t) ? "signed_in" : "failed" : "cancelled";
137
+ seen(e, t) {
138
+ return !!this.dpop?.seen(e, t);
139
+ }
140
+ hasSignInLink() {
141
+ return this.signInLink !== null;
142
+ }
143
+ signIn(e = {}) {
144
+ return import("./signin-Dd1rb2BP.js").then((t) => t.signIn(this, e));
136
145
  }
137
146
  async exchange(e, t) {
138
147
  try {
@@ -147,66 +156,14 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
147
156
  }
148
157
  }
149
158
  async completeRedirect() {
150
- let e = new URL(location.href), t = e.searchParams.get("ck_code"), n = e.searchParams.get("ck_state");
151
- if (!t || !n) return !1;
152
- e.searchParams.delete("ck_code"), e.searchParams.delete("ck_state"), history.replaceState(history.state, "", e.toString());
153
- let r = null;
154
- try {
155
- r = JSON.parse(sessionStorage.getItem(this.pkceKey) ?? "null"), sessionStorage.removeItem(this.pkceKey);
156
- } catch {
157
- return !1;
158
- }
159
- return !r?.verifier || r.state !== n ? !1 : this.exchange(t, r.verifier);
160
- }
161
- get pkceKey() {
162
- return `commentkit:pkce:${this.projectKey}`;
163
- }
164
- authorizeUrl(e, t, n, r) {
165
- let i = new URL(this.authorize ?? "/widget/connect", this.endpoint);
166
- return i.search = new URLSearchParams({
167
- key: this.projectKey,
168
- origin: this.origin,
169
- code_challenge: e,
170
- code_challenge_method: "S256",
171
- state: t,
172
- ...n ? { share: n } : {},
173
- ...r ? { return_to: r } : {}
174
- }).toString(), i;
175
- }
176
- redirect(e, t, n) {
177
- try {
178
- sessionStorage.setItem(this.pkceKey, JSON.stringify({
179
- verifier: t,
180
- state: n
181
- }));
182
- } catch {
183
- return "failed";
184
- }
185
- return location.assign(e.toString()), "redirecting";
159
+ return /[?&]ck_code=/.test(location.search) && import("./signin-Dd1rb2BP.js").then((e) => e.completeRedirect(this));
186
160
  }
187
161
  async signOut() {
188
162
  let e = this.readRefresh();
189
- this.clear(), e && await this.post("/oauth/revoke", { refresh_token: e }).catch(() => void 0);
190
- }
191
- awaitCode(e, t) {
192
- let n = [new URL(this.endpoint).origin, new URL(this.authorize ?? this.endpoint).origin];
193
- return new Promise((r) => {
194
- let i = !1, a = Date.now(), o = (e) => {
195
- i || (i = !0, window.removeEventListener("message", s), clearInterval(l), r(e));
196
- }, s = (e) => {
197
- if (!n.includes(e.origin)) return;
198
- let r = e.data;
199
- r?.type === "commentkit:auth" && r.state === t && typeof r.code == "string" && o(r.code);
200
- };
201
- window.addEventListener("message", s);
202
- let c = null, l = setInterval(() => {
203
- if (Date.now() - a > m) return o(null);
204
- e.closed && (c ?? (c = Date.now())), c !== null && Date.now() - c > 1e3 && o(null);
205
- }, p);
206
- });
163
+ this.clear(), e && await this.post("/oauth/revoke", { refresh_token: e }, !1).catch(() => void 0), await this.dpop?.forget(this.projectKey);
207
164
  }
208
165
  refresh() {
209
- return this.refreshing ?? (this.refreshing = _(`commentkit:refresh:${this.projectKey}`, async () => {
166
+ return this.refreshing ?? (this.refreshing = m(`commentkit:refresh:${this.projectKey}`, async () => {
210
167
  let e = this.readRefresh();
211
168
  if (!e) return !1;
212
169
  try {
@@ -221,7 +178,7 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
221
178
  400,
222
179
  401,
223
180
  403
224
- ].includes(e.status ?? 0) && this.clear(), !1;
181
+ ].includes(e.status ?? 0) && this.clear(!0), !1;
225
182
  }
226
183
  }).finally(() => this.refreshing = null)), this.refreshing;
227
184
  }
@@ -259,12 +216,12 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
259
216
  this.access = {
260
217
  token: e.access_token,
261
218
  expiresAt: Date.now() + e.expires_in * 1e3
262
- }, this.write(e.refresh_token), t || this.emit(!0);
219
+ }, this.handoff = !e.refresh_token, this.handoff || this.write(e.refresh_token), t || this.emit(!0);
263
220
  }
264
221
  write(e) {
265
- this.stores().forEach((t, n) => {
222
+ this.held = e && this.dpop?.volatile(this.projectKey) ? e : null, this.stores().forEach((t, n) => {
266
223
  try {
267
- e && !n ? t.setItem(this.storageKey, JSON.stringify({ refresh_token: e })) : t.removeItem(this.storageKey);
224
+ e && !n && !this.held ? t.setItem(this.storageKey, JSON.stringify({ refresh_token: e })) : t.removeItem(this.storageKey);
268
225
  } catch {}
269
226
  });
270
227
  }
@@ -275,74 +232,79 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
275
232
  return [];
276
233
  }
277
234
  }
278
- clear() {
279
- let e = this.hasSession();
280
- this.access = null, this.write(null), e && this.emit(!1);
235
+ clear(e) {
236
+ let t = !!this.access || this.handoff || this.hasSession();
237
+ this.access = null, this.handoff = !1, this.write(null), this.dpop?.drop(this.projectKey), t && this.emit(!1, e);
281
238
  }
282
239
  readRefresh() {
240
+ if (this.held) return this.held;
283
241
  for (let e of this.stores()) try {
284
242
  let t = JSON.parse(e.getItem(this.storageKey) ?? "null")?.refresh_token;
285
243
  if (typeof t == "string") return t;
286
244
  } catch {}
287
245
  return null;
288
246
  }
289
- emit(e) {
290
- for (let t of [...this.listeners]) t(e);
291
- }
292
- async post(e, t) {
293
- let n = new AbortController(), r = setTimeout(() => n.abort(), h);
294
- try {
295
- let r = await fetch(new URL(`/api/v1${e}`, this.endpoint), {
296
- method: "POST",
297
- headers: {
298
- "Content-Type": "application/json",
299
- Accept: "application/json"
300
- },
301
- body: JSON.stringify(t),
302
- credentials: "omit",
303
- signal: n.signal
304
- });
305
- if (r.status === 204) return;
306
- let i = await r.json().catch((e) => {
307
- if (n.signal.aborted) throw e;
308
- return null;
309
- });
310
- if (!r.ok) throw Object.assign(/* @__PURE__ */ Error("auth_failed"), {
311
- status: r.status,
312
- data: i
313
- });
314
- return i;
315
- } finally {
316
- clearTimeout(r);
247
+ emit(e, t) {
248
+ for (let n of [...this.listeners]) n(e, t);
249
+ }
250
+ async post(e, t, n = !0) {
251
+ let r = new URL(`/api/v1${e}`, this.endpoint).href;
252
+ for (let e = n;; e = !1) {
253
+ let i = {
254
+ "Content-Type": "application/json",
255
+ Accept: "application/json"
256
+ };
257
+ n && (i.DPoP = await this.proof("POST", r));
258
+ let a = s(d);
259
+ try {
260
+ let n = await fetch(r, {
261
+ method: "POST",
262
+ headers: i,
263
+ body: JSON.stringify(t),
264
+ credentials: "omit",
265
+ signal: a.signal
266
+ });
267
+ if (n.status === 204) return;
268
+ let o = await n.json().catch((e) => {
269
+ if (a.signal.aborted) throw e;
270
+ return null;
271
+ });
272
+ if (this.seen(n, o?.code) && e) continue;
273
+ if (!n.ok) throw Object.assign(/* @__PURE__ */ Error("auth_failed"), {
274
+ status: n.status,
275
+ data: o
276
+ });
277
+ return o;
278
+ } finally {
279
+ a.done();
280
+ }
317
281
  }
318
282
  }
319
283
  };
320
- function _(e, t) {
284
+ function m(e, t) {
321
285
  let n = typeof navigator > "u" ? void 0 : navigator.locks;
322
- return n?.request ? n.request(e, t) : import("./lock-C82sC17d.js").then((n) => n.storageLock(e, t));
286
+ return n?.request ? n.request(e, t) : import("./lock-B1JLlOhz.js").then((n) => n.storageLock(e, t));
323
287
  }
324
- //#endregion
325
- //#region src/core/contract.ts
326
- var v = /^pk_(live|test)_[a-z0-9]{20,40}$/, y = /^[A-Za-z0-9._/\-]+$/, b = /^[0-9a-f]{7,64}$/, x = /^[\p{L}\p{N}_.\/\\@+~()[\]{}$%,=!&#-]+(?::\d+){0,2}$/u, S = /^[\p{L}\p{N}_.:\/-]+$/u, C = /* @__PURE__ */ new Set();
327
- function w(e, t, n, r) {
328
- return t == null || t === "" ? null : t.length <= r && n.test(t) ? t : (C.has(e) || (C.add(e), console.warn(`[CommentKit] Left out ${e} "${t.slice(0, 80)}": it has characters CommentKit does not accept. See https://commentkit.dev/docs/errors#validation_failed`)), null);
288
+ // src/core/contract.ts
289
+ var h = /^pk_(live|test)_[a-z0-9]{20,40}$/, g = /^[A-Za-z0-9._/\-]+$/, _ = 2e3, v = /^[0-9a-f]{7,64}$/, y = /^[\p{L}\p{N}_.\/\\@+~()[\]{}$%,=!&#-]+(?::\d+){0,2}$/u, b = /^[\p{L}\p{N}_.:\/-]+$/u, x = /* @__PURE__ */ new Set();
290
+ function S(e, t, n, r) {
291
+ return t == null || t === "" ? null : t.length <= r && n.test(t) ? t : (x.has(e) || (x.add(e), console.warn(`[CommentKit] Left out ${e} "${t.slice(0, 80)}": it has characters CommentKit does not accept. See https://commentkit.dev/docs/errors#validation_failed`)), null);
329
292
  }
330
- function T(e, t) {
293
+ function C(e, t) {
331
294
  return {
332
- branch: w("branch", e?.trim(), y, 255),
333
- commit: w("commit", t?.trim().toLowerCase(), b, 64)
295
+ branch: S("branch", e?.trim(), g, 255),
296
+ commit: S("commit", t?.trim().toLowerCase(), v, 64)
334
297
  };
335
298
  }
336
- function E(e) {
299
+ function w(e) {
337
300
  return {
338
301
  ...e,
339
- source: w("data-ck-src", e.source, x, 500),
340
- ck_id: w("data-ck-id", e.ck_id, S, 200)
302
+ source: S("data-ck-src", e.source, y, 500),
303
+ ck_id: S("data-ck-id", e.ck_id, b, 200)
341
304
  };
342
305
  }
343
- //#endregion
344
- //#region src/core/store.ts
345
- function D(e) {
306
+ // src/core/store.ts
307
+ function T(e) {
346
308
  let t = e, n = /* @__PURE__ */ new Set();
347
309
  return {
348
310
  get: () => t,
@@ -365,49 +327,75 @@ function D(e) {
365
327
  }
366
328
  };
367
329
  }
368
- //#endregion
369
- //#region src/core/client.ts
370
- var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M = (e) => e ? `${e.project.id}|${e.person.type}:${e.person.id}|${e.role}` : null, N = class {
330
+ // src/core/client.ts
331
+ var E = "2.0.0", D = "https://commentkit.dev", O = { of: (e) => e }, k = (e) => void (O = e), A = () => O.url?.() ?? location.href;
332
+ function j() {
333
+ return {
334
+ config: null,
335
+ session: null,
336
+ threads: [],
337
+ loadingThreads: !1,
338
+ moreThreads: !1,
339
+ allBranches: !1,
340
+ unread: 0,
341
+ online: !0,
342
+ error: null,
343
+ here: 0,
344
+ opening: null,
345
+ request: null,
346
+ live: "off",
347
+ theme: null,
348
+ team: []
349
+ };
350
+ }
351
+ function M(e) {
352
+ if (!e?.email) return e;
353
+ let { email: t, ...n } = e;
354
+ return n;
355
+ }
356
+ var N = "edit_conflict", P = 20, F = (e) => e ? `${e.project.id}|${e.person.type}:${e.person.id}|${e.role}` : null, I = class {
371
357
  constructor(e) {
372
- this.options = e, this.listeners = /* @__PURE__ */ new Map(), this.realtime = null, this.live = null, this.attempts = 0, this.configTag = null, this.tags = /* @__PURE__ */ new Map(), this.previews = /* @__PURE__ */ new Map(), this.unreadKnown = !1, this.conversations = /* @__PURE__ */ new Map();
373
- let t = (e.endpoint ?? k).replace(/\/+$/, "");
374
- this.endpoint = t, this.auth = new g(t, e.project), this.api = new r(t, this.auth, O), this.state = D({
375
- config: null,
376
- session: null,
377
- threads: [],
378
- loadingThreads: !1,
379
- allBranches: !1,
380
- unread: 0,
358
+ this.options = e, this.listeners = /* @__PURE__ */ new Map(), this.realtime = null, this.live = null, this.attempts = 0, this.configTag = null, this.tags = /* @__PURE__ */ new Map(), this.previews = /* @__PURE__ */ new Map(), this.unreadKnown = !1, this.conversations = /* @__PURE__ */ new Map(), this.failedEvents = /* @__PURE__ */ new Set(), this.pageLoad = 0, this.threadsPage = null, this.changed = /* @__PURE__ */ new Map();
359
+ let t = (e.endpoint ?? "https://commentkit.dev").replace(/\/+$/, "");
360
+ this.endpoint = t, this.auth = new p(t, e.project), this.api = new o(t, this.auth, E), this.state = T({
361
+ ...j(),
381
362
  online: typeof navigator > "u" || navigator.onLine,
382
- error: null,
383
- here: 0,
384
- opening: null,
385
- request: null,
386
- live: "off",
387
- theme: F(),
388
- team: []
363
+ theme: z()
389
364
  }), this.auth.onChange((e) => {
390
- e || (this.unreadKnown = !1, this.state.set({
365
+ e || (this.unreadKnown = !1, this.prefetchSignIn(), this.state.set({
391
366
  session: null,
392
367
  threads: [],
393
368
  unread: 0
394
- }));
369
+ }), this.emit("session:change", { session: null }));
395
370
  }), this.offSession = this.state.subscribe((e) => {
396
- this.live && M(e.session) !== this.live.key && this.disconnectRealtime();
371
+ this.live && F(e.session) !== this.live.key && this.disconnectRealtime();
397
372
  });
398
373
  }
399
374
  get assets() {
400
375
  return `${this.endpoint}/cdn`;
401
376
  }
402
377
  reportCrash(e, t = "crash") {
403
- import("./report-DoDVi1qT.js").then((n) => n.reportCrash(this.endpoint, this.options.project, O, e, t, this.reportNonce)).catch(() => void 0);
378
+ import("./report-CrIqQZDI.js").then((n) => n.reportCrash(this.endpoint, this.options.project, E, e, t, this.reportNonce, this.auth)).catch(() => void 0);
404
379
  }
405
380
  on(e, t) {
406
381
  let n = this.listeners.get(e) ?? /* @__PURE__ */ new Set();
407
382
  return n.add(t), this.listeners.set(e, n), () => n.delete(t);
408
383
  }
409
384
  emit(e, t) {
410
- for (let n of this.listeners.get(e) ?? []) n(t);
385
+ let n = "session" in t ? {
386
+ ...t,
387
+ session: M(t.session)
388
+ } : t;
389
+ this.listeners.get(e)?.forEach((t) => {
390
+ try {
391
+ t(n);
392
+ } catch (t) {
393
+ this.failedEvents.has(e) || console.error(`[CommentKit] A "${e}" listener threw.`, t), this.failedEvents.add(e);
394
+ }
395
+ });
396
+ }
397
+ prefetchSignIn() {
398
+ import("./signin-Dd1rb2BP.js").catch(() => void 0);
411
399
  }
412
400
  async loadConfig() {
413
401
  let e = this.state.get().config, t = { "X-CommentKit-Load": this.configTag === null ? "1" : "0" };
@@ -421,43 +409,62 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
421
409
  return this.configTag = n ?? "", !r || (r.reporting?.nonce && (this.reportNonce = r.reporting.nonce), delete r.reporting, this.auth.configure(r.auth), e && JSON.stringify(e) === JSON.stringify(r)) ? e : (this.state.set({ config: r }), r);
422
410
  }
423
411
  async loadSession() {
424
- if (!this.auth.hasSession()) return this.state.set({ session: null }), null;
412
+ if (!this.auth.hasSession()) return this.prefetchSignIn(), this.state.set({ session: null }), null;
425
413
  try {
426
414
  let e = await this.api.request("/me");
427
415
  return this.state.set({ session: e }), this.emit("session:change", { session: e }), e;
428
- } catch (t) {
429
- if (t instanceof e && [401, 403].includes(t.status)) return this.state.set({ session: null }), null;
430
- throw t;
416
+ } catch (e) {
417
+ if (e instanceof n && [401, 403].includes(e.status)) return this.prefetchSignIn(), this.state.set({ session: null }), null;
418
+ throw e;
431
419
  }
432
420
  }
433
421
  async signIn(e) {
434
- let t = await this.auth.signIn({
435
- share: e,
436
- mode: this.options.auth
437
- });
438
- return t === "signed_in" && await this.loadSession(), t;
422
+ try {
423
+ let t = await this.auth.signIn({
424
+ share: e,
425
+ mode: this.options.auth
426
+ });
427
+ return t === "signed_in" && await this.loadSession(), t;
428
+ } catch (e) {
429
+ return e instanceof n && this.emit("error", { error: e }), "failed";
430
+ }
439
431
  }
440
432
  async signOut() {
441
433
  await this.auth.signOut(), this.state.set({
442
434
  session: null,
443
435
  threads: []
444
- }), this.emit("session:change", { session: null });
436
+ });
445
437
  }
446
- async loadPageThreads(e = L()) {
447
- this.state.set({ loadingThreads: !0 });
438
+ async loadPageThreads(e = A()) {
439
+ let t = ++this.pageLoad;
440
+ this.changed.clear();
441
+ let n = this.pageKey(), r = this.pageKey(e);
442
+ this.threadsPage !== r && this.state.set({
443
+ threads: [],
444
+ moreThreads: !1
445
+ }), this.threadsPage = r, this.state.set({ loadingThreads: !0 });
446
+ let i = /* @__PURE__ */ new Map(), a;
448
447
  try {
449
- let t = await this.api.request("/threads", { query: {
450
- page_url: e,
451
- status: "active",
452
- limit: 100,
453
- branch: this.branchFilter()
454
- } });
448
+ do {
449
+ let r = await this.api.request("/threads", { query: {
450
+ page_url: e,
451
+ status: "active",
452
+ limit: 100,
453
+ branch: this.branchFilter(),
454
+ cursor: a
455
+ } });
456
+ if (t !== this.pageLoad || n !== this.pageKey()) return [...i.values()];
457
+ let o = i.size;
458
+ r.data.forEach((e) => i.set(e.id, e)), a = i.size > o ? r.next_cursor : null;
459
+ } while (a && i.size < 500);
460
+ O.learn?.(e, i.values().next().value?.page.key), this.threadsPage = this.pageKey(e);
461
+ let r = [...[...this.changed].flatMap(([e, t]) => (i.delete(e), t ? [t] : [])), ...i.values()];
455
462
  return this.state.set({
456
- threads: t.data,
457
- loadingThreads: !1
458
- }), t.data;
459
- } catch (e) {
460
- throw this.state.set({ loadingThreads: !1 }), e;
463
+ threads: r,
464
+ moreThreads: !!a
465
+ }), r;
466
+ } finally {
467
+ t === this.pageLoad && this.state.set({ loadingThreads: !1 });
461
468
  }
462
469
  }
463
470
  branchFilter() {
@@ -483,22 +490,22 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
483
490
  return this.tag(r.id, n), r;
484
491
  }
485
492
  tag(e, t) {
486
- t ? this.tags.set(e, t) : this.tags.delete(e);
493
+ t ? L(this.tags, e, t, 500) : this.tags.delete(e);
487
494
  }
488
495
  async createThread(e) {
489
- let t = T(this.options.branch, this.options.commit), n = await this.thread("/threads", {
496
+ let t = C(this.options.branch, this.options.commit), n = await this.thread("/threads", {
490
497
  method: "POST",
491
498
  idempotent: !0,
492
499
  idempotencyKey: e.idempotencyKey,
493
500
  body: {
494
501
  page: {
495
- url: L(),
502
+ url: A(),
496
503
  title: document.title.slice(0, 500) || null,
497
504
  branch: t.branch,
498
505
  commit_sha: t.commit,
499
506
  deployment_url: this.options.deploymentUrl ?? null
500
507
  },
501
- anchor: E(e.anchor),
508
+ anchor: w(e.anchor),
502
509
  breakpoint: innerWidth,
503
510
  body: e.body,
504
511
  capture: e.capture ?? null,
@@ -510,12 +517,12 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
510
517
  ...e.styleChanges?.length ? { style_changes: e.styleChanges } : {}
511
518
  }
512
519
  });
513
- return this.upsert(n), this.emit("thread:created", { thread: n }), n;
520
+ return O.learn?.(n.page.url, n.page.key), this.upsert(n), this.emit("thread:created", { thread: n }), n;
514
521
  }
515
522
  async updateThread(e, t) {
516
523
  let n = t.description === void 0 ? void 0 : this.tags.get(e), r = t.anchor ? {
517
524
  ...t,
518
- anchor: E(t.anchor)
525
+ anchor: w(t.anchor)
519
526
  } : t, i = await this.edit(e, () => this.thread(`/threads/${encodeURIComponent(e)}`, {
520
527
  method: "PATCH",
521
528
  body: r,
@@ -524,7 +531,7 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
524
531
  return this.upsert(i), this.emit("thread:updated", { thread: i }), i;
525
532
  }
526
533
  async deleteThread(e) {
527
- await this.api.request(`/threads/${encodeURIComponent(e)}`, { method: "DELETE" }), this.state.set((t) => ({ threads: t.threads.filter((t) => t.id !== e) }));
534
+ await this.edit(e, () => this.api.request(`/threads/${encodeURIComponent(e)}`, { method: "DELETE" }), () => this.refreshThread(e)), this.changed.set(e, null), this.state.set((t) => ({ threads: t.threads.filter((t) => t.id !== e) }));
528
535
  }
529
536
  async messages(e, t) {
530
537
  let n = await this.api.request(`/threads/${encodeURIComponent(e)}/messages`, { query: {
@@ -535,8 +542,8 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
535
542
  return n;
536
543
  }
537
544
  async allMessages(e, t = {}) {
538
- let n = t.refresh ? this.conversations.get(e) : void 0, r = n ? n.slice(0, -1) : [], i = n?.at(-1)?.cursor;
539
- for (let t = 0; t < j; t++) {
545
+ let n = t.refresh ? this.conversations.get(e) : void 0, r = n ? n.slice(0, -1) : [], i = n?.[n.length - 1]?.cursor;
546
+ for (let t = 0; t < P; t++) {
540
547
  let t = await this.messages(e, i);
541
548
  if (r.push({
542
549
  cursor: i,
@@ -545,15 +552,12 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
545
552
  }), !t.next_cursor) break;
546
553
  i = t.next_cursor;
547
554
  }
548
- this.conversations.delete(e), this.conversations.set(e, r);
549
- for (let e of this.conversations.keys()) {
550
- if (this.conversations.size <= 10) break;
551
- this.conversations.delete(e);
552
- }
553
- return r.flatMap((e) => e.data);
555
+ return L(this.conversations, e, r, 10), r.flatMap((e) => e.data);
554
556
  }
555
557
  forgetConversationOf(e) {
556
- for (let [t, n] of this.conversations) n.some((t) => t.data.some((t) => t.id === e)) && this.conversations.delete(t);
558
+ let t;
559
+ for (let [n, r] of this.conversations) r.some((t) => t.data.some((t) => t.id === e)) && (t = n, this.conversations.delete(n));
560
+ return t;
557
561
  }
558
562
  async reply(e, t, n = {}) {
559
563
  let { data: r } = await this.api.request(`/threads/${encodeURIComponent(e)}/messages`, {
@@ -570,8 +574,8 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
570
574
  return this.tag(r.id, r.etag), this.emit("message:created", { message: r }), this.refreshThread(e), r;
571
575
  }
572
576
  editMessage(e, t, n = {}) {
573
- let r = this.tags.get(e);
574
- return this.forgetConversationOf(e), this.edit(e, async () => {
577
+ let r = this.tags.get(e), i = this.forgetConversationOf(e);
578
+ return this.edit(e, async () => {
575
579
  let { data: i } = await this.api.request(`/messages/${encodeURIComponent(e)}`, {
576
580
  method: "PATCH",
577
581
  body: {
@@ -581,17 +585,18 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
581
585
  headers: r ? { "If-Match": r } : void 0
582
586
  });
583
587
  return this.tag(e, i.etag), i;
584
- });
588
+ }, i ? () => this.refreshThread(i) : void 0);
585
589
  }
586
- async edit(t, n, r) {
590
+ async edit(e, t, r) {
587
591
  try {
588
- return await n();
589
- } catch (n) {
590
- throw n instanceof e && n.code === "edit_conflict" && (this.tags.delete(t), await r?.()), n;
592
+ return await t();
593
+ } catch (t) {
594
+ throw t instanceof n && (t.code === "edit_conflict" || !t.status) && (this.tags.delete(e), await r?.()), t;
591
595
  }
592
596
  }
593
597
  deleteMessage(e) {
594
- return this.forgetConversationOf(e), this.api.request(`/messages/${encodeURIComponent(e)}`, { method: "DELETE" });
598
+ let t = this.forgetConversationOf(e);
599
+ return this.edit(e, () => this.api.request(`/messages/${encodeURIComponent(e)}`, { method: "DELETE" }), t ? () => this.refreshThread(t) : void 0);
595
600
  }
596
601
  react(e, t, n) {
597
602
  return this.forgetConversationOf(e), this.api.request(`/messages/${encodeURIComponent(e)}/reactions`, {
@@ -611,6 +616,7 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
611
616
  addChecklistItem(e, t) {
612
617
  return this.api.request(`/threads/${encodeURIComponent(e)}/checklist`, {
613
618
  method: "POST",
619
+ idempotent: !0,
614
620
  body: { body: t }
615
621
  }).then((e) => e.data);
616
622
  }
@@ -630,8 +636,7 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
630
636
  return this.api.request("/usage").then((e) => e.storage);
631
637
  }
632
638
  linkPreview(e) {
633
- let t = this.previews.get(e);
634
- return t || (t = this.api.request("/link-preview", { query: { url: e } }).then((e) => e?.data ?? null, () => null), this.previews.set(e, t)), t;
639
+ return this.previews.get(e) ?? L(this.previews, e, this.api.request("/link-preview", { query: { url: e } }).then((e) => e?.data ?? null, () => null), 100);
635
640
  }
636
641
  get inboxLoaded() {
637
642
  return this.unreadKnown;
@@ -660,15 +665,15 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
660
665
  this.unreadKnown = !0, this.state.set({ unread: t.unread });
661
666
  }
662
667
  upload(e, t, n = "file", r = {}) {
663
- return import("./upload-CzPAjktW.js").then((i) => i.upload(this.api, e, t, n, r));
668
+ return import("./upload-MgPYqPyE.js").then((i) => i.upload(this.api, e, t, n, r));
664
669
  }
665
670
  connectRealtime(e) {
666
- let t = this.state.get().config, n = this.state.get().session, r = M(n);
671
+ let t = this.state.get().config, n = this.state.get().session, r = F(n);
667
672
  if (!t?.features.realtime || !t.realtime || !n || !r) return Promise.resolve();
668
673
  if (this.live?.key === r) return this.live.ready;
669
674
  this.disconnectRealtime();
670
675
  let i = ++this.attempts, a = () => this.live?.attempt === i, o = (async () => {
671
- let { connect: r } = await import("./realtime-D22tI_AS.js").then((e) => e.t);
676
+ let { connect: r } = await import("./realtime-C79zF50C.js").then((e) => e.t);
672
677
  if (!a()) return;
673
678
  let i = await r({
674
679
  ...t.realtime,
@@ -709,46 +714,48 @@ var O = "1.5.1", k = "https://commentkit.dev", A = "edit_conflict", j = 20, M =
709
714
  reconnectRealtime() {
710
715
  this.realtime?.reconnect?.();
711
716
  }
712
- async refreshThread(t) {
717
+ async refreshThread(e) {
713
718
  try {
714
- this.upsert(await this.getThread(t));
715
- } catch (n) {
716
- n instanceof e && n.status === 404 && this.state.set((e) => ({ threads: e.threads.filter((e) => e.id !== t) }));
719
+ this.upsert(await this.getThread(e));
720
+ } catch (t) {
721
+ t instanceof n && t.status === 404 && (this.changed.set(e, null), this.state.set((t) => ({ threads: t.threads.filter((t) => t.id !== e) })));
717
722
  }
718
723
  }
724
+ pageKey(e = location.href) {
725
+ return O.of(e);
726
+ }
719
727
  upsert(e) {
720
728
  this.state.set((t) => {
721
- let n = e.page.url === L() && !["resolved", "closed"].includes(e.status), r = t.threads.filter((t) => t.id !== e.id);
729
+ let n = (e.page.key ?? O.of(e.page.url)) === this.pageKey() && !["resolved", "closed"].includes(e.status);
730
+ this.changed.set(e.id, n ? e : null);
731
+ let r = t.threads.filter((t) => t.id !== e.id);
722
732
  return { threads: n ? [e, ...r] : r };
723
733
  });
724
734
  }
725
735
  destroy() {
726
- this.offSession(), this.disconnectRealtime(), this.listeners.clear();
736
+ this.offSession(), this.disconnectRealtime(), this.auth.destroy(), this.listeners.clear();
737
+ }
738
+ };
739
+ function L(e, t, n, r) {
740
+ e.delete(t), e.set(t, n);
741
+ for (let t of e.keys()) {
742
+ if (e.size <= r) break;
743
+ e.delete(t);
727
744
  }
728
- }, P = "commentkit:theme";
729
- function F() {
745
+ return n;
746
+ }
747
+ var R = "commentkit:theme";
748
+ function z() {
730
749
  try {
731
- let e = typeof localStorage > "u" ? null : localStorage.getItem(P);
750
+ let e = typeof localStorage > "u" ? null : localStorage.getItem(R);
732
751
  return e === "auto" || e === "light" || e === "dark" ? e : null;
733
752
  } catch {
734
753
  return null;
735
754
  }
736
755
  }
737
- function I(e) {
756
+ function B(e) {
738
757
  try {
739
- e ? localStorage.setItem(P, e) : localStorage.removeItem(P);
758
+ e ? localStorage.setItem(R, e) : localStorage.removeItem(R);
740
759
  } catch {}
741
760
  }
742
- function L() {
743
- let e = new URL(location.href);
744
- for (let t of [
745
- "ck_thread",
746
- "ck",
747
- "ck_invite",
748
- "ck_code",
749
- "ck_state"
750
- ]) e.searchParams.delete(t);
751
- return e.hash && !e.hash.startsWith("#/") && !e.hash.startsWith("#!/") && (e.hash = ""), e.toString();
752
- }
753
- //#endregion
754
- export { I as a, T as c, l as d, r as f, L as i, g as l, A as n, v as o, e as p, O as r, x as s, N as t, u };
761
+ export { s as _, j as a, t as b, B as c, h as d, y as f, i as g, o as h, E as i, k as l, p as m, D as n, A as o, C as p, N as r, M as s, I as t, _ as u, n as v, e as y };