diff --git a/koap/build.gradle.kts b/koap/build.gradle.kts index 2461c854..b6631007 100644 --- a/koap/build.gradle.kts +++ b/koap/build.gradle.kts @@ -32,12 +32,6 @@ kotlin { } } - val jsMain by getting { - dependencies { - implementation(npm("os-browserify", "0.3.0")) - } - } - val jvmTest by getting { dependencies { implementation(kotlin("test-junit")) diff --git a/koap/webpack.config.d/webpack.js b/koap/webpack.config.d/webpack.js deleted file mode 100644 index 4ddfcfc5..00000000 --- a/koap/webpack.config.d/webpack.js +++ /dev/null @@ -1,4 +0,0 @@ -config.resolve.fallback = { - "path": require.resolve("path-browserify"), - "os": require.resolve("os-browserify/browser"), -} diff --git a/webapp/build.gradle.kts b/webapp/build.gradle.kts index c3584f82..1e89b172 100644 --- a/webapp/build.gradle.kts +++ b/webapp/build.gradle.kts @@ -18,11 +18,14 @@ kotlin { implementation(libs.okio.js) implementation(npm("buffer", "6.0.3")) implementation(npm("cbor", "8.1.0")) - implementation(npm("os-browserify", "0.3.0")) - implementation(npm("path-browserify", "1.0.1")) implementation(npm("process", "0.11.10")) implementation(npm("stream-browserify", "3.0.0")) - implementation(npm("util", "0.12.4")) + } + } + + val test by getting { + dependencies { + implementation(kotlin("test-js")) } } } diff --git a/webapp/src/main/kotlin/webapp.kt b/webapp/src/main/kotlin/webapp.kt index 5147edb2..6d6e10ce 100644 --- a/webapp/src/main/kotlin/webapp.kt +++ b/webapp/src/main/kotlin/webapp.kt @@ -1,6 +1,5 @@ package com.juul.koap -import cbor.decodeFirstSync import cbor.diagnose import com.juul.koap.Message.Option.Accept import com.juul.koap.Message.Option.ContentFormat @@ -68,7 +67,7 @@ fun decode(hex: String?): Promise = GlobalScope.promise { } } -private suspend inline fun decode(bytes: ByteArray): String = try { +internal suspend inline fun decode(bytes: ByteArray): String = try { parse(bytes.decode()) } catch (t: Throwable) { console.error(t) diff --git a/webapp/src/test/kotlin/DecodingTest.kt b/webapp/src/test/kotlin/DecodingTest.kt new file mode 100644 index 00000000..7bc5a700 --- /dev/null +++ b/webapp/src/test/kotlin/DecodingTest.kt @@ -0,0 +1,114 @@ +package com.juul.koap + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.promise +import okio.ByteString.Companion.decodeHex +import kotlin.test.Test +import kotlin.test.assertEquals + +class DecodingTest { + + @Test + fun coapWithJsonPayload() = GlobalScope.promise { + /* Message.Udp( + * type = Confirmable, + * code = GET, + * id = 0xFEED, + * token = 0xCAFE, + * options = listOf( + * UriPath("example"), + * ContentFormat.JSON, + * ), + * payload = {"example": 123} + * ) + */ + val input = "42 01 FE ED CA FE B7 65 78 61 6D 70 6C 65 11 32 FF 7B 22 65 78 61 6D 70 6C 65 22 3A 20 31 32 33 7D" + .replace(" ", "") + .decodeHex() + .toByteArray() + + assertEquals( + expected = """ + Message: + { + "type": "Confirmable", + "code": "GET", + "id": 65261, + "token": 51966, + "options": [ + "UriPath(uri=example)", + "Content-Format: application/json" + ] + } + + Payload (JSON): + { + "example": 123 + } + """.trimIndent(), + actual = decode(input).trim(), + ) + + assertEquals( + expected = "Unsupported number length of 10 bytes", + actual = decode(input), + ) + } + + @Test + fun coapWithCborPayload() = GlobalScope.promise { + /* Message.Udp( + * type = Confirmable, + * code = GET, + * id = 0xFEED, + * token = 0xCAFE, + * options = listOf( + * UriPath("example"), + * ContentFormat.CBOR, + * ), + * payload = , + * ) + * + * CBOR payload: + * BF # map(*) + * 61 # text(1) + * 61 # "a" + * 63 # text(3) + * 313233 # "123" + * 61 # text(1) + * 62 # "b" + * 63 # text(3) + * 393837 # "987" + * FF # primitive(*) + */ + val input = "42 01 FE ED CA FE B7 65 78 61 6D 70 6C 65 11 3C FF BF 61 61 63 31 32 33 61 62 63 39 38 37 FF" + .replace(" ", "") + .decodeHex() + .toByteArray() + + assertEquals( + expected = "Unsupported number length of 10 bytes", + actual = decode(input), + ) + + assertEquals( + expected = """ + Message: + { + "type": "Confirmable", + "code": "GET", + "id": 65261, + "token": 51966, + "options": [ + "UriPath(uri=example)", + "Content-Format: application/cbor" + ] + } + + Payload (CBOR): + {_ "a": "123", "b": "987"} + """.trimIndent(), + actual = decode(input).trim(), + ) + } +} diff --git a/webapp/webpack.config.d/webpack.js b/webapp/webpack.config.d/webpack.js index f22aff6b..16f8c1eb 100644 --- a/webapp/webpack.config.d/webpack.js +++ b/webapp/webpack.config.d/webpack.js @@ -8,8 +8,6 @@ config.plugins = [ config.resolve.fallback = { "buffer": require.resolve("buffer/"), - "os": require.resolve("os-browserify/browser"), "process": require.resolve("process/browser"), "stream": require.resolve("stream-browserify"), - "util": require.resolve("util/"), }