@dxos/client-services 0.8.4-main.ae835ea → 0.8.4-main.bc2380dfbc

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.
Files changed (230) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +1 -1
  3. package/dist/lib/browser/chunk-QCWEHHJW.mjs +24 -0
  4. package/dist/lib/browser/chunk-QCWEHHJW.mjs.map +7 -0
  5. package/dist/lib/browser/{chunk-KPYVJG6G.mjs → chunk-TUCJORVO.mjs} +2153 -3654
  6. package/dist/lib/browser/chunk-TUCJORVO.mjs.map +7 -0
  7. package/dist/lib/browser/chunk-XJRPB3GA.mjs +22 -0
  8. package/dist/lib/browser/chunk-XJRPB3GA.mjs.map +7 -0
  9. package/dist/lib/browser/index.mjs +424 -137
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/packlets/diagnostics/browser-diagnostics-broadcast.mjs +88 -0
  13. package/dist/lib/browser/packlets/diagnostics/browser-diagnostics-broadcast.mjs.map +7 -0
  14. package/dist/lib/browser/packlets/diagnostics/diagnostics-broadcast.mjs +11 -0
  15. package/dist/lib/browser/packlets/diagnostics/diagnostics-broadcast.mjs.map +7 -0
  16. package/dist/lib/browser/packlets/locks/browser.mjs +86 -0
  17. package/dist/lib/browser/packlets/locks/browser.mjs.map +7 -0
  18. package/dist/lib/browser/packlets/locks/node.mjs +48 -0
  19. package/dist/lib/browser/packlets/locks/node.mjs.map +7 -0
  20. package/dist/lib/browser/testing/index.mjs +28 -29
  21. package/dist/lib/browser/testing/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/chunk-2DT3MZRL.mjs +22 -0
  23. package/dist/lib/node-esm/chunk-2DT3MZRL.mjs.map +7 -0
  24. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs +24 -0
  25. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs.map +7 -0
  26. package/dist/lib/node-esm/{chunk-BBBSS6UL.mjs → chunk-IQLAKNSR.mjs} +2099 -3469
  27. package/dist/lib/node-esm/chunk-IQLAKNSR.mjs.map +7 -0
  28. package/dist/lib/node-esm/index.mjs +424 -137
  29. package/dist/lib/node-esm/index.mjs.map +4 -4
  30. package/dist/lib/node-esm/meta.json +1 -1
  31. package/dist/lib/node-esm/packlets/diagnostics/browser-diagnostics-broadcast.mjs +88 -0
  32. package/dist/lib/node-esm/packlets/diagnostics/browser-diagnostics-broadcast.mjs.map +7 -0
  33. package/dist/lib/node-esm/packlets/diagnostics/diagnostics-broadcast.mjs +11 -0
  34. package/dist/lib/node-esm/packlets/diagnostics/diagnostics-broadcast.mjs.map +7 -0
  35. package/dist/lib/node-esm/packlets/locks/browser.mjs +86 -0
  36. package/dist/lib/node-esm/packlets/locks/browser.mjs.map +7 -0
  37. package/dist/lib/node-esm/packlets/locks/node.mjs +48 -0
  38. package/dist/lib/node-esm/packlets/locks/node.mjs.map +7 -0
  39. package/dist/lib/node-esm/testing/index.mjs +28 -29
  40. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  41. package/dist/types/src/index.d.ts +1 -0
  42. package/dist/types/src/index.d.ts.map +1 -1
  43. package/dist/types/src/packlets/agents/edge-agent-manager.d.ts +3 -2
  44. package/dist/types/src/packlets/agents/edge-agent-manager.d.ts.map +1 -1
  45. package/dist/types/src/packlets/agents/edge-agent-service.d.ts +2 -1
  46. package/dist/types/src/packlets/agents/edge-agent-service.d.ts.map +1 -1
  47. package/dist/types/src/packlets/devices/devices-service.d.ts.map +1 -1
  48. package/dist/types/src/packlets/devtools/devtools.d.ts +2 -2
  49. package/dist/types/src/packlets/devtools/devtools.d.ts.map +1 -1
  50. package/dist/types/src/packlets/devtools/feeds.d.ts.map +1 -1
  51. package/dist/types/src/packlets/devtools/keys.d.ts.map +1 -1
  52. package/dist/types/src/packlets/devtools/metadata.d.ts.map +1 -1
  53. package/dist/types/src/packlets/devtools/network.d.ts.map +1 -1
  54. package/dist/types/src/packlets/devtools/spaces.d.ts.map +1 -1
  55. package/dist/types/src/packlets/diagnostics/browser-diagnostics-broadcast.d.ts.map +1 -1
  56. package/dist/types/src/packlets/diagnostics/diagnostics-broadcast.d.ts.map +1 -1
  57. package/dist/types/src/packlets/diagnostics/diagnostics-collector.d.ts.map +1 -1
  58. package/dist/types/src/packlets/diagnostics/diagnostics.d.ts +2 -3
  59. package/dist/types/src/packlets/diagnostics/diagnostics.d.ts.map +1 -1
  60. package/dist/types/src/packlets/diagnostics/index.d.ts +1 -1
  61. package/dist/types/src/packlets/diagnostics/index.d.ts.map +1 -1
  62. package/dist/types/src/packlets/identity/authenticator.d.ts +2 -2
  63. package/dist/types/src/packlets/identity/authenticator.d.ts.map +1 -1
  64. package/dist/types/src/packlets/identity/contacts-service.d.ts.map +1 -1
  65. package/dist/types/src/packlets/identity/identity-manager.d.ts +6 -6
  66. package/dist/types/src/packlets/identity/identity-manager.d.ts.map +1 -1
  67. package/dist/types/src/packlets/identity/identity-recovery-manager.d.ts +8 -7
  68. package/dist/types/src/packlets/identity/identity-recovery-manager.d.ts.map +1 -1
  69. package/dist/types/src/packlets/identity/identity-service.d.ts +6 -10
  70. package/dist/types/src/packlets/identity/identity-service.d.ts.map +1 -1
  71. package/dist/types/src/packlets/identity/identity.d.ts +8 -11
  72. package/dist/types/src/packlets/identity/identity.d.ts.map +1 -1
  73. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts +6 -5
  74. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts.map +1 -1
  75. package/dist/types/src/packlets/invitations/edge-invitation-handler.d.ts +1 -1
  76. package/dist/types/src/packlets/invitations/edge-invitation-handler.d.ts.map +1 -1
  77. package/dist/types/src/packlets/invitations/invitation-guest-extenstion.d.ts.map +1 -1
  78. package/dist/types/src/packlets/invitations/invitation-host-extension.d.ts.map +1 -1
  79. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts +7 -4
  80. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts.map +1 -1
  81. package/dist/types/src/packlets/invitations/invitation-state.d.ts.map +1 -1
  82. package/dist/types/src/packlets/invitations/invitation-topology.d.ts.map +1 -1
  83. package/dist/types/src/packlets/invitations/invitations-handler.d.ts +4 -4
  84. package/dist/types/src/packlets/invitations/invitations-handler.d.ts.map +1 -1
  85. package/dist/types/src/packlets/invitations/invitations-manager.d.ts +3 -3
  86. package/dist/types/src/packlets/invitations/invitations-manager.d.ts.map +1 -1
  87. package/dist/types/src/packlets/invitations/invitations-service.d.ts +3 -3
  88. package/dist/types/src/packlets/invitations/invitations-service.d.ts.map +1 -1
  89. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts +4 -3
  90. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts.map +1 -1
  91. package/dist/types/src/packlets/invitations/utils.d.ts.map +1 -1
  92. package/dist/types/src/packlets/locks/browser.d.ts.map +1 -1
  93. package/dist/types/src/packlets/locks/index.d.ts +1 -1
  94. package/dist/types/src/packlets/locks/index.d.ts.map +1 -1
  95. package/dist/types/src/packlets/locks/node.d.ts.map +1 -1
  96. package/dist/types/src/packlets/logging/logging-service.d.ts +4 -0
  97. package/dist/types/src/packlets/logging/logging-service.d.ts.map +1 -1
  98. package/dist/types/src/packlets/network/network-service.d.ts +5 -4
  99. package/dist/types/src/packlets/network/network-service.d.ts.map +1 -1
  100. package/dist/types/src/packlets/services/client-rpc-server.d.ts +5 -5
  101. package/dist/types/src/packlets/services/client-rpc-server.d.ts.map +1 -1
  102. package/dist/types/src/packlets/services/feed-syncer.d.ts +59 -0
  103. package/dist/types/src/packlets/services/feed-syncer.d.ts.map +1 -0
  104. package/dist/types/src/packlets/services/feed-syncer.test.d.ts +2 -0
  105. package/dist/types/src/packlets/services/feed-syncer.test.d.ts.map +1 -0
  106. package/dist/types/src/packlets/services/platform.d.ts.map +1 -1
  107. package/dist/types/src/packlets/services/service-context.d.ts +13 -9
  108. package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
  109. package/dist/types/src/packlets/services/service-host.d.ts +20 -7
  110. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  111. package/dist/types/src/packlets/services/service-registry.d.ts.map +1 -1
  112. package/dist/types/src/packlets/services/util.d.ts.map +1 -1
  113. package/dist/types/src/packlets/space-export/archive-format.d.ts +9 -0
  114. package/dist/types/src/packlets/space-export/archive-format.d.ts.map +1 -0
  115. package/dist/types/src/packlets/space-export/index.d.ts +4 -1
  116. package/dist/types/src/packlets/space-export/index.d.ts.map +1 -1
  117. package/dist/types/src/packlets/space-export/serialized-space-reader.d.ts +23 -0
  118. package/dist/types/src/packlets/space-export/serialized-space-reader.d.ts.map +1 -0
  119. package/dist/types/src/packlets/space-export/serialized-space-writer.d.ts +36 -0
  120. package/dist/types/src/packlets/space-export/serialized-space-writer.d.ts.map +1 -0
  121. package/dist/types/src/packlets/space-export/space-archive-reader.d.ts +9 -1
  122. package/dist/types/src/packlets/space-export/space-archive-reader.d.ts.map +1 -1
  123. package/dist/types/src/packlets/space-export/space-archive-writer.d.ts +7 -1
  124. package/dist/types/src/packlets/space-export/space-archive-writer.d.ts.map +1 -1
  125. package/dist/types/src/packlets/space-export/space-archive.test.d.ts +2 -0
  126. package/dist/types/src/packlets/space-export/space-archive.test.d.ts.map +1 -0
  127. package/dist/types/src/packlets/spaces/automerge-space-state.d.ts.map +1 -1
  128. package/dist/types/src/packlets/spaces/data-space-manager.d.ts +30 -19
  129. package/dist/types/src/packlets/spaces/data-space-manager.d.ts.map +1 -1
  130. package/dist/types/src/packlets/spaces/data-space.d.ts +26 -9
  131. package/dist/types/src/packlets/spaces/data-space.d.ts.map +1 -1
  132. package/dist/types/src/packlets/spaces/edge-feed-replicator.d.ts +2 -2
  133. package/dist/types/src/packlets/spaces/edge-feed-replicator.d.ts.map +1 -1
  134. package/dist/types/src/packlets/spaces/epoch-migrations.d.ts.map +1 -1
  135. package/dist/types/src/packlets/spaces/genesis.d.ts +2 -1
  136. package/dist/types/src/packlets/spaces/genesis.d.ts.map +1 -1
  137. package/dist/types/src/packlets/spaces/notarization-plugin.d.ts +6 -9
  138. package/dist/types/src/packlets/spaces/notarization-plugin.d.ts.map +1 -1
  139. package/dist/types/src/packlets/spaces/spaces-service.d.ts +10 -7
  140. package/dist/types/src/packlets/spaces/spaces-service.d.ts.map +1 -1
  141. package/dist/types/src/packlets/storage/level.d.ts.map +1 -1
  142. package/dist/types/src/packlets/storage/profile-archive.d.ts.map +1 -1
  143. package/dist/types/src/packlets/storage/storage.d.ts.map +1 -1
  144. package/dist/types/src/packlets/storage/util.d.ts.map +1 -1
  145. package/dist/types/src/packlets/system/system-service.d.ts +1 -1
  146. package/dist/types/src/packlets/system/system-service.d.ts.map +1 -1
  147. package/dist/types/src/packlets/testing/credential-utils.d.ts.map +1 -1
  148. package/dist/types/src/packlets/testing/invitation-utils.d.ts +6 -3
  149. package/dist/types/src/packlets/testing/invitation-utils.d.ts.map +1 -1
  150. package/dist/types/src/packlets/testing/test-builder.d.ts +6 -5
  151. package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
  152. package/dist/types/src/packlets/worker/worker-runtime.d.ts +41 -4
  153. package/dist/types/src/packlets/worker/worker-runtime.d.ts.map +1 -1
  154. package/dist/types/src/packlets/worker/worker-session.d.ts +2 -4
  155. package/dist/types/src/packlets/worker/worker-session.d.ts.map +1 -1
  156. package/dist/types/src/testing/setup.d.ts.map +1 -1
  157. package/dist/types/src/version.d.ts +1 -1
  158. package/dist/types/src/version.d.ts.map +1 -1
  159. package/dist/types/tsconfig.tsbuildinfo +1 -1
  160. package/package.json +71 -56
  161. package/src/index.ts +1 -0
  162. package/src/packlets/agents/edge-agent-manager.ts +8 -5
  163. package/src/packlets/agents/edge-agent-service.ts +4 -2
  164. package/src/packlets/devices/devices-service.test.ts +0 -1
  165. package/src/packlets/devtools/devtools.ts +2 -3
  166. package/src/packlets/diagnostics/diagnostics.ts +1 -2
  167. package/src/packlets/diagnostics/index.ts +1 -1
  168. package/src/packlets/identity/authenticator.ts +2 -2
  169. package/src/packlets/identity/contacts-service.ts +0 -1
  170. package/src/packlets/identity/identity-manager.test.ts +5 -5
  171. package/src/packlets/identity/identity-manager.ts +23 -22
  172. package/src/packlets/identity/identity-recovery-manager.ts +22 -18
  173. package/src/packlets/identity/identity-service.test.ts +6 -27
  174. package/src/packlets/identity/identity-service.ts +13 -81
  175. package/src/packlets/identity/identity.test.ts +2 -2
  176. package/src/packlets/identity/identity.ts +11 -34
  177. package/src/packlets/invitations/device-invitation-protocol.ts +8 -7
  178. package/src/packlets/invitations/edge-invitation-handler.ts +9 -5
  179. package/src/packlets/invitations/invitation-guest-extenstion.ts +6 -4
  180. package/src/packlets/invitations/invitation-host-extension.ts +13 -14
  181. package/src/packlets/invitations/invitation-protocol.ts +7 -4
  182. package/src/packlets/invitations/invitation-state.ts +1 -15
  183. package/src/packlets/invitations/invitations-handler.test.ts +4 -5
  184. package/src/packlets/invitations/invitations-handler.ts +74 -22
  185. package/src/packlets/invitations/invitations-manager.ts +40 -15
  186. package/src/packlets/invitations/invitations-service.ts +9 -9
  187. package/src/packlets/invitations/space-invitation-protocol.test.ts +17 -16
  188. package/src/packlets/invitations/space-invitation-protocol.ts +11 -16
  189. package/src/packlets/locks/index.ts +1 -1
  190. package/src/packlets/logging/logging-service.ts +20 -16
  191. package/src/packlets/network/network-service.test.ts +0 -1
  192. package/src/packlets/network/network-service.ts +10 -8
  193. package/src/packlets/services/client-rpc-server.ts +19 -16
  194. package/src/packlets/services/feed-syncer.test.ts +340 -0
  195. package/src/packlets/services/feed-syncer.ts +377 -0
  196. package/src/packlets/services/platform.ts +7 -1
  197. package/src/packlets/services/service-context.test.ts +3 -2
  198. package/src/packlets/services/service-context.ts +153 -61
  199. package/src/packlets/services/service-host.test.ts +8 -8
  200. package/src/packlets/services/service-host.ts +70 -40
  201. package/src/packlets/services/service-registry.test.ts +0 -1
  202. package/src/packlets/space-export/archive-format.ts +42 -0
  203. package/src/packlets/space-export/index.ts +4 -1
  204. package/src/packlets/space-export/serialized-space-reader.ts +111 -0
  205. package/src/packlets/space-export/serialized-space-writer.ts +252 -0
  206. package/src/packlets/space-export/space-archive-reader.ts +64 -3
  207. package/src/packlets/space-export/space-archive-writer.ts +41 -3
  208. package/src/packlets/space-export/space-archive.test.ts +461 -0
  209. package/src/packlets/spaces/data-space-manager.test.ts +79 -13
  210. package/src/packlets/spaces/data-space-manager.ts +108 -120
  211. package/src/packlets/spaces/data-space.ts +64 -36
  212. package/src/packlets/spaces/edge-feed-replicator.test.ts +1 -1
  213. package/src/packlets/spaces/edge-feed-replicator.ts +11 -9
  214. package/src/packlets/spaces/epoch-migrations.ts +6 -5
  215. package/src/packlets/spaces/genesis.ts +6 -1
  216. package/src/packlets/spaces/notarization-plugin.test.ts +2 -2
  217. package/src/packlets/spaces/notarization-plugin.ts +10 -9
  218. package/src/packlets/spaces/spaces-service.test.ts +18 -11
  219. package/src/packlets/spaces/spaces-service.ts +124 -24
  220. package/src/packlets/storage/storage.ts +4 -4
  221. package/src/packlets/testing/invitation-utils.ts +10 -6
  222. package/src/packlets/testing/test-builder.ts +36 -10
  223. package/src/packlets/worker/worker-runtime.ts +188 -17
  224. package/src/packlets/worker/worker-session.ts +12 -18
  225. package/src/version.ts +1 -1
  226. package/dist/lib/browser/chunk-KPYVJG6G.mjs.map +0 -7
  227. package/dist/lib/node-esm/chunk-BBBSS6UL.mjs.map +0 -7
  228. package/dist/types/src/packlets/identity/default-space-state-machine.d.ts +0 -19
  229. package/dist/types/src/packlets/identity/default-space-state-machine.d.ts.map +0 -1
  230. package/src/packlets/identity/default-space-state-machine.ts +0 -44
