jazz-tools 0.18.3 → 0.18.5

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 (60) hide show
  1. package/.turbo/turbo-build.log +34 -34
  2. package/CHANGELOG.md +22 -0
  3. package/dist/better-auth/auth/react.d.ts +5 -2
  4. package/dist/better-auth/auth/react.d.ts.map +1 -1
  5. package/dist/better-auth/auth/server.d.ts +21 -1
  6. package/dist/better-auth/auth/server.d.ts.map +1 -1
  7. package/dist/better-auth/auth/server.js +10 -5
  8. package/dist/better-auth/auth/server.js.map +1 -1
  9. package/dist/browser/createBrowserContext.d.ts.map +1 -1
  10. package/dist/browser/index.js +7 -0
  11. package/dist/browser/index.js.map +1 -1
  12. package/dist/{chunk-IERUTUXB.js → chunk-3LE7N6TH.js} +121 -36
  13. package/dist/chunk-3LE7N6TH.js.map +1 -0
  14. package/dist/index.js +1 -1
  15. package/dist/react-core/index.js +120 -35
  16. package/dist/react-core/index.js.map +1 -1
  17. package/dist/testing.js +1 -1
  18. package/dist/tools/coValues/account.d.ts.map +1 -1
  19. package/dist/tools/coValues/coFeed.d.ts +12 -0
  20. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  21. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  22. package/dist/tools/implementation/anonymousJazzAgent.d.ts +1 -1
  23. package/dist/tools/implementation/anonymousJazzAgent.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/coExport.d.ts +2 -0
  25. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +19 -0
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts +4 -0
  29. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts.map +1 -1
  31. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  32. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +61 -11
  33. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  34. package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts +2 -0
  35. package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts.map +1 -0
  36. package/dist/tools/testing.d.ts.map +1 -1
  37. package/package.json +5 -4
  38. package/src/better-auth/auth/server.ts +38 -7
  39. package/src/better-auth/auth/tests/server.test.ts +95 -7
  40. package/src/browser/createBrowserContext.ts +8 -0
  41. package/src/tools/coValues/account.ts +3 -1
  42. package/src/tools/coValues/coFeed.ts +5 -0
  43. package/src/tools/coValues/coMap.ts +3 -1
  44. package/src/tools/implementation/anonymousJazzAgent.ts +1 -1
  45. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  46. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +19 -0
  47. package/src/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.ts +8 -1
  48. package/src/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.ts +0 -1
  49. package/src/tools/implementation/zodSchema/unionUtils.ts +0 -1
  50. package/src/tools/subscribe/CoValueCoreSubscription.test.ts +1000 -0
  51. package/src/tools/subscribe/CoValueCoreSubscription.ts +179 -43
  52. package/src/tools/tests/account.test.ts +12 -0
  53. package/src/tools/tests/coFeed.test.ts +25 -0
  54. package/src/tools/tests/coList.test-d.ts +17 -0
  55. package/src/tools/tests/coList.test.ts +20 -0
  56. package/src/tools/tests/coMap.record.test-d.ts +18 -0
  57. package/src/tools/tests/coMap.record.test.ts +1 -0
  58. package/src/tools/tests/coMap.test-d.ts +15 -0
  59. package/src/tools/tests/coMap.test.ts +12 -2
  60. package/dist/chunk-IERUTUXB.js.map +0 -1
