@dxos/client-services 0.8.4-main.e8ec1fe → 0.8.4-main.ef1bc66f44

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 (134) hide show
  1. package/dist/lib/browser/chunk-NQSC7HOE.mjs +22 -0
  2. package/dist/lib/browser/chunk-NQSC7HOE.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-CZSLDMSL.mjs → chunk-NTZOEM4P.mjs} +1197 -1338
  4. package/dist/lib/browser/chunk-NTZOEM4P.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-QCWEHHJW.mjs +24 -0
  6. package/dist/lib/browser/chunk-QCWEHHJW.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +441 -66
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/packlets/diagnostics/browser-diagnostics-broadcast.mjs +93 -0
  11. package/dist/lib/browser/packlets/diagnostics/browser-diagnostics-broadcast.mjs.map +7 -0
  12. package/dist/lib/browser/packlets/diagnostics/diagnostics-broadcast.mjs +11 -0
  13. package/dist/lib/browser/packlets/diagnostics/diagnostics-broadcast.mjs.map +7 -0
  14. package/dist/lib/browser/packlets/locks/browser.mjs +126 -0
  15. package/dist/lib/browser/packlets/locks/browser.mjs.map +7 -0
  16. package/dist/lib/browser/packlets/locks/node.mjs +66 -0
  17. package/dist/lib/browser/packlets/locks/node.mjs.map +7 -0
  18. package/dist/lib/browser/testing/index.mjs +24 -12
  19. package/dist/lib/browser/testing/index.mjs.map +3 -3
  20. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs +24 -0
  21. package/dist/lib/node-esm/chunk-2SZHAWBN.mjs.map +7 -0
  22. package/dist/lib/node-esm/{chunk-EUNILIU5.mjs → chunk-DQWA5ILA.mjs} +689 -699
  23. package/dist/lib/node-esm/chunk-DQWA5ILA.mjs.map +7 -0
  24. package/dist/lib/node-esm/chunk-PKEGMOQ4.mjs +22 -0
  25. package/dist/lib/node-esm/chunk-PKEGMOQ4.mjs.map +7 -0
  26. package/dist/lib/node-esm/index.mjs +441 -66
  27. package/dist/lib/node-esm/index.mjs.map +4 -4
  28. package/dist/lib/node-esm/meta.json +1 -1
  29. package/dist/lib/node-esm/packlets/diagnostics/browser-diagnostics-broadcast.mjs +93 -0
  30. package/dist/lib/node-esm/packlets/diagnostics/browser-diagnostics-broadcast.mjs.map +7 -0
  31. package/dist/lib/node-esm/packlets/diagnostics/diagnostics-broadcast.mjs +11 -0
  32. package/dist/lib/node-esm/packlets/diagnostics/diagnostics-broadcast.mjs.map +7 -0
  33. package/dist/lib/node-esm/packlets/locks/browser.mjs +126 -0
  34. package/dist/lib/node-esm/packlets/locks/browser.mjs.map +7 -0
  35. package/dist/lib/node-esm/packlets/locks/node.mjs +66 -0
  36. package/dist/lib/node-esm/packlets/locks/node.mjs.map +7 -0
  37. package/dist/lib/node-esm/testing/index.mjs +24 -12
  38. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  39. package/dist/types/src/packlets/devtools/devtools.d.ts +2 -2
  40. package/dist/types/src/packlets/devtools/devtools.d.ts.map +1 -1
  41. package/dist/types/src/packlets/diagnostics/index.d.ts +1 -1
  42. package/dist/types/src/packlets/diagnostics/index.d.ts.map +1 -1
  43. package/dist/types/src/packlets/identity/authenticator.d.ts +2 -2
  44. package/dist/types/src/packlets/identity/authenticator.d.ts.map +1 -1
  45. package/dist/types/src/packlets/identity/default-space-state-machine.d.ts +2 -2
  46. package/dist/types/src/packlets/identity/default-space-state-machine.d.ts.map +1 -1
  47. package/dist/types/src/packlets/identity/identity-manager.d.ts +4 -4
  48. package/dist/types/src/packlets/identity/identity-manager.d.ts.map +1 -1
  49. package/dist/types/src/packlets/identity/identity-recovery-manager.d.ts +2 -2
  50. package/dist/types/src/packlets/identity/identity-recovery-manager.d.ts.map +1 -1
  51. package/dist/types/src/packlets/identity/identity.d.ts +2 -2
  52. package/dist/types/src/packlets/identity/identity.d.ts.map +1 -1
  53. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts +4 -4
  54. package/dist/types/src/packlets/invitations/device-invitation-protocol.d.ts.map +1 -1
  55. package/dist/types/src/packlets/invitations/invitation-guest-extenstion.d.ts.map +1 -1
  56. package/dist/types/src/packlets/invitations/invitation-host-extension.d.ts.map +1 -1
  57. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts +2 -3
  58. package/dist/types/src/packlets/invitations/invitation-protocol.d.ts.map +1 -1
  59. package/dist/types/src/packlets/invitations/invitations-handler.d.ts +4 -4
  60. package/dist/types/src/packlets/invitations/invitations-handler.d.ts.map +1 -1
  61. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts +2 -2
  62. package/dist/types/src/packlets/invitations/space-invitation-protocol.d.ts.map +1 -1
  63. package/dist/types/src/packlets/locks/index.d.ts +1 -1
  64. package/dist/types/src/packlets/locks/index.d.ts.map +1 -1
  65. package/dist/types/src/packlets/logging/logging-service.d.ts +4 -0
  66. package/dist/types/src/packlets/logging/logging-service.d.ts.map +1 -1
  67. package/dist/types/src/packlets/services/client-rpc-server.d.ts +2 -2
  68. package/dist/types/src/packlets/services/client-rpc-server.d.ts.map +1 -1
  69. package/dist/types/src/packlets/services/feed-syncer.d.ts +41 -0
  70. package/dist/types/src/packlets/services/feed-syncer.d.ts.map +1 -0
  71. package/dist/types/src/packlets/services/platform.d.ts.map +1 -1
  72. package/dist/types/src/packlets/services/service-context.d.ts +13 -7
  73. package/dist/types/src/packlets/services/service-context.d.ts.map +1 -1
  74. package/dist/types/src/packlets/services/service-host.d.ts +19 -5
  75. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  76. package/dist/types/src/packlets/space-export/space-archive-writer.d.ts.map +1 -1
  77. package/dist/types/src/packlets/spaces/data-space-manager.d.ts +10 -5
  78. package/dist/types/src/packlets/spaces/data-space-manager.d.ts.map +1 -1
  79. package/dist/types/src/packlets/spaces/data-space.d.ts +2 -2
  80. package/dist/types/src/packlets/spaces/data-space.d.ts.map +1 -1
  81. package/dist/types/src/packlets/spaces/edge-feed-replicator.d.ts +2 -2
  82. package/dist/types/src/packlets/spaces/edge-feed-replicator.d.ts.map +1 -1
  83. package/dist/types/src/packlets/spaces/notarization-plugin.d.ts +6 -6
  84. package/dist/types/src/packlets/spaces/notarization-plugin.d.ts.map +1 -1
  85. package/dist/types/src/packlets/spaces/spaces-service.d.ts.map +1 -1
  86. package/dist/types/src/packlets/testing/invitation-utils.d.ts +6 -3
  87. package/dist/types/src/packlets/testing/invitation-utils.d.ts.map +1 -1
  88. package/dist/types/src/packlets/testing/test-builder.d.ts +6 -5
  89. package/dist/types/src/packlets/testing/test-builder.d.ts.map +1 -1
  90. package/dist/types/src/packlets/worker/worker-runtime.d.ts +31 -4
  91. package/dist/types/src/packlets/worker/worker-runtime.d.ts.map +1 -1
  92. package/dist/types/src/packlets/worker/worker-session.d.ts +2 -2
  93. package/dist/types/src/packlets/worker/worker-session.d.ts.map +1 -1
  94. package/dist/types/src/version.d.ts +1 -1
  95. package/dist/types/src/version.d.ts.map +1 -1
  96. package/dist/types/tsconfig.tsbuildinfo +1 -1
  97. package/package.json +70 -48
  98. package/src/packlets/devtools/devtools.ts +2 -2
  99. package/src/packlets/diagnostics/index.ts +1 -1
  100. package/src/packlets/identity/authenticator.ts +2 -2
  101. package/src/packlets/identity/default-space-state-machine.ts +2 -2
  102. package/src/packlets/identity/identity-manager.ts +6 -6
  103. package/src/packlets/identity/identity-recovery-manager.ts +2 -2
  104. package/src/packlets/identity/identity.ts +2 -2
  105. package/src/packlets/invitations/device-invitation-protocol.ts +5 -5
  106. package/src/packlets/invitations/invitation-guest-extenstion.ts +6 -4
  107. package/src/packlets/invitations/invitation-host-extension.ts +6 -4
  108. package/src/packlets/invitations/invitation-protocol.ts +2 -3
  109. package/src/packlets/invitations/invitations-handler.ts +7 -7
  110. package/src/packlets/invitations/space-invitation-protocol.ts +7 -13
  111. package/src/packlets/locks/index.ts +1 -1
  112. package/src/packlets/logging/logging-service.ts +4 -0
  113. package/src/packlets/services/client-rpc-server.ts +4 -4
  114. package/src/packlets/services/feed-syncer.ts +227 -0
  115. package/src/packlets/services/platform.ts +7 -1
  116. package/src/packlets/services/service-context.ts +47 -21
  117. package/src/packlets/services/service-host.ts +56 -16
  118. package/src/packlets/space-export/space-archive-reader.ts +1 -1
  119. package/src/packlets/space-export/space-archive-writer.ts +3 -2
  120. package/src/packlets/spaces/data-space-manager.ts +43 -20
  121. package/src/packlets/spaces/data-space.ts +10 -6
  122. package/src/packlets/spaces/edge-feed-replicator.ts +2 -2
  123. package/src/packlets/spaces/epoch-migrations.ts +2 -2
  124. package/src/packlets/spaces/notarization-plugin.test.ts +2 -2
  125. package/src/packlets/spaces/notarization-plugin.ts +8 -8
  126. package/src/packlets/spaces/spaces-service.ts +10 -7
  127. package/src/packlets/storage/storage.ts +4 -4
  128. package/src/packlets/testing/invitation-utils.ts +7 -4
  129. package/src/packlets/testing/test-builder.ts +36 -10
  130. package/src/packlets/worker/worker-runtime.ts +149 -11
  131. package/src/packlets/worker/worker-session.ts +4 -4
  132. package/src/version.ts +1 -1
  133. package/dist/lib/browser/chunk-CZSLDMSL.mjs.map +0 -7
  134. package/dist/lib/node-esm/chunk-EUNILIU5.mjs.map +0 -7
