@dxos/client-services 0.1.49 → 0.1.50-next.30ca3c7

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 (35) hide show
  1. package/dist/lib/browser/{chunk-YLH4UYUT.mjs → chunk-UL74TKNZ.mjs} +497 -1095
  2. package/dist/lib/browser/chunk-UL74TKNZ.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +601 -9
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/packlets/testing/index.mjs +1 -1
  7. package/dist/lib/node/index.cjs +1098 -1158
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/packlets/testing/index.cjs +238 -726
  11. package/dist/lib/node/packlets/testing/index.cjs.map +4 -4
  12. package/dist/types/src/index.d.ts +1 -0
  13. package/dist/types/src/index.d.ts.map +1 -1
  14. package/dist/types/src/packlets/locks/browser.d.ts +16 -0
  15. package/dist/types/src/packlets/locks/browser.d.ts.map +1 -0
  16. package/dist/types/src/packlets/locks/index.d.ts +3 -0
  17. package/dist/types/src/packlets/locks/index.d.ts.map +1 -0
  18. package/dist/types/src/packlets/locks/node.d.ts +13 -0
  19. package/dist/types/src/packlets/locks/node.d.ts.map +1 -0
  20. package/dist/types/src/packlets/locks/resource-lock.d.ts +16 -0
  21. package/dist/types/src/packlets/locks/resource-lock.d.ts.map +1 -0
  22. package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
  23. package/dist/types/src/packlets/vault/index.d.ts +0 -1
  24. package/dist/types/src/packlets/vault/index.d.ts.map +1 -1
  25. package/package.json +33 -30
  26. package/src/index.ts +1 -0
  27. package/src/packlets/{vault/vault-resource-lock.ts → locks/browser.ts} +12 -11
  28. package/src/packlets/locks/index.ts +6 -0
  29. package/src/packlets/locks/node.ts +52 -0
  30. package/src/packlets/locks/resource-lock.ts +24 -0
  31. package/src/packlets/services/service-host.ts +13 -13
  32. package/src/packlets/vault/index.ts +0 -1
  33. package/dist/lib/browser/chunk-YLH4UYUT.mjs.map +0 -7
  34. package/dist/types/src/packlets/vault/vault-resource-lock.d.ts +0 -20
  35. package/dist/types/src/packlets/vault/vault-resource-lock.d.ts.map +0 -1
@@ -2059,6 +2059,131 @@ var SpaceInvitationProtocol = class {
2059
2059
  }
2060
2060
  };
2061
2061
 
