Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Further documentation for Wrangler can be found [here](https://developers.cloudf
After setting up Kotlin per the linked instructions above,

```
./gradlew :compileProductionExecutableKotlinJs
./gradlew :compileProductionExecutableKotlinWasmJs
```

That will compile your code and package it into a JavaScript executable, after which you can run `wrangler publish` to push it to Cloudflare.

```
wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js
wrangler publish build/js/packages/kotlin-worker-hello-world-wasm-js/kotlin/kotlin-worker-hello-world-wasm-js.mjs
```

For more information on interop between Kotlin and Javascript, see the [Kotlin docs](https://kotlinlang.org/docs/reference/js-interop.html). Regarding coroutines, see [this issue and workaround](https://github.com/cloudflare/kotlin-worker-hello-world/issues/2)
19 changes: 17 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask

plugins {
kotlin("js") version "1.7.10"
kotlin("multiplatform") version "1.9.22"
}

group = "org.example"
Expand All @@ -10,9 +13,21 @@ repositories {
}

kotlin {
js(IR) {
wasmJs {
nodejs {
}
binaries.executable()

// Uncomment the next line to apply Binaryen and get optimized wasm binaries
// applyBinaryen()
}
}

// To run in Node.js
rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "22.0.0-nightly2024021536dcd399c0"
nodeDownloadBaseUrl = "https://nodejs.org/download/nightly"
}
tasks.withType<KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}
7 changes: 4 additions & 3 deletions src/main/kotlin/main.kt → src/wasmJsMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import org.w3c.fetch.Headers
import org.w3c.fetch.Request
import org.w3c.fetch.Response
import org.w3c.fetch.ResponseInit

@OptIn(ExperimentalJsExport::class)
@JsExport
fun fetch(request: Request) : Response {
val headers: dynamic = object {}
headers["content-type"] = "text/plain"
val headers = Headers()
headers.append("content-type", "text/plain")
return Response(
"Kotlin Worker hello world",
"Kotlin Worker hello world".toJsString(),
ResponseInit(headers = headers)
)
}
File renamed without changes.