@commentkit/comment-kit 1.6.0 → 2.0.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 (137) hide show
  1. package/CHANGELOG.md +298 -590
  2. package/README.md +421 -145
  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 +789 -0
  10. package/dist/node/cli/format.js +54 -0
  11. package/dist/node/cli/init.js +686 -0
  12. package/dist/node/cli/main.js +537 -0
  13. package/dist/node/cli/mcp.js +295 -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/dashboard-places.js +33 -0
  29. package/dist/node/shared/endpoint.js +26 -0
  30. package/dist/node/shared/format.js +63 -0
  31. package/dist/node/types/astro.d.ts +11 -0
  32. package/dist/node/types/next.d.ts +34 -0
  33. package/dist/node/types/options.d.ts +59 -0
  34. package/dist/node/types/vite.d.ts +11 -0
  35. package/dist/node/types/webpack-loader.d.ts +22 -0
  36. package/dist/node/types/webpack.d.ts +27 -0
  37. package/dist/npm/THIRD_PARTY_NOTICES.txt +6 -6
  38. package/dist/npm/chunks/AgentSend-DOy3jhyF.js +68 -0
  39. package/dist/npm/chunks/Checklist-CW_SwMpS.js +282 -0
  40. package/dist/npm/chunks/Composer-Bveq5QrK.js +727 -0
  41. package/dist/npm/chunks/Guest-7hcN765o.js +270 -0
  42. package/dist/npm/chunks/MarkupEditor-6bJGpWGO.js +345 -0
  43. package/dist/npm/chunks/Panel-ChPUB68C.js +1211 -0
  44. package/dist/npm/chunks/Settings-CB5eEKnf.js +231 -0
  45. package/dist/npm/chunks/Shortcuts-eAyjJ89P.js +62 -0
  46. package/dist/npm/chunks/StyleEditor-CZTdAA5c.js +46 -0
  47. package/dist/npm/chunks/StylePanel-B4pHQUL9.js +199 -0
  48. package/dist/npm/chunks/TaskFields-C8BX0Sgg.js +289 -0
  49. package/dist/npm/chunks/ThreadCard-B1EsX3PR.js +1160 -0
  50. package/dist/npm/chunks/Viewer-DY6zb90M.js +114 -0
  51. package/dist/npm/chunks/{agent-BDMQtOzH.js → agent-MTQHr9ei.js} +9 -9
  52. package/dist/npm/chunks/{anchor-BsradQhn.js → anchor-BsQNVkHj.js} +108 -85
  53. package/dist/npm/chunks/{ar-8tPch6W4.js → ar-L5cQ-NPe.js} +71 -19
  54. package/dist/npm/chunks/card-bits-CTdrkYn8.js +53 -0
  55. package/dist/npm/chunks/{client-EFTWxHbJ.js → client-7eVxvg5j.js} +282 -290
  56. package/dist/npm/chunks/confetti-BIs5s14P.js +49 -0
  57. package/dist/npm/chunks/context-BuMl7A07.js +48 -0
  58. package/dist/npm/chunks/context-Cf6khm7A.js +20 -0
  59. package/dist/npm/chunks/{de-C63897uD.js → de-D0C-v6tP.js} +62 -18
  60. package/dist/npm/chunks/dialog-DvdIkCW0.js +3 -0
  61. package/dist/npm/chunks/dpop-0A76OPa9.js +96 -0
  62. package/dist/npm/chunks/drafts-B0ctr1g9.js +21 -0
  63. package/dist/npm/chunks/{es-CI2bSpvB.js → es-Bw4Dj9tN.js} +62 -18
  64. package/dist/npm/chunks/extension-DFVOvciD.js +62 -0
  65. package/dist/npm/chunks/{extension-BST6LH1q.js → extension-SmoH5AA4.js} +20 -12
  66. package/dist/npm/chunks/files-4lV9YQIX.js +114 -0
  67. package/dist/npm/chunks/{fr-rsVe-Lqk.js → fr-TkuQaIAa.js} +120 -76
  68. package/dist/npm/chunks/{hi-cvwbSaWP.js → hi-DXSH2U8d.js} +63 -19
  69. package/dist/npm/chunks/{i18n-C4v0M-6z.js → i18n-aVSu9TcJ.js} +136 -92
  70. package/dist/npm/chunks/identify-BGYh2dVo.js +106 -0
  71. package/dist/npm/chunks/{it-C8tnV0BF.js → it-CiWA_bch.js} +81 -37
  72. package/dist/npm/chunks/{ja-BWxDfC2f.js → ja-C-QmR3yz.js} +60 -16
  73. package/dist/npm/chunks/late-words-80_VWt8S.js +14 -0
  74. package/dist/npm/chunks/live-BExWHzQ7.js +85 -0
  75. package/dist/npm/chunks/locale-D8Q4Q2Nt.js +26 -0
  76. package/dist/npm/chunks/{nl-DNQ6eCht.js → nl-CEmZRqjX.js} +63 -19
  77. package/dist/npm/chunks/page-key-HSY3Lze5.js +62 -0
  78. package/dist/npm/chunks/{pick-CLyOdjnD.js → pick-B3sR7jtS.js} +45 -44
  79. package/dist/npm/chunks/pkce-CfZhiNBi.js +8 -0
  80. package/dist/npm/chunks/placement-BF_HOUDD.js +74 -0
  81. package/dist/npm/chunks/privacy-BWUGlHw5.js +57 -0
  82. package/dist/npm/chunks/{pt-BR-CkG7vpU2.js → pt-BR-b-a8sxO8.js} +62 -18
  83. package/dist/npm/chunks/{realtime-DIo-Wth4.js → realtime-C79zF50C.js} +14 -12
  84. package/dist/npm/chunks/report-DaVuoRYS.js +44 -0
  85. package/dist/npm/chunks/show-BoN8AC-F.js +97 -0
  86. package/dist/npm/chunks/signin-BOWr_WCo.js +75 -0
  87. package/dist/npm/chunks/{source-CS7C6wJA.js → source-mk3rGBBQ.js} +1 -1
  88. package/dist/npm/chunks/src-C8rZFyL_.js +215 -0
  89. package/dist/npm/chunks/start-CV4j78_q.js +1894 -0
  90. package/dist/npm/chunks/states-b9Bv3Ci7.js +3 -0
  91. package/dist/npm/chunks/upload-B2mkgMeJ.js +42 -0
  92. package/dist/npm/chunks/uploads-8AxyRq7g.js +1147 -0
  93. package/dist/npm/chunks/wrap-DW6EFbVQ.js +69 -0
  94. package/dist/npm/chunks/{zh-CN-BKuZOLBE.js → zh-CN-JTscW8u8.js} +61 -17
  95. package/dist/npm/element.js +58 -21
  96. package/dist/npm/headless.js +7 -3
  97. package/dist/npm/index.js +3 -3
  98. package/dist/npm/react.js +18 -11
  99. package/dist/npm/svelte.js +28 -12
  100. package/dist/npm/types/adapters/react.d.ts +4 -1
  101. package/dist/npm/types/adapters/svelte.d.ts +4 -1
  102. package/dist/npm/types/adapters/vue.d.ts +35 -2
  103. package/dist/npm/types/anchor/index.d.ts +19 -0
  104. package/dist/npm/types/capture/privacy.d.ts +14 -0
  105. package/dist/npm/types/core/api.d.ts +20 -1
  106. package/dist/npm/types/core/auth.d.ts +40 -21
  107. package/dist/npm/types/core/client.d.ts +60 -4
  108. package/dist/npm/types/core/page-key.d.ts +42 -0
  109. package/dist/npm/types/core/types.d.ts +87 -4
  110. package/dist/npm/types/element.d.ts +17 -7
  111. package/dist/npm/types/headless.d.ts +2 -1
  112. package/dist/npm/types/index.d.ts +16 -6
  113. package/dist/npm/vue.js +44 -9
  114. package/package.json +90 -14
  115. package/dist/npm/chunks/AgentSend-2JPDseOo.js +0 -63
  116. package/dist/npm/chunks/Checklist-BpDXQebo.js +0 -257
  117. package/dist/npm/chunks/Composer-CAyj-c8e.js +0 -677
  118. package/dist/npm/chunks/Guest-ZdFmTcXI.js +0 -187
  119. package/dist/npm/chunks/MarkupEditor-BFplZwFy.js +0 -332
  120. package/dist/npm/chunks/Panel-BhZ4XRDR.js +0 -861
  121. package/dist/npm/chunks/Settings-SPG9EHUZ.js +0 -204
  122. package/dist/npm/chunks/Shortcuts-C28HVDw6.js +0 -58
  123. package/dist/npm/chunks/StyleEditor-ByZMtq10.js +0 -43
  124. package/dist/npm/chunks/StylePanel-Iwhce2tE.js +0 -197
  125. package/dist/npm/chunks/TaskFields-BsP_fVJg.js +0 -273
  126. package/dist/npm/chunks/ThreadCard-BzVZdDCb.js +0 -1030
  127. package/dist/npm/chunks/Viewer-CqtGrdD8.js +0 -108
  128. package/dist/npm/chunks/card-bits-Bna-g3Py.js +0 -124
  129. package/dist/npm/chunks/confetti-DTKpwF3O.js +0 -40
  130. package/dist/npm/chunks/drafts-akdBsOd0.js +0 -50
  131. package/dist/npm/chunks/extension-D-V3utTm.js +0 -53
  132. package/dist/npm/chunks/live-BCpJVBeN.js +0 -33
  133. package/dist/npm/chunks/report-BBrT3r89.js +0 -27
  134. package/dist/npm/chunks/src-C4FsVoUp.js +0 -148
  135. package/dist/npm/chunks/start-i9M-r3aH.js +0 -1800
  136. package/dist/npm/chunks/upload-DSv7EyBD.js +0 -41
  137. package/dist/npm/chunks/uploads-I01kdqQF.js +0 -1103
