Skip to content

Commit

Permalink
Use String instead of Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Apr 8, 2024
1 parent 22497f8 commit 8ad0720
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import io.matthewnelson.kmp.tor.runtime.ctrl.internal.RealTorCtrl
import io.matthewnelson.kmp.tor.runtime.ctrl.internal.checkUnixSockedSupport
import io.matthewnelson.kmp.tor.runtime.ctrl.internal.net_createConnection
import kotlinx.coroutines.*
import org.khronos.webgl.Uint8Array
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeSource

Expand Down Expand Up @@ -165,18 +164,18 @@ public actual interface TorCtrl : Destroyable, TorEvent.Processor, TorCmd.Privil

val rBuf = ReadBuffer.allocate()

fun callback(nread: Int, buf: dynamic) {
val readBuf = try {
val jsBuf = Buffer.wrap(buf)
ReadBuffer.of(jsBuf)
} catch (_: Throwable) {
rBuf
}
run {
fun callback(nread: Int, buf: dynamic) {
val readBuf = try {
val jsBuf = Buffer.wrap(buf)
ReadBuffer.of(jsBuf)
} catch (_: Throwable) {
rBuf
}

feed.onData(readBuf, nread)
}
feed.onData(readBuf, nread)
}

run {
val onReadOptions = js("{}")
onReadOptions["buffer"] = rBuf.buf.unwrap()
onReadOptions["callback"] = ::callback
Expand Down Expand Up @@ -247,21 +246,12 @@ public actual interface TorCtrl : Destroyable, TorEvent.Processor, TorCmd.Privil
override suspend fun write(command: ByteArray) {
if (command.isEmpty()) return

val chunk = Uint8Array(command.size)
@Suppress("LocalVariableName")
val _chunk = chunk.asDynamic()
for (i in command.indices) { _chunk[i] = command[i] }

val wLatch = Job()
var dLatch: Job? = null

try {
val immediate = socket.write(chunk, callback = {
wLatch.cancel()

// fill
for (i in command.indices) { _chunk[i] = 0 }
})
// Must utilize string because windows
val immediate = socket.write(command.decodeToString()) { wLatch.cancel() }

if (!immediate) {
dLatch = Job()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal external class net_Socket: events_EventEmitter {

// @Throws(Throwable::class)
internal fun write(
chunk: Uint8Array,
data: String,
callback: () -> Unit,
): Boolean

Expand Down

0 comments on commit 8ad0720

Please sign in to comment.