Hoprig

Docs

Preview: start your site, open it, check it

A chat of a project has a Preview button in its top bar. It starts the project's website or app on the computer that holds the project, says how it stands, opens it, and checks how it came out. Nothing here uses a model; only Ask the chat to fix it does, and only when you send what it wrote.

What you do

  1. Press Preview in a chat. Hoprig starts the project's site on a free port on this computer's own 127.0.0.1. The sheet says Starting… and then Ready.
  2. Open opens the site in a new tab. Check looks at it (below). Stop ends it and frees the port. On a wide screen (900 px and up) the site opens beside the chat as soon as it is ready, in the place of the diff pane (below).
  3. If Hoprig does not know how to start the project, it asks once: How do I start it?. Type the command; Hoprig remembers it for that folder, in its own state folder, never in your repository. Where the command takes the port as an option, write {port}; the port is also given in the environment as PORT, with HOST=127.0.0.1.
  4. If it fails, the sheet says why in one line, with the last lines of its output, and Ask the chat to fix it puts the command, the reason and those lines into the message box. It does not send them.

How Hoprig knows how to start a project

Hoprig looks in this order and stops at the first one it finds:

  1. The command you confirmed for this folder on this computer. It is kept in Hoprig's own state (previews.json), never in your repository.
  2. A preview block in the project's own project.json, in the style of check and live: "preview": { "run": ["npm", "run", "dev", "--", "--port", "{port}"] }. A list of words, run without a shell. These are someone else's words until you confirm them once, for exactly the words you were shown (Allow and start).
  3. The detector, from the project's files (the table below).
  4. It asks once, "How do I start it?", and remembers your answer.
the project hasHoprig starts
vite in package.json (React, Vue, Svelte, Solid, SvelteKit, Remix on Vite)the script with --port P --strictPort --host 127.0.0.1
next, gatsbythe script with -p P -H 127.0.0.1
astro, nuxt, @angular/clithe script with --port P --host 127.0.0.1
react-scriptsthe script, with PORT, HOST=127.0.0.1 and BROWSER=none in its environment
another package.json scriptthe script with PORT in its environment; its port is read from its first http://localhost:NNNN line
pubspec.yaml with flutter:flutter run -d web-server --web-hostname 127.0.0.1 --web-port P
artisan and composer.jsonphp artisan serve --host=127.0.0.1 --port=P
manage.pypython manage.py runserver 127.0.0.1:P (the project's .venv python when there is one)

The script is dev, else start, else serve. The package manager follows the lockfile (pnpm-lock.yaml, yarn.lock, bun.lock, else npm); only npm needs -- before the added words. Only the npm start has been run for real; the others are written from their documentation. The command runs as a list of words, never through a shell, without Hoprig's secrets in its environment.

The port

Hoprig asks the system for a free port and passes it, so two previews never fight over port 3000. A port you type, or a fixed port in the project's own words, is checked first: if something holds it, Hoprig says what ("Port 3000 is used by node (pid 4123)") and starts nothing. Stop it is offered only for a program of your own user. The dashboard's port, the gateway's and Hoprig's own can never be a preview.

Starting, waiting, stopping

  • The program runs in a terminal of its own named preview-..., in the project folder. Open that terminal to see the output or to type.
  • It is ready when http://127.0.0.1:P/ or [::1]:P gives any HTTP answer, asked every second for up to 3 minutes. A Flutter app is ready later: when Flutter says its build is served and the built app script answers (its web server answers at once, while it still builds, and a page opened then stays white). Until then the sheet says "Building the app: about N s on this computer" (the last measured start), then "Still building…" with the seconds; it waits up to 10 minutes.
  • When it stops by itself you get "It stopped: exit code N." and the last 40 lines of its output.
  • Stop ends the terminal and what runs in it, and the port is free within 5 seconds. Archiving the chat that asked ends it too.
  • After Hoprig restarts, a preview whose terminal is still alive (tmux keeps it) shows as running again. With the built-in terminals the terminal ends with Hoprig, and the preview says "Stopped (Hoprig restarted)".
  • Preview is not built for Windows yet.

The address

The address follows how you opened Hoprig, and the sheet shows that one address:

  • On this computer itself: the other loopback name than the one your dashboard page is on, so your dashboard's sign-in never travels with it, at a port the private proxy holds for this preview (the same port after Stop and Start, while Hoprig runs). It goes through the private proxy like every other address: the dev server's own port is not given out.
  • Through your own domain: a name on that domain, shown through the private proxy. If previews on your domain were never set up, the sheet says so in one line and has one press, Set up previews on your domain. It adds the one rule, makes the name for this project and opens it. The dashboard reconnects once, then never again for a preview.
  • Through the temporary link, or from a phone by a network address: a random address of its own (trycloudflare.com), through the private proxy. It starts only on your press, Get a temporary address, and ends with the preview. The sheet says: changes each time, streaming replies (SSE) do not work there. At most 3 at once. When cloudflared is not installed, one press, Install it, installs it as the public link does.

Open is always a plain link that signs you in to the preview. Check looks at the site from this computer through the proxy's local address, with a sign-in of its own, and asks the other address once without signing in to see that it is private. Every page (yours on any address, and the Check's) reaches the dev server under one name, so a Flutter app runs in all of them. Stop ends the address too: a random tunnel ends, a name on your domain stays reserved for the project and says the preview is not running.

