@dxos/feed 0.8.4-main.6fa680abb7 → 0.8.4-main.74a063c4e0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/feed",
3
- "version": "0.8.4-main.6fa680abb7",
3
+ "version": "0.8.4-main.74a063c4e0",
4
4
  "description": "Append-only replicatable log",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -40,17 +40,17 @@
40
40
  "@effect/sql": "0.49.0",
41
41
  "@effect/sql-sqlite-node": "0.50.1",
42
42
  "@effect/vitest": "0.27.0",
43
- "effect": "3.19.16",
44
- "@dxos/async": "0.8.4-main.6fa680abb7",
45
- "@dxos/effect": "0.8.4-main.6fa680abb7",
46
- "@dxos/errors": "0.8.4-main.6fa680abb7",
47
- "@dxos/invariant": "0.8.4-main.6fa680abb7",
48
- "@dxos/keys": "0.8.4-main.6fa680abb7",
49
- "@dxos/protocols": "0.8.4-main.6fa680abb7",
50
- "@dxos/sql-sqlite": "0.8.4-main.6fa680abb7",
51
- "@dxos/log": "0.8.4-main.6fa680abb7",
52
- "@dxos/util": "0.8.4-main.6fa680abb7",
53
- "@dxos/context": "0.8.4-main.6fa680abb7"
43
+ "effect": "3.20.0",
44
+ "@dxos/async": "0.8.4-main.74a063c4e0",
45
+ "@dxos/context": "0.8.4-main.74a063c4e0",
46
+ "@dxos/invariant": "0.8.4-main.74a063c4e0",
47
+ "@dxos/effect": "0.8.4-main.74a063c4e0",
48
+ "@dxos/keys": "0.8.4-main.74a063c4e0",
49
+ "@dxos/errors": "0.8.4-main.74a063c4e0",
50
+ "@dxos/log": "0.8.4-main.74a063c4e0",
51
+ "@dxos/protocols": "0.8.4-main.74a063c4e0",
52
+ "@dxos/util": "0.8.4-main.74a063c4e0",
53
+ "@dxos/sql-sqlite": "0.8.4-main.74a063c4e0"
54
54
  },
