I just built another Cloudflare Worker using Kotli...
# feed
s
I just built another Cloudflare Worker using Kotlin/JS! This one interacts with Cloudflare D1, their hosted SQLite database. You can check out the code here: https://github.com/StefanOltmann/cloudflare-oni-user-coordinate-like-service There are hardly any examples of using Cloudflare with Kotlin, so it took a lot of trial and error. I hope this can save you some time and be helpful! :)
K 3
m
As you have switched to the Kotlin Wrappers it might be a good idea to be consistent in that otherwise it might be confusing for people. So instead of using
Copy code
import org.w3c.fetch.Headers
import org.w3c.fetch.Request
import org.w3c.fetch.Response
import org.w3c.fetch.ResponseInit
you should better use
Copy code
import web.http.Headers
import web.http.Request
import web.http.Response
import web.http.ResponseInit
s
@Michael Paus Thanks for the suggestion. I gave it a try, and while the code does look cleaner, it ends up doubling the generated JavaScript — from about 4k to 8k lines. So in this case, I’ll prioritize size over consistency. Honestly, I’d even skip the wrappers for subtle-crypto if it were easier to use.
Cloudflare workers must complete in under 10 ms. That's the challenge. The original JS code ran for 2. My optimized Kotlin version is now around 6 ms. The first thing I had ran for 26 ms and started to get killed constantly.
m
Did you try to find out why the generated code gets bigger? Just changing the types shouldn’t make such a difference. The implementation of the code should still be the same and be provided by the environment. Maybe there is a clean-up step missing.
s
I was unable to to webpack / tree-shaking. That seems to be not so easy. Webpack also touches the entry points and breaks everything. I don't know if there is something like a proguard config where you can tell it to not touch XY.
Victor advised me to use an external tooling, but that's an more complicated setup than what I have in mind.
The execution seems definitely to relate with the code size. The 200 lines original JS code is super fast. The 30k lines first attempt takes 26 ms. 3k to 4k lines take their moderate 6 ms.
Threeshaking like ProGuard would be nice, but I'd like to have it out of the box.
m
This doesn’t help you at all in this case but I found it impressive how much an external tool can reduce the code size for Wasm. I recently experimented with wasm-opt from binaryen due to a bug. In the end the code size went down from 26.6 MB to just 4.0 MB by applying this too after the regular build. Thankfully Jetbrains applies that by default for production builds. Maybe there is something similar for JS too. (https://youtrack.jetbrains.com/issue/KT-78426/RuntimeError-unreachable-executed-after-building-with-wasmJsBrowserDistribution)
s
Yes, that's true. My https://stefan-oltmann.de/oni-seed-browser is a Kotlin/WASM app and benefits from that reduction. To make it helpful here I'd need to have a working sample how to deploy Kotlin/WASM to Cloudflare. The PR doing that is old and doesn't work any longer. So I did not go that route. Also I got the hint that Kotlin/JS might still be faster on Cloudflare Workers than WASM.
Kotlin/JS has browser distribution / webpack included, but in my tests that destroyed the entry point. Because it's not made for this scenario. It doesn't know that this code is important. With ProGuard we have ways to tell it "Hey, keep this, even if it seems unused"
I'm glad about my progress to utilize Cloudflare's free computing offerings so far. I'm not too happy with the code or the performance.
m
Did you search the web for “webpack”+“tree shaking”? I’ve seen a lot of discussions about getting this right.
s
No, I did not investigate further. I tried a bit to make this work with the help of AI, but I figured it's not easy and definitely not out-of-the-box.
m
An example of what I mean: https://cube.dev/blog/how-to-build-tree-shakeable-javascript-libraries Writing tree-shakeable JavaScript code simply does not seem to be an out-of-the-box experience.
s
I hope some expert figures out how to do that for Cloudflare Workers in this specific case and sends a PR.
Cloudflare doesn't care for Kotlin. I don't see any support on their site. It's just me who cares for their free computing tier. 😆
A colleague mentioned that with the amount of time I’ve already invested to make it all run on Cloudflare, I could have just paid AWS $50 and been done. But this way, I get to learn much more.
m
I am often using the same excuse 😉.
😄 1