@dxos/feed 0.8.4-staging.ac66bdf99f → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +102 -5
- package/dist/lib/neutral/{chunk-53CMKQL5.mjs → chunk-UC5ETQIJ.mjs} +320 -32
- 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 +3 -9
- package/dist/lib/neutral/testing/index.mjs.map +2 -2
- 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 +15 -1
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +18 -0
- 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.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +3 -3
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -23
- package/src/errors.ts +9 -0
- package/src/feed-store.test.ts +75 -14
- package/src/feed-store.ts +109 -17
- package/src/sync-client.test.ts +63 -0
- package/src/sync-client.ts +198 -7
- package/src/sync-server.ts +64 -8
- package/src/sync.test.ts +2 -2
- package/dist/lib/neutral/chunk-53CMKQL5.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
|
|
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}
|
|
351
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,14 +559,6 @@ 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"));
|
|
492
564
|
/**
|
|
@@ -532,6 +604,19 @@ var decodeCursor = (cursor) => {
|
|
|
532
604
|
|
|
533
605
|
// src/sync-server.ts
|
|
534
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
|
+
});
|
|
535
620
|
var SyncServer = class {
|
|
536
621
|
#peerId;
|
|
537
622
|
#feedStore;
|
|
@@ -546,6 +631,10 @@ var SyncServer = class {
|
|
|
546
631
|
*/
|
|
547
632
|
handleMessage(ctx, message) {
|
|
548
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 });
|
|
549
638
|
const recipientPeerId = message.senderPeerId;
|
|
550
639
|
const withPeerIds = (payload) => ({
|
|
551
640
|
...payload,
|
|
@@ -557,29 +646,72 @@ var SyncServer = class {
|
|
|
557
646
|
const req = message;
|
|
558
647
|
return Effect2.gen(function* () {
|
|
559
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 });
|
|
560
658
|
yield* self.#sendMessage(ctx, withPeerIds({
|
|
561
659
|
_tag: "QueryResponse",
|
|
562
660
|
...response
|
|
563
661
|
}));
|
|
564
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
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
|
+
}));
|
|
568
676
|
}
|
|
569
677
|
case "AppendRequest": {
|
|
570
678
|
const req = message;
|
|
571
679
|
return Effect2.gen(function* () {
|
|
572
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 });
|
|
573
690
|
yield* self.#sendMessage(ctx, withPeerIds({
|
|
574
691
|
_tag: "AppendResponse",
|
|
575
692
|
...response
|
|
576
693
|
}));
|
|
577
|
-
}).pipe(Effect2.catchAll((err) =>
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
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
|
+
}));
|
|
581
709
|
}
|
|
582
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 });
|
|
583
715
|
return Effect2.void;
|
|
584
716
|
}
|
|
585
717
|
}
|
|
@@ -589,17 +721,23 @@ var SyncServer = class {
|
|
|
589
721
|
import * as Array from "effect/Array";
|
|
590
722
|
import * as Deferred from "effect/Deferred";
|
|
591
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;
|
|
592
728
|
var SyncClient = class {
|
|
593
729
|
#peerId;
|
|
594
730
|
#serverPeerId;
|
|
595
731
|
#feedStore;
|
|
596
732
|
#sendMessage;
|
|
733
|
+
#rpcTimeoutMs;
|
|
597
734
|
#handlers = /* @__PURE__ */ new Map();
|
|
598
735
|
constructor(options) {
|
|
599
736
|
this.#peerId = options.peerId;
|
|
600
737
|
this.#serverPeerId = options.serverPeerId;
|
|
601
738
|
this.#feedStore = options.feedStore;
|
|
602
739
|
this.#sendMessage = options.sendMessage;
|
|
740
|
+
this.#rpcTimeoutMs = options.rpcTimeoutMs ?? DEFAULT_SYNC_RPC_TIMEOUT_MS;
|
|
603
741
|
}
|
|
604
742
|
#withPeerIds(payload) {
|
|
605
743
|
if (payload._tag === "QueryRequest") {
|
|
@@ -620,15 +758,64 @@ var SyncClient = class {
|
|
|
620
758
|
*/
|
|
621
759
|
handleMessage(message) {
|
|
622
760
|
const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
|
|
623
|
-
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
|
+
}
|
|
624
768
|
const deferred = this.#handlers.get(requestId);
|
|
625
|
-
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
|
+
}
|
|
626
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 });
|
|
627
785
|
if (message._tag === "Error") {
|
|
628
786
|
return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
|
|
629
787
|
}
|
|
630
788
|
return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
|
|
631
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
|
+
}
|
|
632
819
|
pull(ctx, opts) {
|
|
633
820
|
const self = this;
|
|
634
821
|
return Effect3.gen(function* () {
|
|
@@ -639,6 +826,13 @@ var SyncClient = class {
|
|
|
639
826
|
const requestId = crypto.randomUUID();
|
|
640
827
|
const deferred = yield* Deferred.make();
|
|
641
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
|
+
}
|
|
642
836
|
const request = {
|
|
643
837
|
_tag: "QueryRequest",
|
|
644
838
|
requestId,
|
|
@@ -647,10 +841,26 @@ var SyncClient = class {
|
|
|
647
841
|
position: lastPulledPosition,
|
|
648
842
|
limit: opts.limit
|
|
649
843
|
};
|
|
650
|
-
|
|
651
|
-
|
|
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
|
+
});
|
|
652
857
|
const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
|
|
653
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 });
|
|
654
864
|
return {
|
|
655
865
|
done: true
|
|
656
866
|
};
|
|
@@ -666,11 +876,60 @@ var SyncClient = class {
|
|
|
666
876
|
feedNamespace: opts.feedNamespace,
|
|
667
877
|
lastPulledPosition: maxPulledPosition
|
|
668
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 });
|
|
669
887
|
return {
|
|
670
888
|
done: false
|
|
671
889
|
};
|
|
672
890
|
});
|
|
673
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
|
+
}
|
|
674
933
|
push(ctx, opts) {
|
|
675
934
|
const self = this;
|
|
676
935
|
return Effect3.gen(function* () {
|
|
@@ -681,6 +940,10 @@ var SyncClient = class {
|
|
|
681
940
|
limit: opts.limit
|
|
682
941
|
});
|
|
683
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 });
|
|
684
947
|
return {
|
|
685
948
|
done: true
|
|
686
949
|
};
|
|
@@ -688,6 +951,13 @@ var SyncClient = class {
|
|
|
688
951
|
const requestId = crypto.randomUUID();
|
|
689
952
|
const deferred = yield* Deferred.make();
|
|
690
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
|
+
}
|
|
691
961
|
const request = {
|
|
692
962
|
_tag: "AppendRequest",
|
|
693
963
|
requestId,
|
|
@@ -695,8 +965,18 @@ var SyncClient = class {
|
|
|
695
965
|
feedNamespace: opts.feedNamespace,
|
|
696
966
|
blocks: unpositioned.blocks
|
|
697
967
|
};
|
|
698
|
-
|
|
699
|
-
|
|
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
|
+
});
|
|
700
980
|
const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
|
|
701
981
|
yield* self.#feedStore.setPosition({
|
|
702
982
|
spaceId: opts.spaceId,
|
|
@@ -708,6 +988,12 @@ var SyncClient = class {
|
|
|
708
988
|
position
|
|
709
989
|
}))
|
|
710
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 });
|
|
711
997
|
return {
|
|
712
998
|
done: false
|
|
713
999
|
};
|
|
@@ -726,9 +1012,11 @@ var SyncClient = class {
|
|
|
726
1012
|
};
|
|
727
1013
|
|
|
728
1014
|
export {
|
|
1015
|
+
SyncRpcTimeoutError,
|
|
729
1016
|
PositionConflictError,
|
|
730
1017
|
FeedStore,
|
|
731
1018
|
SyncServer,
|
|
1019
|
+
DEFAULT_SYNC_RPC_TIMEOUT_MS,
|
|
732
1020
|
SyncClient
|
|
733
1021
|
};
|
|
734
|
-
//# sourceMappingURL=chunk-
|
|
1022
|
+
//# sourceMappingURL=chunk-UC5ETQIJ.mjs.map
|