@@ -228,7 +228,9 @@ var _CoMap = class _CoMap extends CoValueBase {
228
228
  */
229
229
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
230
230
  toJSON(_key, processedValues) {
231
- const result = {};
231
+ const result = {
232
+ $jazz: { id: this.$jazz.id }
233
+ };
232
234
  for (const key of this.$jazz.raw.keys()) {
233
235
  const tKey = key;
234
236
  const descriptor = this.$jazz.getDescriptor(tKey);
@@ -1557,6 +1559,7 @@ var _CoFeed = class _CoFeed extends CoValueBase {
1557
1559
  const itemDescriptor = this.$jazz.schema[ItemsSym];
1558
1560
  const mapper = itemDescriptor === "json" ? (v) => v : "encoded" in itemDescriptor ? itemDescriptor.encoded.encode : (v) => v && v.$jazz.id;
1559
1561
  return {
1562
+ $jazz: { id: this.$jazz.id },
1560
1563
  ...Object.fromEntries(
1561
1564
  Object.entries(this).map(([account, entry]) => [
1562
1565
  account,
@@ -2005,6 +2008,7 @@ var FileStream = class extends CoValueBase {
2005
2008
  */
2006
2009
  toJSON() {
2007
2010
  return {
2011
+ $jazz: { id: this.$jazz.id },
2008
2012
  ...this.getChunks()
2009
2013
  };
2010
2014
  }
@@ -2216,7 +2220,9 @@ var Account = class extends CoValueBase {
2216
2220
  }
2217
2221
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2218
2222
  toJSON() {
2219
- return {};
2223
+ return {
2224
+ $jazz: { id: this.$jazz.id }
2225
+ };
2220
2226
  }
2221
2227
  [inspect]() {
2222
2228
  return this.toJSON();
@@ -3416,56 +3422,135 @@ function applyCoValueMigrations(instance) {
3416
3422
 
3417
3423
  // src/tools/subscribe/CoValueCoreSubscription.ts
3418
3424
  var CoValueCoreSubscription = class {
3419
- constructor(node, id, listener, skipRetry) {
3420
- this.node = node;
3421
- this.id = id;
3422
- this.listener = listener;
3423
- this.skipRetry = skipRetry;
3425
+ constructor(localNode, id, listener, skipRetry, branch) {
3424
3426
  this._unsubscribe = () => {
3425
3427
  };
3426
3428
  this.unsubscribed = false;
3427
- const entry = this.node.getCoValue(this.id);
3428
- if (entry?.isAvailable()) {
3429
- this.subscribe(entry.getCurrentContent());
3429
+ this.localNode = localNode;
3430
+ this.listener = listener;
3431
+ this.skipRetry = skipRetry;
3432
+ this.branchName = branch?.name;
3433
+ this.branchOwnerId = branch?.owner?.$jazz.raw.id;
3434
+ this.source = localNode.getCoValue(id);
3435
+ this.initializeSubscription();
3436
+ }
3437
+ /**
3438
+ * Main entry point for subscription initialization.
3439
+ * Determines the subscription strategy based on current availability and branch requirements.
3440
+ */
3441
+ initializeSubscription() {
3442
+ const source = this.source;
3443
+ if (source.isAvailable()) {
3444
+ this.handleAvailableSource(source);
3445
+ return;
3446
+ }
3447
+ if (this.branchName) {
3448
+ this.handleBranchCheckout();
3449
+ return;
3450
+ }
3451
+ this.loadCoValue();
3452
+ }
3453
+ /**
3454
+ * Handles the case where the CoValue source is immediately available.
3455
+ * Either subscribes directly or attempts to get the requested branch.
3456
+ */
3457
+ handleAvailableSource(source) {
3458
+ if (!this.branchName) {
3459
+ this.subscribe(source.getCurrentContent());
3460
+ return;
3461
+ }
3462
+ const branch = source.getBranch(this.branchName, this.branchOwnerId);
3463
+ if (branch.isAvailable()) {
3464
+ this.subscribe(branch.getCurrentContent());
3465
+ return;
3430
3466
  } else {
3431
- this.node.loadCoValueCore(this.id, void 0, skipRetry).then((value) => {
3432
- if (this.unsubscribed) return;
3433
- if (value.isAvailable()) {
3434
- this.subscribe(value.getCurrentContent());
3435
- } else {
3436
- this.subscribeToState();
3437
- this.listener("unavailable");
3438
- }
3439
- }).catch((error) => {
3440
- console.error("Unexpected error loading CoValue: ", error);
3441
- this.listener("unavailable");
3442
- });
3467
+ this.handleBranchCheckout();
3443
3468
  }
3444
3469
  }
3445
- subscribeToState() {
3446
- const entry = this.node.getCoValue(this.id);
3447
- const handleStateChange = (core2, unsubFromStateChange2) => {
3448
- if (this.unsubscribed) {
3449
- unsubFromStateChange2();
3450
- return;
3470
+ /**
3471
+ * Attempts to checkout a specific branch of the CoValue.
3472
+ * This is called when the source isn't available but a branch is requested.
3473
+ */
3474
+ handleBranchCheckout() {
3475
+ this.localNode.checkoutBranch(this.source.id, this.branchName, this.branchOwnerId).then((value) => {
3476
+ if (this.unsubscribed) return;
3477
+ if (value !== "unavailable") {
3478
+ this.subscribe(value);
3479
+ } else {
3480
+ this.handleUnavailableBranch();
3451
3481
  }
3452
- if (core2.isAvailable()) {
3453
- this.subscribe(core2.getCurrentContent());
3454
- unsubFromStateChange2();
3482
+ }).catch((error) => {
3483
+ console.error(error);
3484
+ this.emit("unavailable");
3485
+ });
3486
+ }
3487
+ /**
3488
+ * Handles the case where a branch checkout fails.
3489
+ * Determines whether to retry or report unavailability.
3490
+ */
3491
+ handleUnavailableBranch() {
3492
+ const source = this.source;
3493
+ if (source.isAvailable()) {
3494
+ throw new Error("Branch is unavailable");
3495
+ }
3496
+ this.subscribeToUnavailableSource();
3497
+ this.emit("unavailable");
3498
+ }
3499
+ /**
3500
+ * Loads the CoValue core from the network/storage.
3501
+ * This is the fallback strategy when immediate availability fails.
3502
+ */
3503
+ loadCoValue() {
3504
+ this.localNode.loadCoValueCore(this.source.id, void 0, this.skipRetry).then((value) => {
3505
+ if (this.unsubscribed) return;
3506
+ if (value.isAvailable()) {
3507
+ this.subscribe(value.getCurrentContent());
3508
+ } else {
3509
+ this.subscribeToUnavailableSource();
3510
+ this.emit("unavailable");
3511
+ }
3512
+ }).catch((error) => {
3513
+ console.error(error);
3514
+ this.emit("unavailable");
3515
+ });
3516
+ }
3517
+ /**
3518
+ * Subscribes to state changes of an unavailable CoValue source.
3519
+ * This allows the subscription to become active when the source becomes available after a first loading attempt.
3520
+ */
3521
+ subscribeToUnavailableSource() {
3522
+ const source = this.source;
3523
+ const handleStateChange = (_, unsubFromStateChange) => {
3524
+ if (!source.isAvailable()) {
3525
+ return;
3455
3526
  }
3456
- };
3457
- const unsubFromStateChange = entry.subscribe(handleStateChange);
3458
- this._unsubscribe = () => {
3459
3527
  unsubFromStateChange();
3528
+ if (this.branchName) {
3529
+ this.handleBranchCheckout();
3530
+ } else {
3531
+ this.subscribe(source.getCurrentContent());
3532
+ }
3460
3533
  };
3534
+ this._unsubscribe = source.subscribe(handleStateChange);
3461
3535
  }
3536
+ /**
3537
+ * Subscribes to a specific CoValue and notifies the listener.
3538
+ * This is the final step where we actually start receiving updates.
3539
+ */
3462
3540
  subscribe(value) {
3463
3541
  if (this.unsubscribed) return;
3464
3542
  this._unsubscribe = value.subscribe((value2) => {
3465
- this.listener(value2);
3543
+ this.emit(value2);
3466
3544
  });
3545
+ }
3546
+ emit(value) {
3547
+ if (this.unsubscribed) return;
3467
3548
  this.listener(value);
3468
3549
  }
3550
+ /**
3551
+ * Unsubscribes from all active subscriptions and marks the instance as unsubscribed.
3552
+ * This prevents any further operations and ensures proper cleanup.
3553
+ */
3469
3554
  unsubscribe() {
3470
3555
  if (this.unsubscribed) return;
3471
3556
  this.unsubscribed = true;
@@ -5731,4 +5816,4 @@ export {
5731
5816
  JazzContextManager
5732
5817
  };
5733
5818
  /* istanbul ignore file -- @preserve */
5734
- //# sourceMappingURL=chunk-IERUTUXB.js.map
5819
+ //# sourceMappingURL=chunk-3LE7N6TH.js.map