@vite-hub/comments 0.4.1 → 0.5.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 (87) hide show
  1. package/README.md +23 -7
  2. package/dist/anchor.d.ts +3 -5
  3. package/dist/anchor.js +1 -2
  4. package/dist/{client-diagnostics-Bubm7Shd.js → client-diagnostics-CvfTW5d4.js} +1 -2
  5. package/dist/{client-files-CJZl5Kc4.js → client-files-lC6X7XyS.js} +1 -2
  6. package/dist/client.d.ts +430 -24
  7. package/dist/client.js +413 -20
  8. package/dist/{console-h4KlhIik.d.ts → console-CK2F53QP.d.ts} +2 -4
  9. package/dist/{context-BqIsmYP1.js → context-DbkBjITZ.js} +1 -2
  10. package/dist/database/postgres.d.ts +1 -3
  11. package/dist/database/postgres.js +1 -2
  12. package/dist/{migrate-Bpw3xL5e.js → database-BkPoOdZr.js} +116 -59
  13. package/dist/database-CPua6Q6_.d.ts +573 -0
  14. package/dist/{database-CA1aHKN7.d.ts → database-DUs3c2al.d.ts} +625 -73
  15. package/dist/database.d.ts +4 -5
  16. package/dist/database.js +1 -2
  17. package/dist/{define-CX_Yr_hz.js → define-9uMM3Ewc.js} +2 -3
  18. package/dist/{define-CHTWfHPo.d.ts → define-B1jLSPe9.d.ts} +8 -6
  19. package/dist/{errors-4gelGVmK.js → errors-C0VwRHiy.js} +25 -2
  20. package/dist/{errors-BHsVXlpd.js → errors-DFNiJg67.js} +26 -3
  21. package/dist/{fold-name-B7Zb3-MF.js → fold-name-g-xg0mQk.js} +1 -2
  22. package/dist/hosted/hosted-comments/client-B3dlmmQd.js +4 -0
  23. package/dist/{overlay/dist-BoHMOFax.js → hosted/hosted-comments/dist-DyVmH21U.js} +1 -1
  24. package/dist/hosted/hosted-comments/frame.js +1 -0
  25. package/dist/hosted/hosted-comments/overlay-4-mWX4Uz.js +170 -0
  26. package/dist/hosted/hosted-comments/protocol-BHvtQKte.js +1 -0
  27. package/dist/hosted/hosted-comments/rest-transport-D-9_pNqZ.js +1 -0
  28. package/dist/hosted/hosted-comments/tools-CdwnUCOr.js +126 -0
  29. package/dist/hosted/hosted-comments.js +9 -0
  30. package/dist/{identity-CBOzgxSf.js → identity-CT7-UGmS.js} +1 -2
  31. package/dist/{index-iPGlSAgj.d.ts → index-BEnHTj2Z.d.ts} +109 -15
  32. package/dist/index.d.ts +6 -7
  33. package/dist/index.js +23 -10
  34. package/dist/{install-96NXWnrw.d.ts → install-B-FQpi47.d.ts} +24 -7
  35. package/dist/{install-jRKmuLHK.js → install-BUXxryut.js} +950 -55
  36. package/dist/{installed-g6nDr5ac.js → installed-C9SkldMi.js} +8 -8
  37. package/dist/{invariant-D8_xBqmZ.js → invariant-CmdvZ3vp.js} +1 -2
  38. package/dist/{migrate-B0k_0swk.d.ts → migrate-XjFeQppf.d.ts} +2 -2
  39. package/dist/{object-Da1zyPCR.js → object-CAK6UYR5.js} +1 -2
  40. package/dist/{operations-7ycAhDCw.d.ts → operations-8hqTi0DH.d.ts} +8 -4
  41. package/dist/{operations-9tskoLsk.js → operations-CKC_lMG_.js} +309 -229
  42. package/dist/{operations-D93EV10O.d.ts → operations-Cic9A9e_.d.ts} +9 -7
  43. package/dist/operations.d.ts +2 -2
  44. package/dist/operations.js +2 -2
  45. package/dist/overlay/{client-lZesR40x.js → client-CerbVjTz.js} +3 -3
  46. package/dist/overlay/dist-DyVmH21U.js +14 -0
  47. package/dist/overlay/overlay.js +36 -40
  48. package/dist/page/loader.js +45 -52
  49. package/dist/page-DE71P_gQ.d.ts +11 -0
  50. package/dist/{page-tools-shared-NHUjKI8E.js → page-tools-shared-fvmJQCrP.js} +8 -5
  51. package/dist/{page-DRz_sDY4.js → page-xzgZ6DGm.js} +2 -3
  52. package/dist/{path--zk6PEMz.js → path-8yqMd8zR.js} +0 -1
  53. package/dist/{plugin-BNFd1zGz.js → plugin-B2hETRii.js} +16 -13
  54. package/dist/realtime.d.ts +3 -5
  55. package/dist/realtime.js +4 -5
  56. package/dist/{reply-id-P_bSuEng.js → reply-id-Cm9FGOBs.js} +1 -2
  57. package/dist/request-log-DllKQXj7.js +104 -0
  58. package/dist/request-origin-YPzfE2eh.js +48 -0
  59. package/dist/{rpc-BQqYx8kb.js → rpc-BKY7XRlR.js} +5 -7
  60. package/dist/{rpc-BZ1QNkCC.d.ts → rpc-D-eqtyfh.d.ts} +1 -3
  61. package/dist/rpc.d.ts +1 -1
  62. package/dist/rpc.js +1 -2
  63. package/dist/runtime/auth.d.ts +3 -5
  64. package/dist/runtime/auth.js +2 -3
  65. package/dist/runtime/console.d.ts +2 -4
  66. package/dist/runtime/console.js +4 -5
  67. package/dist/runtime/index.d.ts +12 -27
  68. package/dist/runtime/index.js +160 -31
  69. package/dist/{server-CqyzrqTI.js → server-BGbD7H5s.js} +2 -3
  70. package/dist/server.d.ts +6 -7
  71. package/dist/server.js +6 -6
  72. package/dist/service-CPiWPoiY.d.ts +69 -0
  73. package/dist/{service-C8ZgFWvy.js → service-DDD4Ubxd.js} +44 -18
  74. package/dist/{session-ByjMyTak.js → session-BGN6Vlm6.js} +31 -37
  75. package/dist/{session-CKfW6DGE.d.ts → session-CTh9nisq.d.ts} +3 -5
  76. package/dist/{service-wC9kx9EX.d.ts → store-DvNTDNgn.d.ts} +3 -73
  77. package/dist/{table-names-DfiKUlOV.js → table-names-BIHjuEFK.js} +4 -3
  78. package/dist/testing.d.ts +2 -4
  79. package/dist/testing.js +4 -5
  80. package/dist/{tokens-DSbwkVSy.js → tokens-Bx4lZ0yt.js} +3 -6
  81. package/dist/{types-C7LzhgRb.d.ts → types-BX3vH18p.d.ts} +1 -3
  82. package/dist/{types-C1qzObs2.d.ts → types-l6GOb_yc.d.ts} +79 -6
  83. package/dist/vite.d.ts +22 -21
  84. package/dist/vite.js +161 -104
  85. package/package.json +19 -19
  86. package/dist/errors-BEChnUo7.d.ts +0 -197
  87. package/dist/page-CWwnmF3K.d.ts +0 -22
