jazz-tools 0.18.4 → 0.18.6

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 (80) hide show
  1. package/.turbo/turbo-build.log +54 -54
  2. package/CHANGELOG.md +28 -0
  3. package/dist/better-auth/auth/client.d.ts.map +1 -1
  4. package/dist/better-auth/auth/client.js +7 -1
  5. package/dist/better-auth/auth/client.js.map +1 -1
  6. package/dist/better-auth/auth/react.d.ts +0 -2142
  7. package/dist/better-auth/auth/react.d.ts.map +1 -1
  8. package/dist/better-auth/auth/react.js +2 -14
  9. package/dist/better-auth/auth/react.js.map +1 -1
  10. package/dist/better-auth/auth/server.d.ts +21 -1
  11. package/dist/better-auth/auth/server.d.ts.map +1 -1
  12. package/dist/better-auth/auth/server.js +83 -27
  13. package/dist/better-auth/auth/server.js.map +1 -1
  14. package/dist/better-auth/auth/tests/react.test.d.ts +2 -0
  15. package/dist/better-auth/auth/tests/react.test.d.ts.map +1 -0
  16. package/dist/browser/createBrowserContext.d.ts.map +1 -1
  17. package/dist/browser/index.js +7 -0
  18. package/dist/browser/index.js.map +1 -1
  19. package/dist/{chunk-LHQQZH7I.js → chunk-45VKEOXG.js} +123 -81
  20. package/dist/chunk-45VKEOXG.js.map +1 -0
  21. package/dist/index.js +1 -1
  22. package/dist/inspector/{custom-element-WCY6D3QJ.js → custom-element-IBHKHN27.js} +19 -69
  23. package/dist/inspector/custom-element-IBHKHN27.js.map +1 -0
  24. package/dist/inspector/index.d.ts +5 -1
  25. package/dist/inspector/index.d.ts.map +1 -1
  26. package/dist/inspector/index.js +18 -17
  27. package/dist/inspector/index.js.map +1 -1
  28. package/dist/inspector/register-custom-element.js +1 -1
  29. package/dist/inspector/viewer/new-app.d.ts +0 -3
  30. package/dist/inspector/viewer/new-app.d.ts.map +1 -1
  31. package/dist/react-core/index.js +3 -1
  32. package/dist/react-core/index.js.map +1 -1
  33. package/dist/testing.js +2 -2
  34. package/dist/testing.js.map +1 -1
  35. package/dist/tools/coValues/inbox.d.ts +5 -5
  36. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  37. package/dist/tools/implementation/anonymousJazzAgent.d.ts +1 -1
  38. package/dist/tools/implementation/anonymousJazzAgent.d.ts.map +1 -1
  39. package/dist/tools/implementation/zodSchema/coExport.d.ts +2 -0
  40. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  41. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts +4 -0
  42. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts.map +1 -1
  43. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  45. package/dist/worker/index.d.ts +8 -2
  46. package/dist/worker/index.d.ts.map +1 -1
  47. package/dist/worker/index.js +7 -3
  48. package/dist/worker/index.js.map +1 -1
  49. package/package.json +5 -4
  50. package/src/better-auth/auth/client.ts +8 -2
  51. package/src/better-auth/auth/react.tsx +2 -51
  52. package/src/better-auth/auth/server.ts +132 -31
  53. package/src/better-auth/auth/tests/client.test.ts +92 -4
  54. package/src/better-auth/auth/tests/react.test.tsx +43 -0
  55. package/src/better-auth/auth/tests/server.test.ts +317 -51
  56. package/src/browser/createBrowserContext.ts +8 -0
  57. package/src/inspector/custom-element.tsx +1 -1
  58. package/src/inspector/index.tsx +44 -0
  59. package/src/inspector/viewer/new-app.tsx +0 -18
  60. package/src/tools/coValues/inbox.ts +190 -108
  61. package/src/tools/implementation/anonymousJazzAgent.ts +1 -1
  62. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  63. package/src/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.ts +8 -1
  64. package/src/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.ts +0 -1
  65. package/src/tools/implementation/zodSchema/unionUtils.ts +0 -1
  66. package/src/tools/testing.ts +1 -1
  67. package/src/tools/tests/coFeed.test.ts +33 -22
  68. package/src/tools/tests/coList.test-d.ts +17 -0
  69. package/src/tools/tests/coList.test.ts +6 -4
  70. package/src/tools/tests/coMap.record.test-d.ts +18 -0
  71. package/src/tools/tests/coMap.test-d.ts +15 -0
  72. package/src/tools/tests/coMap.test.ts +13 -5
  73. package/src/tools/tests/exportImport.test.ts +3 -1
  74. package/src/tools/tests/groupsAndAccounts.test.ts +56 -44
  75. package/src/tools/tests/inbox.test.ts +293 -31
  76. package/src/worker/index.ts +15 -5
  77. package/tsup.config.ts +1 -1
  78. package/dist/chunk-LHQQZH7I.js.map +0 -1
  79. package/dist/inspector/custom-element-WCY6D3QJ.js.map +0 -1
  80. package/src/inspector/index.ts +0 -23
