-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-6004 Support TCP and Unix socket for wasm-js and js via NodeJS (#…
…4411) * Drop `jvmAndNix` shared source set * Commonize `ktor-network` and `ktor-network-tls` * Support TCP and Unix sockets for wasm-js and js on Node * Move `supportsUnixDomainSockets` to posix and use Platform instead of expect/actual
- Loading branch information
Showing
63 changed files
with
785 additions
and
239 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
File renamed without changes.
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
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...x/src/io/ktor/network/sockets/Datagram.kt → ...n/src/io/ktor/network/sockets/Datagram.kt
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
2 changes: 1 addition & 1 deletion
2
.../io/ktor/network/sockets/SocketAddress.kt → .../io/ktor/network/sockets/SocketAddress.kt
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
32 changes: 32 additions & 0 deletions
32
ktor-network/common/src/io/ktor/network/sockets/SocketEngine.kt
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,32 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.network.selector.* | ||
|
||
internal expect suspend fun tcpConnect( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
socketOptions: SocketOptions.TCPClientSocketOptions | ||
): Socket | ||
|
||
internal expect suspend fun tcpBind( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
socketOptions: SocketOptions.AcceptorOptions | ||
): ServerSocket | ||
|
||
internal expect suspend fun udpConnect( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): ConnectedDatagramSocket | ||
|
||
internal expect suspend fun udpBind( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): BoundDatagramSocket |
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
4 changes: 2 additions & 2 deletions
4
...ix/src/io/ktor/network/sockets/Sockets.kt → ...on/src/io/ktor/network/sockets/Sockets.kt
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
4 changes: 2 additions & 2 deletions
4
.../io/ktor/network/sockets/TypeOfService.kt → .../io/ktor/network/sockets/TypeOfService.kt
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
37 changes: 37 additions & 0 deletions
37
ktor-network/common/test/io/ktor/network/sockets/tests/TestUtils.kt
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,37 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets.tests | ||
|
||
import io.ktor.network.selector.* | ||
import io.ktor.test.dispatcher.* | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.test.TestResult | ||
import kotlinx.io.files.Path | ||
import kotlinx.io.files.SystemFileSystem | ||
import kotlinx.io.files.SystemTemporaryDirectory | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.uuid.ExperimentalUuidApi | ||
import kotlin.uuid.Uuid | ||
|
||
internal fun testSockets( | ||
timeout: Duration = 1.minutes, | ||
block: suspend CoroutineScope.(SelectorManager) -> Unit | ||
): TestResult = runTestWithRealTime(timeout = timeout) { | ||
SelectorManager().use { selector -> | ||
block(selector) | ||
} | ||
} | ||
|
||
internal expect fun Any.supportsUnixDomainSockets(): Boolean | ||
|
||
@OptIn(ExperimentalUuidApi::class) | ||
internal fun createTempFilePath(basename: String): String { | ||
return Path(SystemTemporaryDirectory, "$basename-${Uuid.random()}").toString() | ||
} | ||
|
||
internal fun removeFile(path: String) { | ||
SystemFileSystem.delete(Path(path), mustExist = false) | ||
} |
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,5 @@ | ||
# | ||
# Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
# | ||
target.js.browser=false | ||
target.wasmJs.browser=false |
7 changes: 0 additions & 7 deletions
7
ktor-network/ios/test/io/ktor/network/sockets/tests/TestUtilsIos.kt
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
ktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.kt
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,45 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets.nodejs | ||
|
||
import io.ktor.network.sockets.* | ||
import org.khronos.webgl.* | ||
|
||
internal actual fun nodeNet(): NodeNet? = js("eval('require')('node:net')").unsafeCast<NodeNet?>() | ||
|
||
internal actual fun TcpCreateConnectionOptions( | ||
block: TcpCreateConnectionOptions.() -> Unit | ||
): TcpCreateConnectionOptions = createObject(block) | ||
|
||
internal actual fun IpcCreateConnectionOptions( | ||
block: IpcCreateConnectionOptions.() -> Unit | ||
): IpcCreateConnectionOptions = createObject(block) | ||
|
||
internal actual fun CreateServerOptions( | ||
block: CreateServerOptions.() -> Unit | ||
): CreateServerOptions = createObject(block) | ||
|
||
internal actual fun ServerListenOptions( | ||
block: ServerListenOptions.() -> Unit | ||
): ServerListenOptions = createObject(block) | ||
|
||
private fun <T> createObject(block: T.() -> Unit): T = js("{}").unsafeCast<T>().apply(block) | ||
|
||
internal actual fun JsError.toThrowable(): Throwable = unsafeCast<Throwable>() | ||
internal actual fun Throwable.toJsError(): JsError? = unsafeCast<JsError>() | ||
|
||
internal actual fun ByteArray.toJsBuffer(fromIndex: Int, toIndex: Int): JsBuffer { | ||
return unsafeCast<Int8Array>().subarray(fromIndex, toIndex).unsafeCast<JsBuffer>() | ||
} | ||
|
||
internal actual fun JsBuffer.toByteArray(): ByteArray { | ||
return Int8Array(unsafeCast<ArrayBuffer>()).unsafeCast<ByteArray>() | ||
} | ||
|
||
internal actual fun ServerLocalAddressInfo.toSocketAddress(): SocketAddress { | ||
if (jsTypeOf(this) == "string") return UnixSocketAddress(unsafeCast<String>()) | ||
val info = unsafeCast<TcpServerLocalAddressInfo>() | ||
return InetSocketAddress(info.address, info.port) | ||
} |
7 changes: 7 additions & 0 deletions
7
ktor-network/jsAndWasmShared/src/io/ktor/network/selector/Selectable.jsAndWasmShared.kt
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 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.selector | ||
|
||
public actual interface Selectable |
Oops, something went wrong.