tunneld 0.1.83 → 0.1.92

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
@@ -4,9 +4,9 @@
4
4
  coding agent, a container: one command, no account.
5
5
 
6
6
  [![npm](https://img.shields.io/npm/v/tunneld)](https://www.npmjs.com/package/tunneld)
7
- [![CI](https://github.com/tunnel-pizza/tunneld/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/tunnel-pizza/tunneld/actions/workflows/ci.yml)
7
+ [![CI](https://github.com/tunneld-it/tunneld/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/tunneld-it/tunneld/actions/workflows/ci.yml)
8
8
  [![Go Reference](https://pkg.go.dev/badge/github.com/tunnel-pizza/tunneld.svg)](https://pkg.go.dev/github.com/tunnel-pizza/tunneld)
9
- [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/tunnel-pizza/tunneld/badge)](https://scorecard.dev/viewer/?uri=github.com/tunnel-pizza/tunneld)
9
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/tunneld-it/tunneld/badge)](https://scorecard.dev/viewer/?uri=github.com/tunneld-it/tunneld)
10
10
  [![License: FSL-1.1-MIT](https://img.shields.io/badge/License-FSL--1.1--MIT-blue.svg)](./LICENSE.md)
11
11
 
12
12
  ```sh
@@ -46,10 +46,10 @@ its own to share your shell.
46
46
  - **npm, for good:** `npm install -g tunneld`, then drop the `npx` from
47
47
  everything below.
48
48
  - **Go:** `go install github.com/tunnel-pizza/tunneld@latest`
49
- - **A binary:** every [release](https://github.com/tunnel-pizza/tunneld/releases)
49
+ - **A binary:** every [release](https://github.com/tunneld-it/tunneld/releases)
50
50
  carries a signed one per platform, and [SECURITY.md](./SECURITY.md) has the
51
51
  recipe to verify it.
52
- - **A container:** `ghcr.io/tunnel-pizza/tunneld`, or `tunneld/tunneld` on
52
+ - **A container:** `ghcr.io/tunneld-it/tunneld`, or `tunneld/tunneld` on
53
53
  Docker Hub (`tunneld/debug` for `kubectl debug`). See
54
54
  [Running in a container](./docs/reference.md#running-in-a-container).
55
55
 
@@ -64,7 +64,7 @@ its own to share your shell.
64
64
  | 🤖 | `npx tunneld claude --resume` | A coding agent you can keep working with from your phone or another computer. |
65
65
  | ✏️ | `npx tunneld nvim` | Your editor, config and plugins included, in a browser tab. |
66
66
  | 🪟 | `npx tunneld :3000 :4000` | Two apps on one hostname, side by side in one window. |
67
- | 🍕 | `npx tunneld :3000 'npm run dev' claude` | `npm run dev`, your app, and a Claude session beside it. |
67
+ | 🚇 | `npx tunneld :3000 'npm run dev' claude` | `npm run dev`, your app, and a Claude session beside it. |
68
68
  | 🐳 | `npx tunneld attach://dockerd/web` | A running container's terminal. Compose service names work too. |
69
69
 
70
70
  Anything that runs in a terminal works: htop, a REPL, Codex, Gemini CLI,
@@ -220,6 +220,9 @@ flag has a `TUNNELD_*` environment variable, and the flag wins.
220
220
  | `--multiview=false` | `TUNNELD_MULTIVIEW` | No panel; each origin keeps only its own `?n` address. |
221
221
  | `--identity-providers=` | `TUNNELD_IDENTITY_PROVIDERS` | Mint anonymously. |
222
222
  | `--qr` | `TUNNELD_QR` | Print your address as a QR code too, and open no tab. Scan it with your phone. |
223
+ | `-s` | `TUNNELD_SILENT` | Print only your address, once the tunnel is up, for a script: `read -r url < <(tunneld -s :3000)`. |
224
+ | `-d` | `TUNNELD_DETACH` | Run in the background and give the prompt back once the address is out. See [In the background](#in-the-background). |
225
+ | `-l` | `TUNNELD_LIST` | List every run on this machine; with `-s`, as JSON. |
223
226
  | `--log-level debug` | `TUNNELD_LOG` | Log to stderr. Silent by default. |
224
227
  | `--provider <host>` | `TUNNELD_PROVIDER` | Mint against another provider. Default `tunnel.pizza`. |
225
228
  | *(the arguments)* | `TUNNELD_ORIGINS` | Origins, comma-separated. |
@@ -233,16 +236,18 @@ Every flag and variable in full: [docs/reference.md](./docs/reference.md#flags).
233
236
 
234
237
  ### In the background
235
238
 
236
- Under `npx`, `-d` as the first word detaches: the addresses print, the prompt
237
- comes back, and the tunnel stays up; run again, it hands back the run
238
- already going. `npx tunneld -k` ends every tunneld run on the machine,
239
- detached or not, the way Ctrl+C would, and `-k<pid>` ends just that one.
240
- `npx tunneld -l` lists them. All three belong to the npm launcher, not to
241
- tunneld, and aren't available on Windows yet.
239
+ `-d` (or `TUNNELD_DETACH=1`) detaches: the addresses print, the prompt comes
240
+ back with a summary, and the tunnel stays up; run again, it hands back the
241
+ run already going. `-l` lists every run on the machine. With `-s`, `-d`
242
+ prints only the address and `-l` prints JSON. Under
243
+ `npx`, `npx tunneld -k` ends every tunneld run on the machine, detached or
244
+ not, the way Ctrl+C would, and `-k<pid>` ends just that one; `-k` belongs to
245
+ the npm launcher. None of them is available on Windows yet.
242
246
 
243
247
  ```sh
244
- npx tunneld -d :3000 claude
245
- npx tunneld -l
248
+ tunneld -d :3000 claude
249
+ addr=$(tunneld -d -s :3000)
250
+ tunneld -l
246
251
  npx tunneld -k73146 # end the run with pid 73146
247
252
  npx tunneld -k # end every run
248
253
  ```
@@ -355,13 +360,13 @@ And the rest of what tunneld requires directly:
355
360
 
356
361
  And Go itself. The license notices for the Go standard library and for every
357
362
  module a release binary links are in `THIRD_PARTY_LICENSES`, attached to each
358
- [release](https://github.com/tunnel-pizza/tunneld/releases) and shipped in the
363
+ [release](https://github.com/tunneld-it/tunneld/releases) and shipped in the
359
364
  npm package.
360
365
 
361
366
  ## Contributing
362
367
 
363
368
  [CONTRIBUTING.md](./CONTRIBUTING.md) has the layout, the dev loop, the
364
- conventions, and how a change gets from an issue to a release. Security
369
+ conventions, and how a change gets from a branch to a release. Security
365
370
  reports go through [SECURITY.md](./SECURITY.md).
366
371
 
367
372
  ## License
@@ -9,7 +9,7 @@
9
9
  "url": "https://github.com/scaffoldly"
10
10
  },
11
11
  "homepage": "https://tunnel.pizza",
12
- "repository": "https://github.com/tunnel-pizza/tunneld",
12
+ "repository": "https://github.com/tunneld-it/tunneld",
13
13
  "license": "FSL-1.1-MIT",
14
14
  "privacyPolicyUrl": "https://tunnel.pizza/privacy/",
15
15
  "mcpServers": "./mcp.json"
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "tunneld",
3
- "version": "0.1.83",
3
+ "version": "0.1.92",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "tunneld",
9
- "version": "0.1.83",
9
+ "version": "0.1.92",
10
10
  "license": "FSL-1.1-MIT",
11
11
  "bin": {
12
12
  "tunneld": "v1/v1.cjs"
@@ -15,10 +15,10 @@
15
15
  "node": ">=20"
16
16
  },
17
17
  "optionalDependencies": {
18
- "@tunnel-pizza/tunneld-darwin-arm64": "0.1.83",
19
- "@tunnel-pizza/tunneld-darwin-x64": "0.1.83",
20
- "@tunnel-pizza/tunneld-linux-arm64": "0.1.83",
21
- "@tunnel-pizza/tunneld-linux-x64": "0.1.83"
18
+ "@tunnel-pizza/tunneld-darwin-arm64": "0.1.92",
19
+ "@tunnel-pizza/tunneld-darwin-x64": "0.1.92",
20
+ "@tunnel-pizza/tunneld-linux-arm64": "0.1.92",
21
+ "@tunnel-pizza/tunneld-linux-x64": "0.1.92"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@opencode/plugin": "^2.0.32"
@@ -30,9 +30,9 @@
30
30
  }
31
31
  },
32
32
  "node_modules/@tunnel-pizza/tunneld-darwin-arm64": {
33
- "version": "0.1.83",
34
- "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-darwin-arm64/-/tunneld-darwin-arm64-0.1.83.tgz",
35
- "integrity": "sha512-B79iln3EBVzHe9xgoTIeXzRHmc7MAQxnn1v3rWbVPUfyhOdYHXiMOSQxKXOAsIzURihAbv8Y/DR4Z87x6GQ+ZQ==",
33
+ "version": "0.1.92",
34
+ "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-darwin-arm64/-/tunneld-darwin-arm64-0.1.92.tgz",
35
+ "integrity": "sha512-ocqoOgzDpUSvCd1igIoEn2xE2hzjAc6ziuzbGnydrldrpQtX/f1YYkDi9LZQCceTrhVh5zfvkjKqbgxOevkjpg==",
36
36
  "cpu": [
37
37
  "arm64"
38
38
  ],
@@ -43,9 +43,9 @@
43
43
  ]
44
44
  },
45
45
  "node_modules/@tunnel-pizza/tunneld-darwin-x64": {
46
- "version": "0.1.83",
47
- "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-darwin-x64/-/tunneld-darwin-x64-0.1.83.tgz",
48
- "integrity": "sha512-5R+n5tANatFenwzU/39fL1LRt73D7huyaoDre8+M5m16WPuGcE2G/IuO1giY5ZD2HvzzVLEDxEJY4ektzuappw==",
46
+ "version": "0.1.92",
47
+ "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-darwin-x64/-/tunneld-darwin-x64-0.1.92.tgz",
48
+ "integrity": "sha512-jO9Kml+PmTaqz+jkymW4ar+ZnO3TuKY6FO0Jqk27vBv50BwbKXH3gP++WZKTofvF8SllDVtEHXEaTqPBovMXFw==",
49
49
  "cpu": [
50
50
  "x64"
51
51
  ],
@@ -56,9 +56,9 @@
56
56
  ]
57
57
  },
58
58
  "node_modules/@tunnel-pizza/tunneld-linux-arm64": {
59
- "version": "0.1.83",
60
- "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-linux-arm64/-/tunneld-linux-arm64-0.1.83.tgz",
61
- "integrity": "sha512-yMBwhYlf6PlDJFlcRZ8wwOr9TryiUHCxUt55010UY4SH43CPWpzyICt67l8PwwCCPBlm98l0EVXhl+yAcEbUqQ==",
59
+ "version": "0.1.92",
60
+ "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-linux-arm64/-/tunneld-linux-arm64-0.1.92.tgz",
61
+ "integrity": "sha512-BG79IhUxVvTeT4VeAxujt1e03Iud6OCs3zQro9NQ0ch8jhYqcxSsWPsnxdzUnWieEeWBzcYKm1W6IyTRuSuzSw==",
62
62
  "cpu": [
63
63
  "arm64"
64
64
  ],
@@ -69,9 +69,9 @@
69
69
  ]
70
70
  },
71
71
  "node_modules/@tunnel-pizza/tunneld-linux-x64": {
72
- "version": "0.1.83",
73
- "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-linux-x64/-/tunneld-linux-x64-0.1.83.tgz",
74
- "integrity": "sha512-QvZBvEzUjaNlaL9km+FGsWF7SoL0LJxBbuXZf0er6Z0PKWuDN1GreeV9AKu13OYmKxo1+jxbLbGjhWSmi07wXQ==",
72
+ "version": "0.1.92",
73
+ "resolved": "https://registry.npmjs.org/@tunnel-pizza/tunneld-linux-x64/-/tunneld-linux-x64-0.1.92.tgz",
74
+ "integrity": "sha512-graaut2RzGJBOVdobaimL/R62u5nKy0yoxuxMq5OpAyATmN3Z/BebGS0SBcrGpqRlx4sl+oZu4o6g7Nv3Id71w==",
75
75
  "cpu": [
76
76
  "x64"
77
77
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tunneld",
3
- "version": "0.1.83",
3
+ "version": "0.1.92",
4
4
  "description": "Slice and share your machine on a public URL: a port, a shell, a coding agent, a container, or your whole workspace. One command, no account.",
5
5
  "engines": {
6
6
  "node": ">=20"
@@ -25,11 +25,11 @@
25
25
  ],
26
26
  "homepage": "https://tunnel.pizza",
27
27
  "bugs": {
28
- "url": "https://github.com/tunnel-pizza/tunneld/issues"
28
+ "url": "https://github.com/tunneld-it/tunneld/issues"
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "git+https://github.com/tunnel-pizza/tunneld.git"
32
+ "url": "git+https://github.com/tunneld-it/tunneld.git"
33
33
  },
34
34
  "license": "FSL-1.1-MIT",
35
35
  "author": "Scaffoldly <legal@scaffold.ly> (https://github.com/scaffoldly)",
@@ -64,9 +64,9 @@
64
64
  }
65
65
  },
66
66
  "optionalDependencies": {
67
- "@tunnel-pizza/tunneld-darwin-arm64": "0.1.83",
68
- "@tunnel-pizza/tunneld-darwin-x64": "0.1.83",
69
- "@tunnel-pizza/tunneld-linux-arm64": "0.1.83",
70
- "@tunnel-pizza/tunneld-linux-x64": "0.1.83"
67
+ "@tunnel-pizza/tunneld-darwin-arm64": "0.1.92",
68
+ "@tunnel-pizza/tunneld-darwin-x64": "0.1.92",
69
+ "@tunnel-pizza/tunneld-linux-arm64": "0.1.92",
70
+ "@tunnel-pizza/tunneld-linux-x64": "0.1.92"
71
71
  }
72
72
  }
package/v1/v1.cjs CHANGED
@@ -29,20 +29,16 @@
29
29
  //
30
30
  // - It refuses a command line that could mean two things (misread, below),
31
31
  // before the run starts, and names the quoting for each.
32
- // - `-d`, as the first word, detaches: the run goes on in the background,
33
- // and once it signals that its addresses are out the launcher hands the
34
- // console back.
35
32
  // - `-k`, as the only word, ends every run on the machine, detached or
36
33
  // not, the way Ctrl-C would, so the teardown runs; `-k<pid>` ends the one
37
34
  // run with that pid. The binary registers each run as a file it holds
38
35
  // open beside its cached spec (v1alpha1/cache), and -k ends whatever
39
36
  // holds one.
40
- // - `-l`, as the only word, lists every run, a line each.
41
37
  //
42
- // One flag at most, read only as the first word and stripped before the
43
- // binary sees the line. Anything else, -kd included, is the binary's, and it
44
- // refuses them as unknown flags, so a brew user and an npx user never mean
45
- // different things by the same flag.
38
+ // -d and -l are the binary's own flags, so they reach it as typed. -k is
39
+ // read only as the only word; anything else, -kd included, is the binary's,
40
+ // and it refuses them as unknown flags, so a brew user and an npx user never
41
+ // mean different things by the same flag.
46
42
 
47
43
  const { spawn, execFileSync } = require("child_process");
48
44
  const fs = require("fs");
@@ -68,16 +64,11 @@ const PLATFORMS = [
68
64
  const STOP_MS = 30000;
69
65
  const POLL_MS = 50;
70
66
 
71
- // What the binary reads to know a launcher is waiting on it; see detach.
72
- const NOTIFY_ENV = "TUNNELD_NOTIFY_PID";
73
67
 
74
68
  // The status the binary exits with when another run holds this one's
75
69
  // lock. Keep in step with RunningCode in v1/v1.go.
76
70
  const RUNNING = 75;
77
71
 
78
- // Names fd 3, the pipe a detaching launcher reads a refused run's status
79
- // from: a line of JSON naming the run holding the lock, { pid, env, log }.
80
- const STATUS_ENV = "TUNNELD_STATUS_FD";
81
72
 
82
73
  function fail(message) {
83
74
  console.error(`${WRAPPER_NAME}: ${message}`);
@@ -290,12 +281,15 @@ function ambiguous(reading, prefix) {
290
281
  );
291
282
  }
292
283
 
293
- // cacheDir is <user cache dir>/tunneld, the binary's own: Go's
294
- // os.UserCacheDir, spelled out. Each run registers there as <key>.pid, beside
295
- // its spec, and logs to <key>.log, which the binary writes and this only
296
- // names.
284
+ // cacheDir is the binary's own: $TUNNELD_CACHE_DIR when set, else
285
+ // <user cache dir>/tunneld, Go's os.UserCacheDir spelled out. Each run
286
+ // registers there as <key>.pid, beside its spec, and logs to <key>.log, which
287
+ // the binary writes and this only names.
297
288
  function cacheDir() {
298
289
  const env = process.env;
290
+ if (env.TUNNELD_CACHE_DIR) {
291
+ return path.resolve(env.TUNNELD_CACHE_DIR);
292
+ }
299
293
  let base;
300
294
  switch (process.platform) {
301
295
  case "darwin":
@@ -335,97 +329,37 @@ function signum(signal) {
335
329
  return 128 + (constants.signals[signal] ?? 0);
336
330
  }
337
331
 
338
- // start runs the binary on this process's own streams, in the foreground or,
339
- // detaching, in a session of its own.
340
- //
341
- // In the foreground this process waits for it and passes on its exit status.
342
- // Listening keeps this process alive until the child is done. On POSIX a
343
- // signal is also forwarded, for the case where only this pid was targeted. On
344
- // Windows the console has already delivered Ctrl-C to the child, and
345
- // child.kill() there is TerminateProcess, which would cut teardown short, so
346
- // it only waits.
347
- //
348
- // Detaching, it waits instead to be told the run is up. stdin is /dev/null —
349
- // a background run has nothing to read, and must not hold the terminal's. The
350
- // banner, the addresses and the origin each reaches go straight to the
351
- // caller, the same two streams a foreground run uses. The telling is a
352
- // signal: the binary is given this process's pid in NOTIFY_ENV, and once its
353
- // addresses are out it points its stdout and stderr at its log, <key>.log
354
- // beside its spec, and sends SIGUSR2 to its parent when that is the pid it was
355
- // given — and to nobody otherwise, since SIGUSR2 ends a process that has not
356
- // asked for it. Moving the streams first is what lets the caller go: once this
357
- // exits, nothing holds them, so `$(npx tunneld -d …)` returns, and nothing
358
- // lands on a prompt later. SIGUSR2 rather than SIGUSR1, which Node keeps for
359
- // its debugger.
360
- //
361
- // A detached run that ends first — a bad origin, --help, a mint that fails —
362
- // has already said so on the caller's streams, and its exit status is passed
363
- // on, so it fails like a foreground one. One refused because the same run is
364
- // already going names that run on the status pipe, fd 3, and that run
365
- // is handed back instead, the way this one would have been: its addresses on
366
- // stdout, the summary on stderr, status 0. Ctrl-C while waiting ends the run: it
367
- // is in a session of its own, where the terminal's Ctrl-C does not reach, and
368
- // nothing has been handed back yet.
369
- function start(bin, args, detaching) {
332
+ // start runs the binary on this process's own streams and waits for it,
333
+ // passing on its exit status. Listening keeps this process alive until the
334
+ // child is done. On POSIX a signal is also forwarded, for the case where only
335
+ // this pid was targeted. On Windows the console has already delivered Ctrl-C
336
+ // to the child, and child.kill() there is TerminateProcess, which would cut
337
+ // teardown short, so it only waits. -d is the binary's: it starts the run
338
+ // again in the background itself and exits once it is up, so this does too.
339
+ function start(bin, args) {
370
340
  const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
371
- let interrupted = null;
372
341
  let child = null;
373
342
 
374
343
  // Leaves by letting the event loop run dry rather than process.exit, which
375
344
  // can cut short a write to a pipe that is still draining.
376
345
  const finish = (status) => {
377
- for (const signal of [...signals, "SIGUSR2"]) {
346
+ for (const signal of signals) {
378
347
  process.removeAllListeners(signal);
379
348
  }
380
349
  process.exitCode = status;
381
350
  };
382
351
 
383
- if (detaching) {
384
- // Listening before the spawn: unheard, SIGUSR2 would end this process.
385
- process.on("SIGUSR2", () => {
386
- child.removeAllListeners("close");
387
- child.stdio[3].destroy();
388
- child.unref();
389
- finish(0);
390
- process.stderr.write(summary(located(child.pid)));
391
- });
392
- }
393
352
  for (const signal of signals) {
394
353
  process.on(signal, () => {
395
- if (detaching) {
396
- interrupted = signal;
397
- child.kill("SIGINT");
398
- } else if (process.platform !== "win32") {
354
+ if (process.platform !== "win32") {
399
355
  child.kill(signal);
400
356
  }
401
357
  });
402
358
  }
403
359
 
404
- child = spawn(
405
- bin,
406
- args,
407
- detaching
408
- ? {
409
- detached: true,
410
- stdio: ["ignore", "inherit", "inherit", "pipe"],
411
- env: { ...process.env, [NOTIFY_ENV]: String(process.pid), [STATUS_ENV]: "3" },
412
- }
413
- : { stdio: "inherit" },
414
- );
360
+ child = spawn(bin, args, { stdio: "inherit" });
415
361
  child.on("error", (err) => fail(`cannot run ${bin}: ${err.message}`));
416
- let status = "";
417
- if (detaching) {
418
- child.stdio[3].setEncoding("utf8").on("data", (chunk) => (status += chunk));
419
- }
420
- // close rather than exit: it waits for the status pipe to be read.
421
362
  child.on("close", (code, signal) => {
422
- const running = code === RUNNING && parse(status);
423
- if (running?.pid > 0) {
424
- finish(0);
425
- process.stdout.write(addresses(running));
426
- process.stderr.write(summary(running, true));
427
- return;
428
- }
429
363
  if (code === RUNNING) {
430
364
  console.error(
431
365
  `${WRAPPER_NAME}: it is running in the background or another terminal: npx ${WRAPPER_NAME} -k ends it`,
@@ -433,195 +367,13 @@ function start(bin, args, detaching) {
433
367
  }
434
368
  // Node ignores some signals itself (SIGPIPE), so re-raising is
435
369
  // unreliable; report a signal death the way a shell does, 128+signum.
436
- finish(interrupted ? signum(interrupted) : signal ? signum(signal) : (code ?? 1));
370
+ finish(signal ? signum(signal) : (code ?? 1));
437
371
  });
438
372
  }
439
373
 
440
- // parse is the status a refused run wrote, a line of JSON, or null.
441
- function parse(text) {
442
- try {
443
- return JSON.parse(text);
444
- } catch {
445
- return null;
446
- }
447
- }
448
-
449
374
  // paint wraps text in an ANSI style when stderr is a terminal that wants
450
375
  // one: not when it is a file or a pipe, not under NO_COLOR, not on a dumb
451
376
  // terminal.
452
- const COLOR = process.stderr.isTTY && !("NO_COLOR" in process.env) && process.env.TERM !== "dumb";
453
- const paint = (code) => (text) => (COLOR ? `\x1b[${code}m${text}\x1b[0m` : text);
454
- const bold = paint("1");
455
- const dim = paint("2");
456
- const cyan = paint("36");
457
-
458
- // home spells a path under $HOME the short way.
459
- function home(p) {
460
- const h = process.env.HOME;
461
- return h && p.startsWith(h + path.sep) ? "~" + p.slice(h.length) : p;
462
- }
463
-
464
- // envFile reads the NAME='value' lines the binary caches a run's spec and
465
- // settings in. The spec line is among them and is never shown.
466
- function envFile(text) {
467
- const out = {};
468
- for (const line of text.split("\n")) {
469
- const m = line.match(/^([A-Z_][A-Z0-9_]*)='(.*)'$/);
470
- if (m) {
471
- out[m[1]] = m[2];
472
- }
473
- }
474
- return out;
475
- }
476
-
477
- // label is an origin as a person reads it: a program as its path and
478
- // arguments rather than the exec:// URL carrying them.
479
- function label(origin) {
480
- try {
481
- const u = new URL(origin);
482
- if (u.protocol === "exec:") {
483
- return [decodeURIComponent(u.pathname), ...u.searchParams.getAll("arg")].map(quote).join(" ");
484
- }
485
- } catch {
486
- // Not a URL this can read; shown as it is.
487
- }
488
- return origin;
489
- }
490
-
491
- // wrap breaks prose into lines of at most width visible columns, indented,
492
- // never inside a word, so a URL stays whole to be clicked. Styling is applied
493
- // by the caller's style function to each word, after measuring.
494
- function wrap(words, width, indent) {
495
- const lines = [];
496
- let line = [];
497
- let used = 0;
498
- for (const [text, style] of words) {
499
- if (line.length > 0 && used + 1 + text.length > width) {
500
- lines.push(indent + line.join(" "));
501
- line = [];
502
- used = 0;
503
- }
504
- used += (line.length > 0 ? 1 : 0) + text.length;
505
- line.push(style(text));
506
- }
507
- if (line.length > 0) {
508
- lines.push(indent + line.join(" "));
509
- }
510
- return lines;
511
- }
512
-
513
- // prose is what the run shares, said as a sentence: the origins, the address
514
- // they are tunneled through, and that the list below is where each one
515
- // answers. In lines of at most columns, indent included.
516
- function prose(host, origins, columns) {
517
- const plain = (s) => s.split(" ").map((w) => [w, (x) => x]);
518
- const one = origins.length === 1;
519
- const names = `(${origins.map(label).join(", ")})`;
520
- const base = `https://${host}/`;
521
- const words = [
522
- ...plain(one ? "Your application" : "Your applications"),
523
- ...plain(names),
524
- ...plain(one ? "is now available, tunneled through" : "are now available, tunneled through"),
525
- [base + ",", () => cyan(base) + ","],
526
- ...plain(one ? "at the following address:" : "at the following addresses:"),
527
- ];
528
- return wrap(words, Math.max(columns, 24) - 2, " ");
529
- }
530
-
531
- // located is the run pid is, the way a refused run's status names one: its
532
- // pid, and the paths of its <key>.env and <key>.log, those that exist. The key
533
- // is found by the <key>.pid naming the pid.
534
- function located(pid) {
535
- const dir = cacheDir();
536
- const run = { pid };
537
- try {
538
- const key = fs
539
- .readdirSync(dir)
540
- .filter((n) => n.endsWith(".pid"))
541
- .map((n) => path.basename(n, ".pid"))
542
- .find((k) => read(path.join(dir, `${k}.pid`)).trim() === String(pid));
543
- for (const ext of key ? ["env", "log"] : []) {
544
- const file = path.join(dir, `${key}.${ext}`);
545
- if (fs.existsSync(file)) {
546
- run[ext] = file;
547
- }
548
- }
549
- } catch {
550
- // No cache directory: nothing more to say than the pid.
551
- }
552
- return run;
553
- }
554
-
555
- // cached is the settings the run <key> cached in <key>.env: empty when there
556
- // are none.
557
- function cached(key) {
558
- return envFile(read(path.join(cacheDir(), `${key}.env`)));
559
- }
560
-
561
- // addresses is what run is printed on stdout, one per line, the way the
562
- // binary prints them: the panel's address alone when there is a panel,
563
- // otherwise one per origin, routed by ?n when there is more than one. Empty
564
- // with no cached settings.
565
- function addresses(run) {
566
- const env = run.env ? envFile(read(run.env)) : {};
567
- const host = env.LIBTUNNEL_HOSTNAME;
568
- if (!host) {
569
- return "";
570
- }
571
- const base = `https://${host}/`;
572
- const origins = (env.TUNNELD_ORIGINS || "").split(",").filter(Boolean);
573
- if (origins.length <= 1 || env.TUNNELD_MULTIVIEW === "true") {
574
- return `${base}\n`;
575
- }
576
- return origins.map((_, i) => `${base}?${i}\n`).join("");
577
- }
578
-
579
- // summary is what a detached run is handed back with: the directory it runs
580
- // in, its pid and its log, then what it shares and the address each origin
581
- // answers on. run is { pid, env, log }, as located finds it or a refused
582
- // run's status names it.
583
- //
584
- // Read from <key>.env, the settings the run settled on and the hostname it
585
- // got, which it writes before signalling. A run under --no-cache writes no
586
- // .env, and gets what its pid and log alone can say; its addresses are on
587
- // stdout above either way.
588
- function summary(run, already = false) {
589
- const env = run.env ? envFile(read(run.env)) : {};
590
-
591
- const lines = ["", `${bold(`🍕 tunneld is ${already ? "already" : "now"} running in the background`)}`, ""];
592
- const row = (name, value) => lines.push(` ${dim(name)} ${value}`);
593
- if (env.PWD) {
594
- row("cwd", home(env.PWD));
595
- }
596
- row("pid", String(run.pid));
597
- if (run.log) {
598
- row("log", home(run.log));
599
- }
600
- lines.push("");
601
-
602
- const host = env.LIBTUNNEL_HOSTNAME;
603
- if (!host) {
604
- lines.push(" Your application is now available at the address above.", "");
605
- } else {
606
- const base = `https://${host}/`;
607
- const origins = (env.TUNNELD_ORIGINS || "").split(",").filter(Boolean);
608
- // The terminal's width where there is one, so a narrow window does not
609
- // wrap every line a second time; 78 at most, and 78 into a file.
610
- const columns = Math.min(process.stderr.columns || 78, 78);
611
- lines.push(...prose(host, origins, columns), "");
612
- if (origins.length <= 1) {
613
- lines.push(` ${cyan(base)} ${dim("→")} ${label(origins[0] || "")}`);
614
- } else {
615
- const width = `${base}?${origins.length - 1}`.length;
616
- if (env.TUNNELD_MULTIVIEW === "true") {
617
- lines.push(` ${cyan(base.padEnd(width))} ${dim("→")} multiview`);
618
- }
619
- origins.forEach((o, i) => lines.push(` ${cyan(`${base}?${i}`.padEnd(width))} ${dim("→")} ${label(o)}`));
620
- }
621
- lines.push("");
622
- }
623
- return lines.join("\n") + "\n";
624
- }
625
377
 
626
378
  // holders is every run holding file open. A run keeps its <key>.pid open
627
379
  // for as long as it runs, and the kernel closes it however the process ends,
@@ -723,29 +475,6 @@ function runs() {
723
475
  return found;
724
476
  }
725
477
 
726
- // list prints every run on stdout, a line each: its pid, then its address,
727
- // directory and origins from its cached settings, or its key when there are
728
- // none.
729
- function list() {
730
- const found = runs();
731
- if (found.length === 0) {
732
- console.error(`${WRAPPER_NAME}: no runs`);
733
- return;
734
- }
735
- const width = Math.max(...found.map((r) => String(r.pid).length));
736
- for (const { pid, key } of found) {
737
- const env = cached(key);
738
- const fields = env.LIBTUNNEL_HOSTNAME
739
- ? [
740
- `https://${env.LIBTUNNEL_HOSTNAME}/`,
741
- env.PWD && home(env.PWD),
742
- (env.TUNNELD_ORIGINS || "").split(",").filter(Boolean).map(label).join(", "),
743
- ]
744
- : [key];
745
- console.log([String(pid).padEnd(width), ...fields.filter(Boolean)].join(" "));
746
- }
747
- }
748
-
749
478
  // kill ends every run, or only the one whose pid is only, with SIGINT, which
750
479
  // is what Ctrl-C sends a foreground run, so each one tears down the way it
751
480
  // would there: the programs its origins started, the attach servers, the
@@ -801,44 +530,32 @@ function kill(only) {
801
530
  }, POLL_MS);
802
531
  }
803
532
 
804
- // FLAG is the launcher's own first word, one at most: -d, -l, -k, or -k with
805
- // a pid and no space between.
806
- const FLAG = /^-(?:d|l|k([1-9]\d*)?)$/;
533
+ // FLAG is the launcher's own first word: -k, or -k with a pid and no space
534
+ // between.
535
+ const FLAG = /^-k([1-9]\d*)?$/;
807
536
 
808
537
  function main() {
809
538
  const argv = process.argv.slice(2);
810
539
  const match = FLAG.exec(argv[0] ?? "");
811
- const flag = match ? argv[0].slice(0, 2) : null;
812
- const args = match ? argv.slice(1) : argv;
813
- if (flag) {
814
- refuseOnWindows(flag);
815
- }
816
-
817
- if (flag === "-k") {
818
- if (args.length > 0) {
540
+ if (match) {
541
+ refuseOnWindows("-k");
542
+ if (argv.length > 1) {
819
543
  fail("-k takes no arguments: it ends every run, or the one -k<pid> names");
820
544
  }
821
545
  kill(match[1] && Number(match[1]));
822
546
  return;
823
547
  }
824
- if (flag === "-l") {
825
- if (args.length > 0) {
826
- fail("-l takes no arguments: it lists every run");
827
- }
828
- list();
829
- return;
830
- }
831
548
 
832
- const reading = misread(args);
549
+ const reading = misread(argv);
833
550
  if (reading) {
834
- fail(ambiguous(reading, flag ? `npx ${WRAPPER_NAME} ${flag}` : `npx ${WRAPPER_NAME}`));
551
+ fail(ambiguous(reading, `npx ${WRAPPER_NAME}`));
835
552
  }
836
553
 
837
- start(binaryPath(), args, flag === "-d");
554
+ start(binaryPath(), argv);
838
555
  }
839
556
 
840
557
  if (require.main === module) {
841
558
  main();
842
559
  }
843
560
 
844
- module.exports = { ambiguous, isOrigin, misread, onPath, prose, quote };
561
+ module.exports = { ambiguous, isOrigin, misread, onPath, quote };