@pleasure1234/vesper 0.1.0 → 0.1.3

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.
Files changed (2) hide show
  1. package/dist/index.mjs +103 -12
  2. package/package.json +10 -2
package/dist/index.mjs CHANGED
@@ -10,14 +10,26 @@ import { spawn } from "node:child_process";
10
10
  import { createHash, randomBytes } from "node:crypto";
11
11
  import { createServer } from "node:http";
12
12
  // package.json
13
- var version = "0.1.0";
14
- // src/types.ts
13
+ var version = "0.1.3";
14
+ // ../../packages/core/src/constants.ts
15
15
  const VISIBILITIES = ["private", "public"];
16
16
  const SHELVES = [
17
17
  "all",
18
18
  "favorites",
19
19
  "archive"
20
20
  ];
21
+ const SCOPES = [
22
+ "entries:create",
23
+ "entries:read",
24
+ "entries:update",
25
+ "entries:delete",
26
+ "photos:create",
27
+ "photos:read",
28
+ "tags:read",
29
+ "tags:update"
30
+ ];
31
+ SCOPES.filter((scope) => scope.endsWith(":read"));
32
+ // src/types.ts
21
33
  const visibility = z.enum(VISIBILITIES);
22
34
  const photo = z.looseObject({
23
35
  id: z.string(),
@@ -109,6 +121,10 @@ const metadataSchema = z.object({
109
121
  });
110
122
  const resourceSchema = z.object({ authorization_servers: z.array(z.string()).min(1) });
111
123
  const clientSchema = z.object({ client_id: z.string() });
124
+ const oauthErrorSchema = z.object({
125
+ error: z.string(),
126
+ error_description: z.string().nullable().default(null)
127
+ });
112
128
  const tokenSchema = z.object({
113
129
  access_token: z.string(),
114
130
  refresh_token: z.string().nullable().default(null),
@@ -209,16 +225,21 @@ async function collect(load, first, all, max) {
209
225
  }
210
226
  }
211
227
  // src/oauth.ts
212
- const SCOPE = "entries:read entries:write offline_access";
228
+ const SCOPE = [...SCOPES, "offline_access"].join(" ");
213
229
  const WAIT_MS = 3e5;
214
230
  var OAuthError = class extends Error {};
215
231
  async function read(schema, response) {
216
- if (!response.ok) {
217
- const text = await response.text();
218
- throw new OAuthError(`${response.status} ${response.statusText}: ${text.slice(0, 300)}`);
219
- }
232
+ if (!response.ok) throw new OAuthError(`${response.status}: ${await reason(response)}`);
220
233
  return schema.parse(await response.json());
221
234
  }
235
+ async function reason(response) {
236
+ const text = await response.text();
237
+ if (!String(response.headers.get("content-type")).includes("json")) return text.slice(0, 300);
238
+ const parsed = oauthErrorSchema.safeParse(JSON.parse(text));
239
+ if (!parsed.success) return text.slice(0, 300);
240
+ const { error, error_description } = parsed.data;
241
+ return error_description === null ? error : `${error_description} (${error})`;
242
+ }
222
243
  const form = (fields) => ({
223
244
  method: "POST",
224
245
  headers: { "content-type": "application/x-www-form-urlencoded" },
@@ -260,7 +281,43 @@ function openBrowser(url) {
260
281
  child.on("error", () => {});
261
282
  child.unref();
262
283
  }
263
- const page = (text) => `<!doctype html><meta charset="utf-8"><title>Vesper</title><body style="font:16px system-ui;padding:3rem">${text}</body>`;
284
+ const page = (ok) => `<!doctype html>
285
+ <html lang="en">
286
+ <meta charset="utf-8">
287
+ <meta name="viewport" content="width=device-width, initial-scale=1">
288
+ <meta name="color-scheme" content="light dark">
289
+ <title>Vesper</title>
290
+ <style>
291
+ :root { --paper: #f6f1e7; --card: #fffdf8; --ink: #2b2620; --muted: #7a7065; --line: #e8dfd0;
292
+ --accent: ${ok ? "#5f7a52" : "#b4553f"}; --tape: rgb(222 196 140 / 0.7); }
293
+ @media (prefers-color-scheme: dark) {
294
+ :root { --paper: #1c1916; --card: #24201c; --ink: #ece4d7; --muted: #a39887; --line: #3a332c;
295
+ --tape: rgb(170 140 90 / 0.45); }
296
+ }
297
+ * { box-sizing: border-box; }
298
+ body { margin: 0; min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
299
+ background: var(--paper); color: var(--ink); font: 1rem/1.6 ui-serif, Georgia, "Songti SC", serif; }
300
+ main { position: relative; width: min(26rem, 100%); padding: 2.5rem 2rem 2rem; background: var(--card);
301
+ border: 1px solid var(--line); border-radius: 0.375rem; transform: rotate(-0.6deg);
302
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 12px 32px -12px rgb(0 0 0 / 0.18);
303
+ background-image: repeating-linear-gradient(transparent 0 1.55rem, var(--line) 1.55rem 1.6rem); }
304
+ main::before { content: ""; position: absolute; top: -0.7rem; left: 50%; width: 6rem; height: 1.4rem;
305
+ background: var(--tape); transform: translateX(-50%) rotate(2deg); }
306
+ .mark { width: 2.75rem; height: 2.75rem; border-radius: 50%; display: grid; place-items: center;
307
+ border: 2px solid var(--accent); color: var(--accent); margin-bottom: 1rem; }
308
+ h1 { margin: 0 0 0.25rem; font-size: 1.5rem; font-weight: 600; letter-spacing: -0.01em; }
309
+ p { margin: 0; color: var(--muted); }
310
+ small { display: block; margin-top: 1.5rem; font: 0.75rem/1.4 ui-monospace, Menlo, monospace; color: var(--muted); }
311
+ </style>
312
+ <main>
313
+ <div class="mark" aria-hidden="true">
314
+ <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">${ok ? "<path d=\"M20 6 9 17l-5-5\"/>" : "<path d=\"M18 6 6 18M6 6l12 12\"/>"}</svg>
315
+ </div>
316
+ <h1>${ok ? "You're signed in" : "Sign-in didn't finish"}</h1>
317
+ <p>${ok ? "The Vesper CLI can use your journal now. You can close this tab and go back to the terminal." : "Something went wrong on the way back. Check the terminal for details, then run vesper login again."}</p>
318
+ <small>vesper · ${ok ? "signed in" : "sign-in failed"}</small>
319
+ </main>
320
+ </html>`;
264
321
  function listen(state) {
265
322
  return new Promise((ready, fail) => {
266
323
  const settle = Promise.withResolvers();
@@ -274,7 +331,7 @@ function listen(state) {
274
331
  const code = url.searchParams.get("code");
275
332
  const matched = url.searchParams.get("state") === state;
276
333
  const ok = !error && code !== null && matched;
277
- response.writeHead(ok ? 200 : 400, { "content-type": "text/html; charset=utf-8" }).end(page(ok ? "Signed in. You can close this tab." : "Sign-in failed. Check the terminal."));
334
+ response.writeHead(ok ? 200 : 400, { "content-type": "text/html; charset=utf-8" }).end(page(ok));
278
335
  server.close();
279
336
  clearTimeout(timer);
280
337
  if (code !== null && ok) settle.resolve(code);
@@ -345,12 +402,14 @@ async function login(origin, notify) {
345
402
  }
346
403
  async function refresh(credentials) {
347
404
  if (!credentials.refreshToken) throw new OAuthError("The sign-in expired. Run `vesper login`.");
348
- const tokens = await read(tokenSchema, await fetch(credentials.tokenEndpoint, form({
405
+ const response = await fetch(credentials.tokenEndpoint, form({
349
406
  grant_type: "refresh_token",
350
407
  refresh_token: credentials.refreshToken,
351
408
  client_id: credentials.clientId,
352
409
  resource: resourceOf(credentials.origin)
353
- })));
410
+ }));
411
+ if (response.status === 400 || response.status === 401) throw new OAuthError(`The sign-in expired: ${await reason(response)}. Run \`vesper login\`.`);
412
+ const tokens = await read(tokenSchema, response);
354
413
  return {
355
414
  ...credentials,
356
415
  accessToken: tokens.access_token,
@@ -474,6 +533,38 @@ program.command("logout").description("Revoke the saved sign-in and forget it").
474
533
  await clearCredentials();
475
534
  print({ signedOut: true });
476
535
  });
536
+ program.command("status").description("Check the server, whether this CLI is signed in, and the workspace it uses").action(async () => {
537
+ const saved = await loadCredentials();
538
+ const via = process.env.VESPER_TOKEN ? "token" : saved ? "oauth" : null;
539
+ const base = {
540
+ version,
541
+ server: saved && !process.env.VESPER_TOKEN ? saved.origin : originOf(urlFlag()),
542
+ via
543
+ };
544
+ if (!via) return print({
545
+ ...base,
546
+ signedIn: false,
547
+ hint: "Run `vesper login`."
548
+ });
549
+ try {
550
+ const me = await (await client()).get("/api/workspace", whoamiSchema);
551
+ print({
552
+ ...base,
553
+ signedIn: true,
554
+ workspace: me.workspace.handle,
555
+ plan: me.workspace.plan,
556
+ write: me.write,
557
+ renewsBefore: saved && via === "oauth" ? new Date(saved.expiresAt).toISOString() : null
558
+ });
559
+ } catch (error) {
560
+ if (!(error instanceof OAuthError || error instanceof ApiError && error.status === 401)) throw error;
561
+ print({
562
+ ...base,
563
+ signedIn: false,
564
+ hint: `${error.message} Run \`vesper login\`.`
565
+ });
566
+ }
567
+ });
477
568
  program.command("whoami").description("Show the workspace, plan, role and whether this sign-in can write").action(async () => print(await (await client()).get("/api/workspace", whoamiSchema)));
478
569
  const entries = program.command("entries").alias("entry").description("Journal entries");
479
570
  entries.addCommand(listCommand("list", "Entries newest first, 50 per page").option("--in <shelf>", "all, favorites or archive", parse(shelfSchema, "all, favorites or archive")).option("--pinned", "only pinned entries").action(async (flags) => print(await listEntries(await client(), flags, {
@@ -537,7 +628,7 @@ photos.addCommand(listCommand("list", "Photos newest first, from 24 entries per
537
628
  photos.command("download").description("Save a photo's original, or its thumbnail").argument("<id>").option("--thumb", "the small JPEG thumbnail instead").option("-o, --output <path>", "where to save it (default <id>.<ext>)").action(async (id, flags) => {
538
629
  const response = await (await client()).download(`/api/photos/${id}`, { size: flags.thumb ? "thumb" : "original" });
539
630
  const type = response.headers.get("content-type") || "application/octet-stream";
540
- const subtype = type.split(";")[0]?.split("/")[1] || "bin";
631
+ const [, subtype = "bin"] = /\/([\w.+-]+)/.exec(type) || [];
541
632
  const output = flags.output || `${id}.${subtype === "jpeg" ? "jpg" : subtype}`;
542
633
  if (!response.body) throw new Error("The server sent no image.");
543
634
  await writeFile(output, response.body);
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@pleasure1234/vesper",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Read and write your Vesper entries from the terminal.",
5
5
  "license": "UNLICENSED",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/vesper-shop/vesper-cloud.git",
9
+ "directory": "apps/cli"
10
+ },
6
11
  "bin": {
7
- "vesper": "./dist/index.mjs"
12
+ "vesper": "dist/index.mjs"
8
13
  },
9
14
  "files": [
10
15
  "dist",
@@ -23,6 +28,9 @@
23
28
  "commander": "15.1.0",
24
29
  "zod": "4.6.7"
25
30
  },
31
+ "devDependencies": {
32
+ "@vesper/core": "workspace:*"
33
+ },
26
34
  "engines": {
27
35
  "node": ">=22.12.0"
28
36
  }