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
package/dist/index.js CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  randomSessionProvider,
38
38
  subscribeToCoValue,
39
39
  zodReExport_exports
40
- } from "./chunk-IERUTUXB.js";
40
+ } from "./chunk-3LE7N6TH.js";
41
41
 
42
42
  // src/tools/auth/clerk/index.ts
43
43
  import {
@@ -314,7 +314,9 @@ var _CoMap = class _CoMap extends CoValueBase {
314
314
  */
315
315
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
316
316
  toJSON(_key, processedValues) {
317
- const result = {};
317
+ const result = {
318
+ $jazz: { id: this.$jazz.id }
319
+ };
318
320
  for (const key of this.$jazz.raw.keys()) {
319
321
  const tKey = key;
320
322
  const descriptor = this.$jazz.getDescriptor(tKey);
@@ -1643,6 +1645,7 @@ var _CoFeed = class _CoFeed extends CoValueBase {
1643
1645
  const itemDescriptor = this.$jazz.schema[ItemsSym];
1644
1646
  const mapper = itemDescriptor === "json" ? (v) => v : "encoded" in itemDescriptor ? itemDescriptor.encoded.encode : (v) => v && v.$jazz.id;
1645
1647
  return {
1648
+ $jazz: { id: this.$jazz.id },
1646
1649
  ...Object.fromEntries(
1647
1650
  Object.entries(this).map(([account, entry]) => [
1648
1651
  account,
@@ -2091,6 +2094,7 @@ var FileStream = class extends CoValueBase {
2091
2094
  */
2092
2095
  toJSON() {
2093
2096
  return {
2097
+ $jazz: { id: this.$jazz.id },
2094
2098
  ...this.getChunks()
2095
2099
  };
2096
2100
  }
@@ -2302,7 +2306,9 @@ var Account = class extends CoValueBase {
2302
2306
  }
2303
2307
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2304
2308
  toJSON() {
2305
- return {};
2309
+ return {
2310
+ $jazz: { id: this.$jazz.id }
2311
+ };
2306
2312
  }
2307
2313
  [inspect2]() {
2308
2314
  return this.toJSON();
@@ -3273,56 +3279,135 @@ function applyCoValueMigrations(instance) {
3273
3279
 
3274
3280
  // src/tools/subscribe/CoValueCoreSubscription.ts
3275
3281
  var CoValueCoreSubscription = class {
3276
- constructor(node, id, listener, skipRetry) {
3277
- this.node = node;
3278
- this.id = id;
3279
- this.listener = listener;
3280
- this.skipRetry = skipRetry;
3282
+ constructor(localNode, id, listener, skipRetry, branch) {
3281
3283
  this._unsubscribe = () => {
3282
3284
  };
3283
3285
  this.unsubscribed = false;
3284
- const entry = this.node.getCoValue(this.id);
3285
- if (entry?.isAvailable()) {
3286
- this.subscribe(entry.getCurrentContent());
3286
+ this.localNode = localNode;
3287
+ this.listener = listener;
3288
+ this.skipRetry = skipRetry;
3289
+ this.branchName = branch?.name;
3290
+ this.branchOwnerId = branch?.owner?.$jazz.raw.id;
3291
+ this.source = localNode.getCoValue(id);
3292
+ this.initializeSubscription();
3293
+ }
3294
+ /**
3295
+ * Main entry point for subscription initialization.
3296
+ * Determines the subscription strategy based on current availability and branch requirements.
3297
+ */
3298
+ initializeSubscription() {
3299
+ const source = this.source;
3300
+ if (source.isAvailable()) {
3301
+ this.handleAvailableSource(source);
3302
+ return;
3303
+ }
3304
+ if (this.branchName) {
3305
+ this.handleBranchCheckout();
3306
+ return;
3307
+ }
3308
+ this.loadCoValue();
3309
+ }
3310
+ /**
3311
+ * Handles the case where the CoValue source is immediately available.
3312
+ * Either subscribes directly or attempts to get the requested branch.
3313
+ */
3314
+ handleAvailableSource(source) {
3315
+ if (!this.branchName) {
3316
+ this.subscribe(source.getCurrentContent());
3317
+ return;
3318
+ }
3319
+ const branch = source.getBranch(this.branchName, this.branchOwnerId);
3320
+ if (branch.isAvailable()) {
3321
+ this.subscribe(branch.getCurrentContent());
3322
+ return;
3287
3323
  } else {
3288
- this.node.loadCoValueCore(this.id, void 0, skipRetry).then((value) => {
3289
- if (this.unsubscribed) return;
3290
- if (value.isAvailable()) {
3291
- this.subscribe(value.getCurrentContent());
3292
- } else {
3293
- this.subscribeToState();
3294
- this.listener("unavailable");
3295
- }
3296
- }).catch((error) => {
3297
- console.error("Unexpected error loading CoValue: ", error);
3298
- this.listener("unavailable");
3299
- });
3324
+ this.handleBranchCheckout();
3300
3325
  }
3301
3326
  }
3302
- subscribeToState() {
3303
- const entry = this.node.getCoValue(this.id);
3304
- const handleStateChange = (core2, unsubFromStateChange2) => {
3305
- if (this.unsubscribed) {
3306
- unsubFromStateChange2();
3307
- return;
3327
+ /**
3328
+ * Attempts to checkout a specific branch of the CoValue.
3329
+ * This is called when the source isn't available but a branch is requested.
3330
+ */
3331
+ handleBranchCheckout() {
3332
+ this.localNode.checkoutBranch(this.source.id, this.branchName, this.branchOwnerId).then((value) => {
3333
+ if (this.unsubscribed) return;
3334
+ if (value !== "unavailable") {
3335
+ this.subscribe(value);
3336
+ } else {
3337
+ this.handleUnavailableBranch();
3308
3338
  }
3309
- if (core2.isAvailable()) {
3310
- this.subscribe(core2.getCurrentContent());
3311
- unsubFromStateChange2();
3339
+ }).catch((error) => {
3340
+ console.error(error);
3341
+ this.emit("unavailable");
3342
+ });
3343
+ }
3344
+ /**
3345
+ * Handles the case where a branch checkout fails.
3346
+ * Determines whether to retry or report unavailability.
3347
+ */
3348
+ handleUnavailableBranch() {
3349
+ const source = this.source;
3350
+ if (source.isAvailable()) {
3351
+ throw new Error("Branch is unavailable");
3352
+ }
3353
+ this.subscribeToUnavailableSource();
3354
+ this.emit("unavailable");
3355
+ }
3356
+ /**
3357
+ * Loads the CoValue core from the network/storage.
3358
+ * This is the fallback strategy when immediate availability fails.
3359
+ */
3360
+ loadCoValue() {
3361
+ this.localNode.loadCoValueCore(this.source.id, void 0, this.skipRetry).then((value) => {
3362
+ if (this.unsubscribed) return;
3363
+ if (value.isAvailable()) {
3364
+ this.subscribe(value.getCurrentContent());
3365
+ } else {
3366
+ this.subscribeToUnavailableSource();
3367
+ this.emit("unavailable");
3368
+ }
3369
+ }).catch((error) => {
3370
+ console.error(error);
3371
+ this.emit("unavailable");
3372
+ });
3373
+ }
3374
+ /**
3375
+ * Subscribes to state changes of an unavailable CoValue source.
3376
+ * This allows the subscription to become active when the source becomes available after a first loading attempt.
3377
+ */
3378
+ subscribeToUnavailableSource() {
3379
+ const source = this.source;
3380
+ const handleStateChange = (_, unsubFromStateChange) => {
3381
+ if (!source.isAvailable()) {
3382
+ return;
3312
3383
  }
3313
- };
3314
- const unsubFromStateChange = entry.subscribe(handleStateChange);
3315
- this._unsubscribe = () => {
3316
3384
  unsubFromStateChange();
3385
+ if (this.branchName) {
3386
+ this.handleBranchCheckout();
3387
+ } else {
3388
+ this.subscribe(source.getCurrentContent());
3389
+ }
3317
3390
  };
3391
+ this._unsubscribe = source.subscribe(handleStateChange);
3318
3392
  }
3393
+ /**
3394
+ * Subscribes to a specific CoValue and notifies the listener.
3395
+ * This is the final step where we actually start receiving updates.
3396
+ */
3319
3397
  subscribe(value) {
3320
3398
  if (this.unsubscribed) return;
3321
3399
  this._unsubscribe = value.subscribe((value2) => {
3322
- this.listener(value2);
3400
+ this.emit(value2);
3323
3401
  });
3402
+ }
3403
+ emit(value) {
3404
+ if (this.unsubscribed) return;
3324
3405
  this.listener(value);
3325
3406
  }
3407
+ /**
3408
+ * Unsubscribes from all active subscriptions and marks the instance as unsubscribed.
3409
+ * This prevents any further operations and ensures proper cleanup.
3410
+ */
3326
3411
  unsubscribe() {
3327
3412
  if (this.unsubscribed) return;
3328
3413
  this.unsubscribed = true;