txiki.js 22.11 released with FFI, WebSocket and more!

txiki.js is a small and powerful JavaScript runtime. It’s built on the shoulders of giants: it uses QuickJS as its JavaScript engine, libuv as the platform layer, wasm3 as the WebAssembly engine and curl as the HTTP client.

Today I’m happy to release txiki.js 22.11, which contains a bunch of improvements that have happened during the last few months. Let’s look at some of those!

FFI

This is a nice one. Not only was it on the list of things I wanted to implement, but it came in as a contribution! Tremendous work went into the PR by Luca, and I’m very happy to have this in. Now you can write things like:

const ffi = tjs.ffi;
const libc = new ffi.Lib(ffi.Lib.LIBC_NAME);
const atoiF
= new ffi.CFunction(libc.symbol('atoi'), ffi.types.sint, [ffi.types.string]); 
atoiF.call("1234");
> 1234

WebSocket

One of the design goals of txiki.js is to implement the Web Platform APIs which Make Sense (TM). WebSocket support is of course one such feature, but it took me quite a while to figure out how to go about it.

For XHR / fetch I used curl for it’s versatility and universality. I’m actually quite happy with how xhr.c ended up looking, but at the time I didn’t know I could do a similar operation for websockets.

At this point I entered a bit of an analysis paralysis phase. I wouldn’t work on txiki.js because I didn’t know how I’d implement WebSocket support in a way I felt comfortable with, so I put that on the back burner.

Fast forward a few months and I ran into this gem while randomly browsing concord, a Discord bot library written in C. Turns out Gustavo Barbieri had written that a couple of years ago. It’s a beautiful hack, allowing one write WebSocket clients using curl. Amazing.

Funny enough, curl has added WebSocket support too! It is, however, experimental, and since it was just released it will likely take a bit of time to trickle down to distros and operating systems, so I’ll keep using the current approach for the time being.

More!

This release is packed with small improvements and API additions, give the release notes a read!

An interesting fact about this release is that it’s the one with more outside contributions. I’ve been working on Open Source for quite a while now, but that feeling of receiving great PRs by strangers on your little project never gets old. I feel like I never walk alone.

Onward

There are many things I’d like to add to txiki.js like an HTTP / WebSocket server, SQLite support, a better CLI experience, top level await (via some hack like Node used to do perhaps) and many more. Stay tuned!

Leave a Reply

Your email address will not be published. Required fields are marked *