@cloudhal/comments 0.0.0-stage → 0.1.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 (46) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +86 -2
  3. package/dist/src/client/comments.browser.js +245 -0
  4. package/dist/src/client/index.d.ts +19 -0
  5. package/dist/src/client/index.js +39 -0
  6. package/dist/src/client/solver.browser.js +118 -0
  7. package/dist/src/crypto.d.ts +64 -0
  8. package/dist/src/crypto.js +170 -0
  9. package/dist/src/errors.d.ts +38 -0
  10. package/dist/src/errors.js +61 -0
  11. package/dist/src/fixtures/comment-text.json +335 -0
  12. package/dist/src/hashes.d.ts +70 -0
  13. package/dist/src/hashes.js +191 -0
  14. package/dist/src/index.d.ts +24 -0
  15. package/dist/src/index.js +24 -0
  16. package/dist/src/limits.d.ts +73 -0
  17. package/dist/src/limits.js +227 -0
  18. package/dist/src/linkify.d.ts +53 -0
  19. package/dist/src/linkify.js +142 -0
  20. package/dist/src/model.d.ts +287 -0
  21. package/dist/src/model.js +341 -0
  22. package/dist/src/normalize.d.ts +17 -0
  23. package/dist/src/normalize.js +125 -0
  24. package/dist/src/pipeline.d.ts +176 -0
  25. package/dist/src/pipeline.js +898 -0
  26. package/dist/src/pow.d.ts +86 -0
  27. package/dist/src/pow.js +239 -0
  28. package/dist/src/purge.d.ts +87 -0
  29. package/dist/src/purge.js +186 -0
  30. package/dist/src/retention.d.ts +32 -0
  31. package/dist/src/retention.js +106 -0
  32. package/dist/src/rules.d.ts +68 -0
  33. package/dist/src/rules.js +218 -0
  34. package/dist/src/settings.d.ts +108 -0
  35. package/dist/src/settings.js +347 -0
  36. package/dist/src/sql/index.d.ts +7 -0
  37. package/dist/src/sql/index.js +29 -0
  38. package/dist/src/sql/portability.d.ts +113 -0
  39. package/dist/src/sql/portability.js +514 -0
  40. package/dist/src/sql/rows.d.ts +54 -0
  41. package/dist/src/sql/rows.js +145 -0
  42. package/dist/src/sql/store.d.ts +312 -0
  43. package/dist/src/sql/store.js +2111 -0
  44. package/dist/src/thread.d.ts +35 -0
  45. package/dist/src/thread.js +64 -0
  46. package/package.json +61 -3
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Types and constants shared by the pure rules, the pipeline and the stores (RFC-0007 §4, §11,
3
+ * §15.1). The stored and returned record is the `Comment` contract from `@cloudhal/core-contracts`;
4
+ * everything here is either a store input or a view that never carries an email, an IP hash or a
5
+ * challenge (invariant C1).
6
+ */
7
+ import type { Actor, Comment, CommentLabel, CommentModeration, CommentStatus } from "@cloudhal/core-contracts";
8
+ export type CommentId = string;
9
+ export type LabelName = CommentLabel["label"];
10
+ export type LabelSource = CommentLabel["source"];
11
+ export type ModerationAction = CommentModeration["action"];
12
+ export type ReasonCode = NonNullable<CommentModeration["reasonCode"]>;
13
+ export type { Actor, Comment, CommentLabel, CommentModeration, CommentStatus };
14
+ export declare const COMMENT_STATUSES: readonly ["pending", "approved", "hidden", "spam", "deleted"];
15
+ export declare const LABELS: readonly ["spam-suspect", "toxic", "off-topic", "question", "needs-reply", "praise", "personal-data", "links", "watch-word", "instruction-like"];
16
+ export declare const REASON_CODES: readonly ["spam", "abuse", "off-topic", "personal-data", "illegal-content", "legal-order", "author-request", "erasure-request", "duplicate", "other"];
17
+ /** Replies are shown two levels deep (§4.3). A constant, not a setting. */
18
+ export declare const COMMENT_DISPLAY_DEPTH = 2;
19
+ /** Top-level comments per rendered page (§8.1). */
20
+ export declare const RENDER_PAGE_SIZE = 50;
21
+ /** Ids per moderation or label call (§4.4, §11.1). */
22
+ export declare const MAX_BULK = 50;
23
+ /**
24
+ * "Ban and hide past comments": comments hidden per batch, and batches per request (one read and
25
+ * three statements each), so one Worker request stays inside D1 Free's 50 queries (§15).
26
+ */
27
+ export declare const HIDE_PAST_BATCH = 200;
28
+ export declare const HIDE_PAST_BATCHES = 5;
29
+ export declare const MAX_LABELS = 32;
30
+ /** Plain-text limits after normalising (§6.4). */
31
+ export declare const TEXT_MIN = 2;
32
+ export declare const TEXT_MAX = 5000;
33
+ export declare const NAME_MAX = 80;
34
+ export declare const EMAIL_MAX = 254;
35
+ export declare const NOTE_MAX = 500;
36
+ export declare const REPORT_DETAILS_MAX = 1000;
37
+ /** Links above this refuse the comment (`400 invalid`), whatever `link_limit` says. */
38
+ export declare const LINK_HARD_LIMIT = 10;
39
+ /** `proof` column: how a stored comment or report got past the bot check. */
40
+ export type CommentProof = "pow" | "turnstile" | "none" | "staff" | "import";
41
+ /** A per-entry thread override (`comment_threads`). */
42
+ export type ThreadMode = "default" | "open" | "closed" | "hidden";
43
+ /** The effective state of an entry's thread (§4.5). */
44
+ export type ThreadState = "open" | "closed" | "hidden" | "off";
45
+ export interface ThreadRow {
46
+ readonly siteId: string;
47
+ readonly nodeId: string;
48
+ readonly mode: ThreadMode;
49
+ /** Explicit close time; `null` = computed from the entry's publication. */
50
+ readonly closesAt: string | null;
51
+ readonly rev: number;
52
+ readonly updatedAt: string;
53
+ readonly updatedBy: string;
54
+ }
55
+ export interface ThreadInput {
56
+ readonly mode: ThreadMode;
57
+ /** Omitted: keep the stored value. `null`: clear it. */
58
+ readonly closesAt?: string | null;
59
+ }
60
+ /** Which actions a moderation op may take (the contract's minus `erase`, which has its own path). */
61
+ export type ModerateAction = Exclude<ModerationAction, "erase">;
62
+ export interface ModerationOp {
63
+ readonly ids: readonly CommentId[];
64
+ readonly action: ModerateAction;
65
+ readonly actor: Actor;
66
+ /** Required for hide, spam and delete (§4.2). */
67
+ readonly reasonCode?: ReasonCode;
68
+ readonly note?: string;
69
+ /** The `rev` each comment had when the caller read it (CAS per comment). */
70
+ readonly expectedRevs: Readonly<Record<CommentId, number>>;
71
+ /** Set when a person approved an agent's request (ADR-0014); kept on the events. */
72
+ readonly approvalId?: string;
73
+ /** For `:own` scopes: comments on other entries answer `not_own` (RFC-0005 §9). */
74
+ readonly ownNodeIds?: readonly string[];
75
+ /** Statements appended to the same batch, last (the audit row). */
76
+ readonly audit?: readonly AuditStatement[];
77
+ }
78
+ export type ModerationError = "conflict" | "not_found" | "not_own" | "invalid_transition";
79
+ export type ModerationResult = {
80
+ readonly id: CommentId;
81
+ readonly ok: true;
82
+ readonly status: CommentStatus;
83
+ readonly rev: number;
84
+ } | {
85
+ readonly id: CommentId;
86
+ readonly ok: false;
87
+ readonly error: ModerationError;
88
+ };
89
+ export interface LabelItem {
90
+ readonly commentId: CommentId;
91
+ readonly add?: readonly LabelName[];
92
+ readonly remove?: readonly LabelName[];
93
+ /** 0–100, recorded on the labels this item adds. */
94
+ readonly confidence?: number;
95
+ }
96
+ export interface LabelOp {
97
+ readonly items: readonly LabelItem[];
98
+ readonly actor: Actor;
99
+ /** Who sets the labels. Agents may add and remove only `agent` labels. */
100
+ readonly source: Extract<LabelSource, "agent" | "person" | "classifier">;
101
+ /** Optional CAS per comment; comments without an entry are labelled at their current `rev`. */
102
+ readonly expectedRevs?: Readonly<Record<CommentId, number>>;
103
+ readonly ownNodeIds?: readonly string[];
104
+ readonly audit?: readonly AuditStatement[];
105
+ }
106
+ /** A person's reply on the site's behalf (approved at once, §4.2). */
107
+ export interface StaffReply {
108
+ /** The comment answered; it must be approved. */
109
+ readonly commentId: CommentId;
110
+ readonly text: string;
111
+ /** The person who wrote or approved the reply (`user_…`). */
112
+ readonly person: {
113
+ readonly id: string;
114
+ readonly name: string;
115
+ };
116
+ readonly badge: "author" | "team";
117
+ /** The agent that drafted the text, when one did. */
118
+ readonly draftedBy?: Actor;
119
+ readonly approvalId?: string;
120
+ readonly audit?: readonly AuditStatement[];
121
+ }
122
+ /** One extra statement a caller appends to a store batch (an audit row). */
123
+ export interface AuditStatement {
124
+ readonly sql: string;
125
+ readonly params?: readonly (string | number | null)[];
126
+ }
127
+ export interface CommentQuery {
128
+ readonly status?: CommentStatus | "reported";
129
+ readonly nodeId?: string;
130
+ readonly nodeIds?: readonly string[];
131
+ readonly label?: LabelName;
132
+ /** Comments created at or after this instant. */
133
+ readonly since?: string;
134
+ readonly cursor?: string;
135
+ /** 1–50, default 20. */
136
+ readonly limit?: number;
137
+ /** Default `newest`. */
138
+ readonly order?: "newest" | "oldest";
139
+ }
140
+ export type CommentCounts = Record<CommentStatus | "reported", number>;
141
+ export interface CountQuery {
142
+ readonly nodeId?: string;
143
+ readonly nodeIds?: readonly string[];
144
+ }
145
+ /** What the pipeline hands the store after step 11 (§6.2 step 12). */
146
+ export interface PreparedSubmission {
147
+ readonly id: CommentId;
148
+ readonly nodeId: string;
149
+ readonly parentId?: CommentId;
150
+ readonly inReplyToId?: CommentId;
151
+ readonly name: string;
152
+ readonly text: string;
153
+ readonly status: "pending" | "spam";
154
+ readonly labels: readonly CommentLabel[];
155
+ readonly moderation?: CommentModeration;
156
+ readonly email?: PreparedEmail;
157
+ readonly ipHmac: string | null;
158
+ readonly challengeId: string | null;
159
+ readonly proof: Extract<CommentProof, "pow" | "turnstile" | "none">;
160
+ readonly createdAt: string;
161
+ /** When this month's salt was not stored yet: insert it in the same batch (§9.2). */
162
+ readonly newSalt?: {
163
+ readonly month: string;
164
+ readonly salt: string;
165
+ };
166
+ }
167
+ export interface PreparedEmail {
168
+ /** A fresh `cmr_…` id; the stored id wins when the address is already known. */
169
+ readonly id: string;
170
+ readonly hmac: string;
171
+ readonly ciphertext: string;
172
+ readonly iv: string;
173
+ }
174
+ export type ReportReason = "spam" | "abuse" | "personal-data" | "illegal-content" | "other";
175
+ export interface PreparedReport {
176
+ readonly id: string;
177
+ readonly commentId: CommentId;
178
+ readonly reason: ReportReason;
179
+ readonly details: string | null;
180
+ readonly email?: PreparedEmail;
181
+ readonly ipHmac: string | null;
182
+ readonly challengeId: string | null;
183
+ readonly createdAt: string;
184
+ readonly newSalt?: {
185
+ readonly month: string;
186
+ readonly salt: string;
187
+ };
188
+ }
189
+ export type SubmitResult = {
190
+ readonly id: string;
191
+ } | {
192
+ readonly duplicate: true;
193
+ }
194
+ /** Another isolate stored a different salt for the month first; recompute and retry once. */
195
+ | {
196
+ readonly saltConflict: true;
197
+ };
198
+ export interface EraseTarget {
199
+ /** Every comment and report of a commenter (`cmr_…`). */
200
+ readonly commenterId?: string;
201
+ /** Or these comments (a guest who left no email, §9.4). */
202
+ readonly commentIds?: readonly CommentId[];
203
+ }
204
+ export interface EraseResult {
205
+ readonly comments: number;
206
+ readonly reports: number;
207
+ readonly emailDeleted: boolean;
208
+ /** Entries whose live page must be purged (takedown). */
209
+ readonly nodeIds: readonly string[];
210
+ }
211
+ /** A rendered page of a thread (§8.1): approved comments, oldest first, two levels deep. */
212
+ export interface RenderPage {
213
+ readonly items: readonly RenderItem[];
214
+ /** Approved comments in the thread. */
215
+ readonly total: number;
216
+ readonly page: number;
217
+ readonly pageCount: number;
218
+ }
219
+ export type RenderItem = {
220
+ readonly comment: Comment;
221
+ readonly replies: readonly Comment[];
222
+ }
223
+ /** A top-level comment no longer public with approved replies under it: "This comment was removed". */
224
+ | {
225
+ readonly removed: {
226
+ readonly id: CommentId;
227
+ readonly createdAt: string;
228
+ };
229
+ readonly replies: readonly Comment[];
230
+ };
231
+ export interface CommentEvent {
232
+ readonly id: string;
233
+ readonly commentId: CommentId;
234
+ readonly action: ModerationAction | "label" | "reply";
235
+ readonly actor: Actor;
236
+ readonly reasonCode: ReasonCode | null;
237
+ readonly note: string | null;
238
+ readonly automated: boolean;
239
+ readonly approvalId: string | null;
240
+ readonly op: string | null;
241
+ readonly at: string;
242
+ }
243
+ export interface CommentReport {
244
+ readonly id: string;
245
+ readonly commentId: CommentId;
246
+ readonly reason: ReportReason;
247
+ /** Untrusted reader text. */
248
+ readonly details: string | null;
249
+ readonly hasEmail: boolean;
250
+ readonly createdAt: string;
251
+ readonly resolvedAt: string | null;
252
+ readonly resolvedBy: string | null;
253
+ readonly resolution: string | null;
254
+ }
255
+ export type BanSubject = "email" | "ip";
256
+ export interface CommentBan {
257
+ readonly id: string;
258
+ readonly subject: BanSubject;
259
+ readonly until: string | null;
260
+ readonly reasonCode: ReasonCode;
261
+ readonly note: string | null;
262
+ readonly by: Actor;
263
+ readonly at: string;
264
+ }
265
+ /** A commenter as people with `commenters:data` see them (§9.4). */
266
+ export interface Commenter {
267
+ readonly id: string;
268
+ readonly email: string;
269
+ readonly firstSeen: string;
270
+ readonly commentIds: readonly CommentId[];
271
+ readonly reportIds: readonly string[];
272
+ }
273
+ /** UTC month `YYYY-MM`. */
274
+ export declare const utcMonth: (d: Date) => string;
275
+ /** UTC day `YYYY-MM-DD`. */
276
+ export declare const utcDay: (d: Date) => string;
277
+ /** 00:00 UTC of `d`'s day, as an ISO instant. */
278
+ export declare const utcDayStart: (d: Date) => string;
279
+ /** Adds milliseconds to an ISO instant. */
280
+ export declare const addMs: (iso: string | Date, ms: number) => string;
281
+ export declare const DAY_MS = 86400000;
282
+ export declare const HOUR_MS = 3600000;
283
+ export declare const MINUTE_MS = 60000;
284
+ /** Coerces a D1/SQLite number (which may arrive as a string or bigint) to a number. */
285
+ export declare const num: (v: unknown) => number;
286
+ /** True for a human actor (`approve`, `restore` and replies need one, §0.1 decision 3). */
287
+ export declare const isPerson: (actor: Actor) => boolean;
@@ -0,0 +1,341 @@
1
+ /**
2
+ * Types and constants shared by the pure rules, the pipeline and the stores (RFC-0007 §4, §11,
3
+ * §15.1). The stored and returned record is the `Comment` contract from `@cloudhal/core-contracts`;
4
+ * everything here is either a store input or a view that never carries an email, an IP hash or a
5
+ * challenge (invariant C1).
6
+ */
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ export const COMMENT_STATUSES = [
23
+ "pending",
24
+ "approved",
25
+ "hidden",
26
+ "spam",
27
+ "deleted",
28
+ ] ;
29
+
30
+ export const LABELS = [
31
+ "spam-suspect",
32
+ "toxic",
33
+ "off-topic",
34
+ "question",
35
+ "needs-reply",
36
+ "praise",
37
+ "personal-data",
38
+ "links",
39
+ "watch-word",
40
+ "instruction-like",
41
+ ] ;
42
+
43
+ export const REASON_CODES = [
44
+ "spam",
45
+ "abuse",
46
+ "off-topic",
47
+ "personal-data",
48
+ "illegal-content",
49
+ "legal-order",
50
+ "author-request",
51
+ "erasure-request",
52
+ "duplicate",
53
+ "other",
54
+ ] ;
55
+
56
+ /** Replies are shown two levels deep (§4.3). A constant, not a setting. */
57
+ export const COMMENT_DISPLAY_DEPTH = 2;
58
+ /** Top-level comments per rendered page (§8.1). */
59
+ export const RENDER_PAGE_SIZE = 50;
60
+ /** Ids per moderation or label call (§4.4, §11.1). */
61
+ export const MAX_BULK = 50;
62
+ /**
63
+ * "Ban and hide past comments": comments hidden per batch, and batches per request (one read and
64
+ * three statements each), so one Worker request stays inside D1 Free's 50 queries (§15).
65
+ */
66
+ export const HIDE_PAST_BATCH = 200;
67
+ export const HIDE_PAST_BATCHES = 5;
68
+ export const MAX_LABELS = 32;
69
+ /** Plain-text limits after normalising (§6.4). */
70
+ export const TEXT_MIN = 2;
71
+ export const TEXT_MAX = 5000;
72
+ export const NAME_MAX = 80;
73
+ export const EMAIL_MAX = 254;
74
+ export const NOTE_MAX = 500;
75
+ export const REPORT_DETAILS_MAX = 1000;
76
+ /** Links above this refuse the comment (`400 invalid`), whatever `link_limit` says. */
77
+ export const LINK_HARD_LIMIT = 10;
78
+
79
+ /** `proof` column: how a stored comment or report got past the bot check. */
80
+
81
+
82
+ /** A per-entry thread override (`comment_threads`). */
83
+
84
+ /** The effective state of an entry's thread (§4.5). */
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+ /** Which actions a moderation op may take (the contract's minus `erase`, which has its own path). */
105
+
106
+
107
+
108
+
109
+
110
+
111
+  
112
+
113
+
114
+
115
+
116
+
117
+
118
+  
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+  
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+ /** A person's reply on the site's behalf (approved at once, §4.2). */
155
+
156
+
157
+
158
+
159
+  
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+ /** One extra statement a caller appends to a store batch (an audit row). */
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+  
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+ /** What the pipeline hands the store after step 11 (§6.2 step 12). */
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+  
212
+
213
+
214
+
215
+
216
+  
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+  
245
+
246
+  
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+ /** A rendered page of a thread (§8.1): approved comments, oldest first, two levels deep. */
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+ /** A commenter as people with `commenters:data` see them (§9.4). */
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+ /** UTC month `YYYY-MM`. */
323
+ export const utcMonth = (d ) => d.toISOString().slice(0, 7);
324
+ /** UTC day `YYYY-MM-DD`. */
325
+ export const utcDay = (d ) => d.toISOString().slice(0, 10);
326
+ /** 00:00 UTC of `d`'s day, as an ISO instant. */
327
+ export const utcDayStart = (d ) => `${utcDay(d)}T00:00:00.000Z`;
328
+
329
+ /** Adds milliseconds to an ISO instant. */
330
+ export const addMs = (iso , ms ) =>
331
+ new Date((typeof iso === "string" ? Date.parse(iso) : iso.getTime()) + ms).toISOString();
332
+
333
+ export const DAY_MS = 86_400_000;
334
+ export const HOUR_MS = 3_600_000;
335
+ export const MINUTE_MS = 60_000;
336
+
337
+ /** Coerces a D1/SQLite number (which may arrive as a string or bigint) to a number. */
338
+ export const num = (v ) => (v === null || v === undefined ? 0 : Number(v));
339
+
340
+ /** True for a human actor (`approve`, `restore` and replies need one, §0.1 decision 3). */
341
+ export const isPerson = (actor ) => actor.kind === "human";
@@ -0,0 +1,17 @@
1
+ /** The contract's own refusal set (for tests and defensive checks). */
2
+ export declare const CONTRACT_FORBIDDEN: RegExp;
3
+ /** Length in Unicode code points (what JSON Schema `minLength`/`maxLength` count). */
4
+ export declare const codePoints: (s: string) => number;
5
+ /** Cuts to at most `max` code points without splitting a surrogate pair. */
6
+ export declare function cutCodePoints(s: string, max: number): string;
7
+ export declare function normalizeText(raw: string): string;
8
+ export declare function normalizeName(raw: string): string;
9
+ /** `undefined` for an empty value (forms send empty strings for optional fields). */
10
+ export declare function normalizeEmail(raw: string | undefined): string | undefined;
11
+ export type FieldProblem = "required" | "too_short" | "too_long" | "link_in_name" | "too_many_links" | "invalid";
12
+ /** Checks a normalised name (§6.4): 1–80 characters, no `://`, no `www.`. */
13
+ export declare function checkName(name: string): FieldProblem | undefined;
14
+ /** Checks a normalised text (§6.4): 2–5,000 characters. */
15
+ export declare function checkText(text: string): FieldProblem | undefined;
16
+ /** Checks a normalised email: ≤ 254, one `@`, no whitespace or control characters. */
17
+ export declare function checkEmail(email: string): FieldProblem | undefined;