@@ -0,0 +1,377 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import type * as SqlClient from '@effect/sql/SqlClient';
6
+ import { Encoder, decode as cborXdecode } from 'cbor-x';
7
+ import * as Effect from 'effect/Effect';
8
+ import * as Schema from 'effect/Schema';
9
+
10
+ import { AsyncTask, scheduleTask } from '@dxos/async';
11
+ import { Context, Resource } from '@dxos/context';
12
+ import { type EdgeConnection, MessageSchema } from '@dxos/edge-client';
13
+ import { RuntimeProvider } from '@dxos/effect';
14
+ import { type FeedStore, SyncClient } from '@dxos/feed';
15
+ import { invariant } from '@dxos/invariant';
16
+ import { SpaceId } from '@dxos/keys';
17
+ import { log } from '@dxos/log';
18
+ import { FeedProtocol } from '@dxos/protocols';
19
+ import { EdgeService } from '@dxos/protocols';
20
+ import { createBuf } from '@dxos/protocols/buf';
21
+ import { type Message as RouterMessage } from '@dxos/protocols/buf/dxos/edge/messenger_pb';
22
+ import type { SqlTransaction } from '@dxos/sql-sqlite';
23
+ import { bufferToArray } from '@dxos/util';
24
+
25
+ const encoder = new Encoder({ tagUint8Array: false, useRecords: false });
26
+
27
+ const DEFAULT_MESSAGE_BLOCKS_LIMIT = 50;
28
+ const DEFAULT_SYNC_CONCURRENCY = 5;
29
+ const DEFAULT_POLLING_INTERVAL = 5_000;
30
+ const DEFAULT_POLL_REQUEST_THROTTLE_MS = 250;
31
+ const MAX_BLOCKING_SYNC_ITERATIONS = 100;
32
+
33
+ interface FeedSyncerOptions {
34
+ runtime: RuntimeProvider.RuntimeProvider<SqlClient.SqlClient | SqlTransaction.SqlTransaction>;
35
+ feedStore: FeedStore;
36
+ edgeClient: EdgeConnection;
37
+ peerId: string;
38
+ getSpaceIds: () => SpaceId[];
39
+
40
+ /**
41
+ * Namespaces to sync.
42
+ */
43
+ syncNamespaces: string[];
44
+
45
+ /**
46
+ * Maximum number of blocks to sync in a single message.
47
+ * @default 50
48
+ */
49
+ messageBlocksLimit?: number;
50
+
51
+ /**
52
+ * Maximum number of spaces to sync concurrently.
53
+ * @default 5
54
+ */
55
+ syncConcurrency?: number;
56
+
57
+ /**
58
+ * Interval between full polls.
59
+ * @default 10 seconds
60
+ */
61
+ pollingInterval?: number;
62
+
63
+ /**
64
+ * Minimum delay between externally requested best-effort polls.
65
+ * @default 250 ms
66
+ */
67
+ pollRequestThrottleMs?: number;
68
+ }
69
+
70
+ export class FeedSyncer extends Resource {
71
+ readonly #syncNamespaces: string[];
72
+ readonly #messageBlocksLimit: number;
73
+ readonly #syncConcurrency: number;
74
+ readonly #pollingInterval: number;
75
+ readonly #pollRequestThrottleMs: number;
76
+
77
+ readonly #runtime: RuntimeProvider.RuntimeProvider<SqlClient.SqlClient | SqlTransaction.SqlTransaction>;
78
+ readonly #feedStore: FeedStore;
79
+ readonly #edgeClient: EdgeConnection;
80
+ readonly #syncClient: SyncClient;
81
+ readonly #getSpaceIds: () => SpaceId[];
82
+
83
+ #spacesToPoll = new Set<SpaceId>();
84
+ /** Last time full poll was completed. */
85
+ #lastFullPoll: number | null = null;
86
+ #throttledPollScheduled = false;
87
+ #lastRequestedPollAt: number | null = null;
88
+
89
+ constructor(options: FeedSyncerOptions) {
90
+ super();
91
+ this.#runtime = options.runtime;
92
+ this.#feedStore = options.feedStore;
93
+ this.#edgeClient = options.edgeClient;
94
+ this.#syncClient = new SyncClient({
95
+ peerId: options.peerId,
96
+ feedStore: options.feedStore,
97
+ sendMessage: this.#sendMessage.bind(this),
98
+ });
99
+ this.#getSpaceIds = options.getSpaceIds;
100
+ this.#syncNamespaces = options.syncNamespaces;
101
+ this.#messageBlocksLimit = options.messageBlocksLimit ?? DEFAULT_MESSAGE_BLOCKS_LIMIT;
102
+ this.#syncConcurrency = options.syncConcurrency ?? DEFAULT_SYNC_CONCURRENCY;
103
+ this.#pollingInterval = options.pollingInterval ?? DEFAULT_POLLING_INTERVAL;
104
+ this.#pollRequestThrottleMs = options.pollRequestThrottleMs ?? DEFAULT_POLL_REQUEST_THROTTLE_MS;
105
+ }
106
+
107
+ protected override async _open(): Promise<void> {
108
+ this._ctx.onDispose(
109
+ this.#edgeClient.onMessage((msg: RouterMessage) => {
110
+ if (!msg.serviceId) {
111
+ return;
112
+ }
113
+ const service = msg.serviceId.split(':')[0];
114
+ if (service !== EdgeService.QUEUE_REPLICATOR) {
115
+ return;
116
+ }
117
+ log('feed sync edge ingress', {
118
+ serviceId: msg.serviceId,
119
+ payloadByteLength: msg.payload?.value?.byteLength,
120
+ });
121
+ const handleMessageEffect = Effect.gen(this, function* () {
122
+ const decoded = yield* Effect.try({
123
+ try: () => cborXdecode(msg.payload!.value),
124
+ catch: (error) => new Error(`Failed to decode feed sync message: ${error}`),
125
+ });
126
+ const payload = yield* Schema.validate(FeedProtocol.ProtocolMessage)(decoded);
127
+ yield* this.#syncClient.handleMessage(payload);
128
+ }).pipe(
129
+ Effect.tapError((cause) =>
130
+ Effect.sync(() =>
131
+ log('feed sync edge message handling failed', {
132
+ serviceId: msg.serviceId,
133
+ payloadByteLength: msg.payload?.value?.byteLength,
134
+ cause: cause instanceof Error ? cause.message : String(cause),
135
+ }),
136
+ ),
137
+ ),
138
+ );
139
+
140
+ void RuntimeProvider.runPromise(this.#runtime)(handleMessageEffect);
141
+ }),
142
+ );
143
+
144
+ this._ctx.onDispose(
145
+ // NOTE: This will fire immediately if the connection is already open.
146
+ this.#edgeClient.onReconnected(async () => {
147
+ log('feed sync edge reconnected', {
148
+ peerKey: this.#edgeClient.peerKey,
149
+ identityKey: this.#edgeClient.identityKey,
150
+ });
151
+ }),
152
+ );
153
+
154
+ this.#feedStore.onNewBlocks.on(this._ctx, () => {
155
+ this.#pushTask.schedule();
156
+ });
157
+
158
+ await this.#pollTask.open();
159
+ await this.#pushTask.open();
160
+
161
+ this.#resetSpacesToPoll();
162
+ this.#pollTask.schedule();
163
+ }
164
+
165
+ protected override async _close(): Promise<void> {
166
+ await this.#pollTask.close();
167
+ await this.#pushTask.close();
168
+ }
169
+
170
+ /**
171
+ * Schedules a best-effort pull without blocking the caller.
172
+ */
173
+ schedulePoll(): void {
174
+ this.#resetSpacesToPoll();
175
+ if (this.#throttledPollScheduled) {
176
+ return;
177
+ }
178
+
179
+ const now = Date.now();
180
+ const delay =
181
+ this.#lastRequestedPollAt == null
182
+ ? 0
183
+ : Math.max(this.#pollRequestThrottleMs - (now - this.#lastRequestedPollAt), 0);
184
+ this.#throttledPollScheduled = true;
185
+ scheduleTask(
186
+ this._ctx,
187
+ () => {
188
+ this.#throttledPollScheduled = false;
189
+ this.#lastRequestedPollAt = Date.now();
190
+ this.#pollTask.schedule();
191
+ },
192
+ delay,
193
+ );
194
+ }
195
+
196
+ /**
197
+ * Performs queue sync and blocks until there are no pending sync batches.
198
+ */
199
+ async syncBlocking(
200
+ ctx: Context,
201
+ {
202
+ spaceId,
203
+ subspaceTag,
204
+ shouldPush = true,
205
+ shouldPull = true,
206
+ }: {
207
+ spaceId: SpaceId;
208
+ subspaceTag: string;
209
+ shouldPush?: boolean;
210
+ shouldPull?: boolean;
211
+ },
212
+ ): Promise<void> {
213
+ invariant(SpaceId.isValid(spaceId));
214
+ invariant(FeedProtocol.isWellKnownNamespace(subspaceTag));
215
+ if (!shouldPush && !shouldPull) {
216
+ return;
217
+ }
218
+
219
+ await RuntimeProvider.runPromise(this.#runtime)(
220
+ Effect.gen(this, function* () {
221
+ let done = false;
222
+ let iterations = 0;
223
+ while (!done) {
224
+ done = true;
225
+ if (shouldPull) {
226
+ const pullResult = yield* this.#syncClient.pull(ctx, {
227
+ spaceId,
228
+ feedNamespace: subspaceTag,
229
+ limit: this.#messageBlocksLimit,
230
+ });
231
+ done &&= pullResult.done;
232
+ }
233
+
234
+ if (shouldPush) {
235
+ const pushResult = yield* this.#syncClient.push(ctx, {
236
+ spaceId,
237
+ feedNamespace: subspaceTag,
238
+ limit: this.#messageBlocksLimit,
239
+ });
240
+ done &&= pushResult.done;
241
+ }
242
+ iterations++;
243
+ if (iterations > MAX_BLOCKING_SYNC_ITERATIONS) {
244
+ throw new Error('Blocking sync exceeded max iterations.');
245
+ }
246
+ }
247
+ }),
248
+ );
249
+ }
250
+
251
+ #resetSpacesToPoll(): void {
252
+ this.#spacesToPoll.clear();
253
+ this.#getSpaceIds().forEach((spaceId) => {
254
+ this.#spacesToPoll.add(spaceId);
255
+ });
256
+ this.#lastFullPoll = Date.now();
257
+ }
258
+
259
+ #sendMessage(
260
+ ctx: Context,
261
+ message: FeedProtocol.QueryRequest | FeedProtocol.AppendRequest,
262
+ ): Effect.Effect<void, unknown, never> {
263
+ return Effect.gen(this, function* () {
264
+ const encoded = encoder.encode(message);
265
+ const serviceId = this.#getTargetServiceId(message);
266
+ const rpcTag = 'blocks' in message ? 'AppendRequest' : 'QueryRequest';
267
+ log('feed sync edge rpc outgoing', {
268
+ tag: rpcTag,
269
+ serviceId,
270
+ payloadByteLength: encoded.byteLength,
271
+ spaceId: message.spaceId,
272
+ feedNamespace: message.feedNamespace,
273
+ requestId: message.requestId,
274
+ });
275
+ yield* Effect.tryPromise(async () =>
276
+ this.#edgeClient.send(
277
+ ctx,
278
+ createBuf(MessageSchema, {
279
+ source: {
280
+ identityKey: this.#edgeClient.identityKey,
281
+ peerKey: this.#edgeClient.peerKey,
282
+ },
283
+ serviceId,
284
+ payload: { value: bufferToArray(encoded) },
285
+ }),
286
+ ),
287
+ ).pipe(
288
+ Effect.tapError((cause) =>
289
+ Effect.sync(() =>
290
+ log('feed sync edge send failed', {
291
+ serviceId,
292
+ tag: rpcTag,
293
+ cause: cause instanceof Error ? cause.message : String(cause),
294
+ }),
295
+ ),
296
+ ),
297
+ );
298
+ });
299
+ }
300
+
301
+ #getTargetServiceId(message: FeedProtocol.QueryRequest | FeedProtocol.AppendRequest): string {
302
+ // TODO(dmaretskyi): Perhaps in the future we will want to include the queue namespace here as well.
303
+ // This would require putting it at the top level of the message.
304
+ // For now, we let the edge router handle it.
305
+ return FeedProtocol.encodeServiceId(message.feedNamespace, message.spaceId as SpaceId);
306
+ }
307
+
308
+ readonly #pollTask = new AsyncTask(async () =>
309
+ Effect.gen(this, function* () {
310
+ yield* Effect.forEach(
311
+ this.#spacesToPoll,
312
+ (spaceId) =>
313
+ Effect.gen(this, function* () {
314
+ let doneForAllNamespaces = true;
315
+ for (const feedNamespace of this.#syncNamespaces) {
316
+ const { done } = yield* this.#syncClient.pull(this._ctx, {
317
+ spaceId,
318
+ feedNamespace,
319
+ limit: this.#messageBlocksLimit,
320
+ });
321
+ if (!done) {
322
+ doneForAllNamespaces = false;
323
+ }
324
+ }
325
+ if (doneForAllNamespaces) {
326
+ this.#spacesToPoll.delete(spaceId);
327
+ }
328
+ }),
329
+ { concurrency: this.#syncConcurrency },
330
+ );
331
+
332
+ // If its time to do a full poll, reset the spaces to poll and schedule the next poll immediately.
333
+ if (this.#lastFullPoll == null || Date.now() - this.#lastFullPoll > this.#pollingInterval) {
334
+ this.#resetSpacesToPoll();
335
+ this.#pollTask.schedule();
336
+ } else if (this.#spacesToPoll.size > 0) {
337
+ // If there are some spaces still syncing, poll them immediately.
338
+ this.#pollTask.schedule();
339
+ } else {
340
+ // All spaces sync, and there's time before the next full poll, schedule it later.
341
+ this.#resetSpacesToPoll();
342
+ scheduleTask(
343
+ this._ctx,
344
+ () => this.#pollTask.schedule(),
345
+ Math.max(this.#pollingInterval - (Date.now() - (this.#lastFullPoll ?? 0)), 0),
346
+ );
347
+ }
348
+ }).pipe(RuntimeProvider.runPromise(this.#runtime)),
349
+ );
350
+
351
+ readonly #pushTask = new AsyncTask(async () =>
352
+ Effect.gen(this, function* () {
353
+ yield* Effect.forEach(
354
+ this.#getSpaceIds(),
355
+ (spaceId) =>
356
+ Effect.gen(this, function* () {
357
+ let doneForAllNamespaces = true;
358
+ for (const feedNamespace of this.#syncNamespaces) {
359
+ const { done } = yield* this.#syncClient.push(this._ctx, {
360
+ spaceId,
361
+ feedNamespace,
362
+ limit: this.#messageBlocksLimit,
363
+ });
364
+ if (!done) {
365
+ doneForAllNamespaces = false;
366
+ }
367
+ }
368
+ if (!doneForAllNamespaces) {
369
+ // Keep pushing until all blocks are pushed.
370
+ this.#pushTask.schedule();
371
+ }
372
+ }),
373
+ { concurrency: this.#syncConcurrency },
374
+ );
375
+ }).pipe(RuntimeProvider.runPromise(this.#runtime)),
376
+ );
377
+ }
@@ -14,12 +14,18 @@ export const getPlatform = (): Platform => {
14
14
  userAgent,
15
15
  uptime: Math.floor((Date.now() - window.performance.timeOrigin) / 1_000),
16
16
  };
