portless-studio 0.1.1 → 0.1.2

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 (29) hide show
  1. package/README.md +60 -218
  2. package/dist/cli/mcp/studio-tools.js +20 -7
  3. package/dist/cli/studio-items.js +5 -2
  4. package/dist/core/api/contract.js +17 -1
  5. package/dist/core/config/studio-config.js +4 -0
  6. package/dist/core/doctor/platform-commands.js +5 -2
  7. package/dist/core/ports/direct-url.js +7 -0
  8. package/dist/core/ports/docker-container.js +85 -0
  9. package/dist/core/routes/route.js +14 -2
  10. package/dist/server/application/container-source.js +19 -0
  11. package/dist/server/application/doctor.js +199 -9
  12. package/dist/server/application/port-discovery.js +49 -6
  13. package/dist/server/application/short-urls.js +136 -0
  14. package/dist/server/application/studio-runtime.js +46 -7
  15. package/dist/server/application/terminals.js +142 -15
  16. package/dist/server/infrastructure/system/docker-containers.js +64 -0
  17. package/dist/server/infrastructure/system/doctor-facts.js +43 -1
  18. package/dist/server/infrastructure/system/port-forwarder.js +78 -0
  19. package/dist/server/infrastructure/system/process-runner.js +2 -1
  20. package/dist/server/infrastructure/terminal/process-tree.js +71 -0
  21. package/dist/server/infrastructure/terminal/terminal-spawner.js +44 -3
  22. package/dist/server/interface/http/http-server.js +38 -2
  23. package/dist/server/main.js +41 -6
  24. package/dist/web/assets/index-BDEwEBPS.js +15 -0
  25. package/dist/web/assets/index-C9wkL1J8.css +1 -0
  26. package/dist/web/index.html +2 -2
  27. package/package.json +1 -1
  28. package/dist/web/assets/index-B3KMqQ-l.js +0 -15
  29. package/dist/web/assets/index-CWujmTe4.css +0 -1
package/README.md CHANGED
@@ -22,7 +22,8 @@ The screenshots use the built-in sample data (`?demo`), not a real machine.
22
22
 
23
23
  ## Install
24
24
 
25
- Requires Node 22 or later.
25
+ Requirements: Node 22 or later on Linux, macOS or Windows. portless is installed for you if it is
26
+ missing.
26
27
 
27
28
  To try it, run it straight from npm:
28
29
 
@@ -40,18 +41,8 @@ portless-studio init
40
41
  The login service starts Studio from the files npm installed. Run through `npx`, those files sit in
41
42
  npm's cache, which npm can clear at any time, and then Studio stops starting at login.
42
43
 
43
- To update, install the latest version and restart Studio:
44
-
45
- ```bash
46
- npm i -g portless-studio@latest
47
- ```
48
-
49
- To remove it, undo the setup first, then remove the package:
50
-
51
- ```bash
52
- portless-studio uninstall
53
- npm rm -g portless-studio
54
- ```
44
+ To update, install the latest version and restart Studio with `npm i -g portless-studio@latest`.
45
+ To remove it, run `portless-studio uninstall`, then `npm rm -g portless-studio`.
55
46
 
56
47
  ## Quick start
57
48
 
@@ -63,61 +54,52 @@ npm rm -g portless-studio
63
54
  - Starts the portless proxy on port 1355 over HTTP, which needs no password, certificate or extra
64
55
  tool on macOS, Windows or Linux, and can set Studio to start when you log in.
65
56
 