@@ -1,9 +1,19 @@
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
+ }
1
11
  // src/core/types.ts
2
- var e = class extends Error {
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,85 +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
- // src/core/pkce.ts
91
- function c(e) {
92
- let t = "";
93
- for (let n of e) t += String.fromCharCode(n);
94
- return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
95
- }
96
- function l(e = 32) {
97
- let t = new Uint8Array(e);
98
- return crypto.getRandomValues(t), c(t);
99
- }
100
- async function u(e) {
101
- let t = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(e));
102
- return c(new Uint8Array(t));
103
- }
104
104
  // src/core/auth.ts
105
- function d(e = navigator.userAgent) {
106
- return /FBAN|FBAV|Instagram|Line\/|WhatsApp|Snapchat|LinkedInApp|Slack|Twitter|MicroMessenger|; wv\)|GSA\//i.test(e);
107
- }
108
- var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h = 1e4, g = "ck_signin", _ = /^[A-Za-z0-9]{16,128}$/, v = class {
105
+ var d = 1e4, f = /^[A-Za-z0-9]{16,128}$/, p = class {
109
106
  constructor(e, t, n = location.origin) {
110
- 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, this.signInLink = 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);
111
113
  }
112
114
  get storageKey() {
113
115
  return `commentkit:v1:${this.projectKey}`;
114
116
  }
115
117
  hasSession() {
116
- return this.access !== null || this.readRefresh() !== null;
118
+ return (this.access?.expiresAt ?? 0) > Date.now() || this.readRefresh() !== null;
117
119
  }
118
120
  onChange(e) {
119
121
  return this.listeners.add(e), () => this.listeners.delete(e);
120
122
  }
121
123
  async accessToken() {
122
- 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;
123
125
  }
124
126
  async recover() {
125
- 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;
126
130
  }
127
- takeSignInLink() {
128
- let e;
129
- try {
130
- e = new URL(location.href);
131
- } catch {
132
- return !1;
133
- }
134
- if (!e.searchParams.has("ck_signin")) return !1;
135
- let t = e.searchParams.get("ck_signin") ?? "";
136
- e.searchParams.delete(g);
137
- try {
138
- history.replaceState(history.state, "", e.toString());
139
- } catch {}
140
- return _.test(t) && (this.signInLink = t), this.signInLink !== null;
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-0A76OPa9.js"))).proof(this.projectKey, e, t, n);
136
+ }
137
+ seen(e, t) {
138
+ return !!this.dpop?.seen(e, t);
141
139
  }
142
140
  hasSignInLink() {
143
141
  return this.signInLink !== null;
144
142
  }
145
- async signIn(e = {}) {
146
- let t = l(48), n = l(24), r = await u(t), i = e.mode ?? "auto", a = i === "redirect" || i === "auto" && d(), o = this.signInLink, s = this.authorizeUrl(r, n, e.share ?? null, a ? location.href : null, o);
147
- if (a) return this.signInLink = null, this.redirect(s, t, n);
148
- let c = window.open(s.toString(), "commentkit-signin", f);
149
- if (!c) return i === "auto" ? (this.signInLink = null, this.redirect(this.authorizeUrl(r, n, e.share ?? null, location.href, o), t, n)) : "popup_blocked";
150
- this.signInLink = null;
151
- let p = await this.awaitCode(c, n);
152
- return p ? await this.exchange(p, t) ? "signed_in" : "failed" : "cancelled";
143
+ signIn(e = {}) {
144
+ return import("./signin-BOWr_WCo.js").then((t) => t.signIn(this, e));
153
145
  }
154
146
  async exchange(e, t) {
155
147
  try {
@@ -164,67 +156,14 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
164
156
  }
165
157
  }
166
158
  async completeRedirect() {
167
- let e = new URL(location.href), t = e.searchParams.get("ck_code"), n = e.searchParams.get("ck_state");
168
- if (!t || !n) return !1;
169
- e.searchParams.delete("ck_code"), e.searchParams.delete("ck_state"), history.replaceState(history.state, "", e.toString());
170
- let r = null;
171
- try {
172
- r = JSON.parse(sessionStorage.getItem(this.pkceKey) ?? "null"), sessionStorage.removeItem(this.pkceKey);
173
- } catch {
174
- return !1;
175
- }
176
- return !r?.verifier || r.state !== n ? !1 : this.exchange(t, r.verifier);
177
- }
178
- get pkceKey() {
179
- return `commentkit:pkce:${this.projectKey}`;
180
- }
181
- authorizeUrl(e, t, n, r, i = null) {
182
- let a = new URL(this.authorize ?? "/widget/connect", this.endpoint), o = i ? new URL("guest", a) : a;
183
- return o.search = new URLSearchParams({
184
- key: this.projectKey,
185
- origin: this.origin,
186
- code_challenge: e,
187
- code_challenge_method: "S256",
188
- state: t,
189
- ...n ? { share: n } : {},
190
- ...i ? { signin: i } : {},
191
- ...r ? { return_to: r } : {}
192
- }).toString(), o;
193
- }
194
- redirect(e, t, n) {
195
- try {
196
- sessionStorage.setItem(this.pkceKey, JSON.stringify({
197
- verifier: t,
198
- state: n
199
- }));
200
- } catch {
201
- return "failed";
202
- }
203
- return location.assign(e.toString()), "redirecting";
159
+ return /[?&]ck_code=/.test(location.search) && import("./signin-BOWr_WCo.js").then((e) => e.completeRedirect(this));
204
160
  }
205
161
  async signOut() {
206
162
  let e = this.readRefresh();
207
- this.clear(), e && await this.post("/oauth/revoke", { refresh_token: e }).catch(() => void 0);
208
- }
209
- awaitCode(e, t) {
210
- let n = [new URL(this.endpoint).origin, new URL(this.authorize ?? this.endpoint).origin];
211
- return new Promise((r) => {
212
- let i = !1, a = Date.now(), o = (e) => {
213
- i || (i = !0, window.removeEventListener("message", s), clearInterval(l), r(e));
214
- }, s = (e) => {
215
- if (!n.includes(e.origin)) return;
216
- let r = e.data;
217
- r?.type === "commentkit:auth" && r.state === t && typeof r.code == "string" && o(r.code);
218
- };
219
- window.addEventListener("message", s);
220
- let c = null, l = setInterval(() => {
221
- if (Date.now() - a > m) return o(null);
222
- e.closed && (c ?? (c = Date.now())), c !== null && Date.now() - c > 1e3 && o(null);
223
- }, p);
224
- });
163
+ this.clear(), e && await this.post("/oauth/revoke", { refresh_token: e }, !1).catch(() => void 0), await this.dpop?.forget(this.projectKey);
225
164
  }
226
165
  refresh() {
227
- return this.refreshing ?? (this.refreshing = y(`commentkit:refresh:${this.projectKey}`, async () => {
166
+ return this.refreshing ?? (this.refreshing = m(`commentkit:refresh:${this.projectKey}`, async () => {
228
167
  let e = this.readRefresh();
229
168
  if (!e) return !1;
230
169
  try {
@@ -239,7 +178,7 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
239
178
  400,
240
179
  401,
241
180
  403
242
- ].includes(e.status ?? 0) && this.clear(), !1;
181
+ ].includes(e.status ?? 0) && this.clear(!0), !1;
243
182
  }
244
183
  }).finally(() => this.refreshing = null)), this.refreshing;
245
184
  }
@@ -277,12 +216,12 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
277
216
  this.access = {
278
217
  token: e.access_token,
279
218
  expiresAt: Date.now() + e.expires_in * 1e3
280
- }, 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);
281
220
  }
282
221
  write(e) {
283
- this.stores().forEach((t, n) => {
222
+ this.held = e && this.dpop?.volatile(this.projectKey) ? e : null, this.stores().forEach((t, n) => {
284
223
  try {
285
- 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);
286
225
  } catch {}
287
226
  });
288
227
  }
@@ -293,72 +232,79 @@ var f = "popup=yes,width=460,height=680,left=120,top=80", p = 500, m = 6e5, h =
293
232
  return [];
294
233
  }
295
234
  }