17
- } else {
17
+ } else if (typeof SharedWorkerGlobalScope !== 'undefined') {
18
18
  // Shared worker.
19
19
  return {
20
20
  type: Platform.PLATFORM_TYPE.SHARED_WORKER,
21
21
  uptime: Math.floor((Date.now() - performance.timeOrigin) / 1_000),
22
22
  };
23
+ } else {
24
+ // Dedicated worker.
25
+ return {
26
+ type: Platform.PLATFORM_TYPE.DEDICATED_WORKER,
27
+ uptime: Math.floor((Date.now() - performance.timeOrigin) / 1_000),
28
+ };
23
29
  }
24
30
  } else {
25
31
  // Node.
@@ -4,6 +4,7 @@
4
4
 
5
5
  import { describe, test } from 'vitest';
6
6
 
7
+ import { Context } from '@dxos/context';
7
8
  import { MemorySignalManager, MemorySignalManagerContext } from '@dxos/messaging';
8
9
  import { Invitation } from '@dxos/protocols/proto/dxos/client/services';
9
10
  import { openAndClose } from '@dxos/test-utils';
@@ -19,7 +20,7 @@ describe('services/ServiceContext', () => {
19
20
  const device2 = await createOpenServiceContext(networkContext);
20
21
  await Promise.all(performInvitation({ host: device1, guest: device2, options: { kind: Invitation.Kind.DEVICE } }));
21
22
 
22
- const space1 = await device1.dataSpaceManager!.createSpace();
23
+ const space1 = await device1.dataSpaceManager!.createSpace(new Context());
23
24
  await device2.dataSpaceManager!.waitUntilSpaceReady(space1!.key);
24
25
  const space2 = await device2.dataSpaceManager!.spaces.get(space1.key);
25
26
  await space2!.inner.controlPipeline.state.waitUntilTimeframe(space1.inner.controlPipeline.state.timeframe);
@@ -35,7 +36,7 @@ describe('services/ServiceContext', () => {
35
36
 
36
37
  const identity2 = await createOpenServiceContext(networkContext);
37
38
  await identity2.createIdentity();
38
- const space1 = await identity2.dataSpaceManager!.createSpace();
39
+ const space1 = await identity2.dataSpaceManager!.createSpace(new Context());
39
40
  await Promise.all(
40
41
  performInvitation({
41
42
  host: identity2,