@muhkoo/cli 0.15.0-alpha.0 → 0.15.0-alpha.10
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.md +1 -0
- package/bin/muhkoo.js +4 -1
- package/package.json +11 -4
- package/src/commands/kv.js +65 -0
- package/src/commands/vfs.js +118 -14
- package/src/index.js +2 -0
- package/src/lib/drain.js +60 -0
- package/src/lib/mount.js +608 -135
- package/src/lib/syncPlan.js +73 -3
- package/src/lib/vfs.js +29 -14
package/src/lib/syncPlan.js
CHANGED
|
@@ -67,6 +67,64 @@ export function planSync({ last, local, remote, unknown = [] }) {
|
|
|
67
67
|
return plan;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/** What a folder is in the state file, in place of a content hash. */
|
|
71
|
+
export const DIR = "dir";
|
|
72
|
+
|
|
73
|
+
/** A folder's key in the state file: its path with a trailing slash, so it can never be a file's. */
|
|
74
|
+
export const dirKey = (path) => `${path}/`;
|
|
75
|
+
|
|
76
|
+
/** The state without its folders: what the file planner, and its breaker, count. */
|
|
77
|
+
export function filesOf(state) {
|
|
78
|
+
const out = {};
|
|
79
|
+
for (const [key, hash] of Object.entries(state)) if (!key.endsWith("/")) out[key] = hash;
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Plan the folders, by the same three-way rule as the files ({@link planPath}),
|
|
85
|
+
* with {@link DIR} standing in for a hash. `local` and `remote` are the folder
|
|
86
|
+
* paths each side holds (`remote` null when the folders could not be listed:
|
|
87
|
+
* then nothing is planned, since a folder not listed is not a folder gone).
|
|
88
|
+
*
|
|
89
|
+
* Returns `{ path, action }` with action one of: "record" (on both sides, not
|
|
90
|
+
* yet in the state), "mkdir-local", "mkdir-remote", "rmdir-local",
|
|
91
|
+
* "rmdir-remote", "forget". Paths in `unknown` are left alone, and so, when
|
|
92
|
+
* `fold` is given (a volume that folds names), is every folder whose folded
|
|
93
|
+
* name more than one spelling shares: which of them this disk holds cannot be
|
|
94
|
+
* told apart, and reading one as missing would plan its removal.
|
|
95
|
+
*/
|
|
96
|
+
export function planDirs({ last, local, remote, unknown = [], fold = null }) {
|
|
97
|
+
if (!remote) return [];
|
|
98
|
+
const isUnknown = coveredBy(unknown);
|
|
99
|
+
const before = new Set(Object.keys(last).filter((k) => k.endsWith("/")).map((k) => k.slice(0, -1)));
|
|
100
|
+
const here = new Set(local);
|
|
101
|
+
const there = new Set(remote);
|
|
102
|
+
const paths = new Set([...before, ...here, ...there]);
|
|
103
|
+
const clash = new Set();
|
|
104
|
+
if (fold) {
|
|
105
|
+
const spellings = new Map();
|
|
106
|
+
for (const path of paths) {
|
|
107
|
+
const folded = fold(path);
|
|
108
|
+
if (!spellings.has(folded)) spellings.set(folded, []);
|
|
109
|
+
spellings.get(folded).push(path);
|
|
110
|
+
}
|
|
111
|
+
for (const group of spellings.values()) if (group.length > 1) for (const path of group) clash.add(path);
|
|
112
|
+
}
|
|
113
|
+
const ACTIONS = { push: "mkdir-remote", pull: "mkdir-local", "delete-remote": "rmdir-remote", "delete-local": "rmdir-local", forget: "forget" };
|
|
114
|
+
const plan = [];
|
|
115
|
+
for (const path of [...paths].sort()) {
|
|
116
|
+
if (isUnknown(path) || clash.has(path)) continue;
|
|
117
|
+
const was = before.has(path) ? DIR : undefined;
|
|
118
|
+
const action = planPath({ last: was, local: here.has(path) ? DIR : undefined, remote: there.has(path) ? DIR : undefined });
|
|
119
|
+
if (action === "none") {
|
|
120
|
+
if (!was) plan.push({ path, action: "record" });
|
|
121
|
+
} else if (ACTIONS[action]) {
|
|
122
|
+
plan.push({ path, action: ACTIONS[action] });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return plan;
|
|
126
|
+
}
|
|
127
|
+
|
|
70
128
|
/**
|
|
71
129
|
* `(path) => boolean`: is `path`, or a directory above it, in `prefixes`?
|
|
72
130
|
*
|
|
@@ -133,6 +191,15 @@ function coveredBy(prefixes) {
|
|
|
133
191
|
* as they were: the next pass sees the same picture rather than "forgotten
|
|
134
192
|
* files" to copy back.
|
|
135
193
|
*
|
|
194
|
+
* One broken view is answered rather than held: a local side that holds
|
|
195
|
+
* nothing at all (`views.bare`: no files, no folders, nothing refused or
|
|
196
|
+
* unreadable) — a mount folder emptied to start again. Its remote deletes
|
|
197
|
+
* become pulls and `restored` counts them, so the files come back instead of
|
|
198
|
+
* the user deleting the state file by hand. Nothing local can be overwritten,
|
|
199
|
+
* as there is nothing local, and nothing remote is deleted. An empty REMOTE is
|
|
200
|
+
* never answered this way: there an empty view more likely means a renamed or
|
|
201
|
+
* missing root, and uploading everything again would put it in the wrong place.
|
|
202
|
+
*
|
|
136
203
|
* What this is NOT: a defence against whatever can write the filesystem. It
|
|
137
204
|
* catches broken views — a renamed root, an empty walk, a wiped checkout. A
|
|
138
205
|
* remote party can add filler files first, so that its deletions are a
|
|
@@ -145,13 +212,16 @@ export function holdMassDeletes(plan, last, views = {}, peak = 0) {
|
|
|
145
212
|
const tracked = Object.keys(last).length;
|
|
146
213
|
const most = Math.max(tracked, peak);
|
|
147
214
|
const held = [];
|
|
215
|
+
let restored = 0;
|
|
148
216
|
for (const [action, other] of [["delete-local", "remote"], ["delete-remote", "local"]]) {
|
|
149
217
|
const count = plan.filter((p) => p.action === action).length;
|
|
150
218
|
if (!count) continue;
|
|
151
|
-
if (views
|
|
219
|
+
if (action === "delete-remote" && views.local === 0 && views.bare === true) restored = count;
|
|
220
|
+
else if (views[other] === 0) held.push({ action, count, tracked, empty: true });
|
|
152
221
|
else if (count > MASS_DELETE_FLOOR && tracked - count < most / 2) held.push({ action, count, tracked });
|
|
153
222
|
}
|
|
154
|
-
if (
|
|
223
|
+
if (restored) plan = plan.map((p) => (p.action === "delete-remote" ? { path: p.path, action: "pull" } : p));
|
|
224
|
+
if (!held.length) return { plan, held, restored };
|
|
155
225
|
const holding = new Set(held.map((h) => h.action));
|
|
156
|
-
return { plan: plan.filter((p) => !holding.has(p.action)), held };
|
|
226
|
+
return { plan: plan.filter((p) => !holding.has(p.action)), held, restored };
|
|
157
227
|
}
|
package/src/lib/vfs.js
CHANGED
|
@@ -30,6 +30,34 @@ import { die, info, step, prompt, promptHidden } from "./ui.js";
|
|
|
30
30
|
* pairing alone, unlocking for this command only.
|
|
31
31
|
*/
|
|
32
32
|
export async function openClient(args) {
|
|
33
|
+
const { cfg, baseUrl, client } = await restoreSession(args);
|
|
34
|
+
|
|
35
|
+
await unlock({ args, cfg, baseUrl, client });
|
|
36
|
+
|
|
37
|
+
// Restore the working directory. A CLI is a fresh process each time, so `cd`
|
|
38
|
+
// only means anything if it survives between invocations — the same reason a
|
|
39
|
+
// shell keeps PWD. A directory that has since been deleted silently falls
|
|
40
|
+
// back to the root rather than making every later command fail.
|
|
41
|
+
if (cfg.vfsCwd && cfg.vfsCwd !== "/") {
|
|
42
|
+
await client.vfs.cd(cfg.vfsCwd).catch(async () => {
|
|
43
|
+
if (!args.readOnly) await saveConfig({ ...cfg, vfsCwd: "/" });
|
|
44
|
+
info(`Working directory ${cfg.vfsCwd} is gone — back to /.`);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return client;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A Client with the session restored and NOTHING unlocked, for commands that
|
|
52
|
+
* only name things (`kv ls`) and never decrypt a value. Unlocking would ask for
|
|
53
|
+
* a password, or pair this machine, for nothing. Saves nothing to ~/.muhkoo.
|
|
54
|
+
*/
|
|
55
|
+
export async function openSession(args) {
|
|
56
|
+
return (await restoreSession(args)).client;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The Client both of the above start from: signed in, session verified, not unlocked. */
|
|
60
|
+
async function restoreSession(args) {
|
|
33
61
|
const cfg = await loadConfig();
|
|
34
62
|
const baseUrl = await resolveBase(args, cfg);
|
|
35
63
|
const token = args.token || process.env.MUHKOO_DEV_TOKEN || cfg.token;
|
|
@@ -61,20 +89,7 @@ export async function openClient(args) {
|
|
|
61
89
|
// which would wipe a seed we had already put there.
|
|
62
90
|
const user = await client.auth.zk.restore();
|
|
63
91
|
if (!user) die("Your session has expired. Run `muhkoo login` and try again.");
|
|
64
|
-
|
|
65
|
-
await unlock({ args, cfg, baseUrl, client });
|
|
66
|
-
|
|
67
|
-
// Restore the working directory. A CLI is a fresh process each time, so `cd`
|
|
68
|
-
// only means anything if it survives between invocations — the same reason a
|
|
69
|
-
// shell keeps PWD. A directory that has since been deleted silently falls
|
|
70
|
-
// back to the root rather than making every later command fail.
|
|
71
|
-
if (cfg.vfsCwd && cfg.vfsCwd !== "/") {
|
|
72
|
-
await client.vfs.cd(cfg.vfsCwd).catch(async () => {
|
|
73
|
-
if (!args.readOnly) await saveConfig({ ...cfg, vfsCwd: "/" });
|
|
74
|
-
info(`Working directory ${cfg.vfsCwd} is gone — back to /.`);
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return client;
|
|
92
|
+
return { cfg, baseUrl, client };
|
|
78
93
|
}
|
|
79
94
|
|
|
80
95
|
/**
|