My website's PC has a mood, and it lives in one Durable Object
"My website's PC is having a long day. Go check on it." That was the whole post when I shared this in a community dev Slack, plus a link, and it got a good reaction. This is the longer version. My website, andresmit.co.za, is a Windows 95 desktop, and My Computer now has a mood. Part of it comes fro

m my actual week. The rest comes from visitors, who can pat the case, pour it a coffee, tell it a joke, run Defrag or dust the fan. Everyone shares the same mood, and it fades. I was thinking of another easter egg for the website, something like that plant watering game, to hopefully keep visitors coming back.
The site already had the data. One GitHub Action reads my coffee loyalty card every day for the Coffee Analytics window, and another reads what I've been watching on Trakt. So the PC starts at 40, gets 15 if I've bought a coffee in the last two days, and 10 if I've watched an episode in the last seven. Pour it a coffee and it tells you my loyalty card says 161 visits (at the time of writing), and its own says one.
The number is real. It comes from the same file Coffee Analytics uses. The visitors' share is a lift from 0 to 50, kept on the server. It's stored as two numbers: how big it was, and when.
It halves every 12 hours, but nothing ever halves it. It's worked out whenever someone asks: /** * The lift `ageMs` after it was stored: it halves every half-life. Worked * out when it's read, so nothing has to wake up to fade it. */ export function decay(lift, ageMs, halfLifeHours) { if (!(lift > 0)) return 0; if (!(ageMs > 0)) return Math.min(lift, MAX_LIFT); return Math.min(lift, MAX_LIFT) * Math.pow(0.5, ageMs / (halfLifeHours * HOUR)); } No cron, no alarm, no timer.
If nobody visits for a week, nothing runs for a week, and the next visitor still gets the right number. Each cheer closes 8% of the gap to the top, so the first is worth 4 and the tenth about 2. The server accepts 30 cheers a minute across everyone, and the lift never passes 50. As I put it in the design doc, the worst a bot can do is make the PC happy.
Keep that in mind. I wrote a design doc before any code, and most of it is about where the shared number should live. Workers KV was out: last write wins, so two cheers in the same second could overwrite each other. D1 would have worked, but the per-minute cap needed extra tables.
A Durable Object is single-threaded, so cheers that arrive together are applied one after the other and none get lost. It gets SQLite storage on the free plan, and the caps can just live in memory. I also considered making it live, with WebSockets on the same object. The doc's verdict was that at about one concurrent visitor, a live feed talks to itself.
That's my site's real traffic, so live updates wait until the numbers say otherwise. The object stores the lift, when it was set, a total, the last action and when, and counts per day. That's it. There are no IP addresses, request logs are off, and a test pins the list of columns, so adding one means changing a test on purpose.
The Worker's tests run against Node's built-in node:sqlite, so the SQL in the tests is the SQL that runs on Cloudflare. All of this runs on Cloudflare's free plan, which stops rather than bills when you hit a limit. The Durable Object has a daily cap of 40,000 requests, after which it rests until midnight UTC and the page stops calling. For a while a comment in the config said this cap protected the Worker request quota too.
It didn't. A request counts the moment the Worker runs, whatever it answers, so the cap only ever spared the Durable Object. What does save Worker requests is blocking before the Worker: a rate limit on /api/, and a custom WAF rule that blocks cross-site calls and cheers without the site's Origin. Anyone determined can fake those headers, so it's a speed bump, not a wall.
If the quota still runs out, the route fails open, /api/mood falls through to a Pages 404, and the PC carries on in offline mode. The cheers-per-minute setting can't go above 30 for a similar reason. Each accepted cheer writes two rows, and 30 a minute all day is 86,400 rows, just under the free plan's 100,000 a day. A normal day is somewhere between 50 and 200 cheers.
On 3 November, the day after shared cheers went on, there were 191, from 23 page loads. On 11 November, 135. On 12 November (UTC) there were 15,522. Pat the case: 3,904.
Tell a joke: 3,923. Run Defrag: 3,866. Dust the fan: 3,826. Pour a coffee: 3.
The stats also count firstCheers. The page sends first: true with its first counted cheer, once per page load. On 12 November that was 1. On 13 November there were another 851 cheers, and it was 0.
So it doesn't look like thousands of visitors. It looks like one page, or one script, left running, and carrying on past midnight UTC until it stopped late on the 13th. I don't know who it was. The design held, which is the point.
The lift can't pass 50, so the PC was simply as happy as it can be. The worst a bot can do is make the PC happy, and it did. 18,704 requests reached the Durable Object that day, so the Worker ran a little more than that: still under a fifth of the free plan's 100,000 a day. At two rows per accepted cheer, that's about 31,000 rows written, under a third of the 100,000.
The daily cap of 40,000 never kicked in. Nothing was billed, and nothing went down. The same numbers are honest about the rest, too. After the script stopped, nobody cheered for three days.
Then I posted it on LinkedIn on the 17th, and there were 57 cheers that day and 80 the next. They're public, as anonymous totals per UTC day: andresmit.co.za/api/mood/stats. The other end of the scale had the opposite problem. The Tired bug turned up while I was preparing the launch clip.
The baseline started at 40 and only ever went up, and the lift is never negative. Tired is 20 to 34. So the PC could never be Tired. The tired face and the yawn worked fine; no real week could get there.
That was in my design doc, so I can't blame anyone else. The fix is one line: if (pcHealthStale(coffeeAt, 7, now) && pcHealthStale(watchedAt, 7, now)) { baseline -= 15; } A week with no coffee and no episode takes 15 off, and the PC sits at 25, Tired. The catch is that only known dates count. If the coffee fetcher hasn't seen a visit yet, or this browser hasn't loaded my watch history, that isn't a quiet week, just missing data.
The test pins the clock with Playwright's page.clock, serves a coffee eight days old and an episode ten days old, and checks for Tired, 25% and the yawn. The site is designed and directed by me and built with Claude. The site itself says Claude's favourite mistake is assuming everything works. This one was mine.
It keeps Cape Town hours. From 23:00 my time it dozes, whatever your clock says, and any button wakes it for ten minutes: "Huh? Oh, hello!" Every line it says lives in the page and goes in with textContent. The API only ever sends action IDs and numbers, so there's nothing a visitor can put on the page.
There are 20 jokes, and you won't hear one twice until you've heard them all. Run Defrag and you'll hear the hard drive going: a 120 Hz hum for the spindle, and 12 ms clicks of filtered noise for the heads seeking. It's synthesised with Web Audio, so there's no file to download. I cannot take full credit at all, but Wilhelm Kirschbaum suggested the defrag sound when defragging.
This is hands down the best! It's MIT and on GitHub: github.com/iiSmitty/my-website. The API is in cf-mood/. The maths is in mood-core.js, with no Cloudflare imports, and the Worker and the Durable Object are in worker.js.
The design doc is in docs/proposals/cheer-up-the-pc.md. What's next? Honestly, I need to go think about it. I think overengineering is my downfall.
Do I really want to flesh it out more on a personal portfolio site? Maybe, if the visitors absolutely love it. If it's after 23:00 in Cape Town, it's asleep. Any button will wake it.
Key Takeaways
- โขThe rest comes from visitors, who can pat the case, pour it a coffee, tell it a joke, run Defrag or dust the fan.
- โขThis story was reported by Dev.to, covering developments in the dev space.
- โขAI advancements continue to reshape industries โ read the full article on Dev.to for complete coverage.
๐ Continue reading the full article:
Read Full Article on Dev.to โShare this article



