@symbo.ls/sdk 3.1.2 → 3.2.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 (187) hide show
  1. package/README.md +143 -2
  2. package/dist/cjs/config/environment.js +98 -30
  3. package/dist/cjs/index.js +144 -24
  4. package/dist/cjs/services/AdminService.js +351 -0
  5. package/dist/cjs/services/AuthService.js +738 -305
  6. package/dist/cjs/services/BaseService.js +158 -6
  7. package/dist/cjs/services/BranchService.js +484 -0
  8. package/dist/cjs/services/CollabService.js +743 -0
  9. package/dist/cjs/services/DnsService.js +340 -0
  10. package/dist/cjs/services/FeatureFlagService.js +175 -0
  11. package/dist/cjs/services/FileService.js +201 -0
  12. package/dist/cjs/services/IntegrationService.js +538 -0
  13. package/dist/cjs/services/MetricsService.js +62 -0
  14. package/dist/cjs/services/PaymentService.js +271 -0
  15. package/dist/cjs/services/PlanService.js +426 -0
  16. package/dist/cjs/services/ProjectService.js +1207 -0
  17. package/dist/cjs/services/PullRequestService.js +503 -0
  18. package/dist/cjs/services/ScreenshotService.js +304 -0
  19. package/dist/cjs/services/SubscriptionService.js +396 -0
  20. package/dist/cjs/services/TrackingService.js +661 -0
  21. package/dist/cjs/services/WaitlistService.js +148 -0
  22. package/dist/cjs/services/index.js +64 -16
  23. package/dist/cjs/state/RootStateManager.js +65 -0
  24. package/dist/cjs/state/rootEventBus.js +74 -0
  25. package/dist/cjs/utils/CollabClient.js +223 -0
  26. package/dist/cjs/utils/TokenManager.js +78 -30
  27. package/dist/cjs/utils/changePreprocessor.js +199 -0
  28. package/dist/cjs/utils/jsonDiff.js +145 -0
  29. package/dist/cjs/utils/ordering.js +309 -0
  30. package/dist/cjs/utils/services.js +301 -103
  31. package/dist/cjs/utils/validation.js +0 -3
  32. package/dist/esm/config/environment.js +98 -30
  33. package/dist/esm/index.js +49505 -8718
  34. package/dist/esm/services/AdminService.js +1132 -0
  35. package/dist/esm/services/AuthService.js +1493 -386
  36. package/dist/esm/services/BaseService.js +757 -6
  37. package/dist/esm/services/BranchService.js +1265 -0
  38. package/dist/esm/services/CollabService.js +26895 -0
  39. package/dist/esm/services/DnsService.js +1121 -0
  40. package/dist/esm/services/FeatureFlagService.js +956 -0
  41. package/dist/esm/services/FileService.js +982 -0
  42. package/dist/esm/services/IntegrationService.js +1319 -0
  43. package/dist/esm/services/MetricsService.js +843 -0
  44. package/dist/esm/services/PaymentService.js +1052 -0
  45. package/dist/esm/services/PlanService.js +1207 -0
  46. package/dist/esm/services/ProjectService.js +2526 -0
  47. package/dist/esm/services/PullRequestService.js +1284 -0
  48. package/dist/esm/services/ScreenshotService.js +1085 -0
  49. package/dist/esm/services/SubscriptionService.js +1177 -0
  50. package/dist/esm/services/TrackingService.js +18454 -0
  51. package/dist/esm/services/WaitlistService.js +929 -0
  52. package/dist/esm/services/index.js +49062 -8569
  53. package/dist/esm/state/RootStateManager.js +90 -0
  54. package/dist/esm/state/rootEventBus.js +56 -0
  55. package/dist/esm/utils/CollabClient.js +18889 -0
  56. package/dist/esm/utils/TokenManager.js +78 -30
  57. package/dist/esm/utils/changePreprocessor.js +542 -0
  58. package/dist/esm/utils/jsonDiff.js +7011 -0
  59. package/dist/esm/utils/ordering.js +291 -0
  60. package/dist/esm/utils/services.js +301 -103
  61. package/dist/esm/utils/validation.js +116 -50
  62. package/dist/node/config/environment.js +98 -30
  63. package/dist/node/index.js +175 -32
  64. package/dist/node/services/AdminService.js +332 -0
  65. package/dist/node/services/AuthService.js +742 -310
  66. package/dist/node/services/BaseService.js +148 -6
  67. package/dist/node/services/BranchService.js +465 -0
  68. package/dist/node/services/CollabService.js +724 -0
  69. package/dist/node/services/DnsService.js +321 -0
  70. package/dist/node/services/FeatureFlagService.js +156 -0
  71. package/dist/node/services/FileService.js +182 -0
  72. package/dist/node/services/IntegrationService.js +519 -0
  73. package/dist/node/services/MetricsService.js +43 -0
  74. package/dist/node/services/PaymentService.js +252 -0
  75. package/dist/node/services/PlanService.js +407 -0
  76. package/dist/node/services/ProjectService.js +1188 -0
  77. package/dist/node/services/PullRequestService.js +484 -0
  78. package/dist/node/services/ScreenshotService.js +285 -0
  79. package/dist/node/services/SubscriptionService.js +377 -0
  80. package/dist/node/services/TrackingService.js +632 -0
  81. package/dist/node/services/WaitlistService.js +129 -0
  82. package/dist/node/services/index.js +64 -16
  83. package/dist/node/state/RootStateManager.js +36 -0
  84. package/dist/node/state/rootEventBus.js +55 -0
  85. package/dist/node/utils/CollabClient.js +194 -0
  86. package/dist/node/utils/TokenManager.js +78 -30
  87. package/dist/node/utils/changePreprocessor.js +180 -0
  88. package/dist/node/utils/jsonDiff.js +116 -0
  89. package/dist/node/utils/ordering.js +290 -0
  90. package/dist/node/utils/services.js +301 -103
  91. package/dist/node/utils/validation.js +0 -3
  92. package/package.json +39 -21
  93. package/src/config/environment.js +99 -28
  94. package/src/index.js +181 -36
  95. package/src/services/AdminService.js +374 -0
  96. package/src/services/AuthService.js +874 -328
  97. package/src/services/BaseService.js +166 -6
  98. package/src/services/BranchService.js +536 -0
  99. package/src/services/CollabService.js +900 -0
  100. package/src/services/DnsService.js +366 -0
  101. package/src/services/FeatureFlagService.js +174 -0
  102. package/src/services/FileService.js +213 -0
  103. package/src/services/IntegrationService.js +548 -0
  104. package/src/services/MetricsService.js +40 -0
  105. package/src/services/PaymentService.js +287 -0
  106. package/src/services/PlanService.js +468 -0
  107. package/src/services/ProjectService.js +1366 -0
  108. package/src/services/PullRequestService.js +537 -0
  109. package/src/services/ScreenshotService.js +258 -0
  110. package/src/services/SubscriptionService.js +425 -0
  111. package/src/services/TrackingService.js +853 -0
  112. package/src/services/WaitlistService.js +130 -0
  113. package/src/services/index.js +80 -13
  114. package/src/services/tests/BranchService/createBranch.test.js +153 -0
  115. package/src/services/tests/BranchService/deleteBranch.test.js +173 -0
  116. package/src/services/tests/BranchService/getBranchChanges.test.js +146 -0
  117. package/src/services/tests/BranchService/listBranches.test.js +87 -0
  118. package/src/services/tests/BranchService/mergeBranch.test.js +210 -0
  119. package/src/services/tests/BranchService/publishVersion.test.js +183 -0
  120. package/src/services/tests/BranchService/renameBranch.test.js +240 -0
  121. package/src/services/tests/BranchService/resetBranch.test.js +152 -0
  122. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +67 -0
  123. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +75 -0
  124. package/src/services/tests/FileService/createFileFormData.test.js +74 -0
  125. package/src/services/tests/FileService/getFileUrl.test.js +69 -0
  126. package/src/services/tests/FileService/updateProjectIcon.test.js +109 -0
  127. package/src/services/tests/FileService/uploadDocument.test.js +36 -0
  128. package/src/services/tests/FileService/uploadFile.test.js +78 -0
  129. package/src/services/tests/FileService/uploadFileWithValidation.test.js +114 -0
  130. package/src/services/tests/FileService/uploadImage.test.js +36 -0
  131. package/src/services/tests/FileService/uploadMultipleFiles.test.js +111 -0
  132. package/src/services/tests/FileService/validateFile.test.js +63 -0
  133. package/src/services/tests/PlanService/createPlan.test.js +104 -0
  134. package/src/services/tests/PlanService/createPlanWithValidation.test.js +523 -0
  135. package/src/services/tests/PlanService/deletePlan.test.js +92 -0
  136. package/src/services/tests/PlanService/getActivePlans.test.js +123 -0
  137. package/src/services/tests/PlanService/getAdminPlans.test.js +84 -0
  138. package/src/services/tests/PlanService/getPlan.test.js +50 -0
  139. package/src/services/tests/PlanService/getPlanByKey.test.js +109 -0
  140. package/src/services/tests/PlanService/getPlanWithValidation.test.js +85 -0
  141. package/src/services/tests/PlanService/getPlans.test.js +53 -0
  142. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +109 -0
  143. package/src/services/tests/PlanService/getPlansWithValidation.test.js +48 -0
  144. package/src/services/tests/PlanService/initializePlans.test.js +75 -0
  145. package/src/services/tests/PlanService/updatePlan.test.js +111 -0
  146. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +556 -0
  147. package/src/state/RootStateManager.js +76 -0
  148. package/src/state/rootEventBus.js +67 -0
  149. package/src/utils/CollabClient.js +248 -0
  150. package/src/utils/TokenManager.js +88 -33
  151. package/src/utils/changePreprocessor.js +239 -0
  152. package/src/utils/jsonDiff.js +144 -0
  153. package/src/utils/ordering.js +271 -0
  154. package/src/utils/services.js +326 -107
  155. package/src/utils/validation.js +0 -3
  156. package/dist/cjs/services/AIService.js +0 -155
  157. package/dist/cjs/services/BasedService.js +0 -1185
  158. package/dist/cjs/services/CoreService.js +0 -1751
  159. package/dist/cjs/services/SocketIOService.js +0 -307
  160. package/dist/cjs/services/SocketService.js +0 -161
  161. package/dist/cjs/services/SymstoryService.js +0 -571
  162. package/dist/cjs/utils/basedQuerys.js +0 -181
  163. package/dist/cjs/utils/symstoryClient.js +0 -259
  164. package/dist/esm/services/AIService.js +0 -185
  165. package/dist/esm/services/BasedService.js +0 -5278
  166. package/dist/esm/services/CoreService.js +0 -2264
  167. package/dist/esm/services/SocketIOService.js +0 -470
  168. package/dist/esm/services/SocketService.js +0 -191
  169. package/dist/esm/services/SymstoryService.js +0 -7041
  170. package/dist/esm/utils/basedQuerys.js +0 -163
  171. package/dist/esm/utils/symstoryClient.js +0 -370
  172. package/dist/node/services/AIService.js +0 -136
  173. package/dist/node/services/BasedService.js +0 -1156
  174. package/dist/node/services/CoreService.js +0 -1722
  175. package/dist/node/services/SocketIOService.js +0 -278
  176. package/dist/node/services/SocketService.js +0 -142
  177. package/dist/node/services/SymstoryService.js +0 -542
  178. package/dist/node/utils/basedQuerys.js +0 -162
  179. package/dist/node/utils/symstoryClient.js +0 -230
  180. package/src/services/AIService.js +0 -150
  181. package/src/services/BasedService.js +0 -1301
  182. package/src/services/CoreService.js +0 -1943
  183. package/src/services/SocketIOService.js +0 -334
  184. package/src/services/SocketService.js +0 -168
  185. package/src/services/SymstoryService.js +0 -649
  186. package/src/utils/basedQuerys.js +0 -164
  187. package/src/utils/symstoryClient.js +0 -252