55
55
  "publishConfig": {
56
56
  "access": "public"
package/src/feed-store.ts CHANGED
@@ -673,6 +673,54 @@ export class FeedStore {
673
673
  `;
674
674
  }
675
675
  }).pipe(Effect.withSpan('FeedStore.setPosition'));
676
+
677
+ /**
678
+ * Gets all feeds and their blocks for a space, organized by feed ID and namespace.
679
+ * Used for space archive export.
680
+ */
681
+ getAllFeedsForSpace = (opts: {
682
+ spaceId: SpaceId;
683
+ }): Effect.Effect<
684
+ Array<{
685
+ feedId: string;
686
+ feedNamespace: string;
687
+ blocks: Block[];
688
+ }>,
689
+ SqlError.SqlError,
690
+ SqlClient.SqlClient
691
+ > =>
692
+ Effect.gen(this, function* () {
693
+ const sql = yield* SqlClient.SqlClient;
694
+
695
+ const feeds = yield* sql<{ feedId: string; feedNamespace: string }>`
696
+ SELECT DISTINCT feedId, feedNamespace FROM feeds WHERE spaceId = ${opts.spaceId}
697
+ `;
698
+
699
+ const result: Array<{ feedId: string; feedNamespace: string; blocks: Block[] }> = [];
700
+
701
+ for (const feed of feeds) {
702
+ const blocks = yield* sql<Block>`
703
+ SELECT blocks.*, feeds.feedId, feeds.feedNamespace
704
+ FROM blocks
705
+ JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
706
+ WHERE feeds.spaceId = ${opts.spaceId}
707
+ AND feeds.feedId = ${feed.feedId}
708
+ AND feeds.feedNamespace = ${feed.feedNamespace}
709
+ ORDER BY blocks.sequence ASC
710
+ `;
711
+
712
+ result.push({
713
+ feedId: feed.feedId,
714
+ feedNamespace: feed.feedNamespace,
715
+ blocks: blocks.map((row) => ({
716
+ ...row,
717
+ data: new Uint8Array(row.data),
718
+ })),
719
+ });
720
+ }
721
+
722
+ return result;
723
+ }).pipe(Effect.withSpan('FeedStore.getAllFeedsForSpace'));
676
724
  }
677
725
 
678
726
  const encodeCursor = (token: string, insertionId: number) => FeedCursor.make(`${token}|${insertionId}`);
@@ -7,6 +7,7 @@ import * as Array from 'effect/Array';
7
7
  import * as Deferred from 'effect/Deferred';
8
8
  import * as Effect from 'effect/Effect';
9
9
 
10
+ import { Context } from '@dxos/context';
10
11
  import type { SpaceId } from '@dxos/keys';
11
12
  import { type FeedProtocol } from '@dxos/protocols';
12
13
  import type { SqlTransaction } from '@dxos/sql-sqlite';
@@ -30,7 +31,7 @@ export type SyncClientOptions = {
30
31
  serverPeerId?: string;
31
32
  feedStore: FeedStore;
32
33
  /** Send a protocol message to the server. Returns Effect. */
33
- sendMessage: (message: RequestMessage) => Effect.Effect<void, unknown, never>;
34
+ sendMessage: (ctx: Context, message: RequestMessage) => Effect.Effect<void, unknown, never>;
34
35
  };
35
36
 
36
37
  /**
@@ -83,11 +84,14 @@ export class SyncClient {
83
84
  return Effect.andThen(Deferred.succeed(deferred, message), () => Effect.void);
84
85
  }
85
86
 
86
- pull(opts: {
87
- spaceId: SpaceId;
88
- feedNamespace: string;
89
- limit?: number;
90
- }): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
87
+ pull(
88
+ ctx: Context,
89
+ opts: {
90
+ spaceId: SpaceId;
91
+ feedNamespace: string;
92
+ limit?: number;
93
+ },
94
+ ): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
91
95
  const self = this;
92
96
  return Effect.gen(function* () {
93
97
  const lastPulledPosition = yield* self.#feedStore.getSyncState({
@@ -105,7 +109,7 @@ export class SyncClient {
105
109
  position: lastPulledPosition,
106
110
  limit: opts.limit,
107
111
  };
108
- yield* self.#sendMessage(self.#withPeerIds(request));
112
+ yield* self.#sendMessage(ctx, self.#withPeerIds(request));
109
113
  const message = yield* Deferred.await(deferred);
110
114
  const response = yield* self.#expectResponse<QueryResponse>(requestId, message, 'QueryResponse');
111
115
  if (response.blocks.length === 0) {
@@ -132,11 +136,14 @@ export class SyncClient {
132
136
  });
133
137
  }
134
138
 
135
- push(opts: {
136
- spaceId: SpaceId;
137
- feedNamespace: string;
138
- limit?: number;
139
- }): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
139
+ push(
140
+ ctx: Context,
141
+ opts: {
142
+ spaceId: SpaceId;
143
+ feedNamespace: string;
144
+ limit?: number;
145
+ },
146
+ ): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
140
147
  const self = this;
141
148
  return Effect.gen(function* () {
142
149
  const unpositioned = yield* self.#feedStore.query({
@@ -158,7 +165,7 @@ export class SyncClient {
158
165
  feedNamespace: opts.feedNamespace,
159
166
  blocks: unpositioned.blocks,
160
167
  };
161
- yield* self.#sendMessage(self.#withPeerIds(request));
168
+ yield* self.#sendMessage(ctx, self.#withPeerIds(request));
162
169
  const message = yield* Deferred.await(deferred);
163
170
  const response = yield* self.#expectResponse<AppendResponse>(requestId, message, 'AppendResponse');
164
171
  yield* self.#feedStore.setPosition({
@@ -5,6 +5,7 @@
5
5
  import type * as SqlClient from '@effect/sql/SqlClient';
6
6
  import * as Effect from 'effect/Effect';
7
7
 
8
+ import { Context } from '@dxos/context';
8
9
  import { type FeedProtocol } from '@dxos/protocols';
9
10
  import type { SqlTransaction } from '@dxos/sql-sqlite';
10
11
 
@@ -20,7 +21,7 @@ export type SyncServerOptions = {
20
21
  peerId: string;
21
22
  feedStore: FeedStore;
22
23
  /** Send a protocol message to a client. Receives full message (recipient is set by server). Returns Effect. */
23
- sendMessage: (message: ProtocolMessage) => Effect.Effect<void, unknown, never>;
24
+ sendMessage: (ctx: Context, message: ProtocolMessage) => Effect.Effect<void, unknown, never>;
24
25
  };
25
26
 
26
27
  /**
@@ -30,7 +31,7 @@ export type SyncServerOptions = {
30
31
  export class SyncServer {
31
32
  readonly #peerId: string;
32
33
  readonly #feedStore: FeedStore;
33
- readonly #sendMessage: (message: ProtocolMessage) => Effect.Effect<void, unknown, never>;
34
+ readonly #sendMessage: SyncServerOptions['sendMessage'];
34
35
 
35
36
  constructor(options: SyncServerOptions) {
36
37
  this.#peerId = options.peerId;
@@ -42,6 +43,7 @@ export class SyncServer {
42
43
  * Receive a message from a client. Handles QueryRequest and AppendRequest; sends response via sendMessage with correct peer ids.
43
44
  */
44
45
  handleMessage(
46
+ ctx: Context,
45
47
  message: ProtocolMessage,
46
48
  ): Effect.Effect<void, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
47
49
  const self = this;
@@ -57,10 +59,11 @@ export class SyncServer {
57
59
  const req = message as QueryRequest;
58
60
  return Effect.gen(function* () {
59
61
  const response: QueryResponse = yield* self.#feedStore.query(req);
60
- yield* self.#sendMessage(withPeerIds({ _tag: 'QueryResponse', ...response }));
62
+ yield* self.#sendMessage(ctx, withPeerIds({ _tag: 'QueryResponse', ...response }));
61
63
  }).pipe(
62
64
  Effect.catchAll((err: unknown) =>
63
65
  self.#sendMessage(
66
+ ctx,
64
67
  withPeerIds({
65
68
  _tag: 'Error',
66
69
  message: err instanceof Error ? err.message : String(err),
@@ -73,10 +76,11 @@ export class SyncServer {
73
76
  const req = message as AppendRequest;
74
77
  return Effect.gen(function* () {
75
78
  const response = yield* self.#feedStore.append(req);
76
- yield* self.#sendMessage(withPeerIds({ _tag: 'AppendResponse', ...response }));
79
+ yield* self.#sendMessage(ctx, withPeerIds({ _tag: 'AppendResponse', ...response }));
77
80
  }).pipe(
78
81
  Effect.catchAll((err: unknown) =>
79
82
  self.#sendMessage(
83
+ ctx,
80
84
  withPeerIds({
81
85
  _tag: 'Error',
82
86
  message: err instanceof Error ? err.message : String(err),
@@ -9,7 +9,7 @@ import * as Effect from 'effect/Effect';
9
9
  import * as Layer from 'effect/Layer';
10
10
  import * as ManagedRuntime from 'effect/ManagedRuntime';
11
11
 
12
- import { Resource } from '@dxos/context';
12
+ import { Context, Resource } from '@dxos/context';
13
13
  import { RuntimeProvider } from '@dxos/effect';
14
14
  import { type SpaceId } from '@dxos/keys';
15
15
  import { FeedProtocol } from '@dxos/protocols';
@@ -52,7 +52,7 @@ export class TestBuilder extends Resource {
52
52
  isServer: i === 0,
53
53
  actorId: `peer-${i}`,
54
54
  serverPeerId: i === 0 ? undefined : 'peer-0',
55
- sendMessage: (msg) => this.#routeMessage(msg),
55
+ sendMessage: (ctx, msg) => this.#routeMessage(ctx, msg),
56
56
  logSql,
57
57
  }),
58
58
  );
@@ -83,14 +83,14 @@ export class TestBuilder extends Resource {
83
83
  }
84
84
 
85
85
  /** Route a message to the peer identified by recipientPeerId. Runs the recipient's handleMessage with that peer's runtime. */
86
- #routeMessage(msg: ProtocolMessage): Effect.Effect<void, unknown, never> {
86
+ #routeMessage(ctx: Context, msg: ProtocolMessage): Effect.Effect<void, unknown, never> {
87
87
  const peer = this.#peers.find((p) => p.peerId === msg.recipientPeerId);
88
88
  if (peer == null) {
89
89
  return Effect.die(new Error(`TestPeer not found: ${msg.recipientPeerId}`));
90
90
  }
91
91
  const handleEffect =
92
92
  peer.syncServer != null
93
- ? peer.syncServer.handleMessage(msg)
93
+ ? peer.syncServer.handleMessage(ctx, msg)
94
94
  : peer.syncClient != null
95
95
  ? peer.syncClient.handleMessage(msg)
96
96
  : null;
@@ -129,7 +129,7 @@ export class TestPeer extends Resource {
129
129
  isServer: boolean;
130
130
  actorId: string;
131
131
  serverPeerId?: string;
132
- sendMessage: (msg: ProtocolMessage) => Effect.Effect<void, unknown, never>;
132
+ sendMessage: (ctx: Context, msg: ProtocolMessage) => Effect.Effect<void, unknown, never>;
133
133
  logSql?: boolean;
134
134
  }) {
135
135
  super();
@@ -211,13 +211,13 @@ export class TestPeer extends Resource {
211
211
 
212
212
  pull({ spaceId, feedNamespace, limit = 10 }: { spaceId: SpaceId; feedNamespace: string; limit?: number }) {
213
213
  return this.#client!
214
- .pull({ spaceId, feedNamespace, limit })
214
+ .pull(Context.default(), { spaceId, feedNamespace, limit })
215
215
  .pipe(RuntimeProvider.runPromise(this.#runtime.runtimeEffect));
216
216
  }
217
217
 
218
218
  push({ spaceId, feedNamespace, limit = 10 }: { spaceId: SpaceId; feedNamespace: string; limit?: number }) {
219
219
  return this.#client!
220
- .push({ spaceId, feedNamespace, limit })
220
+ .push(Context.default(), { spaceId, feedNamespace, limit })
221
221
  .pipe(RuntimeProvider.runPromise(this.#runtime.runtimeEffect));
222
222
  }
223
223
  }