@symbo.ls/sdk 2.34.34 → 3.1.1

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 +13 -315
  2. package/dist/cjs/config/environment.js +50 -124
  3. package/dist/cjs/index.js +22 -169
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +277 -751
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +679 -0
  8. package/dist/cjs/services/SocketIOService.js +309 -0
  9. package/dist/cjs/services/SocketService.js +161 -0
  10. package/dist/cjs/services/SymstoryService.js +485 -0
  11. package/dist/cjs/services/index.js +16 -64
  12. package/dist/cjs/utils/basedQuerys.js +61 -0
  13. package/dist/cjs/utils/permission.js +4 -4
  14. package/dist/cjs/utils/services.js +80 -301
  15. package/dist/cjs/utils/symstoryClient.js +228 -0
  16. package/dist/cjs/utils/validation.js +3 -0
  17. package/dist/esm/config/environment.js +50 -124
  18. package/dist/esm/index.js +11282 -49789
  19. package/dist/esm/services/AIService.js +185 -0
  20. package/dist/esm/services/AuthService.js +358 -1506
  21. package/dist/esm/services/BaseService.js +6 -757
  22. package/dist/esm/services/BasedService.js +4651 -0
  23. package/dist/esm/services/SocketIOService.js +467 -0
  24. package/dist/esm/services/SocketService.js +191 -0
  25. package/dist/esm/services/SymstoryService.js +6849 -0
  26. package/dist/esm/services/index.js +11188 -49352
  27. package/dist/esm/utils/basedQuerys.js +43 -0
  28. package/dist/esm/utils/permission.js +4 -4
  29. package/dist/esm/utils/services.js +80 -301
  30. package/dist/esm/utils/symstoryClient.js +334 -0
  31. package/dist/esm/utils/validation.js +26 -93
  32. package/dist/node/config/environment.js +50 -124
  33. package/dist/node/index.js +26 -202
  34. package/dist/node/services/AIService.js +136 -0
  35. package/dist/node/services/AuthService.js +278 -751
  36. package/dist/node/services/BaseService.js +6 -148
  37. package/dist/node/services/BasedService.js +650 -0
  38. package/dist/node/services/SocketIOService.js +280 -0
  39. package/dist/node/services/SocketService.js +142 -0
  40. package/dist/node/services/SymstoryService.js +456 -0
  41. package/dist/node/services/index.js +16 -64
  42. package/dist/node/utils/basedQuerys.js +42 -0
  43. package/dist/node/utils/permission.js +4 -4
  44. package/dist/node/utils/services.js +80 -301
  45. package/dist/node/utils/symstoryClient.js +199 -0
  46. package/dist/node/utils/validation.js +3 -0
  47. package/package.json +21 -43
  48. package/src/config/environment.js +50 -126
  49. package/src/index.js +24 -208
  50. package/src/services/AIService.js +150 -0
  51. package/src/services/AuthService.js +298 -893
  52. package/src/services/BaseService.js +6 -166
  53. package/src/services/BasedService.js +722 -0
  54. package/src/services/SocketIOService.js +356 -0
  55. package/src/services/SocketService.js +168 -0
  56. package/src/services/SymstoryService.js +563 -0
  57. package/src/services/index.js +13 -80
  58. package/src/utils/basedQuerys.js +41 -0
  59. package/src/utils/permission.js +4 -4
  60. package/src/utils/services.js +83 -325
  61. package/src/utils/symstoryClient.js +218 -0
  62. package/src/utils/validation.js +3 -0
  63. package/dist/cjs/services/AdminService.js +0 -351
  64. package/dist/cjs/services/BranchService.js +0 -484
  65. package/dist/cjs/services/CollabService.js +0 -743
  66. package/dist/cjs/services/DnsService.js +0 -340
  67. package/dist/cjs/services/FeatureFlagService.js +0 -175
  68. package/dist/cjs/services/FileService.js +0 -201
  69. package/dist/cjs/services/IntegrationService.js +0 -538
  70. package/dist/cjs/services/MetricsService.js +0 -62
  71. package/dist/cjs/services/PaymentService.js +0 -271
  72. package/dist/cjs/services/PlanService.js +0 -426
  73. package/dist/cjs/services/ProjectService.js +0 -1207
  74. package/dist/cjs/services/PullRequestService.js +0 -503
  75. package/dist/cjs/services/ScreenshotService.js +0 -304
  76. package/dist/cjs/services/SubscriptionService.js +0 -396
  77. package/dist/cjs/services/TrackingService.js +0 -661
  78. package/dist/cjs/services/WaitlistService.js +0 -148
  79. package/dist/cjs/state/RootStateManager.js +0 -65
  80. package/dist/cjs/state/rootEventBus.js +0 -74
  81. package/dist/cjs/utils/CollabClient.js +0 -223
  82. package/dist/cjs/utils/TokenManager.js +0 -422
  83. package/dist/cjs/utils/changePreprocessor.js +0 -199
  84. package/dist/cjs/utils/jsonDiff.js +0 -145
  85. package/dist/cjs/utils/ordering.js +0 -309
  86. package/dist/esm/services/AdminService.js +0 -1132
  87. package/dist/esm/services/BranchService.js +0 -1265
  88. package/dist/esm/services/CollabService.js +0 -26838
  89. package/dist/esm/services/DnsService.js +0 -1121
  90. package/dist/esm/services/FeatureFlagService.js +0 -956
  91. package/dist/esm/services/FileService.js +0 -982
  92. package/dist/esm/services/IntegrationService.js +0 -1319
  93. package/dist/esm/services/MetricsService.js +0 -843
  94. package/dist/esm/services/PaymentService.js +0 -1052
  95. package/dist/esm/services/PlanService.js +0 -1207
  96. package/dist/esm/services/ProjectService.js +0 -2526
  97. package/dist/esm/services/PullRequestService.js +0 -1284
  98. package/dist/esm/services/ScreenshotService.js +0 -1085
  99. package/dist/esm/services/SubscriptionService.js +0 -1177
  100. package/dist/esm/services/TrackingService.js +0 -18343
  101. package/dist/esm/services/WaitlistService.js +0 -929
  102. package/dist/esm/state/RootStateManager.js +0 -90
  103. package/dist/esm/state/rootEventBus.js +0 -56
  104. package/dist/esm/utils/CollabClient.js +0 -18901
  105. package/dist/esm/utils/TokenManager.js +0 -408
  106. package/dist/esm/utils/changePreprocessor.js +0 -542
  107. package/dist/esm/utils/jsonDiff.js +0 -7011
  108. package/dist/esm/utils/ordering.js +0 -291
  109. package/dist/node/services/AdminService.js +0 -332
  110. package/dist/node/services/BranchService.js +0 -465
  111. package/dist/node/services/CollabService.js +0 -724
  112. package/dist/node/services/DnsService.js +0 -321
  113. package/dist/node/services/FeatureFlagService.js +0 -156
  114. package/dist/node/services/FileService.js +0 -182
  115. package/dist/node/services/IntegrationService.js +0 -519
  116. package/dist/node/services/MetricsService.js +0 -43
  117. package/dist/node/services/PaymentService.js +0 -252
  118. package/dist/node/services/PlanService.js +0 -407
  119. package/dist/node/services/ProjectService.js +0 -1188
  120. package/dist/node/services/PullRequestService.js +0 -484
  121. package/dist/node/services/ScreenshotService.js +0 -285
  122. package/dist/node/services/SubscriptionService.js +0 -377
  123. package/dist/node/services/TrackingService.js +0 -632
  124. package/dist/node/services/WaitlistService.js +0 -129
  125. package/dist/node/state/RootStateManager.js +0 -36
  126. package/dist/node/state/rootEventBus.js +0 -55
  127. package/dist/node/utils/CollabClient.js +0 -194
  128. package/dist/node/utils/TokenManager.js +0 -403
  129. package/dist/node/utils/changePreprocessor.js +0 -180
  130. package/dist/node/utils/jsonDiff.js +0 -116
  131. package/dist/node/utils/ordering.js +0 -290
  132. package/src/services/AdminService.js +0 -374
  133. package/src/services/BranchService.js +0 -536
  134. package/src/services/CollabService.js +0 -900
  135. package/src/services/DnsService.js +0 -366
  136. package/src/services/FeatureFlagService.js +0 -174
  137. package/src/services/FileService.js +0 -213
  138. package/src/services/IntegrationService.js +0 -548
  139. package/src/services/MetricsService.js +0 -40
  140. package/src/services/PaymentService.js +0 -287
  141. package/src/services/PlanService.js +0 -468
  142. package/src/services/ProjectService.js +0 -1366
  143. package/src/services/PullRequestService.js +0 -537
  144. package/src/services/ScreenshotService.js +0 -258
  145. package/src/services/SubscriptionService.js +0 -425
  146. package/src/services/TrackingService.js +0 -853
  147. package/src/services/WaitlistService.js +0 -130
  148. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  149. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  150. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  151. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  152. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  153. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  154. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  155. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  156. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  157. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  158. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  159. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  160. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  161. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  162. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  163. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  164. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  165. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  166. package/src/services/tests/FileService/validateFile.test.js +0 -63
  167. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  168. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  169. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  170. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  171. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  172. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  173. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  174. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  175. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  176. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  177. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  178. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  179. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  180. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  181. package/src/state/RootStateManager.js +0 -76
  182. package/src/state/rootEventBus.js +0 -67
  183. package/src/utils/CollabClient.js +0 -248
  184. package/src/utils/TokenManager.js +0 -479
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -1,148 +0,0 @@
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 WaitlistService_exports = {};
19
- __export(WaitlistService_exports, {
20
- WaitlistService: () => WaitlistService
21
- });
22
- module.exports = __toCommonJS(WaitlistService_exports);
23
- var import_BaseService = require("./BaseService.js");
24
- class WaitlistService extends import_BaseService.BaseService {
25
- // ==================== WAITLIST METHODS ====================
26
- /**
27
- * Join a waitlist campaign (public).
28
- *
29
- * Mirrors: POST /waitlist (WaitlistController.join)
30
- */
31
- async joinWaitlist(data = {}) {
32
- this._requireReady("joinWaitlist");
33
- if (!data || typeof data !== "object") {
34
- throw new Error("Waitlist join payload is required");
35
- }
36
- if (!data.email) {
37
- throw new Error("Email is required");
38
- }
39
- try {
40
- const response = await this._request("/waitlist", {
41
- method: "POST",
42
- body: JSON.stringify(data),
43
- methodName: "joinWaitlist"
44
- });
45
- if (response.success) {
46
- return response.data;
47
- }
48
- throw new Error(response.message);
49
- } catch (error) {
50
- throw new Error(`Failed to join waitlist: ${error.message}`, { cause: error });
51
- }
52
- }
53
- /**
54
- * List waitlist entries (admin).
55
- *
56
- * Mirrors: GET /waitlist (WaitlistController.list)
57
- */
58
- async listWaitlistEntries(options = {}) {
59
- this._requireReady("listWaitlistEntries");
60
- const {
61
- campaignKey,
62
- status,
63
- search,
64
- page,
65
- limit
66
- } = options || {};
67
- const queryParams = new URLSearchParams();
68
- if (campaignKey != null) {
69
- queryParams.append("campaignKey", String(campaignKey));
70
- }
71
- if (status != null) {
72
- queryParams.append("status", String(status));
73
- }
74
- if (search != null) {
75
- queryParams.append("search", String(search));
76
- }
77
- if (page != null) {
78
- queryParams.append("page", String(page));
79
- }
80
- if (limit != null) {
81
- queryParams.append("limit", String(limit));
82
- }
83
- const queryString = queryParams.toString();
84
- const url = `/waitlist${queryString ? `?${queryString}` : ""}`;
85
- try {
86
- const response = await this._request(url, {
87
- method: "GET",
88
- methodName: "listWaitlistEntries"
89
- });
90
- if (response.success) {
91
- return response.data;
92
- }
93
- throw new Error(response.message);
94
- } catch (error) {
95
- throw new Error(`Failed to list waitlist entries: ${error.message}`, { cause: error });
96
- }
97
- }
98
- /**
99
- * Update a waitlist entry (admin).
100
- *
101
- * Mirrors: PATCH /waitlist/:id (WaitlistController.update)
102
- */
103
- async updateWaitlistEntry(id, update = {}) {
104
- this._requireReady("updateWaitlistEntry");
105
- if (!id) {
106
- throw new Error("Waitlist entry ID is required");
107
- }
108
- if (!update || typeof update !== "object") {
109
- throw new Error("Update payload is required");
110
- }
111
- try {
112
- const response = await this._request(`/waitlist/${id}`, {
113
- method: "PATCH",
114
- body: JSON.stringify(update),
115
- methodName: "updateWaitlistEntry"
116
- });
117
- if (response.success) {
118
- return response.data;
119
- }
120
- throw new Error(response.message);
121
- } catch (error) {
122
- throw new Error(`Failed to update waitlist entry: ${error.message}`, { cause: error });
123
- }
124
- }
125
- /**
126
- * Send an invitation email for a waitlist entry (admin).
127
- *
128
- * Mirrors: POST /waitlist/:id/invite (WaitlistController.invite)
129
- */
130
- async inviteWaitlistEntry(id) {
131
- this._requireReady("inviteWaitlistEntry");
132
- if (!id) {
133
- throw new Error("Waitlist entry ID is required");
134
- }
135
- try {
136
- const response = await this._request(`/waitlist/${id}/invite`, {
137
- method: "POST",
138
- methodName: "inviteWaitlistEntry"
139
- });
140
- if (response.success) {
141
- return response.data;
142
- }
143
- throw new Error(response.message);
144
- } catch (error) {
145
- throw new Error(`Failed to invite waitlist entry: ${error.message}`, { cause: error });
146
- }
147
- }
148
- }
@@ -1,65 +0,0 @@
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 RootStateManager_exports = {};
29
- __export(RootStateManager_exports, {
30
- RootStateManager: () => RootStateManager
31
- });
32
- module.exports = __toCommonJS(RootStateManager_exports);
33
- var utils = __toESM(require("@domql/utils"), 1);
34
- var import_rootEventBus = require("./rootEventBus.js");
35
- const { isFunction } = utils.default || utils;
36
- class RootStateManager {
37
- constructor(rootState) {
38
- this._rootState = rootState;
39
- }
40
- /**
41
- * Apply change tuples to the root state using the built-in setPathCollection
42
- * of Symbo.ls APP state tree.
43
- *
44
- * @param {Array} changes – eg. ['update', ['foo'], 'bar']
45
- * @param {Object} opts – forwarded to setPathCollection
46
- */
47
- async applyChanges(changes = [], opts = {}) {
48
- if (!this._rootState || !isFunction(this._rootState.setPathCollection)) {
49
- return;
50
- }
51
- const result = await this._rootState.setPathCollection(changes, {
52
- preventUpdate: true,
53
- ...opts
54
- });
55
- return result;
56
- }
57
- setVersion(v) {
58
- if (this._rootState) {
59
- this._rootState.version = v;
60
- }
61
- }
62
- get root() {
63
- return this._rootState;
64
- }
65
- }
@@ -1,74 +0,0 @@
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 rootEventBus_exports = {};
19
- __export(rootEventBus_exports, {
20
- default: () => rootEventBus_default,
21
- rootBus: () => rootBus
22
- });
23
- module.exports = __toCommonJS(rootEventBus_exports);
24
- const getGlobalBus = () => {
25
- if (globalThis.__SMBLS_ROOT_BUS__) {
26
- return globalThis.__SMBLS_ROOT_BUS__;
27
- }
28
- const events = {};
29
- const lastPayloads = {};
30
- const bus = {
31
- on(event, handler) {
32
- (events[event] ||= []).push(handler);
33
- if (Object.hasOwn(lastPayloads, event)) {
34
- try {
35
- handler(lastPayloads[event]);
36
- } catch (err) {
37
- console.error("[rootBus] handler error for (replay)", event, err);
38
- }
39
- }
40
- },
41
- off(event, handler) {
42
- const list = events[event];
43
- if (!list) {
44
- return;
45
- }
46
- const idx = list.indexOf(handler);
47
- if (idx !== -1) {
48
- list.splice(idx, 1);
49
- }
50
- },
51
- emit(event, payload) {
52
- lastPayloads[event] = payload;
53
- const list = events[event];
54
- if (!list || !list.length) {
55
- return;
56
- }
57
- list.slice().forEach((fn) => {
58
- try {
59
- fn(payload);
60
- } catch (err) {
61
- console.error("[rootBus] handler error for", event, err);
62
- }
63
- });
64
- }
65
- };
66
- Object.defineProperty(bus, "_listeners", {
67
- value: events,
68
- enumerable: false
69
- });
70
- globalThis.__SMBLS_ROOT_BUS__ = bus;
71
- return bus;
72
- };
73
- const rootBus = getGlobalBus();
74
- var rootEventBus_default = rootBus;
@@ -1,223 +0,0 @@
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 CollabClient_exports = {};
29
- __export(CollabClient_exports, {
30
- CollabClient: () => CollabClient
31
- });
32
- module.exports = __toCommonJS(CollabClient_exports);
33
- var import_socket = require("socket.io-client");
34
- var Y = __toESM(require("yjs"), 1);
35
- var import_nanoid = require("nanoid");
36
- var import_environment = __toESM(require("../config/environment.js"), 1);
37
- var import_jsonDiff = require("./jsonDiff.js");
38
- class CollabClient {
39
- /* public fields */
40
- socket = null;
41
- ydoc = null;
42
- branch = "main";
43
- live = false;
44
- projectId = null;
45
- jwt = null;
46
- /* private state */
47
- _buffer = [];
48
- _flushTimer = null;
49
- _clientId = (0, import_nanoid.nanoid)();
50
- _outboxStore = createMemoryOutbox();
51
- // Dexie table fallback
52
- _readyResolve;
53
- ready = new Promise((res) => this._readyResolve = res);
54
- constructor({ jwt, projectId, branch = "main", live = false }) {
55
- Object.assign(this, { jwt, projectId, branch, live });
56
- this.ydoc = new Y.Doc();
57
- const hasIndexedDB = typeof globalThis.indexedDB !== "undefined";
58
- if (typeof window === "undefined" || !hasIndexedDB) {
59
- console.log("[CollabClient] IndexedDB not available \u2013 skipping offline persistence");
60
- } else {
61
- import("y-indexeddb").then(({ IndexeddbPersistence }) => {
62
- new IndexeddbPersistence(`${projectId}:${branch}`, this.ydoc);
63
- }).catch((err) => {
64
- console.warn("[CollabClient] Failed to load IndexeddbPersistence:", err);
65
- });
66
- }
67
- if (typeof window !== "undefined" && hasIndexedDB) {
68
- createDexieOutbox(`${projectId}:${branch}`).then((outboxStore) => {
69
- this._outboxStore = outboxStore;
70
- }).catch((err) => {
71
- console.warn("[CollabClient] Failed to load Dexie:", err);
72
- });
73
- }
74
- this.socket = (0, import_socket.io)(import_environment.default.socketUrl, {
75
- path: "/collab-socket",
76
- transports: ["websocket"],
77
- auth: { token: jwt, projectId, branch, live },
78
- reconnectionAttempts: Infinity,
79
- reconnectionDelayMax: 4e3
80
- });
81
- this.socket.on("snapshot", this._onSnapshot).on("ops", this._onOps).on("commit", this._onCommit).on("liveMode", this._onLiveMode).on("connect", this._onConnect).on("error", this._onError);
82
- this._prevJson = this.ydoc.getMap("root").toJSON();
83
- this.ydoc.on("afterTransaction", (tr) => {
84
- if (tr.origin === "remote") {
85
- return;
86
- }
87
- const currentJson = this.ydoc.getMap("root").toJSON();
88
- const ops = (0, import_jsonDiff.diffJson)(this._prevJson, currentJson);
89
- this._prevJson = currentJson;
90
- if (!ops.length) {
91
- return;
92
- }
93
- this._queueOps(ops);
94
- });
95
- }
96
- /* ---------- public helpers ---------- */
97
- toggleLive(flag) {
98
- this.socket.emit("toggleLive", Boolean(flag));
99
- }
100
- sendCursor(data) {
101
- this.socket.emit("cursor", data);
102
- }
103
- sendPresence(d) {
104
- this.socket.emit("presence", d);
105
- }
106
- /* ---------- private handlers ---------- */
107
- _onSnapshot = ({
108
- data
109
- /* Uint8Array */
110
- }) => {
111
- if (Array.isArray(data) ? data.length : data && data.byteLength) {
112
- Y.applyUpdate(this.ydoc, Uint8Array.from(data));
113
- } else {
114
- console.warn("[collab] Received empty snapshot \u2013 skipping applyUpdate");
115
- }
116
- this._prevJson = this.ydoc.getMap("root").toJSON();
117
- if (typeof this._readyResolve === "function") {
118
- this._readyResolve();
119
- this._readyResolve = null;
120
- }
121
- };
122
- _onOps = ({ changes }) => {
123
- (0, import_jsonDiff.applyOpsToJson)(changes, this.ydoc);
124
- this._prevJson = this.ydoc.getMap("root").toJSON();
125
- };
126
- _onCommit = async ({ version }) => {
127
- await this._outboxStore.clear();
128
- console.info("[collab] committed", version);
129
- };
130
- _onConnect = async () => {
131
- if (typeof this._readyResolve === "function") {
132
- this._readyResolve();
133
- this._readyResolve = null;
134
- }
135
- const queued = await this._outboxStore.toArray();
136
- if (queued.length) {
137
- this.socket.emit("ops", {
138
- changes: queued.flatMap((e) => e.ops),
139
- ts: Date.now(),
140
- clientId: this._clientId
141
- });
142
- await this._outboxStore.clear();
143
- }
144
- };
145
- /* ---------- buffering & debounce ---------- */
146
- _queueOps(ops) {
147
- this._buffer.push(...ops);
148
- this._outboxStore.put({ id: (0, import_nanoid.nanoid)(), ops });
149
- if (this.live && this.socket.connected) {
150
- this._flushNow();
151
- } else {
152
- clearTimeout(this._flushTimer);
153
- this._flushTimer = setTimeout(() => this._flushNow(), 40);
154
- }
155
- }
156
- _flushNow() {
157
- if (!this._buffer.length || !this.socket.connected) {
158
- return;
159
- }
160
- this.socket.emit("ops", {
161
- changes: this._buffer,
162
- ts: Date.now(),
163
- clientId: this._clientId
164
- });
165
- this._buffer.length = 0;
166
- }
167
- dispose() {
168
- var _a;
169
- clearTimeout(this._flushTimer);
170
- this._flushTimer = null;
171
- this._buffer.length = 0;
172
- if ((_a = this._outboxStore) == null ? void 0 : _a.clear) {
173
- try {
174
- const result = this._outboxStore.clear();
175
- if (result && typeof result.catch === "function") {
176
- result.catch(() => {
177
- });
178
- }
179
- } catch (error) {
180
- console.warn("[CollabClient] Failed to clear outbox store during dispose:", error);
181
- }
182
- }
183
- if (this.socket) {
184
- this.socket.off("snapshot", this._onSnapshot);
185
- this.socket.off("ops", this._onOps);
186
- this.socket.off("commit", this._onCommit);
187
- this.socket.off("liveMode", this._onLiveMode);
188
- this.socket.off("connect", this._onConnect);
189
- this.socket.off("error", this._onError);
190
- this.socket.removeAllListeners();
191
- this.socket.disconnect();
192
- this.socket = null;
193
- }
194
- if (this.ydoc) {
195
- this.ydoc.destroy();
196
- this.ydoc = null;
197
- }
198
- if (typeof this._readyResolve === "function") {
199
- this._readyResolve();
200
- this._readyResolve = null;
201
- }
202
- }
203
- _onLiveMode = (flag) => {
204
- this.live = flag;
205
- };
206
- _onError = (e) => {
207
- console.warn("[collab] socket error", e);
208
- };
209
- }
210
- function createMemoryOutbox() {
211
- const store = /* @__PURE__ */ new Map();
212
- return {
213
- put: (item) => store.set(item.id, item),
214
- toArray: () => Array.from(store.values()),
215
- clear: () => store.clear()
216
- };
217
- }
218
- async function createDexieOutbox(name) {
219
- const { default: Dexie } = await import("dexie");
220
- const db = new Dexie(`collab-${name}`);
221
- db.version(1).stores({ outbox: "id, ops" });
222
- return db.table("outbox");
223
- }