cursor-switch 1.0.6 → 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 -225
- 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 +2 -22
- package/dist/core/dashboard-launcher.js +10 -60
- 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 -27
- package/dist/core/outlook-launcher.js +235 -261
- 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 +64 -28
- package/dist/core/pure-login-service.js +527 -346
- 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 -538
- 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
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* 多层弹性架构:优先 Node 22+ 原生 node:sqlite -> 降级系统 sqlite3 CLI -> 优雅容错
|
|
4
|
-
* 确保在任何 Node.js 版本 (Node 18/20/22/24) 与跨平台 (Windows/macOS/Linux) 均零崩溃
|
|
5
|
-
*/
|
|
1
|
+
import { type SafeStorageDecodeOptions, type SafeStorageDecodeResult } from './safe-storage.js';
|
|
2
|
+
import type { AuthWriteMetadata } from './storage-bridge.js';
|
|
6
3
|
export declare class SqliteBridge {
|
|
7
4
|
private dbPath;
|
|
8
|
-
|
|
5
|
+
private decodeOpts;
|
|
6
|
+
constructor(dbPath: string, decodeOpts?: SafeStorageDecodeOptions);
|
|
9
7
|
exists(): boolean;
|
|
10
8
|
createBackup(): string;
|
|
11
|
-
/**
|
|
12
|
-
* 使用 node:sqlite 获取原生数据库连接 (Node 22.5+)
|
|
13
|
-
*/
|
|
14
9
|
private getNativeDb;
|
|
15
10
|
private hasSqliteCli;
|
|
16
11
|
private execSqliteCli;
|
|
17
12
|
readKey(key: string): Promise<string | null>;
|
|
18
13
|
upsertKey(key: string, value: string): Promise<void>;
|
|
19
|
-
|
|
14
|
+
upsertServiceMachineId(serviceMachineId: string): Promise<void>;
|
|
15
|
+
injectAuth(email: string, token: string, refreshToken?: string, metadata?: AuthWriteMetadata): Promise<void>;
|
|
20
16
|
readEmail(): Promise<string | null>;
|
|
21
|
-
|
|
17
|
+
readTokenInfo(overrideOpts?: SafeStorageDecodeOptions): Promise<SafeStorageDecodeResult>;
|
|
18
|
+
readToken(overrideOpts?: SafeStorageDecodeOptions): Promise<string | null>;
|
|
19
|
+
readRefreshToken(overrideOpts?: SafeStorageDecodeOptions): Promise<string | null>;
|
|
20
|
+
private readKeyInfo;
|
|
22
21
|
clearAuth(): Promise<void>;
|
|
23
22
|
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cursor SQLite 数据库 (state.vscdb) 操作引擎
|
|
3
|
-
* 多层弹性架构:优先 Node 22+ 原生 node:sqlite -> 降级系统 sqlite3 CLI -> 优雅容错
|
|
4
|
-
* 确保在任何 Node.js 版本 (Node 18/20/22/24) 与跨平台 (Windows/macOS/Linux) 均零崩溃
|
|
5
|
-
*/
|
|
6
1
|
import fs from 'node:fs';
|
|
7
|
-
import { execSync } from 'node:child_process';
|
|
2
|
+
import { execFileSync, execSync } from 'node:child_process';
|
|
3
|
+
import { CURSOR_STALE_AUTH_KEYS, decodeStoredToken, } from './safe-storage.js';
|
|
8
4
|
export class SqliteBridge {
|
|
9
5
|
dbPath;
|
|
10
|
-
|
|
6
|
+
decodeOpts;
|
|
7
|
+
constructor(dbPath, decodeOpts = {}) {
|
|
11
8
|
this.dbPath = dbPath;
|
|
9
|
+
this.decodeOpts = decodeOpts;
|
|
12
10
|
}
|
|
13
11
|
exists() {
|
|
14
12
|
return fs.existsSync(this.dbPath);
|
|
@@ -27,12 +25,8 @@ export class SqliteBridge {
|
|
|
27
25
|
return '';
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
|
-
/**
|
|
31
|
-
* 使用 node:sqlite 获取原生数据库连接 (Node 22.5+)
|
|
32
|
-
*/
|
|
33
28
|
async getNativeDb() {
|
|
34
29
|
try {
|
|
35
|
-
// @ts-ignore - Node 22.5+ 原生内置
|
|
36
30
|
const { DatabaseSync } = await import('node:sqlite');
|
|
37
31
|
return new DatabaseSync(this.dbPath);
|
|
38
32
|
}
|
|
@@ -50,10 +44,11 @@ export class SqliteBridge {
|
|
|
50
44
|
}
|
|
51
45
|
}
|
|
52
46
|
execSqliteCli(sql) {
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
return execFileSync('sqlite3', [this.dbPath], {
|
|
48
|
+
input: sql,
|
|
55
49
|
encoding: 'utf8',
|
|
56
|
-
stdio: ['
|
|
50
|
+
stdio: ['pipe', 'pipe', 'ignore'],
|
|
51
|
+
windowsHide: true,
|
|
57
52
|
}).trim();
|
|
58
53
|
}
|
|
59
54
|
async readKey(key) {
|
|
@@ -64,7 +59,7 @@ export class SqliteBridge {
|
|
|
64
59
|
try {
|
|
65
60
|
const stmt = db.prepare('SELECT value FROM ItemTable WHERE key = ?');
|
|
66
61
|
const row = stmt.get(key);
|
|
67
|
-
return row?.value
|
|
62
|
+
return row?.value ?? null;
|
|
68
63
|
}
|
|
69
64
|
catch {
|
|
70
65
|
return null;
|
|
@@ -95,9 +90,9 @@ export class SqliteBridge {
|
|
|
95
90
|
if (db) {
|
|
96
91
|
try {
|
|
97
92
|
db.exec('CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT)');
|
|
98
|
-
const stmt = db.prepare(`
|
|
99
|
-
INSERT INTO ItemTable (key, value) VALUES (?, ?)
|
|
100
|
-
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
93
|
+
const stmt = db.prepare(`
|
|
94
|
+
INSERT INTO ItemTable (key, value) VALUES (?, ?)
|
|
95
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
101
96
|
`);
|
|
102
97
|
stmt.run(key, value);
|
|
103
98
|
}
|
|
@@ -118,29 +113,64 @@ export class SqliteBridge {
|
|
|
118
113
|
this.execSqliteCli(`CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT); INSERT INTO ItemTable (key, value) VALUES ('${safeKey}', '${safeVal}') ON CONFLICT(key) DO UPDATE SET value = excluded.value;`);
|
|
119
114
|
}
|
|
120
115
|
catch { }
|
|
116
|
+
return;
|
|
121
117
|
}
|
|
118
|
+
throw new Error('无法写入 Cursor state.vscdb:当前 Node.js 环境不支持内置 node:sqlite(需 Node >= 22.5.0),且系统未检测到 sqlite3 CLI。');
|
|
119
|
+
}
|
|
120
|
+
async upsertServiceMachineId(serviceMachineId) {
|
|
121
|
+
if (!serviceMachineId)
|
|
122
|
+
return;
|
|
123
|
+
await this.upsertKey('storage.serviceMachineId', serviceMachineId);
|
|
122
124
|
}
|
|
123
|
-
async injectAuth(email, token, refreshToken) {
|
|
125
|
+
async injectAuth(email, token, refreshToken, metadata) {
|
|
124
126
|
if (!this.exists())
|
|
125
127
|
return;
|
|
126
|
-
const
|
|
128
|
+
const rawRefresh = refreshToken || token;
|
|
129
|
+
const rToken = rawRefresh.includes('::') ? token : rawRefresh;
|
|
130
|
+
const entries = [
|
|
131
|
+
['cursorAuth/cachedEmail', email],
|
|
132
|
+
['cursorAuth/accessToken', token],
|
|
133
|
+
['cursorAuth/refreshToken', rToken],
|
|
134
|
+
['cursorAuth/cachedSignUpType', metadata?.signUpType || 'Auth_0'],
|
|
135
|
+
['cursor.email', email],
|
|
136
|
+
['cursor.accessToken', token],
|
|
137
|
+
];
|
|
138
|
+
if (metadata?.authId) {
|
|
139
|
+
entries.push(['cursorAuth/stripeMembershipAuthId', metadata.authId], ['glass.lastSignedInAuthId', metadata.authId], ['adminSettings.cachedAuthId', metadata.authId]);
|
|
140
|
+
}
|
|
141
|
+
if (metadata?.userId) {
|
|
142
|
+
entries.push(['cursorAuth/authId', metadata.userId]);
|
|
143
|
+
}
|
|
144
|
+
if (metadata?.membershipType) {
|
|
145
|
+
entries.push(['cursorAuth/stripeMembershipType', metadata.membershipType]);
|
|
146
|
+
}
|
|
147
|
+
if (metadata?.subscriptionStatus) {
|
|
148
|
+
entries.push(['cursorAuth/stripeSubscriptionStatus', metadata.subscriptionStatus]);
|
|
149
|
+
}
|
|
127
150
|
const db = await this.getNativeDb();
|
|
128
151
|
if (db) {
|
|
129
152
|
try {
|
|
130
153
|
db.exec('CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT)');
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
154
|
+
db.exec('BEGIN IMMEDIATE');
|
|
155
|
+
db.exec("DELETE FROM ItemTable WHERE key LIKE 'cursorAuth/%'");
|
|
156
|
+
const delStmt = db.prepare('DELETE FROM ItemTable WHERE key = ?');
|
|
157
|
+
for (const staleKey of CURSOR_STALE_AUTH_KEYS) {
|
|
158
|
+
delStmt.run(staleKey);
|
|
159
|
+
}
|
|
160
|
+
const stmt = db.prepare(`
|
|
161
|
+
INSERT INTO ItemTable (key, value) VALUES (?, ?)
|
|
162
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
134
163
|
`);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
stmt.run('cursorAuth/cachedSignUpType', 'Auth_0');
|
|
140
|
-
stmt.run('cursor.email', email);
|
|
141
|
-
stmt.run('cursor.accessToken', token);
|
|
164
|
+
for (const [k, v] of entries) {
|
|
165
|
+
stmt.run(k, v);
|
|
166
|
+
}
|
|
167
|
+
db.exec('COMMIT');
|
|
142
168
|
}
|
|
143
169
|
catch {
|
|
170
|
+
try {
|
|
171
|
+
db.exec('ROLLBACK');
|
|
172
|
+
}
|
|
173
|
+
catch { }
|
|
144
174
|
}
|
|
145
175
|
finally {
|
|
146
176
|
try {
|
|
@@ -152,20 +182,22 @@ export class SqliteBridge {
|
|
|
152
182
|
}
|
|
153
183
|
if (this.hasSqliteCli()) {
|
|
154
184
|
try {
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
185
|
+
const sqlEsc = (v) => v.replace(/'/g, "''");
|
|
186
|
+
const staleInList = CURSOR_STALE_AUTH_KEYS.map((k) => `'${sqlEsc(k)}'`).join(', ');
|
|
187
|
+
const inserts = entries
|
|
188
|
+
.map(([k, v]) => `INSERT INTO ItemTable (key, value) VALUES ('${sqlEsc(k)}', '${sqlEsc(v)}') ON CONFLICT(key) DO UPDATE SET value = excluded.value;`)
|
|
189
|
+
.join('\n');
|
|
190
|
+
const sql = `CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT);
|
|
191
|
+
BEGIN IMMEDIATE;
|
|
192
|
+
DELETE FROM ItemTable WHERE key LIKE 'cursorAuth/%' OR key IN (${staleInList});
|
|
193
|
+
${inserts}
|
|
194
|
+
COMMIT;`;
|
|
165
195
|
this.execSqliteCli(sql);
|
|
166
196
|
}
|
|
167
197
|
catch { }
|
|
198
|
+
return;
|
|
168
199
|
}
|
|
200
|
+
throw new Error('无法注入 Cursor 认证凭据到 state.vscdb:当前 Node.js 环境不支持内置 node:sqlite(需 Node >= 22.5.0),且系统未检测到 sqlite3 CLI。');
|
|
169
201
|
}
|
|
170
202
|
async readEmail() {
|
|
171
203
|
const email = await this.readKey('cursorAuth/cachedEmail');
|
|
@@ -173,11 +205,31 @@ INSERT INTO ItemTable (key, value) VALUES ('cursor.accessToken', '${safeToken}')
|
|
|
173
205
|
return email;
|
|
174
206
|
return await this.readKey('cursor.email');
|
|
175
207
|
}
|
|
176
|
-
async
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
208
|
+
async readTokenInfo(overrideOpts = {}) {
|
|
209
|
+
return await this.readKeyInfo(['cursorAuth/accessToken', 'cursor.accessToken'], overrideOpts);
|
|
210
|
+
}
|
|
211
|
+
async readToken(overrideOpts = {}) {
|
|
212
|
+
const info = await this.readTokenInfo(overrideOpts);
|
|
213
|
+
return info.token;
|
|
214
|
+
}
|
|
215
|
+
async readRefreshToken(overrideOpts = {}) {
|
|
216
|
+
const info = await this.readKeyInfo(['cursorAuth/refreshToken'], overrideOpts);
|
|
217
|
+
return info.token && !info.token.includes('::') ? info.token : null;
|
|
218
|
+
}
|
|
219
|
+
async readKeyInfo(keys, overrideOpts = {}) {
|
|
220
|
+
let raw = null;
|
|
221
|
+
for (const key of keys) {
|
|
222
|
+
const val = await this.readKey(key);
|
|
223
|
+
if (val) {
|
|
224
|
+
raw = val;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return decodeStoredToken(raw, {
|
|
229
|
+
dbOrStoragePath: this.dbPath,
|
|
230
|
+
...this.decodeOpts,
|
|
231
|
+
...overrideOpts,
|
|
232
|
+
});
|
|
181
233
|
}
|
|
182
234
|
async clearAuth() {
|
|
183
235
|
if (!this.exists())
|
|
@@ -185,9 +237,19 @@ INSERT INTO ItemTable (key, value) VALUES ('cursor.accessToken', '${safeToken}')
|
|
|
185
237
|
const db = await this.getNativeDb();
|
|
186
238
|
if (db) {
|
|
187
239
|
try {
|
|
188
|
-
db.exec(
|
|
240
|
+
db.exec('BEGIN IMMEDIATE');
|
|
241
|
+
db.exec("DELETE FROM ItemTable WHERE key LIKE 'cursorAuth/%'");
|
|
242
|
+
const delStmt = db.prepare('DELETE FROM ItemTable WHERE key = ?');
|
|
243
|
+
for (const staleKey of CURSOR_STALE_AUTH_KEYS) {
|
|
244
|
+
delStmt.run(staleKey);
|
|
245
|
+
}
|
|
246
|
+
db.exec('COMMIT');
|
|
189
247
|
}
|
|
190
248
|
catch {
|
|
249
|
+
try {
|
|
250
|
+
db.exec('ROLLBACK');
|
|
251
|
+
}
|
|
252
|
+
catch { }
|
|
191
253
|
}
|
|
192
254
|
finally {
|
|
193
255
|
try {
|
|
@@ -199,9 +261,12 @@ INSERT INTO ItemTable (key, value) VALUES ('cursor.accessToken', '${safeToken}')
|
|
|
199
261
|
}
|
|
200
262
|
if (this.hasSqliteCli()) {
|
|
201
263
|
try {
|
|
202
|
-
|
|
264
|
+
const staleInList = CURSOR_STALE_AUTH_KEYS.map((k) => `'${k.replace(/'/g, "''")}'`).join(', ');
|
|
265
|
+
this.execSqliteCli(`BEGIN IMMEDIATE; DELETE FROM ItemTable WHERE key LIKE 'cursorAuth/%' OR key IN (${staleInList}); COMMIT;`);
|
|
203
266
|
}
|
|
204
267
|
catch { }
|
|
268
|
+
return;
|
|
205
269
|
}
|
|
270
|
+
throw new Error('无法清理 Cursor state.vscdb 认证状态:当前 Node.js 环境不支持内置 node:sqlite(需 Node >= 22.5.0),且系统未检测到 sqlite3 CLI。');
|
|
206
271
|
}
|
|
207
272
|
}
|
|
@@ -1,18 +1,27 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* storage.json 文件原子读写与安全备份模块
|
|
3
|
-
*/
|
|
4
1
|
import type { MachineIds } from '../types.js';
|
|
2
|
+
import { type SafeStorageDecodeOptions, type SafeStorageDecodeResult } from './safe-storage.js';
|
|
3
|
+
export interface AuthWriteMetadata {
|
|
4
|
+
authId?: string;
|
|
5
|
+
userId?: string;
|
|
6
|
+
signUpType?: string;
|
|
7
|
+
membershipType?: string;
|
|
8
|
+
subscriptionStatus?: string;
|
|
9
|
+
}
|
|
5
10
|
export declare class StorageBridge {
|
|
6
11
|
private filePath;
|
|
7
12
|
constructor(filePath: string);
|
|
8
13
|
exists(): boolean;
|
|
9
14
|
read(): Record<string, any>;
|
|
15
|
+
private readForWrite;
|
|
10
16
|
writeAtomic(data: Record<string, any>): void;
|
|
11
17
|
createBackup(): string;
|
|
12
18
|
readMachineIds(): MachineIds;
|
|
13
19
|
writeMachineIds(ids: MachineIds): void;
|
|
14
|
-
writeAuth(email: string, token: string, refreshToken?: string): void;
|
|
20
|
+
writeAuth(email: string, token: string, refreshToken?: string, metadata?: AuthWriteMetadata): void;
|
|
15
21
|
clearAuth(): void;
|
|
16
22
|
readCurrentEmail(): string | null;
|
|
17
|
-
|
|
23
|
+
readTokenInfo(decodeOpts?: SafeStorageDecodeOptions): SafeStorageDecodeResult;
|
|
24
|
+
readCurrentToken(decodeOpts?: SafeStorageDecodeOptions): string | null;
|
|
25
|
+
readRefreshToken(decodeOpts?: SafeStorageDecodeOptions): string | null;
|
|
26
|
+
private readKeyInfo;
|
|
18
27
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* storage.json 文件原子读写与安全备份模块
|
|
3
|
-
*/
|
|
4
1
|
import fs from 'node:fs';
|
|
5
2
|
import path from 'node:path';
|
|
3
|
+
import { CURSOR_STALE_AUTH_KEYS, decodeStoredToken, } from './safe-storage.js';
|
|
6
4
|
export class StorageBridge {
|
|
7
5
|
filePath;
|
|
8
6
|
constructor(filePath) {
|
|
@@ -23,15 +21,44 @@ export class StorageBridge {
|
|
|
23
21
|
return {};
|
|
24
22
|
}
|
|
25
23
|
}
|
|
24
|
+
readForWrite() {
|
|
25
|
+
if (!this.exists()) {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
const content = fs.readFileSync(this.filePath, 'utf8');
|
|
29
|
+
if (!content.trim()) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const parsed = JSON.parse(content);
|
|
34
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
35
|
+
throw new Error('storage.json 根节点不是有效的 JSON 对象');
|
|
36
|
+
}
|
|
37
|
+
return parsed;
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
throw new Error(`读取 ${this.filePath} 失败,已中止写入以防覆盖现有配置: ${err?.message || err}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
26
43
|
writeAtomic(data) {
|
|
27
44
|
const dir = path.dirname(this.filePath);
|
|
28
45
|
if (!fs.existsSync(dir)) {
|
|
29
46
|
fs.mkdirSync(dir, { recursive: true });
|
|
30
47
|
}
|
|
31
|
-
const tmpPath = `${this.filePath}.tmp_${Date.now()}`;
|
|
48
|
+
const tmpPath = `${this.filePath}.tmp_${process.pid}_${Date.now()}`;
|
|
32
49
|
const jsonStr = JSON.stringify(data, null, 2);
|
|
33
|
-
|
|
34
|
-
|
|
50
|
+
try {
|
|
51
|
+
fs.writeFileSync(tmpPath, jsonStr, 'utf8');
|
|
52
|
+
fs.renameSync(tmpPath, this.filePath);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
try {
|
|
56
|
+
if (fs.existsSync(tmpPath))
|
|
57
|
+
fs.unlinkSync(tmpPath);
|
|
58
|
+
}
|
|
59
|
+
catch { }
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
35
62
|
}
|
|
36
63
|
createBackup() {
|
|
37
64
|
if (!this.exists())
|
|
@@ -55,7 +82,7 @@ export class StorageBridge {
|
|
|
55
82
|
};
|
|
56
83
|
}
|
|
57
84
|
writeMachineIds(ids) {
|
|
58
|
-
const data = this.
|
|
85
|
+
const data = this.readForWrite();
|
|
59
86
|
data['telemetry.devDeviceId'] = ids['telemetry.devDeviceId'];
|
|
60
87
|
data['telemetry.macMachineId'] = ids['telemetry.macMachineId'];
|
|
61
88
|
data['telemetry.machineId'] = ids['telemetry.machineId'];
|
|
@@ -63,28 +90,41 @@ export class StorageBridge {
|
|
|
63
90
|
data['storage.serviceMachineId'] = ids['storage.serviceMachineId'];
|
|
64
91
|
this.writeAtomic(data);
|
|
65
92
|
}
|
|
66
|
-
writeAuth(email, token, refreshToken) {
|
|
67
|
-
const data = this.
|
|
93
|
+
writeAuth(email, token, refreshToken, metadata) {
|
|
94
|
+
const data = this.readForWrite();
|
|
95
|
+
const staleSet = new Set(CURSOR_STALE_AUTH_KEYS);
|
|
96
|
+
for (const k of Object.keys(data)) {
|
|
97
|
+
if (k.startsWith('cursorAuth/') || staleSet.has(k)) {
|
|
98
|
+
delete data[k];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const safeRefresh = refreshToken && !refreshToken.includes('::') ? refreshToken : token;
|
|
68
102
|
data['cursorAuth/cachedEmail'] = email;
|
|
69
103
|
data['cursorAuth/accessToken'] = token;
|
|
70
|
-
data['cursorAuth/refreshToken'] =
|
|
71
|
-
data['cursorAuth/cachedSignUpType'] = 'Auth_0';
|
|
104
|
+
data['cursorAuth/refreshToken'] = safeRefresh || token;
|
|
105
|
+
data['cursorAuth/cachedSignUpType'] = metadata?.signUpType || 'Auth_0';
|
|
72
106
|
data['cursor.email'] = email;
|
|
73
107
|
data['cursor.accessToken'] = token;
|
|
108
|
+
if (metadata?.authId) {
|
|
109
|
+
data['cursorAuth/stripeMembershipAuthId'] = metadata.authId;
|
|
110
|
+
data['glass.lastSignedInAuthId'] = metadata.authId;
|
|
111
|
+
data['adminSettings.cachedAuthId'] = metadata.authId;
|
|
112
|
+
}
|
|
113
|
+
if (metadata?.userId) {
|
|
114
|
+
data['cursorAuth/authId'] = metadata.userId;
|
|
115
|
+
}
|
|
116
|
+
if (metadata?.membershipType) {
|
|
117
|
+
data['cursorAuth/stripeMembershipType'] = metadata.membershipType;
|
|
118
|
+
}
|
|
119
|
+
if (metadata?.subscriptionStatus) {
|
|
120
|
+
data['cursorAuth/stripeSubscriptionStatus'] = metadata.subscriptionStatus;
|
|
121
|
+
}
|
|
74
122
|
this.writeAtomic(data);
|
|
75
123
|
}
|
|
76
124
|
clearAuth() {
|
|
77
|
-
const data = this.
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
'cursorAuth/accessToken',
|
|
81
|
-
'cursorAuth/refreshToken',
|
|
82
|
-
'cursorAuth/cachedSignUpType',
|
|
83
|
-
'cursor.email',
|
|
84
|
-
'cursor.accessToken',
|
|
85
|
-
];
|
|
86
|
-
for (const f of authFields) {
|
|
87
|
-
delete data[f];
|
|
125
|
+
const data = this.readForWrite();
|
|
126
|
+
for (const staleKey of CURSOR_STALE_AUTH_KEYS) {
|
|
127
|
+
delete data[staleKey];
|
|
88
128
|
}
|
|
89
129
|
this.writeAtomic(data);
|
|
90
130
|
}
|
|
@@ -92,8 +132,28 @@ export class StorageBridge {
|
|
|
92
132
|
const data = this.read();
|
|
93
133
|
return data['cursorAuth/cachedEmail'] || data['cursor.email'] || null;
|
|
94
134
|
}
|
|
95
|
-
|
|
135
|
+
readTokenInfo(decodeOpts = {}) {
|
|
136
|
+
return this.readKeyInfo(['cursorAuth/accessToken', 'cursor.accessToken'], decodeOpts);
|
|
137
|
+
}
|
|
138
|
+
readCurrentToken(decodeOpts = {}) {
|
|
139
|
+
return this.readTokenInfo(decodeOpts).token;
|
|
140
|
+
}
|
|
141
|
+
readRefreshToken(decodeOpts = {}) {
|
|
142
|
+
const rt = this.readKeyInfo(['cursorAuth/refreshToken'], decodeOpts).token;
|
|
143
|
+
return rt && !rt.includes('::') ? rt : null;
|
|
144
|
+
}
|
|
145
|
+
readKeyInfo(keys, decodeOpts = {}) {
|
|
96
146
|
const data = this.read();
|
|
97
|
-
|
|
147
|
+
let raw = null;
|
|
148
|
+
for (const key of keys) {
|
|
149
|
+
if (typeof data[key] === 'string' && data[key]) {
|
|
150
|
+
raw = data[key];
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return decodeStoredToken(raw, {
|
|
155
|
+
dbOrStoragePath: this.filePath,
|
|
156
|
+
...decodeOpts,
|
|
157
|
+
});
|
|
98
158
|
}
|
|
99
159
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { AccountInfo, MachineIds, ResolvedCursorPaths } from '../types.js';
|
|
2
|
+
import { AccountStore } from './account-store.js';
|
|
3
|
+
import { SqliteBridge } from './sqlite-bridge.js';
|
|
4
|
+
import { StorageBridge } from './storage-bridge.js';
|
|
5
|
+
import { type FetchLike, type SwitchPreflightIssue } from './token-exchange.js';
|
|
6
|
+
export interface SidecarSyncPayload {
|
|
7
|
+
machineIds?: MachineIds;
|
|
8
|
+
email?: string;
|
|
9
|
+
accessToken?: string;
|
|
10
|
+
refreshToken?: string;
|
|
11
|
+
authId?: string;
|
|
12
|
+
userId?: string;
|
|
13
|
+
clearAuth?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface SidecarSyncReport {
|
|
16
|
+
machineIdFileUpdated: boolean;
|
|
17
|
+
authJsonUpdated: boolean;
|
|
18
|
+
cliConfigUpdated: boolean;
|
|
19
|
+
warnings: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface LocalCursorAuthSnapshot {
|
|
22
|
+
email: string | null;
|
|
23
|
+
token: string | null;
|
|
24
|
+
refreshToken?: string | null;
|
|
25
|
+
source: 'sqlite' | 'storage' | null;
|
|
26
|
+
isEncrypted: boolean;
|
|
27
|
+
decrypted: boolean;
|
|
28
|
+
encryptionError?: string;
|
|
29
|
+
}
|
|
30
|
+
export type StaleTokenResolution = {
|
|
31
|
+
action: 'abort';
|
|
32
|
+
} | {
|
|
33
|
+
action: 'force';
|
|
34
|
+
} | {
|
|
35
|
+
action: 'replace_account';
|
|
36
|
+
account: AccountInfo;
|
|
37
|
+
};
|
|
38
|
+
export interface SwitchExecutionOptions {
|
|
39
|
+
store: AccountStore;
|
|
40
|
+
storage: StorageBridge;
|
|
41
|
+
sqlite: SqliteBridge;
|
|
42
|
+
paths?: ResolvedCursorPaths;
|
|
43
|
+
targetAccount: AccountInfo;
|
|
44
|
+
autoUpgradeWebToken?: boolean;
|
|
45
|
+
autoRefreshExpiring?: boolean;
|
|
46
|
+
resetSystemRegistry?: boolean;
|
|
47
|
+
skipProcessClose?: boolean;
|
|
48
|
+
fetchImpl?: FetchLike;
|
|
49
|
+
confirmOnStaleToken?: (issue: SwitchPreflightIssue, account: AccountInfo) => Promise<boolean>;
|
|
50
|
+
resolveStaleTokenAction?: (issue: SwitchPreflightIssue, account: AccountInfo) => Promise<StaleTokenResolution>;
|
|
51
|
+
_healDepth?: number;
|
|
52
|
+
onStep?: (step: 'resolve' | 'close' | 'backup' | 'machine' | 'auth', detail?: string) => void;
|
|
53
|
+
}
|
|
54
|
+
export interface SwitchExecutionResult {
|
|
55
|
+
success: boolean;
|
|
56
|
+
aborted?: boolean;
|
|
57
|
+
email: string;
|
|
58
|
+
account: AccountInfo;
|
|
59
|
+
machineIds: MachineIds;
|
|
60
|
+
tokenType: 'web' | 'client';
|
|
61
|
+
upgradedFromWeb: boolean;
|
|
62
|
+
refreshed: boolean;
|
|
63
|
+
shouldLogout: boolean;
|
|
64
|
+
preflightIssue?: SwitchPreflightIssue;
|
|
65
|
+
sidecarReport: SidecarSyncReport;
|
|
66
|
+
systemRegistryResult?: {
|
|
67
|
+
ok: boolean;
|
|
68
|
+
skipped?: boolean;
|
|
69
|
+
error?: string;
|
|
70
|
+
};
|
|
71
|
+
warnings: string[];
|
|
72
|
+
}
|
|
73
|
+
export declare function syncSidecarFiles(paths: ResolvedCursorPaths | undefined, payload: SidecarSyncPayload): SidecarSyncReport;
|
|
74
|
+
export declare function resetWindowsSystemMachineGuid(newGuid: string, backupDir?: string): {
|
|
75
|
+
ok: boolean;
|
|
76
|
+
skipped?: boolean;
|
|
77
|
+
backupPath?: string;
|
|
78
|
+
error?: string;
|
|
79
|
+
};
|
|
80
|
+
export declare function applyMachineIdsToCursor(params: {
|
|
81
|
+
storage: StorageBridge;
|
|
82
|
+
sqlite: SqliteBridge;
|
|
83
|
+
paths?: ResolvedCursorPaths;
|
|
84
|
+
machineIds: MachineIds;
|
|
85
|
+
resetSystemRegistry?: boolean;
|
|
86
|
+
}): Promise<{
|
|
87
|
+
sidecarReport: SidecarSyncReport;
|
|
88
|
+
systemRegistryResult?: {
|
|
89
|
+
ok: boolean;
|
|
90
|
+
skipped?: boolean;
|
|
91
|
+
backupPath?: string;
|
|
92
|
+
error?: string;
|
|
93
|
+
};
|
|
94
|
+
}>;
|
|
95
|
+
export declare function readLocalCursorAuth(storage: StorageBridge, sqlite: SqliteBridge): Promise<LocalCursorAuthSnapshot>;
|
|
96
|
+
export declare function executeAccountSwitch(opts: SwitchExecutionOptions): Promise<SwitchExecutionResult>;
|