@dxos/feed 0.8.4-main.40e3dcdf1b → 0.8.4-main.43cb759274
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/dist/lib/neutral/{chunk-2ENONWPH.mjs → chunk-UC5ETQIJ.mjs} +356 -37
- package/dist/lib/neutral/chunk-UC5ETQIJ.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +5 -1
- 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 +42 -6
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/feed-store.d.ts +26 -1
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +22 -3
- package/dist/types/src/sync-client.d.ts.map +1 -1
- package/dist/types/src/sync-client.test.d.ts +2 -0
- package/dist/types/src/sync-client.test.d.ts.map +1 -0
- 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 +18 -23
- package/src/errors.ts +9 -0
- package/src/feed-store.test.ts +75 -14
- package/src/feed-store.ts +157 -17
- package/src/sync-client.test.ts +63 -0
- package/src/sync-client.ts +215 -17
- package/src/sync-server.ts +72 -12
- package/src/sync.test.ts +2 -2
- package/src/testing/test-builder.ts +7 -7
- package/dist/lib/neutral/chunk-2ENONWPH.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.
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
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
|
+
};
|
|
3
11
|
var PositionConflictError = class extends BaseError.extend("PositionConflictError") {
|
|
4
12
|
constructor(args) {
|
|
5
13
|
super({
|
|
@@ -77,7 +85,9 @@ var FeedStore = class {
|
|
|
77
85
|
const rows = yield* sql`
|
|
78
86
|
SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
79
87
|
`;
|
|
80
|
-
if (rows.length > 0)
|
|
88
|
+
if (rows.length > 0) {
|
|
89
|
+
return rows[0].feedPrivateId;
|
|
90
|
+
}
|
|
81
91
|
const newRows = yield* sql`
|
|
82
92
|
INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
|
|
83
93
|
`;
|
|
@@ -89,7 +99,9 @@ var FeedStore = class {
|
|
|
89
99
|
#ensureCursorToken = Effect.fn("Feed.ensureCursorToken")((spaceId) => Effect.gen(this, function* () {
|
|
90
100
|
const sql = yield* SqlClient.SqlClient;
|
|
91
101
|
const rows = yield* sql`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
|
|
92
|
-
if (rows.length > 0)
|
|
102
|
+
if (rows.length > 0) {
|
|
103
|
+
return rows[0].token;
|
|
104
|
+
}
|
|
93
105
|
const token = crypto.randomUUID().replace(/-/g, "").slice(0, 6);
|
|
94
106
|
yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
|
|
95
107
|
return token;
|
|
@@ -240,6 +252,35 @@ var FeedStore = class {
|
|
|
240
252
|
`;
|
|
241
253
|
}).pipe(Effect.withSpan("FeedStore.setSyncState"));
|
|
242
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
|
+
/**
|
|
243
284
|
* Returns the number of stored blocks for a single feed in a space/namespace.
|
|
244
285
|
* Intended as a low-level primitive for callers (for example Cloudflare Worker code)
|
|
245
286
|
* that need to make retention decisions under constrained storage resources.
|
|
@@ -333,22 +374,48 @@ var FeedStore = class {
|
|
|
333
374
|
const feedPrivateId = feedPrivateIds.get(key);
|
|
334
375
|
let positionToInsert = null;
|
|
335
376
|
if (this.#options.assignPositions) {
|
|
336
|
-
|
|
337
|
-
positionToInsert = currentMax + 1;
|
|
338
|
-
maxPositions.set(request.feedNamespace, positionToInsert);
|
|
339
|
-
positions2.push(positionToInsert);
|
|
377
|
+
positionToInsert = maxPositions.get(request.feedNamespace) + 1;
|
|
340
378
|
} else if (block.position != null) {
|
|
341
379
|
positionToInsert = block.position;
|
|
342
380
|
}
|
|
343
|
-
yield* sql`
|
|
381
|
+
const inserted = yield* sql`
|
|
344
382
|
INSERT INTO blocks (
|
|
345
|
-
feedPrivateId, position, sequence, actorId,
|
|
383
|
+
feedPrivateId, position, sequence, actorId,
|
|
346
384
|
prevSequence, prevActorId, timestamp, data
|
|
347
385
|
) VALUES (
|
|
348
386
|
${feedPrivateId}, ${positionToInsert}, ${block.sequence}, ${block.actorId},
|
|
349
387
|
${block.prevSequence}, ${block.prevActorId}, ${block.timestamp}, ${block.data}
|
|
350
|
-
)
|
|
388
|
+
)
|
|
389
|
+
ON CONFLICT(feedPrivateId, sequence, actorId) DO NOTHING
|
|
390
|
+
RETURNING position
|
|
351
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);
|
|
352
419
|
}
|
|
353
420
|
return positions2;
|
|
354
421
|
}));
|
|
@@ -461,6 +528,19 @@ var FeedStore = class {
|
|
|
461
528
|
setPosition = (request) => Effect.gen(this, function* () {
|
|
462
529
|
const sql = yield* SqlClient.SqlClient;
|
|
463
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
|
+
}
|
|
464
544
|
const existing = yield* sql`
|
|
465
545
|
SELECT position FROM blocks
|
|
466
546
|
WHERE feedPrivateId = (
|
|
@@ -479,16 +559,39 @@ var FeedStore = class {
|
|
|
479
559
|
requestedPosition: block.position
|
|
480
560
|
}));
|
|
481
561
|
}
|
|
482
|
-
yield* sql`
|
|
483
|
-
UPDATE blocks SET position = ${block.position}
|
|
484
|
-
WHERE feedPrivateId = (
|
|
485
|
-
SELECT feedPrivateId FROM feeds
|
|
486
|
-
WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
|
|
487
|
-
)
|
|
488
|
-
AND actorId = ${block.actorId} AND sequence = ${block.sequence}
|
|
489
|
-
`;
|
|
490
562
|
}
|
|
491
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"));
|
|
492
595
|
};
|
|
493
596
|
var encodeCursor = (token, insertionId) => FeedCursor.make(`${token}|${insertionId}`);
|
|
494
597
|
var decodeCursor = (cursor) => {
|
|
@@ -501,6 +604,19 @@ var decodeCursor = (cursor) => {
|
|
|
501
604
|
|
|
502
605
|
// src/sync-server.ts
|
|
503
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
|
+
});
|
|
504
620
|
var SyncServer = class {
|
|
505
621
|
#peerId;
|
|
506
622
|
#feedStore;
|
|
@@ -513,8 +629,12 @@ var SyncServer = class {
|
|
|
513
629
|
/**
|
|
514
630
|
* Receive a message from a client. Handles QueryRequest and AppendRequest; sends response via sendMessage with correct peer ids.
|
|
515
631
|
*/
|
|
516
|
-
handleMessage(message) {
|
|
632
|
+
handleMessage(ctx, message) {
|
|
517
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 });
|
|
518
638
|
const recipientPeerId = message.senderPeerId;
|
|
519
639
|
const withPeerIds = (payload) => ({
|
|
520
640
|
...payload,
|
|
@@ -526,29 +646,72 @@ var SyncServer = class {
|
|
|
526
646
|
const req = message;
|
|
527
647
|
return Effect2.gen(function* () {
|
|
528
648
|
const response = yield* self.#feedStore.query(req);
|
|
529
|
-
|
|
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({
|
|
530
659
|
_tag: "QueryResponse",
|
|
531
660
|
...response
|
|
532
661
|
}));
|
|
533
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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
|
+
}));
|
|
537
676
|
}
|
|
538
677
|
case "AppendRequest": {
|
|
539
678
|
const req = message;
|
|
540
679
|
return Effect2.gen(function* () {
|
|
541
680
|
const response = yield* self.#feedStore.append(req);
|
|
542
|
-
|
|
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({
|
|
543
691
|
_tag: "AppendResponse",
|
|
544
692
|
...response
|
|
545
693
|
}));
|
|
546
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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
|
+
}));
|
|
550
709
|
}
|
|
551
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 });
|
|
552
715
|
return Effect2.void;
|
|
553
716
|
}
|
|
554
717
|
}
|
|
@@ -558,17 +721,23 @@ var SyncServer = class {
|
|
|
558
721
|
import * as Array from "effect/Array";
|
|
559
722
|
import * as Deferred from "effect/Deferred";
|
|
560
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;
|
|
561
728
|
var SyncClient = class {
|
|
562
729
|
#peerId;
|
|
563
730
|
#serverPeerId;
|
|
564
731
|
#feedStore;
|
|
565
732
|
#sendMessage;
|
|
733
|
+
#rpcTimeoutMs;
|
|
566
734
|
#handlers = /* @__PURE__ */ new Map();
|
|
567
735
|
constructor(options) {
|
|
568
736
|
this.#peerId = options.peerId;
|
|
569
737
|
this.#serverPeerId = options.serverPeerId;
|
|
570
738
|
this.#feedStore = options.feedStore;
|
|
571
739
|
this.#sendMessage = options.sendMessage;
|
|
740
|
+
this.#rpcTimeoutMs = options.rpcTimeoutMs ?? DEFAULT_SYNC_RPC_TIMEOUT_MS;
|
|
572
741
|
}
|
|
573
742
|
#withPeerIds(payload) {
|
|
574
743
|
if (payload._tag === "QueryRequest") {
|
|
@@ -589,16 +758,65 @@ var SyncClient = class {
|
|
|
589
758
|
*/
|
|
590
759
|
handleMessage(message) {
|
|
591
760
|
const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
|
|
592
|
-
if (requestId == null)
|
|
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
|
+
}
|
|
593
768
|
const deferred = this.#handlers.get(requestId);
|
|
594
|
-
if (deferred == null)
|
|
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
|
+
}
|
|
595
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 });
|
|
596
785
|
if (message._tag === "Error") {
|
|
597
786
|
return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
|
|
598
787
|
}
|
|
599
788
|
return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
|
|
600
789
|
}
|
|
601
|
-
|
|
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) {
|
|
602
820
|
const self = this;
|
|
603
821
|
return Effect3.gen(function* () {
|
|
604
822
|
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
@@ -608,6 +826,13 @@ var SyncClient = class {
|
|
|
608
826
|
const requestId = crypto.randomUUID();
|
|
609
827
|
const deferred = yield* Deferred.make();
|
|
610
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
|
+
}
|
|
611
836
|
const request = {
|
|
612
837
|
_tag: "QueryRequest",
|
|
613
838
|
requestId,
|
|
@@ -616,10 +841,26 @@ var SyncClient = class {
|
|
|
616
841
|
position: lastPulledPosition,
|
|
617
842
|
limit: opts.limit
|
|
618
843
|
};
|
|
619
|
-
|
|
620
|
-
|
|
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
|
+
});
|
|
621
857
|
const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
|
|
622
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 });
|
|
623
864
|
return {
|
|
624
865
|
done: true
|
|
625
866
|
};
|
|
@@ -635,12 +876,61 @@ var SyncClient = class {
|
|
|
635
876
|
feedNamespace: opts.feedNamespace,
|
|
636
877
|
lastPulledPosition: maxPulledPosition
|
|
637
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 });
|
|
638
887
|
return {
|
|
639
888
|
done: false
|
|
640
889
|
};
|
|
641
890
|
});
|
|
642
891
|
}
|
|
643
|
-
|
|
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) {
|
|
644
934
|
const self = this;
|
|
645
935
|
return Effect3.gen(function* () {
|
|
646
936
|
const unpositioned = yield* self.#feedStore.query({
|
|
@@ -650,6 +940,10 @@ var SyncClient = class {
|
|
|
650
940
|
limit: opts.limit
|
|
651
941
|
});
|
|
652
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 });
|
|
653
947
|
return {
|
|
654
948
|
done: true
|
|
655
949
|
};
|
|
@@ -657,6 +951,13 @@ var SyncClient = class {
|
|
|
657
951
|
const requestId = crypto.randomUUID();
|
|
658
952
|
const deferred = yield* Deferred.make();
|
|
659
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
|
+
}
|
|
660
961
|
const request = {
|
|
661
962
|
_tag: "AppendRequest",
|
|
662
963
|
requestId,
|
|
@@ -664,8 +965,18 @@ var SyncClient = class {
|
|
|
664
965
|
feedNamespace: opts.feedNamespace,
|
|
665
966
|
blocks: unpositioned.blocks
|
|
666
967
|
};
|
|
667
|
-
|
|
668
|
-
|
|
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
|
+
});
|
|
669
980
|
const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
|
|
670
981
|
yield* self.#feedStore.setPosition({
|
|
671
982
|
spaceId: opts.spaceId,
|
|
@@ -677,6 +988,12 @@ var SyncClient = class {
|
|
|
677
988
|
position
|
|
678
989
|
}))
|
|
679
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 });
|
|
680
997
|
return {
|
|
681
998
|
done: false
|
|
682
999
|
};
|
|
@@ -695,9 +1012,11 @@ var SyncClient = class {
|
|
|
695
1012
|
};
|
|
696
1013
|
|
|
697
1014
|
export {
|
|
1015
|
+
SyncRpcTimeoutError,
|
|
698
1016
|
PositionConflictError,
|
|
699
1017
|
FeedStore,
|
|
700
1018
|
SyncServer,
|
|
1019
|
+
DEFAULT_SYNC_RPC_TIMEOUT_MS,
|
|
701
1020
|
SyncClient
|
|
702
1021
|
};
|
|
703
|
-
//# sourceMappingURL=chunk-
|
|
1022
|
+
//# sourceMappingURL=chunk-UC5ETQIJ.mjs.map
|