-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync-esm.js
More file actions
233 lines (211 loc) · 9.17 KB
/
Copy pathsync-esm.js
File metadata and controls
233 lines (211 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* ESM wrapper for sync.js (used by background service worker)
*/
import { StarStorage } from './storage-esm.js';
const SYNC_CONFIG_KEY = 'linuxdo_sync_config';
const GIST_FILENAME = 'linuxdo-stars.json';
const GIST_DESCRIPTION = 'LinuxDo Star Collector - Sync Data (do not delete)';
export const SyncManager = {
async getConfig() {
return new Promise(r => {
chrome.storage.local.get([SYNC_CONFIG_KEY], res => {
r(res[SYNC_CONFIG_KEY] || { token: '', gistId: '', lastSyncAt: '', autoSync: true, status: 'disconnected' });
});
});
},
async saveConfig(cfg) {
return new Promise(r => chrome.storage.local.set({ [SYNC_CONFIG_KEY]: cfg }, r));
},
async sync() {
const cfg = await this.getConfig();
if (!cfg.token || !cfg.gistId) return { ok: false, message: '未配置同步' };
try {
await this.updateStatus('syncing');
const remote = await this.readGist(cfg.token, cfg.gistId);
const local = await StarStorage.getAll();
const merged = this.merge(local, remote);
await StarStorage.save(merged);
await this.writeGist(cfg.token, cfg.gistId, merged);
cfg.lastSyncAt = new Date().toISOString();
await this.saveConfig(cfg);
await this.updateStatus('synced');
return { ok: true, message: '同步成功', merged };
} catch (err) {
console.error('[LinuxDo Star Sync]', err);
await this.updateStatus('error', err.message);
return { ok: false, message: err.message };
}
},
async push() { return this.sync(); },
async pull() { return this.sync(); },
async updateStatus(status, error) {
const cfg = await this.getConfig();
cfg.status = status;
if (error) cfg.lastError = error; else delete cfg.lastError;
await this.saveConfig(cfg);
try { chrome.runtime.sendMessage({ type: 'SYNC_STATUS', status, error }); } catch {}
},
async apiRequest(token, url, options = {}) {
const resp = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${token}`,
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
...(options.headers || {}),
},
});
if (!resp.ok) {
const body = await resp.text().catch(() => '');
if (resp.status === 401) throw new Error('Token 无效或已过期');
if (resp.status === 404) throw new Error('Gist 不存在');
throw new Error(`GitHub API 错误 (${resp.status}): ${body.substring(0, 100)}`);
}
return resp.json();
},
async findOrCreateGist(token) {
const gists = await this.apiRequest(token, 'https://api.github.com/gists?per_page=100');
for (const gist of gists) {
if (gist.files[GIST_FILENAME]) return gist.id;
}
const newGist = await this.apiRequest(token, 'https://api.github.com/gists', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
description: GIST_DESCRIPTION, public: false,
files: { [GIST_FILENAME]: { content: JSON.stringify({ collections: {}, bookmarks: {} }, null, 2) } },
}),
});
return newGist.id;
},
async readGist(token, gistId) {
const gist = await this.apiRequest(token, `https://api.github.com/gists/${gistId}`);
const file = gist.files[GIST_FILENAME];
if (!file) throw new Error('Gist 中未找到数据文件');
let content = file.content;
if (file.truncated || typeof content !== 'string') {
if (!file.raw_url) throw new Error('Gist 数据文件过大且无法读取');
const raw = await fetch(file.raw_url, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github.raw',
},
});
if (!raw.ok) throw new Error(`读取 Gist 原始文件失败 (${raw.status})`);
content = await raw.text();
}
try {
const data = JSON.parse(content);
if (!data.collections) data.collections = {};
if (!data.bookmarks) data.bookmarks = {};
if (!data.assets) data.assets = {};
return data;
} catch {
throw new Error('Gist 数据格式损坏,已停止同步以避免覆盖远端存档');
}
},
async writeGist(token, gistId, data) {
await this.apiRequest(token, `https://api.github.com/gists/${gistId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ files: { [GIST_FILENAME]: { content: JSON.stringify(data, null, 2) } } }),
});
},
async validateToken(token) {
try {
const user = await this.apiRequest(token, 'https://api.github.com/user');
return { ok: true, username: user.login, avatar: user.avatar_url };
} catch (err) { return { ok: false, message: err.message }; }
},
async connect(token) {
const v = await this.validateToken(token);
if (!v.ok) return { ok: false, message: v.message };
const gistId = await this.findOrCreateGist(token);
await this.saveConfig({ token, gistId, lastSyncAt: '', autoSync: true, status: 'connected', username: v.username });
const result = await this.sync();
return { ok: true, message: `已连接为 @${v.username}`, gistId, ...result };
},
async disconnect() {
await this.saveConfig({ token: '', gistId: '', lastSyncAt: '', autoSync: false, status: 'disconnected' });
return { ok: true, message: '已断开同步' };
},
// ==================== Merge (with soft-delete) ====================
merge(local, remote) {
const result = { collections: {}, bookmarks: {}, assets: {} };
// Assets are content-addressed, so the union is naturally deduplicated.
const allAssetIds = new Set([
...Object.keys(local.assets || {}),
...Object.keys(remote.assets || {}),
]);
for (const id of allAssetIds) {
const l = local.assets?.[id];
const r = remote.assets?.[id];
if (!l) result.assets[id] = r;
else if (!r) result.assets[id] = l;
else {
result.assets[id] = {
...r,
...l,
dataUrl: l.dataUrl || r.dataUrl,
originalUrl: l.originalUrl || r.originalUrl,
originalUrls: [...new Set([
...(l.originalUrls || (l.originalUrl ? [l.originalUrl] : [])),
...(r.originalUrls || (r.originalUrl ? [r.originalUrl] : [])),
])],
};
}
}
// Collections
const allColIds = new Set([...Object.keys(local.collections || {}), ...Object.keys(remote.collections || {})]);
for (const id of allColIds) {
const l = local.collections?.[id], r = remote.collections?.[id];
if (!l) result.collections[id] = r;
else if (!r) result.collections[id] = l;
else result.collections[id] = this._newer(l, r);
}
if (!result.collections.default) {
result.collections.default = { id: 'default', name: '默认收藏夹', icon: '⭐', color: '#eab308', createdAt: new Date().toISOString(), order: 0 };
}
// Bookmarks (with tombstone support)
const allKeys = new Set([...Object.keys(local.bookmarks || {}), ...Object.keys(remote.bookmarks || {})]);
for (const key of allKeys) {
const l = local.bookmarks?.[key], r = remote.bookmarks?.[key];
if (!l && !r) continue;
if (!l) { result.bookmarks[key] = r; continue; }
if (!r) { result.bookmarks[key] = l; continue; }
const lDel = l._deleted, rDel = r._deleted;
if (lDel && rDel) { result.bookmarks[key] = this._newer(l, r); continue; }
if (lDel) { result.bookmarks[key] = new Date(l._deletedAt || 0) >= new Date(r.updatedAt || r.starredAt || 0) ? l : r; continue; }
if (rDel) { result.bookmarks[key] = new Date(r._deletedAt || 0) >= new Date(l.updatedAt || l.starredAt || 0) ? r : l; continue; }
// Both alive — merge
const merged = { ...this._newer(l, r), posts: {} };
const allPK = new Set([...Object.keys(l.posts || {}), ...Object.keys(r.posts || {})]);
for (const pk of allPK) {
const lp = l.posts?.[pk], rp = r.posts?.[pk];
if (!lp && !rp) continue;
if (!lp) { merged.posts[pk] = rp; continue; }
if (!rp) { merged.posts[pk] = lp; continue; }
const lpD = lp._deleted, rpD = rp._deleted;
if (lpD && rpD) { merged.posts[pk] = this._newer(lp, rp); }
else if (lpD) { merged.posts[pk] = new Date(lp._deletedAt || 0) >= new Date(rp.updatedAt || rp.starredAt || 0) ? lp : rp; }
else if (rpD) { merged.posts[pk] = new Date(rp._deletedAt || 0) >= new Date(lp.updatedAt || lp.starredAt || 0) ? rp : lp; }
else { merged.posts[pk] = this._newer(lp, rp); }
}
result.bookmarks[key] = merged;
}
// Purge old tombstones > 7 days
const cutoff = Date.now() - 7 * 86400000;
for (const [k, bk] of Object.entries(result.bookmarks)) {
if (bk._deleted && new Date(bk._deletedAt || 0).getTime() < cutoff) { delete result.bookmarks[k]; continue; }
if (bk.posts) for (const [pk, p] of Object.entries(bk.posts)) {
if (p._deleted && new Date(p._deletedAt || 0).getTime() < cutoff) delete bk.posts[pk];
}
}
return result;
},
_newer(a, b) {
const ta = new Date(a.updatedAt || a._deletedAt || a.starredAt || a.createdAt || 0).getTime();
const tb = new Date(b.updatedAt || b._deletedAt || b.starredAt || b.createdAt || 0).getTime();
return ta >= tb ? { ...a } : { ...b };
},
};