@dxos/feed 0.8.4-main.ef1bc66f44 → 0.8.4-main.fcc0d83b33
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/README.md +0 -1
- package/dist/lib/neutral/{chunk-Q5PE2Q5P.mjs → chunk-PENEKCN2.mjs} +110 -103
- package/dist/lib/neutral/chunk-PENEKCN2.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1 -5
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/lib/neutral/testing/index.mjs +8 -7
- package/dist/lib/neutral/testing/index.mjs.map +3 -3
- package/dist/types/src/errors.d.ts +17 -57
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/feed-store.d.ts +39 -4
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/sync-client.d.ts +4 -3
- package/dist/types/src/sync-client.d.ts.map +1 -1
- package/dist/types/src/sync-server.d.ts +3 -2
- package/dist/types/src/sync-server.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +5 -5
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -20
- package/src/errors.ts +4 -17
- package/src/feed-store.test.ts +206 -97
- package/src/feed-store.ts +125 -115
- package/src/sync-client.ts +26 -15
- package/src/sync-server.ts +8 -4
- package/src/testing/test-builder.ts +11 -11
- package/dist/lib/neutral/chunk-Q5PE2Q5P.mjs.map +0 -7
package/src/feed-store.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { type SpaceId } from '@dxos/keys';
|
|
|
12
12
|
import { FeedProtocol } from '@dxos/protocols';
|
|
13
13
|
import { SqlTransaction } from '@dxos/sql-sqlite';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import { PositionConflictError } from './errors';
|
|
16
16
|
|
|
17
17
|
type AppendRequest = FeedProtocol.AppendRequest;
|
|
18
18
|
type AppendResponse = FeedProtocol.AppendResponse;
|
|
@@ -124,7 +124,9 @@ export class FeedStore {
|
|
|
124
124
|
const rows = yield* sql<{ feedPrivateId: number }>`
|
|
125
125
|
SELECT feedPrivateId FROM feeds WHERE spaceId = ${spaceId} AND feedId = ${feedId}
|
|
126
126
|
`;
|
|
127
|
-
if (rows.length > 0)
|
|
127
|
+
if (rows.length > 0) {
|
|
128
|
+
return rows[0].feedPrivateId;
|
|
129
|
+
}
|
|
128
130
|
|
|
129
131
|
const newRows = yield* sql<{ feedPrivateId: number }>`
|
|
130
132
|
INSERT INTO feeds (spaceId, feedId, feedNamespace) VALUES (${spaceId}, ${feedId}, ${namespace}) RETURNING feedPrivateId
|
|
@@ -141,7 +143,9 @@ export class FeedStore {
|
|
|
141
143
|
Effect.gen(this, function* () {
|
|
142
144
|
const sql = yield* SqlClient.SqlClient;
|
|
143
145
|
const rows = yield* sql<{ token: string }>`SELECT token FROM cursor_tokens WHERE spaceId = ${spaceId}`;
|
|
144
|
-
if (rows.length > 0)
|
|
146
|
+
if (rows.length > 0) {
|
|
147
|
+
return rows[0].token;
|
|
148
|
+
}
|
|
145
149
|
|
|
146
150
|
const token = crypto.randomUUID().replace(/-/g, '').slice(0, 6);
|
|
147
151
|
yield* sql`INSERT INTO cursor_tokens (spaceId, token) VALUES (${spaceId}, ${token})`;
|
|
@@ -192,7 +196,7 @@ export class FeedStore {
|
|
|
192
196
|
// If spaceId is not provided in request (e.g. feedIds query), we can't easily validate token unless we look up spaceId for feedIds.
|
|
193
197
|
// Ideally spaceId should be required for token validation.
|
|
194
198
|
|
|
195
|
-
/*
|
|
199
|
+
/*
|
|
196
200
|
Logic:
|
|
197
201
|
1. If `cursor` is present, it's `token|insertionId`.
|
|
198
202
|
2. If `position` is present, it's `position` (legacy/manual).
|
|
@@ -355,115 +359,72 @@ export class FeedStore {
|
|
|
355
359
|
}).pipe(Effect.withSpan('FeedStore.setSyncState'));
|
|
356
360
|
|
|
357
361
|
/**
|
|
358
|
-
*
|
|
362
|
+
* Returns the number of stored blocks for a single feed in a space/namespace.
|
|
363
|
+
* Intended as a low-level primitive for callers (for example Cloudflare Worker code)
|
|
364
|
+
* that need to make retention decisions under constrained storage resources.
|
|
359
365
|
*/
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (!this.#options.assignPositions && block.position != null) {
|
|
378
|
-
const positionKey = `${feedPrivateId}|${block.position}`;
|
|
379
|
-
if (positionFeedIdByKey.has(positionKey)) {
|
|
380
|
-
yield* Effect.fail(
|
|
381
|
-
new DuplicateBlockPositionError({
|
|
382
|
-
feedId: block.feedId!,
|
|
383
|
-
position: block.position,
|
|
384
|
-
}),
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
positionFeedIdByKey.set(positionKey, block.feedId!);
|
|
388
|
-
}
|
|
366
|
+
countBlocks = (opts: {
|
|
367
|
+
spaceId: SpaceId;
|
|
368
|
+
feedNamespace: string;
|
|
369
|
+
feedId: string;
|
|
370
|
+
}): Effect.Effect<number, SqlError.SqlError, SqlClient.SqlClient> =>
|
|
371
|
+
Effect.gen(this, function* () {
|
|
372
|
+
const sql = yield* SqlClient.SqlClient;
|
|
373
|
+
const rows = yield* sql<{ count: number }>`
|
|
374
|
+
SELECT COUNT(*) AS count
|
|
375
|
+
FROM blocks
|
|
376
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
377
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
378
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
379
|
+
AND feeds.feedId = ${opts.feedId}
|
|
380
|
+
`;
|
|
381
|
+
return rows[0]?.count ?? 0;
|
|
382
|
+
}).pipe(Effect.withSpan('FeedStore.countBlocks'));
|
|
389
383
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Deletes the oldest blocks for a single feed in a space/namespace.
|
|
386
|
+
* This API intentionally does not enforce any retention policy (such as max size);
|
|
387
|
+
* callers decide when and how much to prune, which is useful for constrained
|
|
388
|
+
* environments like Cloudflare Workers.
|
|
389
|
+
*
|
|
390
|
+
* @returns Number of deleted rows.
|
|
391
|
+
*/
|
|
392
|
+
deleteOldestBlocks = (opts: {
|
|
393
|
+
spaceId: SpaceId;
|
|
394
|
+
feedNamespace: string;
|
|
395
|
+
feedId: string;
|
|
396
|
+
count: number;
|
|
397
|
+
}): Effect.Effect<number, SqlError.SqlError, SqlClient.SqlClient> =>
|
|
398
|
+
Effect.gen(this, function* () {
|
|
399
|
+
const sql = yield* SqlClient.SqlClient;
|
|
400
|
+
if (opts.count <= 0) {
|
|
401
|
+
return 0;
|
|
402
|
+
}
|
|
402
403
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const key = `${lamportConflict.feedPrivateId}|${lamportConflict.actorId}|${lamportConflict.sequence}`;
|
|
418
|
-
yield* Effect.fail(
|
|
419
|
-
new DuplicateBlockLamportTimestampError({
|
|
420
|
-
feedId: lamportFeedIdByKey.get(key)!,
|
|
421
|
-
actorId: lamportConflict.actorId,
|
|
422
|
-
sequence: lamportConflict.sequence,
|
|
423
|
-
}),
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
404
|
+
const deletedRows = yield* sql<{ insertionId: number }>`
|
|
405
|
+
DELETE FROM blocks
|
|
406
|
+
WHERE insertionId IN (
|
|
407
|
+
SELECT blocks.insertionId
|
|
408
|
+
FROM blocks
|
|
409
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
410
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
411
|
+
AND feeds.feedNamespace = ${opts.feedNamespace}
|
|
412
|
+
AND feeds.feedId = ${opts.feedId}
|
|
413
|
+
ORDER BY blocks.insertionId ASC
|
|
414
|
+
LIMIT ${opts.count}
|
|
415
|
+
)
|
|
416
|
+
RETURNING insertionId
|
|
417
|
+
`;
|
|
427
418
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const positionConditions = positionedBlocks.map((block) => {
|
|
431
|
-
const feedPrivateId = feedPrivateIds.get(block.feedId!)!;
|
|
432
|
-
return sql`(feedPrivateId = ${feedPrivateId} AND position = ${block.position})`;
|
|
433
|
-
});
|
|
434
|
-
if (positionConditions.length > 0) {
|
|
435
|
-
const existingPositions = yield* sql<{ feedPrivateId: number; position: number }>`
|
|
436
|
-
SELECT feedPrivateId, position
|
|
437
|
-
FROM blocks
|
|
438
|
-
WHERE position IS NOT NULL
|
|
439
|
-
AND (${sql.or(positionConditions)})
|
|
440
|
-
LIMIT 1
|
|
441
|
-
`;
|
|
442
|
-
const positionConflict = existingPositions[0];
|
|
443
|
-
if (positionConflict) {
|
|
444
|
-
const key = `${positionConflict.feedPrivateId}|${positionConflict.position}`;
|
|
445
|
-
yield* Effect.fail(
|
|
446
|
-
new DuplicateBlockPositionError({
|
|
447
|
-
feedId: positionFeedIdByKey.get(key)!,
|
|
448
|
-
position: positionConflict.position,
|
|
449
|
-
}),
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}),
|
|
455
|
-
);
|
|
419
|
+
return deletedRows.length;
|
|
420
|
+
}).pipe(Effect.withSpan('FeedStore.deleteOldestBlocks'));
|
|
456
421
|
|
|
457
422
|
/**
|
|
458
423
|
* Appends blocks for a space/namespace and optionally assigns global positions.
|
|
459
424
|
*/
|
|
460
425
|
append = (
|
|
461
426
|
request: AppendRequest,
|
|
462
|
-
): Effect.Effect<
|
|
463
|
-
AppendResponse,
|
|
464
|
-
SqlError.SqlError | DuplicateBlockPositionError | DuplicateBlockLamportTimestampError,
|
|
465
|
-
SqlClient.SqlClient | SqlTransaction.SqlTransaction
|
|
466
|
-
> =>
|
|
427
|
+
): Effect.Effect<AppendResponse, SqlError.SqlError, SqlClient.SqlClient | SqlTransaction.SqlTransaction> =>
|
|
467
428
|
Effect.gen(this, function* () {
|
|
468
429
|
if (!request.spaceId) {
|
|
469
430
|
return yield* Effect.die(new Error('spaceId required for append'));
|
|
@@ -506,8 +467,6 @@ export class FeedStore {
|
|
|
506
467
|
{ concurrency: 'unbounded' },
|
|
507
468
|
);
|
|
508
469
|
|
|
509
|
-
yield* this.#validateAppendBlocks(request.blocks, feedPrivateIds);
|
|
510
|
-
|
|
511
470
|
// 2. Get max position per namespace ONCE (not per block).
|
|
512
471
|
const maxPositions = new Map<string, number>();
|
|
513
472
|
if (this.#options.assignPositions) {
|
|
@@ -562,11 +521,7 @@ export class FeedStore {
|
|
|
562
521
|
appendLocal = Effect.fn('Feed.appendLocal')(
|
|
563
522
|
(
|
|
564
523
|
messages: { spaceId: string; feedId: string; feedNamespace: string; data: Uint8Array }[],
|
|
565
|
-
): Effect.Effect<
|
|
566
|
-
Block[],
|
|
567
|
-
SqlError.SqlError | DuplicateBlockPositionError | DuplicateBlockLamportTimestampError,
|
|
568
|
-
SqlClient.SqlClient | SqlTransaction.SqlTransaction
|
|
569
|
-
> =>
|
|
524
|
+
): Effect.Effect<Block[], SqlError.SqlError, SqlClient.SqlClient | SqlTransaction.SqlTransaction> =>
|
|
570
525
|
Effect.gen(this, function* () {
|
|
571
526
|
const sql = yield* SqlClient.SqlClient;
|
|
572
527
|
|
|
@@ -661,17 +616,24 @@ export class FeedStore {
|
|
|
661
616
|
blocksBySpaceNamespace.get(spaceNamespaceKey)!.blocks.push(block);
|
|
662
617
|
}
|
|
663
618
|
|
|
664
|
-
// 4. Call append once per (spaceId, namespace) batch.
|
|
665
|
-
|
|
666
|
-
|
|
619
|
+
// 4. Call append once per (spaceId, namespace) batch and assign returned positions.
|
|
620
|
+
const positionByBlock = new Map<Block, number | null>();
|
|
621
|
+
for (const { spaceId, feedNamespace, blocks: batchBlocks } of blocksBySpaceNamespace.values()) {
|
|
622
|
+
const { positions } = yield* this.append({
|
|
667
623
|
requestId: 'local-append',
|
|
668
|
-
blocks,
|
|
624
|
+
blocks: batchBlocks,
|
|
669
625
|
spaceId,
|
|
670
626
|
feedNamespace,
|
|
671
627
|
});
|
|
628
|
+
for (let i = 0; i < batchBlocks.length; i++) {
|
|
629
|
+
positionByBlock.set(batchBlocks[i], positions[i] ?? null);
|
|
630
|
+
}
|
|
672
631
|
}
|
|
673
632
|
|
|
674
|
-
return blocks
|
|
633
|
+
return blocks.map((block) => ({
|
|
634
|
+
...block,
|
|
635
|
+
position: positionByBlock.get(block) ?? block.position,
|
|
636
|
+
}));
|
|
675
637
|
}).pipe(Effect.withSpan('FeedStore.appendLocal')),
|
|
676
638
|
);
|
|
677
639
|
|
|
@@ -715,6 +677,54 @@ export class FeedStore {
|
|
|
715
677
|
`;
|
|
716
678
|
}
|
|
717
679
|
}).pipe(Effect.withSpan('FeedStore.setPosition'));
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Gets all feeds and their blocks for a space, organized by feed ID and namespace.
|
|
683
|
+
* Used for space archive export.
|
|
684
|
+
*/
|
|
685
|
+
getAllFeedsForSpace = (opts: {
|
|
686
|
+
spaceId: SpaceId;
|
|
687
|
+
}): Effect.Effect<
|
|
688
|
+
Array<{
|
|
689
|
+
feedId: string;
|
|
690
|
+
feedNamespace: string;
|
|
691
|
+
blocks: Block[];
|
|
692
|
+
}>,
|
|
693
|
+
SqlError.SqlError,
|
|
694
|
+
SqlClient.SqlClient
|
|
695
|
+
> =>
|
|
696
|
+
Effect.gen(this, function* () {
|
|
697
|
+
const sql = yield* SqlClient.SqlClient;
|
|
698
|
+
|
|
699
|
+
const feeds = yield* sql<{ feedId: string; feedNamespace: string }>`
|
|
700
|
+
SELECT DISTINCT feedId, feedNamespace FROM feeds WHERE spaceId = ${opts.spaceId}
|
|
701
|
+
`;
|
|
702
|
+
|
|
703
|
+
const result: Array<{ feedId: string; feedNamespace: string; blocks: Block[] }> = [];
|
|
704
|
+
|
|
705
|
+
for (const feed of feeds) {
|
|
706
|
+
const blocks = yield* sql<Block>`
|
|
707
|
+
SELECT blocks.*, feeds.feedId, feeds.feedNamespace
|
|
708
|
+
FROM blocks
|
|
709
|
+
JOIN feeds ON blocks.feedPrivateId = feeds.feedPrivateId
|
|
710
|
+
WHERE feeds.spaceId = ${opts.spaceId}
|
|
711
|
+
AND feeds.feedId = ${feed.feedId}
|
|
712
|
+
AND feeds.feedNamespace = ${feed.feedNamespace}
|
|
713
|
+
ORDER BY blocks.sequence ASC
|
|
714
|
+
`;
|
|
715
|
+
|
|
716
|
+
result.push({
|
|
717
|
+
feedId: feed.feedId,
|
|
718
|
+
feedNamespace: feed.feedNamespace,
|
|
719
|
+
blocks: blocks.map((row) => ({
|
|
720
|
+
...row,
|
|
721
|
+
data: new Uint8Array(row.data),
|
|
722
|
+
})),
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
return result;
|
|
727
|
+
}).pipe(Effect.withSpan('FeedStore.getAllFeedsForSpace'));
|
|
718
728
|
}
|
|
719
729
|
|
|
720
730
|
const encodeCursor = (token: string, insertionId: number) => FeedCursor.make(`${token}|${insertionId}`);
|
package/src/sync-client.ts
CHANGED
|
@@ -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
|
/**
|
|
@@ -73,9 +74,13 @@ export class SyncClient {
|
|
|
73
74
|
*/
|
|
74
75
|
handleMessage(message: ProtocolMessage): Effect.Effect<void, never, never> {
|
|
75
76
|
const requestId = 'requestId' in message && message.requestId != null ? String(message.requestId) : undefined;
|
|
76
|
-
if (requestId == null)
|
|
77
|
+
if (requestId == null) {
|
|
78
|
+
return Effect.void;
|
|
79
|
+
}
|
|
77
80
|
const deferred = this.#handlers.get(requestId);
|
|
78
|
-
if (deferred == null)
|
|
81
|
+
if (deferred == null) {
|
|
82
|
+
return Effect.void;
|
|
83
|
+
}
|
|
79
84
|
this.#handlers.delete(requestId);
|
|
80
85
|
if (message._tag === 'Error') {
|
|
81
86
|
return Effect.andThen(Deferred.fail(deferred, new Error(message.message)), () => Effect.void);
|
|
@@ -83,11 +88,14 @@ export class SyncClient {
|
|
|
83
88
|
return Effect.andThen(Deferred.succeed(deferred, message), () => Effect.void);
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
pull(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
pull(
|
|
92
|
+
ctx: Context,
|
|
93
|
+
opts: {
|
|
94
|
+
spaceId: SpaceId;
|
|
95
|
+
feedNamespace: string;
|
|
96
|
+
limit?: number;
|
|
97
|
+
},
|
|
98
|
+
): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
|
|
91
99
|
const self = this;
|
|
92
100
|
return Effect.gen(function* () {
|
|
93
101
|
const lastPulledPosition = yield* self.#feedStore.getSyncState({
|
|
@@ -105,7 +113,7 @@ export class SyncClient {
|
|
|
105
113
|
position: lastPulledPosition,
|
|
106
114
|
limit: opts.limit,
|
|
107
115
|
};
|
|
108
|
-
yield* self.#sendMessage(self.#withPeerIds(request));
|
|
116
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
109
117
|
const message = yield* Deferred.await(deferred);
|
|
110
118
|
const response = yield* self.#expectResponse<QueryResponse>(requestId, message, 'QueryResponse');
|
|
111
119
|
if (response.blocks.length === 0) {
|
|
@@ -132,11 +140,14 @@ export class SyncClient {
|
|
|
132
140
|
});
|
|
133
141
|
}
|
|
134
142
|
|
|
135
|
-
push(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
143
|
+
push(
|
|
144
|
+
ctx: Context,
|
|
145
|
+
opts: {
|
|
146
|
+
spaceId: SpaceId;
|
|
147
|
+
feedNamespace: string;
|
|
148
|
+
limit?: number;
|
|
149
|
+
},
|
|
150
|
+
): Effect.Effect<{ done: boolean }, unknown, SqlClient.SqlClient | SqlTransaction.SqlTransaction> {
|
|
140
151
|
const self = this;
|
|
141
152
|
return Effect.gen(function* () {
|
|
142
153
|
const unpositioned = yield* self.#feedStore.query({
|
|
@@ -158,7 +169,7 @@ export class SyncClient {
|
|
|
158
169
|
feedNamespace: opts.feedNamespace,
|
|
159
170
|
blocks: unpositioned.blocks,
|
|
160
171
|
};
|
|
161
|
-
yield* self.#sendMessage(self.#withPeerIds(request));
|
|
172
|
+
yield* self.#sendMessage(ctx, self.#withPeerIds(request));
|
|
162
173
|
const message = yield* Deferred.await(deferred);
|
|
163
174
|
const response = yield* self.#expectResponse<AppendResponse>(requestId, message, 'AppendResponse');
|
|
164
175
|
yield* self.#feedStore.setPosition({
|
package/src/sync-server.ts
CHANGED
|
@@ -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:
|
|
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();
|
|
@@ -210,14 +210,14 @@ export class TestPeer extends Resource {
|
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
pull({ spaceId, feedNamespace, limit = 10 }: { spaceId: SpaceId; feedNamespace: string; limit?: number }) {
|
|
213
|
-
return this.#client
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
return this.#client!
|
|
214
|
+
.pull(Context.default(), { spaceId, feedNamespace, limit })
|
|
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
|
-
return this.#client
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
return this.#client!
|
|
220
|
+
.push(Context.default(), { spaceId, feedNamespace, limit })
|
|
221
|
+
.pipe(RuntimeProvider.runPromise(this.#runtime.runtimeEffect));
|
|
222
222
|
}
|
|
223
223
|
}
|