{"title":"Evert Pot's blog","link":{"@attributes":{"type":"application\/atom+xml","rel":"self","href":"https:\/\/evertpot.com\/atom.xml"}},"updated":"2026-11-28T07:13:51+00:00","id":"http:\/\/evertpot.com\/","author":{"name":"Evert Pot","email":"me@evertpot.com"},"entry":[{"id":"http:\/\/evertpot.com\/ssh-tunnel-if-unresolvable\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/ssh-tunnel-if-unresolvable\/"}},"title":"Using a SSH bastion, but only when I'm touching grass","updated":"2026-07-21T01:35:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I\u2019m a few years into my homelab. (might go into a bit more detail about that\nat some point!), and I often need to <code class=\"language-plaintext highlighter-rouge\">ssh<\/code> into one of my servers.<\/p>\n\n<p>At home, I can just do this by using a domain name like:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>ssh ahoy.m.example.ca\nssh bread.m.example.ca\n<\/code><\/pre><\/div><\/div>\n\n<p>I have set up <a href=\"https:\/\/en.wikipedia.org\/wiki\/Split-horizon_DNS\">Split-Horizon DNS<\/a> at home using <a href=\"https:\/\/thekelleys.org.uk\/dnsmasq\/doc.html\">DNSMasq<\/a>, so these domains resolve but\nonly at home.<\/p>\n\n<p>When I\u2019m outside, I use a a \u2018bastion\u2019 server that I can <code class=\"language-plaintext highlighter-rouge\">ssh<\/code> into and hop\nto another server. It\u2019s on a \u2018secret\u2019 port.<\/p>\n\n<p>To automatically tell SSH to go <em>via<\/em> the bastion server, I have this in my <code class=\"language-plaintext highlighter-rouge\">~\/.ssh\/config<\/code>:<\/p>\n\n<div class=\"language-ssh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">Host<\/span> *.m.example.ca\n    <span class=\"k\">ForwardAgent<\/span> <span class=\"no\">yes<\/span>\n    <span class=\"k\">User<\/span> evert\n    <span class=\"k\">ProxyCommand<\/span> ssh -q -W %h:%p -l evert bastion.example.ca -p <span class=\"m\">6767<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p><code class=\"language-plaintext highlighter-rouge\">bastion.example.ca<\/code> does resolve to a real IP (my home IP). The annoyance with\nthis is that if I use one of the above commands, I always go through the bastion\nwhether I\u2019m home or not.<\/p>\n\n<p>I recently found about about the <code class=\"language-plaintext highlighter-rouge\">Match<\/code> and <code class=\"language-plaintext highlighter-rouge\">ProxyJump<\/code> command, and cludged\ntogether this:<\/p>\n\n<div class=\"language-ssh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">Match<\/span> host *.m.example.ca exec \"! getent hosts %h &gt;\/dev\/null\"\n    <span class=\"k\">ForwardAgent<\/span> <span class=\"no\">yes<\/span>\n    <span class=\"k\">User<\/span> evert\n    <span class=\"k\">ProxyJump<\/span> evert@bastion.example.ca:6767\n<\/code><\/pre><\/div><\/div>\n\n<p>This works the same as before, except this rule only kicks in if the command\ncommand <code class=\"language-plaintext highlighter-rouge\">getent hosts %h<\/code> fails.<\/p>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">ProxyJump<\/code> is a slightly simpler command from <code class=\"language-plaintext highlighter-rouge\">ProxyCommand<\/code>. I think this\nhas been around for a bit, but I\u2019ve just been copy-pasting <code class=\"language-plaintext highlighter-rouge\">ProxyCommand<\/code> from\nprevious configs for a decade+.<\/p>\n\n<p>You might not have a DNS setup like this, and might be using <code class=\"language-plaintext highlighter-rouge\">.local<\/code> domains\nto access local servers, if this also work, HOWEVER:<\/p>\n\n<div class=\"language-ssh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">Match<\/span> host *.local exec \"! getent hosts %h &gt;\/dev\/null\"\n    <span class=\"k\">HostName<\/span> %h\n    <span class=\"k\">ForwardAgent<\/span> <span class=\"no\">yes<\/span>\n    <span class=\"k\">User<\/span> evert\n    <span class=\"k\">ProxyJump<\/span> bastion.example.ca:6767\n<\/code><\/pre><\/div><\/div>\n\n<p>Every time you do this, you computer likely first\nbroadcasts on your local network it\u2019s looking for that machine. <code class=\"language-plaintext highlighter-rouge\">.local<\/code> \/ MDNS\nalso tends to be a bit slower when you\u2019re local, and given that you\u2019re paying\nthat cost. <code class=\"language-plaintext highlighter-rouge\">ForwardAgent<\/code> agent can also make this extremely dangerous because\nsomeone might impersonate <code class=\"language-plaintext highlighter-rouge\">target.local<\/code> and get access to your entire SSH keychain.\nSo maybe for that case it\u2019s not the worst thing to just always go\nthrough a bastion server. If so, this config is for you:<\/p>\n\n<div class=\"language-ssh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">Host<\/span> *.local\n    <span class=\"k\">HostName<\/span> %h\n    <span class=\"k\">ForwardAgent<\/span> <span class=\"no\">yes<\/span>\n    <span class=\"k\">User<\/span> evert\n    <span class=\"k\">ProxyJump<\/span> bastion.example.ca:6767\n<\/code><\/pre><\/div><\/div>\n\n<p>This <em>always<\/em> go through the bastion for every SSH connection to a <code class=\"language-plaintext highlighter-rouge\">.local<\/code> host.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/broadcasting-gps-on-local-network\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/broadcasting-gps-on-local-network\/"}},"title":"Broadcasting GPS on the local network","updated":"2026-05-03T21:10:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>Ever since <a href=\"https:\/\/www.omgubuntu.co.uk\/2024\/03\/mozilla-location-services-axed\">Mozilla killed its GPS location service<\/a>, GPS hasn\u2019t been very\naccurate for me on Linux. The system on linux that handles location on many\nlinux systems is called <a href=\"https:\/\/github.com\/erfanoabdi\/geoclue\">Geoclue<\/a>, and this system is used by for example\nFirefox and Gnome Maps (notably not Chrome).<\/p>\n\n<p>Based on the output of\n<code class=\"language-plaintext highlighter-rouge\">\/usr\/libexec\/geoclue-2.0\/demos\/where-am-i<\/code>, it uses a GeoIP database which\nplaces me somewhere in Toronto with 25KM accuracy:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code> \u00bb \/usr\/libexec\/geoclue-2.0\/demos\/where-am-i\nClient object: \/org\/freedesktop\/GeoClue2\/Client\/1\n\nNew location:\nLatitude:    43.706400\u00b0\nLongitude:   -79.398600\u00b0\nAccuracy:    25000 meters\nDescription: GeoIP (ichnaea)\nTimestamp:   Sun 03 May 2026 04:00:10 PM (1777838410 seconds since the Epoch)\n<\/code><\/pre><\/div><\/div>\n\n<p>Note: to install <code class=\"language-plaintext highlighter-rouge\">where-am-i<\/code>, you might need to run:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code># Fedora\nsudo dnf install geoclue2-demos\n\n# Debian family\nsudo apt install geoclue-2-demo\n<\/code><\/pre><\/div><\/div>\n\n<p>I could try to find an alternative service (suggestions welcome!), but I have\nsome servers at home, and it made me wonder if there\u2019s something I can run\nlocally. The servers don\u2019t move, so the logic was that as long as I\u2019m on the\nhome network, I can just decide what GPS coordinates to emit.<\/p>\n\n<p>Turns out, there is!<\/p>\n\n<p>The protocol is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/NMEA_0183\">NMEA 0183<\/a>, which appears to be a suite of\nspecifications for marine electronics (ships!). The messages can be sent\nover a serial port or over a TCP socket.<\/p>\n\n<p>For example, a message with GPS information might look like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>$GPRMC,204049.000,A,5308.3999,N,00601.9266,E,0.000,0.000,030526,,*02\n$GPGGA,204049.000,5308.3999,N,00601.9266,E,1,08,1.0,119.0,M,0.0,M,,*6F\n<\/code><\/pre><\/div><\/div>\n\n<p>It\u2019s also supported, and enabled by default by GeoClue. The settings in \n<code class=\"language-plaintext highlighter-rouge\">\/etc\/geoclue\/geoclue.conf<\/code> look like this for me:<\/p>\n\n<div class=\"language-ini highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c\"># Network NMEA source configuration options\n<\/span><span class=\"nn\">[network-nmea]<\/span>\n\n<span class=\"c\"># Fetch location from NMEA sources on local network?\n<\/span><span class=\"py\">enable<\/span><span class=\"p\">=<\/span><span class=\"s\">true<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The way GeoClue does the look up, is that it searches for an MDNS entry\nfor a service called <code class=\"language-plaintext highlighter-rouge\">_nmea-0183._tcp<\/code>. If it finds one, it connects to the\naddress in the record and gets the GPS information.<\/p>\n\n<p>So, I figured I could just write a small server (with some help from Claude)\nthat emits these lines and registers itself with <a href=\"https:\/\/avahi.org\/\">Avahi<\/a> (the standard\nMDNS implemenetation on Linux, or a Mac it would be Bonjour). MDNS is also\nthe thing that lets you use <code class=\"language-plaintext highlighter-rouge\">.local<\/code> addresses on the local network, or\ndiscover things like printers, TVs and so on.<\/p>\n\n<p>I shared this on Github: <a href=\"https:\/\/github.com\/evert\/nmea-static-gps-server\/blob\/main\/nmea_static_gps_server.py\">https:\/\/github.com\/evert\/nmea-static-gps-server\/blob\/main\/nmea_static_gps_server.py<\/a>.<\/p>\n\n<p>This is the TCP server that emits the GPS info once per second. The repo also\nincludes Avahi configuration that looks like:<\/p>\n\n<div class=\"language-xml highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">&lt;?xml version=\"1.0\" standalone='no'?&gt;<\/span>\n<span class=\"cp\">&lt;!DOCTYPE service-group SYSTEM \"avahi-service.dtd\"&gt;<\/span>\n<span class=\"nt\">&lt;service-group&gt;<\/span>\n  <span class=\"nt\">&lt;name<\/span> <span class=\"na\">replace-wildcards=<\/span><span class=\"s\">\"yes\"<\/span><span class=\"nt\">&gt;<\/span>NMEA GPS (%h)<span class=\"nt\">&lt;\/name&gt;<\/span>\n  <span class=\"nt\">&lt;service&gt;<\/span>\n    <span class=\"nt\">&lt;type&gt;<\/span>_nmea-0183._tcp<span class=\"nt\">&lt;\/type&gt;<\/span>\n    <span class=\"nt\">&lt;port&gt;<\/span>10110<span class=\"nt\">&lt;\/port&gt;<\/span>\n  <span class=\"nt\">&lt;\/service&gt;<\/span>\n<span class=\"nt\">&lt;\/service-group&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>After this file is copied to <code class=\"language-plaintext highlighter-rouge\">\/etc\/avahi\/services\/nmea-statis-gpc.service<\/code>,\nyou can test on other machines to see if the record can be discovered with:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>$ avahi-browse  _nmea-0183._tcp -r -t\n+ wlp192s0 IPv6 NMEA GPS (node05)                             _nmea-0183._tcp      local\n+ wlp192s0 IPv4 NMEA GPS (node05)                             _nmea-0183._tcp      local\n= wlp192s0 IPv6 NMEA GPS (node05)                             _nmea-0183._tcp      local\n   hostname = [node05.local]\n   address = [fe80::a8c2:15de:9af:19b]\n   port = [10110]\n   txt = []\n= wlp192s0 IPv4 NMEA GPS (node05)                             _nmea-0183._tcp      local\n   hostname = [node05.local]\n   address = [192.168.2.205]\n   port = [10110]\n   txt = []\n<\/code><\/pre><\/div><\/div>\n\n<p>In my case the service is running on a machine called <code class=\"language-plaintext highlighter-rouge\">node05.local<\/code>.\nThe service itself can easily be tested with <code class=\"language-plaintext highlighter-rouge\">telnet<\/code>:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nv\">$ <\/span>telnet node05.local 10110\n<\/code><\/pre><\/div><\/div>\n\n<p>Once this is all in place, I just had to restart Geoclue on the client machine\nand it started picking up the GPS coordinates from the server:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nv\">$ <\/span><span class=\"nb\">sudo <\/span>systemctl restart geoclue\n<span class=\"nv\">$ <\/span>\/usr\/libexec\/geoclue-2.0\/demos\/where-am-i\n<\/code><\/pre><\/div><\/div>\n\n<p>Which gave the exact coordinates from the server!<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>Client object: \/org\/freedesktop\/GeoClue2\/Client\/3\n\nNew location:\nLatitude:    43.645758\u00b0\nLongitude:   -79.410510\u00b0\nAccuracy:    0 meters\nAltitude:    119.000000 meters\nSpeed:       0.000000 meters\/second\nDescription: GPS GGA+RMC\nTimestamp:   Sun 03 May 2026 04:58:58 PM (1777841938 seconds since the Epoch)\n<\/code><\/pre><\/div><\/div>\n\n<p>A quick test with Gnome Maps also immediately showed the correct location.\nFirefox needed a restart for me.<\/p>\n\n<figure>\n    <img src=\"\/assets\/posts\/nmea\/gnome-maps.png\" alt=\"Gnome Maps showing the correct location\" class=\"fill-width\" \/>\n    <figcaption>Gnome Maps showing the correct location<\/figcaption>\n<\/figure>\n\n<figure>\n    <img src=\"\/assets\/posts\/nmea\/firefox.png\" alt=\"Firefox showing the correct location\" class=\"fill-width\" \/>\n    <figcaption>Firefox showing the correct location<\/figcaption>\n<\/figure>\n\n<p>Now I never have to wait for a slower, inaccurate GPS lookup again and as long\nas I\u2019m home all my Linux machines will just instantly pick up the correct\nlocation.<\/p>\n\n<p>It also <em>seemed<\/em> to work on Apple Maps on a Mac, but only when Location\nServices was turned off. I didn\u2019t get an exact dot on the map but it got the\narea right.<\/p>\n\n<p>For fun, you could also use this to spoof incorrect locations to your\nLinux-using guests and co-workers.<\/p>\n\n<p>Hope this tool is useful to anyone else. If you\u2019d like to contribute,\nmore\/better setup instructions for other distros are appreciated (if they\nare different).<\/p>\n\n<p>Link once more:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/evert\/nmea-static-gps-server\">https:\/\/github.com\/evert\/nmea-static-gps-server<\/a><\/li>\n<\/ul>\n\n"},{"id":"http:\/\/evertpot.com\/burrow-gopher-client\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/burrow-gopher-client\/"}},"title":"Burrow, a new Gopher client in the browser","updated":"2026-03-27T18:30:07+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>After building a <a href=\"\/now-on-gopher\">gopher server and website<\/a>, I couldn\u2019t quite get enough\nof the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gopher_(protocol)\">Gopher<\/a> bug, and decided to build a Gopher client in the browser.<\/p>\n\n<p>To be honest, this is actually more like a HTTP proxy for Gopher, but I might\nat some point turn this into a standalone client or even port it to some\nnative platforms.<\/p>\n\n<p>Anyway, here\u2019s a screenshot:<\/p>\n\n<figure>\n    <img src=\"\/assets\/posts\/gopher\/burrow.png\" alt=\"Screenshot of Burrow, a Gopher client in the browser\" class=\"fill-width\" \/>\n    <figcaption>Burrow screenshot<\/figcaption>\n<\/figure>\n\n<p>And if you want to try it out, go to <a href=\"https:\/\/burrow.din.gy\/\">https:\/\/burrow.din.gy\/<\/a>,\nand browse Gopherspace!<\/p>\n\n<p>It\u2019s GPL licensed, source code is available on <a href=\"https:\/\/github.com\/evert\/burrow\">GitHub<\/a>.<\/p>\n\n<p>Visit my website, at <code class=\"language-plaintext highlighter-rouge\">gopher:\/\/hole.din.gy\/<\/code> !<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/now-on-gopher\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/now-on-gopher\/"}},"title":"Now available on Gopher!","updated":"2026-02-21T16:09:07+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>When I just got on the internet and spent a lot of time tinkering with\nWindows 98 internet settings, just to get and stay online I first learned\nabout something called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gopher_(protocol)\">Gopher<\/a> via this settings screen:<\/p>\n\n<figure>\n  <img src=\"\/assets\/posts\/gopher\/win98-settings.png\" class=\"fill-width\" \/>\n  <figcaption>Windows 98 proxy setting screen<\/figcaption>\n<\/figure>\n\n<p>I was always a bit curious about this. At the time all major browsers\nsupported Gopher and occasionally would run into an elusive <code class=\"language-plaintext highlighter-rouge\">gopher:\/\/<\/code> link,\nperfectly integrated with the rest of the web, but also feeling somewhat\nalien.<\/p>\n\n<p>In 2006 I learned about <a href=\"https:\/\/en.wikipedia.org\/wiki\/Inetd\">inetd<\/a> on linux and it occured to me that I could\nbuild a gopher server with PHP without needing to learn about sockets (which\nseemed hard). So I made a little server and <a href=\"https:\/\/evertpot.com\/100\/\">published it on this blog<\/a>.\nMost Linuxes don\u2019t ship inetd by default anymore, but at the time it was a\ngreat way to build simple TCP services without knowing anything about TCP.\nJust reading and writing to <code class=\"language-plaintext highlighter-rouge\">STDIN<\/code> and <code class=\"language-plaintext highlighter-rouge\">STDOUT<\/code> was all you needed to do.<\/p>\n\n<p>I never really kept that gopher site going, mainly because in order to\nhost it, you need an IP address (no vhosts!) and a running server, and I\ndidn\u2019t really want to pay for that.<\/p>\n\n<p>Then a few years later all major browsers dropped support for it (and Chrome\nnever had support).<\/p>\n\n<p>Fast forward to 2026, and I think gopher is in a bit of a resurgence.\nThe protocol is very limited, and as a result it\u2019s inherently a place\nwithout tracking, ads, comment boxes, corporate interests, AI,\nmisinformation and short-form video. Now that the excitement and\nidealism of the internet in mid 00\u2019s is dead and buried, Gopher (and\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Gemini_(protocol)\">Gemini<\/a>) offers a little respite.<\/p>\n\n<p>The biggest year for Gopher in this century was actually 2025, when the number\nof active gopher sites reached a whopping <strong>432<\/strong> according to\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Gopher_(protocol)\">Wikipedia<\/a>, Most of these sites are just personal spaces, like the\nhomepages of yore.<\/p>\n\n<p>I also recently built a little homelab, so this seemed like the perfect time\nto finally set up a permament gopher server (or \u2018hole\u2019).<\/p>\n\n<p>If this sounds interesting, go take a look:<\/p>\n\n<ul>\n  <li>My Gopher site: <a href=\"gopher:\/\/hole.din.gy\">gopher:\/\/hole.din.gy<\/a>.<\/li>\n  <li>The source on GH: <a href=\"https:\/\/github.com\/evert\/hole\/\">https:\/\/github.com\/evert\/hole\/<\/a> (feel free to fork and dig your own hole!)<\/li>\n<\/ul>\n\n<p>To access gopher sites, the easiest is probably to use <code class=\"language-plaintext highlighter-rouge\">lynx<\/code>, found in a\npackage manager near you, or you can use the shiny <a href=\"https:\/\/gmi.skyjake.fi\/lagrange\/\">Lagrange<\/a> client.<\/p>\n\n<p>If you\u2019re interested in the history of Gopher, Abort Retry Fail has a great\narticle titled <a href=\"https:\/\/www.abortretry.fail\/p\/the-internet-gopher-from-minnesota\">\u201cThe Internet Gopher from Minnesota\u201d<\/a>.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/multi-step-login-forms-for-password-managers\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/multi-step-login-forms-for-password-managers\/"}},"title":"Building multi-step login forms that work well with password managers","updated":"2025-04-13T10:28:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>Password managers are still the best way to manage credentials, and if you\nare doing things on the web, you should be using one.<\/p>\n\n<p>Increasingly, websites are using multi-step login forms. They might first\nask for <em>just<\/em> an email address and only after present users with a password\nfield.<\/p>\n\n<figure>\n  <img src=\"\/assets\/posts\/login-forms\/google-login.png\" class=\"fill-width\" \/>\n  <figcaption>Google Login form<\/figcaption>\n<\/figure>\n\n<figure>\n  <img src=\"\/assets\/posts\/login-forms\/amazon-login.png\" class=\"fill-width\" \/>\n  <figcaption>Amazon Login form<\/figcaption>\n<\/figure>\n\n<p>Unfortunately, this doesn\u2019t always work well with password managers. Password\nmanagers are such an integral part of the web, I think it\u2019s important as\nweb developers to make this work as best as possible. It\u2019s also an\naccessibility issue.<\/p>\n\n<h2 id=\"why-do-websites-use-multi-step-login-forms\">Why do websites use multi-step login forms?<\/h2>\n\n<p>The short version is that many companies want to centralize their login \/\npassword management systems for all their employees. This lets them change\npasswords in a single place, and also disable accounts after an employee\nleaves the company. This is called Single Sign-On (SSO) and is often facilitates\nusing the SAML or OpenID Connect protocols.<\/p>\n\n<p>When you log in with a web application that supports this, and you enter\nyour email, the first thing the system needs to do is check if a SSO system\nis in place for your account (or the domainname of your email address), and\nif so they will redirect out to your sign-on system.<\/p>\n\n<p>Only if this is not the case, they will present you with a password field.<\/p>\n\n<p>Another reason is that systems increasingly allow users to auhtenticate with\nmeans other than a password, and similarly they first need to know your\nemail address \/ username before they can know what login flow to present you\nwith.<\/p>\n\n<h2 id=\"password-managers\">Password managers<\/h2>\n\n<p>Password managers such as <a href=\"https:\/\/keepassxc.org\/\">KeepassXC<\/a>, <a href=\"https:\/\/bitwarden.com\/\">Bitwarden<\/a> and <a href=\"https:\/\/1password.com\/\">1Password<\/a>,\nbut also the \u2018save password\u2019 feature that\u2019s built into browsers look for\nHTML forms that are roughly \u2018login form shaped\u2019 to do their thing. If something\nlooks like a login form, they will suggest or auto-fill your username and\npassword.<\/p>\n\n<p>The problem with multi-step login forms that ask for an email address first,\nis that they no longer look like plain old login forms, which either means\nthat a user has to do more clicks to complete the login, or in the worst\ncase the password manager can\u2019t detect the fields at all and you end up\ncopy-pasting your password. This is pretty annoying and something I see people\noften yelling about on the internet.<\/p>\n\n<h2 id=\"so-how-do-you-fix-this\">So, how do you fix this?<\/h2>\n\n<h3 id=\"1-make-sure-you-have-the-correct-autocomplete-attribute\">1. Make sure you have the correct autocomplete attribute<\/h3>\n\n<p>If you have a lone <code class=\"language-plaintext highlighter-rouge\">&lt;input&gt;<\/code> field for the username, it should have the\nattribute to signal password managers that this is a username field:<\/p>\n\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nt\">&lt;input<\/span> <span class=\"na\">type=<\/span><span class=\"s\">\"text\"<\/span> <span class=\"na\">autocomplete=<\/span><span class=\"s\">\"username\"<\/span> <span class=\"na\">name=<\/span><span class=\"s\">\"username\"<\/span> <span class=\"na\">required<\/span> <span class=\"nt\">\/&gt;<\/span>\n\nor:\n\n<span class=\"nt\">&lt;input<\/span> <span class=\"na\">type=<\/span><span class=\"s\">\"email\"<\/span> <span class=\"na\">autocomplete=<\/span><span class=\"s\">\"username\"<\/span> <span class=\"na\">name=<\/span><span class=\"s\">\"username\"<\/span> <span class=\"na\">required<\/span> <span class=\"nt\">\/&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"2-add-a-hidden-username-field-on-the-password-page\">2. Add a hidden username field on the password page<\/h3>\n\n<p>The <a href=\"https:\/\/www.chromium.org\/developers\/design-documents\/form-styles-that-chromium-understands\/\">Chrome Wiki<\/a> recommends that when you collect the email first, and\nredirect the user after to a password page, to include the username\nfield again (prefilled), and hidden with CSS.<\/p>\n\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nt\">&lt;input<\/span>\n    <span class=\"na\">type=<\/span><span class=\"s\">\"email\"<\/span>\n    <span class=\"na\">autocomplete=<\/span><span class=\"s\">\"username\"<\/span>\n    <span class=\"na\">name=<\/span><span class=\"s\">\"username\"<\/span>\n    <span class=\"na\">required<\/span>\n    <span class=\"na\">value=<\/span><span class=\"s\">\"spam@evertpot.com\"<\/span>\n    <span class=\"na\">style=<\/span><span class=\"s\">\"display:none\"<\/span>\n<span class=\"nt\">\/&gt;<\/span>\n\n<span class=\"nt\">&lt;input<\/span>\n    <span class=\"na\">type=<\/span><span class=\"s\">\"password\"<\/span>\n    <span class=\"na\">autocomplete=<\/span><span class=\"s\">\"password\"<\/span>\n    <span class=\"na\">name=<\/span><span class=\"s\">\"password\"<\/span>\n    <span class=\"na\">required<\/span>\n<span class=\"nt\">\/&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is particularly great if the user has multiple accounts on your system.\nThe pre-filled username lets a password manager detect which account is\nbeing used to log in.<\/p>\n\n<p>This might also be important for ensuring that password managers offer to save\nthe credentials in the first place.<\/p>\n\n<h3 id=\"3-if-youre-using-one-time-codes-also-annotate-those-fields\">3. If you\u2019re using one-time-codes, also annotate those fields<\/h3>\n\n<p>Many login systems use SMS codes or TOTP (Authenticator App) codes for\nadditional security. Password managers let you store TOTP codes, but in order\nfor your password manager to detect the field, you also need to annotate this\ncorrectly:<\/p>\n\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nt\">&lt;input<\/span>\n    <span class=\"na\">type=<\/span><span class=\"s\">\"text\"<\/span>\n    <span class=\"na\">autocomplete=<\/span><span class=\"s\">\"one-time-code\"<\/span>\n    <span class=\"na\">name=<\/span><span class=\"s\">\"otp\"<\/span>\n    <span class=\"na\">required<\/span>\n    <span class=\"na\">pattern=<\/span><span class=\"s\">\"0-9{6}\"<\/span>\n    <span class=\"na\">inputmode=<\/span><span class=\"s\">\"numeric\"<\/span>\n\n<span class=\"nt\">\/&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The key attribute here is \u2018autocomplete\u2019. You can even get this auto-filled\nwith SMS codes, if you use a <a href=\"https:\/\/wicg.github.io\/sms-one-time-codes\/\">specially formatted SMS<\/a>, which looks a bit\nlike this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>Your one-time code is 123123\n\n@mydomain #123123\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"if-you-want-users-to-be-able-to-auto-login-use-html-correctly\">If you want users to be able to auto-login use HTML correctly.<\/h3>\n\n<p>The <a href=\"https:\/\/developer.1password.com\/docs\/web\/compatible-website-design\/\">1Password<\/a> site describes that they simulate a \u2018click\u2019 on a login\nbutton to trigger the login.<\/p>\n\n<p>But if there\u2019s no <code class=\"language-plaintext highlighter-rouge\">&lt;button&gt;<\/code>, it\u2019s less likely password managers will be able\nto find it. Using HTML correctly is always a good idea. If you use a component\nlibrary, check what it generates for buttons and if it\u2019s not <code class=\"language-plaintext highlighter-rouge\">&lt;div&gt;<\/code>\u2019s all the\nway down.<\/p>\n\n<h2 id=\"password-manager-developers\">Password manager developers:<\/h2>\n\n<p>Please also try to follow these recommendations! For better or worse, a lot of\napplications are going to this multi-step flow. <a href=\"https:\/\/github.com\/keepassxreboot\/keepassxc-browser\">KeepassXC-Browser<\/a> in\nparticular doesn\u2019t handle these cases well, unless you\u2019re one of\nof their [blessed sites][<a href=\"https:\/\/github.com\/keepassxreboot\/keepassxc-browser\/blob\/b186eb1fd286bdc9515bb59f1e5c7e5ec568b41c\/keepassxc-browser\/common\/sites.js#L3\">11<\/a> (<a href=\"https:\/\/github.com\/keepassxreboot\/keepassxc-browser\/issues\/2436\">feature request<\/a>).<\/p>\n\n<h2 id=\"references-and-further-reading\">References and further reading:<\/h2>\n\n<ul>\n  <li><a href=\"https:\/\/wicg.github.io\/sms-one-time-codes\/\">Origin-bound one-time codes delivered via SMS<\/a><\/li>\n  <li><a href=\"https:\/\/developer.1password.com\/docs\/web\/compatible-website-design\/\">Design your website to work best with 1Password<\/a><\/li>\n  <li><a href=\"https:\/\/www.chromium.org\/developers\/design-documents\/form-styles-that-chromium-understands\/\">Password Form Styles that Chromium Understands<\/a><\/li>\n  <li><a href=\"https:\/\/www.chromium.org\/developers\/design-documents\/create-amazing-password-forms\/\">Create Amazing Password Forms (chromium.org)<\/a><\/li>\n  <li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Reference\/Elements\/input\/password\"><code class=\"language-plaintext highlighter-rouge\">&lt;input type=\"password\"&gt;<\/code> (MDN)<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/keepassxreboot\/keepassxc-browser\/issues\/2436\">KeeppassXC-browser rejected feature request to better support multi-step login forms<\/a><\/li>\n<\/ul>\n\n<h2 id=\"thoughts\">Thoughts?<\/h2>\n\n<p>If you have further workarounds to make password managers behave well, you\ncan reply to these posts on Mastodon or Bluesky to automatically appear on this\nwebsite as a comment:<\/p>\n\n<ul>\n  <li>Mastodon: <a href=\"https:\/\/indieweb.social\/@evert\/114330222551324994\">https:\/\/indieweb.social\/@evert\/114330222551324994<\/a><\/li>\n  <li>Bluesky: <a href=\"https:\/\/bsky.app\/profile\/evertpot.com\/post\/3lmouioj5hh2h\">https:\/\/bsky.app\/profile\/evertpot.com\/post\/3lmouioj5hh2h<\/a><\/li>\n<\/ul>\n\n"},{"id":"http:\/\/evertpot.com\/letting-curveball-die\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/letting-curveball-die\/"}},"title":"Putting Curveball in maintenance mode","updated":"2024-12-29T15:00:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p><a href=\"https:\/\/curveballjs.org\/\">Curveball<\/a> is a server-side webframework for Node.js. I\u2019ve been working on\nit for 6 years as a side-project, but it wasn\u2019t successful and it\u2019s time to let\nit go. This is a hard thing to do, because I probably have 1000\u2019s of hours into\nthis project, and there\u2019s a strong sense of missed potential.<\/p>\n\n<p>I\u2019m writing this article because my psyche requires closure in order for them to\nnot take up space in my mind. This decision was probably a few years late.\nHopefully this creates more space for new things.<\/p>\n\n<h2 id=\"some-quick-numbers\">Some quick numbers.<\/h2>\n\n<ul>\n  <li>Number of <a href=\"https:\/\/www.npmjs.com\/search?q=%40curveball\">NPM packages<\/a>: <strong>25<\/strong><\/li>\n  <li>Number of released packages: <strong>377<\/strong><\/li>\n  <li>Total number of commits: <strong>4882<\/strong><\/li>\n  <li>Contributers: <strong>32<\/strong><\/li>\n  <li>Total downloads from NPM: <strong>1,254,023<\/strong><\/li>\n  <li>Active users: <strong>10?<\/strong><\/li>\n<\/ul>\n\n<h2 id=\"why-build-another-framework\">Why build another framework<\/h2>\n\n<p>In 2018, Typescript just really started popping off and at the time I was\ndeeply interested in optimizing hypermedia-centric REST APIs, <a href=\"https:\/\/koajs.com\/\">Koa<\/a> was\na favourite framework of mine, but it was missing some features I was\nparticularly interested in.<\/p>\n\n<ul>\n  <li>Good Typescript support (This is pretty much fixed in Koa, but at the time\nit was buggy).<\/li>\n  <li>The ability to do internal \u201csub-requests\u201d without going over the network.\n(think: hitting a different route in your framework with a local function).\nKoa (and express) wrap the <code class=\"language-plaintext highlighter-rouge\">http<\/code> Node.js library, and don\u2019t have a good\ninternal abstraction for requests and responses. This makes sub-requests\nhard and deploying into non-node runtimes such as AWS Lambdas very painful\nand requires mocking the whole Node <code class=\"language-plaintext highlighter-rouge\">http<\/code> library or worse: run full HTTP\nserver inside the lambda function just so requests can be forwarded.\nNew frameworks like <a href=\"https:\/\/hono.dev\/\">Hono<\/a> do this perfectly these days, but this wasn\u2019t\naround at the time.<\/li>\n  <li>Native <a href=\"https:\/\/evertpot.com\/jsx-template\/\">\u2018JSX as a template engine\u2019<\/a> support. <a href=\"https:\/\/hono.dev\/docs\/guides\/jsx\">Hono<\/a> also does this.<\/li>\n  <li>Deeply integrated Websockets. Probably <a href=\"https:\/\/bun.sh\/docs\/api\/websockets\">Bun<\/a> does this best at the\nmoment.<\/li>\n  <li>Support HTTP\/2. A big motivator for me was being able to push resources\ndown the pipe with push, and <a href=\"https:\/\/evertpot.com\/h2-parallelism\/\">solve the N+1 problem for REST<\/a>. <a href=\"https:\/\/www.npmjs.com\/package\/@curveball\/http-errors\">Push is\npretty much dead<\/a> now, so less of a priority.<\/li>\n  <li>Good support for <code class=\"language-plaintext highlighter-rouge\">103 Early Hints<\/code>. Not seeing a lot love for this in the\nnew batch of frameworks.<\/li>\n<\/ul>\n\n<h2 id=\"why-did-curveball-fail\">Why did Curveball fail<\/h2>\n\n<p>If I\u2019m being generous with myself, I think the tech was decent and generally\nliked by those using it. It was fairly sticky, but it never really reached\nanything close to critical mass.<\/p>\n\n<p>Documentation was bad, and crucially marketing was pretty much non-existant.\nOver the entire 6 years it was mostly me working on it (with some great smaller\ncontributions!), but I have a very hard time with marketing and speaking about\nthe project with some consistency. The last time I blogged about a release was\nin 2020. It never had a chance.<\/p>\n\n<p>I made the same mistake I made many times before, thinking \u2018If I build it, they\nwill come\u2019 and I guess I had a hope it would attract some early strong\ncontributors that could help pick up the slack in areas where I was weak, but\nobviously this never happened.<\/p>\n\n<p>I also don\u2019t see how I could have gotten the time and motivation to promote\nthe project. I find self-promotion somewhat painful to do and Curveball was\nalways at best <em>just<\/em> incrementally better than what was out there when I\nstarted it, so not a lot of people are going to take the chance on something\nwith low user numbers, even if they were able to find it.<\/p>\n\n<p>I\u2019m still interested in Open Source and love building things, but probably the\nbiggest lesson here is that if I want it to be successful I need to do group\nprojects with people who make up for the qualities I lack.<\/p>\n\n<h2 id=\"im-using-curveball-for-my-project-now-what\">I\u2019m using Curveball for my project, now what?<\/h2>\n\n<p>The project has been very stable and only had 1 major breaking change, which\nwas the move to ESM.<\/p>\n\n<p>Express has gotten by without a major release in over a decade, so if you\u2019re\nusing Curveball today I think there\u2019s very little chance you need to do\nanything in a long while. I will even fix bugs as they are reported, but this\nblog post is effectively my statement that I\u2019m no longer going to put much\nenergy in it. Curveball is stable. Don\u2019t use it for new projects.<\/p>\n\n<p>If you need advice on a migration path, I\u2019m happy to do a free 30 minute\nconsultation! You probably know how to reach me.<\/p>\n\n<h2 id=\"if-you-like-the-project-why-cant-you-work-on-it-despite-not-having-many-users\">If you like the project, why can\u2019t you work on it despite not having many users<\/h2>\n\n<p>I get a lot of energy from working with people, getting feedback and seeing\nmy ideas validated. If this is not happening, to me it feels a bit like writing\na book that nobody reads. It\u2019s a constant reminder of failure.<\/p>\n\n<h2 id=\"are-there-any-bits-salvagable\">Are there any bits salvagable?<\/h2>\n\n<ul>\n  <li>I\u2019m going to rename <a href=\"https:\/\/www.npmjs.com\/package\/@curveball\/http-errors\">@curveball\/http-errors<\/a> because it sucks less than\nsome other HTTP error packages, it\u2019s not tied to the framework and is\ngenerally useful.<\/li>\n  <li>I think the built-in <a href=\"https:\/\/github.com\/curveball\/browser\/\">HATEOAS API browser<\/a> is great and I\u2019m going to\nport it to a new framework. (most likely Hono as it\u2019s the closest in spirit).<\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/a12n-server\">a12n-server<\/a>, which is an authn\/authz server similar to keycloak is\nstill very interesting to me. It needs its own brand and logo and will port\nthis project to a different framework at some future date. Despite being\nunder the \u2018curveball\u2019 namespace I always saw this as its own project. This\nproject does suffer from a similar problem though. Low uptake and just me\nmaintaining it. I need to set some goals for 2025 and kill this as well if\nI don\u2019t meet them.<\/li>\n<\/ul>\n\n<h2 id=\"thank-you\">Thank you<\/h2>\n\n<p>If you\u2019re a current user of Curveball: thank you for giving this a shot! I\nhope this news is not too frustrating.<\/p>\n\n<p>To my friends that gave me encouragement and feedback over the years: I love\nyou and appreciate you!<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/using-top-level-await-is-bc-break\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/using-top-level-await-is-bc-break\/"}},"title":"In the future using top-level await might be cause a backwards compatibility break in Node","updated":"2024-10-18T01:13:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p><a href=\"https:\/\/nodejs.org\/en\/blog\/release\/v23.0.0\">Node 23<\/a> was released this week, and the hot ticket item probably is the\nfact that you can now <code class=\"language-plaintext highlighter-rouge\">require()<\/code> files that use ESM (<code class=\"language-plaintext highlighter-rouge\">import<\/code>\/<code class=\"language-plaintext highlighter-rouge\">export<\/code>).<\/p>\n\n<p>This is helpful because ESM and CommonJS (<code class=\"language-plaintext highlighter-rouge\">require<\/code>\/<code class=\"language-plaintext highlighter-rouge\">module.exports<\/code>) are kind\nof different worlds and before this change if you wanted to use a \u201cmodule\u201d\nfrom your CommonJS file, you would need to do something like:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">theThing<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"k\">import<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">some\/module\/file.mjs<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is called a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/import\">dynamic import<\/a> and it\u2019s extra annoying, because you\ncan\u2019t just put this at the top of your file. It returns a Promise, so you\ncan only import these modules \u2018asynchronously\u2019 or in functions. The reason\nis that you can only <code class=\"language-plaintext highlighter-rouge\">await<\/code> inside functions in CommonJS files. So this\nsyntax prevents importing a \u201cmodule\u201d and immediately using it. To use a\nmodule in CommonJS, you\u2019ll need some kind of initialization logic.<\/p>\n\n<p>Many Javascript packages resort to <a href=\"https:\/\/evertpot.com\/universal-commonjs-esm-typescript-packages\/\">shipping both a \u2018CommonJS\u2019 and a \u2018ESM\u2019\nversion of their code<\/a> to reduce this kind of annoyance, but not everyone\ndoes.<\/p>\n\n<h2 id=\"the-node-23-change\">The Node 23 change<\/h2>\n\n<p>Node 23 makes it possible to load ESM modules transparently via <code class=\"language-plaintext highlighter-rouge\">require()<\/code>.\nThis means that the above example can be rewritten to:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">theThing<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">require<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">some\/module\/file.mjs<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The important part here is not the usage of <code class=\"language-plaintext highlighter-rouge\">require<\/code>, but the absense of\n<code class=\"language-plaintext highlighter-rouge\">await<\/code>. This allows ESM modules to be loaded without this kind of\ninitialization logic.<\/p>\n\n<p>But there\u2019s a big caveat:<\/p>\n\n<p>This <em>only<\/em> works if the module you loaded in is not using top-level await.\n\u201cTop level await\u201d means awaiting things outside of (async) functions, which\nis possible in modules (but not CommonJS).<\/p>\n\n<p>If a top-level await <em>was<\/em> used, a <code class=\"language-plaintext highlighter-rouge\">ERR_REQUIRE_ASYNC_MODULE<\/code> error will be\nthrown. But the important thing to note is that this doesn\u2019t just apply to\nthe file you are directly importing\/requiring. It also applies to any files\nloaded by that file, at any level in either your project or any dependency or\nsub-dependencies.<\/p>\n\n<h2 id=\"using-top-level-await-is-now-a-bc-break\">Using top-level await is now a BC break<\/h2>\n\n<p>Typically when we think of backwards compatibility breaks that require a major\nnew version in semver, you might think of functions changing, or removing or\narguments no longer being supported.<\/p>\n\n<p>Before this change in Node, if your project was fully switched to ESM you might\nnot think of placing a top-level <code class=\"language-plaintext highlighter-rouge\">await<\/code> anywhere in your code as a backwards\ncompatibility break, but if it\u2019s the first <code class=\"language-plaintext highlighter-rouge\">await<\/code> you might now inadvertently\nbreak Node.js users, if they used <code class=\"language-plaintext highlighter-rouge\">require()<\/code> to bring in your module.<\/p>\n\n<p>This means that the first top-level <code class=\"language-plaintext highlighter-rouge\">await<\/code> in your project (or any of your\ndependencies) might now constitute a new major version if you follow <a href=\"https:\/\/semver.org\/\">semver<\/a>.<\/p>\n\n<p>If you don\u2019t want to do this, here are some other things you could do:<\/p>\n\n<h3 id=\"1-tell-your-users-you-dont-support-require\">1. Tell your users you don\u2019t support require()<\/h3>\n\n<p>You could effectively tell them if they use <code class=\"language-plaintext highlighter-rouge\">require()<\/code> they\u2019re on their own.\nChances are that your users won\u2019t read this and do it anyway, but arguably they\nshould read the readme of a package they bring in.<\/p>\n\n<h3 id=\"2-add-a-dummy-await\">2. Add a dummy await<\/h3>\n\n<p>Do you think you or your dependencies might use a top-level await in the\nfuture, but you don\u2019t yet? You could add this line to your source to reserve\nthe right to do it later:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">await<\/span> <span class=\"dl\">\"<\/span><span class=\"s2\">Good things come to those that support await<\/span><span class=\"dl\">\"<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"3-explictly-break-commonjs\">3. Explictly break CommonJS<\/h3>\n\n<p>Packages can provide both CommonJS and ESM support via the <a href=\"https:\/\/nodejs.org\/api\/packages.html#exports\"><code class=\"language-plaintext highlighter-rouge\">exports<\/code><\/a> key\nin <code class=\"language-plaintext highlighter-rouge\">package.json<\/code>. It could be an option to export a single CommonJS file that\njust throws an error to warn CommonJS users they shouldn\u2019t use this package.<\/p>\n\n<p>Out of these 3 options, I kind of like 2 because if Node.js ever changes\nbehavior and <em>does<\/em> support loading modules that use top-level await in\nCommonJS files it will one day just start working.<\/p>\n\n<p>Regardless. it does feel important to me that package maintainers that only\nship ESM, or ship CommonJS and intend to use this new feature have a\nstrategy, because it\u2019s bound to blow up if not.<\/p>\n\n<h2 id=\"how-do-other-runtimes-do-it\">How do other runtimes do it?<\/h2>\n\n<p>In Bun you can use <a href=\"https:\/\/docs.deno.com\/runtime\/tutorials\/cjs_to_esm\/\"><code class=\"language-plaintext highlighter-rouge\">import<\/code> and <code class=\"language-plaintext highlighter-rouge\">require<\/code><\/a> in the same file, so there\u2019s\nfewer trade-offs to make. But if you do use <code class=\"language-plaintext highlighter-rouge\">require()<\/code> to import a module\nwith top-level await it still has the same issue. There\u2019s just fewer reasons\nin Bun to even want to do this.<\/p>\n\n<p>Deno <a href=\"https:\/\/docs.deno.com\/runtime\/tutorials\/cjs_to_esm\/\">doesn\u2019t support CommonJS<\/a> so it doesn\u2019t have that problem.<\/p>\n\n<h2 id=\"does-the-issue-exists-when-importing-commonjs-files-into-modules\">Does the issue exists when importing CommonJS files into modules?<\/h2>\n\n<p>Importing CommonJS files into modules was always possible via the standard\n<code class=\"language-plaintext highlighter-rouge\">import<\/code> syntax, and because CommonJS can\u2019t asynchronously export this\nproblem doesn\u2019t exist there.<\/p>\n\n<h2 id=\"what-should-i-do-as-a-user\">What should I do as a user?<\/h2>\n\n<p>Most of this article was about the effects for package maintainers, but not every\npackage will do this well so as a user you\u2019re still potentially exposed to this\nissue.<\/p>\n\n<p>In order of preference, consider the following:<\/p>\n\n<ol>\n  <li>Stop using CommonJS. Start using ESM everywhere. This issue only exists in\nCommonJS files using ESM, not the reverse. ESM is the future. You\u2019re working\nwith dead-end technology.<\/li>\n  <li>Have some testing in place that at the very least loads the entirely of your\ncode-base. If all (top-level, not-dynamic) <code class=\"language-plaintext highlighter-rouge\">requires()<\/code> are ran\/resolved,\nany changes in depndencies should just blow up your CI environment and\napplication.<\/li>\n  <li>Find out if any of your dependencies are ESM-only, and never <code class=\"language-plaintext highlighter-rouge\">require()<\/code>\nthem. This protects you from issues with direct dependencies. But keep\nin mind that this can still be an issue in sub-dependencies even if your\ndirect dependencies are CommonJS themselves!<\/li>\n<\/ol>\n\n<h2 id=\"my-opinion\">My opinion<\/h2>\n\n<p>For the above reasons I don\u2019t think this should land in a stable Node.js\nversion. The intentions are good but it creates yet another avenue of confusion.<\/p>\n\n<p>It effectively expands the number of Javascript flavors from 2 to 3:<\/p>\n\n<ol>\n  <li>CommonJS<\/li>\n  <li>ESM<\/li>\n  <li>ESM but without top-level await.<\/li>\n<\/ol>\n\n<p>If a module uses flavor #3, it\u2019s compatible with flavor #1, but if anywhere in\nthe dependency tree something starts using top-level await, suddenly the entire\ntree ESM dependency tree cascades from flavor #3 to #2 and breaks compatibilty\nwith flavor #1.<\/p>\n\n<p>Given the already confusing landscape (and reputation) of Javascript modules,\nthis feels like a step in the wrong direction, as it adds a feature to\nCommonJS (which in my opinion is time to freeze), and makes ESM <em>less<\/em>\nreliable as a result.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/discovering-features-with-http-options\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/discovering-features-with-http-options\/"}},"title":"Discovering features using HTTP OPTIONS","updated":"2024-10-16T13:44:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>Say you have an API, and you want to communicate what sort of things a user can\ndo on a specific endpoint. You can use external description formats like OpenAPI\nor JSON Schema, but sometimes it\u2019s nice to also dynamically communicate this on\nthe API itself.<\/p>\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Methods\/OPTIONS\"><code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code><\/a> is the method used for that. You may know this HTTP method from\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS\">CORS<\/a>, but it\u2019s general purpose is for clients to passively find out \u2018What\ncan I do here?\u2019.<\/p>\n\n<p>All HTTP clients typically support making <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> request. For example with\n<code class=\"language-plaintext highlighter-rouge\">fetch()<\/code>:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">response<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">https:\/\/example.org<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">{<\/span><span class=\"na\">method<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">OPTIONS<\/span><span class=\"dl\">'<\/span><span class=\"p\">}<\/span>\n<span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>A basic <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> response might might look like this:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span> <span class=\"m\">204<\/span> <span class=\"ne\">No Content<\/span>\n<span class=\"na\">Date<\/span><span class=\"p\">:<\/span> <span class=\"s\">Mon, 23 Sep 2024 02:57:38 GMT<\/span>\n<span class=\"na\">Server<\/span><span class=\"p\">:<\/span> <span class=\"s\">KKachel\/1.2<\/span>\n<span class=\"na\">Allow<\/span><span class=\"p\">:<\/span> <span class=\"s\">GET, PUT, POST, DELETE, OPTIONS<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Based on the <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9110.html#field.allow\" title=\"Allow header\"><code class=\"language-plaintext highlighter-rouge\">Allow<\/code><\/a> header you can quickly tell which HTTP methods\nare available at a given endpoint. Many web frameworks emit this automatically\nand generate the list of methods dynamically per route, so chances are that you\nget this one for free.<\/p>\n\n<p>To find out if your server does, try running the command below (with your\nURL!):<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>curl -X OPTIONS http:\/\/localhost:3000\/some\/endpoint\/\n<\/code><\/pre><\/div><\/div>\n\n<p>One nice thing you could do with the <code class=\"language-plaintext highlighter-rouge\">Allow<\/code> header, is that you could also\ncommunicate access-control information on a very basic level. For example,\nyou could only include <code class=\"language-plaintext highlighter-rouge\">DELETE<\/code> and <code class=\"language-plaintext highlighter-rouge\">PUT<\/code> if a user has write access to\na resource.<\/p>\n\n<h2 id=\"accept-and-accept-encoding\">Accept and Accept-Encoding<\/h2>\n\n<p>There\u2019s server other standard headers for discovery. Here\u2019s an example showing\na few at once:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span> <span class=\"m\">204<\/span> <span class=\"ne\">No Content<\/span>\n<span class=\"na\">Date<\/span><span class=\"p\">:<\/span> <span class=\"s\">Mon, 23 Sep 2024 02:57:38 GMT<\/span>\n<span class=\"na\">Server<\/span><span class=\"p\">:<\/span> <span class=\"s\">KKachel\/1.2<\/span>\n<span class=\"na\">Allow<\/span><span class=\"p\">:<\/span> <span class=\"s\">GET, PUT, POST, DELETE, OPTIONS<\/span>\n<span class=\"na\">Accept<\/span><span class=\"p\">:<\/span> <span class=\"s\">application\/vnd.my-company-api+json, application\/json, text\/html<\/span>\n<span class=\"na\">Accept-Encoding<\/span><span class=\"p\">:<\/span> <span class=\"s\">gzip,brotli,identity<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>You may already be familiar with <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9110.html#name-accept\" title=\"Accept header\"><code class=\"language-plaintext highlighter-rouge\">Accept<\/code><\/a> and <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9110.html#name-accept-encoding\" title=\"Accept-Encoding header\"><code class=\"language-plaintext highlighter-rouge\">Accept-Encoding<\/code><\/a> from\nHTTP requests, but they can also appear in responses. <code class=\"language-plaintext highlighter-rouge\">Accept<\/code> in a response\nlets you tell the client which kind of mimetypes are available at an endpoint.\nI like adding <code class=\"language-plaintext highlighter-rouge\">text\/html<\/code> to every JSON api endpoint and making sure that\nAPI urls can be opened in browsers and shared between devs for easy debugging.<\/p>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">Accept-Encoding<\/code> lets a client know in this case that they can compress\ntheir request bodies with either <code class=\"language-plaintext highlighter-rouge\">gzip<\/code> or <code class=\"language-plaintext highlighter-rouge\">brotli<\/code> (<code class=\"language-plaintext highlighter-rouge\">identity<\/code> means no\ncompression).<\/p>\n\n<h2 id=\"patching-posting-and-querying\">Patching, posting and querying<\/h2>\n\n<p>3 other headers that can be used are <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc5789#section-3.1\" title=\"Accept-Patch header\"><code class=\"language-plaintext highlighter-rouge\">Accept-Patch<\/code><\/a>, <a href=\"https:\/\/www.w3.org\/TR\/ldp\/#header-accept-post\" title=\"Accept-Post header\"><code class=\"language-plaintext highlighter-rouge\">Accept-Post<\/code><\/a>\nand <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/draft-ietf-httpbis-safe-method-w-body-05#name-the-accept-query-header-fie\" title=\"Accept-Query header\"><code class=\"language-plaintext highlighter-rouge\">Accept-Query<\/code><\/a>. These three headers are used to tell a client what\ncontent-types are available for the <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc5789#section-2\" title=\"PATCH method\"><code class=\"language-plaintext highlighter-rouge\">PATCH<\/code><\/a>, <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9110.html#name-post\" title=\"POST method\"><code class=\"language-plaintext highlighter-rouge\">POST<\/code><\/a> and\n<a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/draft-ietf-httpbis-safe-method-w-body-05#name-query\" title=\"QUERY method\"><code class=\"language-plaintext highlighter-rouge\">QUERY<\/code><\/a> http methods respectively.<\/p>\n\n<p>For all of these headers, their values effectively dictate what valid\nvalues are for the <code class=\"language-plaintext highlighter-rouge\">Content-Type<\/code> header when making the request.<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span> <span class=\"m\">204<\/span> <span class=\"ne\">No Content<\/span>\n<span class=\"na\">Date<\/span><span class=\"p\">:<\/span> <span class=\"s\">Mon, 23 Sep 2024 02:57:38 GMT<\/span>\n<span class=\"na\">Server<\/span><span class=\"p\">:<\/span> <span class=\"s\">KKachel\/1.2<\/span>\n<span class=\"na\">Allow<\/span><span class=\"p\">:<\/span> <span class=\"s\">OPTIONS, QUERY, POST, PATCH<\/span>\n<span class=\"na\">Accept-Patch<\/span><span class=\"p\">:<\/span> <span class=\"s\">application\/json-patch+json, application\/merge-patch+json<\/span>\n<span class=\"na\">Accept-Query<\/span><span class=\"p\">:<\/span> <span class=\"s\">application\/graphql<\/span>\n<span class=\"na\">Accept-Post<\/span><span class=\"p\">:<\/span> <span class=\"s\">multipart\/form-data, application\/vnd.custom.rpc+json<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>In the above response, the server indicates it supports both <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc6902\" title=\"JSON Patch\">JSON Patch<\/a>\nand <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7386\" title=\"JSON Merge Patch\">JSON Merge Patch<\/a> content-types in <code class=\"language-plaintext highlighter-rouge\">PATCH<\/code> requests. It also suggests\nthat GraphQL can be used via the <code class=\"language-plaintext highlighter-rouge\">QUERY<\/code> method, and for <code class=\"language-plaintext highlighter-rouge\">POST<\/code> it supports\nboth standard file uploads and some custom JSON-based format.<\/p>\n\n<p>Typically you wouldn\u2019t find all of these at the same endpoint, but I wanted\nto show a few examples together.<\/p>\n\n<h2 id=\"wheres-put\">Where\u2019s PUT?<\/h2>\n\n<p>Oddly, there\u2019s no specific header for <code class=\"language-plaintext highlighter-rouge\">PUT<\/code> requests. Arguably you could say\nthat <code class=\"language-plaintext highlighter-rouge\">GET<\/code> and <code class=\"language-plaintext highlighter-rouge\">PUT<\/code> are symmetrical, so perhaps the <code class=\"language-plaintext highlighter-rouge\">Accept<\/code> header kind of\nextends to both. But the spec is not clear on this.<\/p>\n\n<p>I think the actual reality is that <code class=\"language-plaintext highlighter-rouge\">Accept-Patch<\/code> was the first header in\nthis category that really clearly defined this as a means of feature discovery\non <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code>. <code class=\"language-plaintext highlighter-rouge\">Accept-Post<\/code> and <code class=\"language-plaintext highlighter-rouge\">Accept-Query<\/code> followed suit. I think\n<code class=\"language-plaintext highlighter-rouge\">Accept-Patch<\/code> in <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> was modelled after in-the-wild usage of <code class=\"language-plaintext highlighter-rouge\">Accept<\/code>\nin <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code>, even though the HTTP specific doesn\u2019t super clearly define this.<\/p>\n\n<p>If I\u2019m wrong with my interpretation here, I would love to know!<\/p>\n\n<p><small><em>\nAside: If you\u2019re wondering about <code class=\"language-plaintext highlighter-rouge\">DELETE<\/code>, <code class=\"language-plaintext highlighter-rouge\">DELETE<\/code> should never have a body,\nso all a user would need to know is <em>can<\/em> they delete, which you can see\nin the <code class=\"language-plaintext highlighter-rouge\">Allow<\/code> header.\nIf this is new to you to, <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc8631.html#section-4.2\" title=\"service-desc link relationship\">read my other article<\/a> about <code class=\"language-plaintext highlighter-rouge\">GET<\/code> request\nbodies. Most of the information there is applicable to <code class=\"language-plaintext highlighter-rouge\">DELETE<\/code> as well.\n<\/em><\/small><\/p>\n\n<h2 id=\"linking-to-documentation\">Linking to documentation<\/h2>\n\n<p>The <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> response is also a great place to tell users where to find\nadditional documentation. In the below example, I included both a\nmachine-readable link to a documentation site, a link to an OpenAPI definition,\nand a message intended for humans in the response body:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span> <span class=\"m\">200<\/span> <span class=\"ne\">OK<\/span>\n<span class=\"na\">Date<\/span><span class=\"p\">:<\/span> <span class=\"s\">Mon, 23 Sep 2024 04:45:38 GMT<\/span>\n<span class=\"na\">Allow<\/span><span class=\"p\">:<\/span> <span class=\"s\">GET, QUERY, OPTIONS<\/span>\n<span class=\"na\">Link<\/span><span class=\"p\">:<\/span> <span class=\"s\">&lt;https:\/\/docs.example.org\/api\/some-endpoint&gt;; rel=\"service-doc\"<\/span>\n<span class=\"na\">Link<\/span><span class=\"p\">:<\/span> <span class=\"s\">&lt;https:\/\/api.example.org\/openapi.yml&gt;; rel=\"service-desc\" type=\"application\/openapi+yaml\"<\/span>\n<span class=\"na\">Content-Type<\/span><span class=\"p\">:<\/span> <span class=\"s\">text\/plain<\/span>\n\nHey there!\n\nThanks for checking out this API. You can find the docs for this\nspecific endpoint at: https:\/\/docs.example.org\/api\/some-endpoint\n\nCheers,\nThe dev team\n<\/code><\/pre><\/div><\/div>\n\n<p>I recommend keeping the response body as mostly informal and minimal\nany real information should probably just live on its own URL and be linked to.<\/p>\n\n<p>I used the <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc8631.html#section-4.1\" title=\"service-doc link relationship\"><code class=\"language-plaintext highlighter-rouge\">service-doc<\/code><\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\" title=\"Mime types\"><code class=\"language-plaintext highlighter-rouge\">service-desc<\/code><\/a> link relationships here,\nbut you can of course use any of the <a href=\"https:\/\/www.iana.org\/assignments\/link-relations\/link-relations.xhtml\" title=\"IANA link relations registry\">IANA link relationship types<\/a> here\nor a custom one. Also see the <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc8288\" title=\"Web Linking\">Web linking<\/a> spec for more info.<\/p>\n\n<h2 id=\"obscure-uses\">Obscure uses<\/h2>\n\n<h3 id=\"webdav-usage\">WebDAV usage<\/h3>\n\n<p>WebDAV, CalDAV and CardDAV also use OPTIONS for feature discovery. For example:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span> <span class=\"m\">204<\/span> <span class=\"ne\">No Content<\/span>\n<span class=\"na\">Date<\/span><span class=\"p\">:<\/span> <span class=\"s\">Mon, 23 Sep 2024 05:01:50 GMT<\/span>\n<span class=\"na\">Allow<\/span><span class=\"p\">:<\/span> <span class=\"s\">GET, PROPFIND, ACL, PROPPATCH, MKCOL, LOCK, UNLOCK<\/span>\n<span class=\"na\">DAV<\/span><span class=\"p\">:<\/span> <span class=\"s\">1, 2, 3, access-control, addressbook, calendar-access<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"the-server-wide-asterisk-request\">The server-wide asterisk request<\/h3>\n\n<p>Normally HTTP requests are made to a path on the server, and the first line\nlooks a bit like the following in HTTP\/1.1:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nf\">GET<\/span> <span class=\"nn\">\/path<\/span> <span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>But, there are a few other \u201crequest line\u201d formats that are rarely used. One of\nthem lets you discover features available on an entire server, using the\nasterisk:<\/p>\n\n<div class=\"language-http highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nf\">OPTIONS<\/span> <span class=\"nn\">*<\/span> <span class=\"k\">HTTP<\/span><span class=\"o\">\/<\/span><span class=\"m\">1.1<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The asterisk here is not a path. Normally asterisks aren\u2019t even allowed in\nURIs. Many HTTP clients (including <code class=\"language-plaintext highlighter-rouge\">fetch()<\/code>) don\u2019t even support this request.<\/p>\n\n<p>Classic webservers like Apache and Nginx should support this. To try it out,\nuse CURL<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>curl <span class=\"nt\">-vX<\/span> OPTIONS <span class=\"nt\">--request-target<\/span> <span class=\"s1\">'*'<\/span> http:\/\/example.org\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"final-notes\">Final notes<\/h2>\n\n<p>If you have a reason to allow clients to discover features on an endpoint,\nconsider using <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> instead of a proprietary approach! As you can\nsee in many of these examples, it\u2019s especially useful if you use <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Basics_of_HTTP\/MIME_types\" title=\"Mime types\">mimetypes<\/a>\nwell.<\/p>\n\n<p>If you have questions, other novel uses of <code class=\"language-plaintext highlighter-rouge\">OPTIONS<\/code> or other ideas around\nfeature discovery, you can respond via:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/indieweb.social\/@evert\/113317676213353403\">This post on Mastodon<\/a><\/li>\n  <li><a href=\"https:\/\/bsky.app\/profile\/evertpot.com\/post\/3l6n7363zii23\">This post on Bluesky<\/a><\/li>\n  <li>Via the <a href=\"https:\/\/www.w3.org\/TR\/webmention\/\">Webmention<\/a> protocol!<\/li>\n<\/ul>\n\n"},{"id":"http:\/\/evertpot.com\/structured-fields-javascript-v2\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/structured-fields-javascript-v2\/"}},"title":"New Structured Fields RFC out, and so is my Javascript package","updated":"2024-10-03T15:56:31+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>A new RFC was released for Structured Fields: <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc9651.html\" title=\"Structured Field Values for HTTP\">RFC9651<\/a>.<\/p>\n\n<h2 id=\"what-is-it\">What is it?<\/h2>\n\n<p>HTTP headers have been a bit of a free-for all in terms of how complex values\nare encoded, with many headers requiring their own mini-parser.<\/p>\n\n<p>A while back an effort was started to fix this for headers going forward,\nnamed \u2018Structured Fields\u2019. They\u2019re called Fields and not \u2018Headers\u2019 because\nHTTP has both Headers and Trailers!<\/p>\n\n<p>Structured fields let you encode things like lists, dictionaries, strings,\nnumbers, booleans and binary data. The <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc8941.html\" title=\"Structured Field Values for HTTP old spec\">original RFC<\/a> from 2021 is\npretty successful and although many existing headers can\u2019t be retrofitted\nto this format, a lot of new standards are taking advantage.<\/p>\n\n<p>Some examples:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code># Parsed an ASCII string\nHeader: \"foo\"\n\n# A simple string, called a 'Token' in the spec\nHeader: foo\n\n# Parsed as number\nHeader: 5\nHeader: -10\nHeader: 5.01415\n\n# Parsed into boolean\nHeader: ?1\nHeader: ?0\n\n# Binaries are base64 encoded\nHeader: :RE0gbWUgZm9yIGEgZnJlZSBjb29raWU=:\n\n# Items can have parameters\nHeader: \"Hello world\"; a=\"5\"\n\n# A simple list\nHeader: 5, \"foo\", bar, ?1\n\n# Each element can have parameters\nHeader: sometoken; param1; param2=hi, 42\n\n# A list can also contain lists itself. These are called 'inner lists' and\n# use parenthesis\nHeader: sometoken, (innerlistitem1 innerlistitem2), (anotherlist)\n\n# A simple dictionary\nHeader: fn=\"evert\", ln=\"pot\", coffee=?1\n\n# Each item may have parameters too\nHeader: foo=123; q=1, bar=123, q=0.5\n\n# A dictionary value may be an inner list again\nHeader: foo=(1 2 3)\n<\/code><\/pre><\/div><\/div>\n\n<p>The new RFC published last week adds 2 new data types: Dates and\n\u2018Display strings\u2019, which is a Unicode serialization that fits in the HTTP\nheader (and trailer) format.<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code># Parsed into a Date object\nHeader: @1686634251\n\n# A Unicode string, called a 'Display String' in the spec. They use\n# percent encoding, but encode a different set of characters than\n# URLs.\nHeader %\"Frysl%C3%A2n\"\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"why-should-you-care\">Why should you care?<\/h2>\n\n<p>If you encounter these headers in the wild, it\u2019s a really good idea to use\na standard parser. One of the reasons is that with using structured-fields,\nthere\u2019s a built-in extension mechanism. You\u2019ll want to make sure that when\na new parameter appears your application doesn\u2019t suddenly break.<\/p>\n\n<p>You may also want to define and use your own HTTP headers. The structured\nfields format is a very good \u2018default choice\u2019 that removes decisions such\nas \u2018How should I encode a key value object\u2019 or \u2018how do I encode a UTF-8\nstring\u2019.<\/p>\n\n<p>With parsers popping up for every language, you don\u2019t have to worry about\nwriting your own one-off formats.<\/p>\n\n<h2 id=\"javascript-package\">Javascript package<\/h2>\n\n<p>I\u2019m the maintainer of a Javascript library for Structured Fields, called\n<a href=\"https:\/\/github.com\/badgateway\/structured-headers\" title=\"Structured Fields parser\/serializer for Javascript and Typescript\">\u201cstructured headers\u201d<\/a>, which I\u2019ve also updated for this new RFC. I wish\nI picked the name \u201cstructured-fields\u201d, but I picked the name before the\noriginal standard changed it\u2019s name.<\/p>\n\n<p>I\u2019ve just released v2 of this library supporting these new types, and also\nadded ES Modules support.<\/p>\n\n<h2 id=\"comments\">Comments?<\/h2>\n\n<p>Reply to one of these:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/indieweb.social\/@evert\/113247162498865971\" title=\"Mastodon post\">Mastodon post<\/a><\/li>\n  <li><a href=\"https:\/\/bsky.app\/profile\/evertp.bsky.social\/post\/3l5nvxuyfv32m\" title=\"Bluesky post\">Bluesky post<\/a><\/li>\n<\/ul>\n\n"},{"id":"http:\/\/evertpot.com\/hello-world-kian\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/hello-world-kian\/"}},"title":"Hello World, meet Kian","updated":"2024-10-02T15:08:31+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>One week ago on September 24th my son Kian was born, after a 5 year fertility\njourney with my wife Roxy. Roxy and Kian are well and I\u2019m really excited for\neverything that comes next!<\/p>\n\n<p><img src=\"\/assets\/posts\/kian.jpg\" class=\"fill-width\" title=\"Evert holding his son Kian at the day of his birth\" \/><\/p>\n"},{"id":"http:\/\/evertpot.com\/webcomponent-download-counter\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/webcomponent-download-counter\/"}},"title":"Creating a fake download counter with Web Components","updated":"2024-07-02T16:07:33+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>Over the years I\u2019ve written several open source libraries. They\u2019re mostly\nunglamorous and utilitarian, but a bunch of them obtained got a decent\ndownload count, so I thought it would be fun to try and get a grand total\nand show a \u2018live\u2019 download counter on my blog.<\/p>\n\n<p>This is how that looks like:<\/p>\n\n<blockquote>\n  <p>My open source packages were downloaded roughly\n<strong><download-counter inc-per-day=\"157444\" date=\"2024-06-29T15:34:00Z\">138945563<\/download-counter><\/strong> times.<\/p>\n<\/blockquote>\n\n<p>Like most live counters, this number isn\u2019t tracked in real-time. Instead it\njust uses a start number and updates the number based on an average number\nof downloads.<\/p>\n\n<p>One day it might be nice to make it live, but this is a static blog and this\nwould need proper hosting and a database.<\/p>\n\n<h3 id=\"why-is-this-number-so-high\">Why is this number so high?<\/h3>\n\n<p>This data comes from NPM and Packagist, and they both\ncount any download. So this number doesn\u2019t represent necessarily 138 million\nusers, but simply this many downloads by any means including bots, CI\nenvironments and so on. I think for both package managers the goal was\nprobably not to have a realistic representation of users, but rather a number\nthat makes developers feel good. And I like that. It\u2019s nice to see a number\ngo up and it\u2019s still a nice proxy for relative popularity.<\/p>\n\n<h2 id=\"the-web-component\">The Web Component<\/h2>\n\n<p>This seemed like a good use-case for a web component. Always wanted to build\none! I was surprised how easy it was.<\/p>\n\n<p>This is how this looks in the HTML:<\/p>\n\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nt\">&lt;p&gt;<\/span>\n  My open source packages were downloaded roughly\n  <span class=\"nt\">&lt;strong&gt;<\/span>\n    <span class=\"nt\">&lt;download-counter<\/span> <span class=\"na\">inc-per-day=<\/span><span class=\"s\">\"157444\"<\/span> <span class=\"na\">date=<\/span><span class=\"s\">\"2024-06-29T15:34:00Z\"<\/span> <span class=\"nt\">&gt;<\/span>\n      138945563\n    <span class=\"nt\">&lt;\/download-counter&gt;<\/span>\n  <span class=\"nt\">&lt;\/strong&gt;<\/span> times.\n<span class=\"nt\">&lt;\/p&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>What\u2019s nice is that if Javascript is not enabled, or Web Components are\nnot supported, this will just fall back on showing the static number.<\/p>\n\n<p>I included 3 parameters:<\/p>\n\n<ul>\n  <li>The last recorded download count (in the element value)<\/li>\n  <li>When that number was recorded (date)<\/li>\n  <li>Average number of downloads per day.<\/li>\n<\/ul>\n\n<p>I wanted to include the date because I only intend to update these numbers\nrarely, so we need to know how many downloads have elapsed since the last\ntime.<\/p>\n\n<p>Writing the web component was surprisingly straightforward too. Here is it\nin its fully glory:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">class<\/span> <span class=\"nx\">DownloadCounter<\/span> <span class=\"kd\">extends<\/span> <span class=\"nx\">HTMLElement<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"nx\">connectedCallback<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">count<\/span> <span class=\"o\">=<\/span> <span class=\"o\">+<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">textContent<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">date<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nb\">Date<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">getAttribute<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">date<\/span><span class=\"dl\">'<\/span><span class=\"p\">));<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">inc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"o\">+<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">getAttribute<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">inc-per-day<\/span><span class=\"dl\">'<\/span><span class=\"p\">))<\/span> <span class=\"o\">\/<\/span> <span class=\"p\">(<\/span><span class=\"mi\">3600<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">24<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">1000<\/span><span class=\"p\">)<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">calculateCurrentDownloads<\/span><span class=\"p\">();<\/span>\n\n  <span class=\"p\">}<\/span>\n\n  <span class=\"nx\">calculateCurrentDownloads<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"kd\">const<\/span> <span class=\"nx\">currentDownloads<\/span> <span class=\"o\">=<\/span>\n      <span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nx\">floor<\/span><span class=\"p\">(<\/span>\n      <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">count<\/span> <span class=\"o\">+<\/span>\n      <span class=\"p\">(<\/span><span class=\"nb\">Date<\/span><span class=\"p\">.<\/span><span class=\"nx\">now<\/span><span class=\"p\">()<\/span><span class=\"o\">-<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">date<\/span><span class=\"p\">)<\/span> <span class=\"o\">*<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">inc<\/span>\n      <span class=\"p\">);<\/span>\n\n    <span class=\"c1\">\/\/ Intl.NumberFormat adds thousands separators<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">textContent<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">Intl<\/span><span class=\"p\">.<\/span><span class=\"nx\">NumberFormat<\/span><span class=\"p\">().<\/span><span class=\"nx\">format<\/span><span class=\"p\">(<\/span><span class=\"nx\">currentDownloads<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"nx\">setTimeout<\/span><span class=\"p\">(<\/span>\n      <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">calculateCurrentDownloads<\/span><span class=\"p\">(),<\/span>\n      <span class=\"c1\">\/\/ Add some randomnes<\/span>\n      <span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nx\">floor<\/span><span class=\"p\">(<\/span><span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nx\">random<\/span><span class=\"p\">()<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">150<\/span><span class=\"p\">)<\/span><span class=\"o\">+<\/span><span class=\"mi\">50<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">);<\/span>\n\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">}<\/span>\n\n<span class=\"nx\">customElements<\/span><span class=\"p\">.<\/span><span class=\"nx\">define<\/span><span class=\"p\">(<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">download-counter<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"nx\">DownloadCounter<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Now just include the file in the HTML and you\u2019re good to go:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"o\">&lt;<\/span><span class=\"nx\">script<\/span> <span class=\"nx\">type<\/span><span class=\"o\">=<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">module<\/span><span class=\"dl\">\"<\/span> <span class=\"nx\">src<\/span><span class=\"o\">=<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">\/assets\/js\/downloadcounter.mjs<\/span><span class=\"dl\">\"<\/span><span class=\"o\">&gt;&lt;<\/span><span class=\"sr\">\/script<\/span><span class=\"err\">&gt;\n<\/span><\/code><\/pre><\/div><\/div>\n\n<h2 id=\"obtaining-the-data\">Obtaining the data<\/h2>\n\n<p>I\u2019ve published 20 PHP libraries on <a href=\"https:\/\/packagist.org\/\">packagist<\/a> and 30\nJavascript libraries on <a href=\"https:\/\/www.npmjs.com\/\">NPM<\/a>. This is too much to\ncount, so instead I wrote some scripts that pull in the data for their\nrespective APIs.<\/p>\n\n<p>You cannot easily ask these APIs what the download count or download rate at a\ngiven date was, and for both the numbers might be a bit delayed. So I\u2019ve opted\nto simply:<\/p>\n\n<ul>\n  <li>Get a grand total of all downloads up until this date.<\/li>\n  <li>Wait at least 24 hours or more.<\/li>\n  <li>Get another grand total and use the difference to caclulate average download rate.<\/li>\n<\/ul>\n\n<p>Below is my approach for getting the numbers from NPM and Packagist. It\u2019s a bit\nmessy and imperative.<\/p>\n\n<h3 id=\"npm\">NPM<\/h3>\n\n<p>The NPM api gave me weird, incomplete results for getting the whole list of packages\nI\u2019ve authored, so I worked around this by hardcoding some package names, and then\naugmenting this with the result of 3 searches.<\/p>\n\n<p>This gives us the full list of packages I\u2019m interested in:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ Searches<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">npmSearches<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span>\n  <span class=\"c1\">\/\/ by author<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">author:evrt<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n\n  <span class=\"c1\">\/\/ by org name<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">scope:badgateway<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">scope:curveball<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">];<\/span>\n\n<span class=\"c1\">\/\/ Hardcoded extra packages that for some reason didn't get returned with<\/span>\n<span class=\"c1\">\/\/ the searches<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">npmPackages<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">davclient.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">structured-headers<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">bigint-money<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">fetch-mw-oauth2<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">hal-types<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">react-ketting<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">ketting<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">html-form-enhancer<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">changelog-tool<\/span><span class=\"dl\">'<\/span><span class=\"p\">:<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">};<\/span>\n\n\n<span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">fetchNpmPackageList<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">search<\/span> <span class=\"k\">of<\/span> <span class=\"nx\">npmSearches<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"kd\">const<\/span> <span class=\"nx\">res<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">https:\/\/registry.npmjs.org\/-\/v1\/search?text=<\/span><span class=\"dl\">'<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">search<\/span><span class=\"p\">);<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">res<\/span><span class=\"p\">.<\/span><span class=\"nx\">json<\/span><span class=\"p\">();<\/span>\n\n    <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">object<\/span> <span class=\"k\">of<\/span> <span class=\"nx\">body<\/span><span class=\"p\">.<\/span><span class=\"nx\">objects<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n      <span class=\"nx\">npmPackages<\/span><span class=\"p\">[<\/span><span class=\"nx\">object<\/span><span class=\"p\">.<\/span><span class=\"kr\">package<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>NPM doesn\u2019t return absolute total download counts but instead we can get a\ncount for a specific time range with a maximum range of 18 months. I\u2019ve\nopted to instead to get download counts per year, counting backwards for\neach package until get a result of 0.<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">fetchNpmDownloadCounts<\/span><span class=\"p\">(<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">let<\/span> <span class=\"nx\">year<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nb\">Date<\/span><span class=\"p\">().<\/span><span class=\"nx\">getFullYear<\/span><span class=\"p\">();<\/span>\n  <span class=\"kd\">let<\/span> <span class=\"nx\">count<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n  <span class=\"kd\">let<\/span> <span class=\"nx\">yearCount<\/span><span class=\"p\">;<\/span>\n\n  <span class=\"k\">do<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">yearCount<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetchNpmDownloadCountsByYear<\/span><span class=\"p\">(<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">year<\/span><span class=\"p\">);<\/span>\n    <span class=\"nx\">count<\/span> <span class=\"o\">+=<\/span> <span class=\"nx\">yearCount<\/span><span class=\"p\">;<\/span>\n    <span class=\"nx\">year<\/span><span class=\"o\">--<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span> <span class=\"k\">while<\/span> <span class=\"p\">(<\/span><span class=\"nx\">yearCount<\/span> <span class=\"o\">&gt;<\/span> <span class=\"mi\">0<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">%s: %i<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">count<\/span><span class=\"p\">);<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">count<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">fetchNpmDownloadCountsByYear<\/span><span class=\"p\">(<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">year<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">res<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span><span class=\"s2\">`https:\/\/api.npmjs.org\/downloads\/point\/<\/span><span class=\"p\">${<\/span><span class=\"nx\">year<\/span><span class=\"p\">}<\/span><span class=\"s2\">-01-01:<\/span><span class=\"p\">${<\/span><span class=\"nx\">year<\/span><span class=\"p\">}<\/span><span class=\"s2\">-12-31\/<\/span><span class=\"p\">${<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">}<\/span><span class=\"s2\">`<\/span><span class=\"p\">);<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">res<\/span><span class=\"p\">.<\/span><span class=\"nx\">json<\/span><span class=\"p\">();<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">body<\/span><span class=\"p\">.<\/span><span class=\"nx\">downloads<\/span><span class=\"p\">;<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"packagist\">Packagist<\/h3>\n\n<p>Getting totals from packagist was considerably easier:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">packagistOrgs<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">sabre<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">evert<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">];<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">packagistPackages<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{};<\/span>\n\n<span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">fetchPackagistPackages<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">vendor<\/span> <span class=\"k\">of<\/span> <span class=\"nx\">packagistOrgs<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">res<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span><span class=\"s2\">`https:\/\/packagist.org\/packages\/list.json?vendor=<\/span><span class=\"p\">${<\/span><span class=\"nx\">vendor<\/span><span class=\"p\">}<\/span><span class=\"s2\">`<\/span><span class=\"p\">);<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">res<\/span><span class=\"p\">.<\/span><span class=\"nx\">json<\/span><span class=\"p\">();<\/span>\n    <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">pkg<\/span> <span class=\"k\">of<\/span> <span class=\"nx\">body<\/span><span class=\"p\">.<\/span><span class=\"nx\">packageNames<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n      <span class=\"nx\">packagistPackages<\/span><span class=\"p\">[<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">}<\/span>\n\n<span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">fetchPackagistDownloadCounts<\/span><span class=\"p\">(<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">res<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span><span class=\"s2\">`https:\/\/packagist.org\/packages\/<\/span><span class=\"p\">${<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">}<\/span><span class=\"s2\">\/stats.json`<\/span><span class=\"p\">);<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">json<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">res<\/span><span class=\"p\">.<\/span><span class=\"nx\">json<\/span><span class=\"p\">();<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">count<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">json<\/span><span class=\"p\">.<\/span><span class=\"nx\">downloads<\/span><span class=\"p\">.<\/span><span class=\"nx\">total<\/span><span class=\"p\">;<\/span>\n\n  <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">%s: %i<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">count<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"nx\">count<\/span><span class=\"p\">;<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"putting-it-together\">Putting it together<\/h3>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">main<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">await<\/span> <span class=\"nx\">fetchNpmPackageList<\/span><span class=\"p\">();<\/span>\n  <span class=\"k\">await<\/span> <span class=\"nx\">fetchPackagistPackages<\/span><span class=\"p\">();<\/span>\n\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">pkg<\/span> <span class=\"k\">of<\/span> <span class=\"nb\">Object<\/span><span class=\"p\">.<\/span><span class=\"nx\">keys<\/span><span class=\"p\">(<\/span><span class=\"nx\">npmPackages<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">npmPackages<\/span><span class=\"p\">[<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetchNpmDownloadCounts<\/span><span class=\"p\">(<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">);<\/span>\n  <span class=\"p\">}<\/span>\n\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"nx\">pkg<\/span> <span class=\"k\">of<\/span> <span class=\"nb\">Object<\/span><span class=\"p\">.<\/span><span class=\"nx\">keys<\/span><span class=\"p\">(<\/span><span class=\"nx\">packagistPackages<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">packagistPackages<\/span><span class=\"p\">[<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">fetchPackagistDownloadCounts<\/span><span class=\"p\">(<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">);<\/span>\n  <span class=\"p\">}<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">packagesCombined<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[];<\/span>\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"p\">[<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">downloads<\/span><span class=\"p\">]<\/span> <span class=\"k\">of<\/span> <span class=\"nb\">Object<\/span><span class=\"p\">.<\/span><span class=\"nx\">entries<\/span><span class=\"p\">(<\/span><span class=\"nx\">npmPackages<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">packagesCombined<\/span><span class=\"p\">.<\/span><span class=\"nx\">push<\/span><span class=\"p\">({<\/span>\n      <span class=\"na\">ecosystem<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">npm<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span>\n      <span class=\"nx\">downloads<\/span>\n    <span class=\"p\">});<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"k\">for<\/span><span class=\"p\">(<\/span><span class=\"kd\">const<\/span> <span class=\"p\">[<\/span><span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span> <span class=\"nx\">downloads<\/span><span class=\"p\">]<\/span> <span class=\"k\">of<\/span> <span class=\"nb\">Object<\/span><span class=\"p\">.<\/span><span class=\"nx\">entries<\/span><span class=\"p\">(<\/span><span class=\"nx\">packagistPackages<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">packagesCombined<\/span><span class=\"p\">.<\/span><span class=\"nx\">push<\/span><span class=\"p\">({<\/span>\n      <span class=\"na\">ecosystem<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">packagist<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"nx\">packageName<\/span><span class=\"p\">,<\/span>\n      <span class=\"nx\">downloads<\/span>\n    <span class=\"p\">});<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">packagesCombined<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">Total: %i<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">packagesCombined<\/span><span class=\"p\">.<\/span><span class=\"nx\">reduce<\/span><span class=\"p\">((<\/span><span class=\"nx\">acc<\/span><span class=\"p\">,<\/span> <span class=\"nx\">cur<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nx\">acc<\/span><span class=\"o\">+<\/span><span class=\"nx\">cur<\/span><span class=\"p\">.<\/span><span class=\"nx\">downloads<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">));<\/span>\n\n<span class=\"p\">}<\/span>\n<span class=\"k\">await<\/span> <span class=\"nx\">main<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n<p>Whenever I need small bits of Javascript to enhance a web application (and\nnot a full-blown framework) I tend to write code that looks for an element\nwith a particular class, and then start my logic and hook up events.<\/p>\n\n<p>Web Components seems like a really great replacement for that.<\/p>\n\n<p>Got comments? Liked this aticle? You can reply to <a href=\"https:\/\/indieweb.social\/@evert\/112717778570631838\">this Mastodon post<\/a>\nto make the comments show up here.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/bigint-money-2\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/bigint-money-2\/"}},"title":"Moving on from Mocha, Chai and nyc.","updated":"2024-04-21T03:00:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I\u2019m a maintainer of several small open-source libraries. It\u2019s a fun activity.\nIf the scope of the library is small enough, the maintenance burden is\ntypically fairly low. They\u2019re usually mostly \u2018done\u2019, and I occasionally just need to\nanswer a few questions per year, and do the occasional release to bring it\nback up to the current \u2018meta\u2019 of the ecosystem.<\/p>\n\n<p>Also even though it\u2019s \u2018done\u2019, in use by a bunch of people and well tested,\nit\u2019s also good to do a release from time to time to not give the impression\nof abandonment.<\/p>\n\n<p>This weekend I released a 2.0 version of my <a href=\"https:\/\/github.com\/evert\/bigint-money\">bigint-money<\/a> library, which\nis a fast library for currency math.<\/p>\n\n<p>I originally wrote this in 2018, so the big BC break was switching everything\nover to <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Modules\" title=\"JavaScript modules\">ESM<\/a>. For a while I <a href=\"https:\/\/evertpot.com\/universal-commonjs-esm-typescript-packages\/\">tried to support both CommonJS and ESM<\/a>\nbuilds for my packages, but only a year after all that effort it frankly no\nlonger feels needed. I was worried the ecosystem was going to\nsplit, but people stuck on (unsupported) versions of Node that don\u2019t\nsupport ESM aren\u2019t going to proactively keep their other dependencies updated,\nso CommonJS is for (and many others) in the past now. (yay!)<\/p>\n\n<p>Probably <em>the single best way<\/em> to keep maintenance burden for packages low is\nto have few dependencies. Many of my packages have 0 dependencies.<\/p>\n\n<p>Reducing <code class=\"language-plaintext highlighter-rouge\">devDependencies<\/code> also helps. If you didn\u2019t know, <code class=\"language-plaintext highlighter-rouge\">node<\/code> now has a\nbuilt-in testrunner. I\u2019ve been using <a href=\"https:\/\/mochajs.org\/\">Mocha<\/a> + <a href=\"https:\/\/www.chaijs.com\/\">Chai<\/a> for many many\nyears. They were awesome and want to thank the maintainers, but <code class=\"language-plaintext highlighter-rouge\">node --test<\/code>\nis pretty good now and has pretty output.<\/p>\n\n<p>It also:<\/p>\n\n<ul>\n  <li>Is much faster (about twice as fast with Typescript and code coverage\nreporting, but I suspect the difference will grow with larger code bases).<\/li>\n  <li>Easier to configure (especially when you\u2019re also using Typescript. Just use <code class=\"language-plaintext highlighter-rouge\">tsx --test<\/code>).<\/li>\n  <li>It can output test coverage with (<code class=\"language-plaintext highlighter-rouge\">--experimental-test-coverage<\/code>).<\/li>\n<\/ul>\n\n<video src=\"\/assets\/video\/node-test.webm\" class=\"fill-width\" controls=\"\" loop=\"\"><\/video>\n\n<p>Furthermore, while <a href=\"https:\/\/nodejs.org\/api\/assert.html\">node:assert<\/a> doesn\u2019t have all features of Chai, it has\nthe important ones (deep compare) and adds better Promise support.<\/p>\n\n<p>All in all this reduced my <code class=\"language-plaintext highlighter-rouge\">node_modules<\/code> directory from a surprising 159M\nto 97M, most of which is now Typescript and ESLint, and my total dependency\ncount from 335 to 141 (almost all of which is ESLint).<\/p>\n\n<p>Make sure that Node\u2019s test library, coverage and assertion library is right\nfor you. It may not have all the features you expect, but I keep my testing\nsetup relatively simple, so the switch was easy.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/oauth2-client-updates\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/oauth2-client-updates\/"}},"title":"OAuth2 client updates","updated":"2024-02-05T15:00:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I just released v2.3.0 of <a href=\"https:\/\/www.npmjs.com\/package\/@badgateway\/oauth2-client\"><code class=\"language-plaintext highlighter-rouge\">@badgateway\/oauth2-client<\/code><\/a>, which I wrote\nbecause there weren\u2019t any lean, 0-dependency oauth2 clients with modern\nfeatures such as <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7636\">PKCE<\/a>.<\/p>\n\n<p>This new version includes support for:<\/p>\n\n<ul>\n  <li>Resource Indicators for OAuth 2.0 (<a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc8707\" title=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc8707\">RFC8707<\/a>).<\/li>\n  <li>OAuth2 Token Revocation (<a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7009\" title=\"OAuth 2.0 Token Revocation\">RFC7009<\/a>).<\/li>\n<\/ul>\n\n<p>Hope you like it!<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/jsx-template\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/jsx-template\/"}},"title":"Using JSX on the server as a template engine","updated":"2023-11-14T13:14:02+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>The React\/Next.js ecosystem is spinning out of control in terms of magic and complexity.\nThe stack has failed to stay focused and simple, and it\u2019s my belief\nthat software stacks that are too complex and magical must eventually fail,\nbecause as sensibilities around software design change they will be unable to\nadapt to those changes without cannibalizing their existing userbase.<\/p>\n\n<p>So while React\/Next.js may be relegated to the enterprise and legacy systems in\na few years, they completely transformed front-end development and created ripple\neffects in many other technologies. One of many great ideas stemming from this\nstack is <a href=\"https:\/\/en.wikipedia.org\/wiki\/JSX_(JavaScript)\">JSX<\/a>. I think JSX has a chance to stay relevant and useful beyond\nReact\/Next.<\/p>\n\n<p>One of it\u2019s use-cases is for server-side templating. I\u2019ve been using JSX as a\ntemplate engine to replace template engines like <a href=\"https:\/\/ejs.co\/\">EJS<\/a> and\n<a href=\"https:\/\/handlebarsjs.com\/\">Handlebars<\/a>, and more than once people were surprised this was possible\nwithout bulky frameworks such as Next.js.<\/p>\n\n<p>So in this article I\u2019m digging into what JSX is, where it comes from and how one\nmight go about using it as a simple server-side HTML template engine.<\/p>\n\n<h2 id=\"what-is-jsx\">What is JSX?<\/h2>\n\n<p>JSX is an extension to the Javascript language, and was introduced with React.\nIt usually has a <code class=\"language-plaintext highlighter-rouge\">.jsx<\/code> extension and it needs to be compiled <em>to<\/em> Javascript.\nMost build tools people already use, like ESbuild, Babel, Vite, etc. all\nsupport this natively or through a plugin.\nTypescript also natively supports it, so if you use Typescript you can just start\nusing it without adding another tool.<\/p>\n\n<p>It looks like this:<\/p>\n\n<div class=\"language-jsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Hello world!<span class=\"p\">&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">p<\/span><span class=\"p\">&gt;<\/span>Sup<span class=\"p\">&lt;\/<\/span><span class=\"nt\">p<\/span><span class=\"p\">&gt;<\/span>\n<span class=\"p\">&lt;\/<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>As you can see here, some HTML is directly embedded into Javascript, without\nquotes. It\u2019s all syntax. It lets you use the full power of Javascript, such\nas variables and loops:<\/p>\n\n<div class=\"language-jsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">user<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">Evert<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">todos<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">Wash clothes<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">Do dishes<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">];<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Hello <span class=\"si\">{<\/span><span class=\"nx\">evert<\/span><span class=\"si\">}<\/span><span class=\"p\">&lt;\/<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">ul<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"si\">{<\/span><span class=\"nx\">todos<\/span><span class=\"p\">.<\/span><span class=\"nx\">map<\/span><span class=\"p\">(<\/span> <span class=\"nx\">todo<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">li<\/span><span class=\"p\">&gt;<\/span>todo<span class=\"p\">&lt;\/<\/span><span class=\"nt\">li<\/span><span class=\"p\">&gt;)<\/span><span class=\"si\">}<\/span>\n  <span class=\"p\">&lt;\/<\/span><span class=\"nt\">ul<\/span><span class=\"p\">&gt;<\/span>\n<span class=\"p\">&lt;\/<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>It has a convention to treat tags that start with a lowercase character such\nas <code class=\"language-plaintext highlighter-rouge\">&lt;h1&gt;<\/code> as output, but if the tag starts with an uppercase character,\nit\u2019s a component, which usually is represented by a function:<\/p>\n\n<div class=\"language-jsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function<\/span> <span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">(<\/span><span class=\"nx\">props<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Hello <span class=\"p\">&lt;<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span><span class=\"si\">{<\/span><span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"si\">}<\/span><span class=\"p\">&lt;\/<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>\n\n<span class=\"p\">}<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">section<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nc\">HelloWorldComponent<\/span> <span class=\"na\">name<\/span><span class=\"p\">=<\/span><span class=\"s\">\"Evert\"<\/span> <span class=\"p\">\/&gt;<\/span>\n<span class=\"p\">&lt;\/<\/span><span class=\"nt\">section<\/span><span class=\"p\">&gt;;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Anyway, if you\u2019re reading this you likely knew most of this but it\u2019s important\nto state that this are all JSX features, not React.<\/p>\n\n<h2 id=\"inspo\">#Inspo<\/h2>\n\n<p>JSX probably has it\u2019s roots in <a href=\"https:\/\/en.wikipedia.org\/wiki\/ECMAScript_for_XML\">E4X<\/a> (and more directly <a href=\"https:\/\/docs.hhvm.com\/hack\/XHP\/introduction\">XHP<\/a>). E4X was a\nway to embed XML in Javascript. E4X has been supported in Firefox for years, and\nwas a part of ActionScript 3 onwards, but there\u2019s a major conceptual difference\nbetween E4X and JSX.<\/p>\n\n<p>With E4X you embed XML documents as data, similar to\ndefining a JSON object in a Javascript file. After defining the XML document\nyou can manipulate it. A fictional transpiler for E4X could (as far as I can\ntell) could just take the XML structure and turn it into a string and pass it to\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/DOMParser\/parseFromString\"><code class=\"language-plaintext highlighter-rouge\">DOMParser.parseFromString()<\/code><\/a>. In E4X there are no variables, functions,\ncomponents, or logic. Similar to how a Regular expression is part of the JS\nlanguage.<\/p>\n\n<p>JSX is quite different to this. It\u2019s fully integrated in the language and it\neffectively compiles down to nested function definitions. These functions are\ndon\u2019t get turned into HTML until they are called by <a href=\"https:\/\/legacy.reactjs.org\/docs\/rendering-elements.html#rendering-an-element-into-the-dom\" title=\"Rendering an Element into the DOM\">some render function<\/a>.\nBefore this they are defined but not evaluated.<\/p>\n\n<p>So while E4X and JSX share that they both make XML\/HTML first-class citizens\nin the language, the goals and features are wildly different. JSX really is a\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Domain-specific_language\">DSL<\/a>.<\/p>\n\n<h2 id=\"jsx-transpiled\">JSX Transpiled<\/h2>\n\n<p>Lets turn the previous example into Typescript, and see what Typescript does with\nit:<\/p>\n\n<div class=\"language-jsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function<\/span> <span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">(<\/span><span class=\"nx\">props<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"nl\">name<\/span><span class=\"p\">:<\/span> <span class=\"nx\">string<\/span> <span class=\"p\">})<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Hello <span class=\"p\">&lt;<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span><span class=\"si\">{<\/span><span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"si\">}<\/span><span class=\"p\">&lt;\/<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>\n\n<span class=\"p\">}<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">section<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nc\">HelloWorldComponent<\/span> <span class=\"na\">name<\/span><span class=\"p\">=<\/span><span class=\"s\">\"Evert\"<\/span> <span class=\"p\">\/&gt;<\/span>\n<span class=\"p\">&lt;\/<\/span><span class=\"nt\">section<\/span><span class=\"p\">&gt;;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>By default, Typescript will turn it into this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function<\/span> <span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">(<\/span><span class=\"nx\">props<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"nx\">React<\/span><span class=\"p\">.<\/span><span class=\"nx\">createElement<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">h1<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"kc\">null<\/span><span class=\"p\">,<\/span>\n        <span class=\"dl\">\"<\/span><span class=\"s2\">Hello <\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span>\n        <span class=\"nx\">React<\/span><span class=\"p\">.<\/span><span class=\"nx\">createElement<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">span<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"kc\">null<\/span><span class=\"p\">,<\/span> <span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"p\">));<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">React<\/span><span class=\"p\">.<\/span><span class=\"nx\">createElement<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">section<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"kc\">null<\/span><span class=\"p\">,<\/span>\n    <span class=\"nx\">React<\/span><span class=\"p\">.<\/span><span class=\"nx\">createElement<\/span><span class=\"p\">(<\/span><span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span> <span class=\"na\">name<\/span><span class=\"p\">:<\/span> <span class=\"dl\">\"<\/span><span class=\"s2\">Evert<\/span><span class=\"dl\">\"<\/span> <span class=\"p\">}));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Now, there\u2019s definitely a react dependency here, but in recent versions of\nTypescript, we can configure it to use a different \u2018factory\u2019 for JSX. By\nsetting the <a href=\"https:\/\/www.typescriptlang.org\/tsconfig#jsx\">jsx<\/a> and <a href=\"https:\/\/www.typescriptlang.org\/tsconfig#jsxFactory\">jsxFactory<\/a> settings in <code class=\"language-plaintext highlighter-rouge\">tsconfig.json<\/code>, we\ncan get Typescript to output more generic code:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function<\/span> <span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">(<\/span><span class=\"nx\">props<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"nx\">_jsxs<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">h1<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span> <span class=\"na\">children<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">Hello <\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"nx\">_jsx<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">span<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span> <span class=\"na\">children<\/span><span class=\"p\">:<\/span> <span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span> <span class=\"p\">})]<\/span> <span class=\"p\">});<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">_jsx<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">section<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span> <span class=\"na\">children<\/span><span class=\"p\">:<\/span> <span class=\"nx\">_jsx<\/span><span class=\"p\">(<\/span><span class=\"nx\">HelloWorldComponent<\/span><span class=\"p\">,<\/span> <span class=\"p\">{<\/span> <span class=\"na\">name<\/span><span class=\"p\">:<\/span> <span class=\"dl\">\"<\/span><span class=\"s2\">Evert<\/span><span class=\"dl\">\"<\/span> <span class=\"p\">})<\/span> <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>So what\u2019s <code class=\"language-plaintext highlighter-rouge\">_jsx<\/code>? This is a \u2018jsx factory\u2019 function that can be provided by\nReact, but also a number of other libraries such as <a href=\"https:\/\/preactjs.com\/\">Preact<\/a> or\n<a href=\"https:\/\/www.solidjs.com\/\">Solid.js<\/a>. It\u2019s also relatively easy to <a href=\"https:\/\/lwebapp.com\/en\/post\/custom-jsx\" title=\"Custom JSX Factory Function\">create your own<\/a>.<\/p>\n\n<p>What\u2019s interesting then about JSX is that while it\u2019s syntax, it\u2019s not\nalways clear what this yields:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">foo<\/span> <span class=\"o\">=<\/span> <span class=\"o\">&lt;<\/span><span class=\"nx\">h1<\/span><span class=\"o\">&gt;<\/span><span class=\"nx\">Sup<\/span><span class=\"o\">!&lt;<\/span><span class=\"sr\">\/h1<\/span><span class=\"err\">&gt;\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>Because what\u2019s in <code class=\"language-plaintext highlighter-rouge\">foo<\/code> depends on what JSX Factory was used during\ntranspiling. This is frustrating because it requires out of band information\nand external configuration, but also a benefit because it opens the doors\nto alternative implementations.<\/p>\n\n<h2 id=\"using-jsx-as-a-template-engine\">Using JSX as a template engine<\/h2>\n\n<p>When generating HTML on the server with Node, it\u2019s pretty common to use\ntemplate engines such as <a href=\"https:\/\/ejs.co\/\">EJS<\/a> or <a href=\"https:\/\/handlebarsjs.com\/\">Handlebars<\/a>. When building a\nnew (multi-page) web application, it occurred to me that neither are quit\nas good as JSX.<\/p>\n\n<p>Some of the advantages of JSX are:<\/p>\n\n<ul>\n  <li>Deep IDE\/Language server\/intellisense integration, because it\u2019s all\nsyntax.<\/li>\n  <li>Static type checking with Typescript.<\/li>\n  <li>It also enforced correctly structured HTML. No way to forget to close\nan element.<\/li>\n  <li>By default dynamic data is escaped. They made it challenging to get\nunescaped HTML!<\/li>\n<\/ul>\n\n<p>This had me wondering, can we use server-side microframeworks such\nas <a href=\"https:\/\/koajs.com\/\">Koa<\/a>, <a href=\"https:\/\/fastify.dev\/\">Fastify<\/a> or <a href=\"https:\/\/expressjs.com\/\">Express<\/a> but instead of string-based\ntemplate parsers and get all the benefits of JSX?<\/p>\n\n<p>Turns out the answer is, yes! It\u2019s not only pretty simple to implement,\nit works exceedingly well.<\/p>\n\n<p>I\u2019ve implemented this pattern 3 times now for different frameworks, here\u2019s\nsome sample code that works:<\/p>\n\n<h3 id=\"koa\">Koa<\/h3>\n\n<p>Here\u2019s an example of a small controller:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">Router<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">koa-router<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">router<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Router<\/span><span class=\"p\">();<\/span>\n\n<span class=\"nx\">router<\/span><span class=\"p\">.<\/span><span class=\"kd\">get<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">\/foo.html<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">ctx<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Hello world with JSX in Koa!<span class=\"p\">&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;;<\/span>\n\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>As you can see here we can set JSX straight on the body property. A middleware\nensures this gets transformed into HTML.<\/p>\n\n<h3 id=\"fastify\">Fastify<\/h3>\n\n<p>Similar to Koa, we can use JSX instead where otherwise HTML strings would be\nused:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">FastifyInstance<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">fastify<\/span><span class=\"dl\">'<\/span>\n\n<span class=\"k\">export<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">routes<\/span><span class=\"p\">(<\/span><span class=\"nx\">fastify<\/span><span class=\"p\">:<\/span> <span class=\"nx\">FastifyInstance<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"nx\">fastify<\/span><span class=\"p\">.<\/span><span class=\"kd\">get<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">\/hello-world<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">request<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Sup Fastify!<span class=\"p\">&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;);<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"how-does-this-magic-work\">How does this magic work?<\/h2>\n\n<p>For both of these I used the React library. Despite it\u2019s ecosystem being\nrather bulky, standalone React is still pretty lean and highly optimized.<\/p>\n\n<p>For both of these frameworks, I simply created a middleware that checks if\nthe body is a React node, and if so use React\u2019s  <a href=\"https:\/\/react.dev\/reference\/react-dom\/server\/renderToStaticMarkup\">renderToStaticMarkup<\/a> to do\nthe transformation before the response is sent.<\/p>\n\n<h3 id=\"koa-middleware\">Koa middleware<\/h3>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">renderToStaticMarkup<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">react-dom\/server<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">isValidElement<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">react<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">Context<\/span><span class=\"p\">,<\/span> <span class=\"nx\">Middleware<\/span><span class=\"p\">,<\/span> <span class=\"nx\">Next<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">koa<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"k\">export<\/span> <span class=\"kd\">const<\/span> <span class=\"nx\">jsx<\/span><span class=\"p\">:<\/span> <span class=\"nx\">Middleware<\/span> <span class=\"o\">=<\/span> <span class=\"k\">async<\/span><span class=\"p\">(<\/span><span class=\"nx\">ctx<\/span><span class=\"p\">:<\/span> <span class=\"nx\">Context<\/span><span class=\"p\">,<\/span> <span class=\"nx\">next<\/span><span class=\"p\">:<\/span> <span class=\"nx\">Next<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">await<\/span> <span class=\"nx\">next<\/span><span class=\"p\">();<\/span>\n\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">isValidElement<\/span><span class=\"p\">(<\/span><span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">&lt;!DOCTYPE html&gt;<\/span><span class=\"se\">\\n<\/span><span class=\"dl\">'<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">renderToStaticMarkup<\/span><span class=\"p\">(<\/span>\n      <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span>\n    <span class=\"p\">);<\/span>\n    <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"kd\">type<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">text\/html; charset=utf-8<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is how it\u2019s used:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">application<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Koa<\/span><span class=\"p\">();<\/span>\n<span class=\"nx\">application<\/span><span class=\"p\">.<\/span><span class=\"nx\">use<\/span><span class=\"p\">(<\/span><span class=\"nx\">jsx<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"fastify-hooks\">Fastify hooks<\/h3>\n\n<p>This one needed a few more hacks, but the result is worth it:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">onSendHookHandler<\/span><span class=\"p\">,<\/span> <span class=\"nx\">preSerializationHookHandler<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">fastify<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">isValidElement<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">react<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">renderToStaticMarkup<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">react-dom\/server<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"cm\">\/**\n * The preserialization hook lets us transform the response body\n * before it's json-encoded.\n *\n * We use this to turn React components into an object with a ___jsx key\n * that has the serialized HTML.\n *\/<\/span>\n<span class=\"k\">export<\/span> <span class=\"kd\">const<\/span> <span class=\"nx\">preSerialization<\/span><span class=\"p\">:<\/span> <span class=\"nx\">preSerializationHookHandler<\/span><span class=\"o\">&lt;<\/span><span class=\"nx\">unknown<\/span><span class=\"o\">&gt;<\/span> <span class=\"o\">=<\/span> <span class=\"k\">async<\/span> <span class=\"p\">(<\/span><span class=\"nx\">_request<\/span><span class=\"p\">,<\/span> <span class=\"nx\">reply<\/span><span class=\"p\">,<\/span> <span class=\"nx\">payload<\/span><span class=\"p\">:<\/span> <span class=\"nx\">unknown<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">isValidElement<\/span><span class=\"p\">(<\/span><span class=\"nx\">payload<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nx\">reply<\/span><span class=\"p\">.<\/span><span class=\"nx\">header<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">Content-Type<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">text\/html<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n    <span class=\"k\">return<\/span> <span class=\"p\">{<\/span>\n      <span class=\"na\">___jsx<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">&lt;!DOCTYPE html&gt;<\/span><span class=\"se\">\\n<\/span><span class=\"dl\">'<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">renderToStaticMarkup<\/span><span class=\"p\">(<\/span><span class=\"nx\">payload<\/span> <span class=\"k\">as<\/span> <span class=\"kr\">any<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">};<\/span>\n  <span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"nx\">payload<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">};<\/span>\n\n<span class=\"cm\">\/**\n * The onSendHookHandler lets us transform the response body (as a string)\n * We detect the ___jsx key and unwrap the HTML.\n *\/<\/span>\n<span class=\"k\">export<\/span> <span class=\"kd\">const<\/span> <span class=\"nx\">onSend<\/span><span class=\"p\">:<\/span> <span class=\"nx\">onSendHookHandler<\/span><span class=\"o\">&lt;<\/span><span class=\"nx\">unknown<\/span><span class=\"o\">&gt;<\/span> <span class=\"o\">=<\/span> <span class=\"k\">async<\/span> <span class=\"p\">(<\/span><span class=\"nx\">_request<\/span><span class=\"p\">,<\/span> <span class=\"nx\">_reply<\/span><span class=\"p\">,<\/span> <span class=\"nx\">payload<\/span><span class=\"p\">:<\/span> <span class=\"nx\">unknown<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"k\">typeof<\/span> <span class=\"nx\">payload<\/span><span class=\"o\">===<\/span><span class=\"dl\">'<\/span><span class=\"s1\">string<\/span><span class=\"dl\">'<\/span> <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">payload<\/span><span class=\"p\">.<\/span><span class=\"nx\">startsWith<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">{\"___jsx\":\"<\/span><span class=\"dl\">'<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"nx\">JSON<\/span><span class=\"p\">.<\/span><span class=\"nx\">parse<\/span><span class=\"p\">(<\/span><span class=\"nx\">payload<\/span><span class=\"p\">).<\/span><span class=\"nx\">___jsx<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">payload<\/span><span class=\"p\">;<\/span>\n\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>To use the hook:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">fastify<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">Fastify<\/span><span class=\"p\">();<\/span>\n\n<span class=\"c1\">\/\/ These 2 hooks allow us to render React\/jsx tags straight to HTML<\/span>\n<span class=\"nx\">fastify<\/span><span class=\"p\">.<\/span><span class=\"nx\">addHook<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">preSerialization<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">jsxRender<\/span><span class=\"p\">.<\/span><span class=\"nx\">preSerialization<\/span><span class=\"p\">);<\/span>\n<span class=\"nx\">fastify<\/span><span class=\"p\">.<\/span><span class=\"nx\">addHook<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">onSend<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">jsxRender<\/span><span class=\"p\">.<\/span><span class=\"nx\">onSend<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Using Preact or your own factory should also totally work here.<\/p>\n\n<h2 id=\"limitations-to-this-approach\">Limitations to this approach<\/h2>\n\n<p>Users of React and other frameworks may be used to pulling in data\nasynchronously and updating their document. This approach only allows\nfor a single pass, so this means that all dynamic data to your JSX\ntemplates needs to be fetched in advance and passed down as props.<\/p>\n\n<p>This means no hooks or state.<\/p>\n\n<p>To me this is an advantage because the paradigm it replaces is regular\ntemplates, and with those the data is typically also passed in \u2018at the\ntop\u2019.<\/p>\n\n<p>It would certainly be possible to implement Suspend and allow for\nasynchronous data fetching or wait for other signals, but I haven\u2019t\nyet needed this.<\/p>\n\n<h2 id=\"frequently-asked-questions\">Frequently asked questions<\/h2>\n\n<h3 id=\"how-do-you-handle-routing\">How do you handle routing?<\/h3>\n\n<p>The short answer is: you don\u2019t. Routing is already handled by your\nframework, and each route\/endpoint\/controller is responsible for rendering\nit\u2019s entire page.<\/p>\n\n<p>To reuse things like the global layout, you use components. A slightly\nfictionalized example of a page for us looks like this:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"p\">&lt;<\/span><span class=\"nc\">PublicLayout<\/span><span class=\"p\">&gt;<\/span>\n\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>Welcome back!<span class=\"p\">&lt;\/<\/span><span class=\"nt\">h1<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;\/<\/span><span class=\"nt\">div<\/span><span class=\"p\">&gt;<\/span>\n  <span class=\"p\">&lt;<\/span><span class=\"nt\">form<\/span> <span class=\"na\">method<\/span><span class=\"p\">=<\/span><span class=\"s\">\"post\"<\/span> <span class=\"na\">action<\/span><span class=\"p\">=<\/span><span class=\"s\">\"\/login\"<\/span><span class=\"p\">&gt;<\/span>\n\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">label<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span>Email<span class=\"p\">&lt;\/<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">input<\/span> <span class=\"na\">type<\/span><span class=\"p\">=<\/span><span class=\"s\">\"email\"<\/span> <span class=\"na\">name<\/span><span class=\"p\">=<\/span><span class=\"s\">\"email\"<\/span> <span class=\"na\">required<\/span> <span class=\"na\">minLength<\/span><span class=\"p\">=<\/span><span class=\"si\">{<\/span><span class=\"mi\">10<\/span><span class=\"si\">}<\/span> <span class=\"p\">\/&gt;<\/span>\n    <span class=\"p\">&lt;\/<\/span><span class=\"nt\">label<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">label<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span>Password<span class=\"p\">&lt;\/<\/span><span class=\"nt\">span<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">input<\/span> <span class=\"na\">type<\/span><span class=\"p\">=<\/span><span class=\"s\">\"password\"<\/span> <span class=\"na\">name<\/span><span class=\"p\">=<\/span><span class=\"s\">\"password\"<\/span> <span class=\"na\">required<\/span> <span class=\"na\">minLength<\/span><span class=\"p\">=<\/span><span class=\"si\">{<\/span><span class=\"mi\">4<\/span><span class=\"si\">}<\/span> <span class=\"p\">\/&gt;<\/span>\n    <span class=\"p\">&lt;\/<\/span><span class=\"nt\">label<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">button<\/span> <span class=\"na\">type<\/span><span class=\"p\">=<\/span><span class=\"s\">\"submit\"<\/span><span class=\"p\">&gt;<\/span>Log In<span class=\"p\">&lt;\/<\/span><span class=\"nt\">button<\/span><span class=\"p\">&gt;<\/span>\n\n  <span class=\"p\">&lt;\/<\/span><span class=\"nt\">form<\/span><span class=\"p\">&gt;<\/span>\n<span class=\"p\">&lt;\/<\/span><span class=\"nc\">PublicLayout<\/span><span class=\"p\">&gt;;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This in effect \u2018inherits\u2019 from <code class=\"language-plaintext highlighter-rouge\">PublicLayout<\/code>, which looks like this:<\/p>\n\n<div class=\"language-tsx highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">type<\/span> <span class=\"nx\">Props<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n  <span class=\"na\">children<\/span><span class=\"p\">:<\/span> <span class=\"nx\">JSX<\/span><span class=\"p\">.<\/span><span class=\"nx\">Element<\/span><span class=\"p\">[]<\/span><span class=\"o\">|<\/span><span class=\"nx\">JSX<\/span><span class=\"p\">.<\/span><span class=\"nx\">Element<\/span><span class=\"p\">;<\/span>\n  <span class=\"nl\">className<\/span><span class=\"p\">:<\/span> <span class=\"kr\">string<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"cm\">\/**\n * This is the main application wrapper, shared by all HTML pages.\n *\/<\/span>\n<span class=\"k\">export<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">PublicLayout<\/span><span class=\"p\">(<\/span><span class=\"nx\">props<\/span><span class=\"p\">:<\/span> <span class=\"nx\">Props<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"p\">&lt;<\/span><span class=\"nt\">html<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">head<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">title<\/span><span class=\"p\">&gt;<\/span>Sprout Family<span class=\"p\">&lt;\/<\/span><span class=\"nt\">title<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">link<\/span> <span class=\"na\">href<\/span><span class=\"p\">=<\/span><span class=\"s\">\"\/css\/main.css\"<\/span> <span class=\"na\">rel<\/span><span class=\"p\">=<\/span><span class=\"s\">\"stylesheet\"<\/span> <span class=\"na\">type<\/span><span class=\"p\">=<\/span><span class=\"s\">\"text\/css\"<\/span> <span class=\"p\">\/&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">meta<\/span> <span class=\"na\">name<\/span><span class=\"p\">=<\/span><span class=\"s\">\"viewport\"<\/span> <span class=\"na\">content<\/span><span class=\"p\">=<\/span><span class=\"s\">\"width=device-width, initial-scale=1\"<\/span> <span class=\"p\">\/&gt;<\/span>\n      <span class=\"p\">&lt;<\/span><span class=\"nt\">script<\/span> <span class=\"na\">src<\/span><span class=\"p\">=<\/span><span class=\"s\">\"\/js\/utils.js\"<\/span><span class=\"p\">&gt;&lt;\/<\/span><span class=\"nt\">script<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;\/<\/span><span class=\"nt\">head<\/span><span class=\"p\">&gt;<\/span>\n    <span class=\"p\">&lt;<\/span><span class=\"nt\">body<\/span> <span class=\"na\">className<\/span><span class=\"p\">=<\/span><span class=\"si\">{<\/span><span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">className<\/span><span class=\"si\">}<\/span><span class=\"p\">&gt;<\/span>\n      <span class=\"si\">{<\/span> <span class=\"nx\">props<\/span><span class=\"p\">.<\/span><span class=\"nx\">children<\/span> <span class=\"si\">}<\/span>\n    <span class=\"p\">&lt;\/<\/span><span class=\"nt\">body<\/span><span class=\"p\">&gt;<\/span>\n\n  <span class=\"p\">&lt;\/<\/span><span class=\"nt\">html<\/span><span class=\"p\">&gt;;<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"is-there-still-a-reason-to-use-handlebars-or-ejs\">Is there still a reason to use Handlebars or EJS?<\/h3>\n\n<p>I think one benefit of these template languages are they they are their own\nisolated format with limited capabilities.<\/p>\n\n<p>This is helpful when for example you build an application and let your own\nusers edit templates. Perhaps you for example have a \u2018welcome email\u2019 and want\nto let your users\/tenants modify it.<\/p>\n\n<p>Giving them the full power of a Javascript + a DSL that needs to be transpiled\nmight be a negative here, because limiting features makes it easier to evolve\nsystems and there\u2019s also a major security component.<\/p>\n\n<p>My template engine of choice for these is probably <a href=\"https:\/\/handlebarsjs.com\/\">handlebars<\/a> or even\nthe more limited variant <a href=\"https:\/\/mustache.github.io\/\">mustache<\/a>.<\/p>\n\n<h2 id=\"anyway\">Anyway<\/h2>\n\n<p>Hope this was interesting. If there\u2019s interest in turning my Koa and Fastify\ncode into real NPM packages, let me know! Happy to do it if there\u2019s a non-0\nnumber of users.<\/p>\n\n<p>In the future I might even be interested in developing my own JSX Factory that\nallows awaiting for async components.<\/p>\n\n<p>If any of this sounds interesting, you have a scathing critique or found\npunctuation in the wrong place, you can leave a comment by replying\nto this <a href=\"https:\/\/indieweb.social\/@evert\/111409207581858234\">Mastodon post<\/a>.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/on-80-percent-jobs\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/on-80-percent-jobs\/"}},"title":"Why aren't there more 80% jobs?","updated":"2023-10-12T20:29:12+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>There\u2019s been a bit of a trend recently for some companies to move to 4-day\nworkweeks. This is making a decent amount of noise, but the actual number of\ncompanies offering this still seems pretty few and far between. It\u2019s not hard\nto imagine why CEOs might feel this is risky, considering that many don\u2019t even\ntrust people to work from home.<\/p>\n\n<p>What I don\u2019t see much are 80% jobs, which are 32 hour jobs, at 80% salary.\nThis should be a low-risk proposition that I think a lot of people in the tech\nindustry would take, if it were an option.<\/p>\n\n<h2 id=\"my-background\">My background<\/h2>\n\n<p>I grew up in the Netherlands, but I moved to Canada for my first programmer\njob at age 20. I stayed for a few years, moved back and started a job there,\nand eventually moved <em>back<\/em> to Canada again and pretty much settled here.<\/p>\n\n<p>I got strong roots and professional experience in both places, but there was\none thing that surprised me most in the Netherlands that I haven\u2019t really seen\nin North America.<\/p>\n\n<p>Apparently Netherlands has a very high rate of part-time careers. Statistics\nsuggest that they are the highest in the world by a large margin. My personal\nexperience matches this too. I know a number of people in\nthe Netherlands that don\u2019t work full-time, and I think there are more opportunities\nfor part-time career jobs. I don\u2019t see them much in North America.<\/p>\n\n<iframe src=\"https:\/\/data.oecd.org\/chart\/7de9\" width=\"860\" height=\"645\" style=\"border: 0; width: 100%; max-width: 100%\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\" allowfullscreen=\"true\"><a href=\"https:\/\/data.oecd.org\/chart\/7de9\" target=\"_blank\">OECD Chart: Part-time employment rate, Total, % of employment, Annual, 2022<\/a><\/iframe>\n\n<p>My first-hand experience is when I was interviewing a few years into my\ncareer at a company named Ibuildings in Utrecht, Netherlands. After the\ninterview I was told I pretty much had the job, and then the interviewer\nasked me if I wanted to work 4 or 5 days per week.<\/p>\n\n<p>I only had Canadian work experience, so I was pretty shocked being asked\nthis. Picking 4 days seemed like a no-brainer to me.\nI would get paid for 32 hours per week, instead of 40 and got to pick a\nday of the week I wanted off (I picked Friday). At this company\nI believe most people took the 4-day option. So yes, I got paid 20% less,\nbut with the tax bracket I was in this was closer to 10%.<\/p>\n\n<p>For me it was great. 5 days actually feels like a lot, and 2 days in a\nweekend never feels quite enough. A sentiment so common it\u2019s boring talk\nabout it. I used the extra time to work on open source, errands, and picked\na few small freelance gigs here and there.<\/p>\n\n<p>I\u2019m leaning pretty socialist, but even with a capitalist hat on, I\u2019m\nsurprised this doesn\u2019t happen more. Why not give employees the option to\ndo this? Not everyone will take the option, but for those that do here\u2019s some\nadvantages:<\/p>\n\n<h2 id=\"advantages-of-offering-80-jobs\">Advantages of offering 80% jobs<\/h2>\n\n<ul>\n  <li>For the salary cost of 4 employees at 40 hours, you get 5 employees at 32\nhours.<\/li>\n  <li>Those 5 employees are more rested, and may be generally happier.<\/li>\n  <li>There\u2019s a lower risk of burn-out and attrition.<\/li>\n  <li>You get the combined experience of 5 people instead of 4.<\/li>\n  <li>Also I don\u2019t believe for the 80% workers, you only get 80% output. Most\npeople just aren\u2019t productive all the time.<\/li>\n  <li>Advertising this as a benefit in your company may also be attractive to\ncandidates for hiring.<\/li>\n<\/ul>\n\n<p>There are also some drawbacks. Every employee will have some overhead such as\na laptop, benefits and general admin. Probably a larger expense is the cost\nto recruit, although in the long run a lower attrition rate might make up for\nthat a bit.<\/p>\n\n<p>But my intuition tells me that this overhead is probably well worth the\nbenefit of a smarter, larger, happier workforce.<\/p>\n\n<p>Some notes:<\/p>\n\n<ul>\n  <li>I want to stress that working 4 days should be presented as a <em>choice<\/em>. Some\npeople prefer to work more for more money, and you don\u2019t want to scare them.<\/li>\n  <li>Another, even more lightweight option for irriationally risk-averse\ntraditionalists is to offer 90% jobs, resulting in an extra day every 2\nweeks.<\/li>\n<\/ul>\n"},{"id":"http:\/\/evertpot.com\/oauth2-usability\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/oauth2-usability\/"}},"title":"Does OAuth2 have a usability problem? (yes!)","updated":"2023-04-27T02:18:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I read an <a href=\"https:\/\/news.ycombinator.com\/item?id=35713518\">interesting thread<\/a> on Hackernews in response to a post:\n\u201cWhy is OAuth still hard in 2023\u201d. The post and comments bring up a lot\nof real issues with OAuth. The article ends with a pitch to\nuse the author\u2019s product <a href=\"https:\/\/www.nango.dev\/\">Nango<\/a> that advertises support\nfor supporting OAuth2 flows for 90+ APIs and justifying the existence\nof the product.<\/p>\n\n<p>We don\u2019t need 90 browsers to open 90 websites, so why\nis this the case with OAuth2? In a similar vain, the popular <a href=\"https:\/\/www.passportjs.org\/\">passport.js<\/a>\nproject has 538(!) modules for authenticating with various services,\nmost of which likely use OAuth2. All of these are NPM packages.<\/p>\n\n<p>Anyway, I\u2019ve been wanting to write this article for a while. It\u2019s not\na direct response to the Nango article, but it\u2019s a similar take with\na different solution.<\/p>\n\n<h2 id=\"my-perspective\">My perspective<\/h2>\n\n<p>I\u2019ve been working on an <a href=\"https:\/\/github.com\/curveball\/a12n-server\">OAuth2 server<\/a> for a few years now, and last\nyear I released an open source <a href=\"https:\/\/github.com\/badgateway\/oauth2-client\">OAuth2 client<\/a>.<\/p>\n\n<p>Since I released the client, I\u2019ve gotten several new features and requests\nthat were all contributed by users of the library, a few of note are:<\/p>\n\n<ul>\n  <li>Allowing <code class=\"language-plaintext highlighter-rouge\">client_id<\/code> and <code class=\"language-plaintext highlighter-rouge\">client_secret<\/code> to be sent in request bodies\ninstead of the <code class=\"language-plaintext highlighter-rouge\">Authorization<\/code> header.<\/li>\n  <li>Allow \u2018extra parameters\u2019 to be sent with some OAuth2 flows. Many servers,\nincluding Auth0 require these.<\/li>\n  <li>Allow users to add their own HTTP headers, for the same reason.<\/li>\n<\/ul>\n\n<p>What these have in common is that there\u2019s a lot of different OAuth2 servers\nthat want things in a slightly different\/specific way.<\/p>\n\n<p>I kind of expected this. It wasn\u2019t going to be enough to just implement\nOAuth2. This library will only work once people start trying it with different\nservers and run into mild incompatibilities that this library will have to\nadd workarounds for.<\/p>\n\n<p>Although I think OAuth2 is pretty well defined, the full breadth of specs and\nimplementations makes it so that it\u2019s not enough to (as an API developer) to just\ntell your users: \u201cWe use OAuth2\u201d.<\/p>\n\n<p>For the typical case, you might have to tell them something like this:<\/p>\n\n<ul>\n  <li>We use OAuth2.<\/li>\n  <li>We use the <code class=\"language-plaintext highlighter-rouge\">authorization_code<\/code> flow.<\/li>\n  <li>Your <code class=\"language-plaintext highlighter-rouge\">client_id<\/code> is X.<\/li>\n  <li>Our \u2018token endpoint\u2019 is Y.<\/li>\n  <li>Our \u2018authorization endpoint\u2019 is Z.<\/li>\n  <li>We require PKCE.<\/li>\n  <li>Requests to the \u201ctoken\u201d endpoint require credentials to be sent in a body.<\/li>\n  <li>Any custom non-standard extensions.<\/li>\n<\/ul>\n\n<p>To some extent this is by design. The OAuth2 spec calls itself: \u201cThe OAuth 2.0\nAuthorization Framework\u201d. It\u2019s not saying it is <em>the<\/em> protocol, but rather it\u2019s\na set of really good building blocks to implement your own authentication.<\/p>\n\n<p>But for users that want to use generic OAuth2 tooling this is not ideal.\nNot only because of the amount of information that needs to be shared, but also\nit requires users of your API to be familiar with all these terms.<\/p>\n\n<p>A side-effect of this is that API vendors that use OAuth2 will be more likely\nroll their own SDKs, so they can insulate users from these implementation details.\nIt also creates a market for products like Nango and Passport.js.<\/p>\n\n<p>Another result is that I see <em>many<\/em> people invent their own authentication flows\nwith JWT and refresh tokens from scratch, even though OAuth2 would be good fit.\nMost people only need a small part of OAuth2, but to understand <em>which<\/em> small\npart you need you\u2019ll need to wade through and understand a dozen IETF RFC\ndocuments, some of wich are still drafts.<\/p>\n\n<p><em>Sidenote: <a href=\"https:\/\/openid.net\/connect\/\">OpenID Connect<\/a> is another dimension on top of this. OpenID Connect builds on\nOAuth2 and adds many features and another set of dense technical specs that are\n(in my opinion) even harder to read.<\/em><\/p>\n\n<p>OAuth2 as a framework is really good and very successful. But it\u2019s not as good\nat being a generic protocol that people can write generic code for.<\/p>\n\n<h2 id=\"solving-the-setup-issue\">Solving the setup issue<\/h2>\n\n<p>There\u2019s a nice OAuth2 feature called \u201cOAuth 2.0 Authorization Server Metadata\u201d,\ndefined in <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc8414\">RFC8414<\/a>. This is a JSON document sitting at a predictable URL:\n<code class=\"language-plaintext highlighter-rouge\">https:\/\/your-server\/.well-known\/oauth-authorization-server<\/code>, and can tell\nclients:<\/p>\n\n<ul>\n  <li>Which flows and features are supported<\/li>\n  <li>How to pass credentials<\/li>\n  <li>URLs to every endpoint.<\/li>\n<\/ul>\n\n<p>Here\u2019s an example from my server:<\/p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n\n    <\/span><span class=\"nl\">\"issuer\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"http:\/\/localhost:8531\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"authorization_endpoint\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/authorize\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"token_endpoint\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/token\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"token_endpoint_auth_methods_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"client_secret_basic\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"token_endpoint_auth_signing_alg_values_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"RS256\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"jwks_uri\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"http:\/\/localhost:8531\/.well-known\/jwks.json\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"scopes_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"openid\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"response_types_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"token\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"code\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"code id_token\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"grant_types_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"client_credentials\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"authorization_code\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"refresh_token\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"id_token_signing_alg_values_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"RS256\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"service_documentation\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"http:\/\/localhost:8531\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"ui_locales_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"en\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">],<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"introspection_endpoint\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/introspect\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"revocation_endpoint\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/revoke\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"revocation_endpoint_auth_methods_supported\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n        <\/span><span class=\"s2\">\"client_secret_basic\"<\/span><span class=\"w\">\n    <\/span><span class=\"p\">]<\/span><span class=\"w\">\n\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>If your server and client supports this, it can simplify the setup a great\ndeal. Here\u2019s an example using my client:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">OAuth2Client<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@badgateway\/oauth2-client<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">client<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">OAuth2Client<\/span><span class=\"p\">({<\/span>\n  <span class=\"na\">server<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">https:\/\/my-auth-server\/<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"na\">clientId<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">my-client-id<\/span><span class=\"dl\">'<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The problem is majority of OAuth2 servers and clients don\u2019t support this,\nso even if your server did, your setup instructions would still need to\ninclude all the \u2018setup info\u2019 for clients that don\u2019t support the discovery\ndocument.<\/p>\n\n<p>And for the clients that <em>do<\/em> support it, you would need to call out that\nyour users\u2019 client needs support for RFC8414.<\/p>\n\n<p>So while I think the discovery spec is great and solves real problems;\nit alone cannot solve the OAuth2 usability problem.<\/p>\n\n<h2 id=\"my-proposal\">My proposal<\/h2>\n\n<p>Currently work is underway to define <a href=\"https:\/\/www.ietf.org\/archive\/id\/draft-ietf-oauth-v2-1-08.html\">OAuth 2.1<\/a>. OAuth 2.1 will remove\nfeatures that are considered insecure or bad practices (such as <code class=\"language-plaintext highlighter-rouge\">implicit<\/code>\nand <code class=\"language-plaintext highlighter-rouge\">password<\/code> flows) and PKCE is brought in as a core feature.<\/p>\n\n<p>If you\u2019re a OAuth 2 client or server maintainer and kept up with the specs,\nyou likely are already compatible with OAuth 2.1.<\/p>\n\n<p>I don\u2019t think OAuth 2.1 goes far enough.<\/p>\n\n<p>I think that this proposal should <em>require<\/em> support for the discovery document\nand make it a required step to find features and endpoints. I also think\nit should have an opinion on how clients should support custom extensions and\nhow they might work. (or forbid them).<\/p>\n\n<p>This version of OAuth should also provide a way to discover the discovery\nendpoint (hear me out).<\/p>\n\n<p>If a client makes a HTTP request to an API, and the API replies with <code class=\"language-plaintext highlighter-rouge\">401<\/code>, it\nshould tell the client where to find the discovery document and which\nflow(s) to use.<\/p>\n\n<p>Lastly, I think that it should be renamed to OAuth 3, so API vendors no longer\nhave to state:<\/p>\n\n<ul>\n  <li>We use OAuth 2.<\/li>\n  <li>We use the <code class=\"language-plaintext highlighter-rouge\">authorization_code<\/code> flow.<\/li>\n  <li>Your <code class=\"language-plaintext highlighter-rouge\">client_id<\/code> is X.<\/li>\n  <li>Our \u2018token endpoint\u2019 is Y.<\/li>\n  <li>Our \u2018authorization endpoint\u2019 is Z.<\/li>\n  <li>We require PKCE.<\/li>\n  <li>Requests to the \u201ctoken\u201d endpoint require credentials to be sent in a body.<\/li>\n  <li>Any custom non-standard extensions.<\/li>\n<\/ul>\n\n<p>But instead:<\/p>\n\n<ul>\n  <li>We use OAuth 3.<\/li>\n  <li>Your <code class=\"language-plaintext highlighter-rouge\">client_id<\/code> is X.<\/li>\n<\/ul>\n\n<p>A nice aspect of this proposal is that OAuth 2 clients can still talk to\nOAuth 3 servers, it also doesn\u2019t obsolete the OAuth 2 framework.<\/p>\n\n<p>Perhaps you could name this \u201cOAuth 2.1: the good parts\u201d, but I think increasing\nthe major version number sends a clear signal to users they should be looking\nfor a OAuth 3 library.<\/p>\n\n<p>Then <em>maybe<\/em>, 10 years from now we no longer need 538 Passport.js modules for\n538 APIs. Perhaps browsers could even facilitate authentication.<\/p>\n\n<h2 id=\"final-notes\">Final notes<\/h2>\n\n<ul>\n  <li>A future OAuth version should also explicitly allow <code class=\"language-plaintext highlighter-rouge\">http:\/\/localhost<\/code> as\na <code class=\"language-plaintext highlighter-rouge\">redirect_url<\/code>. We need to be able to test.<\/li>\n  <li>I\u2019m aware that there was a OAuth 3 proposal, which is now called <a href=\"https:\/\/openid.net\/connect\/\">XYZ (or\nmaybe GNAP?)<\/a>. I\u2019m not very familiar with this proposed protocol.<\/li>\n  <li><a href=\"https:\/\/xkcd.com\/927\/\">XKCD 927<\/a> is funny, but ultimately a conversation stopper and a bit\ncynical.<\/li>\n<\/ul>\n\n"},{"id":"http:\/\/evertpot.com\/switching-to-fedora\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/switching-to-fedora\/"}},"title":"Switching to Fedora from Ubuntu","updated":"2023-03-30T16:00:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>It seems like every 7-8 years I\u2019m switching operating systems. In 2006 I first\nstarted using Apple, because it was just so damn cool to have a well working\nUnix-like system. (I\u2019ll never forget you Snow Leopard).<\/p>\n\n<p>In 2015 I switched to Ubuntu. Apple\u2019s Software seemed to hit rock bottom at\nthis point from a quality perspective, and hardware seemed to go obsolete at\na rate I hadn\u2019t seen before. Fed up paying a premium price for a sub-par\nproduct, it was <a href=\"https:\/\/evertpot.com\/switching-to-linux\/\">time for a change<\/a>.<\/p>\n\n<p><img style=\"float: left; max-width: 25%; padding: 0 10px 10px 0\" alt=\"Ubuntu's logo deterioated\" src=\"\/assets\/posts\/fedora\/ubuntu-fried.png\" \/><\/p>\n\n<h2 id=\"ubuntus-fall\">Ubuntu\u2019s fall<\/h2>\n\n<p>Ubuntu was the obvious choice. I want something that just works, and <a href=\"https:\/\/www.dell.com\/community\/Developer-Blogs\/Dell-XPS-13-Plus-developer-edition-with-Ubuntu-22-04-LTS-pre\/ba-p\/8255332\">Dell\u2019s\nXPS 13 Developer Edition<\/a> ships with Ubuntu which means hardware support\nfrom Dell itself. Breath of fresh air and fast as hell. The experience was\nsimilar to what people have been saying about the new M1 chips.\nBut it\u2019s fast because of software, not hardware.<\/p>\n\n<p>But something changed with Ubuntu in recent years. I think linux users have\na thicker than usual skin when it comes to software issues and are willing\nto look past more things. But Ubuntu\u2019s quality has been consistently falling.\nI was being worn down, and it seems to be a common sentiment in my bubble.<\/p>\n\n<p>The best example is Ubuntu\u2019s package manager Snap. A pretty good idea, I like\nthe branding too but the execution hasn\u2019t been there. Ubuntu users have been\neffectively beta-testing this for years. Just to give you an idea I made a\n<a href=\"https:\/\/evertpot.com\/firefox-ubuntu-snap\/\">giant list of bugs<\/a> that I ran into when Ubuntu switched Firefox from apt\nto Snap.<\/p>\n\n<p>To be honest I feel a bit bad ragging on Ubuntu, because without knowing\n<em>anything<\/em> about how the project and Canonical is run, my intuition is\nthat the steam is just kind of running out for them. Ubuntu feels \u2018depressed\u2019,\nbut maybe it\u2019s all in my head.<\/p>\n\n<p><img style=\"float: right; max-width: 25%; padding: 0 0 0 20px\" alt=\"Fedora logo\" src=\"\/assets\/posts\/fedora\/fedora.svg\" \/><\/p>\n\n<h2 id=\"onto-fedora\">Onto Fedora<\/h2>\n\n<p>So I pledged to try something new in 2022, and it took me another 14 months\nto find the energy and motivation to actually follow through.<\/p>\n\n<p>I went for Fedora. Named after a fashion faux pas, it seems to be on the #2\nspot for desktop linux. It\u2019s funded by Red Hat, and seems to have a focus on\nkeeping it\u2019s packages very up to date, which I like and why I originally\nswitched from Debian to Ubuntu!<\/p>\n\n<p>I\u2019m a week and a bit in, and here are my first impressions.<\/p>\n\n<h3 id=\"installation\">Installation<\/h3>\n\n<p>Installation was super smooth. I always forget to make a separate <code class=\"language-plaintext highlighter-rouge\">\/home<\/code>\nmount, so it took a while to move everything to an external disk and back.<\/p>\n\n<p>The <em>one<\/em> thing I always forget to move is my MySQL databases, and today\nwas no exception.<\/p>\n\n<h3 id=\"non-free-stuff\">Non-free stuff<\/h3>\n\n<p>Fedora does not ship with things that aren\u2019t open source. Nothing against that\nphilosophy (awesome in fact), but personally I don\u2019t mind adding some binaries\nfor a better experience.<\/p>\n\n<p>I miss Ubuntu\u2019s <a href=\"https:\/\/askubuntu.com\/questions\/47506\/how-do-i-install-additional-drivers\">Additional Drivers<\/a> tool, because it told me what to\ninstall. I\u2019m sure the drivers I need are available for Fedora, but I don\u2019t\nknow what to look for which makes me slightly worried my computer is not\nrunning optimally. Battery feels worse but that could also be my imagination.<\/p>\n\n<p>Video in Firefox didn\u2019t work at all in stock Fedora. I had to install\nffmpeg to get it to barely function, but then I discovered <a href=\"https:\/\/rpmfusion.org\/\">RPM Fusion<\/a>, where\nI got an even better ffmpeg, plus gstreamer and Intel drivers and I can now watch\nbeautiful smooth 4K video, and confirmed with <code class=\"language-plaintext highlighter-rouge\">intel_gpu_top<\/code> that I\u2019m using\nhardware acceleration.<\/p>\n\n<p><a href=\"\/assets\/posts\/fedora\/gpu-top.png\"><img class=\"fill-width\" alt=\"intel_gpu_top output\" src=\"\/assets\/posts\/fedora\/gpu-top.png\" \/><\/a><\/p>\n\n<h3 id=\"gnome\">Gnome<\/h3>\n\n<p>Ubuntu used to have their own desktop environment called Unity. In\n2018 they switched to Gnome, but they modified Gnome to keep their\nUnity look.<\/p>\n\n<p><a href=\"\/assets\/posts\/fedora\/ubuntu.png\"><img class=\"fill-width\" alt=\"Ubuntu 22.10 look\" src=\"\/assets\/posts\/fedora\/ubuntu.png\" \/><\/a><\/p>\n\n<p>This felt like a good move, because it let them kept their look while\ntaking advantage of all the Gnome plumbing.<\/p>\n\n<p>One drawback is that Ubuntu was usually a bit behind with Gnome\nfeatures.<\/p>\n\n<p>Fedora uses stock Gnome. As a result there\u2019s more consistency overall,\nand it\u2019s nice to have the latest features. I miss the strong visual\nidentity Ubuntu has though. It sets itself apart from Mac and Windows.<\/p>\n\n<p><a href=\"\/assets\/posts\/fedora\/fedora.png\"><img class=\"fill-width\" alt=\"Gnome's neutral colors\" src=\"\/assets\/posts\/fedora\/fedora.png\" \/><\/a><\/p>\n\n<p>I\u2019m sure I can customize Fedora to be more fun, but if I\u2019m being real\nwith myself I haven\u2019t changed my desktop background in 10 years,\nand so this will never happen.<\/p>\n\n<h3 id=\"flatpak\">Flatpak<\/h3>\n\n<p>The only thing that Flatpak had to do to be better than Snap\nwas to not remind me of its existence unless I\u2019m installing something, and\nso far it\u2019s done that. <a href=\"https:\/\/flathub.org\/home\">Flathub<\/a> is nice, and I love the idea of developers\nnot having to repackage for every distro under the sun.<\/p>\n\n<h3 id=\"bugginess\">Bugginess<\/h3>\n\n<p>Fedora does generally feel more stable. Fewer background processes\npegged at 100%. 3rd party application support doesn\u2019t seem as good on\nfirst sight. I had to find workarounds for <a href=\"https:\/\/keepassxc.org\/\">KeeppassXC<\/a> and\n<a href=\"https:\/\/flathub.org\/apps\/details\/im.riot.Riot\">Element<\/a> to not crash all the time.<\/p>\n\n<p>This is no fault of the Fedora team, but it does tell you something\nabout how much Fedora is on other people\u2019s radars. If developers test\n1 linux distro, it will be Ubuntu.<\/p>\n\n<h3 id=\"software-center-app\">Software Center app<\/h3>\n\n<p>I falsely assumed that the Software Center application was buggy\ndue to Ubuntu\u2019s Snap changes, but it also hangs all the time in Fedora.<\/p>\n\n<p>The solution is to kill the <code class=\"language-plaintext highlighter-rouge\">gnome-software<\/code> process if you want\nto use \u2018Software\u2019 more than once per session. Apparently this has\nbeen an issue for <a href=\"https:\/\/www.reddit.com\/r\/Fedora\/comments\/tt04l1\/hows_software_still_having_issues_like_these\/\">at least 12 months<\/a><\/p>\n\n<h2 id=\"would-i-recommend-fedora\">Would I recommend Fedora?<\/h2>\n\n<p>Things are never perfect, but even with some of the issues I ran into\nI\u2019m very happy I switched. It\u2019s hard to describe, but things feel more\nsolid.<\/p>\n\n<p>To get to this state I did have to put in some work and research, but not\nat a level of recompiling kernels.<\/p>\n\n<p>If you are comfortable googling, using the terminal for some commands\nand interpreting an occasional error message I would now recommend Fedora\nover Ubuntu.<\/p>\n\n<p>If you are not a technical user or programmer-adjacent, I think Ubuntu\nis still going to be the choice with the least amount of friction. In\nlarge I think this is simply because it is the biggest, has the most\neyes and the most support.<\/p>\n\n<p>I\u2019m excited though. Fresh start!<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/universal-commonjs-esm-typescript-packages\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/universal-commonjs-esm-typescript-packages\/"}},"title":"Supporting CommonJS and ESM with Typescript and Node","updated":"2023-03-21T17:11:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I maintain a few dozen Javascript libraries, and recently updated many of them\nto support CommonJS and Ecmascript modules at the same time.<\/p>\n\n<p>The first half of this article describes why and how I did it, and then all the\nhoops I had to jump through to make things work.<\/p>\n\n<p>Hopefully it\u2019s a helpful document for the next challenger.<\/p>\n\n<p>A quick refresher, CommonJS code typically looks like this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">MyApp<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">require<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">.\/my-app<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n<span class=\"nx\">module<\/span><span class=\"p\">.<\/span><span class=\"nx\">exports<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span><span class=\"na\">foo<\/span><span class=\"p\">:<\/span> <span class=\"mi\">5<\/span><span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>And ESM like this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">MyApp<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/my-app.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">export<\/span> <span class=\"k\">default<\/span> <span class=\"p\">{<\/span><span class=\"na\">foo<\/span><span class=\"p\">:<\/span> <span class=\"mi\">5<\/span><span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Except if you use Typescript! Most Typescript uses ESM syntax, but actually\nbuilds to CommonJS. So if your Typescript code looks like the second and\nthink \u2018I\u2019m good\u2019, make sure you also take a look at the built Javascript\ncode to see what you actually use.<\/p>\n\n<h2 id=\"why-support-esm\">Why support ESM<\/h2>\n\n<p>The general vibe is that ESM is going to be the future of Javascript code.\nEven though I don\u2019t think the developer experience is quite there yet, but\nmore people will start trying ESM.<\/p>\n\n<p>If you decided to plunge in with ESM, I want my libraries to feel first-class.<\/p>\n\n<p>For example, I\u2019d want you to be able to default-import:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">Controller<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@curveball\/controller<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>At the same time most people are still on CommonJS, and I want to continue\nto support this without breaking backwards compatibility for the forseeable\nfuture.<\/p>\n\n<h2 id=\"the-general-approach\">The general approach<\/h2>\n\n<p>My goal is for my packages to \u2018default\u2019 to ESM. From the perspective of a\nlibrary maintainer you will be dealing with ESM.<\/p>\n\n<p>During the build phase steps are being made to generate a secondary CommonJS\nbuild. As a maintainer you might run into CommonJS-specific issues, but\nI suspect people will only see those if the CI system reports an issue.<\/p>\n\n<p>Our published NPM packages will have a directory structure roughly like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>- package.json\n- tsconfig.json\n\n- src\/     # Typescript source\n- cjs\/     # CommonJS\n- esm\/     # ESM\n- test\/\n<\/code><\/pre><\/div><\/div>\n\n<p>We include the original typescript sources to make step-through debugging work\nwell, and have a seperate directory for the ESM and CommonJS builds.<\/p>\n\n<p>If you just want to skip to the end and see an example of a package that has\nall this work done, check out my <code class=\"language-plaintext highlighter-rouge\">@curveball\/core<\/code> package:<\/p>\n\n<ul>\n  <li>Github source: <a href=\"https:\/\/github.com\/curveball\/core\">https:\/\/github.com\/curveball\/core<\/a><\/li>\n  <li>Published NPM package contents: <a href=\"https:\/\/www.npmjs.com\/package\/@curveball\/core?activeTab=code\">https:\/\/www.npmjs.com\/package\/@curveball\/core?activeTab=code<\/a><\/li>\n<\/ul>\n\n<h2 id=\"typescript-features-we-dont-use\">Typescript features we don\u2019t use<\/h2>\n\n<h3 id=\"esmoduleinterop\">esModuleInterop<\/h3>\n\n<p>We don\u2019t use the <code class=\"language-plaintext highlighter-rouge\">esModuleInterop<\/code> setting in <code class=\"language-plaintext highlighter-rouge\">tsconfig.json<\/code>. This flag\nlets you default-import non-ESM packages like this:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">path<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:path<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>instead of the more awkward:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">path<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:path<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>On the surface <code class=\"language-plaintext highlighter-rouge\">esModuleInterop<\/code> seems like it would be helpful, but it\nhas a major problem: if our libraries <em>use<\/em> <code class=\"language-plaintext highlighter-rouge\">esModuleInterop<\/code>, anyone\nwho uses that  library is forced to also turn it on.<\/p>\n\n<p>So turning by turning <code class=\"language-plaintext highlighter-rouge\">esModuleInterop<\/code> off we don\u2019t force anyone\ndownstream into a specific setting. I generally recommend anyone writing\nlibraries to turn this off to reduce friction and ironically increase\ninteropability.<\/p>\n\n<h3 id=\"path-mapping\">Path mapping<\/h3>\n\n<p>Typescript lets you map arbitrary paths to specific directories in your\nsource. This is popular because it lets you do things like:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">MyController<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@controllers\/my<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Instead of being forced to always do relative import<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">MyController<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">..\/..\/..\/controllers\/my<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is quite a popular feature for larger code-bases because people find\nrelative paths annoying.<\/p>\n\n<p>I\u2019m also not using this feature. A big issue is that only Typescript\nis aware of this configuration, so any other tooling that uses your files\nmay need to be configured separately to also understand this, which\ndoesn\u2019t always work.<\/p>\n\n<p>So while I don\u2019t have an exact list of exact reasons or configurations\nwhere this fails, it\u2019s a common enough issue that I\u2019ve decided to just\navoid this feature altogether, for the sake of reducing magic. If you\ndo insist on using path mapping, you\u2019re on your own.<\/p>\n\n<h2 id=\"configuring-packagejson\">Configuring package.json<\/h2>\n\n<p>Modern node versions now have <a href=\"https:\/\/nodejs.org\/api\/packages.html#exports\">a syntax<\/a> in <code class=\"language-plaintext highlighter-rouge\">package.json<\/code> that lets you\nspecify both the default CommonJS and ESM files. These are the relevant\nlines of one of our packages:<\/p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"@curveball\/controller\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"0.5.0\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"description\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"A simple controller pattern for Curveball.js\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"module\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"exports\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"require\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\".\/cjs\/index.js\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"import\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\".\/esm\/index.js\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">},<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"main\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"cjs\/index.js\"<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>When specifying your <code class=\"language-plaintext highlighter-rouge\">package.json<\/code> this way, Node will automatically load\nthe correct (cjs\/esm) directory depending on what the user needs. This all\nhappens automatically. Neat!<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">main<\/code> is no longer used, but I\u2019m keeping it in case of older tooling.<\/p>\n\n<h2 id=\"building-with-typescript\">Building with Typescript<\/h2>\n\n<p>Before we made this change, we just had a <code class=\"language-plaintext highlighter-rouge\">src\/<\/code> and <code class=\"language-plaintext highlighter-rouge\">dist\/<\/code> directory\nfor Typescript and Javascript files respectively. To do the build, we\ncould just run <code class=\"language-plaintext highlighter-rouge\">npx tsc<\/code>.<\/p>\n\n<p>This still works, except <code class=\"language-plaintext highlighter-rouge\">npx tsc<\/code> now builds to <code class=\"language-plaintext highlighter-rouge\">esm<\/code> for the regular\ndeveloper flow.<\/p>\n\n<p>But when we build for creating the NPM package, we need to create both.\nWe use a <code class=\"language-plaintext highlighter-rouge\">Makefile<\/code> for this, but these are roughly the commands to run:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>npx tsc <span class=\"nt\">--module<\/span> commonjs <span class=\"nt\">--outDir<\/span> cjs\/\n<span class=\"nb\">echo<\/span> <span class=\"s1\">'{\"type\": \"commonjs\"}'<\/span> <span class=\"o\">&gt;<\/span> cjs\/package.json\n\nnpx tsc <span class=\"nt\">--module<\/span> es2022 <span class=\"nt\">--outDir<\/span> esm\/\n<span class=\"nb\">echo<\/span> <span class=\"s1\">'{\"type\": \"module\"}'<\/span> <span class=\"o\">&gt;<\/span> esm\/package.json\n<\/code><\/pre><\/div><\/div>\n\n<p>As you can also see that both our <code class=\"language-plaintext highlighter-rouge\">cjs<\/code> and <code class=\"language-plaintext highlighter-rouge\">esm<\/code> packages get a 1-line\n<code class=\"language-plaintext highlighter-rouge\">package.json<\/code> file with a <code class=\"language-plaintext highlighter-rouge\">type<\/code> property.<\/p>\n\n<p>This is <em>required<\/em> because Node needs to know wether files with a <code class=\"language-plaintext highlighter-rouge\">.js<\/code>\nextensions should be interpreted as CommonJS or ESM. It can\u2019t figure it\nout after opening it.<\/p>\n\n<p>Node will look at the nearest <code class=\"language-plaintext highlighter-rouge\">package.json<\/code> to see if the <code class=\"language-plaintext highlighter-rouge\">type<\/code> property\nwas specified.<\/p>\n\n<p>It\u2019s also possible to use <code class=\"language-plaintext highlighter-rouge\">.cjs<\/code> or <code class=\"language-plaintext highlighter-rouge\">.mjs<\/code> file extensions, but this\ndoesn\u2019t play well with Typescript as Typescript can\u2019t automatically adjust\nimport paths for you.<\/p>\n\n<h2 id=\"changes-we-had-to-make-in-our-code\">Changes we had to make in our code<\/h2>\n\n<p>I think it\u2019s reasonable to say that that vanilla javascript and\njavascript modules are slightly different programming languages<\/p>\n\n<p>They are interopable, but not the same. They have slightly different\nsyntax and behavior. This is why you also need to tell HTML in advance\nwhat kind of flavour of Javascript you\u2019re going to be using:<\/p>\n\n<div class=\"language-html highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nt\">&lt;script <\/span><span class=\"na\">type=<\/span><span class=\"s\">\"module\"<\/span> <span class=\"na\">src=<\/span><span class=\"s\">\"my-module.mjs\"<\/span><span class=\"nt\">&gt;&lt;\/script&gt;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>So naturally I expected to have to make some changes to write code in\na way that works identical in both targets.<\/p>\n\n<p>An obvious example is top-level <code class=\"language-plaintext highlighter-rouge\">await<\/code> which only works in ESM. Here\nare all the things I ran into:<\/p>\n\n<h3 id=\"extensions-in-imports\">Extensions in imports<\/h3>\n\n<p>All our (local) typescript imports had to change from:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">Foo<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/foo<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>to:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">Foo<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/foo.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>ESM <em>requires<\/em> extensions, whereas in CommonJS it\u2019s not needed. Here\u2019s a <code class=\"language-plaintext highlighter-rouge\">sed<\/code>\ncommand I used to change all these in bulk (probably not perfect, so you\u2019ll\nreally want to be able to roll this back):<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>find <span class=\"nb\">.<\/span> <span class=\"nt\">-name<\/span> <span class=\"s2\">\"*.ts\"<\/span> | xargs <span class=\"nt\">-n1<\/span> <span class=\"nb\">sed<\/span> <span class=\"nt\">-i<\/span> <span class=\"s2\">\"s\/from '<\/span><span class=\"se\">\\(<\/span><span class=\"s2\">.*<\/span><span class=\"se\">\\)<\/span><span class=\"s2\">';\/from '<\/span><span class=\"se\">\\\\<\/span><span class=\"s2\">1.js'\/g\"<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>So the strange thing with all this is that your code will have statements\nlike:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"nx\">Foo<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/foo.js<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>But actually the file in that directory is called <code class=\"language-plaintext highlighter-rouge\">.\/foo.ts<\/code> (<code class=\"language-plaintext highlighter-rouge\">.ts<\/code> not <code class=\"language-plaintext highlighter-rouge\">.js<\/code>)\nyes, this is confusing and annoying.<\/p>\n\n<p>I don\u2019t really want a future contributor of my library to have to understand\nthe build chain, so it\u2019s a leaky abstraction and a limitation of Typescript.<\/p>\n\n<p>I hope this is rectified in the future. I understand the philosophy of wanting\nto avoid magic as much as possible, but <em>just<\/em> give me a setting to globally\nrewrite a <code class=\"language-plaintext highlighter-rouge\">.ts<\/code> to <code class=\"language-plaintext highlighter-rouge\">.js<\/code> when generating the <code class=\"language-plaintext highlighter-rouge\">.js<\/code> files.<\/p>\n\n<p>I secretly suspect that despite Typescript explicitly not supporting this,\nthis might still happen later anyway. Maybe they don\u2019t want to commit to\nbuilding this before they have a good plan how \ud83e\udd1e.<\/p>\n\n<h3 id=\"directory-imports\">Directory imports<\/h3>\n\n<p>In CommonJS in node you can import a directory, and if a <code class=\"language-plaintext highlighter-rouge\">index.js<\/code> exists\nin that directory, it will open that. ESM requires exact paths, so your<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/controllers<\/span><span class=\"dl\">'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Now has to be:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/controllers\/index.js<\/span><span class=\"dl\">'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h3 id=\"__dirname-and-__filename\"><code class=\"language-plaintext highlighter-rouge\">__dirname<\/code> and <code class=\"language-plaintext highlighter-rouge\">__filename<\/code><\/h3>\n\n<p>Node defines 2 useful very variables in every CommonJS file, probably borrowed\nfrom PHP.<\/p>\n\n<p><code class=\"language-plaintext highlighter-rouge\">__dirname<\/code> is a reference to the path the <em>current<\/em> file is in, and\n<code class=\"language-plaintext highlighter-rouge\">__filename<\/code> is the whole path + filename.<\/p>\n\n<p>I used these to load in non-javascript assets from relative paths. For\nexample, I had a snippet like this to get the version of the current\npackage:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">fs<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:fs<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">pkg<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">JSON<\/span><span class=\"p\">.<\/span><span class=\"nx\">parse<\/span><span class=\"p\">(<\/span>\n  <span class=\"nx\">fs<\/span><span class=\"p\">.<\/span><span class=\"nx\">readFileSync<\/span><span class=\"p\">(<\/span>\n     <span class=\"nx\">__dirname<\/span> <span class=\"o\">+<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">..\/package.json<\/span><span class=\"dl\">'<\/span>\n  <span class=\"p\">)<\/span>\n<span class=\"p\">);<\/span>\n\n<span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">.<\/span><span class=\"nx\">version<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Unfortunately, these variables no longer exist in ESM. Instead we have\n<code class=\"language-plaintext highlighter-rouge\">import.meta<\/code>. In Node, this is an object looking like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>{\n  url: 'file:\/\/\/home\/evert\/src\/curveball\/controller\/test.mjs'\n}\n<\/code><\/pre><\/div><\/div>\n\n<p>So instead of a filesystem path, we get a <code class=\"language-plaintext highlighter-rouge\">file:\/\/<\/code> url with the location. So to\nwrite the equivalent in ESM we get something like this:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">fs<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:fs<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">url<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:url<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">pkg<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">JSON<\/span><span class=\"p\">.<\/span><span class=\"nx\">parse<\/span><span class=\"p\">(<\/span>\n  <span class=\"k\">await<\/span> <span class=\"nx\">fs<\/span><span class=\"p\">.<\/span><span class=\"nx\">readFile<\/span><span class=\"p\">(<\/span>\n    <span class=\"nx\">url<\/span><span class=\"p\">.<\/span><span class=\"nx\">fileURLToPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">url<\/span><span class=\"p\">.<\/span><span class=\"nx\">resolve<\/span><span class=\"p\">(<\/span><span class=\"k\">import<\/span><span class=\"p\">.<\/span><span class=\"nx\">meta<\/span><span class=\"p\">.<\/span><span class=\"nx\">url<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.\/package.json<\/span><span class=\"dl\">'<\/span><span class=\"p\">)),<\/span>\n    <span class=\"dl\">'<\/span><span class=\"s1\">utf-8<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">)<\/span>\n<span class=\"p\">);<\/span>\n\n<span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">pkg<\/span><span class=\"p\">.<\/span><span class=\"nx\">version<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The challenge is writing this code in a way that will work with both. The\n\u2018crazy\u2019 way to solve this is to take an <code class=\"language-plaintext highlighter-rouge\">Error<\/code> object, and get the path\nfrom the string that appears in the stack trace. This means parsing the\nstacktrace :(<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">url<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:url<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">path<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:path<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">function<\/span> <span class=\"nx\">getDirName<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">err<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nb\">Error<\/span><span class=\"p\">()<\/span>\n  <span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">err<\/span><span class=\"p\">.<\/span><span class=\"nx\">stack<\/span><span class=\"p\">?.<\/span><span class=\"nx\">split<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"se\">\\n<\/span><span class=\"dl\">'<\/span><span class=\"p\">)[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]);<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">fileUrl<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">err<\/span><span class=\"p\">.<\/span><span class=\"nx\">stack<\/span><span class=\"p\">?.<\/span><span class=\"nx\">split<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"se\">\\n<\/span><span class=\"dl\">'<\/span><span class=\"p\">)[<\/span><span class=\"mi\">1<\/span><span class=\"p\">].<\/span><span class=\"nx\">match<\/span><span class=\"p\">(<\/span><span class=\"sr\">\/at .* <\/span><span class=\"se\">\\((<\/span><span class=\"sr\">file:.*<\/span><span class=\"se\">)<\/span><span class=\"sr\">:<\/span><span class=\"se\">\\d<\/span><span class=\"sr\">+:<\/span><span class=\"se\">\\d<\/span><span class=\"sr\">+<\/span><span class=\"se\">\\)<\/span><span class=\"sr\">$\/<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"nx\">fileUrl<\/span><span class=\"p\">)<\/span> <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nb\">Error<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">This misrable idea failed<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"nx\">path<\/span><span class=\"p\">.<\/span><span class=\"nx\">dirname<\/span><span class=\"p\">(<\/span><span class=\"nx\">url<\/span><span class=\"p\">.<\/span><span class=\"nx\">fileURLToPath<\/span><span class=\"p\">(<\/span><span class=\"nx\">fileUrl<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]));<\/span>\n\n<span class=\"p\">}<\/span>\n\n<span class=\"nx\">console<\/span><span class=\"p\">.<\/span><span class=\"nx\">log<\/span><span class=\"p\">(<\/span><span class=\"nx\">getDirName<\/span><span class=\"p\">());<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>I don\u2019t believe that the exact format of the stack trace is stable or even\nthe same across different interpreters, so this seems like a bad idea.<\/p>\n\n<p>The problem is that the more obvious approach doesn\u2019t work:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">path<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:path<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">url<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:url<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"cm\">\/** @ts-ignore CommonJS\/ESM fun *\/<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">dirname<\/span> <span class=\"o\">=<\/span> \n  <span class=\"k\">typeof<\/span> <span class=\"nx\">__dirname<\/span> <span class=\"o\">!==<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">undefined<\/span><span class=\"dl\">'<\/span> <span class=\"p\">?<\/span>\n  <span class=\"nx\">__dirname<\/span> <span class=\"p\">:<\/span>\n  <span class=\"cm\">\/** @ts-ignore CommonJS\/ESM fun *\/<\/span>\n  <span class=\"nx\">path<\/span><span class=\"p\">.<\/span><span class=\"nx\">dirname<\/span><span class=\"p\">(<\/span><span class=\"nx\">url<\/span><span class=\"p\">.<\/span><span class=\"nx\">fileURLToPath<\/span><span class=\"p\">(<\/span><span class=\"k\">import<\/span><span class=\"p\">.<\/span><span class=\"nx\">meta<\/span><span class=\"p\">.<\/span><span class=\"nx\">url<\/span><span class=\"p\">));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The above code creates a new <code class=\"language-plaintext highlighter-rouge\">dirname<\/code> variable. While\nthis works for ESM, it breaks for CommonJS on Node. The reason is that\n<code class=\"language-plaintext highlighter-rouge\">import<\/code> is not a regular object, it\u2019s syntax and even if the branch with\n<code class=\"language-plaintext highlighter-rouge\">import.meta<\/code> never gets run, Node will error while parsing:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>\/home\/evert\/src\/curveball\/controller\/cjs\/dirname.js:9\n    path.dirname(url.fileURLToPath(import.meta.url));\n                                          ^^^^\n\nSyntaxError: Cannot use 'import.meta' outside a module\n<\/code><\/pre><\/div><\/div>\n\n<p>Sadness!<\/p>\n\n<p>I don\u2019t have a satisfying solution for this yet. I\u2019ve worked around this in\nsome of my packages by avoiding this altogether, or rewriting certain files\nafter running them. <code class=\"language-plaintext highlighter-rouge\">eval('import.meta.url')<\/code> causes the ESM build to fail\nbecause technically things running in <code class=\"language-plaintext highlighter-rouge\">eval<\/code> is no longer in a module scope.<\/p>\n\n<h3 id=\"importing-commonjs-modules-with-default-exports\">Importing CommonJS modules with \u2018default exports\u2019<\/h3>\n\n<p>Some of our packages rely on 3rd party dependencies that are written in\nplain Javascript and use CommonJS.<\/p>\n\n<p>In CommonJS dependencies you can \u2018default\u2019 export things like:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nx\">module<\/span><span class=\"p\">.<\/span><span class=\"nx\">exports<\/span> <span class=\"o\">=<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">foo<\/span><span class=\"p\">(<\/span><span class=\"nx\">a<\/span><span class=\"p\">,<\/span> <span class=\"nx\">b<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">a<\/span><span class=\"o\">+<\/span><span class=\"nx\">b<\/span><span class=\"p\">;<\/span> <span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>If we were to use this library in Typescript (CJS build) we could use it\nthis way:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">WebSocket<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">ws<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">ws<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">WebSocket<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">ws:\/\/localhost:8000<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>But if we are in ESM land in Typescript, this doens\u2019t work. We actually\nget an object with a property named <code class=\"language-plaintext highlighter-rouge\">.default<\/code>. For these modules, this\nis the approach I\u2019ve come up with:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">WebSocketImp<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">ws<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"c1\">\/\/ ESM shenanigans<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">WebSocket<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">default<\/span><span class=\"dl\">'<\/span> <span class=\"k\">in<\/span> <span class=\"nx\">WebSocketImp<\/span> <span class=\"p\">?<\/span> <span class=\"p\">(<\/span><span class=\"nx\">WebSocketImp<\/span><span class=\"p\">.<\/span><span class=\"k\">default<\/span> <span class=\"k\">as<\/span> <span class=\"kr\">any<\/span><span class=\"p\">)<\/span> <span class=\"p\">:<\/span> <span class=\"nx\">WebSocketImp<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Yep! It\u2019s a pain. I\u2019ve had to do this for <code class=\"language-plaintext highlighter-rouge\">websocket<\/code>, <code class=\"language-plaintext highlighter-rouge\">chai-as-promised<\/code>,\n<code class=\"language-plaintext highlighter-rouge\">ajv<\/code>, <code class=\"language-plaintext highlighter-rouge\">raw-body<\/code>, <code class=\"language-plaintext highlighter-rouge\">accepts<\/code> and <code class=\"language-plaintext highlighter-rouge\">http-link-header<\/code>. In case you haven\u2019t\nfigured it out, I\u2019m building a <a href=\"https:\/\/curveball.js\/\">framework<\/a>.<\/p>\n\n<h2 id=\"testing\">Testing<\/h2>\n\n<p>To have confidence in both builds, I want to write tests that run against\nboth. My tests are written using Mocha. My tests are <em>also<\/em> written in\nTypescript. By default, they only run against the ESM build, and I expect\nmyself and other developers to be in this mode during the main \u2018development\nloop.<\/p>\n\n<p>To make Mocha understand Typescript, we need to install the <code class=\"language-plaintext highlighter-rouge\">ts-node<\/code> package,\nand add the following properties to the <em>root<\/em> <code class=\"language-plaintext highlighter-rouge\">package.json<\/code>:<\/p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nl\">\"mocha\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"loader\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"ts-node\/esm\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">],<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"recursive\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">true<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"extension\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"ts\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"js\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"tsx\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>This all works perfectly well, but what about CommonJS? For this I had 2\napproaches: Either reconfigure mocha to use the CommonJS Typescript\nloader instead of the ESM typescript loader, but this was too much of\na pain to get right with writing configuration files on the fly.<\/p>\n\n<p>Instead, I decided to just ask Typescript to build the entire project\nincluding tests in a separate directory and then just run Mocha on\nthe javascript files.<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">mkdir<\/span> <span class=\"nt\">-p<\/span> cjs-test\n<span class=\"nb\">cd test<\/span><span class=\"p\">;<\/span> npx tsc <span class=\"nt\">--module<\/span> commonjs <span class=\"nt\">--outdir<\/span> ..\/cjs-test\n<span class=\"nb\">echo<\/span> <span class=\"s1\">'{\"type\": \"commonjs\"}'<\/span> <span class=\"o\">&gt;<\/span> cjs-test\/package.json\n<span class=\"nb\">cd <\/span>cjs-test<span class=\"p\">;<\/span> npx mocha <span class=\"nt\">--no-package<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This required a separate <code class=\"language-plaintext highlighter-rouge\">tsconfig.json<\/code> in our <code class=\"language-plaintext highlighter-rouge\">test<\/code> directory.<\/p>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n<p>So this is a fairly large amount of annoyances to work through, some without\na solution. Would I recommend this?<\/p>\n\n<p>I think if you\u2019re in my position where:<\/p>\n\n<ul>\n  <li>You\u2019re doing this for a library.<\/li>\n  <li>You consider ESM the future, but want to continue to support CommonJS users.<\/li>\n  <li>The experience of users of your library is far more important than your own experience.<\/li>\n<\/ul>\n\n<p>Then, I think this is a good idea. If you don\u2019t want this hassle and want just\none target, I think CommonJS will still give you the best experience.<\/p>\n\n<p>ESM will probably catch up at some point, but right now I think we\u2019re still in the\nawkward phase.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/bye-badgateway\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/bye-badgateway\/"}},"title":"Winding down Bad Gateway","updated":"2023-02-20T19:18:15+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>In 2019 I started Bad Gateway as a software development agency. Last year we\ngrew all the way to 7 people. It was crazy challenging, especially with Covid\nin the mix; but ultimately could not get the company into a good financial\nstate to be able to carry on.<\/p>\n\n<p>Big thanks to my co-workers and partners <a href=\"https:\/\/www.linkedin.com\/in\/juhangsin\/\">Ju<\/a>, <a href=\"https:\/\/becky.dev\/\">Becky<\/a>, <a href=\"https:\/\/www.linkedin.com\/in\/philippeschwyter\/\">Phil<\/a>,\n<a href=\"https:\/\/michaelhumiston.com\/\">Michael<\/a>, <a href=\"http:\/\/www.lfo-industries.com\/\">Siep<\/a>, <a href=\"https:\/\/www.linkedin.com\/in\/rswitzer\/\">Richard<\/a> and <a href=\"https:\/\/www.linkedin.com\/in\/syed-farhan-kabir\/\">Syed<\/a>.\nI\u2019m incredibly grateful you came on this journey with me. We shared some\ntears, exchanged some words but mostly had lots of laughs. Despite the\nchallenges I feel my relationship with you has only strengthened and I wish\nyou well in the next steps of your career.<\/p>\n\n<p>Also thank you to our customers and especially <a href=\"https:\/\/underknown.com\/\">Underknown<\/a> who\u2019ve stuck\nwith us since the start.<\/p>\n\n<p>Despite this outcome, I have a hard time seeing the last few years as a\nfailure. It\u2019s been hella fun, and I feel we stuck to our values even in times\nof crisis. Off to the next adventure.<\/p>\n\n<p><img class=\"fill-width\" src=\"\/assets\/posts\/bg-502.png\" alt=\"Image of person standing in front of a gateway.\" \/><\/p>\n\n"},{"id":"http:\/\/evertpot.com\/node-changelog-cli-tool\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/node-changelog-cli-tool\/"}},"title":"Building a simple CLI tool with modern Node.js","updated":"2023-02-13T19:05:14+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I\u2019m a maintainer of several dozen open source libraries. One thing I\u2019ve\nalways done is maintain a hand-written changelog.<\/p>\n\n<p>Here\u2019s an example from <a href=\"https:\/\/github.com\/curveball\/a12n-server\/\" title=\"An awesome lightweight OAuth2 server\">a12n-server<\/a><\/p>\n\n<div class=\"language-markdown highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"gh\">0.22.0 (2022-09-27)\n-------------------\n<\/span>\nWarning note for upgraders. This release has a database migration on the\n<span class=\"sb\">`oauth2_tokens`<\/span> table. For most users this is the largest table, some\ndowntime may be expected while the server runs its migrations.\n<span class=\"p\">\n*<\/span> #425: Using a <span class=\"sb\">`client_secret`<\/span> is now supported with <span class=\"sb\">`authorization_code`<\/span>,\n  and it's read from either the request body or HTTP Basic Authorization\n  header.\n<span class=\"p\">*<\/span> The service now keeps track when issuing access tokens, whether those tokens\n  have used a <span class=\"sb\">`client_secret`<\/span> or not, which <span class=\"sb\">`grant_type`<\/span> was used to issue them\n  and what scopes were requested. This work is done to better support OAuth2\n  scopes in the future, and eventually OpenID Connect.\n<span class=\"p\">*<\/span> Fixed broken 'principal uri' in introspection endpoint response.\n<span class=\"p\">*<\/span> OAuth2 service is almost entirely rewritten.\n<span class=\"p\">*<\/span> The number of tokens issued is now displayed on the home page.\n<span class=\"p\">*<\/span> Large numbers are now abbreviated with <span class=\"sb\">`K`<\/span> and <span class=\"sb\">`M`<\/span>.\n<span class=\"p\">*<\/span> #426: Updated to Curveball 0.20.\n<span class=\"p\">*<\/span> #427: Typescript types for the database schema are now auto-generated with\n  <span class=\"sb\">`mysql-types-generator`<\/span>.\n<\/code><\/pre><\/div><\/div>\n\n<p>These are all written in Markdown. You might think: isn\u2019t Git also a log? Why\nbother hand-writing these?<\/p>\n\n<p>The reason is that the audience for these is a bit different. I want to bring\nattention to the things that are the most important for the end-user, and\nfocus on the impact of the change to the user.<\/p>\n\n<p>I thought it would be handy to write a CLI tool that makes it a bit easier to\nmaintain these. <a href=\"https:\/\/github.com\/evert\/changelog-tool\" title=\"The changelog tool!\">So, I did!<\/a>. If you are curious what kind of technology\nchoices went into this, read on.<\/p>\n\n<h2 id=\"goals-and-features\">Goals and features<\/h2>\n\n<p>The tool should be able to do the following:<\/p>\n\n<ul>\n  <li>Reformat changelogs (a bit like prettify) (<code class=\"language-plaintext highlighter-rouge\">changelog format<\/code>)<\/li>\n  <li>Add an entry via the command line (<code class=\"language-plaintext highlighter-rouge\">changelog add --minor -m \"New feature\"<\/code>).<\/li>\n  <li>Automatically set the release date (<code class=\"language-plaintext highlighter-rouge\">changelog release<\/code>)<\/li>\n  <li>Pipe a log of a specific version to STDOUT, so it can be used by other tools\n(like integrating with github releases).<\/li>\n<\/ul>\n\n<p>I also had a bunch of a non-functional requirements:<\/p>\n\n<ul>\n  <li>Use the latest Node features.<\/li>\n  <li>Use up to date Javascript standards and features (ESM).<\/li>\n  <li>Avoid dependendencies unless it\u2019s unreasonable to do so.<\/li>\n  <li>Make it low maintanance.<\/li>\n<\/ul>\n\n<p>Want to find the finished tool right now? It\u2019s open source so just go to <a href=\"https:\/\/github.com\/evert\/changelog-tool\" title=\"The changelog tool!\">Github<\/a>.<\/p>\n\n<h2 id=\"the-implementation\">The implementation<\/h2>\n\n<h3 id=\"esm--typescript\">ESM &amp; Typescript<\/h3>\n\n<p>Ecmascripts modules worked really well here. It\u2019s a small change of habits,\nbut the general recommendation I would have is to just save your files\nas <code class=\"language-plaintext highlighter-rouge\">.mjs<\/code> and start using it.<\/p>\n\n<p>Here\u2019s the first few lines of <code class=\"language-plaintext highlighter-rouge\">parse.mjs<\/code>:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ @ts-check<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">Changelog<\/span><span class=\"p\">,<\/span> <span class=\"nx\">VersionLog<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">\"<\/span><span class=\"s2\">.\/changelog.mjs<\/span><span class=\"dl\">\"<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">readFile<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:fs\/promises<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"cm\">\/**\n * @param {string} filename\n * @returns {Promise&lt;Changelog&gt;}\n *\/<\/span>\n<span class=\"k\">export<\/span> <span class=\"k\">async<\/span> <span class=\"kd\">function<\/span> <span class=\"nx\">parseFile<\/span><span class=\"p\">(<\/span><span class=\"nx\">filename<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"k\">return<\/span> <span class=\"nx\">parse<\/span><span class=\"p\">(<\/span>\n    <span class=\"k\">await<\/span> <span class=\"nx\">readFile<\/span><span class=\"p\">(<\/span><span class=\"nx\">filename<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">utf-8<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span>\n  <span class=\"p\">);<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The CommonJS -&gt; ESM transition is not without pain, but for an new project\nlike this it\u2019s really the ideal choice. (top level <code class=\"language-plaintext highlighter-rouge\">await<\/code> \ud83c\udf89)<\/p>\n\n<p>I\u2019ve also made the choice to not write my code in Typescript, but use JSDoc\nannotations instead (These are the <code class=\"language-plaintext highlighter-rouge\">@param<\/code> and <code class=\"language-plaintext highlighter-rouge\">@returns<\/code> comments).<\/p>\n\n<p>Not everyone knows that you don\u2019t need to write .ts files to benefit from\nTypescript. Typescript can also check your Javascript files quite strictly,\nand there\u2019s a lot of work still being done in adding new features to this\nsyntax.<\/p>\n\n<p>This has the benefit of not needing a build phase. You don\u2019t even need\nTypescript during development which reduces the barrier to entry.<\/p>\n\n<p>Here\u2019s my minimal <code class=\"language-plaintext highlighter-rouge\">tsconfig.json<\/code>:<\/p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"compilerOptions\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"target\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"es2022\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"module\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"esnext\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"rootDir\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\".\/\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"allowJs\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">true<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"checkJs\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">true<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n    <\/span><span class=\"nl\">\"moduleResolution\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"node\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n     \n    <\/span><span class=\"nl\">\"noEmit\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">true<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"strict\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">true<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"useUnknownInCatchVariables\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"kc\">false<\/span><span class=\"p\">,<\/span><span class=\"w\">\n  \n  <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<p>The Typescript documentation has a <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/jsdoc-supported-types.html\" title=\"JSDoc support in Typescript\">page detailing the JSDoc annotations they\nsupport<\/a>, if you\u2019d like to learn more.<\/p>\n\n<h3 id=\"command-line-parsing\">Command line parsing<\/h3>\n\n<p>CLI tools need to be able to parse command line options. Since Node 18.3\n(backported to Node 16.17) Node comes with a built-in options parser.<\/p>\n\n<p>Here\u2019s a sample of the code:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">parseArgs<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:util<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">positionals<\/span><span class=\"p\">,<\/span> <span class=\"nx\">values<\/span> <span class=\"p\">}<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">parseArgs<\/span><span class=\"p\">({<\/span>\n  <span class=\"na\">options<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">help<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n      <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boolean<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"na\">short<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">h<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"na\">default<\/span><span class=\"p\">:<\/span> <span class=\"kc\">false<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"na\">all<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n      <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boolean<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"na\">default<\/span><span class=\"p\">:<\/span> <span class=\"kc\">false<\/span><span class=\"p\">,<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"na\">message<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n      <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">string<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n      <span class=\"na\">short<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">m<\/span><span class=\"dl\">'<\/span>\n    <span class=\"p\">},<\/span>\n    <span class=\"na\">patch<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boolean<\/span><span class=\"dl\">'<\/span> <span class=\"p\">},<\/span>\n    <span class=\"na\">minor<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boolean<\/span><span class=\"dl\">'<\/span> <span class=\"p\">},<\/span>\n    <span class=\"na\">major<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span> <span class=\"na\">type<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">boolean<\/span><span class=\"dl\">'<\/span> <span class=\"p\">},<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">allowPositionals<\/span><span class=\"p\">:<\/span> <span class=\"kc\">true<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This confguration adds flags like <code class=\"language-plaintext highlighter-rouge\">--major<\/code>, lets you specify a message\nwith <code class=\"language-plaintext highlighter-rouge\">--message \"hello!\"<\/code> or use a short-hand alternative like <code class=\"language-plaintext highlighter-rouge\">-m \"Hi\"<\/code>.<\/p>\n\n<p>Does it do everything? No! There\u2019s packages out there that are more\nsophisticated, use colors, automatically create help screens but they\nalso ship with large dependency trees.<\/p>\n\n<p>In my case, this was good enough.<\/p>\n\n<p>Check out the <a href=\"https:\/\/nodejs.org\/api\/util.html#utilparseargsconfig\" title=\"Parsing arguments with Node\">Node docs<\/a> for more info.<\/p>\n\n<h3 id=\"testing\">Testing<\/h3>\n\n<p>Most people probably use Jest or Mocha as their test framework, but since\nNode 18 (also backported to 16) Node has a built-in test-runner.<\/p>\n\n<p>It has an API similar to Mocha and Jest with keywords like <code class=\"language-plaintext highlighter-rouge\">it<\/code>, <code class=\"language-plaintext highlighter-rouge\">test<\/code>,\n<code class=\"language-plaintext highlighter-rouge\">describe<\/code>, <code class=\"language-plaintext highlighter-rouge\">before<\/code>, etc.<\/p>\n\n<p>Here\u2019s a sample of one of my tests:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ @ts-check<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">test<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:test<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">parse<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">..\/parse.mjs<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"o\">*<\/span> <span class=\"k\">as<\/span> <span class=\"nx\">assert<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">node:assert<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"nx\">test<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">Parsing changelog metadata<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"k\">async<\/span> <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">input<\/span> <span class=\"o\">=<\/span> <span class=\"s2\">`Time for a change\n=========\n\n0.2.0 (????-??-??)\n------------------\n\n* Implemented the 'list' command.\n* Added testing framework.\n\n0.1.0 (2023-02-08)\n------------------\n\n* Implemented the 'help' and 'init' commands.\n*\n`<\/span><span class=\"p\">;<\/span>\n\n  <span class=\"kd\">const<\/span> <span class=\"nx\">result<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">parse<\/span><span class=\"p\">(<\/span><span class=\"nx\">input<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">Time for a change<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">title<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"mi\">2<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">versions<\/span><span class=\"p\">.<\/span><span class=\"nx\">length<\/span><span class=\"p\">);<\/span>\n  \n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"kc\">null<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">versions<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"nx\">date<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">0.2.0<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">versions<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"nx\">version<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">2023-02-08<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">versions<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">].<\/span><span class=\"nx\">date<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">assert<\/span><span class=\"p\">.<\/span><span class=\"nx\">equal<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">0.1.0<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nx\">result<\/span><span class=\"p\">.<\/span><span class=\"nx\">versions<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">].<\/span><span class=\"nx\">version<\/span><span class=\"p\">);<\/span>\n\n<span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>To run the tests, just run <code class=\"language-plaintext highlighter-rouge\">node --test<\/code>. No configuration needed, it will\ndiscover tests following directory and filename conventions.<\/p>\n\n<p>The Node 18 test output is a bit rough, it\u2019s the TAP format and looks like this:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>TAP version 13\n# Subtest: \/home\/evert\/src\/changelog-tool\/test\/parse.mjs\n    # Subtest: Parsing changelog metadata\n    ok 1 - Parsing changelog metadata\n      ---\n      duration_ms: 1.713409\n      ...\n    # Subtest: Parsing changelog entries\n    ok 2 - Parsing changelog entries\n      ---\n      duration_ms: 0.2595\n      ...\n    # Subtest: Preface and postface\n    ok 3 - Preface and postface\n      ---\n      duration_ms: 0.193591\n      ...\n    1..3\nok 1 - \/home\/evert\/src\/changelog-tool\/test\/parse.mjs\n  ---\n  duration_ms: 70.901055\n  ...\n1..1\n# tests 1\n# pass 1\n# fail 0\n# cancelled 0\n# skipped 0\n# todo 0\n# duration_ms 81.481441\n<\/code><\/pre><\/div><\/div>\n\n<p>In Node 19 new test reporters will be shipped, but I haven\u2019t tested this yet.<\/p>\n\n<p>Frankly, after using this I don\u2019t know if would use Mocha anymore. I\u2019ve been\nusing probably over a decade, and it has some nice features and benefits,\nfor the kinds of tests I write (and I write a lot), I think there\u2019s anything\nI need beyond what Node is offering here.<\/p>\n\n<p>Some links:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/nodejs.org\/api\/test.html\" title=\"Node Test Runner\">node:test package<\/a>.<\/li>\n  <li><a href=\"https:\/\/nodejs.org\/api\/assert.html\" title=\"Assertion Testing\">node:assert package<\/a>.<\/li>\n  <li><a href=\"https:\/\/nodejs.org\/api\/test.html#mocking\" title=\"Mocking in Node tests\">Mocking in node<\/a>.<\/li>\n<\/ul>\n\n<h2 id=\"packagejson-annotated\">package.json, annotated<\/h2>\n\n<p>I wanted to end this article with how I\u2019ve set up my <code class=\"language-plaintext highlighter-rouge\">package.json<\/code>, so you\ncan see how it all ties together. (If only <code class=\"language-plaintext highlighter-rouge\">npm<\/code> <a href=\"\/json5\/\">supported JSON5<\/a> so I\ncould keep my comments right in the package \ud83d\ude2d).<\/p>\n\n<div class=\"language-json highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"p\">{<\/span><span class=\"w\">\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">The<\/span><span class=\"w\"> <\/span><span class=\"err\">name<\/span><span class=\"w\"> <\/span><span class=\"err\">of<\/span><span class=\"w\"> <\/span><span class=\"err\">the<\/span><span class=\"w\"> <\/span><span class=\"err\">package<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"err\">and<\/span><span class=\"w\"> <\/span><span class=\"err\">how<\/span><span class=\"w\"> <\/span><span class=\"err\">it's<\/span><span class=\"w\"> <\/span><span class=\"err\">published<\/span><span class=\"w\"> <\/span><span class=\"err\">on<\/span><span class=\"w\"> <\/span><span class=\"err\">NPM<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"name\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"changelog-tool\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">Package<\/span><span class=\"w\"> <\/span><span class=\"err\">version!<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"version\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"0.5.0\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">This<\/span><span class=\"w\"> <\/span><span class=\"err\">will<\/span><span class=\"w\"> <\/span><span class=\"err\">show<\/span><span class=\"w\"> <\/span><span class=\"err\">up<\/span><span class=\"w\"> <\/span><span class=\"err\">in<\/span><span class=\"w\"> <\/span><span class=\"err\">NPM<\/span><span class=\"w\"> <\/span><span class=\"err\">searches<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"description\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"A CLI tool for manipulating changelogs\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">This<\/span><span class=\"w\"> <\/span><span class=\"err\">tells<\/span><span class=\"w\"> <\/span><span class=\"err\">Node<\/span><span class=\"w\"> <\/span><span class=\"err\">to<\/span><span class=\"w\"> <\/span><span class=\"err\">assume<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">is<\/span><span class=\"w\"> <\/span><span class=\"err\">an<\/span><span class=\"w\"> <\/span><span class=\"err\">ESM<\/span><span class=\"w\"> <\/span><span class=\"err\">package.<\/span><span class=\"w\"> <\/span><span class=\"err\">Not<\/span><span class=\"w\"> <\/span><span class=\"err\">strictly<\/span><span class=\"w\"> <\/span><span class=\"err\">needed<\/span><span class=\"w\">\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">if<\/span><span class=\"w\"> <\/span><span class=\"err\">we<\/span><span class=\"w\"> <\/span><span class=\"err\">use<\/span><span class=\"w\"> <\/span><span class=\"err\">the<\/span><span class=\"w\"> <\/span><span class=\"err\">.mjs<\/span><span class=\"w\"> <\/span><span class=\"err\">extension<\/span><span class=\"w\"> <\/span><span class=\"err\">everywhere<\/span><span class=\"w\"> <\/span><span class=\"err\">though.<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"type\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"module\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">If<\/span><span class=\"w\"> <\/span><span class=\"err\">you<\/span><span class=\"w\"> <\/span><span class=\"err\">use<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">package<\/span><span class=\"w\"> <\/span><span class=\"err\">programmatically<\/span><span class=\"w\"> <\/span><span class=\"err\">(not<\/span><span class=\"w\"> <\/span><span class=\"err\">on<\/span><span class=\"w\"> <\/span><span class=\"err\">CLI)<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">file<\/span><span class=\"w\"> <\/span><span class=\"err\">is<\/span><span class=\"w\">\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">where<\/span><span class=\"w\"> <\/span><span class=\"err\">all<\/span><span class=\"w\"> <\/span><span class=\"err\">'exports'<\/span><span class=\"w\"> <\/span><span class=\"err\">are<\/span><span class=\"w\"> <\/span><span class=\"err\">for<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">package.<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"main\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"index.mjs\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"nl\">\"scripts\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">Test<\/span><span class=\"w\"> <\/span><span class=\"err\">runner!<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"test\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"node --test\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">I<\/span><span class=\"w\"> <\/span><span class=\"err\">like<\/span><span class=\"w\"> <\/span><span class=\"err\">to<\/span><span class=\"w\"> <\/span><span class=\"err\">keep<\/span><span class=\"w\"> <\/span><span class=\"err\">Typescript<\/span><span class=\"w\"> <\/span><span class=\"err\">running<\/span><span class=\"w\"> <\/span><span class=\"err\">in<\/span><span class=\"w\"> <\/span><span class=\"err\">the<\/span><span class=\"w\"> <\/span><span class=\"err\">terminal<\/span><span class=\"w\"> <\/span><span class=\"err\">to<\/span><span class=\"w\"> <\/span><span class=\"err\">insta-warn<\/span><span class=\"w\"> <\/span><span class=\"err\">me<\/span><span class=\"w\"> <\/span><span class=\"err\">of<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">of<\/span><span class=\"w\"> <\/span><span class=\"err\">any<\/span><span class=\"w\"> <\/span><span class=\"err\">issues.<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"watch\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"tsc --watch\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">},<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">This<\/span><span class=\"w\"> <\/span><span class=\"err\">helps<\/span><span class=\"w\"> <\/span><span class=\"err\">people<\/span><span class=\"w\"> <\/span><span class=\"err\">discover<\/span><span class=\"w\"> <\/span><span class=\"err\">the<\/span><span class=\"w\"> <\/span><span class=\"err\">package<\/span><span class=\"w\"> <\/span><span class=\"err\">on<\/span><span class=\"w\"> <\/span><span class=\"err\">npmjs.org<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"keywords\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"changelog\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"s2\">\"markdown\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">],<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">It's<\/span><span class=\"w\"> <\/span><span class=\"err\">me!<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"author\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"Evert Pot (https:\/\/evertpot.com\/)\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">Do<\/span><span class=\"w\"> <\/span><span class=\"err\">(almost)<\/span><span class=\"w\"> <\/span><span class=\"err\">anything<\/span><span class=\"w\"> <\/span><span class=\"err\">you<\/span><span class=\"w\"> <\/span><span class=\"err\">want<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"license\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"MIT\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n\n  <\/span><span class=\"nl\">\"engine\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">Warn<\/span><span class=\"w\"> <\/span><span class=\"err\">people<\/span><span class=\"w\"> <\/span><span class=\"err\">if<\/span><span class=\"w\"> <\/span><span class=\"err\">they<\/span><span class=\"w\"> <\/span><span class=\"err\">haven't<\/span><span class=\"w\"> <\/span><span class=\"err\">upgraded<\/span><span class=\"w\"> <\/span><span class=\"err\">yet<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"node\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"&gt;16\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">},<\/span><span class=\"w\">\n\n  <\/span><span class=\"nl\">\"bin\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">When<\/span><span class=\"w\"> <\/span><span class=\"err\">people<\/span><span class=\"w\"> <\/span><span class=\"err\">install<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">package<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">makes<\/span><span class=\"w\"> <\/span><span class=\"err\">`npx<\/span><span class=\"w\"> <\/span><span class=\"err\">changelog`<\/span><span class=\"w\"> <\/span><span class=\"err\">work.<\/span><span class=\"w\"> <\/span><span class=\"err\">If<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">you<\/span><span class=\"w\"> <\/span><span class=\"err\">installed<\/span><span class=\"w\"> <\/span><span class=\"err\">this<\/span><span class=\"w\"> <\/span><span class=\"err\">package<\/span><span class=\"w\"> <\/span><span class=\"err\">globally<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"err\">you<\/span><span class=\"w\"> <\/span><span class=\"err\">now<\/span><span class=\"w\"> <\/span><span class=\"err\">have<\/span><span class=\"w\"> <\/span><span class=\"err\">a<\/span><span class=\"w\"> <\/span><span class=\"err\">`changelog`<\/span><span class=\"w\"> <\/span><span class=\"err\">command<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">at<\/span><span class=\"w\"> <\/span><span class=\"err\">your<\/span><span class=\"w\"> <\/span><span class=\"err\">disposal.<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"changelog\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\".\/cli.mjs\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">},<\/span><span class=\"w\">\n  <\/span><span class=\"nl\">\"devDependencies\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">The<\/span><span class=\"w\"> <\/span><span class=\"err\">only<\/span><span class=\"w\"> <\/span><span class=\"mi\">2<\/span><span class=\"w\"> <\/span><span class=\"err\">dependencies.<\/span><span class=\"w\"> <\/span><span class=\"err\">You<\/span><span class=\"w\"> <\/span><span class=\"err\">don't<\/span><span class=\"w\"> <\/span><span class=\"err\">even<\/span><span class=\"w\"> <\/span><span class=\"err\">really<\/span><span class=\"w\"> <\/span><span class=\"err\">need<\/span><span class=\"w\"> <\/span><span class=\"err\">these<\/span><span class=\"w\"> <\/span><span class=\"err\">if<\/span><span class=\"w\"> <\/span><span class=\"err\">you<\/span><span class=\"w\"> <\/span><span class=\"err\">just<\/span><span class=\"w\"> <\/span><span class=\"err\">want<\/span><span class=\"w\">\n    <\/span><span class=\"err\">\/\/<\/span><span class=\"w\"> <\/span><span class=\"err\">use<\/span><span class=\"w\"> <\/span><span class=\"err\">or<\/span><span class=\"w\"> <\/span><span class=\"err\">hack<\/span><span class=\"w\"> <\/span><span class=\"err\">the<\/span><span class=\"w\"> <\/span><span class=\"err\">package.<\/span><span class=\"w\"> <\/span><span class=\"err\">Github<\/span><span class=\"w\"> <\/span><span class=\"err\">actions<\/span><span class=\"w\"> <\/span><span class=\"err\">will<\/span><span class=\"w\"> <\/span><span class=\"err\">run<\/span><span class=\"w\"> <\/span><span class=\"err\">Typescript<\/span><span class=\"w\"> <\/span><span class=\"err\">as<\/span><span class=\"w\"> <\/span><span class=\"err\">well.<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"@types\/node\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"^18.11.19\"<\/span><span class=\"p\">,<\/span><span class=\"w\">\n    <\/span><span class=\"nl\">\"typescript\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"^4.9.5\"<\/span><span class=\"w\">\n  <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/code><\/pre><\/div><\/div>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n<p>I love building new things and being deliberate about every choice.<\/p>\n\n<p>The result is that I\u2019m more likely to end up with something minimal,\nlow-maintance and I gain a deeper understanding of the tools I use.<\/p>\n\n<p>In the near future I would probably make all these choices again. The Node test\nrunner is fast and low-cruft, ESM is great when it works and not needing a build\nstep feels right for a project this size.<\/p>\n\n<p>I hope this inspires someone in the future to start their next project from\nan empty directory instead of copying a large boilerplate project.<\/p>\n\n<p><a href=\"https:\/\/github.com\/evert\/changelog-tool\" title=\"The changelog tool!\">changelog-tool project on Github<\/a>.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/knex-sql-injection\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/knex-sql-injection\/"}},"title":"Knex (with MySQL) had a very scary SQL injection","updated":"2023-01-12T21:31:43+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p><a href=\"https:\/\/knexjs.org\/\">Knex<\/a> recently released a new version this week (2.4.0). Before this version,\nKnex had a pretty scary SQL injection. Knex currently has 1.3 million weekly\ndownloads and is quite popular.<\/p>\n\n<p>The security bug is probably one of the worst SQL injections I\u2019ve seen in recent\nmemory, especially considering the scope and popularity.<\/p>\n\n<p>If you want to get straight to the details:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/knex\/knex\/issues\/1227\">Check out the Github issue<\/a>, which was opened 7 years ago(!)<\/li>\n  <li><a href=\"https:\/\/www.ghostccamm.com\/blog\/knex_sqli\/\">An article from Ghostccamm<\/a> explaining the vulnerability.<\/li>\n  <li><a href=\"https:\/\/nvd.nist.gov\/vuln\/detail\/CVE-2016-20018\">CVE-2016-20018<\/a>.<\/li>\n<\/ul>\n\n<h2 id=\"my-understanding-of-this-bug\">My understanding of this bug<\/h2>\n\n<p>If I understand the vulnerability correctly, I feel this can impact a very\nlarge number of sites using Knex. Even more so if you use Express.<\/p>\n\n<p>I\u2019ll try to explain through a simple example. Say, you have MySQL table structured\nlike this:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">CREATE<\/span> <span class=\"k\">TABLE<\/span> <span class=\"nv\">`users`<\/span> <span class=\"p\">(<\/span>\n  <span class=\"nv\">`id`<\/span> <span class=\"nb\">int<\/span> <span class=\"k\">NOT<\/span> <span class=\"k\">NULL<\/span> <span class=\"n\">AUTO_INCREMENT<\/span><span class=\"p\">,<\/span>\n  <span class=\"nv\">`name`<\/span> <span class=\"nb\">varchar<\/span><span class=\"p\">(<\/span><span class=\"mi\">100<\/span><span class=\"p\">)<\/span> <span class=\"k\">DEFAULT<\/span> <span class=\"k\">NULL<\/span><span class=\"p\">,<\/span>\n  <span class=\"k\">PRIMARY<\/span> <span class=\"k\">KEY<\/span> <span class=\"p\">(<\/span><span class=\"nv\">`id`<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">)<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>And you have a query that does a <code class=\"language-plaintext highlighter-rouge\">SELECT<\/code> using Knex:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">lookupId<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">2<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">result<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">knex<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">users<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span>\n  <span class=\"p\">.<\/span><span class=\"nx\">select<\/span><span class=\"p\">([<\/span><span class=\"dl\">'<\/span><span class=\"s1\">id<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">name<\/span><span class=\"dl\">'<\/span><span class=\"p\">])<\/span>\n  <span class=\"p\">.<\/span><span class=\"nx\">where<\/span><span class=\"p\">({<\/span>\n    <span class=\"na\">id<\/span><span class=\"p\">:<\/span> <span class=\"nx\">lookupId<\/span>\n  <span class=\"p\">});<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>You\u2019d expect the query to end up roughly like this<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"nv\">`id`<\/span><span class=\"p\">,<\/span> <span class=\"nv\">`name`<\/span> <span class=\"k\">FROM<\/span> <span class=\"nv\">`users`<\/span> <span class=\"k\">WHERE<\/span> <span class=\"nv\">`id`<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">2<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>The issue is when the user controls the value of <code class=\"language-plaintext highlighter-rouge\">lookupId<\/code>. If somehow they\ncan turn this into an object like this:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">lookupId<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n  <span class=\"na\">name<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">foo<\/span><span class=\"dl\">'<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>You might expect an error from Knex, but instead it generates the following query:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"nv\">`id`<\/span><span class=\"p\">,<\/span> <span class=\"nv\">`name`<\/span> <span class=\"k\">FROM<\/span> <span class=\"nv\">`users`<\/span> <span class=\"k\">WHERE<\/span> <span class=\"nv\">`id`<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">`name`<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'foo'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This query is not invalid. I don\u2019t fully understand MySQL\u2019s behavior,\nbut it causes the WHERE clause to be ignored and the result is equivalent to:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"nv\">`id`<\/span><span class=\"p\">,<\/span> <span class=\"nv\">`name`<\/span> <span class=\"k\">FROM<\/span> <span class=\"nv\">`users`<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>I think it has something to do with how MySQL casts things. In MySQL this yields <code class=\"language-plaintext highlighter-rouge\">true<\/code> (or <code class=\"language-plaintext highlighter-rouge\">1<\/code>):<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"mi\">1<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'foo'<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'bar'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<h2 id=\"query-strings-and-express\">Query strings and Express<\/h2>\n\n<p>One place where this is especially scary, is if <code class=\"language-plaintext highlighter-rouge\">lookupId<\/code> was provided by a user,\nvia a JSON body or query string.<\/p>\n\n<p>Consider this example from an Express route:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"kd\">get<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">\/<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"k\">async<\/span> <span class=\"p\">(<\/span><span class=\"nx\">req<\/span><span class=\"p\">,<\/span> <span class=\"nx\">res<\/span><span class=\"p\">)<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">result<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">knex<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">users<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">.<\/span><span class=\"nx\">select<\/span><span class=\"p\">([<\/span><span class=\"dl\">'<\/span><span class=\"s1\">id<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">name<\/span><span class=\"dl\">'<\/span><span class=\"p\">])<\/span>\n     <span class=\"p\">.<\/span><span class=\"nx\">where<\/span><span class=\"p\">({<\/span><span class=\"na\">id<\/span><span class=\"p\">:<\/span> <span class=\"nx\">req<\/span><span class=\"p\">.<\/span><span class=\"nx\">query<\/span><span class=\"p\">.<\/span><span class=\"nx\">id<\/span><span class=\"p\">});<\/span>\n\n  <span class=\"nx\">res<\/span><span class=\"p\">.<\/span><span class=\"nx\">send<\/span><span class=\"p\">(<\/span><span class=\"nx\">result<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">})<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Here the <code class=\"language-plaintext highlighter-rouge\">id<\/code> in the <code class=\"language-plaintext highlighter-rouge\">WHERE<\/code> clause is provided by the URL query string:<\/p>\n\n<p>If a the server is opened with something like:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>http:\/\/localhost:3000\/?id=2\n<\/code><\/pre><\/div><\/div>\n\n<p>It will return a single user, but if it was opened using:<\/p>\n\n<div class=\"language-plaintext highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>http:\/\/localhost:3000\/?id%5Bname%5D=foo\n<\/code><\/pre><\/div><\/div>\n\n<p>It will return <em>every user<\/em>. And this issue is not limited to <code class=\"language-plaintext highlighter-rouge\">SELECT<\/code>,\nit could also trigger in a <code class=\"language-plaintext highlighter-rouge\">WHERE<\/code> clause in <code class=\"language-plaintext highlighter-rouge\">DELETE<\/code>.<\/p>\n\n<p>The reason this works is that by crafting URL query parameters in a special\nway, a user can have things in <code class=\"language-plaintext highlighter-rouge\">req.query<\/code> show up as objects or arrays.<\/p>\n\n<p>Express calls this the \u2018extended\u2019 syntax and turns it on by default. In my\nopinion this is a bad default because it\u2019s not really what users expect will\nhappen. PHP does this as well, and I believe this may have been where Express\n(or specifically the <a href=\"https:\/\/www.npmjs.com\/package\/qs\">qs<\/a> library) got the syntax from.<\/p>\n\n<p>This is why I think Express users are expecially likely to be vulnerable.\nI think that most developers in professional settings are decent at\nvalidating JSON request bodies with a variety of tools, but I\u2019ve noticed\nthis is often not the case for query parameters. I believe a big reason for\nthis is that the \u2018extended\u2019 syntax is not well known and \u2018surprising\u2019 behavior.\nMany people assume these are just strings. This is not intended as a plug,\nbut this is also why the default behavior in our <a href=\"https:\/\/curveballjs.org\/\">Curveball framework<\/a> is\nto not support this. In most cases it\u2019s not needed, and if you do want it you can\nuse <code class=\"language-plaintext highlighter-rouge\">qs<\/code> and explicitly opt-in.<\/p>\n\n<h2 id=\"other-examples\">Other examples<\/h2>\n\n<p>But of course, this issue is not limited to query strings. If you\u2019re not\nvalidating input somewhere and this ends up in a <code class=\"language-plaintext highlighter-rouge\">.where()<\/code> statement there\u2019s\nrisk.<\/p>\n\n<p>A specific example is a situation where part of the contents of your <code class=\"language-plaintext highlighter-rouge\">WHERE<\/code>\nclause is unguessable.<\/p>\n\n<p>For instance, say you had a database table with records that users can only\nsee if they know a secret code, and it\u2019s selected with:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"o\">*<\/span> <span class=\"k\">FROM<\/span> <span class=\"n\">coupons<\/span> <span class=\"k\">WHERE<\/span> <span class=\"n\">product_id<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">5<\/span> <span class=\"k\">AND<\/span> <span class=\"n\">coupon_code<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'BIG_OOF2023'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>A user presumably would have to <em>know<\/em> the <code class=\"language-plaintext highlighter-rouge\">coupon_code<\/code> to see the coupons,\nbut if there\u2019s no code to validate <code class=\"language-plaintext highlighter-rouge\">coupon_code<\/code> is a string, a user would have the\npower to change the query to this:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"o\">*<\/span> <span class=\"k\">FROM<\/span> <span class=\"n\">coupons<\/span> <span class=\"k\">WHERE<\/span> <span class=\"n\">product_id<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">5<\/span> <span class=\"k\">AND<\/span> <span class=\"n\">coupon_code<\/span> <span class=\"o\">=<\/span> <span class=\"n\">product_id<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'bla'<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Which is equivalent to:<\/p>\n\n<div class=\"language-sql highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">SELECT<\/span> <span class=\"o\">*<\/span> <span class=\"k\">FROM<\/span> <span class=\"n\">coupons<\/span> <span class=\"k\">WHERE<\/span> <span class=\"n\">product_id<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">5<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>And thus no longer requiring coupon codes to get ALL the discounts.<\/p>\n\n<p>Lastly, if you can rewrite a query that expects 1 result to return every\nrecord in a database is also an easy way to cause a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Denial-of-service_attack\">Denial of service<\/a> attack.<\/p>\n\n<p>This does demonstrate again how is critical validating is and throw errors\nwhenever you get data you don\u2019t expect. Even if under normal circumstances\nnothing weird can happen, a library you use might do the wrong thing with\nunexpected data instead of just rejecting it.<\/p>\n\n<h2 id=\"on-knex\">On Knex<\/h2>\n\n<p>It\u2019s quite unfortunate to see that this went unpatched for so long. I\u2019d\ninvite anyone reading this to try to not pile on on the (likely volunteer)\nauthors but think about the larger ecosystem.<\/p>\n\n<p>This bug was hidden in plain sight. Lots of people must have randomly\nran into it and a ticket was opened. Probably the most responsible thing to\ndo would have been what <a href=\"https:\/\/github.com\/knex\/knex\/issues\/1227#issuecomment-1358165470\">@rgmz<\/a> did: do their best to contact the authors,\nfailing that contact Github Security Team. After the Github Security Team also\nweren\u2019t able to connect to the authors, make a CVE which puts this on every\neveryone\u2019s radar. This ultimately led to a random bystander make their first\ncontribution and <a href=\"https:\/\/github.com\/knex\/knex\/pull\/5417\">submit a fix<\/a>.<\/p>\n\n<p>Knex feels high risk now though, and I can\u2019t help wondering what else might\nbe unpatched. I\u2019ve only recently made the jump from just writing my own queries\nfor like 20 years to Knex, but I\u2019m probably reversing that decision for my\nnext project.<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/json5\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/json5\/"}},"title":"I wish JSON5 was more popular","updated":"2023-01-09T21:29:36+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>As developers we write a lot of code, but we also deal with a lot of\nconfiguration files.<\/p>\n\n<p>The three major formats I tend to use day to day are:<\/p>\n\n<ul>\n  <li>JSON<\/li>\n  <li>YAML<\/li>\n  <li>.env<\/li>\n<\/ul>\n\n<p>And, they all kinda suck. JSON feels like it should\n<em>never<\/em> have become a format that people hand-write. So many quotes, and\nand configuration files <strong>need comments<\/strong> to tell users <em>why<\/em> certain decisions\nwere made. <code class=\"language-plaintext highlighter-rouge\">.env<\/code> has a specific purpose (and it\u2019s ok at that), but it\u2019s not a\ngreat universal format, and YAML has always been difficult to read and write to me.\nI can somehow never retain the syntax and end up copy-pasting things from examples.<\/p>\n\n<h2 id=\"why-yaml-is-difficult-for-me\">Why YAML is difficult for me<\/h2>\n\n<p>A small example from Github workflows\/actions:<\/p>\n\n<div class=\"language-yaml highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"na\">steps<\/span><span class=\"pi\">:<\/span>\n  <span class=\"pi\">-<\/span> <span class=\"na\">uses<\/span><span class=\"pi\">:<\/span> <span class=\"s\">actions\/checkout@v2<\/span>\n  <span class=\"pi\">-<\/span> <span class=\"na\">uses<\/span><span class=\"pi\">:<\/span> <span class=\"s\">actions\/setup-node@v2<\/span>\n    <span class=\"na\">with<\/span><span class=\"pi\">:<\/span>\n      <span class=\"na\">node-version<\/span><span class=\"pi\">:<\/span> <span class=\"m\">14<\/span>\n      <span class=\"na\">registry-url<\/span><span class=\"pi\">:<\/span> <span class=\"s\">https:\/\/registry.npmjs.org\/<\/span>\n  <span class=\"pi\">-<\/span> <span class=\"na\">run<\/span><span class=\"pi\">:<\/span> <span class=\"s\">npm ci<\/span>\n  <span class=\"pi\">-<\/span> <span class=\"na\">run<\/span><span class=\"pi\">:<\/span> <span class=\"s\">npm publish<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>I couldn\u2019t tell you  why <code class=\"language-plaintext highlighter-rouge\">uses<\/code> has a dash in front, and <code class=\"language-plaintext highlighter-rouge\">node-version<\/code> does\nnot. If there\u2019s a difference in how a YAML reader outputs them, I\u2019m not sure\nhow I would be able to retain this while writing YAML.<\/p>\n\n<p>I also use\/love <a href=\"https:\/\/www.home-assistant.io\/\">home assistant<\/a>, which lets you write some pretty cool\nautomations using YAML. I wanted to play with\nthis but it\u2019s been a barrier I\u2019ve not been able to overcome. I don\u2019t know\nif it\u2019s me. I\u2019m been working as a programmer for 22 years. I\u2019m decent at it,\nbut when when I chat with some of my peers (hi mhum!) they did not share my\nsentiment.<\/p>\n\n<p>YAML can also have very <a href=\"https:\/\/www.infoworld.com\/article\/3669238\/7-yaml-gotchas-to-avoidand-how-to-avoid-them.html\">surprising behavior<\/a>, with casting types:<\/p>\n\n<p>From the <a href=\"https:\/\/www.infoworld.com\/article\/3669238\/7-yaml-gotchas-to-avoidand-how-to-avoid-them.html\">linked article<\/a>, this:<\/p>\n\n<div class=\"language-yaml highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"pi\">-<\/span> <span class=\"na\">country1<\/span><span class=\"pi\">:<\/span> <span class=\"s\">ca<\/span>\n<span class=\"pi\">-<\/span> <span class=\"na\">country2<\/span><span class=\"pi\">:<\/span> <span class=\"s\">no<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Becomes:<\/p>\n\n<div class=\"language-yaml highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"pi\">-<\/span> <span class=\"na\">country1<\/span><span class=\"pi\">:<\/span> <span class=\"s\">ca<\/span>\n<span class=\"pi\">-<\/span> <span class=\"na\">country2<\/span><span class=\"pi\">:<\/span> <span class=\"no\">false<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>It\u2019s a bit cherry picked, and I\u2019m sure there\u2019s YAML linters out there that\nhelp avoid the pitfalls, but in my mind configuration files should be simple.<\/p>\n\n<p>There\u2019s some configuration formats I like, such as <a href=\"https:\/\/toml.io\/en\/\">TOML<\/a> and <a href=\"https:\/\/json5.org\/\">JSON5<\/a>.\nThey strike the right balance to me with being easy to read and\nwrite, unambigious, supporting comments, strictness and not being incredibly\nhard to write a parser for.<\/p>\n\n<p>TOML is like ini files on steroids, and JSON5 is JSON but with fewer quotes,\ncomments and multi-line strings.<\/p>\n\n<p>I <em>could<\/em> write my NPM configuration file as <code class=\"language-plaintext highlighter-rouge\">package.json5<\/code> and automatically\nconvert it to <code class=\"language-plaintext highlighter-rouge\">package.json<\/code> but that feels too surprising. My projects are\nalready kind of eclectic, so I want the \u2018plumbing\u2019 to be unsurprising. Plus\nthere\u2019s the whole chicken and egg thing with needing a JSON5 parser before we\nhave dependencies.<\/p>\n\n<p>I\u2019d love the NPM project to adopt JSON5. It seems like a great fit. JSON and\nYAML can\u2019t be the final word for human-maintained data formats. It\u2019s so\nobviously sub-optimal.<\/p>\n\n<p>If NPM adopted JSON5, I would annotate so much in my <code class=\"language-plaintext highlighter-rouge\">package.json<\/code>. I\u2019d\ndocument why a dependency is needed,  why we are stuck using a previous major\nversion of a dependency and what the purpose is of each script.<\/p>\n\n<p>I wouldn\u2019t know what format would be ideal for Github Actions. Maybe the\nanswer is \u2018nothing\u2019 and they need a good DSL.<\/p>\n\n<p>And while we\u2019re at it, stop polluting my projects root directory! Can\u2019t we\nall agree on a <code class=\"language-plaintext highlighter-rouge\">.meta<\/code> directory for finding configuration files?<\/p>\n\n"},{"id":"http:\/\/evertpot.com\/neko\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/neko\/"}},"title":"Neko - A brief history and porting to Javascript","updated":"2022-11-06T21:50:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p><span id=\"neko1\"><\/span><\/p>\n\n<p>In the early 90\u2019s, being a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Frisia\">frisian<\/a> kid obsessed with computers there weren\u2019t a\nton of ways to get access to new software or learn more about computers.<\/p>\n\n<p>The two main ways were exchanging 3.5\u201d diskettes with friends, or go to the\nlibrary. One book I remember more than others was \u201cWindows for Kinderen\u201d\n(\u201cWindows for Kids\u201d) by Addo Stuur.<\/p>\n\n<p>I must have been around 10 years old and was obsessed by this book. It covered\nWindows 3.0, and when you got the book from the library, it came with a diskette\nfilled to the brim with shareware. Mostly games and\nuseless toys, but it still baffles me thinking they were able to cram it all on\na 1.44 megabyte disk. Using floppys from the libraries was even back then a\nrisky business given that they\u2019re writable! Luckily this mostly went ok.<\/p>\n\n<p>One that I remembered particularly well was \u2018Neko\u2019, an application that\nrenders a cat in a window that follows your mouse. This must have been a\npopular thing to make at the time, because the diskette somehow had space\nfor 3(!) different ports of this same application.<\/p>\n\n<p>I don\u2019t know what reminded me of Neko last week, but I started doing some\nmore research, and found out that the first version was written all the\nway back in the 1980\u2019s by Naoshi Watanabe for the <a href=\"https:\/\/en.wikipedia.org\/wiki\/PC-9800_series\">NEC PC 9801<\/a>.<\/p>\n\n<figure>\n  <img src=\"\/assets\/posts\/neko\/neko-pc9801.gif\" alt=\"Neko for the NEC PC 9801\" style=\"width: 320px; max-width: 100%; image-rendering: crisp-edges; image-rendering: pixelated\" \/><br \/>\n  <figcaption>Neko for the NEC PC 9801 (1980's)<\/figcaption>\n<\/figure>\n\n<p>After that, it was ported to the Macintosh in 1989 by Kenji Gotoh, and this\nart style seems to be the basis of almost every future port:<\/p>\n\n<figure>\n  <img src=\"\/assets\/posts\/neko\/neko-macintosh.png\" alt=\"Neko on Macintosh\" style=\"width: 584px; max-width: 100%; image-rendering: crisp-edges; image-rendering: pixelated\" \/>\n  <figcaption>Neko on Macintosh (1989)<\/figcaption>\n<\/figure>\n\n<p>In 1991, IBM included it in OS\/2! Apparently they paid 300,000 YEN, which\nis about $3000 CAD in todays money. At this point it also became public\ndomain.<\/p>\n\n<figure>\n  <img src=\"\/assets\/posts\/neko\/neko-os2.png\" alt=\"Neko on OS\/2\" style=\"max-width: 100%\" \/>\n  <figcaption>Neko on OS\/2 (1991)<\/figcaption>\n<\/figure>\n\n<p>Since then there\u2019s been countless ports for every platform. If you\u2019re running\nlinux you might be able to install one by just running:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>apt <span class=\"nb\">install <\/span>oneko\n<\/code><\/pre><\/div><\/div>\n\n<p>I also decided to <a href=\"https:\/\/github.com\/evert\/jneko\" title=\"jneko source\">make a version<\/a>. Neko is now close to 40, so my Neko is\nno longer interested in following the mouse, and prefers to just sleep all day\nunless you wake it.<\/p>\n\n<div id=\"neko2\"><\/div>\n\n<p>If you want to know more, the Eliot Akira wrote <a href=\"https:\/\/eliotakira.com\/neko\/\" title=\"Neko: History of a Software Pet\">a great article with way\nmore information about Neko and all its ports<\/a>,\nthis is also where I took the screenshots from this page.<\/p>\n\n<p>You can also check out the <a href=\"https:\/\/github.com\/evert\/jneko\" title=\"jneko source\">source<\/a> of my port \u2018jneko\u2019. It uses the assets\nof <a href=\"https:\/\/github.com\/tie\/oneko\" title=\"ONeko source\">oneko<\/a>, which are public domain. It was a fun exercise to create a\nlittle state machine.<\/p>\n\n<h2 id=\"creating-the-assets\">Creating the assets<\/h2>\n\n<p>To create the assets for <a href=\"https:\/\/github.com\/evert\/jneko\" title=\"jneko source\">jneko<\/a>, I had to convert them from the <code class=\"language-plaintext highlighter-rouge\">.xbm<\/code>\nformat from <a href=\"https:\/\/github.com\/tie\/oneko\" title=\"ONeko source\">oneko<\/a>.<\/p>\n\n<p>I did this with <code class=\"language-plaintext highlighter-rouge\">imagemagick<\/code> on the command line using a command like this\nto loop through all the files:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">for <\/span>f <span class=\"k\">in<\/span> ..\/oneko\/bitmaps\/neko\/<span class=\"k\">*<\/span>.xbm\n  convert <span class=\"s2\">\"<\/span><span class=\"nv\">$f<\/span><span class=\"s2\">\"<\/span> <span class=\"sb\">`<\/span><span class=\"nb\">basename<\/span> <span class=\"s2\">\"<\/span><span class=\"k\">${<\/span><span class=\"nv\">f<\/span><span class=\"p\">%.xbm<\/span><span class=\"k\">}<\/span><span class=\"s2\">.png\"<\/span><span class=\"sb\">`<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Sadly I still had to open each one of them with Gimp and add the alpha layer.<\/p>\n\n<p>I hadn\u2019t really heard of the <code class=\"language-plaintext highlighter-rouge\">.xbm<\/code> format, but when I accidentally opened\none with an editor I was surprised to see that they\u2019re actually a text\nformat:<\/p>\n\n<div class=\"language-c highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#define awake_width 32\n#define awake_height 32\n<\/span><span class=\"k\">static<\/span> <span class=\"kt\">char<\/span> <span class=\"n\">awake_bits<\/span><span class=\"p\">[]<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x20<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x40<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x10<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x10<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x80<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x28<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x28<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x01<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x49<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x24<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x06<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x44<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x44<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x60<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x18<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x84<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x42<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x18<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x60<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x82<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x83<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x06<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x80<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x22<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x88<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x0f<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x22<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x88<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x78<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x22<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x88<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x80<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x3a<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xb9<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x40<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x08<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x20<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x70<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x1c<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x40<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x05<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x20<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x88<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x10<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x50<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x08<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x20<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x01<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x0b<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xa0<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x01<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x80<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x0c<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x61<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x02<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x40<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x18<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x31<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x40<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x10<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x11<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x04<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xc0<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x11<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x11<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x07<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x60<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x90<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x13<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x0c<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xe0<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xff<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0xfe<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x0f<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span>\n   <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">,<\/span> <span class=\"mh\">0x00<\/span><span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Turns out this is the <a href=\"https:\/\/en.wikipedia.org\/wiki\/X_BitMap\" title=\"X BitMap file format\">X BitMap<\/a> format, which has the interesting property\nthat it aside from an image format, they\u2019re also valid C source files.\nThis has the advantage that they can easily be statically compiled into C files without\nrequiring a parser.<\/p>\n\n<p>This is really similar to the history of the JSON format, which was designed\nto be a subset of Javascript, allowing Javascript programs to read the\nformat without a new parser (using <code class=\"language-plaintext highlighter-rouge\">eval<\/code>). Ironically the current JSON format\nis no longer a subset of Javascript, and Javascript engines now have a\n<code class=\"language-plaintext highlighter-rouge\">JSON.parse()<\/code> function built in.<\/p>\n\n<h2 id=\"a-deep-dive-into-the-source\">A deep dive into the source<\/h2>\n\n<p>jneko uses a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Finite-state_machine\">state machine<\/a>. All its possible states and transitions are\nexpressed with this configuration object:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">stateMachine<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"c1\">\/\/ Name of the state<\/span>\n  <span class=\"na\">sleep<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"c1\">\/\/ Which images are used for the state<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"dl\">'<\/span><span class=\"s1\">sleep1<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">sleep2<\/span><span class=\"dl\">'<\/span><span class=\"p\">],<\/span>\n\n    <span class=\"c1\">\/\/ How quickly we loop through these images<\/span>\n    <span class=\"na\">imageInterval<\/span><span class=\"p\">:<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span>\n\n    <span class=\"c1\">\/\/ What state does this go to when clicked<\/span>\n    <span class=\"na\">click<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">awake<\/span><span class=\"dl\">'<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">awake<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">awake<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n\n    <span class=\"c1\">\/\/ We automaticall transition to this state<\/span>\n    <span class=\"na\">nextState<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n\n    <span class=\"c1\">\/\/ How long it takes to transition<\/span>\n    <span class=\"na\">nextStateDelay<\/span><span class=\"p\">:<\/span> <span class=\"mf\">2.5<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">normal<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">mati2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n\n    <span class=\"c1\">\/\/ If there's multiple nextState values, a random one is picked.<\/span>\n    <span class=\"c1\">\/\/ You make make 1 state more likely by addding it multiple times<\/span>\n    <span class=\"c1\">\/\/ to the array<\/span>\n    <span class=\"na\">nextState<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">tilt<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">scratch<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">yawn<\/span><span class=\"dl\">'<\/span><span class=\"p\">],<\/span>\n    <span class=\"na\">nextStateDelay<\/span><span class=\"p\">:<\/span> <span class=\"mf\">1.5<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">tilt<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">jare2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n    <span class=\"na\">nextState<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n    <span class=\"na\">nextStateDelay<\/span><span class=\"p\">:<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">yawn<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">mati3<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n    <span class=\"na\">nextState<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">sleep<\/span><span class=\"dl\">'<\/span><span class=\"p\">],<\/span>\n    <span class=\"na\">nextStateDelay<\/span><span class=\"p\">:<\/span> <span class=\"mi\">1<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">},<\/span>\n  <span class=\"na\">scratch<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"na\">image<\/span><span class=\"p\">:<\/span> <span class=\"p\">[<\/span><span class=\"dl\">'<\/span><span class=\"s1\">kaki1<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">kaki2<\/span><span class=\"dl\">'<\/span><span class=\"p\">],<\/span>\n    <span class=\"na\">imageInterval<\/span><span class=\"p\">:<\/span> <span class=\"mf\">0.1<\/span><span class=\"p\">,<\/span>\n    <span class=\"na\">nextState<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">normal<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n    <span class=\"na\">nextStateDelay<\/span><span class=\"p\">:<\/span> <span class=\"mi\">3<\/span><span class=\"p\">,<\/span>\n  <span class=\"p\">}<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Before Neko starts, we need to preload the images. This ensures that the\nanimations happen instantly and not after a delay.<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cm\">\/**\n * The filenames are a bit funny because they were taken straight from\n * the oneko project.\n *\/<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">imageNames<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">awake<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">jare2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">kaki1<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">kaki2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">mati2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">mati3<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">sleep1<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n  <span class=\"dl\">'<\/span><span class=\"s1\">sleep2<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">];<\/span>\n<span class=\"cm\">\/**\n * Images are actually 32x32 but it's too small for modern screens.\n *\/<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">nekoSize<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">64<\/span><span class=\"p\">;<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">images<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">Object<\/span><span class=\"p\">.<\/span><span class=\"nx\">fromEntries<\/span><span class=\"p\">(<\/span><span class=\"nx\">imageNames<\/span><span class=\"p\">.<\/span><span class=\"nx\">map<\/span><span class=\"p\">(<\/span><span class=\"nx\">name<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">image<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Image<\/span><span class=\"p\">(<\/span><span class=\"nx\">nekoSize<\/span><span class=\"p\">,<\/span> <span class=\"nx\">nekoSize<\/span><span class=\"p\">);<\/span>\n  <span class=\"nx\">image<\/span><span class=\"p\">.<\/span><span class=\"nx\">src<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">\/assets\/posts\/neko\/bitmaps\/<\/span><span class=\"dl\">'<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">name<\/span> <span class=\"o\">+<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">.png<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">return<\/span> <span class=\"p\">[<\/span><span class=\"nx\">name<\/span><span class=\"p\">,<\/span> <span class=\"nx\">image<\/span><span class=\"p\">]<\/span>\n<span class=\"p\">}));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Finally, this class does all the heavy lifting:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">class<\/span> <span class=\"nx\">Neko<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"kd\">constructor<\/span><span class=\"p\">(<\/span><span class=\"nx\">elem<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"c1\">\/\/ The HTML element that hosts neko<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">elem<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">elem<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">stateMachine<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">stateMachine<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"c1\">\/\/ Creating a new &lt;img&gt; element<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imgElem<\/span> <span class=\"o\">=<\/span>  <span class=\"k\">new<\/span> <span class=\"nx\">Image<\/span><span class=\"p\">(<\/span><span class=\"nx\">nekoSize<\/span><span class=\"p\">,<\/span> <span class=\"nx\">nekoSize<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"nx\">elem<\/span><span class=\"p\">.<\/span><span class=\"nx\">appendChild<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imgElem<\/span><span class=\"p\">);<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imgElem<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">click<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">onClick<\/span><span class=\"p\">());<\/span>\n\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">setState<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">sleep<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n\n  <span class=\"p\">}<\/span>\n\n  <span class=\"cm\">\/**\n   * This property is used to keep track of states with multiple frames\n   *\/<\/span>\n  <span class=\"err\">#<\/span><span class=\"nx\">animationIndex<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n  <span class=\"nx\">renderImage<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"kd\">let<\/span> <span class=\"nx\">name<\/span> <span class=\"o\">=<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">stateMachine<\/span><span class=\"p\">[<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">state<\/span><span class=\"p\">].<\/span><span class=\"nx\">image<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">animationIndex<\/span><span class=\"o\">++<\/span><span class=\"p\">;<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nb\">Array<\/span><span class=\"p\">.<\/span><span class=\"nx\">isArray<\/span><span class=\"p\">(<\/span><span class=\"nx\">name<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n      <span class=\"nx\">name<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">name<\/span><span class=\"p\">[<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">animationIndex<\/span> <span class=\"o\">%<\/span> <span class=\"nx\">name<\/span><span class=\"p\">.<\/span><span class=\"nx\">length<\/span><span class=\"p\">];<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imgElem<\/span><span class=\"p\">.<\/span><span class=\"nx\">src<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">images<\/span><span class=\"p\">[<\/span><span class=\"nx\">name<\/span><span class=\"p\">].<\/span><span class=\"nx\">src<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n\n  <span class=\"err\">#<\/span><span class=\"nx\">state<\/span> <span class=\"o\">=<\/span> <span class=\"kc\">null<\/span><span class=\"p\">;<\/span>\n  <span class=\"err\">#<\/span><span class=\"nx\">nextStateTimeout<\/span> <span class=\"o\">=<\/span> <span class=\"kc\">null<\/span><span class=\"p\">;<\/span>\n  <span class=\"err\">#<\/span><span class=\"nx\">imageCycleInterval<\/span> <span class=\"o\">=<\/span> <span class=\"kc\">null<\/span><span class=\"p\">;<\/span>\n\n  <span class=\"nx\">setState<\/span><span class=\"p\">(<\/span><span class=\"nx\">stateName<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n    <span class=\"nx\">clearTimeout<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">nextStateTimeout<\/span><span class=\"p\">);<\/span>\n    <span class=\"nx\">clearInterval<\/span><span class=\"p\">(<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imageCycleInterval<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nb\">Array<\/span><span class=\"p\">.<\/span><span class=\"nx\">isArray<\/span><span class=\"p\">(<\/span><span class=\"nx\">stateName<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n      <span class=\"c1\">\/\/ If stateName was supplied as an array of strings, we'll randomly<\/span>\n      <span class=\"c1\">\/\/ pick a new state.<\/span>\n      <span class=\"nx\">stateName<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">stateName<\/span><span class=\"p\">[<\/span><span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nx\">floor<\/span><span class=\"p\">(<\/span><span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nx\">random<\/span><span class=\"p\">()<\/span><span class=\"o\">*<\/span><span class=\"p\">(<\/span><span class=\"nx\">stateName<\/span><span class=\"p\">.<\/span><span class=\"nx\">length<\/span><span class=\"p\">))];<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">stateMachine<\/span><span class=\"p\">[<\/span><span class=\"nx\">stateName<\/span><span class=\"p\">])<\/span> <span class=\"p\">{<\/span>\n      <span class=\"k\">throw<\/span> <span class=\"k\">new<\/span> <span class=\"nb\">Error<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">Uknown state: <\/span><span class=\"dl\">'<\/span> <span class=\"o\">+<\/span> <span class=\"nx\">stateName<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">state<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">stateName<\/span><span class=\"p\">;<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">stateData<\/span> <span class=\"o\">=<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">stateMachine<\/span><span class=\"p\">[<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">state<\/span><span class=\"p\">];<\/span>\n\n    <span class=\"c1\">\/\/ If there was a nextState, we automatically transition there after<\/span>\n    <span class=\"c1\">\/\/ a delay.<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">nextState<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n      <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">nextStateTimeout<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">setTimeout<\/span><span class=\"p\">(<\/span>\n        <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">setState<\/span><span class=\"p\">(<\/span><span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">nextState<\/span><span class=\"p\">),<\/span>\n        <span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">nextStateDelay<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">1000<\/span>\n      <span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"c1\">\/\/ This block is responsible for cycling through multiple images<\/span>\n    <span class=\"c1\">\/\/ of the current state.<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">imageInterval<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n\n      <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">imageCycleInterval<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">setInterval<\/span><span class=\"p\">(<\/span>\n        <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">renderImage<\/span><span class=\"p\">(),<\/span>\n        <span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">imageInterval<\/span><span class=\"o\">*<\/span><span class=\"mi\">1000<\/span>\n      <span class=\"p\">);<\/span>\n\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">renderImage<\/span><span class=\"p\">();<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"nx\">onClick<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n    <span class=\"kd\">const<\/span> <span class=\"nx\">stateData<\/span> <span class=\"o\">=<\/span> <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">stateMachine<\/span><span class=\"p\">[<\/span><span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"err\">#<\/span><span class=\"nx\">state<\/span><span class=\"p\">];<\/span>\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">click<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n      <span class=\"c1\">\/\/ If the current state had a 'click' property, we'll transition<\/span>\n      <span class=\"c1\">\/\/ to that state, otherwise it's ignored.<\/span>\n      <span class=\"k\">this<\/span><span class=\"p\">.<\/span><span class=\"nx\">setState<\/span><span class=\"p\">(<\/span><span class=\"nx\">stateData<\/span><span class=\"p\">.<\/span><span class=\"nx\">click<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n  <span class=\"p\">}<\/span>\n\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Now if you\u2019d like to call Neko, make a <code class=\"language-plaintext highlighter-rouge\">&lt;div id=\"neko\"&gt;&lt;\/div&gt;<\/code> somewhere. and\nhook up Neko with:<\/p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">new<\/span> <span class=\"nx\">Neko<\/span><span class=\"p\">(<\/span><span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">getElementById<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">neko<\/span><span class=\"dl\">'<\/span><span class=\"p\">));<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>To make Neko not look ugly, tell the browser to not use anti-aliasing when\nscaling:<\/p>\n\n<div class=\"language-css highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nf\">#neko<\/span> <span class=\"nt\">img<\/span> <span class=\"p\">{<\/span>\n  <span class=\"c\">\/* Both are needed to support all browsers currently. *\/<\/span>\n  <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">pixelated<\/span><span class=\"p\">;<\/span>\n  <span class=\"nl\">image-rendering<\/span><span class=\"p\">:<\/span> <span class=\"n\">crisp-edges<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Thanks for reading! If you have any comments you can reply to <a href=\"https:\/\/twitter.com\/evertp\/status\/1589376231157690368\">this tweet<\/a>\nor <a href=\"https:\/\/indieweb.social\/web\/@evert\/109299079542445353\">this mastadon post<\/a> to make it show up below this article automatically.<\/p>\n\n<div id=\"neko3\"><\/div>\n\n<script src=\"\/assets\/posts\/neko\/jneko.js\"><\/script>\n\n<script>\n\nfunction loadNeko() {\n\n  new Neko(document.getElementById('neko1'));\n  new Neko(document.getElementById('neko2'));\n  new Neko(document.getElementById('neko3'));\n\n}\n\nwindow.addEventListener('DOMContentLoaded', () => loadNeko());\n<\/script>\n\n"},{"id":"http:\/\/evertpot.com\/hello-mastodon\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/hello-mastodon\/"}},"title":"Taking a look at Mastodon","updated":"2022-11-01T19:35:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p>I\u2019ve been a Twitter user and fan since 2007. With Twitter\u2019s future\nlooking a bit grim, I started looking around if there\u2019s another place to go.<\/p>\n\n<p>Twitter can\u2019t really be replaced with anything else, because everyone\u2019s\nTwitter experience is unique to them and their community. For me, it\u2019s\nthe main way I stay in touch with my Open Source \/ HTTP \/ API \/ Hypermedia\nbubble and some friends. Losing that would suck! Unfortunately, there\u2019s no way\nthat group would all flock to another platform.<\/p>\n\n<p>But for the ones that do try something else, the talk of the town seems\nto be <a href=\"https:\/\/joinmastodon.org\/\">Mastodon<\/a>.<\/p>\n\n<p>Mastodon is interesting. On the surface it might just seem like a\nTwitter clone, but it\u2019s based on a federated protocol called \u2018ActivityPub\u2019.\nWhat this means in practice is that there\u2019s no central server. There\u2019s\nmany <a href=\"https:\/\/joinmastodon.org\/servers\">instances<\/a>. Each of these instances\nis managed by different people, and many of them focus on specific interests.<\/p>\n\n<p>With email, it doesn\u2019t matter which provider you go with. Thanks to universal\nSMTP standards that every server uses, you can exchange messages with everyone\nelse. This is the same with Mastodon. You\u2019re not siloed into a single instance,\nand you can follow people from any other instance. Unlike email, it appears\nthat with Mastodon you can actually migrate to different instances if you don\u2019t\nlike your current one.<\/p>\n\n<p>This has some interesting side effects too. I joined the\n<a href=\"https:\/\/indieweb.org\/\">IndieWeb<\/a> instance, which is a community I already\nloved. And even though I\u2019m not siloed in, I get access to a local feed of\nlike-minded people from that community. Everything feels new and more\nintimate.<\/p>\n\n<p>Also, instead of one central authority that you have to trust to make the right\nmoderation decisions, you can join one of many that aligns with your values,\nand you can block entire instances that don\u2019t.<\/p>\n\n<p>So should you join? If you use Twitter to stay on top of news and follow high\nprofile people then probably not. If you\u2019re like me, you might be able to\nfind a community that fits your interest.<\/p>\n\n<p>Will I stick to this? Who knows\u2026 but Twitter, like everything before,\nwill fall out of favor one day and I\u2019m enjoying Mastodon\u2019s ad-free, open source,\ndeveloper-friendly experience. Reminds me of early Twitter or mid-2000\u2019s\nblogging culture.<\/p>\n\n<h2 id=\"links\">Links<\/h2>\n\n<ul>\n  <li>If you\u2019re just getting started, check out <a href=\"https:\/\/joinmastodon.org\/\">https:\/\/joinmastodon.org\/<\/a> and\nfind a server.<\/li>\n  <li><a href=\"https:\/\/indieweb.social\/web\/@evert\">Follow me!<\/a><\/li>\n  <li>To find your Twitter friends on Mastodon try <a href=\"https:\/\/twitodon.com\/\">Twitodon<\/a> and <a href=\"https:\/\/fedifinder.glitch.me\/\">Fedifinder<\/a>.<\/li>\n  <li>To automatically cross-post everything from Twitter to Mastodon and vice versa, use <a href=\"https:\/\/moa.party\/\">Moa<\/a>.<\/li>\n<\/ul>\n\n<p>Lastly, one of the interesting results of Mastodon building on open protocols,\nis that it allows alternative implementations.<\/p>\n\n<p>The <a href=\"https:\/\/microblog.pub\/\">Microblog.pub<\/a> project lets you self-host a\nsingle-user instance. Instead of joining some large instance, you deploy\nan instance on your own domain that\u2019s just for you. Can\u2019t get more control\nthan that, and this might be something I\u2019ll consider in the future.<\/p>\n\n<p>I don\u2019t see why this blog couldn\u2019t one day also be a \u2018microblog\u2019 and part of the\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Fediverse\">fediverse<\/a>.<\/p>\n"},{"id":"http:\/\/evertpot.com\/bun-curveball-framework\/","link":{"@attributes":{"type":"text\/html","rel":"alternate","href":"https:\/\/evertpot.com\/bun-curveball-framework\/"}},"title":"Porting Curveball to Bun","updated":"2022-09-13T16:30:00+00:00","author":{"name":"Evert Pot","email":"me@evertpot.com"},"content":"<p><a href=\"https:\/\/bun.sh\/\">Bun<\/a> is the hot new server-side Javascript runtime, in the same category\nas <a href=\"https:\/\/nodejs.org\/\">Node<\/a> and <a href=\"https:\/\/deno.land\/\">Deno<\/a>. Bun uses the <a href=\"https:\/\/github.com\/WebKit\/WebKit\/tree\/main\/Source\/JavaScriptCore\">JavascriptCore<\/a> engine from\nWebkit, unlike Node and Deno which use <a href=\"https:\/\/v8.dev\/\">V8<\/a>. A big selling point is that\nit\u2019s coming out faster in a many benchmarks, however the things I\u2019m personally\nexcited about is some of it\u2019s quality of life features:<\/p>\n\n<ul>\n  <li>It parses Typescript and JSX by default (but doesn\u2019t type check), which\nmeans there\u2019s no need for a separate \u2018dist\u2019 directory, or a separate tool\nlike <code class=\"language-plaintext highlighter-rouge\">ts-node<\/code>.<\/li>\n  <li>It loads <code class=\"language-plaintext highlighter-rouge\">.env<\/code> files by default.<\/li>\n  <li>It\u2019s compatible with NPM, <code class=\"language-plaintext highlighter-rouge\">package.json<\/code>, and many built-in Node modules.<\/li>\n<\/ul>\n\n<p>I also like that it\u2019s \u2018Hello world HTTP server\u2019 is as simple as writing this\nfile:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ http.ts<\/span>\n<span class=\"k\">export<\/span> <span class=\"k\">default<\/span> <span class=\"p\">{<\/span>\n  <span class=\"na\">port<\/span><span class=\"p\">:<\/span> <span class=\"mi\">3000<\/span><span class=\"p\">,<\/span>\n  <span class=\"nx\">fetch<\/span><span class=\"p\">(<\/span><span class=\"na\">request<\/span><span class=\"p\">:<\/span> <span class=\"nx\">Request<\/span><span class=\"p\">):<\/span> <span class=\"nb\">Promise<\/span><span class=\"o\">&lt;<\/span><span class=\"nx\">Response<\/span><span class=\"o\">&gt;<\/span> <span class=\"p\">{<\/span>\n    <span class=\"k\">return<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Response<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">Hello world!<\/span><span class=\"dl\">\"<\/span><span class=\"p\">);<\/span>\n  <span class=\"p\">},<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>And then running it with:<\/p>\n\n<div class=\"language-sh highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code>bun run http.ts\n<\/code><\/pre><\/div><\/div>\n\n<p>Bun will recognize that an object with a <code class=\"language-plaintext highlighter-rouge\">fetch<\/code> function was default-exported,\nand start a server on port 3000. As you can see here, this uses the standard\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Request\">Request<\/a> and <a href=\"https:\/\/github.com\/curveball\/aws-lambda\">Response<\/a> objects you use in a browser, and can use\nasync\/await.<\/p>\n\n<p>These are all things that didn\u2019t exist when Node and Express were first\ncreated, but seem like pretty good ideas for something built today. I don\u2019t think\nusing <code class=\"language-plaintext highlighter-rouge\">Request<\/code> and <code class=\"language-plaintext highlighter-rouge\">Response<\/code> are good for more complex use-cases (streaming\nresponses, 1xx responses, trailers, upgrading to other protocols, getting tcp\nconnection metadata like remoteAddr are some that come to mind),\nbecause these objects are designed for clients first.<\/p>\n\n<p>But in many cases people are just building simple endpoints, and for that it\u2019s\ngreat.<\/p>\n\n<p>Bun supports a ton of the standard Node modules, but it\u2019s also missing some\nsuch as support for server-side websockets and the node http\/https\/https\npackages, which for now makes it incompatible with popular frameworks like\nExpress.<\/p>\n\n<h2 id=\"porting-curveball\">Porting Curveball<\/h2>\n\n<p><a href=\"https:\/\/curveballjs.org\/\">Curveball<\/a> is a Typescript micro-framework we\u2019ve been developing since\nmid-2018 as a modern replacement for Express and Koa. A key difference between\nCurveball and these two frameworks is that it fully abstracts and encapsulates\nthe core \u2018Request\u2019 and \u2018Response\u2019 objects Node provides.<\/p>\n\n<p>This made it very easy to create a lambda integration in the past; instead of\nmapping to Node\u2019s Request and Response types, All I needed was simple mapping\nfunction for Lambdas idea of what a request and response looks like.<\/p>\n\n<p>To get Express to run on AWS Lambda the Node <code class=\"language-plaintext highlighter-rouge\">http<\/code> stack needs to be emulated, or\na full-blown HTTP\/TCP server needs to be started and proxied to. Each of these\nworkarounds require a ton of code from libraries like <a href=\"https:\/\/github.com\/vendia\/serverless-express\">serverless-express<\/a>.<\/p>\n\n<p>So with Bun up and coming, either the same work would need to be done to emulate\nNode\u2019s APIs, or Bun would would need to add full compability for the Node <code class=\"language-plaintext highlighter-rouge\">http<\/code>\nmodule (which is eventually coming).<\/p>\n\n<p>But because of Curveball\u2019s message abstractions it was relatively easy to get\nup and running. Most of the work was moving the Node-specific code into a new\npackage.<\/p>\n\n<p>Here\u2019s the Curveball \u2018hello world\u2019 on Bun:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">Application<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@curveball\/kernel<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">app<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Application<\/span><span class=\"p\">();<\/span>\n\n<span class=\"c1\">\/\/ Add all your middlewares here! This should look familiar to Koa users.<\/span>\n<span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nx\">use<\/span><span class=\"p\">(<\/span> <span class=\"nx\">ctx<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span><span class=\"na\">msg<\/span><span class=\"p\">:<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">hello world!<\/span><span class=\"dl\">'<\/span><span class=\"p\">};<\/span> \n<span class=\"p\">});<\/span>\n\n<span class=\"k\">export<\/span> <span class=\"k\">default<\/span> <span class=\"p\">{<\/span>\n  <span class=\"na\">port<\/span><span class=\"p\">:<\/span> <span class=\"mi\">3000<\/span><span class=\"p\">,<\/span>\n  <span class=\"na\">fetch<\/span><span class=\"p\">:<\/span> <span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nx\">fetch<\/span><span class=\"p\">.<\/span><span class=\"nx\">bind<\/span><span class=\"p\">(<\/span><span class=\"nx\">app<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>It\u2019s still a bit experimental, but the following middlewares are tested:<\/p>\n\n<ul>\n  <li><a href=\"https:\/\/github.com\/curveball\/router\">router<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/controller\">controller<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/cors\">cors<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/accesslog\">accesslog<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/bodyparser\">bodyparser<\/a><\/li>\n  <li><a href=\"https:\/\/github.com\/curveball\/validator\">validator<\/a><\/li>\n<\/ul>\n\n<p>And because JSX also just works in Bun, it\u2019s relatively easy to use it to\ngenerate HTML:<\/p>\n\n<div class=\"language-typescript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"k\">import<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">Application<\/span> <span class=\"p\">}<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@curveball\/kernel<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"nx\">reactMw<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">@curveball\/react<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">import<\/span> <span class=\"nx\">React<\/span> <span class=\"k\">from<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">react<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n\n<span class=\"kd\">const<\/span> <span class=\"nx\">app<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nx\">Application<\/span><span class=\"p\">();<\/span>\n\n<span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nx\">use<\/span><span class=\"p\">(<\/span><span class=\"nx\">reactMw<\/span><span class=\"p\">());<\/span>\n\n<span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nx\">use<\/span><span class=\"p\">(<\/span> <span class=\"nx\">ctx<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"p\">{<\/span>\n\n  <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"kd\">type<\/span> <span class=\"o\">=<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">text\/html; charset=utf-8<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n  <span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nx\">response<\/span><span class=\"p\">.<\/span><span class=\"nx\">body<\/span> <span class=\"o\">=<\/span> <span class=\"o\">&lt;<\/span><span class=\"nx\">html<\/span><span class=\"o\">&gt;<\/span>\n    <span class=\"o\">&lt;<\/span><span class=\"nx\">body<\/span><span class=\"o\">&gt;<\/span>\n      <span class=\"o\">&lt;<\/span><span class=\"nx\">div<\/span><span class=\"o\">&gt;<\/span>\n        <span class=\"o\">&lt;<\/span><span class=\"nx\">h1<\/span><span class=\"o\">&gt;<\/span><span class=\"nx\">Hello<\/span> <span class=\"nx\">world<\/span><span class=\"o\">!&lt;<\/span><span class=\"sr\">\/h1<\/span><span class=\"err\">&gt;\n<\/span>      <span class=\"o\">&lt;<\/span><span class=\"sr\">\/div<\/span><span class=\"err\">&gt;\n<\/span>    <span class=\"o\">&lt;<\/span><span class=\"sr\">\/body<\/span><span class=\"err\">&gt;\n<\/span>  <span class=\"o\">&lt;<\/span><span class=\"sr\">\/html&gt;<\/span><span class=\"err\">;\n<\/span>\n<span class=\"p\">});<\/span>\n\n<span class=\"k\">export<\/span> <span class=\"k\">default<\/span> <span class=\"p\">{<\/span>\n  <span class=\"na\">port<\/span><span class=\"p\">:<\/span> <span class=\"mi\">3000<\/span><span class=\"p\">,<\/span>\n  <span class=\"na\">fetch<\/span><span class=\"p\">:<\/span> <span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nx\">fetch<\/span><span class=\"p\">.<\/span><span class=\"nx\">bind<\/span><span class=\"p\">(<\/span><span class=\"nx\">app<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">};<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>This is not a full-blown hydration\/server-rendering solution, but JSX has\nreplaced template engines like EJS and Handlebars for us at\n<a href=\"https:\/\/badgateway.net\/\">Bad Gateway<\/a>. JSX lets you do the same things, but you get the advantage\nof type checking, it\u2019s harder to create XSS bugs and full Javascript access.<\/p>\n\n<h2 id=\"final-thoughts-on-bun\">Final thoughts on Bun<\/h2>\n\n<p>Compared to Deno, It was considerably easier to port Curveball to Bun.\nDeno was a much greater challenge, due to it having such a radically\ndifferent idea about packages and module loading. This was over a year\nago, so it\u2019s worth giving a shot again.<\/p>\n\n<p>So, I\u2019m curious where all of this goes! Perhaps Bun is the future, or\nperhaps we\u2019ll see Node get parity with Bun and making it obsolete. Either\nway competition is good.<\/p>\n\n<p>I feel Bun has a better chance than Deno, because it delivers many of\nits advantages and features while mostly staying inside with the Node\necosystem.<\/p>\n\n<p>Although as it turns out Deno also has changed their tune and also made\nit <a href=\"https:\/\/deno.com\/blog\/changes\">a new goal<\/a> to be compatible. I can\u2019t help wondering if this was\ninspired by Bun\u2019s recent success as well.<\/p>\n\n"}]}