nemu-cli 1.6.1 → 1.9.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/CHANGELOG.md +43 -0
- package/dist/clients/claude-plugin.js +68 -0
- package/dist/clients/registry.js +9 -1
- package/dist/commands/login.js +9 -4
- package/dist/commands/private.js +259 -0
- package/dist/commands/status.js +42 -4
- package/dist/commands/sync.js +10 -0
- package/dist/commands/vault.js +71 -3
- package/dist/device.js +14 -8
- package/dist/index.js +5 -1
- package/dist/vault/api.js +156 -6
- package/dist/vault/connect.js +19 -1
- package/dist/vault/engine.js +24 -20
- package/dist/vault/files.js +10 -10
- package/dist/vault/ignore.js +6 -3
- package/dist/vault/private/api.js +110 -0
- package/dist/vault/private/crypto.js +212 -0
- package/dist/vault/private/keystore.js +104 -0
- package/dist/vault/private/locks.js +59 -0
- package/dist/vault/private/strength.js +22 -0
- package/dist/vault/state.js +14 -3
- package/dist/version.js +1 -1
- package/package.json +12 -1
- package/plugin/.claude-plugin/marketplace.json +14 -0
- package/plugin/nemu/.claude-plugin/plugin.json +8 -0
- package/plugin/nemu/.mcp.json +8 -0
- package/plugin/nemu/commands/inspo.md +22 -0
- package/plugin/nemu/commands/remember.md +16 -0
package/dist/commands/vault.js
CHANGED
|
@@ -9,7 +9,9 @@ import { cli_transport, client_label, signed_in } from "../vault/connect.js";
|
|
|
9
9
|
import { is_conflict_copy } from "../vault/paths.js";
|
|
10
10
|
import { empty_state, find_root, marker_path, read_marker, read_state, remove_marker, remove_state, state_path_for, write_marker, write_state, } from "../vault/state.js";
|
|
11
11
|
import { is_default_ignored } from "../vault/ignore.js";
|
|
12
|
-
|
|
12
|
+
import { load_vault_key } from "../vault/private/keystore.js";
|
|
13
|
+
import { approve_requests, create_private_vault, forget_key, unlock_vault } from "./private.js";
|
|
14
|
+
const USAGE = "Use: nemu vault list | link <folder> [--vault <name|id>] [--create [--private]] [--obsidian] | unlink [folder] | status [folder] | unlock [folder] [--password|--recovery|--approve] | approve [folder] | forget [folder]";
|
|
13
15
|
function size_of(bytes) {
|
|
14
16
|
if (bytes === undefined)
|
|
15
17
|
return "";
|
|
@@ -44,6 +46,10 @@ async function link(args) {
|
|
|
44
46
|
const workspace = flag(args, "workspace", "w");
|
|
45
47
|
const wanted = flag(args, "vault");
|
|
46
48
|
const create = bool_flag(args, "create");
|
|
49
|
+
const is_private = bool_flag(args, "private");
|
|
50
|
+
if (is_private && !create) {
|
|
51
|
+
throw new CliError("--private makes a new vault, so it goes with --create.", "invalid_arguments");
|
|
52
|
+
}
|
|
47
53
|
const enclosing = await find_root(dirname(folder));
|
|
48
54
|
if (enclosing !== null && enclosing !== folder) {
|
|
49
55
|
throw new CliError(`${folder} is inside ${enclosing}, which is already linked to a vault.`, "already_linked");
|
|
@@ -54,7 +60,9 @@ async function link(args) {
|
|
|
54
60
|
const vaults = await list_vaults(transport);
|
|
55
61
|
let vault = pick_vault(vaults, wanted);
|
|
56
62
|
if (vault === null && create) {
|
|
57
|
-
vault =
|
|
63
|
+
vault = is_private
|
|
64
|
+
? await create_private_vault(transport, wanted ?? basename(folder))
|
|
65
|
+
: await create_vault(transport, wanted ?? basename(folder));
|
|
58
66
|
}
|
|
59
67
|
if (vault === null) {
|
|
60
68
|
const names = vaults.map((entry) => `"${entry.name}"`).join(", ");
|
|
@@ -81,6 +89,7 @@ async function link(args) {
|
|
|
81
89
|
workspace_id,
|
|
82
90
|
server: credentials.api_url,
|
|
83
91
|
linked_at: new Date().toISOString(),
|
|
92
|
+
mode: info.data.mode === "private" ? "private" : "standard",
|
|
84
93
|
});
|
|
85
94
|
if (previous === null || previous.root !== folder || existing === null) {
|
|
86
95
|
await write_state(state_path, empty_state(vault.id, folder, {
|
|
@@ -95,7 +104,10 @@ async function link(args) {
|
|
|
95
104
|
emit({ ok: true, folder, vault: { id: vault.id, name: vault.name }, workspace_id, marker: marker_path(folder) });
|
|
96
105
|
return 0;
|
|
97
106
|
}
|
|
98
|
-
success(`Linked ${folder} to the vault ${bold(vault.name)}`);
|
|
107
|
+
success(`Linked ${folder} to the ${info.data.mode === "private" ? "private " : ""}vault ${bold(vault.name)}`);
|
|
108
|
+
if (info.data.mode === "private" && (await load_vault_key(vault.id)) === null) {
|
|
109
|
+
out(dim(` It is locked on this computer. Run nemu vault unlock ${raw} first.`));
|
|
110
|
+
}
|
|
99
111
|
out(dim(` Run nemu sync ${raw} to sync it, or nemu sync ${raw} --watch to keep it in sync.`));
|
|
100
112
|
return 0;
|
|
101
113
|
}
|
|
@@ -146,6 +158,7 @@ async function status(args) {
|
|
|
146
158
|
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
147
159
|
const state = await read_state(state_path_for(marker.vault_id));
|
|
148
160
|
const conflicts = await conflict_copies(folder);
|
|
161
|
+
const marker_mode = await vault_mode(marker);
|
|
149
162
|
const summary = {
|
|
150
163
|
folder,
|
|
151
164
|
vault: { id: marker.vault_id, name: marker.vault_name },
|
|
@@ -154,7 +167,10 @@ async function status(args) {
|
|
|
154
167
|
tracked: state === null ? 0 : Object.keys(state.files).length,
|
|
155
168
|
last_sync: state?.synced_at ?? null,
|
|
156
169
|
conflicts_mode: state?.settings.conflicts ?? "merge",
|
|
170
|
+
private: marker_mode === "private",
|
|
171
|
+
unlocked: marker_mode === "private" ? (await load_vault_key(marker.vault_id)) !== null : null,
|
|
157
172
|
obsidian_settings: state?.settings.obsidian ?? false,
|
|
173
|
+
obsidian_plugins: state?.settings.obsidian_plugins ?? false,
|
|
158
174
|
conflicts,
|
|
159
175
|
};
|
|
160
176
|
if (is_json_mode()) {
|
|
@@ -164,9 +180,15 @@ async function status(args) {
|
|
|
164
180
|
out(` ${bold(marker.vault_name)} ${dim(marker.vault_id)}`);
|
|
165
181
|
out(` ${dim("folder ")} ${folder}`);
|
|
166
182
|
out(` ${dim("files ")} ${summary.tracked}`);
|
|
183
|
+
if (summary.private) {
|
|
184
|
+
out(` ${dim("private ")} ${summary.unlocked === true ? "open on this computer" : "locked on this computer (nemu vault unlock)"}`);
|
|
185
|
+
}
|
|
167
186
|
out(` ${dim("last sync")} ${summary.last_sync ?? "never"}`);
|
|
168
187
|
out(` ${dim("conflicts")} ${summary.conflicts_mode === "merge" ? "merge when possible" : "always write a conflict file"}`);
|
|
169
188
|
out(` ${dim(".obsidian")} ${summary.obsidian_settings ? "synced" : "not synced on this device"}`);
|
|
189
|
+
if (summary.obsidian_settings) {
|
|
190
|
+
out(` ${dim("plugins ")} ${summary.obsidian_plugins ? "synced" : "not synced (plugins are code)"}`);
|
|
191
|
+
}
|
|
170
192
|
if (conflicts.length > 0) {
|
|
171
193
|
out();
|
|
172
194
|
out(` ${bold(`${conflicts.length} open conflict${conflicts.length === 1 ? "" : "s"}`)} ${dim("(delete the copy you do not want)")}`);
|
|
@@ -175,6 +197,46 @@ async function status(args) {
|
|
|
175
197
|
}
|
|
176
198
|
return 0;
|
|
177
199
|
}
|
|
200
|
+
async function vault_mode(marker) {
|
|
201
|
+
try {
|
|
202
|
+
await signed_in();
|
|
203
|
+
const info = await new VaultApi(cli_transport(marker.workspace_id), marker.vault_id, client_label()).info();
|
|
204
|
+
return info.data.mode ?? "standard";
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return "unknown";
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async function linked_marker(args) {
|
|
211
|
+
const folder = await locate(args);
|
|
212
|
+
const marker = await read_marker(folder);
|
|
213
|
+
if (marker === null)
|
|
214
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
215
|
+
await signed_in();
|
|
216
|
+
return marker;
|
|
217
|
+
}
|
|
218
|
+
async function unlock(args) {
|
|
219
|
+
const marker = await linked_marker(args);
|
|
220
|
+
await unlock_vault(cli_transport(marker.workspace_id), marker.vault_id, marker.vault_name, {
|
|
221
|
+
password: bool_flag(args, "password"),
|
|
222
|
+
recovery: bool_flag(args, "recovery"),
|
|
223
|
+
approve: bool_flag(args, "approve"),
|
|
224
|
+
});
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
async function approve(args) {
|
|
228
|
+
const marker = await linked_marker(args);
|
|
229
|
+
await approve_requests(cli_transport(marker.workspace_id), marker.vault_id, marker.vault_name);
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
async function forget(args) {
|
|
233
|
+
const folder = await locate(args);
|
|
234
|
+
const marker = await read_marker(folder);
|
|
235
|
+
if (marker === null)
|
|
236
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
237
|
+
await forget_key(marker.vault_id, marker.vault_name);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
178
240
|
export async function vault(args) {
|
|
179
241
|
const action = args.positional[0] ?? "list";
|
|
180
242
|
if (action === "list")
|
|
@@ -185,5 +247,11 @@ export async function vault(args) {
|
|
|
185
247
|
return unlink(args);
|
|
186
248
|
if (action === "status" || action === "conflicts")
|
|
187
249
|
return status(args);
|
|
250
|
+
if (action === "unlock")
|
|
251
|
+
return unlock(args);
|
|
252
|
+
if (action === "approve")
|
|
253
|
+
return approve(args);
|
|
254
|
+
if (action === "forget")
|
|
255
|
+
return forget(args);
|
|
188
256
|
throw new CliError(USAGE, "invalid_arguments");
|
|
189
257
|
}
|
package/dist/device.js
CHANGED
|
@@ -28,16 +28,22 @@ export async function ensure_device(force = false) {
|
|
|
28
28
|
});
|
|
29
29
|
return device.id;
|
|
30
30
|
}
|
|
31
|
-
export async function
|
|
31
|
+
export async function device_state() {
|
|
32
32
|
const credentials = await read_credentials();
|
|
33
33
|
const settings = await read_settings();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const devices = await list_devices();
|
|
38
|
-
const mine = devices.find((device) => device.id === settings.device_id);
|
|
39
|
-
if (mine !== undefined && mine.revoked_at === null)
|
|
40
|
-
return mine.id;
|
|
34
|
+
const id = settings.device_id;
|
|
35
|
+
if (credentials === null || id === undefined || settings.device_api_url !== credentials.api_url) {
|
|
36
|
+
return { state: "unregistered" };
|
|
41
37
|
}
|
|
38
|
+
const devices = await list_devices();
|
|
39
|
+
const mine = devices.find((device) => device.id === id);
|
|
40
|
+
if (mine === undefined)
|
|
41
|
+
return { state: "unknown", id };
|
|
42
|
+
return { state: mine.revoked_at === null ? "active" : "revoked", id };
|
|
43
|
+
}
|
|
44
|
+
export async function verify_device() {
|
|
45
|
+
const current = await device_state();
|
|
46
|
+
if (current.state === "active")
|
|
47
|
+
return current.id;
|
|
42
48
|
return ensure_device(true);
|
|
43
49
|
}
|
package/dist/index.js
CHANGED
|
@@ -91,11 +91,15 @@ const HELP = `
|
|
|
91
91
|
|
|
92
92
|
${bold("Vaults")}
|
|
93
93
|
vault list Vaults in this workspace
|
|
94
|
-
vault link <folder> [--vault <name|id>] [--create] [--obsidian]
|
|
94
|
+
vault link <folder> [--vault <name|id>] [--create [--private]] [--obsidian]
|
|
95
95
|
Link a folder to a vault (writes <folder>/.nemu/vault.json)
|
|
96
96
|
vault unlink [folder] Stop syncing a folder; its files stay
|
|
97
97
|
vault status [folder] Last sync, settings and open conflicts
|
|
98
|
+
vault unlock [folder] Open a private vault on this computer (password, recovery key, or approval)
|
|
99
|
+
vault approve [folder] Let another device open a private vault, after comparing its code
|
|
100
|
+
vault forget [folder] Lock a private vault on this computer again
|
|
98
101
|
sync [folder] [--watch] [--dry-run] [--conflicts=merge|file] [--allow-deletes]
|
|
102
|
+
[--obsidian on|off] [--obsidian-plugins on|off]
|
|
99
103
|
Sync a linked folder with its vault
|
|
100
104
|
|
|
101
105
|
${bold("Account")}
|
package/dist/vault/api.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hash_of } from "./files.js";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
import { CliError } from "../errors.js";
|
|
3
4
|
const note_schema = z.object({
|
|
@@ -9,10 +10,12 @@ const note_schema = z.object({
|
|
|
9
10
|
size: z.number(),
|
|
10
11
|
revision: z.number(),
|
|
11
12
|
deleted: z.boolean(),
|
|
13
|
+
sealed_path: z.string().optional(),
|
|
12
14
|
});
|
|
13
15
|
const vault_schema = z.object({
|
|
14
16
|
id: z.string(),
|
|
15
17
|
name: z.string(),
|
|
18
|
+
mode: z.enum(["standard", "private"]).optional(),
|
|
16
19
|
revision: z.number(),
|
|
17
20
|
notes: z.number().optional(),
|
|
18
21
|
stored_bytes: z.number().optional(),
|
|
@@ -26,12 +29,22 @@ const info_schema = z.object({
|
|
|
26
29
|
.object({
|
|
27
30
|
max_file_bytes: z.number(),
|
|
28
31
|
max_storage_bytes: z.number().nullable(),
|
|
32
|
+
max_upload_file_bytes: z.number().optional(),
|
|
33
|
+
part_bytes: z.number().optional(),
|
|
29
34
|
})
|
|
30
35
|
.optional(),
|
|
31
36
|
});
|
|
37
|
+
const started_schema = z.object({
|
|
38
|
+
data: z.object({
|
|
39
|
+
upload_id: z.string(),
|
|
40
|
+
part_bytes: z.number().int().positive(),
|
|
41
|
+
parts: z.number().int().positive(),
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
32
44
|
const version_schema = z.object({
|
|
33
45
|
version: z.number().int(),
|
|
34
46
|
path: z.string(),
|
|
47
|
+
sealed_path: z.string().optional(),
|
|
35
48
|
hash: z.string(),
|
|
36
49
|
size: z.number(),
|
|
37
50
|
deleted: z.boolean(),
|
|
@@ -81,6 +94,8 @@ export class VaultApi {
|
|
|
81
94
|
transport;
|
|
82
95
|
vault_id;
|
|
83
96
|
client_label;
|
|
97
|
+
single_bytes = null;
|
|
98
|
+
can_upload_parts = false;
|
|
84
99
|
constructor(transport, vault_id, client_label) {
|
|
85
100
|
this.transport = transport;
|
|
86
101
|
this.vault_id = vault_id;
|
|
@@ -98,12 +113,55 @@ export class VaultApi {
|
|
|
98
113
|
await wait(retry_delay(response, attempt));
|
|
99
114
|
}
|
|
100
115
|
}
|
|
116
|
+
hash(content) {
|
|
117
|
+
return hash_of(content);
|
|
118
|
+
}
|
|
101
119
|
base() {
|
|
102
120
|
return `/api/v1/vaults/${encodeURIComponent(this.vault_id)}`;
|
|
103
121
|
}
|
|
104
122
|
async info() {
|
|
105
123
|
const response = await this.call("GET", this.base());
|
|
106
|
-
|
|
124
|
+
const info = parse(info_schema, await response.json());
|
|
125
|
+
if (info.limits !== undefined) {
|
|
126
|
+
this.single_bytes = info.limits.max_file_bytes;
|
|
127
|
+
this.can_upload_parts =
|
|
128
|
+
info.limits.part_bytes !== undefined &&
|
|
129
|
+
(info.limits.max_upload_file_bytes ?? 0) > info.limits.max_file_bytes;
|
|
130
|
+
}
|
|
131
|
+
return info;
|
|
132
|
+
}
|
|
133
|
+
async write_parts(path, content, base_version, sealed) {
|
|
134
|
+
const uploads = `${this.base()}/uploads`;
|
|
135
|
+
const response = await this.call("POST", uploads, {
|
|
136
|
+
headers: { "content-type": "application/json", "x-nemu-client": this.client_label },
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
path,
|
|
139
|
+
size: content.length,
|
|
140
|
+
base_version,
|
|
141
|
+
...(sealed === undefined ? {} : { sealed_path: sealed.sealed_path, content_hash: sealed.content_hash }),
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
const upload = parse(started_schema, await response.json()).data;
|
|
145
|
+
try {
|
|
146
|
+
for (let index = 0; index < upload.parts; index += 1) {
|
|
147
|
+
await this.call("PUT", `${uploads}/${upload.upload_id}/${index}`, {
|
|
148
|
+
headers: { "content-type": "application/octet-stream", "x-nemu-client": this.client_label },
|
|
149
|
+
body: content.subarray(index * upload.part_bytes, (index + 1) * upload.part_bytes),
|
|
150
|
+
timeout_ms: 120_000,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const finished = await this.call("POST", `${uploads}/${upload.upload_id}`, {
|
|
154
|
+
headers: { "x-nemu-client": this.client_label },
|
|
155
|
+
timeout_ms: 300_000,
|
|
156
|
+
});
|
|
157
|
+
return parse(z.object({ data: note_schema }), await finished.json()).data;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
await this.call("DELETE", `${uploads}/${upload.upload_id}`, {
|
|
161
|
+
headers: { "x-nemu-client": this.client_label },
|
|
162
|
+
}).catch(() => undefined);
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
107
165
|
}
|
|
108
166
|
async notes(trash = false) {
|
|
109
167
|
const response = await this.call("GET", `${this.base()}/notes`, {
|
|
@@ -133,13 +191,19 @@ export class VaultApi {
|
|
|
133
191
|
}
|
|
134
192
|
return { content, id, version, hash };
|
|
135
193
|
}
|
|
136
|
-
async write(path, content, base_version) {
|
|
194
|
+
async write(path, content, base_version, sealed) {
|
|
195
|
+
if (this.can_upload_parts && this.single_bytes !== null && content.length > this.single_bytes) {
|
|
196
|
+
return this.write_parts(path, content, base_version, sealed);
|
|
197
|
+
}
|
|
137
198
|
const response = await this.call("PUT", `${this.base()}/files`, {
|
|
138
199
|
query: { path },
|
|
139
200
|
headers: {
|
|
140
201
|
"content-type": "application/octet-stream",
|
|
141
202
|
"x-nemu-client": this.client_label,
|
|
142
203
|
...(base_version === null ? { "if-none-match": "*" } : { "if-match": `"${base_version}"` }),
|
|
204
|
+
...(sealed === undefined
|
|
205
|
+
? {}
|
|
206
|
+
: { "x-nemu-sealed-path": sealed.sealed_path, "x-nemu-content-mac": sealed.content_hash }),
|
|
143
207
|
},
|
|
144
208
|
body: content,
|
|
145
209
|
timeout_ms: 120_000,
|
|
@@ -153,10 +217,21 @@ export class VaultApi {
|
|
|
153
217
|
});
|
|
154
218
|
return parse(z.object({ data: note_schema }), await response.json()).data;
|
|
155
219
|
}
|
|
156
|
-
async move(from, to, base_version) {
|
|
220
|
+
async move(from, to, base_version, sealed) {
|
|
157
221
|
const response = await this.call("POST", `${this.base()}/files/move`, {
|
|
158
222
|
headers: { "content-type": "application/json", "x-nemu-client": this.client_label },
|
|
159
|
-
body: JSON.stringify({
|
|
223
|
+
body: JSON.stringify({
|
|
224
|
+
from,
|
|
225
|
+
to,
|
|
226
|
+
base_version,
|
|
227
|
+
...(sealed === undefined
|
|
228
|
+
? {}
|
|
229
|
+
: {
|
|
230
|
+
sealed_path: sealed.sealed_path,
|
|
231
|
+
content_hash: sealed.content_hash,
|
|
232
|
+
content: Buffer.from(sealed.content).toString("base64url"),
|
|
233
|
+
}),
|
|
234
|
+
}),
|
|
160
235
|
});
|
|
161
236
|
return parse(z.object({ data: note_schema }), await response.json()).data;
|
|
162
237
|
}
|
|
@@ -187,16 +262,91 @@ export class VaultApi {
|
|
|
187
262
|
return parse(z.object({ data: z.array(version_schema) }), await response.json()).data;
|
|
188
263
|
}
|
|
189
264
|
}
|
|
265
|
+
const lock_schema = z.object({
|
|
266
|
+
id: z.string(),
|
|
267
|
+
kind: z.string(),
|
|
268
|
+
label: z.string(),
|
|
269
|
+
params: z.record(z.string(), z.union([z.string(), z.number()])),
|
|
270
|
+
created_at: z.string(),
|
|
271
|
+
last_used_at: z.string().nullable(),
|
|
272
|
+
});
|
|
273
|
+
const key_request_schema = z.object({
|
|
274
|
+
id: z.string(),
|
|
275
|
+
label: z.string(),
|
|
276
|
+
commitment: z.string(),
|
|
277
|
+
approver_key: z.string().nullable(),
|
|
278
|
+
public_key: z.string().nullable(),
|
|
279
|
+
nonce: z.string().nullable(),
|
|
280
|
+
created_at: z.string(),
|
|
281
|
+
expires_at: z.string(),
|
|
282
|
+
});
|
|
283
|
+
export class LockApi {
|
|
284
|
+
transport;
|
|
285
|
+
vault_id;
|
|
286
|
+
constructor(transport, vault_id) {
|
|
287
|
+
this.transport = transport;
|
|
288
|
+
this.vault_id = vault_id;
|
|
289
|
+
}
|
|
290
|
+
async call(method, path, body) {
|
|
291
|
+
const response = await this.transport(method, `/api/v1/vaults/${encodeURIComponent(this.vault_id)}${path}`, {
|
|
292
|
+
...(body === undefined
|
|
293
|
+
? {}
|
|
294
|
+
: { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }),
|
|
295
|
+
});
|
|
296
|
+
if (!response.ok)
|
|
297
|
+
throw await error_of(response);
|
|
298
|
+
return response.json();
|
|
299
|
+
}
|
|
300
|
+
async locks() {
|
|
301
|
+
return parse(z.object({ data: z.array(lock_schema) }), await this.call("GET", "/locks")).data;
|
|
302
|
+
}
|
|
303
|
+
async wrapped(lock_id) {
|
|
304
|
+
return parse(z.object({ data: z.object({ wrapped: z.string() }) }), await this.call("GET", `/locks/${encodeURIComponent(lock_id)}`)).data.wrapped;
|
|
305
|
+
}
|
|
306
|
+
async add(lock) {
|
|
307
|
+
return parse(z.object({ data: z.object({ id: z.string() }) }), await this.call("POST", "/locks", lock)).data.id;
|
|
308
|
+
}
|
|
309
|
+
async remove(lock_id) {
|
|
310
|
+
await this.call("DELETE", `/locks/${encodeURIComponent(lock_id)}`);
|
|
311
|
+
}
|
|
312
|
+
async requests() {
|
|
313
|
+
return parse(z.object({ data: z.array(key_request_schema) }), await this.call("GET", "/requests")).data;
|
|
314
|
+
}
|
|
315
|
+
async ask(commitment, label) {
|
|
316
|
+
return parse(z.object({ data: z.object({ id: z.string(), expires_at: z.string() }) }), await this.call("POST", "/requests", { commitment, label })).data;
|
|
317
|
+
}
|
|
318
|
+
async answer_of(request_id) {
|
|
319
|
+
return parse(z.object({
|
|
320
|
+
data: z.object({
|
|
321
|
+
status: z.string(),
|
|
322
|
+
approver_key: z.string().optional(),
|
|
323
|
+
wrapped: z.string().optional(),
|
|
324
|
+
}),
|
|
325
|
+
}), await this.call("GET", `/requests/${encodeURIComponent(request_id)}`)).data;
|
|
326
|
+
}
|
|
327
|
+
async reveal(request_id, public_key, nonce) {
|
|
328
|
+
await this.call("POST", `/requests/${encodeURIComponent(request_id)}/reveal`, { public_key, nonce });
|
|
329
|
+
}
|
|
330
|
+
async offer(request_id, approver_key) {
|
|
331
|
+
await this.call("POST", `/requests/${encodeURIComponent(request_id)}/offer`, { approver_key });
|
|
332
|
+
}
|
|
333
|
+
async request(request_id) {
|
|
334
|
+
return (await this.requests()).find((entry) => entry.id === request_id) ?? null;
|
|
335
|
+
}
|
|
336
|
+
async answer(request_id, wrapped) {
|
|
337
|
+
await this.call("POST", `/requests/${encodeURIComponent(request_id)}`, { wrapped });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
190
340
|
export async function list_vaults(transport) {
|
|
191
341
|
const response = await transport("GET", "/api/v1/vaults", {});
|
|
192
342
|
if (!response.ok)
|
|
193
343
|
throw await error_of(response);
|
|
194
344
|
return parse(z.object({ data: z.array(vault_schema) }), await response.json()).data;
|
|
195
345
|
}
|
|
196
|
-
export async function create_vault(transport, name) {
|
|
346
|
+
export async function create_vault(transport, name, privacy) {
|
|
197
347
|
const response = await transport("POST", "/api/v1/vaults", {
|
|
198
348
|
headers: { "content-type": "application/json" },
|
|
199
|
-
body: JSON.stringify({ name }),
|
|
349
|
+
body: JSON.stringify(privacy === undefined ? { name } : { name, id: privacy.id, mode: "private", locks: privacy.locks }),
|
|
200
350
|
});
|
|
201
351
|
if (!response.ok)
|
|
202
352
|
throw await error_of(response);
|
package/dist/vault/connect.js
CHANGED
|
@@ -4,6 +4,9 @@ import { CliError } from "../errors.js";
|
|
|
4
4
|
import { send } from "../http.js";
|
|
5
5
|
import { VERSION } from "../version.js";
|
|
6
6
|
import { VaultApi } from "./api.js";
|
|
7
|
+
import { vault_keys } from "./private/crypto.js";
|
|
8
|
+
import { PrivateVaultApi } from "./private/api.js";
|
|
9
|
+
import { load_vault_key } from "./private/keystore.js";
|
|
7
10
|
import { state_path_for } from "./state.js";
|
|
8
11
|
export function cli_transport(workspace) {
|
|
9
12
|
return async (method, path, options) => {
|
|
@@ -34,8 +37,23 @@ export async function open_linked(marker) {
|
|
|
34
37
|
credentials.workspace.id !== marker.workspace_id) {
|
|
35
38
|
throw new CliError(`This folder belongs to another workspace than the API key you signed in with (${credentials.workspace.name}). Nothing was synced.`, "workspace_mismatch");
|
|
36
39
|
}
|
|
40
|
+
const api = new VaultApi(cli_transport(marker.workspace_id), marker.vault_id, client_label());
|
|
41
|
+
const info = await api.info();
|
|
42
|
+
const vault_key = await load_vault_key(marker.vault_id);
|
|
43
|
+
const is_pinned_private = marker.mode === "private" || vault_key !== null;
|
|
44
|
+
const says_private = info.data.mode === "private";
|
|
45
|
+
if (is_pinned_private !== says_private && (marker.mode !== undefined || vault_key !== null)) {
|
|
46
|
+
throw new CliError(`Nemu now reports "${marker.vault_name}" as ${says_private ? "private" : "not private"}, but it was ${is_pinned_private ? "private" : "not private"} when this folder was linked. A vault never changes this, so nothing was synced. Check the vault in the console.`, "vault_mode_changed");
|
|
47
|
+
}
|
|
48
|
+
if (!says_private) {
|
|
49
|
+
return { api, state_path: state_path_for(marker.vault_id), is_private: false };
|
|
50
|
+
}
|
|
51
|
+
if (vault_key === null) {
|
|
52
|
+
throw new CliError(`"${marker.vault_name}" is a private vault and is locked on this computer. Run nemu vault unlock first. Nothing was synced.`, "vault_locked");
|
|
53
|
+
}
|
|
37
54
|
return {
|
|
38
|
-
api: new
|
|
55
|
+
api: new PrivateVaultApi(api, vault_keys(marker.vault_id, vault_key)),
|
|
39
56
|
state_path: state_path_for(marker.vault_id),
|
|
57
|
+
is_private: true,
|
|
40
58
|
};
|
|
41
59
|
}
|
package/dist/vault/engine.js
CHANGED
|
@@ -3,7 +3,7 @@ import { readFile } from "node:fs/promises";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { CliError } from "../errors.js";
|
|
5
5
|
import { RemoteError } from "./api.js";
|
|
6
|
-
import { delete_local,
|
|
6
|
+
import { delete_local, read_if, rename_local, scan_folder, write_atomic_file, } from "./files.js";
|
|
7
7
|
import { ignore_fingerprint, is_ignored, parse_ignore } from "./ignore.js";
|
|
8
8
|
import { acquire_lock } from "./lock.js";
|
|
9
9
|
import { merge_text } from "./merge.js";
|
|
@@ -108,7 +108,11 @@ function linked_groups(actions) {
|
|
|
108
108
|
}
|
|
109
109
|
async function load_ignore(root, settings) {
|
|
110
110
|
const source = await readFile(join(root, ".nemuignore"), "utf-8").catch(() => "");
|
|
111
|
-
const options = {
|
|
111
|
+
const options = {
|
|
112
|
+
obsidian: settings.obsidian,
|
|
113
|
+
obsidian_plugins: settings.obsidian_plugins,
|
|
114
|
+
rules: parse_ignore(source),
|
|
115
|
+
};
|
|
112
116
|
return { options, fingerprint: ignore_fingerprint(options, source) };
|
|
113
117
|
}
|
|
114
118
|
function entry_of(note) {
|
|
@@ -207,6 +211,7 @@ export async function run_sync(options) {
|
|
|
207
211
|
}
|
|
208
212
|
}
|
|
209
213
|
async function sync_locked(options) {
|
|
214
|
+
const hasher = (content) => options.api.hash(content);
|
|
210
215
|
const dry_run = options.dry_run === true;
|
|
211
216
|
const report = new_report(options.api.vault_id, dry_run);
|
|
212
217
|
const emit = (event) => options.on_event?.(event);
|
|
@@ -221,6 +226,9 @@ async function sync_locked(options) {
|
|
|
221
226
|
state.settings.conflicts = options.settings.conflicts;
|
|
222
227
|
if (options.settings?.obsidian !== undefined)
|
|
223
228
|
state.settings.obsidian = options.settings.obsidian;
|
|
229
|
+
if (options.settings?.obsidian_plugins !== undefined) {
|
|
230
|
+
state.settings.obsidian_plugins = options.settings.obsidian_plugins;
|
|
231
|
+
}
|
|
224
232
|
const ignore = await load_ignore(options.root, state.settings);
|
|
225
233
|
if (ignore.fingerprint !== state.ignore_fingerprint)
|
|
226
234
|
state.full_next = true;
|
|
@@ -240,7 +248,9 @@ async function sync_locked(options) {
|
|
|
240
248
|
}
|
|
241
249
|
if (info.limits !== undefined)
|
|
242
250
|
state.limits = info.limits;
|
|
243
|
-
const max_file_bytes = state.limits?.
|
|
251
|
+
const max_file_bytes = state.limits?.part_bytes !== undefined && state.limits.max_upload_file_bytes !== undefined
|
|
252
|
+
? Math.max(state.limits.max_file_bytes, state.limits.max_upload_file_bytes)
|
|
253
|
+
: (state.limits?.max_file_bytes ?? DEFAULT_MAX_FILE_BYTES);
|
|
244
254
|
const max_cycles = options.max_cycles ?? 4;
|
|
245
255
|
for (let cycle = 1; cycle <= max_cycles; cycle += 1) {
|
|
246
256
|
report.cycles = cycle;
|
|
@@ -269,7 +279,7 @@ async function sync_locked(options) {
|
|
|
269
279
|
continue;
|
|
270
280
|
base.set(path_key(entry.path), entry);
|
|
271
281
|
}
|
|
272
|
-
const scan = await scan_folder(options.root, ignore.options, max_file_bytes, (key) => base.get(key)?.path);
|
|
282
|
+
const scan = await scan_folder(options.root, ignore.options, max_file_bytes, (key) => base.get(key)?.path, hasher);
|
|
273
283
|
if (cycle === 1) {
|
|
274
284
|
report.skipped.push(...scan.skipped);
|
|
275
285
|
for (const skipped of scan.skipped)
|
|
@@ -332,7 +342,7 @@ async function sync_locked(options) {
|
|
|
332
342
|
const ready_at = scan_started + quiet_ms;
|
|
333
343
|
if (Date.now() < ready_at)
|
|
334
344
|
await wait(ready_at - Date.now());
|
|
335
|
-
const content = await read_if(options.root, local.disk, local.hash);
|
|
345
|
+
const content = await read_if(options.root, local.disk, local.hash, hasher);
|
|
336
346
|
if (content === null) {
|
|
337
347
|
if (!report.waiting.includes(local.path))
|
|
338
348
|
report.waiting.push(local.path);
|
|
@@ -363,7 +373,7 @@ async function sync_locked(options) {
|
|
|
363
373
|
catch (error) {
|
|
364
374
|
if (error instanceof RemoteError && error.status === 409) {
|
|
365
375
|
const current = error.current;
|
|
366
|
-
if (current !== null && current !== undefined && !current.deleted && current.hash ===
|
|
376
|
+
if (current !== null && current !== undefined && !current.deleted && current.hash === hasher(content)) {
|
|
367
377
|
record_note(current);
|
|
368
378
|
return current;
|
|
369
379
|
}
|
|
@@ -387,7 +397,7 @@ async function sync_locked(options) {
|
|
|
387
397
|
const fetch_remote = async (path, note_id) => {
|
|
388
398
|
try {
|
|
389
399
|
const fetched = await options.api.read(path);
|
|
390
|
-
if (fetched.id !== note_id ||
|
|
400
|
+
if (fetched.id !== note_id || hasher(fetched.content) !== fetched.hash) {
|
|
391
401
|
stale = true;
|
|
392
402
|
throw new GroupFailed(true);
|
|
393
403
|
}
|
|
@@ -402,13 +412,10 @@ async function sync_locked(options) {
|
|
|
402
412
|
}
|
|
403
413
|
};
|
|
404
414
|
const keep_both = async (remote_entry, local, copy_path, local_content, remote_content) => {
|
|
405
|
-
if (!(await write_atomic_file(options.root, copy_path, local_content, null)))
|
|
415
|
+
if (!(await write_atomic_file(options.root, copy_path, local_content, null, hasher)))
|
|
406
416
|
throw new GroupFailed(true);
|
|
407
417
|
const fetched = remote_content ?? (await fetch_remote(remote_entry.path, remote_entry.note_id));
|
|
408
|
-
if (!(await write_atomic_file(options.root, remote_entry.path, fetched.content, {
|
|
409
|
-
disk: local.disk,
|
|
410
|
-
hash: local.hash,
|
|
411
|
-
}))) {
|
|
418
|
+
if (!(await write_atomic_file(options.root, remote_entry.path, fetched.content, { disk: local.disk, hash: local.hash }, hasher))) {
|
|
412
419
|
throw new GroupFailed(true);
|
|
413
420
|
}
|
|
414
421
|
record({ path: remote_entry.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
|
|
@@ -425,7 +432,7 @@ async function sync_locked(options) {
|
|
|
425
432
|
const execute = async (action) => {
|
|
426
433
|
switch (action.kind) {
|
|
427
434
|
case "delete_local": {
|
|
428
|
-
if (!(await delete_local(options.root, action.disk, action.hash)))
|
|
435
|
+
if (!(await delete_local(options.root, action.disk, action.hash, hasher)))
|
|
429
436
|
throw new GroupFailed(true);
|
|
430
437
|
report.deleted_local.push(action.disk);
|
|
431
438
|
emit({ kind: "deleted_local", path: action.disk });
|
|
@@ -472,7 +479,7 @@ async function sync_locked(options) {
|
|
|
472
479
|
}
|
|
473
480
|
case "pull": {
|
|
474
481
|
const fetched = await fetch_remote(action.path, action.note_id);
|
|
475
|
-
const written = await write_atomic_file(options.root, action.path, fetched.content, action.replace === null ? null : { disk: action.replace.disk, hash: action.replace.hash });
|
|
482
|
+
const written = await write_atomic_file(options.root, action.path, fetched.content, action.replace === null ? null : { disk: action.replace.disk, hash: action.replace.hash }, hasher);
|
|
476
483
|
if (!written)
|
|
477
484
|
throw new GroupFailed(true);
|
|
478
485
|
record({ path: action.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
|
|
@@ -518,7 +525,7 @@ async function sync_locked(options) {
|
|
|
518
525
|
throw error;
|
|
519
526
|
}
|
|
520
527
|
const texts = [base_content, local_content, fetched.content].map((value) => value === null ? null : decode(value));
|
|
521
|
-
const merged = base_content !== null &&
|
|
528
|
+
const merged = base_content !== null && hasher(base_content) === action.base.hash && texts.every((text) => text !== null)
|
|
522
529
|
? merge_text(texts[0], texts[1], texts[2])
|
|
523
530
|
: { kind: "conflict" };
|
|
524
531
|
if (merged.kind === "conflict") {
|
|
@@ -528,14 +535,11 @@ async function sync_locked(options) {
|
|
|
528
535
|
return;
|
|
529
536
|
}
|
|
530
537
|
const bytes = Buffer.from(merged.text, "utf-8");
|
|
531
|
-
if (!(await write_atomic_file(options.root, action.remote.path, bytes, {
|
|
532
|
-
disk: action.local.disk,
|
|
533
|
-
hash: action.local.hash,
|
|
534
|
-
}))) {
|
|
538
|
+
if (!(await write_atomic_file(options.root, action.remote.path, bytes, { disk: action.local.disk, hash: action.local.hash }, hasher))) {
|
|
535
539
|
throw new GroupFailed(true);
|
|
536
540
|
}
|
|
537
541
|
known.set(fetched.id, fetched.version);
|
|
538
|
-
if (
|
|
542
|
+
if (hasher(bytes) === fetched.hash) {
|
|
539
543
|
record({ path: action.remote.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
|
|
540
544
|
}
|
|
541
545
|
else {
|