Skip to content

Commit

Permalink
Reduce number of npm dependencies and simplify webpack configuration (
Browse files Browse the repository at this point in the history
  • Loading branch information
twyatt committed Dec 22, 2021
1 parent 00ab8c8 commit d4a8943
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 17 deletions.
6 changes: 0 additions & 6 deletions koap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 0 additions & 4 deletions koap/webpack.config.d/webpack.js

This file was deleted.

9 changes: 6 additions & 3 deletions webapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/main/kotlin/webapp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -68,7 +67,7 @@ fun decode(hex: String?): Promise<String> = GlobalScope.promise {
}
}

private suspend inline fun <reified T : Message> decode(bytes: ByteArray): String = try {
internal suspend inline fun <reified T : Message> decode(bytes: ByteArray): String = try {
parse(bytes.decode<T>())
} catch (t: Throwable) {
console.error(t)
Expand Down
114 changes: 114 additions & 0 deletions webapp/src/test/kotlin/DecodingTest.kt
Original file line number Diff line number Diff line change
@@ -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 = """
<b>Message:</b>
{
"type": "Confirmable",
"code": "GET",
"id": 65261,
"token": 51966,
"options": [
"UriPath(uri=example)",
"Content-Format: application/json"
]
}
<b>Payload (JSON):</b>
{
"example": 123
}
""".trimIndent(),
actual = decode<Message.Udp>(input).trim(),
)

assertEquals(
expected = "Unsupported number length of 10 bytes",
actual = decode<Message.Tcp>(input),
)
}

@Test
fun coapWithCborPayload() = GlobalScope.promise {
/* Message.Udp(
* type = Confirmable,
* code = GET,
* id = 0xFEED,
* token = 0xCAFE,
* options = listOf(
* UriPath("example"),
* ContentFormat.CBOR,
* ),
* payload = <CBOR>,
* )
*
* 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<Message.Tcp>(input),
)

assertEquals(
expected = """
<b>Message:</b>
{
"type": "Confirmable",
"code": "GET",
"id": 65261,
"token": 51966,
"options": [
"UriPath(uri=example)",
"Content-Format: application/cbor"
]
}
<b>Payload (CBOR):</b>
{_ "a": "123", "b": "987"}
""".trimIndent(),
actual = decode<Message.Udp>(input).trim(),
)
}
}
2 changes: 0 additions & 2 deletions webapp/webpack.config.d/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/"),
}

0 comments on commit d4a8943

Please sign in to comment.