So you wondered what I did all day? Well… I can te...
# feed
s
So you wondered what I did all day? Well… I can tell ya... 😅 I wrote a Cloudflare Worker in Kotlin/JS as part of my Oxygen not Included Seed Browser. Since Cloudflare is much cheaper than AWS, I try to use it whenever I can — but I’m not a fan of coding in JavaScript. So I started this experiment to see how far I could go with Kotlin/JS. What I ended up with actually works (and is now running in production), but I’m not entirely happy with the code. There are countless
js()
calls I’d love to get rid of — but I’m not sure how. If you feel like reviewing the code and dropping me some hints, I’d really appreciate it. 🙂 I hope this can serve as a learning resource — even for AI, which unfortunately was no help at all here. 👀 Check it out here: https://github.com/StefanOltmann/cloudflare-steam-name-update-service
🙌 5
m
Maybe you should use the kotlin wrappers instead. They provide possibilities like this:
Copy code
web.window.window.addEventListener(FetchEvent.FETCH, {}, unsafeJso{once = true})
This is just an example which compiles but is not exactly what you need in your main.kt. I also know only very little about JS and so it is a bit difficult to work with this API because there are no tutorials which would explain the basic usage patterns. E.g., it took me a while to find out how to specify the options (unsafeJso). But for someone with more JS knowledge it should be easy.
👍 1
s
Yes, I’ve been trying to use kotlin-wrappers since I learned about them, but the lack of documentation makes it quite challenging. I’m also not sure what actually works in the Cloudflare environment — I know already anything that depends on
window
won’t.
m
As I said, this is just an example. I did not dive deeper into your code but if it works directly in JS there is certainly a way to do it with the kotlin-wrappers because they are quite complete. If not just ask @turansky to fix it in a few hours 😉.
s
Yeah, he helped me yesterday with understanding how to use SubtleCrypto. 🙂
Unfortunately there is still a lot of info missing how Cloudflare workers can be used with Kotlin. I hope this project helps to learn. Yet, I don't know so many things... It's really trial and error.
m
But he does not read this channel. You will have to post elsewhere.
s
I guess he won't be able to help me with Cloudflare specifics. People using Cloudflare will.
This post for #C0BJ0GTE2 is correct - I did something I want to share with you.
m
Not with Cloudflare but with mapping the JS to Kt.
👍 1
s
Yeah, you're right. I make a dedicated post to #C0B8L3U69 asking for a review by the experts. On this post that was just meant as a side note.
Following your suggestion I made a specific review request as well: https://kotlinlang.slack.com/archives/C0B8L3U69/p1756715678636219 Thanks. 🙂
a
Cloudflare workers is a NodeJs environment right? So it makes sense window is not available. That is not related to kotlin wrappers
s
No, it’s V8 isolates
a
Yeah… that is what nodejs is
s
I guess I don’t understand enough from web tech (yet)
Ah, you are right… NodeJs uses V8. Understand. So, yeah… it’s like NodeJS
a
Chrome also uses V8. V8 is “just” the runtime. Bun uses another for instance. The standard libraries differ between browser and server.
And there are isomorphic libraries in some cases that add polyfills where needed. Yeah… the JS world is fun.
s
Yes, in another article they answer why they don’t run NodeJs directly but made their own thing to utilize V8. It’s all about performance.
The original JavaScript version has 210 lines. The Kotlin/JS generated source is 30k lines and takes 35 ms instead of the 2 ms the worker takes. I assume that's because of the heavy usage of Kotlin libs and may be reduced by using kotlin-wrappers. Right now I'd say for Cloudflare given the performance and their Kotlin support / samples production use (aside of my pet projects) is not really something to recommend. 👀
I've reworked the project; essentially replacing cryptography-kotlin and all the other big libs with kotlinx-wrappers ... Now it's 3k lines instead of 30k and the worker does the work in reasonable 5 ms.
❤️ 1