wiki-viewer 1.1.2 → 1.2.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.
Files changed (49) hide show
  1. package/.next/standalone/.next/BUILD_ID +1 -1
  2. package/.next/standalone/.next/build-manifest.json +3 -3
  3. package/.next/standalone/.next/prerender-manifest.json +3 -3
  4. package/.next/standalone/.next/server/app/_global-error.html +1 -1
  5. package/.next/standalone/.next/server/app/_global-error.rsc +1 -1
  6. package/.next/standalone/.next/server/app/_global-error.segments/__PAGE__.segment.rsc +1 -1
  7. package/.next/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  8. package/.next/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  9. package/.next/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  10. package/.next/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  11. package/.next/standalone/.next/server/app/_not-found.html +1 -1
  12. package/.next/standalone/.next/server/app/_not-found.rsc +1 -1
  13. package/.next/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  14. package/.next/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  15. package/.next/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  16. package/.next/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  17. package/.next/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  18. package/.next/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  19. package/.next/standalone/.next/server/app/api/wiki/upload/route.js.nft.json +1 -1
  20. package/.next/standalone/.next/server/app/index.html +1 -1
  21. package/.next/standalone/.next/server/app/index.rsc +1 -1
  22. package/.next/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  23. package/.next/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  24. package/.next/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  25. package/.next/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  26. package/.next/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  27. package/.next/standalone/.next/server/app/signin.html +1 -1
  28. package/.next/standalone/.next/server/app/signin.rsc +1 -1
  29. package/.next/standalone/.next/server/app/signin.segments/_full.segment.rsc +1 -1
  30. package/.next/standalone/.next/server/app/signin.segments/_head.segment.rsc +1 -1
  31. package/.next/standalone/.next/server/app/signin.segments/_index.segment.rsc +1 -1
  32. package/.next/standalone/.next/server/app/signin.segments/_tree.segment.rsc +1 -1
  33. package/.next/standalone/.next/server/app/signin.segments/signin/__PAGE__.segment.rsc +1 -1
  34. package/.next/standalone/.next/server/app/signin.segments/signin.segment.rsc +1 -1
  35. package/.next/standalone/.next/server/middleware-build-manifest.js +3 -3
  36. package/.next/standalone/.next/server/middleware-manifest.json +5 -5
  37. package/.next/standalone/.next/server/pages/404.html +1 -1
  38. package/.next/standalone/.next/server/pages/500.html +1 -1
  39. package/.next/standalone/.next/server/server-reference-manifest.js +1 -1
  40. package/.next/standalone/.next/server/server-reference-manifest.json +1 -1
  41. package/.next/standalone/README.md +36 -1
  42. package/.next/standalone/bin/wiki-viewer.js +177 -4
  43. package/.next/standalone/package.json +1 -1
  44. package/README.md +36 -1
  45. package/bin/wiki-viewer.js +177 -4
  46. package/package.json +1 -1
  47. /package/.next/standalone/.next/static/{8im3AoVCFwRIj5snIjj9L → Rg1RfvRWNzfcK3n1DDIQs}/_buildManifest.js +0 -0
  48. /package/.next/standalone/.next/static/{8im3AoVCFwRIj5snIjj9L → Rg1RfvRWNzfcK3n1DDIQs}/_clientMiddlewareManifest.js +0 -0
  49. /package/.next/standalone/.next/static/{8im3AoVCFwRIj5snIjj9L → Rg1RfvRWNzfcK3n1DDIQs}/_ssgManifest.js +0 -0