296
- clear() {
297
- let e = this.hasSession();
298
- 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);
299
238
  }
300
239
  readRefresh() {
240
+ if (this.held) return this.held;
301
241
  for (let e of this.stores()) try {
302
242
  let t = JSON.parse(e.getItem(this.storageKey) ?? "null")?.refresh_token;
303
243
  if (typeof t == "string") return t;
304
244
  } catch {}
305
245
  return null;
306
246
  }
307
- emit(e) {
308
- for (let t of [...this.listeners]) t(e);
309
- }
310
- async post(e, t) {
311
- let n = new AbortController(), r = setTimeout(() => n.abort(), h);
312
- try {
313
- let r = await fetch(new URL(`/api/v1${e}`, this.endpoint), {
314
- method: "POST",
315
- headers: {
316
- "Content-Type": "application/json",
317
- Accept: "application/json"
318
- },
319
- body: JSON.stringify(t),
320
- credentials: "omit",
321
- signal: n.signal
322
- });
323
- if (r.status === 204) return;
324
- let i = await r.json().catch((e) => {
325
- if (n.signal.aborted) throw e;
326
- return null;
327
- });
328
- if (!r.ok) throw Object.assign(/* @__PURE__ */ Error("auth_failed"), {
329
- status: r.status,
330
- data: i
331
- });
332
- return i;
333
- } finally {
334
- 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
+ }
335
281
  }
