portless 0.4.2 → 0.5.1

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 ADDED
@@ -0,0 +1,260 @@
1
+ # portless
2
+
3
+ Replace port numbers with stable, named .localhost URLs. For humans and agents.
4
+
5
+ ```diff
6
+ - "dev": "next dev" # http://localhost:3000
7
+ + "dev": "portless run next dev" # http://myapp.localhost:1355
8
+ ```
9
+
10
+ ## Quick Start
11
+
12
+ ```bash
13
+ # Install
14
+ npm install -g portless
15
+
16
+ # Run your app (auto-starts the proxy if needed)
17
+ portless run next dev
18
+ # -> http://<project>.localhost:1355
19
+
20
+ # Or specify a name explicitly
21
+ portless myapp next dev
22
+ # -> http://myapp.localhost:1355
23
+ ```
24
+
25
+ > The proxy auto-starts when you run an app. You can also start it explicitly with `portless proxy start`.
26
+
27
+ ## Why
28
+
29
+ Local dev with port numbers is fragile:
30
+
31
+ - **Port conflicts** -- two projects default to the same port and you get `EADDRINUSE`
32
+ - **Memorizing ports** -- was the API on 3001 or 8080?
33
+ - **Refreshing shows the wrong app** -- stop one server, start another on the same port, and your open tab now shows something completely different
34
+ - **Monorepo multiplier** -- every problem above scales with each service in the repo
35
+ - **Agents test the wrong port** -- AI coding agents guess or hardcode the wrong port, especially in monorepos
36
+ - **Cookie and storage clashes** -- cookies set on `localhost` bleed across apps on different ports; localStorage is lost when ports shift
37
+ - **Hardcoded ports in config** -- CORS allowlists, OAuth redirect URIs, and `.env` files all break when ports change
38
+ - **Sharing URLs with teammates** -- "what port is that on?" becomes a Slack question
39
+ - **Browser history is useless** -- your history for `localhost:3000` is a jumble of unrelated projects
40
+
41
+ Portless fixes all of this by giving each dev server a stable, named `.localhost` URL that both humans and agents can rely on.
42
+
43
+ ## Usage
44
+
45
+ ```bash
46
+ # Auto-infer name from package.json / git / directory
47
+ portless run next dev
48
+ # -> http://<project>.localhost:1355
49
+
50
+ # Explicit name
51
+ portless myapp next dev
52
+ # -> http://myapp.localhost:1355
53
+
54
+ # Subdomains
55
+ portless api.myapp pnpm start
56
+ # -> http://api.myapp.localhost:1355
57
+
58
+ portless docs.myapp next dev
59
+ # -> http://docs.myapp.localhost:1355
60
+
61
+ # Wildcard subdomains (no extra registration needed)
62
+ # Any subdomain of a registered route routes automatically:
63
+ # tenant1.myapp.localhost:1355 -> myapp
64
+ # tenant2.myapp.localhost:1355 -> myapp
65
+ ```
66
+
67
+ ### In package.json
68
+
69
+ ```json
70
+ {
71
+ "scripts": {
72
+ "dev": "portless run next dev"
73
+ }
74
+ }
75
+ ```
76
+
77
+ The proxy auto-starts when you run an app. Or start it explicitly: `portless proxy start`.
78
+
79
+ ## How It Works
80
+
81
+ ```mermaid
82
+ flowchart TD
83
+ Browser["Browser\nmyapp.localhost:1355"]
84
+ Proxy["portless proxy<br>(port 1355)"]
85
+ App1[":4123\nmyapp"]
86
+ App2[":4567\napi"]
87
+
88
+ Browser -->|port 1355| Proxy
89
+ Proxy --> App1
90
+ Proxy --> App2
91
+ ```
92
+
93
+ 1. **Start the proxy** -- auto-starts when you run an app, or start explicitly with `portless proxy start`
94
+ 2. **Run apps** -- `portless <name> <command>` assigns a free port and registers with the proxy
95
+ 3. **Access via URL** -- `http://<name>.localhost:1355` routes through the proxy to your app
96
+
97
+ Apps are assigned a random port (4000-4999) via the `PORT` and `HOST` environment variables. Most frameworks (Next.js, Express, Nuxt, etc.) respect these automatically. For frameworks that ignore `PORT` (Vite, Astro, React Router, Angular, Expo, React Native), portless auto-injects the correct `--port` and `--host` flags.
98
+
99
+ ## HTTP/2 + HTTPS
100
+
101
+ Enable HTTP/2 for faster dev server page loads. Browsers limit HTTP/1.1 to 6 connections per host, which bottlenecks dev servers that serve many unbundled files (Vite, Nuxt, etc.). HTTP/2 multiplexes all requests over a single connection.
102
+
103
+ ```bash
104
+ # Start with HTTPS/2 -- generates certs and trusts them automatically
105
+ portless proxy start --https
106
+
107
+ # First run prompts for sudo once to add the CA to your system trust store.
108
+ # After that, no prompts. No browser warnings.
109
+
110
+ # Make it permanent (add to .bashrc / .zshrc)
111
+ export PORTLESS_HTTPS=1
112
+ portless proxy start # HTTPS by default now
113
+
114
+ # Use your own certs (e.g., from mkcert)
115
+ portless proxy start --cert ./cert.pem --key ./key.pem
116
+
117
+ # If you skipped sudo on first run, trust the CA later
118
+ sudo portless trust
119
+ ```
120
+
121
+ On Linux, `portless trust` supports Debian/Ubuntu, Arch, Fedora/RHEL/CentOS, and openSUSE (via `update-ca-certificates` or `update-ca-trust`).
122
+
123
+ ## Commands
124
+
125
+ ```bash
126
+ portless run <cmd> [args...] # Infer name from project, run through proxy
127
+ portless <name> <cmd> [args...] # Run app at http://<name>.localhost:1355
128
+ portless alias <name> <port> # Register a static route (e.g. for Docker)
129
+ portless alias <name> <port> --force # Overwrite an existing route
130
+ portless alias --remove <name> # Remove a static route
131
+ portless list # Show active routes
132
+ portless trust # Add local CA to system trust store
133
+ portless hosts sync # Add routes to /etc/hosts (fixes Safari)
134
+ portless hosts clean # Remove portless entries from /etc/hosts
135
+
136
+ # Disable portless (run command directly)
137
+ PORTLESS=0 pnpm dev # Bypasses proxy, uses default port
138
+ # Also accepts PORTLESS=skip
139
+
140
+ # Proxy control
141
+ portless proxy start # Start the proxy (port 1355, daemon)
142
+ portless proxy start --https # Start with HTTP/2 + TLS
143
+ portless proxy start -p 80 # Start on port 80 (requires sudo)
144
+ portless proxy start --foreground # Start in foreground (for debugging)
145
+ portless proxy stop # Stop the proxy
146
+
147
+ # Options
148
+ -p, --port <number> # Port for the proxy (default: 1355)
149
+ # Ports < 1024 require sudo
150
+ --https # Enable HTTP/2 + TLS with auto-generated certs
151
+ --cert <path> # Use a custom TLS certificate (implies --https)
152
+ --key <path> # Use a custom TLS private key (implies --https)
153
+ --no-tls # Disable HTTPS (overrides PORTLESS_HTTPS)
154
+ --foreground # Run proxy in foreground instead of daemon
155
+ --app-port <number> # Use a fixed port for the app (skip auto-assignment)
156
+ --force # Override a route registered by another process
157
+ --name <name> # Use <name> as the app name (bypasses subcommand dispatch)
158
+ -- # Stop flag parsing; everything after is passed to the child
159
+
160
+ # Injected into child processes
161
+ PORT # Ephemeral port the child should listen on
162
+ HOST # Always 127.0.0.1
163
+ PORTLESS_URL # Public URL (e.g. http://myapp.localhost:1355)
164
+
165
+ # Configuration
166
+ PORTLESS_PORT=<number> # Override the default proxy port
167
+ PORTLESS_APP_PORT=<number> # Use a fixed port for the app (same as --app-port)
168
+ PORTLESS_HTTPS=1|true # Always enable HTTPS
169
+ PORTLESS_SYNC_HOSTS=1 # Auto-sync /etc/hosts when routes change
170
+ PORTLESS_STATE_DIR=<path> # Override the state directory
171
+
172
+ # Info
173
+ portless --help # Show help
174
+ portless run --help # Show help for a specific subcommand
175
+ portless --version # Show version
176
+ ```
177
+
178
+ > **Reserved names:** `run`, `alias`, `hosts`, `list`, `trust`, and `proxy` are subcommands and cannot be used as app names directly. Use `portless run <cmd>` to infer the name from your project, or `portless --name <name> <cmd>` to force any name including reserved ones.
179
+
180
+ ## State Directory
181
+
182
+ Portless stores its state (routes, PID file, port file) in a directory that depends on the proxy port:
183
+
184
+ - **Port < 1024** (sudo required): `/tmp/portless` -- shared between root and user processes
185
+ - **Port >= 1024** (no sudo): `~/.portless` -- user-scoped, no root involvement
186
+
187
+ Override with the `PORTLESS_STATE_DIR` environment variable if needed.
188
+
189
+ ## Development
190
+
191
+ This repo is a pnpm workspace monorepo using [Turborepo](https://turbo.build). The publishable package lives in `packages/portless/`.
192
+
193
+ ```bash
194
+ pnpm install # Install all dependencies
195
+ pnpm build # Build all packages
196
+ pnpm test # Run tests
197
+ pnpm test:coverage # Run tests with coverage
198
+ pnpm test:watch # Run tests in watch mode
199
+ pnpm lint # Lint all packages
200
+ pnpm typecheck # Type-check all packages
201
+ pnpm format # Format all files with Prettier
202
+ ```
203
+
204
+ ## Safari / DNS
205
+
206
+ `.localhost` subdomains auto-resolve to `127.0.0.1` in Chrome, Firefox, and Edge. Safari relies on the system DNS resolver, which may not handle `.localhost` subdomains on all configurations.
207
+
208
+ If Safari can't find your `.localhost` URL:
209
+
210
+ ```bash
211
+ # Add current routes to /etc/hosts (requires sudo)
212
+ sudo portless hosts sync
213
+
214
+ # Clean up later
215
+ sudo portless hosts clean
216
+ ```
217
+
218
+ To auto-sync `/etc/hosts` whenever routes change, set `PORTLESS_SYNC_HOSTS=1` and start the proxy with sudo:
219
+
220
+ ```bash
221
+ export PORTLESS_SYNC_HOSTS=1
222
+ sudo portless proxy start
223
+ ```
224
+
225
+ ## Proxying Between Portless Apps
226
+
227
+ If your frontend dev server (e.g. Vite, webpack) proxies API requests to another portless app, make sure the proxy rewrites the `Host` header. Without this, the proxy sends the **original** Host header, causing portless to route the request back to the frontend in an infinite loop.
228
+
229
+ **Vite** (`vite.config.ts`):
230
+
231
+ ```ts
232
+ server: {
233
+ proxy: {
234
+ "/api": {
235
+ target: "http://api.myapp.localhost:1355",
236
+ changeOrigin: true, // Required: rewrites Host header to match target
237
+ ws: true,
238
+ },
239
+ },
240
+ }
241
+ ```
242
+
243
+ **webpack-dev-server** (`webpack.config.js`):
244
+
245
+ ```js
246
+ devServer: {
247
+ proxy: [{
248
+ context: ["/api"],
249
+ target: "http://api.myapp.localhost:1355",
250
+ changeOrigin: true, // Required: rewrites Host header to match target
251
+ }],
252
+ }
253
+ ```
254
+
255
+ Portless detects this misconfiguration and responds with `508 Loop Detected` along with a message pointing to this fix.
256
+
257
+ ## Requirements
258
+
259
+ - Node.js 20+
260
+ - macOS or Linux