Compare commits
No commits in common. "40fe234070de6340858d2918cbf4555dc57997d6" and "8175bacfb8d78b75435e87adce22a633fd933e20" have entirely different histories.
40fe234070
...
8175bacfb8
@ -2,7 +2,7 @@
|
||||
|
||||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
||||
|
||||
Current stable version is __0.4.3-SNAPSHOT__. The full-dpulex RPC over websock is ok, the security level is still disabled until the protocol part will be sabilized.
|
||||
Current stable version is __0.4.2-SNAPSHOT__. The full-dpulex RPC over websock is ok, the security level is still disabled until the protocol part will be sabilized.
|
||||
|
||||
This is a connection-agnostic, kotlin multiplaftorm library providing full-duplex RPC type binary protocol, effective to work with binary data, such as encrypted data, keys, multimedia, etc. Default implementation uses websockets transport (binary frames) available on all supported platofrms (currently, JS and JVM).
|
||||
|
||||
@ -46,7 +46,7 @@ repsitories {
|
||||
|
||||
//...
|
||||
dependencies {
|
||||
api("net.sergeych:parsec3:0.4.3-SNAPSHOT")
|
||||
api("net.sergeych:parsec3:0.4.2-SNAPSHOT")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -10,7 +10,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "net.sergeych"
|
||||
version = "0.4.3-SNAPSHOT"
|
||||
version = "0.4.2-SNAPSHOT"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -16,40 +16,15 @@ import kotlin.reflect.typeOf
|
||||
interface KVStorage {
|
||||
operator fun get(key: String): ByteArray?
|
||||
operator fun set(key: String, value: ByteArray?)
|
||||
|
||||
/**
|
||||
* Check whether key is in storage.
|
||||
* Default implementation uses [keys]. You may override it for performance
|
||||
*/
|
||||
operator fun contains(key: String): Boolean = key in keys
|
||||
|
||||
val keys: Set<String>
|
||||
operator fun contains(key: String): Boolean
|
||||
|
||||
|
||||
/**
|
||||
* Get number of object in the storage
|
||||
* Default implementation uses [keys]. You may override it for performance
|
||||
*/
|
||||
val size: Int get() = keys.size
|
||||
val keys: Collection<String>
|
||||
|
||||
/**
|
||||
* Clears all objects in the storage
|
||||
* Default implementation uses [keys]. You may override it for performance
|
||||
*/
|
||||
fun clear() {
|
||||
for (k in keys) this[k] = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation uses [keys]. You may override it for performance
|
||||
*/
|
||||
fun isEmpty() = size == 0
|
||||
|
||||
/**
|
||||
* Default implementation uses [keys]. You may override it for performance
|
||||
*/
|
||||
fun isNotEmpty() = size != 0
|
||||
|
||||
fun addAll(other: KVStorage) {
|
||||
for (k in other.keys) {
|
||||
this[k] = other[k]
|
||||
@ -63,8 +38,6 @@ inline operator fun <reified T> KVStorage.invoke(defaultValue: T,overrideName: S
|
||||
|
||||
inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) =
|
||||
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
|
||||
inline fun <reified T> KVStorage.optStored(defaultValue: T?=null, overrideName: String? = null) =
|
||||
KVStorageDelegate<T?>(this, typeOf<T?>(), defaultValue, overrideName)
|
||||
|
||||
class KVStorageDelegate<T>(
|
||||
private val storage: KVStorage,
|
||||
@ -137,7 +110,7 @@ class MemoryKVStorage(copyFrom: KVStorage? = null) : KVStorage {
|
||||
return key in data
|
||||
}
|
||||
|
||||
override val keys: Set<String>
|
||||
override val keys: Collection<String>
|
||||
get() = underlying?.keys ?: data.keys
|
||||
|
||||
override fun clear() {
|
||||
|
@ -1,39 +0,0 @@
|
||||
package net.sergeych.parsec3
|
||||
|
||||
import net.sergeych.mp_tools.decodeBase64Compact
|
||||
import net.sergeych.mp_tools.encodeToBase64Compact
|
||||
import org.w3c.dom.Storage
|
||||
import org.w3c.dom.set
|
||||
|
||||
/**
|
||||
* Default KV storage in browser. Use if with `localStorage` or `sessionStorage`. Uses
|
||||
* prefix for storage values to not to collide with other data
|
||||
*/
|
||||
class BrowserKVStorage(keyPrefix: String, private val bst: Storage) : KVStorage {
|
||||
|
||||
private val prefix = "$keyPrefix:"
|
||||
fun k(key: String) = "$prefix$key"
|
||||
override fun get(key: String): ByteArray? {
|
||||
return bst.getItem(k(key))?.decodeBase64Compact()
|
||||
}
|
||||
|
||||
override fun set(key: String, value: ByteArray?) {
|
||||
val corrected = k(key)
|
||||
if (value == null)
|
||||
bst.removeItem(corrected)
|
||||
else
|
||||
bst.set(corrected, value.encodeToBase64Compact())
|
||||
}
|
||||
|
||||
override val keys: Set<String>
|
||||
get() {
|
||||
val kk = mutableListOf<String>()
|
||||
for (i in 0 until bst.length) {
|
||||
val k = bst.key(i) ?: break
|
||||
if( k.startsWith(prefix)) {
|
||||
kk += k.substring(prefix.length)
|
||||
}
|
||||
}
|
||||
return kk.toSet()
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package net.sergeych.parsec3
|
||||
|
||||
import kotlinx.browser.localStorage
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
actual fun defaultNamedStorage(name: String): KVStorage = BrowserKVStorage(name, localStorage)
|
||||
actual fun defaultNamedStorage(name: String): KVStorage {
|
||||
TODO("Not yet implemented")
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
import kotlinx.browser.sessionStorage
|
||||
import net.sergeych.parsec3.BrowserKVStorage
|
||||
import net.sergeych.parsec3.optStored
|
||||
import kotlin.test.*
|
||||
|
||||
class testBrowserKVStorage {
|
||||
@Test
|
||||
fun testStorage() {
|
||||
val st = BrowserKVStorage("tpr", sessionStorage)
|
||||
var foo: String? by st.optStored()
|
||||
assertNull(foo)
|
||||
foo = "bar"
|
||||
assertEquals("bar", foo)
|
||||
println(st.keys)
|
||||
assertTrue { "foo" in st.keys }
|
||||
assertFalse { "bar" in st.keys }
|
||||
var bar: Int? by st.optStored()
|
||||
assertNull(bar)
|
||||
bar = 42
|
||||
assertEquals(42, bar)
|
||||
assertTrue { "foo" in st.keys }
|
||||
assertTrue { "bar" in st.keys }
|
||||
assertEquals("bar", foo)
|
||||
|
||||
val st2 = BrowserKVStorage("tpr", sessionStorage)
|
||||
val foo2: String? by st.optStored(overrideName = "foo")
|
||||
assertEquals("bar", foo2)
|
||||
|
||||
assertTrue(st2.isNotEmpty())
|
||||
assertTrue(st.isNotEmpty())
|
||||
|
||||
st.clear()
|
||||
assertTrue(st.isEmpty())
|
||||
|
||||
}
|
||||
}
|
@ -97,7 +97,7 @@ internal class WsServerKtTest {
|
||||
@Test
|
||||
fun testWsServerReconnect() {
|
||||
|
||||
embeddedServer(Netty, port = 8090) {
|
||||
embeddedServer(Netty, port = 8080) {
|
||||
parsec3TransportServer(
|
||||
TestApiServer,
|
||||
) {
|
||||
@ -112,7 +112,7 @@ internal class WsServerKtTest {
|
||||
}.start(wait = false)
|
||||
|
||||
Log.connectConsole(Log.Level.DEBUG)
|
||||
val client = Parsec3WSClient("ws://localhost:8090/api/p3", TestApiClient) {
|
||||
val client = Parsec3WSClient("ws://localhost:8080/api/p3", TestApiClient) {
|
||||
on(api.bar) {
|
||||
"bar:$it"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user