@pleasure1234/vesper 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/dist/index.mjs +91 -11
  2. package/package.json +10 -2
package/dist/index.mjs CHANGED
@@ -10,14 +10,15 @@ 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.1";
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
+ // src/types.ts
21
22
  const visibility = z.enum(VISIBILITIES);
22
23
  const photo = z.looseObject({
23
24
  id: z.string(),
@@ -109,6 +110,10 @@ const metadataSchema = z.object({
109
110
  });
110
111
  const resourceSchema = z.object({ authorization_servers: z.array(z.string()).min(1) });
111
112
  const clientSchema = z.object({ client_id: z.string() });
113
+ const oauthErrorSchema = z.object({
114
+ error: z.string(),
115
+ error_description: z.string().nullable().default(null)
116
+ });
112
117
  const tokenSchema = z.object({
113
118
  access_token: z.string(),
114
119
  refresh_token: z.string().nullable().default(null),
@@ -213,12 +218,17 @@ const SCOPE = "entries:read entries:write offline_access";
213
218
  const WAIT_MS = 3e5;
214
219
  var OAuthError = class extends Error {};
215
220
  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
- }
221
+ if (!response.ok) throw new OAuthError(`${response.status}: ${await reason(response)}`);
220
222
  return schema.parse(await response.json());
221
223
  }
224
+ async function reason(response) {
225
+ const text = await response.text();
226
+ if (!String(response.headers.get("content-type")).includes("json")) return text.slice(0, 300);
227
+ const parsed = oauthErrorSchema.safeParse(JSON.parse(text));
228
+ if (!parsed.success) return text.slice(0, 300);
229
+ const { error, error_description } = parsed.data;
230
+ return error_description === null ? error : `${error_description} (${error})`;
231
+ }
222
232
  const form = (fields) => ({
223
233
  method: "POST",
224
234
  headers: { "content-type": "application/x-www-form-urlencoded" },
@@ -260,7 +270,43 @@ function openBrowser(url) {
260
270
  child.on("error", () => {});
261
271
  child.unref();
262
272
  }
263
- const page = (text) => `<!doctype html><meta charset="utf-8"><title>Vesper</title><body style="font:16px system-ui;padding:3rem">${text}</body>`;
273
+ const page = (ok) => `<!doctype html>
274
+ <html lang="en">
275
+ <meta charset="utf-8">
276
+ <meta name="viewport" content="width=device-width, initial-scale=1">
277
+ <meta name="color-scheme" content="light dark">
278
+ <title>Vesper</title>
279
+ <style>
280
+ :root { --paper: #f6f1e7; --card: #fffdf8; --ink: #2b2620; --muted: #7a7065; --line: #e8dfd0;
281
+ --accent: ${ok ? "#5f7a52" : "#b4553f"}; --tape: rgb(222 196 140 / 0.7); }
282
+ @media (prefers-color-scheme: dark) {
283
+ :root { --paper: #1c1916; --card: #24201c; --ink: #ece4d7; --muted: #a39887; --line: #3a332c;
284
+ --tape: rgb(170 140 90 / 0.45); }
285
+ }
286
+ * { box-sizing: border-box; }
287
+ body { margin: 0; min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
288
+ background: var(--paper); color: var(--ink); font: 1rem/1.6 ui-serif, Georgia, "Songti SC", serif; }
289
+ main { position: relative; width: min(26rem, 100%); padding: 2.5rem 2rem 2rem; background: var(--card);
290
+ border: 1px solid var(--line); border-radius: 0.375rem; transform: rotate(-0.6deg);
291
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 12px 32px -12px rgb(0 0 0 / 0.18);
292
+ background-image: repeating-linear-gradient(transparent 0 1.55rem, var(--line) 1.55rem 1.6rem); }
293
+ main::before { content: ""; position: absolute; top: -0.7rem; left: 50%; width: 6rem; height: 1.4rem;
294
+ background: var(--tape); transform: translateX(-50%) rotate(2deg); }
295
+ .mark { width: 2.75rem; height: 2.75rem; border-radius: 50%; display: grid; place-items: center;
296
+ border: 2px solid var(--accent); color: var(--accent); margin-bottom: 1rem; }
297
+ h1 { margin: 0 0 0.25rem; font-size: 1.5rem; font-weight: 600; letter-spacing: -0.01em; }
298
+ p { margin: 0; color: var(--muted); }
299
+ small { display: block; margin-top: 1.5rem; font: 0.75rem/1.4 ui-monospace, Menlo, monospace; color: var(--muted); }
300
+ </style>
301
+ <main>
302
+ <div class="mark" aria-hidden="true">
303
+ <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>
304
+ </div>
305
+ <h1>${ok ? "You're signed in" : "Sign-in didn't finish"}</h1>
306
+ <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>
307
+ <small>vesper · ${ok ? "signed in" : "sign-in failed"}</small>
308
+ </main>
309
+ </html>`;
264
310
  function listen(state) {
265
311
  return new Promise((ready, fail) => {
266
312
  const settle = Promise.withResolvers();
@@ -274,7 +320,7 @@ function listen(state) {
274
320
  const code = url.searchParams.get("code");
275
321
  const matched = url.searchParams.get("state") === state;
276
322
  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."));
323
+ response.writeHead(ok ? 200 : 400, { "content-type": "text/html; charset=utf-8" }).end(page(ok));
278
324
  server.close();
279
325
  clearTimeout(timer);
280
326
  if (code !== null && ok) settle.resolve(code);
@@ -345,12 +391,14 @@ async function login(origin, notify) {
345
391
  }
346
392
  async function refresh(credentials) {
347
393
  if (!credentials.refreshToken) throw new OAuthError("The sign-in expired. Run `vesper login`.");
348
- const tokens = await read(tokenSchema, await fetch(credentials.tokenEndpoint, form({
394
+ const response = await fetch(credentials.tokenEndpoint, form({
349
395
  grant_type: "refresh_token",
350
396
  refresh_token: credentials.refreshToken,
351
397
  client_id: credentials.clientId,
352
398
  resource: resourceOf(credentials.origin)
353
- })));
399
+ }));
400
+ if (response.status === 400 || response.status === 401) throw new OAuthError(`The sign-in expired: ${await reason(response)}. Run \`vesper login\`.`);
401
+ const tokens = await read(tokenSchema, response);
354
402
  return {
355
403
  ...credentials,
356
404
  accessToken: tokens.access_token,
@@ -474,6 +522,38 @@ program.command("logout").description("Revoke the saved sign-in and forget it").
474
522
  await clearCredentials();
475
523
  print({ signedOut: true });
476
524
  });
525
+ program.command("status").description("Check the server, whether this CLI is signed in, and the workspace it uses").action(async () => {
526
+ const saved = await loadCredentials();
527
+ const via = process.env.VESPER_TOKEN ? "token" : saved ? "oauth" : null;
528
+ const base = {
529
+ version,
530
+ server: saved && !process.env.VESPER_TOKEN ? saved.origin : originOf(urlFlag()),
531
+ via
532
+ };
533
+ if (!via) return print({
534
+ ...base,
535
+ signedIn: false,
536
+ hint: "Run `vesper login`."
537
+ });
538
+ try {
539
+ const me = await (await client()).get("/api/workspace", whoamiSchema);
540
+ print({
541
+ ...base,
542
+ signedIn: true,
543
+ workspace: me.workspace.handle,
544
+ plan: me.workspace.plan,
545
+ write: me.write,
546
+ renewsBefore: saved && via === "oauth" ? new Date(saved.expiresAt).toISOString() : null
547
+ });
548
+ } catch (error) {
549
+ if (!(error instanceof OAuthError || error instanceof ApiError && error.status === 401)) throw error;
550
+ print({
551
+ ...base,
552
+ signedIn: false,
553
+ hint: `${error.message} Run \`vesper login\`.`
554
+ });
555
+ }
556
+ });
477
557
  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
558
  const entries = program.command("entries").alias("entry").description("Journal entries");
479
559
  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 +617,7 @@ photos.addCommand(listCommand("list", "Photos newest first, from 24 entries per
537
617
  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
618
  const response = await (await client()).download(`/api/photos/${id}`, { size: flags.thumb ? "thumb" : "original" });
539
619
  const type = response.headers.get("content-type") || "application/octet-stream";
540
- const subtype = type.split(";")[0]?.split("/")[1] || "bin";
620
+ const [, subtype = "bin"] = /\/([\w.+-]+)/.exec(type) || [];
541
621
  const output = flags.output || `${id}.${subtype === "jpeg" ? "jpg" : subtype}`;
542
622
  if (!response.body) throw new Error("The server sent no image.");
543
623
  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.1",
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
  }