sync by worker

This commit is contained in:
Gabe Yuan
2023-09-18 15:45:32 +08:00
parent fbaeff6b7b
commit 92ffda5220
2 changed files with 62 additions and 31 deletions

View File

@@ -293,8 +293,14 @@ export const DEFAULT_SETTING = {
export const DEFAULT_RULES = [GLOBLA_RULE];
export const OPT_SYNCTYPE_WORKER = "KISS-Worker";
export const OPT_SYNCTYPE_WEBDAV = "WebDAV";
export const OPT_SYNCTYPE_ALL = [OPT_SYNCTYPE_WORKER, OPT_SYNCTYPE_WEBDAV];
export const DEFAULT_SYNC = {
syncType: OPT_SYNCTYPE_WORKER, // 同步方式
syncUrl: "", // 数据同步接口
syncUser: "", // 数据同步用户名
syncKey: "", // 数据同步密钥
settingUpdateAt: 0,
settingSyncAt: 0,

View File

@@ -3,6 +3,7 @@ import {
KV_RULES_KEY,
KV_RULES_SHARE_KEY,
KV_SALT_SHARE,
OPT_SYNCTYPE_WEBDAV,
} from "../config";
import {
getSyncWithDefault,
@@ -15,36 +16,59 @@ import {
import { apiSyncData } from "../apis";
import { sha256 } from "./utils";
const syncByWorker = async ({
key,
value,
syncUrl,
syncKey,
updateAt = 0,
syncAt = 0,
isBg = false,
isForce = false,
}) => {
if (isForce) {
updateAt = Date.now();
}
return await apiSyncData(
`${syncUrl}/sync`,
syncKey,
{
key,
value,
updateAt: syncAt === 0 ? 0 : updateAt,
},
isBg
);
};
/**
* 同步设置
* @returns
*/
const syncSetting = async (isBg = false, isForce = false) => {
let {
const {
syncType,
syncUrl,
syncUser,
syncKey,
settingUpdateAt = 0,
settingSyncAt = 0,
} = await getSyncWithDefault();
if (!syncUrl || !syncKey) {
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
return;
}
if (isForce) {
settingUpdateAt = Date.now();
}
const setting = await getSettingWithDefault();
const res = await apiSyncData(
`${syncUrl}/sync`,
syncKey,
{
const res = await syncByWorker({
key: KV_SETTING_KEY,
value: setting,
updateAt: settingSyncAt === 0 ? 0 : settingUpdateAt,
},
isBg
);
syncUrl,
syncKey,
updateAt: settingUpdateAt,
syncAt: settingSyncAt,
isBg,
isForce,
});
if (res.updateAt > settingUpdateAt) {
await setSetting(res.value);
@@ -70,31 +94,29 @@ export const trySyncSetting = async (isBg = false, isForce = false) => {
* @returns
*/
const syncRules = async (isBg = false, isForce = false) => {
let {
const {
syncType,
syncUrl,
syncUser,
syncKey,
rulesUpdateAt = 0,
rulesSyncAt = 0,
} = await getSyncWithDefault();
if (!syncUrl || !syncKey) {
if (!syncUrl || !syncKey || (syncType === OPT_SYNCTYPE_WEBDAV && !syncUser)) {
return;
}
if (isForce) {
rulesUpdateAt = Date.now();
}
const rules = await getRulesWithDefault();
const res = await apiSyncData(
`${syncUrl}/sync`,
syncKey,
{
const res = await syncByWorker({
key: KV_RULES_KEY,
value: rules,
updateAt: rulesSyncAt === 0 ? 0 : rulesUpdateAt,
},
isBg
);
syncUrl,
syncKey,
updateAt: rulesUpdateAt,
syncAt: rulesSyncAt,
isBg,
isForce,
});
if (res.updateAt > rulesUpdateAt) {
await setRules(res.value);
@@ -121,10 +143,13 @@ export const trySyncRules = async (isBg = false, isForce = false) => {
* @returns
*/
export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
await apiSyncData(`${syncUrl}/sync`, syncKey, {
await syncByWorker({
key: KV_RULES_SHARE_KEY,
value: rules,
syncUrl,
syncKey,
updateAt: Date.now(),
syncAt: Date.now(),
});
const psk = await sha256(syncKey, KV_SALT_SHARE);
const shareUrl = `${syncUrl}/rules?psk=${psk}`;