@celljs/ta-sync-cloud 0.0.0-stage → 4.13.0
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.en.md +31 -0
- package/README.md +30 -2
- package/host/index.mjs +810 -0
- package/package.json +61 -4
package/README.en.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Cell - Ta Sync Cloud
|
|
2
|
+
|
|
3
|
+
A pi package that comes with Ta (on by default, Settings → Extensions turns it off): sync of your computers through storage of your own, **with no relay**.
|
|
4
|
+
|
|
5
|
+
- **WebDAV**: Jianguoyun, Nextcloud, ownCloud, a NAS of Synology or QNAP, `rclone serve webdav`. The address of a folder, a user and an (app) password.
|
|
6
|
+
- **A bucket of S3**: AWS, Cloudflare R2, MinIO, Tencent COS, Alibaba OSS, Backblaze B2. The address of the service (none for AWS), the bucket, a folder in it and a region if you like, and the keys.
|
|
7
|
+
- **A git repository**: a **private** repository of yours on GitHub, GitLab, Gitee, Gitea or a server of your own. Its address, a branch (`main` unless said) and a folder in it if you like; for an HTTPS address a token that may push (a fine-grained token of GitHub with Contents read and write on that repository is enough), for SSH (`git@github.com:you/ta-sync.git`) the keys of this computer. Needs git installed. Ta keeps a clone of the last commit alone under `sync/git/` of the agent directory, and what it wrote goes up in one commit about 1.5 s after; when another computer pushed first, Ta takes their commit and puts the files it wrote on it again (each computer writes only its own directory: nothing to merge). The token goes to git as a header of its runs, never in `.git/config` of the clone. The history of the repository grows with sync: delete the branch on the web now and then to start over.
|
|
8
|
+
|
|
9
|
+
In Settings → Relays, Sync in WebDAV, Sync in S3 or Sync in Git and fill it in: Ta tries it first, writing and removing a small file, and only then keeps it in `relays.json` and syncs there. What sync writes there is sealed on this computer, names too: the storage reads none of it.
|
|
10
|
+
|
|
11
|
+
Or write `relays.json` by hand:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"relays": {
|
|
16
|
+
"nas": { "type": "webdav", "url": "https://dav.example.com/remote.php/dav/files/me/Ta", "user": "me", "password": "app password" },
|
|
17
|
+
"r2": { "type": "s3", "endpoint": "https://<account>.r2.cloudflarestorage.com", "bucket": "ta", "accessKeyId": "…", "secretAccessKey": "…" },
|
|
18
|
+
"repo": { "type": "git", "url": "https://github.com/me/ta-sync.git", "folder": "ta", "token": "github_pat_…" }
|
|
19
|
+
},
|
|
20
|
+
"use": { "sync": "nas" }
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Passwords, keys and tokens stay in `relays.json` of this computer (readable by you alone), never sent to a page; the link of Add a device carries them, so show it only to your own computers. See [docs/design/ta-relays.md](../../docs/design/ta-relays.md) 5 and 11.
|
|
25
|
+
|
|
26
|
+
## Development
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun run build # tsc + host/index.mjs
|
|
30
|
+
bun test ./lib
|
|
31
|
+
```
|
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - Ta Sync Cloud
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ta 自带的 pi 包(默认开启,可在 设置 → 扩展 关闭):让设备同步经你自己的存储进行,**不经过中继**。
|
|
4
|
+
|
|
5
|
+
- **WebDAV**:坚果云、Nextcloud、ownCloud、群晖或威联通的 NAS、`rclone serve webdav`。填一个文件夹的地址、用户名和(应用)密码。
|
|
6
|
+
- **S3 存储桶**:AWS、Cloudflare R2、MinIO、腾讯云 COS、阿里云 OSS、Backblaze B2。填服务地址(AWS 留空)、存储桶、可选的桶内文件夹和地域,以及访问密钥。
|
|
7
|
+
- **git 仓库**:GitHub、GitLab、Gitee、Gitea 或自建服务器上你的一个**私有**仓库。填仓库地址、可选的分支(默认 `main`)和仓库内文件夹;HTTPS 地址再填能推送的访问令牌(GitHub 的 fine-grained token 给这个仓库 Contents 读写即可),SSH 地址(`git@github.com:you/ta-sync.git`)用本机已有的密钥。需要本机装有 git。Ta 在 agent 目录的 `sync/git/` 里保留一个只取最新一次提交的克隆,写入后约 1.5 秒合成一次提交推送;另一台电脑先推送时,Ta 取下它的提交再把自己写的文件放上去重推(每台电脑只写自己的目录,不需要合并)。令牌只在运行 git 时作为请求头传入,不写进克隆的 `.git/config`。仓库历史会随同步慢慢变大,可以偶尔在网页上删掉分支重来。
|
|
8
|
+
|
|
9
|
+
在 设置 → 中继服务 里点"用 WebDAV 同步""用 S3 同步"或"用 Git 同步"填写即可:Ta 先写入再删掉一个小文件试一下,能用才保存到 `relays.json` 并选给设备同步。同步写进去的内容和文件名在本机就已加密,存储服务看不到。
|
|
10
|
+
|
|
11
|
+
也可以直接写 `relays.json`:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"relays": {
|
|
16
|
+
"nas": { "type": "webdav", "url": "https://dav.jianguoyun.com/dav/Ta", "user": "me@example.com", "password": "应用密码" },
|
|
17
|
+
"r2": { "type": "s3", "endpoint": "https://<account>.r2.cloudflarestorage.com", "bucket": "ta", "accessKeyId": "…", "secretAccessKey": "…" },
|
|
18
|
+
"repo": { "type": "git", "url": "https://github.com/me/ta-sync.git", "folder": "ta", "token": "github_pat_…" }
|
|
19
|
+
},
|
|
20
|
+
"use": { "sync": "nas" }
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
密码、密钥和令牌只存在这台电脑的 `relays.json`(只有你能读),不会发给页面;"添加设备"给出的链接带着它们,只发给自己的电脑。设计记录见 [docs/design/ta-relays.md](../../docs/design/ta-relays.md) 第 5、11 节。
|
|
25
|
+
|
|
26
|
+
## 开发
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun run build # tsc + host/index.mjs
|
|
30
|
+
bun test ./lib
|
|
31
|
+
```
|
package/host/index.mjs
ADDED
|
@@ -0,0 +1,810 @@
|
|
|
1
|
+
// src/host.ts
|
|
2
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
3
|
+
import { join as join2 } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/git-store.ts
|
|
6
|
+
import { execFile } from "node:child_process";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
// src/store-util.ts
|
|
12
|
+
var StoreUtil;
|
|
13
|
+
((StoreUtil) => {
|
|
14
|
+
function elements(xml, name) {
|
|
15
|
+
const found = [];
|
|
16
|
+
const pattern = new RegExp(`<(?:[\\w.-]+:)?${name}(?:\\s[^>]*)?>([\\s\\S]*?)</(?:[\\w.-]+:)?${name}>`, "g");
|
|
17
|
+
for (let match = pattern.exec(xml);match; match = pattern.exec(xml)) {
|
|
18
|
+
found.push(match[1]);
|
|
19
|
+
}
|
|
20
|
+
return found;
|
|
21
|
+
}
|
|
22
|
+
StoreUtil.elements = elements;
|
|
23
|
+
function element(xml, name) {
|
|
24
|
+
const [first] = elements(xml, name);
|
|
25
|
+
return first === undefined ? undefined : decode(first.trim());
|
|
26
|
+
}
|
|
27
|
+
StoreUtil.element = element;
|
|
28
|
+
function has(xml, name) {
|
|
29
|
+
return new RegExp(`<(?:[\\w.-]+:)?${name}(?:\\s[^>]*)?/?>`).test(xml);
|
|
30
|
+
}
|
|
31
|
+
StoreUtil.has = has;
|
|
32
|
+
function decode(text2) {
|
|
33
|
+
return text2.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&#(\d+);/g, (_match, code) => String.fromCodePoint(Number(code))).replace(/&#x([0-9a-f]+);/gi, (_match, code) => String.fromCodePoint(Number.parseInt(code, 16))).replace(/&/g, "&");
|
|
34
|
+
}
|
|
35
|
+
StoreUtil.decode = decode;
|
|
36
|
+
function encode(text2) {
|
|
37
|
+
return encodeURIComponent(text2).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
38
|
+
}
|
|
39
|
+
StoreUtil.encode = encode;
|
|
40
|
+
function encodePath(path) {
|
|
41
|
+
return path.split("/").map((part) => encode(part)).join("/");
|
|
42
|
+
}
|
|
43
|
+
StoreUtil.encodePath = encodePath;
|
|
44
|
+
async function failure(response, what) {
|
|
45
|
+
const text2 = await response.text().catch(() => "");
|
|
46
|
+
const said = element(text2, "Message") ?? element(text2, "message") ?? "";
|
|
47
|
+
const status = response.status === 401 || response.status === 403 ? "the user, the password or the keys are not right" : response.status === 404 ? "it is not there" : `HTTP ${response.status}`;
|
|
48
|
+
return new Error(`${what}: ${status}${said ? ` (${said.slice(0, 160)})` : ""}.`);
|
|
49
|
+
}
|
|
50
|
+
StoreUtil.failure = failure;
|
|
51
|
+
function text(value) {
|
|
52
|
+
return typeof value === "string" ? value.trim() : value === undefined || value === null ? "" : String(value).trim();
|
|
53
|
+
}
|
|
54
|
+
StoreUtil.text = text;
|
|
55
|
+
})(StoreUtil ||= {});
|
|
56
|
+
|
|
57
|
+
// src/git-store.ts
|
|
58
|
+
var GitUtil;
|
|
59
|
+
((GitUtil) => {
|
|
60
|
+
function url(value) {
|
|
61
|
+
const text = StoreUtil.text(value);
|
|
62
|
+
if (text.startsWith("-")) {
|
|
63
|
+
throw new Error(`${text} is not the address of a git repository.`);
|
|
64
|
+
}
|
|
65
|
+
if (/^[\w.-]+@[\w.-]+:\S+$/.test(text) || /^(ssh|git|file):\/\/\S+$/i.test(text)) {
|
|
66
|
+
return text;
|
|
67
|
+
}
|
|
68
|
+
let parsed;
|
|
69
|
+
try {
|
|
70
|
+
parsed = new URL(text);
|
|
71
|
+
} catch {
|
|
72
|
+
throw new Error(`${text || "Nothing"} is not the address of a git repository.`);
|
|
73
|
+
}
|
|
74
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
75
|
+
throw new Error(`${text} is not the address of a git repository.`);
|
|
76
|
+
}
|
|
77
|
+
parsed.username = "";
|
|
78
|
+
parsed.password = "";
|
|
79
|
+
parsed.hash = "";
|
|
80
|
+
return parsed.toString().replace(/\/+$/, "");
|
|
81
|
+
}
|
|
82
|
+
GitUtil.url = url;
|
|
83
|
+
function signIn(options) {
|
|
84
|
+
let user = StoreUtil.text(options.user);
|
|
85
|
+
let token = StoreUtil.text(options.token);
|
|
86
|
+
try {
|
|
87
|
+
const parsed = new URL(StoreUtil.text(options.url));
|
|
88
|
+
if (parsed.protocol === "https:" || parsed.protocol === "http:") {
|
|
89
|
+
user ||= decodeURIComponent(parsed.username);
|
|
90
|
+
token ||= decodeURIComponent(parsed.password);
|
|
91
|
+
}
|
|
92
|
+
} catch {}
|
|
93
|
+
return { user, token };
|
|
94
|
+
}
|
|
95
|
+
GitUtil.signIn = signIn;
|
|
96
|
+
function branch(options) {
|
|
97
|
+
const branch2 = StoreUtil.text(options.branch) || "main";
|
|
98
|
+
if (!/^[\w./-]+$/.test(branch2) || branch2.includes("..") || branch2.startsWith("-")) {
|
|
99
|
+
throw new Error(`${branch2} is not the name of a branch.`);
|
|
100
|
+
}
|
|
101
|
+
return branch2;
|
|
102
|
+
}
|
|
103
|
+
GitUtil.branch = branch;
|
|
104
|
+
function folder(options) {
|
|
105
|
+
return StoreUtil.text(options.folder).split("/").filter((part) => part && part !== "." && part !== "..").join("/");
|
|
106
|
+
}
|
|
107
|
+
GitUtil.folder = folder;
|
|
108
|
+
function where(options) {
|
|
109
|
+
const text = url(options.url);
|
|
110
|
+
const ssh = /^[\w.-]+@([\w.-]+):(.+)$/.exec(text);
|
|
111
|
+
const shown = ssh ? `${ssh[1]}/${ssh[2]}` : text.replace(/^[a-z]+:\/\//i, "");
|
|
112
|
+
const at = folder(options);
|
|
113
|
+
return `${shown.replace(/\.git$/, "")}${at ? `/${at}` : ""} · ${branch(options)}`;
|
|
114
|
+
}
|
|
115
|
+
GitUtil.where = where;
|
|
116
|
+
})(GitUtil ||= {});
|
|
117
|
+
|
|
118
|
+
class GitFailure extends Error {
|
|
119
|
+
args;
|
|
120
|
+
said;
|
|
121
|
+
constructor(args, said) {
|
|
122
|
+
super(said);
|
|
123
|
+
this.args = args;
|
|
124
|
+
this.said = said;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
class GitStore {
|
|
129
|
+
dir;
|
|
130
|
+
static queues = new Map;
|
|
131
|
+
static written = new Map;
|
|
132
|
+
flushDelayMs = 1500;
|
|
133
|
+
fetchEveryMs = 15000;
|
|
134
|
+
url;
|
|
135
|
+
branch;
|
|
136
|
+
folder;
|
|
137
|
+
env;
|
|
138
|
+
ready;
|
|
139
|
+
remote = false;
|
|
140
|
+
fetchedAt = 0;
|
|
141
|
+
timer;
|
|
142
|
+
failure;
|
|
143
|
+
constructor(options, dir) {
|
|
144
|
+
this.dir = dir;
|
|
145
|
+
const given = options;
|
|
146
|
+
this.url = GitUtil.url(options.url);
|
|
147
|
+
this.branch = GitUtil.branch(given);
|
|
148
|
+
this.folder = GitUtil.folder(given);
|
|
149
|
+
const { user, token } = GitUtil.signIn(given);
|
|
150
|
+
this.env = {
|
|
151
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
152
|
+
GIT_SSH_COMMAND: process.env.GIT_SSH_COMMAND ?? "ssh -o BatchMode=yes",
|
|
153
|
+
GIT_AUTHOR_NAME: "Ta",
|
|
154
|
+
GIT_AUTHOR_EMAIL: "ta@localhost",
|
|
155
|
+
GIT_COMMITTER_NAME: "Ta",
|
|
156
|
+
GIT_COMMITTER_EMAIL: "ta@localhost",
|
|
157
|
+
LC_ALL: "C"
|
|
158
|
+
};
|
|
159
|
+
if (token && /^https?:/i.test(this.url)) {
|
|
160
|
+
this.env.GIT_CONFIG_COUNT = "1";
|
|
161
|
+
this.env.GIT_CONFIG_KEY_0 = "http.extraHeader";
|
|
162
|
+
this.env.GIT_CONFIG_VALUE_0 = `Authorization: Basic ${Buffer.from(`${user || "x-access-token"}:${token}`).toString("base64")}`;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
get dirty() {
|
|
166
|
+
let dirty = GitStore.written.get(this.dir);
|
|
167
|
+
if (!dirty) {
|
|
168
|
+
dirty = new Set;
|
|
169
|
+
GitStore.written.set(this.dir, dirty);
|
|
170
|
+
}
|
|
171
|
+
return dirty;
|
|
172
|
+
}
|
|
173
|
+
async list(dir) {
|
|
174
|
+
await this.open();
|
|
175
|
+
if (this.failure) {
|
|
176
|
+
const failure = this.failure;
|
|
177
|
+
this.failure = undefined;
|
|
178
|
+
throw failure;
|
|
179
|
+
}
|
|
180
|
+
if (this.dirty.size > 0 || Date.now() - this.fetchedAt >= this.fetchEveryMs) {
|
|
181
|
+
await this.sync();
|
|
182
|
+
}
|
|
183
|
+
return this.locked(async () => {
|
|
184
|
+
const found = [];
|
|
185
|
+
const at = this.path(dir);
|
|
186
|
+
for (const entry of await readdir(at, { withFileTypes: true }).catch(() => [])) {
|
|
187
|
+
if (entry.name.startsWith(".")) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (entry.isDirectory()) {
|
|
191
|
+
found.push({ name: entry.name, tag: "", dir: true });
|
|
192
|
+
} else if (entry.isFile()) {
|
|
193
|
+
const info = await stat(join(at, entry.name)).catch(() => {
|
|
194
|
+
return;
|
|
195
|
+
});
|
|
196
|
+
if (info) {
|
|
197
|
+
found.push({ name: entry.name, tag: `${info.mtimeMs}:${info.size}` });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return found.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async read(path) {
|
|
205
|
+
await this.open();
|
|
206
|
+
return this.locked(async () => {
|
|
207
|
+
try {
|
|
208
|
+
return new Uint8Array(await readFile(this.path(path)));
|
|
209
|
+
} catch {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
async write(path, data) {
|
|
215
|
+
await this.open();
|
|
216
|
+
await this.locked(async () => {
|
|
217
|
+
const file = this.path(path);
|
|
218
|
+
await mkdir(join(file, ".."), { recursive: true });
|
|
219
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
220
|
+
await writeFile(temporary, data);
|
|
221
|
+
await rename(temporary, file);
|
|
222
|
+
this.dirty.add(this.inRepository(path));
|
|
223
|
+
});
|
|
224
|
+
this.later();
|
|
225
|
+
}
|
|
226
|
+
async remove(path) {
|
|
227
|
+
await this.open();
|
|
228
|
+
await this.locked(async () => {
|
|
229
|
+
await rm(this.path(path), { force: true });
|
|
230
|
+
this.dirty.add(this.inRepository(path));
|
|
231
|
+
});
|
|
232
|
+
this.later();
|
|
233
|
+
}
|
|
234
|
+
async sync() {
|
|
235
|
+
clearTimeout(this.timer);
|
|
236
|
+
this.timer = undefined;
|
|
237
|
+
await this.open();
|
|
238
|
+
await this.locked(async () => {
|
|
239
|
+
for (let attempt = 0;; attempt++) {
|
|
240
|
+
await this.fetch();
|
|
241
|
+
const written = new Map;
|
|
242
|
+
for (const path of this.dirty) {
|
|
243
|
+
written.set(path, await readFile(join(this.dir, path)).catch(() => {
|
|
244
|
+
return;
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
if (this.remote) {
|
|
248
|
+
await this.git(["reset", "-q", "--hard", `origin/${this.branch}`]);
|
|
249
|
+
await this.git(["clean", "-q", "-f", "-d"]);
|
|
250
|
+
}
|
|
251
|
+
for (const [path, data] of written) {
|
|
252
|
+
const file = join(this.dir, path);
|
|
253
|
+
if (data) {
|
|
254
|
+
await mkdir(join(file, ".."), { recursive: true });
|
|
255
|
+
await writeFile(file, data);
|
|
256
|
+
} else {
|
|
257
|
+
await rm(file, { force: true });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (written.size === 0) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
await this.git(["add", "-A", "--", "."]);
|
|
264
|
+
const changed = await this.git(["diff", "--cached", "--quiet"]).then(() => false, () => true);
|
|
265
|
+
if (changed) {
|
|
266
|
+
await this.git(["commit", "-q", "--no-verify", "--no-gpg-sign", "-m", `Ta sync: ${written.size} ${written.size === 1 ? "file" : "files"}`]);
|
|
267
|
+
try {
|
|
268
|
+
await this.push();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (attempt < 4 && error instanceof GitFailure && GitStore.refused(error)) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
for (const path of written.keys()) {
|
|
277
|
+
this.dirty.delete(path);
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
open() {
|
|
284
|
+
this.ready ??= this.locked(async () => {
|
|
285
|
+
await mkdir(this.dir, { recursive: true });
|
|
286
|
+
if (!existsSync(join(this.dir, ".git"))) {
|
|
287
|
+
await this.git(["init", "-q"]);
|
|
288
|
+
await this.git(["remote", "add", "origin", this.url]);
|
|
289
|
+
} else {
|
|
290
|
+
await this.git(["remote", "set-url", "origin", this.url]);
|
|
291
|
+
await rm(join(this.dir, ".git", "index.lock"), { force: true });
|
|
292
|
+
}
|
|
293
|
+
await this.git(["config", "core.autocrlf", "false"]);
|
|
294
|
+
await this.git(["config", "core.quotepath", "false"]);
|
|
295
|
+
await this.fetch();
|
|
296
|
+
const head = await this.git(["rev-parse", "--verify", "-q", "HEAD"]).then(() => true, () => false);
|
|
297
|
+
if (head) {
|
|
298
|
+
for (const line of (await this.git(["status", "--porcelain", "-z", "--untracked-files=all"])).split("\x00")) {
|
|
299
|
+
const path = line.slice(3);
|
|
300
|
+
if (path) {
|
|
301
|
+
this.dirty.add(path);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
} else if (this.remote) {
|
|
305
|
+
await this.git(["reset", "-q", "--hard", `origin/${this.branch}`]);
|
|
306
|
+
}
|
|
307
|
+
if (!this.remote) {
|
|
308
|
+
if (!head) {
|
|
309
|
+
await this.git(["commit", "-q", "--allow-empty", "--no-verify", "--no-gpg-sign", "-m", "Ta sync"]);
|
|
310
|
+
}
|
|
311
|
+
await this.push();
|
|
312
|
+
this.remote = true;
|
|
313
|
+
} else {
|
|
314
|
+
await this.git(["push", "-q", "--dry-run", "--no-verify", "origin", `HEAD:refs/heads/${this.branch}`]).catch((error) => {
|
|
315
|
+
if (error instanceof GitFailure && GitStore.refused(error)) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
throw error;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}).catch((error) => {
|
|
322
|
+
this.ready = undefined;
|
|
323
|
+
throw this.explain(error);
|
|
324
|
+
});
|
|
325
|
+
return this.ready;
|
|
326
|
+
}
|
|
327
|
+
async fetch() {
|
|
328
|
+
try {
|
|
329
|
+
await this.git(["fetch", "-q", "--depth", "1", "--no-tags", "origin", `+refs/heads/${this.branch}:refs/remotes/origin/${this.branch}`]);
|
|
330
|
+
this.remote = true;
|
|
331
|
+
} catch (error) {
|
|
332
|
+
if (error instanceof GitFailure && /couldn't find remote ref|not our ref|no such ref/i.test(error.said)) {
|
|
333
|
+
this.remote = false;
|
|
334
|
+
} else {
|
|
335
|
+
throw error;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
this.fetchedAt = Date.now();
|
|
339
|
+
}
|
|
340
|
+
async push() {
|
|
341
|
+
await this.git(["push", "-q", "--no-verify", "origin", `HEAD:refs/heads/${this.branch}`]);
|
|
342
|
+
}
|
|
343
|
+
static refused(error) {
|
|
344
|
+
return /rejected|fetch first|non-fast-forward|stale info|cannot lock ref/i.test(error.said);
|
|
345
|
+
}
|
|
346
|
+
later() {
|
|
347
|
+
clearTimeout(this.timer);
|
|
348
|
+
this.timer = setTimeout(() => {
|
|
349
|
+
this.timer = undefined;
|
|
350
|
+
this.sync().catch((error) => {
|
|
351
|
+
this.failure = this.explain(error);
|
|
352
|
+
});
|
|
353
|
+
}, this.flushDelayMs);
|
|
354
|
+
this.timer.unref?.();
|
|
355
|
+
}
|
|
356
|
+
locked(work) {
|
|
357
|
+
const before = GitStore.queues.get(this.dir) ?? Promise.resolve();
|
|
358
|
+
const run = before.then(work, work);
|
|
359
|
+
const tail = run.catch(() => {
|
|
360
|
+
return;
|
|
361
|
+
});
|
|
362
|
+
GitStore.queues.set(this.dir, tail);
|
|
363
|
+
tail.then(() => {
|
|
364
|
+
if (GitStore.queues.get(this.dir) === tail) {
|
|
365
|
+
GitStore.queues.delete(this.dir);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
return run;
|
|
369
|
+
}
|
|
370
|
+
git(args) {
|
|
371
|
+
return new Promise((resolve, reject) => {
|
|
372
|
+
execFile("git", ["-c", "core.hooksPath=/dev/null", ...args], { cwd: this.dir, env: { ...process.env, ...this.env }, timeout: 120000, maxBuffer: 16 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
373
|
+
if (!error) {
|
|
374
|
+
resolve(String(stdout));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (error.code === "ENOENT") {
|
|
378
|
+
reject(new GitFailure(args, "git is not installed on this computer."));
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
reject(new GitFailure(args, String(stderr || error.message).trim()));
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
explain(error) {
|
|
386
|
+
if (!(error instanceof GitFailure)) {
|
|
387
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
388
|
+
}
|
|
389
|
+
const said = error.said.split(`
|
|
390
|
+
`).filter((line) => line.trim() && !/^hint:/.test(line)).slice(0, 3).join(" ").replace(/Authorization: Basic \S+/gi, "Authorization: …");
|
|
391
|
+
if (/Authentication failed|could not read Username|terminal prompts disabled|\b40[13]\b|Permission denied|access denied|not authorized/i.test(said)) {
|
|
392
|
+
return new Error(`git cannot sign in to ${this.url}: the user, the token or the keys are not right, or may not push (${said.slice(0, 200)}).`);
|
|
393
|
+
}
|
|
394
|
+
return new Error(`git ${error.args[0]} of ${this.url} failed: ${said.slice(0, 300)}`);
|
|
395
|
+
}
|
|
396
|
+
inRepository(path) {
|
|
397
|
+
return [this.folder, ...path.split("/").filter(Boolean)].filter(Boolean).join("/");
|
|
398
|
+
}
|
|
399
|
+
path(path) {
|
|
400
|
+
return join(this.dir, ...this.inRepository(path).split("/").filter(Boolean));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/s3-store.ts
|
|
405
|
+
import { createHash, createHmac } from "node:crypto";
|
|
406
|
+
var EMPTY = createHash("sha256").update("").digest("hex");
|
|
407
|
+
var VIRTUAL = /(^|\.)(amazonaws\.com|aliyuncs\.com|myqcloud\.com)$/i;
|
|
408
|
+
var sha256 = (data) => createHash("sha256").update(data).digest("hex");
|
|
409
|
+
var hmac = (key, text) => createHmac("sha256", key).update(text).digest();
|
|
410
|
+
var S3Signer;
|
|
411
|
+
((S3Signer) => {
|
|
412
|
+
function amzDate(date) {
|
|
413
|
+
return date.toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");
|
|
414
|
+
}
|
|
415
|
+
S3Signer.amzDate = amzDate;
|
|
416
|
+
function sign(request, credentials, region, date = new Date) {
|
|
417
|
+
const url = new URL(request.url);
|
|
418
|
+
const stamp = amzDate(date);
|
|
419
|
+
const day = stamp.slice(0, 8);
|
|
420
|
+
const payload = request.payloadHash ?? EMPTY;
|
|
421
|
+
const headers = {};
|
|
422
|
+
for (const [name, value] of Object.entries(request.headers ?? {})) {
|
|
423
|
+
headers[name.toLowerCase()] = String(value).trim().replace(/\s+/g, " ");
|
|
424
|
+
}
|
|
425
|
+
headers.host = url.host;
|
|
426
|
+
headers["x-amz-content-sha256"] = payload;
|
|
427
|
+
headers["x-amz-date"] = stamp;
|
|
428
|
+
const names = Object.keys(headers).sort();
|
|
429
|
+
const query = [...url.searchParams].map(([key2, value]) => [StoreUtil.encode(key2), StoreUtil.encode(value)]).sort(([a, x], [b, y]) => a < b ? -1 : a > b ? 1 : x < y ? -1 : x > y ? 1 : 0).map(([key2, value]) => `${key2}=${value}`).join("&");
|
|
430
|
+
const canonical = [
|
|
431
|
+
request.method,
|
|
432
|
+
(url.pathname || "/").split("/").map((part) => StoreUtil.encode(decodeURIComponent(part))).join("/"),
|
|
433
|
+
query,
|
|
434
|
+
names.map((name) => `${name}:${headers[name]}
|
|
435
|
+
`).join(""),
|
|
436
|
+
names.join(";"),
|
|
437
|
+
payload
|
|
438
|
+
].join(`
|
|
439
|
+
`);
|
|
440
|
+
const scope = `${day}/${region}/s3/aws4_request`;
|
|
441
|
+
const toSign = ["AWS4-HMAC-SHA256", stamp, scope, sha256(canonical)].join(`
|
|
442
|
+
`);
|
|
443
|
+
const key = hmac(hmac(hmac(hmac(`AWS4${credentials.secretAccessKey}`, day), region), "s3"), "aws4_request");
|
|
444
|
+
const signature = createHmac("sha256", key).update(toSign).digest("hex");
|
|
445
|
+
return {
|
|
446
|
+
...headers,
|
|
447
|
+
authorization: `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope}, SignedHeaders=${names.join(";")}, Signature=${signature}`
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
S3Signer.sign = sign;
|
|
451
|
+
})(S3Signer ||= {});
|
|
452
|
+
var S3Util;
|
|
453
|
+
((S3Util) => {
|
|
454
|
+
function bucketUrl(options) {
|
|
455
|
+
const bucket = StoreUtil.text(options.bucket);
|
|
456
|
+
if (!/^[a-z0-9][a-z0-9.-]{1,62}$/i.test(bucket)) {
|
|
457
|
+
throw new Error(`${bucket || "Nothing"} is not the name of a bucket.`);
|
|
458
|
+
}
|
|
459
|
+
const endpoint = StoreUtil.text(options.endpoint);
|
|
460
|
+
if (!endpoint) {
|
|
461
|
+
return `https://${bucket}.s3.${region(options)}.amazonaws.com/`;
|
|
462
|
+
}
|
|
463
|
+
let url;
|
|
464
|
+
try {
|
|
465
|
+
url = new URL(/^https?:\/\//i.test(endpoint) ? endpoint : `https://${endpoint}`);
|
|
466
|
+
} catch {
|
|
467
|
+
throw new Error(`${endpoint} is not the address of a storage of S3.`);
|
|
468
|
+
}
|
|
469
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
470
|
+
if (url.hostname.toLowerCase().startsWith(`${bucket.toLowerCase()}.`)) {
|
|
471
|
+
return `${url.origin}${path}/`;
|
|
472
|
+
}
|
|
473
|
+
if (VIRTUAL.test(url.hostname)) {
|
|
474
|
+
return `${url.protocol}//${bucket}.${url.host}${path}/`;
|
|
475
|
+
}
|
|
476
|
+
return `${url.origin}${path}/${bucket}/`;
|
|
477
|
+
}
|
|
478
|
+
S3Util.bucketUrl = bucketUrl;
|
|
479
|
+
function region(options) {
|
|
480
|
+
const given = StoreUtil.text(options.region);
|
|
481
|
+
if (given) {
|
|
482
|
+
return given;
|
|
483
|
+
}
|
|
484
|
+
return /r2\.cloudflarestorage\.com/i.test(StoreUtil.text(options.endpoint)) ? "auto" : "us-east-1";
|
|
485
|
+
}
|
|
486
|
+
S3Util.region = region;
|
|
487
|
+
function prefix(options) {
|
|
488
|
+
return StoreUtil.text(options.prefix).replace(/^\/+|\/+$/g, "");
|
|
489
|
+
}
|
|
490
|
+
S3Util.prefix = prefix;
|
|
491
|
+
function listing(xml, prefix2) {
|
|
492
|
+
const entries = [];
|
|
493
|
+
for (const contents of StoreUtil.elements(xml, "Contents")) {
|
|
494
|
+
const key = StoreUtil.element(contents, "Key") ?? "";
|
|
495
|
+
const name = key.slice(prefix2.length);
|
|
496
|
+
if (!key.startsWith(prefix2) || !name || name.includes("/")) {
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
const tag = [StoreUtil.element(contents, "ETag"), StoreUtil.element(contents, "LastModified"), StoreUtil.element(contents, "Size")].filter(Boolean).join(":");
|
|
500
|
+
entries.push({ name, tag });
|
|
501
|
+
}
|
|
502
|
+
for (const common of StoreUtil.elements(xml, "CommonPrefixes")) {
|
|
503
|
+
const key = StoreUtil.element(common, "Prefix") ?? "";
|
|
504
|
+
const name = key.slice(prefix2.length).replace(/\/$/, "");
|
|
505
|
+
if (key.startsWith(prefix2) && name && !name.includes("/")) {
|
|
506
|
+
entries.push({ name, tag: "", dir: true });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
const truncated = StoreUtil.element(xml, "IsTruncated") === "true";
|
|
510
|
+
const next = truncated ? StoreUtil.element(xml, "NextContinuationToken") : undefined;
|
|
511
|
+
return { entries, ...next ? { next } : {} };
|
|
512
|
+
}
|
|
513
|
+
S3Util.listing = listing;
|
|
514
|
+
})(S3Util ||= {});
|
|
515
|
+
|
|
516
|
+
class S3Store {
|
|
517
|
+
now;
|
|
518
|
+
bucket;
|
|
519
|
+
root;
|
|
520
|
+
region;
|
|
521
|
+
credentials;
|
|
522
|
+
constructor(options, now = () => new Date) {
|
|
523
|
+
this.now = now;
|
|
524
|
+
this.bucket = S3Util.bucketUrl(options);
|
|
525
|
+
this.region = S3Util.region(options);
|
|
526
|
+
const prefix = S3Util.prefix(options);
|
|
527
|
+
this.root = prefix ? `${prefix}/` : "";
|
|
528
|
+
this.credentials = { accessKeyId: StoreUtil.text(options.accessKeyId), secretAccessKey: StoreUtil.text(options.secretAccessKey) };
|
|
529
|
+
if (!this.credentials.accessKeyId || !this.credentials.secretAccessKey) {
|
|
530
|
+
throw new Error("A storage of S3 needs its access key and secret.");
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
async list(dir) {
|
|
534
|
+
const prefix = this.root + (dir ? `${dir}/` : "");
|
|
535
|
+
const entries = [];
|
|
536
|
+
let token;
|
|
537
|
+
do {
|
|
538
|
+
const url = new URL(this.bucket);
|
|
539
|
+
url.searchParams.set("list-type", "2");
|
|
540
|
+
url.searchParams.set("delimiter", "/");
|
|
541
|
+
url.searchParams.set("prefix", prefix);
|
|
542
|
+
if (token) {
|
|
543
|
+
url.searchParams.set("continuation-token", token);
|
|
544
|
+
}
|
|
545
|
+
const response = await this.send("GET", url.toString());
|
|
546
|
+
if (!response.ok) {
|
|
547
|
+
throw await StoreUtil.failure(response, `S3 cannot list ${dir || "the bucket"}`);
|
|
548
|
+
}
|
|
549
|
+
const page = S3Util.listing(await response.text(), prefix);
|
|
550
|
+
entries.push(...page.entries);
|
|
551
|
+
token = page.next;
|
|
552
|
+
} while (token);
|
|
553
|
+
return entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
554
|
+
}
|
|
555
|
+
async read(path) {
|
|
556
|
+
const response = await this.send("GET", this.url(path));
|
|
557
|
+
if (response.status === 404) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (!response.ok) {
|
|
561
|
+
throw await StoreUtil.failure(response, `S3 cannot read ${path}`);
|
|
562
|
+
}
|
|
563
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
564
|
+
}
|
|
565
|
+
async write(path, data) {
|
|
566
|
+
const response = await this.send("PUT", this.url(path), data);
|
|
567
|
+
if (!response.ok) {
|
|
568
|
+
throw await StoreUtil.failure(response, `S3 cannot write ${path}`);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
async remove(path) {
|
|
572
|
+
const response = await this.send("DELETE", this.url(path));
|
|
573
|
+
if (!response.ok && response.status !== 404) {
|
|
574
|
+
throw await StoreUtil.failure(response, `S3 cannot remove ${path}`);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
url(path) {
|
|
578
|
+
return this.bucket + StoreUtil.encodePath(this.root + path);
|
|
579
|
+
}
|
|
580
|
+
send(method, url, body) {
|
|
581
|
+
const headers = S3Signer.sign({ method, url, payloadHash: body ? sha256(body) : EMPTY }, this.credentials, this.region, this.now());
|
|
582
|
+
delete headers.host;
|
|
583
|
+
return fetch(url, { method, headers, ...body ? { body } : {} });
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// src/webdav-store.ts
|
|
588
|
+
var PROPFIND = `<?xml version="1.0" encoding="utf-8"?><d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/><d:getetag/><d:getlastmodified/><d:getcontentlength/></d:prop></d:propfind>`;
|
|
589
|
+
var WebDavUtil;
|
|
590
|
+
((WebDavUtil) => {
|
|
591
|
+
function base(url) {
|
|
592
|
+
const text = StoreUtil.text(url);
|
|
593
|
+
let parsed;
|
|
594
|
+
try {
|
|
595
|
+
parsed = new URL(text);
|
|
596
|
+
} catch {
|
|
597
|
+
throw new Error(`${text || "Nothing"} is not the address of a WebDAV folder.`);
|
|
598
|
+
}
|
|
599
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
600
|
+
throw new Error(`${text} is not the address of a WebDAV folder.`);
|
|
601
|
+
}
|
|
602
|
+
parsed.hash = "";
|
|
603
|
+
parsed.search = "";
|
|
604
|
+
return parsed.toString().replace(/\/*$/, "/");
|
|
605
|
+
}
|
|
606
|
+
WebDavUtil.base = base;
|
|
607
|
+
function entries(xml, dir) {
|
|
608
|
+
const found = [];
|
|
609
|
+
const within = decodeURIComponent(dir).replace(/\/+$/, "");
|
|
610
|
+
for (const response of StoreUtil.elements(xml, "response")) {
|
|
611
|
+
const href = StoreUtil.element(response, "href");
|
|
612
|
+
if (!href) {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
let path;
|
|
616
|
+
try {
|
|
617
|
+
path = decodeURIComponent(new URL(href, "http://dav.invalid").pathname).replace(/\/+$/, "");
|
|
618
|
+
} catch {
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (path === within || !path.startsWith(`${within}/`)) {
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const name = path.slice(within.length + 1);
|
|
625
|
+
if (!name || name.includes("/") || name.startsWith(".")) {
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
const collection = StoreUtil.has(response, "collection");
|
|
629
|
+
const tag = [
|
|
630
|
+
StoreUtil.element(response, "getetag"),
|
|
631
|
+
StoreUtil.element(response, "getlastmodified"),
|
|
632
|
+
StoreUtil.element(response, "getcontentlength")
|
|
633
|
+
].filter(Boolean).join(":");
|
|
634
|
+
found.push(collection ? { name, tag: "", dir: true } : { name, tag });
|
|
635
|
+
}
|
|
636
|
+
return found.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
637
|
+
}
|
|
638
|
+
WebDavUtil.entries = entries;
|
|
639
|
+
})(WebDavUtil ||= {});
|
|
640
|
+
|
|
641
|
+
class WebDavStore {
|
|
642
|
+
base;
|
|
643
|
+
headers;
|
|
644
|
+
made = new Set;
|
|
645
|
+
constructor(options) {
|
|
646
|
+
this.base = WebDavUtil.base(options.url);
|
|
647
|
+
const user = StoreUtil.text(options.user);
|
|
648
|
+
const password = StoreUtil.text(options.password);
|
|
649
|
+
this.headers = user || password ? { authorization: `Basic ${Buffer.from(`${user}:${password}`).toString("base64")}` } : {};
|
|
650
|
+
}
|
|
651
|
+
async list(dir) {
|
|
652
|
+
const url = this.url(dir ? `${dir}/` : "");
|
|
653
|
+
const response = await fetch(url, {
|
|
654
|
+
method: "PROPFIND",
|
|
655
|
+
headers: { ...this.headers, depth: "1", "content-type": "application/xml; charset=utf-8" },
|
|
656
|
+
body: PROPFIND
|
|
657
|
+
});
|
|
658
|
+
if (response.status === 404) {
|
|
659
|
+
return [];
|
|
660
|
+
}
|
|
661
|
+
if (response.status !== 207 && !response.ok) {
|
|
662
|
+
throw await StoreUtil.failure(response, `WebDAV cannot list ${dir || "its folder"}`);
|
|
663
|
+
}
|
|
664
|
+
return WebDavUtil.entries(await response.text(), new URL(url).pathname);
|
|
665
|
+
}
|
|
666
|
+
async read(path) {
|
|
667
|
+
const response = await fetch(this.url(path), { headers: this.headers });
|
|
668
|
+
if (response.status === 404) {
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (!response.ok) {
|
|
672
|
+
throw await StoreUtil.failure(response, `WebDAV cannot read ${path}`);
|
|
673
|
+
}
|
|
674
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
675
|
+
}
|
|
676
|
+
async write(path, data) {
|
|
677
|
+
const put = () => fetch(this.url(path), { method: "PUT", headers: { ...this.headers, "content-type": "application/octet-stream" }, body: data });
|
|
678
|
+
const folder = path.split("/").slice(0, -1).join("/");
|
|
679
|
+
if (folder && !this.made.has(folder)) {
|
|
680
|
+
await this.makeFolders(folder);
|
|
681
|
+
}
|
|
682
|
+
let response = await put();
|
|
683
|
+
if (response.status === 409 || response.status === 404) {
|
|
684
|
+
this.made.clear();
|
|
685
|
+
await this.makeFolders(folder);
|
|
686
|
+
response = await put();
|
|
687
|
+
}
|
|
688
|
+
if (!response.ok) {
|
|
689
|
+
throw await StoreUtil.failure(response, `WebDAV cannot write ${path}`);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
async remove(path) {
|
|
693
|
+
const response = await fetch(this.url(path), { method: "DELETE", headers: this.headers });
|
|
694
|
+
if (!response.ok && response.status !== 404) {
|
|
695
|
+
throw await StoreUtil.failure(response, `WebDAV cannot remove ${path}`);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
async makeFolders(folder) {
|
|
699
|
+
const parts = folder.split("/").filter(Boolean);
|
|
700
|
+
for (let end = 1;end <= parts.length; end++) {
|
|
701
|
+
const path = parts.slice(0, end).join("/");
|
|
702
|
+
if (this.made.has(path)) {
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
const response = await fetch(this.url(`${path}/`), { method: "MKCOL", headers: this.headers });
|
|
706
|
+
if (!response.ok && response.status !== 405 && response.status !== 301 && response.status !== 302) {
|
|
707
|
+
throw await StoreUtil.failure(response, `WebDAV cannot make the folder ${path}`);
|
|
708
|
+
}
|
|
709
|
+
this.made.add(path);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
url(path) {
|
|
713
|
+
return this.base + StoreUtil.encodePath(path);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// src/host.ts
|
|
718
|
+
var WEBDAV = {
|
|
719
|
+
title: { en: "WebDAV", "zh-CN": "WebDAV" },
|
|
720
|
+
hint: {
|
|
721
|
+
en: "Jianguoyun, Nextcloud, a NAS of Synology or QNAP: the address of a folder of yours, and an app password.",
|
|
722
|
+
"zh-CN": "坚果云、Nextcloud、群晖或威联通的 NAS:填你的一个文件夹的地址,以及应用密码。"
|
|
723
|
+
},
|
|
724
|
+
icon: "Cloud",
|
|
725
|
+
options: [
|
|
726
|
+
{ key: "url", label: { en: "Address of a folder", "zh-CN": "文件夹地址" }, placeholder: "https://dav.jianguoyun.com/dav/Ta" },
|
|
727
|
+
{ key: "user", label: { en: "User", "zh-CN": "用户名" }, optional: true },
|
|
728
|
+
{ key: "password", label: { en: "Password", "zh-CN": "密码" }, secret: true, optional: true }
|
|
729
|
+
],
|
|
730
|
+
key: (options) => `${WebDavUtil.base(options.url)}|${StoreUtil.text(options.user)}`,
|
|
731
|
+
where: (options) => {
|
|
732
|
+
try {
|
|
733
|
+
const url = new URL(WebDavUtil.base(options.url));
|
|
734
|
+
return `${url.host}${decodeURIComponent(url.pathname).replace(/\/$/, "")}`;
|
|
735
|
+
} catch {
|
|
736
|
+
return StoreUtil.text(options.url);
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
create: (options) => new WebDavStore(options)
|
|
740
|
+
};
|
|
741
|
+
var S3 = {
|
|
742
|
+
title: { en: "S3", "zh-CN": "S3" },
|
|
743
|
+
hint: {
|
|
744
|
+
en: "AWS, Cloudflare R2, MinIO, Tencent COS, Alibaba OSS: a bucket, and keys that may read and write it. Leave the address empty for AWS.",
|
|
745
|
+
"zh-CN": "AWS、Cloudflare R2、MinIO、腾讯云 COS、阿里云 OSS:一个存储桶,以及能读写它的密钥。用 AWS 时地址留空。"
|
|
746
|
+
},
|
|
747
|
+
icon: "Database",
|
|
748
|
+
options: [
|
|
749
|
+
{ key: "endpoint", label: { en: "Address", "zh-CN": "服务地址" }, placeholder: "https://<account>.r2.cloudflarestorage.com", optional: true },
|
|
750
|
+
{ key: "bucket", label: { en: "Bucket", "zh-CN": "存储桶" }, placeholder: "my-ta" },
|
|
751
|
+
{ key: "prefix", label: { en: "Folder in it", "zh-CN": "桶内文件夹" }, placeholder: "ta", optional: true },
|
|
752
|
+
{ key: "region", label: { en: "Region", "zh-CN": "地域" }, placeholder: "us-east-1", optional: true },
|
|
753
|
+
{ key: "accessKeyId", label: { en: "Access key", "zh-CN": "访问密钥 ID" } },
|
|
754
|
+
{ key: "secretAccessKey", label: { en: "Secret", "zh-CN": "访问密钥" }, secret: true }
|
|
755
|
+
],
|
|
756
|
+
key: (options) => `${S3Util.bucketUrl(options)}|${S3Util.prefix(options)}`,
|
|
757
|
+
where: (options) => {
|
|
758
|
+
const bucket = StoreUtil.text(options.bucket);
|
|
759
|
+
const prefix = S3Util.prefix(options);
|
|
760
|
+
const endpoint = StoreUtil.text(options.endpoint).replace(/^https?:\/\//i, "").replace(/\/.*$/, "");
|
|
761
|
+
return `${bucket}${prefix ? `/${prefix}` : ""} · ${endpoint || "AWS"}`;
|
|
762
|
+
},
|
|
763
|
+
create: (options) => new S3Store(options)
|
|
764
|
+
};
|
|
765
|
+
function gitType(agentDir) {
|
|
766
|
+
const key = (options) => `${GitUtil.url(options.url)}#${GitUtil.branch(options)}|${GitUtil.folder(options)}`;
|
|
767
|
+
return {
|
|
768
|
+
title: { en: "Git", "zh-CN": "Git" },
|
|
769
|
+
hint: {
|
|
770
|
+
en: "GitHub, GitLab, Gitee, Gitea: a private repository of yours, and a token that may push to it; an SSH address needs no token, it uses the keys of this computer. Needs git installed.",
|
|
771
|
+
"zh-CN": "GitHub、GitLab、Gitee、Gitea:你的一个私有仓库,以及能推送到它的令牌;用 SSH 地址时不用令牌,用本机的密钥。需要本机装有 git。"
|
|
772
|
+
},
|
|
773
|
+
icon: "GitBranch",
|
|
774
|
+
options: [
|
|
775
|
+
{ key: "url", label: { en: "Repository", "zh-CN": "仓库地址" }, placeholder: "https://github.com/you/ta-sync.git" },
|
|
776
|
+
{ key: "branch", label: { en: "Branch", "zh-CN": "分支" }, placeholder: "main", optional: true },
|
|
777
|
+
{ key: "folder", label: { en: "Folder in it", "zh-CN": "仓库内文件夹" }, placeholder: "ta", optional: true },
|
|
778
|
+
{ key: "user", label: { en: "User", "zh-CN": "用户名" }, optional: true },
|
|
779
|
+
{ key: "token", label: { en: "Token", "zh-CN": "访问令牌" }, secret: true, optional: true }
|
|
780
|
+
],
|
|
781
|
+
key,
|
|
782
|
+
where: (options) => {
|
|
783
|
+
try {
|
|
784
|
+
return GitUtil.where(options);
|
|
785
|
+
} catch {
|
|
786
|
+
return StoreUtil.text(options.url);
|
|
787
|
+
}
|
|
788
|
+
},
|
|
789
|
+
create: (options) => new GitStore(options, join2(agentDir, "sync", "git", createHash2("sha256").update(key(options)).digest("hex").slice(0, 16)))
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
function activate(host) {
|
|
793
|
+
if (typeof host.relays?.registerType !== "function") {
|
|
794
|
+
return {};
|
|
795
|
+
}
|
|
796
|
+
const stops = [host.relays.registerType("webdav", WEBDAV), host.relays.registerType("s3", S3), host.relays.registerType("git", gitType(host.agentDir))];
|
|
797
|
+
return {
|
|
798
|
+
dispose: () => {
|
|
799
|
+
for (const stop of stops) {
|
|
800
|
+
stop();
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
export {
|
|
806
|
+
gitType,
|
|
807
|
+
activate as default,
|
|
808
|
+
WEBDAV,
|
|
809
|
+
S3
|
|
810
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-sync-cloud",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.13.0",
|
|
4
|
+
"description": "A pi package for Ta: sync of the user's computers in storage of their own, a WebDAV server (Jianguoyun, Nextcloud, a NAS) a bucket of S3 (AWS, Cloudflare R2, MinIO) or a git repository, sealed, with no relay",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"files": [
|
|
18
|
+
"esm",
|
|
19
|
+
"host",
|
|
20
|
+
"!**/*.map"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -b && bun build src/host.ts --format esm --target node --outfile host/index.mjs",
|
|
24
|
+
"clean": "rm -rf lib host *.tsbuildinfo",
|
|
25
|
+
"test": "bun test ./lib",
|
|
26
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@celljs/ta-protocol": "4.13.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"pi": {
|
|
35
|
+
"title": {
|
|
36
|
+
"en": "Sync in your own storage",
|
|
37
|
+
"zh-CN": "自己的存储同步"
|
|
38
|
+
},
|
|
39
|
+
"summary": {
|
|
40
|
+
"en": "Sync your computers through storage of your own: a WebDAV server (Jianguoyun, Nextcloud, a NAS) or a bucket of S3 (AWS, Cloudflare R2, MinIO) or a git repository (GitHub, GitLab, Gitee). Sealed before it leaves, no relay.",
|
|
41
|
+
"zh-CN": "让几台电脑经你自己的存储同步:WebDAV(坚果云、Nextcloud、NAS)或 S3 存储桶(AWS、Cloudflare R2、MinIO)或 git 仓库(GitHub、GitLab、Gitee)。离开电脑前就已加密,不经过中继。"
|
|
42
|
+
},
|
|
43
|
+
"icon": "Cloud",
|
|
44
|
+
"host": "./host/index.mjs",
|
|
45
|
+
"permissions": {
|
|
46
|
+
"host": {
|
|
47
|
+
"*": "settings.manage"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"exports": {
|
|
52
|
+
"./package.json": "./package.json",
|
|
53
|
+
"./lib/*.js": {
|
|
54
|
+
"import": "./esm/*.js",
|
|
55
|
+
"default": "./lib/*.js"
|
|
56
|
+
},
|
|
57
|
+
"./lib/*": {
|
|
58
|
+
"import": "./esm/*.js",
|
|
59
|
+
"default": "./lib/*.js"
|
|
60
|
+
},
|
|
61
|
+
"./*": "./*"
|
|
62
|
+
}
|
|
63
|
+
}
|