@@ -1,6 +1,5 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
  import {
3
- ClientRpcServer,
4
3
  ClientServicesHost,
5
4
  DataSpace,
6
5
  DataSpaceManager,
@@ -17,7 +16,6 @@ import {
17
16
  InvitationsHandler,
18
17
  InvitationsManager,
19
18
  InvitationsServiceImpl,
20
- Lock,
21
19
  ServiceContext,
22
20
  ServiceRegistry,
23
21
  SpaceInvitationProtocol,
@@ -25,17 +23,12 @@ import {
25
23
  TrustedKeySetAuthVerifier,
26
24
  createAdmissionKeypair,
27
25
  createAuthProvider,
28
- createCollectDiagnosticsBroadcastHandler,
29
- createCollectDiagnosticsBroadcastSender,
30
26
  createDiagnostics,
31
27
  createLevel,
32
28
  createStorageObjects,
33
- decodeProfileArchive,
34
- encodeProfileArchive,
35
- exportProfileData,
29
+ diagnostics_exports,
36
30
  getNetworkPeers,
37
- importProfileData,
38
- isLocked,
31
+ locks_exports,
39
32
  subscribeToFeedBlocks,
40
33
  subscribeToFeeds,
41
34
  subscribeToNetworkStatus,
@@ -43,31 +36,332 @@ import {
43
36
  subscribeToSignal,
44
37
  subscribeToSpaces,
45
38
  subscribeToSwarmInfo
46
- } from "./chunk-EUNILIU5.mjs";
39
+ } from "./chunk-DQWA5ILA.mjs";
40
+ import "./chunk-PKEGMOQ4.mjs";
41
+ import {
42
+ __export,
43
+ __reExport
44
+ } from "./chunk-2SZHAWBN.mjs";
45
+
46
+ // src/index.ts
47
+ var index_exports = {};
48
+ __export(index_exports, {
49
+ ClientRpcServer: () => ClientRpcServer,
50
+ ClientServicesHost: () => ClientServicesHost,
51
+ DataSpace: () => DataSpace,
52
+ DataSpaceManager: () => DataSpaceManager,
53
+ DeviceInvitationProtocol: () => DeviceInvitationProtocol,
54
+ DevtoolsHostEvents: () => DevtoolsHostEvents,
55
+ DevtoolsServiceImpl: () => DevtoolsServiceImpl,
56
+ DiagnosticsCollector: () => DiagnosticsCollector,
57
+ EdgeAgentManager: () => EdgeAgentManager,
58
+ EdgeAgentServiceImpl: () => EdgeAgentServiceImpl,
59
+ EdgeFeedReplicator: () => EdgeFeedReplicator,
60
+ Identity: () => Identity,
61
+ IdentityManager: () => IdentityManager,
62
+ IdentityServiceImpl: () => IdentityServiceImpl,
63
+ InvitationsHandler: () => InvitationsHandler,
64
+ InvitationsManager: () => InvitationsManager,
65
+ InvitationsServiceImpl: () => InvitationsServiceImpl,
66
+ ServiceContext: () => ServiceContext,
67
+ ServiceRegistry: () => ServiceRegistry,
68
+ SpaceInvitationProtocol: () => SpaceInvitationProtocol,
69
+ SpacesServiceImpl: () => SpacesServiceImpl,
70
+ TrustedKeySetAuthVerifier: () => TrustedKeySetAuthVerifier,
71
+ WorkerRuntime: () => WorkerRuntime,
72
+ WorkerSession: () => WorkerSession,
73
+ createAdmissionKeypair: () => createAdmissionKeypair,
74
+ createAuthProvider: () => createAuthProvider,
75
+ createDiagnostics: () => createDiagnostics,
76
+ createLevel: () => createLevel,
77
+ createStorageObjects: () => createStorageObjects,
78
+ decodeProfileArchive: () => decodeProfileArchive,
79
+ encodeProfileArchive: () => encodeProfileArchive,
80
+ exportProfileData: () => exportProfileData,
81
+ getNetworkPeers: () => getNetworkPeers,
82
+ importProfileData: () => importProfileData,
83
+ subscribeToFeedBlocks: () => subscribeToFeedBlocks,
84
+ subscribeToFeeds: () => subscribeToFeeds,
85
+ subscribeToNetworkStatus: () => subscribeToNetworkStatus,
86
+ subscribeToNetworkTopics: () => subscribeToNetworkTopics,
87
+ subscribeToSignal: () => subscribeToSignal,
88
+ subscribeToSpaces: () => subscribeToSpaces,
89
+ subscribeToSwarmInfo: () => subscribeToSwarmInfo
90
+ });
91
+ __reExport(index_exports, diagnostics_exports);
92
+ __reExport(index_exports, locks_exports);
93
+
94
+ // src/packlets/services/client-rpc-server.ts
95
+ import { Stream } from "@dxos/codec-protobuf";
96
+ import { raise } from "@dxos/debug";
97
+ import { RpcPeer, parseMethodName } from "@dxos/rpc";
98
+ import { MapCounter, trace } from "@dxos/tracing";
99
+ function _ts_decorate(decorators, target, key, desc) {
100
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
101
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
102
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
103
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
104
+ }
105
+ var ClientRpcServer = class {
106
+ _serviceRegistry;
107
+ _rpcPeer;
108
+ _handlerCache = /* @__PURE__ */ new Map();
109
+ _handleCall;
110
+ _handleStream;
111
+ _callMetrics = new MapCounter();
112
+ get _services() {
113
+ return Object.keys(this._serviceRegistry.services);
114
+ }
115
+ constructor(params) {
116
+ const { serviceRegistry, handleCall, handleStream, ...rpcOptions } = params;
117
+ this._handleCall = handleCall;
118
+ this._handleStream = handleStream;
119
+ this._serviceRegistry = serviceRegistry;
120
+ this._rpcPeer = new RpcPeer({
121
+ ...rpcOptions,
122
+ callHandler: (method, params2) => {
123
+ const [serviceName, methodName] = parseMethodName(method);
124
+ const handler = (method2, params3) => this._getServiceHandler(serviceName).call(method2, params3);
125
+ this._callMetrics.inc(`${serviceName}.${methodName} request`);
126
+ if (this._handleCall) {
127
+ return this._handleCall(methodName, params2, handler);
128
+ } else {
129
+ return handler(methodName, params2);
130
+ }
131
+ },
132
+ streamHandler: (method, params2) => {
133
+ const [serviceName, methodName] = parseMethodName(method);
134
+ const handler = (method2, params3) => this._getServiceHandler(serviceName).callStream(method2, params3);
135
+ this._callMetrics.inc(`${serviceName}.${methodName} request stream`);
136
+ if (this._handleStream) {
137
+ return Stream.map(Stream.unwrapPromise(this._handleStream(methodName, params2, handler)), (data) => {
138
+ this._callMetrics.inc(`${serviceName}.${methodName} response stream`);
139
+ return data;
140
+ });
141
+ } else {
142
+ return handler(methodName, params2);
143
+ }
144
+ }
145
+ });
146
+ }
147
+ async open() {
148
+ await this._rpcPeer.open();
149
+ }
150
+ async close() {
151
+ await this._rpcPeer.close();
152
+ }
153
+ _getServiceHandler(serviceName) {
154
+ if (!this._handlerCache.has(serviceName)) {
155
+ const [key, descriptor] = Object.entries(this._serviceRegistry.descriptors).find(([key2, descriptor2]) => descriptor2.name === serviceName) ?? raise(new Error(`Service not available: ${serviceName}`));
156
+ const service = this._serviceRegistry.services[key];
157
+ if (!service) {
158
+ throw new Error(`Service not available: ${serviceName}`);
159
+ }
160
+ this._handlerCache.set(serviceName, descriptor.createServer(service));
161
+ }
162
+ return this._handlerCache.get(serviceName);
163
+ }
164
+ };
165
+ _ts_decorate([
166
+ trace.metricsCounter()
167
+ ], ClientRpcServer.prototype, "_callMetrics", void 0);
168
+ _ts_decorate([
169
+ trace.info()
170
+ ], ClientRpcServer.prototype, "_services", null);
171
+ ClientRpcServer = _ts_decorate([
172
+ trace.resource()
173
+ ], ClientRpcServer);
174
+
175
+ // src/packlets/storage/profile-archive.ts
176
+ import { cbor } from "@automerge/automerge-repo";
177
+ import { invariant } from "@dxos/invariant";
178
+ import { log } from "@dxos/log";
179
+ import { ProfileArchiveEntryType } from "@dxos/protocols";
180
+ import { arrayToBuffer } from "@dxos/util";
181
+ var __dxlog_file = "/__w/dxos/dxos/packages/sdk/client-services/src/packlets/storage/profile-archive.ts";
182
+ var encodeProfileArchive = (profile) => cbor.encode(profile);
183
+ var decodeProfileArchive = (data) => cbor.decode(data);
184
+ var exportProfileData = async ({ storage, level }) => {
185
+ const archive = {
186
+ storage: [],
187
+ meta: {
188
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
189
+ }
190
+ };
191
+ {
192
+ const directory = await storage.createDirectory();
193
+ const files = await directory.list();
194
+ log.info("begin exporting files", {
195
+ count: files.length
196
+ }, {
197
+ F: __dxlog_file,
198
+ L: 31,
199
+ S: void 0,
200
+ C: (f, a) => f(...a)
201
+ });
202
+ for (const filename of files) {
203
+ const file = await directory.getOrCreateFile(filename);
204
+ const { size } = await file.stat();
205
+ const data = await file.read(0, size);
206
+ archive.storage.push({
207
+ type: ProfileArchiveEntryType.FILE,
208
+ key: filename,
209
+ value: data
210
+ });
211
+ }
212
+ log.info("done exporting files", {
213
+ count: files.length
214
+ }, {
215
+ F: __dxlog_file,
216
+ L: 42,
217
+ S: void 0,
218
+ C: (f, a) => f(...a)
219
+ });
220
+ }
221
+ {
222
+ log.info("begin exporting kv pairs", void 0, {
223
+ F: __dxlog_file,
224
+ L: 46,
225
+ S: void 0,
226
+ C: (f, a) => f(...a)
227
+ });
228
+ const iter = await level.iterator({
229
+ keyEncoding: "binary",
230
+ valueEncoding: "binary"
231
+ });
232
+ let count = 0;
233
+ for await (const [key, value] of iter) {
234
+ archive.storage.push({
235
+ type: ProfileArchiveEntryType.KEY_VALUE,
236
+ key,
237
+ value
238
+ });
239
+ count++;
240
+ }
241
+ log.info("done exporting kv pairs", {
242
+ count
243
+ }, {
244
+ F: __dxlog_file,
245
+ L: 57,
246
+ S: void 0,
247
+ C: (f, a) => f(...a)
248
+ });
249
+ }
250
+ return archive;
251
+ };
252
+ var importProfileData = async ({ storage, level }, archive) => {
253
+ let batch = level.batch();
254
+ let count = 0;
255
+ for (const entry of archive.storage) {
256
+ switch (entry.type) {
257
+ case ProfileArchiveEntryType.FILE: {
258
+ const directory = await storage.createDirectory();
259
+ invariant(typeof entry.key === "string", "Invalid key type", {
260
+ F: __dxlog_file,
261
+ L: 80,
262
+ S: void 0,
263
+ A: [
264
+ "typeof entry.key === 'string'",
265
+ "'Invalid key type'"
266
+ ]
267
+ });
268
+ const file = await directory.getOrCreateFile(entry.key);
269
+ invariant(entry.value instanceof Uint8Array, "Invalid value type", {
270
+ F: __dxlog_file,
271
+ L: 82,
272
+ S: void 0,
273
+ A: [
274
+ "entry.value instanceof Uint8Array",
275
+ "'Invalid value type'"
276
+ ]
277
+ });
278
+ await file.write(0, arrayToBuffer(entry.value));
279
+ await file.close();
280
+ break;
281
+ }
282
+ case ProfileArchiveEntryType.KEY_VALUE: {
283
+ invariant(entry.key instanceof Uint8Array, "Invalid key type", {
284
+ F: __dxlog_file,
285
+ L: 88,
286
+ S: void 0,
287
+ A: [
288
+ "entry.key instanceof Uint8Array",
289
+ "'Invalid key type'"
290
+ ]
291
+ });
292
+ invariant(entry.value instanceof Uint8Array, "Invalid value type", {
293
+ F: __dxlog_file,
294
+ L: 89,
295
+ S: void 0,
296
+ A: [
297
+ "entry.value instanceof Uint8Array",
298
+ "'Invalid value type'"
299
+ ]
300
+ });
301
+ batch.put(entry.key, entry.value, {
302
+ keyEncoding: "binary",
303
+ valueEncoding: "binary"
304
+ });
305
+ break;
306
+ }
307
+ default:
308
+ throw new Error(`Invalid entry type: ${entry.type}`);
309
+ }
310
+ if (++count % 1e3 === 0) {
311
+ await batch.write();
312
+ batch = level.batch();
313
+ log.info("importing", {
314
+ count,
315
+ total: archive.storage.length,
316
+ progress: `${(count / archive.storage.length * 100).toFixed()}%`
317
+ }, {
318
+ F: __dxlog_file,
319
+ L: 102,
320
+ S: void 0,
321
+ C: (f, a) => f(...a)
322
+ });
323
+ }
324
+ }
325
+ log.info("committing changes..", void 0, {
326
+ F: __dxlog_file,
327
+ L: 110,
328
+ S: void 0,
329
+ C: (f, a) => f(...a)
330
+ });
331
+ await batch.write();
332
+ };
47
333
 
48
334
  // src/packlets/worker/worker-runtime.ts
335
+ import * as Reactivity from "@effect/experimental/Reactivity";
336
+ import * as Effect from "effect/Effect";
337
+ import * as Layer from "effect/Layer";
338
+ import * as ManagedRuntime from "effect/ManagedRuntime";
49
339
  import { Trigger as Trigger2 } from "@dxos/async";
50
340
  import { DEFAULT_WORKER_BROADCAST_CHANNEL } from "@dxos/client-protocol";
51
341
  import { Context } from "@dxos/context";
52
- import { invariant as invariant2 } from "@dxos/invariant";
53
- import { log as log2 } from "@dxos/log";
342
+ import { invariant as invariant3 } from "@dxos/invariant";
343
+ import { log as log3 } from "@dxos/log";
54
344
  import { MemorySignalManager, MemorySignalManagerContext, WebsocketSignalManager, setIdentityTags } from "@dxos/messaging";
55
345
  import { RtcTransportProxyFactory } from "@dxos/network-manager";
346
+ import * as OpfsWorker from "@dxos/sql-sqlite/OpfsWorker";
347
+ import * as SqlExport from "@dxos/sql-sqlite/SqlExport";
348
+ import * as SqliteClient from "@dxos/sql-sqlite/SqliteClient";
349
+ import * as SqlTransaction from "@dxos/sql-sqlite/SqlTransaction";
56
350
 
57
351
  // src/packlets/worker/worker-session.ts
58
352
  import { Trigger, asyncTimeout } from "@dxos/async";
59
353
  import { PROXY_CONNECTION_TIMEOUT, iframeServiceBundle, workerServiceBundle } from "@dxos/client-protocol";
60
- import { invariant } from "@dxos/invariant";
61
- import { log, logInfo } from "@dxos/log";
354
+ import { invariant as invariant2 } from "@dxos/invariant";
355
+ import { log as log2, logInfo } from "@dxos/log";
62
356
  import { createProtoRpcPeer } from "@dxos/rpc";
63
357
  import { Callback } from "@dxos/util";
64
- function _ts_decorate(decorators, target, key, desc) {
358
+ function _ts_decorate2(decorators, target, key, desc) {
65
359
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
66
360
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
67
361
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
68
362
  return c > 3 && r && Object.defineProperty(target, key, r), r;
69
363
  }
70
- var __dxlog_file = "/__w/dxos/dxos/packages/sdk/client-services/src/packlets/worker/worker-session.ts";
364
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/client-services/src/packlets/worker/worker-session.ts";
71
365
  var WorkerSession = class {
72
366
  _clientRpc;
73
367
  _shellClientRpc;
@@ -82,8 +376,8 @@ var WorkerSession = class {
82
376
  lockKey;
83
377
  bridgeService;
84
378
  constructor({ serviceHost, systemPort, appPort, shellPort, readySignal }) {
85
- invariant(serviceHost, void 0, {
86
- F: __dxlog_file,
379
+ invariant2(serviceHost, void 0, {
380
+ F: __dxlog_file2,
87
381
  L: 54,
88
382
  S: this,
89
383
  A: [
@@ -139,8 +433,8 @@ var WorkerSession = class {
139
433
  try {
140
434
  await this.close();
141
435
  } catch (err) {
142
- log.catch(err, void 0, {
143
- F: __dxlog_file,
436
+ log2.catch(err, void 0, {
437
+ F: __dxlog_file2,
144
438
  L: 108,
145
439
  S: this,
146
440
  C: (f, a) => f(...a)
@@ -156,8 +450,8 @@ var WorkerSession = class {
156
450
  this.bridgeService = this._iframeRpc.rpc.BridgeService;
157
451
  }
158
452
  async open() {
159
- log.info("opening...", void 0, {
160
- F: __dxlog_file,
453
+ log2.info("opening...", void 0, {
454
+ F: __dxlog_file2,
161
455
  L: 122,
162
456
  S: this,
163
457
  C: (f, a) => f(...a)
@@ -173,16 +467,16 @@ var WorkerSession = class {
173
467
  if (this.lockKey) {
174
468
  void this._afterLockReleases(this.lockKey, () => this.close());
175
469
  }
176
- log.info("opened", void 0, {
177
- F: __dxlog_file,
470
+ log2.info("opened", void 0, {
471
+ F: __dxlog_file2,
178
472
  L: 133,
179
473
  S: this,
180
474
  C: (f, a) => f(...a)
181
475
  });
182
476
  }
183
477
  async close() {
184
- log.info("closing...", void 0, {
185
- F: __dxlog_file,
478
+ log2.info("closing...", void 0, {
479
+ F: __dxlog_file2,
186
480
  L: 137,
187
481
  S: this,
188
482
  C: (f, a) => f(...a)
@@ -190,8 +484,8 @@ var WorkerSession = class {
190
484
  try {
191
485
  await this.onClose.callIfSet();
192
486
  } catch (err) {
193
- log.catch(err, void 0, {
194
- F: __dxlog_file,
487
+ log2.catch(err, void 0, {
488
+ F: __dxlog_file2,
195
489
  L: 141,
196
490
  S: this,
197
491
  C: (f, a) => f(...a)
@@ -201,8 +495,8 @@ var WorkerSession = class {
201
495
  this._clientRpc.close(),
202
496
  this._iframeRpc.close()
203
497
  ]);
204
- log.info("closed", void 0, {
205
- F: __dxlog_file,
498
+ log2.info("closed", void 0, {
499
+ F: __dxlog_file2,
206
500
  L: 145,
207
501
  S: this,
208
502
  C: (f, a) => f(...a)
@@ -212,8 +506,8 @@ var WorkerSession = class {
212
506
  try {
213
507
  this._shellClientRpc && await asyncTimeout(this._shellClientRpc.open(), 1e3);
214
508
  } catch {
215
- log.info("No shell connected.", void 0, {
216
- F: __dxlog_file,
509
+ log2.info("No shell connected.", void 0, {
510
+ F: __dxlog_file2,
217
511
  L: 152,
218
512
  S: this,
219
513
  C: (f, a) => f(...a)
@@ -225,15 +519,15 @@ var WorkerSession = class {
225
519
  }).then(callback);
226
520
  }
227
521
  };
228
- _ts_decorate([
522
+ _ts_decorate2([
229
523
  logInfo
230
524
  ], WorkerSession.prototype, "origin", void 0);
231
- _ts_decorate([
525
+ _ts_decorate2([
232
526
  logInfo
233
527
  ], WorkerSession.prototype, "lockKey", void 0);
234
528
 
235
529
  // src/packlets/worker/worker-runtime.ts
236
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/client-services/src/packlets/worker/worker-runtime.ts";
530
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/sdk/client-services/src/packlets/worker/worker-runtime.ts";
237
531
  var WorkerRuntime = class {
238
532
  _configProvider;
239
533
  _acquireLock;
@@ -244,6 +538,8 @@ var WorkerRuntime = class {
244
538
  _sessions = /* @__PURE__ */ new Set();
245
539
  _clientServices;
246
540
  _channel;
541
+ _automaticallyConnectWebrtc;
542
+ _livenessLock = new WebLockWrapper(`@dxos/client-services/WorkerRuntime/${crypto.randomUUID()}`);
247
543
  _broadcastChannel;
248
544
  _sessionForNetworking;
249
545
  _config;
@@ -251,29 +547,49 @@ var WorkerRuntime = class {
251
547
  runtime: "worker-runtime"
252
548
  };
253
549
  _signalTelemetryEnabled = false;
254
- constructor({ channel = DEFAULT_WORKER_BROADCAST_CHANNEL, configProvider, acquireLock, releaseLock, onStop }) {
550
+ _runtime;
551
+ constructor({ channel = DEFAULT_WORKER_BROADCAST_CHANNEL, configProvider, acquireLock, releaseLock, onStop, automaticallyConnectWebrtc = true, sqliteLayer }) {
255
552
  this._configProvider = configProvider;
256
553
  this._acquireLock = acquireLock;
257
554
  this._releaseLock = releaseLock;
258
555
  this._onStop = onStop;
259
556
  this._channel = channel;
557
+ if (sqliteLayer) {
558
+ log3.warn("Using testing SQLite layer", void 0, {
559
+ F: __dxlog_file3,
560
+ L: 103,
561
+ S: this,
562
+ C: (f, a) => f(...a)
563
+ });
564
+ }
565
+ this._runtime = ManagedRuntime.make(SqlTransaction.layer.pipe(Layer.provideMerge(sqliteLayer ?? LocalSqliteOpfsLayer), Layer.provideMerge(Reactivity.layer)).pipe(Layer.orDie));
260
566
  this._clientServices = new ClientServicesHost({
261
567
  callbacks: {
262
568
  onReset: async () => this.stop()
569
+ },
570
+ runtime: this._runtime.runtimeEffect,
571
+ runtimeProps: {
572
+ // Auto-activate spaces that were previously active after leader changeover.
573
+ autoActivateSpaces: true
263
574
  }
264
575
  });
576
+ this._automaticallyConnectWebrtc = automaticallyConnectWebrtc;
265
577
  }
266
578
  get host() {
267
579
  return this._clientServices;
268
580
  }
581
+ get livenessLockKey() {
582
+ return this._livenessLock.key;
583
+ }
269
584
  async start() {
270
- log2("starting...", void 0, {
271
- F: __dxlog_file2,
272
- L: 85,
585
+ log3("starting...", void 0, {
586
+ F: __dxlog_file3,
587
+ L: 132,
273
588
  S: this,
274
589
  C: (f, a) => f(...a)
275
590
  });
276
591
  try {
592
+ void this._livenessLock.acquire();
277
593
  this._broadcastChannel = new BroadcastChannel(this._channel);
278
594
  this._broadcastChannel.postMessage({
279
595
  action: "stop"
@@ -292,13 +608,13 @@ var WorkerRuntime = class {
292
608
  transportFactory: this._transportFactory
293
609
  });
294
610
  await this._clientServices.open(new Context(void 0, {
295
- F: __dxlog_file2,
296
- L: 108
611
+ F: __dxlog_file3,
612
+ L: 158
297
613
  }));
298
614
  this._ready.wake(void 0);
299
- log2("started", void 0, {
300
- F: __dxlog_file2,
301
- L: 110,
615
+ log3("started", void 0, {
616
+ F: __dxlog_file3,
617
+ L: 160,
302
618
  S: this,
303
619
  C: (f, a) => f(...a)
304
620
  });
@@ -311,9 +627,9 @@ var WorkerRuntime = class {
311
627
  });
312
628
  } catch (err) {
313
629
  this._ready.wake(err);
314
- log2.error("starting", err, {
315
- F: __dxlog_file2,
316
- L: 120,
630
+ log3.error("starting", err, {
631
+ F: __dxlog_file3,
632
+ L: 170,
317
633
  S: this,
318
634
  C: (f, a) => f(...a)
319
635
  });
@@ -324,12 +640,14 @@ var WorkerRuntime = class {
324
640
  this._broadcastChannel?.close();
325
641
  this._broadcastChannel = void 0;
326
642
  await this._clientServices.close();
643
+ await this._runtime.dispose();
327
644
  await this._onStop?.();
645
+ await this._livenessLock.release();
328
646
  }
329
647
  /**
330
648
  * Create a new session.
331
649
  */
332
- async createSession({ appPort, systemPort, shellPort }) {
650
+ async createSession({ appPort, systemPort, shellPort, onClose }) {
333
651
  const session = new WorkerSession({
334
652
  serviceHost: this._clientServices,
335
653
  appPort,
@@ -342,13 +660,16 @@ var WorkerRuntime = class {
342
660
  if (this._sessions.size === 0) {
343
661
  await this.stop();
344
662
  } else {
345
- this._reconnectWebrtc();
663
+ if (this._automaticallyConnectWebrtc) {
664
+ this._reconnectWebrtc();
665
+ }
346
666
  }
667
+ await onClose?.();
347
668
  });
348
669
  await session.open();
349
- invariant2(!this._signalMetadataTags.origin || this._signalMetadataTags.origin === session.origin, `worker origin changed from ${this._signalMetadataTags.origin} to ${session.origin}?`, {
350
- F: __dxlog_file2,
351
- L: 158,
670
+ invariant3(!this._signalMetadataTags.origin || this._signalMetadataTags.origin === session.origin, `worker origin changed from ${this._signalMetadataTags.origin} to ${session.origin}?`, {
671
+ F: __dxlog_file3,
672
+ L: 213,
352
673
  S: this,
353
674
  A: [
354
675
  "!this._signalMetadataTags.origin || this._signalMetadataTags.origin === session.origin",
@@ -361,15 +682,30 @@ var WorkerRuntime = class {
361
682
  this._signalTelemetryEnabled = session.signalTelemetryEnabled ?? false;
362
683
  this._signalMetadataTags.origin = session.origin;
363
684
  this._sessions.add(session);
364
- this._reconnectWebrtc();
685
+ if (this._automaticallyConnectWebrtc) {
686
+ this._reconnectWebrtc();
687
+ }
688
+ return session;
689
+ }
690
+ /**
691
+ * Connects the WebRTC bridge to the specified session.
692
+ * If no session is provided, disconnects the WebRTC bridge.
693
+ *
694
+ * Called automatically if `automaticallyConnectWebrtc` is true.
695
+ *
696
+ * @param session The session to connect the WebRTC bridge to.
697
+ */
698
+ connectWebrtcBridge(session) {
699
+ this._sessionForNetworking = session;
700
+ this._transportFactory.setBridgeService(session?.bridgeService);
365
701
  }
366
702
  /**
367
703
  * Selects one of the existing session for WebRTC networking.
368
704
  */
369
705
  _reconnectWebrtc() {
370
- log2("reconnecting webrtc...", void 0, {
371
- F: __dxlog_file2,
372
- L: 176,
706
+ log3("reconnecting webrtc...", void 0, {
707
+ F: __dxlog_file3,
708
+ L: 248,
373
709
  S: this,
374
710
  C: (f, a) => f(...a)
375
711
  });
@@ -380,15 +716,58 @@ var WorkerRuntime = class {
380
716
  }
381
717
  if (!this._sessionForNetworking) {
382
718
  const selected = Array.from(this._sessions).find((session) => session.bridgeService);
383
- if (selected) {
384
- this._sessionForNetworking = selected;
385
- this._transportFactory.setBridgeService(selected.bridgeService);
386
- } else {
387
- this._transportFactory.setBridgeService(void 0);
388
- }
719
+ this.connectWebrtcBridge(selected);
389
720
  }
390
721
  }
391
722
  };
723
+ var DB_NAME = "DXOS";
724
+ var SqlExportLayer = Layer.effect(SqlExport.SqlExport, Effect.gen(function* () {
725
+ const sql = yield* SqliteClient.SqliteClient;
726
+ return {
727
+ export: sql.export
728
+ };
729
+ }));
730
+ var LocalSqliteOpfsLayer = Layer.unwrapScoped(Effect.gen(function* () {
731
+ const { port1: clientPort, port2: serverPort } = new MessageChannel();
732
+ clientPort.start();
733
+ serverPort.start();
734
+ yield* Effect.addFinalizer(() => Effect.sync(() => {
735
+ clientPort.close();
736
+ serverPort.close();
737
+ }));
738
+ yield* Effect.forkScoped(OpfsWorker.run({
739
+ port: serverPort,
740
+ dbName: DB_NAME
741
+ }));
742
+ return SqlExportLayer.pipe(Layer.provideMerge(SqliteClient.layer({
743
+ worker: Effect.succeed(clientPort)
744
+ })));
745
+ }));
746
+ var WebLockWrapper = class {
747
+ #key;
748
+ #release;
749
+ constructor(key) {
750
+ this.#key = key;
751
+ }
752
+ get key() {
753
+ return this.#key;
754
+ }
755
+ acquire(options = {}) {
756
+ return navigator.locks.request(this.#key, options, async () => {
757
+ await new Promise((resolve) => {
758
+ this.#release = resolve;
759
+ });
760
+ this.#release = void 0;
761
+ });
762
+ }
763
+ release() {
764
+ this.#release?.();
765
+ this.#release = void 0;
766
+ }
767
+ [Symbol.dispose]() {
768
+ this.release();
769
+ }
770
+ };
392
771
  export {
393
772
  ClientRpcServer,
394
773
  ClientServicesHost,
@@ -407,7 +786,6 @@ export {
407
786
  InvitationsHandler,
408
787
  InvitationsManager,
409
788
  InvitationsServiceImpl,
410
- Lock,
411
789
  ServiceContext,
412
790
  ServiceRegistry,
413
791
  SpaceInvitationProtocol,
@@ -417,8 +795,6 @@ export {
417
795
  WorkerSession,
418
796
  createAdmissionKeypair,
419
797
  createAuthProvider,
420
- createCollectDiagnosticsBroadcastHandler,
421
- createCollectDiagnosticsBroadcastSender,
422
798
  createDiagnostics,
423
799
  createLevel,
424
800
  createStorageObjects,
@@ -427,7 +803,6 @@ export {
427
803
  exportProfileData,
428
804
  getNetworkPeers,
429
805
  importProfileData,
430
- isLocked,
431
806
  subscribeToFeedBlocks,
432
807
  subscribeToFeeds,
433
808
  subscribeToNetworkStatus,