-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e99c5c5
commit 8f4cb59
Showing
14 changed files
with
312 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@file:Suppress("FunctionName") | ||
|
||
package dev.kord.gateway | ||
|
||
import dev.kord.common.annotation.KordInternal | ||
import io.ktor.websocket.* | ||
|
||
/** @suppress */ | ||
@KordInternal // Only public for interface, binary API might change at any time | ||
public interface Decompressor : AutoCloseable { | ||
public fun Frame.decompress(): String | ||
|
||
public companion object Noop : Decompressor { | ||
override fun Frame.decompress(): String = data.decodeToString() | ||
override fun close() {} | ||
} | ||
} | ||
|
||
internal expect fun ZLibDecompressor(): Decompressor | ||
internal expect fun ZstdDecompressor(): Decompressor | ||
|
||
/** | ||
* Different compression modes for the Discord gateway. | ||
* | ||
* @property name the name used by the Discord API | ||
*/ | ||
public sealed interface Compression { | ||
public val name: String? | ||
public fun newDecompressor(): Decompressor | ||
|
||
/** | ||
* Implementation using no compression. | ||
*/ | ||
public data object None : Compression { | ||
override val name: String? = null | ||
override fun newDecompressor(): Decompressor = Decompressor.Noop | ||
} | ||
|
||
/** | ||
* Implementation using [zlib](https://zlib.net/). | ||
*/ | ||
public data object ZLib : Compression { | ||
override val name: String = "zlib-stream" | ||
override fun newDecompressor(): Decompressor = ZLibDecompressor() | ||
} | ||
|
||
/** | ||
* Implementation using [Zstandard](https://facebook.github.io/zstd/) | ||
*/ | ||
public data object Zstd : Compression { | ||
override val name: String = "zstd-stream" | ||
override fun newDecompressor(): Decompressor = ZstdDecompressor() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dev.kord.gateway | ||
|
||
import dev.kord.gateway.internal.ZSTDDecompress | ||
import io.ktor.websocket.* | ||
import js.typedarrays.toUint8Array | ||
import node.stream.DuplexEvent | ||
import web.encoding.TextDecoder | ||
|
||
internal actual fun ZstdDecompressor() = object : Decompressor { | ||
private val stream = ZSTDDecompress() | ||
private val decoder = TextDecoder() | ||
|
||
override fun Frame.decompress(): String { | ||
try { | ||
stream.write(data.toUint8Array()) | ||
stream.on(DuplexEvent.FINISH) { | ||
println("finish") | ||
} | ||
stream.on(DuplexEvent.DATA) { | ||
println("Data: $it") | ||
} | ||
} catch (exception: Exception) { | ||
exception.printStackTrace() | ||
} | ||
return "" | ||
} | ||
|
||
override fun close() = stream.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@file:JsModule("simple-zstd") | ||
|
||
package dev.kord.gateway.internal | ||
|
||
import node.stream.Transform | ||
|
||
internal external class ZSTDDecompress : Transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.kord.gateway | ||
|
||
import com.github.luben.zstd.ZstdInputStream | ||
import io.ktor.websocket.* | ||
import java.io.ByteArrayInputStream | ||
import java.io.InputStream | ||
|
||
internal actual fun ZstdDecompressor() = object : Decompressor { | ||
|
||
private val input = UpdatableByteArrayInputStream() | ||
private val zstdStream = ZstdInputStream(input).apply { continuous = true } | ||
|
||
override fun Frame.decompress(): String { | ||
input.updateDelegate(data) | ||
return zstdStream.readAllBytes().decodeToString() | ||
} | ||
|
||
override fun close() { | ||
zstdStream.close() | ||
} | ||
} | ||
|
||
private class UpdatableByteArrayInputStream : InputStream() { | ||
private var delegate: ByteArrayInputStream? = null | ||
|
||
private val d: InputStream get() = delegate ?: error("No data available") | ||
|
||
override fun read(): Int = d.read() | ||
|
||
fun updateDelegate(bytes: ByteArray) { | ||
delegate = ByteArrayInputStream(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.