@@ -31,12 +31,18 @@ function printUsage() {
31
31
  console.error(" -H, --host <host> Host to bind to (default: localhost)");
32
32
  console.error(" --https Enable HTTPS (self-signed cert, enables service workers)");
33
33
  console.error("");
34
+ console.error(" -e, --env <KEY=VALUE> Set an app env var (repeatable; persisted with service install)");
35
+ console.error("");
34
36
  console.error("Commands:");
35
37
  console.error(" service install [dir] [options] Install as a user service (persists across reboot)");
36
38
  console.error(" service uninstall Remove the user service");
37
39
  console.error(" service status Show service status");
38
40
  console.error(" service logs Tail service logs");
41
+ console.error(" service restart Restart the service");
39
42
  console.error(" service run Run from saved config (used internally by the service)");
43
+ console.error(" config show Print the saved config");
44
+ console.error(" config set KEY=VALUE Set an app env var in the config");
45
+ console.error(" config unset KEY Remove an app env var from the config");
40
46
  console.error(" update Update wiki-viewer to the latest version and restart");
41
47
  console.error("");
42
48
  console.error("Examples:");
@@ -44,6 +50,8 @@ function printUsage() {
44
50
  console.error(" wiki-viewer ~/notes --https");
45
51
  console.error(" wiki-viewer ~/notes -p 8080 -H 0.0.0.0");
46
52
  console.error(" wiki-viewer service install ~/notes -H 0.0.0.0 -p 3003 --https");
53
+ console.error(" wiki-viewer service install ~/notes --env GOOGLE_CLIENT_ID=... --env GOOGLE_CLIENT_SECRET=...");
54
+ console.error(" wiki-viewer config set AUTH_ALLOWED_DOMAIN=example.com");
47
55
  console.error(" wiki-viewer update");
48
56
  }
49
57
 
@@ -60,6 +68,7 @@ function parseServeArgs(args) {
60
68
  const a = args[i];
61
69
  if (a === "-p" || a === "--port") { port = args[++i] ?? port; userSpecifiedPort = true; }
62
70
  else if (a === "-H" || a === "--host") host = args[++i] ?? host;
71
+ else if (a === "-e" || a === "--env") { i++; } // consumed by parseEnvFlags
63
72
  else if (a === "--https") useHttps = true;
64
73
  else if (!a.startsWith("-") && rootDir === undefined) rootDir = a;
65
74
  }
@@ -84,16 +93,21 @@ function saveConfig(cfg) {
84
93
  writeFileSync(configPath, JSON.stringify(cfg, null, 2) + "\n");
85
94
  }
86
95
 
87
- // Ad-hoc serve: pure CLI flags + built-in defaults. Does NOT read the config
88
- // file, so an installed service never silently alters a one-off invocation.
96
+ // Ad-hoc serve: CLI flags + built-in defaults. The run *shape* (dir/host/port)
97
+ // does NOT read the config file, so an installed service never silently alters
98
+ // a one-off invocation. App-level env (config.env) is still read, because those
99
+ // are settings about the app itself (allowlists, OAuth) rather than the bind.
89
100
  function resolveServeOptions(args) {
90
101
  const cli = parseServeArgs(args);
102
+ const cfg = loadConfig();
91
103
  return {
92
104
  rootDir: cli.rootDir ? path.resolve(cli.rootDir) : null,
93
105
  port: String(cli.port ?? "3000"),
94
106
  host: cli.host ?? "localhost",
95
107
  useHttps: Boolean(cli.useHttps),
96
108
  userSpecifiedPort: cli.userSpecifiedPort,
109
+ // App env: config.env as base, ad-hoc --env flags override per run.
110
+ configEnv: { ...(cfg.env ?? {}), ...parseEnvFlags(args) },
97
111
  };
98
112
  }
99
113
 
@@ -117,9 +131,77 @@ function resolveRunOptions(args) {
117
131
  host,
118
132
  useHttps: Boolean(useHttps),
119
133
  userSpecifiedPort,
134
+ configEnv: { ...(cfg.env ?? {}), ...parseEnvFlags(args) },
120
135
  };
121
136
  }
122
137
 
138
+ // ── environment resolution ───────────────────────────────────────────────────
139
+
140
+ // Env vars the app reads that the bin can derive or manage on the user's behalf.
141
+ // Everything else in config.env is passed through verbatim.
142
+ const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "::1", ""]);
143
+
144
+ function isLocalHost(h) {
145
+ return LOCAL_HOSTS.has(h);
146
+ }
147
+
148
+ // Build the env for the spawned server.
149
+ // Precedence (highest first):
150
+ // 1. the shell environment the user launched us with (explicit override)
151
+ // 2. config.env from ~/.wiki-viewer/config.json
152
+ // 3. values derived by the bin from the run options (e.g. BETTER_AUTH_URL)
153
+ //
154
+ // Returns { env, warnings } where warnings are human-readable strings to print
155
+ // before the server starts.
156
+ function computeServerEnv({ host, port, useHttps, configEnv }) {
157
+ const warnings = [];
158
+ const derived = {};
159
+
160
+ const scheme = useHttps ? "https" : "http";
161
+ const urlHost = isLocalHost(host) || host === "0.0.0.0" ? "localhost" : host;
162
+ const isSecureContext = useHttps || isLocalHost(host);
163
+
164
+ // BETTER_AUTH_URL: the app requires this in production so cookies and OAuth
165
+ // callbacks resolve. Derive it from the bind so the common case needs no
166
+ // config at all.
167
+ derived.BETTER_AUTH_URL = `${scheme}://${urlHost}:${port}`;
168
+
169
+ // The app refuses to boot in production over an insecure context unless
170
+ // WIKI_ALLOW_INSECURE=1. localhost http is fine (browsers treat it as secure).
171
+ // Remote http is not: auth cookies and service workers will silently break.
172
+ if (!isSecureContext) {
173
+ derived.WIKI_ALLOW_INSECURE = "1";
174
+ warnings.push(
175
+ `Serving plain HTTP on a non-local host (${host}). Browsers treat this as\n` +
176
+ ` an insecure context: login cookies, service workers and PDF.js will not\n` +
177
+ ` work reliably, and OAuth callbacks will fail.\n` +
178
+ ` Fix: re-run with --https, or put a TLS-terminating proxy in front and set\n` +
179
+ ` BETTER_AUTH_URL to its public https:// URL (see "env" in ${configPath}).`,
180
+ );
181
+ } else if (isLocalHost(host) && !useHttps) {
182
+ // localhost http is a secure context for the browser, but the app's prod
183
+ // guard still wants the bypass flag set explicitly. Do it for the user.
184
+ derived.WIKI_ALLOW_INSECURE = "1";
185
+ }
186
+
187
+ // Layer: derived < config.env < shell env.
188
+ const env = { ...derived, ...configEnv };
189
+
190
+ // Note when the shell or config overrode a derived value so the user isn't
191
+ // surprised that --host/--port didn't change the auth URL.
192
+ if (configEnv.BETTER_AUTH_URL && configEnv.BETTER_AUTH_URL !== derived.BETTER_AUTH_URL) {
193
+ // config wins over derived; that's intentional, no warning needed.
194
+ }
195
+ if (process.env.BETTER_AUTH_URL && process.env.BETTER_AUTH_URL !== env.BETTER_AUTH_URL) {
196
+ warnings.push(
197
+ `BETTER_AUTH_URL is set in your shell (${process.env.BETTER_AUTH_URL}) and\n` +
198
+ ` overrides the derived/config value.`,
199
+ );
200
+ }
201
+
202
+ return { env, warnings };
203
+ }
204
+
123
205
  // ── HTTPS cert generation ──────────────────────────────────────────────────