66
- `init --yes` accepts every default without asking. After that, `portless-studio` starts
67
- Studio at `http://127.0.0.1:4949`, registers it in portless as `studio` and opens your browser.
68
- Your apps open at names like `http://web.myapp.localhost:1355`: Chrome, Edge and Firefox send
69
- every `*.localhost` name to your own machine, with no hosts file and no DNS setup.
57
+ Then `portless-studio` starts Studio at `http://127.0.0.1:4949`, registers it in portless as
58
+ `studio` and opens your browser. Your apps open at names like `http://web.myapp.localhost:1355`:
59
+ Chrome, Edge and Firefox send every `*.localhost` name to your own machine, with no hosts file and
60
+ no DNS setup. Turn on Short URLs in Settings to drop the `:1355` (`http://web.myapp.localhost`)
61
+ where your system lets you listen on port 80; see
62
+ [troubleshooting](docs/troubleshooting.md#names-have-1355-and-i-want-port-80).
70
63
 
71
- To try the page without portless, start Studio and open `http://127.0.0.1:4949/?demo` for sample
72
- data, or `?demo&noportless` to walk through the install flow with a fake install.
64
+ [Getting started](docs/getting-started.md) walks through the first five minutes: naming a dev
65
+ server, naming a port by hand, running a command in a Studio terminal and turning on auto mode.
66
+ To try the page without portless, open `http://127.0.0.1:4949/?demo` for sample data.
73
67
 
74
68
  ## What it does
75
69
 
76
70
  - **Live list** of portless routes: name, URL, port, app or alias, uptime and any Tailscale, ngrok
77
71
  or Cloudflare tunnel. It updates as soon as a route appears or goes away.
78
72
  - **Detected ports:** listening ports that answer HTTP but have no name yet, with the process, the
79
- project folder and a suggested name. Name one, name them all, or ignore one.
80
- - **Auto mode** (off by default): every new dev server gets its suggested name by itself, once
81
- it has shown up unnamed in two scans in a row, so a server started with `portless <name>` keeps
82
- the name portless gives it. Names auto mode gave are removed 10 minutes after their port closes.
73
+ project folder and a suggested name. Name one, name them all, or ignore one. Ports a running
74
+ Docker container publishes show here too.
75
+ - **Auto mode** (off by default): every new dev server gets its suggested name by itself. Names
76
+ auto mode gave are removed 10 minutes after their port closes.
83
77
  - **Name a port** that portless did not start, such as a database in Docker (`db` gives
84
78
  `db.localhost`). Rename or remove those names later.
85
79
  - **Where each server runs:** every row shows the git branch, the linked worktree and the project
86
- folder. A route whose server is down keeps the last place it ran.
87
- - **Views:** group by project, worktree, kind or status, or into your own groups; sort, and
88
- filter by kind, status or project from the Filter button; save the combination as a named view
89
- and pick the one that opens by default.
80
+ folder.
81
+ - **Views:** group by project, worktree, kind or status, or into your own groups; filter, sort and
82
+ save the combination as a named view.
90
83
  - **Terminals:** run a command per project inside Studio and watch its output in a panel. You, the
91
- CLI and your AI agents drive the same terminals. See [docs/terminals.md](docs/terminals.md).
84
+ CLI and your AI agents drive the same terminals.
92
85
  - **Agents:** an MCP server lets Claude Code, Cursor and other agents list, name and stop dev
93
- servers and run commands in Studio terminals. See [docs/mcp.md](docs/mcp.md).
94
- - **Open with:** right-click a row to open its folder in an editor found on your machine (VS Code,
95
- Cursor, Zed, WebStorm, IntelliJ IDEA, Sublime Text), reveal it in the file manager or copy its
96
- path. Add your own apps in Settings.
97
- - **Health:** each route's dot says whether its port answers. The Health panel and
98
- `portless-studio doctor` find a proxy left running as root, two proxies at once, an untrusted
99
- certificate or a taken port, and give the fix.
100
- - **Proxy and Studio control:** start, stop or restart the portless proxy, and stop or restart
101
- Studio, from the page.
102
- - **Stop a dev server** from its row, after a confirmation. Only your own processes on a port
103
- Studio shows.
104
- - **Project icons:** the favicon the server serves, else one in the project folder, else the
105
- project's initials. Set an emoji or an image per project in Settings.
106
- - **Notifications** in the page when a server is detected or named, and optionally from the
107
- operating system.
108
- - **Install portless** from the page when the `portless` command is missing.
86
+ servers and run commands in Studio terminals.
87
+ - **Health:** each route's dot says whether its port answers, and `portless-studio doctor` finds a
88
+ proxy left running as root, two proxies at once, an untrusted certificate or a taken port, and
89
+ gives the fix.
109
90
  - **Keyboard first:** every action has a key, `?` lists them and `Ctrl+K` (`Cmd+K`) runs any of
110
91
  them by name.
111
92
 
112
- ## How naming works
93
+ Also: start, stop or restart the proxy and Studio from the page, stop a dev server from its row,
94
+ open a project in your editor, project icons, and notifications in the page or from the operating
95
+ system.
96
+
97
+ ## Naming
113
98
 
114
99
  A suggested name says what the work is, then the app, then the repository. The package
115
100
  `@acme/web` in the `acme-monorepo` folder on the branch `feat/ED-1234-add-x` gets
116
- `ed-1234.web.acme`, so two worktrees of one repository get different names. That is the order
117
- portless uses for worktrees, `<branch>.<name>.localhost`.
118
-
119
- Change it with `nameTemplate`, for example `{ticket}.{repo}` or `{package}-{port}`.
120
- [docs/naming.md](docs/naming.md) lists every token and how empty or repeated parts are dropped.
101
+ `ed-1234.web.acme`, so two worktrees of one repository get different names. Change the pattern
102
+ with `nameTemplate`; [docs/naming.md](docs/naming.md) lists every token.
121
103
 
122
104
  Anything that names a route through the portless CLI also shows up in Studio:
123
105
 
@@ -126,177 +108,42 @@ portless web.myapp pnpm dev # a dev server, at web.myapp.localhost
126
108
  portless alias db.myapp 5432 # a port something else opened, at db.myapp.localhost
127
109
  ```
128
110
 
129
- ## Agents and MCP
130
-
131
- `portless-studio mcp` is an [MCP](https://modelcontextprotocol.io) server on stdio. It talks to the
132
- Studio already running, so whatever an agent changes shows up in the page at once. For Claude Code:
111
+ ## Agents and terminals
133
112
 
134
113
  ```bash
135
114
  claude mcp add --scope user portless-studio -- npx -y portless-studio mcp
115
+ portless-studio run -- pnpm dev
136
116
  ```
137
117
 
138
- [docs/mcp.md](docs/mcp.md) has the setup for Cursor and other clients, the list of tools, and
139
- instructions to paste into your agent so it names dev servers after the ticket or pull request.
140
- `portless-studio list --json` and `doctor --json` give the same data to scripts.
118
+ The first line connects Claude Code to Studio's MCP server. The second runs a dev server in a
119
+ Studio terminal, prints its output in your shell and names the port it opens.
120
+ [docs/agents.md](docs/agents.md) shows how to keep agents in sync with Studio, including
121
+ instructions to paste and Claude Code hooks; [docs/mcp.md](docs/mcp.md) has the setup for other
122
+ clients and [docs/terminals.md](docs/terminals.md) covers the terminal panel.
141
123
 
142
- ## Terminals
124
+ ## Documentation
143
125
 
144
- ```bash
145
- portless-studio run -- pnpm dev
146
- ```
126
+ - [Getting started](docs/getting-started.md) and [Troubleshooting](docs/troubleshooting.md)
127
+ - [Command line](docs/cli.md), [Configuration](docs/configuration.md) and
128
+ [Keyboard shortcuts](docs/keyboard-shortcuts.md)
129
+ - [Naming](docs/naming.md), [Terminals](docs/terminals.md) and
130
+ [Linux, macOS and Windows](docs/platforms.md)
131
+ - [Agents](docs/agents.md) and [MCP server](docs/mcp.md)
132
+ - [Every page, with one line each](docs/README.md)
133
+
134
+ ## How it relates to portless
147
135
 
148
- `run` starts the command in a Studio terminal in the current folder, prints its output here,
149
- names the first port it opens and stops it on Ctrl+C. The terminal stays visible in the page.
150
- [docs/terminals.md](docs/terminals.md) covers the panel, the CLI and the security rules.
151
-
152
- ## Platforms
153
-
154
- Studio runs on Linux, macOS and Windows with Node 22 or later and installs nothing beyond portless. Each
155
- feature uses tools the system already has (`/proc` on Linux, `lsof` on macOS, `netstat` and
156
- PowerShell on Windows). [docs/platforms.md](docs/platforms.md) says what each system uses and what
157
- does not work the same everywhere.
158
-
159
- ## Commands
160
-
161
- | Command | What it does |
162
- | --- | --- |
163
- | `portless-studio` | Starts Studio with the settings from the config file. |
164
- | `portless-studio init [--yes]` | The setup above. Safe to run again. |
165
- | `portless-studio uninstall [--yes]` | Undoes the setup: removes the login service, the names auto mode gave and the `studio` name, and stops the proxy if Studio started it. Names you or an agent gave stay. Asks before deleting the settings folder. |
166
- | `portless-studio service install` | Starts Studio when you log in: a systemd user unit on Linux, a LaunchAgent on macOS, a script in the Startup folder on Windows. |
167
- | `portless-studio service uninstall` | Undoes `service install`. |
168
- | `portless-studio service status` | Says whether the login service is installed and running. |
169
- | `portless-studio config [--edit] [--json]` | Prints the config file path and contents, or opens it in `$EDITOR`. |
170
- | `portless-studio doctor [--json]` | Checks portless, the proxy, the certificate, Studio's port and the login service, and prints the fix for each problem. Exits with 1 when something fails. |
171
- | `portless-studio list [--json]` | Lists named routes and ports without a name. |
172
- | `portless-studio mcp` | Runs the MCP server on stdio for AI agents. |
173
- | `portless-studio run [--name <name>] [--cwd <dir>] -- <command...>` | Runs a command in a Studio terminal and names the port it opens. |
174
- | `portless-studio open [target]` | Brings Studio to the front showing a route, project, terminal id or `port:<n>`. |
175
-
176
- | Flag | Effect |
177
- | --- | --- |
178
- | `--port <n>` | Port for the dashboard on 127.0.0.1. Default: `port` in the config, 4949. `0` picks a free one. |
179
- | `--no-open` | Do not open the browser. |
180
- | `--no-register` | Do not register the `studio` name in portless. |
181
- | `-y`, `--yes` | With `init` and `uninstall`: accept every default. |
182
- | `-h`, `--help` | Show the help. |
183
- | `-v`, `--version` | Show the version. |
184
-
185
- | Environment variable | What it does |
186
- | --- | --- |
187
- | `PORTLESS_STUDIO_CONFIG` | Path of the config file. |
188
- | `PORTLESS_STATE_DIR` | portless state folder. Default `~/.portless`. |
189
- | `PORTLESS_BIN` | portless binary used to create and remove names. Default `portless`. |
190
- | `PORTLESS_STUDIO_URL` | Studio that `mcp`, `list`, `run` and `open` talk to. Default `http://127.0.0.1:<port>`. |
191
- | `PORTLESS_STUDIO_NO_UPDATE_CHECK` | `1` turns the daily update check off, whatever the config says. |
192
- | `EDITOR` | Editor that `config --edit` opens. |
193
-
194
- ## Config file
195
-
196
- Settings live in one JSON file: `~/.config/portless-studio/config.json` on Linux and macOS (or
197
- under `$XDG_CONFIG_HOME`), `%APPDATA%\portless-studio\config.json` on Windows. `portless-studio
198
- config` prints the path. Edit it by hand or from the Settings panel; a running Studio picks up the
199
- changes without a restart. Missing fields take their default.
200
-
201
- | Field | Default | What it does |
202
- | --- | --- | --- |
203
- | `port` | `4949` | Port Studio listens on, on 127.0.0.1. |
204
- | `autoName` | `false` | Gives every new dev server a portless name automatically. |
205
- | `autoRemoveAfterMinutes` | `10` | Minutes a name auto mode gave survives after its port closes. `0` keeps them. |
206
- | `openBrowser` | `true` | Opens the browser when Studio starts. |
207
- | `registerStudioAlias` | `true` | Registers Studio itself in portless as `studio`. |
208
- | `startProxy` | `true` | Starts the portless proxy when Studio starts and it is not running. |
209
- | `proxyPort` | `1355` | Port of that proxy. 1355 needs no administrator rights; 443 removes the port from URLs. |
210
- | `proxyHttps` | `false` | Serves the names over HTTPS. See the FAQ. |
211
- | `scanIntervalMs` | `3000` | How often Studio looks for new listening ports, in milliseconds (at least 1000). |
212
- | `ignoredPorts` | `[]` | Ports never shown as detected and never named automatically. |
213
- | `ignoredProcesses` | editors, chat apps, browsers | Process names whose ports are never shown. |
214
- | `views` | one view, `All`, grouped by project | Saved views: grouping, sort, filters and custom groups. |
215
- | `defaultViewId` | `"all"` | The view the page opens with. |
216
- | `favorites` | `[]` | Routes pinned above every group, by name or as `"port:3000"`. |
217
- | `desktopNotifications` | `false` | Notifies from an open Studio tab while it is in the background. |
218
- | `systemNotifications` | `false` | Asks the operating system to notify when no Studio tab is open. |
219
- | `nameTemplate` | `"{work}.{package}.{repo}"` | How suggested names are built. See [docs/naming.md](docs/naming.md). |
220
- | `projectIcons` | `{}` | Icon per project: an emoji, an http(s) image URL or a `data:image` URL. |
221
- | `checkForUpdates` | `true` | Once a day, asks npm whether a newer portless-studio exists. See "Update notice" below. |
222
-
223
- The `--port`, `--no-open` and `--no-register` flags win over the file for one run. Apps you add to
224
- the Open with menu live in `open-with.json` next to the config file.
225
-
226
- ### Update notice
227
-
228
- Once a day Studio asks the npm registry for the latest version of portless-studio. When a newer
229
- one exists, it prints one line on start and shows a banner in the page with the command to update;
230
- dismissing the banner hides it until the next version. The request is a plain
231
- `GET https://registry.npmjs.org/portless-studio/latest`: Studio sends nothing about your machine,
232
- your projects or your routes. It gives up after 3 seconds and stays quiet when you are offline,
233
- and it keeps the time of the last check in `update-check.json` next to the config file. It never
234
- runs when the `CI` variable is set. Turn it off with `checkForUpdates: false`, the Settings
235
- switch, or `PORTLESS_STUDIO_NO_UPDATE_CHECK=1`.
236
-
237
- ## Keyboard shortcuts
238
-
239
- | Keys | Action |
240
- | --- | --- |
241
- | `j` / `↓`, `k` / `↑` | Next and previous row |
242
- | `Home`, `End` | First and last row |
243
- | `/` | Search routes |
244
- | `Enter` or `o` | Open the selected route |
245
- | `c` | Copy its URL |
246
- | `r` | Rename an alias |
247
- | `Delete` or `Backspace` | Remove an alias |
248
- | `f` | Pin or unpin as favorite |
249
- | `.` or `Shift+F10` | Open with, copy path and more |
250
- | `t` | Open a terminal in its folder |
251
- | `n` | Name a port |
252
- | `v` / `Shift+V` | Next view / open the view menu |
253
- | `Shift+F` | Open the filters |
254
- | `[`, `]` | Collapse or expand all groups |
255
- | `Ctrl+K` / `Cmd+K` | Command menu |
256
- | `,` | Settings |
257
- | `?` | All keyboard shortcuts |
258
- | `Esc` | Close a dialog or cancel |
136
+ Studio is an independent project, not part of portless and not made by its authors. It reads the
137
+ state portless keeps in `~/.portless` and names routes through the `portless` command, so anything
138
+ you or an agent do with portless shows up in Studio, and Studio never changes how portless works.
259
139
 
260
140
  ## Security
261
141
 
262
- Studio is a local tool and only listens on `127.0.0.1`. It rejects requests whose `Host` or
263
- `Origin` is not local, so other websites open in your browser cannot read your routes or call the
264
- API. Every change (naming, removing, installing portless, controlling the proxy or Studio, stopping
265
- a dev server, running terminals) also needs a token that changes on every run. The page reads it from
266
- Studio, and browsers only let pages served by Studio itself read it. The portless install always runs the same fixed command.
267
- A dev server can be stopped only when it belongs to your user and listens on a port Studio shows,
268
- and Studio never runs a command that needs sudo. See [SECURITY.md](SECURITY.md) to report a
269
- problem.
270
-
271
- ## FAQ
272
-
273
- **Safari does not open `*.localhost` names.** Safari does not always send `.localhost` names to
274
- your machine. Open Studio at `http://127.0.0.1:4949` and each app at `http://127.0.0.1:<its port>`,
275
- or use Chrome, Edge or Firefox.
276
-
277
- **Why HTTP on port 1355, and how do I get HTTPS or port 443?** Port 1355 needs no administrator
278
- rights on any system, and browsers treat plain HTTP on a `.localhost` name as a secure context, so
279
- most apps work as they would on HTTPS. For URLs without a port, set `proxyPort` to `443`; on Linux
280
- that needs sudo once to start the proxy. For HTTPS, set `proxyHttps` to `true` and trust the
281
- portless certificate: `portless-studio doctor` prints the exact command for your system, and the
282
- Health panel shows it too.
283
-
284
- **Does it work on Windows?** Yes, with Node 22 or later and PowerShell 5 (included in Windows 10 and 11).
285
- Ports come from `netstat`, process details from PowerShell, and start at login uses a hidden
286
- script in the Startup folder. Windows has no service manager Studio can ask, so the page can stop
287
- Studio but not restart it, and the doctor only sees the proxy portless records.
288
- [docs/platforms.md](docs/platforms.md) lists every difference.
289
-
290
- **How does it relate to portless?** Studio is an independent project, not part of portless and not
291
- made by its authors. It reads the state portless keeps in `~/.portless` and names routes through
292
- the `portless` command, so anything you or an agent do with portless shows up in Studio, and
293
- Studio never changes how portless works.
294
-
295
- **How do I remove it?** Run `portless-studio uninstall` (or `npx portless-studio uninstall` if you
296
- never installed it globally). It removes the login service, the names auto mode gave and the
297
- `studio` name, stops the proxy if Studio started it, and asks before deleting the settings folder.
298
- Then remove the package with `npm rm -g portless-studio`. portless itself stays installed; remove it with
299
- `npm uninstall -g portless`.
142
+ Studio only listens on `127.0.0.1` and rejects requests whose `Host` or `Origin` is not local, so
143
+ other websites open in your browser cannot read your routes or call the API. Every change also
144
+ needs a token that changes on every run and that only Studio's own page can read. A dev server can
145
+ be stopped only when it belongs to your user, and Studio never runs a command that needs sudo.
146
+ See [SECURITY.md](SECURITY.md) to report a problem.
300
147
 
301
148
  ## Development
302
149
 
@@ -306,16 +153,11 @@ cd portless-studio
306
153
  pnpm install
307
154
  pnpm dev # API on :4949 and the web app with hot reload on :5173
308
155
  pnpm test # unit and component tests
309
- pnpm typecheck
310
- pnpm check # lint and format check
311
- pnpm build # builds dist/, then `pnpm start` runs it
312
- pnpm smoke # starts the built CLI on a free port and checks the API answers
313
156
  ```
314
157
 
315
- The code is split so the web app can talk to another backend later: see
316
- [docs/architecture.md](docs/architecture.md). Conventions are in
317
- [docs/conventions.md](docs/conventions.md), and [CONTRIBUTING.md](CONTRIBUTING.md) covers pull
318
- requests.
158
+ [CONTRIBUTING.md](CONTRIBUTING.md) covers the checks to run and pull requests,
159
+ [docs/architecture.md](docs/architecture.md) the layers and
160
+ [docs/conventions.md](docs/conventions.md) the code conventions.
319
161
 
320
162
  ## License
321
163
 
@@ -1,4 +1,4 @@
1
- import { API_PATHS, } from "../../core/api/contract.js";
1
+ import { API_PATHS, INCLUDE_STUDIO_PARAM, } from "../../core/api/contract.js";
2
2
  import { err, ok } from "../../core/result.js";
3
3
  import { plainText } from "../../core/terminals/terminal.js";
4
4
  import { describeProxy, detectedItem, proxyItem, routeItem } from "../studio-items.js";
@@ -37,10 +37,20 @@ export function createStudioTools(client) {
37
37
  title: "List named routes",
38
38
  description: "Lists every portless route Studio knows: name, browser URL, local port, kind (app or alias), " +
39
39
  "health (up, error, down), project and git worktree it runs from, and pid. Use it to find the " +
40
- "URL of a running app or which ports a worktree uses.",
41
- inputSchema: NO_INPUT,
42
- call: async () => {
43
- const snapshot = await readSnapshot(client);
40
+ "URL of a running app or which ports a worktree uses. Studio's own route is left out unless " +
41
+ "include_studio is true.",
42
+ inputSchema: {
43
+ type: "object",
44
+ properties: {
45
+ include_studio: {
46
+ type: "boolean",
47
+ description: "Also list Studio's own route (studio), hidden by default.",
48
+ },
49
+ },
50
+ additionalProperties: false,
51
+ },
52
+ call: async (args) => {
53
+ const snapshot = await readSnapshot(client, args.include_studio === true);
44
54
  if (!snapshot.ok)
45
55
  return snapshot.error;
46
56
  const { routes, proxy } = snapshot.value;
@@ -553,8 +563,11 @@ function terminalItem(terminal) {
553
563
  port: terminal.naming?.port ?? null,
554
564
  };
555
565
  }
556
- async function readSnapshot(client) {
557
- const result = await client.read(API_PATHS.snapshot);
566
+ async function readSnapshot(client, includeStudio = false) {
567
+ const path = includeStudio
568
+ ? `${API_PATHS.snapshot}?${INCLUDE_STUDIO_PARAM}=1`
569
+ : API_PATHS.snapshot;
570
+ const result = await client.read(path);
558
571
  if (!result.ok)
559
572
  return err(failure(result.error));
560
573
  return ok(result.value);
@@ -22,10 +22,13 @@ export function detectedItem(port) {
22
22
  };
23
23
  }
24
24
  export function proxyItem(proxy) {
25
- return { running: proxy.running, port: proxy.port };
25
+ const item = { running: proxy.running, port: proxy.port };
26
+ return proxy.publicPort == null ? item : { ...item, publicPort: proxy.publicPort };
26
27
  }
28
+ /** `publicPort` is set while Studio forwards it to the proxy, for URLs without a port. */
27
29
  export function describeProxy(proxy) {
28
30
  if (!proxy.running)
29
31
  return "proxy stopped";
30
- return proxy.port === null ? "proxy running" : `proxy running on port ${proxy.port}`;
32
+ const running = proxy.port === null ? "proxy running" : `proxy running on port ${proxy.port}`;
33
+ return proxy.publicPort == null ? running : `${running}, names open on port ${proxy.publicPort}`;
31
34
  }
@@ -29,6 +29,8 @@ export const API_PATHS = {
29
29
  studio: (action) => `/api/studio/${action}`,
30
30
  /** POST: stops a dev server listening on a detected or routed port. */
31
31
  stopProcess: (pid) => `/api/processes/${pid}/stop`,
32
+ /** POST: runs `docker stop` for a container the last `docker ps` listed. */
33
+ stopContainer: (name) => `/api/containers/${encodeURIComponent(name)}/stop`,
32
34
  /** GET: the editors found on this machine, the file manager and the custom apps. */
33
35
  openWithApps: "/api/open-with/apps",
34
36
  /** POST `OpenWithRequest`: opens a folder Studio knows with one of those apps. */
@@ -43,7 +45,16 @@ export const API_PATHS = {
43
45
  terminals: "/api/terminals",
44
46
  /** Server-sent events: `terminals` with the whole list on connect and after every change. */
45
47
  terminalsEvents: "/api/terminals/events",
46
- /** GET returns `TerminalInfo`; DELETE stops the command if it runs and closes the terminal. */
48
+ /**
49
+ * POST `CreateTerminalRequest` from the page's folder picker: `cwd` may be any existing folder,
50
+ * typed by the person (`~` is the home folder). Every other page request runs only in folders
51
+ * Studio shows.
52
+ */
53
+ terminalsInFolder: "/api/terminals/in-folder",
54
+ /**
55
+ * GET returns `TerminalInfo`; PATCH `RenameTerminalRequest` renames it; DELETE stops the command
56
+ * if it runs and closes the terminal.
57
+ */
47
58
  terminal: (id) => `/api/terminals/${encodeURIComponent(id)}`,
48
59
  /** GET `?from=<seq>&max=<chars>` returns `TerminalOutputResponse`. */
49
60
  terminalOutput: (id) => `/api/terminals/${encodeURIComponent(id)}/output`,
@@ -70,6 +81,11 @@ export const API_PATHS = {
70
81
  * no custom headers.
71
82
  */
72
83
  export const TOKEN_PARAM = "token";
84
+ /**
85
+ * `GET /api/snapshot?includeStudio=1` keeps Studio's own route in `routes`, which the snapshot
86
+ * leaves out unless `showStudioRoute` is on.
87
+ */
88
+ export const INCLUDE_STUDIO_PARAM = "includeStudio";
73
89
  export const SNAPSHOT_EVENT = "snapshot";
74
90
  /** Server-sent event type that carries one `StudioEvent`. */
75
91
  export const STUDIO_EVENT = "event";
@@ -36,6 +36,8 @@ export const DEFAULT_CONFIG = {
36
36
  projectIcons: {},
37
37
  nameTemplate: DEFAULT_NAME_TEMPLATE,
38
38
  checkForUpdates: true,
39
+ shortUrls: false,
40
+ showStudioRoute: false,
39
41
  };
40
42
  const MIN_SCAN_INTERVAL_MS = 1000;
41
43
  /**
@@ -57,6 +59,8 @@ export function parseConfig(input) {
57
59
  "desktopNotifications",
58
60
  "systemNotifications",
59
61
  "checkForUpdates",
62
+ "shortUrls",
63
+ "showStudioRoute",
60
64
  ]) {
61
65
  const value = raw[key];
62
66
  if (value === undefined)
@@ -27,9 +27,12 @@ export function killCommand(platform, pids, admin) {
27
27
  export function adminWords(platform) {
28
28
  return platform === "win32" ? "from a terminal opened with Run as Administrator" : "with sudo";
29
29
  }
30
- /** Whether binding this port needs root. Windows and macOS (10.14 and later) let anyone bind it. */
30
+ /**
31
+ * Whether portless asks for sudo to start its proxy on this port. It does on Linux and macOS for
32
+ * every port below 1024, even where the system would let this user bind it; Windows never asks.
33
+ */
31
34
  export function portNeedsRoot(platform, port) {
32
- return port < 1024 && platform !== "win32" && platform !== "darwin";
35
+ return port < 1024 && platform !== "win32";
33
36
  }
34
37
  /** Trusts the portless certificate authority in the store this system's browsers read. */
35
38
  export function trustCertificateCommand(platform, caFile) {
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The address a dev server answers on without the portless proxy, e.g. `http://localhost:3000`.
3
+ * For a Docker container it is the host port the container publishes.
4
+ */
5
+ export function directUrl(port) {
6
+ return `http://localhost:${port}`;
7
+ }
@@ -0,0 +1,85 @@
1
+ // Docker's own rule for container names; it also keeps names from reading as flags.
2
+ const CONTAINER_NAME = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
3
+ // A host binding such as `0.0.0.0:8000-8002->8000-8002/tcp` or `[::]:8080->80/tcp`.
4
+ const BINDING = /:(\d+)(?:-(\d+))?->\d+(?:-\d+)?\/(tcp|udp|sctp)$/;
5
+ // A range wider than this is a mistake in the compose file, not something to probe port by port.
6
+ const MAX_RANGE = 1000;
7
+ export function isContainerName(value) {
8
+ return CONTAINER_NAME.test(value);
9
+ }
10
+ /** Host TCP ports from the `Ports` column; unpublished ports (`80/tcp`) and UDP are skipped. */
11
+ export function parsePublishedPorts(text) {
12
+ const ports = new Set();
13
+ for (const part of text.split(",")) {
14
+ const match = BINDING.exec(part.trim());
15
+ if (match?.[3] !== "tcp")
16
+ continue;
17
+ const first = Number(match[1]);
18
+ const last = match[2] === undefined ? first : Number(match[2]);
19
+ if (last < first || last - first >= MAX_RANGE)
20
+ continue;
21
+ for (let port = first; port <= last; port += 1) {
22
+ if (port >= 1 && port <= 65535)
23
+ ports.add(port);
24
+ }
25
+ }
26
+ return [...ports].sort((a, b) => a - b);
27
+ }
28
+ /** Reads `docker ps --format '{{json .}}'`: one JSON object per line. Bad lines are skipped. */
29
+ export function parseDockerPs(text) {
30
+ const containers = [];
31
+ for (const raw of text.split("\n")) {
32
+ const row = parseLine(raw.trim());
33
+ if (row === null)
34
+ continue;
35
+ const name = (stringField(row, "Names") ?? "").split(",")[0]?.trim() ?? "";
36
+ if (!isContainerName(name))
37
+ continue;
38
+ const labels = parseLabels(stringField(row, "Labels") ?? "");
39
+ containers.push({
40
+ name,
41
+ image: stringField(row, "Image") ?? "",
42
+ composeProject: labels.get("com.docker.compose.project") || null,
43
+ composeService: labels.get("com.docker.compose.service") || null,
44
+ ports: parsePublishedPorts(stringField(row, "Ports") ?? ""),
45
+ });
46
+ }
47
+ return containers;
48
+ }
49
+ /**
50
+ * What the name template reads for a container: the compose service as the package and the
51
+ * compose project as the repository (`api.acme`), else the container name as the folder.
52
+ */
53
+ export function containerProjectInfo(container) {
54
+ return {
55
+ packageName: container.composeService,
56
+ repoName: container.composeProject,
57
+ folderName: container.name,
58
+ worktreeName: null,
59
+ };
60
+ }
61
+ function parseLine(line) {
62
+ if (!line.startsWith("{"))
63
+ return null;
64
+ try {
65
+ const value = JSON.parse(line);
66
+ return typeof value === "object" && value !== null ? value : null;
67
+ }
68
+ catch {
69
+ return null;
70
+ }
71
+ }
72
+ function stringField(row, key) {
73
+ const value = row[key];
74
+ return typeof value === "string" ? value : null;
75
+ }
76
+ /** `a=1,b=2`; label values with commas are rare and only the compose labels matter here. */
77
+ function parseLabels(text) {
78
+ const labels = new Map();
79
+ for (const pair of text.split(",")) {
80
+ const index = pair.indexOf("=");
81
+ if (index > 0)
82
+ labels.set(pair.slice(0, index).trim(), pair.slice(index + 1).trim());
83
+ }
84
+ return labels;
85
+ }
@@ -19,10 +19,22 @@ export function nameFromHostname(hostname, tlds) {
19
19
  }
20
20
  return hostname;
21
21
  }
