@actuate-media/cms-core 0.20.0 → 0.22.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.
- package/LICENSE +21 -21
- package/dist/__tests__/actions/document-crud.test.js +74 -2
- package/dist/__tests__/actions/document-crud.test.js.map +1 -1
- package/dist/__tests__/api/cache-headers.test.d.ts +2 -0
- package/dist/__tests__/api/cache-headers.test.d.ts.map +1 -0
- package/dist/__tests__/api/cache-headers.test.js +135 -0
- package/dist/__tests__/api/cache-headers.test.js.map +1 -0
- package/dist/__tests__/api/comments-routes.test.d.ts +2 -0
- package/dist/__tests__/api/comments-routes.test.d.ts.map +1 -0
- package/dist/__tests__/api/comments-routes.test.js +266 -0
- package/dist/__tests__/api/comments-routes.test.js.map +1 -0
- package/dist/__tests__/api/notifications-routes.test.d.ts +2 -0
- package/dist/__tests__/api/notifications-routes.test.d.ts.map +1 -0
- package/dist/__tests__/api/notifications-routes.test.js +225 -0
- package/dist/__tests__/api/notifications-routes.test.js.map +1 -0
- package/dist/__tests__/api/preview-and-scheduling.test.js +53 -0
- package/dist/__tests__/api/preview-and-scheduling.test.js.map +1 -1
- package/dist/__tests__/fields/component-block.test.d.ts +2 -0
- package/dist/__tests__/fields/component-block.test.d.ts.map +1 -0
- package/dist/__tests__/fields/component-block.test.js +129 -0
- package/dist/__tests__/fields/component-block.test.js.map +1 -0
- package/dist/__tests__/fields/relations.test.js +62 -8
- package/dist/__tests__/fields/relations.test.js.map +1 -1
- package/dist/__tests__/preview-event-bus.test.d.ts +2 -0
- package/dist/__tests__/preview-event-bus.test.d.ts.map +1 -0
- package/dist/__tests__/preview-event-bus.test.js +89 -0
- package/dist/__tests__/preview-event-bus.test.js.map +1 -0
- package/dist/__tests__/preview-sse-stream.test.d.ts +2 -0
- package/dist/__tests__/preview-sse-stream.test.d.ts.map +1 -0
- package/dist/__tests__/preview-sse-stream.test.js +117 -0
- package/dist/__tests__/preview-sse-stream.test.js.map +1 -0
- package/dist/__tests__/realtime/comments.test.d.ts +2 -0
- package/dist/__tests__/realtime/comments.test.d.ts.map +1 -0
- package/dist/__tests__/realtime/comments.test.js +508 -0
- package/dist/__tests__/realtime/comments.test.js.map +1 -0
- package/dist/__tests__/realtime/gateway-ws-integration.test.d.ts +2 -0
- package/dist/__tests__/realtime/gateway-ws-integration.test.d.ts.map +1 -0
- package/dist/__tests__/realtime/gateway-ws-integration.test.js +170 -0
- package/dist/__tests__/realtime/gateway-ws-integration.test.js.map +1 -0
- package/dist/__tests__/realtime/gateway.test.d.ts +2 -0
- package/dist/__tests__/realtime/gateway.test.d.ts.map +1 -0
- package/dist/__tests__/realtime/gateway.test.js +402 -0
- package/dist/__tests__/realtime/gateway.test.js.map +1 -0
- package/dist/__tests__/realtime/notifications.test.d.ts +2 -0
- package/dist/__tests__/realtime/notifications.test.d.ts.map +1 -0
- package/dist/__tests__/realtime/notifications.test.js +317 -0
- package/dist/__tests__/realtime/notifications.test.js.map +1 -0
- package/dist/__tests__/realtime/persistence.test.d.ts +2 -0
- package/dist/__tests__/realtime/persistence.test.d.ts.map +1 -0
- package/dist/__tests__/realtime/persistence.test.js +124 -0
- package/dist/__tests__/realtime/persistence.test.js.map +1 -0
- package/dist/__tests__/schema-preview-features.test.d.ts +2 -0
- package/dist/__tests__/schema-preview-features.test.d.ts.map +1 -0
- package/dist/__tests__/schema-preview-features.test.js +42 -0
- package/dist/__tests__/schema-preview-features.test.js.map +1 -0
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +71 -26
- package/dist/actions.js.map +1 -1
- package/dist/api/handlers.d.ts.map +1 -1
- package/dist/api/handlers.js +734 -188
- package/dist/api/handlers.js.map +1 -1
- package/dist/cache/__tests__/http.test.d.ts +2 -0
- package/dist/cache/__tests__/http.test.d.ts.map +1 -0
- package/dist/cache/__tests__/http.test.js +285 -0
- package/dist/cache/__tests__/http.test.js.map +1 -0
- package/dist/cache/http.d.ts +119 -0
- package/dist/cache/http.d.ts.map +1 -0
- package/dist/cache/http.js +188 -0
- package/dist/cache/http.js.map +1 -0
- package/dist/config/types.d.ts +153 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/fields/component-block.d.ts +23 -0
- package/dist/fields/component-block.d.ts.map +1 -0
- package/dist/fields/component-block.js +137 -0
- package/dist/fields/component-block.js.map +1 -0
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +2 -0
- package/dist/fields/index.js.map +1 -1
- package/dist/fields/relations.d.ts.map +1 -1
- package/dist/fields/relations.js +43 -11
- package/dist/fields/relations.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/page-builder/schema.d.ts +2 -2
- package/dist/preview/event-bus.d.ts +39 -0
- package/dist/preview/event-bus.d.ts.map +1 -0
- package/dist/preview/event-bus.js +73 -0
- package/dist/preview/event-bus.js.map +1 -0
- package/dist/preview/sse-stream.d.ts +50 -0
- package/dist/preview/sse-stream.d.ts.map +1 -0
- package/dist/preview/sse-stream.js +120 -0
- package/dist/preview/sse-stream.js.map +1 -0
- package/dist/realtime/comments.d.ts +217 -0
- package/dist/realtime/comments.d.ts.map +1 -0
- package/dist/realtime/comments.js +282 -0
- package/dist/realtime/comments.js.map +1 -0
- package/dist/realtime/gateway.d.ts +107 -0
- package/dist/realtime/gateway.d.ts.map +1 -0
- package/dist/realtime/gateway.js +215 -0
- package/dist/realtime/gateway.js.map +1 -0
- package/dist/realtime/index.d.ts +18 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +18 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/realtime/notifications.d.ts +209 -0
- package/dist/realtime/notifications.d.ts.map +1 -0
- package/dist/realtime/notifications.js +209 -0
- package/dist/realtime/notifications.js.map +1 -0
- package/dist/realtime/persistence.d.ts +72 -0
- package/dist/realtime/persistence.d.ts.map +1 -0
- package/dist/realtime/persistence.js +53 -0
- package/dist/realtime/persistence.js.map +1 -0
- package/package.json +13 -4
- package/prisma/cms-schema.prisma +437 -306
- package/prisma/migrations/0001_init/migration.sql +384 -384
- package/prisma/migrations/0002_folders/migration.sql +39 -39
- package/prisma/migrations/0003_search_and_webhooks/migration.sql +50 -50
- package/prisma/migrations/0004_script_tags/migration.sql +21 -21
- package/prisma/migrations/0005_password_reset_tokens/migration.sql +20 -20
- package/prisma/migrations/0006_page_builder/migration.sql +38 -38
- package/prisma/migrations/migration_lock.toml +3 -3
- package/prisma/schema.prisma +563 -549
- package/generated/browser.ts +0 -109
- package/generated/client.ts +0 -133
- package/generated/commonInputTypes.ts +0 -709
- package/generated/enums.ts +0 -125
- package/generated/internal/class.ts +0 -376
- package/generated/internal/prismaNamespace.ts +0 -2617
- package/generated/internal/prismaNamespaceBrowser.ts +0 -611
- package/generated/models/ApiKey.ts +0 -1550
- package/generated/models/AuditLog.ts +0 -1206
- package/generated/models/BackupRecord.ts +0 -1250
- package/generated/models/ContentLock.ts +0 -1472
- package/generated/models/ContentTemplate.ts +0 -1416
- package/generated/models/Document.ts +0 -3005
- package/generated/models/Folder.ts +0 -1904
- package/generated/models/FormSubmission.ts +0 -1200
- package/generated/models/InAppNotification.ts +0 -1457
- package/generated/models/Media.ts +0 -2340
- package/generated/models/MediaUsage.ts +0 -1472
- package/generated/models/OAuthAccount.ts +0 -1463
- package/generated/models/Redirect.ts +0 -1284
- package/generated/models/Session.ts +0 -1492
- package/generated/models/Site.ts +0 -1206
- package/generated/models/User.ts +0 -3513
- package/generated/models/Version.ts +0 -1511
- package/generated/models/WorkflowState.ts +0 -1514
- package/generated/models.ts +0 -29
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document comments — anchored, threaded discussion attached to documents.
|
|
3
|
+
*
|
|
4
|
+
* This module owns the *pure* comment logic: validation, normalization, and
|
|
5
|
+
* the storage-agnostic CRUD entry points. The HTTP layer in `api/handlers.ts`
|
|
6
|
+
* is a thin wrapper that handles auth + JSON envelope; everything else lives
|
|
7
|
+
* here so the unit tests don't need a Request loop.
|
|
8
|
+
*
|
|
9
|
+
* Storage shape — see `packages/cms-core/prisma/cms-schema.prisma`,
|
|
10
|
+
* model `DocumentComment`. We require only a minimal Prisma surface so the
|
|
11
|
+
* helpers can be tested against an in-memory fake.
|
|
12
|
+
*
|
|
13
|
+
* Anchors are Yjs *relative positions* serialised as Base64. Storing them in
|
|
14
|
+
* a JSON column (rather than Bytes) keeps the REST contract trivial: the
|
|
15
|
+
* client posts `{ from: "AQ...", to: "AQ..." }`, we round-trip the strings.
|
|
16
|
+
* Slice 5 reconstitutes them with `Y.createAbsolutePositionFromRelativePosition`
|
|
17
|
+
* to paint anchored highlights in the editor; nothing in the API layer needs
|
|
18
|
+
* to interpret the bytes.
|
|
19
|
+
*/
|
|
20
|
+
/** Maximum length we accept for a comment body (markdown source). */
|
|
21
|
+
export const MAX_COMMENT_BODY_LENGTH = 10_000;
|
|
22
|
+
/** Cap on returned comments per `listComments` call. */
|
|
23
|
+
export const MAX_COMMENT_LIST_PAGE_SIZE = 200;
|
|
24
|
+
/** Default page size when the caller does not request one. */
|
|
25
|
+
export const DEFAULT_COMMENT_LIST_PAGE_SIZE = 100;
|
|
26
|
+
const MAX_ANCHOR_FIELD_LENGTH = 4_096;
|
|
27
|
+
function err(code, message) {
|
|
28
|
+
return { code, message };
|
|
29
|
+
}
|
|
30
|
+
function toIso(value) {
|
|
31
|
+
if (value === null)
|
|
32
|
+
return null;
|
|
33
|
+
if (value instanceof Date)
|
|
34
|
+
return value.toISOString();
|
|
35
|
+
// Tolerate fakes that already store ISO strings.
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function toIsoRequired(value) {
|
|
39
|
+
return value instanceof Date ? value.toISOString() : value;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Strip a comment row down to the public DTO. Centralised so the response
|
|
43
|
+
* shape stays consistent between create / list / update / resolve.
|
|
44
|
+
*/
|
|
45
|
+
export function toCommentDTO(row) {
|
|
46
|
+
return {
|
|
47
|
+
id: row.id,
|
|
48
|
+
documentId: row.documentId,
|
|
49
|
+
parentId: row.parentId,
|
|
50
|
+
userId: row.userId,
|
|
51
|
+
body: row.body,
|
|
52
|
+
anchor: row.anchor,
|
|
53
|
+
resolvedAt: toIso(row.resolvedAt),
|
|
54
|
+
resolvedById: row.resolvedById,
|
|
55
|
+
deletedAt: toIso(row.deletedAt),
|
|
56
|
+
createdAt: toIsoRequired(row.createdAt),
|
|
57
|
+
updatedAt: toIsoRequired(row.updatedAt),
|
|
58
|
+
isResolved: row.resolvedAt !== null,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parse + validate an anchor blob coming from an untrusted client. Returns
|
|
63
|
+
* `null` when the caller did not provide one (document-level comments).
|
|
64
|
+
*/
|
|
65
|
+
export function parseAnchor(input) {
|
|
66
|
+
if (input === undefined || input === null) {
|
|
67
|
+
return { ok: true, value: null };
|
|
68
|
+
}
|
|
69
|
+
if (typeof input !== 'object' || Array.isArray(input)) {
|
|
70
|
+
return { ok: false, error: err('validation', 'anchor must be an object with `from` and `to`') };
|
|
71
|
+
}
|
|
72
|
+
const obj = input;
|
|
73
|
+
if (typeof obj.from !== 'string' || typeof obj.to !== 'string') {
|
|
74
|
+
return { ok: false, error: err('validation', 'anchor.from and anchor.to must be strings') };
|
|
75
|
+
}
|
|
76
|
+
if (obj.from.length === 0 ||
|
|
77
|
+
obj.from.length > MAX_ANCHOR_FIELD_LENGTH ||
|
|
78
|
+
obj.to.length === 0 ||
|
|
79
|
+
obj.to.length > MAX_ANCHOR_FIELD_LENGTH) {
|
|
80
|
+
return {
|
|
81
|
+
ok: false,
|
|
82
|
+
error: err('validation', `anchor fields must be 1..${MAX_ANCHOR_FIELD_LENGTH} chars`),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { ok: true, value: { from: obj.from, to: obj.to } };
|
|
86
|
+
}
|
|
87
|
+
/** Normalize whitespace + enforce length limits on a posted body. */
|
|
88
|
+
export function normaliseBody(input) {
|
|
89
|
+
if (typeof input !== 'string') {
|
|
90
|
+
return { ok: false, error: err('validation', 'body must be a string') };
|
|
91
|
+
}
|
|
92
|
+
const trimmed = input.trim();
|
|
93
|
+
if (trimmed.length === 0) {
|
|
94
|
+
return { ok: false, error: err('validation', 'body cannot be empty') };
|
|
95
|
+
}
|
|
96
|
+
if (trimmed.length > MAX_COMMENT_BODY_LENGTH) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
error: err('validation', `body cannot exceed ${MAX_COMMENT_BODY_LENGTH} characters`),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return { ok: true, value: trimmed };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a comment. Enforces:
|
|
106
|
+
* - body validation (non-empty, length-capped)
|
|
107
|
+
* - anchor shape validation
|
|
108
|
+
* - single-level threading: a `parentId` must reference an existing,
|
|
109
|
+
* non-deleted, non-reply comment under the same document.
|
|
110
|
+
*
|
|
111
|
+
* The caller (HTTP handler) is responsible for auth — once we reach here we
|
|
112
|
+
* trust the supplied `userId`.
|
|
113
|
+
*/
|
|
114
|
+
export async function createComment(db, input) {
|
|
115
|
+
if (typeof input.documentId !== 'string' || input.documentId.length === 0) {
|
|
116
|
+
return { ok: false, error: err('validation', 'documentId is required') };
|
|
117
|
+
}
|
|
118
|
+
if (typeof input.userId !== 'string' || input.userId.length === 0) {
|
|
119
|
+
return { ok: false, error: err('validation', 'userId is required') };
|
|
120
|
+
}
|
|
121
|
+
const body = normaliseBody(input.body);
|
|
122
|
+
if (!body.ok)
|
|
123
|
+
return body;
|
|
124
|
+
const anchor = parseAnchor(input.anchor);
|
|
125
|
+
if (!anchor.ok)
|
|
126
|
+
return anchor;
|
|
127
|
+
let parentId = null;
|
|
128
|
+
if (input.parentId != null && input.parentId !== '') {
|
|
129
|
+
if (typeof input.parentId !== 'string') {
|
|
130
|
+
return { ok: false, error: err('validation', 'parentId must be a string') };
|
|
131
|
+
}
|
|
132
|
+
const parent = await db.documentComment.findUnique({ where: { id: input.parentId } });
|
|
133
|
+
if (!parent) {
|
|
134
|
+
return { ok: false, error: err('not_found', 'parent comment not found') };
|
|
135
|
+
}
|
|
136
|
+
if (parent.documentId !== input.documentId) {
|
|
137
|
+
return { ok: false, error: err('validation', 'parent comment is on a different document') };
|
|
138
|
+
}
|
|
139
|
+
if (parent.parentId !== null) {
|
|
140
|
+
return {
|
|
141
|
+
ok: false,
|
|
142
|
+
error: err('validation', 'replies cannot themselves be replied to (one level only)'),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (parent.deletedAt !== null) {
|
|
146
|
+
return { ok: false, error: err('not_found', 'parent comment has been deleted') };
|
|
147
|
+
}
|
|
148
|
+
parentId = parent.id;
|
|
149
|
+
}
|
|
150
|
+
const row = await db.documentComment.create({
|
|
151
|
+
data: {
|
|
152
|
+
documentId: input.documentId,
|
|
153
|
+
userId: input.userId,
|
|
154
|
+
body: body.value,
|
|
155
|
+
anchor: anchor.value,
|
|
156
|
+
parentId,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
return { ok: true, value: toCommentDTO(row) };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* List comments for a document. Returns the rows ordered oldest-first so a
|
|
163
|
+
* naive client can render threads top-down. Pagination is "show the first
|
|
164
|
+
* N"; the higher-level UI in slice 5 will switch to cursor pagination once
|
|
165
|
+
* volume justifies it.
|
|
166
|
+
*/
|
|
167
|
+
export async function listComments(db, filter) {
|
|
168
|
+
if (typeof filter.documentId !== 'string' || filter.documentId.length === 0) {
|
|
169
|
+
return { ok: false, error: err('validation', 'documentId is required') };
|
|
170
|
+
}
|
|
171
|
+
const where = { documentId: filter.documentId };
|
|
172
|
+
if (!filter.includeResolved)
|
|
173
|
+
where.resolvedAt = null;
|
|
174
|
+
if (!filter.includeDeleted)
|
|
175
|
+
where.deletedAt = null;
|
|
176
|
+
const pageSize = clampPageSize(filter.pageSize);
|
|
177
|
+
const rows = await db.documentComment.findMany({
|
|
178
|
+
where,
|
|
179
|
+
orderBy: { createdAt: 'asc' },
|
|
180
|
+
take: pageSize,
|
|
181
|
+
});
|
|
182
|
+
return { ok: true, value: rows.map(toCommentDTO) };
|
|
183
|
+
}
|
|
184
|
+
function clampPageSize(n) {
|
|
185
|
+
if (n === undefined || Number.isNaN(n))
|
|
186
|
+
return DEFAULT_COMMENT_LIST_PAGE_SIZE;
|
|
187
|
+
if (n < 1)
|
|
188
|
+
return 1;
|
|
189
|
+
if (n > MAX_COMMENT_LIST_PAGE_SIZE)
|
|
190
|
+
return MAX_COMMENT_LIST_PAGE_SIZE;
|
|
191
|
+
return Math.floor(n);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Edit a comment body. Only the author can edit, and only if the comment is
|
|
195
|
+
* not soft-deleted. Resolved threads remain editable so a moderator can
|
|
196
|
+
* append context.
|
|
197
|
+
*/
|
|
198
|
+
export async function updateComment(db, id, input, actor) {
|
|
199
|
+
const existing = await db.documentComment.findUnique({ where: { id } });
|
|
200
|
+
if (!existing || existing.deletedAt !== null) {
|
|
201
|
+
return { ok: false, error: err('not_found', 'comment not found') };
|
|
202
|
+
}
|
|
203
|
+
if (!actor.isAdmin && existing.userId !== actor.userId) {
|
|
204
|
+
return { ok: false, error: err('forbidden', 'only the author can edit this comment') };
|
|
205
|
+
}
|
|
206
|
+
const body = normaliseBody(input.body);
|
|
207
|
+
if (!body.ok)
|
|
208
|
+
return body;
|
|
209
|
+
const row = await db.documentComment.update({
|
|
210
|
+
where: { id },
|
|
211
|
+
data: { body: body.value },
|
|
212
|
+
});
|
|
213
|
+
return { ok: true, value: toCommentDTO(row) };
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Mark a comment thread as resolved. Either the comment's author or any
|
|
217
|
+
* admin / write-role caller can resolve. Resolving an already-resolved
|
|
218
|
+
* comment is a no-op (returns the existing row) so a duplicate click in the
|
|
219
|
+
* UI does not surface as an error.
|
|
220
|
+
*/
|
|
221
|
+
export async function resolveComment(db, id, actor) {
|
|
222
|
+
const existing = await db.documentComment.findUnique({ where: { id } });
|
|
223
|
+
if (!existing || existing.deletedAt !== null) {
|
|
224
|
+
return { ok: false, error: err('not_found', 'comment not found') };
|
|
225
|
+
}
|
|
226
|
+
if (!actor.canResolve && existing.userId !== actor.userId) {
|
|
227
|
+
return { ok: false, error: err('forbidden', 'not allowed to resolve this comment') };
|
|
228
|
+
}
|
|
229
|
+
if (existing.resolvedAt !== null) {
|
|
230
|
+
return { ok: true, value: toCommentDTO(existing) };
|
|
231
|
+
}
|
|
232
|
+
const row = await db.documentComment.update({
|
|
233
|
+
where: { id },
|
|
234
|
+
data: { resolvedAt: new Date(), resolvedById: actor.userId },
|
|
235
|
+
});
|
|
236
|
+
return { ok: true, value: toCommentDTO(row) };
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Re-open a resolved thread. Same permission as `resolveComment`.
|
|
240
|
+
*/
|
|
241
|
+
export async function reopenComment(db, id, actor) {
|
|
242
|
+
const existing = await db.documentComment.findUnique({ where: { id } });
|
|
243
|
+
if (!existing || existing.deletedAt !== null) {
|
|
244
|
+
return { ok: false, error: err('not_found', 'comment not found') };
|
|
245
|
+
}
|
|
246
|
+
if (!actor.canResolve && existing.userId !== actor.userId) {
|
|
247
|
+
return { ok: false, error: err('forbidden', 'not allowed to re-open this comment') };
|
|
248
|
+
}
|
|
249
|
+
if (existing.resolvedAt === null) {
|
|
250
|
+
return { ok: true, value: toCommentDTO(existing) };
|
|
251
|
+
}
|
|
252
|
+
const row = await db.documentComment.update({
|
|
253
|
+
where: { id },
|
|
254
|
+
data: { resolvedAt: null, resolvedById: null },
|
|
255
|
+
});
|
|
256
|
+
return { ok: true, value: toCommentDTO(row) };
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Soft-delete a comment. The row is preserved (so replies don't lose their
|
|
260
|
+
* thread root) but stops being returned by `listComments` unless explicitly
|
|
261
|
+
* requested with `includeDeleted: true`.
|
|
262
|
+
*
|
|
263
|
+
* Only the author or an admin may delete.
|
|
264
|
+
*/
|
|
265
|
+
export async function deleteComment(db, id, actor) {
|
|
266
|
+
const existing = await db.documentComment.findUnique({ where: { id } });
|
|
267
|
+
if (!existing) {
|
|
268
|
+
return { ok: false, error: err('not_found', 'comment not found') };
|
|
269
|
+
}
|
|
270
|
+
if (!actor.isAdmin && existing.userId !== actor.userId) {
|
|
271
|
+
return { ok: false, error: err('forbidden', 'only the author can delete this comment') };
|
|
272
|
+
}
|
|
273
|
+
if (existing.deletedAt !== null) {
|
|
274
|
+
return { ok: true, value: toCommentDTO(existing) };
|
|
275
|
+
}
|
|
276
|
+
const row = await db.documentComment.update({
|
|
277
|
+
where: { id },
|
|
278
|
+
data: { deletedAt: new Date() },
|
|
279
|
+
});
|
|
280
|
+
return { ok: true, value: toCommentDTO(row) };
|
|
281
|
+
}
|
|
282
|
+
//# sourceMappingURL=comments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comments.js","sourceRoot":"","sources":["../../src/realtime/comments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,qEAAqE;AACrE,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAA;AAE7C,wDAAwD;AACxD,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,CAAA;AAE7C,8DAA8D;AAC9D,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,CAAA;AAejD,MAAM,uBAAuB,GAAG,KAAK,CAAA;AAiHrC,SAAS,GAAG,CAAC,IAA2B,EAAE,OAAe;IACvD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAA;AAC1B,CAAC;AAED,SAAS,KAAK,CAAC,KAA2B;IACxC,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IAC/B,IAAI,KAAK,Y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AAI,QAAQ,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QACvD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,WAAW,EAAE,yCAAyC,CAAC,EAAE,CAAA;IAC1F,CAAC;IACD,IAAI,QAAQ,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;QAChC,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAA;IACpD,CAAC;IACD,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC;QAC1C,KAAK,EAAE,EAAE,EAAE,EAAE;QACb,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,EAAE;KAChC,CAAC,CAAA;IACF,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,CAAC,GAAG,CAAC,EAAE,CAAA;AAC/C,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime gateway for `@actuate-media/realtime` rooms.
|
|
3
|
+
*
|
|
4
|
+
* The gateway is **transport-agnostic by design**: it does not depend on
|
|
5
|
+
* `ws`, `uWebSockets.js`, Cloudflare Workers, or any specific framework.
|
|
6
|
+
* Consumers create a `WebSocketLike` adapter around their server's socket
|
|
7
|
+
* primitive and pass it to `gateway.handleConnection(socket, req)`.
|
|
8
|
+
*
|
|
9
|
+
* Responsibilities:
|
|
10
|
+
* 1. Authenticate the connection (delegated to `options.authenticate`).
|
|
11
|
+
* 2. Look up (or lazily create) the `Room` for the document.
|
|
12
|
+
* 3. Pump bytes between the socket and the room.
|
|
13
|
+
* 4. Persist edits via the configured `DocumentPersistence`.
|
|
14
|
+
* 5. Reap idle rooms (no connections + no pending writes) to bound memory.
|
|
15
|
+
*
|
|
16
|
+
* The companion file `persistence.ts` provides
|
|
17
|
+
* `createPrismaDocumentPersistence(db)` which the gateway uses by default
|
|
18
|
+
* when callers don't supply their own.
|
|
19
|
+
*/
|
|
20
|
+
import { type DocumentPersistence } from '@actuate-media/realtime';
|
|
21
|
+
/**
|
|
22
|
+
* Minimal contract the gateway needs from a real WebSocket implementation.
|
|
23
|
+
* Compatible with `ws.WebSocket`, browser-native `WebSocket`, the
|
|
24
|
+
* Cloudflare Workers `WebSocket` Hibernation API, and Bun's WebSocket.
|
|
25
|
+
*
|
|
26
|
+
* Adapter authors typically just wrap their library's socket in a thin
|
|
27
|
+
* shim that conforms to this shape.
|
|
28
|
+
*/
|
|
29
|
+
export interface WebSocketLike {
|
|
30
|
+
/** Send a binary frame. */
|
|
31
|
+
send(data: Uint8Array): void;
|
|
32
|
+
/** Close the socket. Idempotent. */
|
|
33
|
+
close(code?: number, reason?: string): void;
|
|
34
|
+
/** Subscribe to inbound binary frames. */
|
|
35
|
+
on(event: 'message', handler: (data: Uint8Array) => void): void;
|
|
36
|
+
/** Subscribe to close events. */
|
|
37
|
+
on(event: 'close', handler: () => void): void;
|
|
38
|
+
/** Subscribe to error events. */
|
|
39
|
+
on(event: 'error', handler: (err: Error) => void): void;
|
|
40
|
+
}
|
|
41
|
+
/** Information about the inbound HTTP upgrade request. */
|
|
42
|
+
export interface UpgradeRequestInfo {
|
|
43
|
+
/** Path + query, e.g. `/api/cms/realtime/sync?documentId=doc_1`. */
|
|
44
|
+
url: string;
|
|
45
|
+
/** Lowercased header bag. Authentication usually reads `cookie` or `authorization`. */
|
|
46
|
+
headers: Readonly<Record<string, string | undefined>>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Outcome of the authentication / routing decision. Return `null` to reject
|
|
50
|
+
* the connection (the gateway will close it with code 4401).
|
|
51
|
+
*
|
|
52
|
+
* `connectionId` should be globally unique within the gateway instance —
|
|
53
|
+
* usually a `crypto.randomUUID()` or `req.headers['sec-websocket-key']`.
|
|
54
|
+
*/
|
|
55
|
+
export interface AuthenticatedConnection {
|
|
56
|
+
documentId: string;
|
|
57
|
+
connectionId: string;
|
|
58
|
+
/** Tag returned via `room.onUpdate` so applications can audit edits. */
|
|
59
|
+
userId?: string;
|
|
60
|
+
}
|
|
61
|
+
export interface AttachRealtimeGatewayOptions {
|
|
62
|
+
/**
|
|
63
|
+
* Validate the upgrade request and map it to a documentId. Return `null`
|
|
64
|
+
* to reject. Throws are treated as 401 and the connection is closed.
|
|
65
|
+
*/
|
|
66
|
+
authenticate: (req: UpgradeRequestInfo) => Promise<AuthenticatedConnection | null>;
|
|
67
|
+
/** Storage adapter. Defaults to a noop in-memory persistence (rooms are not durable). */
|
|
68
|
+
persistence?: DocumentPersistence;
|
|
69
|
+
/**
|
|
70
|
+
* Reap a room after it has had zero connections for this many ms. Default 60000.
|
|
71
|
+
* Set to `Infinity` to disable reaping.
|
|
72
|
+
*/
|
|
73
|
+
idleTimeoutMs?: number;
|
|
74
|
+
/** Debounce window for persistence writes (ms). Forwarded to `bindPersistence`. */
|
|
75
|
+
persistenceDebounceMs?: number;
|
|
76
|
+
/** Maximum time between writes (ms). Forwarded to `bindPersistence`. */
|
|
77
|
+
persistenceMaxIdleMs?: number;
|
|
78
|
+
/** Called whenever a room is created. Useful for metrics. */
|
|
79
|
+
onRoomOpen?: (documentId: string) => void;
|
|
80
|
+
/** Called whenever a room is destroyed. Useful for metrics. */
|
|
81
|
+
onRoomClose?: (documentId: string) => void;
|
|
82
|
+
/** Called when a persistence save fails. Defaults to console.error. */
|
|
83
|
+
onPersistenceError?: (documentId: string, err: unknown) => void;
|
|
84
|
+
}
|
|
85
|
+
export interface RealtimeGateway {
|
|
86
|
+
/** Number of currently active rooms. Useful for /health endpoints. */
|
|
87
|
+
readonly roomCount: number;
|
|
88
|
+
/** Number of currently connected sockets across all rooms. */
|
|
89
|
+
readonly connectionCount: number;
|
|
90
|
+
/**
|
|
91
|
+
* Bind an authenticated socket to its room. Call this from your HTTP
|
|
92
|
+
* upgrade handler after `wss.handleUpgrade(...)` has produced a socket.
|
|
93
|
+
*/
|
|
94
|
+
handleConnection(socket: WebSocketLike, req: UpgradeRequestInfo): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Force-close every connection, flush every room, and tear everything
|
|
97
|
+
* down. Call from your process's SIGTERM handler.
|
|
98
|
+
*/
|
|
99
|
+
shutdown(): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Create a realtime gateway. The returned object is intended to live for the
|
|
103
|
+
* lifetime of the server process — one per node. Calling
|
|
104
|
+
* `shutdown()` is mandatory for graceful exit (it flushes pending writes).
|
|
105
|
+
*/
|
|
106
|
+
export declare function createRealtimeGateway(opts: AttachRealtimeGatewayOptions): RealtimeGateway;
|
|
107
|
+
//# sourceMappingURL=gateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.d.ts","sourceRoot":"","sources":["../../src/realtime/gateway.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAIL,KAAK,mBAAmB,EAEzB,MAAM,yBAAyB,CAAA;AAEhC;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa;IAC5B,2BAA2B;IAC3B,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAAA;IAC5B,oCAAoC;IACpC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3C,0CAA0C;IAC1C,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAA;IAC/D,iCAAiC;IACjC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI,CAAA;IAC7C,iCAAiC;IACjC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,GAAG,IAAI,CAA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AAC,CAAA;IAC/E;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1B;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,4BAA4B,GAAG,eAAe,CAuLzF"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime gateway for `@actuate-media/realtime` rooms.
|
|
3
|
+
*
|
|
4
|
+
* The gateway is **transport-agnostic by design**: it does not depend on
|
|
5
|
+
* `ws`, `uWebSockets.js`, Cloudflare Workers, or any specific framework.
|
|
6
|
+
* Consumers create a `WebSocketLike` adapter around their server's socket
|
|
7
|
+
* primitive and pass it to `gateway.handleConnection(socket, req)`.
|
|
8
|
+
*
|
|
9
|
+
* Responsibilities:
|
|
10
|
+
* 1. Authenticate the connection (delegated to `options.authenticate`).
|
|
11
|
+
* 2. Look up (or lazily create) the `Room` for the document.
|
|
12
|
+
* 3. Pump bytes between the socket and the room.
|
|
13
|
+
* 4. Persist edits via the configured `DocumentPersistence`.
|
|
14
|
+
* 5. Reap idle rooms (no connections + no pending writes) to bound memory.
|
|
15
|
+
*
|
|
16
|
+
* The companion file `persistence.ts` provides
|
|
17
|
+
* `createPrismaDocumentPersistence(db)` which the gateway uses by default
|
|
18
|
+
* when callers don't supply their own.
|
|
19
|
+
*/
|
|
20
|
+
import { bindPersistence, createRoom, } from '@actuate-media/realtime';
|
|
21
|
+
/**
|
|
22
|
+
* Create a realtime gateway. The returned object is intended to live for the
|
|
23
|
+
* lifetime of the server process — one per node. Calling
|
|
24
|
+
* `shutdown()` is mandatory for graceful exit (it flushes pending writes).
|
|
25
|
+
*/
|
|
26
|
+
export function createRealtimeGateway(opts) {
|
|
27
|
+
const persistence = opts.persistence ?? createNoopPersistence();
|
|
28
|
+
const idleTimeoutMs = opts.idleTimeoutMs ?? 60_000;
|
|
29
|
+
const onError = opts.onPersistenceError ??
|
|
30
|
+
((documentId, err) => {
|
|
31
|
+
// eslint-disable-next-line no-console
|
|
32
|
+
console.error(`[cms-core realtime] save failed for ${documentId}:`, err);
|
|
33
|
+
});
|
|
34
|
+
const rooms = new Map();
|
|
35
|
+
let connectionCount = 0;
|
|
36
|
+
let shuttingDown = false;
|
|
37
|
+
async function getOrCreateRoom(documentId) {
|
|
38
|
+
const existing = rooms.get(documentId);
|
|
39
|
+
if (existing) {
|
|
40
|
+
if (existing.reapTimer) {
|
|
41
|
+
clearTimeout(existing.reapTimer);
|
|
42
|
+
existing.reapTimer = null;
|
|
43
|
+
}
|
|
44
|
+
return existing;
|
|
45
|
+
}
|
|
46
|
+
const initial = (await persistence.load(documentId)) ?? undefined;
|
|
47
|
+
const room = createRoom({ documentId, initialState: initial });
|
|
48
|
+
const binding = bindPersistence(room, persistence, {
|
|
49
|
+
debounceMs: opts.persistenceDebounceMs,
|
|
50
|
+
maxIdleMs: opts.persistenceMaxIdleMs,
|
|
51
|
+
onError: (err) => onError(documentId, err),
|
|
52
|
+
});
|
|
53
|
+
const entry = { room, binding, connections: 0, reapTimer: null };
|
|
54
|
+
rooms.set(documentId, entry);
|
|
55
|
+
opts.onRoomOpen?.(documentId);
|
|
56
|
+
return entry;
|
|
57
|
+
}
|
|
58
|
+
function scheduleReap(documentId) {
|
|
59
|
+
const entry = rooms.get(documentId);
|
|
60
|
+
if (!entry)
|
|
61
|
+
return;
|
|
62
|
+
if (entry.connections > 0)
|
|
63
|
+
return;
|
|
64
|
+
if (entry.reapTimer)
|
|
65
|
+
return;
|
|
66
|
+
if (!Number.isFinite(idleTimeoutMs))
|
|
67
|
+
return;
|
|
68
|
+
entry.reapTimer = setTimeout(() => {
|
|
69
|
+
void destroyRoom(documentId);
|
|
70
|
+
}, idleTimeoutMs);
|
|
71
|
+
}
|
|
72
|
+
async function destroyRoom(documentId) {
|
|
73
|
+
const entry = rooms.get(documentId);
|
|
74
|
+
if (!entry)
|
|
75
|
+
return;
|
|
76
|
+
rooms.delete(documentId);
|
|
77
|
+
if (entry.reapTimer) {
|
|
78
|
+
clearTimeout(entry.reapTimer);
|
|
79
|
+
entry.reapTimer = null;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
await entry.binding.flush();
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
onError(documentId, err);
|
|
86
|
+
}
|
|
87
|
+
entry.binding.unbind();
|
|
88
|
+
entry.room.destroy();
|
|
89
|
+
opts.onRoomClose?.(documentId);
|
|
90
|
+
}
|
|
91
|
+
async function handleConnection(socket, req) {
|
|
92
|
+
if (shuttingDown) {
|
|
93
|
+
try {
|
|
94
|
+
socket.close(1001, 'server shutting down');
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
/* ignore */
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let auth;
|
|
102
|
+
try {
|
|
103
|
+
auth = await opts.authenticate(req);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
auth = null;
|
|
107
|
+
}
|
|
108
|
+
if (!auth) {
|
|
109
|
+
try {
|
|
110
|
+
socket.close(4401, 'unauthorized');
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
/* ignore */
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const entry = await getOrCreateRoom(auth.documentId);
|
|
118
|
+
if (entry.reapTimer) {
|
|
119
|
+
clearTimeout(entry.reapTimer);
|
|
120
|
+
entry.reapTimer = null;
|
|
121
|
+
}
|
|
122
|
+
const conn = {
|
|
123
|
+
id: auth.connectionId,
|
|
124
|
+
send: (bytes) => {
|
|
125
|
+
try {
|
|
126
|
+
socket.send(bytes);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
/* surfaced via close handler */
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
close: (reason) => {
|
|
133
|
+
try {
|
|
134
|
+
socket.close(1000, reason ?? 'closed');
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
/* ignore */
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
try {
|
|
142
|
+
entry.room.addConnection(conn);
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// duplicate connection id — close and bail out.
|
|
146
|
+
try {
|
|
147
|
+
socket.close(4409, 'duplicate connection id');
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* ignore */
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
entry.connections += 1;
|
|
155
|
+
connectionCount += 1;
|
|
156
|
+
socket.on('message', (data) => {
|
|
157
|
+
try {
|
|
158
|
+
entry.room.handleMessage(auth.connectionId, data);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// Malformed frame — close the offending peer; other connections continue.
|
|
162
|
+
try {
|
|
163
|
+
socket.close(1003, 'malformed frame');
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
/* ignore */
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
socket.on('error', () => {
|
|
171
|
+
try {
|
|
172
|
+
socket.close(1011, 'socket error');
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
/* ignore */
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
socket.on('close', () => {
|
|
179
|
+
entry.room.removeConnection(auth.connectionId);
|
|
180
|
+
entry.connections = Math.max(0, entry.connections - 1);
|
|
181
|
+
connectionCount = Math.max(0, connectionCount - 1);
|
|
182
|
+
if (entry.connections === 0) {
|
|
183
|
+
scheduleReap(auth.documentId);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
async function shutdown() {
|
|
188
|
+
shuttingDown = true;
|
|
189
|
+
const docIds = Array.from(rooms.keys());
|
|
190
|
+
await Promise.all(docIds.map((id) => destroyRoom(id)));
|
|
191
|
+
connectionCount = 0;
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
get roomCount() {
|
|
195
|
+
return rooms.size;
|
|
196
|
+
},
|
|
197
|
+
get connectionCount() {
|
|
198
|
+
return connectionCount;
|
|
199
|
+
},
|
|
200
|
+
handleConnection,
|
|
201
|
+
shutdown,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** Persistence that swallows writes — used when consumers don't pass one. */
|
|
205
|
+
function createNoopPersistence() {
|
|
206
|
+
return {
|
|
207
|
+
async load() {
|
|
208
|
+
return null;
|
|
209
|
+
},
|
|
210
|
+
async save() {
|
|
211
|
+
/* discard */
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../../src/realtime/gateway.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EACL,eAAe,EACf,UAAU,GAIX,MAAM,yBAAyB,CAAA;AAuFhC;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAkC;IACtE,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,qBAAqB,EAAE,CAAA;IAC/D,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,IAAI,MAAM,CAAA;IAClD,MAAM,OAAO,GACX,IAAI,CAAC,kBAAkB;QACvB,CAAC,CAAC,UAAU,EAAE,GAAG,EAAE,EAAE;YACnB,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,uCAAuC,UAAU,GAAG,EAAE,GAAG,CAAC,CAAA;QAC1E,CAAC,CAAC,CAAA;IAWJ,MAAM,KAAK,GAAG,IAAI,GAAG,EAAqB,CAAA;IAC1C,IAAI,eAAe,GAAG,CAAC,CAAA;IACvB,IAAI,YAAY,GAAG,KAAK,CAAA;IAExB,KAAK,UAAU,eAAe,CAAC,UAAkB;QAC/C,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,UAAU,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,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;QACtD,eAAe,GAAG,CAAC,CAAA;IACrB,CAAC;IAED,OAAO;QACL,IAAI,SAAS;YACX,OAAO,KAAK,CAAC,IAAI,CAAA;QACnB,CAAC;QACD,IAAI,eAAe;YACjB,OAAO,eAAe,CAAA;QACxB,CAAC;QACD,gBAAgB;QAChB,QAAQ;KACT,CAAA;AACH,CAAC;AAED,6EAA6E;AAC7E,SAAS,qBAAqB;IAC5B,OAAO;QACL,KAAK,CAAC,IAAI;YACR,OAAO,IAAI,CAAA;QACb,CAAC;QACD,KAAK,CAAC,IAAI;YACR,aAAa;QACf,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime collaboration surface for `@actuate-media/cms-core`.
|
|
3
|
+
*
|
|
4
|
+
* - `createRealtimeGateway` — transport-agnostic gateway that owns rooms,
|
|
5
|
+
* debounces persistence, and reaps idle documents. Consumers wrap their
|
|
6
|
+
* server's socket primitive in a `WebSocketLike` adapter and feed it via
|
|
7
|
+
* `gateway.handleConnection(socket, req)`.
|
|
8
|
+
* - `createPrismaDocumentPersistence` — Prisma-backed implementation of
|
|
9
|
+
* `DocumentPersistence` against the `DocumentCRDT` table.
|
|
10
|
+
*
|
|
11
|
+
* See `packages/realtime/README.md` for the protocol details and
|
|
12
|
+
* `docs/roadmap-status.md` for the Phase 3 slice plan.
|
|
13
|
+
*/
|
|
14
|
+
export { createRealtimeGateway, type AttachRealtimeGatewayOptions, type AuthenticatedConnection, type RealtimeGateway, type UpgradeRequestInfo, type WebSocketLike, } from './gateway.js';
|
|
15
|
+
export { createPrismaDocumentPersistence, type PrismaDocumentCRDTClient, type PrismaDocumentPersistenceOptions, } from './persistence.js';
|
|
16
|
+
export { createComment, deleteComment, listComments, normaliseBody, parseAnchor, reopenComment, resolveComment, toCommentDTO, updateComment, DEFAULT_COMMENT_LIST_PAGE_SIZE, MAX_COMMENT_BODY_LENGTH, MAX_COMMENT_LIST_PAGE_SIZE, type CommentAnchor, type CommentDTO, type CommentsDB, type CommentsError, type CommentsResult, type CreateCommentInput, type DocumentCommentRow, type ListCommentsFilter, type UpdateCommentInput, } from './comments.js';
|
|
17
|
+
export { createNotification, excerptForNotification, extractMentionedUserIds, isNotificationKind, listForUser as listNotificationsForUser, markAllRead as markAllNotificationsRead, markRead as markNotificationRead, toNotificationDTO, unreadCount as notificationsUnreadCount, DEFAULT_NOTIFICATION_LIST_PAGE_SIZE, MAX_NOTIFICATION_LIST_PAGE_SIZE, type CreateNotificationInput, type DocumentNotificationRow, type ListNotificationsFilter, type NotificationDTO, type NotificationKind, type NotificationPayload, type NotificationsDB, type NotificationsError, type NotificationsResult, } from './notifications.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/realtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EACL,qBAAqB,EACrB,KAAK,4BAA4B,EACjC,KAAK,uBAAuB,EAC5B,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,aAAa,GACnB,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,+BAA+B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,gCAAgC,GACtC,MAAM,kBAAkB,CAAA;AAEzB,OAAO,EACL,aAAa,EACb,aAAa,EACb,YAAY,EACZ,aAAa,EACb,WAAW,EACX,aAAa,EACb,cAAc,EACd,YAAY,EACZ,aAAa,EACb,8BAA8B,EAC9B,uBAAuB,EACvB,0BAA0B,EAC1B,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,GACxB,MAAM,eAAe,CAAA;AAEtB,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,EAClB,WAAW,IAAI,wBAAwB,EACvC,WAAW,IAAI,wBAAwB,EACvC,QAAQ,IAAI,oBAAoB,EAChC,iBAAiB,EACjB,WAAW,IAAI,wBAAwB,EACvC,mCAAmC,EACnC,+BAA+B,EAC/B,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC5B,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACzB,MAAM,oBAAoB,CAAA"}
|