-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
141 lines (130 loc) · 3.89 KB
/
index.ts
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
import { Octokit } from '@octokit/core';
interface IConfig {
authToken: string;
owner: string;
repo: string;
dbs: string[];
}
interface IDBData {
sha: string;
data: any;
dirty: boolean;
path: string;
}
export class DBhub {
public config: IConfig = null;
public inited: boolean = false;
public dbMap: Map<string, IDBData> = new Map();
public octokit: Octokit = null;
constructor(config: IConfig) {
this.config = config;
this.octokit = new Octokit({
auth: this.config.authToken
})
}
public connect(callBack: () => void) {
let count = this.config.dbs.length;
let finishCall = () => {
count--;
if (count <= 0) {
console.log('DBhub');
callBack();
}
}
for (let db of this.config.dbs) {
this.getDB(db, (sha, content) => {
if (sha == null) {
this.createDB(db, (sha, content) => {
this.dbMap.set(db, { sha: sha, data: JSON.parse(content), dirty: false, path: db });
finishCall();
})
} else {
this.dbMap.set(db, { sha: sha, data: JSON.parse(content), dirty: false, path: db });
finishCall();
}
})
}
}
public getDB(path, cb) {
this.octokit.request("GET /repos/{owner}/{repo}/contents/{path}", {
owner: this.config.owner,
repo: this.config.repo,
path: path + '.raw'
}).then((res: any) => {
let content = res.data.content;
let data = Buffer.from(content, 'base64').toString('utf-8');
cb(res.data.sha, data)
}).catch(err => {
cb(null, null);
})
}
public createDB(path, cb): void {
this.octokit.request("PUT /repos/{owner}/{repo}/contents/{path}", {
owner: this.config.owner,
repo: this.config.repo,
path: path + '.raw',
message: 'vercel',
content: Buffer.from(JSON.stringify({}), 'utf-8').toString('base64')
}).then(res => {
cb(res.data.content.sha, "{}")
}).catch(err => {
cb(null, null)
})
}
public sync(cb) {
let array = [];
this.dbMap.forEach((value, key) => {
if (value.dirty) {
array.push(value)
}
})
let call = () => {
let db = array.shift();
if (db == null) {
console.log('同步完成')
cb?.();
return;
}
db.dirty = false;
let content = Buffer.from(JSON.stringify(db.data), 'utf-8').toString('base64');
console.log('同步数据库:' + db.path)
this.syncDB(db.path, db.sha, content, (result) => {
if (db.dirty == true) {
call();
} else {
db.dirty = !result;
call();
}
})
}
call();
}
public syncDB(path, sha, content, cb) {
this.octokit.request("PUT /repos/{owner}/{repo}/contents/{path}", {
owner: this.config.owner,
repo: this.config.repo,
path: path + '.raw',
message: 'DBhub',
content: content,
sha: sha
}).then(res => {
cb(true)
}).catch(err => {
cb(false)
})
}
public setData(path, key, value) {
let db = this.dbMap.get(path);
let data = db.data;
if (data[key] && data[key] == value) {
return;
}
data[key] = value;
db.dirty = true;
}
public getData(path, key) {
let db = this.dbMap.get(path);
let data = db.data;
return data[key];
}
}