22
- /** Builds the browser URL; omits the port when it is the protocol default. */
22
+ /**
23
+ * Builds the browser URL on the port names open on: the public port when there is one, else the
24
+ * proxy's. Omits the port when it is the protocol default.
25
+ */
23
26
  export function routeUrl(hostname, proxy) {
24
27
  const scheme = proxy.https ? "https" : "http";
25
28
  const defaultPort = proxy.https ? 443 : 80;
26
- const port = proxy.port === null || proxy.port === defaultPort ? "" : `:${proxy.port}`;
29
+ const open = proxy.publicPort ?? proxy.port;
30
+ const port = open === null || open === defaultPort ? "" : `:${open}`;
27
31
  return `${scheme}://${hostname}${port}`;
28
32
  }
33
+ /** Points every route URL at `publicPort`, the port Studio forwards to the proxy; null keeps them. */
34
+ export function withPublicPort(state, publicPort) {
35
+ if (publicPort === null)
36
+ return state;
37
+ const proxy = { ...state.proxy, publicPort };
38
+ const routes = state.routes.map((route) => ({ ...route, url: routeUrl(route.hostname, proxy) }));
39
+ return { ...state, proxy, routes };
40
+ }
@@ -0,0 +1,19 @@
1
+ import { isContainerName } from "../../core/ports/docker-container.js";
2
+ import { err, ok } from "../../core/result.js";
3
+ import { appError } from "./app-error.js";
4
+ /**
5
+ * Stops a container from its row. Only a container the last `docker ps` listed can be stopped, so
6
+ * the endpoint cannot be used to stop anything else.
7
+ */
8
+ export async function stopContainer(name, source) {
9
+ if (!isContainerName(name)) {
10
+ return err(appError("invalid_request", "That is not a Docker container name."));
11
+ }
12
+ const reading = await source.read();
13
+ const listed = reading.state === "ok" && reading.containers.some((item) => item.name === name);
14
+ if (!listed) {
15
+ return err(appError("process_not_allowed", `Container ${name} is not one Studio shows; it may have stopped.`));
16
+ }
17
+ const stopped = await source.stop(name);
18
+ return stopped.ok ? ok({ name }) : stopped;
19
+ }