cursor-switch 1.0.7 → 1.0.8
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 +76 -75
- package/SECURITY.md +22 -19
- package/bin/cswitch.js +31 -8
- package/dist/cli.d.ts +0 -4
- package/dist/cli.js +404 -241
- package/dist/core/account-store.d.ts +0 -7
- package/dist/core/account-store.js +22 -28
- package/dist/core/cdp-engine.d.ts +11 -49
- package/dist/core/cdp-engine.js +155 -110
- package/dist/core/config-store.d.ts +7 -6
- package/dist/core/config-store.js +33 -12
- package/dist/core/cursor-paths.d.ts +8 -18
- package/dist/core/cursor-paths.js +18 -37
- package/dist/core/dashboard-launcher.d.ts +0 -20
- package/dist/core/dashboard-launcher.js +3 -53
- package/dist/core/extract-client.d.ts +57 -0
- package/dist/core/extract-client.js +219 -0
- package/dist/core/extract-menu.d.ts +3 -0
- package/dist/core/extract-menu.js +360 -0
- package/dist/core/extract-service.d.ts +64 -0
- package/dist/core/extract-service.js +435 -0
- package/dist/core/fingerprint.d.ts +0 -4
- package/dist/core/fingerprint.js +0 -6
- package/dist/core/oauth-receiver.d.ts +1 -11
- package/dist/core/oauth-receiver.js +509 -527
- package/dist/core/official-api.d.ts +0 -17
- package/dist/core/official-api.js +4 -23
- package/dist/core/outlook-launcher.d.ts +0 -31
- package/dist/core/outlook-launcher.js +194 -248
- package/dist/core/process-manager.d.ts +0 -14
- package/dist/core/process-manager.js +7 -22
- package/dist/core/pure-login-service.d.ts +54 -38
- package/dist/core/pure-login-service.js +483 -395
- package/dist/core/refund-client.d.ts +0 -22
- package/dist/core/refund-client.js +7 -27
- package/dist/core/refund-service.d.ts +0 -16
- package/dist/core/refund-service.js +8 -34
- package/dist/core/safe-storage.d.ts +27 -0
- package/dist/core/safe-storage.js +284 -0
- package/dist/core/sqlite-bridge.d.ts +10 -11
- package/dist/core/sqlite-bridge.js +112 -47
- package/dist/core/storage-bridge.d.ts +14 -5
- package/dist/core/storage-bridge.js +84 -24
- package/dist/core/switch-orchestrator.d.ts +96 -0
- package/dist/core/switch-orchestrator.js +341 -0
- package/dist/core/token-exchange.d.ts +81 -0
- package/dist/core/token-exchange.js +275 -0
- package/dist/core/token-parser.d.ts +56 -20
- package/dist/core/token-parser.js +354 -43
- package/dist/core/update-checker.d.ts +1 -29
- package/dist/core/update-checker.js +28 -43
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -3
- package/dist/menu.d.ts +0 -7
- package/dist/menu.js +445 -548
- package/dist/types.d.ts +12 -4
- package/dist/types.js +0 -4
- package/dist/utils/account-text-importer.d.ts +9 -0
- package/dist/utils/account-text-importer.js +96 -0
- package/dist/utils/browser-utils.d.ts +0 -24
- package/dist/utils/browser-utils.js +0 -57
- package/dist/utils/clipboard.d.ts +0 -4
- package/dist/utils/clipboard.js +0 -5
- package/dist/utils/credential-bundle-ui.d.ts +36 -0
- package/dist/utils/credential-bundle-ui.js +246 -0
- package/dist/utils/live-menu.d.ts +37 -9
- package/dist/utils/live-menu.js +510 -81
- package/dist/utils/plan-formatter.d.ts +0 -26
- package/dist/utils/plan-formatter.js +10 -44
- package/dist/utils/prompt-utils.d.ts +0 -18
- package/dist/utils/prompt-utils.js +0 -23
- package/dist/utils/proxy-bootstrap.d.ts +12 -0
- package/dist/utils/proxy-bootstrap.js +141 -0
- package/dist/utils/security.d.ts +2 -4
- package/dist/utils/security.js +36 -4
- package/dist/utils/table-renderer.d.ts +0 -4
- package/dist/utils/table-renderer.js +24 -8
- package/dist/utils/time-formatter.d.ts +0 -4
- package/dist/utils/time-formatter.js +0 -8
- package/dist/utils/token-preflight-ui.d.ts +28 -0
- package/dist/utils/token-preflight-ui.js +222 -0
- package/dist/utils/window-focus.d.ts +3 -0
- package/dist/utils/window-focus.js +106 -0
- package/package.json +12 -5
- package/dist/core/welcome-offer-service.d.ts +0 -52
- package/dist/core/welcome-offer-service.js +0 -369
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 智能凭据解析引擎
|
|
3
|
-
* 仅支持 WorkOS Session Token (user_xxx::eyJ...) 与 纯 JWT Token (eyJ...)
|
|
4
|
-
*/
|
|
5
1
|
export class TokenParser {
|
|
6
|
-
/**
|
|
7
|
-
* 极简 base64url 解码
|
|
8
|
-
*/
|
|
9
2
|
static decodeBase64Url(str) {
|
|
10
3
|
let base64 = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
11
4
|
while (base64.length % 4 !== 0) {
|
|
@@ -13,9 +6,6 @@ export class TokenParser {
|
|
|
13
6
|
}
|
|
14
7
|
return Buffer.from(base64, 'base64').toString('utf8');
|
|
15
8
|
}
|
|
16
|
-
/**
|
|
17
|
-
* 提取纯 Token 部分(去除前缀 user_xxx::)
|
|
18
|
-
*/
|
|
19
9
|
static extractTokenPart(rawToken) {
|
|
20
10
|
const trimmed = rawToken.trim().replace(/%3A/gi, ':');
|
|
21
11
|
if (trimmed.includes('::')) {
|
|
@@ -24,18 +14,12 @@ export class TokenParser {
|
|
|
24
14
|
}
|
|
25
15
|
return trimmed;
|
|
26
16
|
}
|
|
27
|
-
/**
|
|
28
|
-
* 判断是否具有 JWT 的基本形态
|
|
29
|
-
*/
|
|
30
17
|
static isJwtShaped(str) {
|
|
31
18
|
if (!str.startsWith('eyJ'))
|
|
32
19
|
return false;
|
|
33
20
|
const parts = str.split('.');
|
|
34
21
|
return parts.length === 3 && parts.every((p) => p.length > 0);
|
|
35
22
|
}
|
|
36
|
-
/**
|
|
37
|
-
* 解析 JWT Payload
|
|
38
|
-
*/
|
|
39
23
|
static parseJwtPayload(jwt) {
|
|
40
24
|
if (!this.isJwtShaped(jwt))
|
|
41
25
|
return null;
|
|
@@ -48,59 +32,386 @@ export class TokenParser {
|
|
|
48
32
|
return null;
|
|
49
33
|
}
|
|
50
34
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
let cleaned = input.trim().replace(/^["']|["']$/g, '').trim();
|
|
35
|
+
static EMAIL_RE = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
|
|
36
|
+
static PLACEHOLDER_PARTS = new Set(['-', 'null', 'none', '无', '(无)', '空', '(空)', 'n/a']);
|
|
37
|
+
static cleanSegmentText(seg) {
|
|
38
|
+
let cleaned = seg.trim().replace(/^["']|["']$/g, '').trim();
|
|
56
39
|
if (cleaned.toLowerCase().startsWith('workoscursorsessiontoken=')) {
|
|
57
40
|
cleaned = cleaned.slice('workoscursorsessiontoken='.length).trim().replace(/^["']|["']$/g, '').trim();
|
|
58
41
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
return cleaned.replace(/%3A/gi, ':');
|
|
43
|
+
}
|
|
44
|
+
static isCompleteSingleCredential(piece) {
|
|
45
|
+
const normalized = this.cleanSegmentText(piece);
|
|
46
|
+
if (!normalized)
|
|
47
|
+
return false;
|
|
48
|
+
if (normalized.startsWith('user_') && normalized.includes('::')) {
|
|
49
|
+
return this.isJwtShaped(this.extractTokenPart(normalized));
|
|
50
|
+
}
|
|
51
|
+
return this.isJwtShaped(normalized);
|
|
52
|
+
}
|
|
53
|
+
static splitCredentialSegments(input) {
|
|
54
|
+
const raw = (input || '').trim().replace(/^["']|["']$/g, '').trim();
|
|
55
|
+
if (!raw)
|
|
56
|
+
return [];
|
|
57
|
+
if (raw.includes('----') || raw.includes('|') || raw.includes(';') || raw.includes(';') || raw.includes('\t')) {
|
|
58
|
+
return raw
|
|
59
|
+
.split(/----|\||;|;|\t/)
|
|
60
|
+
.map((s) => this.cleanSegmentText(s.replace(/[\r\n\s]+/g, '')))
|
|
61
|
+
.filter((s) => Boolean(s) && !this.PLACEHOLDER_PARTS.has(s.toLowerCase()));
|
|
62
|
+
}
|
|
63
|
+
if (/[\r\n\s]/.test(raw)) {
|
|
64
|
+
const wsParts = raw
|
|
65
|
+
.split(/[\r\n\s]+/)
|
|
66
|
+
.map((s) => this.cleanSegmentText(s))
|
|
67
|
+
.filter((s) => Boolean(s) && !this.PLACEHOLDER_PARTS.has(s.toLowerCase()));
|
|
68
|
+
const hasStandaloneToken = wsParts.some((p) => this.isCompleteSingleCredential(p));
|
|
69
|
+
if (wsParts.length >= 2 && wsParts.length <= 5 && hasStandaloneToken) {
|
|
70
|
+
return wsParts;
|
|
71
|
+
}
|
|
72
|
+
const joined = this.cleanSegmentText(raw.replace(/[\r\n\s]+/g, ''));
|
|
73
|
+
return joined ? [joined] : [];
|
|
62
74
|
}
|
|
63
|
-
|
|
64
|
-
|
|
75
|
+
const single = this.cleanSegmentText(raw);
|
|
76
|
+
return single ? [single] : [];
|
|
77
|
+
}
|
|
78
|
+
static extractEmailFromPayload(payload) {
|
|
79
|
+
if (!payload)
|
|
80
|
+
return undefined;
|
|
81
|
+
return (payload.email ||
|
|
82
|
+
payload['https://cursor.com/email'] ||
|
|
83
|
+
payload['https://cursor.sh/email'] ||
|
|
84
|
+
payload.preferred_username ||
|
|
85
|
+
(typeof payload.sub === 'string' && payload.sub.includes('@') ? payload.sub : undefined));
|
|
86
|
+
}
|
|
87
|
+
static parseSingleCredential(normalized) {
|
|
65
88
|
if (normalized.startsWith('user_') && normalized.includes('::')) {
|
|
66
89
|
const tokenPart = this.extractTokenPart(normalized);
|
|
67
90
|
if (this.isJwtShaped(tokenPart)) {
|
|
68
91
|
const payload = this.parseJwtPayload(tokenPart);
|
|
69
|
-
const email = payload?.email ||
|
|
70
|
-
payload?.['https://cursor.com/email'] ||
|
|
71
|
-
payload?.['https://cursor.sh/email'] ||
|
|
72
|
-
payload?.preferred_username ||
|
|
73
|
-
(payload?.sub?.includes('@') ? payload.sub : undefined);
|
|
74
92
|
const expiresAtMs = payload?.exp ? payload.exp * 1000 : undefined;
|
|
93
|
+
const jwtType = String(payload?.type || '').toLowerCase();
|
|
75
94
|
return {
|
|
76
|
-
email,
|
|
95
|
+
email: this.extractEmailFromPayload(payload),
|
|
77
96
|
token: tokenPart,
|
|
78
97
|
workosSessionToken: normalized,
|
|
79
98
|
expiresAtMs,
|
|
80
99
|
sub: payload?.sub,
|
|
100
|
+
userId: this.extractUserId(normalized),
|
|
101
|
+
tokenType: jwtType === 'web' ? 'web' : 'client',
|
|
102
|
+
segmentCount: 1,
|
|
81
103
|
isValid: true,
|
|
82
104
|
format: 'session',
|
|
83
105
|
};
|
|
84
106
|
}
|
|
85
107
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const payload = this.parseJwtPayload(trimmed);
|
|
89
|
-
const email = payload?.email ||
|
|
90
|
-
payload?.['https://cursor.com/email'] ||
|
|
91
|
-
payload?.['https://cursor.sh/email'] ||
|
|
92
|
-
payload?.preferred_username ||
|
|
93
|
-
(payload?.sub?.includes('@') ? payload.sub : undefined);
|
|
108
|
+
if (this.isJwtShaped(normalized)) {
|
|
109
|
+
const payload = this.parseJwtPayload(normalized);
|
|
94
110
|
const expiresAtMs = payload?.exp ? payload.exp * 1000 : undefined;
|
|
111
|
+
const jwtType = String(payload?.type || '').toLowerCase();
|
|
95
112
|
return {
|
|
96
|
-
email,
|
|
97
|
-
token:
|
|
113
|
+
email: this.extractEmailFromPayload(payload),
|
|
114
|
+
token: normalized,
|
|
98
115
|
expiresAtMs,
|
|
99
116
|
sub: payload?.sub,
|
|
117
|
+
userId: this.extractUserId(normalized),
|
|
118
|
+
tokenType: jwtType === 'web' ? 'web' : 'client',
|
|
119
|
+
segmentCount: 1,
|
|
100
120
|
isValid: true,
|
|
101
121
|
format: 'jwt',
|
|
102
122
|
};
|
|
103
123
|
}
|
|
104
|
-
return
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
static parse(input) {
|
|
127
|
+
const segments = this.splitCredentialSegments(input);
|
|
128
|
+
if (segments.length === 0) {
|
|
129
|
+
return { token: '', isValid: false, format: 'unknown' };
|
|
130
|
+
}
|
|
131
|
+
if (segments.length === 1) {
|
|
132
|
+
return this.parseSingleCredential(segments[0]) || { token: segments[0], isValid: false, format: 'unknown' };
|
|
133
|
+
}
|
|
134
|
+
let explicitEmail;
|
|
135
|
+
let accessJwt;
|
|
136
|
+
let accessPayload = null;
|
|
137
|
+
let webJwt;
|
|
138
|
+
let webPayload = null;
|
|
139
|
+
let workosSessionToken;
|
|
140
|
+
let workosIsWeb = false;
|
|
141
|
+
let sessionInnerJwt;
|
|
142
|
+
let sessionInnerPayload = null;
|
|
143
|
+
let refreshToken;
|
|
144
|
+
for (const seg of segments) {
|
|
145
|
+
if (!explicitEmail && this.EMAIL_RE.test(seg)) {
|
|
146
|
+
explicitEmail = seg.toLowerCase();
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (seg.startsWith('user_') && seg.includes('::')) {
|
|
150
|
+
const inner = this.extractTokenPart(seg);
|
|
151
|
+
if (this.isJwtShaped(inner)) {
|
|
152
|
+
const payload = this.parseJwtPayload(inner);
|
|
153
|
+
const jwtType = String(payload?.type || '').toLowerCase();
|
|
154
|
+
if (!workosSessionToken || (!workosIsWeb && jwtType === 'web')) {
|
|
155
|
+
workosSessionToken = seg;
|
|
156
|
+
workosIsWeb = jwtType === 'web';
|
|
157
|
+
}
|
|
158
|
+
if (jwtType === 'web') {
|
|
159
|
+
if (!webJwt) {
|
|
160
|
+
webJwt = inner;
|
|
161
|
+
webPayload = payload;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else if (!sessionInnerJwt) {
|
|
165
|
+
sessionInnerJwt = inner;
|
|
166
|
+
sessionInnerPayload = payload;
|
|
167
|
+
}
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (this.isJwtShaped(seg)) {
|
|
172
|
+
const payload = this.parseJwtPayload(seg);
|
|
173
|
+
const jwtType = String(payload?.type || '').toLowerCase();
|
|
174
|
+
const scope = String(payload?.scope || '').toLowerCase();
|
|
175
|
+
if (jwtType === 'web') {
|
|
176
|
+
if (!webJwt) {
|
|
177
|
+
webJwt = seg;
|
|
178
|
+
webPayload = payload;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else if (scope.includes('refresh')) {
|
|
182
|
+
if (!refreshToken) {
|
|
183
|
+
refreshToken = seg;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else if (!accessJwt) {
|
|
187
|
+
accessJwt = seg;
|
|
188
|
+
accessPayload = payload;
|
|
189
|
+
}
|
|
190
|
+
else if (!refreshToken) {
|
|
191
|
+
refreshToken = seg;
|
|
192
|
+
}
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (!seg.includes('::') &&
|
|
196
|
+
!seg.includes('@') &&
|
|
197
|
+
seg.length >= 10 &&
|
|
198
|
+
/^[A-Za-z0-9._$!+\-/=:-]{10,}$/.test(seg)) {
|
|
199
|
+
if (!refreshToken) {
|
|
200
|
+
refreshToken = seg;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const primaryToken = accessJwt || sessionInnerJwt || webJwt || '';
|
|
205
|
+
if (!primaryToken || !this.isJwtShaped(primaryToken)) {
|
|
206
|
+
return { token: segments[0], isValid: false, format: 'unknown' };
|
|
207
|
+
}
|
|
208
|
+
const primaryPayload = accessPayload || sessionInnerPayload || webPayload;
|
|
209
|
+
const derivedUserId = this.extractUserId(workosSessionToken) ||
|
|
210
|
+
this.extractUserId(primaryToken) ||
|
|
211
|
+
this.extractUserId(webJwt);
|
|
212
|
+
if (!workosSessionToken && webJwt && derivedUserId) {
|
|
213
|
+
workosSessionToken = `${derivedUserId}::${webJwt}`;
|
|
214
|
+
}
|
|
215
|
+
const cleanRefresh = refreshToken && !refreshToken.includes('::') && refreshToken !== webJwt ? refreshToken : undefined;
|
|
216
|
+
const primaryJwtType = String(primaryPayload?.type || '').toLowerCase();
|
|
217
|
+
const tokenType = primaryJwtType === 'web' && !cleanRefresh ? 'web' : 'client';
|
|
218
|
+
return {
|
|
219
|
+
email: explicitEmail || this.extractEmailFromPayload(primaryPayload) || this.extractEmailFromPayload(webPayload),
|
|
220
|
+
token: primaryToken,
|
|
221
|
+
refreshToken: cleanRefresh,
|
|
222
|
+
workosSessionToken,
|
|
223
|
+
expiresAtMs: primaryPayload?.exp ? primaryPayload.exp * 1000 : undefined,
|
|
224
|
+
sub: primaryPayload?.sub || webPayload?.sub,
|
|
225
|
+
userId: derivedUserId,
|
|
226
|
+
tokenType,
|
|
227
|
+
segmentCount: segments.length,
|
|
228
|
+
isValid: true,
|
|
229
|
+
format: 'bundle',
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
static extractAuthId(rawToken) {
|
|
233
|
+
if (!rawToken)
|
|
234
|
+
return undefined;
|
|
235
|
+
const jwt = this.extractTokenPart(rawToken);
|
|
236
|
+
const payload = this.parseJwtPayload(jwt);
|
|
237
|
+
if (typeof payload?.sub === 'string' && payload.sub.trim()) {
|
|
238
|
+
return payload.sub.trim();
|
|
239
|
+
}
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
static extractUserId(rawTokenOrSub) {
|
|
243
|
+
if (!rawTokenOrSub)
|
|
244
|
+
return undefined;
|
|
245
|
+
const normalized = rawTokenOrSub.trim().replace(/%3A/gi, ':');
|
|
246
|
+
if (normalized.includes('::')) {
|
|
247
|
+
const prefix = normalized.split('::')[0].trim();
|
|
248
|
+
const match = prefix.match(/(user_[A-Za-z0-9]+)/);
|
|
249
|
+
if (match)
|
|
250
|
+
return match[1];
|
|
251
|
+
if (prefix)
|
|
252
|
+
return prefix;
|
|
253
|
+
}
|
|
254
|
+
const jwt = this.extractTokenPart(normalized);
|
|
255
|
+
const payload = this.parseJwtPayload(jwt);
|
|
256
|
+
const rawSub = (typeof payload?.sub === 'string' && payload.sub) ||
|
|
257
|
+
(typeof payload?.userId === 'string' && payload.userId) ||
|
|
258
|
+
(typeof payload?.user_id === 'string' && payload.user_id) ||
|
|
259
|
+
normalized;
|
|
260
|
+
const bareMatch = String(rawSub).match(/(user_[A-Za-z0-9]+)/);
|
|
261
|
+
if (bareMatch)
|
|
262
|
+
return bareMatch[1];
|
|
263
|
+
if (String(rawSub).includes('|')) {
|
|
264
|
+
const tail = String(rawSub).split('|').pop()?.trim();
|
|
265
|
+
return tail || undefined;
|
|
266
|
+
}
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
static classify(token, explicitRefreshToken, nowMs = Date.now()) {
|
|
270
|
+
const cleanToken = this.extractTokenPart(token);
|
|
271
|
+
const payload = this.parseJwtPayload(cleanToken);
|
|
272
|
+
const jwtType = String(payload?.type || '').toLowerCase();
|
|
273
|
+
const email = payload?.email ||
|
|
274
|
+
payload?.['https://cursor.com/email'] ||
|
|
275
|
+
payload?.['https://cursor.sh/email'] ||
|
|
276
|
+
payload?.preferred_username ||
|
|
277
|
+
(typeof payload?.sub === 'string' && payload.sub.includes('@') ? payload.sub : undefined);
|
|
278
|
+
const authId = typeof payload?.sub === 'string' && payload.sub.trim() ? payload.sub.trim() : undefined;
|
|
279
|
+
const userId = this.extractUserId(token) || this.extractUserId(authId);
|
|
280
|
+
const expiresAtMs = typeof payload?.exp === 'number' ? payload.exp * 1000 : undefined;
|
|
281
|
+
const rawIssuedSec = Number(payload?.time ?? payload?.iat);
|
|
282
|
+
const issuedAtMs = Number.isFinite(rawIssuedSec) && rawIssuedSec > 1_000_000_000
|
|
283
|
+
? rawIssuedSec * 1000
|
|
284
|
+
: undefined;
|
|
285
|
+
const ageHours = issuedAtMs !== undefined && nowMs >= issuedAtMs
|
|
286
|
+
? Math.round(((nowMs - issuedAtMs) / 3_600_000) * 10) / 10
|
|
287
|
+
: undefined;
|
|
288
|
+
const cleanRefreshRaw = explicitRefreshToken ? explicitRefreshToken.trim() : '';
|
|
289
|
+
const hasUsableRefreshToken = Boolean(cleanRefreshRaw &&
|
|
290
|
+
!cleanRefreshRaw.includes('::') &&
|
|
291
|
+
!(jwtType === 'web' && cleanRefreshRaw === cleanToken));
|
|
292
|
+
const tokenType = jwtType === 'web' && !hasUsableRefreshToken ? 'web' : 'client';
|
|
293
|
+
const isExpired = Boolean(expiresAtMs && expiresAtMs <= nowMs);
|
|
294
|
+
const credentialState = isExpired && !hasUsableRefreshToken
|
|
295
|
+
? 'expired'
|
|
296
|
+
: tokenType === 'web'
|
|
297
|
+
? 'web_only'
|
|
298
|
+
: hasUsableRefreshToken
|
|
299
|
+
? 'client_with_rt'
|
|
300
|
+
: 'client_no_rt';
|
|
301
|
+
return {
|
|
302
|
+
tokenType,
|
|
303
|
+
credentialState,
|
|
304
|
+
cleanToken,
|
|
305
|
+
cleanRefreshToken: hasUsableRefreshToken ? cleanRefreshRaw : undefined,
|
|
306
|
+
hasUsableRefreshToken,
|
|
307
|
+
isExpired,
|
|
308
|
+
isExpiringSoon: Boolean(expiresAtMs && expiresAtMs <= nowMs + 5 * 60 * 1000),
|
|
309
|
+
expiresAtMs,
|
|
310
|
+
issuedAtMs,
|
|
311
|
+
ageHours,
|
|
312
|
+
authId,
|
|
313
|
+
userId,
|
|
314
|
+
email: typeof email === 'string' ? email : undefined,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
static extractWebSessionToken(rawOrBundle) {
|
|
318
|
+
const raw = (rawOrBundle || '').trim();
|
|
319
|
+
if (!raw)
|
|
320
|
+
return '';
|
|
321
|
+
const parsed = TokenParser.parse(raw);
|
|
322
|
+
if (!parsed.isValid)
|
|
323
|
+
return raw;
|
|
324
|
+
return parsed.workosSessionToken || parsed.token;
|
|
325
|
+
}
|
|
326
|
+
static extractEffectiveRefreshToken(token, explicitRefreshToken, workosSessionToken) {
|
|
327
|
+
const cleanExplicit = explicitRefreshToken?.trim();
|
|
328
|
+
if (cleanExplicit) {
|
|
329
|
+
return cleanExplicit;
|
|
330
|
+
}
|
|
331
|
+
if (!workosSessionToken) {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
const rawAccessClean = TokenParser.extractTokenPart(token);
|
|
335
|
+
const legacyCandidate = workosSessionToken.trim();
|
|
336
|
+
const legacyJwtType = String(TokenParser.parseJwtPayload(legacyCandidate)?.type || '').toLowerCase();
|
|
337
|
+
if (legacyCandidate &&
|
|
338
|
+
!legacyCandidate.includes('::') &&
|
|
339
|
+
legacyCandidate !== rawAccessClean &&
|
|
340
|
+
legacyJwtType !== 'web') {
|
|
341
|
+
return legacyCandidate;
|
|
342
|
+
}
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
static classifyAccount(account, nowMs = Date.now()) {
|
|
346
|
+
const effectiveRefresh = TokenParser.extractEffectiveRefreshToken(account.token, account.refresh_token, account.workos_cursor_session_token);
|
|
347
|
+
return TokenParser.classify(account.token, effectiveRefresh, nowMs);
|
|
348
|
+
}
|
|
349
|
+
static formatCredentialBadge(cls) {
|
|
350
|
+
switch (cls.credentialState) {
|
|
351
|
+
case 'client_with_rt':
|
|
352
|
+
return '60d/RT';
|
|
353
|
+
case 'client_no_rt':
|
|
354
|
+
return '客户端';
|
|
355
|
+
case 'web_only':
|
|
356
|
+
return '仅Web';
|
|
357
|
+
case 'expired':
|
|
358
|
+
return '已过期';
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
static formatCredentialBundles(account, opts = {}) {
|
|
362
|
+
const separator = opts.separator || '----';
|
|
363
|
+
const classification = TokenParser.classifyAccount(account);
|
|
364
|
+
const accessToken = classification.cleanToken || undefined;
|
|
365
|
+
const refreshToken = classification.cleanRefreshToken;
|
|
366
|
+
let workosToken;
|
|
367
|
+
const rawWorkos = account.workos_cursor_session_token?.trim().replace(/%3A/gi, ':');
|
|
368
|
+
if (rawWorkos && rawWorkos.startsWith('user_') && rawWorkos.includes('::')) {
|
|
369
|
+
const inner = TokenParser.extractTokenPart(rawWorkos);
|
|
370
|
+
if (TokenParser.isJwtShaped(inner)) {
|
|
371
|
+
workosToken = rawWorkos;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else if (rawWorkos && TokenParser.isJwtShaped(rawWorkos)) {
|
|
375
|
+
const uid = TokenParser.extractUserId(rawWorkos) || classification.userId;
|
|
376
|
+
if (uid) {
|
|
377
|
+
workosToken = `${uid}::${rawWorkos}`;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else if (!rawWorkos && classification.tokenType === 'web' && accessToken && classification.userId) {
|
|
381
|
+
workosToken = `${classification.userId}::${accessToken}`;
|
|
382
|
+
}
|
|
383
|
+
const singleToken = classification.tokenType === 'web' && workosToken
|
|
384
|
+
? workosToken
|
|
385
|
+
: accessToken || (account.token || '').trim();
|
|
386
|
+
let tripleBundle;
|
|
387
|
+
if (classification.credentialState === 'client_with_rt' && accessToken && refreshToken) {
|
|
388
|
+
const webSegment = workosToken ||
|
|
389
|
+
(classification.userId && TokenParser.isJwtShaped(accessToken)
|
|
390
|
+
? `${classification.userId}::${accessToken}`
|
|
391
|
+
: accessToken);
|
|
392
|
+
tripleBundle = [accessToken, refreshToken, webSegment].join(separator);
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
classification,
|
|
396
|
+
accessToken,
|
|
397
|
+
refreshToken,
|
|
398
|
+
workosToken,
|
|
399
|
+
singleToken,
|
|
400
|
+
tripleBundle,
|
|
401
|
+
separator,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
static formatAgeText(ageHours) {
|
|
405
|
+
if (ageHours === undefined || !Number.isFinite(ageHours)) {
|
|
406
|
+
return '未知签发时间';
|
|
407
|
+
}
|
|
408
|
+
if (ageHours < 1) {
|
|
409
|
+
return '不到 1 小时前';
|
|
410
|
+
}
|
|
411
|
+
if (ageHours >= 24) {
|
|
412
|
+
const days = Math.round((ageHours / 24) * 10) / 10;
|
|
413
|
+
return `约 ${Math.round(ageHours)} 小时(${days} 天)前`;
|
|
414
|
+
}
|
|
415
|
+
return `约 ${ageHours} 小时前`;
|
|
105
416
|
}
|
|
106
417
|
}
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 全自动跨平台版本检查与自升级引擎 (UpdateChecker & Auto-Upgrader)
|
|
3
|
-
* 特性:
|
|
4
|
-
* 1. 24小时本地缓存 + 0ms 零阻塞启动
|
|
5
|
-
* 2. 官方 npm 与淘宝 npmmirror 双源故障转移容灾 (2.5秒超快超时熔断)
|
|
6
|
-
* 3. 智能包管理器探测 (自动适配 npm / pnpm / yarn / bun)
|
|
7
|
-
* 4. 零第三方依赖,纯 Node.js 原生轻量实现
|
|
8
|
-
*/
|
|
9
1
|
export declare const CURRENT_VERSION: string;
|
|
2
|
+
export declare const IS_PRIVATE_EDITION: boolean;
|
|
10
3
|
export declare const PACKAGE_NAME = "cursor-switch";
|
|
11
4
|
export interface VersionCheckResult {
|
|
12
5
|
currentVersion: string;
|
|
@@ -20,41 +13,20 @@ export interface UpgradeResult {
|
|
|
20
13
|
packageManager: string;
|
|
21
14
|
error?: string;
|
|
22
15
|
}
|
|
23
|
-
/**
|
|
24
|
-
* 比较两个语义化版本号,若 v1 > v2 则返回 true
|
|
25
|
-
*/
|
|
26
16
|
export declare function isVersionGreater(v1: string, v2: string): boolean;
|
|
27
17
|
export declare class UpdateChecker {
|
|
28
18
|
private configStore;
|
|
29
19
|
private static isCheckingBackground;
|
|
30
20
|
constructor();
|
|
31
|
-
/**
|
|
32
|
-
* 检查最新版本 (默认优先使用缓存,绝不阻塞主 UI)
|
|
33
|
-
*/
|
|
34
21
|
check(forceFresh?: boolean): Promise<VersionCheckResult>;
|
|
35
|
-
/**
|
|
36
|
-
* 后台非阻塞异步静默检查
|
|
37
|
-
*/
|
|
38
22
|
triggerBackgroundCheck(): void;
|
|
39
|
-
/**
|
|
40
|
-
* 极速无感知检测本地缓存,如果已有新版本则打印单行轻量提示 (0ms 纯本地无网络)
|
|
41
|
-
*/
|
|
42
23
|
static printCachedNoticeIfAvailable(): void;
|
|
43
|
-
/**
|
|
44
|
-
* 快速从 npm 官方源或国内 npmmirror 镜像探测最新版本
|
|
45
|
-
*/
|
|
46
24
|
private fetchLatestFromRegistries;
|
|
47
|
-
/**
|
|
48
|
-
* 智能嗅探当前环境使用的包管理器 (支持国内 npmmirror 加速参数)
|
|
49
|
-
*/
|
|
50
25
|
detectPackageManager(useMirror?: boolean): {
|
|
51
26
|
name: 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
52
27
|
cmd: string;
|
|
53
28
|
args: string[];
|
|
54
29
|
};
|
|
55
|
-
/**
|
|
56
|
-
* 执行一键自升级
|
|
57
|
-
*/
|
|
58
30
|
performUpgrade(options?: {
|
|
59
31
|
useMirror?: boolean;
|
|
60
32
|
onProgress?: (line: string) => void;
|
|
@@ -1,36 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 全自动跨平台版本检查与自升级引擎 (UpdateChecker & Auto-Upgrader)
|
|
3
|
-
* 特性:
|
|
4
|
-
* 1. 24小时本地缓存 + 0ms 零阻塞启动
|
|
5
|
-
* 2. 官方 npm 与淘宝 npmmirror 双源故障转移容灾 (2.5秒超快超时熔断)
|
|
6
|
-
* 3. 智能包管理器探测 (自动适配 npm / pnpm / yarn / bun)
|
|
7
|
-
* 4. 零第三方依赖,纯 Node.js 原生轻量实现
|
|
8
|
-
*/
|
|
9
1
|
import fs from 'node:fs';
|
|
10
2
|
import path from 'node:path';
|
|
11
3
|
import { fileURLToPath } from 'node:url';
|
|
12
4
|
import { spawn } from 'node:child_process';
|
|
13
5
|
import chalk from 'chalk';
|
|
14
6
|
import { ConfigStore } from './config-store.js';
|
|
15
|
-
function
|
|
7
|
+
function readPackageMeta() {
|
|
16
8
|
try {
|
|
17
9
|
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
18
10
|
const pkgPath = path.resolve(currentDir, '..', '..', 'package.json');
|
|
19
11
|
if (fs.existsSync(pkgPath)) {
|
|
20
12
|
const data = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
return {
|
|
14
|
+
version: data?.version ? String(data.version) : '1.0.8',
|
|
15
|
+
isPrivateEdition: Boolean(data?.private === true || data?.edition === 'private'),
|
|
16
|
+
};
|
|
23
17
|
}
|
|
24
18
|
}
|
|
25
19
|
catch { }
|
|
26
|
-
return '1.0.
|
|
20
|
+
return { version: '1.0.8', isPrivateEdition: false };
|
|
27
21
|
}
|
|
28
|
-
|
|
22
|
+
const PACKAGE_META = readPackageMeta();
|
|
23
|
+
export const CURRENT_VERSION = PACKAGE_META.version;
|
|
24
|
+
export const IS_PRIVATE_EDITION = PACKAGE_META.isPrivateEdition;
|
|
29
25
|
export const PACKAGE_NAME = 'cursor-switch';
|
|
30
|
-
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
31
|
-
/**
|
|
32
|
-
* 比较两个语义化版本号,若 v1 > v2 则返回 true
|
|
33
|
-
*/
|
|
26
|
+
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
34
27
|
export function isVersionGreater(v1, v2) {
|
|
35
28
|
const clean1 = (v1 || '').replace(/^[v^~]/i, '').trim();
|
|
36
29
|
const clean2 = (v2 || '').replace(/^[v^~]/i, '').trim();
|
|
@@ -55,13 +48,17 @@ export class UpdateChecker {
|
|
|
55
48
|
constructor() {
|
|
56
49
|
this.configStore = new ConfigStore();
|
|
57
50
|
}
|
|
58
|
-
/**
|
|
59
|
-
* 检查最新版本 (默认优先使用缓存,绝不阻塞主 UI)
|
|
60
|
-
*/
|
|
61
51
|
async check(forceFresh = false) {
|
|
52
|
+
if (IS_PRIVATE_EDITION) {
|
|
53
|
+
return {
|
|
54
|
+
currentVersion: CURRENT_VERSION,
|
|
55
|
+
latestVersion: CURRENT_VERSION,
|
|
56
|
+
hasUpdate: false,
|
|
57
|
+
fromCache: true,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
62
60
|
const cache = this.configStore.getUpdateCache();
|
|
63
61
|
const now = Date.now();
|
|
64
|
-
// 1. 如果缓存有效且非强制刷新,立即 0ms 内存返回
|
|
65
62
|
if (!forceFresh && cache.lastCheck && now - cache.lastCheck < CHECK_INTERVAL_MS && cache.latestVersion) {
|
|
66
63
|
return {
|
|
67
64
|
currentVersion: CURRENT_VERSION,
|
|
@@ -70,7 +67,6 @@ export class UpdateChecker {
|
|
|
70
67
|
fromCache: true,
|
|
71
68
|
};
|
|
72
69
|
}
|
|
73
|
-
// 2. 缓存已过期但非强制刷新:先立即返回上次缓存结果,并在后台静默触发检测
|
|
74
70
|
if (!forceFresh && cache.latestVersion) {
|
|
75
71
|
this.triggerBackgroundCheck();
|
|
76
72
|
return {
|
|
@@ -80,7 +76,6 @@ export class UpdateChecker {
|
|
|
80
76
|
fromCache: true,
|
|
81
77
|
};
|
|
82
78
|
}
|
|
83
|
-
// 3. 首次冷启动或用户主动点击「检查更新」时,发起网络请求
|
|
84
79
|
const latest = await this.fetchLatestFromRegistries();
|
|
85
80
|
if (latest) {
|
|
86
81
|
this.configStore.setUpdateCache(latest);
|
|
@@ -91,7 +86,6 @@ export class UpdateChecker {
|
|
|
91
86
|
fromCache: false,
|
|
92
87
|
};
|
|
93
88
|
}
|
|
94
|
-
// 兜底退回当前版本
|
|
95
89
|
return {
|
|
96
90
|
currentVersion: CURRENT_VERSION,
|
|
97
91
|
latestVersion: cache.latestVersion || CURRENT_VERSION,
|
|
@@ -99,14 +93,10 @@ export class UpdateChecker {
|
|
|
99
93
|
fromCache: false,
|
|
100
94
|
};
|
|
101
95
|
}
|
|
102
|
-
/**
|
|
103
|
-
* 后台非阻塞异步静默检查
|
|
104
|
-
*/
|
|
105
96
|
triggerBackgroundCheck() {
|
|
106
|
-
if (UpdateChecker.isCheckingBackground)
|
|
97
|
+
if (IS_PRIVATE_EDITION || UpdateChecker.isCheckingBackground)
|
|
107
98
|
return;
|
|
108
99
|
UpdateChecker.isCheckingBackground = true;
|
|
109
|
-
// 异步执行,忽略所有异常,不让网络错误影响主流程
|
|
110
100
|
this.fetchLatestFromRegistries()
|
|
111
101
|
.then((latest) => {
|
|
112
102
|
if (latest) {
|
|
@@ -118,10 +108,9 @@ export class UpdateChecker {
|
|
|
118
108
|
UpdateChecker.isCheckingBackground = false;
|
|
119
109
|
});
|
|
120
110
|
}
|
|
121
|
-
/**
|
|
122
|
-
* 极速无感知检测本地缓存,如果已有新版本则打印单行轻量提示 (0ms 纯本地无网络)
|
|
123
|
-
*/
|
|
124
111
|
static printCachedNoticeIfAvailable() {
|
|
112
|
+
if (IS_PRIVATE_EDITION)
|
|
113
|
+
return;
|
|
125
114
|
try {
|
|
126
115
|
const store = new ConfigStore();
|
|
127
116
|
const cache = store.getUpdateCache();
|
|
@@ -135,9 +124,6 @@ export class UpdateChecker {
|
|
|
135
124
|
}
|
|
136
125
|
catch { }
|
|
137
126
|
}
|
|
138
|
-
/**
|
|
139
|
-
* 快速从 npm 官方源或国内 npmmirror 镜像探测最新版本
|
|
140
|
-
*/
|
|
141
127
|
async fetchLatestFromRegistries() {
|
|
142
128
|
const registries = [
|
|
143
129
|
`https://registry.npmjs.org/${PACKAGE_NAME}/latest`,
|
|
@@ -157,14 +143,10 @@ export class UpdateChecker {
|
|
|
157
143
|
}
|
|
158
144
|
}
|
|
159
145
|
catch {
|
|
160
|
-
// 单个源失败或超时,自动切换备用源
|
|
161
146
|
}
|
|
162
147
|
}
|
|
163
148
|
return null;
|
|
164
149
|
}
|
|
165
|
-
/**
|
|
166
|
-
* 智能嗅探当前环境使用的包管理器 (支持国内 npmmirror 加速参数)
|
|
167
|
-
*/
|
|
168
150
|
detectPackageManager(useMirror = false) {
|
|
169
151
|
const userAgent = process.env.npm_config_user_agent || '';
|
|
170
152
|
const registryArg = useMirror ? ['--registry=https://registry.npmmirror.com'] : [];
|
|
@@ -183,10 +165,14 @@ export class UpdateChecker {
|
|
|
183
165
|
const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
184
166
|
return { name: 'npm', cmd, args: ['install', '-g', `${PACKAGE_NAME}@latest`, ...registryArg] };
|
|
185
167
|
}
|
|
186
|
-
/**
|
|
187
|
-
* 执行一键自升级
|
|
188
|
-
*/
|
|
189
168
|
async performUpgrade(options) {
|
|
169
|
+
if (IS_PRIVATE_EDITION) {
|
|
170
|
+
return {
|
|
171
|
+
success: false,
|
|
172
|
+
packageManager: 'private-workspace',
|
|
173
|
+
error: '当前包处于私有受保护模式,已拦截从公共 npm 覆盖安装;请在本地工作区修改或重新编译。',
|
|
174
|
+
};
|
|
175
|
+
}
|
|
190
176
|
const onProgress = typeof options === 'function' ? options : options?.onProgress;
|
|
191
177
|
const useMirror = typeof options === 'object' ? !!options?.useMirror : false;
|
|
192
178
|
const pm = this.detectPackageManager(useMirror);
|
|
@@ -219,7 +205,6 @@ export class UpdateChecker {
|
|
|
219
205
|
});
|
|
220
206
|
child.on('close', async (code) => {
|
|
221
207
|
if (code === 0) {
|
|
222
|
-
// 重新强制刷新并重置本地缓存
|
|
223
208
|
const latest = await this.fetchLatestFromRegistries();
|
|
224
209
|
if (latest) {
|
|
225
210
|
this.configStore.setUpdateCache(latest);
|