@vite-hub/comments 0.7.0 → 0.9.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 (70) hide show
  1. package/README.md +4 -10
  2. package/dist/anchor.d.ts +2 -2
  3. package/dist/anchor.js +0 -1
  4. package/dist/{client-files-lC6X7XyS.js → client-files-CMPJEd21.js} +0 -1
  5. package/dist/client-request-B1DCz3a9.js +29 -0
  6. package/dist/client.d.ts +21 -241
  7. package/dist/client.js +4 -425
  8. package/dist/database.d.ts +1 -2
  9. package/dist/database.js +1 -1
  10. package/dist/{define-BqPcuDhf.js → define-hs9Z38io.js} +1 -2
  11. package/dist/{errors-C0VwRHiy.js → errors-C5utwZ5H.js} +0 -24
  12. package/dist/{errors-CBvIpwbo.js → errors-DeSEHzRf.js} +0 -12
  13. package/dist/hosted/hosted-comments/frame.js +4 -1
  14. package/dist/hosted/hosted-comments/tools.js +71 -77
  15. package/dist/hosted/hosted-comments.js +1 -1
  16. package/dist/{index-D1T7RJ4R.d.ts → index-CbXek64X.d.ts} +5 -113
  17. package/dist/index.d.ts +5 -6
  18. package/dist/index.js +6 -23
  19. package/dist/{install-CqBWFmU7.js → install--HecUN5c.js} +8 -789
  20. package/dist/{install-9L4ASzUC.d.ts → install-C3CW6Vvw.d.ts} +6 -12
  21. package/dist/{installed-wUYjqk_k.js → installed-BqQKo6WC.js} +2 -7
  22. package/dist/{database-BuSNVwTx.js → migrate-cxoJK4JQ.js} +2 -64
  23. package/dist/{operations-Qt8ASQqW.d.ts → operations-D0EO1pzL.d.ts} +36 -8
  24. package/dist/{operations-kMPw-Cyb.js → operations-DHXmbtb3.js} +47 -75
  25. package/dist/operations.d.ts +3 -3
  26. package/dist/operations.js +2 -2
  27. package/dist/overlay/client-BszZKIf8.js +4 -0
  28. package/dist/overlay/overlay.js +50 -43
  29. package/dist/page/loader.js +37 -30
  30. package/dist/{page-2NzFladc.js → page-CJz1K67L.js} +25 -2
  31. package/dist/page-DRM1j84-.d.ts +32 -0
  32. package/dist/{page-tools-shared-BNw-aWEH.js → page-tools-shared-BCdvqA4S.js} +40 -2
  33. package/dist/{path-8yqMd8zR.js → path-sv8kjq3i.js} +0 -4
  34. package/dist/{plugin-ChOHe7vq.js → plugin-DczA2K-y.js} +9 -9
  35. package/dist/realtime.d.ts +7 -9
  36. package/dist/realtime.js +2 -4
  37. package/dist/{rpc-kkxzQ3Qy.js → rpc-51X1TWKb.js} +1 -1
  38. package/dist/{rpc-D-eqtyfh.d.ts → rpc-DZB3yU0z.d.ts} +1 -1
  39. package/dist/rpc.d.ts +1 -1
  40. package/dist/rpc.js +1 -1
  41. package/dist/runtime/auth.d.ts +1 -2
  42. package/dist/runtime/auth.js +1 -1
  43. package/dist/runtime/client-request-plugin.d.ts +2 -0
  44. package/dist/runtime/client-request-plugin.js +21 -0
  45. package/dist/runtime/console.js +2 -2
  46. package/dist/runtime/index.d.ts +6 -9
  47. package/dist/runtime/index.js +25 -175
  48. package/dist/{server-8o9u8leM.js → server-CAMs6viR.js} +1 -1
  49. package/dist/server.d.ts +12 -8
  50. package/dist/server.js +8 -7
  51. package/dist/{service-XT2A8YEF.js → service-DFMwCr60.js} +34 -42
  52. package/dist/{store-DvNTDNgn.d.ts → service-qaMuJ7UX.d.ts} +68 -2
  53. package/dist/{session-DGdFKWDB.js → session-1SI3_wLJ.js} +212 -66
  54. package/dist/{session-CN4irEqf.d.ts → session-CK3Rnjto.d.ts} +2 -3
  55. package/dist/{table-names-BIHjuEFK.js → table-names-DUmWROM4.js} +1 -3
  56. package/dist/testing.d.ts +1 -1
  57. package/dist/testing.js +1 -2
  58. package/dist/{tokens-Bx4lZ0yt.js → tokens-WRh__ppm.js} +0 -1
  59. package/dist/{types-BX3vH18p.d.ts → types-BmzTyGt-.d.ts} +4 -0
  60. package/dist/{define-B1jLSPe9.d.ts → types-VSVs4jIw.d.ts} +157 -4
  61. package/dist/vite.d.ts +3 -20
  62. package/dist/vite.js +15 -82
  63. package/package.json +8 -8
  64. package/dist/database-CPua6Q6_.d.ts +0 -573
  65. package/dist/overlay/client-CrnKCZXc.js +0 -4
  66. package/dist/overlay/dist-yIZmvSaU.js +0 -1
  67. package/dist/page-DE71P_gQ.d.ts +0 -11
  68. package/dist/request-origin-B-CPVAVk.js +0 -53
  69. package/dist/service-IvgrI4XM.d.ts +0 -71
  70. package/dist/types-l6GOb_yc.d.ts +0 -206