@@ -2677,7 +2677,9 @@ var Profile = class extends CoMap {
2677
2677
  };
2678
2678
 
2679
2679
  // src/tools/coValues/inbox.ts
2680
- import { RawAccount as RawAccount2 } from "cojson";
2680
+ import {
2681
+ RawAccount as RawAccount2
2682
+ } from "cojson";
2681
2683
 
2682
2684
  // src/tools/lib/id.ts
2683
2685
  function isCoValueId(id) {
@@ -2704,6 +2706,20 @@ function createInboxRoot(account) {
2704
2706
  inviteLink
2705
2707
  };
2706
2708
  }
2709
+ var IncrementalFeed = class {
2710
+ constructor(feed) {
2711
+ this.feed = feed;
2712
+ this.sessions = {};
2713
+ }
2714
+ getNewItems() {
2715
+ const items = this.feed.getValidTransactions({
2716
+ ignorePrivateTransactions: false,
2717
+ from: this.sessions
2718
+ });
2719
+ this.sessions = this.feed.knownState().sessions;
2720
+ return items;
2721
+ }
2722
+ };
2707
2723
  async function createInboxMessage(payload, inboxOwner) {
2708
2724
  const group = payload.$jazz.raw.group;
2709
2725
  if (group instanceof RawAccount2) {
@@ -2720,108 +2736,133 @@ async function createInboxMessage(payload, inboxOwner) {
2720
2736
  await message.core.waitForSync();
2721
2737
  return message;
2722
2738
  }
2739
+ var MessageQueue = class {
2740
+ constructor(concurrencyLimit = 10, processMessage, handleError) {
2741
+ this.processMessage = processMessage;
2742
+ this.handleError = handleError;
2743
+ this.queue = [];
2744
+ this.processing = /* @__PURE__ */ new Set();
2745
+ this.activeCount = 0;
2746
+ this.concurrencyLimit = concurrencyLimit;
2747
+ }
2748
+ enqueue(txKey, messageId) {
2749
+ this.queue.push({ txKey, messageId });
2750
+ this.processNext();
2751
+ }
2752
+ async processNext() {
2753
+ if (this.activeCount >= this.concurrencyLimit || this.queue.length === 0) {
2754
+ return;
2755
+ }
2756
+ const { txKey, messageId } = this.queue.shift();
2757
+ if (this.processing.has(txKey)) {
2758
+ this.processNext();
2759
+ return;
2760
+ }
2761
+ this.processing.add(txKey);
2762
+ this.activeCount++;
2763
+ try {
2764
+ await this.processMessage(txKey, messageId);
2765
+ } catch (error) {
2766
+ this.handleError(txKey, messageId, error);
2767
+ } finally {
2768
+ this.processing.delete(txKey);
2769
+ this.activeCount--;
2770
+ this.processNext();
2771
+ }
2772
+ }
2773
+ };
2723
2774
  var Inbox = class _Inbox {
2724
2775
  constructor(account, root, messages, processed, failed) {
2725
- this.processing = /* @__PURE__ */ new Set();
2726
2776
  this.account = account;
2727
2777
  this.root = root;
2728
2778
  this.messages = messages;
2729
2779
  this.processed = processed;
2730
2780
  this.failed = failed;
2731
2781
  }
2732
- subscribe(Schema4, callback, options = {}) {
2782
+ subscribe(Schema4, callback, options) {
2733
2783
  const processed = /* @__PURE__ */ new Set();
2734
- const failed = /* @__PURE__ */ new Map();
2735
2784
  const node = this.account.$jazz.localNode;
2736
- this.processed.subscribe((stream) => {
2737
- for (const items of Object.values(stream.items)) {
2738
- for (const item of items) {
2739
- processed.add(item.value);
2740
- }
2785
+ const concurrencyLimit = options?.concurrencyLimit ?? 10;
2786
+ const processedFeed = new IncrementalFeed(this.processed.core);
2787
+ this.processed.subscribe(() => {
2788
+ for (const { changes } of processedFeed.getNewItems()) {
2789
+ processed.add(changes[0]);
2741
2790
  }
2742
2791
  });
2743
2792
  const { account } = this;
2744
- const { retries = 3 } = options;
2745
- let failTimer = void 0;
2746
- const clearFailTimer = () => {
2747
- clearTimeout(failTimer);
2748
- failTimer = void 0;
2793
+ const messagesFeed = new IncrementalFeed(this.messages.core);
2794
+ const processMessage = async (txKey, messageId) => {
2795
+ const message = await node.load(messageId);
2796
+ if (message === "unavailable") {
2797
+ throw new Error(`Inbox: message ${messageId} is unavailable`);
2798
+ }
2799
+ const value = await loadCoValue(
2800
+ coValueClassFromCoValueClassOrSchema(Schema4),
2801
+ message.get("payload"),
2802
+ {
2803
+ loadAs: account
2804
+ }
2805
+ );
2806
+ if (!value) {
2807
+ throw new Error(
2808
+ `Inbox: Unable to load the payload of message ${messageId}`
2809
+ );
2810
+ }
2811
+ const accountID = getAccountIDfromSessionID(
2812
+ txKey.split("/")[0]
2813
+ );
2814
+ if (!accountID) {
2815
+ throw new Error(`Inbox: Unknown account for message ${messageId}`);
2816
+ }
2817
+ const result = await callback(value, accountID);
2818
+ const inboxMessage = node.expectCoValueLoaded(messageId).getCurrentContent();
2819
+ if (result) {
2820
+ inboxMessage.set("result", result.$jazz.id);
2821
+ }
2822
+ inboxMessage.set("processed", true);
2823
+ this.processed.push(txKey);
2824
+ };
2825
+ const handleError = (txKey, messageId, error) => {
2826
+ console.error(error);
2827
+ const stringifiedError = String(error);
2828
+ this.processed.push(txKey);
2829
+ this.failed.push({ errors: [stringifiedError], value: messageId });
2830
+ try {
2831
+ const inboxMessage = node.expectCoValueLoaded(messageId).getCurrentContent();
2832
+ inboxMessage.set("error", stringifiedError);
2833
+ inboxMessage.set("processed", true);
2834
+ } catch (error2) {
2835
+ }
2749
2836
  };
2750
- const handleNewMessages = (stream) => {
2751
- clearFailTimer();
2752
- for (const [sessionID, items] of Object.entries(stream.items)) {
2753
- const accountID = getAccountIDfromSessionID(sessionID);
2837
+ const messageQueue = new MessageQueue(
2838
+ concurrencyLimit,
2839
+ processMessage,
2840
+ handleError
2841
+ );
2842
+ const handleNewMessages = () => {
2843
+ for (const tx of messagesFeed.getNewItems()) {
2844
+ const accountID = getAccountIDfromSessionID(tx.txID.sessionID);
2754
2845
  if (!accountID) {
2755
- console.warn("Received message from unknown account", sessionID);
2846
+ console.warn(
2847
+ "Received message from unknown account",
2848
+ tx.txID.sessionID
2849
+ );
2756
2850
  continue;
2757
2851
  }
2758
- for (const item of items) {
2759
- const txKey = `${sessionID}/${item.tx.txIndex}`;
2760
- if (!processed.has(txKey) && !this.processing.has(txKey)) {
2761
- this.processing.add(txKey);
2762
- const id = item.value;
2763
- node.load(id).then((message) => {
2764
- if (message === "unavailable") {
2765
- return Promise.reject(
2766
- new Error("Unable to load inbox message " + id)
2767
- );
2768
- }
2769
- return loadCoValue(
2770
- coValueClassFromCoValueClassOrSchema(Schema4),
2771
- message.get("payload"),
2772
- {
2773
- loadAs: account
2774
- }
2775
- );
2776
- }).then((value) => {
2777
- if (!value) {
2778
- return Promise.reject(
2779
- new Error("Unable to load inbox message " + id)
2780
- );
2781
- }
2782
- return callback(value, accountID);
2783
- }).then((result) => {
2784
- const inboxMessage = node.expectCoValueLoaded(item.value).getCurrentContent();
2785
- if (result) {
2786
- inboxMessage.set("result", result.$jazz.id);
2787
- }
2788
- inboxMessage.set("processed", true);
2789
- this.processed.push(txKey);
2790
- this.processing.delete(txKey);
2791
- }).catch((error) => {
2792
- console.error("Error processing inbox message", error);
2793
- this.processing.delete(txKey);
2794
- const errors = failed.get(txKey) ?? [];
2795
- const stringifiedError = String(error);
2796
- errors.push(stringifiedError);
2797
- let inboxMessage;
2798
- try {
2799
- inboxMessage = node.expectCoValueLoaded(item.value).getCurrentContent();
2800
- inboxMessage.set("error", stringifiedError);
2801
- } catch (error2) {
2802
- }
2803
- if (errors.length > retries) {
2804
- inboxMessage?.set("processed", true);
2805
- this.processed.push(txKey);
2806
- this.failed.push({ errors, value: item.value });
2807
- } else {
2808
- failed.set(txKey, errors);
2809
- if (!failTimer) {
2810
- failTimer = setTimeout(
2811
- () => handleNewMessages(stream),
2812
- 100
2813
- );
2814
- }
2815
- }
2816
- });
2817
- }
2852
+ const id = tx.changes[0];
2853
+ if (!isCoValueId(id)) {
2854
+ continue;
2855
+ }
2856
+ const txKey = `${tx.txID.sessionID}/${tx.txID.txIndex}`;
2857
+ if (processed.has(txKey)) {
2858
+ continue;
2818
2859
  }
2860
+ messageQueue.enqueue(txKey, id);
2819
2861
  }
2820
2862
  };
2821
2863
  const unsubscribe = this.messages.subscribe(handleNewMessages);
2822
2864
  return () => {
2823
2865
  unsubscribe();
2824
- clearFailTimer();
2825
2866
  };
2826
2867
  }
2827
2868
  static async load(account) {
@@ -2845,6 +2886,7 @@ var Inbox = class _Inbox {
2845
2886
  if (messages === "unavailable" || processed === "unavailable" || failed === "unavailable") {
2846
2887
  throw new Error("Inbox not found");
2847
2888
  }
2889
+ await processed.core.waitForFullStreaming();
2848
2890
  return new _Inbox(account, root, messages, processed, failed);
2849
2891
  }
2850
2892
  };
@@ -5816,4 +5858,4 @@ export {
5816
5858
  JazzContextManager
5817
5859
  };
5818
5860
  /* istanbul ignore file -- @preserve */
5819
- //# sourceMappingURL=chunk-LHQQZH7I.js.map
5861
+ //# sourceMappingURL=chunk-45VKEOXG.js.map