@@ -26,7 +26,7 @@ class TokenManager {
26
26
  constructor(options = {}) {
27
27
  this.config = {
28
28
  storagePrefix: "symbols_",
29
- storageType: "localStorage",
29
+ storageType: typeof window === "undefined" || process.env.NODE_ENV === "test" || process.env.NODE_ENV === "testing" ? "memory" : "localStorage",
30
30
  // 'localStorage' | 'sessionStorage' | 'memory'
31
31
  refreshBuffer: 60 * 1e3,
32
32
  // Refresh 1 minute before expiry
@@ -66,13 +66,26 @@ class TokenManager {
66
66
  if (typeof window === "undefined") {
67
67
  return this._memoryStorage;
68
68
  }
69
+ const safeGetStorage = (provider) => {
70
+ try {
71
+ const storage = provider();
72
+ const testKey = `${this.config.storagePrefix}__tm_test__`;
73
+ storage.setItem(testKey, "1");
74
+ storage.removeItem(testKey);
75
+ return storage;
76
+ } catch {
77
+ return null;
78
+ }
79
+ };
80
+ const localStorageInstance = safeGetStorage(() => window.localStorage);
81
+ const sessionStorageInstance = safeGetStorage(() => window.sessionStorage);
69
82
  switch (this.config.storageType) {
70
83
  case "sessionStorage":
71
- return window.sessionStorage;
84
+ return sessionStorageInstance || this._memoryStorage;
72
85
  case "memory":
73
86
  return this._memoryStorage;
74
87
  default:
75
- return window.localStorage;
88
+ return localStorageInstance || this._memoryStorage;
76
89
  }
77
90
  }
78
91
  /**
@@ -153,7 +166,28 @@ class TokenManager {
153
166
  return true;
154
167
  }
155
168
  const now = Date.now();
156
- return now < this.tokens.expiresAt - this.config.refreshBuffer;
169
+ const isValid = now < this.tokens.expiresAt - this.config.refreshBuffer;
170
+ if (!isValid) {
171
+ console.log("[TokenManager] Access token is expired or near expiry:", {
172
+ now: new Date(now).toISOString(),
173
+ expiresAt: new Date(this.tokens.expiresAt).toISOString(),
174
+ refreshBuffer: this.config.refreshBuffer
175
+ });
176
+ }
177
+ return isValid;
178
+ }
179
+ /**
180
+ * Check if access token exists and is not expired (without refresh buffer)
181
+ */
182
+ isAccessTokenActuallyValid() {
183
+ if (!this.tokens.accessToken) {
184
+ return false;
185
+ }
186
+ if (!this.tokens.expiresAt) {
187
+ return true;
188
+ }
189
+ const now = Date.now();
190
+ return now < this.tokens.expiresAt;
157
191
  }
158
192
  /**
159
193
  * Check if tokens exist (regardless of expiry)
@@ -279,40 +313,54 @@ class TokenManager {
279
313
  * Save tokens to storage
280
314
  */
281
315
  saveTokens() {
282
- const { storage } = this;
283
- const keys = this.storageKeys;
284
- if (this.tokens.accessToken) {
285
- storage.setItem(keys.accessToken, this.tokens.accessToken);
286
- }
287
- if (this.tokens.refreshToken) {
288
- storage.setItem(keys.refreshToken, this.tokens.refreshToken);
289
- }
290
- if (this.tokens.expiresAt) {
291
- storage.setItem(keys.expiresAt, this.tokens.expiresAt.toString());
292
- }
293
- if (this.tokens.expiresIn) {
294
- storage.setItem(keys.expiresIn, this.tokens.expiresIn.toString());
316
+ try {
317
+ const { storage } = this;
318
+ const keys = this.storageKeys;
319
+ if (this.tokens.accessToken) {
320
+ storage.setItem(keys.accessToken, this.tokens.accessToken);
321
+ }
322
+ if (this.tokens.refreshToken) {
323
+ storage.setItem(keys.refreshToken, this.tokens.refreshToken);
324
+ }
325
+ if (this.tokens.expiresAt) {
326
+ storage.setItem(keys.expiresAt, this.tokens.expiresAt.toString());
327
+ }
328
+ if (this.tokens.expiresIn) {
329
+ storage.setItem(keys.expiresIn, this.tokens.expiresIn.toString());
330
+ }
331
+ } catch (error) {
332
+ console.error("[TokenManager] Error saving tokens to storage:", error);
295
333
  }
296
334
  }
297
335
  /**
298
336
  * Load tokens from storage
299
337
  */
300
338
  loadTokens() {
301
- const { storage } = this;
302
- const keys = this.storageKeys;
303
- const accessToken = storage.getItem(keys.accessToken);
304
- const refreshToken = storage.getItem(keys.refreshToken);
305
- const expiresAt = storage.getItem(keys.expiresAt);
306
- const expiresIn = storage.getItem(keys.expiresIn);
307
- if (accessToken) {
339
+ try {
340
+ const { storage } = this;
341
+ const keys = this.storageKeys;
342
+ const accessToken = storage.getItem(keys.accessToken);
343
+ const refreshToken = storage.getItem(keys.refreshToken);
344
+ const expiresAt = storage.getItem(keys.expiresAt);
345
+ const expiresIn = storage.getItem(keys.expiresIn);
346
+ if (accessToken) {
347
+ this.tokens = {
348
+ accessToken,
349
+ refreshToken,
350
+ expiresAt: expiresAt ? parseInt(expiresAt, 10) : null,
351
+ expiresIn: expiresIn ? parseInt(expiresIn, 10) : null,
352
+ tokenType: "Bearer"
353
+ };
354
+ this.scheduleRefresh();
355
+ }
356
+ } catch (error) {
357
+ console.error("[TokenManager] Error loading tokens from storage:", error);
308
358
  this.tokens = {
309
- accessToken,
310
- refreshToken,
311
- expiresAt: expiresAt ? parseInt(expiresAt, 10) : null,
312
- expiresIn: expiresIn ? parseInt(expiresIn, 10) : null,
313
- tokenType: "Bearer"
359
+ accessToken: null,
360
+ refreshToken: null,
361
+ expiresAt: null,
362
+ expiresIn: null
314
363
  };
315
- this.scheduleRefresh();
316
364
  }
317
365
  }
318
366
  /**
@@ -0,0 +1,199 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var changePreprocessor_exports = {};
19
+ __export(changePreprocessor_exports, {
20
+ preprocessChanges: () => preprocessChanges
21
+ });
22
+ module.exports = __toCommonJS(changePreprocessor_exports);
23
+ var import_jsonDiff = require("./jsonDiff.js");
24
+ var import_ordering = require("./ordering.js");
25
+ function isPlainObject(val) {
26
+ return val && typeof val === "object" && !Array.isArray(val);
27
+ }
28
+ function getByPathSafe(root, path) {
29
+ if (!root || typeof root.getByPath !== "function") {
30
+ return null;
31
+ }
32
+ try {
33
+ return root.getByPath(path);
34
+ } catch {
35
+ return null;
36
+ }
37
+ }
38
+ function resolveNextValueFromTuples(tuples, path) {
39
+ if (!Array.isArray(tuples) || !Array.isArray(path)) {
40
+ return null;
41
+ }
42
+ for (let i = tuples.length - 1; i >= 0; i--) {
43
+ const t = tuples[i];
44
+ if (!Array.isArray(t) || t.length < 3) {
45
+ continue;
46
+ }
47
+ const [action, tuplePath, tupleValue] = t;
48
+ if (action !== "update" && action !== "set" || !Array.isArray(tuplePath)) {
49
+ continue;
50
+ }
51
+ if (tuplePath.length > path.length) {
52
+ continue;
53
+ }
54
+ let isPrefix = true;
55
+ for (let j = 0; j < tuplePath.length; j++) {
56
+ if (tuplePath[j] !== path[j]) {
57
+ isPrefix = false;
58
+ break;
59
+ }
60
+ }
61
+ if (!isPrefix) {
62
+ continue;
63
+ }
64
+ if (tuplePath.length === path.length) {
65
+ return tupleValue;
66
+ }
67
+ let current = tupleValue;
68
+ for (let j = tuplePath.length; j < path.length; j++) {
69
+ if (current == null) {
70
+ return null;
71
+ }
72
+ current = current[path[j]];
73
+ }
74
+ if (current !== null) {
75
+ return current;
76
+ }
77
+ }
78
+ return null;
79
+ }
80
+ function preprocessChanges(root, tuples = [], options = {}) {
81
+ const expandTuple = (t) => {
82
+ const [action, path, value] = t || [];
83
+ const isSchemaPath = Array.isArray(path) && path[0] === "schema";
84
+ const isFilesPath = Array.isArray(path) && path[0] === "files";
85
+ if (action === "delete") {
86
+ return [t];
87
+ }
88
+ const canConsiderExpansion = action === "update" && Array.isArray(path) && (path.length === 1 || path.length === 2 || isSchemaPath && path.length === 3) && isPlainObject(value);
89
+ if (!canConsiderExpansion || isFilesPath || value && value.type === "files") {
90
+ return [t];
91
+ }
92
+ const prevRaw = getByPathSafe(root, path);
93
+ const isCreatePath = Array.isArray(path) && action === "update" && // e.g. ['update', ['components', 'NewKey'], {...}]
94
+ (!isSchemaPath && path.length === 2 || // e.g. ['update', ['schema', 'components', 'NewKey'], {...}]
95
+ isSchemaPath && path.length === 3) && (prevRaw === null || typeof prevRaw === "undefined");
96
+ if (isCreatePath) {
97
+ return [t];
98
+ }
99
+ const prev = prevRaw || {};
100
+ const next = value || {};
101
+ if (!isPlainObject(prev) || !isPlainObject(next)) {
102
+ return [t];
103
+ }
104
+ const ops = (0, import_jsonDiff.diffJson)(prev, next, []);
105
+ if (!ops.length) {
106
+ return [t];
107
+ }
108
+ const out = [];
109
+ for (let i = 0; i < ops.length; i++) {
110
+ const op = ops[i];
111
+ const fullPath = [...path, ...op.path];
112
+ const last = fullPath[fullPath.length - 1];
113
+ if (op.action === "set") {
114
+ out.push(["update", fullPath, op.value]);
115
+ } else if (op.action === "del") {
116
+ if (last !== "__order") {
117
+ out.push(["delete", fullPath]);
118
+ }
119
+ }
120
+ }
121
+ return out;
122
+ };
123
+ const minimizeTuples = (input) => {
124
+ const out = [];
125
+ const seen2 = /* @__PURE__ */ new Set();
126
+ for (let i = 0; i < input.length; i++) {
127
+ const expanded = expandTuple(input[i]);
128
+ for (let k = 0; k < expanded.length; k++) {
129
+ const tuple = expanded[k];
130
+ const isDelete = Array.isArray(tuple) && tuple[0] === "delete";
131
+ const isOrderKey = isDelete && Array.isArray(tuple[1]) && tuple[1][tuple[1].length - 1] === "__order";
132
+ if (!isOrderKey) {
133
+ const key = JSON.stringify(tuple);
134
+ if (!seen2.has(key)) {
135
+ seen2.add(key);
136
+ out.push(tuple);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ return out;
142
+ };
143
+ const granularChanges = (() => {
144
+ try {
145
+ const res = minimizeTuples(tuples);
146
+ if (options.append && options.append.length) {
147
+ res.push(...options.append);
148
+ }
149
+ return res;
150
+ } catch {
151
+ return Array.isArray(tuples) ? tuples.slice() : [];
152
+ }
153
+ })();
154
+ const hydratedGranularChanges = granularChanges.map((t) => {
155
+ if (!Array.isArray(t) || t.length < 3) {
156
+ return t;
157
+ }
158
+ const [action, path] = t;
159
+ if (action !== "update" && action !== "set" || !Array.isArray(path)) {
160
+ return t;
161
+ }
162
+ const nextValue = resolveNextValueFromTuples(tuples, path);
163
+ if (nextValue === null) {
164
+ return t;
165
+ }
166
+ return [action, path, nextValue];
167
+ });
168
+ const baseOrders = (0, import_ordering.computeOrdersForTuples)(root, hydratedGranularChanges);
169
+ const preferOrdersMap = /* @__PURE__ */ new Map();
170
+ for (let i = 0; i < tuples.length; i++) {
171
+ const t = tuples[i];
172
+ if (!Array.isArray(t) || t.length < 3) {
173
+ continue;
174
+ }
175
+ const [action, path, value] = t;
176
+ const isFilesPath = Array.isArray(path) && path[0] === "files";
177
+ if (action !== "update" || !Array.isArray(path) || path.length !== 1 && path.length !== 2 || !isPlainObject(value) || isFilesPath || value && value.type === "files") {
178
+ continue;
179
+ }
180
+ const keys = Object.keys(value).filter((k) => k !== "__order");
181
+ const key = JSON.stringify(path);
182
+ preferOrdersMap.set(key, { path, keys });
183
+ }
184
+ const mergedOrders = [];
185
+ const seen = /* @__PURE__ */ new Set();
186
+ preferOrdersMap.forEach((v, k) => {
187
+ seen.add(k);
188
+ mergedOrders.push(v);
189
+ });
190
+ for (let i = 0; i < baseOrders.length; i++) {
191
+ const v = baseOrders[i];
192
+ const k = JSON.stringify(v.path);
193
+ if (!seen.has(k)) {
194
+ seen.add(k);
195
+ mergedOrders.push(v);
196
+ }
197
+ }
198
+ return { granularChanges: hydratedGranularChanges, orders: mergedOrders };
199
+ }
@@ -0,0 +1,145 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var jsonDiff_exports = {};
29
+ __export(jsonDiff_exports, {
30
+ applyOpsToJson: () => applyOpsToJson,
31
+ diffJson: () => diffJson
32
+ });
33
+ module.exports = __toCommonJS(jsonDiff_exports);
34
+ var Y = __toESM(require("yjs"), 1);
35
+ function isPlainObject(o) {
36
+ return o && typeof o === "object" && !Array.isArray(o);
37
+ }
38
+ function deepEqual(a, b) {
39
+ if (Object.is(a, b)) {
40
+ return true;
41
+ }
42
+ if (typeof a === "function" && typeof b === "function") {
43
+ try {
44
+ return a.toString() === b.toString();
45
+ } catch {
46
+ return false;
47
+ }
48
+ }
49
+ if (typeof a === "function" || typeof b === "function") {
50
+ return false;
51
+ }
52
+ if (a instanceof Date && b instanceof Date) {
53
+ return a.getTime() === b.getTime();
54
+ }
55
+ if (a instanceof RegExp && b instanceof RegExp) {
56
+ return String(a) === String(b);
57
+ }
58
+ if (Array.isArray(a) && Array.isArray(b)) {
59
+ if (a.length !== b.length) {
60
+ return false;
61
+ }
62
+ for (let i = 0; i < a.length; i++) {
63
+ if (!deepEqual(a[i], b[i])) {
64
+ return false;
65
+ }
66
+ }
67
+ return true;
68
+ }
69
+ if (a && b && typeof a === "object" && typeof b === "object") {
70
+ const aKeys = Object.keys(a);
71
+ const bKeys = Object.keys(b);
72
+ if (aKeys.length !== bKeys.length) {
73
+ return false;
74
+ }
75
+ for (let i = 0; i < aKeys.length; i++) {
76
+ const key = aKeys[i];
77
+ if (!Object.hasOwn(b, key)) {
78
+ return false;
79
+ }
80
+ if (!deepEqual(a[key], b[key])) {
81
+ return false;
82
+ }
83
+ }
84
+ return true;
85
+ }
86
+ return false;
87
+ }
88
+ function getRootMap(ydoc) {
89
+ return ydoc.getMap("root");
90
+ }
91
+ function diffJson(prev, next, prefix = []) {
92
+ const ops = [];
93
+ const _prefix = Array.isArray(prefix) ? prefix : [];
94
+ for (const key in prev) {
95
+ if (Object.hasOwn(prev, key) && !(key in next)) {
96
+ ops.push({ action: "del", path: [..._prefix, key] });
97
+ }
98
+ }
99
+ for (const key in next) {
100
+ if (Object.hasOwn(next, key)) {
101
+ const pVal = prev == null ? void 0 : prev[key];
102
+ const nVal = next[key];
103
+ if (isPlainObject(pVal) && isPlainObject(nVal)) {
104
+ ops.push(...diffJson(pVal, nVal, [..._prefix, key]));
105
+ } else if (!deepEqual(pVal, nVal)) {
106
+ ops.push({ action: "set", path: [..._prefix, key], value: nVal });
107
+ }
108
+ }
109
+ }
110
+ return ops;
111
+ }
112
+ function applyOpsToJson(ops, ydoc) {
113
+ if (!ydoc || !Array.isArray(ops) || !ops.length) {
114
+ return;
115
+ }
116
+ ydoc.transact(() => {
117
+ const root = getRootMap(ydoc);
118
+ ops.forEach((op) => {
119
+ const { action, path = [], value } = op || {};
120
+ if (!path.length) {
121
+ return;
122
+ }
123
+ let target = root;
124
+ for (let i = 0; i < path.length - 1; i++) {
125
+ const key = path[i];
126
+ let next = target.get(key);
127
+ if (!(next instanceof Y.Map)) {
128
+ const fresh = new Y.Map();
129
+ if (isPlainObject(next)) {
130
+ Object.entries(next).forEach(([k, v]) => fresh.set(k, v));
131
+ }
132
+ target.set(key, fresh);
133
+ next = fresh;
134
+ }
135
+ target = next;
136
+ }
137
+ const last = path[path.length - 1];
138
+ if (action === "set") {
139
+ target.set(last, value);
140
+ } else if (action === "del") {
141
+ target.delete(last);
142
+ }
143
+ });
144
+ }, "remote");
145
+ }