package/dist/server.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { b as validateCommentsDefinition, d as CommentsPeople } from "./define-B1jLSPe9.js";
2
- import { a as maximumCommentBodyLength, i as CommentsRuntime, n as CommentsAttachmentStore, r as CommentsForgeHooks } from "./service-IvgrI4XM.js";
3
- import { i as createCommentsStore, n as CommentsStore, r as CommentsTables, t as CommentsDatabase } from "./store-DvNTDNgn.js";
4
- import { a as commentsOperations, i as commentInputSchemas, n as commentActions, r as commentAnchorSchema, t as CommentsCaller } from "./operations-Qt8ASQqW.js";
5
- import { n as createRpcTransport, t as RpcTransportOptions } from "./rpc-D-eqtyfh.js";
6
- import { n as installComments, t as InstallCommentsOptions } from "./install-9L4ASzUC.js";
1
+ import { F as validateCommentsDefinition, T as CommentsPeople } from "./types-VSVs4jIw.js";
2
+ import { a as maximumCommentBodyLength, c as CommentsTables, i as CommentsRuntime, l as createCommentsStore, n as CommentsAttachmentStore, o as CommentsDatabase, r as CommentsForgeHooks, s as CommentsStore } from "./service-qaMuJ7UX.js";
3
+ import { D as CommentUser } from "./types-BmzTyGt-.js";
4
+ import { a as commentInputSchemas, i as commentAnchorSchema, n as CommentsCaller, o as commentsOperations, r as commentActions, t as CommentThreadsPage } from "./operations-D0EO1pzL.js";
5
+ import { n as createRpcTransport, t as RpcTransportOptions } from "./rpc-DZB3yU0z.js";
6
+ import { n as installComments, t as InstallCommentsOptions } from "./install-C3CW6Vvw.js";
7
+ import { a as hostedCommentsEndedNotice, i as hostedCommentsChangeNotice, o as hostedCommentsNoticeDefinition, r as hostedCommentNoticeRoom } from "./page-DRM1j84-.js";
7
8
  /** Where mentions find people. The same shape as `defineComments({ people })`. */
8
9
  export type CommentsUsers = CommentsPeople;
9
10
  /**
@@ -15,5 +16,8 @@ export declare function installCommentsRuntime(runtime: CommentsRuntime | (() =>
15
16
  export declare function commentsRuntime(): Promise<CommentsRuntime>;
16
17
  export declare function hasCommentsRuntime(): boolean;
17
18
  /** People from a Better Auth instance, through its internal adapter. The generated Nitro plugin passes it when the app has Auth. */
