localgate 0.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.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 localgate contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
package/README.md ADDED
@@ -0,0 +1,287 @@
1
+ # localgate
2
+
3
+ Stable names for local dev servers.
4
+
5
+ [![CI](https://github.com/ludekvodicka/LocalGate/actions/workflows/ci.yml/badge.svg)](https://github.com/ludekvodicka/LocalGate/actions/workflows/ci.yml)
6
+ [![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
7
+
8
+ > **Windows only today.** The process-tree kill behind `restart` is written for Windows, and the
9
+ > port-80 bind assumes it. See [Status](#status).
10
+
11
+ One reverse proxy on port 80 multiplexes every dev server on the machine by `Host` header, so each app
12
+ gets a name instead of a port: `myapp.localhost` locally, and optionally `myapp.<label>.<your-domain>`
13
+ so a colleague on the LAN can open it too.
14
+
15
+ It also owns the dev process it starts, which is the part editors cannot give you: `localgate restart`
16
+ restarts the server running inside your editor's debug terminal, without stealing the route or the
17
+ terminal, and `localgate status --json` answers where and how an app is running - the answer a coding
18
+ agent needs before it touches anything.
19
+
20
+ ![localgate running a dev server, answering on its name, and restarting it from another terminal](docs/media/localgate-terminal.png)
21
+
22
+ ```bash
23
+ localgate run npm run dev # in your editor's launch config
24
+ localgate list # what is running
25
+ localgate restart # restart the app in this directory
26
+ localgate status --json # machine-readable state
27
+ ```
28
+
29
+ ## How it routes
30
+
31
+ Every dev server binds an ephemeral loopback port and registers its name. The proxy routes by the
32
+ `Host` header, so nothing else has to listen on 80.
33
+
34
+ ```mermaid
35
+ flowchart LR
36
+ local["this PC"] -->|"local: myapp.localhost"| proxy
37
+ lan["the LAN"] -->|"lan: myapp.dev.example.com"| proxy
38
+ net["a tunnel"] -->|"internet: same name"| proxy
39
+
40
+ proxy["localgate :80"]
41
+
42
+ proxy --> app1["myapp :41000"]
43
+ proxy --> app2["docs :41001"]
44
+ proxy --> alias["alias :8001"]
45
+ ```
46
+
47
+ The tunnel is not localgate's - it is whatever you already run, pointed at this machine's LAN address
48
+ on port 80. It keeps the `Host` header, so a request arriving through it is routed exactly like a LAN
49
+ request and localgate needs to know nothing about it.
50
+
51
+ The table lives in the proxy's memory. There is no config file listing apps, no port assignment to keep
52
+ in sync, and nothing to clean up after a crash.
53
+
54
+ ## How restart works
55
+
56
+ The runner sits in your editor's debug terminal and owns the dev server as its child. That is what lets
57
+ a restart keep the terminal, the debugger and the route, and swap only the process underneath.
58
+
59
+ With no argument, `localgate restart` asks the proxy which route owns the current directory, and posts
60
+ to that runner's own control endpoint, which listens on loopback and nowhere else.
61
+
62
+ ```mermaid
63
+ sequenceDiagram
64
+ participant Cli as localgate restart
65
+ participant Runner as localgate run
66
+ participant Dev as npm run dev
67
+
68
+ Cli->>Runner: POST /restart
69
+ Runner->>Dev: kill the tree
70
+ Runner->>Dev: spawn on the same port
71
+ Note over Cli,Dev: same route, same port, the browser keeps its URL
72
+ ```
73
+
74
+ While the dev server is down the proxy answers with a readable page instead of a connection error.
75
+ Websocket upgrades are proxied, and on Next.js dev endpoints (`/_next/*`, `/__nextjs*`) the `Origin`
76
+ and `Referer` headers are mapped back to `.localhost` - which is what hot module reload needs to work
77
+ through a shared name, without an `allowedDevOrigins` entry in the project.
78
+
79
+ ## Install
80
+
81
+ Windows and Node 24+.
82
+
83
+ ```bash
84
+ npm install -g localgate
85
+ ```
86
+
87
+ Or from a clone, which is also how you develop on it:
88
+
89
+ ```bash
90
+ git clone https://github.com/ludekvodicka/LocalGate.git
91
+ cd LocalGate
92
+ pnpm install
93
+ pnpm link --global # puts `localgate` on your PATH
94
+ ```
95
+
96
+ Then, in your editor's launch configuration, run the dev server through it:
97
+
98
+ ```jsonc
99
+ // .vscode/launch.json
100
+ {
101
+ "name": "dev",
102
+ "type": "node-terminal",
103
+ "request": "launch",
104
+ "command": "localgate run npm run dev"
105
+ }
106
+ ```
107
+
108
+ The debugger attaches to the child as usual, and the terminal stays where it is across a restart.
109
+
110
+ ### Starting it again while it is already running
111
+
112
+ Press the editor's restart button and you start a *second* `localgate run` for the same project, which
113
+ used to fail twice over: the name moved to the new runner and left the old one unreachable, and the
114
+ dev server then refused the port because of its own lock. So the collision is settled before anything
115
+ is claimed - the new run finds its predecessor and asks, in the debug terminal it was started from:
116
+
117
+ ```
118
+ localgate myapp.localhost is already running
119
+
120
+ command npm run dev
121
+ upstream 127.0.0.1:54382
122
+ started 2026-08-12 09:04:11
123
+ pids runner 45568, child 4544
124
+ debugger attached
125
+
126
+ Stop it and take over? [Y/n]
127
+ ```
128
+
129
+ Yes stops the old one the good way: its own runner kills the child tree, releases the port and
130
+ deregisters itself, so its terminal ends cleanly and the new run takes the name. Killing by pid is
131
+ only the fallback for a runner that no longer answers.
132
+
133
+ A route whose runner is already gone is not a question. Its dev server usually outlived it and still
134
+ holds the port - that is the orphan a framework lock trips over - so it is cleared and the name reused
135
+ without asking.
136
+
137
+ ## Commands
138
+
139
+ | Command | What it does |
140
+ |---|---|
141
+ | `localgate run [--force] <cmd...>` | Run a dev server behind its name. `--force` takes over a running one without asking. |
142
+ | `localgate list [--json]` | Show active routes. |
143
+ | `localgate status [app] [--json]` | State of one route, with a tail of its output in `--json`. |
144
+ | `localgate restart [app]` | Restart a route's dev server. |
145
+ | `localgate stop [app]` | Stop a route's dev server. |
146
+ | `localgate logs [app] [--lines N]` | Tail a route's captured output. |
147
+ | `localgate alias <name> <port>` | Register a static route, e.g. a docker service. |
148
+ | `localgate cloudflare-info` | Print the DNS and ingress entries to paste. |
149
+ | `localgate prune` | Drop routes whose runner is gone. |
150
+
151
+ With no `[app]` argument, a command acts on the route owning the current directory.
152
+
153
+ ## Working with a coding agent
154
+
155
+ An agent editing your project has a directory and no idea what is running in it. These three answer
156
+ that without it having to find, read or guess a port:
157
+
158
+ ```bash
159
+ localgate status --json # state, port, command, pids, whether a debugger is attached, log tail
160
+ localgate restart # restart this project's dev server, keeping its name and port
161
+ localgate list --json # everything running on the machine
162
+ ```
163
+
164
+ `status --json` includes the last lines the dev server printed, so an agent can check whether its
165
+ change compiled without owning the terminal it compiled in.
166
+
167
+ The takeover question above is the one place this matters for safety. **With no terminal to ask in -
168
+ an agent, a CI job - `localgate run` prints what is already running and exits 1 instead of asking.**
169
+ It never kills the developer's session by accident; `localgate run --force` takes over deliberately.
170
+
171
+ ## Names, and who can reach them
172
+
173
+ A project gets its name from `package.json`, and nothing else is needed to work locally:
174
+
175
+ | Mode | Names | Reachable from |
176
+ |---|---|---|
177
+ | `local` (default) | `myapp.localhost` | this machine |
178
+ | `lan` | + `myapp.<label>.<your-domain>` | any PC on the LAN that resolves your domain |
179
+ | `internet` | the same shared name | wherever you point a tunnel at it |
180
+
181
+ The name is the package name, with a scope dropped (`@org/web` becomes `web`), and it has to be a
182
+ usable hostname label. When the package name is long or is not what you want to type, override it -
183
+ that plus the mode is everything a repository ever carries:
184
+
185
+ ```jsonc
186
+ // package.json
187
+ {
188
+ "name": "@acme/myapp-frontend",
189
+ "localgate": {
190
+ "mode": "lan", // local (default) | lan | internet
191
+ "name": "myapp" // optional: the hostname label, instead of the package name
192
+ }
193
+ }
194
+ ```
195
+
196
+ Everything machine-specific stays out of the repository, in `~/.localgate/config.json`. This is where
197
+ your own domain is configured, and it is per machine, so two developers can serve the same project on
198
+ their own names without either name being in the repository:
199
+
200
+ ```jsonc
201
+ {
202
+ "label": "dev", // your machine, as a DNS label
203
+ "baseDomain": "example.com", // a domain whose wildcard resolves to this LAN
204
+ "lanIp": "192.0.2.10", // the address the proxy also listens on
205
+ "autoRestart": false // see "When a dev server stops answering"
206
+ }
207
+ ```
208
+
209
+ With that file present, `myapp` in mode `lan` also answers on `myapp.dev.example.com`. Browser-facing
210
+ environment variables (`NEXT_PUBLIC_*`, `*_PUBLIC_URL`, `AUTH_URL`, `NEXTAUTH_URL`) are rewritten from
211
+ `.localhost` to that name on the way into the dev server, in memory - a remote browser resolves
212
+ `.localhost` to itself, so those URLs have to carry the shared name, while server-side variables stay
213
+ on `.localhost` where they are faster and need no DNS.
214
+
215
+ For reaching an app from outside the LAN, `localgate cloudflare-info` prints the DNS record and the
216
+ tunnel ingress entry to paste. It prints them and stops: localgate holds no API token and sends nothing
217
+ anywhere. Worth knowing before you paste them: a specific DNS record wins over the wildcard, so from
218
+ then on the name goes out to Cloudflare and back for everyone, the LAN included, and anyone holding the
219
+ URL reaches the dev server. That is why `internet` is a mode you switch back from.
220
+
221
+ ## When a dev server stops answering
222
+
223
+ Detection is free: the proxy forwards every request anyway, so it sees the answer that never came. You
224
+ get a readable page instead of a browser error, and it says what to do next:
225
+
226
+ | State | What the browser gets |
227
+ |---|---|
228
+ | `starting` | 503, "still starting, reload in a moment" - a refused connection right after launch is normal |
229
+ | `dead` | 503, "nothing is listening" plus the `prune` hint - the route outlived its dev server |
230
+ | `unresponsive` | 504, after three requests in a row went unanswered, with the exact `localgate restart` line to run |
231
+
232
+ Acting on it is the risky half, so it is opt-in: `"autoRestart": true` in `~/.localgate/config.json`
233
+ lets the proxy restart an unresponsive app by itself, bounded by two attempts and a five-minute
234
+ cooldown. **It never fires while a debugger is attached** - a process paused at a breakpoint is
235
+ externally indistinguishable from a wedged one, and killing it would destroy the session you are
236
+ standing in. Reporting always happens; only the restart is behind the switch.
237
+
238
+ If the proxy itself dies, every running app notices within ten seconds and re-registers its route, so
239
+ the names come back without touching a single dev server.
240
+
241
+ ## Design rules
242
+
243
+ - **Nothing machine-specific is committed.** The label, the domain and the LAN IP live in
244
+ `~/.localgate/config.json`; a repository carries at most a `package.json` key holding a mode
245
+ (`local` / `lan` / `internet`) and an optional name. Absent key means `local`, which is names on
246
+ `.localhost` only.
247
+ - **Nothing is persisted.** The route table is live state; after a reboot nothing runs until the first
248
+ command. Aliases are part of that: `localgate alias` has to be re-run after the proxy exits.
249
+ - **The proxy is not a service.** The first `run` or `alias` starts it, and it exits once the last route
250
+ disappears.
251
+ - **No TLS, no tunnels of its own.** Plain http; put Cloudflare in front when something must be reachable
252
+ from the internet.
253
+
254
+ ## Security
255
+
256
+ The control API - register, restart, stop, enumerate - is served **only on the loopback listener**, and
257
+ each runner's restart endpoint listens on `127.0.0.1` alone. Nothing on the LAN can register a route or
258
+ restart a process.
259
+
260
+ The mode is enforced by the listener, not just by which names exist. A `.localhost` name means "the
261
+ machine that resolved it", so the LAN listener never answers one, whatever `Host` header a request
262
+ carries and whether it is a plain request or a websocket upgrade. An app in the default `local` mode
263
+ has no other name, which is what keeps it on this machine.
264
+
265
+ Those dev servers are the real exposure: in `lan` mode anyone on the network reaches them with no
266
+ authentication, and in `internet` mode anyone holding the URL does. Use `lan` on networks you trust, and
267
+ switch back from `internet` when you are done. There is no TLS - it is plain http by design.
268
+
269
+ ## Development
270
+
271
+ ```bash
272
+ pnpm run verify # type-check + lint + tests
273
+ pnpm test # vitest
274
+ ```
275
+
276
+ The CLI runs the TypeScript sources directly through tsx, so a working copy is always runnable with no
277
+ build step.
278
+
279
+ ## Status
280
+
281
+ Early, and Windows-only for now: the process-tree kill that makes `restart`
282
+ reliable is implemented with `taskkill`, and the port-80 bind assumes Windows' lack of privileged-port
283
+ restrictions. The rest is plain Node and portable.
284
+
285
+ Bug reports and ideas are welcome - see [CONTRIBUTING.md](CONTRIBUTING.md), and
286
+ [SECURITY.md](SECURITY.md) for anything security-related. Licensed under
287
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ // Runs the TypeScript sources directly through tsx: no build step, so a working copy is always
3
+ // runnable. A published build would swap this for compiled output.
4
+ import { register } from "tsx/esm/api";
5
+
6
+ register();
7
+
8
+ const { LocalgateCli } = await import("../logic/cli/localgateCli.ts");
9
+ process.exitCode = await LocalgateCli.main(process.argv.slice(2));
@@ -0,0 +1,56 @@
1
+ import type { LocalgateRoute } from "../proxy/localgateRegistry.ts";
2
+
3
+ // What a command prints once a name answers. `run` and `alias` share it because to the person reading the
4
+ // terminal they are the same event, and the one-line variant `alias` used to print is how a shared name
5
+ // goes unnoticed - the script that registered one then wrote its own `.localhost` block instead.
6
+ //
7
+ // The route is the ONLY input. The title, the column alignment and even which command to type next are
8
+ // decided here rather than passed in, because a caller that hand-builds a padded string is a caller that
9
+ // can drift out of format again.
10
+ export class LocalgateBanner
11
+ {
12
+ private static readonly labelWidthConst = 11;
13
+
14
+ static render(route: LocalgateRoute): string
15
+ {
16
+ const [local, shared] = route.names;
17
+ const [nextLabel, nextCommand] = LocalgateBanner.next(route);
18
+
19
+ const lines = ["", ` localgate ${LocalgateBanner.appName(route)} · ${LocalgateBanner.subtitle(route)}`, ""];
20
+
21
+ if (shared) lines.push(LocalgateBanner.row("shared", `http://${shared}`));
22
+ lines.push(LocalgateBanner.row("local", `http://${local}`));
23
+ lines.push("", LocalgateBanner.row("upstream", `127.0.0.1:${route.port}`), LocalgateBanner.row(nextLabel, nextCommand), "");
24
+
25
+ return `${lines.join("\n")}\n`;
26
+ }
27
+
28
+ // The short name a person types, which is the first label of the local name.
29
+ static appName(route: LocalgateRoute): string
30
+ {
31
+ return route.names[0].split(".")[0];
32
+ }
33
+
34
+ private static subtitle(route: LocalgateRoute): string
35
+ {
36
+ if (route.kind == "app") return `mode ${route.mode}`;
37
+ else if (route.kind == "alias") return `alias · mode ${route.mode}`;
38
+ else
39
+ throw new Error(`Unknown route kind: ${JSON.stringify(route.kind)}`);
40
+ }
41
+
42
+ // An alias has no process, so there is nothing to restart - what it needs instead is the line that puts
43
+ // it back, because the route table is in memory and a proxy restart drops it.
44
+ private static next(route: LocalgateRoute): [string, string]
45
+ {
46
+ if (route.kind == "app") return ["restart", "localgate restart"];
47
+ else if (route.kind == "alias") return ["re-add", `localgate alias ${LocalgateBanner.appName(route)} ${route.port}`];
48
+ else
49
+ throw new Error(`Unknown route kind: ${JSON.stringify(route.kind)}`);
50
+ }
51
+
52
+ private static row(label: string, value: string): string
53
+ {
54
+ return ` ${label.padEnd(LocalgateBanner.labelWidthConst)}${value}`;
55
+ }
56
+ }