The private proxy

A project's dev server listens on this computer only. For every address, this computer's own too, the proxy is the one door in front of it:

  • It is a listener of its own on this computer's loopback (port 7435 unless the settings say another: "preview": { "port": 7500 } in the config file). It is never the dashboard's listener, and the dashboard never serves a preview.
  • Nothing listens until the first preview is opened, unless previews on your own domain are on: then Hoprig holds the port from its start, so that no other program can sit behind the wildcard rule.
  • It tells previews apart by the address they are opened under. An address it does not know gets a plain "Not found". A preview that was stopped shows "This preview is not running." On this computer each preview has a port of its own (a browser sends a name's cookies to every port of it, so the cookie's name carries the port), and only the loopback names are answered there.
  • While a preview starts, a page opened at any of its addresses shows "Starting…" with the seconds so far and becomes the site by itself at Ready; nothing reaches the dev server before.

Who may open a preview

Only you, signed in to Hoprig.

  1. You press Open. That is a plain link to /preview/go/<id> on your dashboard, which needs your sign-in.
  2. The dashboard makes a ticket: random, used once, valid for 60 seconds, for that one preview on that one address. It sends you on to the preview's own address with the ticket in it.
  3. The proxy swaps the ticket for a cookie of its own (hoprig_pv): random, valid for 12 hours, for that address only, not readable by scripts, and sent only over HTTPS. Then it opens the front page. On this computer the address is plain http on a loopback name, so the cookie is hoprig_pv_<port> for that name and port, and the same sign-in is also kept partitioned (hoprig_pvf_<port>) for the pane beside the chat, which is another site.

Anyone else, and you without the cookie, see "This preview is private. Open it from your Hoprig." It shows no project name and no address of your dashboard. One request is answered without the cookie: the web app manifest a browser asks for without cookies; it gets a fixed stand-in with nothing of the project, so a page shows no failed request for it. A WebSocket without the cookie is refused. Five wrong tickets in a row from one visitor lock the swap for a while. Only a hash of a ticket or a cookie is kept, in memory. Stopping a preview ends its cookies; started again it needs a new ticket.

Your dashboard's own cookie never goes to the program that is previewed, and the proxy adds no key or token of any kind.

What the dev server sees

