@dxos/feed 0.8.4-main.ef1bc66f44 → 0.8.4-main.f466a3d56e
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 +102 -5
- package/README.md +0 -1
- package/dist/lib/neutral/{chunk-Q5PE2Q5P.mjs → chunk-YYASRWLE.mjs} +264 -119
- package/dist/lib/neutral/chunk-YYASRWLE.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1 -5
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/testing/index.mjs +8 -7
- package/dist/lib/neutral/testing/index.mjs.map +3 -3
- package/dist/types/src/errors.d.ts +17 -57
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/feed-store.d.ts +39 -4
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +4 -3
- package/dist/types/src/sync-client.d.ts.map +1 -1
- package/dist/types/src/sync-server.d.ts +3 -2
- package/dist/types/src/sync-server.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +5 -5
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -21
- package/src/errors.ts +4 -17
- package/src/feed-store.test.ts +206 -97
- package/src/feed-store.ts +142 -123
- package/src/sync-client.ts +109 -17
- package/src/sync-server.ts +70 -12
- package/src/testing/test-builder.ts +11 -11
- package/dist/lib/neutral/chunk-Q5PE2Q5P.mjs.map +0 -7
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
2
|
import { BaseError } from "@dxos/errors";
|
|
3
|
-
var DuplicateBlockPositionError = class extends BaseError.extend("DuplicateBlockPositionError") {
|
|
4
|
-
constructor(args) {
|
|
5
|
-
super(`Non-unique block position detected (feedId=${args.feedId} position=${args.position}).`);
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
var DuplicateBlockLamportTimestampError = class extends BaseError.extend("DuplicateBlockLamportTimestampError") {
|
|
9
|
-
constructor(args) {
|
|
10
|
-
super(`Non-unique block Lamport timestamp detected (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
3
|
var PositionConflictError = class extends BaseError.extend("PositionConflictError") {
|
|
14
4
|
constructor(args) {
|
|
15
|
-
super(
|
|
5
|
+
super({
|
|
6
|
+
message: `Block already has position ${args.currentPosition}, cannot set to ${args.requestedPosition} (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
|
|
7
|
+
context: args
|
|
8
|
+
});
|
|
16
9
|
}
|
|
17
10
|
};
|
|
18
11
|
|
|
@@ -84,7 +77,9 @@ var FeedStore = class {
|
|
|
84
77
|
const rows = yield* sql`
|
|
85
78
|
SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
86
79
|
`;
|
|
87
|
-
if (rows.length > 0)
|
|
80
|
+
if (rows.length > 0) {
|
|
81
|
+
return rows[0].feedPrivateId;
|
|
82
|
+
}
|
|
88
83
|
const newRows = yield* sql`
|
|
89
84
|
INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
|
|
90
85
|
`;
|
|
@@ -96,7 +91,9 @@ var FeedStore = class {
|
|
|
96
91
|
#ensureCursorToken = Effect.fn("Feed.ensureCursorToken")((spaceId) => Effect.gen(this, function* () {
|
|
97
92
|
const sql = yield* SqlClient.SqlClient;
|
|
98
93
|
const rows = yield* sql`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
|
|
99
|
-
if (rows.length > 0)
|
|
94
|
+
if (rows.length > 0) {
|
|
95
|
+
return rows[0].token;
|
|
96
|
+
}
|
|
100
97
|
const token = crypto.randomUUID().replace(/-/g, "").slice(0, 6);
|
|
101
98
|
yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
|
|
102
99
|
return token;
|
|
@@ -247,80 +244,51 @@ var FeedStore = class {
|
|
|
247
244
|
`;
|
|
248
245
|
}).pipe(Effect.withSpan("FeedStore.setSyncState"));
|
|
249
246
|
/**
|
|
250
|
-
*
|
|
247
|
+
* Returns the number of stored blocks for a single feed in a space/namespace.
|
|
248
|
+
* Intended as a low-level primitive for callers (for example Cloudflare Worker code)
|
|
249
|
+
* that need to make retention decisions under constrained storage resources.
|
|
251
250
|
*/
|
|
252
|
-
|
|
251
|
+
countBlocks = (opts) => Effect.gen(this, function* () {
|
|
253
252
|
const sql = yield* SqlClient.SqlClient;
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
lamportFeedIdByKey.set(lamportKey, block.feedId);
|
|
277
|
-
}
|
|
278
|
-
const lamportConditions = blocks.map((block) => {
|
|
279
|
-
const feedPrivateId = feedPrivateIds.get(block.feedId);
|
|
280
|
-
return sql`(feedPrivateId = ${feedPrivateId} AND actorId = ${block.actorId} AND sequence = ${block.sequence})`;
|
|
281
|
-
});
|
|
282
|
-
if (lamportConditions.length > 0) {
|
|
283
|
-
const existingLamports = yield* sql`
|
|
284
|
-
SELECT feedPrivateId, actorId, sequence
|
|
285
|
-
FROM blocks
|
|
286
|
-
WHERE ${sql.or(lamportConditions)}
|
|
287
|
-
LIMIT 1
|
|
288
|
-
`;
|
|
289
|
-
const lamportConflict = existingLamports[0];
|
|
290
|
-
if (lamportConflict) {
|
|
291
|
-
const key = `${lamportConflict.feedPrivateId}|${lamportConflict.actorId}|${lamportConflict.sequence}`;
|
|
292
|
-
yield* Effect.fail(new DuplicateBlockLamportTimestampError({
|
|
293
|
-
feedId: lamportFeedIdByKey.get(key),
|
|
294
|
-
actorId: lamportConflict.actorId,
|
|
295
|
-
sequence: lamportConflict.sequence
|
|
296
|
-
}));
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
if (!this.#options.assignPositions) {
|
|
300
|
-
const positionedBlocks = blocks.filter((block) => block.position != null);
|
|
301
|
-
const positionConditions = positionedBlocks.map((block) => {
|
|
302
|
-
const feedPrivateId = feedPrivateIds.get(block.feedId);
|
|
303
|
-
return sql`(feedPrivateId = ${feedPrivateId} AND position = ${block.position})`;
|
|
304
|
-
});
|
|
305
|
-
if (positionConditions.length > 0) {
|
|
306
|
-
const existingPositions = yield* sql`
|
|
307
|
-
SELECT feedPrivateId, position
|
|
308
|
-
FROM blocks
|
|
309
|
-
WHERE position IS NOT NULL
|
|
310
|
-
AND (${sql.or(positionConditions)})
|
|
311
|
-
LIMIT 1
|
|
312
|
-
`;
|
|
313
|
-
const positionConflict = existingPositions[0];
|
|
314
|
-
if (positionConflict) {
|
|
315
|
-
const key = `${positionConflict.feedPrivateId}|${positionConflict.position}`;
|
|
316
|
-
yield* Effect.fail(new DuplicateBlockPositionError({
|
|
317
|
-
feedId: positionFeedIdByKey.get(key),
|
|
318
|
-
position: positionConflict.position
|
|
319
|
-
}));
|
|
320
|
-
}
|
|
321
|
-
}
|
|
253
|
+
const rows = yield* sql`
|
|
254
|
+
SELECT COUNT(*) AS count
|
|
255
|
+
FROM blocks
|
|
256
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
257
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
258
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
259
|
+
AND feeds.feedId = ${opts.feedId}
|
|
260
|
+
`;
|
|
261
|
+
return rows[0]?.count ?? 0;
|
|
262
|
+
}).pipe(Effect.withSpan("FeedStore.countBlocks"));
|
|
263
|
+
/**
|
|
264
|
+
* Deletes the oldest blocks for a single feed in a space/namespace.
|
|
265
|
+
* This API intentionally does not enforce any retention policy (such as max size);
|
|
266
|
+
* callers decide when and how much to prune, which is useful for constrained
|
|
267
|
+
* environments like Cloudflare Workers.
|
|
268
|
+
*
|
|
269
|
+
* @returns Number of deleted rows.
|
|
270
|
+
*/
|
|
271
|
+
deleteOldestBlocks = (opts) => Effect.gen(this, function* () {
|
|
272
|
+
const sql = yield* SqlClient.SqlClient;
|
|
273
|
+
if (opts.count <= 0) {
|
|
274
|
+
return 0;
|
|
322
275
|
}
|
|
323
|
-
|
|
276
|
+
const deletedRows = yield* sql`
|
|
277
|
+
DELETE FROM blocks
|
|
278
|
+
WHERE insertionId IN (
|
|
279
|
+
SELECT blocks.insertionId
|
|
280
|
+
FROM blocks
|
|
281
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
282
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
283
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
284
|
+
AND feeds.feedId = ${opts.feedId}
|
|
285
|
+
ORDER BY blocks.insertionId ASC
|
|
286
|
+
LIMIT ${opts.count}
|
|
287
|
+
)
|
|
288
|
+
RETURNING insertionId
|
|
289
|
+
`;
|
|
290
|
+
return deletedRows.length;
|
|
291
|
+
}).pipe(Effect.withSpan("FeedStore.deleteOldestBlocks"));
|
|
324
292
|
/**
|
|
325
293
|
* Appends blocks for a space/namespace and optionally assigns global positions.
|
|
326
294
|
*/
|
|
@@ -353,7 +321,6 @@ var FeedStore = class {
|
|
|
353
321
|
}), {
|
|
354
322
|
concurrency: "unbounded"
|
|
355
323
|
});
|
|
356
|
-
yield* this.#validateAppendBlocks(request.blocks, feedPrivateIds);
|
|
357
324
|
const maxPositions = /* @__PURE__ */ new Map();
|
|
358
325
|
if (this.#options.assignPositions) {
|
|
359
326
|
const maxPosResult = yield* sql`
|
|
@@ -475,15 +442,22 @@ var FeedStore = class {
|
|
|
475
442
|
}
|
|
476
443
|
blocksBySpaceNamespace.get(spaceNamespaceKey).blocks.push(block);
|
|
477
444
|
}
|
|
478
|
-
|
|
479
|
-
|
|
445
|
+
const positionByBlock = /* @__PURE__ */ new Map();
|
|
446
|
+
for (const { spaceId, feedNamespace, blocks: batchBlocks } of blocksBySpaceNamespace.values()) {
|
|
447
|
+
const { positions } = yield* this.append({
|
|
480
448
|
requestId: "local-append",
|
|
481
|
-
blocks:
|
|
449
|
+
blocks: batchBlocks,
|
|
482
450
|
spaceId,
|
|
483
451
|
feedNamespace
|
|
484
452
|
});
|
|
453
|
+
for (let i = 0; i < batchBlocks.length; i++) {
|
|
454
|
+
positionByBlock.set(batchBlocks[i], positions[i] ?? null);
|
|
455
|
+
}
|
|
485
456
|
}
|
|
486
|
-
return blocks
|
|
457
|
+
return blocks.map((block) => ({
|
|
458
|
+
...block,
|
|
459
|
+
position: positionByBlock.get(block) ?? block.position
|
|
460
|
+
}));
|
|
487
461
|
}).pipe(Effect.withSpan("FeedStore.appendLocal")));
|
|
488
462
|
/**
|
|
489
463
|
* Sets positions for existing blocks while preventing conflicting reassignments.
|
|
@@ -491,6 +465,19 @@ var FeedStore = class {
|
|
|
491
465
|
setPosition = (request) => Effect.gen(this, function* () {
|
|
492
466
|
const sql = yield* SqlClient.SqlClient;
|
|
493
467
|
for (const block of request.blocks) {
|
|
468
|
+
const updated = yield* sql`
|
|
469
|
+
UPDATE blocks SET position = ${block.position}
|
|
470
|
+
WHERE feedPrivateId = (
|
|
471
|
+
SELECT feedPrivateId FROM feeds
|
|
472
|
+
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
473
|
+
)
|
|
474
|
+
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
475
|
+
AND (position IS NULL OR position = ${block.position})
|
|
476
|
+
RETURNING position
|
|
477
|
+
`;
|
|
478
|
+
if (updated.length > 0) {
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
494
481
|
const existing = yield* sql`
|
|
495
482
|
SELECT position FROM blocks
|
|
496
483
|
WHERE feedPrivateId = (
|
|
@@ -509,16 +496,39 @@ var FeedStore = class {
|
|
|
509
496
|
requestedPosition: block.position
|
|
510
497
|
}));
|
|
511
498
|
}
|
|
512
|
-
yield* sql`
|
|
513
|
-
UPDATE blocks SET position = ${block.position}
|
|
514
|
-
WHERE feedPrivateId = (
|
|
515
|
-
SELECT feedPrivateId FROM feeds
|
|
516
|
-
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
517
|
-
)
|
|
518
|
-
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
519
|
-
`;
|
|
520
499
|
}
|
|
521
500
|
}).pipe(Effect.withSpan("FeedStore.setPosition"));
|
|
501
|
+
/**
|
|
502
|
+
* Gets all feeds and their blocks for a space, organized by feed ID and namespace.
|
|
503
|
+
* Used for space archive export.
|
|
504
|
+
*/
|
|
505
|
+
getAllFeedsForSpace = (opts) => Effect.gen(this, function* () {
|
|
506
|
+
const sql = yield* SqlClient.SqlClient;
|
|
507
|
+
const feeds = yield* sql`
|
|
508
|
+
SELECT DISTINCT feedId, feedNamespace FROM feeds WHERE spaceId = ${opts.spaceId}
|
|
509
|
+
`;
|
|
510
|
+
const result = [];
|
|
511
|
+
for (const feed of feeds) {
|
|
512
|
+
const blocks = yield* sql`
|
|
513
|
+
SELECT blocks.*, feeds.feedId, feeds.feedNamespace
|
|
514
|
+
FROM blocks
|
|
515
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
516
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
517
|
+
AND feeds.feedId = ${feed.feedId}
|
|
518
|
+
AND feeds.feedNamespace = ${feed.feedNamespace}
|
|
519
|
+
ORDER BY blocks.sequence ASC
|
|
520
|
+
`;
|
|
521
|
+
result.push({
|
|
522
|
+
feedId: feed.feedId,
|
|
523
|
+
feedNamespace: feed.feedNamespace,
|
|
524
|
+
blocks: blocks.map((row) => ({
|
|
525
|
+
...row,
|
|
526
|
+
data: new Uint8Array(row.data)
|
|
527
|
+
}))
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
return result;
|
|
531
|
+
}).pipe(Effect.withSpan("FeedStore.getAllFeedsForSpace"));
|
|
522
532
|
};
|
|
523
533
|
var encodeCursor = (token, insertionId) => FeedCursor.make(`${token}|${insertionId}`);
|
|
524
534
|
var decodeCursor = (cursor) => {
|
|
@@ -531,6 +541,19 @@ var decodeCursor = (cursor) => {
|
|
|
531
541
|
|
|
532
542
|
// src/sync-server.ts
|
|
533
543
|
import * as Effect2 from "effect/Effect";
|
|
544
|
+
import { log } from "@dxos/log";
|
|
545
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-server.ts";
|
|
546
|
+
var inboundSummary = (message) => ({
|
|
547
|
+
tag: message._tag,
|
|
548
|
+
senderPeerId: message.senderPeerId,
|
|
549
|
+
recipientPeerId: message.recipientPeerId,
|
|
550
|
+
requestId: "requestId" in message ? message.requestId : void 0,
|
|
551
|
+
spaceId: "spaceId" in message ? message.spaceId : void 0,
|
|
552
|
+
feedNamespace: "feedNamespace" in message ? message.feedNamespace : void 0,
|
|
553
|
+
blockCount: "blocks" in message ? message.blocks.length : void 0,
|
|
554
|
+
position: "position" in message ? message.position : void 0,
|
|
555
|
+
limit: "limit" in message ? message.limit : void 0
|
|
556
|
+
});
|
|
534
557
|
var SyncServer = class {
|
|
535
558
|
#peerId;
|
|
536
559
|
#feedStore;
|
|
@@ -543,8 +566,12 @@ var SyncServer = class {
|
|
|
543
566
|
/**
|
|
544
567
|
* Receive a message from a client. Handles QueryRequest and AppendRequest; sends response via sendMessage with correct peer ids.
|
|
545
568
|
*/
|
|
546
|
-
handleMessage(message) {
|
|
569
|
+
handleMessage(ctx, message) {
|
|
547
570
|
const self = this;
|
|
571
|
+
log("feed sync server received message", {
|
|
572
|
+
peerId: self.#peerId,
|
|
573
|
+
...inboundSummary(message)
|
|
574
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 33, S: this });
|
|
548
575
|
const recipientPeerId = message.senderPeerId;
|
|
549
576
|
const withPeerIds = (payload) => ({
|
|
550
577
|
...payload,
|
|
@@ -556,29 +583,70 @@ var SyncServer = class {
|
|
|
556
583
|
const req = message;
|
|
557
584
|
return Effect2.gen(function* () {
|
|
558
585
|
const response = yield* self.#feedStore.query(req);
|
|
559
|
-
|
|
586
|
+
log("feed sync server query completed", {
|
|
587
|
+
peerId: self.#peerId,
|
|
588
|
+
recipientPeerId,
|
|
589
|
+
requestId: req.requestId,
|
|
590
|
+
spaceId: req.spaceId,
|
|
591
|
+
feedNamespace: req.feedNamespace,
|
|
592
|
+
returnedBlocks: response.blocks.length,
|
|
593
|
+
hasMore: response.hasMore
|
|
594
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 49, S: this });
|
|
595
|
+
yield* self.#sendMessage(ctx, withPeerIds({
|
|
560
596
|
_tag: "QueryResponse",
|
|
561
597
|
...response
|
|
562
598
|
}));
|
|
563
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
599
|
+
}).pipe(Effect2.catchAll((err) => {
|
|
600
|
+
log("feed sync server query failed", {
|
|
601
|
+
peerId: self.#peerId,
|
|
602
|
+
recipientPeerId,
|
|
603
|
+
requestId: req.requestId,
|
|
604
|
+
spaceId: req.spaceId,
|
|
605
|
+
feedNamespace: req.feedNamespace,
|
|
606
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
607
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 63, S: this });
|
|
608
|
+
return self.#sendMessage(ctx, withPeerIds({
|
|
609
|
+
_tag: "Error",
|
|
610
|
+
message: err instanceof Error ? err.message : String(err)
|
|
611
|
+
}));
|
|
612
|
+
}));
|
|
567
613
|
}
|
|
568
614
|
case "AppendRequest": {
|
|
569
615
|
const req = message;
|
|
570
616
|
return Effect2.gen(function* () {
|
|
571
617
|
const response = yield* self.#feedStore.append(req);
|
|
572
|
-
|
|
618
|
+
log("feed sync server append completed", {
|
|
619
|
+
peerId: self.#peerId,
|
|
620
|
+
recipientPeerId,
|
|
621
|
+
requestId: req.requestId,
|
|
622
|
+
spaceId: req.spaceId,
|
|
623
|
+
feedNamespace: req.feedNamespace,
|
|
624
|
+
assignedPositions: response.positions.length
|
|
625
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 82, S: this });
|
|
626
|
+
yield* self.#sendMessage(ctx, withPeerIds({
|
|
573
627
|
_tag: "AppendResponse",
|
|
574
628
|
...response
|
|
575
629
|
}));
|
|
576
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
630
|
+
}).pipe(Effect2.catchAll((err) => {
|
|
631
|
+
log("feed sync server append failed", {
|
|
632
|
+
peerId: self.#peerId,
|
|
633
|
+
recipientPeerId,
|
|
634
|
+
requestId: req.requestId,
|
|
635
|
+
spaceId: req.spaceId,
|
|
636
|
+
feedNamespace: req.feedNamespace,
|
|
637
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
638
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 95, S: this });
|
|
639
|
+
return self.#sendMessage(ctx, withPeerIds({
|
|
640
|
+
_tag: "Error",
|
|
641
|
+
message: err instanceof Error ? err.message : String(err)
|
|
642
|
+
}));
|
|
643
|
+
}));
|
|
580
644
|
}
|
|
581
645
|
default:
|
|
646
|
+
log("feed sync server ignoring unsupported message", {
|
|
647
|
+
peerId: self.#peerId,
|
|
648
|
+
tag: message._tag
|
|
649
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 110, S: this });
|
|
582
650
|
return Effect2.void;
|
|
583
651
|
}
|
|
584
652
|
}
|
|
@@ -588,6 +656,9 @@ var SyncServer = class {
|
|
|
588
656
|
import * as Array from "effect/Array";
|
|
589
657
|
import * as Deferred from "effect/Deferred";
|
|
590
658
|
import * as Effect3 from "effect/Effect";
|
|
659
|
+
import { ContextDisposedError } from "@dxos/context";
|
|
660
|
+
import { log as log2 } from "@dxos/log";
|
|
661
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/echo/feed/src/sync-client.ts";
|
|
591
662
|
var SyncClient = class {
|
|
592
663
|
#peerId;
|
|
593
664
|
#serverPeerId;
|
|
@@ -619,16 +690,36 @@ var SyncClient = class {
|
|
|
619
690
|
*/
|
|
620
691
|
handleMessage(message) {
|
|
621
692
|
const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
|
|
622
|
-
if (requestId == null)
|
|
693
|
+
if (requestId == null) {
|
|
694
|
+
log2.trace("feed sync client response ignored (no request id)", {
|
|
695
|
+
tag: message._tag,
|
|
696
|
+
senderPeerId: message.senderPeerId
|
|
697
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 45, S: this });
|
|
698
|
+
return Effect3.void;
|
|
699
|
+
}
|
|
623
700
|
const deferred = this.#handlers.get(requestId);
|
|
624
|
-
if (deferred == null)
|
|
701
|
+
if (deferred == null) {
|
|
702
|
+
log2.trace("feed sync client response ignored (no pending rpc)", {
|
|
703
|
+
requestId,
|
|
704
|
+
tag: message._tag,
|
|
705
|
+
senderPeerId: message.senderPeerId
|
|
706
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 53, S: this });
|
|
707
|
+
return Effect3.void;
|
|
708
|
+
}
|
|
625
709
|
this.#handlers.delete(requestId);
|
|
710
|
+
log2("feed sync client rpc completed", {
|
|
711
|
+
requestId,
|
|
712
|
+
tag: message._tag,
|
|
713
|
+
blockCount: "blocks" in message ? message.blocks.length : void 0,
|
|
714
|
+
positionCount: "positions" in message ? message.positions.length : void 0,
|
|
715
|
+
error: message._tag === "Error" ? message.message : void 0
|
|
716
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 61, S: this });
|
|
626
717
|
if (message._tag === "Error") {
|
|
627
718
|
return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
|
|
628
719
|
}
|
|
629
720
|
return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
|
|
630
721
|
}
|
|
631
|
-
pull(opts) {
|
|
722
|
+
pull(ctx, opts) {
|
|
632
723
|
const self = this;
|
|
633
724
|
return Effect3.gen(function* () {
|
|
634
725
|
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
@@ -638,6 +729,13 @@ var SyncClient = class {
|
|
|
638
729
|
const requestId = crypto.randomUUID();
|
|
639
730
|
const deferred = yield* Deferred.make();
|
|
640
731
|
self.#handlers.set(requestId, deferred);
|
|
732
|
+
const cleanupDispose = ctx.disposed ? () => {
|
|
733
|
+
} : ctx.onDispose(() => {
|
|
734
|
+
Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
735
|
+
});
|
|
736
|
+
if (ctx.disposed) {
|
|
737
|
+
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
738
|
+
}
|
|
641
739
|
const request = {
|
|
642
740
|
_tag: "QueryRequest",
|
|
643
741
|
requestId,
|
|
@@ -646,10 +744,25 @@ var SyncClient = class {
|
|
|
646
744
|
position: lastPulledPosition,
|
|
647
745
|
limit: opts.limit
|
|
648
746
|
};
|
|
649
|
-
|
|
650
|
-
|
|
747
|
+
log2("feed sync client pull rpc sending", {
|
|
748
|
+
requestId,
|
|
749
|
+
spaceId: opts.spaceId,
|
|
750
|
+
feedNamespace: opts.feedNamespace,
|
|
751
|
+
afterPosition: lastPulledPosition,
|
|
752
|
+
limit: opts.limit
|
|
753
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 97, S: this });
|
|
754
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
755
|
+
const message = yield* Effect3.ensuring(Deferred.await(deferred), Effect3.sync(() => {
|
|
756
|
+
cleanupDispose();
|
|
757
|
+
self.#handlers.delete(requestId);
|
|
758
|
+
}));
|
|
651
759
|
const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
|
|
652
760
|
if (response.blocks.length === 0) {
|
|
761
|
+
log2.trace("feed sync client pull done (empty batch)", {
|
|
762
|
+
requestId,
|
|
763
|
+
spaceId: opts.spaceId,
|
|
764
|
+
feedNamespace: opts.feedNamespace
|
|
765
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 111, S: this });
|
|
653
766
|
return {
|
|
654
767
|
done: true
|
|
655
768
|
};
|
|
@@ -665,12 +778,20 @@ var SyncClient = class {
|
|
|
665
778
|
feedNamespace: opts.feedNamespace,
|
|
666
779
|
lastPulledPosition: maxPulledPosition
|
|
667
780
|
});
|
|
781
|
+
log2("feed sync client pull applied batch", {
|
|
782
|
+
requestId,
|
|
783
|
+
spaceId: opts.spaceId,
|
|
784
|
+
feedNamespace: opts.feedNamespace,
|
|
785
|
+
batchSize: response.blocks.length,
|
|
786
|
+
hasMore: response.hasMore,
|
|
787
|
+
maxPulledPosition
|
|
788
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 132, S: this });
|
|
668
789
|
return {
|
|
669
790
|
done: false
|
|
670
791
|
};
|
|
671
792
|
});
|
|
672
793
|
}
|
|
673
|
-
push(opts) {
|
|
794
|
+
push(ctx, opts) {
|
|
674
795
|
const self = this;
|
|
675
796
|
return Effect3.gen(function* () {
|
|
676
797
|
const unpositioned = yield* self.#feedStore.query({
|
|
@@ -680,6 +801,10 @@ var SyncClient = class {
|
|
|
680
801
|
limit: opts.limit
|
|
681
802
|
});
|
|
682
803
|
if (unpositioned.blocks.length === 0) {
|
|
804
|
+
log2.trace("feed sync client push skipped (nothing to send)", {
|
|
805
|
+
spaceId: opts.spaceId,
|
|
806
|
+
feedNamespace: opts.feedNamespace
|
|
807
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 155, S: this });
|
|
683
808
|
return {
|
|
684
809
|
done: true
|
|
685
810
|
};
|
|
@@ -687,6 +812,13 @@ var SyncClient = class {
|
|
|
687
812
|
const requestId = crypto.randomUUID();
|
|
688
813
|
const deferred = yield* Deferred.make();
|
|
689
814
|
self.#handlers.set(requestId, deferred);
|
|
815
|
+
const cleanupDispose = ctx.disposed ? () => {
|
|
816
|
+
} : ctx.onDispose(() => {
|
|
817
|
+
Effect3.runFork(Deferred.fail(deferred, new ContextDisposedError()));
|
|
818
|
+
});
|
|
819
|
+
if (ctx.disposed) {
|
|
820
|
+
yield* Deferred.fail(deferred, new ContextDisposedError());
|
|
821
|
+
}
|
|
690
822
|
const request = {
|
|
691
823
|
_tag: "AppendRequest",
|
|
692
824
|
requestId,
|
|
@@ -694,8 +826,17 @@ var SyncClient = class {
|
|
|
694
826
|
feedNamespace: opts.feedNamespace,
|
|
695
827
|
blocks: unpositioned.blocks
|
|
696
828
|
};
|
|
697
|
-
|
|
698
|
-
|
|
829
|
+
log2("feed sync client push rpc sending", {
|
|
830
|
+
requestId,
|
|
831
|
+
spaceId: opts.spaceId,
|
|
832
|
+
feedNamespace: opts.feedNamespace,
|
|
833
|
+
blockCount: unpositioned.blocks.length
|
|
834
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 179, S: this });
|
|
835
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
836
|
+
const message = yield* Effect3.ensuring(Deferred.await(deferred), Effect3.sync(() => {
|
|
837
|
+
cleanupDispose();
|
|
838
|
+
self.#handlers.delete(requestId);
|
|
839
|
+
}));
|
|
699
840
|
const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
|
|
700
841
|
yield* self.#feedStore.setPosition({
|
|
701
842
|
spaceId: opts.spaceId,
|
|
@@ -707,6 +848,12 @@ var SyncClient = class {
|
|
|
707
848
|
position
|
|
708
849
|
}))
|
|
709
850
|
});
|
|
851
|
+
log2("feed sync client push positions applied", {
|
|
852
|
+
requestId,
|
|
853
|
+
spaceId: opts.spaceId,
|
|
854
|
+
feedNamespace: opts.feedNamespace,
|
|
855
|
+
positionCount: response.positions.length
|
|
856
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 201, S: this });
|
|
710
857
|
return {
|
|
711
858
|
done: false
|
|
712
859
|
};
|
|
@@ -725,11 +872,9 @@ var SyncClient = class {
|
|
|
725
872
|
};
|
|
726
873
|
|
|
727
874
|
export {
|
|
728
|
-
DuplicateBlockPositionError,
|
|
729
|
-
DuplicateBlockLamportTimestampError,
|
|
730
875
|
PositionConflictError,
|
|
731
876
|
FeedStore,
|
|
732
877
|
SyncServer,
|
|
733
878
|
SyncClient
|
|
734
879
|
};
|
|
735
|
-
//# sourceMappingURL=chunk-
|
|
880
|
+
//# sourceMappingURL=chunk-YYASRWLE.mjs.map
|