· 5 min read · updated

A small tool only I can open

I had more than 200 cover drafts to choose from, so I built a page to pick them. It's a Vite plugin that only runs in development, uses the same code as my commands, and turns away requests from other sites.

Toolbox

I can make covers that follow my site's theme with two commands, one to make drafts and one to use my favourite. It works, but choosing was slow. I had more than 200 drafts in dozens of folders, and they're magenta and lime until I use one. I couldn't see a draft the way my readers would.

So I built a page for it. It shows each draft in any theme, with its glow and its real card, next to the current cover. I can use a draft, reject it, fix its title or make more.

The cover picker: a list of posts, a large draft beside the current cover, a strip of drafts, and forms for the scene and new drafts
The picker in the Dracula theme, with the draft on the left and the current cover on the right.

A page that's never built

My site is static, so every SvelteKit route ends up in the build. This page can change files on my computer, so I don't want any of it there. Instead, it's a small Vite plugin with apply: 'serve', so it only runs with the dev server. It answers /covergen with a tiny HTML page, and Vite serves the Svelte component like any other file. Nothing on the site imports it, so it never reaches the build.

It can still use the site's own code. The previews use the same component and CSS as the real covers, so I don't have a copy that could start to look different.

Local doesn't mean private

It's easy to think that only you can use a page on localhost. But any site open in your browser can send a request to it. The site usually can't read the answer, but my page doesn't just show things. It also replaces covers and starts programs, so the request alone could do damage. These are the checks:

ts
if (!localAddress.includes(address) || !localHost.test(headers.host ?? '')) return false;
if (method === 'GET') return true;
return (
	method === 'POST' &&
	headers.origin === `http://${headers.host}` &&
	headers['content-type'] === 'application/json'
);
  • The address has to be this computer's.
  • Host has to be a local name. A site can point its own name at 127.0.0.1, so your browser thinks both are the same site. This is called DNS rebinding.
  • Origin says which page sent the request. The browser sets it, and the page's JavaScript can't change it. Only my picker can change anything.
  • JSON only: an ordinary HTML form can't send that content type.

After that, the server only accepts posts and drafts that exist, and numbers within a set range. It starts the command with spawn() and no shell, so each value is just an argument. next; ls can't become a second command. These checks are a small function of their own, and its tests include all the requests it should refuse.

One way to make drafts

The page could have made the drafts itself, but then I'd have two versions of the same code. Instead, it runs the same command I'd type in a terminal and shows its output. It even shows the command, so I can copy it.

The drafts are made in a separate process, so the dev server stays quick. But now I could start drafts from the page and a terminal at once, and two jobs would use too much memory. So the command now creates a lock file with its process id. If that process no longer exists, the lock is ignored, so a crash can't block the next job.

Stop sends the command a signal. It cancels the image, saves what's finished and ends. Ctrl+C in a terminal now does the same.

Two files that only work together

My site won't load if a post has a cover but no alt text. That's a good rule, but it means a button that only saves the cover would break the site until I added the text.

So Use this cover asks for the alt text first, in every language the post has. The server makes and checks everything, and only then writes any files. If something's wrong with the text, nothing changes. It can't rule out a disk error halfway through, but mistakes are caught before anything is written.

Saving a scene needed care too. At first, every scene was in one big settings file. A YAML library wrote the whole file back and changed the line breaks in almost every scene, so one new title would have filled the Git diff. I got around it by replacing only that scene's lines, but the real fix was keeping each cover's details next to its post. Each file now has only one job, so it's safe to write back.

Testing Stop without a GPU

When I went to test Stop, there wasn't enough free memory to load the image model. I didn't want to get around my own safety limit, and the model wasn't what I was testing anyway.

So I wrote a stand-in server of about 15 lines that answers like the real one. It waits a few seconds and returns an image I already have. The page, the command, the lock and Stop were all real. It also kept a list of every request, so I could check that the first draft was saved, the second was cancelled and the memory was freed.

It can't tell me whether the real model works from the page. I still have to try that. But it's a much better way to test Stop, because the timing is the same every time.

A tool like this doesn't have to be fancy. It only needs to do the job, use the same code as everything else, and be as careful as anything that can change your files.