The project needs no change for the address it is shown under.

  • Host is localhost:<port>, so Vite, webpack-dev-server, Angular and Next see a local request.
  • Origin and Referer of the preview itself are rewritten to the local address. Another site's stay as they are.
  • Removed before the program sees the request: Hoprig's cookies (sid, hoprig_*), X-Forwarded-*, Forwarded, Via, X-Real-IP, True-Client-IP and Cloudflare's Cf-* headers. The app's own cookies and Authorization header go through untouched.
  • WebSockets (hot reload) and streams (Server-Sent Events) are passed on as they come, with no time limit.
  • On the way back, a redirect to the local address goes to the preview address, an app's cookies lose their Domain (they stay on the preview address), and X-Frame-Options is replaced by a rule that only your dashboard may frame the page. An app that sets its own frame-ancestors is left alone.
  • A Flutter app's debug script (*.bootstrap.js) names the address its debug client connects to, made from the local name; the proxy makes it the page's own address, so the client connects through the proxy too. Without it a page on a phone never starts the app and a Reload never reaches it. Nothing else of any page is changed.

What it refuses

  • A write or a WebSocket from another origin than the preview's own, even with the cookie. On your own domain another preview is the same site, so its pages could send your cookie along; the proxy does not take what they send.
  • A preview address is never one of the dashboard's allowed origins: the dashboard refuses a write or a terminal WebSocket that comes from a preview page.

What cannot be fixed by a proxy

An app that writes an absolute http://localhost:3000 into its pages or into an OAuth callback must use the address it is shown under there.

Previews on your own domain

Needs the Custom address of the public link, made with Hoprig (Settings, Public link). The addresses below use example.com; yours is the domain your dashboard already uses.

Once. One press, Set up previews on your domain, adds one rule to the tunnel: *.example.com leads to the private preview proxy. The rule comes after every exact name (the dashboard, the gateway, other services) and before the closing 404. The tunnel starts once more, so the dashboard reconnects in a few seconds. This is the only restart.

Per project. The first preview of a project makes one exact name, <project>-<computer>-preview.example.com (lower case, letters, digits and -, at most 63), with one DNS record (cloudflared tunnel route dns). It is not a wildcard record, so no other name on your domain is touched. The name is one level under the domain because Cloudflare's free certificate covers only the domain and its first-level names. If the name is taken, Hoprig uses -2, then -3 and says so; if all three are taken it says so and stops. The name is kept for the project on that computer and reused. A new name changes nothing in the tunnel's config file, so the tunnel is not restarted.

The domain is the dashboard's name without its first label (pc.example.com gives example.com).

A tunnel made with a token in the Cloudflare dashboard: Hoprig cannot change its names. The card says: add one public hostname, *.example.com to http://127.0.0.1:<proxy port>, in the Cloudflare dashboard, then press Check. Check asks a made-up name under the domain for the proxy's answer (the header X-Hoprig-Preview, from GET /__hoprig/ping); only a good answer turns previews on. Cloudflare's wildcard makes the DNS, so Hoprig makes no record per project.

Stop using previews on my domain removes the rule (one more restart) and lists the names to delete in Cloudflare: cloudflared cannot remove a DNS record. The records stay and lead to the proxy, which says the preview is not running.

What it never does. It never opens anything until you press Set up previews on your domain, or Get a temporary address. It never makes a wildcard DNS record. It never runs cloudflared through a shell: every command is an array of words. Tests run against a stand-in of cloudflared; your own Cloudflare account and DNS are not touched by them.

Check

Check opens the running site in a real browser on this computer (Chrome, Chromium or Edge; a browser of its own, with a profile that is removed afterwards) at three sizes: phone 390 px, tablet 768 px and wide 1440 px, and takes a picture of each, and of the dark theme where it differs. Pictures are WebP files of the whole page, up to five screens tall. It tells you in plain words:

  • The page itself: it did not answer, or answered 400 or more.
  • Page errors: an uncaught error or a console error, with the file and line.
  • Failed requests: a request that failed or answered 400 or more (the page's own, and other sites' marked so). A missing favicon is not counted.
  • A page that stays blank: after its load the page gets 10 seconds to draw something that is not one flat colour (read from the picture itself, so an app that draws on a canvas counts too); still flat: "The page stayed blank at phone size." The picture is taken once the page stands still (two pictures 0.4 seconds apart are the same), not at its first drawn frame; a page that never stands still is taken at the 10 seconds.
  • Sideways scroll: the page is wider than the screen, with the widest elements named.
  • Your address (when a public address for the preview exists): one request without a cookie must get the private page.