336
282
  }
337
283
  };
338
- function y(e, t) {
284
+ function m(e, t) {
339
285
  let n = typeof navigator > "u" ? void 0 : navigator.locks;
340
286
  return n?.request ? n.request(e, t) : import("./lock-B1JLlOhz.js").then((n) => n.storageLock(e, t));
341
287
  }
342
288
  // src/core/contract.ts
343
- var b = /^pk_(live|test)_[a-z0-9]{20,40}$/, x = /^[A-Za-z0-9._/\-]+$/, S = /^[0-9a-f]{7,64}$/, C = /^[\p{L}\p{N}_.\/\\@+~()[\]{}$%,=!&#-]+(?::\d+){0,2}$/u, w = /^[\p{L}\p{N}_.:\/-]+$/u, T = /* @__PURE__ */ new Set();
344
- function E(e, t, n, r) {
345
- return t == null || t === "" ? null : t.length <= r && n.test(t) ? t : (T.has(e) || (T.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);
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);
346
292
  }
347
- function D(e, t) {
293
+ function C(e, t) {
348
294
  return {
349
- branch: E("branch", e?.trim(), x, 255),
350
- commit: E("commit", t?.trim().toLowerCase(), S, 64)
295
+ branch: S("branch", e?.trim(), g, 255),
296
+ commit: S("commit", t?.trim().toLowerCase(), v, 64)
351
297
  };
352
298
  }
353
- function O(e) {
299
+ function w(e) {
354
300
  return {
355
301
  ...e,
356
- source: E("data-ck-src", e.source, C, 500),
357
- ck_id: E("data-ck-id", e.ck_id, w, 200)
302
+ source: S("data-ck-src", e.source, y, 500),
303
+ ck_id: S("data-ck-id", e.ck_id, b, 200)
358
304
  };
359
305
  }
360
306
  // src/core/store.ts
361
- function k(e) {
307
+ function T(e) {
362
308
  let t = e, n = /* @__PURE__ */ new Set();
363
309
  return {
364
310
  get: () => t,
@@ -382,47 +328,74 @@ function k(e) {
382
328
  };
383
329
  }
384
330
  // src/core/client.ts
385
- var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P = (e) => e ? `${e.project.id}|${e.person.type}:${e.person.id}|${e.role}` : null, F = class {
331
+ var E = "2.0.1", 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 {
386
357
  constructor(e) {
387
- 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();
388
- let t = (e.endpoint ?? j).replace(/\/+$/, "");
389
- this.endpoint = t, this.auth = new v(t, e.project), this.api = new r(t, this.auth, A), this.state = k({
390
- config: null,
391
- session: null,
392
- threads: [],
393
- loadingThreads: !1,
394
- allBranches: !1,
395
- 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(),
396
362
  online: typeof navigator > "u" || navigator.onLine,
397
- error: null,
398
- here: 0,
399
- opening: null,
400
- request: null,
401
- live: "off",
402
- theme: L(),
403
- team: []
363
+ theme: z()
404
364
  }), this.auth.onChange((e) => {
405
- e || (this.unreadKnown = !1, this.state.set({
365
+ e || (this.unreadKnown = !1, this.prefetchSignIn(), this.state.set({
406
366
  session: null,
407
367
  threads: [],
408
368
  unread: 0
409
- }));
369
+ }), this.emit("session:change", { session: null }));
410
370
  }), this.offSession = this.state.subscribe((e) => {
411
- this.live && P(e.session) !== this.live.key && this.disconnectRealtime();
371
+ this.live && F(e.session) !== this.live.key && this.disconnectRealtime();
412
372
  });
413
373
  }
414
374
  get assets() {
415
375
  return `${this.endpoint}/cdn`;
416
376
  }
417
377
  reportCrash(e, t = "crash") {
418
- import("./report-BBrT3r89.js").then((n) => n.reportCrash(this.endpoint, this.options.project, A, e, t, this.reportNonce)).catch(() => void 0);
378
+ import("./report-DaVuoRYS.js").then((n) => n.reportCrash(this.endpoint, this.options.project, E, e, t, this.reportNonce, this.auth)).catch(() => void 0);
419
379
  }
420
380
  on(e, t) {
421
381
  let n = this.listeners.get(e) ?? /* @__PURE__ */ new Set();
422
382
  return n.add(t), this.listeners.set(e, n), () => n.delete(t);
423
383
  }
424
384
  emit(e, t) {
425
- 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-BOWr_WCo.js").catch(() => void 0);
426
399
  }
427
400
  async loadConfig() {
428
401
  let e = this.state.get().config, t = { "X-CommentKit-Load": this.configTag === null ? "1" : "0" };
@@ -436,43 +409,62 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
436
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);
437
410
  }
438
411
  async loadSession() {
439
- 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;
440
413
  try {
441
414
  let e = await this.api.request("/me");
442
415
  return this.state.set({ session: e }), this.emit("session:change", { session: e }), e;
443
- } catch (t) {
444
- if (t instanceof e && [401, 403].includes(t.status)) return this.state.set({ session: null }), null;
445
- 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;
446
419
  }
447
420
  }
448
421
  async signIn(e) {
449
- let t = await this.auth.signIn({
450
- share: e,
451
- mode: this.options.auth
452
- });
453
- 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
+ }
454
431
  }
455
432
  async signOut() {
456
433
  await this.auth.signOut(), this.state.set({
457
434
  session: null,
458
435
  threads: []
459
- }), this.emit("session:change", { session: null });
436
+ });
460
437
  }
461
- async loadPageThreads(e = z()) {
462
- 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;
463
447
  try {
464
- let t = await this.api.request("/threads", { query: {
465
- page_url: e,
466
- status: "active",
467
- limit: 100,
468
- branch: this.branchFilter()
469
- } });
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()];
470
462
  return this.state.set({
471
- threads: t.data,
472
- loadingThreads: !1
473
- }), t.data;
474
- } catch (e) {
475
- 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 });
476
468
  }
477
469
  }
478
470
  branchFilter() {
@@ -498,22 +490,22 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
498
490
  return this.tag(r.id, n), r;
499
491
  }
500
492
  tag(e, t) {
501
- t ? this.tags.set(e, t) : this.tags.delete(e);
493
+ t ? L(this.tags, e, t, 500) : this.tags.delete(e);
502
494
  }
503
495
  async createThread(e) {
504
- let t = D(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", {
505
497
  method: "POST",
506
498
  idempotent: !0,
507
499
  idempotencyKey: e.idempotencyKey,
508
500
  body: {
509
501
  page: {
510
- url: z(),
502
+ url: A(),
511
503
  title: document.title.slice(0, 500) || null,
512
504
  branch: t.branch,
513
505
  commit_sha: t.commit,
514
506
  deployment_url: this.options.deploymentUrl ?? null
515
507
  },
516
- anchor: O(e.anchor),
508
+ anchor: w(e.anchor),
517
509
  breakpoint: innerWidth,
518
510
  body: e.body,
519
511
  capture: e.capture ?? null,
@@ -525,12 +517,12 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
525
517
  ...e.styleChanges?.length ? { style_changes: e.styleChanges } : {}
526
518
  }
527
519
  });
528
- 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;
529
521
  }
