@dxos/feed 0.8.4-main.52d7546f51 → 0.8.4-main.60689f5b1c

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 CHANGED
@@ -1,8 +1,105 @@
1
- MIT License
2
- Copyright (c) 2022 DXOS
1
+ # Functional Source License, Version 1.1, ALv2 Future License
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ ## Abbreviation
5
4
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ FSL-1.1-Apache-2.0
7
6
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
@@ -260,4 +260,3 @@ All timestamps (e.g., `insertionTimestamp`) are Unix timestamps in milliseconds.
260
260
  ### TODOs
261
261
 
262
262
  - [ ] Sync protocol only works if each namespace is synced separately.
263
-
@@ -1,21 +1,5 @@
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({
6
- message: `Non-unique block position detected (feedId=${args.feedId} position=${args.position}).`,
7
- context: args
8
- });
9
- }
10
- };
11
- var DuplicateBlockLamportTimestampError = class extends BaseError.extend("DuplicateBlockLamportTimestampError") {
12
- constructor(args) {
13
- super({
14
- message: `Non-unique block Lamport timestamp detected (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
15
- context: args
16
- });
17
- }
18
- };
19
3
  var PositionConflictError = class extends BaseError.extend("PositionConflictError") {
20
4
  constructor(args) {
21
5
  super({
@@ -93,7 +77,9 @@ var FeedStore = class {
93
77
  const rows = yield* sql`
94
78
  SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
95
79
  `;
96
- if (rows.length > 0) return rows[0].feedPrivateId;
80
+ if (rows.length > 0) {
81
+ return rows[0].feedPrivateId;
82
+ }
97
83
  const newRows = yield* sql`
98
84
  INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
99
85
  `;
@@ -105,7 +91,9 @@ var FeedStore = class {
105
91
  #ensureCursorToken = Effect.fn("Feed.ensureCursorToken")((spaceId) => Effect.gen(this, function* () {
106
92
  const sql = yield* SqlClient.SqlClient;
107
93
  const rows = yield* sql`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
108
- if (rows.length > 0) return rows[0].token;
94
+ if (rows.length > 0) {
95
+ return rows[0].token;
96
+ }
109
97
  const token = crypto.randomUUID().replace(/-/g, "").slice(0, 6);
110
98
  yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
111
99
  return token;
@@ -256,80 +244,51 @@ var FeedStore = class {
256
244
  `;
257
245
  }).pipe(Effect.withSpan("FeedStore.setSyncState"));
258
246
  /**
259
- * Validates that appended blocks do not introduce position or Lamport conflicts.
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.
260
250
  */
261
- #validateAppendBlocks = Effect.fn("FeedStore.validateAppendBlocks")((blocks, feedPrivateIds) => Effect.gen(this, function* () {
251
+ countBlocks = (opts) => Effect.gen(this, function* () {
262
252
  const sql = yield* SqlClient.SqlClient;
263
- const positionFeedIdByKey = /* @__PURE__ */ new Map();
264
- const lamportFeedIdByKey = /* @__PURE__ */ new Map();
265
- for (const block of blocks) {
266
- const feedPrivateId = feedPrivateIds.get(block.feedId);
267
- if (!this.#options.assignPositions && block.position != null) {
268
- const positionKey = `${feedPrivateId}|${block.position}`;
269
- if (positionFeedIdByKey.has(positionKey)) {
270
- yield* Effect.fail(new DuplicateBlockPositionError({
271
- feedId: block.feedId,
272
- position: block.position
273
- }));
274
- }
275
- positionFeedIdByKey.set(positionKey, block.feedId);
276
- }
277
- const lamportKey = `${feedPrivateId}|${block.actorId}|${block.sequence}`;
278
- if (lamportFeedIdByKey.has(lamportKey)) {
279
- yield* Effect.fail(new DuplicateBlockLamportTimestampError({
280
- feedId: block.feedId,
281
- actorId: block.actorId,
282
- sequence: block.sequence
283
- }));
284
- }
285
- lamportFeedIdByKey.set(lamportKey, block.feedId);
286
- }
287
- const lamportConditions = blocks.map((block) => {
288
- const feedPrivateId = feedPrivateIds.get(block.feedId);
289
- return sql`(feedPrivateId = ${feedPrivateId} AND actorId = ${block.actorId} AND sequence = ${block.sequence})`;
290
- });
291
- if (lamportConditions.length > 0) {
292
- const existingLamports = yield* sql`
293
- SELECT feedPrivateId, actorId, sequence
294
- FROM blocks
295
- WHERE ${sql.or(lamportConditions)}
296
- LIMIT 1
297
- `;
298
- const lamportConflict = existingLamports[0];
299
- if (lamportConflict) {
300
- const key = `${lamportConflict.feedPrivateId}|${lamportConflict.actorId}|${lamportConflict.sequence}`;
301
- yield* Effect.fail(new DuplicateBlockLamportTimestampError({
302
- feedId: lamportFeedIdByKey.get(key),
303
- actorId: lamportConflict.actorId,
304
- sequence: lamportConflict.sequence
305
- }));
306
- }
307
- }
308
- if (!this.#options.assignPositions) {
309
- const positionedBlocks = blocks.filter((block) => block.position != null);
310
- const positionConditions = positionedBlocks.map((block) => {
311
- const feedPrivateId = feedPrivateIds.get(block.feedId);
312
- return sql`(feedPrivateId = ${feedPrivateId} AND position = ${block.position})`;
313
- });
314
- if (positionConditions.length > 0) {
315
- const existingPositions = yield* sql`
316
- SELECT feedPrivateId, position
317
- FROM blocks
318
- WHERE position IS NOT NULL
319
- AND (${sql.or(positionConditions)})
320
- LIMIT 1
321
- `;
322
- const positionConflict = existingPositions[0];
323
- if (positionConflict) {
324
- const key = `${positionConflict.feedPrivateId}|${positionConflict.position}`;
325
- yield* Effect.fail(new DuplicateBlockPositionError({
326
- feedId: positionFeedIdByKey.get(key),
327
- position: positionConflict.position
328
- }));
329
- }
330
- }
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;
331
275
  }
332
- }));
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"));
333
292
  /**
334
293
  * Appends blocks for a space/namespace and optionally assigns global positions.
335
294
  */
@@ -362,7 +321,6 @@ var FeedStore = class {
362
321
  }), {
363
322
  concurrency: "unbounded"
364
323
  });
365
- yield* this.#validateAppendBlocks(request.blocks, feedPrivateIds);
366
324
  const maxPositions = /* @__PURE__ */ new Map();
367
325
  if (this.#options.assignPositions) {
368
326
  const maxPosResult = yield* sql`
@@ -484,15 +442,22 @@ var FeedStore = class {
484
442
  }
485
443
  blocksBySpaceNamespace.get(spaceNamespaceKey).blocks.push(block);
486
444
  }
487
- for (const { spaceId, feedNamespace, blocks: blocks2 } of blocksBySpaceNamespace.values()) {
488
- yield* this.append({
445
+ const positionByBlock = /* @__PURE__ */ new Map();
446
+ for (const { spaceId, feedNamespace, blocks: batchBlocks } of blocksBySpaceNamespace.values()) {
447
+ const { positions } = yield* this.append({
489
448
  requestId: "local-append",
490
- blocks: blocks2,
449
+ blocks: batchBlocks,
491
450
  spaceId,
492
451
  feedNamespace
493
452
  });
453
+ for (let i = 0; i < batchBlocks.length; i++) {
454
+ positionByBlock.set(batchBlocks[i], positions[i] ?? null);
455
+ }
494
456
  }
495
- return blocks;
457
+ return blocks.map((block) => ({
458
+ ...block,
459
+ position: positionByBlock.get(block) ?? block.position
460
+ }));
496
461
  }).pipe(Effect.withSpan("FeedStore.appendLocal")));
497
462
  /**
498
463
  * Sets positions for existing blocks while preventing conflicting reassignments.
@@ -500,6 +465,19 @@ var FeedStore = class {
500
465
  setPosition = (request) => Effect.gen(this, function* () {
501
466
  const sql = yield* SqlClient.SqlClient;
502
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
+ }
503
481
  const existing = yield* sql`
504
482
  SELECT position FROM blocks
505
483
  WHERE feedPrivateId = (
@@ -518,16 +496,39 @@ var FeedStore = class {
518
496
  requestedPosition: block.position
519
497
  }));
520
498
  }
521
- yield* sql`
522
- UPDATE blocks SET position = ${block.position}
523
- WHERE feedPrivateId = (
524
- SELECT feedPrivateId FROM feeds
525
- WHERE spaceId = ${request.spaceId} AND feedId = ${block.feedId} AND feedNamespace = ${block.feedNamespace}
526
- )
527
- AND actorId = ${block.actorId} AND sequence = ${block.sequence}
528
- `;
529
499
  }
530
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"));
531
532
  };
532
533
  var encodeCursor = (token, insertionId) => FeedCursor.make(`${token}|${insertionId}`);
533
534
  var decodeCursor = (cursor) => {
@@ -540,6 +541,19 @@ var decodeCursor = (cursor) => {
540
541
 
541
542
  // src/sync-server.ts
542
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
+ });
543
557
  var SyncServer = class {
544
558
  #peerId;
545
559
  #feedStore;
@@ -552,8 +566,12 @@ var SyncServer = class {
552
566
  /**
553
567
  * Receive a message from a client. Handles QueryRequest and AppendRequest; sends response via sendMessage with correct peer ids.
554
568
  */
555
- handleMessage(message) {
569
+ handleMessage(ctx, message) {
556
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 });
557
575
  const recipientPeerId = message.senderPeerId;
558
576
  const withPeerIds = (payload) => ({
559
577
  ...payload,
@@ -565,29 +583,70 @@ var SyncServer = class {
565
583
  const req = message;
566
584
  return Effect2.gen(function* () {
567
585
  const response = yield* self.#feedStore.query(req);
568
- yield* self.#sendMessage(withPeerIds({
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({
569
596
  _tag: "QueryResponse",
570
597
  ...response
571
598
  }));
572
- }).pipe(Effect2.catchAll((err) => self.#sendMessage(withPeerIds({
573
- _tag: "Error",
574
- message: err instanceof Error ? err.message : String(err)
575
- }))));
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
+ }));
576
613
  }
