@dxos/feed 0.10.0 → 0.11.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.
- package/dist/lib/chunk-sync-client.mjs +1009 -0
- package/dist/lib/chunk-sync-client.mjs.map +1 -0
- package/dist/lib/index.mjs +2 -0
- package/dist/lib/testing.mjs +168 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/src/feed-store.d.ts +12 -0
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -15
- package/src/feed-store.ts +11 -0
- package/dist/lib/neutral/chunk-UC5ETQIJ.mjs +0 -1022
- package/dist/lib/neutral/chunk-UC5ETQIJ.mjs.map +0 -7
- package/dist/lib/neutral/index.mjs +0 -17
- package/dist/lib/neutral/index.mjs.map +0 -7
- package/dist/lib/neutral/meta.json +0 -1
- package/dist/lib/neutral/testing/index.mjs +0 -174
- package/dist/lib/neutral/testing/index.mjs.map +0 -7
|
@@ -1,1022 +0,0 @@
|
|
|
1
|
-
// src/errors.ts
|
|
2
|
-
import { BaseError } from "@dxos/errors";
|
|
3
|
-
var SyncRpcTimeoutError = class extends BaseError.extend("SyncRpcTimeoutError") {
|
|
4
|
-
constructor(args) {
|
|
5
|
-
super({
|
|
6
|
-
message: `Feed sync ${args.rpcTag} timed out after ${args.timeoutMs}ms (spaceId=${args.spaceId} feedNamespace=${args.feedNamespace} requestId=${args.requestId}).`,
|
|
7
|
-
context: args
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
var PositionConflictError = class extends BaseError.extend("PositionConflictError") {
|
|
12
|
-
constructor(args) {
|
|
13
|
-
super({
|
|
14
|
-
message: `Block already has position ${args.currentPosition}, cannot set to ${args.requestedPosition} (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
|
|
15
|
-
context: args
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// src/feed-store.ts
|
|
21
|
-
import * as SqlClient from "@effect/sql/SqlClient";
|
|
22
|
-
import * as Effect from "effect/Effect";
|
|
23
|
-
import { Event } from "@dxos/async";
|
|
24
|
-
import { assertArgument } from "@dxos/invariant";
|
|
25
|
-
import { FeedProtocol } from "@dxos/protocols";
|
|
26
|
-
import { SqlTransaction } from "@dxos/sql-sqlite";
|
|
27
|
-
var FeedCursor = FeedProtocol.FeedCursor;
|
|
28
|
-
var isWellKnownNamespace = FeedProtocol.isWellKnownNamespace;
|
|
29
|
-
var FeedStore = class {
|
|
30
|
-
#options;
|
|
31
|
-
constructor(options) {
|
|
32
|
-
this.#options = options;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Emits after successful block append operations.
|
|
36
|
-
*/
|
|
37
|
-
onNewBlocks = new Event();
|
|
38
|
-
/**
|
|
39
|
-
* Creates required feed store tables and indexes if they do not exist.
|
|
40
|
-
*/
|
|
41
|
-
migrate = Effect.fn("FeedStore.migrate")(() => Effect.gen(function* () {
|
|
42
|
-
const sql = yield* SqlClient.SqlClient;
|
|
43
|
-
yield* sql`CREATE TABLE IF NOT EXISTS feeds (
|
|
44
|
-
feedPrivateId INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
45
|
-
spaceId TEXT NOT NULL,
|
|
46
|
-
feedId TEXT NOT NULL,
|
|
47
|
-
feedNamespace TEXT
|
|
48
|
-
)`;
|
|
49
|
-
yield* sql`CREATE UNIQUE INDEX IF NOT EXISTS idx_feeds_spaceId_feedId ON feeds(spaceId, feedId)`;
|
|
50
|
-
yield* sql`CREATE TABLE IF NOT EXISTS blocks (
|
|
51
|
-
insertionId INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
52
|
-
feedPrivateId INTEGER NOT NULL,
|
|
53
|
-
position INTEGER,
|
|
54
|
-
sequence INTEGER NOT NULL,
|
|
55
|
-
actorId TEXT NOT NULL,
|
|
56
|
-
prevSequence INTEGER,
|
|
57
|
-
prevActorId TEXT,
|
|
58
|
-
timestamp INTEGER NOT NULL,
|
|
59
|
-
data BLOB NOT NULL,
|
|
60
|
-
FOREIGN KEY(feedPrivateId) REFERENCES feeds(feedPrivateId)
|
|
61
|
-
)`;
|
|
62
|
-
yield* sql`CREATE UNIQUE INDEX IF NOT EXISTS idx_blocks_feedPrivateId_position ON blocks(feedPrivateId, position)`;
|
|
63
|
-
yield* sql`CREATE UNIQUE INDEX IF NOT EXISTS idx_blocks_feedPrivateId_sequence_actorId ON blocks(feedPrivateId, sequence, actorId)`;
|
|
64
|
-
yield* sql`CREATE TABLE IF NOT EXISTS subscriptions (
|
|
65
|
-
subscriptionId TEXT PRIMARY KEY,
|
|
66
|
-
expiresAt INTEGER NOT NULL,
|
|
67
|
-
feedPrivateIds TEXT NOT NULL -- JSON array
|
|
68
|
-
)`;
|
|
69
|
-
yield* sql`CREATE TABLE IF NOT EXISTS cursor_tokens (
|
|
70
|
-
spaceId TEXT PRIMARY KEY,
|
|
71
|
-
token TEXT NOT NULL
|
|
72
|
-
)`;
|
|
73
|
-
yield* sql`CREATE TABLE IF NOT EXISTS sync_state (
|
|
74
|
-
spaceId TEXT NOT NULL,
|
|
75
|
-
feedNamespace TEXT NOT NULL,
|
|
76
|
-
lastPulledPosition INTEGER NOT NULL DEFAULT -1,
|
|
77
|
-
PRIMARY KEY (spaceId, feedNamespace)
|
|
78
|
-
)`;
|
|
79
|
-
}).pipe(Effect.withSpan("FeedStore.migrate")));
|
|
80
|
-
/**
|
|
81
|
-
* Ensures a feed row exists and returns its internal feed ID.
|
|
82
|
-
*/
|
|
83
|
-
#ensureFeed = Effect.fn("Feed.ensureFeed")((spaceId, feedId, namespace) => Effect.gen(this, function* () {
|
|
84
|
-
const sql = yield* SqlClient.SqlClient;
|
|
85
|
-
const rows = yield* sql`
|
|
86
|
-
SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
87
|
-
`;
|
|
88
|
-
if (rows.length > 0) {
|
|
89
|
-
return rows[0].feedPrivateId;
|
|
90
|
-
}
|
|
91
|
-
const newRows = yield* sql`
|
|
92
|
-
INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
|
|
93
|
-
`;
|
|
94
|
-
return newRows[0].feedPrivateId;
|
|
95
|
-
}).pipe(Effect.withSpan("FeedStore.ensureFeed")));
|
|
96
|
-
/**
|
|
97
|
-
* Ensures cursor token exists for a space and returns it.
|
|
98
|
-
*/
|
|
99
|
-
#ensureCursorToken = Effect.fn("Feed.ensureCursorToken")((spaceId) => Effect.gen(this, function* () {
|
|
100
|
-
const sql = yield* SqlClient.SqlClient;
|
|
101
|
-
const rows = yield* sql`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
|
|
102
|
-
if (rows.length > 0) {
|
|
103
|
-
return rows[0].token;
|
|
104
|
-
}
|
|
105
|
-
const token = crypto.randomUUID().replace(/-/g, "").slice(0, 6);
|
|
106
|
-
yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
|
|
107
|
-
return token;
|
|
108
|
-
}).pipe(Effect.withSpan("FeedStore.ensureCursorToken")));
|
|
109
|
-
/**
|
|
110
|
-
* Queries feed blocks by feed IDs or subscription with cursor/position pagination.
|
|
111
|
-
*/
|
|
112
|
-
query = Effect.fn("Feed.query")((request) => Effect.gen(this, function* () {
|
|
113
|
-
const sql = yield* SqlClient.SqlClient;
|
|
114
|
-
let feedIds = [];
|
|
115
|
-
let cursorInsertionId = -1;
|
|
116
|
-
let cursorToken;
|
|
117
|
-
if (!request.spaceId) {
|
|
118
|
-
return yield* Effect.die(new Error("spaceId is required"));
|
|
119
|
-
}
|
|
120
|
-
if ((request.position !== void 0 ? 1 : 0) + (request.cursor !== void 0 ? 1 : 0) + (request.unpositionedOnly === true ? 1 : 0) > 1) {
|
|
121
|
-
return yield* Effect.die(new Error("Only one of position, cursor, or unpositionedOnly can be used"));
|
|
122
|
-
}
|
|
123
|
-
if (request.cursor) {
|
|
124
|
-
const { token, insertionId } = decodeCursor(request.cursor);
|
|
125
|
-
if (!token || insertionId === void 0 || isNaN(insertionId)) {
|
|
126
|
-
return yield* Effect.die(new Error(`Invalid cursor format`));
|
|
127
|
-
}
|
|
128
|
-
cursorToken = token;
|
|
129
|
-
cursorInsertionId = insertionId;
|
|
130
|
-
}
|
|
131
|
-
const validCursorToken = yield* this.#ensureCursorToken(request.spaceId);
|
|
132
|
-
if (request.cursor && cursorToken !== validCursorToken) {
|
|
133
|
-
return yield* Effect.die(new Error(`Cursor token mismatch`));
|
|
134
|
-
}
|
|
135
|
-
const position = request.position ?? -1;
|
|
136
|
-
if (request.query && "subscriptionId" in request.query) {
|
|
137
|
-
const rows2 = yield* sql`
|
|
138
|
-
SELECT feedPrivateIds, expiresAt FROM subscriptions WHERE subscriptionId = ${request.query.subscriptionId}
|
|
139
|
-
`;
|
|
140
|
-
if (rows2.length > 0) {
|
|
141
|
-
const { feedPrivateIds, expiresAt } = rows2[0];
|
|
142
|
-
if (Date.now() <= expiresAt) {
|
|
143
|
-
const privateIds = JSON.parse(feedPrivateIds);
|
|
144
|
-
if (privateIds.length > 0) {
|
|
145
|
-
const feedRows = yield* sql`
|
|
146
|
-
SELECT feedId FROM feeds WHERE feedPrivateId IN ${sql.in(privateIds)}
|
|
147
|
-
`;
|
|
148
|
-
feedIds = feedRows.map((r) => r.feedId);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
} else if (request.query && "feedIds" in request.query) {
|
|
153
|
-
feedIds = [
|
|
154
|
-
...request.query.feedIds
|
|
155
|
-
];
|
|
156
|
-
} else {
|
|
157
|
-
feedIds = void 0;
|
|
158
|
-
}
|
|
159
|
-
if (feedIds !== void 0 && feedIds.length === 0) {
|
|
160
|
-
return {
|
|
161
|
-
requestId: request.requestId,
|
|
162
|
-
blocks: [],
|
|
163
|
-
nextCursor: encodeCursor(validCursorToken, -1),
|
|
164
|
-
hasMore: false
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
const query = sql`
|
|
168
|
-
SELECT blocks.*, feeds.feedId, feeds.feedNamespace
|
|
169
|
-
FROM blocks
|
|
170
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
171
|
-
WHERE 1=1
|
|
172
|
-
${feedIds !== void 0 ? sql`AND feeds.feedId IN ${sql.in(feedIds)}` : sql``}
|
|
173
|
-
${request.spaceId ? sql`AND feeds.spaceId = ${request.spaceId}` : sql``}
|
|
174
|
-
${sql`AND feeds.feedNamespace = ${request.feedNamespace}`}
|
|
175
|
-
`;
|
|
176
|
-
const filter = request.cursor ? sql`AND blocks.insertionId > ${cursorInsertionId}` : request.unpositionedOnly ? sql`AND blocks.position IS NULL` : sql`AND (blocks.position > ${position} OR blocks.position IS NULL)`;
|
|
177
|
-
const orderBy = request.cursor ? sql`ORDER BY blocks.insertionId ASC` : sql`ORDER BY blocks.position ASC NULLS LAST`;
|
|
178
|
-
const requestLimit = request.limit;
|
|
179
|
-
const queryLimit = requestLimit != null ? requestLimit + 1 : void 0;
|
|
180
|
-
const rows = yield* sql`
|
|
181
|
-
${query}
|
|
182
|
-
${filter}
|
|
183
|
-
${orderBy}
|
|
184
|
-
${queryLimit != null ? sql`LIMIT ${queryLimit}` : sql``}
|
|
185
|
-
`;
|
|
186
|
-
const hasMore = requestLimit != null && rows.length > requestLimit;
|
|
187
|
-
const slice = hasMore ? rows.slice(0, requestLimit) : rows;
|
|
188
|
-
const blocks = slice.map((row) => ({
|
|
189
|
-
...row,
|
|
190
|
-
// Have to clone buffer otherwise we get empty Uint8Array.
|
|
191
|
-
data: new Uint8Array(row.data)
|
|
192
|
-
}));
|
|
193
|
-
let nextCursor = request.cursor ?? encodeCursor(validCursorToken, -1);
|
|
194
|
-
if (blocks.length > 0 && request.spaceId) {
|
|
195
|
-
const lastBlock = blocks[blocks.length - 1];
|
|
196
|
-
if (lastBlock.insertionId !== void 0) {
|
|
197
|
-
nextCursor = encodeCursor(validCursorToken, lastBlock.insertionId);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return {
|
|
201
|
-
requestId: request.requestId,
|
|
202
|
-
blocks,
|
|
203
|
-
nextCursor,
|
|
204
|
-
hasMore
|
|
205
|
-
};
|
|
206
|
-
}).pipe(Effect.withSpan("FeedStore.query")));
|
|
207
|
-
/**
|
|
208
|
-
* Creates a subscription and stores the resolved internal feed IDs.
|
|
209
|
-
*/
|
|
210
|
-
subscribe = Effect.fn("Feed.subscribe")((request) => Effect.gen(this, function* () {
|
|
211
|
-
const sql = yield* SqlClient.SqlClient;
|
|
212
|
-
const ttl = 60 * 60 * 1e3;
|
|
213
|
-
const subscriptionId = crypto.randomUUID();
|
|
214
|
-
const expiresAt = Date.now() + ttl;
|
|
215
|
-
if (!request.spaceId) {
|
|
216
|
-
return yield* Effect.die(new Error("spaceId required for subscribe"));
|
|
217
|
-
}
|
|
218
|
-
const feedPrivateIds = yield* Effect.forEach(request.feedIds, (feedId) => this.#ensureFeed(request.spaceId, feedId), {
|
|
219
|
-
concurrency: "unbounded"
|
|
220
|
-
});
|
|
221
|
-
yield* sql`
|
|
222
|
-
INSERT INTO subscriptions (subscriptionId, expiresAt, feedPrivateIds)
|
|
223
|
-
VALUES (${subscriptionId}, ${expiresAt}, ${JSON.stringify(feedPrivateIds)})
|
|
224
|
-
`;
|
|
225
|
-
return {
|
|
226
|
-
requestId: request.requestId,
|
|
227
|
-
subscriptionId,
|
|
228
|
-
expiresAt
|
|
229
|
-
};
|
|
230
|
-
}).pipe(Effect.withSpan("FeedStore.subscribe")));
|
|
231
|
-
/**
|
|
232
|
-
* Get the last pulled position for the given space and namespace.
|
|
233
|
-
* Returns -1 if no sync state exists yet.
|
|
234
|
-
*/
|
|
235
|
-
getSyncState = (opts) => Effect.gen(this, function* () {
|
|
236
|
-
const sql = yield* SqlClient.SqlClient;
|
|
237
|
-
const rows = yield* sql`
|
|
238
|
-
SELECT lastPulledPosition FROM sync_state
|
|
239
|
-
WHERE spaceId = ${opts.spaceId} AND feedNamespace = ${opts.feedNamespace}
|
|
240
|
-
`;
|
|
241
|
-
return rows[0]?.lastPulledPosition ?? -1;
|
|
242
|
-
}).pipe(Effect.withSpan("FeedStore.getSyncState"));
|
|
243
|
-
/**
|
|
244
|
-
* Update the last pulled position for the given space and namespace.
|
|
245
|
-
*/
|
|
246
|
-
setSyncState = (opts) => Effect.gen(this, function* () {
|
|
247
|
-
const sql = yield* SqlClient.SqlClient;
|
|
248
|
-
yield* sql`
|
|
249
|
-
INSERT INTO sync_state (spaceId, feedNamespace, lastPulledPosition)
|
|
250
|
-
VALUES (${opts.spaceId}, ${opts.feedNamespace}, ${opts.lastPulledPosition})
|
|
251
|
-
ON CONFLICT (spaceId, feedNamespace) DO UPDATE SET lastPulledPosition = ${opts.lastPulledPosition}
|
|
252
|
-
`;
|
|
253
|
-
}).pipe(Effect.withSpan("FeedStore.setSyncState"));
|
|
254
|
-
/**
|
|
255
|
-
* Returns the number of blocks pending push (no global position yet) in a space/namespace.
|
|
256
|
-
*/
|
|
257
|
-
countUnpositionedBlocks = (opts) => Effect.gen(this, function* () {
|
|
258
|
-
const sql = yield* SqlClient.SqlClient;
|
|
259
|
-
const rows = yield* sql`
|
|
260
|
-
SELECT COUNT(*) AS count
|
|
261
|
-
FROM blocks
|
|
262
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
263
|
-
WHERE feeds.spaceId = ${opts.spaceId}
|
|
264
|
-
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
265
|
-
AND blocks.position IS NULL
|
|
266
|
-
`;
|
|
267
|
-
return rows[0]?.count ?? 0;
|
|
268
|
-
}).pipe(Effect.withSpan("FeedStore.countUnpositionedBlocks"));
|
|
269
|
-
/**
|
|
270
|
-
* Returns the total number of blocks stored locally for a space/namespace.
|
|
271
|
-
*/
|
|
272
|
-
countNamespaceBlocks = (opts) => Effect.gen(this, function* () {
|
|
273
|
-
const sql = yield* SqlClient.SqlClient;
|
|
274
|
-
const rows = yield* sql`
|
|
275
|
-
SELECT COUNT(*) AS count
|
|
276
|
-
FROM blocks
|
|
277
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
278
|
-
WHERE feeds.spaceId = ${opts.spaceId}
|
|
279
|
-
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
280
|
-
`;
|
|
281
|
-
return rows[0]?.count ?? 0;
|
|
282
|
-
}).pipe(Effect.withSpan("FeedStore.countNamespaceBlocks"));
|
|
283
|
-
/**
|
|
284
|
-
* Returns the number of stored blocks for a single feed in a space/namespace.
|
|
285
|
-
* Intended as a low-level primitive for callers (for example Cloudflare Worker code)
|
|
286
|
-
* that need to make retention decisions under constrained storage resources.
|
|
287
|
-
*/
|
|
288
|
-
countBlocks = (opts) => Effect.gen(this, function* () {
|
|
289
|
-
const sql = yield* SqlClient.SqlClient;
|
|
290
|
-
const rows = yield* sql`
|
|
291
|
-
SELECT COUNT(*) AS count
|
|
292
|
-
FROM blocks
|
|
293
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
294
|
-
WHERE feeds.spaceId = ${opts.spaceId}
|
|
295
|
-
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
296
|
-
AND feeds.feedId = ${opts.feedId}
|
|
297
|
-
`;
|
|
298
|
-
return rows[0]?.count ?? 0;
|
|
299
|
-
}).pipe(Effect.withSpan("FeedStore.countBlocks"));
|
|
300
|
-
/**
|
|
301
|
-
* Deletes the oldest blocks for a single feed in a space/namespace.
|
|
302
|
-
* This API intentionally does not enforce any retention policy (such as max size);
|
|
303
|
-
* callers decide when and how much to prune, which is useful for constrained
|
|
304
|
-
* environments like Cloudflare Workers.
|
|
305
|
-
*
|
|
306
|
-
* @returns Number of deleted rows.
|
|
307
|
-
*/
|
|
308
|
-
deleteOldestBlocks = (opts) => Effect.gen(this, function* () {
|
|
309
|
-
const sql = yield* SqlClient.SqlClient;
|
|
310
|
-
if (opts.count <= 0) {
|
|
311
|
-
return 0;
|
|
312
|
-
}
|
|
313
|
-
const deletedRows = yield* sql`
|
|
314
|
-
DELETE FROM blocks
|
|
315
|
-
WHERE insertionId IN (
|
|
316
|
-
SELECT blocks.insertionId
|
|
317
|
-
FROM blocks
|
|
318
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
319
|
-
WHERE feeds.spaceId = ${opts.spaceId}
|
|
320
|
-
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
321
|
-
AND feeds.feedId = ${opts.feedId}
|
|
322
|
-
ORDER BY blocks.insertionId ASC
|
|
323
|
-
LIMIT ${opts.count}
|
|
324
|
-
)
|
|
325
|
-
RETURNING insertionId
|
|
326
|
-
`;
|
|
327
|
-
return deletedRows.length;
|
|
328
|
-
}).pipe(Effect.withSpan("FeedStore.deleteOldestBlocks"));
|
|
329
|
-
/**
|
|
330
|
-
* Appends blocks for a space/namespace and optionally assigns global positions.
|
|
331
|
-
*/
|
|
332
|
-
append = (request) => Effect.gen(this, function* () {
|
|
333
|
-
if (!request.spaceId) {
|
|
334
|
-
return yield* Effect.die(new Error("spaceId required for append"));
|
|
335
|
-
}
|
|
336
|
-
assertArgument(isWellKnownNamespace(request.feedNamespace), "request.feedNamespace", "specified well-known namespace");
|
|
337
|
-
for (const block of request.blocks) {
|
|
338
|
-
assertArgument(block.feedId, "block.feedId", "feedId is required");
|
|
339
|
-
}
|
|
340
|
-
const sqlTransaction = yield* SqlTransaction.SqlTransaction;
|
|
341
|
-
const positions = yield* sqlTransaction.withTransaction(Effect.gen(this, function* () {
|
|
342
|
-
const sql = yield* SqlClient.SqlClient;
|
|
343
|
-
const feedKeys = /* @__PURE__ */ new Map();
|
|
344
|
-
for (const block of request.blocks) {
|
|
345
|
-
const key = block.feedId;
|
|
346
|
-
if (!feedKeys.has(key)) {
|
|
347
|
-
feedKeys.set(key, {
|
|
348
|
-
feedId: block.feedId
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
const feedPrivateIds = /* @__PURE__ */ new Map();
|
|
353
|
-
yield* Effect.forEach([
|
|
354
|
-
...feedKeys.entries()
|
|
355
|
-
], ([key, { feedId }]) => Effect.gen(this, function* () {
|
|
356
|
-
const id = yield* this.#ensureFeed(request.spaceId, feedId, request.feedNamespace);
|
|
357
|
-
feedPrivateIds.set(key, id);
|
|
358
|
-
}), {
|
|
359
|
-
concurrency: "unbounded"
|
|
360
|
-
});
|
|
361
|
-
const maxPositions = /* @__PURE__ */ new Map();
|
|
362
|
-
if (this.#options.assignPositions) {
|
|
363
|
-
const maxPosResult = yield* sql`
|
|
364
|
-
SELECT MAX(position) as maxPos
|
|
365
|
-
FROM blocks
|
|
366
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
367
|
-
WHERE feeds.spaceId = ${request.spaceId} AND feeds.feedNamespace = ${request.feedNamespace}
|
|
368
|
-
`;
|
|
369
|
-
maxPositions.set(request.feedNamespace, maxPosResult[0]?.maxPos ?? -1);
|
|
370
|
-
}
|
|
371
|
-
const positions2 = [];
|
|
372
|
-
for (const block of request.blocks) {
|
|
373
|
-
const key = block.feedId;
|
|
374
|
-
const feedPrivateId = feedPrivateIds.get(key);
|
|
375
|
-
let positionToInsert = null;
|
|
376
|
-
if (this.#options.assignPositions) {
|
|
377
|
-
positionToInsert = maxPositions.get(request.feedNamespace) + 1;
|
|
378
|
-
} else if (block.position != null) {
|
|
379
|
-
positionToInsert = block.position;
|
|
380
|
-
}
|
|
381
|
-
const inserted = yield* sql`
|
|
382
|
-
INSERT INTO blocks (
|
|
383
|
-
feedPrivateId, position, sequence, actorId,
|
|
384
|
-
prevSequence, prevActorId, timestamp, data
|
|
385
|
-
) VALUES (
|
|
386
|
-
${feedPrivateId}, ${positionToInsert}, ${block.sequence}, ${block.actorId},
|
|
387
|
-
${block.prevSequence}, ${block.prevActorId}, ${block.timestamp}, ${block.data}
|
|
388
|
-
)
|
|
389
|
-
ON CONFLICT(feedPrivateId, sequence, actorId) DO NOTHING
|
|
390
|
-
RETURNING position
|
|
391
|
-
`;
|
|
392
|
-
if (!this.#options.assignPositions) {
|
|
393
|
-
continue;
|
|
394
|
-
}
|
|
395
|
-
if (inserted.length > 0) {
|
|
396
|
-
positions2.push(positionToInsert);
|
|
397
|
-
maxPositions.set(request.feedNamespace, positionToInsert);
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
const existing = yield* sql`
|
|
401
|
-
SELECT position FROM blocks
|
|
402
|
-
WHERE feedPrivateId = ${feedPrivateId}
|
|
403
|
-
AND actorId = ${block.actorId}
|
|
404
|
-
AND sequence = ${block.sequence}
|
|
405
|
-
`;
|
|
406
|
-
const existingPosition = existing[0]?.position;
|
|
407
|
-
if (existingPosition != null) {
|
|
408
|
-
positions2.push(existingPosition);
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
yield* sql`
|
|
412
|
-
UPDATE blocks SET position = ${positionToInsert}
|
|
413
|
-
WHERE feedPrivateId = ${feedPrivateId}
|
|
414
|
-
AND actorId = ${block.actorId}
|
|
415
|
-
AND sequence = ${block.sequence}
|
|
416
|
-
`;
|
|
417
|
-
positions2.push(positionToInsert);
|
|
418
|
-
maxPositions.set(request.feedNamespace, positionToInsert);
|
|
419
|
-
}
|
|
420
|
-
return positions2;
|
|
421
|
-
}));
|
|
422
|
-
this.onNewBlocks.emit();
|
|
423
|
-
return {
|
|
424
|
-
requestId: request.requestId,
|
|
425
|
-
positions
|
|
426
|
-
};
|
|
427
|
-
}).pipe(Effect.withSpan("FeedStore.append"));
|
|
428
|
-
/**
|
|
429
|
-
* Creates local blocks with sequential predecessors and appends grouped batches.
|
|
430
|
-
*/
|
|
431
|
-
appendLocal = Effect.fn("Feed.appendLocal")((messages) => Effect.gen(this, function* () {
|
|
432
|
-
const sql = yield* SqlClient.SqlClient;
|
|
433
|
-
const feedKeys = /* @__PURE__ */ new Map();
|
|
434
|
-
for (const msg of messages) {
|
|
435
|
-
const key = `${msg.spaceId}|${msg.feedNamespace}|${msg.feedId}`;
|
|
436
|
-
if (!feedKeys.has(key)) {
|
|
437
|
-
feedKeys.set(key, {
|
|
438
|
-
spaceId: msg.spaceId,
|
|
439
|
-
feedId: msg.feedId,
|
|
440
|
-
feedNamespace: msg.feedNamespace
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
const feedPrivateIds = /* @__PURE__ */ new Map();
|
|
445
|
-
yield* Effect.forEach([
|
|
446
|
-
...feedKeys.entries()
|
|
447
|
-
], ([key, { spaceId, feedId, feedNamespace }]) => Effect.gen(this, function* () {
|
|
448
|
-
const id = yield* this.#ensureFeed(spaceId, feedId, feedNamespace);
|
|
449
|
-
feedPrivateIds.set(key, id);
|
|
450
|
-
}), {
|
|
451
|
-
concurrency: "unbounded"
|
|
452
|
-
});
|
|
453
|
-
const lastSeqs = /* @__PURE__ */ new Map();
|
|
454
|
-
for (const [key] of feedKeys) {
|
|
455
|
-
const feedPrivateId = feedPrivateIds.get(key);
|
|
456
|
-
const lastBlockResult = yield* sql`
|
|
457
|
-
SELECT sequence, actorId FROM blocks
|
|
458
|
-
WHERE feedPrivateId = ${feedPrivateId}
|
|
459
|
-
ORDER BY sequence DESC
|
|
460
|
-
LIMIT 1
|
|
461
|
-
`;
|
|
462
|
-
lastSeqs.set(key, lastBlockResult[0] ?? null);
|
|
463
|
-
}
|
|
464
|
-
const currentSeqs = /* @__PURE__ */ new Map();
|
|
465
|
-
const blocks = [];
|
|
466
|
-
const blocksBySpaceNamespace = /* @__PURE__ */ new Map();
|
|
467
|
-
for (const msg of messages) {
|
|
468
|
-
const key = `${msg.spaceId}|${msg.feedNamespace}|${msg.feedId}`;
|
|
469
|
-
let sequence;
|
|
470
|
-
let prevSequence;
|
|
471
|
-
let prevActorId;
|
|
472
|
-
const inFlight = currentSeqs.get(key);
|
|
473
|
-
if (inFlight) {
|
|
474
|
-
sequence = inFlight.sequence + 1;
|
|
475
|
-
prevSequence = inFlight.sequence;
|
|
476
|
-
prevActorId = inFlight.actorId;
|
|
477
|
-
} else {
|
|
478
|
-
const lastBlock = lastSeqs.get(key);
|
|
479
|
-
sequence = (lastBlock?.sequence ?? -1) + 1;
|
|
480
|
-
prevSequence = lastBlock?.sequence ?? null;
|
|
481
|
-
prevActorId = lastBlock?.actorId ?? null;
|
|
482
|
-
}
|
|
483
|
-
const block = {
|
|
484
|
-
feedId: msg.feedId,
|
|
485
|
-
actorId: this.#options.localActorId,
|
|
486
|
-
sequence,
|
|
487
|
-
prevActorId,
|
|
488
|
-
prevSequence,
|
|
489
|
-
timestamp: Date.now(),
|
|
490
|
-
data: msg.data,
|
|
491
|
-
position: null
|
|
492
|
-
};
|
|
493
|
-
blocks.push(block);
|
|
494
|
-
currentSeqs.set(key, {
|
|
495
|
-
sequence,
|
|
496
|
-
actorId: this.#options.localActorId
|
|
497
|
-
});
|
|
498
|
-
const spaceNamespaceKey = `${msg.spaceId}|${msg.feedNamespace}`;
|
|
499
|
-
if (!blocksBySpaceNamespace.has(spaceNamespaceKey)) {
|
|
500
|
-
blocksBySpaceNamespace.set(spaceNamespaceKey, {
|
|
501
|
-
spaceId: msg.spaceId,
|
|
502
|
-
feedNamespace: msg.feedNamespace,
|
|
503
|
-
blocks: []
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
blocksBySpaceNamespace.get(spaceNamespaceKey).blocks.push(block);
|
|
507
|
-
}
|
|
508
|
-
const positionByBlock = /* @__PURE__ */ new Map();
|
|
509
|
-
for (const { spaceId, feedNamespace, blocks: batchBlocks } of blocksBySpaceNamespace.values()) {
|
|
510
|
-
const { positions } = yield* this.append({
|
|
511
|
-
requestId: "local-append",
|
|
512
|
-
blocks: batchBlocks,
|
|
513
|
-
spaceId,
|
|
514
|
-
feedNamespace
|
|
515
|
-
});
|
|
516
|
-
for (let i = 0; i < batchBlocks.length; i++) {
|
|
517
|
-
positionByBlock.set(batchBlocks[i], positions[i] ?? null);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
return blocks.map((block) => ({
|
|
521
|
-
...block,
|
|
522
|
-
position: positionByBlock.get(block) ?? block.position
|
|
523
|
-
}));
|
|
524
|
-
}).pipe(Effect.withSpan("FeedStore.appendLocal")));
|
|
525
|
-
/**
|
|
526
|
-
* Sets positions for existing blocks while preventing conflicting reassignments.
|
|
527
|
-
*/
|
|
528
|
-
setPosition = (request) => Effect.gen(this, function* () {
|
|
529
|
-
const sql = yield* SqlClient.SqlClient;
|
|
530
|
-
for (const block of request.blocks) {
|
|
531
|
-
const updated = yield* sql`
|
|
532
|
-
UPDATE blocks SET position = ${block.position}
|
|
533
|
-
WHERE feedPrivateId = (
|
|
534
|
-
SELECT feedPrivateId FROM feeds
|
|
535
|
-
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
536
|
-
)
|
|
537
|
-
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
538
|
-
AND (position IS NULL OR position = ${block.position})
|
|
539
|
-
RETURNING position
|
|
540
|
-
`;
|
|
541
|
-
if (updated.length > 0) {
|
|
542
|
-
continue;
|
|
543
|
-
}
|
|
544
|
-
const existing = yield* sql`
|
|
545
|
-
SELECT position FROM blocks
|
|
546
|
-
WHERE feedPrivateId = (
|
|
547
|
-
SELECT feedPrivateId FROM feeds
|
|
548
|
-
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
549
|
-
)
|
|
550
|
-
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
551
|
-
`;
|
|
552
|
-
const current = existing[0];
|
|
553
|
-
if (current?.position != null && current.position !== block.position) {
|
|
554
|
-
yield* Effect.fail(new PositionConflictError({
|
|
555
|
-
feedId: block.feedId,
|
|
556
|
-
actorId: block.actorId,
|
|
557
|
-
sequence: block.sequence,
|
|
558
|
-
currentPosition: current.position,
|
|
559
|
-
requestedPosition: block.position
|
|
560
|
-
}));
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
}).pipe(Effect.withSpan("FeedStore.setPosition"));
|
|
564
|
-
/**
|
|
565
|
-
* Gets all feeds and their blocks for a space, organized by feed ID and namespace.
|
|
566
|
-
* Used for space archive export.
|
|
567
|
-
*/
|
|
568
|
-
getAllFeedsForSpace = (opts) => Effect.gen(this, function* () {
|
|
569
|
-
const sql = yield* SqlClient.SqlClient;
|
|
570
|
-
const feeds = yield* sql`
|
|
571
|
-
SELECT DISTINCT feedId, feedNamespace FROM feeds WHERE spaceId = ${opts.spaceId}
|
|
572
|
-
`;
|
|
573
|
-
const result = [];
|
|
574
|
-
for (const feed of feeds) {
|
|
575
|
-
const blocks = yield* sql`
|
|
576
|
-
SELECT blocks.*, feeds.feedId, feeds.feedNamespace
|
|
577
|
-
FROM blocks
|
|
578
|
-
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
579
|
-
WHERE feeds.spaceId = ${opts.spaceId}
|
|
580
|
-
AND feeds.feedId = ${feed.feedId}
|
|
581
|
-
AND feeds.feedNamespace = ${feed.feedNamespace}
|
|
582
|
-
ORDER BY blocks.sequence ASC
|
|
583
|
-
`;
|
|
584
|
-
result.push({
|
|
585
|
-
feedId: feed.feedId,
|
|
586
|
-
feedNamespace: feed.feedNamespace,
|
|
587
|
-
blocks: blocks.map((row) => ({
|
|
588
|
-
...row,
|
|
589
|
-
data: new Uint8Array(row.data)
|
|
590
|
-
}))
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
return result;
|
|
594
|
-
}).pipe(Effect.withSpan("FeedStore.getAllFeedsForSpace"));
|
|
595
|
-
};
|
|
596
|
-
var encodeCursor = (token, insertionId) => FeedCursor.make(`${token}|${insertionId}`);
|
|
597
|
-
var decodeCursor = (cursor) => {
|
|
598
|
-
const [token, insertionId] = cursor.split("|");
|
|
599
|
-
return {
|
|
600
|
-
token,
|
|
601
|
-
insertionId: Number(insertionId)
|
|
602
|
-
};
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
// src/sync-server.ts
|
|
606
|
-
import * as Effect2 from "effect/Effect";
|
|
607
|
-
import { log } from "@dxos/log";
|
|
608
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-server.ts";
|
|
609
|
-
var inboundSummary = (message) => ({
|
|
610
|
-
tag: message._tag,
|
|
611
|
-
senderPeerId: message.senderPeerId,
|
|
612
|
-
recipientPeerId: message.recipientPeerId,
|
|
613
|
-
requestId: "requestId" in message ? message.requestId : void 0,
|
|
614
|
-
spaceId: "spaceId" in message ? message.spaceId : void 0,
|
|
615
|
-
feedNamespace: "feedNamespace" in message ? message.feedNamespace : void 0,
|
|
616
|
-
blockCount: "blocks" in message ? message.blocks.length : void 0,
|
|
617
|
-
position: "position" in message ? message.position : void 0,
|
|
618
|
-
limit: "limit" in message ? message.limit : void 0
|
|
619
|
-
});
|
|
620
|
-
var SyncServer = class {
|
|
621
|
-
#peerId;
|
|
622
|
-
#feedStore;
|
|
623
|
-
#sendMessage;
|
|
624
|
-
constructor(options) {
|
|
625
|
-
this.#peerId = options.peerId;
|
|
626
|
-
this.#feedStore = options.feedStore;
|
|
627
|
-
this.#sendMessage = options.sendMessage;
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Receive a message from a client. Handles QueryRequest and AppendRequest; sends response via sendMessage with correct peer ids.
|
|
631
|
-
*/
|
|
632
|
-
handleMessage(ctx, message) {
|
|
633
|
-
const self = this;
|
|
634
|
-
log("feed sync server received message", {
|
|
635
|
-
peerId: self.#peerId,
|
|
636
|
-
...inboundSummary(message)
|
|
637
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 33, S: this });
|
|
638
|
-
const recipientPeerId = message.senderPeerId;
|
|
639
|
-
const withPeerIds = (payload) => ({
|
|
640
|
-
...payload,
|
|
641
|
-
senderPeerId: self.#peerId,
|
|
642
|
-
recipientPeerId
|
|
643
|
-
});
|
|
644
|
-
switch (message._tag) {
|
|
645
|
-
case "QueryRequest": {
|
|
646
|
-
const req = message;
|
|
647
|
-
return Effect2.gen(function* () {
|
|
648
|
-
const response = yield* self.#feedStore.query(req);
|
|
649
|
-
log("feed sync server query completed", {
|
|
650
|
-
peerId: self.#peerId,
|
|
651
|
-
recipientPeerId,
|
|
652
|
-
requestId: req.requestId,
|
|
653
|
-
spaceId: req.spaceId,
|
|
654
|
-
feedNamespace: req.feedNamespace,
|
|
655
|
-
returnedBlocks: response.blocks.length,
|
|
656
|
-
hasMore: response.hasMore
|
|
657
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 49, S: this });
|
|
658
|
-
yield* self.#sendMessage(ctx, withPeerIds({
|
|
659
|
-
_tag: "QueryResponse",
|
|
660
|
-
...response
|
|
661
|
-
}));
|
|
662
|
-
}).pipe(Effect2.catchAll((err) => {
|
|
663
|
-
log("feed sync server query failed", {
|
|
664
|
-
peerId: self.#peerId,
|
|
665
|
-
recipientPeerId,
|
|
666
|
-
requestId: req.requestId,
|
|
667
|
-
spaceId: req.spaceId,
|
|
668
|
-
feedNamespace: req.feedNamespace,
|
|
669
|
-
cause: err instanceof Error ? err.message : String(err)
|
|
670
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 63, S: this });
|
|
671
|
-
return self.#sendMessage(ctx, withPeerIds({
|
|
672
|
-
_tag: "Error",
|
|
673
|
-
message: err instanceof Error ? err.message : String(err)
|
|
674
|
-
}));
|
|
675
|
-
}));
|
|
676
|
-
}
|
|
677
|
-
case "AppendRequest": {
|
|
678
|
-
const req = message;
|
|
679
|
-
return Effect2.gen(function* () {
|
|
680
|
-
const response = yield* self.#feedStore.append(req);
|
|
681
|
-
log("feed sync server append completed", {
|
|
682
|
-
peerId: self.#peerId,
|
|
683
|
-
recipientPeerId,
|
|
684
|
-
requestId: req.requestId,
|
|
685
|
-
spaceId: req.spaceId,
|
|
686
|
-
feedNamespace: req.feedNamespace,
|
|
687
|
-
blockCount: req.blocks.length,
|
|
688
|
-
assignedPositions: response.positions.length
|
|
689
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 82, S: this });
|
|
690
|
-
yield* self.#sendMessage(ctx, withPeerIds({
|
|
691
|
-
_tag: "AppendResponse",
|
|
692
|
-
...response
|
|
693
|
-
}));
|
|
694
|
-
}).pipe(Effect2.catchAll((err) => {
|
|
695
|
-
log("feed sync server append failed", {
|
|
696
|
-
peerId: self.#peerId,
|
|
697
|
-
recipientPeerId,
|
|
698
|
-
requestId: req.requestId,
|
|
699
|
-
spaceId: req.spaceId,
|
|
700
|
-
feedNamespace: req.feedNamespace,
|
|
701
|
-
blockCount: req.blocks.length,
|
|
702
|
-
cause: err instanceof Error ? err.message : String(err)
|
|
703
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 96, S: this });
|
|
704
|
-
return self.#sendMessage(ctx, withPeerIds({
|
|
705
|
-
_tag: "Error",
|
|
706
|
-
message: err instanceof Error ? err.message : String(err)
|
|
707
|
-
}));
|
|
708
|
-
}));
|
|
709
|
-
}
|
|
710
|
-
default:
|
|
711
|
-
log("feed sync server ignoring unsupported message", {
|
|
712
|
-
peerId: self.#peerId,
|
|
713
|
-
tag: message._tag
|
|
714
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 112, S: this });
|
|
715
|
-
return Effect2.void;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
};
|
|
719
|
-
|
|
720
|
-
// src/sync-client.ts
|
|
721
|
-
import * as Array from "effect/Array";
|
|
722
|
-
import * as Deferred from "effect/Deferred";
|
|
723
|
-
import * as Effect3 from "effect/Effect";
|
|
724
|
-
import { ContextDisposedError } from "@dxos/context";
|
|
725
|
-
import { log as log2 } from "@dxos/log";
|
|
726
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-client.ts";
|
|
727
|
-
var DEFAULT_SYNC_RPC_TIMEOUT_MS = 3e4;
|
|
728
|
-
var SyncClient = class {
|
|
729
|
-
#peerId;
|
|
730
|
-
#serverPeerId;
|
|
731
|
-
#feedStore;
|
|
732
|
-
#sendMessage;
|
|
733
|
-
#rpcTimeoutMs;
|
|
734
|
-
#handlers = /* @__PURE__ */ new Map();
|
|
735
|
-
constructor(options) {
|
|
736
|
-
this.#peerId = options.peerId;
|
|
737
|
-
this.#serverPeerId = options.serverPeerId;
|
|
738
|
-
this.#feedStore = options.feedStore;
|
|
739
|
-
this.#sendMessage = options.sendMessage;
|
|
740
|
-
this.#rpcTimeoutMs = options.rpcTimeoutMs ?? DEFAULT_SYNC_RPC_TIMEOUT_MS;
|
|
741
|
-
}
|
|
742
|
-
#withPeerIds(payload) {
|
|
743
|
-
if (payload._tag === "QueryRequest") {
|
|
744
|
-
return {
|
|
745
|
-
...payload,
|
|
746
|
-
senderPeerId: this.#peerId,
|
|
747
|
-
recipientPeerId: this.#serverPeerId
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
return {
|
|
751
|
-
...payload,
|
|
752
|
-
senderPeerId: this.#peerId,
|
|
753
|
-
recipientPeerId: this.#serverPeerId
|
|
754
|
-
};
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* Receive a message from the server. If a handler is registered for the message's requestId, complete it and remove; else noop.
|
|
758
|
-
*/
|
|
759
|
-
handleMessage(message) {
|
|
760
|
-
const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
|
|
761
|
-
if (requestId == null) {
|
|
762
|
-
log2.trace("feed sync client response ignored (no request id)", {
|
|
763
|
-
tag: message._tag,
|
|
764
|
-
senderPeerId: message.senderPeerId
|
|
765
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 49, S: this });
|
|
766
|
-
return Effect3.void;
|
|
767
|
-
}
|
|
768
|
-
const deferred = this.#handlers.get(requestId);
|
|
769
|
-
if (deferred == null) {
|
|
770
|
-
log2.trace("feed sync client response ignored (no pending rpc)", {
|
|
771
|
-
requestId,
|
|
772
|
-
tag: message._tag,
|
|
773
|
-
senderPeerId: message.senderPeerId
|
|
774
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 57, S: this });
|
|
775
|
-
return Effect3.void;
|
|
776
|
-
}
|
|
777
|
-
this.#handlers.delete(requestId);
|
|
778
|
-
log2("feed sync client rpc completed", {
|
|
779
|
-
requestId,
|
|
780
|
-
tag: message._tag,
|
|
781
|
-
blockCount: "blocks" in message ? message.blocks.length : void 0,
|
|
782
|
-
positionCount: "positions" in message ? message.positions.length : void 0,
|
|
783
|
-
error: message._tag === "Error" ? message.message : void 0
|
|
784
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 65, S: this });
|
|
785
|
-
if (message._tag === "Error") {
|
|
786
|
-
return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
|
|
787
|
-
}
|
|
788
|
-
return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
|
|
789
|
-
}
|
|
790
|
-
/** Removes the pending handler entry and unregisters the ctx onDispose hook. Idempotent. */
|
|
791
|
-
#disposeHandler(requestId, cleanupDispose) {
|
|
792
|
-
cleanupDispose();
|
|
793
|
-
this.#handlers.delete(requestId);
|
|
794
|
-
}
|
|
795
|
-
#awaitRpcResponse(requestId, deferred, cleanupDispose, meta) {
|
|
796
|
-
const self = this;
|
|
797
|
-
const timeoutMs = self.#rpcTimeoutMs;
|
|
798
|
-
return Effect3.ensuring(Deferred.await(deferred).pipe(Effect3.timeoutFail({
|
|
799
|
-
duration: timeoutMs,
|
|
800
|
-
onTimeout: () => new SyncRpcTimeoutError({
|
|
801
|
-
requestId,
|
|
802
|
-
spaceId: meta.spaceId,
|
|
803
|
-
feedNamespace: meta.feedNamespace,
|
|
804
|
-
rpcTag: meta.rpcTag,
|
|
805
|
-
timeoutMs
|
|
806
|
-
})
|
|
807
|
-
}), Effect3.tapError((cause) => Effect3.sync(() => {
|
|
808
|
-
if (cause instanceof SyncRpcTimeoutError) {
|
|
809
|
-
log2("feed sync client rpc timed out", {
|
|
810
|
-
requestId,
|
|
811
|
-
spaceId: meta.spaceId,
|
|
812
|
-
feedNamespace: meta.feedNamespace,
|
|
813
|
-
rpcTag: meta.rpcTag,
|
|
814
|
-
timeoutMs
|
|
815
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 95, S: this });
|
|
816
|
-
}
|
|
817
|
-
}))), Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose)));
|
|
818
|
-
}
|
|
819
|
-
pull(ctx, opts) {
|
|
820
|
-
const self = this;
|
|
821
|
-
return Effect3.gen(function* () {
|
|
822
|
-
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
823
|
-
spaceId: opts.spaceId,
|
|
824
|
-
feedNamespace: opts.feedNamespace
|
|
825
|
-
});
|
|
826
|
-
const requestId = crypto.randomUUID();
|
|
827
|
-
const deferred = yield* Deferred.make();
|
|
828
|
-
self.#handlers.set(requestId, deferred);
|
|
829
|
-
const cleanupDispose = ctx.disposed ? () => {
|
|
830
|
-
} : ctx.onDispose(() => {
|
|
831
|
-
Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
832
|
-
});
|
|
833
|
-
if (ctx.disposed) {
|
|
834
|
-
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
835
|
-
}
|
|
836
|
-
const request = {
|
|
837
|
-
_tag: "QueryRequest",
|
|
838
|
-
requestId,
|
|
839
|
-
spaceId: opts.spaceId,
|
|
840
|
-
feedNamespace: opts.feedNamespace,
|
|
841
|
-
position: lastPulledPosition,
|
|
842
|
-
limit: opts.limit
|
|
843
|
-
};
|
|
844
|
-
log2("feed sync client pull rpc sending", {
|
|
845
|
-
requestId,
|
|
846
|
-
spaceId: opts.spaceId,
|
|
847
|
-
feedNamespace: opts.feedNamespace,
|
|
848
|
-
afterPosition: lastPulledPosition,
|
|
849
|
-
limit: opts.limit
|
|
850
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 129, S: this });
|
|
851
|
-
yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
|
|
852
|
-
const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
|
|
853
|
-
spaceId: opts.spaceId,
|
|
854
|
-
feedNamespace: opts.feedNamespace,
|
|
855
|
-
rpcTag: "QueryRequest"
|
|
856
|
-
});
|
|
857
|
-
const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
|
|
858
|
-
if (response.blocks.length === 0) {
|
|
859
|
-
log2.trace("feed sync client pull done (empty batch)", {
|
|
860
|
-
requestId,
|
|
861
|
-
spaceId: opts.spaceId,
|
|
862
|
-
feedNamespace: opts.feedNamespace
|
|
863
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 144, S: this });
|
|
864
|
-
return {
|
|
865
|
-
done: true
|
|
866
|
-
};
|
|
867
|
-
}
|
|
868
|
-
yield* self.#feedStore.append({
|
|
869
|
-
spaceId: opts.spaceId,
|
|
870
|
-
feedNamespace: opts.feedNamespace,
|
|
871
|
-
blocks: response.blocks
|
|
872
|
-
});
|
|
873
|
-
const maxPulledPosition = response.blocks.reduce((max, block) => block.position != null && block.position > max ? block.position : max, lastPulledPosition);
|
|
874
|
-
yield* self.#feedStore.setSyncState({
|
|
875
|
-
spaceId: opts.spaceId,
|
|
876
|
-
feedNamespace: opts.feedNamespace,
|
|
877
|
-
lastPulledPosition: maxPulledPosition
|
|
878
|
-
});
|
|
879
|
-
log2("feed sync client pull applied batch", {
|
|
880
|
-
requestId,
|
|
881
|
-
spaceId: opts.spaceId,
|
|
882
|
-
feedNamespace: opts.feedNamespace,
|
|
883
|
-
batchSize: response.blocks.length,
|
|
884
|
-
hasMore: response.hasMore,
|
|
885
|
-
maxPulledPosition
|
|
886
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 165, S: this });
|
|
887
|
-
return {
|
|
888
|
-
done: false
|
|
889
|
-
};
|
|
890
|
-
});
|
|
891
|
-
}
|
|
892
|
-
/**
|
|
893
|
-
* Probes remote for blocks after the last pulled position without mutating local storage.
|
|
894
|
-
* Returns the number of blocks in the first batch (0 when caught up with remote).
|
|
895
|
-
*/
|
|
896
|
-
peekPull(ctx, opts) {
|
|
897
|
-
const self = this;
|
|
898
|
-
return Effect3.gen(function* () {
|
|
899
|
-
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
900
|
-
spaceId: opts.spaceId,
|
|
901
|
-
feedNamespace: opts.feedNamespace
|
|
902
|
-
});
|
|
903
|
-
const requestId = crypto.randomUUID();
|
|
904
|
-
const deferred = yield* Deferred.make();
|
|
905
|
-
self.#handlers.set(requestId, deferred);
|
|
906
|
-
const cleanupDispose = ctx.disposed ? () => {
|
|
907
|
-
} : ctx.onDispose(() => {
|
|
908
|
-
Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
909
|
-
});
|
|
910
|
-
if (ctx.disposed) {
|
|
911
|
-
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
912
|
-
}
|
|
913
|
-
const request = {
|
|
914
|
-
_tag: "QueryRequest",
|
|
915
|
-
requestId,
|
|
916
|
-
spaceId: opts.spaceId,
|
|
917
|
-
feedNamespace: opts.feedNamespace,
|
|
918
|
-
position: lastPulledPosition,
|
|
919
|
-
limit: opts.limit
|
|
920
|
-
};
|
|
921
|
-
yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
|
|
922
|
-
const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
|
|
923
|
-
spaceId: opts.spaceId,
|
|
924
|
-
feedNamespace: opts.feedNamespace,
|
|
925
|
-
rpcTag: "QueryRequest"
|
|
926
|
-
});
|
|
927
|
-
const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
|
|
928
|
-
return {
|
|
929
|
-
blocksToPull: response.blocks.length
|
|
930
|
-
};
|
|
931
|
-
});
|
|
932
|
-
}
|
|
933
|
-
push(ctx, opts) {
|
|
934
|
-
const self = this;
|
|
935
|
-
return Effect3.gen(function* () {
|
|
936
|
-
const unpositioned = yield* self.#feedStore.query({
|
|
937
|
-
spaceId: opts.spaceId,
|
|
938
|
-
feedNamespace: opts.feedNamespace,
|
|
939
|
-
unpositionedOnly: true,
|
|
940
|
-
limit: opts.limit
|
|
941
|
-
});
|
|
942
|
-
if (unpositioned.blocks.length === 0) {
|
|
943
|
-
log2.trace("feed sync client push skipped (nothing to send)", {
|
|
944
|
-
spaceId: opts.spaceId,
|
|
945
|
-
feedNamespace: opts.feedNamespace
|
|
946
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 227, S: this });
|
|
947
|
-
return {
|
|
948
|
-
done: true
|
|
949
|
-
};
|
|
950
|
-
}
|
|
951
|
-
const requestId = crypto.randomUUID();
|
|
952
|
-
const deferred = yield* Deferred.make();
|
|
953
|
-
self.#handlers.set(requestId, deferred);
|
|
954
|
-
const cleanupDispose = ctx.disposed ? () => {
|
|
955
|
-
} : ctx.onDispose(() => {
|
|
956
|
-
Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
957
|
-
});
|
|
958
|
-
if (ctx.disposed) {
|
|
959
|
-
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
960
|
-
}
|
|
961
|
-
const request = {
|
|
962
|
-
_tag: "AppendRequest",
|
|
963
|
-
requestId,
|
|
964
|
-
spaceId: opts.spaceId,
|
|
965
|
-
feedNamespace: opts.feedNamespace,
|
|
966
|
-
blocks: unpositioned.blocks
|
|
967
|
-
};
|
|
968
|
-
log2("feed sync client push rpc sending", {
|
|
969
|
-
requestId,
|
|
970
|
-
spaceId: opts.spaceId,
|
|
971
|
-
feedNamespace: opts.feedNamespace,
|
|
972
|
-
blockCount: unpositioned.blocks.length
|
|
973
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 251, S: this });
|
|
974
|
-
yield* self.#sendMessage(ctx, self.#withPeerIds(request)).pipe(Effect3.tapErrorCause(() => Effect3.sync(() => self.#disposeHandler(requestId, cleanupDispose))));
|
|
975
|
-
const message = yield* self.#awaitRpcResponse(requestId, deferred, cleanupDispose, {
|
|
976
|
-
spaceId: opts.spaceId,
|
|
977
|
-
feedNamespace: opts.feedNamespace,
|
|
978
|
-
rpcTag: "AppendRequest"
|
|
979
|
-
});
|
|
980
|
-
const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
|
|
981
|
-
yield* self.#feedStore.setPosition({
|
|
982
|
-
spaceId: opts.spaceId,
|
|
983
|
-
blocks: Array.zipWith(response.positions, unpositioned.blocks, (position, block) => ({
|
|
984
|
-
feedId: block.feedId,
|
|
985
|
-
feedNamespace: opts.feedNamespace,
|
|
986
|
-
actorId: block.actorId,
|
|
987
|
-
sequence: block.sequence,
|
|
988
|
-
position
|
|
989
|
-
}))
|
|
990
|
-
});
|
|
991
|
-
log2("feed sync client push positions applied", {
|
|
992
|
-
requestId,
|
|
993
|
-
spaceId: opts.spaceId,
|
|
994
|
-
feedNamespace: opts.feedNamespace,
|
|
995
|
-
positionCount: response.positions.length
|
|
996
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 274, S: this });
|
|
997
|
-
return {
|
|
998
|
-
done: false
|
|
999
|
-
};
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
#expectResponse(requestId, message, expectedTag) {
|
|
1003
|
-
if (message._tag === "Error") {
|
|
1004
|
-
return Effect3.fail(new Error(message.message));
|
|
1005
|
-
}
|
|
1006
|
-
const requestIdMsg = "requestId" in message ? String(message.requestId) : void 0;
|
|
1007
|
-
if (message._tag !== expectedTag || requestIdMsg !== requestId) {
|
|
1008
|
-
return Effect3.fail(new Error(`Unexpected message: expected ${expectedTag} with requestId ${requestId}`));
|
|
1009
|
-
}
|
|
1010
|
-
return Effect3.succeed(message);
|
|
1011
|
-
}
|
|
1012
|
-
};
|
|
1013
|
-
|
|
1014
|
-
export {
|
|
1015
|
-
SyncRpcTimeoutError,
|
|
1016
|
-
PositionConflictError,
|
|
1017
|
-
FeedStore,
|
|
1018
|
-
SyncServer,
|
|
1019
|
-
DEFAULT_SYNC_RPC_TIMEOUT_MS,
|
|
1020
|
-
SyncClient
|
|
1021
|
-
};
|
|
1022
|
-
//# sourceMappingURL=chunk-UC5ETQIJ.mjs.map
|