530
522
  async updateThread(e, t) {
531
523
  let n = t.description === void 0 ? void 0 : this.tags.get(e), r = t.anchor ? {
532
524
  ...t,
533
- anchor: O(t.anchor)
525
+ anchor: w(t.anchor)
534
526
  } : t, i = await this.edit(e, () => this.thread(`/threads/${encodeURIComponent(e)}`, {
535
527
  method: "PATCH",
536
528
  body: r,
@@ -539,7 +531,7 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
539
531
  return this.upsert(i), this.emit("thread:updated", { thread: i }), i;
540
532
  }
541
533
  async deleteThread(e) {
542
- 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) }));
543
535
  }
544
536
  async messages(e, t) {
545
537
  let n = await this.api.request(`/threads/${encodeURIComponent(e)}/messages`, { query: {
@@ -550,8 +542,8 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
550
542
  return n;
551
543
  }
552
544
  async allMessages(e, t = {}) {
553
- let n = t.refresh ? this.conversations.get(e) : void 0, r = n ? n.slice(0, -1) : [], i = n?.at(-1)?.cursor;
554
- for (let t = 0; t < N; 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++) {
555
547
  let t = await this.messages(e, i);
556
548
  if (r.push({
557
549
  cursor: i,
@@ -560,15 +552,12 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
560
552
  }), !t.next_cursor) break;
561
553
  i = t.next_cursor;
562
554
  }
563
- this.conversations.delete(e), this.conversations.set(e, r);
564
- for (let e of this.conversations.keys()) {
565
- if (this.conversations.size <= 10) break;
566
- this.conversations.delete(e);
567
- }
568
- return r.flatMap((e) => e.data);
555
+ return L(this.conversations, e, r, 10), r.flatMap((e) => e.data);
569
556
  }
570
557
  forgetConversationOf(e) {
571
- 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;
572
561
  }
573
562
  async reply(e, t, n = {}) {
574
563
  let { data: r } = await this.api.request(`/threads/${encodeURIComponent(e)}/messages`, {
@@ -585,8 +574,8 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
585
574
  return this.tag(r.id, r.etag), this.emit("message:created", { message: r }), this.refreshThread(e), r;
586
575
  }
587
576
  editMessage(e, t, n = {}) {
588
- let r = this.tags.get(e);
589
- 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 () => {
590
579
  let { data: i } = await this.api.request(`/messages/${encodeURIComponent(e)}`, {
591
580
  method: "PATCH",
592
581
  body: {
@@ -596,17 +585,18 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
596
585
  headers: r ? { "If-Match": r } : void 0
597
586
  });
598
587
  return this.tag(e, i.etag), i;
599
- });
588
+ }, i ? () => this.refreshThread(i) : void 0);
600
589
  }
601
- async edit(t, n, r) {
590
+ async edit(e, t, r) {
602
591
  try {
603
- return await n();
604
- } catch (n) {
605
- 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;
606
595
  }
607
596
  }
608
597
  deleteMessage(e) {
609
- 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);
610
600
  }
611
601
  react(e, t, n) {
612
602
  return this.forgetConversationOf(e), this.api.request(`/messages/${encodeURIComponent(e)}/reactions`, {
@@ -626,6 +616,7 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
626
616
  addChecklistItem(e, t) {
627
617
  return this.api.request(`/threads/${encodeURIComponent(e)}/checklist`, {
628
618
  method: "POST",
619
+ idempotent: !0,
629
620
  body: { body: t }
630
621
  }).then((e) => e.data);
631
622
  }
@@ -645,8 +636,7 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
645
636
  return this.api.request("/usage").then((e) => e.storage);
646
637
  }
647
638
  linkPreview(e) {
648
- let t = this.previews.get(e);
649
- 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);
650
640
  }
651
641
  get inboxLoaded() {
652
642
  return this.unreadKnown;
@@ -675,15 +665,15 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
675
665
  this.unreadKnown = !0, this.state.set({ unread: t.unread });
676
666
  }
677
667
  upload(e, t, n = "file", r = {}) {
678
- return import("./upload-DSv7EyBD.js").then((i) => i.upload(this.api, e, t, n, r));
668
+ return import("./upload-B2mkgMeJ.js").then((i) => i.upload(this.api, e, t, n, r));
679
669
  }
680
670
  connectRealtime(e) {
681
- let t = this.state.get().config, n = this.state.get().session, r = P(n);
671
+ let t = this.state.get().config, n = this.state.get().session, r = F(n);
682
672
  if (!t?.features.realtime || !t.realtime || !n || !r) return Promise.resolve();
683
673
  if (this.live?.key === r) return this.live.ready;
684
674
  this.disconnectRealtime();
685
675
  let i = ++this.attempts, a = () => this.live?.attempt === i, o = (async () => {
686
- let { connect: r } = await import("./realtime-DIo-Wth4.js").then((e) => e.t);
676
+ let { connect: r } = await import("./realtime-C79zF50C.js").then((e) => e.t);
687
677
  if (!a()) return;
688
678
  let i = await r({
689
679
  ...t.realtime,
@@ -724,46 +714,48 @@ var A = "1.6.0", j = "https://commentkit.dev", M = "edit_conflict", N = 20, P =
724
714
  reconnectRealtime() {
725
715
  this.realtime?.reconnect?.();
726
716
  }
727
- async refreshThread(t) {
717
+ async refreshThread(e) {
728
718
  try {
729
- this.upsert(await this.getThread(t));
730
- } catch (n) {
731
- 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) })));
732
722
  }
733
723
  }
724
+ pageKey(e = location.href) {
725
+ return O.of(e);
726
+ }
734
727
  upsert(e) {
735
728
  this.state.set((t) => {
736
- let n = e.page.url === z() && !["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);
737
732
  return { threads: n ? [e, ...r] : r };
738
733
  });
739
734
  }
740
735
  destroy() {
741
- this.offSession(), this.disconnectRealtime(), this.listeners.clear();
736
+ this.offSession(), this.disconnectRealtime(), this.auth.destroy(), this.listeners.clear();
742
737
  }
743
- }, I = "commentkit:theme";
744
- function L() {
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);
744
+ }
745
+ return n;
746
+ }
747
+ var R = "commentkit:theme";
748
+ function z() {
745
749
  try {
746
- let e = typeof localStorage > "u" ? null : localStorage.getItem(I);
750
+ let e = typeof localStorage > "u" ? null : localStorage.getItem(R);
747
751
  return e === "auto" || e === "light" || e === "dark" ? e : null;
748
752
  } catch {
749
753
  return null;
750
754
  }
751
755
  }
752
- function R(e) {
756
+ function B(e) {
753
757
  try {
754
- e ? localStorage.setItem(I, e) : localStorage.removeItem(I);
758
+ e ? localStorage.setItem(R, e) : localStorage.removeItem(R);
755
759
  } catch {}
756
760
  }
757
- function z() {
758
- let e = new URL(location.href);
759
- for (let t of [
760
- "ck_thread",
761
- "ck",
762
- "ck_invite",
763
- "ck_code",
764
- "ck_state",
765
- "ck_signin"
766
- ]) e.searchParams.delete(t);
767
- return e.hash && !e.hash.startsWith("#/") && !e.hash.startsWith("#!/") && (e.hash = ""), e.toString();
768
- }
769
- export { R as a, D as c, l as d, r as f, z as i, v as l, M as n, b as o, e as p, A as r, C as s, F 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 };