agentleFS
joserprietojoserprieto/ai-skills/skills/portless-service-integration/SKILL.md

portless-service-integration

Skill2 starsChanged 10 months agoMITView on GitHub

Use when wiring a local development service (dev server, preview server, static site, any long-running HTTP upstream) to portless so it gets a stable `.localhost` URL instead of a port number. Also use when user says "portless", "replace ports with names", "stable localhost URL", ".localhost domain", "portless proxy", "PORTLESS_*" env vars, "sin puerto", or when integrating portless into Makefile targets or package.json scripts. Covers proxy setup, naming conventions, env vars, troubleshooting (including the cert-0-bytes stuck state), and points to ecosystem-specific references (Python, Node/Vite/Storybook, Go) loaded on demand.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 1 is distinctive, and 10 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 10

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Do NOT write proxy-alive checks in Makefiles: before invoking portless <name> <cmd> — the command itself handles auto-start.

    line 92
  2. Distinctive:

    NOT a second route: two names for one component means which one answers depends on what happens to be running.

    line 125
  3. Very specific:

    When you run portless <name> <cmd>, portless sets these env vars for the child:

    line 130
  4. Very specific:

    If your tool doesn't read PORT automatically, pass it explicitly:

    line 148
  5. Very specific:

    Avoid .dev (Google-owned, HSTS-forced in browsers) and .local (collides with mDNS/Bonjour).

    line 241
  6. Very specific:

    Run portless doctor first — it checks the proxy, the routes, DNS resolution and CA trust in one pass, and points at the failing layer …

    line 260
  7. Very specific:

    If you see a .pem file with size 0, portless failed to sign the CSR silently:

    line 278
  8. Very specific:

    Impact of proxy restart: all other active routes need their parent processes to re-run (they'll re-register on next portless <name> <cmd>).

    line 302
  9. Very specific:

    If curl http://127.0.0.1:<port>/ works but curl https://<name>.localhost/ fails with SSL_ERROR_SYSCALL, the issue is almost always the cert.

    line 308
  10. Very specific:

    If portless list shows -> localhost:4321 and your upstream binds to ::1:4321 but portless connects to 127.0.0.1:4321, the upstream is …

    line 313
  11. Very specific:

    If service A (e.g. Vite with /api proxy) forwards to service B without rewriting the Host header, portless routes back to A → loop.

    line 338

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Portless Service Integration

portless (npm package) replaces port numbers with stable .localhost URLs. Instead of memorizing localhost:5173 vs localhost:4321 vs localhost:8765, each service gets a named URL like dev.ps.myorg.localhost. Same URL across restarts, profiles, and machines.

Value proposition:

  • Zero port conflicts across projects
  • Stable URLs in bookmarks, docs, and .env files
  • Cookies and localStorage isolated per service (no clashes at localhost:X)
  • HTTPS with HTTP/2 out of the box (via local CA)
  • Language-agnostic (works with any process that speaks HTTP on a TCP port)

This skill is deliberately ecosystem-agnostic. Load an ecosystem-specific reference for concrete patterns:

  • references/python-uvicorn.md — Python ASGI servers (uvicorn + starlette, aiohttp). Do NOT use python -m http.server behind portless — see reference for why.
  • references/vite-storybook.md — Vite, Storybook, Astro, Next, and other Node frameworks where portless auto-injects --port/--host.
  • references/go-net-http.md — Go net/http services (stdlib is HTTP/1.1 keep-alive by default).

Workflow

digraph portless_flow {
  rankdir=TB;
  "Proxy auto-starts" [shape=box, style=filled, fillcolor="#d7e1d8"];
  "Pick service name" -> "Choose invocation mode";
  "Choose invocation mode" -> "portless <name> <cmd>" [label="managed lifecycle"];
  "Choose invocation mode" -> "portless alias <name> <port>" [label="pre-running / Docker"];
  "Choose invocation mode" -> "portless run <cmd>" [label="infer name from package.json or git"];
  "portless <name> <cmd>" -> "Access via <name>.localhost";
  "portless alias <name> <port>" -> "Access via <name>.localhost";
  "portless run <cmd>" -> "Access via <inferred>.localhost";
}

Step 1: Proxy setup (auto-starts, rarely manual)

Portless runs a background proxy that binds .localhost → your ports. It auto-starts the first time you run portless <name> <cmd> or portless run <cmd>. Manual start is only needed for specific scenarios.

# Start HTTPS proxy on port 443 (requires sudo first time — installs local CA)
portless proxy start

# HTTP-only on port 80 (no sudo, no TLS)
portless proxy start --no-tls

# Custom proxy port (no sudo, no special privileges)
portless proxy start -p 1355

# Foreground for debugging
portless proxy start --foreground

# Stop proxy
portless proxy stop

# Health check — proxy, routes, DNS, CA trust in one pass (0.15.0+)
portless doctor

# Start the proxy automatically when the OS starts
portless service install

# Kill orphaned dev servers left behind by crashed sessions
portless prune

Portless requires Node 24+ (since 0.13.1). Since 0.15.4 the proxy binds loopback-only unless LAN mode is explicitly enabled — dev services are not exposed to the network by default.

Do NOT write proxy-alive checks in Makefiles before invoking portless <name> <cmd> — the command itself handles auto-start. Checks like lsof -iTCP:443 -sTCP:LISTEN are unreliable because privileged port owners may not be visible without root, and return false negatives.

Step 2: Naming convention (component first)

[<worktree>.]<component>.<environment>.<project>.<owner>.localhost

The first thing you read is the thing you are looking at — the way public DNS reads (api.github.com, not prod.api.github.com). Five axes:

  • component: WHAT answers at the URL — app, core, storybook, preview, dashboard, ... Name the responsibility, not the interface (core, not api).
  • environment: a real taxonomy, not a synonym for "local" — localdev (a developer's machine), dev (a SHARED deployed dev server), qa, preprod, prod. A URL served from a laptop is localdev, never dev.
  • project: project short name (myapp)
  • owner: org/tenant namespace (myorg, acme) — drop it for single-org setups
  • worktree: composed BY PORTLESS, never by your Makefile. portless run --name <base> keeps the base name on the main checkout and prepends the branch as the LEFTMOST label in a linked git worktree (0.15.5+), so presence is detected by label count, not position.

Examples (using myorg as the owner):

  • app.localdev.myapp.myorg.localhost — the app, on a developer's machine
  • core.localdev.myapp.myorg.localhost — the domain service behind it
  • storybook.localdev.myapp.myorg.localhost — the component workshop
  • fix-ui.app.localdev.myapp.myorg.localhost — same app, linked worktree on branch fix-ui (prefix added by portless, zero Makefile involvement)

A mode toggle (mock vs real backend, feature profile) is an env var flowing into the child process, NOT a second route: two names for one component means which one answers depends on what happens to be running.

Step 3: Env vars portless injects into child processes

When you run portless <name> <cmd>, portless sets these env vars for the child:

  • PORT — ephemeral port (4000-4999 range) the child must listen on
  • HOST — always 127.0.0.1 (IPv4). The proxy connects upstream to this address
  • PORTLESS_URL — the public URL (e.g. https://myapp.localhost) for cross-service refs

Upstream must bind to 127.0.0.1 (IPv4 explicit). The docs are clear on this. If your server binds to localhost and the OS resolves it to IPv6 (::1), portless can't reach it — you'll see the TLS handshake fail with SSL_ERROR_SYSCALL at the proxy edge (proxy never logs the request).

Does your tool respect PORT env var?

  • Yes (by convention): Next.js, Express, Nuxt, uvicorn (when you pass --port "$PORT"), most mature servers
  • No (portless auto-injects --port and --host flags): Vite, Astro, React Router, Angular, Expo, React Native
  • No (you must wire it yourself): python -m http.server, ruby -run -e httpd, any custom CLI

If your tool doesn't read PORT automatically, pass it explicitly:

portless myapp sh -c 'exec <tool> --port "$PORT" --host 127.0.0.1 ...'

Step 4: Invocation modes

Mode A — Managed lifecycle (most common)

portless app.localdev.myapp vite
# → https://app.localdev.myapp.localhost
# Portless assigns PORT, launches vite, registers route
# Worktree-aware variant (branch prefixed as leftmost label in linked worktrees):
portless run --name app.localdev.myapp vite

Mode B — Infer name from project

cd my-project
portless run vite
# → https://my-project.localhost (inferred from package.json or git root)

Mode C — Static alias (pre-running services, Docker)

# Service already listens on a fixed port (e.g. PostgreSQL at 5432)
portless alias my-postgres 5432
# → https://my-postgres.localhost → 127.0.0.1:5432

Alias is for services portless does not launch (Docker, system services, already-running processes). For services you want portless to manage, use Mode A.

Mode D — Get URL for cross-service reference

BACKEND_URL=$(portless get backend)
# → https://backend.localhost

Step 5: Wiring into Makefile or package.json

Portless composes with standard tooling. No abstraction layer (like a service-manager.ts) is required — it's a plain CLI command you invoke.

Directly in Makefile target

serve: generate
	@portless dashboard.localdev.myapp <command>

In package.json

{
  "scripts": {
    "dev": "portless run vite"
  }
}

Then pnpm dev gets the portless URL without config duplication.

Bypass portless for CI / scripted runs

PORTLESS=0 pnpm dev     # runs command directly on its default port

Step 6: DNS resolution — .localhost auto-resolves

Modern macOS, Linux, and Windows resolve *.localhost to 127.0.0.1 automatically per RFC 6761. No /etc/hosts entries needed. This works out of the box with curl, ping, Chrome, Firefox, Edge.

Exceptions that may need /etc/hosts sync:

  • Safari: relies on system resolver and can miss certain subdomain configurations
  • Custom TLD (--tld test): .test does not auto-resolve; portless auto-syncs /etc/hosts when you use it

Custom TLD rules (0.15.1+):

  • --tld is repeatable and PORTLESS_TLD takes a comma-separated list. Multi-TLD serves the SAME app names under every TLD — not different apps under different ones. PORTLESS_URL carries the first.
  • Since 0.15.5 a TLD may be multi-segment (--tld internal.acme), so local URLs can mirror production structure. Labels are DNS-validated; overlapping TLDs resolve longest-match.
  • Avoid .dev (Google-owned, HSTS-forced in browsers) and .local (collides with mDNS/Bonjour). .test is IANA-reserved and safe, but needs the hosts sync above.
  • LAN mode ignores a custom TLD — it serves under .local; the two cannot be combined.

Manual sync:

portless hosts sync    # Write current routes to /etc/hosts
portless hosts clean   # Remove portless entries from /etc/hosts

Environment flag to enable auto-sync for .localhost:

export PORTLESS_SYNC_HOSTS=1

Step 7: Troubleshooting

Run portless doctor first — it checks the proxy, the routes, DNS resolution and CA trust in one pass, and points at the failing layer before you start guessing.

Cert stuck at 0 bytes (most common stuck state)

Symptoms:

  • curl returns LibreSSL SSL_connect: SSL_ERROR_SYSCALL
  • Proxy log (/tmp/portless/proxy.log) has no entry for the failed request (the TLS handshake dies before the proxy logs the upstream attempt)
  • Other portless routes work fine

Diagnosis:

ls -la /tmp/portless/host-certs/ | grep <my-host>

If you see a .pem file with size 0, portless failed to sign the CSR silently:

-rw-r--r--  root  219  myapp_myorg_localhost-ext.cnf
-rw-------  root  227  myapp_myorg_localhost-key.pem
-rw-r--r--  root  391  myapp_myorg_localhost.csr
-rw-r--r--  root    0  myapp_myorg_localhost.pem    ← BUG: empty cert

Fix (quirúrgico):

# 1. Remove the corrupt cert files (owned by root, need sudo)
sudo rm /tmp/portless/host-certs/<my-host>*

# 2. Restart the proxy (regenerates certs on next request)
portless proxy stop
portless proxy start

# 3. Verify
curl -sS -I https://<my-host>.localhost/
# Expected: HTTP/2 200  +  x-portless: 1

Impact of proxy restart: all other active routes need their parent processes to re-run (they'll re-register on next portless <name> <cmd>). If that's too disruptive, try step 1 alone first and let portless regenerate on next request.

Upstream reachable directly but not via proxy

If curl http://127.0.0.1:<port>/ works but curl https://<name>.localhost/ fails with SSL_ERROR_SYSCALL, the issue is almost always the cert. See above.

IPv4/IPv6 mismatch

If portless list shows -> localhost:4321 and your upstream binds to ::1:4321 but portless connects to 127.0.0.1:4321, the upstream is unreachable. Always bind upstreams to 127.0.0.1 explicitly — don't use localhost (which may resolve to ::1) or 0.0.0.0 (works but exposes the service on all interfaces). Since 0.15.2 an IPv6-only upstream no longer 502s, but explicit 127.0.0.1 remains the convention here — it is what the proxy dials first and it keeps the service off the LAN.

Proxy log location

/tmp/portless/proxy.log        # daemon log
/tmp/portless/host-certs/      # per-host certs
/tmp/portless/routes.json      # registered routes
/tmp/portless/ca.srl, ca.pem   # local CA

Tail during a failing request to capture the live error:

tail -f /tmp/portless/proxy.log &
curl https://<my-host>.localhost/

Infinite loop between portless apps

If service A (e.g. Vite with /api proxy) forwards to service B without rewriting the Host header, portless routes back to A → loop. Portless detects this and responds with 508 Loop Detected. Fix: in your frontend framework's proxy config, set changeOrigin: true (Vite, webpack-dev-server).

Common mistakes

MistakeCorrect approach
Bind upstream to localhost (may resolve to ::1)Bind to 127.0.0.1 explicit
Bind to 0.0.0.0 in devUse 127.0.0.1 (matches portless + doesn't expose to LAN)
Manual proxy-alive check in Makefile before portless <cmd>Trust auto-start; portless handles it
Use portless alias for services you launch yourselfUse portless <name> <cmd> (managed lifecycle)
Assume .localhost needs /etc/hosts entriesModern OS resolvers handle *.localhost per RFC 6761
Wrap everything in service-manager.ts abstractionDirect portless calls in Makefile / package.json work fine
Use python -m http.server behind portlessSee references/python-uvicorn.md — use uvicorn instead
Use --no-tls on :443 (http on :443)Stick with default HTTPS on :443 for browser compat
Ignore 0-byte cert files silentlyCheck /tmp/portless/host-certs/ when TLS fails — fix per Step 7

Related skills

  • makefile-service-conventions — canonical Makefile targets (start/stop/restart/status, aggregate AND per-service …/<svc> — the full quartet is mandatory per service) that wire into portless. restart is part of the primary set; for managed (Mode A) routes a naive restart is the classic breaker — stop must whole-tree-kill the route's process AND clean by route pid before start re-registers (idempotent start required); a Mode C alias just re-runs alias --remove + alias.
  • service-manager — optional higher-level orchestrator when you have many services with PID tracking needs (not required for portless use)
  • repo-kickstart — bootstrap a new project with portless wired in

Ecosystem-specific references

Load these on demand when working with a specific stack:

  • Python servers: references/python-uvicorn.md
  • Vite / Storybook / Node frameworks: references/vite-storybook.md
  • Go services: references/go-net-http.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.