18
- export declare function createAuthUsers(getAuth: () => object | Promise<object>): CommentsPeople;
19
- export { type CommentsAttachmentStore, CommentsCaller, type CommentsDatabase, type CommentsForgeHooks, type CommentsRuntime, type CommentsStore, type CommentsTables, type InstallCommentsOptions, RpcTransportOptions, commentActions, commentAnchorSchema, commentInputSchemas, commentsOperations, createCommentsStore, createRpcTransport, installComments, maximumCommentBodyLength, validateCommentsDefinition };
19
+ export declare function createAuthUsers(getAuth: () => object | Promise<object>): {
20
+ byIds(ids: readonly string[]): Promise<CommentUser[]>;
21
+ search(query: string, limit: number): Promise<CommentUser[]>;
22
+ };
23
+ export { CommentThreadsPage, type CommentsAttachmentStore, CommentsCaller, type CommentsDatabase, type CommentsForgeHooks, type CommentsRuntime, type CommentsStore, type CommentsTables, type InstallCommentsOptions, RpcTransportOptions, commentActions, commentAnchorSchema, commentInputSchemas, commentsOperations, createCommentsStore, createRpcTransport, hostedCommentNoticeRoom, hostedCommentsChangeNotice, hostedCommentsEndedNotice, hostedCommentsNoticeDefinition, installComments, maximumCommentBodyLength, validateCommentsDefinition };
package/dist/server.js CHANGED
@@ -1,7 +1,8 @@
1
- import { i as installCommentsRuntime, n as createAuthUsers, r as hasCommentsRuntime, t as commentsRuntime } from "./server-8o9u8leM.js";
2
- import { i as commentsOperations, n as commentAnchorSchema, r as commentInputSchemas, t as commentActions } from "./operations-kMPw-Cyb.js";
3
- import { t as createRpcTransport } from "./rpc-kkxzQ3Qy.js";
4
- import { f as maximumCommentBodyLength } from "./service-XT2A8YEF.js";
5
- import { r as createCommentsStore, t as installComments } from "./install-CqBWFmU7.js";
6
- import { c as validateCommentsDefinition } from "./define-BqPcuDhf.js";
7
- export { commentActions, commentAnchorSchema, commentInputSchemas, commentsOperations, commentsRuntime, createAuthUsers, createCommentsStore, createRpcTransport, hasCommentsRuntime, installComments, installCommentsRuntime, maximumCommentBodyLength, validateCommentsDefinition };
1
+ import { i as installCommentsRuntime, n as createAuthUsers, r as hasCommentsRuntime, t as commentsRuntime } from "./server-CAMs6viR.js";
2
+ import { i as commentsOperations, n as commentAnchorSchema, r as commentInputSchemas, t as commentActions } from "./operations-DHXmbtb3.js";
3
+ import { t as createRpcTransport } from "./rpc-51X1TWKb.js";
4
+ import { r as createCommentsStore, t as installComments } from "./install--HecUN5c.js";
5
+ import { f as maximumCommentBodyLength } from "./service-DFMwCr60.js";
6
+ import { c as validateCommentsDefinition } from "./define-hs9Z38io.js";
7
+ import { c as hostedCommentNoticeRoom, d as hostedCommentsNoticeDefinition, l as hostedCommentsChangeNotice, u as hostedCommentsEndedNotice } from "./page-CJz1K67L.js";
8
+ export { commentActions, commentAnchorSchema, commentInputSchemas, commentsOperations, commentsRuntime, createAuthUsers, createCommentsStore, createRpcTransport, hasCommentsRuntime, hostedCommentNoticeRoom, hostedCommentsChangeNotice, hostedCommentsEndedNotice, hostedCommentsNoticeDefinition, installComments, installCommentsRuntime, maximumCommentBodyLength, validateCommentsDefinition };
@@ -1,9 +1,9 @@
1
1
  import { t as replyMessageId } from "./reply-id-Cm9FGOBs.js";
2
- import { t as isPagePath } from "./path-8yqMd8zR.js";
3
- import { t as commentsErrorDiagnostics } from "./errors-CBvIpwbo.js";
2
+ import { t as isPagePath } from "./path-sv8kjq3i.js";
3
+ import { t as commentsErrorDiagnostics } from "./errors-DeSEHzRf.js";
4
4
  import { t as foldName } from "./fold-name-g-xg0mQk.js";