@@ -1,63 +1,78 @@
1
- import { t as commentsErrorDiagnostics } from "./errors-BHsVXlpd.js";
1
+ import { t as commentsErrorDiagnostics } from "./errors-DFNiJg67.js";
2
2
  import { sql } from "drizzle-orm";
3
3
  import { index, integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
4
- //#region src/operations-core/database.ts
4
+ // src/operations-core/database.ts
5
+ /**
6
+ * The thread, message, and counter tables of Comments under other names, for an app that keeps more than one set
7
+ * in one Database. Pass them to `installComments({ tables, tenant })`. Messages have no foreign key to their thread,
8
+ * because a thread is stored after its first message: delete the messages of a deleted thread with a trigger.
9
+ */
10
+ function commentsSqliteTables(options) {
11
+ const { prefix, scope } = options;
12
+ const scopeColumn = () => scope ? text("scope").notNull().references(scope, { onDelete: "cascade" }) : text("scope").notNull();
13
+ return {
14
+ commentThreads: sqliteTable(`${prefix}_threads`, {
15
+ id: text("id").primaryKey(),
16
+ scope: scopeColumn(),
17
+ n: integer("n").notNull(),
18
+ subjectKind: text("subject_kind").notNull(),
19
+ subjectId: text("subject_id").notNull(),
20
+ anchor: text("anchor").notNull(),
21
+ status: text("status", { enum: ["open", "resolved"] }).notNull(),
22
+ resolvedBy: text("resolved_by"),
23
+ resolvedAt: integer("resolved_at"),
24
+ authorId: text("author_id").notNull(),
25
+ authorName: text("author_name"),
26
+ authorImage: text("author_image"),
27
+ actor: text("actor"),
28
+ /** JSON of the fields that app code keeps on the thread. */
29
+ metadata: text("metadata"),
30
+ /** JSON of the page, element, components, browser, and build that the overlay read. */
31
+ context: text("context"),
32
+ /** The version of the subject that the thread was started on. */
33
+ revision: integer("revision"),
34
+ /** The retry key of `comments.create`. Unique inside `scope`. */
35
+ requestId: text("request_id"),
36
+ createdAt: integer("created_at").notNull(),
37
+ updatedAt: integer("updated_at").notNull()
38
+ }, (table) => [
39
+ uniqueIndex(`${prefix}_threads_scope_n`).on(table.scope, table.n),
40
+ uniqueIndex(`${prefix}_threads_scope_request`).on(table.scope, table.requestId),
41
+ index(`${prefix}_threads_subject`).on(table.subjectKind, table.subjectId),
42
+ index(`${prefix}_threads_status`).on(table.status, table.updatedAt)
43
+ ]),
44
+ commentMessages: sqliteTable(`${prefix}_messages`, {
45
+ id: text("id").primaryKey(),
46
+ threadId: text("thread_id").notNull(),
47
+ body: text("body").notNull(),
48
+ authorId: text("author_id").notNull(),
49
+ authorName: text("author_name"),
50
+ authorImage: text("author_image"),
51
+ actor: text("actor"),
52
+ mentions: text("mentions").notNull(),
53
+ attachments: text("attachments").notNull(),
54
+ createdAt: integer("created_at").notNull(),
55
+ editedAt: integer("edited_at")
56
+ }, (table) => [index(`${prefix}_messages_thread`).on(table.threadId, table.createdAt)]),
57
+ commentCounters: sqliteTable(`${prefix}_counters`, {
58
+ scope: (scope ? scopeColumn() : text("scope")).primaryKey(),
59
+ next: integer("next").notNull()
60
+ })
61
+ };
62
+ }
63
+ const defaultTables = commentsSqliteTables({ prefix: "vitehub_comment" });
5
64
  /**
6
65
  * Threads of comments. `n` is unique inside `scope`, which is what `?comment=<n>` names. `anchor`, `actor`,
7
66
  * `resolvedBy`, `metadata`, and `context` hold JSON. Times are milliseconds since the epoch.
8
67
  */
9
- const commentThreads = sqliteTable("vitehub_comment_threads", {
10
- id: text("id").primaryKey(),
11
- scope: text("scope").notNull(),
12
- n: integer("n").notNull(),
13
- subjectKind: text("subject_kind").notNull(),
14
- subjectId: text("subject_id").notNull(),
15
- anchor: text("anchor").notNull(),
16
- status: text("status", { enum: ["open", "resolved"] }).notNull(),
17
- resolvedBy: text("resolved_by"),
18
- resolvedAt: integer("resolved_at"),
19
- authorId: text("author_id").notNull(),
20
- authorName: text("author_name"),
21
- authorImage: text("author_image"),
22
- actor: text("actor"),
23
- /** JSON of the fields that app code keeps on the thread. */
24
- metadata: text("metadata"),
25
- /** JSON of the page, element, components, browser, and build that the overlay read. */
26
- context: text("context"),
27
- /** The version of the subject that the thread was started on. */
28
- revision: integer("revision"),
29
- /** The retry key of `comments.create`. Unique inside `scope`. */
30
- requestId: text("request_id"),
31
- createdAt: integer("created_at").notNull(),
32
- updatedAt: integer("updated_at").notNull()
33
- }, (table) => [
34
- uniqueIndex("vitehub_comment_threads_scope_n").on(table.scope, table.n),
35
- uniqueIndex("vitehub_comment_threads_scope_request").on(table.scope, table.requestId),
36
- index("vitehub_comment_threads_subject").on(table.subjectKind, table.subjectId),
37
- index("vitehub_comment_threads_status").on(table.status, table.updatedAt)
38
- ]);
68
+ const commentThreads = defaultTables.commentThreads;
39
69
  /** One Markdown message. The first message of a thread is the comment. `mentions` and `attachments` hold JSON lists. */
40
- const commentMessages = sqliteTable("vitehub_comment_messages", {
41
- id: text("id").primaryKey(),
42
- threadId: text("thread_id").notNull(),
43
- body: text("body").notNull(),
44
- authorId: text("author_id").notNull(),
45
- authorName: text("author_name"),
46
- authorImage: text("author_image"),
47
- actor: text("actor"),
48
- mentions: text("mentions").notNull(),
49
- attachments: text("attachments").notNull(),
50
- createdAt: integer("created_at").notNull(),
51
- editedAt: integer("edited_at")
52
- }, (table) => [index("vitehub_comment_messages_thread").on(table.threadId, table.createdAt)]);
70
+ const commentMessages = defaultTables.commentMessages;
53
71
  /**
54
72
  * The next thread number of each scope. A number is handed out once, so a deleted thread never gives its number to a
55
73
  * new one, and a link such as `?comment=3` keeps naming the same thread.
56
74
  */
57
- const commentCounters = sqliteTable("vitehub_comment_counters", {
58
- scope: text("scope").primaryKey(),
59
- next: integer("next").notNull()
60
- });
75
+ const commentCounters = defaultTables.commentCounters;
61
76
  /**
62
77
  * The pull request comment of each message, when `defineComments({ forge })` posts threads. `id` is a message id,
63
78
  * `summary:<repository>#<pull request>`, or `cleanup:<repository>#<remote id>` for a pending delete.
@@ -92,8 +107,7 @@ const commentsSchema = {
92
107
  vitehubCommentCounters: commentCounters,
93
108
  vitehubCommentForge: commentForge
94
109
  };
95
- //#endregion
96
- //#region src/tables.ts
110
+ // src/tables.ts
97
111
  /**
98
112
  * The tables of `commentsSchema` as SQLite statements that are safe to run on every start. A database that Comments
99
113
  * opens itself gets them on first use; a ViteHub Database gets the same tables from `vitehub database generate` instead.
@@ -113,8 +127,7 @@ const commentsTables = [
113
127
  "CREATE INDEX IF NOT EXISTS `vitehub_comment_forge_thread` ON `vitehub_comment_forge` (`thread_id`)",
114
128
  "CREATE INDEX IF NOT EXISTS `vitehub_comment_forge_pull_request` ON `vitehub_comment_forge` (`repository`,`pull_request`)"
115
129
  ];
116
- //#endregion
117
- //#region src/migrate.ts
130
+ // src/migrate.ts
118
131
  /** The live-site threads, after the migration renames them. The migration never deletes them. */
119
132
  const liveThreadsBackup = "vitehub_comment_threads_live";
120
133
  /** The columns that one model added to `vitehub_comment_threads` after its first version. */
@@ -203,13 +216,14 @@ function runSqliteStatements(db, statements, start = 0) {
203
216
  function names(rows) {
204
217
  return (Array.isArray(rows) ? rows : Array.isArray(Reflect.get(Object(rows), "rows")) ? Reflect.get(Object(rows), "rows") : []).map((row) => String(Array.isArray(row) ? row[0] : Object.values(Object(row))[0]));
205
218
  }
206
- async function columns(db, postgres) {
207
- if (postgres) return names(await db.execute(sql.raw("SELECT column_name FROM information_schema.columns WHERE table_name = 'vitehub_comment_threads' AND table_schema = current_schema()")));
208
- return names(await db.all(sql.raw("SELECT name FROM pragma_table_info('vitehub_comment_threads')")));
219
+ async function columns(db, postgres, table = "vitehub_comment_threads") {
220
+ const name = `'${table.replaceAll("'", "''")}'`;
221
+ if (postgres) return names(await db.execute(sql.raw(`SELECT column_name FROM information_schema.columns WHERE table_name = ${name} AND table_schema = current_schema()`)));
222
+ return names(await db.all(sql.raw(`SELECT name FROM pragma_table_info(${name})`)));
209
223
  }
210
224
  /** What the Database has: no comments yet, the live-site tables, one model without newer columns, or nothing to do. */
211
225
  async function commentsSchemaState(db, options = {}) {
212
- const found = await columns(db, options.dialect === "postgresql");
226
+ const found = await columns(db, options.dialect === "postgresql", options.table);
213
227
  if (!found.length) return {
214
228
  missing: [],
215
229
  state: "empty"
@@ -285,5 +299,48 @@ function commentsSchemaGate(db, owner, options = {}) {
285
299
  return ready;
286
300
  };
287
301
  }
288
- //#endregion
289
- export { migrateComments as a, commentForge as c, commentsSchema as d, liveThreadsBackup as i, commentMessages as l, commentsSchemaGate as n, commentsTables as o, commentsSchemaState as r, commentCounters as s, commentsMigrationSql as t, commentThreads as u };
302
+ // src/send/database.ts
303
+ /**
304
+ * One row for each send: the member, the threads it carried with the last message of each, and the read token. `threads`
305
+ * holds JSON, `[{ id, message, role }]`, and `sender` the member as Comments knew them when they sent. The token is
306
+ * kept as its SHA-256 hash.
307
+ */
308
+ const commentSends = sqliteTable("vitehub_comment_sends", {
309
+ id: text("id").primaryKey(),
310
+ member: text("member").notNull(),
311
+ subjectKind: text("subject_kind").notNull(),
312
+ subjectId: text("subject_id").notNull(),
313
+ page: text("page").notNull(),
314
+ threads: text("threads").notNull(),
315
+ key: text("key").notNull(),
316
+ state: text("state", { enum: [
317
+ "pending",
318
+ "sent",
319
+ "working",
320
+ "done",
321
+ "failed"
322
+ ] }).notNull(),
323
+ delivery: text("delivery"),
324
+ endpoint: text("endpoint"),
325
+ thread: text("thread"),
326
+ error: text("error"),
327
+ sender: text("sender").notNull(),
328
+ tokenHash: text("token_hash"),
329
+ tokenExpiresAt: integer("token_expires_at"),
330
+ createdAt: integer("created_at").notNull(),
331
+ updatedAt: integer("updated_at").notNull()
332
+ }, (table) => [
333
+ index("vitehub_comment_sends_member").on(table.member, table.createdAt),
334
+ index("vitehub_comment_sends_subject").on(table.subjectKind, table.subjectId, table.createdAt),
335
+ uniqueIndex("vitehub_comment_sends_live_key").on(table.key).where(sql`${table.state} <> 'failed'`)
336
+ ]);
337
+ /** The tables of Send, for a Database contribution. Comments adds them only when `defineComments({ send })` is on. */
338
+ const commentSendsSchema = { commentSends };
339
+ /** `commentSendsSchema` as SQLite statements that are safe to run on every start. */
340
+ const commentSendsTables = [
341
+ "CREATE TABLE IF NOT EXISTS `vitehub_comment_sends` (`id` text PRIMARY KEY NOT NULL, `member` text NOT NULL, `subject_kind` text NOT NULL, `subject_id` text NOT NULL, `page` text NOT NULL, `threads` text NOT NULL, `key` text NOT NULL, `state` text NOT NULL, `delivery` text, `endpoint` text, `thread` text, `error` text, `sender` text NOT NULL, `token_hash` text, `token_expires_at` integer, `created_at` integer NOT NULL, `updated_at` integer NOT NULL)",
342
+ "CREATE INDEX IF NOT EXISTS `vitehub_comment_sends_member` ON `vitehub_comment_sends` (`member`,`created_at`)",
343
+ "CREATE INDEX IF NOT EXISTS `vitehub_comment_sends_subject` ON `vitehub_comment_sends` (`subject_kind`,`subject_id`,`created_at`)",
344
+ "CREATE UNIQUE INDEX IF NOT EXISTS `vitehub_comment_sends_live_key` ON `vitehub_comment_sends` (`key`) WHERE `state` <> 'failed'"
345
+ ];
346
+ export { commentsSchemaGate as a, migrateComments as c, commentForge as d, commentMessages as f, commentsSqliteTables as h, commentsMigrationSql as i, commentsTables as l, commentsSchema as m, commentSendsSchema as n, commentsSchemaState as o, commentThreads as p, commentSendsTables as r, liveThreadsBackup as s, commentSends as t, commentCounters as u };