@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.
- package/README.md +13 -315
- package/dist/cjs/config/environment.js +50 -124
- package/dist/cjs/index.js +22 -169
- package/dist/cjs/services/AIService.js +155 -0
- package/dist/cjs/services/AuthService.js +277 -751
- package/dist/cjs/services/BaseService.js +6 -158
- package/dist/cjs/services/BasedService.js +679 -0
- package/dist/cjs/services/SocketIOService.js +309 -0
- package/dist/cjs/services/SocketService.js +161 -0
- package/dist/cjs/services/SymstoryService.js +485 -0
- package/dist/cjs/services/index.js +16 -64
- package/dist/cjs/utils/basedQuerys.js +61 -0
- package/dist/cjs/utils/permission.js +4 -4
- package/dist/cjs/utils/services.js +80 -301
- package/dist/cjs/utils/symstoryClient.js +228 -0
- package/dist/cjs/utils/validation.js +3 -0
- package/dist/esm/config/environment.js +50 -124
- package/dist/esm/index.js +11282 -49789
- package/dist/esm/services/AIService.js +185 -0
- package/dist/esm/services/AuthService.js +358 -1506
- package/dist/esm/services/BaseService.js +6 -757
- package/dist/esm/services/BasedService.js +4651 -0
- package/dist/esm/services/SocketIOService.js +467 -0
- package/dist/esm/services/SocketService.js +191 -0
- package/dist/esm/services/SymstoryService.js +6849 -0
- package/dist/esm/services/index.js +11188 -49352
- package/dist/esm/utils/basedQuerys.js +43 -0
- package/dist/esm/utils/permission.js +4 -4
- package/dist/esm/utils/services.js +80 -301
- package/dist/esm/utils/symstoryClient.js +334 -0
- package/dist/esm/utils/validation.js +26 -93
- package/dist/node/config/environment.js +50 -124
- package/dist/node/index.js +26 -202
- package/dist/node/services/AIService.js +136 -0
- package/dist/node/services/AuthService.js +278 -751
- package/dist/node/services/BaseService.js +6 -148
- package/dist/node/services/BasedService.js +650 -0
- package/dist/node/services/SocketIOService.js +280 -0
- package/dist/node/services/SocketService.js +142 -0
- package/dist/node/services/SymstoryService.js +456 -0
- package/dist/node/services/index.js +16 -64
- package/dist/node/utils/basedQuerys.js +42 -0
- package/dist/node/utils/permission.js +4 -4
- package/dist/node/utils/services.js +80 -301
- package/dist/node/utils/symstoryClient.js +199 -0
- package/dist/node/utils/validation.js +3 -0
- package/package.json +21 -43
- package/src/config/environment.js +50 -126
- package/src/index.js +24 -208
- package/src/services/AIService.js +150 -0
- package/src/services/AuthService.js +298 -893
- package/src/services/BaseService.js +6 -166
- package/src/services/BasedService.js +722 -0
- package/src/services/SocketIOService.js +356 -0
- package/src/services/SocketService.js +168 -0
- package/src/services/SymstoryService.js +563 -0
- package/src/services/index.js +13 -80
- package/src/utils/basedQuerys.js +41 -0
- package/src/utils/permission.js +4 -4
- package/src/utils/services.js +83 -325
- package/src/utils/symstoryClient.js +218 -0
- package/src/utils/validation.js +3 -0
- package/dist/cjs/services/AdminService.js +0 -351
- package/dist/cjs/services/BranchService.js +0 -484
- package/dist/cjs/services/CollabService.js +0 -743
- package/dist/cjs/services/DnsService.js +0 -340
- package/dist/cjs/services/FeatureFlagService.js +0 -175
- package/dist/cjs/services/FileService.js +0 -201
- package/dist/cjs/services/IntegrationService.js +0 -538
- package/dist/cjs/services/MetricsService.js +0 -62
- package/dist/cjs/services/PaymentService.js +0 -271
- package/dist/cjs/services/PlanService.js +0 -426
- package/dist/cjs/services/ProjectService.js +0 -1207
- package/dist/cjs/services/PullRequestService.js +0 -503
- package/dist/cjs/services/ScreenshotService.js +0 -304
- package/dist/cjs/services/SubscriptionService.js +0 -396
- package/dist/cjs/services/TrackingService.js +0 -661
- package/dist/cjs/services/WaitlistService.js +0 -148
- package/dist/cjs/state/RootStateManager.js +0 -65
- package/dist/cjs/state/rootEventBus.js +0 -74
- package/dist/cjs/utils/CollabClient.js +0 -223
- package/dist/cjs/utils/TokenManager.js +0 -422
- package/dist/cjs/utils/changePreprocessor.js +0 -199
- package/dist/cjs/utils/jsonDiff.js +0 -145
- package/dist/cjs/utils/ordering.js +0 -309
- package/dist/esm/services/AdminService.js +0 -1132
- package/dist/esm/services/BranchService.js +0 -1265
- package/dist/esm/services/CollabService.js +0 -26838
- package/dist/esm/services/DnsService.js +0 -1121
- package/dist/esm/services/FeatureFlagService.js +0 -956
- package/dist/esm/services/FileService.js +0 -982
- package/dist/esm/services/IntegrationService.js +0 -1319
- package/dist/esm/services/MetricsService.js +0 -843
- package/dist/esm/services/PaymentService.js +0 -1052
- package/dist/esm/services/PlanService.js +0 -1207
- package/dist/esm/services/ProjectService.js +0 -2526
- package/dist/esm/services/PullRequestService.js +0 -1284
- package/dist/esm/services/ScreenshotService.js +0 -1085
- package/dist/esm/services/SubscriptionService.js +0 -1177
- package/dist/esm/services/TrackingService.js +0 -18343
- package/dist/esm/services/WaitlistService.js +0 -929
- package/dist/esm/state/RootStateManager.js +0 -90
- package/dist/esm/state/rootEventBus.js +0 -56
- package/dist/esm/utils/CollabClient.js +0 -18901
- package/dist/esm/utils/TokenManager.js +0 -408
- package/dist/esm/utils/changePreprocessor.js +0 -542
- package/dist/esm/utils/jsonDiff.js +0 -7011
- package/dist/esm/utils/ordering.js +0 -291
- package/dist/node/services/AdminService.js +0 -332
- package/dist/node/services/BranchService.js +0 -465
- package/dist/node/services/CollabService.js +0 -724
- package/dist/node/services/DnsService.js +0 -321
- package/dist/node/services/FeatureFlagService.js +0 -156
- package/dist/node/services/FileService.js +0 -182
- package/dist/node/services/IntegrationService.js +0 -519
- package/dist/node/services/MetricsService.js +0 -43
- package/dist/node/services/PaymentService.js +0 -252
- package/dist/node/services/PlanService.js +0 -407
- package/dist/node/services/ProjectService.js +0 -1188
- package/dist/node/services/PullRequestService.js +0 -484
- package/dist/node/services/ScreenshotService.js +0 -285
- package/dist/node/services/SubscriptionService.js +0 -377
- package/dist/node/services/TrackingService.js +0 -632
- package/dist/node/services/WaitlistService.js +0 -129
- package/dist/node/state/RootStateManager.js +0 -36
- package/dist/node/state/rootEventBus.js +0 -55
- package/dist/node/utils/CollabClient.js +0 -194
- package/dist/node/utils/TokenManager.js +0 -403
- package/dist/node/utils/changePreprocessor.js +0 -180
- package/dist/node/utils/jsonDiff.js +0 -116
- package/dist/node/utils/ordering.js +0 -290
- package/src/services/AdminService.js +0 -374
- package/src/services/BranchService.js +0 -536
- package/src/services/CollabService.js +0 -900
- package/src/services/DnsService.js +0 -366
- package/src/services/FeatureFlagService.js +0 -174
- package/src/services/FileService.js +0 -213
- package/src/services/IntegrationService.js +0 -548
- package/src/services/MetricsService.js +0 -40
- package/src/services/PaymentService.js +0 -287
- package/src/services/PlanService.js +0 -468
- package/src/services/ProjectService.js +0 -1366
- package/src/services/PullRequestService.js +0 -537
- package/src/services/ScreenshotService.js +0 -258
- package/src/services/SubscriptionService.js +0 -425
- package/src/services/TrackingService.js +0 -853
- package/src/services/WaitlistService.js +0 -130
- package/src/services/tests/BranchService/createBranch.test.js +0 -153
- package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
- package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
- package/src/services/tests/BranchService/listBranches.test.js +0 -87
- package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
- package/src/services/tests/BranchService/publishVersion.test.js +0 -183
- package/src/services/tests/BranchService/renameBranch.test.js +0 -240
- package/src/services/tests/BranchService/resetBranch.test.js +0 -152
- package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
- package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
- package/src/services/tests/FileService/createFileFormData.test.js +0 -74
- package/src/services/tests/FileService/getFileUrl.test.js +0 -69
- package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
- package/src/services/tests/FileService/uploadDocument.test.js +0 -36
- package/src/services/tests/FileService/uploadFile.test.js +0 -78
- package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
- package/src/services/tests/FileService/uploadImage.test.js +0 -36
- package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
- package/src/services/tests/FileService/validateFile.test.js +0 -63
- package/src/services/tests/PlanService/createPlan.test.js +0 -104
- package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
- package/src/services/tests/PlanService/deletePlan.test.js +0 -92
- package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
- package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
- package/src/services/tests/PlanService/getPlan.test.js +0 -50
- package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
- package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
- package/src/services/tests/PlanService/getPlans.test.js +0 -53
- package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
- package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
- package/src/services/tests/PlanService/initializePlans.test.js +0 -75
- package/src/services/tests/PlanService/updatePlan.test.js +0 -111
- package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
- package/src/state/RootStateManager.js +0 -76
- package/src/state/rootEventBus.js +0 -67
- package/src/utils/CollabClient.js +0 -248
- package/src/utils/TokenManager.js +0 -479
- package/src/utils/changePreprocessor.js +0 -239
- package/src/utils/jsonDiff.js +0 -144
- package/src/utils/ordering.js +0 -271
|
@@ -1,403 +0,0 @@
|
|
|
1
|
-
class TokenManager {
|
|
2
|
-
constructor(options = {}) {
|
|
3
|
-
this.config = {
|
|
4
|
-
storagePrefix: "symbols_",
|
|
5
|
-
storageType: typeof window === "undefined" || process.env.NODE_ENV === "test" || process.env.NODE_ENV === "testing" ? "memory" : "localStorage",
|
|
6
|
-
// 'localStorage' | 'sessionStorage' | 'memory'
|
|
7
|
-
refreshBuffer: 60 * 1e3,
|
|
8
|
-
// Refresh 1 minute before expiry
|
|
9
|
-
maxRetries: 3,
|
|
10
|
-
apiUrl: options.apiUrl || "/api",
|
|
11
|
-
onTokenRefresh: options.onTokenRefresh || null,
|
|
12
|
-
onTokenExpired: options.onTokenExpired || null,
|
|
13
|
-
onTokenError: options.onTokenError || null,
|
|
14
|
-
...options
|
|
15
|
-
};
|
|
16
|
-
this.tokens = {
|
|
17
|
-
accessToken: null,
|
|
18
|
-
refreshToken: null,
|
|
19
|
-
expiresAt: null,
|
|
20
|
-
expiresIn: null
|
|
21
|
-
};
|
|
22
|
-
this.refreshPromise = null;
|
|
23
|
-
this.refreshTimeout = null;
|
|
24
|
-
this.retryCount = 0;
|
|
25
|
-
this.loadTokens();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Storage keys
|
|
29
|
-
*/
|
|
30
|
-
get storageKeys() {
|
|
31
|
-
return {
|
|
32
|
-
accessToken: `${this.config.storagePrefix}access_token`,
|
|
33
|
-
refreshToken: `${this.config.storagePrefix}refresh_token`,
|
|
34
|
-
expiresAt: `${this.config.storagePrefix}expires_at`,
|
|
35
|
-
expiresIn: `${this.config.storagePrefix}expires_in`
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Get storage instance based on configuration
|
|
40
|
-
*/
|
|
41
|
-
get storage() {
|
|
42
|
-
if (typeof window === "undefined") {
|
|
43
|
-
return this._memoryStorage;
|
|
44
|
-
}
|
|
45
|
-
const safeGetStorage = (provider) => {
|
|
46
|
-
try {
|
|
47
|
-
const storage = provider();
|
|
48
|
-
const testKey = `${this.config.storagePrefix}__tm_test__`;
|
|
49
|
-
storage.setItem(testKey, "1");
|
|
50
|
-
storage.removeItem(testKey);
|
|
51
|
-
return storage;
|
|
52
|
-
} catch {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const localStorageInstance = safeGetStorage(() => window.localStorage);
|
|
57
|
-
const sessionStorageInstance = safeGetStorage(() => window.sessionStorage);
|
|
58
|
-
switch (this.config.storageType) {
|
|
59
|
-
case "sessionStorage":
|
|
60
|
-
return sessionStorageInstance || this._memoryStorage;
|
|
61
|
-
case "memory":
|
|
62
|
-
return this._memoryStorage;
|
|
63
|
-
default:
|
|
64
|
-
return localStorageInstance || this._memoryStorage;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Memory storage fallback for server-side rendering
|
|
69
|
-
*/
|
|
70
|
-
_memoryStorage = {
|
|
71
|
-
_data: {},
|
|
72
|
-
getItem: (key) => this._memoryStorage._data[key] || null,
|
|
73
|
-
setItem: (key, value) => {
|
|
74
|
-
this._memoryStorage._data[key] = value;
|
|
75
|
-
},
|
|
76
|
-
removeItem: (key) => {
|
|
77
|
-
delete this._memoryStorage._data[key];
|
|
78
|
-
},
|
|
79
|
-
clear: () => {
|
|
80
|
-
this._memoryStorage._data = {};
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Set tokens and persist to storage
|
|
85
|
-
*/
|
|
86
|
-
setTokens(tokenData) {
|
|
87
|
-
const {
|
|
88
|
-
access_token: accessToken,
|
|
89
|
-
refresh_token: refreshToken,
|
|
90
|
-
expires_in: expiresIn,
|
|
91
|
-
token_type: tokenType = "Bearer"
|
|
92
|
-
} = tokenData;
|
|
93
|
-
if (!accessToken) {
|
|
94
|
-
throw new Error("Access token is required");
|
|
95
|
-
}
|
|
96
|
-
const now = Date.now();
|
|
97
|
-
const expiresAt = expiresIn ? now + expiresIn * 1e3 : null;
|
|
98
|
-
this.tokens = {
|
|
99
|
-
accessToken,
|
|
100
|
-
refreshToken: refreshToken || this.tokens.refreshToken,
|
|
101
|
-
expiresAt,
|
|
102
|
-
expiresIn,
|
|
103
|
-
tokenType
|
|
104
|
-
};
|
|
105
|
-
this.saveTokens();
|
|
106
|
-
this.scheduleRefresh();
|
|
107
|
-
if (this.config.onTokenRefresh) {
|
|
108
|
-
this.config.onTokenRefresh(this.tokens);
|
|
109
|
-
}
|
|
110
|
-
return this.tokens;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Get current access token
|
|
114
|
-
*/
|
|
115
|
-
getAccessToken() {
|
|
116
|
-
return this.tokens.accessToken;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Get current refresh token
|
|
120
|
-
*/
|
|
121
|
-
getRefreshToken() {
|
|
122
|
-
return this.tokens.refreshToken;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Get authorization header value
|
|
126
|
-
*/
|
|
127
|
-
getAuthHeader() {
|
|
128
|
-
const token = this.getAccessToken();
|
|
129
|
-
if (!token) {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
return `${this.tokens.tokenType || "Bearer"} ${token}`;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Check if access token is valid and not expired
|
|
136
|
-
*/
|
|
137
|
-
isAccessTokenValid() {
|
|
138
|
-
if (!this.tokens.accessToken) {
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
if (!this.tokens.expiresAt) {
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
const now = Date.now();
|
|
145
|
-
const isValid = now < this.tokens.expiresAt - this.config.refreshBuffer;
|
|
146
|
-
if (!isValid) {
|
|
147
|
-
console.log("[TokenManager] Access token is expired or near expiry:", {
|
|
148
|
-
now: new Date(now).toISOString(),
|
|
149
|
-
expiresAt: new Date(this.tokens.expiresAt).toISOString(),
|
|
150
|
-
refreshBuffer: this.config.refreshBuffer
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
return isValid;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Check if access token exists and is not expired (without refresh buffer)
|
|
157
|
-
*/
|
|
158
|
-
isAccessTokenActuallyValid() {
|
|
159
|
-
if (!this.tokens.accessToken) {
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
if (!this.tokens.expiresAt) {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
const now = Date.now();
|
|
166
|
-
return now < this.tokens.expiresAt;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Check if tokens exist (regardless of expiry)
|
|
170
|
-
*/
|
|
171
|
-
hasTokens() {
|
|
172
|
-
return Boolean(this.tokens.accessToken);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Check if refresh token exists
|
|
176
|
-
*/
|
|
177
|
-
hasRefreshToken() {
|
|
178
|
-
return Boolean(this.tokens.refreshToken);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Automatically refresh tokens if needed
|
|
182
|
-
*/
|
|
183
|
-
async ensureValidToken() {
|
|
184
|
-
if (!this.hasTokens()) {
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
|
-
if (this.isAccessTokenValid()) {
|
|
188
|
-
return this.getAccessToken();
|
|
189
|
-
}
|
|
190
|
-
if (!this.hasRefreshToken()) {
|
|
191
|
-
this.clearTokens();
|
|
192
|
-
if (this.config.onTokenExpired) {
|
|
193
|
-
this.config.onTokenExpired();
|
|
194
|
-
}
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
try {
|
|
198
|
-
await this.refreshTokens();
|
|
199
|
-
return this.getAccessToken();
|
|
200
|
-
} catch (error) {
|
|
201
|
-
this.clearTokens();
|
|
202
|
-
if (this.config.onTokenError) {
|
|
203
|
-
this.config.onTokenError(error);
|
|
204
|
-
}
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Refresh access token using refresh token
|
|
210
|
-
*/
|
|
211
|
-
async refreshTokens() {
|
|
212
|
-
if (this.refreshPromise) {
|
|
213
|
-
return this.refreshPromise;
|
|
214
|
-
}
|
|
215
|
-
if (!this.hasRefreshToken()) {
|
|
216
|
-
throw new Error("No refresh token available");
|
|
217
|
-
}
|
|
218
|
-
if (this.retryCount >= this.config.maxRetries) {
|
|
219
|
-
throw new Error("Max refresh retries exceeded");
|
|
220
|
-
}
|
|
221
|
-
this.refreshPromise = this._performRefresh();
|
|
222
|
-
try {
|
|
223
|
-
const result = await this.refreshPromise;
|
|
224
|
-
this.retryCount = 0;
|
|
225
|
-
return result;
|
|
226
|
-
} catch (error) {
|
|
227
|
-
this.retryCount++;
|
|
228
|
-
throw error;
|
|
229
|
-
} finally {
|
|
230
|
-
this.refreshPromise = null;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Perform the actual token refresh request
|
|
235
|
-
*/
|
|
236
|
-
async _performRefresh() {
|
|
237
|
-
var _a;
|
|
238
|
-
const refreshToken = this.getRefreshToken();
|
|
239
|
-
const response = await fetch(`${this.config.apiUrl}/core/auth/refresh`, {
|
|
240
|
-
method: "POST",
|
|
241
|
-
headers: {
|
|
242
|
-
"Content-Type": "application/json"
|
|
243
|
-
},
|
|
244
|
-
body: JSON.stringify({ refreshToken })
|
|
245
|
-
});
|
|
246
|
-
if (!response.ok) {
|
|
247
|
-
const errorData = await response.json().catch(() => ({}));
|
|
248
|
-
throw new Error(errorData.message || `Token refresh failed: ${response.status}`);
|
|
249
|
-
}
|
|
250
|
-
const responseData = await response.json();
|
|
251
|
-
if (responseData.success && responseData.data && responseData.data.tokens) {
|
|
252
|
-
const { tokens } = responseData.data;
|
|
253
|
-
const tokenData = {
|
|
254
|
-
access_token: tokens.accessToken,
|
|
255
|
-
refresh_token: tokens.refreshToken,
|
|
256
|
-
expires_in: (_a = tokens.accessTokenExp) == null ? void 0 : _a.expiresIn,
|
|
257
|
-
token_type: "Bearer"
|
|
258
|
-
};
|
|
259
|
-
return this.setTokens(tokenData);
|
|
260
|
-
}
|
|
261
|
-
return this.setTokens(responseData);
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Schedule automatic token refresh
|
|
265
|
-
*/
|
|
266
|
-
scheduleRefresh() {
|
|
267
|
-
if (this.refreshTimeout) {
|
|
268
|
-
clearTimeout(this.refreshTimeout);
|
|
269
|
-
this.refreshTimeout = null;
|
|
270
|
-
}
|
|
271
|
-
if (!this.tokens.expiresAt || !this.hasRefreshToken()) {
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
const now = Date.now();
|
|
275
|
-
const refreshTime = this.tokens.expiresAt - this.config.refreshBuffer;
|
|
276
|
-
const delay = Math.max(0, refreshTime - now);
|
|
277
|
-
this.refreshTimeout = setTimeout(async () => {
|
|
278
|
-
try {
|
|
279
|
-
await this.refreshTokens();
|
|
280
|
-
} catch (error) {
|
|
281
|
-
console.error("Automatic token refresh failed:", error);
|
|
282
|
-
if (this.config.onTokenError) {
|
|
283
|
-
this.config.onTokenError(error);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}, delay);
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Save tokens to storage
|
|
290
|
-
*/
|
|
291
|
-
saveTokens() {
|
|
292
|
-
try {
|
|
293
|
-
const { storage } = this;
|
|
294
|
-
const keys = this.storageKeys;
|
|
295
|
-
if (this.tokens.accessToken) {
|
|
296
|
-
storage.setItem(keys.accessToken, this.tokens.accessToken);
|
|
297
|
-
}
|
|
298
|
-
if (this.tokens.refreshToken) {
|
|
299
|
-
storage.setItem(keys.refreshToken, this.tokens.refreshToken);
|
|
300
|
-
}
|
|
301
|
-
if (this.tokens.expiresAt) {
|
|
302
|
-
storage.setItem(keys.expiresAt, this.tokens.expiresAt.toString());
|
|
303
|
-
}
|
|
304
|
-
if (this.tokens.expiresIn) {
|
|
305
|
-
storage.setItem(keys.expiresIn, this.tokens.expiresIn.toString());
|
|
306
|
-
}
|
|
307
|
-
} catch (error) {
|
|
308
|
-
console.error("[TokenManager] Error saving tokens to storage:", error);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Load tokens from storage
|
|
313
|
-
*/
|
|
314
|
-
loadTokens() {
|
|
315
|
-
try {
|
|
316
|
-
const { storage } = this;
|
|
317
|
-
const keys = this.storageKeys;
|
|
318
|
-
const accessToken = storage.getItem(keys.accessToken);
|
|
319
|
-
const refreshToken = storage.getItem(keys.refreshToken);
|
|
320
|
-
const expiresAt = storage.getItem(keys.expiresAt);
|
|
321
|
-
const expiresIn = storage.getItem(keys.expiresIn);
|
|
322
|
-
if (accessToken) {
|
|
323
|
-
this.tokens = {
|
|
324
|
-
accessToken,
|
|
325
|
-
refreshToken,
|
|
326
|
-
expiresAt: expiresAt ? parseInt(expiresAt, 10) : null,
|
|
327
|
-
expiresIn: expiresIn ? parseInt(expiresIn, 10) : null,
|
|
328
|
-
tokenType: "Bearer"
|
|
329
|
-
};
|
|
330
|
-
this.scheduleRefresh();
|
|
331
|
-
}
|
|
332
|
-
} catch (error) {
|
|
333
|
-
console.error("[TokenManager] Error loading tokens from storage:", error);
|
|
334
|
-
this.tokens = {
|
|
335
|
-
accessToken: null,
|
|
336
|
-
refreshToken: null,
|
|
337
|
-
expiresAt: null,
|
|
338
|
-
expiresIn: null
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Clear all tokens
|
|
344
|
-
*/
|
|
345
|
-
clearTokens() {
|
|
346
|
-
this.tokens = {
|
|
347
|
-
accessToken: null,
|
|
348
|
-
refreshToken: null,
|
|
349
|
-
expiresAt: null,
|
|
350
|
-
expiresIn: null
|
|
351
|
-
};
|
|
352
|
-
const { storage } = this;
|
|
353
|
-
const keys = this.storageKeys;
|
|
354
|
-
Object.values(keys).forEach((key) => {
|
|
355
|
-
storage.removeItem(key);
|
|
356
|
-
});
|
|
357
|
-
if (this.refreshTimeout) {
|
|
358
|
-
clearTimeout(this.refreshTimeout);
|
|
359
|
-
this.refreshTimeout = null;
|
|
360
|
-
}
|
|
361
|
-
this.retryCount = 0;
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Get token status information
|
|
365
|
-
*/
|
|
366
|
-
getTokenStatus() {
|
|
367
|
-
const hasTokens = this.hasTokens();
|
|
368
|
-
const isValid = this.isAccessTokenValid();
|
|
369
|
-
const { expiresAt } = this.tokens;
|
|
370
|
-
const timeToExpiry = expiresAt ? expiresAt - Date.now() : null;
|
|
371
|
-
return {
|
|
372
|
-
hasTokens,
|
|
373
|
-
isValid,
|
|
374
|
-
hasRefreshToken: this.hasRefreshToken(),
|
|
375
|
-
expiresAt,
|
|
376
|
-
timeToExpiry,
|
|
377
|
-
willExpireSoon: timeToExpiry ? timeToExpiry < this.config.refreshBuffer : false
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Cleanup resources
|
|
382
|
-
*/
|
|
383
|
-
destroy() {
|
|
384
|
-
if (this.refreshTimeout) {
|
|
385
|
-
clearTimeout(this.refreshTimeout);
|
|
386
|
-
this.refreshTimeout = null;
|
|
387
|
-
}
|
|
388
|
-
this.refreshPromise = null;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
let defaultTokenManager = null;
|
|
392
|
-
const getTokenManager = (options) => {
|
|
393
|
-
if (!defaultTokenManager) {
|
|
394
|
-
defaultTokenManager = new TokenManager(options);
|
|
395
|
-
}
|
|
396
|
-
return defaultTokenManager;
|
|
397
|
-
};
|
|
398
|
-
const createTokenManager = (options) => new TokenManager(options);
|
|
399
|
-
export {
|
|
400
|
-
TokenManager,
|
|
401
|
-
createTokenManager,
|
|
402
|
-
getTokenManager
|
|
403
|
-
};
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { diffJson } from "./jsonDiff.js";
|
|
2
|
-
import { computeOrdersForTuples } from "./ordering.js";
|
|
3
|
-
function isPlainObject(val) {
|
|
4
|
-
return val && typeof val === "object" && !Array.isArray(val);
|
|
5
|
-
}
|
|
6
|
-
function getByPathSafe(root, path) {
|
|
7
|
-
if (!root || typeof root.getByPath !== "function") {
|
|
8
|
-
return null;
|
|
9
|
-
}
|
|
10
|
-
try {
|
|
11
|
-
return root.getByPath(path);
|
|
12
|
-
} catch {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function resolveNextValueFromTuples(tuples, path) {
|
|
17
|
-
if (!Array.isArray(tuples) || !Array.isArray(path)) {
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
for (let i = tuples.length - 1; i >= 0; i--) {
|
|
21
|
-
const t = tuples[i];
|
|
22
|
-
if (!Array.isArray(t) || t.length < 3) {
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
const [action, tuplePath, tupleValue] = t;
|
|
26
|
-
if (action !== "update" && action !== "set" || !Array.isArray(tuplePath)) {
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
if (tuplePath.length > path.length) {
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
let isPrefix = true;
|
|
33
|
-
for (let j = 0; j < tuplePath.length; j++) {
|
|
34
|
-
if (tuplePath[j] !== path[j]) {
|
|
35
|
-
isPrefix = false;
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (!isPrefix) {
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
if (tuplePath.length === path.length) {
|
|
43
|
-
return tupleValue;
|
|
44
|
-
}
|
|
45
|
-
let current = tupleValue;
|
|
46
|
-
for (let j = tuplePath.length; j < path.length; j++) {
|
|
47
|
-
if (current == null) {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
current = current[path[j]];
|
|
51
|
-
}
|
|
52
|
-
if (current !== null) {
|
|
53
|
-
return current;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
function preprocessChanges(root, tuples = [], options = {}) {
|
|
59
|
-
const expandTuple = (t) => {
|
|
60
|
-
const [action, path, value] = t || [];
|
|
61
|
-
const isSchemaPath = Array.isArray(path) && path[0] === "schema";
|
|
62
|
-
const isFilesPath = Array.isArray(path) && path[0] === "files";
|
|
63
|
-
if (action === "delete") {
|
|
64
|
-
return [t];
|
|
65
|
-
}
|
|
66
|
-
const canConsiderExpansion = action === "update" && Array.isArray(path) && (path.length === 1 || path.length === 2 || isSchemaPath && path.length === 3) && isPlainObject(value);
|
|
67
|
-
if (!canConsiderExpansion || isFilesPath || value && value.type === "files") {
|
|
68
|
-
return [t];
|
|
69
|
-
}
|
|
70
|
-
const prevRaw = getByPathSafe(root, path);
|
|
71
|
-
const isCreatePath = Array.isArray(path) && action === "update" && // e.g. ['update', ['components', 'NewKey'], {...}]
|
|
72
|
-
(!isSchemaPath && path.length === 2 || // e.g. ['update', ['schema', 'components', 'NewKey'], {...}]
|
|
73
|
-
isSchemaPath && path.length === 3) && (prevRaw === null || typeof prevRaw === "undefined");
|
|
74
|
-
if (isCreatePath) {
|
|
75
|
-
return [t];
|
|
76
|
-
}
|
|
77
|
-
const prev = prevRaw || {};
|
|
78
|
-
const next = value || {};
|
|
79
|
-
if (!isPlainObject(prev) || !isPlainObject(next)) {
|
|
80
|
-
return [t];
|
|
81
|
-
}
|
|
82
|
-
const ops = diffJson(prev, next, []);
|
|
83
|
-
if (!ops.length) {
|
|
84
|
-
return [t];
|
|
85
|
-
}
|
|
86
|
-
const out = [];
|
|
87
|
-
for (let i = 0; i < ops.length; i++) {
|
|
88
|
-
const op = ops[i];
|
|
89
|
-
const fullPath = [...path, ...op.path];
|
|
90
|
-
const last = fullPath[fullPath.length - 1];
|
|
91
|
-
if (op.action === "set") {
|
|
92
|
-
out.push(["update", fullPath, op.value]);
|
|
93
|
-
} else if (op.action === "del") {
|
|
94
|
-
if (last !== "__order") {
|
|
95
|
-
out.push(["delete", fullPath]);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return out;
|
|
100
|
-
};
|
|
101
|
-
const minimizeTuples = (input) => {
|
|
102
|
-
const out = [];
|
|
103
|
-
const seen2 = /* @__PURE__ */ new Set();
|
|
104
|
-
for (let i = 0; i < input.length; i++) {
|
|
105
|
-
const expanded = expandTuple(input[i]);
|
|
106
|
-
for (let k = 0; k < expanded.length; k++) {
|
|
107
|
-
const tuple = expanded[k];
|
|
108
|
-
const isDelete = Array.isArray(tuple) && tuple[0] === "delete";
|
|
109
|
-
const isOrderKey = isDelete && Array.isArray(tuple[1]) && tuple[1][tuple[1].length - 1] === "__order";
|
|
110
|
-
if (!isOrderKey) {
|
|
111
|
-
const key = JSON.stringify(tuple);
|
|
112
|
-
if (!seen2.has(key)) {
|
|
113
|
-
seen2.add(key);
|
|
114
|
-
out.push(tuple);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return out;
|
|
120
|
-
};
|
|
121
|
-
const granularChanges = (() => {
|
|
122
|
-
try {
|
|
123
|
-
const res = minimizeTuples(tuples);
|
|
124
|
-
if (options.append && options.append.length) {
|
|
125
|
-
res.push(...options.append);
|
|
126
|
-
}
|
|
127
|
-
return res;
|
|
128
|
-
} catch {
|
|
129
|
-
return Array.isArray(tuples) ? tuples.slice() : [];
|
|
130
|
-
}
|
|
131
|
-
})();
|
|
132
|
-
const hydratedGranularChanges = granularChanges.map((t) => {
|
|
133
|
-
if (!Array.isArray(t) || t.length < 3) {
|
|
134
|
-
return t;
|
|
135
|
-
}
|
|
136
|
-
const [action, path] = t;
|
|
137
|
-
if (action !== "update" && action !== "set" || !Array.isArray(path)) {
|
|
138
|
-
return t;
|
|
139
|
-
}
|
|
140
|
-
const nextValue = resolveNextValueFromTuples(tuples, path);
|
|
141
|
-
if (nextValue === null) {
|
|
142
|
-
return t;
|
|
143
|
-
}
|
|
144
|
-
return [action, path, nextValue];
|
|
145
|
-
});
|
|
146
|
-
const baseOrders = computeOrdersForTuples(root, hydratedGranularChanges);
|
|
147
|
-
const preferOrdersMap = /* @__PURE__ */ new Map();
|
|
148
|
-
for (let i = 0; i < tuples.length; i++) {
|
|
149
|
-
const t = tuples[i];
|
|
150
|
-
if (!Array.isArray(t) || t.length < 3) {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const [action, path, value] = t;
|
|
154
|
-
const isFilesPath = Array.isArray(path) && path[0] === "files";
|
|
155
|
-
if (action !== "update" || !Array.isArray(path) || path.length !== 1 && path.length !== 2 || !isPlainObject(value) || isFilesPath || value && value.type === "files") {
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
const keys = Object.keys(value).filter((k) => k !== "__order");
|
|
159
|
-
const key = JSON.stringify(path);
|
|
160
|
-
preferOrdersMap.set(key, { path, keys });
|
|
161
|
-
}
|
|
162
|
-
const mergedOrders = [];
|
|
163
|
-
const seen = /* @__PURE__ */ new Set();
|
|
164
|
-
preferOrdersMap.forEach((v, k) => {
|
|
165
|
-
seen.add(k);
|
|
166
|
-
mergedOrders.push(v);
|
|
167
|
-
});
|
|
168
|
-
for (let i = 0; i < baseOrders.length; i++) {
|
|
169
|
-
const v = baseOrders[i];
|
|
170
|
-
const k = JSON.stringify(v.path);
|
|
171
|
-
if (!seen.has(k)) {
|
|
172
|
-
seen.add(k);
|
|
173
|
-
mergedOrders.push(v);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return { granularChanges: hydratedGranularChanges, orders: mergedOrders };
|
|
177
|
-
}
|
|
178
|
-
export {
|
|
179
|
-
preprocessChanges
|
|
180
|
-
};
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
function isPlainObject(o) {
|
|
2
|
-
return o && typeof o === "object" && !Array.isArray(o);
|
|
3
|
-
}
|
|
4
|
-
function deepEqual(a, b) {
|
|
5
|
-
if (Object.is(a, b)) {
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
if (typeof a === "function" && typeof b === "function") {
|
|
9
|
-
try {
|
|
10
|
-
return a.toString() === b.toString();
|
|
11
|
-
} catch {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
if (typeof a === "function" || typeof b === "function") {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
if (a instanceof Date && b instanceof Date) {
|
|
19
|
-
return a.getTime() === b.getTime();
|
|
20
|
-
}
|
|
21
|
-
if (a instanceof RegExp && b instanceof RegExp) {
|
|
22
|
-
return String(a) === String(b);
|
|
23
|
-
}
|
|
24
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
25
|
-
if (a.length !== b.length) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
for (let i = 0; i < a.length; i++) {
|
|
29
|
-
if (!deepEqual(a[i], b[i])) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
36
|
-
const aKeys = Object.keys(a);
|
|
37
|
-
const bKeys = Object.keys(b);
|
|
38
|
-
if (aKeys.length !== bKeys.length) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
for (let i = 0; i < aKeys.length; i++) {
|
|
42
|
-
const key = aKeys[i];
|
|
43
|
-
if (!Object.hasOwn(b, key)) {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
if (!deepEqual(a[key], b[key])) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
import * as Y from "yjs";
|
|
55
|
-
function getRootMap(ydoc) {
|
|
56
|
-
return ydoc.getMap("root");
|
|
57
|
-
}
|
|
58
|
-
function diffJson(prev, next, prefix = []) {
|
|
59
|
-
const ops = [];
|
|
60
|
-
const _prefix = Array.isArray(prefix) ? prefix : [];
|
|
61
|
-
for (const key in prev) {
|
|
62
|
-
if (Object.hasOwn(prev, key) && !(key in next)) {
|
|
63
|
-
ops.push({ action: "del", path: [..._prefix, key] });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
for (const key in next) {
|
|
67
|
-
if (Object.hasOwn(next, key)) {
|
|
68
|
-
const pVal = prev == null ? void 0 : prev[key];
|
|
69
|
-
const nVal = next[key];
|
|
70
|
-
if (isPlainObject(pVal) && isPlainObject(nVal)) {
|
|
71
|
-
ops.push(...diffJson(pVal, nVal, [..._prefix, key]));
|
|
72
|
-
} else if (!deepEqual(pVal, nVal)) {
|
|
73
|
-
ops.push({ action: "set", path: [..._prefix, key], value: nVal });
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return ops;
|
|
78
|
-
}
|
|
79
|
-
function applyOpsToJson(ops, ydoc) {
|
|
80
|
-
if (!ydoc || !Array.isArray(ops) || !ops.length) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
ydoc.transact(() => {
|
|
84
|
-
const root = getRootMap(ydoc);
|
|
85
|
-
ops.forEach((op) => {
|
|
86
|
-
const { action, path = [], value } = op || {};
|
|
87
|
-
if (!path.length) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
let target = root;
|
|
91
|
-
for (let i = 0; i < path.length - 1; i++) {
|
|
92
|
-
const key = path[i];
|
|
93
|
-
let next = target.get(key);
|
|
94
|
-
if (!(next instanceof Y.Map)) {
|
|
95
|
-
const fresh = new Y.Map();
|
|
96
|
-
if (isPlainObject(next)) {
|
|
97
|
-
Object.entries(next).forEach(([k, v]) => fresh.set(k, v));
|
|
98
|
-
}
|
|
99
|
-
target.set(key, fresh);
|
|
100
|
-
next = fresh;
|
|
101
|
-
}
|
|
102
|
-
target = next;
|
|
103
|
-
}
|
|
104
|
-
const last = path[path.length - 1];
|
|
105
|
-
if (action === "set") {
|
|
106
|
-
target.set(last, value);
|
|
107
|
-
} else if (action === "del") {
|
|
108
|
-
target.delete(last);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}, "remote");
|
|
112
|
-
}
|
|
113
|
-
export {
|
|
114
|
-
applyOpsToJson,
|
|
115
|
-
diffJson
|
|
116
|
-
};
|