577
614
  case "AppendRequest": {
578
615
  const req = message;
579
616
  return Effect2.gen(function* () {
580
617
  const response = yield* self.#feedStore.append(req);
581
- yield* self.#sendMessage(withPeerIds({
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({
582
627
  _tag: "AppendResponse",
583
628
  ...response
584
629
  }));
585
- }).pipe(Effect2.catchAll((err) => self.#sendMessage(withPeerIds({
586
- _tag: "Error",
587
- message: err instanceof Error ? err.message : String(err)
588
- }))));
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
+ }));
589
644
  }
590
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 });
591
650
  return Effect2.void;
592
651
  }
593
652
  }
@@ -597,6 +656,9 @@ var SyncServer = class {
597
656
  import * as Array from "effect/Array";
598
657
  import * as Deferred from "effect/Deferred";
599
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";
600
662
  var SyncClient = class {
601
663
  #peerId;
602
664
  #serverPeerId;
@@ -628,16 +690,36 @@ var SyncClient = class {
628
690
  */
629
691
  handleMessage(message) {
630
692
  const requestId = "requestId" in message && message.requestId != null ? String(message.requestId) : void 0;
631
- if (requestId == null) return Effect3.void;
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
+ }
632
700
  const deferred = this.#handlers.get(requestId);
633
- if (deferred == null) return Effect3.void;
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
+ }
634
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 });
635
717
  if (message._tag === "Error") {
636
718
  return Effect3.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect3.void);
