-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathprocessor.ts
More file actions
274 lines (245 loc) · 9.2 KB
/
processor.ts
File metadata and controls
274 lines (245 loc) · 9.2 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Copyright (c) 2022 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import BackupDatabase from "@db/backup-database"
import optionHolder from "@service/components/option-holder"
import itemService from "@service/item-service"
import metaService from "@service/meta-service"
import { formatTimeYMD, getBirthday } from "@util/time"
import GistCoordinator from "./gist/coordinator"
import ObsidianCoordinator from "./obsidian/coordinator"
import WebDAVCoordinator from "./web-dav/coordinator"
const storage = chrome.storage.local
const syncDb = new BackupDatabase(storage)
export type AuthCheckResult = {
option: timer.option.BackupOption
auth: timer.backup.Auth
ext: timer.backup.TypeExt
type: timer.backup.Type
coordinator: timer.backup.Coordinator<unknown>
errorMsg?: string
}
class CoordinatorContextWrapper<Cache> implements timer.backup.CoordinatorContext<Cache> {
auth: timer.backup.Auth
ext?: timer.backup.TypeExt
cache: Cache = {} as unknown as Cache
type: timer.backup.Type
cid: string
constructor(cid: string, auth: timer.backup.Auth, ext: timer.backup.TypeExt, type: timer.backup.Type) {
this.cid = cid
this.auth = auth
this.ext = ext
this.type = type
}
async init(): Promise<timer.backup.CoordinatorContext<Cache>> {
this.cache = await syncDb.getCache(this.type) as Cache
return this
}
handleCacheChanged(): Promise<void> {
return syncDb.updateCache(this.type, this.cache)
}
}
/**
* Declare type of NavigatorUAData
*/
type NavigatorUAData = {
brands?: {
brand?: string
version?: string
}[]
platform?: string
}
type Result<T> = {
success: boolean
errorMsg?: string
data?: T
}
function error<T>(msg: string): Result<T> {
return { success: false, errorMsg: msg, }
}
function success<T>(data?: T): Result<T> {
return { success: true, data }
}
function generateCid() {
const uaData = (navigator as any)?.userAgentData as NavigatorUAData
let prefix = 'unknown'
if (uaData) {
const brand = uaData.brands
?.map(e => e.brand)
?.filter(brand => brand && brand !== "Chromium" && !brand.includes("Not"))
?.[0]
?.replace(' ', '-')
const platform = uaData.platform
brand && platform && (prefix = `${platform.toLowerCase()}-${brand.toLowerCase()}`)
}
return prefix + '-' + new Date().getTime()
}
/**
* Get client id or generate it lazily
*/
async function lazyGetCid(): Promise<string> {
let cid = await metaService.getCid()
if (!cid) {
cid = generateCid()
await metaService.updateCid(cid)
}
return cid
}
async function syncFull(
context: timer.backup.CoordinatorContext<unknown>,
coordinator: timer.backup.Coordinator<unknown>,
client: timer.backup.Client
): Promise<timer.backup.Snapshot> {
// 1. select rows
let start = getBirthday()
let end = new Date()
const rows = await itemService.selectItems({ date: [start, end] })
const allDates = rows.map(r => r.date).sort((a, b) => a == b ? 0 : a > b ? 1 : -1)
client.maxDate = allDates[allDates.length - 1]
client.minDate = allDates[0]
// 2. upload
await coordinator.upload(context, rows)
return {
ts: end.getTime(),
date: formatTimeYMD(end),
}
}
function filterClient(c: timer.backup.Client, excludeLocal: boolean, localClientId: string, start?: string, end?: string) {
// Exclude local client
if (excludeLocal && c.id === localClientId) return false
// Judge range
if (start && c.maxDate && c.maxDate < start) return false
if (end && c.minDate && c.minDate > end) return false
return true
}
function prepareAuth(option: timer.option.BackupOption): timer.backup.Auth {
const type = option?.backupType || 'none'
const token = option?.backupAuths?.[type]
const login = option.backupLogin?.[type]
return { token, login }
}
export type RemoteQueryParam = {
start: Date
end: Date
specCid?: string
excludeLocal?: boolean
}
class Processor {
coordinators: {
[type in timer.backup.Type]: timer.backup.Coordinator<unknown>
}
constructor() {
this.coordinators = {
none: null as unknown as timer.backup.Coordinator<never>,
gist: new GistCoordinator(),
obsidian_local_rest_api: new ObsidianCoordinator(),
web_dav: new WebDAVCoordinator(),
}
}
async syncData(): Promise<Result<number>> {
const { option, auth, ext, type, coordinator, errorMsg } = await this.checkAuth()
if (errorMsg) return error(errorMsg)
const cid = await lazyGetCid()
const context: timer.backup.CoordinatorContext<unknown> = await new CoordinatorContextWrapper<unknown>(cid, auth, ext, type).init()
const client: timer.backup.Client = {
id: cid,
name: option.clientName,
minDate: undefined,
maxDate: undefined
}
try {
let snapshot: timer.backup.Snapshot = await syncFull(context, coordinator, client)
await syncDb.updateSnapshot(type, snapshot)
const clients: timer.backup.Client[] = (await coordinator.listAllClients(context)).filter(a => a.id !== cid) || []
clients.push(client)
await coordinator.updateClients(context, clients)
// Update time
const now = Date.now()
metaService.updateBackUpTime(type, now)
return success(now)
} catch (e) {
console.error("Error to sync data", e)
const msg = (e as Error)?.message ?? e?.toString?.()
return error(msg)
}
}
async listClients(): Promise<Result<timer.backup.Client[]>> {
const { auth, ext, type, coordinator, errorMsg } = await this.checkAuth()
if (errorMsg) return error(errorMsg)
const cid = await lazyGetCid()
const context: timer.backup.CoordinatorContext<unknown> = await new CoordinatorContextWrapper<unknown>(cid, auth, ext, type).init()
const clients = await coordinator.listAllClients(context)
return success(clients)
}
async checkAuth(): Promise<AuthCheckResult> {
const option = await optionHolder.get()
const { backupType: type, backupExts } = option
const ext = backupExts?.[type] ?? {}
const auth = prepareAuth(option)
const coordinator: timer.backup.Coordinator<unknown> = type && this.coordinators[type]
if (!coordinator) {
// no coordinator, do nothing
return { option, auth, ext, type, coordinator, errorMsg: "Invalid type" }
}
let errorMsg
try {
errorMsg = await coordinator.testAuth(auth, ext)
} catch (e) {
errorMsg = (e as Error)?.message || 'Unknown error'
}
return { option, auth, ext, type, coordinator, errorMsg }
}
async query(param: RemoteQueryParam): Promise<timer.backup.Row[]> {
const { type, coordinator, auth, ext, errorMsg } = await this.checkAuth()
if (errorMsg || !coordinator) {
return []
}
const { start = getBirthday(), end, specCid, excludeLocal } = param
let localCid = await lazyGetCid()
// 1. init context
const context: timer.backup.CoordinatorContext<unknown> = await new CoordinatorContextWrapper<unknown>(localCid, auth, ext, type).init()
// 2. query all clients, and filter them
let startStr = start ? formatTimeYMD(start) : undefined
let endStr = end ? formatTimeYMD(end) : undefined
const allClients = (await coordinator.listAllClients(context))
.filter(c => filterClient(c, !!excludeLocal, localCid, startStr, endStr))
.filter(c => !specCid || c.id === specCid)
// 3. iterate clients
const result: timer.backup.Row[] = []
await Promise.all(
allClients.map(async client => {
const { id, name } = client
const rows = await coordinator.download(context, start, end, id)
rows.forEach(row => result.push({
...row,
cid: id,
cname: name,
}))
})
)
console.log(`Queried ${result.length} remote items`)
return result
}
async clear(cid: string): Promise<Result<void>> {
const { auth, ext, type, coordinator, errorMsg } = await this.checkAuth()
if (errorMsg) return error(errorMsg)
let localCid = await lazyGetCid()
const context: timer.backup.CoordinatorContext<unknown> = await new CoordinatorContextWrapper<unknown>(localCid, auth, ext, type).init()
// 1. Find the client
const allClients = await coordinator.listAllClients(context)
const client = allClients?.filter(c => c?.id === cid)?.[0]
if (!client) {
return success()
}
// 2. clear
await coordinator.clear(context, client)
// 3. remove client
const newClients = allClients.filter(c => c?.id !== cid)
await coordinator.updateClients(context, newClients)
return success()
}
}
export default new Processor()