124
206
 
125
207
  function ensureCerts(host) {
@@ -189,7 +271,7 @@ function getNetworkAddress() {
189
271
  // ── start ──────────────────────────────────────────────────────────────────
190
272
 
191
273
  async function start(opts) {
192
- const { rootDir: resolvedRoot, useHttps } = opts;
274
+ const { rootDir: resolvedRoot, useHttps, configEnv = {} } = opts;
193
275
  let { port, host, userSpecifiedPort } = opts;
194
276
 
195
277
  if (!existsSync(serverJs)) {
@@ -221,11 +303,18 @@ async function start(opts) {
221
303
  // Otherwise it must bind to the user-requested host directly.
222
304
  const internalHost = useHttps ? "127.0.0.1" : host;
223
305
 
306
+ // Resolve the app environment (config.env + shell overrides + derived
307
+ // BETTER_AUTH_URL etc.) against the user-facing host/port, then print any
308
+ // guidance before the server boots.
309
+ const { env: appEnv, warnings } = computeServerEnv({ host, port, useHttps, configEnv });
310
+ for (const w of warnings) console.log(`\n⚠️ ${w}`);
311
+
224
312
  const child = spawn(process.execPath, [serverJs], {
225
313
  cwd: path.join(appRoot, ".next", "standalone"),
226
314
  stdio: "inherit",
227
315
  env: {
228
- ...process.env,
316
+ ...appEnv, // derived defaults + config.env
317
+ ...process.env, // shell env always wins over derived/config
229
318
  ...(resolvedRoot ? { ROOT_DIR: resolvedRoot } : {}),
230
319
  PORT: internalPort,
231
320
  HOSTNAME: internalHost,
@@ -310,17 +399,38 @@ function runQuiet(cmd, args) {
310
399
 
311
400
  // ── service: install ──────────────────────────────────────────────────────────
312
401
 
402
+ // Pull repeatable `--env KEY=VALUE` (or `-e KEY=VALUE`) pairs out of an argv
403
+ // list. Returns the collected map; the caller has already parsed the rest.
404
+ function parseEnvFlags(args) {
405
+ const env = {};
406
+ for (let i = 0; i < args.length; i++) {
407
+ if (args[i] === "--env" || args[i] === "-e") {
408
+ const pair = args[++i];
409
+ if (!pair || !pair.includes("=")) {
410
+ console.error(`Error: --env expects KEY=VALUE (got: ${pair ?? "(nothing)"})`);
411
+ process.exit(1);
412
+ }
413
+ const idx = pair.indexOf("=");
414
+ env[pair.slice(0, idx)] = pair.slice(idx + 1);
415
+ }
416
+ }
417
+ return env;
418
+ }
419
+
313
420
  function serviceInstall(args) {
314
421
  const p = requireSupportedPlatform();
315
422
 
316
423
  // Capture the run config from flags (falling back to existing config), persist it.
317
424
  const cli = parseServeArgs(args);
425
+ const envFlags = parseEnvFlags(args);
318
426
  const existing = loadConfig();
427
+ const mergedEnv = { ...(existing.env ?? {}), ...envFlags };
319
428
  const cfg = {
320
429
  rootDir: cli.rootDir != null ? path.resolve(cli.rootDir) : existing.rootDir ?? null,
321
430
  host: cli.host ?? existing.host ?? "localhost",
322
431
  port: cli.port ?? existing.port ?? "3000",
323
432
  https: cli.useHttps ?? existing.https ?? false,
433
+ ...(Object.keys(mergedEnv).length ? { env: mergedEnv } : {}),
324
434
  };
325
435
  saveConfig(cfg);
326
436
  console.log(`Saved config to ${configPath}`);
@@ -328,12 +438,72 @@ function serviceInstall(args) {
328
438
  console.log(` host: ${cfg.host}`);
329
439
  console.log(` port: ${cfg.port}`);
330
440
  console.log(` https: ${cfg.https}`);
441
+ if (cfg.env) {
442
+ console.log(` env: ${Object.keys(cfg.env).join(", ")}`);
443
+ }
331
444
  console.log("");
332
445
 
446
+ // Surface the same secure-context guidance the server would, at install time.
447
+ const { warnings } = computeServerEnv({
448
+ host: cfg.host, port: String(cfg.port), useHttps: Boolean(cfg.https), configEnv: cfg.env ?? {},
449
+ });
450
+ for (const w of warnings) console.log(`⚠️ ${w}\n`);
451
+
333
452
  if (p === "linux") installSystemd();
334
453
  else installLaunchd();
335
454
  }
336
455
 
456
+ // `wiki-viewer config` — show or edit the saved config (incl. env).
457
+ function configCommand(args) {
458
+ const cfg = loadConfig();
459
+
460
+ if (args.length === 0 || args[0] === "show" || args[0] === "list") {
461
+ if (!existsSync(configPath)) {
462
+ console.log(`No config yet at ${configPath}`);
463
+ console.log(`Create one with: wiki-viewer service install [dir] [options] [--env KEY=VALUE]`);
464
+ return;
465
+ }
466
+ console.log(configPath);
467
+ console.log(JSON.stringify(cfg, null, 2));
468
+ return;
469
+ }
470
+
471
+ if (args[0] === "set") {
472
+ const pairs = args.slice(1);
473
+ if (pairs.length === 0) {
474
+ console.error("Usage: wiki-viewer config set KEY=VALUE [KEY=VALUE ...]");
475
+ process.exit(1);
476
+ }
477
+ cfg.env = cfg.env ?? {};
478
+ for (const pair of pairs) {
479
+ const idx = pair.indexOf("=");
480
+ if (idx < 0) { console.error(`Error: expected KEY=VALUE (got: ${pair})`); process.exit(1); }
481
+ cfg.env[pair.slice(0, idx)] = pair.slice(idx + 1);
482
+ }
483
+ saveConfig(cfg);
484
+ console.log(`Updated env in ${configPath}: ${pairs.map((p) => p.split("=")[0]).join(", ")}`);
485
+ if (serviceIsInstalled()) console.log("Run `wiki-viewer service restart` to apply.");
486
+ return;
487
+ }
488
+
489
+ if (args[0] === "unset") {
490
+ const keys = args.slice(1);
491
+ if (keys.length === 0) { console.error("Usage: wiki-viewer config unset KEY [KEY ...]"); process.exit(1); }
492
+ cfg.env = cfg.env ?? {};
493
+ for (const k of keys) delete cfg.env[k];
494
+ saveConfig(cfg);
495
+ console.log(`Removed from env: ${keys.join(", ")}`);
496
+ if (serviceIsInstalled()) console.log("Run `wiki-viewer service restart` to apply.");
497
+ return;
498
+ }
499
+
500
+ if (args[0] === "path") { console.log(configPath); return; }
501
+
502
+ console.error(`Unknown config command: ${args[0]}`);
503
+ console.error("Try: show | set KEY=VALUE | unset KEY | path");
504
+ process.exit(1);
505
+ }
506
+
337
507
  function installSystemd() {
338
508
  const unitDir = path.join(os.homedir(), ".config", "systemd", "user");
339
509
  mkdirSync(unitDir, { recursive: true });
@@ -547,6 +717,9 @@ switch (cmd) {
547
717
  }
548
718
  break;
549
719
  }
720
+ case "config":
721
+ configCommand(rest);
722
+ break;
550
723
  case "update":
551
724
  update();
552
725
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-viewer",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Local file viewer and wiki — view markdown, HTML, PDFs, notebooks, office docs, and more from any directory",
5
5
  "license": "MIT",
6
6
  "type": "module",