The verdict is No problems found at phone, tablet and wide., or N problems found. with one line each, worst first (ten shown, then "and N more"). Under it two lines that are always there: what was tested (page loads, a page that stays blank, page errors, failed requests, sideways scroll) and that the pictures are only looked at. "No problems found" means none of the tested rows failed; it never says the site looks right.

What it does not do: it presses nothing and fills in nothing, because that could change data; pages behind the site's own sign-in are seen as a visitor sees them; links are not followed yet. Under the rows above, the Check also shows the project's own lines of hoprig check (its tools after your yes; and for a project you said is a public website, site and page weight, read from the running preview). With no browser on the computer, the page-answers row still runs and the card says Pictures and page errors need Chrome or Chromium on this computer. Nothing that needs the browser is counted as passed.

One Check runs at a time on a computer; a second press waits ("Waiting for the other check"). It stops at 90 seconds and says what it got. The last 10 Checks of each project are kept in Hoprig's state folder, under checks/, and are served only to your signed-in page. The pictures of an unfinished site never leave the computer.

A mobile app: where it opens

For a Flutter, Expo or React Native app, a native Android app or a native iPhone app, Preview first asks where to open it: Android emulator, iPhone simulator, Web look (the app's web version in a phone frame) or My own phone. Hoprig reads the app's own files and this computer (hardware virtualisation, free memory, processors, the Android SDK and its emulator, Xcode on a Mac) and starts nothing until you press. Every device that can be used here is listed, each with one line of how long its start takes here: measured once it has started here, else "not measured here yet" or the stated cost of a first build. One row is marked Recommended: your last pick for the project, else your own phone when one is connected, else the device of a native app's kind, else on a Mac the iPhone simulator, elsewhere the Android emulator when the computer has the memory and is not busy, else the Web look (after your own phone's APK when plugins of the app have no web version), else your own phone's APK. Only a device Hoprig can open is ever Recommended. The rest fold into Not on this computer, each with why. In this version the Web look of a Flutter app opens from the sheet, My own phone makes its APK (not on Windows, and not on Linux on ARM) and the Android emulator opens the app (not on Windows); the iPhone simulator says what it would take and has no button yet.

