nemu-cli 1.4.0 → 1.6.1
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 +54 -0
- package/LICENSE +0 -0
- package/README.md +38 -0
- package/dist/args.js +9 -1
- package/dist/auth/token.js +1 -1
- package/dist/commands/key.js +95 -0
- package/dist/commands/login.js +3 -0
- package/dist/commands/mcp.js +2 -2
- package/dist/commands/sync.js +197 -0
- package/dist/commands/vault.js +189 -0
- package/dist/commands/whoami.js +19 -0
- package/dist/config.js +5 -0
- package/dist/context/api.js +9 -9
- package/dist/device.js +1 -1
- package/dist/http.js +36 -19
- package/dist/index.js +14 -0
- package/dist/ui.js +35 -0
- package/dist/vault/api.js +204 -0
- package/dist/vault/connect.js +41 -0
- package/dist/vault/engine.js +634 -0
- package/dist/vault/files.js +187 -0
- package/dist/vault/ignore.js +100 -0
- package/dist/vault/lock.js +85 -0
- package/dist/vault/merge.js +236 -0
- package/dist/vault/paths.js +99 -0
- package/dist/vault/plan.js +279 -0
- package/dist/vault/state.js +112 -0
- package/dist/vault/watch.js +101 -0
- package/dist/version.js +1 -1
- package/package.json +4 -2
package/dist/commands/whoami.js
CHANGED
|
@@ -2,6 +2,7 @@ import { is_expired, read_credentials } from "../config.js";
|
|
|
2
2
|
import { client } from "../session.js";
|
|
3
3
|
import { detail, is_json_mode } from "../output.js";
|
|
4
4
|
import { bold, dim, out } from "../ui.js";
|
|
5
|
+
import { describe_key } from "./key.js";
|
|
5
6
|
export async function whoami() {
|
|
6
7
|
const credentials = await read_credentials();
|
|
7
8
|
if (credentials === null) {
|
|
@@ -16,6 +17,24 @@ export async function whoami() {
|
|
|
16
17
|
out("Your session has expired. Run nemu login.");
|
|
17
18
|
return 1;
|
|
18
19
|
}
|
|
20
|
+
if (credentials.kind === "key") {
|
|
21
|
+
const key = await describe_key(credentials.api_url, credentials.access_token);
|
|
22
|
+
if (is_json_mode()) {
|
|
23
|
+
detail({
|
|
24
|
+
signed_in: true,
|
|
25
|
+
kind: "key",
|
|
26
|
+
key: key.name,
|
|
27
|
+
workspace: key.workspace,
|
|
28
|
+
scopes: key.scopes,
|
|
29
|
+
api_url: credentials.api_url,
|
|
30
|
+
});
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
out(`${bold(`key ${key.name}`)} ${dim(`in ${key.workspace.name}`)}`);
|
|
34
|
+
out(dim(` ${credentials.api_url}`));
|
|
35
|
+
out(dim(` access ${key.scopes.join(", ") || "none"}`));
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
19
38
|
const nemu = await client();
|
|
20
39
|
const account = await nemu.user.get();
|
|
21
40
|
if (is_json_mode()) {
|
package/dist/config.js
CHANGED
|
@@ -6,12 +6,16 @@ export const DEFAULT_API_URL = "https://app.nemu.cc";
|
|
|
6
6
|
export const DEFAULT_CLIENT_ID = "nemu_cli";
|
|
7
7
|
export const API_VERSION = "2026-07-14";
|
|
8
8
|
const credentials_schema = z.object({
|
|
9
|
+
kind: z.enum(["oauth", "key"]).default("oauth"),
|
|
9
10
|
access_token: z.string().min(1),
|
|
10
11
|
refresh_token: z.string().min(1).optional(),
|
|
11
12
|
expires_at: z.number(),
|
|
12
13
|
scope: z.string(),
|
|
13
14
|
api_url: z.string().url(),
|
|
14
15
|
account: z.string().optional(),
|
|
16
|
+
workspace: z
|
|
17
|
+
.object({ id: z.string().min(1), name: z.string() })
|
|
18
|
+
.optional(),
|
|
15
19
|
});
|
|
16
20
|
const settings_schema = z.object({
|
|
17
21
|
device_id: z
|
|
@@ -25,6 +29,7 @@ const settings_schema = z.object({
|
|
|
25
29
|
update_declined_version: z.string().max(64).optional(),
|
|
26
30
|
update_declined_at: z.number().optional(),
|
|
27
31
|
setup_version: z.string().max(64).optional(),
|
|
32
|
+
context_workspace: z.string().min(1).max(64).optional(),
|
|
28
33
|
});
|
|
29
34
|
export function api_url() {
|
|
30
35
|
return (process.env.NEMU_API_URL ?? DEFAULT_API_URL).replace(/\/+$/, "");
|
package/dist/context/api.js
CHANGED
|
@@ -52,23 +52,23 @@ const device_schema = z.object({
|
|
|
52
52
|
name: z.string(),
|
|
53
53
|
});
|
|
54
54
|
export async function open_project(identity) {
|
|
55
|
-
const result = await api("POST", "/api/v1/
|
|
55
|
+
const result = await api("POST", "/api/v1/projects", z.object({ data: project_schema }), { body: identity });
|
|
56
56
|
return result.data;
|
|
57
57
|
}
|
|
58
58
|
export async function list_projects() {
|
|
59
|
-
const result = await api("GET", "/api/v1/
|
|
59
|
+
const result = await api("GET", "/api/v1/projects", z.object({ data: z.array(project_schema) }));
|
|
60
60
|
return result.data;
|
|
61
61
|
}
|
|
62
62
|
export async function load_project(project_id, branch) {
|
|
63
|
-
const result = await api("GET", `/api/v1/
|
|
63
|
+
const result = await api("GET", `/api/v1/projects/${encodeURIComponent(project_id)}/load`, z.object({ data: load_schema }), { query: { branch } });
|
|
64
64
|
return result.data;
|
|
65
65
|
}
|
|
66
66
|
export async function search_project(project_id, query, limit = 10) {
|
|
67
|
-
const result = await api("GET", `/api/v1/
|
|
67
|
+
const result = await api("GET", `/api/v1/projects/${encodeURIComponent(project_id)}/search`, z.object({ data: z.array(entry_schema) }), { query: { q: query, limit } });
|
|
68
68
|
return result.data;
|
|
69
69
|
}
|
|
70
70
|
export async function write_batch(project_id, entries) {
|
|
71
|
-
const result = await api("POST", `/api/v1/
|
|
71
|
+
const result = await api("POST", `/api/v1/projects/${encodeURIComponent(project_id)}/entries/batch`, z.object({
|
|
72
72
|
data: z.array(z.object({
|
|
73
73
|
name: z.string(),
|
|
74
74
|
status: z.enum(["created", "updated", "skipped"]),
|
|
@@ -79,21 +79,21 @@ export async function write_batch(project_id, entries) {
|
|
|
79
79
|
return result.data;
|
|
80
80
|
}
|
|
81
81
|
export async function register_device(input) {
|
|
82
|
-
const result = await api("POST", "/api/v1/
|
|
82
|
+
const result = await api("POST", "/api/v1/devices", z.object({ data: device_schema }), { body: input, device: false });
|
|
83
83
|
return result.data;
|
|
84
84
|
}
|
|
85
85
|
export async function mark_stale(project_id, stale, fresh) {
|
|
86
86
|
if (stale.length === 0 && fresh.length === 0)
|
|
87
87
|
return;
|
|
88
|
-
await api("POST", `/api/v1/
|
|
88
|
+
await api("POST", `/api/v1/projects/${encodeURIComponent(project_id)}/stale`, z.object({ marked: z.number(), cleared: z.number() }), { body: { stale: stale.slice(0, 400), fresh: fresh.slice(0, 400) } });
|
|
89
89
|
}
|
|
90
90
|
export async function list_devices() {
|
|
91
|
-
const result = await api("GET", "/api/v1/
|
|
91
|
+
const result = await api("GET", "/api/v1/devices", z.object({
|
|
92
92
|
data: z.array(z.object({ id: z.string(), name: z.string(), revoked_at: z.string().nullable() })),
|
|
93
93
|
}), { device: false });
|
|
94
94
|
return result.data;
|
|
95
95
|
}
|
|
96
96
|
export async function export_all() {
|
|
97
|
-
const result = await api("GET", "/api/v1/
|
|
97
|
+
const result = await api("GET", "/api/v1/projects/export", z.object({ data: z.unknown() }));
|
|
98
98
|
return result.data;
|
|
99
99
|
}
|
package/dist/device.js
CHANGED
|
@@ -3,7 +3,7 @@ import { read_credentials, read_settings, update_settings } from "./config.js";
|
|
|
3
3
|
import { list_devices, register_device } from "./context/api.js";
|
|
4
4
|
import { VERSION } from "./version.js";
|
|
5
5
|
const PLATFORMS = new Set(["darwin", "linux", "win32", "freebsd"]);
|
|
6
|
-
function device_name() {
|
|
6
|
+
export function device_name() {
|
|
7
7
|
const raw = hostname().replace(/\.local$/, "").replace(/[^\w .-]/g, "").trim();
|
|
8
8
|
return (raw.length > 0 ? raw : "This computer").slice(0, 64);
|
|
9
9
|
}
|
package/dist/http.js
CHANGED
|
@@ -8,7 +8,7 @@ const error_body = z.object({
|
|
|
8
8
|
code: z.string().optional(),
|
|
9
9
|
reference: z.string().optional(),
|
|
10
10
|
});
|
|
11
|
-
export async function
|
|
11
|
+
export async function send(method, path, options = {}) {
|
|
12
12
|
const credentials = await fresh_credentials();
|
|
13
13
|
const settings = await read_settings();
|
|
14
14
|
const url = new URL(`${credentials.api_url}${path}`);
|
|
@@ -20,39 +20,56 @@ export async function api(method, path, schema, options = {}) {
|
|
|
20
20
|
authorization: `Bearer ${credentials.access_token}`,
|
|
21
21
|
"nemu-api-version": API_VERSION,
|
|
22
22
|
"user-agent": `nemu-cli/${VERSION}`,
|
|
23
|
+
...(options.headers ?? {}),
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const is_key = credentials.kind === "key";
|
|
26
|
+
if (!is_key &&
|
|
27
|
+
options.device !== false &&
|
|
27
28
|
settings.device_id !== undefined &&
|
|
28
29
|
settings.device_api_url === credentials.api_url) {
|
|
29
30
|
headers["x-nemu-device"] = settings.device_id;
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
const workspace = options.workspace ?? settings.context_workspace;
|
|
33
|
+
if (!is_key && workspace !== undefined) {
|
|
34
|
+
headers["x-nemu-workspace"] = workspace;
|
|
35
|
+
}
|
|
32
36
|
try {
|
|
33
|
-
response = await fetch(url, {
|
|
37
|
+
const response = await fetch(url, {
|
|
34
38
|
method,
|
|
35
39
|
headers,
|
|
36
|
-
body: options.body
|
|
40
|
+
body: options.body,
|
|
37
41
|
signal: AbortSignal.timeout(options.timeout_ms ?? 20_000),
|
|
38
42
|
});
|
|
43
|
+
return { api_url: credentials.api_url, response };
|
|
39
44
|
}
|
|
40
45
|
catch {
|
|
41
46
|
throw new CliError(`Could not reach ${credentials.api_url}.`, "network_failed");
|
|
42
47
|
}
|
|
48
|
+
}
|
|
49
|
+
export async function failure_of(response) {
|
|
50
|
+
const payload = await response.json().catch(() => null);
|
|
51
|
+
const failure = error_body.safeParse(payload);
|
|
52
|
+
const message = failure.success && failure.data.error !== undefined
|
|
53
|
+
? failure.data.error
|
|
54
|
+
: `Request failed with status ${response.status}`;
|
|
55
|
+
return new CliError(message, failure.success && failure.data.code !== undefined ? failure.data.code : `http_${response.status}`, {
|
|
56
|
+
status: response.status,
|
|
57
|
+
reference: (failure.success ? failure.data.reference : undefined) ??
|
|
58
|
+
response.headers.get("x-nemu-reference") ??
|
|
59
|
+
undefined,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export async function api(method, path, schema, options = {}) {
|
|
63
|
+
const { response } = await send(method, path, {
|
|
64
|
+
query: options.query,
|
|
65
|
+
headers: options.body !== undefined ? { "content-type": "application/json" } : {},
|
|
66
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
67
|
+
device: options.device,
|
|
68
|
+
timeout_ms: options.timeout_ms,
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw await failure_of(response);
|
|
43
72
|
const payload = await response.json().catch(() => null);
|
|
44
|
-
if (!response.ok) {
|
|
45
|
-
const failure = error_body.safeParse(payload);
|
|
46
|
-
const message = failure.success && failure.data.error !== undefined
|
|
47
|
-
? failure.data.error
|
|
48
|
-
: `Request failed with status ${response.status}`;
|
|
49
|
-
throw new CliError(message, failure.success && failure.data.code !== undefined ? failure.data.code : `http_${response.status}`, {
|
|
50
|
-
status: response.status,
|
|
51
|
-
reference: (failure.success ? failure.data.reference : undefined) ??
|
|
52
|
-
response.headers.get("x-nemu-reference") ??
|
|
53
|
-
undefined,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
73
|
const parsed = schema.safeParse(payload);
|
|
57
74
|
if (!parsed.success) {
|
|
58
75
|
throw new CliError("Nemu sent an answer this CLI does not understand. Update nemu-cli.", "invalid_response");
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,8 @@ import { send_report } from "./report.js";
|
|
|
20
20
|
import { maybe_offer_update, update_command } from "./update.js";
|
|
21
21
|
import { migrate_after_update } from "./migrations.js";
|
|
22
22
|
import { repair } from "./commands/repair.js";
|
|
23
|
+
import { vault } from "./commands/vault.js";
|
|
24
|
+
import { sync } from "./commands/sync.js";
|
|
23
25
|
import { CliError } from "./errors.js";
|
|
24
26
|
import { VERSION } from "./version.js";
|
|
25
27
|
import { bold, dim, fail, out } from "./ui.js";
|
|
@@ -57,6 +59,8 @@ const COMMANDS = {
|
|
|
57
59
|
config: config_command,
|
|
58
60
|
update: update_command,
|
|
59
61
|
repair,
|
|
62
|
+
vault,
|
|
63
|
+
sync,
|
|
60
64
|
};
|
|
61
65
|
const HELP = `
|
|
62
66
|
${bold("nemu")} ${dim(VERSION)}
|
|
@@ -85,8 +89,18 @@ const HELP = `
|
|
|
85
89
|
update [--yes] Check for a new version and install it
|
|
86
90
|
repair Bring hooks and editor entries up to date
|
|
87
91
|
|
|
92
|
+
${bold("Vaults")}
|
|
93
|
+
vault list Vaults in this workspace
|
|
94
|
+
vault link <folder> [--vault <name|id>] [--create] [--obsidian]
|
|
95
|
+
Link a folder to a vault (writes <folder>/.nemu/vault.json)
|
|
96
|
+
vault unlink [folder] Stop syncing a folder; its files stay
|
|
97
|
+
vault status [folder] Last sync, settings and open conflicts
|
|
98
|
+
sync [folder] [--watch] [--dry-run] [--conflicts=merge|file] [--allow-deletes]
|
|
99
|
+
Sync a linked folder with its vault
|
|
100
|
+
|
|
88
101
|
${bold("Account")}
|
|
89
102
|
login [--force] [--no-browser] Sign in with the device flow
|
|
103
|
+
login key Sign in with a workspace API key (asked for, or NEMU_API_KEY)
|
|
90
104
|
logout Delete the stored token
|
|
91
105
|
whoami Show the signed in account
|
|
92
106
|
user Full account record
|
package/dist/ui.js
CHANGED
|
@@ -93,3 +93,38 @@ export async function confirm(question, fallback) {
|
|
|
93
93
|
reader.close();
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
+
export async function ask_secret(question) {
|
|
97
|
+
const input = process.stdin;
|
|
98
|
+
process.stdout.write(` ${question} `);
|
|
99
|
+
input.setRawMode(true);
|
|
100
|
+
input.resume();
|
|
101
|
+
input.setEncoding("utf8");
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
let value = "";
|
|
104
|
+
const finish = (error) => {
|
|
105
|
+
input.setRawMode(false);
|
|
106
|
+
input.pause();
|
|
107
|
+
input.removeListener("data", on_data);
|
|
108
|
+
process.stdout.write("\n");
|
|
109
|
+
if (error === null)
|
|
110
|
+
resolve(value);
|
|
111
|
+
else
|
|
112
|
+
reject(error);
|
|
113
|
+
};
|
|
114
|
+
const on_data = (chunk) => {
|
|
115
|
+
for (const character of chunk) {
|
|
116
|
+
if (character === "\r" || character === "\n")
|
|
117
|
+
return finish(null);
|
|
118
|
+
if (character === "\u0003")
|
|
119
|
+
return finish(new Error("Cancelled."));
|
|
120
|
+
if (character === "\u007f" || character === "\b") {
|
|
121
|
+
value = value.slice(0, -1);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (character >= " ")
|
|
125
|
+
value += character;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
input.on("data", on_data);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { CliError } from "../errors.js";
|
|
3
|
+
const note_schema = z.object({
|
|
4
|
+
id: z.string(),
|
|
5
|
+
path: z.string(),
|
|
6
|
+
kind: z.string(),
|
|
7
|
+
version: z.number().int(),
|
|
8
|
+
hash: z.string(),
|
|
9
|
+
size: z.number(),
|
|
10
|
+
revision: z.number(),
|
|
11
|
+
deleted: z.boolean(),
|
|
12
|
+
});
|
|
13
|
+
const vault_schema = z.object({
|
|
14
|
+
id: z.string(),
|
|
15
|
+
name: z.string(),
|
|
16
|
+
revision: z.number(),
|
|
17
|
+
notes: z.number().optional(),
|
|
18
|
+
stored_bytes: z.number().optional(),
|
|
19
|
+
created_at: z.string().optional(),
|
|
20
|
+
updated_at: z.string().optional(),
|
|
21
|
+
});
|
|
22
|
+
const info_schema = z.object({
|
|
23
|
+
data: vault_schema,
|
|
24
|
+
workspace_id: z.string().optional(),
|
|
25
|
+
limits: z
|
|
26
|
+
.object({
|
|
27
|
+
max_file_bytes: z.number(),
|
|
28
|
+
max_storage_bytes: z.number().nullable(),
|
|
29
|
+
})
|
|
30
|
+
.optional(),
|
|
31
|
+
});
|
|
32
|
+
const version_schema = z.object({
|
|
33
|
+
version: z.number().int(),
|
|
34
|
+
path: z.string(),
|
|
35
|
+
hash: z.string(),
|
|
36
|
+
size: z.number(),
|
|
37
|
+
deleted: z.boolean(),
|
|
38
|
+
origin: z.string(),
|
|
39
|
+
origin_id: z.string().nullable(),
|
|
40
|
+
client: z.string().nullable(),
|
|
41
|
+
created_at: z.string(),
|
|
42
|
+
});
|
|
43
|
+
const failure_schema = z.object({
|
|
44
|
+
error: z.string().optional(),
|
|
45
|
+
code: z.string().optional(),
|
|
46
|
+
reference: z.string().optional(),
|
|
47
|
+
retry_after: z.number().optional(),
|
|
48
|
+
current: note_schema.nullable().optional(),
|
|
49
|
+
});
|
|
50
|
+
export class RemoteError extends CliError {
|
|
51
|
+
current;
|
|
52
|
+
constructor(message, code, status, current, reference) {
|
|
53
|
+
super(message, code, { status, reference });
|
|
54
|
+
this.name = "RemoteError";
|
|
55
|
+
this.current = current;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const MAX_WAIT_MS = 60_000;
|
|
59
|
+
const ATTEMPTS = 4;
|
|
60
|
+
function wait(ms) {
|
|
61
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
62
|
+
}
|
|
63
|
+
async function error_of(response) {
|
|
64
|
+
const parsed = failure_schema.safeParse(await response.json().catch(() => null));
|
|
65
|
+
const body = parsed.success ? parsed.data : {};
|
|
66
|
+
return new RemoteError(body.error ?? `Request failed with status ${response.status}`, body.code ?? `http_${response.status}`, response.status, body.current, body.reference ?? response.headers.get("x-nemu-reference") ?? undefined);
|
|
67
|
+
}
|
|
68
|
+
function retry_delay(response, attempt) {
|
|
69
|
+
const header = Number(response.headers.get("retry-after") ?? "");
|
|
70
|
+
const seconds = Number.isFinite(header) && header > 0 ? header : 2 ** attempt;
|
|
71
|
+
return Math.min(MAX_WAIT_MS, seconds * 1000);
|
|
72
|
+
}
|
|
73
|
+
function parse(schema, value) {
|
|
74
|
+
const parsed = schema.safeParse(value);
|
|
75
|
+
if (!parsed.success) {
|
|
76
|
+
throw new CliError("Nemu sent an answer this CLI does not understand. Update nemu-cli.", "invalid_response");
|
|
77
|
+
}
|
|
78
|
+
return parsed.data;
|
|
79
|
+
}
|
|
80
|
+
export class VaultApi {
|
|
81
|
+
transport;
|
|
82
|
+
vault_id;
|
|
83
|
+
client_label;
|
|
84
|
+
constructor(transport, vault_id, client_label) {
|
|
85
|
+
this.transport = transport;
|
|
86
|
+
this.vault_id = vault_id;
|
|
87
|
+
this.client_label = client_label;
|
|
88
|
+
}
|
|
89
|
+
async call(method, path, options = {}) {
|
|
90
|
+
for (let attempt = 1;; attempt += 1) {
|
|
91
|
+
const response = await this.transport(method, path, options);
|
|
92
|
+
if (response.status !== 429 || attempt >= ATTEMPTS) {
|
|
93
|
+
if (!response.ok)
|
|
94
|
+
throw await error_of(response);
|
|
95
|
+
return response;
|
|
96
|
+
}
|
|
97
|
+
await response.body?.cancel().catch(() => undefined);
|
|
98
|
+
await wait(retry_delay(response, attempt));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
base() {
|
|
102
|
+
return `/api/v1/vaults/${encodeURIComponent(this.vault_id)}`;
|
|
103
|
+
}
|
|
104
|
+
async info() {
|
|
105
|
+
const response = await this.call("GET", this.base());
|
|
106
|
+
return parse(info_schema, await response.json());
|
|
107
|
+
}
|
|
108
|
+
async notes(trash = false) {
|
|
109
|
+
const response = await this.call("GET", `${this.base()}/notes`, {
|
|
110
|
+
query: trash ? { trash: "true" } : {},
|
|
111
|
+
});
|
|
112
|
+
const body = parse(z.object({ vault: vault_schema, data: z.array(note_schema) }), await response.json());
|
|
113
|
+
return { revision: body.vault.revision, notes: body.data };
|
|
114
|
+
}
|
|
115
|
+
async changes(since) {
|
|
116
|
+
const response = await this.call("GET", `${this.base()}/changes`, {
|
|
117
|
+
query: { since },
|
|
118
|
+
});
|
|
119
|
+
const body = parse(z.object({ data: z.array(note_schema), revision: z.number(), has_more: z.boolean() }), await response.json());
|
|
120
|
+
return { notes: body.data, revision: body.revision, has_more: body.has_more };
|
|
121
|
+
}
|
|
122
|
+
async read(path) {
|
|
123
|
+
const response = await this.call("GET", `${this.base()}/files`, {
|
|
124
|
+
query: { path },
|
|
125
|
+
timeout_ms: 120_000,
|
|
126
|
+
});
|
|
127
|
+
const content = Buffer.from(await response.arrayBuffer());
|
|
128
|
+
const id = response.headers.get("x-nemu-note-id");
|
|
129
|
+
const version = Number(response.headers.get("x-nemu-version"));
|
|
130
|
+
const hash = response.headers.get("x-nemu-hash");
|
|
131
|
+
if (id === null || hash === null || !Number.isInteger(version)) {
|
|
132
|
+
throw new CliError("Nemu sent a file without its version. Update nemu-cli.", "invalid_response");
|
|
133
|
+
}
|
|
134
|
+
return { content, id, version, hash };
|
|
135
|
+
}
|
|
136
|
+
async write(path, content, base_version) {
|
|
137
|
+
const response = await this.call("PUT", `${this.base()}/files`, {
|
|
138
|
+
query: { path },
|
|
139
|
+
headers: {
|
|
140
|
+
"content-type": "application/octet-stream",
|
|
141
|
+
"x-nemu-client": this.client_label,
|
|
142
|
+
...(base_version === null ? { "if-none-match": "*" } : { "if-match": `"${base_version}"` }),
|
|
143
|
+
},
|
|
144
|
+
body: content,
|
|
145
|
+
timeout_ms: 120_000,
|
|
146
|
+
});
|
|
147
|
+
return parse(z.object({ data: note_schema }), await response.json()).data;
|
|
148
|
+
}
|
|
149
|
+
async remove(path, version) {
|
|
150
|
+
const response = await this.call("DELETE", `${this.base()}/files`, {
|
|
151
|
+
query: { path, version },
|
|
152
|
+
headers: { "x-nemu-client": this.client_label },
|
|
153
|
+
});
|
|
154
|
+
return parse(z.object({ data: note_schema }), await response.json()).data;
|
|
155
|
+
}
|
|
156
|
+
async move(from, to, base_version) {
|
|
157
|
+
const response = await this.call("POST", `${this.base()}/files/move`, {
|
|
158
|
+
headers: { "content-type": "application/json", "x-nemu-client": this.client_label },
|
|
159
|
+
body: JSON.stringify({ from, to, base_version }),
|
|
160
|
+
});
|
|
161
|
+
return parse(z.object({ data: note_schema }), await response.json()).data;
|
|
162
|
+
}
|
|
163
|
+
async record_merge(input) {
|
|
164
|
+
await this.call("POST", `${this.base()}/merges`, {
|
|
165
|
+
headers: { "content-type": "application/json", "x-nemu-client": this.client_label },
|
|
166
|
+
body: JSON.stringify({
|
|
167
|
+
path: input.path,
|
|
168
|
+
merged_version: input.merged_version,
|
|
169
|
+
remote_version: input.remote_version,
|
|
170
|
+
content: Buffer.from(input.local).toString("base64"),
|
|
171
|
+
client: this.client_label,
|
|
172
|
+
}),
|
|
173
|
+
timeout_ms: 120_000,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
async read_version(note_id, version) {
|
|
177
|
+
const response = await this.call("GET", `${this.base()}/history/version`, {
|
|
178
|
+
query: { note_id, version },
|
|
179
|
+
timeout_ms: 120_000,
|
|
180
|
+
});
|
|
181
|
+
return Buffer.from(await response.arrayBuffer());
|
|
182
|
+
}
|
|
183
|
+
async history(note_id, limit = 200) {
|
|
184
|
+
const response = await this.call("GET", `${this.base()}/history`, {
|
|
185
|
+
query: { note_id, limit },
|
|
186
|
+
});
|
|
187
|
+
return parse(z.object({ data: z.array(version_schema) }), await response.json()).data;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export async function list_vaults(transport) {
|
|
191
|
+
const response = await transport("GET", "/api/v1/vaults", {});
|
|
192
|
+
if (!response.ok)
|
|
193
|
+
throw await error_of(response);
|
|
194
|
+
return parse(z.object({ data: z.array(vault_schema) }), await response.json()).data;
|
|
195
|
+
}
|
|
196
|
+
export async function create_vault(transport, name) {
|
|
197
|
+
const response = await transport("POST", "/api/v1/vaults", {
|
|
198
|
+
headers: { "content-type": "application/json" },
|
|
199
|
+
body: JSON.stringify({ name }),
|
|
200
|
+
});
|
|
201
|
+
if (!response.ok)
|
|
202
|
+
throw await error_of(response);
|
|
203
|
+
return parse(z.object({ data: vault_schema }), await response.json()).data;
|
|
204
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { read_credentials } from "../config.js";
|
|
2
|
+
import { device_name } from "../device.js";
|
|
3
|
+
import { CliError } from "../errors.js";
|
|
4
|
+
import { send } from "../http.js";
|
|
5
|
+
import { VERSION } from "../version.js";
|
|
6
|
+
import { VaultApi } from "./api.js";
|
|
7
|
+
import { state_path_for } from "./state.js";
|
|
8
|
+
export function cli_transport(workspace) {
|
|
9
|
+
return async (method, path, options) => {
|
|
10
|
+
const { response } = await send(method, path, {
|
|
11
|
+
...options,
|
|
12
|
+
workspace: workspace ?? undefined,
|
|
13
|
+
});
|
|
14
|
+
return response;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function client_label() {
|
|
18
|
+
return `nemu-sync/${VERSION} ${device_name()}`.replace(/[^\x20-\x7e]/g, "").slice(0, 64);
|
|
19
|
+
}
|
|
20
|
+
export async function signed_in() {
|
|
21
|
+
const credentials = await read_credentials();
|
|
22
|
+
if (credentials === null)
|
|
23
|
+
throw new CliError("Not signed in. Run nemu login.", "not_signed_in");
|
|
24
|
+
return credentials;
|
|
25
|
+
}
|
|
26
|
+
export async function open_linked(marker) {
|
|
27
|
+
const credentials = await signed_in();
|
|
28
|
+
if (credentials.api_url !== marker.server) {
|
|
29
|
+
throw new CliError(`This folder is linked to a vault on ${marker.server}, but you are signed in to ${credentials.api_url}. Nothing was synced.`, "server_mismatch");
|
|
30
|
+
}
|
|
31
|
+
if (credentials.kind === "key" &&
|
|
32
|
+
credentials.workspace !== undefined &&
|
|
33
|
+
marker.workspace_id !== null &&
|
|
34
|
+
credentials.workspace.id !== marker.workspace_id) {
|
|
35
|
+
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
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
api: new VaultApi(cli_transport(marker.workspace_id), marker.vault_id, client_label()),
|
|
39
|
+
state_path: state_path_for(marker.vault_id),
|
|
40
|
+
};
|
|
41
|
+
}
|