2062
+ // packages/sdk/client-services/src/packlets/locks/browser.ts
2063
+ import { asyncTimeout, Trigger as Trigger4 } from "@dxos/async";
2064
+ import { RESOURCE_LOCK_TIMEOUT } from "@dxos/client-protocol";
2065
+ import { log as log8, logInfo } from "@dxos/log";
2066
+ var __decorate = function(decorators, target, key, desc) {
2067
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2068
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2069
+ r = Reflect.decorate(decorators, target, key, desc);
2070
+ else
2071
+ for (var i = decorators.length - 1; i >= 0; i--)
2072
+ if (d = decorators[i])
2073
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2074
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2075
+ };
2076
+ var Message;
2077
+ (function(Message2) {
2078
+ Message2["ACQUIRING"] = "acquiring";
2079
+ })(Message || (Message = {}));
2080
+ var Lock = class {
2081
+ constructor({ lockKey, onAcquire, onRelease }) {
2082
+ this._broadcastChannel = new BroadcastChannel("vault-resource-lock");
2083
+ this._releaseTrigger = new Trigger4();
2084
+ this._lockKey = lockKey;
2085
+ this._onAcquire = onAcquire;
2086
+ this._onRelease = onRelease;
2087
+ this._broadcastChannel.onmessage = this._onMessage.bind(this);
2088
+ }
2089
+ get lockKey() {
2090
+ return this._lockKey;
2091
+ }
2092
+ async acquire() {
2093
+ this._broadcastChannel.postMessage({
2094
+ message: Message.ACQUIRING
2095
+ });
2096
+ try {
2097
+ log8("aquiring lock...", {}, {
2098
+ file: "browser.ts",
2099
+ line: 42,
2100
+ scope: this,
2101
+ callSite: (f, a) => f(...a)
2102
+ });
2103
+ await asyncTimeout(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
2104
+ log8("acquired lock", {}, {
2105
+ file: "browser.ts",
2106
+ line: 44,
2107
+ scope: this,
2108
+ callSite: (f, a) => f(...a)
2109
+ });
2110
+ } catch (e) {
2111
+ log8("stealing lock...", {}, {
2112
+ file: "browser.ts",
2113
+ line: 46,
2114
+ scope: this,
2115
+ callSite: (f, a) => f(...a)
2116
+ });
2117
+ await this._requestLock(true);
2118
+ log8("stolen lock", {}, {
2119
+ file: "browser.ts",
2120
+ line: 48,
2121
+ scope: this,
2122
+ callSite: (f, a) => f(...a)
2123
+ });
2124
+ }
2125
+ }
2126
+ async release() {
2127
+ this._releaseTrigger.wake();
2128
+ }
2129
+ _onMessage(event) {
2130
+ if (event.data.message === Message.ACQUIRING) {
2131
+ this._releaseTrigger.wake();
2132
+ }
2133
+ }
2134
+ async _requestLock(steal = false) {
2135
+ log8("requesting lock...", {
2136
+ steal
2137
+ }, {
2138
+ file: "browser.ts",
2139
+ line: 63,
2140
+ scope: this,
2141
+ callSite: (f, a) => f(...a)
2142
+ });
2143
+ const acquired = new Trigger4();
2144
+ void navigator.locks.request(this._lockKey, {
2145
+ steal
2146
+ }, async () => {
2147
+ var _a, _b;
2148
+ await ((_a = this._onAcquire) == null ? void 0 : _a.call(this));
2149
+ acquired.wake();
2150
+ this._releaseTrigger = new Trigger4();
2151
+ await this._releaseTrigger.wait();
2152
+ log8("releasing lock...", {}, {
2153
+ file: "browser.ts",
2154
+ line: 72,
2155
+ scope: this,
2156
+ callSite: (f, a) => f(...a)
2157
+ });
2158
+ await ((_b = this._onRelease) == null ? void 0 : _b.call(this));
2159
+ log8("released lock", {}, {
2160
+ file: "browser.ts",
2161
+ line: 74,
2162
+ scope: this,
2163
+ callSite: (f, a) => f(...a)
2164
+ });
2165
+ }).catch(async () => {
2166
+ var _a;
2167
+ await ((_a = this._onRelease) == null ? void 0 : _a.call(this));
2168
+ });
2169
+ await acquired.wait();
2170
+ log8("recieved lock", {
2171
+ steal
2172
+ }, {
2173
+ file: "browser.ts",
2174
+ line: 81,
2175
+ scope: this,
2176
+ callSite: (f, a) => f(...a)
2177
+ });
2178
+ }
2179
+ };
2180
+ __decorate([
2181
+ logInfo
2182
+ ], Lock.prototype, "lockKey", null);
2183
+ var isLocked = (lockPath) => {
2184
+ throw new Error("Not implemented");
2185
+ };
2186
+
2062
2187
  // packages/sdk/client-services/src/packlets/spaces/data-space.ts
2063
2188
  import { Event as Event5, scheduleTask as scheduleTask4, trackLeaks } from "@dxos/async";
2064
2189
  import { AUTH_TIMEOUT as AUTH_TIMEOUT2 } from "@dxos/client-protocol";
@@ -2067,17 +2192,17 @@ import { timed } from "@dxos/debug";
2067
2192
  import { createMappedFeedWriter } from "@dxos/echo-pipeline";
2068
2193
  import { CancelledError, SystemError } from "@dxos/errors";
2069
2194
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2070
- import { log as log9 } from "@dxos/log";
2195
+ import { log as log10 } from "@dxos/log";
2071
2196
  import { SpaceState } from "@dxos/protocols/proto/dxos/client/services";
2072
2197
  import { AdmittedFeed as AdmittedFeed3 } from "@dxos/protocols/proto/dxos/halo/credentials";
2073
2198
  import { ComplexSet as ComplexSet2 } from "@dxos/util";
2074
2199
 
2075
2200
  // packages/sdk/client-services/src/packlets/spaces/notarization-plugin.ts
2076
2201
  import assert9 from "@dxos/node-std/assert";
2077
- import { DeferredTask, Event as Event4, scheduleTask as scheduleTask3, sleep as sleep2, TimeoutError as TimeoutError2, Trigger as Trigger4 } from "@dxos/async";
2202
+ import { DeferredTask, Event as Event4, scheduleTask as scheduleTask3, sleep as sleep2, TimeoutError as TimeoutError2, Trigger as Trigger5 } from "@dxos/async";
2078
2203
  import { Context as Context5, rejectOnDispose } from "@dxos/context";
2079
2204
  import { PublicKey as PublicKey6 } from "@dxos/keys";
2080
- import { log as log8 } from "@dxos/log";
2205
+ import { log as log9 } from "@dxos/log";
2081
2206
  import { schema as schema3 } from "@dxos/protocols";
2082
2207
  import { RpcExtension as RpcExtension2 } from "@dxos/teleport";
2083
2208
  import { ComplexMap as ComplexMap2, ComplexSet, entry } from "@dxos/util";
@@ -2102,7 +2227,7 @@ var NotarizationPlugin = class {
2102
2227
  * Request credentials to be notarized.
2103
2228
  */
2104
2229
  async notarize({ ctx: opCtx, credentials, timeout = DEFAULT_NOTARIZE_TIMEOUT, retryTimeout = DEFAULT_RETRY_TIMEOUT, successDelay = DEFAULT_SUCCESS_DELAY }) {
2105
- log8("notarize", {
2230
+ log9("notarize", {
2106
2231
  credentials
2107
2232
  }, {
2108
2233
  file: "notarization-plugin.ts",
@@ -2111,10 +2236,10 @@ var NotarizationPlugin = class {
2111
2236
  callSite: (f, a) => f(...a)
2112
2237
  });
2113
2238
  assert9(credentials.every((credential) => credential.id), "Credentials must have an id");
2114
- const errors = new Trigger4();
2239
+ const errors = new Trigger5();
2115
2240
  const ctx = this._ctx.derive({
2116
2241
  onError: (err) => {
2117
- log8.warn("Notarization error", {
2242
+ log9.warn("Notarization error", {
2118
2243
  err
2119
2244
  }, {
2120
2245
  file: "notarization-plugin.ts",
@@ -2129,7 +2254,7 @@ var NotarizationPlugin = class {
2129
2254
  opCtx == null ? void 0 : opCtx.onDispose(() => ctx.dispose());
2130
2255
  if (timeout !== 0) {
2131
2256
  scheduleTask3(ctx, () => {
2132
- log8.warn("Notarization timeout", {
2257
+ log9.warn("Notarization timeout", {
2133
2258
  timeout,
2134
2259
  peers: Array.from(this._extensions).map((extension) => extension.remotePeerId)
2135
2260
  }, {
@@ -2153,7 +2278,7 @@ var NotarizationPlugin = class {
2153
2278
  ...this._extensions
2154
2279
  ].find((peer2) => !peersTried.has(peer2));
2155
2280
  if (!peer) {
2156
- log8.warn("Exhausted all peers to notarize with", {
2281
+ log9.warn("Exhausted all peers to notarize with", {
2157
2282
  retryIn: retryTimeout
2158
2283
  }, {
2159
2284
  file: "notarization-plugin.ts",
@@ -2166,7 +2291,7 @@ var NotarizationPlugin = class {
2166
2291
  return;
2167
2292
  }
2168
2293
  peersTried.add(peer);
2169
- log8("try notarizing", {
2294
+ log9("try notarizing", {
2170
2295
  peer: peer.localPeerId,
2171
2296
  credentialId: credentials.map((credential) => credential.id)
2172
2297
  }, {
@@ -2178,7 +2303,7 @@ var NotarizationPlugin = class {
2178
2303
  await peer.rpc.NotarizationService.notarize({
2179
2304
  credentials: credentials.filter((credential) => !this._processedCredentials.has(credential.id))
2180
2305
  });
2181
- log8("success", {}, {
2306
+ log9("success", {}, {
2182
2307
  file: "notarization-plugin.ts",
2183
2308
  line: 144,
2184
2309
  scope: this,
@@ -2187,7 +2312,7 @@ var NotarizationPlugin = class {
2187
2312
  await sleep2(successDelay);
2188
2313
  } catch (err) {
2189
2314
  if (!ctx.disposed && !err.message.includes(WRITER_NOT_SET_ERROR_CODE)) {
2190
- log8.warn("error notarizing (recoverable)", err, {
2315
+ log9.warn("error notarizing (recoverable)", err, {
2191
2316
  file: "notarization-plugin.ts",
2192
2317
  line: 148,
2193
2318
  scope: this,
@@ -2205,7 +2330,7 @@ var NotarizationPlugin = class {
2205
2330
  allNotarized,
2206
2331
  errors.wait()
2207
2332
  ]);
2208
- log8("done", {}, {
2333
+ log9("done", {}, {
2209
2334
  file: "notarization-plugin.ts",
2210
2335
  line: 159,
2211
2336
  scope: this,
@@ -2235,7 +2360,7 @@ var NotarizationPlugin = class {
2235
2360
  if (this._processedCredentials.has(id)) {
2236
2361
  return;
2237
2362
  }
2238
- await entry(this._processCredentialsTriggers, id).orInsert(new Trigger4()).value.wait();
2363
+ await entry(this._processCredentialsTriggers, id).orInsert(new Trigger5()).value.wait();
2239
2364
  }
2240
2365
  /**
2241
2366
  * Requests from other peers to notarize credentials.
@@ -2256,7 +2381,7 @@ var NotarizationPlugin = class {
2256
2381
  createExtension() {
2257
2382
  const extension = new NotarizationTeleportExtension({
2258
2383
  onOpen: async () => {
2259
- log8("extension opened", {
2384
+ log9("extension opened", {
2260
2385
  peer: extension.localPeerId
2261
2386
  }, {
2262
2387
  file: "notarization-plugin.ts",
@@ -2268,7 +2393,7 @@ var NotarizationPlugin = class {
2268
2393
  this._extensionOpened.emit();
2269
2394
  },
2270
2395
  onClose: async () => {
2271
- log8("extension closed", {
2396
+ log9("extension closed", {
2272
2397
  peer: extension.localPeerId
2273
2398
  }, {
2274
2399
  file: "notarization-plugin.ts",
@@ -2315,7 +2440,7 @@ var NotarizationTeleportExtension = class extends RpcExtension2 {
2315
2440
  };
2316
2441
 
2317
2442
  // packages/sdk/client-services/src/packlets/spaces/data-space.ts
2318
- var __decorate = function(decorators, target, key, desc) {
2443
+ var __decorate2 = function(decorators, target, key, desc) {
2319
2444
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2320
2445
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2321
2446
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2410,7 +2535,7 @@ var DataSpace = class DataSpace2 {
2410
2535
  await this.initializeDataPipeline();
2411
2536
  } catch (err) {
2412
2537
  if (err instanceof CancelledError) {
2413
- log9("Data pipeline initialization cancelled", err, {
2538
+ log10("Data pipeline initialization cancelled", err, {
2414
2539
  file: "data-space.ts",
2415
2540
  line: 172,
2416
2541
  scope: this,
@@ -2418,7 +2543,7 @@ var DataSpace = class DataSpace2 {
2418
2543
  });
2419
2544
  return;
2420
2545
  }
2421
- log9.error("Error initializing data pipeline", err, {
2546
+ log10.error("Error initializing data pipeline", err, {
2422
2547
  file: "data-space.ts",
2423
2548
  line: 176,
2424
2549
  scope: this,
@@ -2444,7 +2569,7 @@ var DataSpace = class DataSpace2 {
2444
2569
  });
2445
2570
  this.metrics.controlPipelineReady = new Date();
2446
2571
  await this._createWritableFeeds();
2447
- log9("Writable feeds created", {}, {
2572
+ log10("Writable feeds created", {}, {
2448
2573
  file: "data-space.ts",
2449
2574
  line: 201,
2450
2575
  scope: this,
@@ -2459,7 +2584,7 @@ var DataSpace = class DataSpace2 {
2459
2584
  await this._inner.initializeDataPipeline();
2460
2585
  this.metrics.dataPipelineOpen = new Date();
2461
2586
  await cancelWithContext2(this._ctx, this._inner.dataPipeline.ensureEpochInitialized());
2462
- log9("waiting for data pipeline to reach target timeframe", {}, {
2587
+ log10("waiting for data pipeline to reach target timeframe", {}, {
2463
2588
  file: "data-space.ts",
2464
2589
  line: 220,
2465
2590
  scope: this,
@@ -2470,7 +2595,7 @@ var DataSpace = class DataSpace2 {
2470
2595
  breakOnStall: false
2471
2596
  });
2472
2597
  this.metrics.dataPipelineReady = new Date();
2473
- log9("data pipeline ready", {}, {
2598
+ log10("data pipeline ready", {}, {
2474
2599
  file: "data-space.ts",
2475
2600
  line: 229,
2476
2601
  scope: this,
@@ -2546,10 +2671,10 @@ var DataSpace = class DataSpace2 {
2546
2671
  }
2547
2672
  }
2548
2673
  };
2549
- __decorate([
2674
+ __decorate2([
2550
2675
  timed(1e4)
2551
2676
  ], DataSpace.prototype, "_createWritableFeeds", null);
2552
- DataSpace = __decorate([
2677
+ DataSpace = __decorate2([
2553
2678
  trackLeaks("open", "close")
2554
2679
  ], DataSpace);
2555
2680
 
@@ -2559,7 +2684,7 @@ import { Event as Event6, synchronized, trackLeaks as trackLeaks2 } from "@dxos/
2559
2684
  import { cancelWithContext as cancelWithContext3, Context as Context7 } from "@dxos/context";
2560
2685
  import { getCredentialAssertion as getCredentialAssertion2 } from "@dxos/credentials";
2561
2686
  import { PublicKey as PublicKey8 } from "@dxos/keys";
2562
- import { log as log10 } from "@dxos/log";
2687
+ import { log as log11 } from "@dxos/log";
2563
2688
  import { trace as trace4 } from "@dxos/protocols";
2564
2689
  import { SpaceState as SpaceState2 } from "@dxos/protocols/proto/dxos/client/services";
2565
2690
  import { Gossip, Presence } from "@dxos/teleport-extension-gossip";
@@ -2636,7 +2761,7 @@ var spaceGenesis = async (keyring, signingContext, space) => {
2636
2761
  };
2637
2762
 
2638
2763
  // packages/sdk/client-services/src/packlets/spaces/data-space-manager.ts
2639
- var __decorate2 = function(decorators, target, key, desc) {
2764
+ var __decorate3 = function(decorators, target, key, desc) {
2640
2765
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2641
2766
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2642
2767
  r = Reflect.decorate(decorators, target, key, desc);
@@ -2665,13 +2790,13 @@ var DataSpaceManager = class DataSpaceManager2 {
2665
2790
  return this._spaces;
2666
2791
  }
2667
2792
  async open() {
2668
- log10("open", {}, {
2793
+ log11("open", {}, {
2669
2794
  file: "data-space-manager.ts",
2670
2795
  line: 80,
2671
2796
  scope: this,
2672
2797
  callSite: (f, a) => f(...a)
2673
2798
  });
2674
- log10.trace("dxos.echo.data-space-manager.open", trace4.begin({
2799
+ log11.trace("dxos.echo.data-space-manager.open", trace4.begin({
2675
2800
  id: this._instanceId
2676
2801
  }), {
2677
2802
  file: "data-space-manager.ts",
@@ -2680,7 +2805,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2680
2805
  callSite: (f, a) => f(...a)
2681
2806
  });
2682
2807
  await this._metadataStore.load();
2683
- log10("metadata loaded", {
2808
+ log11("metadata loaded", {
2684
2809
  spaces: this._metadataStore.spaces.length
2685
2810
  }, {
2686
2811
  file: "data-space-manager.ts",
@@ -2690,7 +2815,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2690
2815
  });
2691
2816
  for (const spaceMetadata of this._metadataStore.spaces) {
2692
2817
  try {
2693
- log10("load space", {
2818
+ log11("load space", {
2694
2819
  spaceMetadata
2695
2820
  }, {
2696
2821
  file: "data-space-manager.ts",
@@ -2701,7 +2826,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2701
2826
  const space = await this._constructSpace(spaceMetadata);
2702
2827
  space.initializeDataPipelineAsync();
2703
2828
  } catch (err) {
2704
- log10.error("Error loading space", {
2829
+ log11.error("Error loading space", {
2705
2830
  spaceMetadata,
2706
2831
  err
2707
2832
  }, {
@@ -2714,7 +2839,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2714
2839
  }
2715
2840
  this._isOpen = true;
2716
2841
  this.updated.emit();
2717
- log10.trace("dxos.echo.data-space-manager.open", trace4.end({
2842
+ log11.trace("dxos.echo.data-space-manager.open", trace4.end({
2718
2843
  id: this._instanceId
2719
2844
  }), {
2720
2845
  file: "data-space-manager.ts",
@@ -2724,7 +2849,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2724
2849
  });
2725
2850
  }
2726
2851
  async close() {
2727
- log10("close", {}, {
2852
+ log11("close", {}, {
2728
2853
  file: "data-space-manager.ts",
2729
2854
  line: 102,
2730
2855
  scope: this,
@@ -2750,7 +2875,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2750
2875
  controlFeedKey,
2751
2876
  dataFeedKey
2752
2877
  };
2753
- log10("creating space...", {
2878
+ log11("creating space...", {
2754
2879
  spaceKey
2755
2880
  }, {
2756
2881
  file: "data-space-manager.ts",
@@ -2770,7 +2895,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2770
2895
  }
2771
2896
  // TODO(burdon): Rename join space.
2772
2897
  async acceptSpace(opts) {
2773
- log10("accept space", {
2898
+ log11("accept space", {
2774
2899
  opts
2775
2900
  }, {
2776
2901
  file: "data-space-manager.ts",
@@ -2804,7 +2929,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2804
2929
  }));
2805
2930
  }
2806
2931
  async _constructSpace(metadata) {
2807
- log10("construct space", {
2932
+ log11("construct space", {
2808
2933
  metadata
2809
2934
  }, {
2810
2935
  file: "data-space-manager.ts",
@@ -2842,7 +2967,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2842
2967
  session.addExtension("dxos.mesh.teleport.notarization", dataSpace.notarizationPlugin.createExtension());
2843
2968
  },
2844
2969
  onAuthFailure: () => {
2845
- log10.warn("auth failure", {}, {
2970
+ log11.warn("auth failure", {}, {
2846
2971
  file: "data-space-manager.ts",
2847
2972
  line: 212,
2848
2973
  scope: this,
@@ -2863,7 +2988,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2863
2988
  signingContext: this._signingContext,
2864
2989
  callbacks: {
2865
2990
  beforeReady: async () => {
2866
- log10("before space ready", {
2991
+ log11("before space ready", {
2867
2992
  space: space.key
2868
2993
  }, {
2869
2994
  file: "data-space-manager.ts",
@@ -2874,7 +2999,7 @@ var DataSpaceManager = class DataSpaceManager2 {
2874
2999
  this._dataServiceSubscriptions.registerSpace(space.key, dataSpace.dataPipeline.databaseHost.createDataServiceHost());
2875
3000
  },
2876
3001
  afterReady: async () => {
2877
- log10("after space ready", {
3002
+ log11("after space ready", {
2878
3003
  space: space.key,
2879
3004
  open: this._isOpen
2880
3005
  }, {
@@ -2901,19 +3026,19 @@ var DataSpaceManager = class DataSpaceManager2 {
2901
3026
  return dataSpace;
2902
3027
  }
2903
3028
  };
2904
- __decorate2([
3029
+ __decorate3([
2905
3030
  synchronized
2906
3031
  ], DataSpaceManager.prototype, "open", null);
2907
- __decorate2([
3032
+ __decorate3([
2908
3033
  synchronized
2909
3034
  ], DataSpaceManager.prototype, "close", null);
2910
- __decorate2([
3035
+ __decorate3([
2911
3036
  synchronized
2912
3037
  ], DataSpaceManager.prototype, "createSpace", null);
2913
- __decorate2([
3038
+ __decorate3([
2914
3039
  synchronized
2915
3040
  ], DataSpaceManager.prototype, "acceptSpace", null);
2916
- DataSpaceManager = __decorate2([
3041
+ DataSpaceManager = __decorate3([
2917
3042
  trackLeaks2("open", "close")
2918
3043
  ], DataSpaceManager);
2919
3044
 
@@ -2922,7 +3047,7 @@ import { EventSubscriptions as EventSubscriptions2, scheduleTask as scheduleTask
2922
3047
  import { Stream as Stream9 } from "@dxos/codec-protobuf";
2923
3048
  import { raise, todo as todo2 } from "@dxos/debug";
2924
3049
  import { SpaceNotFoundError } from "@dxos/echo-pipeline";
2925
- import { log as log11 } from "@dxos/log";
3050
+ import { log as log12 } from "@dxos/log";
2926
3051
  import { encodeError } from "@dxos/protocols";
2927
3052
  import { SpaceMember as SpaceMember2 } from "@dxos/protocols/proto/dxos/client/services";
2928
3053
  import { humanize } from "@dxos/util";
@@ -2950,7 +3075,7 @@ var SpacesServiceImpl = class {
2950
3075
  const onUpdate = async () => {
2951
3076
  const dataSpaceManager = await this._getDataSpaceManager();
2952
3077
  const spaces = Array.from(dataSpaceManager.spaces.values()).map((space) => this._transformSpace(space));
2953
- log11("update", {
3078
+ log12("update", {
2954
3079
  spaces
2955
3080
  }, {
2956
3081
  file: "spaces-service.ts",
@@ -3122,906 +3247,130 @@ var toStorageType = (type) => {
3122
3247
  }
3123
3248
  };
3124
3249
 
3125
- // packages/sdk/client-services/src/packlets/vault/shell-runtime.ts
3250
+ // packages/sdk/client-services/src/packlets/services/service-context.ts
3126
3251
  import assert11 from "@dxos/node-std/assert";
3127
- import { Event as Event7 } from "@dxos/async";
3128
- import { appServiceBundle, shellServiceBundle } from "@dxos/client-protocol";
3129
- import { ShellLayout } from "@dxos/protocols/proto/dxos/iframe";
3130
- import { createProtoRpcPeer } from "@dxos/rpc";
3131
- var ShellRuntimeImpl = class {
3132
- constructor(_port) {
3133
- this._port = _port;
3134
- this.layoutUpdate = new Event7();
3135
- this._layout = ShellLayout.DEFAULT;
3136
- }
3137
- get layout() {
3138
- return this._layout;
3139
- }
3140
- get invitationCode() {
3141
- return this._invitationCode;
3142
- }
3143
- get spaceKey() {
3144
- return this._spaceKey;
3145
- }
3146
- setLayout(layout, options = {}) {
3147
- this._layout = layout;
3148
- this._invitationCode = options.invitationCode;
3149
- this._spaceKey = options.spaceKey;
3150
- this.layoutUpdate.emit({
3151
- layout,
3152
- ...options
3252
+ import { Trigger as Trigger6 } from "@dxos/async";
3253
+ import { getCredentialAssertion as getCredentialAssertion3 } from "@dxos/credentials";
3254
+ import { failUndefined as failUndefined3 } from "@dxos/debug";
3255
+ import { valueEncoding, MetadataStore, SpaceManager, DataServiceSubscriptions, SnapshotStore } from "@dxos/echo-pipeline";
3256
+ import { FeedFactory, FeedStore } from "@dxos/feed-store";
3257
+ import { Keyring } from "@dxos/keyring";
3258
+ import { PublicKey as PublicKey9 } from "@dxos/keys";
3259
+ import { log as log13 } from "@dxos/log";
3260
+ import { STORAGE_VERSION, trace as trace5 } from "@dxos/protocols";
3261
+ import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
3262
+ var ServiceContext = class {
3263
+ // prettier-ignore
3264
+ constructor(storage, networkManager, signalManager, modelFactory) {
3265
+ this.storage = storage;
3266
+ this.networkManager = networkManager;
3267
+ this.signalManager = signalManager;
3268
+ this.modelFactory = modelFactory;
3269
+ this.initialized = new Trigger6();
3270
+ this.dataServiceSubscriptions = new DataServiceSubscriptions();
3271
+ this._handlerFactories = /* @__PURE__ */ new Map();
3272
+ this._instanceId = PublicKey9.random().toHex();
3273
+ this.metadataStore = new MetadataStore(storage.createDirectory("metadata"));
3274
+ this.snapshotStore = new SnapshotStore(storage.createDirectory("snapshots"));
3275
+ this.keyring = new Keyring(storage.createDirectory("keyring"));
3276
+ this.feedStore = new FeedStore({
3277
+ factory: new FeedFactory({
3278
+ root: storage.createDirectory("feeds"),
3279
+ signer: this.keyring,
3280
+ hypercore: {
3281
+ valueEncoding
3282
+ }
3283
+ })
3153
3284
  });
3154
- }
3155
- async setAppContext(context) {
3156
- assert11(this._appRpc, "runtime not open");
3157
- await this._appRpc.rpc.AppService.setContext(context);
3285
+ this.spaceManager = new SpaceManager({
3286
+ feedStore: this.feedStore,
3287
+ networkManager: this.networkManager,
3288
+ metadataStore: this.metadataStore,
3289
+ modelFactory: this.modelFactory,
3290
+ snapshotStore: this.snapshotStore
3291
+ });
3292
+ this.identityManager = new IdentityManager(this.metadataStore, this.keyring, this.feedStore, this.spaceManager);
3293
+ this.invitations = new InvitationsHandler(this.networkManager);
3294
+ this._handlerFactories.set(Invitation5.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => {
3295
+ var _a;
3296
+ return (_a = this.identityManager.identity) != null ? _a : failUndefined3();
3297
+ }, this._acceptIdentity.bind(this)));
3158
3298
  }
3159
3299
  async open() {
3160
- this._appRpc = createProtoRpcPeer({
3161
- requested: appServiceBundle,
3162
- exposed: shellServiceBundle,
3163
- handlers: {
3164
- ShellService: {
3165
- setLayout: async (request) => {
3166
- this._layout = request.layout;
3167
- this._invitationCode = request.invitationCode;
3168
- this._spaceKey = request.spaceKey;
3169
- this.layoutUpdate.emit(request);
3170
- }
3171
- }
3172
- },
3173
- port: this._port
3300
+ log13.trace("dxos.sdk.service-context.open", trace5.begin({
3301
+ id: this._instanceId
3302
+ }), {
3303
+ file: "service-context.ts",
3304
+ line: 119,
3305
+ scope: this,
3306
+ callSite: (f, a) => f(...a)
3307
+ });
3308
+ await this._checkStorageVersion();
3309
+ log13("opening...", {}, {
3310
+ file: "service-context.ts",
3311
+ line: 123,
3312
+ scope: this,
3313
+ callSite: (f, a) => f(...a)
3314
+ });
3315
+ await this.signalManager.open();
3316
+ await this.networkManager.open();
3317
+ await this.spaceManager.open();
3318
+ await this.identityManager.open();
3319
+ if (this.identityManager.identity) {
3320
+ await this._initialize();
3321
+ }
3322
+ log13("opened", {}, {
3323
+ file: "service-context.ts",
3324
+ line: 131,
3325
+ scope: this,
3326
+ callSite: (f, a) => f(...a)
3327
+ });
3328
+ log13.trace("dxos.sdk.service-context.open", trace5.end({
3329
+ id: this._instanceId
3330
+ }), {
3331
+ file: "service-context.ts",
3332
+ line: 132,
3333
+ scope: this,
3334
+ callSite: (f, a) => f(...a)
3174
3335
  });
3175
- await this._appRpc.open();
3176
3336
  }
3177
3337
  async close() {
3178
- var _a;
3179
- await ((_a = this._appRpc) == null ? void 0 : _a.close());
3180
- this._appRpc = void 0;
3181
- }
3182
- };
3183
- var MemoryShellRuntime = class {
3184
- constructor({ layout, invitationCode, spaceKey } = {}) {
3185
- this.layoutUpdate = new Event7();
3186
- this.contextUpdate = new Event7();
3187
- this._layout = layout != null ? layout : ShellLayout.DEFAULT;
3188
- this._invitationCode = invitationCode;
3189
- this._spaceKey = spaceKey;
3190
- }
3191
- get layout() {
3192
- return this._layout;
3193
- }
3194
- get invitationCode() {
3195
- return this._invitationCode;
3196
- }
3197
- get spaceKey() {
3198
- return this._spaceKey;
3199
- }
3200
- setLayout(layout, options = {}) {
3201
- this._layout = layout;
3202
- this._invitationCode = options.invitationCode;
3203
- this._spaceKey = options.spaceKey;
3204
- this.layoutUpdate.emit({
3205
- layout,
3206
- ...options
3338
+ var _a, _b;
3339
+ log13("closing...", {}, {
3340
+ file: "service-context.ts",
3341
+ line: 136,
3342
+ scope: this,
3343
+ callSite: (f, a) => f(...a)
3344
+ });
3345
+ await ((_a = this._deviceSpaceSync) == null ? void 0 : _a.close());
3346
+ await ((_b = this.dataSpaceManager) == null ? void 0 : _b.close());
3347
+ await this.identityManager.close();
3348
+ await this.spaceManager.close();
3349
+ await this.feedStore.close();
3350
+ await this.networkManager.close();
3351
+ await this.signalManager.close();
3352
+ this.dataServiceSubscriptions.clear();
3353
+ log13("closed", {}, {
3354
+ file: "service-context.ts",
3355
+ line: 145,
3356
+ scope: this,
3357
+ callSite: (f, a) => f(...a)
3207
3358
  });
3208
3359
  }
3209
- async setAppContext(context) {
3210
- this.contextUpdate.emit(context);
3360
+ async createIdentity(params = {}) {
3361
+ const identity = await this.identityManager.createIdentity(params);
3362
+ await this._initialize();
3363
+ return identity;
3211
3364
  }
3212
- };
3213
-
3214
- // packages/sdk/client-services/src/packlets/vault/iframe-host-runtime.ts
3215
- import { Trigger as Trigger10 } from "@dxos/async";
3216
- import { PROXY_CONNECTION_TIMEOUT as PROXY_CONNECTION_TIMEOUT2 } from "@dxos/client-protocol";
3217
- import { log as log19, logInfo as logInfo3 } from "@dxos/log";
3218
- import { MemorySignalManager as MemorySignalManager2, MemorySignalManagerContext as MemorySignalManagerContext2, WebsocketSignalManager as WebsocketSignalManager3 } from "@dxos/messaging";
3219
- import { createWebRTCTransportFactory as createWebRTCTransportFactory2 } from "@dxos/network-manager";
3220
- import { getAsyncValue as getAsyncValue2 } from "@dxos/util";
3221
-
3222
- // packages/sdk/client-services/src/packlets/services/service-host.ts
3223
- import assert14 from "@dxos/node-std/assert";
3224
- import { Event as Event9 } from "@dxos/async";
3225
- import { clientServiceBundle, createDefaultModelFactory } from "@dxos/client-protocol";
3226
- import { DataServiceImpl } from "@dxos/echo-pipeline";
3227
- import { PublicKey as PublicKey10 } from "@dxos/keys";
3228
- import { log as log18 } from "@dxos/log";
3229
- import { WebsocketSignalManager as WebsocketSignalManager2 } from "@dxos/messaging";
3230
- import { createWebRTCTransportFactory, NetworkManager } from "@dxos/network-manager";
3231
- import { trace as trace6 } from "@dxos/protocols";
3232
- import { SystemStatus } from "@dxos/protocols/proto/dxos/client/services";
3233
-
3234
- // packages/sdk/client-services/src/packlets/devices/devices-service.ts
3235
- import { EventSubscriptions as EventSubscriptions3 } from "@dxos/async";
3236
- import { Stream as Stream10 } from "@dxos/codec-protobuf";
3237
- import { DeviceKind } from "@dxos/protocols/proto/dxos/client/services";
3238
- var DevicesServiceImpl = class {
3239
- constructor(_identityManager) {
3240
- this._identityManager = _identityManager;
3365
+ getInvitationHandler(invitation) {
3366
+ const factory = this._handlerFactories.get(invitation.kind);
3367
+ assert11(factory, `Unknown invitation kind: ${invitation.kind}`);
3368
+ return factory(invitation);
3241
3369
  }
3242
- updateDevice(request) {
3243
- throw new Error("Method not implemented.");
3244
- }
3245
- queryDevices() {
3246
- return new Stream10(({ next }) => {
3247
- const update = () => {
3248
- var _a;
3249
- const deviceKeys = (_a = this._identityManager.identity) == null ? void 0 : _a.authorizedDeviceKeys;
3250
- if (!deviceKeys) {
3251
- next({
3252
- devices: []
3253
- });
3254
- } else {
3255
- next({
3256
- devices: Array.from(deviceKeys.values()).map((key) => {
3257
- var _a2;
3258
- return {
3259
- deviceKey: key,
3260
- kind: ((_a2 = this._identityManager.identity) == null ? void 0 : _a2.deviceKey.equals(key)) ? DeviceKind.CURRENT : DeviceKind.TRUSTED
3261
- };
3262
- })
3263
- });
3264
- }
3265
- };
3266
- const subscriptions = new EventSubscriptions3();
3267
- subscriptions.add(this._identityManager.stateUpdate.on(() => {
3268
- update();
3269
- if (this._identityManager.identity) {
3270
- subscriptions.add(this._identityManager.identity.stateUpdate.on(() => {
3271
- update();
3272
- }));
3273
- }
3274
- }));
3275
- update();
3276
- return () => subscriptions.clear();
3277
- });
3278
- }
3279
- };
3280
-
3281
- // packages/sdk/client-services/src/packlets/logging/logging-service.ts
3282
- import { Event as Event8 } from "@dxos/async";
3283
- import { Stream as Stream11 } from "@dxos/codec-protobuf";
3284
- import { log as log12, shouldLog, getContextFromEntry } from "@dxos/log";
3285
- import { jsonify } from "@dxos/util";
3286
- var LoggingServiceImpl = class {
3287
- constructor() {
3288
- this._logs = new Event8();
3289
- this._logProcessor = (_config, entry2) => {
3290
- this._logs.emit(entry2);
3291
- };
3292
- }
3293
- async open() {
3294
- log12.runtimeConfig.processors.push(this._logProcessor);
3295
- }
3296
- async close() {
3297
- const index = log12.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
3298
- log12.runtimeConfig.processors.splice(index, 1);
3299
- }
3300
- queryLogs(request) {
3301
- var _a;
3302
- const filters = ((_a = request.filters) == null ? void 0 : _a.length) ? request.filters : void 0;
3303
- return new Stream11(({ ctx, next }) => {
3304
- const handler = (entry2) => {
3305
- var _a2, _b, _c;
3306
- if (((_a2 = entry2.meta) == null ? void 0 : _a2.file.includes("logging-service")) || entry2.context && Object.values(entry2.context).some((value) => typeof value === "string" && value.includes("LoggingService"))) {
3307
- return;
3308
- }
3309
- if (shouldLog(entry2, filters)) {
3310
- const record = {
3311
- ...entry2,
3312
- context: jsonify(getContextFromEntry(entry2))
3313
- };
3314
- (_b = record.meta) == null ? true : delete _b.bugcheck;
3315
- (_c = record.meta) == null ? true : delete _c.scope;
3316
- next(record);
3317
- }
3318
- };
3319
- ctx.onDispose(() => {
3320
- this._logs.off(handler);
3321
- });
3322
- this._logs.on(handler);
3323
- });
3324
- }
3325
- };
3326
-
3327
- // packages/sdk/client-services/src/packlets/network/network-service.ts
3328
- import { Stream as Stream12 } from "@dxos/codec-protobuf";
3329
- var NetworkServiceImpl = class {
3330
- constructor(networkManager, signalManager) {
3331
- this.networkManager = networkManager;
3332
- this.signalManager = signalManager;
3333
- }
3334
- queryStatus() {
3335
- return new Stream12(({ next }) => {
3336
- const update = () => {
3337
- next({
3338
- swarm: this.networkManager.connectionState,
3339
- signaling: this.signalManager.getStatus().map(({ host, state }) => ({
3340
- server: host,
3341
- state
3342
- }))
3343
- });
3344
- };
3345
- const unsubscribeSwarm = this.networkManager.connectionStateChanged.on(() => update());
3346
- const unsubscribeSignal = this.signalManager.statusChanged.on(() => update());
3347
- update();
3348
- return () => {
3349
- unsubscribeSwarm();
3350
- unsubscribeSignal();
3351
- };
3352
- });
3353
- }
3354
- async updateConfig(request) {
3355
- await this.networkManager.setConnectionState(request.swarm);
3356
- }
3357
- };
3358
-
3359
- // packages/sdk/client-services/src/packlets/system/system-service.ts
3360
- import { Stream as Stream13 } from "@dxos/codec-protobuf";
3361
- var SystemServiceImpl = class {
3362
- constructor({ config, statusUpdate, onUpdateStatus, getCurrentStatus, onReset }) {
3363
- this._config = config;
3364
- this._statusUpdate = statusUpdate;
3365
- this._getCurrentStatus = getCurrentStatus;
3366
- this._onUpdateStatus = onUpdateStatus;
3367
- this._onReset = onReset;
3368
- }
3369
- async getConfig() {
3370
- var _a, _b;
3371
- return (_b = (_a = this._config) == null ? void 0 : _a.values) != null ? _b : {};
3372
- }
3373
- async updateStatus({ status }) {
3374
- await this._onUpdateStatus(status);
3375
- }
3376
- queryStatus() {
3377
- return new Stream13(({ next }) => {
3378
- const update = () => {
3379
- next({
3380
- status: this._getCurrentStatus()
3381
- });
3382
- };
3383
- update();
3384
- const unsubscribe = this._statusUpdate.on(() => update());
3385
- const interval = setInterval(update, 3e3);
3386
- return () => {
3387
- clearInterval(interval);
3388
- unsubscribe();
3389
- };
3390
- });
3391
- }
3392
- async reset() {
3393
- await this._onReset();
3394
- }
3395
- };
3396
-
3397
- // packages/sdk/client-services/src/packlets/vault/iframe-proxy-runtime.ts
3398
- import { Trigger as Trigger5 } from "@dxos/async";
3399
- import { iframeServiceBundle, workerServiceBundle } from "@dxos/client-protocol";
3400
- import { RemoteServiceConnectionError } from "@dxos/errors";
3401
- import { log as log13 } from "@dxos/log";
3402
- import { WebRTCTransportService } from "@dxos/network-manager";
3403
- import { createProtoRpcPeer as createProtoRpcPeer2 } from "@dxos/rpc";
3404
- import { getAsyncValue } from "@dxos/util";
3405
- var IFrameProxyRuntime = class {
3406
- constructor({ config, systemPort, shellPort }) {
3407
- this._id = String(Math.floor(Math.random() * 1e6));
3408
- this._release = new Trigger5();
3409
- this._configProvider = config;
3410
- this._systemPort = systemPort;
3411
- this._shellPort = shellPort;
3412
- if (this._shellPort) {
3413
- this._shellRuntime = new ShellRuntimeImpl(this._shellPort);
3414
- }
3415
- }
3416
- get shell() {
3417
- return this._shellRuntime;
3418
- }
3419
- async open(origin) {
3420
- var _a;
3421
- this._config = await getAsyncValue(this._configProvider);
3422
- this._transportService = new WebRTCTransportService({
3423
- iceServers: this._config.get("runtime.services.ice")
3424
- });
3425
- this._systemRpc = createProtoRpcPeer2({
3426
- requested: workerServiceBundle,
3427
- exposed: iframeServiceBundle,
3428
- handlers: {
3429
- BridgeService: this._transportService
3430
- },
3431
- port: this._systemPort,
3432
- timeout: 200
3433
- });
3434
- let lockKey;
3435
- if (typeof navigator !== "undefined") {
3436
- lockKey = this._lockKey(origin);
3437
- this._release = new Trigger5();
3438
- const ready = new Trigger5();
3439
- void navigator.locks.request(lockKey, async () => {
3440
- ready.wake();
3441
- await this._release.wait();
3442
- });
3443
- await ready.wait();
3444
- }
3445
- try {
3446
- await this._systemRpc.open();
3447
- await this._systemRpc.rpc.WorkerService.start({
3448
- origin,
3449
- lockKey
3450
- });
3451
- } catch (err) {
3452
- log13.catch(err, {}, {
3453
- file: "iframe-proxy-runtime.ts",
3454
- line: 85,
3455
- scope: this,
3456
- callSite: (f, a) => f(...a)
3457
- });
3458
- throw new RemoteServiceConnectionError("Failed to connect to worker");
3459
- }
3460
- await ((_a = this._shellRuntime) == null ? void 0 : _a.open());
3461
- }
3462
- async close() {
3463
- var _a;
3464
- this._release.wake();
3465
- await ((_a = this._shellRuntime) == null ? void 0 : _a.close());
3466
- await this._systemRpc.rpc.WorkerService.stop();
3467
- await this._systemRpc.close();
3468
- }
3469
- _lockKey(origin) {
3470
- return `${origin}-${this._id}`;
3471
- }
3472
- };
3473
-
3474
- // packages/sdk/client-services/src/packlets/vault/vault-resource-lock.ts
3475
- import { asyncTimeout, Trigger as Trigger6 } from "@dxos/async";
3476
- import { RESOURCE_LOCK_TIMEOUT } from "@dxos/client-protocol";
3477
- import { log as log14, logInfo } from "@dxos/log";
3478
- var __decorate3 = function(decorators, target, key, desc) {
3479
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3480
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3481
- r = Reflect.decorate(decorators, target, key, desc);
3482
- else
3483
- for (var i = decorators.length - 1; i >= 0; i--)
3484
- if (d = decorators[i])
3485
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3486
- return c > 3 && r && Object.defineProperty(target, key, r), r;
3487
- };
3488
- var Message;
3489
- (function(Message2) {
3490
- Message2["ACQUIRING"] = "acquiring";
3491
- })(Message || (Message = {}));
3492
- var VaultResourceLock = class {
3493
- constructor({ lockKey, onAcquire, onRelease }) {
3494
- this._broadcastChannel = new BroadcastChannel("vault-resource-lock");
3495
- this._releaseTrigger = new Trigger6();
3496
- this._lockKey = lockKey;
3497
- this._onAcquire = onAcquire;
3498
- this._onRelease = onRelease;
3499
- this._broadcastChannel.onmessage = this._onMessage.bind(this);
3500
- }
3501
- get lockKey() {
3502
- return this._lockKey;
3503
- }
3504
- async acquire() {
3505
- this._broadcastChannel.postMessage({
3506
- message: Message.ACQUIRING
3507
- });
3508
- try {
3509
- log14("aquiring lock...", {}, {
3510
- file: "vault-resource-lock.ts",
3511
- line: 46,
3512
- scope: this,
3513
- callSite: (f, a) => f(...a)
3514
- });
3515
- await asyncTimeout(this._requestLock(), RESOURCE_LOCK_TIMEOUT);
3516
- log14("acquired lock", {}, {
3517
- file: "vault-resource-lock.ts",
3518
- line: 48,
3519
- scope: this,
3520
- callSite: (f, a) => f(...a)
3521
- });
3522
- } catch (e) {
3523
- log14("stealing lock...", {}, {
3524
- file: "vault-resource-lock.ts",
3525
- line: 50,
3526
- scope: this,
3527
- callSite: (f, a) => f(...a)
3528
- });
3529
- await this._requestLock(true);
3530
- log14("stolen lock", {}, {
3531
- file: "vault-resource-lock.ts",
3532
- line: 52,
3533
- scope: this,
3534
- callSite: (f, a) => f(...a)
3535
- });
3536
- }
3537
- }
3538
- async release() {
3539
- this._releaseTrigger.wake();
3540
- }
3541
- _onMessage(event) {
3542
- if (event.data.message === Message.ACQUIRING) {
3543
- this._releaseTrigger.wake();
3544
- }
3545
- }
3546
- async _requestLock(steal = false) {
3547
- log14("requesting lock...", {
3548
- steal
3549
- }, {
3550
- file: "vault-resource-lock.ts",
3551
- line: 67,
3552
- scope: this,
3553
- callSite: (f, a) => f(...a)
3554
- });
3555
- const acquired = new Trigger6();
3556
- void navigator.locks.request(this._lockKey, {
3557
- steal
3558
- }, async () => {
3559
- var _a, _b;
3560
- await ((_a = this._onAcquire) == null ? void 0 : _a.call(this));
3561
- acquired.wake();
3562
- this._releaseTrigger = new Trigger6();
3563
- await this._releaseTrigger.wait();
3564
- log14("releasing lock...", {}, {
3565
- file: "vault-resource-lock.ts",
3566
- line: 76,
3567
- scope: this,
3568
- callSite: (f, a) => f(...a)
3569
- });
3570
- await ((_b = this._onRelease) == null ? void 0 : _b.call(this));
3571
- log14("released lock", {}, {
3572
- file: "vault-resource-lock.ts",
3573
- line: 78,
3574
- scope: this,
3575
- callSite: (f, a) => f(...a)
3576
- });
3577
- }).catch(async () => {
3578
- var _a;
3579
- await ((_a = this._onRelease) == null ? void 0 : _a.call(this));
3580
- });
3581
- await acquired.wait();
3582
- log14("recieved lock", {
3583
- steal
3584
- }, {
3585
- file: "vault-resource-lock.ts",
3586
- line: 85,
3587
- scope: this,
3588
- callSite: (f, a) => f(...a)
3589
- });
3590
- }
3591
- };
3592
- __decorate3([
3593
- logInfo
3594
- ], VaultResourceLock.prototype, "lockKey", null);
3595
-
3596
- // packages/sdk/client-services/src/packlets/vault/worker-runtime.ts
3597
- import { Trigger as Trigger8 } from "@dxos/async";
3598
- import { log as log16 } from "@dxos/log";
3599
- import { MemorySignalManager, MemorySignalManagerContext, WebsocketSignalManager } from "@dxos/messaging";
3600
- import { WebRTCTransportProxyFactory } from "@dxos/network-manager";
3601
-
3602
- // packages/sdk/client-services/src/packlets/vault/worker-session.ts
3603
- import assert12 from "@dxos/node-std/assert";
3604
- import { asyncTimeout as asyncTimeout2, Trigger as Trigger7 } from "@dxos/async";
3605
- import { iframeServiceBundle as iframeServiceBundle2, PROXY_CONNECTION_TIMEOUT, workerServiceBundle as workerServiceBundle2 } from "@dxos/client-protocol";
3606
- import { log as log15, logInfo as logInfo2 } from "@dxos/log";
3607
- import { createProtoRpcPeer as createProtoRpcPeer3 } from "@dxos/rpc";
3608
- import { Callback } from "@dxos/util";
3609
-
3610
- // packages/sdk/client-services/src/packlets/services/client-rpc-server.ts
3611
- import { Stream as Stream14 } from "@dxos/codec-protobuf";
3612
- import { raise as raise2 } from "@dxos/debug";
3613
- import { parseMethodName, RpcPeer } from "@dxos/rpc";
3614
- var ClientRpcServer = class {
3615
- constructor(params) {
3616
- this._handlerCache = /* @__PURE__ */ new Map();
3617
- const { serviceRegistry, handleCall, handleStream, ...rpcOptions } = params;
3618
- this._handleCall = handleCall;
3619
- this._handleStream = handleStream;
3620
- this._serviceRegistry = serviceRegistry;
3621
- this._rpcPeer = new RpcPeer({
3622
- ...rpcOptions,
3623
- callHandler: (method, params2) => {
3624
- const [serviceName, methodName] = parseMethodName(method);
3625
- const handler = (method2, params3) => this._getServiceHandler(serviceName).call(method2, params3);
3626
- if (this._handleCall) {
3627
- return this._handleCall(methodName, params2, handler);
3628
- } else {
3629
- return handler(methodName, params2);
3630
- }
3631
- },
3632
- streamHandler: (method, params2) => {
3633
- const [serviceName, methodName] = parseMethodName(method);
3634
- const handler = (method2, params3) => this._getServiceHandler(serviceName).callStream(method2, params3);
3635
- if (this._handleStream) {
3636
- return Stream14.unwrapPromise(this._handleStream(methodName, params2, handler));
3637
- } else {
3638
- return handler(methodName, params2);
3639
- }
3640
- }
3641
- });
3642
- }
3643
- async open() {
3644
- await this._rpcPeer.open();
3645
- }
3646
- async close() {
3647
- await this._rpcPeer.close();
3648
- }
3649
- _getServiceHandler(serviceName) {
3650
- var _a;
3651
- if (!this._handlerCache.has(serviceName)) {
3652
- const [key, descriptor] = (_a = Object.entries(this._serviceRegistry.descriptors).find(([key2, descriptor2]) => descriptor2.name === serviceName)) != null ? _a : raise2(new Error(`Service not available: ${serviceName}`));
3653
- const service = this._serviceRegistry.services[key];
3654
- if (!service) {
3655
- throw new Error(`Service not available: ${serviceName}`);
3656
- }
3657
- this._handlerCache.set(serviceName, descriptor.createServer(service));
3658
- }
3659
- return this._handlerCache.get(serviceName);
3660
- }
3661
- };
3662
-
3663
- // packages/sdk/client-services/src/packlets/vault/worker-session.ts
3664
- var __decorate4 = function(decorators, target, key, desc) {
3665
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3666
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
3667
- r = Reflect.decorate(decorators, target, key, desc);
3668
- else
3669
- for (var i = decorators.length - 1; i >= 0; i--)
3670
- if (d = decorators[i])
3671
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3672
- return c > 3 && r && Object.defineProperty(target, key, r), r;
3673
- };
3674
- var WorkerSession = class {
3675
- constructor({ serviceHost, systemPort, appPort, shellPort, readySignal }) {
3676
- this._startTrigger = new Trigger7();
3677
- this.onClose = new Callback();
3678
- assert12(serviceHost);
3679
- this._serviceHost = serviceHost;
3680
- const middleware = {
3681
- handleCall: async (method, params, handler) => {
3682
- const error = await readySignal.wait({
3683
- timeout: PROXY_CONNECTION_TIMEOUT
3684
- });
3685
- if (error) {
3686
- throw error;
3687
- }
3688
- return handler(method, params);
3689
- },
3690
- handleStream: async (method, params, handler) => {
3691
- const error = await readySignal.wait({
3692
- timeout: PROXY_CONNECTION_TIMEOUT
3693
- });
3694
- if (error) {
3695
- throw error;
3696
- }
3697
- return handler(method, params);
3698
- }
3699
- };
3700
- this._clientRpc = new ClientRpcServer({
3701
- serviceRegistry: this._serviceHost.serviceRegistry,
3702
- port: appPort,
3703
- ...middleware
3704
- });
3705
- this._shellClientRpc = new ClientRpcServer({
3706
- serviceRegistry: this._serviceHost.serviceRegistry,
3707
- port: shellPort,
3708
- ...middleware
3709
- });
3710
- this._iframeRpc = createProtoRpcPeer3({
3711
- requested: iframeServiceBundle2,
3712
- exposed: workerServiceBundle2,
3713
- handlers: {
3714
- WorkerService: {
3715
- start: async (request) => {
3716
- this.origin = request.origin;
3717
- this.lockKey = request.lockKey;
3718
- this._startTrigger.wake();
3719
- },
3720
- stop: async () => {
3721
- setTimeout(async () => {
3722
- try {
3723
- await this.close();
3724
- } catch (err) {
3725
- log15.catch(err, {}, {
3726
- file: "worker-session.ts",
3727
- line: 100,
3728
- scope: this,
3729
- callSite: (f, a) => f(...a)
3730
- });
3731
- }
3732
- });
3733
- }
3734
- }
3735
- },
3736
- port: systemPort,
3737
- timeout: 1e3
3738
- });
3739
- this.bridgeService = this._iframeRpc.rpc.BridgeService;
3740
- }
3741
- async open() {
3742
- log15.info("opening..", {}, {
3743
- file: "worker-session.ts",
3744
- line: 114,
3745
- scope: this,
3746
- callSite: (f, a) => f(...a)
3747
- });
3748
- await Promise.all([
3749
- this._clientRpc.open(),
3750
- this._iframeRpc.open(),
3751
- this._maybeOpenShell()
3752
- ]);
3753
- await this._startTrigger.wait({
3754
- timeout: PROXY_CONNECTION_TIMEOUT
3755
- });
3756
- if (this.lockKey) {
3757
- void this._afterLockReleases(this.lockKey, () => this.close());
3758
- }
3759
- }
3760
- async close() {
3761
- log15.info("closing..", {}, {
3762
- file: "worker-session.ts",
3763
- line: 124,
3764
- scope: this,
3765
- callSite: (f, a) => f(...a)
3766
- });
3767
- try {
3768
- await this.onClose.callIfSet();
3769
- } catch (err) {
3770
- log15.catch(err, {}, {
3771
- file: "worker-session.ts",
3772
- line: 128,
3773
- scope: this,
3774
- callSite: (f, a) => f(...a)
3775
- });
3776
- }
3777
- await Promise.all([
3778
- this._clientRpc.close(),
3779
- this._iframeRpc.close()
3780
- ]);
3781
- }
3782
- async _maybeOpenShell() {
3783
- try {
3784
- await asyncTimeout2(this._shellClientRpc.open(), 1e3);
3785
- } catch (e) {
3786
- log15.info("No shell connected.", {}, {
3787
- file: "worker-session.ts",
3788
- line: 138,
3789
- scope: this,
3790
- callSite: (f, a) => f(...a)
3791
- });
3792
- }
3793
- }
3794
- _afterLockReleases(lockKey, callback) {
3795
- return navigator.locks.request(lockKey, () => {
3796
- }).then(callback);
3797
- }
3798
- };
3799
- __decorate4([
3800
- logInfo2
3801
- ], WorkerSession.prototype, "origin", void 0);
3802
- __decorate4([
3803
- logInfo2
3804
- ], WorkerSession.prototype, "lockKey", void 0);
3805
-
3806
- // packages/sdk/client-services/src/packlets/vault/worker-runtime.ts
3807
- var WorkerRuntime = class {
3808
- // prettier-ignore
3809
- constructor(_configProvider) {
3810
- this._configProvider = _configProvider;
3811
- this._transportFactory = new WebRTCTransportProxyFactory();
3812
- this._ready = new Trigger8();
3813
- this.sessions = /* @__PURE__ */ new Set();
3814
- this._clientServices = new ClientServicesHost();
3815
- }
3816
- get host() {
3817
- return this._clientServices;
3818
- }
3819
- async start() {
3820
- log16("starting...", {}, {
3821
- file: "worker-runtime.ts",
3822
- line: 48,
3823
- scope: this,
3824
- callSite: (f, a) => f(...a)
3825
- });
3826
- try {
3827
- this._config = await this._configProvider();
3828
- const signals = this._config.get("runtime.services.signaling");
3829
- this._clientServices.initialize({
3830
- config: this._config,
3831
- signalManager: signals ? new WebsocketSignalManager(signals) : new MemorySignalManager(new MemorySignalManagerContext()),
3832
- transportFactory: this._transportFactory
3833
- });
3834
- await this._clientServices.open();
3835
- this._ready.wake(void 0);
3836
- log16("started", {}, {
3837
- file: "worker-runtime.ts",
3838
- line: 62,
3839
- scope: this,
3840
- callSite: (f, a) => f(...a)
3841
- });
3842
- } catch (err) {
3843
- this._ready.wake(err);
3844
- log16.catch(err, {}, {
3845
- file: "worker-runtime.ts",
3846
- line: 65,
3847
- scope: this,
3848
- callSite: (f, a) => f(...a)
3849
- });
3850
- }
3851
- }
3852
- async stop() {
3853
- await this._clientServices.close();
3854
- }
3855
- /**
3856
- * Create a new session.
3857
- */
3858
- async createSession({ appPort, systemPort, shellPort }) {
3859
- const session = new WorkerSession({
3860
- serviceHost: this._clientServices,
3861
- appPort,
3862
- systemPort,
3863
- shellPort,
3864
- readySignal: this._ready
3865
- });
3866
- session.onClose.set(async () => {
3867
- this.sessions.delete(session);
3868
- this._reconnectWebrtc();
3869
- });
3870
- await session.open();
3871
- this.sessions.add(session);
3872
- this._reconnectWebrtc();
3873
- }
3874
- /**
3875
- * Selects one of the existing session fro WebRTC networking.
3876
- */
3877
- _reconnectWebrtc() {
3878
- log16("reconnecting webrtc...", {}, {
3879
- file: "worker-runtime.ts",
3880
- line: 102,
3881
- scope: this,
3882
- callSite: (f, a) => f(...a)
3883
- });
3884
- if (this._sessionForNetworking) {
3885
- if (!this.sessions.has(this._sessionForNetworking)) {
3886
- this._sessionForNetworking = void 0;
3887
- }
3888
- }
3889
- if (!this._sessionForNetworking) {
3890
- const selected = Array.from(this.sessions).find((session) => session.bridgeService);
3891
- if (selected) {
3892
- this._sessionForNetworking = selected;
3893
- this._transportFactory.setBridgeService(selected.bridgeService);
3894
- } else {
3895
- this._transportFactory.setBridgeService(void 0);
3896
- }
3897
- }
3898
- }
3899
- };
3900
-
3901
- // packages/sdk/client-services/src/packlets/services/service-context.ts
3902
- import assert13 from "@dxos/node-std/assert";
3903
- import { Trigger as Trigger9 } from "@dxos/async";
3904
- import { getCredentialAssertion as getCredentialAssertion3 } from "@dxos/credentials";
3905
- import { failUndefined as failUndefined3 } from "@dxos/debug";
3906
- import { valueEncoding, MetadataStore, SpaceManager, DataServiceSubscriptions, SnapshotStore } from "@dxos/echo-pipeline";
3907
- import { FeedFactory, FeedStore } from "@dxos/feed-store";
3908
- import { Keyring } from "@dxos/keyring";
3909
- import { PublicKey as PublicKey9 } from "@dxos/keys";
3910
- import { log as log17 } from "@dxos/log";
3911
- import { STORAGE_VERSION, trace as trace5 } from "@dxos/protocols";
3912
- import { Invitation as Invitation5 } from "@dxos/protocols/proto/dxos/client/services";
3913
- var ServiceContext = class {
3914
- // prettier-ignore
3915
- constructor(storage, networkManager, signalManager, modelFactory) {
3916
- this.storage = storage;
3917
- this.networkManager = networkManager;
3918
- this.signalManager = signalManager;
3919
- this.modelFactory = modelFactory;
3920
- this.initialized = new Trigger9();
3921
- this.dataServiceSubscriptions = new DataServiceSubscriptions();
3922
- this._handlerFactories = /* @__PURE__ */ new Map();
3923
- this._instanceId = PublicKey9.random().toHex();
3924
- this.metadataStore = new MetadataStore(storage.createDirectory("metadata"));
3925
- this.snapshotStore = new SnapshotStore(storage.createDirectory("snapshots"));
3926
- this.keyring = new Keyring(storage.createDirectory("keyring"));
3927
- this.feedStore = new FeedStore({
3928
- factory: new FeedFactory({
3929
- root: storage.createDirectory("feeds"),
3930
- signer: this.keyring,
3931
- hypercore: {
3932
- valueEncoding
3933
- }
3934
- })
3935
- });
3936
- this.spaceManager = new SpaceManager({
3937
- feedStore: this.feedStore,
3938
- networkManager: this.networkManager,
3939
- metadataStore: this.metadataStore,
3940
- modelFactory: this.modelFactory,
3941
- snapshotStore: this.snapshotStore
3942
- });
3943
- this.identityManager = new IdentityManager(this.metadataStore, this.keyring, this.feedStore, this.spaceManager);
3944
- this.invitations = new InvitationsHandler(this.networkManager);
3945
- this._handlerFactories.set(Invitation5.Kind.DEVICE, () => new DeviceInvitationProtocol(this.keyring, () => {
3946
- var _a;
3947
- return (_a = this.identityManager.identity) != null ? _a : failUndefined3();
3948
- }, this._acceptIdentity.bind(this)));
3949
- }
3950
- async open() {
3951
- log17.trace("dxos.sdk.service-context.open", trace5.begin({
3952
- id: this._instanceId
3953
- }), {
3954
- file: "service-context.ts",
3955
- line: 119,
3956
- scope: this,
3957
- callSite: (f, a) => f(...a)
3958
- });
3959
- await this._checkStorageVersion();
3960
- log17("opening...", {}, {
3961
- file: "service-context.ts",
3962
- line: 123,
3963
- scope: this,
3964
- callSite: (f, a) => f(...a)
3965
- });
3966
- await this.signalManager.open();
3967
- await this.networkManager.open();
3968
- await this.spaceManager.open();
3969
- await this.identityManager.open();
3970
- if (this.identityManager.identity) {
3971
- await this._initialize();
3972
- }
3973
- log17("opened", {}, {
3974
- file: "service-context.ts",
3975
- line: 131,
3976
- scope: this,
3977
- callSite: (f, a) => f(...a)
3978
- });
3979
- log17.trace("dxos.sdk.service-context.open", trace5.end({
3980
- id: this._instanceId
3981
- }), {
3982
- file: "service-context.ts",
3983
- line: 132,
3984
- scope: this,
3985
- callSite: (f, a) => f(...a)
3986
- });
3987
- }
3988
- async close() {
3989
- var _a, _b;
3990
- log17("closing...", {}, {
3991
- file: "service-context.ts",
3992
- line: 136,
3993
- scope: this,
3994
- callSite: (f, a) => f(...a)
3995
- });
3996
- await ((_a = this._deviceSpaceSync) == null ? void 0 : _a.close());
3997
- await ((_b = this.dataSpaceManager) == null ? void 0 : _b.close());
3998
- await this.identityManager.close();
3999
- await this.spaceManager.close();
4000
- await this.feedStore.close();
4001
- await this.networkManager.close();
4002
- await this.signalManager.close();
4003
- this.dataServiceSubscriptions.clear();
4004
- log17("closed", {}, {
4005
- file: "service-context.ts",
4006
- line: 145,
4007
- scope: this,
4008
- callSite: (f, a) => f(...a)
4009
- });
4010
- }
4011
- async createIdentity(params = {}) {
4012
- const identity = await this.identityManager.createIdentity(params);
4013
- await this._initialize();
4014
- return identity;
4015
- }
4016
- getInvitationHandler(invitation) {
4017
- const factory = this._handlerFactories.get(invitation.kind);
4018
- assert13(factory, `Unknown invitation kind: ${invitation.kind}`);
4019
- return factory(invitation);
4020
- }
4021
- async _acceptIdentity(params) {
4022
- const identity = await this.identityManager.acceptIdentity(params);
4023
- await this._initialize();
4024
- return identity;
3370
+ async _acceptIdentity(params) {
3371
+ const identity = await this.identityManager.acceptIdentity(params);
3372
+ await this._initialize();
3373
+ return identity;
4025
3374
  }
4026
3375
  async _checkStorageVersion() {
4027
3376
  await this.metadataStore.load();
@@ -4032,7 +3381,7 @@ var ServiceContext = class {
4032
3381
  // Called when identity is created.
4033
3382
  async _initialize() {
4034
3383
  var _a;
4035
- log17("initializing spaces...", {}, {
3384
+ log13("initializing spaces...", {}, {
4036
3385
  file: "service-context.ts",
4037
3386
  line: 178,
4038
3387
  scope: this,
@@ -4055,7 +3404,7 @@ var ServiceContext = class {
4055
3404
  this.dataSpaceManager = new DataSpaceManager(this.spaceManager, this.metadataStore, this.dataServiceSubscriptions, this.keyring, signingContext, this.feedStore);
4056
3405
  await this.dataSpaceManager.open();
4057
3406
  this._handlerFactories.set(Invitation5.Kind.SPACE, (invitation) => {
4058
- assert13(this.dataSpaceManager, "dataSpaceManager not initialized yet");
3407
+ assert11(this.dataSpaceManager, "dataSpaceManager not initialized yet");
4059
3408
  return new SpaceInvitationProtocol(this.dataSpaceManager, signingContext, this.keyring, invitation.spaceKey);
4060
3409
  });
4061
3410
  this.initialized.wake();
@@ -4069,7 +3418,7 @@ var ServiceContext = class {
4069
3418
  return;
4070
3419
  }
4071
3420
  if (!this.dataSpaceManager) {
4072
- log17("dataSpaceManager not initialized yet, ignoring space admission", {
3421
+ log13("dataSpaceManager not initialized yet, ignoring space admission", {
4073
3422
  details: assertion
4074
3423
  }, {
4075
3424
  file: "service-context.ts",
@@ -4080,7 +3429,7 @@ var ServiceContext = class {
4080
3429
  return;
4081
3430
  }
4082
3431
  if (this.dataSpaceManager.spaces.has(assertion.spaceKey)) {
4083
- log17("space already exists, ignoring space admission", {
3432
+ log13("space already exists, ignoring space admission", {
4084
3433
  details: assertion
4085
3434
  }, {
4086
3435
  file: "service-context.ts",
@@ -4091,7 +3440,7 @@ var ServiceContext = class {
4091
3440
  return;
4092
3441
  }
4093
3442
  try {
4094
- log17("accepting space recorded in halo", {
3443
+ log13("accepting space recorded in halo", {
4095
3444
  details: assertion
4096
3445
  }, {
4097
3446
  file: "service-context.ts",
@@ -4104,7 +3453,7 @@ var ServiceContext = class {
4104
3453
  genesisFeedKey: assertion.genesisFeedKey
4105
3454
  });
4106
3455
  } catch (err) {
4107
- log17.catch(err, {}, {
3456
+ log13.catch(err, {}, {
4108
3457
  file: "service-context.ts",
4109
3458
  line: 232,
4110
3459
  scope: this,
@@ -4141,6 +3490,181 @@ var ServiceRegistry = class {
4141
3490
  }
4142
3491
  };
4143
3492
 
3493
+ // packages/sdk/client-services/src/packlets/services/service-host.ts
3494
+ import assert12 from "@dxos/node-std/assert";
3495
+ import { Event as Event8 } from "@dxos/async";
3496
+ import { clientServiceBundle, createDefaultModelFactory } from "@dxos/client-protocol";
3497
+ import { DataServiceImpl } from "@dxos/echo-pipeline";
3498
+ import { PublicKey as PublicKey10 } from "@dxos/keys";
3499
+ import { log as log15 } from "@dxos/log";
3500
+ import { WebsocketSignalManager } from "@dxos/messaging";
3501
+ import { createWebRTCTransportFactory, NetworkManager } from "@dxos/network-manager";
3502
+ import { trace as trace6 } from "@dxos/protocols";
3503
+ import { SystemStatus } from "@dxos/protocols/proto/dxos/client/services";
3504
+
3505
+ // packages/sdk/client-services/src/packlets/devices/devices-service.ts
3506
+ import { EventSubscriptions as EventSubscriptions3 } from "@dxos/async";
3507
+ import { Stream as Stream10 } from "@dxos/codec-protobuf";
3508
+ import { DeviceKind } from "@dxos/protocols/proto/dxos/client/services";
3509
+ var DevicesServiceImpl = class {
3510
+ constructor(_identityManager) {
3511
+ this._identityManager = _identityManager;
3512
+ }
3513
+ updateDevice(request) {
3514
+ throw new Error("Method not implemented.");
3515
+ }
3516
+ queryDevices() {
3517
+ return new Stream10(({ next }) => {
3518
+ const update = () => {
3519
+ var _a;
3520
+ const deviceKeys = (_a = this._identityManager.identity) == null ? void 0 : _a.authorizedDeviceKeys;
3521
+ if (!deviceKeys) {
3522
+ next({
3523
+ devices: []
3524
+ });
3525
+ } else {
3526
+ next({
3527
+ devices: Array.from(deviceKeys.values()).map((key) => {
3528
+ var _a2;
3529
+ return {
3530
+ deviceKey: key,
3531
+ kind: ((_a2 = this._identityManager.identity) == null ? void 0 : _a2.deviceKey.equals(key)) ? DeviceKind.CURRENT : DeviceKind.TRUSTED
3532
+ };
3533
+ })
3534
+ });
3535
+ }
3536
+ };
3537
+ const subscriptions = new EventSubscriptions3();
3538
+ subscriptions.add(this._identityManager.stateUpdate.on(() => {
3539
+ update();
3540
+ if (this._identityManager.identity) {
3541
+ subscriptions.add(this._identityManager.identity.stateUpdate.on(() => {
3542
+ update();
3543
+ }));
3544
+ }
3545
+ }));
3546
+ update();
3547
+ return () => subscriptions.clear();
3548
+ });
3549
+ }
3550
+ };
3551
+
3552
+ // packages/sdk/client-services/src/packlets/logging/logging-service.ts
3553
+ import { Event as Event7 } from "@dxos/async";
3554
+ import { Stream as Stream11 } from "@dxos/codec-protobuf";
3555
+ import { log as log14, shouldLog, getContextFromEntry } from "@dxos/log";
3556
+ import { jsonify } from "@dxos/util";
3557
+ var LoggingServiceImpl = class {
3558
+ constructor() {
3559
+ this._logs = new Event7();
3560
+ this._logProcessor = (_config, entry2) => {
3561
+ this._logs.emit(entry2);
3562
+ };
3563
+ }
3564
+ async open() {
3565
+ log14.runtimeConfig.processors.push(this._logProcessor);
3566
+ }
3567
+ async close() {
3568
+ const index = log14.runtimeConfig.processors.findIndex((processor) => processor === this._logProcessor);
3569
+ log14.runtimeConfig.processors.splice(index, 1);
3570
+ }
3571
+ queryLogs(request) {
3572
+ var _a;
3573
+ const filters = ((_a = request.filters) == null ? void 0 : _a.length) ? request.filters : void 0;
3574
+ return new Stream11(({ ctx, next }) => {
3575
+ const handler = (entry2) => {
3576
+ var _a2, _b, _c;
3577
+ if (((_a2 = entry2.meta) == null ? void 0 : _a2.file.includes("logging-service")) || entry2.context && Object.values(entry2.context).some((value) => typeof value === "string" && value.includes("LoggingService"))) {
3578
+ return;
3579
+ }
3580
+ if (shouldLog(entry2, filters)) {
3581
+ const record = {
3582
+ ...entry2,
3583
+ context: jsonify(getContextFromEntry(entry2))
3584
+ };
3585
+ (_b = record.meta) == null ? true : delete _b.bugcheck;
3586
+ (_c = record.meta) == null ? true : delete _c.scope;
3587
+ next(record);
3588
+ }
3589
+ };
3590
+ ctx.onDispose(() => {
3591
+ this._logs.off(handler);
3592
+ });
3593
+ this._logs.on(handler);
3594
+ });
3595
+ }
3596
+ };
3597
+
3598
+ // packages/sdk/client-services/src/packlets/network/network-service.ts
3599
+ import { Stream as Stream12 } from "@dxos/codec-protobuf";
3600
+ var NetworkServiceImpl = class {
3601
+ constructor(networkManager, signalManager) {
3602
+ this.networkManager = networkManager;
3603
+ this.signalManager = signalManager;
3604
+ }
3605
+ queryStatus() {
3606
+ return new Stream12(({ next }) => {
3607
+ const update = () => {
3608
+ next({
3609
+ swarm: this.networkManager.connectionState,
3610
+ signaling: this.signalManager.getStatus().map(({ host, state }) => ({
3611
+ server: host,
3612
+ state
3613
+ }))
3614
+ });
3615
+ };
3616
+ const unsubscribeSwarm = this.networkManager.connectionStateChanged.on(() => update());
3617
+ const unsubscribeSignal = this.signalManager.statusChanged.on(() => update());
3618
+ update();
3619
+ return () => {
3620
+ unsubscribeSwarm();
3621
+ unsubscribeSignal();
3622
+ };
3623
+ });
3624
+ }
3625
+ async updateConfig(request) {
3626
+ await this.networkManager.setConnectionState(request.swarm);
3627
+ }
3628
+ };
3629
+
3630
+ // packages/sdk/client-services/src/packlets/system/system-service.ts
3631
+ import { Stream as Stream13 } from "@dxos/codec-protobuf";
3632
+ var SystemServiceImpl = class {
3633
+ constructor({ config, statusUpdate, onUpdateStatus, getCurrentStatus, onReset }) {
3634
+ this._config = config;
3635
+ this._statusUpdate = statusUpdate;
3636
+ this._getCurrentStatus = getCurrentStatus;
3637
+ this._onUpdateStatus = onUpdateStatus;
3638
+ this._onReset = onReset;
3639
+ }
3640
+ async getConfig() {
3641
+ var _a, _b;
3642
+ return (_b = (_a = this._config) == null ? void 0 : _a.values) != null ? _b : {};
3643
+ }
3644
+ async updateStatus({ status }) {
3645
+ await this._onUpdateStatus(status);
3646
+ }
3647
+ queryStatus() {
3648
+ return new Stream13(({ next }) => {
3649
+ const update = () => {
3650
+ next({
3651
+ status: this._getCurrentStatus()
3652
+ });
3653
+ };
3654
+ update();
3655
+ const unsubscribe = this._statusUpdate.on(() => update());
3656
+ const interval = setInterval(update, 3e3);
3657
+ return () => {
3658
+ clearInterval(interval);
3659
+ unsubscribe();
3660
+ };
3661
+ });
3662
+ }
3663
+ async reset() {
3664
+ await this._onReset();
3665
+ }
3666
+ };
3667
+
4144
3668
  // packages/sdk/client-services/src/packlets/services/service-host.ts
4145
3669
  var ClientServicesHost = class {
4146
3670
  constructor({
@@ -4154,7 +3678,7 @@ var ClientServicesHost = class {
4154
3678
  // TODO(wittjosiah): Turn this on by default.
4155
3679
  lockKey
4156
3680
  } = {}) {
4157
- this._statusUpdate = new Event9();
3681
+ this._statusUpdate = new Event8();
4158
3682
  this._opening = false;
4159
3683
  this._open = false;
4160
3684
  this._storage = storage;
@@ -4166,15 +3690,17 @@ var ClientServicesHost = class {
4166
3690
  signalManager
4167
3691
  });
4168
3692
  }
4169
- this._resourceLock = lockKey ? new VaultResourceLock({
4170
- lockKey,
4171
- onAcquire: () => {
4172
- if (!this._opening) {
4173
- void this.open();
4174
- }
4175
- },
4176
- onRelease: () => this.close()
4177
- }) : void 0;
3693
+ if (lockKey) {
3694
+ this._resourceLock = new Lock({
3695
+ lockKey,
3696
+ onAcquire: () => {
3697
+ if (!this._opening) {
3698
+ void this.open();
3699
+ }
3700
+ },
3701
+ onRelease: () => this.close()
3702
+ });
3703
+ }
4178
3704
  this._systemService = new SystemServiceImpl({
4179
3705
  config: this._config,
4180
3706
  statusUpdate: this._statusUpdate,
@@ -4215,9 +3741,9 @@ var ClientServicesHost = class {
4215
3741
  */
4216
3742
  initialize({ config, ...options }) {
4217
3743
  var _a, _b, _c;
4218
- assert14(!this._open, "service host is open");
3744
+ assert12(!this._open, "service host is open");
4219
3745
  if (config) {
4220
- assert14(!this._config, "config already set");
3746
+ assert12(!this._config, "config already set");
4221
3747
  this._config = config;
4222
3748
  if (!this._storage) {
4223
3749
  this._storage = createStorageObjects(config.get("runtime.client.storage", {})).storage;
@@ -4225,9 +3751,9 @@ var ClientServicesHost = class {
4225
3751
  }
4226
3752
  const { connectionLog = true, transportFactory = createWebRTCTransportFactory({
4227
3753
  iceServers: (_a = this._config) == null ? void 0 : _a.get("runtime.services.ice")
4228
- }), signalManager = new WebsocketSignalManager2((_c = (_b = this._config) == null ? void 0 : _b.get("runtime.services.signaling")) != null ? _c : []) } = options;
3754
+ }), signalManager = new WebsocketSignalManager((_c = (_b = this._config) == null ? void 0 : _b.get("runtime.services.signaling")) != null ? _c : []) } = options;
4229
3755
  this._signalManager = signalManager;
4230
- assert14(!this._networkManager, "network manager already set");
3756
+ assert12(!this._networkManager, "network manager already set");
4231
3757
  this._networkManager = new NetworkManager({
4232
3758
  log: connectionLog,
4233
3759
  transportFactory,
@@ -4240,7 +3766,7 @@ var ClientServicesHost = class {
4240
3766
  return;
4241
3767
  }
4242
3768
  const traceId = PublicKey10.random().toHex();
4243
- log18.trace("dxos.sdk.client-services-host.open", trace6.begin({
3769
+ log15.trace("dxos.sdk.client-services-host.open", trace6.begin({
4244
3770
  id: traceId
4245
3771
  }), {
4246
3772
  file: "service-host.ts",
@@ -4248,12 +3774,12 @@ var ClientServicesHost = class {
4248
3774
  scope: this,
4249
3775
  callSite: (f, a) => f(...a)
4250
3776
  });
4251
- assert14(this._config, "config not set");
4252
- assert14(this._storage, "storage not set");
4253
- assert14(this._signalManager, "signal manager not set");
4254
- assert14(this._networkManager, "network manager not set");
3777
+ assert12(this._config, "config not set");
3778
+ assert12(this._storage, "storage not set");
3779
+ assert12(this._signalManager, "signal manager not set");
3780
+ assert12(this._networkManager, "network manager not set");
4255
3781
  this._opening = true;
4256
- log18("opening...", {
3782
+ log15("opening...", {
4257
3783
  lockKey: (_a = this._resourceLock) == null ? void 0 : _a.lockKey
4258
3784
  }, {
4259
3785
  file: "service-host.ts",
@@ -4288,7 +3814,7 @@ var ClientServicesHost = class {
4288
3814
  this._open = true;
4289
3815
  this._statusUpdate.emit();
4290
3816
  const deviceKey = (_c = this._serviceContext.identityManager.identity) == null ? void 0 : _c.deviceKey;
4291
- log18("opened", {
3817
+ log15("opened", {
4292
3818
  deviceKey
4293
3819
  }, {
4294
3820
  file: "service-host.ts",
@@ -4296,7 +3822,7 @@ var ClientServicesHost = class {
4296
3822
  scope: this,
4297
3823
  callSite: (f, a) => f(...a)
4298
3824
  });
4299
- log18.trace("dxos.sdk.client-services-host.open", trace6.end({
3825
+ log15.trace("dxos.sdk.client-services-host.open", trace6.end({
4300
3826
  id: traceId
4301
3827
  }), {
4302
3828
  file: "service-host.ts",
@@ -4311,7 +3837,7 @@ var ClientServicesHost = class {
4311
3837
  return;
4312
3838
  }
4313
3839
  const deviceKey = (_a = this._serviceContext.identityManager.identity) == null ? void 0 : _a.deviceKey;
4314
- log18("closing...", {
3840
+ log15("closing...", {
4315
3841
  deviceKey
4316
3842
  }, {
4317
3843
  file: "service-host.ts",
@@ -4326,7 +3852,7 @@ var ClientServicesHost = class {
4326
3852
  await this._serviceContext.close();
4327
3853
  this._open = false;
4328
3854
  this._statusUpdate.emit();
4329
- log18("closed", {
3855
+ log15("closed", {
4330
3856
  deviceKey
4331
3857
  }, {
4332
3858
  file: "service-host.ts",
@@ -4338,7 +3864,7 @@ var ClientServicesHost = class {
4338
3864
  async reset() {
4339
3865
  var _a;
4340
3866
  const traceId = PublicKey10.random().toHex();
4341
- log18.trace("dxos.sdk.client-services-host.reset", trace6.begin({
3867
+ log15.trace("dxos.sdk.client-services-host.reset", trace6.begin({
4342
3868
  id: traceId
4343
3869
  }), {
4344
3870
  file: "service-host.ts",
@@ -4346,7 +3872,7 @@ var ClientServicesHost = class {
4346
3872
  scope: this,
4347
3873
  callSite: (f, a) => f(...a)
4348
3874
  });
4349
- log18("resetting...", {}, {
3875
+ log15("resetting...", {}, {
4350
3876
  file: "service-host.ts",
4351
3877
  line: 273,
4352
3878
  scope: this,
@@ -4354,13 +3880,13 @@ var ClientServicesHost = class {
4354
3880
  });
4355
3881
  await ((_a = this._serviceContext) == null ? void 0 : _a.close());
4356
3882
  await this._storage.reset();
4357
- log18("reset", {}, {
3883
+ log15("reset", {}, {
4358
3884
  file: "service-host.ts",
4359
3885
  line: 276,
4360
3886
  scope: this,
4361
3887
  callSite: (f, a) => f(...a)
4362
3888
  });
4363
- log18.trace("dxos.sdk.client-services-host.reset", trace6.end({
3889
+ log15.trace("dxos.sdk.client-services-host.reset", trace6.end({
4364
3890
  id: traceId
4365
3891
  }), {
4366
3892
  file: "service-host.ts",
@@ -4393,125 +3919,6 @@ var LocalClientServices = class {
4393
3919
  }
4394
3920
  };
4395
3921
 
4396
- // packages/sdk/client-services/src/packlets/vault/iframe-host-runtime.ts
4397
- var __decorate5 = function(decorators, target, key, desc) {
4398
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4399
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
4400
- r = Reflect.decorate(decorators, target, key, desc);
4401
- else
4402
- for (var i = decorators.length - 1; i >= 0; i--)
4403
- if (d = decorators[i])
4404
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
4405
- return c > 3 && r && Object.defineProperty(target, key, r), r;
4406
- };
4407
- var LOCK_KEY = "DXOS_RESOURCE_LOCK";
4408
- var IFrameHostRuntime = class {
4409
- constructor({ config, origin, appPort, shellPort }) {
4410
- this._ready = new Trigger10();
4411
- this._configProvider = config;
4412
- this.origin = origin;
4413
- this._appPort = appPort;
4414
- this._shellPort = shellPort;
4415
- if (this._shellPort) {
4416
- this._shellRuntime = new ShellRuntimeImpl(this._shellPort);
4417
- }
4418
- }
4419
- get services() {
4420
- return this._clientServices;
4421
- }
4422
- get shell() {
4423
- return this._shellRuntime;
4424
- }
4425
- async start() {
4426
- var _a;
4427
- log19("starting...", {}, {
4428
- file: "iframe-host-runtime.ts",
4429
- line: 70,
4430
- scope: this,
4431
- callSite: (f, a) => f(...a)
4432
- });
4433
- try {
4434
- this._config = await getAsyncValue2(this._configProvider);
4435
- this._transportFactory = createWebRTCTransportFactory2({
4436
- iceServers: this._config.get("runtime.services.ice")
4437
- });
4438
- const signals = this._config.get("runtime.services.signaling");
4439
- this._clientServices = new LocalClientServices({
4440
- lockKey: LOCK_KEY,
4441
- config: this._config,
4442
- signalManager: signals ? new WebsocketSignalManager3(signals) : new MemorySignalManager2(new MemorySignalManagerContext2()),
4443
- transportFactory: this._transportFactory
4444
- });
4445
- const middleware = {
4446
- handleCall: async (method, params, handler) => {
4447
- const error = await this._ready.wait({
4448
- timeout: PROXY_CONNECTION_TIMEOUT2
4449
- });
4450
- if (error) {
4451
- throw error;
4452
- }
4453
- return handler(method, params);
4454
- },
4455
- handleStream: async (method, params, handler) => {
4456
- const error = await this._ready.wait({
4457
- timeout: PROXY_CONNECTION_TIMEOUT2
4458
- });
4459
- if (error) {
4460
- throw error;
4461
- }
4462
- return handler(method, params);
4463
- }
4464
- };
4465
- this._clientRpc = new ClientRpcServer({
4466
- serviceRegistry: this._clientServices.host.serviceRegistry,
4467
- port: this._appPort,
4468
- ...middleware
4469
- });
4470
- await Promise.all([
4471
- this._clientServices.open(),
4472
- this._clientRpc.open(),
4473
- (_a = this._shellRuntime) == null ? void 0 : _a.open()
4474
- ]);
4475
- this._ready.wake(void 0);
4476
- log19("started", {}, {
4477
- file: "iframe-host-runtime.ts",
4478
- line: 113,
4479
- scope: this,
4480
- callSite: (f, a) => f(...a)
4481
- });
4482
- } catch (err) {
4483
- this._ready.wake(err);
4484
- log19.catch(err, {}, {
4485
- file: "iframe-host-runtime.ts",
4486
- line: 116,
4487
- scope: this,
4488
- callSite: (f, a) => f(...a)
4489
- });
4490
- }
4491
- }
4492
- async stop() {
4493
- var _a;
4494
- log19("stopping...", {}, {
4495
- file: "iframe-host-runtime.ts",
4496
- line: 121,
4497
- scope: this,
4498
- callSite: (f, a) => f(...a)
4499
- });
4500
- await this._clientRpc.close();
4501
- await this._clientServices.close();
4502
- await ((_a = this._shellRuntime) == null ? void 0 : _a.close());
4503
- log19("stopped", {}, {
4504
- file: "iframe-host-runtime.ts",
4505
- line: 125,
4506
- scope: this,
4507
- callSite: (f, a) => f(...a)
4508
- });
4509
- }
4510
- };
4511
- __decorate5([
4512
- logInfo3
4513
- ], IFrameHostRuntime.prototype, "origin", void 0);
4514
-
4515
3922
  export {
4516
3923
  subscribeToFeeds,
4517
3924
  subscribeToFeedBlocks,
@@ -4532,20 +3939,15 @@ export {
4532
3939
  InvitationsHandler,
4533
3940
  InvitationsServiceImpl,
4534
3941
  SpaceInvitationProtocol,
3942
+ Lock,
3943
+ isLocked,
4535
3944
  DataSpace,
4536
3945
  DataSpaceManager,
4537
3946
  SpacesServiceImpl,
4538
3947
  createStorageObjects,
4539
- ShellRuntimeImpl,
4540
- MemoryShellRuntime,
4541
- IFrameHostRuntime,
4542
- IFrameProxyRuntime,
4543
- VaultResourceLock,
4544
- WorkerSession,
4545
- WorkerRuntime,
4546
3948
  ServiceContext,
4547
3949
  ServiceRegistry,
4548
3950
  ClientServicesHost,
4549
3951
  LocalClientServices
4550
3952
  };
4551
- //# sourceMappingURL=chunk-YLH4UYUT.mjs.map
3953
+ //# sourceMappingURL=chunk-UL74TKNZ.mjs.map