The Android emulator. It is offered where the computer has hardware virtualisation that your user may use (/dev/kvm on Linux; on Linux a /dev/kvm you may not use says to add your user to the kvm group), 4 GB of free memory and 4 processors. When parts are missing its row has Set up (about 2 GB to download, 7 GB on disk, about 10 min): the sheet shows what goes where, the commands, and the licences of Google's SDK; nothing is installed until you accept them. It installs, into the Android SDK folder PC setup uses, only what is missing of emulator, platform-tools and one Google APIs system image for this processor, then makes one phone of Hoprig's own (hoprig-phone); a phone that is there already is used as it is. Remove the emulator takes away exactly what Hoprig put there. Open starts the emulator headless (one per computer; ready only when the phone has booted and its screen shows something), then runs flutter run -d <emulator> in a terminal tab of the chat: Reload (hot reload) and Restart (hot restart) are the tool's own keys, and the sheet says the tool's own line. The emulator stops with Stop (the sheet then shows the device list again), after 30 minutes unused, and with Hoprig. The phone keeps what you do on it, like a phone on your desk: Stop frees the sheet at once, and the emulator then ends the normal way and saves the phone's state behind it; an Open pressed while it still saves says so and starts when that is done. A start without a saved state is a full boot (about 32 s on a computer with 4 processors); later starts come from the saved state (about 15 s there), and its measured start shows on its row next time. A dialog of an app that is not responding, in front when you stop, gets its Wait pressed first (and each one that comes next, up to three): when the dialog then goes, the phone's state is saved without it; when it stays, or has no Wait button Hoprig can find, the emulator ends without saving, so the phone wakes next time in the state it had before this run (Hoprig's log says which way it went). When Hoprig itself stops, the emulator has only Hoprig's own short stopping time to save; if that is not enough, the next start is a full one. On a computer with 4 processors or fewer the emulator gets 2 of them, so a build beside it keeps the rest. The sheet stays open the whole way and says each step: starting the emulator, building the app (with the tool's own build progress, never the device's own log), running, or why it did not start (the lines of the tool's output that carry the cause first, at most 12, your home folder written as ~). The app is built with the Java PC setup finds (JAVA_HOME, your PATH, Android Studio's own, also at the workspace's development/android/android-studio, a JDK of the system); with none on the computer, the emulator's row says so and has no Open. Open on the sheet shows the emulator's screen inside Hoprig (below).

Check on the emulator. While the app runs on the emulator and no website of the project runs, Check takes one picture of the device's screen and tests that it is not blank: "No problems found on the Android emulator." or the problem, then always what it did not look at: crashes, Flutter errors, cut-off widgets, other screens and the dark theme are not looked at yet, and no button or form is pressed. The picture is kept with the other Checks. It first asks the device what is in front. When that is not the app (the home screen, another app, the lock screen, or a dialog that says an app is not responding or has stopped), it says so, with the name the device gave for another app or a dialog, as "Could not check: …", and that nothing of the app was tested. The app's name comes from the project's Android files (applicationId); when they name none, the Check says it could not tell whether the app in front is this project's.

The device's screen

When a device preview runs (an Android emulator started for the app), Open on the sheet shows the device's screen inside Hoprig: beside the chat on a wide screen, the whole screen on a phone with ← back to the chat. You press, long-press, swipe and type on it. Presses reach the device one at a time, in the order you made them; a tap goes through the emulator's own console, keys, text and swipes through adb shell input. Its bar has Back, Home, Recents, the keyboard (your phone's own keyboard types into it; the emulator takes plain letters, digits and signs for now), Reload (the app's hot reload: r of flutter run) and Picture (a PNG of the screen). Stop on the sheet ends the emulator, and the screen with it.

  • While the device starts: "Starting the device…" with the seconds so far; a device that did not start: "The device did not start" with the emulator's own sentence ("The emulator did not start within 240 s: …"); one that ran and then ended: "The device stopped" with "The emulator stopped: …"
  • The browser decodes the video itself (WebCodecs: Chrome, Edge, Safari 17 and later, Firefox 130 and later). A browser without it says "This browser cannot play the screen: use Chrome, or Safari 17 or later." and shows a picture every second instead; presses still work.
  • One screen at a time per device: another one that opens it says "Opened on another screen." with Take over.
  • When the page is hidden the stream pauses. A cut connection says "The screen stopped: the connection was cut." and connects again by itself (after 0.3 s, then 1 s, then every 2 s), with Reconnect.
  • Under the screen: "Press to screen: about N ms here · N frames a second", measured in your browser from a press to the first changed picture (the median of your last 20 presses). A small change such as one digit counts, and so does a blinking text cursor; the video's own noise does not.
  • A stream that falls behind is not queued: the viewer and the server drop to a fresh stream of the screen of now.

The screen comes over one WebSocket of the dashboard, /api/preview/<id>/screen (the preview's id): your sign-in and the dashboard's own Origin, as a terminal; for a project on another computer, /m/<computer>/api/preview/<id>/screen through the main computer's relay with that computer's token (the relay is there; the Preview sheet itself still opens only on the computer that runs the project). Presses, keys and text are checked on the server (inside the device's size, a key of a closed list, at most 200 characters at a time) before they reach the device. Nothing of the stream is kept. The framing is described at the top of src/preview/screenwire.mjs.

For another program

The routes are the owner's own page only (a token of another computer and the assistant's door are refused):

  • GET /api/preview?chat=<id>: the state, the address and how to open it (and what is missing, with its one press), how to start it, the last Check's line.
  • POST /api/preview/start { chat, command? } and POST /api/preview/stop { chat }.
  • POST /api/preview/address { chat, action }: the one press of a missing address, setup-domain, random or install-tunnel.
  • POST /api/preview/check { chat }, GET /api/preview/check?chat=<id>, and a picture at GET /api/preview/check/<id>/<size>-<theme>.webp (a device's Check: android-screen.png).
  • GET /api/preview/devices?chat=<id>: a mobile app's devices (the rows, the Recommended one, why the rest cannot here), or { mobile: false } for a website; POST /api/preview/device { chat, device } remembers the device he opens it on, for that project on this computer.
  • POST /api/preview/reload { chat }: the hot restart of a Flutter app's web look (the R key typed into its terminal); a website reloads in the browser instead. The answer says "Restarted" only when Flutter printed that it restarted. When no page of the app is open yet (just after Ready), the press is remembered: the page beside the chat is loaded again, the restart is sent the moment a page of the app connects, and the sheet says "Restarting…" until then. If no page connects within 30 s (or the app does not answer the restart within 15 s), the sheet says the change is not in the app and offers Stop and start, which builds it again from your files.
  • GET /api/preview/apk?chat=<id>, POST /api/preview/apk { chat } and POST /api/preview/apk/stop { chat }: My own phone. The press builds flutter build apk --debug in a terminal tab of the chat; the built file is copied into Hoprig's own state folder and kept 12 hours, or until Stop. The build runs without a Gradle or Kotlin daemon, and what it leaves running ends with it. It gets the Java PC setup finds, as the emulator's app run does. GET /api/preview/apk/<id>/<name>.apk gives it as a download (application/vnd.android.package-archive) to the owner's signed-in page or his signed-in phone only.
  • GET /api/preview/emulator?chat=<id> and POST /api/preview/emulator/open, /reload { chat, how } (reload or restart), /stop, /setup { chat, seen? }, /setup/stop and /remove, each with { chat }: the Android emulator. /setup without seen only reads what it would do and the licences; with seen (the hash of the licences shown) it runs. GET /api/preview/emulator/shot?chat=<id> gives a picture of its screen (PNG).
  • POST /api/preview/command and /free: the owner's own page, or another computer that controls this one. They are described at the top of src/preview/feature.mjs.
  • GET /preview/go/<id>: the link behind Open for a preview shown through the private proxy.
  • WS /api/preview/<id>/screen (?take=1 to take over, &video=0 for pictures only): a device preview's screen; GET /api/preview and GET /api/preview/emulator carry screen ({ id, state, width, height, since, error }, id being the socket's) while the project's emulator runs.

The project is the chat's own folder; no path comes from the browser. The start, stop and state come from the runner through its adapter (src/preview/adapter.mjs). When the project's own start words wait for your yes, the sheet shows them with one button, Allow and start.

Beside the chat (a wide screen)

On a screen 900 px wide or more, a preview that becomes ready while the sheet waits opens in a pane beside the chat; Show beside the chat on the sheet opens it there again. It is the diff pane's frame and place: one of the two is open at a time, and its × (or Esc) closes it. The preview keeps running.

  • Phone (390 px), Tablet (768 px), Full (the pane's own width). Tablet is offered only in a pane at least 768 px wide (narrower, it would look the same as Full). A Flutter app starts at Phone, a website at Full; your choice is kept.
  • Reload loads the site again (a Flutter app gets its hot restart, as the sheet's Reload). Open in a new tab opens the same address. Under the buttons, the one address the sheet shows, to read and copy.
  • A site whose own headers say it may not be framed (its own frame-ancestors) shows "This site does not allow being shown inside Hoprig." and Open in a new tab, never an empty box. Hoprig reads that from the site's first page; it strips nothing more.
  • A temporary address is not framed: the browser keeps its sign-in out of a frame of another site. The pane says so and offers Open in a new tab.
  • The pane stays open across a reload of the page while the preview runs. Stop closes it. A preview that stops by itself says so in the pane with the sheet's words, Ask the chat to fix it and Start again.
  • On a phone nothing changes: Open goes to the preview's own tab.

The dashboard page may frame only itself, the other loopback name (on this computer) and names under its own zone (on your domain); nothing else.

Not built yet

Sharing a link, a terminal tab for the output, Test, links followed by the Check, Expo's web look, the iPhone simulator, and the rest of a device's Check (walking its tabs, crashes and Flutter errors from its log, the dark theme).