637
719
  }
638
720
  return Effect3.andThen(Deferred.succeed(deferred, message), () => Effect3.void);
639
721
  }
640
- pull(opts) {
722
+ pull(ctx, opts) {
641
723
  const self = this;
642
724
  return Effect3.gen(function* () {
643
725
  const lastPulledPosition = yield* self.#feedStore.getSyncState({
@@ -647,6 +729,13 @@ var SyncClient = class {
647
729
  const requestId = crypto.randomUUID();
648
730
  const deferred = yield* Deferred.make();
649
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
+ }
650
739
  const request = {
651
740
  _tag: "QueryRequest",
652
741
  requestId,
@@ -655,10 +744,25 @@ var SyncClient = class {
655
744
  position: lastPulledPosition,
656
745
  limit: opts.limit
657
746
  };
658
- yield* self.#sendMessage(self.#withPeerIds(request));
659
- const message = yield* Deferred.await(deferred);
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
+ }));
660
759
  const response = yield* self.#expectResponse(requestId, message, "QueryResponse");
661
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 });
662
766
  return {
663
767
  done: true
664
768
  };
@@ -674,12 +778,20 @@ var SyncClient = class {
674
778
  feedNamespace: opts.feedNamespace,
675
779
  lastPulledPosition: maxPulledPosition
676
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 });
677
789
  return {
678
790
  done: false
679
791
  };
680
792
  });
681
793
  }
682
- push(opts) {
794
+ push(ctx, opts) {
683
795
  const self = this;
684
796
  return Effect3.gen(function* () {
685
797
  const unpositioned = yield* self.#feedStore.query({
@@ -689,6 +801,10 @@ var SyncClient = class {
689
801
  limit: opts.limit
690
802
  });
691
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 });
692
808
  return {
693
809
  done: true
694
810
  };
@@ -696,6 +812,13 @@ var SyncClient = class {
696
812
  const requestId = crypto.randomUUID();
697
813
  const deferred = yield* Deferred.make();
698
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
+ }
699
822
  const request = {
700
823
  _tag: "AppendRequest",
701
824
  requestId,
@@ -703,8 +826,17 @@ var SyncClient = class {
703
826
  feedNamespace: opts.feedNamespace,
704
827
  blocks: unpositioned.blocks
705
828
  };
706
- yield* self.#sendMessage(self.#withPeerIds(request));
707
- const message = yield* Deferred.await(deferred);
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
+ }));
708
840
  const response = yield* self.#expectResponse(requestId, message, "AppendResponse");
709
841
  yield* self.#feedStore.setPosition({
710
842
  spaceId: opts.spaceId,
@@ -716,6 +848,12 @@ var SyncClient = class {
716
848
  position
717
849
  }))
718
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 });
719
857
  return {
720
858
  done: false
721
859
  };
@@ -734,11 +872,9 @@ var SyncClient = class {
734
872
  };
735
873
 
736
874
  export {
737
- DuplicateBlockPositionError,
738
- DuplicateBlockLamportTimestampError,
739
875
  PositionConflictError,
740
876
  FeedStore,
741
877
  SyncServer,
742
878
  SyncClient
743
879
  };
744
- //# sourceMappingURL=chunk-UOP2Q6KY.mjs.map
880
+ //# sourceMappingURL=chunk-YYASRWLE.mjs.map