5
5
  import { r as guestIdPrefix } from "./identity-CT7-UGmS.js";
6
- import { i as defaultPolicy, o as deniedForMissingPolicy, t as builtinCommentSubjects } from "./define-BqPcuDhf.js";
6
+ import { i as defaultPolicy, o as deniedForMissingPolicy, t as builtinCommentSubjects } from "./define-hs9Z38io.js";
7
7
  import { t as logFailure } from "./request-log-DllKQXj7.js";
8
8
  import * as v from "valibot";
9
9
  // ../internal/src/runtime/client-rate-limit.ts
@@ -105,7 +105,6 @@ const attachmentTypes = [
105
105
  "application/pdf",
106
106
  "text/plain"
107
107
  ];
108
- /** The media types that a browser may show inline. Every other file downloads. */
109
108
  const inlineTypes = [
110
109
  "image/png",
111
110
  "image/jpeg",
@@ -173,7 +172,6 @@ async function can(runtime, context, gate = {}) {
173
172
  return false;
174
173
  }
175
174
  let warned = false;
176
- /** Says once per process, in the server log, that signed-in people wait for an `authorize` policy. */
177
175
  function warnMissingPolicy() {
178
176
  if (warned) return;
179
177
  warned = true;
@@ -189,7 +187,6 @@ function deniedError(runtime, action, user, gate = {}) {
189
187
  if (deniedForMissingPolicy(runtime.definition, user, checked)) return fail(commentsErrorDiagnostics.COMMENTS_R0006(), 403);
190
188
  return fail(commentsErrorDiagnostics.COMMENTS_R0009({ action }), 403);
191
189
  }
192
- /** Whether `user` may mention `person`: `canMention`, else whether `person` may read the thread. */
193
190
  async function mayMention(runtime, user, found, target) {
194
191
  const subject = target.subject ?? target.thread?.subject;
195
192
  const person = { ...found };
@@ -211,7 +208,6 @@ async function mayMention(runtime, user, found, target) {
211
208
  return false;
212
209
  }
213
210
  }
214
- /** Where `@` finds people: `people`, else Auth, else the authors of threads that `reader` may read. */
215
211
  function peopleOf(runtime, reader, gate, console) {
216
212
  const explicit = runtime.definition.people ?? runtime.users;
217
213
  if (explicit) return explicit;
@@ -228,7 +224,13 @@ function peopleOf(runtime, reader, gate, console) {
228
224
  }
229
225
  /** People that `@` suggests to `user`, without the ones that may not be mentioned in the thread. */
230
226
  async function suggest(runtime, input) {
231
- const found = await peopleOf(runtime, input.user, input.gate, input.console).search(input.query, input.limit * 2);
227
+ const subject = input.subject ?? input.thread?.subject;
228
+ const context = {
229
+ user: input.user,
230
+ ...input.thread ? { thread: input.thread } : {},
231
+ ...subject ? { subject } : {}
232
+ };
233
+ const found = await peopleOf(runtime, input.user, input.gate, input.console).search(input.query, input.limit * 2, context);
232
234
  const needle = foldName(input.query.trim());
233
235
  const people = [];
234
236
  for (const person of found) {
@@ -321,7 +323,6 @@ function check(schema, value, field) {
321
323
  if (!parsed.success) throw invalid(parsed.issues, field);
322
324
  return parsed.output;
323
325
  }
324
- /** The trimmed text. Empty only with attachments, or on a subject that allows it. */
325
326
  function cleanBody(runtime, input) {
326
327
  if (input.body.length > 1e4) throw fail(commentsErrorDiagnostics.COMMENTS_R0018({ message: `body: Keep a message under ${maximumCommentBodyLength.toLocaleString("en")} characters.` }), 400);
327
328
  const trimmed = input.body.trim();
@@ -337,18 +338,31 @@ function attachmentsOf(list) {
337
338
  function newId(prefix) {
338
339
  return `${prefix}_${crypto.randomUUID()}`;
339
340
  }
340
- /**
341
- * Runs `onComment` before the answer and `onMention` after it, for one saved message. A failure is logged and never
342
- * fails the comment.
343
- */
344
341
  async function announce(runtime, thread, message, mentions, fresh, options) {
345
342
  if (!message) return;
346
343
  const { onComment, onMention } = runtime.definition;
344
+ const keep = (promise) => {
345
+ options.waitUntil?.(promise);
346
+ runtime.background?.(promise);
347
+ };
348
+ const failedLater = (hook) => (error) => {
349
+ logFailure({
350
+ field: "comments",
351
+ fields: {
352
+ hook,
353
+ thread: thread.id
354
+ },
355
+ message: `Comments ${hook} failed`,
356
+ error,
357
+ waitUntil: keep
358
+ }, () => globalThis.console.error(`[vitehub] Comments ${hook} failed:`, error));
359
+ };
347
360
  if (options.comment && onComment) try {
348
361
  await onComment({
349
362
  mentions,
350
363
  message,
351
- thread
364
+ thread,
365
+ waitUntil: (promise) => keep(promise.catch(failedLater("onComment")))
352
366
  });
353
367
  } catch (error) {
354
368
  logFailure({
@@ -364,33 +378,12 @@ async function announce(runtime, thread, message, mentions, fresh, options) {
364
378
  }
365
379
  if (!onMention || !fresh.length) return;
366
380
  const link = subjectDefinition(runtime, thread.subject.kind)?.link?.(thread);
367
- const keep = (promise) => {
368
- options.waitUntil?.(promise);
369
- runtime.background?.(promise);
370
- };
371
- const task = (async () => {
372
- try {
373
- await onMention({
374
- message,
375
- thread,
376
- users: fresh,
377
- ...link ? { link } : {}
378
- });
379
- } catch (error) {
380
- logFailure({
381
- field: "comments",
382
- fields: {
383
- hook: "onMention",
384
- thread: thread.id
385
- },
386
- message: "Comments onMention failed",
387
- error,
388
- waitUntil: keep
389
- }, () => globalThis.console.error("[vitehub] Comments onMention failed:", error));
390
- }
391
- })();
392
- options.waitUntil?.(task);
393
- runtime.background?.(task);
381
+ keep((async () => onMention({
382
+ message,
383
+ thread,
384
+ users: fresh,
385
+ ...link ? { link } : {}
386
+ }))().catch(failedLater("onMention")));
394
387
  }
395
388
  /** The thread that an earlier `createThread` with this `requestId` stored. A retry gets it back before the create checks. */
396
389
  async function earlierThread(runtime, input) {
@@ -566,7 +559,6 @@ function decodeBase64(data) {
566
559
  for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index);
567
560
  return bytes;
568
561
  }
569
- /** Binds an upload to its exact key and bytes, including after a process restart or an app Blob replacement. */
570
562
  async function attachmentRecord(key, data) {
571
563
  const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new Uint8Array(data)));
572
564
  const sha256 = Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
@@ -1,6 +1,19 @@
1
+ import { C as CommentsDefinition, T as CommentsPeople, j as CommentsUser } from "./types-VSVs4jIw.js";
1
2
  import { a as commentThreads, i as commentMessages, n as commentCounters } from "./database-DUs3c2al.js";
2
- import { D as CommentUser, S as CommentStatus, T as CommentThread, b as CommentMetadata, n as CommentActor, r as CommentAnchor, s as CommentContext, v as CommentListQuery, w as CommentSubject } from "./types-BX3vH18p.js";
3
+ import { D as CommentUser, S as CommentStatus, T as CommentThread, b as CommentMetadata, n as CommentActor, r as CommentAnchor, s as CommentContext, v as CommentListQuery, w as CommentSubject } from "./types-BmzTyGt-.js";
4
+ import "valibot";
3
5
  import { LibSQLDatabase } from "drizzle-orm/libsql";
6
+ /** The part of `checkRateLimit()` from `vite-hub/rate-limit` that a public write calls. */
7
+ type SharedRateLimit = (request: Request, name: string, options: {
8
+ key: string;
9
+ limit: number;
10
+ window: `${number}${"ms" | "s" | "m" | "h" | "d"}`;
11
+ }) => Promise<[unknown, {
12
+ allowed: boolean;
13
+ reason?: string;
14
+ retryAfter?: number;
15
+ cause?: unknown;
16
+ } | undefined]>;
4
17
  /**
5
18
  * The Drizzle database that the store uses. The store calls `select`, `insert`, `update`, and `delete` (with
6
19
  * `onConflictDoNothing()` and `returning()`), which SQLite, libSQL, D1, and Postgres Drizzle databases all offer, and
@@ -92,4 +105,57 @@ interface CommentsStore {
92
105
  knownUsersByIds: (ids: readonly string[], canRead: (thread: CommentThread) => Promise<boolean>) => Promise<CommentUser[]>;
93
106
  }
94
107
  declare function createCommentsStore(database: CommentsDatabase, tables?: CommentsTables): CommentsStore;
95
- export { createCommentsStore as i, CommentsStore as n, CommentsTables as r, CommentsDatabase as t };
108
+ /** Longest message, in characters. */
109
+ declare const maximumCommentBodyLength = 10000;
110
+ /** The parts of a Blob store that attachments use: `blob` of `@vite-hub/blob`. */
111
+ interface CommentsAttachmentStore {
112
+ put: (pathname: string, body: Uint8Array, options: {
113
+ contentType: string;
114
+ }) => Promise<readonly [unknown, unknown]>;
115
+ get?: (pathname: string) => Promise<readonly [unknown, Blob | null | undefined]>;
116
+ }
117
+ /** A file to answer, with the headers that keep a browser from running it. */
118
+ interface CommentsAttachmentBody {
119
+ key: string;
120
+ name: string;
121
+ /** The stored media type when Comments attaches that type, else `application/octet-stream`. */
122
+ type: string;
123
+ size: number;
124
+ /** Whether a browser may show it in the page: images other than SVG. */
125
+ inline: boolean;
126
+ data: Blob;
127
+ }
128
+ /** The pull request sync of `defineComments({ forge })`. */
129
+ interface CommentsForgeHooks {
130
+ /** Starts the sync of a thread after a change. */
131
+ changed: (threadId: string) => void;
132
+ /** The threads with their `forge` link. */
133
+ attach: (threads: CommentThread[]) => Promise<CommentThread[]>;
134
+ }
135
+ /** Everything that the comment operations, the server API, and the overlay API share. */
136
+ interface CommentsRuntime {
137
+ definition: CommentsDefinition;
138
+ /**
139
+ * The app's `identify`, given the server event of a call. With it, the comment operations ask it for the user, as
140
+ * the overlay API does, instead of taking the Console's user.
141
+ */
142
+ identify?: (event: unknown) => Promise<CommentsUser | null>;
143
+ store: CommentsStore;
144
+ /** The Auth adapter. `people` wins over it. Without both, `@` finds the authors that the store knows. */
145
+ users?: CommentsPeople;
146
+ /** Vite's serve command, for the default guest policy. */
147
+ development?: boolean;
148
+ /** The Blob store for attachments, when the app has one and no `storeAttachment`. */
149
+ attachments?: CommentsAttachmentStore;
150
+ forge?: CommentsForgeHooks;
151
+ /**
152
+ * `checkRateLimit` of ViteHub Rate Limit, when it counts in a KV store that every server process shares. Guest writes
153
+ * then count under the Rate Limit name `comments`. Without it, they count in the memory of this process.
154
+ */
155
+ rateLimit?: SharedRateLimit;
156
+ /** Work that runs after the answer, such as `onMention`. `flush()` of the server API waits for it. */
157
+ background?: (task: Promise<unknown>) => void;
158
+ /** Tells the open pages of a thread that it changed, such as through a Realtime notice to its page room. */
159
+ changed?: (thread: CommentThread) => Promise<void>;
160
+ }
161
+ export { maximumCommentBodyLength as a, CommentsTables as c, CommentsRuntime as i, createCommentsStore as l, CommentsAttachmentStore as n, CommentsDatabase as o, CommentsForgeHooks as r, CommentsStore as s, CommentsAttachmentBody as t, SharedRateLimit as u };