@muhkoo/cli 0.15.0-alpha.0 → 0.15.0-alpha.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.
package/README.md CHANGED
@@ -109,6 +109,7 @@ Backend provision · tables · agents · functions
109
109
  Files vfs cd|pwd|ls|stat|tree|cat|put|get|mkdir|rm|cp|mv|find|history|restore
110
110
  vfs import|mount|ignore · vfs sweep|use-app|app
111
111
  vcs status|commit|log|show|diff|branch|switch|checkout|merge|restore · vcs merge --abort
112
+ kv ls [prefix]
112
113
  Hosting deploy · promote · hosting status|rollback|rm-release|unpublish · domains
113
114
  Tools logs · eject
114
115
  ```
package/bin/muhkoo.js CHANGED
@@ -10,9 +10,30 @@
10
10
  import { run } from "../src/index.js";
11
11
  import { printable } from "../src/lib/ui.js";
12
12
 
13
+ /**
14
+ * Resolves once everything already written to stdout and stderr has left the
15
+ * process. On macOS a write to a pipe is asynchronous, so exiting straight
16
+ * after a large write dropped everything past the pipe's buffer (64 KiB or
17
+ * less): `muhkoo kv ls vfs/ | wc -l` lost keys and still exited 0. An empty
18
+ * write's callback runs after every write queued before it. It also runs when
19
+ * the reader has gone away (`| head`), and so does the `error` listener, so
20
+ * this never waits on a closed pipe and a closed pipe does not crash the exit.
21
+ */
22
+ const drained = () =>
23
+ Promise.all(
24
+ [process.stdout, process.stderr].map(
25
+ (stream) =>
26
+ new Promise((resolve) => {
27
+ stream.once("error", () => resolve());
28
+ stream.write("", () => resolve());
29
+ }),
30
+ ),
31
+ );
32
+
13
33
  run(process.argv.slice(2))
34
+ .then(drained)
14
35
  .then(() => process.exit(0))
15
- .catch((e) => {
36
+ .catch(async (e) => {
16
37
  // Escaped like everything else printed: an error's message can carry a
17
38
  // name or a server's words. The message is one line; the stack's frames
18
39
  // are Node's own lines.
@@ -20,5 +41,6 @@ run(process.argv.slice(2))
20
41
  const stack = e instanceof Error && typeof e.stack === "string" ? e.stack : "";
21
42
  const frames = stack.startsWith(head) ? stack.slice(head.length).split("\n").filter(Boolean) : [];
22
43
  console.error(["", `✗ ${printable(head)}`, ...frames.map(printable), ""].join("\n"));
44
+ await drained();
23
45
  process.exit(1);
24
46
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muhkoo/cli",
3
- "version": "0.15.0-alpha.0",
3
+ "version": "0.15.0-alpha.3",
4
4
  "description": "Command-line interface for the Muhkoo platform — provision backends, deploy apps, and manage agents, functions, databases, and domains.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  "release": "npm publish --tag alpha && npm dist-tag add @muhkoo/cli@$npm_package_version latest"
41
41
  },
42
42
  "dependencies": {
43
- "@muhkoo/connect": "0.15.0-alpha.0",
43
+ "@muhkoo/connect": "0.15.0-alpha.3",
44
44
  "snarkjs": "^0.7.0"
45
45
  },
46
46
  "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
@@ -0,0 +1,65 @@
1
+ /**
2
+ * `muhkoo kv` — the keys in your personal key/value store.
3
+ *
4
+ * The store is shared by every app you sign into: `client.kv` collections
5
+ * (`todos/…`), your filesystem (`vfs/…`), version control (`vcs/…`) and the
6
+ * SDK's own keys. This lists NAMES only and never reads a value, so it needs a
7
+ * session but not an unlocked vault. (`client.kv` encrypts values by default,
8
+ * but an app may store one with `encrypt: false`, so nothing here says every
9
+ * value is encrypted.)
10
+ * Keys come from whoever wrote them, so they are printed escaped (`info`,
11
+ * `json`), never raw.
12
+ */
13
+
14
+ import { openSession } from "../lib/vfs.js";
15
+ import { json, info, die } from "../lib/ui.js";
16
+
17
+ export const help = `muhkoo kv — the keys in your personal key/value store
18
+
19
+ Usage:
20
+ muhkoo kv ls [<prefix>] [--json] keys that start with <prefix> (default: all)
21
+
22
+ Options:
23
+ --app <slug> the app the read is made as (default: the one from
24
+ \`muhkoo vfs use-app\`); reads work without one
25
+ --json a JSON array of keys
26
+
27
+ Keys only: this command never reads a value. Put --json after "ls".
28
+
29
+ Examples:
30
+ muhkoo kv ls todos/ one client.kv collection
31
+ muhkoo kv ls vfs/d/ your filesystem's directory records`;
32
+
33
+ export default async function kv(args) {
34
+ // The shared parser gives `--json` the word after it, so `kv ls --json todos/`
35
+ // arrives as `json: "todos/"`. That word goes back on the END of the
36
+ // positionals, as `vfs`'s `booleanFlags` does. So `--json` works anywhere
37
+ // after the subcommand, but not before it: `kv --json ls todos/` reads as
38
+ // `kv todos/ ls` and is refused as an unknown subcommand.
39
+ const words = [...args._.slice(1), ...(typeof args.json === "string" ? [args.json] : [])];
40
+ const sub = words[0] ?? "ls";
41
+
42
+ switch (sub) {
43
+ case "ls": {
44
+ if (words.length > 2) return die("Usage: muhkoo kv ls [<prefix>] [--json]");
45
+ const prefix = words[1] ?? "";
46
+ const client = await openSession(args);
47
+ const keys = await client.kv.keys(prefix);
48
+ if (args.json) return json(keys);
49
+ // A human hint, not data: stderr, so `kv ls todos/ | while read k; do …`
50
+ // never reads this sentence as a key. Stdout stays empty and the exit
51
+ // code stays 0.
52
+ if (!keys.length)
53
+ return void process.stderr.write(
54
+ `${prefix ? `No keys start with "${prefix}".` : "Your personal store is empty."}\n`,
55
+ );
56
+ // One key a line, a thousand lines a call: a large space can hold more
57
+ // keys than one function call takes arguments.
58
+ for (let i = 0; i < keys.length; i += 1000) info(...keys.slice(i, i + 1000));
59
+ return;
60
+ }
61
+
62
+ default:
63
+ return die(`Unknown subcommand "kv ${sub}". See \`muhkoo kv --help\`.`);
64
+ }
65
+ }
package/src/index.js CHANGED
@@ -22,6 +22,7 @@ const COMMANDS = {
22
22
  promote: () => import("./commands/promote.js"),
23
23
  vfs: () => import("./commands/vfs.js"),
24
24
  vcs: () => import("./commands/vcs.js"),
25
+ kv: () => import("./commands/kv.js"),
25
26
  devices: () => import("./commands/devices.js"),
26
27
  hosting: () => import("./commands/hosting.js"),
27
28
  domains: () => import("./commands/domains.js"),
@@ -63,6 +64,7 @@ Files
63
64
  vfs sweep|use-app|app reclaim records; pick the app writes are made as
64
65
  vcs status|commit|log|show|diff|branch|switch|checkout|merge|restore
65
66
  version control over it
67
+ kv ls [prefix] keys in your personal key/value store
66
68
 
67
69
  Hosting
68
70
  deploy deploy a built client to Muhkoo hosting (test env)
package/src/lib/vfs.js CHANGED
@@ -30,6 +30,34 @@ import { die, info, step, prompt, promptHidden } from "./ui.js";
30
30
  * pairing alone, unlocking for this command only.
31
31
  */
32
32
  export async function openClient(args) {
33
+ const { cfg, baseUrl, client } = await restoreSession(args);
34
+
35
+ await unlock({ args, cfg, baseUrl, client });
36
+
37
+ // Restore the working directory. A CLI is a fresh process each time, so `cd`
38
+ // only means anything if it survives between invocations — the same reason a
39
+ // shell keeps PWD. A directory that has since been deleted silently falls
40
+ // back to the root rather than making every later command fail.
41
+ if (cfg.vfsCwd && cfg.vfsCwd !== "/") {
42
+ await client.vfs.cd(cfg.vfsCwd).catch(async () => {
43
+ if (!args.readOnly) await saveConfig({ ...cfg, vfsCwd: "/" });
44
+ info(`Working directory ${cfg.vfsCwd} is gone — back to /.`);
45
+ });
46
+ }
47
+ return client;
48
+ }
49
+
50
+ /**
51
+ * A Client with the session restored and NOTHING unlocked, for commands that
52
+ * only name things (`kv ls`) and never decrypt a value. Unlocking would ask for
53
+ * a password, or pair this machine, for nothing. Saves nothing to ~/.muhkoo.
54
+ */
55
+ export async function openSession(args) {
56
+ return (await restoreSession(args)).client;
57
+ }
58
+
59
+ /** The Client both of the above start from: signed in, session verified, not unlocked. */
60
+ async function restoreSession(args) {
33
61
  const cfg = await loadConfig();
34
62
  const baseUrl = await resolveBase(args, cfg);
35
63
  const token = args.token || process.env.MUHKOO_DEV_TOKEN || cfg.token;
@@ -61,20 +89,7 @@ export async function openClient(args) {
61
89
  // which would wipe a seed we had already put there.
62
90
  const user = await client.auth.zk.restore();
63
91
  if (!user) die("Your session has expired. Run `muhkoo login` and try again.");
64
-
65
- await unlock({ args, cfg, baseUrl, client });
66
-
67
- // Restore the working directory. A CLI is a fresh process each time, so `cd`
68
- // only means anything if it survives between invocations — the same reason a
69
- // shell keeps PWD. A directory that has since been deleted silently falls
70
- // back to the root rather than making every later command fail.
71
- if (cfg.vfsCwd && cfg.vfsCwd !== "/") {
72
- await client.vfs.cd(cfg.vfsCwd).catch(async () => {
73
- if (!args.readOnly) await saveConfig({ ...cfg, vfsCwd: "/" });
74
- info(`Working directory ${cfg.vfsCwd} is gone — back to /.`);
75
- });
76
- }
77
- return client;
92
+ return { cfg, baseUrl, client };
78
93
  }
79
94
 
80
95
  /**